mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
script: Implement Element::GetHTML
and ShadowRoot::GetHTML
(#36106)
* Serialize html fragments without going through html5ever Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Implement ShadowRoot::GetHtml / Element::GetHtml Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Update WPT expectations Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Propagate CanGc annotations Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> --------- Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
parent
1c9f486f88
commit
19d5f5f06f
12 changed files with 286 additions and 15941 deletions
|
@ -17,6 +17,7 @@ use devtools_traits::AttrInfo;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use embedder_traits::InputMethodType;
|
use embedder_traits::InputMethodType;
|
||||||
use euclid::default::{Rect, Size2D};
|
use euclid::default::{Rect, Size2D};
|
||||||
|
use html5ever::serialize::TraversalScope;
|
||||||
use html5ever::serialize::TraversalScope::{ChildrenOnly, IncludeNode};
|
use html5ever::serialize::TraversalScope::{ChildrenOnly, IncludeNode};
|
||||||
use html5ever::{
|
use html5ever::{
|
||||||
LocalName, Namespace, Prefix, QualName, local_name, namespace_prefix, namespace_url, ns,
|
LocalName, Namespace, Prefix, QualName, local_name, namespace_prefix, namespace_url, ns,
|
||||||
|
@ -67,7 +68,9 @@ use crate::dom::attr::{Attr, AttrHelpersForLayout};
|
||||||
use crate::dom::bindings::cell::{DomRefCell, Ref, RefMut, ref_filter_map};
|
use crate::dom::bindings::cell::{DomRefCell, Ref, RefMut, ref_filter_map};
|
||||||
use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::ElementBinding::{ElementMethods, ShadowRootInit};
|
use crate::dom::bindings::codegen::Bindings::ElementBinding::{
|
||||||
|
ElementMethods, GetHTMLOptions, ShadowRootInit,
|
||||||
|
};
|
||||||
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
|
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
|
||||||
use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
|
use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||||
|
@ -2951,17 +2954,31 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
|
||||||
Node::replace_all(Some(frag.upcast()), &target);
|
Node::replace_all(Some(frag.upcast()), &target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#dom-element-gethtml>
|
||||||
|
fn GetHTML(&self, options: &GetHTMLOptions, can_gc: CanGc) -> DOMString {
|
||||||
|
// > Element's getHTML(options) method steps are to return the result of HTML fragment serialization
|
||||||
|
// > algorithm with this, options["serializableShadowRoots"], and options["shadowRoots"].
|
||||||
|
self.upcast::<Node>().html_serialize(
|
||||||
|
TraversalScope::ChildrenOnly(None),
|
||||||
|
options.serializableShadowRoots,
|
||||||
|
options.shadowRoots.clone(),
|
||||||
|
can_gc,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-element-innerhtml>
|
/// <https://html.spec.whatwg.org/multipage/#dom-element-innerhtml>
|
||||||
fn GetInnerHTML(&self) -> Fallible<DOMString> {
|
fn GetInnerHTML(&self, can_gc: CanGc) -> Fallible<DOMString> {
|
||||||
let qname = QualName::new(
|
let qname = QualName::new(
|
||||||
self.prefix().clone(),
|
self.prefix().clone(),
|
||||||
self.namespace().clone(),
|
self.namespace().clone(),
|
||||||
self.local_name().clone(),
|
self.local_name().clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// FIXME: This should use the fragment serialization algorithm, which takes
|
||||||
|
// care of distinguishing between html/xml documents
|
||||||
let result = if self.owner_document().is_html_document() {
|
let result = if self.owner_document().is_html_document() {
|
||||||
self.upcast::<Node>()
|
self.upcast::<Node>()
|
||||||
.html_serialize(ChildrenOnly(Some(qname)))
|
.html_serialize(ChildrenOnly(Some(qname)), false, vec![], can_gc)
|
||||||
} else {
|
} else {
|
||||||
self.upcast::<Node>()
|
self.upcast::<Node>()
|
||||||
.xml_serialize(XmlChildrenOnly(Some(qname)))
|
.xml_serialize(XmlChildrenOnly(Some(qname)))
|
||||||
|
@ -3001,9 +3018,12 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-element-outerhtml>
|
/// <https://html.spec.whatwg.org/multipage/#dom-element-outerhtml>
|
||||||
fn GetOuterHTML(&self) -> Fallible<DOMString> {
|
fn GetOuterHTML(&self, can_gc: CanGc) -> Fallible<DOMString> {
|
||||||
|
// FIXME: This should use the fragment serialization algorithm, which takes
|
||||||
|
// care of distinguishing between html/xml documents
|
||||||
let result = if self.owner_document().is_html_document() {
|
let result = if self.owner_document().is_html_document() {
|
||||||
self.upcast::<Node>().html_serialize(IncludeNode)
|
self.upcast::<Node>()
|
||||||
|
.html_serialize(IncludeNode, false, vec![], can_gc)
|
||||||
} else {
|
} else {
|
||||||
self.upcast::<Node>().xml_serialize(XmlIncludeNode)
|
self.upcast::<Node>().xml_serialize(XmlIncludeNode)
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,6 +21,7 @@ use constellation_traits::{
|
||||||
use devtools_traits::NodeInfo;
|
use devtools_traits::NodeInfo;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use euclid::default::{Rect, Size2D, Vector2D};
|
use euclid::default::{Rect, Size2D, Vector2D};
|
||||||
|
use html5ever::serialize::HtmlSerializer;
|
||||||
use html5ever::{Namespace, Prefix, QualName, namespace_url, ns, serialize as html_serialize};
|
use html5ever::{Namespace, Prefix, QualName, namespace_url, ns, serialize as html_serialize};
|
||||||
use js::jsapi::JSObject;
|
use js::jsapi::JSObject;
|
||||||
use js::rust::HandleObject;
|
use js::rust::HandleObject;
|
||||||
|
@ -110,6 +111,7 @@ use crate::dom::nodelist::NodeList;
|
||||||
use crate::dom::processinginstruction::ProcessingInstruction;
|
use crate::dom::processinginstruction::ProcessingInstruction;
|
||||||
use crate::dom::range::WeakRangeVec;
|
use crate::dom::range::WeakRangeVec;
|
||||||
use crate::dom::raredata::NodeRareData;
|
use crate::dom::raredata::NodeRareData;
|
||||||
|
use crate::dom::servoparser::serialize_html_fragment;
|
||||||
use crate::dom::shadowroot::{IsUserAgentWidget, LayoutShadowRootHelpers, ShadowRoot};
|
use crate::dom::shadowroot::{IsUserAgentWidget, LayoutShadowRootHelpers, ShadowRoot};
|
||||||
use crate::dom::stylesheetlist::StyleSheetListOwner;
|
use crate::dom::stylesheetlist::StyleSheetListOwner;
|
||||||
use crate::dom::svgsvgelement::{LayoutSVGSVGElementHelpers, SVGSVGElement};
|
use crate::dom::svgsvgelement::{LayoutSVGSVGElementHelpers, SVGSVGElement};
|
||||||
|
@ -2862,22 +2864,34 @@ impl Node {
|
||||||
pub(crate) fn html_serialize(
|
pub(crate) fn html_serialize(
|
||||||
&self,
|
&self,
|
||||||
traversal_scope: html_serialize::TraversalScope,
|
traversal_scope: html_serialize::TraversalScope,
|
||||||
|
serialize_shadow_roots: bool,
|
||||||
|
shadow_roots: Vec<DomRoot<ShadowRoot>>,
|
||||||
|
can_gc: CanGc,
|
||||||
) -> DOMString {
|
) -> DOMString {
|
||||||
let mut writer = vec![];
|
let mut writer = vec![];
|
||||||
html_serialize::serialize(
|
let mut serializer = HtmlSerializer::new(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
&self,
|
|
||||||
html_serialize::SerializeOpts {
|
html_serialize::SerializeOpts {
|
||||||
traversal_scope,
|
traversal_scope: traversal_scope.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
serialize_html_fragment(
|
||||||
|
self,
|
||||||
|
&mut serializer,
|
||||||
|
traversal_scope,
|
||||||
|
serialize_shadow_roots,
|
||||||
|
shadow_roots,
|
||||||
|
can_gc,
|
||||||
)
|
)
|
||||||
.expect("Cannot serialize node");
|
.expect("Serializing node failed");
|
||||||
|
|
||||||
// FIXME(ajeffrey): Directly convert UTF8 to DOMString
|
// FIXME(ajeffrey): Directly convert UTF8 to DOMString
|
||||||
DOMString::from(String::from_utf8(writer).unwrap())
|
DOMString::from(String::from_utf8(writer).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <https://w3c.github.io/DOM-Parsing/#dfn-xml-serialization>
|
||||||
pub(crate) fn xml_serialize(
|
pub(crate) fn xml_serialize(
|
||||||
&self,
|
&self,
|
||||||
traversal_scope: xml_serialize::TraversalScope,
|
traversal_scope: xml_serialize::TraversalScope,
|
||||||
|
@ -2895,14 +2909,23 @@ impl Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#fragment-serializing-algorithm-steps>
|
/// <https://html.spec.whatwg.org/multipage/#fragment-serializing-algorithm-steps>
|
||||||
pub(crate) fn fragment_serialization_algorithm(&self, require_well_formed: bool) -> DOMString {
|
pub(crate) fn fragment_serialization_algorithm(
|
||||||
|
&self,
|
||||||
|
require_well_formed: bool,
|
||||||
|
can_gc: CanGc,
|
||||||
|
) -> DOMString {
|
||||||
// Step 1. Let context document be node's node document.
|
// Step 1. Let context document be node's node document.
|
||||||
let context_document = self.owner_document();
|
let context_document = self.owner_document();
|
||||||
|
|
||||||
// Step 2. If context document is an HTML document, return the result of HTML fragment serialization algorithm
|
// Step 2. If context document is an HTML document, return the result of HTML fragment serialization algorithm
|
||||||
// with node, false, and « ».
|
// with node, false, and « ».
|
||||||
if context_document.is_html_document() {
|
if context_document.is_html_document() {
|
||||||
return self.html_serialize(html_serialize::TraversalScope::ChildrenOnly(None));
|
return self.html_serialize(
|
||||||
|
html_serialize::TraversalScope::ChildrenOnly(None),
|
||||||
|
false,
|
||||||
|
vec![],
|
||||||
|
can_gc,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3. Return the XML serialization of node given require well-formed.
|
// Step 3. Return the XML serialization of node given require well-formed.
|
||||||
|
|
|
@ -7,16 +7,19 @@
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use html5ever::QualName;
|
|
||||||
use html5ever::buffer_queue::BufferQueue;
|
use html5ever::buffer_queue::BufferQueue;
|
||||||
use html5ever::serialize::TraversalScope::IncludeNode;
|
use html5ever::serialize::TraversalScope::IncludeNode;
|
||||||
use html5ever::serialize::{AttrRef, Serialize, Serializer, TraversalScope};
|
use html5ever::serialize::{AttrRef, Serialize, Serializer, TraversalScope};
|
||||||
use html5ever::tokenizer::{Tokenizer as HtmlTokenizer, TokenizerOpts, TokenizerResult};
|
use html5ever::tokenizer::{Tokenizer as HtmlTokenizer, TokenizerOpts, TokenizerResult};
|
||||||
use html5ever::tree_builder::{TreeBuilder, TreeBuilderOpts};
|
use html5ever::tree_builder::{TreeBuilder, TreeBuilderOpts};
|
||||||
|
use html5ever::{QualName, local_name, namespace_url, ns};
|
||||||
use script_bindings::trace::CustomTraceable;
|
use script_bindings::trace::CustomTraceable;
|
||||||
use servo_url::ServoUrl;
|
use servo_url::ServoUrl;
|
||||||
|
use xml5ever::LocalName;
|
||||||
|
|
||||||
use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
|
use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRootMode;
|
||||||
|
use crate::dom::bindings::codegen::GenericBindings::ShadowRootBinding::ShadowRoot_Binding::ShadowRootMethods;
|
||||||
use crate::dom::bindings::inheritance::{Castable, CharacterDataTypeId, NodeTypeId};
|
use crate::dom::bindings::inheritance::{Castable, CharacterDataTypeId, NodeTypeId};
|
||||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||||
use crate::dom::characterdata::CharacterData;
|
use crate::dom::characterdata::CharacterData;
|
||||||
|
@ -29,6 +32,7 @@ use crate::dom::htmltemplateelement::HTMLTemplateElement;
|
||||||
use crate::dom::node::Node;
|
use crate::dom::node::Node;
|
||||||
use crate::dom::processinginstruction::ProcessingInstruction;
|
use crate::dom::processinginstruction::ProcessingInstruction;
|
||||||
use crate::dom::servoparser::{ParsingAlgorithm, Sink};
|
use crate::dom::servoparser::{ParsingAlgorithm, Sink};
|
||||||
|
use crate::dom::shadowroot::ShadowRoot;
|
||||||
use crate::script_runtime::CanGc;
|
use crate::script_runtime::CanGc;
|
||||||
|
|
||||||
#[derive(JSTraceable, MallocSizeOf)]
|
#[derive(JSTraceable, MallocSizeOf)]
|
||||||
|
@ -121,54 +125,91 @@ fn start_element<S: Serializer>(node: &Element, serializer: &mut S) -> io::Resul
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end_element<S: Serializer>(node: &Element, serializer: &mut S) -> io::Result<()> {
|
|
||||||
let name = QualName::new(None, node.namespace().clone(), node.local_name().clone());
|
|
||||||
serializer.end_elem(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
enum SerializationCommand {
|
enum SerializationCommand {
|
||||||
OpenElement(DomRoot<Element>),
|
OpenElement(DomRoot<Element>),
|
||||||
CloseElement(DomRoot<Element>),
|
CloseElement(QualName),
|
||||||
SerializeNonelement(DomRoot<Node>),
|
SerializeNonelement(DomRoot<Node>),
|
||||||
|
SerializeShadowRoot(DomRoot<ShadowRoot>),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SerializationIterator {
|
struct SerializationIterator {
|
||||||
stack: Vec<SerializationCommand>,
|
stack: Vec<SerializationCommand>,
|
||||||
|
|
||||||
|
/// Whether or not shadow roots should be serialized
|
||||||
|
serialize_shadow_roots: bool,
|
||||||
|
|
||||||
|
/// List of shadow root objects that should be serialized
|
||||||
|
shadow_roots: Vec<DomRoot<ShadowRoot>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rev_children_iter(n: &Node, can_gc: CanGc) -> impl Iterator<Item = DomRoot<Node>> + use<'_> {
|
enum SerializationChildrenIterator<C, S> {
|
||||||
if n.downcast::<Element>().is_some_and(|e| e.is_void()) {
|
None,
|
||||||
return Node::new_document_node().rev_children();
|
Children(C),
|
||||||
}
|
ShadowContents(S),
|
||||||
|
|
||||||
match n.downcast::<HTMLTemplateElement>() {
|
|
||||||
Some(t) => t.Content(can_gc).upcast::<Node>().rev_children(),
|
|
||||||
None => n.rev_children(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SerializationIterator {
|
impl SerializationIterator {
|
||||||
fn new(node: &Node, skip_first: bool, can_gc: CanGc) -> SerializationIterator {
|
fn new(
|
||||||
let mut ret = SerializationIterator { stack: vec![] };
|
node: &Node,
|
||||||
|
skip_first: bool,
|
||||||
|
serialize_shadow_roots: bool,
|
||||||
|
shadow_roots: Vec<DomRoot<ShadowRoot>>,
|
||||||
|
can_gc: CanGc,
|
||||||
|
) -> SerializationIterator {
|
||||||
|
let mut ret = SerializationIterator {
|
||||||
|
stack: vec![],
|
||||||
|
serialize_shadow_roots,
|
||||||
|
shadow_roots,
|
||||||
|
};
|
||||||
if skip_first || node.is::<DocumentFragment>() || node.is::<Document>() {
|
if skip_first || node.is::<DocumentFragment>() || node.is::<Document>() {
|
||||||
for c in rev_children_iter(node, can_gc) {
|
ret.handle_node_contents(node, can_gc);
|
||||||
ret.push_node(&c);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
ret.push_node(node);
|
ret.push_node(node);
|
||||||
}
|
}
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_node(&mut self, n: &Node) {
|
fn handle_node_contents(&mut self, node: &Node, can_gc: CanGc) {
|
||||||
match n.downcast::<Element>() {
|
if node.downcast::<Element>().is_some_and(Element::is_void) {
|
||||||
Some(e) => self
|
return;
|
||||||
.stack
|
|
||||||
.push(SerializationCommand::OpenElement(DomRoot::from_ref(e))),
|
|
||||||
None => self.stack.push(SerializationCommand::SerializeNonelement(
|
|
||||||
DomRoot::from_ref(n),
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(template_element) = node.downcast::<HTMLTemplateElement>() {
|
||||||
|
for child in template_element
|
||||||
|
.Content(can_gc)
|
||||||
|
.upcast::<Node>()
|
||||||
|
.rev_children()
|
||||||
|
{
|
||||||
|
self.push_node(&child);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for child in node.rev_children() {
|
||||||
|
self.push_node(&child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(shadow_root) = node.downcast::<Element>().and_then(Element::shadow_root) {
|
||||||
|
let should_be_serialized = (self.serialize_shadow_roots && shadow_root.Serializable()) ||
|
||||||
|
self.shadow_roots.contains(&shadow_root);
|
||||||
|
if !shadow_root.is_user_agent_widget() && should_be_serialized {
|
||||||
|
self.stack
|
||||||
|
.push(SerializationCommand::SerializeShadowRoot(shadow_root));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_node(&mut self, node: &Node) {
|
||||||
|
let Some(element) = node.downcast::<Element>() else {
|
||||||
|
self.stack.push(SerializationCommand::SerializeNonelement(
|
||||||
|
DomRoot::from_ref(node),
|
||||||
|
));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
self.stack
|
||||||
|
.push(SerializationCommand::OpenElement(DomRoot::from_ref(
|
||||||
|
element,
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,71 +217,139 @@ impl Iterator for SerializationIterator {
|
||||||
type Item = SerializationCommand;
|
type Item = SerializationCommand;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<SerializationCommand> {
|
fn next(&mut self) -> Option<SerializationCommand> {
|
||||||
let res = self.stack.pop();
|
let res = self.stack.pop()?;
|
||||||
|
|
||||||
if let Some(SerializationCommand::OpenElement(ref e)) = res {
|
match &res {
|
||||||
self.stack
|
SerializationCommand::OpenElement(element) => {
|
||||||
.push(SerializationCommand::CloseElement(e.clone()));
|
let name = QualName::new(
|
||||||
for c in rev_children_iter(e.upcast::<Node>(), CanGc::note()) {
|
None,
|
||||||
self.push_node(&c);
|
element.namespace().clone(),
|
||||||
}
|
element.local_name().clone(),
|
||||||
|
);
|
||||||
|
self.stack.push(SerializationCommand::CloseElement(name));
|
||||||
|
self.handle_node_contents(element.upcast(), CanGc::note());
|
||||||
|
},
|
||||||
|
SerializationCommand::SerializeShadowRoot(shadow_root) => {
|
||||||
|
self.stack
|
||||||
|
.push(SerializationCommand::CloseElement(QualName::new(
|
||||||
|
None,
|
||||||
|
ns!(),
|
||||||
|
local_name!("template"),
|
||||||
|
)));
|
||||||
|
self.handle_node_contents(shadow_root.upcast(), CanGc::note());
|
||||||
|
},
|
||||||
|
_ => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
Some(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#html-fragment-serialisation-algorithm>
|
||||||
|
pub(crate) fn serialize_html_fragment<S: Serializer>(
|
||||||
|
node: &Node,
|
||||||
|
serializer: &mut S,
|
||||||
|
traversal_scope: TraversalScope,
|
||||||
|
serialize_shadow_roots: bool,
|
||||||
|
shadow_roots: Vec<DomRoot<ShadowRoot>>,
|
||||||
|
can_gc: CanGc,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
let iter = SerializationIterator::new(
|
||||||
|
node,
|
||||||
|
traversal_scope != IncludeNode,
|
||||||
|
serialize_shadow_roots,
|
||||||
|
shadow_roots,
|
||||||
|
can_gc,
|
||||||
|
);
|
||||||
|
|
||||||
|
for cmd in iter {
|
||||||
|
match cmd {
|
||||||
|
SerializationCommand::OpenElement(n) => {
|
||||||
|
start_element(&n, serializer)?;
|
||||||
|
},
|
||||||
|
SerializationCommand::CloseElement(name) => {
|
||||||
|
serializer.end_elem(name)?;
|
||||||
|
},
|
||||||
|
SerializationCommand::SerializeNonelement(n) => match n.type_id() {
|
||||||
|
NodeTypeId::DocumentType => {
|
||||||
|
let doctype = n.downcast::<DocumentType>().unwrap();
|
||||||
|
serializer.write_doctype(doctype.name())?;
|
||||||
|
},
|
||||||
|
|
||||||
|
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => {
|
||||||
|
let cdata = n.downcast::<CharacterData>().unwrap();
|
||||||
|
serializer.write_text(&cdata.data())?;
|
||||||
|
},
|
||||||
|
|
||||||
|
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
|
||||||
|
let cdata = n.downcast::<CharacterData>().unwrap();
|
||||||
|
serializer.write_comment(&cdata.data())?;
|
||||||
|
},
|
||||||
|
|
||||||
|
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
|
||||||
|
let pi = n.downcast::<ProcessingInstruction>().unwrap();
|
||||||
|
let data = pi.upcast::<CharacterData>().data();
|
||||||
|
serializer.write_processing_instruction(pi.target(), &data)?;
|
||||||
|
},
|
||||||
|
|
||||||
|
NodeTypeId::DocumentFragment(_) => {},
|
||||||
|
|
||||||
|
NodeTypeId::Document(_) => panic!("Can't serialize Document node itself"),
|
||||||
|
NodeTypeId::Element(_) => panic!("Element shouldn't appear here"),
|
||||||
|
NodeTypeId::Attr => panic!("Attr shouldn't appear here"),
|
||||||
|
},
|
||||||
|
SerializationCommand::SerializeShadowRoot(shadow_root) => {
|
||||||
|
// Shadow roots are serialized as template elements with a fixed set of
|
||||||
|
// attributes. Because these template elements don't actually exist in the DOM
|
||||||
|
// we have to make up a vector of attributes ourselves.
|
||||||
|
let mut attributes = vec![];
|
||||||
|
let mut push_attribute = |name, value| {
|
||||||
|
let qualified_name = QualName::new(None, ns!(), LocalName::from(name));
|
||||||
|
attributes.push((qualified_name, value))
|
||||||
|
};
|
||||||
|
|
||||||
|
let mode = if shadow_root.Mode() == ShadowRootMode::Open {
|
||||||
|
"open"
|
||||||
|
} else {
|
||||||
|
"closed"
|
||||||
|
};
|
||||||
|
push_attribute("shadowrootmode", mode);
|
||||||
|
|
||||||
|
if shadow_root.DelegatesFocus() {
|
||||||
|
push_attribute("shadowrootdelegatesfocus", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
if shadow_root.Serializable() {
|
||||||
|
push_attribute("shadowrootserializable", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
if shadow_root.Clonable() {
|
||||||
|
push_attribute("shadowrootclonable", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
let name = QualName::new(None, ns!(), local_name!("template"));
|
||||||
|
serializer.start_elem(name, attributes.iter().map(|(a, b)| (a, *b)))?;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: This trait confuses the concepts of XML serialization and HTML serialization and
|
||||||
|
// the impl should go away eventually
|
||||||
impl Serialize for &Node {
|
impl Serialize for &Node {
|
||||||
fn serialize<S: Serializer>(
|
fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()>
|
||||||
&self,
|
where
|
||||||
serializer: &mut S,
|
S: Serializer,
|
||||||
traversal_scope: TraversalScope,
|
{
|
||||||
) -> io::Result<()> {
|
serialize_html_fragment(
|
||||||
let node = *self;
|
self,
|
||||||
|
serializer,
|
||||||
let iter = SerializationIterator::new(node, traversal_scope != IncludeNode, CanGc::note());
|
traversal_scope,
|
||||||
|
false,
|
||||||
for cmd in iter {
|
vec![],
|
||||||
match cmd {
|
CanGc::note(),
|
||||||
SerializationCommand::OpenElement(n) => {
|
)
|
||||||
start_element(&n, serializer)?;
|
|
||||||
},
|
|
||||||
|
|
||||||
SerializationCommand::CloseElement(n) => {
|
|
||||||
end_element(&n, serializer)?;
|
|
||||||
},
|
|
||||||
|
|
||||||
SerializationCommand::SerializeNonelement(n) => match n.type_id() {
|
|
||||||
NodeTypeId::DocumentType => {
|
|
||||||
let doctype = n.downcast::<DocumentType>().unwrap();
|
|
||||||
serializer.write_doctype(doctype.name())?;
|
|
||||||
},
|
|
||||||
|
|
||||||
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => {
|
|
||||||
let cdata = n.downcast::<CharacterData>().unwrap();
|
|
||||||
serializer.write_text(&cdata.data())?;
|
|
||||||
},
|
|
||||||
|
|
||||||
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
|
|
||||||
let cdata = n.downcast::<CharacterData>().unwrap();
|
|
||||||
serializer.write_comment(&cdata.data())?;
|
|
||||||
},
|
|
||||||
|
|
||||||
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
|
|
||||||
let pi = n.downcast::<ProcessingInstruction>().unwrap();
|
|
||||||
let data = pi.upcast::<CharacterData>().data();
|
|
||||||
serializer.write_processing_instruction(pi.target(), &data)?;
|
|
||||||
},
|
|
||||||
|
|
||||||
NodeTypeId::DocumentFragment(_) => {},
|
|
||||||
|
|
||||||
NodeTypeId::Document(_) => panic!("Can't serialize Document node itself"),
|
|
||||||
NodeTypeId::Element(_) => panic!("Element shouldn't appear here"),
|
|
||||||
NodeTypeId::Attr => panic!("Attr shouldn't appear here"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,6 +81,8 @@ mod html;
|
||||||
mod prefetch;
|
mod prefetch;
|
||||||
mod xml;
|
mod xml;
|
||||||
|
|
||||||
|
pub(crate) use html::serialize_html_fragment;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
/// The parser maintains two input streams: one for input from script through
|
/// The parser maintains two input streams: one for input from script through
|
||||||
/// document.write(), and one for input from network.
|
/// document.write(), and one for input from network.
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::collections::HashMap;
|
||||||
use std::collections::hash_map::Entry;
|
use std::collections::hash_map::Entry;
|
||||||
|
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use html5ever::serialize::TraversalScope;
|
||||||
use servo_arc::Arc;
|
use servo_arc::Arc;
|
||||||
use style::author_styles::AuthorStyles;
|
use style::author_styles::AuthorStyles;
|
||||||
use style::dom::TElement;
|
use style::dom::TElement;
|
||||||
|
@ -17,6 +18,7 @@ use stylo_atoms::Atom;
|
||||||
|
|
||||||
use crate::conversions::Convert;
|
use crate::conversions::Convert;
|
||||||
use crate::dom::bindings::cell::DomRefCell;
|
use crate::dom::bindings::cell::DomRefCell;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::ElementBinding::GetHTMLOptions;
|
||||||
use crate::dom::bindings::codegen::Bindings::HTMLSlotElementBinding::HTMLSlotElement_Binding::HTMLSlotElementMethods;
|
use crate::dom::bindings::codegen::Bindings::HTMLSlotElementBinding::HTMLSlotElement_Binding::HTMLSlotElementMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRoot_Binding::ShadowRootMethods;
|
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRoot_Binding::ShadowRootMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{
|
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{
|
||||||
|
@ -405,11 +407,24 @@ impl ShadowRootMethods<crate::DomTypeHolder> for ShadowRoot {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#dom-shadowroot-gethtml>
|
||||||
|
fn GetHTML(&self, options: &GetHTMLOptions, can_gc: CanGc) -> DOMString {
|
||||||
|
// > ShadowRoot's getHTML(options) method steps are to return the result of HTML fragment serialization
|
||||||
|
// > algorithm with this, options["serializableShadowRoots"], and options["shadowRoots"].
|
||||||
|
self.upcast::<Node>().html_serialize(
|
||||||
|
TraversalScope::ChildrenOnly(None),
|
||||||
|
options.serializableShadowRoots,
|
||||||
|
options.shadowRoots.clone(),
|
||||||
|
can_gc,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-shadowroot-innerhtml>
|
/// <https://html.spec.whatwg.org/multipage/#dom-shadowroot-innerhtml>
|
||||||
fn InnerHTML(&self) -> DOMString {
|
fn InnerHTML(&self, can_gc: CanGc) -> DOMString {
|
||||||
// ShadowRoot's innerHTML getter steps are to return the result of running fragment serializing
|
// ShadowRoot's innerHTML getter steps are to return the result of running fragment serializing
|
||||||
// algorithm steps with this and true.
|
// algorithm steps with this and true.
|
||||||
self.upcast::<Node>().fragment_serialization_algorithm(true)
|
self.upcast::<Node>()
|
||||||
|
.fragment_serialization_algorithm(true, can_gc)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-shadowroot-innerhtml>
|
/// <https://html.spec.whatwg.org/multipage/#dom-shadowroot-innerhtml>
|
||||||
|
|
|
@ -804,7 +804,7 @@ pub(crate) fn handle_get_page_source(
|
||||||
.find_document(pipeline)
|
.find_document(pipeline)
|
||||||
.ok_or(ErrorStatus::UnknownError)
|
.ok_or(ErrorStatus::UnknownError)
|
||||||
.and_then(|document| match document.GetDocumentElement() {
|
.and_then(|document| match document.GetDocumentElement() {
|
||||||
Some(element) => match element.GetOuterHTML() {
|
Some(element) => match element.GetOuterHTML(can_gc) {
|
||||||
Ok(source) => Ok(source.to_string()),
|
Ok(source) => Ok(source.to_string()),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
match XMLSerializer::new(document.window(), None, can_gc)
|
match XMLSerializer::new(document.window(), None, can_gc)
|
||||||
|
|
|
@ -190,7 +190,7 @@ DOMInterfaces = {
|
||||||
},
|
},
|
||||||
|
|
||||||
'Element': {
|
'Element': {
|
||||||
'canGc': ['SetHTMLUnsafe', 'SetInnerHTML', 'SetOuterHTML', 'InsertAdjacentHTML', 'GetClientRects', 'GetBoundingClientRect', 'InsertAdjacentText', 'ToggleAttribute', 'SetAttribute', 'SetAttributeNS', 'SetId','SetClassName','Prepend','Append','ReplaceChildren','Before','After','ReplaceWith', 'SetRole', 'SetAriaAtomic', 'SetAriaAutoComplete', 'SetAriaBrailleLabel', 'SetAriaBrailleRoleDescription', 'SetAriaBusy', 'SetAriaChecked', 'SetAriaColCount', 'SetAriaColIndex', 'SetAriaColIndexText', 'SetAriaColSpan', 'SetAriaCurrent', 'SetAriaDescription', 'SetAriaDisabled', 'SetAriaExpanded', 'SetAriaHasPopup', 'SetAriaHidden', 'SetAriaInvalid', 'SetAriaKeyShortcuts', 'SetAriaLabel', 'SetAriaLevel', 'SetAriaLive', 'SetAriaModal', 'SetAriaMultiLine', 'SetAriaMultiSelectable', 'SetAriaOrientation', 'SetAriaPlaceholder', 'SetAriaPosInSet', 'SetAriaPressed','SetAriaReadOnly', 'SetAriaRelevant', 'SetAriaRequired', 'SetAriaRoleDescription', 'SetAriaRowCount', 'SetAriaRowIndex', 'SetAriaRowIndexText', 'SetAriaRowSpan', 'SetAriaSelected', 'SetAriaSetSize','SetAriaSort', 'SetAriaValueMax', 'SetAriaValueMin', 'SetAriaValueNow', 'SetAriaValueText', 'SetScrollTop', 'SetScrollLeft', 'Scroll', 'Scroll_', 'ScrollBy', 'ScrollBy_', 'ScrollWidth', 'ScrollHeight', 'ScrollTop', 'ScrollLeft', 'ClientTop', 'ClientLeft', 'ClientWidth', 'ClientHeight', 'RequestFullscreen'],
|
'canGc': ['SetHTMLUnsafe', 'SetInnerHTML', 'SetOuterHTML', 'InsertAdjacentHTML', 'GetClientRects', 'GetBoundingClientRect', 'InsertAdjacentText', 'ToggleAttribute', 'SetAttribute', 'SetAttributeNS', 'SetId','SetClassName','Prepend','Append','ReplaceChildren','Before','After','ReplaceWith', 'SetRole', 'SetAriaAtomic', 'SetAriaAutoComplete', 'SetAriaBrailleLabel', 'SetAriaBrailleRoleDescription', 'SetAriaBusy', 'SetAriaChecked', 'SetAriaColCount', 'SetAriaColIndex', 'SetAriaColIndexText', 'SetAriaColSpan', 'SetAriaCurrent', 'SetAriaDescription', 'SetAriaDisabled', 'SetAriaExpanded', 'SetAriaHasPopup', 'SetAriaHidden', 'SetAriaInvalid', 'SetAriaKeyShortcuts', 'SetAriaLabel', 'SetAriaLevel', 'SetAriaLive', 'SetAriaModal', 'SetAriaMultiLine', 'SetAriaMultiSelectable', 'SetAriaOrientation', 'SetAriaPlaceholder', 'SetAriaPosInSet', 'SetAriaPressed','SetAriaReadOnly', 'SetAriaRelevant', 'SetAriaRequired', 'SetAriaRoleDescription', 'SetAriaRowCount', 'SetAriaRowIndex', 'SetAriaRowIndexText', 'SetAriaRowSpan', 'SetAriaSelected', 'SetAriaSetSize','SetAriaSort', 'SetAriaValueMax', 'SetAriaValueMin', 'SetAriaValueNow', 'SetAriaValueText', 'SetScrollTop', 'SetScrollLeft', 'Scroll', 'Scroll_', 'ScrollBy', 'ScrollBy_', 'ScrollWidth', 'ScrollHeight', 'ScrollTop', 'ScrollLeft', 'ClientTop', 'ClientLeft', 'ClientWidth', 'ClientHeight', 'RequestFullscreen', 'GetHTML', 'GetInnerHTML', 'GetOuterHTML'],
|
||||||
},
|
},
|
||||||
|
|
||||||
'ElementInternals': {
|
'ElementInternals': {
|
||||||
|
@ -530,7 +530,7 @@ DOMInterfaces = {
|
||||||
},
|
},
|
||||||
|
|
||||||
'ShadowRoot': {
|
'ShadowRoot': {
|
||||||
'canGc': ['ElementFromPoint', 'ElementsFromPoint', 'SetInnerHTML'],
|
'canGc': ['ElementFromPoint', 'ElementsFromPoint', 'SetInnerHTML', 'GetHTML', 'InnerHTML'],
|
||||||
},
|
},
|
||||||
|
|
||||||
'StaticRange': {
|
'StaticRange': {
|
||||||
|
|
|
@ -120,14 +120,20 @@ partial interface Element {
|
||||||
readonly attribute long clientHeight;
|
readonly attribute long clientHeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://w3c.github.io/DOM-Parsing/#extensions-to-the-element-interface
|
// https://html.spec.whatwg.org/multipage/#dom-parsing-and-serialization
|
||||||
partial interface Element {
|
partial interface Element {
|
||||||
[CEReactions] undefined setHTMLUnsafe(DOMString html);
|
[CEReactions] undefined setHTMLUnsafe(DOMString html);
|
||||||
|
DOMString getHTML(optional GetHTMLOptions options = {});
|
||||||
|
|
||||||
[CEReactions, Throws] attribute [LegacyNullToEmptyString] DOMString innerHTML;
|
[CEReactions, Throws] attribute [LegacyNullToEmptyString] DOMString innerHTML;
|
||||||
[CEReactions, Throws] attribute [LegacyNullToEmptyString] DOMString outerHTML;
|
[CEReactions, Throws] attribute [LegacyNullToEmptyString] DOMString outerHTML;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
dictionary GetHTMLOptions {
|
||||||
|
boolean serializableShadowRoots = false;
|
||||||
|
sequence<ShadowRoot> shadowRoots = [];
|
||||||
|
};
|
||||||
|
|
||||||
// https://fullscreen.spec.whatwg.org/#api
|
// https://fullscreen.spec.whatwg.org/#api
|
||||||
partial interface Element {
|
partial interface Element {
|
||||||
Promise<undefined> requestFullscreen();
|
Promise<undefined> requestFullscreen();
|
||||||
|
|
|
@ -26,7 +26,7 @@ ShadowRoot includes DocumentOrShadowRoot;
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-parsing-and-serialization
|
// https://html.spec.whatwg.org/multipage/#dom-parsing-and-serialization
|
||||||
partial interface ShadowRoot {
|
partial interface ShadowRoot {
|
||||||
// [CEReactions] undefined setHTMLUnsafe((TrustedHTML or DOMString) html);
|
// [CEReactions] undefined setHTMLUnsafe((TrustedHTML or DOMString) html);
|
||||||
// DOMString getHTML(optional GetHTMLOptions options = {});
|
DOMString getHTML(optional GetHTMLOptions options = {});
|
||||||
|
|
||||||
// [CEReactions] attribute (TrustedHTML or [LegacyNullToEmptyString] DOMString) innerHTML;
|
// [CEReactions] attribute (TrustedHTML or [LegacyNullToEmptyString] DOMString) innerHTML;
|
||||||
[CEReactions] attribute [LegacyNullToEmptyString] DOMString innerHTML;
|
[CEReactions] attribute [LegacyNullToEmptyString] DOMString innerHTML;
|
||||||
|
|
|
@ -4613,12 +4613,6 @@
|
||||||
[DOMStringList interface: calling contains(DOMString) on location.ancestorOrigins with too few arguments must throw TypeError]
|
[DOMStringList interface: calling contains(DOMString) on location.ancestorOrigins with too few arguments must throw TypeError]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Element interface: document.createElement("noscript") must inherit property "getHTML(optional GetHTMLOptions)" with the proper type]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Element interface: calling getHTML(optional GetHTMLOptions) on document.createElement("noscript") with too few arguments must throw TypeError]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Element interface: document.createElement("noscript") must inherit property "innerHTML" with the proper type]
|
[Element interface: document.createElement("noscript") must inherit property "innerHTML" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
@ -5918,12 +5912,6 @@
|
||||||
[ShadowRoot interface: operation setHTMLUnsafe((TrustedHTML or DOMString))]
|
[ShadowRoot interface: operation setHTMLUnsafe((TrustedHTML or DOMString))]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[ShadowRoot interface: operation getHTML(optional GetHTMLOptions)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Element interface: operation getHTML(optional GetHTMLOptions)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[OffscreenCanvasRenderingContext2D interface: operation getContextAttributes()]
|
[OffscreenCanvasRenderingContext2D interface: operation getContextAttributes()]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
@ -5966,12 +5954,6 @@
|
||||||
[OffscreenCanvasRenderingContext2D interface: attribute lang]
|
[OffscreenCanvasRenderingContext2D interface: attribute lang]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Element interface: document.createElement("div") must inherit property "getHTML(optional GetHTMLOptions)" with the proper type]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Element interface: calling getHTML(optional GetHTMLOptions) on document.createElement("div") with too few arguments must throw TypeError]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Element interface: document.createElement("div") must inherit property "innerHTML" with the proper type]
|
[Element interface: document.createElement("div") must inherit property "innerHTML" with the proper type]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,6 @@
|
||||||
[gethtml-ordering.html]
|
[gethtml-ordering.html]
|
||||||
expected: ERROR
|
[template position]
|
||||||
|
expected: FAIL
|
||||||
|
|
||||||
|
[both template and attribute position]
|
||||||
|
expected: FAIL
|
||||||
|
|
15816
tests/wpt/meta/shadow-dom/declarative/gethtml.html.ini
vendored
15816
tests/wpt/meta/shadow-dom/declarative/gethtml.html.ini
vendored
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue