Skip to content

Truly independent web browser

LibWeb: Don’t block scripts on style sheets whose media doesn’t match

sideshowbarker6d4151efca90
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
index 0468bf052a6..22790693c62 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
@@ -384,7 +384,9 @@ bool HTMLLinkElement::contributes_a_script_blocking_style_sheet() const
// FIXME: el is either a style element or a link element that was an external resource link that contributes to the styling processing model when the el was created by the parser.
- // FIXME: el's media attribute's value matches the environment.
+ // el's media attribute's value matches the environment.
+ if (!CSS::RustMediaList::parse(attribute(AttributeNames::media).value_or({})).evaluate(document()))
+ return false;
// el's style sheet was enabled when the element was created by the parser.
if (!m_was_enabled_when_created_by_parser)
diff --git a/Tests/LibWeb/Text/expected/HTML/link-stylesheet-media-not-matching-does-not-block-scripts.txt b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-media-not-matching-does-not-block-scripts.txt
new file mode 100644
index 00000000000..97f2c98c0b7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-media-not-matching-does-not-block-scripts.txt
@@ -0,0 +1,4 @@
+The print style sheet loaded before the script after it ran: false
+The screen style sheet loaded before the script after it ran: true
+media: all
+color: rgb(0, 128, 0), font-weight: 700
diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-media-not-matching-does-not-block-scripts.html b/Tests/LibWeb/Text/input/HTML/link-stylesheet-media-not-matching-does-not-block-scripts.html
new file mode 100644
index 00000000000..1a4882700a8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-media-not-matching-does-not-block-scripts.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../include.js"></script>
+<script>
+ const loaded = new Set();
+ document.addEventListener("load", event => {
+ if (event.target.localName === "link")
+ loaded.add(event.target.media);
+ }, true);
+</script>
+<link id="printSheet" rel="stylesheet" href="data:text/css,p%7Bcolor:green%7D" media="print">
+<script>
+ // A style sheet whose media doesn't match the environment doesn't block this script, so the script can still
+ // listen for its load event, and apply it then.
+ const printSheetLoadedBeforeItsScript = loaded.has("print");
+ printSheet.addEventListener("load", () => (printSheet.media = "all"));
+</script>
+<link rel="stylesheet" href="data:text/css,p%7Bfont-weight:bold%7D" media="screen">
+<script>
+ // A style sheet whose media matches the environment does block this script.
+ const screenSheetLoadedBeforeItsScript = loaded.has("screen");
+</script>
+</head>
+<body>
+<p id="text">text</p>
+<script>
+ asyncTest(done => {
+ window.addEventListener("load", () => {
+ println(`The print style sheet loaded before the script after it ran: ${printSheetLoadedBeforeItsScript}`);
+ println(`The screen style sheet loaded before the script after it ran: ${screenSheetLoadedBeforeItsScript}`);
+ println(`media: ${printSheet.media}`);
+ const style = getComputedStyle(document.getElementById("text"));
+ println(`color: ${style.color}, font-weight: ${style.fontWeight}`);
+ done();
+ });
+ });
+</script>
+</body>
+</html>