Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(css): enforce exact style element type

ldm09725d7610d50
diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt
index fd33793e4..0dd377c01 100644
--- a/moli-benchmark/wpt-cross-current/failed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/failed-cases.txt
@@ -3218,8 +3218,6 @@ html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme
html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-head.html
html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-shadow-tree.html
html/semantics/document-metadata/the-meta-element/http-equiv-and-name-1.html
-html/semantics/document-metadata/the-style-element/style_type_change.html
-html/semantics/document-metadata/the-style-element/style_type_html.html
html/semantics/document-metadata/the-style-element/tentative/style-element-basic-import.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-allowed.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-nonce-allowed.html
diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt
index 825ae3bf5..ce94c5df5 100644
--- a/moli-benchmark/wpt-cross-current/passed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt
@@ -5312,6 +5312,8 @@ html/semantics/document-metadata/the-style-element/style_load_event.html
html/semantics/document-metadata/the-style-element/style_media.html
html/semantics/document-metadata/the-style-element/style_media_change.html
html/semantics/document-metadata/the-style-element/style_non_matching_media.html
+html/semantics/document-metadata/the-style-element/style_type_change.html
+html/semantics/document-metadata/the-style-element/style_type_html.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-blocked.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-both-blocked.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-nonce-script-only.html
diff --git a/moli-core/tests/web_apis.rs b/moli-core/tests/web_apis.rs
index 18afe945c..3df65acfd 100644
--- a/moli-core/tests/web_apis.rs
+++ b/moli-core/tests/web_apis.rs
@@ -477,7 +477,7 @@ async fn style_link_stylesheet_accessors_keep_link_sheet_null_without_source() -
page.serialize_html_async()
.await
.unwrap()
- .contains("data-style-accessors=\"text/css:text/css:screen:render:true:false\"")
+ .contains("data-style-accessors=\":text/css:screen:render:true:false\"")
);
assert!(
page.serialize_html_async()
diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
index a719e2810..fae3223fb 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
@@ -1214,11 +1214,10 @@ impl JsContextHost {
owner: DomHandle,
) -> Option<std::sync::Arc<[crate::style_engine::StylesheetFontFaceDescriptor]>> {
let dom_host = self.dom_host();
+ let element = dom_host.node(owner).and_then(Node::as_element)?;
if !dom_host.is_connected(owner)
|| dom_host.get_attribute(owner, "disabled").is_some()
- || !moli_web_mime::is_stylesheet_type_attribute(
- dom_host.get_attribute(owner, "type").as_deref(),
- )
+ || !crate::style_engine::stylesheet_owner_type_is_supported(element)
{
return None;
}
@@ -1227,7 +1226,7 @@ impl JsContextHost {
.owner_style_sheet_processing_source(owner)
.map(|source| source.font_faces());
}
- if dom_host.is_html_element_named(owner, "link") {
+ if element.is_html_element("link") {
return self
.linked_stylesheet_source_for_owner(owner)
.map(|source| source.font_faces());
diff --git a/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs b/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs
index d2a9ccfa6..3cc10420f 100644
--- a/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs
+++ b/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs
@@ -37,10 +37,9 @@ pub(super) fn owner_font_face_projection(
owner: DomHandle,
) -> Option<OwnerFontFaceProjection> {
let dom_host = host.dom_host();
+ let element = dom_host.node(owner)?.as_element()?;
if !dom_host.is_connected(owner)
- || !moli_web_mime::is_stylesheet_type_attribute(
- dom_host.get_attribute(owner, "type").as_deref(),
- )
+ || !crate::style_engine::stylesheet_owner_type_is_supported(element)
{
return None;
}
diff --git a/moli-renderer-v8/src/native_bridge/document/css_state/shared.rs b/moli-renderer-v8/src/native_bridge/document/css_state/shared.rs
index bb5cffbc2..50730a963 100644
--- a/moli-renderer-v8/src/native_bridge/document/css_state/shared.rs
+++ b/moli-renderer-v8/src/native_bridge/document/css_state/shared.rs
@@ -20,7 +20,3 @@ pub(super) fn object_bool_property(
}
Some(value.boolean_value(scope))
}
-
-pub(super) fn style_is_css_type(value: Option<String>) -> bool {
- moli_web_mime::is_stylesheet_type_attribute(value.as_deref())
-}
diff --git a/moli-renderer-v8/src/native_bridge/document/css_state/style_sheets.rs b/moli-renderer-v8/src/native_bridge/document/css_state/style_sheets.rs
index ddbd7ded6..cb91bb87b 100644
--- a/moli-renderer-v8/src/native_bridge/document/css_state/style_sheets.rs
+++ b/moli-renderer-v8/src/native_bridge/document/css_state/style_sheets.rs
@@ -1,4 +1,4 @@
-use super::shared::{STYLE_SHEETS_SLOT, object_bool_property, style_is_css_type};
+use super::shared::{STYLE_SHEETS_SLOT, object_bool_property};
use crate::{
context_bootstrap::set_style_sheet_list_contents,
document_runtime::DomHandle,
@@ -24,10 +24,10 @@ pub(super) fn sync_document_style_sheets<'s>(
let Some(node) = dom_host.node(sheet_handle) else {
continue;
};
- let Some(local_name) = node.local_name() else {
+ let Some(element) = node.as_element() else {
continue;
};
- if !style_is_css_type(dom_host.get_attribute(sheet_handle, "type")) {
+ if !crate::style_engine::stylesheet_owner_type_is_supported(element) {
continue;
}
let wrapper = if document_is_connected {
@@ -40,7 +40,8 @@ pub(super) fn sync_document_style_sheets<'s>(
let Some(wrapper) = wrapper else {
continue;
};
- if local_name == "link" && object_bool_property(scope, wrapper, "disabled").unwrap_or(false)
+ if element.local_name() == "link"
+ && object_bool_property(scope, wrapper, "disabled").unwrap_or(false)
{
continue;
}
diff --git a/moli-renderer-v8/src/native_bridge/element.rs b/moli-renderer-v8/src/native_bridge/element.rs
index 5cf3704ee..35be7b11a 100644
--- a/moli-renderer-v8/src/native_bridge/element.rs
+++ b/moli-renderer-v8/src/native_bridge/element.rs
@@ -637,7 +637,6 @@ pub(crate) fn computed_style_property_for_handle(
use stylesheets::{
link_disabled_getter_function, link_disabled_setter_function, style_blocking_getter_function,
style_blocking_setter_function, style_disabled_getter_function, style_disabled_setter_function,
- style_type_getter_function, style_type_setter_function,
};
pub(super) use template_install::{
install_specialized_instance_properties, install_specialized_template,
@@ -4034,8 +4033,9 @@ struct HtmlLinkElementUrlPrototypeDeclaration {
struct HtmlStyleElementPrototypeDeclaration {
#[webapi(
accessor_property,
- getter = style_type_getter_function,
- setter = style_type_setter_function
+ getter = dom_string_reflection_getter_function,
+ setter = dom_string_reflection_setter_function,
+ data = DomStringReflection::StyleType
)]
r#type: (),
#[webapi(
diff --git a/moli-renderer-v8/src/native_bridge/element/reflection.rs b/moli-renderer-v8/src/native_bridge/element/reflection.rs
index 8cca28f80..96e2d8892 100644
--- a/moli-renderer-v8/src/native_bridge/element/reflection.rs
+++ b/moli-renderer-v8/src/native_bridge/element/reflection.rs
@@ -233,6 +233,7 @@ pub(super) enum DomStringReflection {
SourceSizes,
SourceType,
StyleMedia,
+ StyleType,
TableBorder,
TableCellAbbr,
TableCellAxis,
@@ -623,6 +624,15 @@ const DOM_STRING_REFLECTION_DESCRIPTORS: &[(DomStringReflection, DomStringReflec
DomStringReflection::StyleMedia,
DomStringReflectionDescriptor::new("HTMLStyleElement", "media", "media"),
),
+ (
+ DomStringReflection::StyleType,
+ DomStringReflectionDescriptor::new_html_element(
+ "HTMLStyleElement",
+ "style",
+ "type",
+ "type",
+ ),
+ ),
(
DomStringReflection::TableBorder,
DomStringReflectionDescriptor::new("HTMLTableElement", "border", "border"),
diff --git a/moli-renderer-v8/src/native_bridge/element/shadow_dom/root_accessors.rs b/moli-renderer-v8/src/native_bridge/element/shadow_dom/root_accessors.rs
index 5cfff52b2..e6ab88437 100644
--- a/moli-renderer-v8/src/native_bridge/element/shadow_dom/root_accessors.rs
+++ b/moli-renderer-v8/src/native_bridge/element/shadow_dom/root_accessors.rs
@@ -22,8 +22,7 @@ use crate::{
use moli_encoding::decode_text_for_legacy_web;
use moli_v8_util::throw_type_error;
use moli_web_mime::{
- MimeSniffingContext, data_url_body_and_computed_mime_type, is_css_mime,
- is_stylesheet_type_attribute, mime_charset,
+ MimeSniffingContext, data_url_body_and_computed_mime_type, is_css_mime, mime_charset,
};
use std::collections::{HashMap, HashSet};
@@ -92,13 +91,15 @@ fn sync_shadow_root_style_sheets<'s>(
) {
let mut sheets = Vec::new();
for sheet_handle in shadow_root_style_sheet_handles_in_tree_order(runtime, root) {
+ let Some(element) = runtime
+ .dom_host()
+ .node(sheet_handle)
+ .and_then(crate::dom::native::Node::as_element)
+ else {
+ continue;
+ };
if !runtime.dom_host().is_connected(sheet_handle)
- || !is_stylesheet_type_attribute(
- runtime
- .dom_host()
- .get_attribute(sheet_handle, "type")
- .as_deref(),
- )
+ || !crate::style_engine::stylesheet_owner_type_is_supported(element)
{
continue;
}
@@ -124,19 +125,17 @@ fn sync_detached_shadow_root_style_sheets<'s>(
) {
let mut sheets = Vec::new();
for sheet_handle in shadow_root_style_sheet_handles_in_tree_order(runtime, root) {
- if !is_stylesheet_type_attribute(
- runtime
- .dom_host()
- .get_attribute(sheet_handle, "type")
- .as_deref(),
- ) {
- continue;
- }
- if runtime
+ let Some(element) = runtime
.dom_host()
.node(sheet_handle)
- .and_then(|node| node.local_name())
- .is_some_and(|name| name.eq_ignore_ascii_case("link"))
+ .and_then(crate::dom::native::Node::as_element)
+ else {
+ continue;
+ };
+ if !crate::style_engine::stylesheet_owner_type_is_supported(element) {
+ continue;
+ }
+ if element.local_name().eq_ignore_ascii_case("link")
&& !runtime
.dom_host()
.get_attribute(sheet_handle, "rel")
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
index d96fac3da..c3f111749 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
@@ -1,7 +1,5 @@
use std::rc::Rc;
-use moli_web_mime::is_stylesheet_type_attribute;
-
use crate::{
document_runtime::DomHandle,
dom::native::{DomHost, Node},
@@ -9,7 +7,7 @@ use crate::{
FullStyleWorldSnapshot, IncrementalStyleWorldUpdate, PreparedStyleWorldUpdate,
StyleSourceId, StyleTreeScopeVersions, StyleViewport, StyleWorldEnvironment,
StyleWorldUpdatePlan, StyloStyleEnvironment, StyloStylesheetSource,
- link_rel_qualifies_as_stylesheet,
+ link_rel_qualifies_as_stylesheet, stylesheet_owner_type_is_supported,
},
stylesheet_blocking::link_rel_includes_token,
};
@@ -301,9 +299,7 @@ pub(super) fn active_stylesheet_handles(
element.is_html_element("link") && link_stylesheet_is_enabled(runtime, *handle);
(style || link)
&& (include_detached || stylesheet_is_active_in_scope(runtime, root, *handle))
- && is_stylesheet_type_attribute(
- runtime.dom_host().get_attribute(*handle, "type").as_deref(),
- )
+ && stylesheet_owner_type_is_supported(element)
})
.collect::<Vec<_>>();
let preferred_title = handles
diff --git a/moli-renderer-v8/src/native_bridge/element/stylesheets/mod.rs b/moli-renderer-v8/src/native_bridge/element/stylesheets/mod.rs
index f4021b666..b4e24d1c4 100644
--- a/moli-renderer-v8/src/native_bridge/element/stylesheets/mod.rs
+++ b/moli-renderer-v8/src/native_bridge/element/stylesheets/mod.rs
@@ -11,5 +11,5 @@ pub(crate) use sheet::{
};
pub(in crate::native_bridge) use style_element::{
style_blocking_getter_function, style_blocking_setter_function, style_disabled_getter_function,
- style_disabled_setter_function, style_type_getter_function, style_type_setter_function,
+ style_disabled_setter_function,
};
diff --git a/moli-renderer-v8/src/native_bridge/element/stylesheets/sheet.rs b/moli-renderer-v8/src/native_bridge/element/stylesheets/sheet.rs
index b4d9326ba..c95f9ed3f 100644
--- a/moli-renderer-v8/src/native_bridge/element/stylesheets/sheet.rs
+++ b/moli-renderer-v8/src/native_bridge/element/stylesheets/sheet.rs
@@ -8,10 +8,9 @@ use crate::style_engine::link_rel_qualifies_as_stylesheet;
use crate::util::{
context_host_ptr_from_global_bridge, get_private_value, set_private_value, throw_type_error,
};
-use moli_web_mime::is_stylesheet_type_attribute;
+use moli_web_mime::is_css_stylesheet_type_hint;
use super::super::super::node::node_runtime_and_handle_from_object;
-use super::super::element_attribute;
pub(super) const STYLE_SHEET_CACHE_SLOT: &str = "__moliStyleSheet";
@@ -134,7 +133,7 @@ pub(crate) fn style_sheet_for_element<'s>(
let is_css_link = element.is_html_element("link")
&& link_rel_qualifies_as_stylesheet(element.attribute("rel"), element.attribute("title"));
if (!is_style && !is_css_link)
- || !is_stylesheet_type_attribute(element_attribute(runtime, handle, "type").as_deref())
+ || !crate::style_engine::stylesheet_owner_type_is_supported(element)
|| (is_css_link
&& runtime
.dom_host()
@@ -283,7 +282,7 @@ fn style_sheet_for_processing_instruction<'s>(
return None;
}
let type_attr = processing_instruction_pseudo_attribute(data, "type");
- if !is_stylesheet_type_attribute(type_attr.as_deref()) {
+ if !is_css_stylesheet_type_hint(type_attr.as_deref()) {
return None;
}
processing_instruction_pseudo_attribute(data, "href")?;
diff --git a/moli-renderer-v8/src/native_bridge/element/stylesheets/style_element.rs b/moli-renderer-v8/src/native_bridge/element/stylesheets/style_element.rs
index 62dcdde5d..7cdf9e326 100644
--- a/moli-renderer-v8/src/native_bridge/element/stylesheets/style_element.rs
+++ b/moli-renderer-v8/src/native_bridge/element/stylesheets/style_element.rs
@@ -1,50 +1,9 @@
-use crate::{
- context_bootstrap::css_stylesheet_runtime::css_style_sheet_disabled,
- util::{v8_string, v8str},
-};
+use crate::{context_bootstrap::css_stylesheet_runtime::css_style_sheet_disabled, util::v8str};
-use super::super::super::node::node_runtime_and_handle_from_object_or_detached;
use super::super::{
- attribute_property_getter_from_object_or_detached, element_attribute,
- set_dom_string_attribute_property_on_object,
+ attribute_property_getter_from_object_or_detached, set_dom_string_attribute_property_on_object,
};
-pub(in crate::native_bridge) fn style_type_getter_function<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
-) {
- let Ok((runtime_ptr, handle)) =
- node_runtime_and_handle_from_object_or_detached(scope, args.this())
- else {
- rv.set_empty_string();
- return;
- };
- let value = element_attribute(unsafe { &*runtime_ptr }, handle, "type")
- .unwrap_or_else(|| "text/css".to_owned());
- if let Some(value) = v8_string(scope, &value) {
- rv.set(value.into());
- } else {
- rv.set_empty_string();
- }
-}
-
-pub(in crate::native_bridge) fn style_type_setter_function<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
-) {
- set_dom_string_attribute_property_on_object(
- scope,
- args.this(),
- "type",
- args.get(0),
- "HTMLStyleElement",
- "type",
- );
- rv.set_undefined();
-}
-
pub(in crate::native_bridge) fn style_blocking_getter_function<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
diff --git a/moli-renderer-v8/src/runtime/page_css.rs b/moli-renderer-v8/src/runtime/page_css.rs
index da5c7376a..c003e19d3 100644
--- a/moli-renderer-v8/src/runtime/page_css.rs
+++ b/moli-renderer-v8/src/runtime/page_css.rs
@@ -4,7 +4,6 @@ use crate::runtime::page_surface::{
RendererStyleSheetHeader, RendererStyleSheetInventoryUpdate, RendererStyleSheetPayload,
};
use crate::stylesheet_blocking::link_rel_includes_token;
-use moli_web_mime::is_stylesheet_type_attribute;
use std::collections::HashSet;
impl PageVm {
@@ -120,7 +119,7 @@ impl PageVm {
let dom_host = self.vm().document_runtime.dom_host();
let node = dom_host.node(handle)?;
let element = node.as_element()?;
- if !is_stylesheet_type_attribute(element.attribute("type")) {
+ if !crate::style_engine::stylesheet_owner_type_is_supported(element) {
return None;
}
diff --git a/moli-renderer-v8/src/runtime/script_preloads.rs b/moli-renderer-v8/src/runtime/script_preloads.rs
index b29ca12d2..c5e97b8d1 100644
--- a/moli-renderer-v8/src/runtime/script_preloads.rs
+++ b/moli-renderer-v8/src/runtime/script_preloads.rs
@@ -1386,7 +1386,7 @@ impl HtmlPreloadScannerSink {
if !is_stylesheet && !is_style_preload {
return;
}
- if !moli_web_mime::is_stylesheet_type_attribute(
+ if !moli_web_mime::is_css_stylesheet_type_hint(
html_attr_value(&tag.attrs, "type").as_deref(),
) {
return;
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs
index 0be21411a..e76bdc871 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs
@@ -7137,7 +7137,7 @@ fn detached_resource_template_accessors_use_owner_prototypes() {
for (const name of ["type", "media", "blocking", "disabled"]) {
assert(!own(style, name), `style.${name} should not be own before set`);
}
- assert(style.type === "text/css", "style type default");
+ assert(style.type === "", "style type default");
style.type = "text/less";
style.media = "print";
style.blocking = "render";
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index 2c8ccc2d0..f78434ffe 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -7383,7 +7383,7 @@ fn cssom_linkstyle_math_and_svg_element_surfaces_match_idlharness() {
assert_eq!(
result,
- "function|true|true|true|function|true|true|true|true"
+ "function|true|true|false|function|true|true|true|true"
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 4806eb27c..e21f7bc82 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -356,6 +356,56 @@ fn computed_style_exposes_root_pointer_events() {
assert_eq!(result, "none|none|true");
}
+#[test]
+fn style_element_type_attribute_uses_raw_exact_css_match() {
+ let mut vm = new_parsed_test_vm(
+ "https://style-type-attribute.test/",
+ r#"<!doctype html>
+<html><head>
+ <style id="missing">#missing-target { color: rgb(0, 128, 0); }</style>
+ <style id="empty" type="">#empty-target { color: rgb(0, 128, 0); }</style>
+ <style id="mixed" type="TeXt/CsS">#mixed-target { color: rgb(0, 128, 0); }</style>
+ <style id="spaced" type=" text/css ">#spaced-target { color: rgb(0, 128, 0); }</style>
+ <style id="parameter" type="text/css; charset=utf-8">#parameter-target { color: rgb(0, 128, 0); }</style>
+</head><body>
+ <div id="missing-target"></div><div id="empty-target"></div>
+ <div id="mixed-target"></div><div id="spaced-target"></div>
+ <div id="parameter-target"></div>
+</body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const names = ['missing', 'empty', 'mixed', 'spaced', 'parameter'];
+ const green = 'rgb(0, 128, 0)';
+ const applied = name => getComputedStyle(document.getElementById(`${name}-target`)).color === green;
+ const styles = names.map(name => document.getElementById(name));
+ const initial = {
+ applied: names.map(applied),
+ sheets: styles.map(style => style.sheet !== null),
+ count: document.styleSheets.length,
+ reflected: styles.map(style => style.type)
+ };
+
+ const spaced = document.getElementById('spaced');
+ spaced.type = 'text/css';
+ const valid = [applied('spaced'), spaced.sheet !== null, document.styleSheets.length];
+ spaced.type = ' text/css ';
+ const invalid = [applied('spaced'), spaced.sheet === null, document.styleSheets.length];
+ return JSON.stringify({ initial, valid, invalid });
+})()
+"#,
+ )
+ .expect("style type attribute semantics should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"initial":{"applied":[true,true,true,false,false],"sheets":[true,true,true,false,false],"count":3,"reflected":["","","TeXt/CsS"," text/css ","text/css; charset=utf-8"]},"valid":[true,true,4],"invalid":[false,true,3]}"#
+ );
+}
+
fn child_document_handle_for_frame_id(vm: &ScriptVm, frame_id: &str) -> DomHandle {
let frame = element_handle_by_id(vm, frame_id);
vm._context_host
diff --git a/moli-renderer-v8/src/style_engine/mod.rs b/moli-renderer-v8/src/style_engine/mod.rs
index 3f901edee..1d2d71339 100644
--- a/moli-renderer-v8/src/style_engine/mod.rs
+++ b/moli-renderer-v8/src/style_engine/mod.rs
@@ -131,7 +131,9 @@ use source_id::StyleSourceKind;
use source_id::{StyleInvalidationSourceTarget, StyleScopeId};
pub(crate) use source_lifecycle::OwnedStyleSourceDocumentContext;
use source_lifecycle::StyleSourceDocumentContext;
-pub(crate) use source_owner::link_rel_qualifies_as_stylesheet;
+pub(crate) use source_owner::{
+ link_rel_qualifies_as_stylesheet, stylesheet_owner_type_is_supported,
+};
pub(crate) use stylesheet_resources::{StylesheetResourceGeneration, StylesheetResourceSnapshot};
#[cfg(test)]
use stylesheet_resources::{
diff --git a/moli-renderer-v8/src/style_engine/source/mod.rs b/moli-renderer-v8/src/style_engine/source/mod.rs
index a04ab72d5..09068a547 100644
--- a/moli-renderer-v8/src/style_engine/source/mod.rs
+++ b/moli-renderer-v8/src/style_engine/source/mod.rs
@@ -520,6 +520,18 @@ impl MoliStyleEngine {
owner,
host.text_content(owner).unwrap_or_default(),
);
+ } else if matches!(
+ change.kind(),
+ DomStylesheetOwnerChangeKind::Attribute {
+ namespace: None,
+ local_name,
+ } if local_name == "type"
+ ) {
+ // The type attribute changes whether the already-parsed
+ // owner source participates in its TreeScope. The source
+ // text and identity stay stable, but retained worlds must
+ // refresh their active stylesheet projection immediately.
+ self.invalidate_owner_stylesheet_set_for_owner_with_host(host, owner);
} else if matches!(
change.kind(),
DomStylesheetOwnerChangeKind::Unregistered
diff --git a/moli-renderer-v8/src/style_engine/source_owner.rs b/moli-renderer-v8/src/style_engine/source_owner.rs
index b0427a41a..67e4ef006 100644
--- a/moli-renderer-v8/src/style_engine/source_owner.rs
+++ b/moli-renderer-v8/src/style_engine/source_owner.rs
@@ -1,7 +1,7 @@
use crate::{
context_bootstrap::evaluate_match_media_query_list_with_viewport,
document_runtime::DomHandle,
- dom::native::{DomHost, Node},
+ dom::native::{DomHost, Element, Node},
protocol_types::EmulatedMediaOverrides,
style_engine::StyleViewport,
};
@@ -18,6 +18,19 @@ pub(crate) fn link_rel_qualifies_as_stylesheet(rel: Option<&str>, title: Option<
&& (!includes_token("alternate") || title.is_some_and(|title| !title.is_empty()))
}
+pub(crate) fn stylesheet_owner_type_is_supported(element: &Element) -> bool {
+ let type_attribute = element.attribute("type");
+ if element.is_html_element("style")
+ || (element.namespace() == "http://www.w3.org/2000/svg" && element.local_name() == "style")
+ {
+ return moli_web_mime::is_css_style_element_type_attribute(type_attribute);
+ }
+ if element.is_html_element("link") {
+ return moli_web_mime::is_css_stylesheet_type_hint(type_attribute);
+ }
+ false
+}
+
pub(super) fn stylesheet_owner_is_stylesheet_source_enabled(
host: &DomHost,
handle: DomHandle,
@@ -57,14 +70,12 @@ fn style_element_is_stylesheet_source_enabled(
emulated_media: &EmulatedMediaOverrides,
viewport: StyleViewport,
) -> bool {
- let Some(node) = host.node(handle) else {
+ let Some(element) = host.node(handle).and_then(Node::as_element) else {
return false;
};
- node.as_element()
- .is_some_and(|element| element.is_inline_style_element())
- && moli_web_mime::is_stylesheet_type_attribute(
- host.get_attribute(handle, "type").as_deref(),
- )
+ element.is_inline_style_element()
+ && host.get_attribute(handle, "disabled").is_none()
+ && stylesheet_owner_type_is_supported(element)
&& stylesheet_source_media_matches(media_text, emulated_media, viewport)
}
diff --git a/moli-renderer-v8/src/stylesheet_runtime/connected.rs b/moli-renderer-v8/src/stylesheet_runtime/connected.rs
index dbcca9683..f03337b4e 100644
--- a/moli-renderer-v8/src/stylesheet_runtime/connected.rs
+++ b/moli-renderer-v8/src/stylesheet_runtime/connected.rs
@@ -1842,7 +1842,7 @@ fn connected_style_owner_kind(
if super::is_inline_style_element(element) {
return if is_declarative_css_module_style_element(element) {
Some(ConnectedStyleOwnerKind::DeclarativeCssModule)
- } else if moli_web_mime::is_stylesheet_type_attribute(element.attribute("type")) {
+ } else if crate::style_engine::stylesheet_owner_type_is_supported(element) {
Some(ConnectedStyleOwnerKind::ClassicStyle)
} else {
None
diff --git a/moli-web-mime/src/classification.rs b/moli-web-mime/src/classification.rs
index 86a2ea717..2322ec9ac 100644
--- a/moli-web-mime/src/classification.rs
+++ b/moli-web-mime/src/classification.rs
@@ -51,7 +51,17 @@ pub fn is_text_mime_essence(input: &str) -> bool {
input.starts_with("text/")
}
-pub fn is_stylesheet_type_attribute(value: Option<&str>) -> bool {
+/// Whether a `style` element's raw `type` attribute selects classic CSS.
+///
+/// HTML's update-a-style-block algorithm compares the untrimmed attribute
+/// value directly; unlike a MIME type hint, parameters and surrounding ASCII
+/// whitespace are not accepted.
+pub fn is_css_style_element_type_attribute(value: Option<&str>) -> bool {
+ value.is_none_or(|value| value.is_empty() || value.eq_ignore_ascii_case("text/css"))
+}
+
+/// Whether a stylesheet link or processing-instruction MIME hint selects CSS.
+pub fn is_css_stylesheet_type_hint(value: Option<&str>) -> bool {
let Some(value) = value else {
return true;
};
diff --git a/moli-web-mime/src/lib.rs b/moli-web-mime/src/lib.rs
index 28aa6c033..615e882d2 100644
--- a/moli-web-mime/src/lib.rs
+++ b/moli-web-mime/src/lib.rs
@@ -10,10 +10,10 @@ mod sniffing;
pub use classification::{
is_audio_mime, is_audio_mime_essence, is_binary_document_mime_type, is_css_mime,
- is_dom_parser_xml_mime, is_font_mime, is_font_mime_essence, is_form_urlencoded_mime,
- is_html_document_mime, is_image_mime, is_image_mime_essence, is_javascript_mime,
- is_javascript_mime_essence, is_json_module_mime, is_multipart_form_data_mime,
- is_png_image_mime, is_png_image_mime_essence, is_stylesheet_type_attribute,
+ is_css_style_element_type_attribute, is_css_stylesheet_type_hint, is_dom_parser_xml_mime,
+ is_font_mime, is_font_mime_essence, is_form_urlencoded_mime, is_html_document_mime,
+ is_image_mime, is_image_mime_essence, is_javascript_mime, is_javascript_mime_essence,
+ is_json_module_mime, is_multipart_form_data_mime, is_png_image_mime, is_png_image_mime_essence,
is_supported_document_mime_type, is_svg_image_mime, is_svg_image_mime_essence, is_text_mime,
is_text_mime_essence, is_video_mime, is_video_mime_essence, is_webassembly_mime,
multipart_form_data_boundary,
diff --git a/moli-web-mime/src/tests.rs b/moli-web-mime/src/tests.rs
index c45c63ef2..418cd384d 100644
--- a/moli-web-mime/src/tests.rs
+++ b/moli-web-mime/src/tests.rs
@@ -190,12 +190,21 @@ fn matches_script_and_form_content_types() {
assert!(is_text_mime("Text/Plain; charset=utf-8"));
assert!(is_text_mime_essence("text/csv"));
assert!(!is_text_mime("application/json"));
- assert!(is_stylesheet_type_attribute(None));
- assert!(is_stylesheet_type_attribute(Some("")));
- assert!(is_stylesheet_type_attribute(Some(
+ assert!(is_css_style_element_type_attribute(None));
+ assert!(is_css_style_element_type_attribute(Some("")));
+ assert!(is_css_style_element_type_attribute(Some("TeXt/CsS")));
+ assert!(!is_css_style_element_type_attribute(Some(" text/css ")));
+ assert!(!is_css_style_element_type_attribute(Some(
+ "text/css; charset=utf-8"
+ )));
+ assert!(!is_css_style_element_type_attribute(Some("text/plain")));
+
+ assert!(is_css_stylesheet_type_hint(None));
+ assert!(is_css_stylesheet_type_hint(Some("")));
+ assert!(is_css_stylesheet_type_hint(Some(
" Text/CSS ; charset=utf-8 "
)));
- assert!(!is_stylesheet_type_attribute(Some("text/plain")));
+ assert!(!is_css_stylesheet_type_hint(Some("text/plain")));
assert!(is_image_mime("Image/PNG; charset=binary"));
assert!(is_image_mime_essence("image/svg+xml"));