diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index e7ea25c565f..00486b9033f 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -533,6 +533,7 @@ pub mod xmldocument; pub mod xmlhttprequest; pub mod xmlhttprequesteventtarget; pub mod xmlhttprequestupload; +pub mod xmlserializer; pub mod xr; pub mod xrframe; pub mod xrlayer; diff --git a/components/script/dom/webidls/XMLSerializer.webidl b/components/script/dom/webidls/XMLSerializer.webidl new file mode 100644 index 00000000000..cdc0f2f0342 --- /dev/null +++ b/components/script/dom/webidls/XMLSerializer.webidl @@ -0,0 +1,13 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +/* + * The origin of this IDL file is + * https://w3c.github.io/DOM-Parsing/#the-domparser-interface + */ + +[Constructor] +interface XMLSerializer { + [Throws] + DOMString serializeToString(Node root); +}; diff --git a/components/script/dom/xmlserializer.rs b/components/script/dom/xmlserializer.rs new file mode 100644 index 00000000000..350da26abf9 --- /dev/null +++ b/components/script/dom/xmlserializer.rs @@ -0,0 +1,60 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::codegen::Bindings::XMLSerializerBinding; +use crate::dom::bindings::codegen::Bindings::XMLSerializerBinding::XMLSerializerMethods; +use crate::dom::bindings::error::{Error, Fallible}; +use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::DOMString; +use crate::dom::node::Node; +use crate::dom::window::Window; +use dom_struct::dom_struct; +use xml5ever::serialize::{serialize, SerializeOpts, TraversalScope}; + +#[dom_struct] +pub struct XMLSerializer { + reflector_: Reflector, + window: Dom, +} + +impl XMLSerializer { + fn new_inherited(window: &Window) -> XMLSerializer { + XMLSerializer { + reflector_: Reflector::new(), + window: Dom::from_ref(window), + } + } + + pub fn new(window: &Window) -> DomRoot { + reflect_dom_object( + Box::new(XMLSerializer::new_inherited(window)), + window, + XMLSerializerBinding::Wrap, + ) + } + + pub fn Constructor(window: &Window) -> Fallible> { + Ok(XMLSerializer::new(window)) + } +} + +impl XMLSerializerMethods for XMLSerializer { + // https://w3c.github.io/DOM-Parsing/#the-xmlserializer-interface + fn SerializeToString(&self, root: &Node) -> Fallible { + let mut writer = vec![]; + match serialize( + &mut writer, + &root, + SerializeOpts { + traversal_scope: TraversalScope::IncludeNode, + }, + ) { + Ok(_) => Ok(DOMString::from(String::from_utf8(writer).unwrap())), + Err(_) => Err(Error::Type(String::from( + "root must be a Node or an Attr object", + ))), + } + } +} diff --git a/tests/wpt/metadata/domparsing/XMLSerializer-serializeToString.html.ini b/tests/wpt/metadata/domparsing/XMLSerializer-serializeToString.html.ini index 31520be0a5e..eb1f8013236 100644 --- a/tests/wpt/metadata/domparsing/XMLSerializer-serializeToString.html.ini +++ b/tests/wpt/metadata/domparsing/XMLSerializer-serializeToString.html.ini @@ -1,11 +1,5 @@ [XMLSerializer-serializeToString.html] type: testharness - [check XMLSerializer.serializeToString method could parsing xmldoc to string] - expected: FAIL - - [check XMLSerializer.serializeToString method could parsing xmldoc to string] - expected: FAIL - [Check if the default namespace is correctly reset.] expected: FAIL diff --git a/tests/wpt/metadata/domparsing/interfaces.any.js.ini b/tests/wpt/metadata/domparsing/interfaces.any.js.ini index 3a75b5fefe5..9fe8940d8ce 100644 --- a/tests/wpt/metadata/domparsing/interfaces.any.js.ini +++ b/tests/wpt/metadata/domparsing/interfaces.any.js.ini @@ -5,39 +5,6 @@ [interfaces] expected: FAIL - [XMLSerializer interface: existence and properties of interface object] - expected: FAIL - - [XMLSerializer interface object length] - expected: FAIL - - [XMLSerializer interface object name] - expected: FAIL - - [XMLSerializer interface: existence and properties of interface prototype object] - expected: FAIL - - [XMLSerializer interface: existence and properties of interface prototype object's "constructor" property] - expected: FAIL - - [XMLSerializer interface: existence and properties of interface prototype object's @@unscopables property] - expected: FAIL - - [XMLSerializer interface: operation serializeToString(Node)] - expected: FAIL - - [Stringification of new XMLSerializer()] - expected: FAIL - - [XMLSerializer interface: calling serializeToString(Node) on new XMLSerializer() with too few arguments must throw TypeError] - expected: FAIL - - [XMLSerializer must be primary interface of new XMLSerializer()] - expected: FAIL - - [XMLSerializer interface: new XMLSerializer() must inherit property "serializeToString(Node)" with the proper type] - expected: FAIL - [ShadowRoot interface: attribute innerHTML] expected: FAIL diff --git a/tests/wpt/metadata/domparsing/xml-serialization.xhtml.ini b/tests/wpt/metadata/domparsing/xml-serialization.xhtml.ini index b1bd8c2f837..15ff6265f55 100644 --- a/tests/wpt/metadata/domparsing/xml-serialization.xhtml.ini +++ b/tests/wpt/metadata/domparsing/xml-serialization.xhtml.ini @@ -1,20 +1,5 @@ [xml-serialization.xhtml] type: testharness - [Comment: containing --] - expected: FAIL - - [Comment: starting with -] - expected: FAIL - - [Comment: ending with -] - expected: FAIL - - [Comment: containing -->] - expected: FAIL - - [DocumentType: empty public and system id] - expected: FAIL - [DocumentType: empty system id] expected: FAIL @@ -33,18 +18,6 @@ [DocumentType: 'APOSTROPHE' (U+0027) and 'QUOTATION MARK' (U+0022)] expected: FAIL - [ProcessingInstruction: empty data] - expected: FAIL - - [ProcessingInstruction: non-empty data] - expected: FAIL - - [ProcessingInstruction: target contains xml] - expected: FAIL - - [ProcessingInstruction: target contains a 'COLON' (U+003A)] - expected: FAIL - [Element: href attributes are not percent-encoded] expected: FAIL diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index a5ce5aa85ff..4ed5f502112 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -20254,7 +20254,7 @@ "testharness" ], "mozilla/interfaces.html": [ - "f8d8ab879f84bf03f89b12d5d010c83c1025265a", + "a2b9fd23948319fabc0b5fff550b9565704b6678", "testharness" ], "mozilla/interfaces.js": [ diff --git a/tests/wpt/mozilla/tests/mozilla/interfaces.html b/tests/wpt/mozilla/tests/mozilla/interfaces.html index f8d8ab879f8..a2b9fd23948 100644 --- a/tests/wpt/mozilla/tests/mozilla/interfaces.html +++ b/tests/wpt/mozilla/tests/mozilla/interfaces.html @@ -246,6 +246,7 @@ test_interfaces([ "XMLHttpRequest", "XMLHttpRequestEventTarget", "XMLHttpRequestUpload", + "XMLSerializer", "console", ]);