diff --git a/moli-renderer-v8/src/document_runtime/document_write.rs b/moli-renderer-v8/src/document_runtime/document_write.rs index 13e07c72250..a6869032ae6 100644 --- a/moli-renderer-v8/src/document_runtime/document_write.rs +++ b/moli-renderer-v8/src/document_runtime/document_write.rs @@ -32,6 +32,12 @@ enum DocumentWriteParserPumpInput<'a> { QueuedOrBuffered, } +#[derive(Clone, Copy, Eq, PartialEq)] +enum HtmlFragmentParserContextMode { + Standard, + SiblingInsertion, +} + struct DocumentWriteParserMutationOwner<'a, 'scope, 'pin> { runtime: &'a mut DocumentRuntime, scope: &'a mut v8::PinScope<'scope, 'pin>, @@ -523,7 +529,7 @@ impl DocumentRuntime { } } - fn build_fragment_from_html_internal( + fn build_fragment_from_html_with_context_mode( &mut self, scope: &mut v8::PinScope<'_, '_>, host_ptr: *mut PageScriptHost, @@ -532,6 +538,7 @@ impl DocumentRuntime { html: &str, scripts_already_started: bool, custom_element_upgrade_timing: HtmlFragmentCustomElementUpgradeTiming, + context_mode: HtmlFragmentParserContextMode, scripting_enabled: bool, allow_declarative_shadow_roots: bool, ) -> Option { @@ -540,10 +547,19 @@ impl DocumentRuntime { let context_node = context_handle.and_then(|element| self.dom_host().node(element)); let context_namespace = context_node .and_then(Node::namespace) - .unwrap_or("http://www.w3.org/1999/xhtml"); - let context_local_name = context_node.and_then(Node::local_name).unwrap_or("body"); - let context = - HtmlFragmentContext::new(context_handle, context_namespace, context_local_name); + .unwrap_or("http://www.w3.org/1999/xhtml") + .to_owned(); + let mut context_local_name = context_node + .and_then(Node::local_name) + .unwrap_or("body") + .to_owned(); + if context_mode == HtmlFragmentParserContextMode::SiblingInsertion + && context_namespace == "http://www.w3.org/1999/xhtml" + && context_local_name.eq_ignore_ascii_case("html") + { + context_local_name = "body".to_owned(); + } + let context = HtmlFragmentContext::new(context_handle, &context_namespace, &context_local_name); let is_frameset_context = context_namespace == "http://www.w3.org/1999/xhtml" && context_local_name.eq_ignore_ascii_case("frameset"); let registry_association = match context_handle { @@ -641,7 +657,7 @@ impl DocumentRuntime { html: &str, ) -> Option { let scripting_enabled = unsafe { &*host_ptr }.document_scripting_enabled(document_handle); - self.build_fragment_from_html_internal( + self.build_fragment_from_html_with_context_mode( scope, host_ptr, document_handle, @@ -649,6 +665,7 @@ impl DocumentRuntime { html, false, HtmlFragmentCustomElementUpgradeTiming::InReturnedFragment, + HtmlFragmentParserContextMode::Standard, scripting_enabled, false, ) @@ -665,7 +682,7 @@ impl DocumentRuntime { custom_element_upgrade_timing: HtmlFragmentCustomElementUpgradeTiming, ) -> Option { let scripting_enabled = unsafe { &*host_ptr }.document_scripting_enabled(document_handle); - self.build_fragment_from_html_internal( + self.build_fragment_from_html_with_context_mode( scope, host_ptr, document_handle, @@ -673,6 +690,7 @@ impl DocumentRuntime { html, scripts_already_started, custom_element_upgrade_timing, + HtmlFragmentParserContextMode::Standard, scripting_enabled, false, ) @@ -688,7 +706,7 @@ impl DocumentRuntime { custom_element_upgrade_timing: HtmlFragmentCustomElementUpgradeTiming, ) -> Option { let scripting_enabled = unsafe { &*host_ptr }.document_scripting_enabled(document_handle); - self.build_fragment_from_html_internal( + self.build_fragment_from_html_with_context_mode( scope, host_ptr, document_handle, @@ -696,6 +714,7 @@ impl DocumentRuntime { html, true, custom_element_upgrade_timing, + HtmlFragmentParserContextMode::Standard, scripting_enabled, true, ) @@ -951,7 +970,13 @@ impl DocumentRuntime { DomHandle, ) -> bool, ) -> bool { - let Some(fragment) = self.build_fragment_from_html( + let scripting_enabled = unsafe { &*host_ptr }.document_scripting_enabled(document_handle); + let context_mode = if context_handle == target { + HtmlFragmentParserContextMode::Standard + } else { + HtmlFragmentParserContextMode::SiblingInsertion + }; + let Some(fragment) = self.build_fragment_from_html_with_context_mode( scope, host_ptr, document_handle, @@ -959,11 +984,13 @@ impl DocumentRuntime { html, true, HtmlFragmentCustomElementUpgradeTiming::AfterInsertion, + context_mode, + scripting_enabled, + false, ) else { return false; }; let added_children = self.dom_host().child_handles(fragment).collect::>(); - let _ = target; let changed = insert(self, scope, host_ptr, fragment); if changed && !self.upgrade_inserted_html_fragment_custom_elements( diff --git a/moli-renderer-v8/src/native_bridge/element/tree_mutation/callbacks.rs b/moli-renderer-v8/src/native_bridge/element/tree_mutation/callbacks.rs index 45e73731ddd..f57c0b9c6d9 100644 --- a/moli-renderer-v8/src/native_bridge/element/tree_mutation/callbacks.rs +++ b/moli-renderer-v8/src/native_bridge/element/tree_mutation/callbacks.rs @@ -282,15 +282,26 @@ pub(in crate::native_bridge) fn node_insert_adjacent_html_callback<'s>( position, InsertAdjacentPosition::BeforeBegin | InsertAdjacentPosition::AfterEnd ); - if needs_parent - && unsafe { &*runtime_ptr } + if needs_parent { + let parent = unsafe { &*runtime_ptr } .dom_host() .node(target) - .and_then(Node::parent_node) - .is_none() - { - rv.set_undefined(); - return; + .and_then(Node::parent_node); + let has_no_modifiable_parent = parent.is_none_or(|parent| { + unsafe { &*runtime_ptr } + .dom_host() + .node(parent) + .is_none_or(Node::is_document) + }); + if has_no_modifiable_parent { + throw_dom_exception( + scope, + "NoModificationAllowedError", + 7, + "The element has no modifiable parent for sibling insertion.", + ); + return; + } } let Some(document_handle) = insert_adjacent_document_handle(unsafe { &*runtime_ptr }, target, position) diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/tree_mutations.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/tree_mutations.rs index 5ba9e8411e7..2ad4da591c9 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/tree_mutations.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/tree_mutations.rs @@ -419,6 +419,81 @@ fn element_insert_adjacent_methods_parse_webidl_arguments() { ); } +#[test] +fn insert_adjacent_html_enforces_sibling_context_rules() { + let mut vm = new_storage_test_vm("https://insert-adjacent-sibling-context.test/"); + + let result = vm + .eval( + r#" +(() => { + const probe = callback => { + try { + callback(); + return 'missing'; + } catch (error) { + return `${error.name}:${error.code}`; + } + }; + const sources = ['', 'text', '', '
']; + const positions = ['beforebegin', 'afterend']; + const detached = document.createElement('div'); + const root = document.documentElement || + document.appendChild(document.createElement('html')); + const detachedErrors = positions.flatMap(position => + sources.map(source => probe(() => detached.insertAdjacentHTML(position, source))) + ); + const documentErrors = positions.flatMap(position => + sources.map(source => probe(() => root.insertAdjacentHTML(position, source))) + ); + + while (root.firstChild) { + root.removeChild(root.firstChild); + } + root.insertAdjacentHTML( + 'afterbegin', + '' + ); + const preservedInnerHtmlContext = + document.head?.id === 'inside-head' && + document.body?.id === 'inside-body' && + root.firstChild === document.head && + root.lastChild === document.body; + const head = document.head || + root.insertBefore(document.createElement('head'), root.firstChild); + const body = document.body || root.appendChild(document.createElement('body')); + head.insertAdjacentHTML('beforebegin', '

'); + body.insertAdjacentHTML('afterend', '

'); + const beforeHead = document.getElementById('before-head'); + const afterBody = document.getElementById('after-body'); + + return JSON.stringify({ + detachedErrors, + documentErrors, + preservedInnerHtmlContext, + counts: [ + document.getElementsByTagName('html').length, + document.getElementsByTagName('head').length, + document.getElementsByTagName('body').length + ], + placement: [ + beforeHead.nextSibling === head, + body.nextSibling === afterBody, + beforeHead.parentNode === root, + afterBody.parentNode === root + ] + }); +})() +"#, + ) + .expect("insertAdjacentHTML sibling context rules should evaluate"); + + assert_eq!( + result, + r#"{"detachedErrors":["NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7"],"documentErrors":["NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7","NoModificationAllowedError:7"],"preservedInnerHtmlContext":true,"counts":[1,1,1],"placement":[true,true,true,true]}"# + ); +} + #[test] fn document_fragment_and_shadow_root_get_element_by_id_match_browser_lookup_boundaries() { let mut vm = new_storage_test_vm("https://fragment-shadow-get-by-id.test/");