Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix: preserve nested parser input order

ldm0a46043736e62
diff --git a/moli-parser/src/html.rs b/moli-parser/src/html.rs
index 6470b7c59..b1cec45fa 100644
--- a/moli-parser/src/html.rs
+++ b/moli-parser/src/html.rs
@@ -611,6 +611,16 @@ impl DocumentStream {
}
}
+ /// Append input at the end of the currently active parser-inserted frame.
+ ///
+ /// This is used when a parser script has already inserted input and then
+ /// continues writing while that input is blocked on a nested resource.
+ /// The input must remain after the blocked frame's unconsumed tail rather
+ /// than becoming a newer nested insertion.
+ pub fn append_to_current_inserted_input(&mut self, chunk: &str) -> bool {
+ self.inner.append_to_current_inserted_input(chunk)
+ }
+
pub fn has_pending_input(&self) -> bool {
self.inner.has_script_input()
|| self.inner.has_buffered_input()
diff --git a/moli-parser/src/session.rs b/moli-parser/src/session.rs
index 94e86f14a..9deec4060 100644
--- a/moli-parser/src/session.rs
+++ b/moli-parser/src/session.rs
@@ -195,6 +195,17 @@ impl HtmlParserSession {
self.input_buffer.push_back(input);
}
+ pub(super) fn append_to_current_inserted_input(&mut self, input: StrTendril) -> bool {
+ if input.is_empty() {
+ return true;
+ }
+ if self.suspended_input_buffers.is_empty() {
+ return false;
+ }
+ self.input_buffer.push_back(input);
+ true
+ }
+
pub(super) fn has_buffered_input(&self) -> bool {
!self.input_buffer.is_empty()
|| self
diff --git a/moli-parser/src/stream.rs b/moli-parser/src/stream.rs
index 6988b7759..d94e9f2c4 100644
--- a/moli-parser/src/stream.rs
+++ b/moli-parser/src/stream.rs
@@ -519,6 +519,11 @@ impl HtmlTreeSinkStream {
self.pump_parser_step_with_source(chunk, true)
}
+ pub fn append_to_current_inserted_input(&mut self, chunk: &str) -> bool {
+ self.parser
+ .append_to_current_inserted_input(StrTendril::from(chunk))
+ }
+
fn pump_parser_step_with_source(
&mut self,
chunk: &str,
@@ -1739,6 +1744,32 @@ mod tests {
assert_eq!((start_line, start_column), (0, 0));
}
+ #[test]
+ fn blocked_nested_writes_keep_each_input_at_its_insertion_depth() {
+ let mut stream = DocumentStream::new_parser_stream_for_testing(
+ Url::parse("https://example.test/page.html").expect("test url"),
+ );
+
+ let outer = stream.pump_parser_step("<script>outer()</script>e");
+ assert!(matches!(
+ outer.result,
+ ParserPumpStep::Yield(ParserYield::Script(_))
+ ));
+ let nested = stream.pump_parser_inserted_step("<script src='nested.js'></script>r");
+ assert!(matches!(
+ nested.result,
+ ParserPumpStep::Yield(ParserYield::Script(_))
+ ));
+
+ assert!(stream.append_to_current_inserted_input("k"));
+ stream.append_to_end("d".to_owned());
+ stream
+ .script_input_session()
+ .enqueue_script_input_html("wo".to_owned());
+
+ assert_eq!(stream.snapshot_pending_input(), "worked");
+ }
+
#[test]
fn parser_token_crossing_inserted_and_original_input_stays_unknown() {
let mut stream = DocumentStream::new_parser_stream_for_testing(
diff --git a/moli-renderer-v8/src/live_document_parser.rs b/moli-renderer-v8/src/live_document_parser.rs
index b17b58945..eed26201c 100644
--- a/moli-renderer-v8/src/live_document_parser.rs
+++ b/moli-renderer-v8/src/live_document_parser.rs
@@ -48,9 +48,10 @@ fn pump_next_live_document_parser_step(
}
pub(crate) enum LiveDocumentParserStepOutcome {
- /// The current parser input was consumed without producing a synchronous
- /// parser lifecycle handoff.
- Continue,
+ /// The current parser input boundary was reached without producing a
+ /// synchronous parser lifecycle handoff. Parser-owned parent input may
+ /// already have been restored and still require another step.
+ InputBoundary,
/// The tree builder needs author-defined custom element construction before
/// parser insertion can continue in the same document parser.
CustomElementConstructionHandoff(Box<ParserCustomElementConstructionHandoff>),
@@ -149,7 +150,7 @@ fn live_document_parser_advance_from_outcome(
blocking_stylesheet_inputs: discovered_blocking_stylesheet_inputs,
};
let outcome = match result {
- ParserPumpStep::InputDrained => LiveDocumentParserStepOutcome::Continue,
+ ParserPumpStep::InputDrained => LiveDocumentParserStepOutcome::InputBoundary,
ParserPumpStep::Yield(ParserYield::CustomElementConstruction(handoff)) => {
LiveDocumentParserStepOutcome::CustomElementConstructionHandoff(handoff)
}
@@ -612,7 +613,7 @@ impl DocumentParserSession {
}
}
- pub(crate) fn finishes_when_drained(&self) -> bool {
+ pub(crate) fn finishes_on_empty_input(&self) -> bool {
matches!(
self.lifetime,
DocumentParserLifetime::Finite | DocumentParserLifetime::Closing
@@ -657,6 +658,15 @@ impl DocumentParserSession {
}
}
+ pub(crate) fn append_to_current_inserted_input(&mut self, source: &str) -> bool {
+ match self.backend() {
+ ExecutableDocumentParserBackend::Html(stream) => {
+ stream.borrow_mut().append_to_current_inserted_input(source)
+ }
+ ExecutableDocumentParserBackend::Xml(_) => false,
+ }
+ }
+
pub(crate) fn declare_eof(&mut self) {
if let ExecutableDocumentParserBackend::Xml(stream) = self.backend() {
stream.borrow_mut().declare_eof();
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs
index 0165fbac4..9708da138 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/live_parser.rs
@@ -15,7 +15,7 @@ use crate::{
live_document_parser::{
DocumentParserCloseDisposition, DocumentParserLifetime, DocumentParserRunState,
DocumentParserSession, LiveDocumentParserOwner, LiveDocumentParserStepOutcome,
- ParserSuspensionCause,
+ ParserStopReason, ParserSuspensionCause,
},
modulepreload::{
invalid_modulepreload_as_value, invalid_modulepreload_as_warning,
@@ -408,43 +408,80 @@ impl ParserElementCreationConsumer for ChildFrameLiveParserOwner<'_, '_, '_> {
}
#[derive(Debug, Clone, PartialEq, Eq)]
-enum ChildLiveDocumentParserProgress {
- /// The parser handoff was recorded without blocking the parser.
- Continue,
- /// The parser drained the current input and can finish the child document.
- Complete,
+enum ParserProgress {
+ /// Every parser-owned input frame is empty and this parser's lifetime
+ /// requires finalization.
+ ReadyToFinish,
+ /// Every parser-owned input frame is empty, but an explicitly open parser
+ /// remains resident for a later `document.write()` or `document.close()`.
+ WaitingForInput,
/// The parser queued a parser-blocking classic script and must be saved until
/// that script completes or reports source failure.
BlockedOnParserScript {
- cause: ParserSuspensionCause,
ready_work: Option<Box<FrameDocumentClassicScriptSchedulerWork>>,
},
/// The parser stopped immediately after a parser-created blocking
/// stylesheet in the body. The exact parser entry remains resident until
/// that document owner's stylesheet readiness settles.
- BlockedOnStylesheetPause { owner: DomHandle },
- /// The parser produced a blocking script, but it could not attach that script
- /// to the current child document owner; finish the parser without saving it.
- FailedToQueueParserScript,
+ BlockedOnStylesheetPause,
+ /// Parser ownership changed while a synchronous handoff was being applied.
+ /// The stale parser must be stopped rather than finalized or parked.
+ Stopped,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+enum ScriptDisposition {
+ /// The handoff was applied, skipped, or otherwise settled synchronously.
+ Continue,
+ /// A parser-blocking classic script owns the next parser wake.
+ BlockedOnParserScript {
+ cause: ParserSuspensionCause,
+ ready_work: Option<Box<FrameDocumentClassicScriptSchedulerWork>>,
+ },
+ /// The script could not enter its selected scheduler. The parser driver
+ /// decides whether to skip it for the current Document or stop a stale
+ /// parser.
+ AdmissionFailed { script_handle: DomHandle },
}
impl JsContextHost {
- fn suspend_live_child_parser_for_progress(
- parser: &mut DocumentParserSession,
- progress: &ChildLiveDocumentParserProgress,
- ) {
- let cause = match progress {
- ChildLiveDocumentParserProgress::BlockedOnParserScript { cause, .. } => Some(*cause),
- ChildLiveDocumentParserProgress::BlockedOnStylesheetPause { owner } => {
- Some(ParserSuspensionCause::ParserCreatedStylesheet { owner: *owner })
- }
- ChildLiveDocumentParserProgress::Continue
- | ChildLiveDocumentParserProgress::Complete
- | ChildLiveDocumentParserProgress::FailedToQueueParserScript => None,
- };
- if let Some(cause) = cause {
- let _ = parser.suspend(cause);
+ fn suspend_live_child_parser(parser: &mut DocumentParserSession, cause: ParserSuspensionCause) {
+ let _ = parser.suspend(cause);
+ }
+
+ fn live_child_parser_document_is_current(
+ &self,
+ child_handle: DomHandle,
+ document_handle: DomHandle,
+ ) -> bool {
+ self.child_browsing_context_host_for_document_handle(document_handle) == Some(child_handle)
+ }
+
+ fn recover_current_child_parser_script_admission_failure(
+ &mut self,
+ child_handle: DomHandle,
+ document_handle: DomHandle,
+ script_handle: DomHandle,
+ ) -> bool {
+ if !self.live_child_parser_document_is_current(child_handle, document_handle) {
+ tracing::debug!(
+ ?child_handle,
+ ?document_handle,
+ ?script_handle,
+ "stopping stale child parser after script scheduler admission failed"
+ );
+ return false;
}
+ let _ = self
+ .dom_host_mut()
+ .set_script_already_started(script_handle, true);
+ tracing::warn!(
+ ?child_handle,
+ ?document_handle,
+ ?script_handle,
+ "skipping current child parser script after scheduler admission failed"
+ );
+ true
}
fn drive_live_child_document_parser(
@@ -453,8 +490,12 @@ impl JsContextHost {
child_handle: DomHandle,
document_handle: DomHandle,
parser: &mut DocumentParserSession,
- ) -> ChildLiveDocumentParserProgress {
+ ) -> ParserProgress {
loop {
+ if !self.live_child_parser_document_is_current(child_handle, document_handle) {
+ parser.stop(ParserStopReason::DocumentReplacement);
+ return ParserProgress::Stopped;
+ }
let outcome = {
let mut owner = ChildFrameLiveParserOwner::new(self, scope, document_handle);
parser.advance_queued_or_resume_step(&mut owner)
@@ -466,9 +507,13 @@ impl JsContextHost {
discovery_signals,
);
match outcome {
- LiveDocumentParserStepOutcome::Continue => {
+ LiveDocumentParserStepOutcome::InputBoundary => {
if parser.input_is_empty() {
- return ChildLiveDocumentParserProgress::Complete;
+ return if parser.finishes_on_empty_input() {
+ ParserProgress::ReadyToFinish
+ } else {
+ ParserProgress::WaitingForInput
+ };
}
// Draining one insertion frame restores its parent input
// inside the same feed and still reports an input
@@ -490,11 +535,13 @@ impl JsContextHost {
self.frame_document_blocking_stylesheets.has_pending(owner)
});
if remains_blocked {
- let progress = ChildLiveDocumentParserProgress::BlockedOnStylesheetPause {
- owner: pause.node_id,
- };
- Self::suspend_live_child_parser_for_progress(parser, &progress);
- return progress;
+ Self::suspend_live_child_parser(
+ parser,
+ ParserSuspensionCause::ParserCreatedStylesheet {
+ owner: pause.node_id,
+ },
+ );
+ return ParserProgress::BlockedOnStylesheetPause;
}
}
LiveDocumentParserStepOutcome::ScriptHandoff(handoff) => {
@@ -503,10 +550,20 @@ impl JsContextHost {
document_handle,
*handoff,
) {
- ChildLiveDocumentParserProgress::Continue => {}
- progress => {
- Self::suspend_live_child_parser_for_progress(parser, &progress);
- return progress;
+ ScriptDisposition::Continue => {}
+ ScriptDisposition::BlockedOnParserScript { cause, ready_work } => {
+ Self::suspend_live_child_parser(parser, cause);
+ return ParserProgress::BlockedOnParserScript { ready_work };
+ }
+ ScriptDisposition::AdmissionFailed { script_handle } => {
+ if !self.recover_current_child_parser_script_admission_failure(
+ child_handle,
+ document_handle,
+ script_handle,
+ ) {
+ parser.stop(ParserStopReason::DocumentReplacement);
+ return ParserProgress::Stopped;
+ }
}
}
}
@@ -519,7 +576,7 @@ impl JsContextHost {
child_handle: DomHandle,
document_handle: DomHandle,
handoff: ParserScriptHandoff,
- ) -> ChildLiveDocumentParserProgress {
+ ) -> ScriptDisposition {
let (script_handle, start_line, start_column) = match &handoff {
ParserScriptHandoff::BlockingClassic {
node_id,
@@ -589,7 +646,7 @@ impl JsContextHost {
.set_script_already_started(node_id, true);
}
}
- return ChildLiveDocumentParserProgress::Continue;
+ return ScriptDisposition::Continue;
}
match handoff {
@@ -617,12 +674,14 @@ impl JsContextHost {
} else {
ParserSuspensionCause::ParserClassicSource { script: node_id }
};
- ChildLiveDocumentParserProgress::BlockedOnParserScript {
+ ScriptDisposition::BlockedOnParserScript {
cause,
ready_work: queued.ready_work.map(Box::new),
}
} else {
- ChildLiveDocumentParserProgress::FailedToQueueParserScript
+ ScriptDisposition::AdmissionFailed {
+ script_handle: node_id,
+ }
}
}
ParserScriptHandoff::AsyncPostParse {
@@ -685,7 +744,7 @@ impl JsContextHost {
);
}
}
- ChildLiveDocumentParserProgress::Continue
+ ScriptDisposition::Continue
}
ParserScriptHandoff::NoExecution {
node_id, outcome, ..
@@ -698,7 +757,7 @@ impl JsContextHost {
if let (_, _, Some(run)) = outcome.into_parts() {
self.record_parser_no_execution_run(run);
}
- ChildLiveDocumentParserProgress::Continue
+ ScriptDisposition::Continue
}
ParserScriptHandoff::PreparationFailure {
node_id, failure, ..
@@ -708,7 +767,7 @@ impl JsContextHost {
node_id,
failure.element_state_transition(),
);
- ChildLiveDocumentParserProgress::Continue
+ ScriptDisposition::Continue
}
}
}
@@ -720,7 +779,7 @@ impl JsContextHost {
script_handle: DomHandle,
_start_line: u64,
mut script: crate::planning::PreparedScript,
- ) -> ChildLiveDocumentParserProgress {
+ ) -> ScriptDisposition {
if script.kind == ScriptKind::Module {
return self.queue_live_child_parser_post_parse_module_handoff(
child_handle,
@@ -730,10 +789,10 @@ impl JsContextHost {
);
}
if script.kind != ScriptKind::Classic || script.source_kind != ScriptSourceKind::External {
- return ChildLiveDocumentParserProgress::Continue;
+ return ScriptDisposition::Continue;
}
if !matches!(script.source, ScriptSource::External) {
- return ChildLiveDocumentParserProgress::Continue;
+ return ScriptDisposition::Continue;
}
script.node_id = crate::dom::NodeId::new(script_handle.index());
if !self.queue_child_external_classic_document_script_for_current_document(
@@ -742,9 +801,9 @@ impl JsContextHost {
script_handle,
script,
) {
- return ChildLiveDocumentParserProgress::FailedToQueueParserScript;
+ return ScriptDisposition::AdmissionFailed { script_handle };
}
- ChildLiveDocumentParserProgress::Continue
+ ScriptDisposition::Continue
}
fn queue_live_child_parser_post_parse_module_handoff(
@@ -755,9 +814,9 @@ impl JsContextHost {
crate::stylesheet_blocking::DocumentBlockingStylesheetSignature,
>,
script: crate::planning::PreparedScript,
- ) -> ChildLiveDocumentParserProgress {
+ ) -> ScriptDisposition {
if script.kind != ScriptKind::Module {
- return ChildLiveDocumentParserProgress::Continue;
+ return ScriptDisposition::Continue;
}
if !self.queue_child_parser_module_root_for_current_document(
child_handle,
@@ -765,12 +824,12 @@ impl JsContextHost {
blocking_stylesheet_signatures,
script,
) {
- return ChildLiveDocumentParserProgress::FailedToQueueParserScript;
+ return ScriptDisposition::AdmissionFailed { script_handle };
}
let _ = self
.dom_host_mut()
.set_script_already_started(script_handle, true);
- ChildLiveDocumentParserProgress::Continue
+ ScriptDisposition::Continue
}
fn queue_live_child_parser_non_async_post_parse_script_handoff(
@@ -783,7 +842,7 @@ impl JsContextHost {
crate::stylesheet_blocking::DocumentBlockingStylesheetSignature,
>,
script: crate::planning::PreparedScript,
- ) -> ChildLiveDocumentParserProgress {
+ ) -> ScriptDisposition {
if script.kind == ScriptKind::Module {
return self.queue_live_child_parser_post_parse_module_handoff(
child_handle,
@@ -793,10 +852,10 @@ impl JsContextHost {
);
}
if script.kind != ScriptKind::Classic || script.source_kind != ScriptSourceKind::External {
- return ChildLiveDocumentParserProgress::Continue;
+ return ScriptDisposition::Continue;
}
if !matches!(script.source, ScriptSource::External) {
- return ChildLiveDocumentParserProgress::Continue;
+ return ScriptDisposition::Continue;
}
if self
.push_child_parser_classic_script_for_current_document(
@@ -811,9 +870,9 @@ impl JsContextHost {
)
.is_none()
{
- return ChildLiveDocumentParserProgress::FailedToQueueParserScript;
+ return ScriptDisposition::AdmissionFailed { script_handle };
}
- ChildLiveDocumentParserProgress::Continue
+ ScriptDisposition::Continue
}
fn finish_live_child_document_parser(
@@ -1037,28 +1096,60 @@ impl JsContextHost {
let Some(mut entry) = self.child_document_parsers.take(owner) else {
return !close_requested;
};
+ let executing_parser_script =
+ chunk.is_some() && self.child_document_is_executing_parser_script(document_handle);
let parser_insertion_only = chunk.is_some()
&& !close_requested
- && (entry.lifetime() == DocumentParserLifetime::Finite
- || self.child_document_is_executing_parser_script(document_handle));
- if let Some(chunk) = chunk {
- entry
- .stream_handle()
- .borrow()
- .script_input_session()
- .enqueue_script_input_html(chunk);
- }
+ && (entry.lifetime() == DocumentParserLifetime::Finite || executing_parser_script);
let parser_ready_to_advance = if close_requested {
entry.request_close() == DocumentParserCloseDisposition::DrainNow
} else {
entry.run_state() == DocumentParserRunState::Ready
};
+ if let Some(chunk) = chunk {
+ if parser_ready_to_advance {
+ entry
+ .stream_handle()
+ .borrow()
+ .script_input_session()
+ .enqueue_script_input_html(chunk);
+ } else if executing_parser_script {
+ if !entry.append_to_current_inserted_input(&chunk) {
+ tracing::warn!(
+ ?child_handle,
+ ?document_handle,
+ "child parser script write had no active inserted input frame"
+ );
+ entry
+ .stream_handle()
+ .borrow()
+ .script_input_session()
+ .enqueue_script_input_html(chunk);
+ }
+ } else if parser_insertion_only {
+ entry
+ .stream_handle()
+ .borrow()
+ .script_input_session()
+ .enqueue_script_input_html(chunk);
+ } else {
+ // A write from outside the blocked child parser belongs after
+ // every input frame already owned by that parser. Treating it
+ // as a fresh script insertion would move it ahead of buffered
+ // tails from nested document.write() calls.
+ entry.queue_arrived_chunk(chunk);
+ }
+ }
if !parser_ready_to_advance {
self.child_document_parsers.replace(owner, entry);
return true;
}
loop {
+ if !self.live_child_parser_document_is_current(child_handle, document_handle) {
+ entry.stop(ParserStopReason::DocumentReplacement);
+ return false;
+ }
let outcome = {
let mut parser_owner = ChildFrameLiveParserOwner::new(self, scope, document_handle);
entry.advance_queued_or_resume_step(&mut parser_owner)
@@ -1070,7 +1161,7 @@ impl JsContextHost {
discovery_signals,
);
match outcome {
- LiveDocumentParserStepOutcome::Continue => {
+ LiveDocumentParserStepOutcome::InputBoundary => {
if parser_insertion_only {
// Script-inserted input parks after one boundary; the
// post-script resume drains the remaining stack in its
@@ -1087,7 +1178,7 @@ impl JsContextHost {
// parking.
continue;
}
- if entry.finishes_when_drained() {
+ if entry.finishes_on_empty_input() {
if close_requested {
self.finish_child_document_write_parser_on_current_stack(
scope,
@@ -1134,10 +1225,12 @@ impl JsContextHost {
"child document.write parser paused on a body blocking stylesheet"
);
if self.frame_document_blocking_stylesheets.has_pending(owner) {
- let progress = ChildLiveDocumentParserProgress::BlockedOnStylesheetPause {
- owner: pause.node_id,
- };
- Self::suspend_live_child_parser_for_progress(&mut entry, &progress);
+ Self::suspend_live_child_parser(
+ &mut entry,
+ ParserSuspensionCause::ParserCreatedStylesheet {
+ owner: pause.node_id,
+ },
+ );
self.child_document_parsers.replace(owner, entry);
return false;
}
@@ -1169,69 +1262,25 @@ impl JsContextHost {
*handoff,
);
match progress {
- ChildLiveDocumentParserProgress::Continue => entry,
- ChildLiveDocumentParserProgress::BlockedOnParserScript {
- cause,
- ready_work,
- } => {
- let progress =
- ChildLiveDocumentParserProgress::BlockedOnParserScript {
- cause,
- ready_work: None,
- };
- Self::suspend_live_child_parser_for_progress(
- &mut entry, &progress,
- );
+ ScriptDisposition::Continue => entry,
+ ScriptDisposition::BlockedOnParserScript { cause, ready_work } => {
+ Self::suspend_live_child_parser(&mut entry, cause);
self.child_document_parsers.replace(owner, entry);
if let Some(ready_work) = ready_work {
self.push_child_document_script_ready_input(*ready_work);
}
return true;
}
- ChildLiveDocumentParserProgress::Complete => {
- debug_assert!(
- false,
- "script handoff cannot complete a parser stream"
- );
- entry
- }
- ChildLiveDocumentParserProgress::BlockedOnStylesheetPause {
- owner: stylesheet_owner,
- } => {
- let progress =
- ChildLiveDocumentParserProgress::BlockedOnStylesheetPause {
- owner: stylesheet_owner,
- };
- Self::suspend_live_child_parser_for_progress(
- &mut entry, &progress,
- );
- self.child_document_parsers.replace(owner, entry);
- return false;
- }
- ChildLiveDocumentParserProgress::FailedToQueueParserScript => {
- if entry.finishes_when_drained() && entry.input_is_empty() {
- if close_requested {
- self.finish_child_document_write_parser_on_current_stack(
- scope,
- script_context,
- child_handle,
- owner,
- document_handle,
- entry,
- );
- } else {
- self.finish_and_queue_live_child_document_parser(
- scope,
- child_handle,
- owner,
- document_handle,
- entry,
- );
- }
- } else {
- self.child_document_parsers.replace(owner, entry);
+ ScriptDisposition::AdmissionFailed { script_handle } => {
+ if !self.recover_current_child_parser_script_admission_failure(
+ child_handle,
+ document_handle,
+ script_handle,
+ ) {
+ entry.stop(ParserStopReason::DocumentReplacement);
+ return false;
}
- return false;
+ entry
}
}
}
@@ -1542,8 +1591,7 @@ impl JsContextHost {
&mut parser,
);
match outcome {
- ChildLiveDocumentParserProgress::Continue
- | ChildLiveDocumentParserProgress::Complete => {
+ ParserProgress::ReadyToFinish => {
let parser_stop_action = self.finish_live_child_document_parser(
scope,
child_handle,
@@ -1553,29 +1601,19 @@ impl JsContextHost {
);
ChildLiveDocumentParserStartResult::parser_stopped(parser_stop_action)
}
- ChildLiveDocumentParserProgress::BlockedOnParserScript { ready_work, .. } => {
+ ParserProgress::WaitingForInput => {
+ self.child_document_parsers.replace(owner, parser);
+ ChildLiveDocumentParserStartResult::parser_blocked(None)
+ }
+ ParserProgress::BlockedOnParserScript { ready_work } => {
self.child_document_parsers.replace(owner, parser);
ChildLiveDocumentParserStartResult::parser_blocked(ready_work.map(|work| *work))
}
- ChildLiveDocumentParserProgress::BlockedOnStylesheetPause { .. } => {
+ ParserProgress::BlockedOnStylesheetPause => {
self.child_document_parsers.replace(owner, parser);
ChildLiveDocumentParserStartResult::parser_blocked(None)
}
- ChildLiveDocumentParserProgress::FailedToQueueParserScript => {
- if parser.input_is_empty() {
- let parser_stop_action = self.finish_live_child_document_parser(
- scope,
- child_handle,
- owner,
- document_handle,
- parser,
- );
- ChildLiveDocumentParserStartResult::parser_stopped(parser_stop_action)
- } else {
- self.child_document_parsers.replace(owner, parser);
- ChildLiveDocumentParserStartResult::parser_blocked(None)
- }
- }
+ ParserProgress::Stopped => ChildLiveDocumentParserStartResult::parser_stopped(None),
}
}
@@ -1657,43 +1695,29 @@ impl JsContextHost {
let outcome =
self.drive_live_child_document_parser(scope, child_handle, document_handle, &mut entry);
match outcome {
- ChildLiveDocumentParserProgress::Continue
- | ChildLiveDocumentParserProgress::Complete => {
- if entry.finishes_when_drained() {
- self.finish_and_queue_live_child_document_parser(
- scope,
- child_handle,
- owner,
- document_handle,
- entry,
- );
- } else {
- self.child_document_parsers.replace(owner, entry);
- }
+ ParserProgress::ReadyToFinish => {
+ self.finish_and_queue_live_child_document_parser(
+ scope,
+ child_handle,
+ owner,
+ document_handle,
+ entry,
+ );
FrameDocumentClassicParserResumeApplication::resumed(None)
}
- ChildLiveDocumentParserProgress::BlockedOnParserScript { ready_work, .. } => {
+ ParserProgress::WaitingForInput => {
self.child_document_parsers.replace(owner, entry);
- FrameDocumentClassicParserResumeApplication::resumed(ready_work.map(|work| *work))
+ FrameDocumentClassicParserResumeApplication::resumed(None)
}
- ChildLiveDocumentParserProgress::BlockedOnStylesheetPause { .. } => {
+ ParserProgress::BlockedOnParserScript { ready_work } => {
self.child_document_parsers.replace(owner, entry);
- FrameDocumentClassicParserResumeApplication::resumed(None)
+ FrameDocumentClassicParserResumeApplication::resumed(ready_work.map(|work| *work))
}
- ChildLiveDocumentParserProgress::FailedToQueueParserScript => {
- if entry.finishes_when_drained() && entry.input_is_empty() {
- self.finish_and_queue_live_child_document_parser(
- scope,
- child_handle,
- owner,
- document_handle,
- entry,
- );
- } else {
- self.child_document_parsers.replace(owner, entry);
- }
+ ParserProgress::BlockedOnStylesheetPause => {
+ self.child_document_parsers.replace(owner, entry);
FrameDocumentClassicParserResumeApplication::resumed(None)
}
+ ParserProgress::Stopped => FrameDocumentClassicParserResumeApplication::resumed(None),
}
}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/parser_store.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/parser_store.rs
index 00a56144b..183f1501d 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/parser_store.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/parser_store.rs
@@ -158,7 +158,7 @@ mod tests {
let mut entry = store.take(owner).expect("open parser entry");
assert_eq!(entry.lifetime(), DocumentParserLifetime::Open);
entry.request_close();
- assert!(entry.finishes_when_drained());
+ assert!(entry.finishes_on_empty_input());
assert!(store.is_complete_for(owner));
}
}
diff --git a/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs b/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs
index 89fec17e8..d67c4ed43 100644
--- a/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs
+++ b/moli-renderer-v8/src/runtime/phase_one/parser_turn.rs
@@ -1456,7 +1456,7 @@ impl<'loader, 'state> ParserDriver<'loader, 'state> {
}
match outcome {
- LiveDocumentParserStepOutcome::Continue => {
+ LiveDocumentParserStepOutcome::InputBoundary => {
break ParserStepAdvanceOutcome::Continue;
}
LiveDocumentParserStepOutcome::CustomElementConstructionHandoff(handoff) => {
@@ -1566,7 +1566,7 @@ impl<'loader, 'state> ParserDriver<'loader, 'state> {
}
match outcome {
- LiveDocumentParserStepOutcome::Continue => {
+ LiveDocumentParserStepOutcome::InputBoundary => {
break ParserStepAdvanceOutcome::Continue;
}
LiveDocumentParserStepOutcome::CustomElementConstructionHandoff(handoff) => {
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index bbe8d76a8..290e694a4 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -5929,6 +5929,119 @@ fn child_document_write_nested_write_preserves_parser_insertion_point() {
);
}
+// Ported from Chromium's WPT copy at
+// html/webappapis/dynamic-markup-insertion/document-write/iframe_003.html.
+#[test]
+fn child_document_write_script_restores_character_chunked_tail() {
+ let mut vm = new_storage_test_vm("https://child-write-character-tail.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const frame = document.createElement("iframe");
+ (document.body || document.documentElement || document).appendChild(frame);
+ const childDocument = frame.contentDocument;
+ const source =
+ "<script>document.write(\"<i id='a'>Filler Text</i>\")<\/script>" +
+ "<b id=b>Filler Text</b>";
+ for (const character of source) {
+ childDocument.write(character);
+ }
+ childDocument.close();
+ return Array.from(childDocument.body.children)
+ .map(element => [element.localName, element.id, element.textContent].join(":"))
+ .join("|");
+})()
+"#,
+ )
+ .expect("a written script must restore the character-chunked child parser tail");
+
+ assert_eq!(result, "i:a:Filler Text|b:b:Filler Text");
+}
+
+// Ported from Chromium's WPT copy at
+// html/webappapis/dynamic-markup-insertion/document-write/iframe_010.html.
+#[test]
+fn child_document_close_inside_written_script_preserves_insertion_stack() {
+ let mut vm = new_storage_test_vm("https://child-write-script-close-stack.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const frame = document.createElement("iframe");
+ (document.body || document.documentElement || document).appendChild(frame);
+ const childDocument = frame.contentDocument;
+ const source =
+ "<script>document.write('<table><plaintext>Filler '); document.close();<\/script>";
+ for (const character of source) {
+ childDocument.write(character);
+ }
+ const children = childDocument.body.children;
+ return [
+ children.length,
+ children[0]?.localName,
+ children[0]?.textContent,
+ children[1]?.localName,
+ ].join("|");
+})()
+"#,
+ )
+ .expect("document.close() in a written script must preserve its parser insertion stack");
+
+ assert_eq!(result, "2|plaintext|Filler |table");
+}
+
+// Ported from Chromium's WPT copy at
+// html/webappapis/dynamic-markup-insertion/document-write/nested-document-write-2.html.
+#[tokio::test]
+async fn child_nested_external_document_write_preserves_input_order() {
+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
+ loader.set_network_offline(true);
+ let mut vm =
+ new_storage_test_vm_with_loader("https://child-nested-external-write.test/", &loader);
+
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.createElement("iframe");
+ (document.body || document.documentElement || document).appendChild(frame);
+ const childDocument = frame.contentDocument;
+ childDocument.open();
+
+ const nestedSource =
+ '<script src="data:text/javascript,document.write(%22w%22)%3Bdocument.write(%22o%22)%3B"><\/script>r';
+ const nestedLiteral = JSON.stringify(nestedSource).replace("</script>", "<\\/script>");
+ childDocument.write(
+ "<script>document.write(" + nestedLiteral + "); document.write('k');<\/script>e"
+ );
+ childDocument.write("d");
+ childDocument.close();
+})()
+"#,
+ )
+ .expect("nested external child document.write fixture should evaluate");
+
+ while vm
+ .run_next_child_frame_semantic_turn_for_test()
+ .await
+ .is_some()
+ {
+ // Drain the data-script source load, nested writes, and parser resume.
+ }
+
+ let result = vm
+ .eval(
+ r#"
+document.querySelector("iframe").contentDocument.body.textContent
+"#,
+ )
+ .expect("the nested external writer must restore all parent parser input");
+
+ assert_eq!(result, "worked");
+}
+
#[test]
fn child_parser_script_document_close_drains_restored_parent_input() {
let mut vm = new_storage_test_vm("https://child-parser-close.test/");