diff --git a/moli-dom/src/native/host/clone.rs b/moli-dom/src/native/host/clone.rs index 337fe4ca145..ebd8ec76852 100644 --- a/moli-dom/src/native/host/clone.rs +++ b/moli-dom/src/native/host/clone.rs @@ -715,6 +715,65 @@ mod tests { } } + #[test] + fn document_type_clone_and_foreign_import_preserve_all_utf16_fields() { + use crate::native::DomStringValue; + let mut source = test_host(); + let values = [ + DomStringValue::from_utf16(&[0xd800]), + DomStringValue::from_utf16(&[0xd801, 0]), + DomStringValue::from_utf16(&[0xdc00, 0xd83d, 0xde00]), + ]; + let handle = source.create_document_type(&values[0], &values[1], &values[2]); + let clone = source.clone_node(handle, true).unwrap(); + let mut markup: Vec = "".encode_utf16()); + assert_eq!( + source.outer_html_utf16_with_shadow_roots( + handle, + &|_| true, + crate::native::ShadowRootInclusion::None, + None + ), + Some(markup) + ); + let mut target = test_host(); + target.create_comment("offset the arena"); + let imported = target + .import_foreign_node(target.document_handle(), source.dom(), handle, true) + .unwrap(); + for (host, id) in [(&source, handle), (&source, clone), (&target, imported)] { + let node = host.node(id).unwrap(); + let doctype = node.as_document_type().unwrap(); + assert_eq!(doctype.name_value(), &values[0]); + assert_eq!(doctype.public_id_value(), &values[1]); + assert_eq!(doctype.system_id_value(), &values[2]); + assert_eq!(node.node_name_value(), values[0]); + } + assert!( + source + .node(handle) + .unwrap() + .is_equal_node(source.dom(), source.node(clone).unwrap()) + ); + for index in 0..3 { + let mut different = values.clone(); + different[index] = DomStringValue::from_utf16(&[0xfffd]); + let other = source.create_document_type(&different[0], &different[1], &different[2]); + assert!( + !source + .node(handle) + .unwrap() + .is_equal_node(source.dom(), source.node(other).unwrap()) + ); + } + } + #[test] fn character_data_clone_and_foreign_import_preserve_utf16_values() { use crate::native::DomStringValue; @@ -1154,46 +1213,4 @@ mod tests { } } } - #[test] - fn document_type_clone_and_foreign_import_preserve_all_utf16_fields() { - use crate::native::DomStringValue; - let mut source = test_host(); - let values = [ - DomStringValue::from_utf16(&[0xd800]), - DomStringValue::from_utf16(&[0xd801, 0]), - DomStringValue::from_utf16(&[0xdc00, 0xd83d, 0xde00]), - ]; - let handle = source.create_document_type(&values[0], &values[1], &values[2]); - let clone = source.clone_node(handle, true).unwrap(); - let mut target = test_host(); - target.create_comment("offset the arena"); - let imported = target - .import_foreign_node(target.document_handle(), source.dom(), handle, true) - .unwrap(); - for (host, id) in [(&source, handle), (&source, clone), (&target, imported)] { - let node = host.node(id).unwrap(); - let doctype = node.as_document_type().unwrap(); - assert_eq!(doctype.name_value(), &values[0]); - assert_eq!(doctype.public_id_value(), &values[1]); - assert_eq!(doctype.system_id_value(), &values[2]); - assert_eq!(node.node_name_value(), values[0]); - } - assert!( - source - .node(handle) - .unwrap() - .is_equal_node(source.dom(), source.node(clone).unwrap()) - ); - for index in 0..3 { - let mut different = values.clone(); - different[index] = DomStringValue::from_utf16(&[0xfffd]); - let other = source.create_document_type(&different[0], &different[1], &different[2]); - assert!( - !source - .node(handle) - .unwrap() - .is_equal_node(source.dom(), source.node(other).unwrap()) - ); - } - } } diff --git a/moli-dom/src/native/html_serialization/engine.rs b/moli-dom/src/native/html_serialization/engine.rs index 9ac2b344ad5..f390415a4b4 100644 --- a/moli-dom/src/native/html_serialization/engine.rs +++ b/moli-dom/src/native/html_serialization/engine.rs @@ -696,12 +696,12 @@ impl<'dom, S: HtmlSerializationSink, const EMBEDDED: bool, const POLICY: bool> } NodeData::DocumentType(document_type) => { self.out.push_str("'); diff --git a/moli-renderer-v8/src/context_bootstrap/constructors/dom_implementation.rs b/moli-renderer-v8/src/context_bootstrap/constructors/dom_implementation.rs index 97394d6d4fe..02f6b265c13 100644 --- a/moli-renderer-v8/src/context_bootstrap/constructors/dom_implementation.rs +++ b/moli-renderer-v8/src/context_bootstrap/constructors/dom_implementation.rs @@ -45,19 +45,22 @@ struct DomImplementationPrototypeMethodsDeclaration { struct DomImplementationCreateDocumentTypeArgs { #[webidl( required, + converter = "dom_string16", missing_message = "Failed to execute 'createDocumentType' on 'DOMImplementation': 3 arguments required." )] - qualified_name: String, + qualified_name: Vec, #[webidl( required, + converter = "dom_string16", missing_message = "Failed to execute 'createDocumentType' on 'DOMImplementation': 3 arguments required." )] - public_id: String, + public_id: Vec, #[webidl( required, + converter = "dom_string16", missing_message = "Failed to execute 'createDocumentType' on 'DOMImplementation': 3 arguments required." )] - system_id: String, + system_id: Vec, } #[derive(webidl::WebIdlArgs)] @@ -123,9 +126,9 @@ fn create_document_type_value<'s>( parsed: &DomImplementationCreateDocumentTypeArgs, owner_document: Option>, ) -> Option> { - let qualified_name = v8_string(scope, &parsed.qualified_name)?; - let public_id = v8_string(scope, &parsed.public_id)?; - let system_id = v8_string(scope, &parsed.system_id)?; + let qualified_name = crate::util::v8_string_from_utf16_units(scope, &parsed.qualified_name)?; + let public_id = crate::util::v8_string_from_utf16_units(scope, &parsed.public_id)?; + let system_id = crate::util::v8_string_from_utf16_units(scope, &parsed.system_id)?; let mut argv = vec![qualified_name.into(), public_id.into(), system_id.into()]; if let Some(owner_document) = owner_document { argv.push(owner_document.into()); @@ -164,10 +167,10 @@ fn dom_implementation_create_document_type_callback<'s>( } } -fn is_valid_document_type_name(name: &str) -> bool { +fn is_valid_document_type_name(name: &[u16]) -> bool { !name - .chars() - .any(|ch| ch == '\0' || ch.is_ascii_whitespace() || ch == '>') + .iter() + .any(|unit| matches!(*unit, 0 | 9 | 10 | 12 | 13 | 32 | 62)) } fn dom_implementation_create_html_document_callback<'s>( diff --git a/moli-renderer-v8/src/document_runtime/dom_facade.rs b/moli-renderer-v8/src/document_runtime/dom_facade.rs index 99017f29c55..3fd674602e6 100644 --- a/moli-renderer-v8/src/document_runtime/dom_facade.rs +++ b/moli-renderer-v8/src/document_runtime/dom_facade.rs @@ -517,9 +517,9 @@ impl DocumentRuntime { pub(crate) fn create_document_type( &mut self, - name: &str, - public_id: &str, - system_id: &str, + name: impl Into, + public_id: impl Into, + system_id: impl Into, ) -> DomHandle { self.dom_host .create_document_type(name, public_id, system_id) diff --git a/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/nodes.rs b/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/nodes.rs index 9d56b2665e8..ea1d13ff7f8 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/nodes.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/nodes.rs @@ -4,9 +4,9 @@ use crate::util::context_host_ptr_from_global_bridge; pub(in crate::native_bridge::document) fn build_detached_document_type_object<'s>( scope: &mut v8::PinScope<'s, '_>, - name: &str, - public_id: &str, - system_id: &str, + name: &moli_dom::native::DomStringValue, + public_id: &moli_dom::native::DomStringValue, + system_id: &moli_dom::native::DomStringValue, ) -> Option> { let object = new_detached_object_with_prototype( scope, @@ -17,17 +17,17 @@ pub(in crate::native_bridge::document) fn build_detached_document_type_object<'s let _ = state.set( scope, v8str(scope, "name").into(), - v8_string(scope, name)?.into(), + crate::util::v8_string_from_dom_string_value(scope, name)?.into(), ); let _ = state.set( scope, v8str(scope, "publicId").into(), - v8_string(scope, public_id)?.into(), + crate::util::v8_string_from_dom_string_value(scope, public_id)?.into(), ); let _ = state.set( scope, v8str(scope, "systemId").into(), - v8_string(scope, system_id)?.into(), + crate::util::v8_string_from_dom_string_value(scope, system_id)?.into(), ); define_detached_state(scope, object, state); if let Some(runtime_ptr) = context_host_ptr_from_global_bridge(scope) { diff --git a/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/state.rs b/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/state.rs index 4f7d4c25d26..6bbd80554ad 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/state.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_objects/builders/state.rs @@ -11,7 +11,7 @@ pub(in crate::native_bridge::document) fn new_detached_state_object<'s>( scope: &mut v8::PinScope<'s, '_>, kind: &str, node_type: i32, - node_name: &str, + node_name: impl Into, ) -> Option> { let state = new_null_prototype_object(scope); let _ = state.set( @@ -27,7 +27,7 @@ pub(in crate::native_bridge::document) fn new_detached_state_object<'s>( let _ = state.set( scope, v8str(scope, "nodeName").into(), - v8_string(scope, node_name)?.into(), + crate::util::v8_string_from_dom_string_value(scope, &node_name.into())?.into(), ); let _ = state.set( scope, diff --git a/moli-renderer-v8/src/native_bridge/document/detached_objects/clone.rs b/moli-renderer-v8/src/native_bridge/document/detached_objects/clone.rs index b9069daf687..e33a8562427 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_objects/clone.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_objects/clone.rs @@ -524,9 +524,9 @@ fn clone_processing_instruction_target<'s>( } struct CloneDocumentTypeMetadata { - name: String, - public_id: String, - system_id: String, + name: moli_dom::native::DomStringValue, + public_id: moli_dom::native::DomStringValue, + system_id: moli_dom::native::DomStringValue, } fn clone_document_type_metadata<'s>( @@ -540,21 +540,21 @@ fn clone_document_type_metadata<'s>( .and_then(Node::as_document_type) { return CloneDocumentTypeMetadata { - name: doctype.name().to_owned(), - public_id: doctype.public_id().to_owned(), - system_id: doctype.system_id().to_owned(), + name: doctype.name_value().clone(), + public_id: doctype.public_id_value().clone(), + system_id: doctype.system_id_value().clone(), }; } CloneDocumentTypeMetadata { name: detached_doctype_name(scope, node) - .or_else(|| object_string_property(scope, node, "name")) - .or_else(|| object_string_property(scope, node, "nodeName")) + .or_else(|| object_dom_string_property(scope, node, "name")) + .or_else(|| object_dom_string_property(scope, node, "nodeName")) .unwrap_or_default(), public_id: detached_doctype_public_id(scope, node) - .or_else(|| object_string_property(scope, node, "publicId")) + .or_else(|| object_dom_string_property(scope, node, "publicId")) .unwrap_or_default(), system_id: detached_doctype_system_id(scope, node) - .or_else(|| object_string_property(scope, node, "systemId")) + .or_else(|| object_dom_string_property(scope, node, "systemId")) .unwrap_or_default(), } } @@ -1048,29 +1048,14 @@ pub(in crate::native_bridge::document) fn clone_js_node_like_into_document_objec public_id, system_id, } = clone_document_type_metadata(scope, node); - if detached_state_kind(scope, document).as_deref() == Some("document") { - let cloned = - build_detached_document_type_object(scope, &name, &public_id, &system_id)?; - detached_set_owner_document(scope, cloned, document); - return Some(cloned); - } - let implementation = document.get(scope, v8str(scope, "implementation").into())?; - let implementation = v8::Local::::try_from(implementation).ok()?; - let create_doctype = - implementation.get(scope, v8str(scope, "createDocumentType").into())?; - let create_doctype = v8::Local::::try_from(create_doctype).ok()?; - let cloned = call_script_visible_function( - scope, - create_doctype, - implementation.into(), - &[ - v8_string(scope, &name)?.into(), - v8_string(scope, &public_id)?.into(), - v8_string(scope, &system_id)?.into(), - ], - "detached clone createDocumentType fallback", - )?; - v8::Local::::try_from(cloned).ok() + let context = crate::native_bridge::node_relevant_context(scope, document) + .or_else(|| document.get_creation_context(scope)) + .unwrap_or_else(|| scope.get_current_context()); + let target_scope = &mut v8::ContextScope::new(scope, context); + let cloned = + build_detached_document_type_object(target_scope, &name, &public_id, &system_id)?; + detached_set_owner_document(target_scope, cloned, document); + Some(cloned) } 11 => { let cloned = build_detached_document_fragment_object(scope, document)?; diff --git a/moli-renderer-v8/src/native_bridge/document/detached_objects/mutation.rs b/moli-renderer-v8/src/native_bridge/document/detached_objects/mutation.rs index 8d78e5c96f7..55416a9e53a 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_objects/mutation.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_objects/mutation.rs @@ -231,14 +231,14 @@ fn live_native_attribute_snapshot<'s>( struct LiveNativeNodeSnapshot { node_type: i32, - node_name: String, + node_name: moli_dom::native::DomStringValue, local_name: Option, namespace_uri: Option, prefix: Option, script_already_started: bool, - doctype_name: Option, - doctype_public_id: Option, - doctype_system_id: Option, + doctype_name: Option, + doctype_public_id: Option, + doctype_system_id: Option, } fn live_native_node_snapshot( @@ -251,7 +251,7 @@ fn live_native_node_snapshot( let doctype = node.as_document_type(); Some(LiveNativeNodeSnapshot { node_type: node.node_type() as i32, - node_name: node.node_name(), + node_name: node.node_name_value(), local_name: element.map(|element| element.local_name().to_owned()), namespace_uri: element .and_then(|element| (!element.namespace().is_empty()).then(|| element.namespace())) @@ -260,9 +260,9 @@ fn live_native_node_snapshot( script_already_started: element .filter(|element| element.local_name().eq_ignore_ascii_case("script")) .is_some_and(|element| element.script_already_started()), - doctype_name: doctype.map(|doctype| doctype.name().to_owned()), - doctype_public_id: doctype.map(|doctype| doctype.public_id().to_owned()), - doctype_system_id: doctype.map(|doctype| doctype.system_id().to_owned()), + doctype_name: doctype.map(|doctype| doctype.name_value().clone()), + doctype_public_id: doctype.map(|doctype| doctype.public_id_value().clone()), + doctype_system_id: doctype.map(|doctype| doctype.system_id_value().clone()), }) } @@ -415,7 +415,7 @@ fn adopt_live_node_as_detached_with_parent<'s>( let node_name = native_snapshot .as_ref() .map(|snapshot| snapshot.node_name.clone()) - .or_else(|| object_string_property(scope, node, "nodeName")) + .or_else(|| object_dom_string_property(scope, node, "nodeName")) .unwrap_or_default(); let state_kind = match node_type { 1 => "element", @@ -441,7 +441,7 @@ fn adopt_live_node_as_detached_with_parent<'s>( .and_then(|snapshot| snapshot.local_name.clone()) .or_else(|| object_string_property(scope, node, "localName")) .or_else(|| object_string_property(scope, node, "tagName")) - .unwrap_or_else(|| node_name.clone()); + .unwrap_or_else(|| node_name.as_str_lossy().to_owned()); let namespace_uri = native_snapshot .as_ref() .and_then(|snapshot| snapshot.namespace_uri.clone()) @@ -476,7 +476,7 @@ fn adopt_live_node_as_detached_with_parent<'s>( "xml" }; let node_name = if document_kind == "html" { - node_name.clone() + node_name.as_str_lossy().to_owned() } else { qualified_name.clone() }; @@ -593,7 +593,7 @@ fn adopt_live_node_as_detached_with_parent<'s>( let _ = state.set( scope, v8str(scope, "target").into(), - v8_string(scope, &node_name)?.into(), + crate::util::v8_string_from_dom_string_value(scope, &node_name)?.into(), ); } if native_snapshot.is_none() { @@ -627,12 +627,12 @@ fn adopt_live_node_as_detached_with_parent<'s>( let value = native_doctype_values .as_ref() .map(|values| values[index].clone()) - .or_else(|| object_string_property(scope, node, property)) + .or_else(|| object_dom_string_property(scope, node, property)) .unwrap_or_default(); let _ = state.set( scope, v8str(scope, property).into(), - v8_string(scope, &value)?.into(), + crate::util::v8_string_from_dom_string_value(scope, &value)?.into(), ); } define_detached_state(scope, node, state); diff --git a/moli-renderer-v8/src/native_bridge/document/detached_objects/object_access.rs b/moli-renderer-v8/src/native_bridge/document/detached_objects/object_access.rs index 22f7d8ac11c..dc148a77835 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_objects/object_access.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_objects/object_access.rs @@ -1,6 +1,7 @@ use super::*; pub(in crate::native_bridge::document) use crate::util::{ call_object_method, get_property as object_property_value, + object_defined_dom_string_property as object_dom_string_property, object_defined_string_property as object_string_property, object_property_as_object, }; use moli_webapi_declare::WebApiObject; diff --git a/moli-renderer-v8/src/native_bridge/document/detached_objects/state_tree.rs b/moli-renderer-v8/src/native_bridge/document/detached_objects/state_tree.rs index 5feb089d94d..ec9f24e1029 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_objects/state_tree.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_objects/state_tree.rs @@ -333,63 +333,51 @@ pub(in crate::native_bridge::document) fn read_detached_native_node_type<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, ) -> Option { - let runtime_ptr = context_host_ptr_from_global_bridge(scope)?; - let handle = detached_native_handle_for_runtime(scope, runtime_ptr, node)?; + let (runtime_ptr, handle) = + node_runtime_and_handle_from_object_or_detached(scope, node).ok()?; unsafe { &*runtime_ptr } .dom_host() .node(handle) .map(|node| node.node_type() as i32) } -pub(in crate::native_bridge::document) fn read_detached_native_node_name<'s>( - scope: &mut v8::PinScope<'s, '_>, - node: v8::Local<'s, v8::Object>, -) -> Option { - let runtime_ptr = context_host_ptr_from_global_bridge(scope)?; - let handle = detached_native_handle_for_runtime(scope, runtime_ptr, node)?; - unsafe { &*runtime_ptr } - .dom_host() - .node(handle) - .map(|node| node.node_name()) -} - pub(in crate::native_bridge::document) fn read_detached_native_doctype_name<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - let runtime_ptr = context_host_ptr_from_global_bridge(scope)?; - let handle = detached_native_handle_for_runtime(scope, runtime_ptr, node)?; +) -> Option { + let (runtime_ptr, handle) = + node_runtime_and_handle_from_object_or_detached(scope, node).ok()?; unsafe { &*runtime_ptr } .dom_host() .node(handle) .and_then(|node| node.as_document_type()) - .map(|doctype| doctype.name().to_owned()) + .map(|doctype| doctype.name_value().clone()) } pub(in crate::native_bridge::document) fn read_detached_native_doctype_public_id<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - let runtime_ptr = context_host_ptr_from_global_bridge(scope)?; - let handle = detached_native_handle_for_runtime(scope, runtime_ptr, node)?; +) -> Option { + let (runtime_ptr, handle) = + node_runtime_and_handle_from_object_or_detached(scope, node).ok()?; unsafe { &*runtime_ptr } .dom_host() .node(handle) .and_then(|node| node.as_document_type()) - .map(|doctype| doctype.public_id().to_owned()) + .map(|doctype| doctype.public_id_value().clone()) } pub(in crate::native_bridge::document) fn read_detached_native_doctype_system_id<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - let runtime_ptr = context_host_ptr_from_global_bridge(scope)?; - let handle = detached_native_handle_for_runtime(scope, runtime_ptr, node)?; +) -> Option { + let (runtime_ptr, handle) = + node_runtime_and_handle_from_object_or_detached(scope, node).ok()?; unsafe { &*runtime_ptr } .dom_host() .node(handle) .and_then(|node| node.as_document_type()) - .map(|doctype| doctype.system_id().to_owned()) + .map(|doctype| doctype.system_id_value().clone()) } pub(in crate::native_bridge::document) fn read_detached_native_processing_instruction_target<'s>( @@ -414,36 +402,49 @@ pub(in crate::native_bridge::document) fn detached_node_type<'s>( .or_else(|| object_node_type(scope, node)) } -pub(in crate::native_bridge::document) fn detached_node_name<'s>( +pub(in crate::native_bridge::document) fn detached_node_name_value<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - detached_state_string(scope, node, "nodeName") - .or_else(|| read_detached_native_node_name(scope, node)) +) -> Option { + if let Ok((runtime_ptr, handle)) = node_runtime_and_handle_from_object_or_detached(scope, node) + { + return unsafe { &*runtime_ptr } + .dom_host() + .node(handle) + .map(Node::node_name_value); + } + detached_state_object(scope, node) + .and_then(|state| object_dom_string_property(scope, state, "nodeName")) } pub(in crate::native_bridge) fn detached_doctype_name<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - read_detached_native_doctype_name(scope, node) - .or_else(|| detached_state_string(scope, node, "name")) +) -> Option { + read_detached_native_doctype_name(scope, node).or_else(|| { + detached_state_object(scope, node) + .and_then(|state| object_dom_string_property(scope, state, "name")) + }) } pub(in crate::native_bridge) fn detached_doctype_public_id<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - read_detached_native_doctype_public_id(scope, node) - .or_else(|| detached_state_string(scope, node, "publicId")) +) -> Option { + read_detached_native_doctype_public_id(scope, node).or_else(|| { + detached_state_object(scope, node) + .and_then(|state| object_dom_string_property(scope, state, "publicId")) + }) } pub(in crate::native_bridge) fn detached_doctype_system_id<'s>( scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>, -) -> Option { - read_detached_native_doctype_system_id(scope, node) - .or_else(|| detached_state_string(scope, node, "systemId")) +) -> Option { + read_detached_native_doctype_system_id(scope, node).or_else(|| { + detached_state_object(scope, node) + .and_then(|state| object_dom_string_property(scope, state, "systemId")) + }) } pub(in crate::native_bridge) fn detached_processing_instruction_target<'s>( diff --git a/moli-renderer-v8/src/native_bridge/document/detached_surface/creation/dom_implementation.rs b/moli-renderer-v8/src/native_bridge/document/detached_surface/creation/dom_implementation.rs index cd72a751091..d290cfb6dec 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_surface/creation/dom_implementation.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_surface/creation/dom_implementation.rs @@ -26,9 +26,12 @@ pub(in crate::native_bridge) fn bridge_create_detached_document_type_callback<'a args: v8::FunctionCallbackArguments<'a>, mut rv: v8::ReturnValue<'_, v8::Value>, ) { - let name = callback_arg_string(scope, &args, 0).unwrap_or_default(); - let public_id = callback_arg_string(scope, &args, 1).unwrap_or_default(); - let system_id = callback_arg_string(scope, &args, 2).unwrap_or_default(); + let Some(parsed) = crate::webidl::parse_args::(scope, &args) else { + return; + }; + let name = moli_dom::native::DomStringValue::from_utf16(&parsed.name); + let public_id = moli_dom::native::DomStringValue::from_utf16(&parsed.public_id); + let system_id = moli_dom::native::DomStringValue::from_utf16(&parsed.system_id); match build_detached_document_type_object(scope, &name, &public_id, &system_id) { Some(doctype) => { if let Ok(owner_document) = v8::Local::::try_from(args.get(3)) { @@ -40,6 +43,16 @@ pub(in crate::native_bridge) fn bridge_create_detached_document_type_callback<'a } } +#[derive(crate::webidl::WebIdlArgs)] +struct DetachedDocumentTypeArgs { + #[webidl(required, converter = "dom_string16")] + name: Vec, + #[webidl(required, converter = "dom_string16")] + public_id: Vec, + #[webidl(required, converter = "dom_string16")] + system_id: Vec, +} + pub(in crate::native_bridge) fn bridge_create_detached_html_document_callback<'a>( scope: &mut v8::PinScope<'a, '_>, args: v8::FunctionCallbackArguments<'a>, diff --git a/moli-renderer-v8/src/native_bridge/document/detached_surface/node_metadata.rs b/moli-renderer-v8/src/native_bridge/document/detached_surface/node_metadata.rs index 0501f65770e..34de0710a5d 100644 --- a/moli-renderer-v8/src/native_bridge/document/detached_surface/node_metadata.rs +++ b/moli-renderer-v8/src/native_bridge/document/detached_surface/node_metadata.rs @@ -22,8 +22,10 @@ pub(in crate::native_bridge) fn bridge_detached_node_name_callback<'a>( rv.set_empty_string(); return; }; - let node_name = detached_node_name(scope, node).unwrap_or_default(); - set_string_return_value(scope, &mut rv, &node_name); + let node_name = detached_node_name_value(scope, node).unwrap_or_default(); + if let Some(value) = crate::util::v8_string_from_dom_string_value(scope, &node_name) { + rv.set(value.into()); + } } pub(in crate::native_bridge) fn bridge_detached_node_value_getter_callback<'a>( @@ -101,7 +103,9 @@ pub(in crate::native_bridge) fn bridge_detached_doctype_name_callback<'a>( return; }; let name = detached_doctype_name(scope, node).unwrap_or_default(); - set_string_return_value(scope, &mut rv, &name); + if let Some(value) = crate::util::v8_string_from_dom_string_value(scope, &name) { + rv.set(value.into()); + } } pub(in crate::native_bridge) fn bridge_detached_doctype_public_id_callback<'a>( @@ -114,7 +118,9 @@ pub(in crate::native_bridge) fn bridge_detached_doctype_public_id_callback<'a>( return; }; let public_id = detached_doctype_public_id(scope, node).unwrap_or_default(); - set_string_return_value(scope, &mut rv, &public_id); + if let Some(value) = crate::util::v8_string_from_dom_string_value(scope, &public_id) { + rv.set(value.into()); + } } pub(in crate::native_bridge) fn bridge_detached_doctype_system_id_callback<'a>( @@ -127,7 +133,9 @@ pub(in crate::native_bridge) fn bridge_detached_doctype_system_id_callback<'a>( return; }; let system_id = detached_doctype_system_id(scope, node).unwrap_or_default(); - set_string_return_value(scope, &mut rv, &system_id); + if let Some(value) = crate::util::v8_string_from_dom_string_value(scope, &system_id) { + rv.set(value.into()); + } } pub(in crate::native_bridge) fn bridge_detached_processing_instruction_target_callback<'a>( diff --git a/moli-renderer-v8/src/native_bridge/node.rs b/moli-renderer-v8/src/native_bridge/node.rs index 95b16299c66..7fbc67de3ee 100644 --- a/moli-renderer-v8/src/native_bridge/node.rs +++ b/moli-renderer-v8/src/native_bridge/node.rs @@ -197,14 +197,14 @@ struct DocumentTypePrototypeDeclaration { fn set_document_type_string_value<'s>( scope: &mut v8::PinScope<'s, '_>, - value: Option, + value: Option, mut rv: v8::ReturnValue<'s, v8::Value>, ) { let Some(value) = value else { rv.set_undefined(); return; }; - let Some(value) = v8_string(scope, &value) else { + let Some(value) = crate::util::v8_string_from_dom_string_value(scope, &value) else { rv.set_null(); return; }; @@ -396,8 +396,8 @@ fn live_document_type_value<'s>( scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments<'s>, member: &str, - value: for<'a> fn(&'a DocumentType) -> &'a str, -) -> std::result::Result { + value: for<'a> fn(&'a DocumentType) -> &'a moli_dom::native::DomStringValue, +) -> std::result::Result { let Ok((runtime_ptr, handle)) = node_runtime_and_handle_from_args(scope, args) else { webidl::throw_type_error( scope, @@ -410,7 +410,7 @@ fn live_document_type_value<'s>( .node(handle) .and_then(Node::as_document_type) .map(value) - .map(str::to_owned) + .cloned() else { webidl::throw_type_error( scope, @@ -427,7 +427,7 @@ fn document_type_name_getter_function<'s>( rv: v8::ReturnValue<'s, v8::Value>, ) { let value = super::document::detached_doctype_name(scope, args.this()) - .or_else(|| live_document_type_value(scope, &args, "name", DocumentType::name).ok()); + .or_else(|| live_document_type_value(scope, &args, "name", DocumentType::name_value).ok()); set_document_type_string_value(scope, value, rv); } @@ -437,7 +437,7 @@ fn document_type_public_id_getter_function<'s>( rv: v8::ReturnValue<'s, v8::Value>, ) { let value = super::document::detached_doctype_public_id(scope, args.this()).or_else(|| { - live_document_type_value(scope, &args, "publicId", DocumentType::public_id).ok() + live_document_type_value(scope, &args, "publicId", DocumentType::public_id_value).ok() }); set_document_type_string_value(scope, value, rv); } @@ -448,7 +448,7 @@ fn document_type_system_id_getter_function<'s>( rv: v8::ReturnValue<'s, v8::Value>, ) { let value = super::document::detached_doctype_system_id(scope, args.this()).or_else(|| { - live_document_type_value(scope, &args, "systemId", DocumentType::system_id).ok() + live_document_type_value(scope, &args, "systemId", DocumentType::system_id_value).ok() }); set_document_type_string_value(scope, value, rv); } diff --git a/moli-renderer-v8/src/native_bridge/node/accessors.rs b/moli-renderer-v8/src/native_bridge/node/accessors.rs index 6da6e41934f..320557a693e 100644 --- a/moli-renderer-v8/src/native_bridge/node/accessors.rs +++ b/moli-renderer-v8/src/native_bridge/node/accessors.rs @@ -31,16 +31,19 @@ pub(super) fn node_node_name_getter_function<'s>( return; }; let name = match node { - NativeNodeReference::Attr(attr) => attr.name(scope), + NativeNodeReference::Attr(attr) => attr.name(scope).map(DomStringValue::from), NativeNodeReference::Tree { node, .. } => { let runtime = unsafe { &*node.runtime_ptr }; runtime.dom_host().node(node.handle).map(|value| { element_name_for_owner_document(runtime, node.handle) - .unwrap_or_else(|| value.node_name()) + .map(DomStringValue::from) + .unwrap_or_else(|| value.node_name_value()) }) } }; - if let Some(name) = name.and_then(|name| v8_string(scope, &name)) { + if let Some(name) = + name.and_then(|name| crate::util::v8_string_from_dom_string_value(scope, &name)) + { rv.set(name.into()); } } diff --git a/moli-renderer-v8/src/native_bridge/node/foreign/arg.rs b/moli-renderer-v8/src/native_bridge/node/foreign/arg.rs index cc8f5d5ed2d..1e4d3e266b4 100644 --- a/moli-renderer-v8/src/native_bridge/node/foreign/arg.rs +++ b/moli-renderer-v8/src/native_bridge/node/foreign/arg.rs @@ -88,11 +88,11 @@ fn materialize_foreign_node_arg_handle( Some(handle) } -fn materialize_foreign_node_arg_handle_for_identity( - scope: &mut v8::PinScope<'_, '_>, +fn materialize_foreign_node_arg_handle_for_identity<'s>( + scope: &mut v8::PinScope<'s, '_>, runtime_ptr: *mut PageScriptHost, document_handle: DomHandle, - object: v8::Local<'_, v8::Object>, + object: v8::Local<'s, v8::Object>, ) -> Option { let handle = clone_js_node_like_into_document(scope, runtime_ptr, document_handle, object, true)?; diff --git a/moli-renderer-v8/src/native_bridge/node/foreign/clone.rs b/moli-renderer-v8/src/native_bridge/node/foreign/clone.rs index db461e00841..07d0e4d6f38 100644 --- a/moli-renderer-v8/src/native_bridge/node/foreign/clone.rs +++ b/moli-renderer-v8/src/native_bridge/node/foreign/clone.rs @@ -47,11 +47,11 @@ fn append_cloned_child_without_mutation_effects( .append_child_without_mutation_effects(parent, child) } -pub(super) fn clone_js_node_like_into_document( - scope: &mut v8::PinScope<'_, '_>, +pub(super) fn clone_js_node_like_into_document<'s>( + scope: &mut v8::PinScope<'s, '_>, runtime_ptr: *mut PageScriptHost, document_handle: DomHandle, - object: v8::Local<'_, v8::Object>, + object: v8::Local<'s, v8::Object>, deep: bool, ) -> Option { let node_type = js_node_type(scope, object)?; @@ -118,11 +118,23 @@ pub(super) fn clone_js_node_like_into_document( Some(document) } 10 => { - let name = js_string_property(scope, object, "name") - .or_else(|| js_string_property(scope, object, "nodeName")) - .unwrap_or_default(); - let public_id = js_string_property(scope, object, "publicId").unwrap_or_default(); - let system_id = js_string_property(scope, object, "systemId").unwrap_or_default(); + let native = node_runtime_and_handle_from_object_or_detached(scope, object) + .ok() + .and_then(|(owner, handle)| unsafe { &*owner }.dom_host().node(handle)) + .and_then(Node::as_document_type) + .map(|doctype| { + [ + doctype.name_value().clone(), + doctype.public_id_value().clone(), + doctype.system_id_value().clone(), + ] + }); + let [name, public_id, system_id] = native.unwrap_or_else(|| { + ["name", "publicId", "systemId"].map(|key| { + crate::util::object_defined_dom_string_property(scope, object, key) + .unwrap_or_default() + }) + }); Some(unsafe { &mut *runtime_ptr }.create_document_type(&name, &public_id, &system_id)) } 11 => { diff --git a/moli-renderer-v8/src/script_vm/tests/document_type_utf16_complete.js b/moli-renderer-v8/src/script_vm/tests/document_type_utf16_complete.js new file mode 100644 index 00000000000..581ae568a8e --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/document_type_utf16_complete.js @@ -0,0 +1,84 @@ +(async () => { + const checks = []; + const check = (name, actual, expected) => checks.push({name, actual, expected, passed: JSON.stringify(actual) === JSON.stringify(expected)}); + const observe = (name, action, expected) => { let actual; try { actual = action(); } catch (error) { actual = {exception: error.name}; } check(name, actual, expected); }; + const quote = id => (id.includes('"') ? "'" : '"') + id + (id.includes('"') ? "'" : '"'); + const markup = ([name, pub, sys]) => ''; + const fields = node => [node.name, node.nodeName, node.publicId, node.systemId]; + const cases = [ + ['root', '', ''], ['', '', ''], ['\uD800', '\uD801', '\uDC00'], ['\uD801', 'p', 's'], + ['\uDC00', 'p\0', 's\0'], ['root', 'p', ''], ['root', '', 's'], + ['root', 'p"b', ''], ['root', '', 's"b'], ['root', "p'b", ''], ['root', '', "s'b"], + ['root', 'p"\'b', 's"\'b'], ['\uFFFD', '\uFFFD', '\uFFFD'], ['😀', '😀', '😀'], + ['root', '&<>\t\n\r', '&<>\t\n\r'], ['root', 'p\0\uD800', 's\uDC00\0'], + ]; + const frame = document.createElement('iframe'); document.body.appendChild(frame); + let popup; + const includePopup = globalThis.__documentTypeIncludePopup !== false; + try { + const realms = [['main', window], ['iframe', frame.contentWindow]]; + if (includePopup) { + popup = window.open('', '_blank'); + if (!popup) throw new Error('popup unavailable'); + await new Promise(resolve => setTimeout(resolve, 0)); + realms.push(['popup', popup]); + } + for (const [ownerName, owner] of realms) { + const documents = [ + ['live', owner.document], ['html', owner.document.implementation.createHTMLDocument('')], + ['xml', owner.document.implementation.createDocument(null, 'root')], + ['parsed-html', new owner.DOMParser().parseFromString('', 'text/html')], + ['parsed-xml', new owner.DOMParser().parseFromString('', 'application/xml')], + ['new-document', new owner.Document()], + ]; + for (const [docName, doc] of documents) for (const [calleeName, callee] of realms) { + const prefix = `${ownerName}/${docName}/${calleeName}`; + const create = callee.DOMImplementation.prototype.createDocumentType; + const serialize = node => callee.XMLSerializer.prototype.serializeToString.call(new callee.XMLSerializer(), node); + for (const [index, input] of cases.entries()) { + const label = prefix + '/' + index; + const expected = [input[0], input[0], input[1], input[2]]; + const node = create.call(doc.implementation, ...input); + check(label + '/fields', fields(node), expected); + check(label + '/owner', [node.ownerDocument === doc, node instanceof owner.DocumentType], [true, true]); + check(label + '/serialize', serialize(node), markup(input)); + for (const deep of [false, true]) { + observe(label + '/clone-' + deep, () => { + const clone = callee.Node.prototype.cloneNode.call(node, deep); + return [fields(clone), node.isEqualNode(clone), clone !== node, clone.ownerDocument === doc]; + }, [expected, true, true, true]); + } + const destination = owner.document.implementation.createDocument(null, 'root'); + const imported = destination.importNode(node, true); + check(label + '/import', [fields(imported), imported.ownerDocument === destination, imported.isEqualNode(node)], [expected, true, true]); + destination.insertBefore(imported, destination.documentElement); + check(label + '/connected', [fields(destination.doctype), serialize(destination)], [expected, markup(input) + '']); + const adopted = owner.document.implementation.createDocument(null, 'root'); + const source = destination.doctype; + check(label + '/adopt', [adopted.adoptNode(source) === source, source.ownerDocument === adopted, fields(source)], [true, true, expected]); + adopted.insertBefore(source, adopted.documentElement); + check(label + '/adopt-connected', [fields(adopted.doctype), serialize(adopted)], [expected, markup(input) + '']); + for (const key of ['name', 'nodeName', 'publicId', 'systemId']) Object.defineProperty(node, key, {get() {throw new Error('author getter: ' + key);}, configurable: true}); + observe(label + '/native-fields', () => { + const cleanClone = callee.Node.prototype.cloneNode.call(node, false); + return [serialize(node), fields(cleanClone), cleanClone.isEqualNode(node)]; + }, [markup(input), expected, true]); + } + for (const field of ['name', 'publicId', 'systemId']) { + const left = ['root', 'p', 's']; const right = left.slice(); + const position = field === 'name' ? 0 : field === 'publicId' ? 1 : 2; + left[position] = '\uD800'; right[position] = '\uD801'; + const a = create.call(doc.implementation, ...left), b = create.call(doc.implementation, ...right); + right[position] = '\uFFFD'; const replacement = create.call(doc.implementation, ...right); + check(prefix + '/identity/' + field, [a.isEqualNode(b), b.isEqualNode(a), a.isEqualNode(replacement), a.isEqualNode(callee.Node.prototype.cloneNode.call(a))], [false, false, false, true]); + } + const log = []; + const strings = ['r\uD800', 'p\uD801', 's\uDC00']; + const converted = create.call(doc.implementation, ...strings.map((value, index) => ({toString() {log.push(index); return value;}}))); + check(prefix + '/conversion', [log, fields(converted)], [[0, 1, 2], [strings[0], strings[0], strings[1], strings[2]]]); + } + } + globalThis.__uiEventResults = {complete: true, includePopup, total: checks.length, passed: checks.filter(row => row.passed).length, checks}; + return true; + } finally { frame.remove(); if (popup) popup.close(); } +})() diff --git a/moli-renderer-v8/src/script_vm/tests/observer_callbacks.rs b/moli-renderer-v8/src/script_vm/tests/observer_callbacks.rs index 9596ff4ea5a..583ae05d3a0 100644 --- a/moli-renderer-v8/src/script_vm/tests/observer_callbacks.rs +++ b/moli-renderer-v8/src/script_vm/tests/observer_callbacks.rs @@ -305,6 +305,32 @@ fn mutation_observer_observe_accepts_registered_native_proxy_targets_and_receive })()"#).unwrap(), "true"); } +#[tokio::test(flavor = "current_thread")] +async fn document_type_preserves_utf16_across_native_copy_and_serialization() { + let loader = static_http_loader(std::iter::empty::()); + let mut vm = new_page_task_executor_test_vm_with_loader("https://doctype-utf16.test/", &loader); + // Real popup Pages run the same fixture over CDP. + vm.exec("globalThis.__documentTypeIncludePopup = false;", None) + .unwrap(); + vm.exec(include_str!("document_type_utf16_complete.js"), None) + .unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(globalThis.__uiEventResults?.complete === true)", + "true", + "DocumentType native DOMString storage", + ) + .await; + assert_eq!( + vm.eval("JSON.stringify(__uiEventResults.checks.filter(row => !row.passed))") + .unwrap(), + "[]" + ); + assert_eq!(vm.eval("__uiEventResults.includePopup").unwrap(), "false"); + assert_eq!(vm.eval("__uiEventResults.total").unwrap(), "3936"); +} + #[tokio::test(flavor = "current_thread")] async fn dom_implementation_derives_receiver_and_argument_conversion_in_each_document_realm() { let loader = static_http_loader(std::iter::empty::()); diff --git a/moli-renderer-v8/src/util.rs b/moli-renderer-v8/src/util.rs index 5bd0b15ccd4..1d96548a881 100644 --- a/moli-renderer-v8/src/util.rs +++ b/moli-renderer-v8/src/util.rs @@ -1015,7 +1015,7 @@ pub(crate) fn v8_value_to_dom_string_u16<'s>( } pub(crate) fn v8_string_from_dom_string_value<'s>( - scope: &mut v8::PinScope<'s, '_>, + scope: &v8::PinScope<'s, '_>, value: &moli_dom::native::DomStringValue, ) -> Option> { if let Some(text) = value.as_str() { @@ -1025,6 +1025,21 @@ pub(crate) fn v8_string_from_dom_string_value<'s>( } } +pub(crate) fn object_defined_dom_string_property( + scope: &mut v8::PinScope<'_, '_>, + object: v8::Local<'_, v8::Object>, + key: &str, +) -> Option { + let value = get_property(scope, object, key)?; + if value.is_null_or_undefined() { + return None; + } + let value = value.to_string(scope)?; + Some(moli_dom::native::DomStringValue::from_utf16( + v8_string_to_u16_string(scope, value).as_slice(), + )) +} + pub(crate) fn utf16_units(value: &str) -> Vec { value.encode_utf16().collect() } diff --git a/moli-renderer-v8/src/xml_serializer.rs b/moli-renderer-v8/src/xml_serializer.rs index 18f5c1405de..bd9fcdad98f 100644 --- a/moli-renderer-v8/src/xml_serializer.rs +++ b/moli-renderer-v8/src/xml_serializer.rs @@ -135,6 +135,21 @@ impl XmlMarkup { self.units.extend(value.encode_utf16()); } + fn push_value(&mut self, value: &DomStringValue) { + value.append_utf16_units_to(&mut self.units); + } + + fn push_doctype_id(&mut self, value: &DomStringValue) { + let quote = if value.as_str_lossy().contains('"') { + 0x27 + } else { + 0x22 + }; + self.units.push(quote); + self.push_value(value); + self.units.push(quote); + } + fn escaped_units(&mut self, units: impl Iterator, attribute: bool) { for unit in units { let replacement = match unit { @@ -263,17 +278,16 @@ fn serialize_native_node( return; }; markup.push_str(""); } diff --git a/moli-v8-util/src/strings.rs b/moli-v8-util/src/strings.rs index 68cf2860bbc..d20b451c987 100644 --- a/moli-v8-util/src/strings.rs +++ b/moli-v8-util/src/strings.rs @@ -18,7 +18,7 @@ pub fn is_nullish(value: Local<'_, Value>) -> bool { } pub fn v8_string_from_utf16_units<'s>( - scope: &mut v8::PinScope<'s, '_>, + scope: &v8::PinScope<'s, '_>, units: &[u16], ) -> Option> { v8::String::new_from_two_byte(scope, units, v8::NewStringType::Normal)