Procházet zdrojové kódy

LibWeb: Forward media attribute from link element to loaded style sheet

Otherwise, we will treat the loaded style sheet as if it had media="all"
which is not always appropriate.
Andreas Kling před 2 roky
rodič
revize
8a43f5a64a

+ 1 - 0
Tests/LibWeb/Text/expected/link-element-media-attribute.txt

@@ -0,0 +1 @@
+document background: rgb(0, 128, 0)

+ 3 - 0
Tests/LibWeb/Text/input/body-background-color-red.css

@@ -0,0 +1,3 @@
+body {
+    background-color: red;
+}

+ 17 - 0
Tests/LibWeb/Text/input/link-element-media-attribute.html

@@ -0,0 +1,17 @@
+<style>
+body {
+    background: green;
+}
+</style>
+<script src="include.js"></script>
+<script>
+    let link = document.createElement("link");
+    link.setAttribute("rel", "stylesheet");
+    link.setAttribute("href", "body-background-color-red.css");
+    link.setAttribute("media", "print")
+    document.head.appendChild(link)
+
+    window.onload = function() {
+        println("document background: " + getComputedStyle(document.body).backgroundColor)
+    }
+</script>

+ 1 - 0
Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp

@@ -364,6 +364,7 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
 
         if (m_loaded_style_sheet) {
             m_loaded_style_sheet->set_owner_node(this);
+            m_loaded_style_sheet->set_media(attribute(HTML::AttributeNames::media));
             document().style_sheets().add_sheet(*m_loaded_style_sheet);
         } else {
             dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url());