From e43baed585a4a2b1ffa74df9da7f126cf4309e2a Mon Sep 17 00:00:00 2001 From: webbeef Date: Mon, 20 Jan 2025 18:21:16 -0800 Subject: [PATCH] Add support for HTMLStyleElement.type (#35038) Signed-off-by: webbeef --- components/script/dom/htmlstyleelement.rs | 54 ++++++++- .../dom/webidls/HTMLStyleElement.webidl | 5 +- .../meta/html/dom/idlharness.https.html.ini | 6 - .../html/dom/reflection-metadata.html.ini | 114 ------------------ .../style_type_change.html.ini | 9 -- .../style_type_html.html.ini | 6 - ...tyle-block-ascii-case-insensitive.html.ini | 2 - 7 files changed, 51 insertions(+), 145 deletions(-) delete mode 100644 tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_change.html.ini delete mode 100644 tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_html.html.ini delete mode 100644 tests/wpt/meta/html/semantics/document-metadata/the-style-element/update-style-block-ascii-case-insensitive.html.ini diff --git a/components/script/dom/htmlstyleelement.rs b/components/script/dom/htmlstyleelement.rs index 5160918640c..ad96a14d836 100644 --- a/components/script/dom/htmlstyleelement.rs +++ b/components/script/dom/htmlstyleelement.rs @@ -15,14 +15,16 @@ use style::parser::ParserContext as CssParserContext; use style::stylesheets::{AllowImportRules, CssRuleType, Origin, Stylesheet, UrlExtraData}; use style_traits::ParsingMode; +use crate::dom::attr::Attr; use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::HTMLStyleElementBinding::HTMLStyleElementMethods; use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::root::{DomRoot, MutNullableDom}; +use crate::dom::bindings::str::DOMString; use crate::dom::cssstylesheet::CSSStyleSheet; use crate::dom::document::Document; -use crate::dom::element::{Element, ElementCreator}; +use crate::dom::element::{AttributeMutation, Element, ElementCreator}; use crate::dom::htmlelement::HTMLElement; use crate::dom::node::{BindContext, ChildrenMutation, Node, NodeTraits, UnbindContext}; use crate::dom::stylesheet::StyleSheet as DOMStyleSheet; @@ -88,6 +90,13 @@ impl HTMLStyleElement { let element = self.upcast::(); assert!(node.is_connected()); + // Step 4. of + let mut type_attribute = self.Type(); + type_attribute.make_ascii_lowercase(); + if !type_attribute.is_empty() && type_attribute != "text/css" { + return; + } + let window = node.owner_window(); let doc = self.owner_document(); @@ -179,6 +188,14 @@ impl HTMLStyleElement { } self.cssom_stylesheet.set(None); } + + fn remove_stylesheet(&self) { + if let Some(s) = self.stylesheet.borrow_mut().take() { + self.clean_stylesheet_ownership(); + self.stylesheet_list_owner() + .remove_stylesheet(self.upcast(), &s) + } + } } impl VirtualMethods for HTMLStyleElement { @@ -194,7 +211,10 @@ impl VirtualMethods for HTMLStyleElement { // "The element is not on the stack of open elements of an HTML parser or XML parser, // and one of its child nodes is modified by a script." // TODO: Handle Text child contents being mutated. - if self.upcast::().is_in_a_document_tree() && !self.in_stack_of_open_elements.get() { + let node = self.upcast::(); + if (node.is_in_a_document_tree() || node.is_in_a_shadow_tree()) && + !self.in_stack_of_open_elements.get() + { self.parse_own_css(); } } @@ -229,11 +249,27 @@ impl VirtualMethods for HTMLStyleElement { } if context.tree_connected { - if let Some(s) = self.stylesheet.borrow_mut().take() { - self.clean_stylesheet_ownership(); - self.stylesheet_list_owner() - .remove_stylesheet(self.upcast(), &s) + self.remove_stylesheet(); + } + } + + fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) { + if let Some(s) = self.super_type() { + s.attribute_mutated(attr, mutation); + } + + let node = self.upcast::(); + if attr.name() == "type" && + (node.is_in_a_document_tree() || node.is_in_a_shadow_tree()) && + !self.in_stack_of_open_elements.get() + { + if let AttributeMutation::Set(Some(old_value)) = mutation { + if **old_value == **attr.value() { + return; + } } + self.remove_stylesheet(); + self.parse_own_css(); } } } @@ -292,4 +328,10 @@ impl HTMLStyleElementMethods for HTMLStyleElement { sheet.set_disabled(value); } } + + // + make_getter!(Type, "type"); + + // + make_setter!(SetType, "type"); } diff --git a/components/script/dom/webidls/HTMLStyleElement.webidl b/components/script/dom/webidls/HTMLStyleElement.webidl index 0bcb86af24b..a5ed437de19 100644 --- a/components/script/dom/webidls/HTMLStyleElement.webidl +++ b/components/script/dom/webidls/HTMLStyleElement.webidl @@ -10,8 +10,9 @@ interface HTMLStyleElement : HTMLElement { attribute boolean disabled; // [CEReactions] // attribute DOMString media; - // [CEReactions] - // attribute DOMString type; + + [CEReactions] attribute DOMString type; + // [CEReactions] // attribute boolean scoped; }; diff --git a/tests/wpt/meta/html/dom/idlharness.https.html.ini b/tests/wpt/meta/html/dom/idlharness.https.html.ini index fa346c7f862..7cc0cc110f4 100644 --- a/tests/wpt/meta/html/dom/idlharness.https.html.ini +++ b/tests/wpt/meta/html/dom/idlharness.https.html.ini @@ -6208,18 +6208,12 @@ [HTMLStyleElement interface: attribute blocking] expected: FAIL - [HTMLStyleElement interface: attribute type] - expected: FAIL - [HTMLStyleElement interface: document.createElement("style") must inherit property "media" with the proper type] expected: FAIL [HTMLStyleElement interface: document.createElement("style") must inherit property "blocking" with the proper type] expected: FAIL - [HTMLStyleElement interface: document.createElement("style") must inherit property "type" with the proper type] - expected: FAIL - [HTMLBodyElement interface: attribute link] expected: FAIL diff --git a/tests/wpt/meta/html/dom/reflection-metadata.html.ini b/tests/wpt/meta/html/dom/reflection-metadata.html.ini index 0c49620b2dd..db0682f9c87 100644 --- a/tests/wpt/meta/html/dom/reflection-metadata.html.ini +++ b/tests/wpt/meta/html/dom/reflection-metadata.html.ini @@ -2477,120 +2477,6 @@ [style.nonce: IDL set to object "test-valueOf"] expected: FAIL - [style.type: typeof IDL attribute] - expected: FAIL - - [style.type: IDL get with DOM attribute unset] - expected: FAIL - - [style.type: setAttribute() to ""] - expected: FAIL - - [style.type: setAttribute() to " \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07 \\b\\t\\n\\v\\f\\r\\x0e\\x0f \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17 \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f foo "] - expected: FAIL - - [style.type: setAttribute() to undefined] - expected: FAIL - - [style.type: setAttribute() to 7] - expected: FAIL - - [style.type: setAttribute() to 1.5] - expected: FAIL - - [style.type: setAttribute() to "5%"] - expected: FAIL - - [style.type: setAttribute() to "+100"] - expected: FAIL - - [style.type: setAttribute() to ".5"] - expected: FAIL - - [style.type: setAttribute() to true] - expected: FAIL - - [style.type: setAttribute() to false] - expected: FAIL - - [style.type: setAttribute() to object "[object Object\]"] - expected: FAIL - - [style.type: setAttribute() to NaN] - expected: FAIL - - [style.type: setAttribute() to Infinity] - expected: FAIL - - [style.type: setAttribute() to -Infinity] - expected: FAIL - - [style.type: setAttribute() to "\\0"] - expected: FAIL - - [style.type: setAttribute() to null] - expected: FAIL - - [style.type: setAttribute() to object "test-toString"] - expected: FAIL - - [style.type: setAttribute() to object "test-valueOf"] - expected: FAIL - - [style.type: IDL set to ""] - expected: FAIL - - [style.type: IDL set to " \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07 \\b\\t\\n\\v\\f\\r\\x0e\\x0f \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17 \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f foo "] - expected: FAIL - - [style.type: IDL set to undefined] - expected: FAIL - - [style.type: IDL set to 7] - expected: FAIL - - [style.type: IDL set to 1.5] - expected: FAIL - - [style.type: IDL set to "5%"] - expected: FAIL - - [style.type: IDL set to "+100"] - expected: FAIL - - [style.type: IDL set to ".5"] - expected: FAIL - - [style.type: IDL set to true] - expected: FAIL - - [style.type: IDL set to false] - expected: FAIL - - [style.type: IDL set to object "[object Object\]"] - expected: FAIL - - [style.type: IDL set to NaN] - expected: FAIL - - [style.type: IDL set to Infinity] - expected: FAIL - - [style.type: IDL set to -Infinity] - expected: FAIL - - [style.type: IDL set to "\\0"] - expected: FAIL - - [style.type: IDL set to null] - expected: FAIL - - [style.type: IDL set to object "test-toString"] - expected: FAIL - - [style.type: IDL set to object "test-valueOf"] - expected: FAIL - [head.tabIndex: setAttribute() to "7\\v"] expected: FAIL diff --git a/tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_change.html.ini b/tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_change.html.ini deleted file mode 100644 index 112a376c55c..00000000000 --- a/tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_change.html.ini +++ /dev/null @@ -1,9 +0,0 @@ -[style_type_change.html] - [Check initial styleSheets length type] - expected: FAIL - - [Change type from invalid type to valid type] - expected: FAIL - - [Change type from valid type to invalid type] - expected: FAIL diff --git a/tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_html.html.ini b/tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_html.html.ini deleted file mode 100644 index e90502f3cf5..00000000000 --- a/tests/wpt/meta/html/semantics/document-metadata/the-style-element/style_type_html.html.ini +++ /dev/null @@ -1,6 +0,0 @@ -[style_type_html.html] - [With a whitespace-surrounded type attribute, the style should not apply] - expected: FAIL - - [With a charset parameter in the type attribute, the style should not apply] - expected: FAIL diff --git a/tests/wpt/meta/html/semantics/document-metadata/the-style-element/update-style-block-ascii-case-insensitive.html.ini b/tests/wpt/meta/html/semantics/document-metadata/the-style-element/update-style-block-ascii-case-insensitive.html.ini deleted file mode 100644 index 8103ea2a307..00000000000 --- a/tests/wpt/meta/html/semantics/document-metadata/the-style-element/update-style-block-ascii-case-insensitive.html.ini +++ /dev/null @@ -1,2 +0,0 @@ -[update-style-block-ascii-case-insensitive.html] - expected: FAIL