From 085ad9ea48c045cbdddad1d29cc021a8d56a94b9 Mon Sep 17 00:00:00 2001 From: elomscansio <163124154+elomscansio@users.noreply.github.com> Date: Sun, 27 Apr 2025 21:23:22 +0100 Subject: [PATCH] script_thread: HTML parser doesn't set relevant option (#36622) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch ensures that the Servo HTML parser uses the appropriate `TreeBuilderOpts` settings as specified by the HTML specification. Changes: - **iframe_srcdoc:** Detect if the parsed document's URL scheme is `about:srcdoc`, and set the parser’s iframe_srcdoc option accordingly. - **quirks_mode:** Use the associated Document's quirks mode to set the parser’s quirks mode flag, improving fragment parsing behavior. - **scripting_enabled:** Add a `scripting_enabled` method to Document, based on whether it has a browsing context, and set this flag for the parser. These updates align Servo's parsing behavior more closely with the specification: https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #35478 - [ ] There are tests for these changes Signed-off-by: Emmanuel Elom --- components/script/dom/document.rs | 6 ++++++ components/script/dom/servoparser/html.rs | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 02bdd343d89..1e2a3747751 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -1108,6 +1108,12 @@ impl Document { self.scripting_enabled } + /// Return whether scripting is enabled or not + /// + pub(crate) fn scripting_enabled(&self) -> bool { + self.has_browsing_context() + } + /// Return the element that currently has focus. // https://w3c.github.io/uievents/#events-focusevent-doc-focus pub(crate) fn get_focused_element(&self) -> Option> { diff --git a/components/script/dom/servoparser/html.rs b/components/script/dom/servoparser/html.rs index 97856256268..07848c87678 100644 --- a/components/script/dom/servoparser/html.rs +++ b/components/script/dom/servoparser/html.rs @@ -11,11 +11,12 @@ use html5ever::buffer_queue::BufferQueue; use html5ever::serialize::TraversalScope::IncludeNode; use html5ever::serialize::{AttrRef, Serialize, Serializer, TraversalScope}; use html5ever::tokenizer::{Tokenizer as HtmlTokenizer, TokenizerOpts}; -use html5ever::tree_builder::{TreeBuilder, TreeBuilderOpts}; +use html5ever::tree_builder::{QuirksMode as HTML5EverQuirksMode, TreeBuilder, TreeBuilderOpts}; use html5ever::{QualName, local_name, ns}; use markup5ever::TokenizerResult; use script_bindings::trace::CustomTraceable; use servo_url::ServoUrl; +use style::context::QuirksMode as StyleContextQuirksMode; use xml5ever::LocalName; use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods; @@ -58,9 +59,17 @@ impl Tokenizer { parsing_algorithm, }; + let quirks_mode = match document.quirks_mode() { + StyleContextQuirksMode::Quirks => HTML5EverQuirksMode::Quirks, + StyleContextQuirksMode::LimitedQuirks => HTML5EverQuirksMode::LimitedQuirks, + StyleContextQuirksMode::NoQuirks => HTML5EverQuirksMode::NoQuirks, + }; + let options = TreeBuilderOpts { ignore_missing_rules: true, - scripting_enabled: document.has_browsing_context(), + scripting_enabled: document.scripting_enabled(), + iframe_srcdoc: document.url().as_str() == "about:srcdoc", + quirks_mode, ..Default::default() };