Determine the initial state for fragment parsing using the scripting flag of the context element (#37704)

When parsing a html fragment, the initial parser state depends on
whether or not scripting is enabled. So far we've used the scripting
flag of the parser, but that is wrong - the parser's scripting flag is
always false, because the fragment document has no browsing context.
Instead we should use the scripting flag of the context element.

Testing: A new web platform test passes

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-06-26 22:40:13 +02:00 committed by GitHub
parent 4dded465a4
commit cbb0407ae6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 48 additions and 35 deletions

View file

@ -199,7 +199,7 @@ impl ServoParser {
}
}
// https://html.spec.whatwg.org/multipage/#parsing-html-fragments
/// <https://html.spec.whatwg.org/multipage/#parsing-html-fragments>
pub(crate) fn parse_html_fragment(
context: &Element,
input: DOMString,
@ -211,7 +211,7 @@ impl ServoParser {
let window = context_document.window();
let url = context_document.url();
// Step 1.
// Step 1. Let document be a Document node whose type is "html".
let loader = DocumentLoader::new_with_threads(
context_document.loader().resource_threads().clone(),
Some(url.clone()),
@ -237,9 +237,16 @@ impl ServoParser {
can_gc,
);
// Step 2.
// Step 2. If context's node document is in quirks mode, then set document's mode to "quirks".
// Step 3. Otherwise, if context's node document is in limited-quirks mode, then set document's
// mode to "limited-quirks".
document.set_quirks_mode(context_document.quirks_mode());
// NOTE: The following steps happened as part of Step 1.
// Step 4. If allowDeclarativeShadowRoots is true, then set document's
// allow declarative shadow roots to true.
// Step 5. Create a new HTML parser, and associate it with document.
// Step 11.
let form = context_node
.inclusive_ancestors(ShadowIncluding::No)
@ -248,6 +255,7 @@ impl ServoParser {
let fragment_context = FragmentContext {
context_elem: context_node,
form_elem: form.as_deref(),
context_element_allows_scripting: context_document.scripting_enabled(),
};
let parser = ServoParser::new(
@ -1121,6 +1129,7 @@ impl PreInvoke for ParserContext {}
pub(crate) struct FragmentContext<'a> {
pub(crate) context_elem: &'a Node,
pub(crate) form_elem: Option<&'a Node>,
pub(crate) context_element_allows_scripting: bool,
}
#[cfg_attr(crown, allow(crown::unrooted_must_root))]