Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(parser): preserve pauses across mutation callbacks
lanyue-llk9abac5771d9d
diff --git a/moli-parser/src/html.rs b/moli-parser/src/html.rsindex ce351cf5e..2eb2c8dc8 100644--- a/moli-parser/src/html.rs+++ b/moli-parser/src/html.rs@@ -1539,12 +1539,6 @@ impl DocumentSink {.drain_pending_custom_element_construction_handoffs()}- pub(super) fn has_pending_custom_element_construction_handoff(&self) -> bool {- self.target- .borrow()- .has_pending_custom_element_construction_handoff()- }-pub(super) fn pending_custom_element_construction_handoff_placeholder(&self,) -> Option<NativeNodeId> {diff --git a/moli-parser/src/live_target.rs b/moli-parser/src/live_target.rsindex 99f1620d9..8ca5aff17 100644--- a/moli-parser/src/live_target.rs+++ b/moli-parser/src/live_target.rs@@ -2920,13 +2920,6 @@ impl ParserStreamHtmlTreeSinkTarget {.collect()}- pub(super) fn has_pending_custom_element_construction_handoff(&self) -> bool {- !self- .state- .pending_custom_element_construction_handoffs- .is_empty()- }-pub(super) fn front_pending_custom_element_construction_handoff(&self,) -> Option<&ParserCustomElementConstructionHandoff> {diff --git a/moli-parser/src/session.rs b/moli-parser/src/session.rsindex de78b3a77..6666145b5 100644--- a/moli-parser/src/session.rs+++ b/moli-parser/src/session.rs@@ -1,4 +1,7 @@-use std::{cell::RefCell, rc::Rc};+use std::{+ cell::{Cell, RefCell},+ rc::Rc,+};use html5ever::{LocalName, Namespace, QualName};use html5ever::{@@ -43,12 +46,14 @@ enum TokenizerPause {struct EmbedderPausingTreeBuilder {inner: TreeBuilder<ParseHandle, DocumentSink>,+ deferred_owner_interruption: Cell<bool>,}impl EmbedderPausingTreeBuilder {fn new(sink: DocumentSink, opts: TreeBuilderOpts) -> Self {Self {inner: TreeBuilder::new(sink, opts),+ deferred_owner_interruption: Cell::new(false),}}@@ -59,6 +64,7 @@ impl EmbedderPausingTreeBuilder {) -> Self {Self {inner: TreeBuilder::new_for_fragment(sink, context_handle, None, opts),+ deferred_owner_interruption: Cell::new(false),}}@@ -122,22 +128,6 @@ impl EmbedderPausingTreeBuilder {// ordinary tokenizer pause contract at this narrow adapter.return TokenSinkResult::Script(ParseHandle::new(script, None));}- if let Some(placeholder) = self- .inner- .sink- .pending_custom_element_construction_handoff_placeholder()- {- // html5ever has no custom-element pause result. Moli interprets the- // script handoff handle as a custom-element handoff when the parser- // sink has a matching pending construction record.- return TokenSinkResult::Script(ParseHandle::new(placeholder, None));- }- if let Some(stylesheet) = self.inner.sink.pending_blocking_stylesheet_pause() {- // html5ever exposes one generic tokenizer-yield result. The stream- // layer distinguishes this parser-created stylesheet boundary from- // actual script and custom-element handoffs using sink-owned state.- return TokenSinkResult::Script(ParseHandle::new(stylesheet, None));- }result}}@@ -146,12 +136,47 @@ impl TokenSink for EmbedderPausingTreeBuilder {type Handle = TokenizerPause;fn process_token(&self, token: Token, line_number: u64) -> TokenSinkResult<Self::Handle> {+ // html5ever requires character, comment, doctype and EOF tokens to+ // return Continue. A parser mutation callback may make a stylesheet or+ // custom-element pause visible only after one of those tokens. Retain+ // the sink-owned pause until a tag token can yield it safely.+ let can_pause = matches!(&token, Token::TagToken(_));let result = self.process_token_before_callbacks(token, line_number);- if self.sink().finish_parser_dom_mutations().is_break() {+ let current_owner_interruption = self.sink().finish_parser_dom_mutations().is_break();+ let deferred_owner_interruption = self.deferred_owner_interruption.replace(false);+ let owner_interrupted = current_owner_interruption || deferred_owner_interruption;+ if !can_pause {+ self.deferred_owner_interruption.set(owner_interrupted);+ return TokenSinkResult::Continue;+ }+ if owner_interrupted {// A nested parser invocation already handed its blocker to the// owner. Stop this outer feed before it consumes another token.return TokenSinkResult::Script(TokenizerPause::OwnerInterrupted);}+ if matches!(+ &result,+ TokenSinkResult::Continue | TokenSinkResult::EncodingIndicator(_)+ ) {+ if let Some(placeholder) = self+ .sink()+ .pending_custom_element_construction_handoff_placeholder()+ {+ // html5ever has no custom-element pause result. The stream+ // distinguishes this handle using sink-owned state.+ return TokenSinkResult::Script(TokenizerPause::Handoff(ParseHandle::new(+ placeholder,+ None,+ )));+ }+ if let Some(stylesheet) = self.sink().pending_blocking_stylesheet_pause() {+ // Query after mutation callbacks so the handle matches+ // the queue entry consumed by the stream layer.+ return TokenSinkResult::Script(TokenizerPause::Handoff(ParseHandle::new(+ stylesheet, None,+ )));+ }+ }match result {TokenSinkResult::Continue => TokenSinkResult::Continue,TokenSinkResult::Script(handle) => {diff --git a/moli-parser/src/stream.rs b/moli-parser/src/stream.rsindex 5188b73be..bac940312 100644--- a/moli-parser/src/stream.rs+++ b/moli-parser/src/stream.rs@@ -553,12 +553,23 @@ impl HtmlTreeSinkStream {}let tokenizer_result = self.parser.feed();- let paused_for_custom_element =- matches!(tokenizer_result, HtmlParserSessionResult::Script(_))- && self.has_pending_custom_element_construction_handoff();- let paused_for_stylesheet = matches!(tokenizer_result, HtmlParserSessionResult::Script(_))- && !paused_for_custom_element- && self.peek_pending_blocking_stylesheet_pause().is_some();+ let paused_for_custom_element = matches!(+ &tokenizer_result,+ HtmlParserSessionResult::Script(handle)+ if self.peek_pending_custom_element_construction_handoff_placeholder()+ == Some(handle.node_id())+ );+ let paused_for_stylesheet = !paused_for_custom_element+ && matches!(+ &tokenizer_result,+ HtmlParserSessionResult::Script(handle)+ if self.peek_pending_blocking_stylesheet_pause() == Some(handle.node_id())+ );+ // Claim this yield before discovery and script-preparation callbacks+ // can register another stylesheet pause on the same live document.+ let claimed_stylesheet_pause = paused_for_stylesheet+ .then(|| self.pop_pending_blocking_stylesheet_pause())+ .flatten();let result = match tokenizer_result {HtmlParserSessionResult::OwnerInterrupted => RawParserStep::OwnerInterrupted,HtmlParserSessionResult::Script(handle) if paused_for_custom_element => {@@ -569,10 +580,7 @@ impl HtmlTreeSinkStream {RawParserStep::CustomElementConstruction}HtmlParserSessionResult::Script(handle) if paused_for_stylesheet => {- debug_assert_eq!(- self.peek_pending_blocking_stylesheet_pause(),- Some(handle.node_id())- );+ debug_assert_eq!(claimed_stylesheet_pause, Some(handle.node_id()));RawParserStep::BlockingStylesheet(handle.node_id())}HtmlParserSessionResult::Script(handle) => RawParserStep::Script(handle.node_id()),@@ -664,9 +672,8 @@ impl HtmlTreeSinkStream {blocking_signatures_before,))))} else if let RawParserStep::BlockingStylesheet(node_id) = result {- let pending_node_id = self.pop_pending_blocking_stylesheet_pause();assert_eq!(- pending_node_id,+ claimed_stylesheet_pause,Some(node_id),"tokenizer stylesheet yield must consume its matching pending parser pause");@@ -794,12 +801,6 @@ impl HtmlTreeSinkStream {.drain_pending_custom_element_construction_handoffs()}- fn has_pending_custom_element_construction_handoff(&self) -> bool {- self.parser- .sink()- .has_pending_custom_element_construction_handoff()- }-fn peek_pending_custom_element_construction_handoff_placeholder(&self) -> Option<NativeNodeId> {self.parser.sink()