mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Auto merge of #18888 - tigercosmos:xml, r=jdm
fix #18776: use XML fragment serialization for innerHTML in XML documents <!-- Please describe your changes on the following line: --> I am not sure whether my solution is in the right way. @jdm Can you give some advises? --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #18776 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18888) <!-- Reviewable:end -->
This commit is contained in:
commit
adb45eb727
4 changed files with 29 additions and 315 deletions
|
@ -120,6 +120,11 @@ use style::values::{CSSFloat, Either};
|
|||
use style::values::{specified, computed};
|
||||
use stylesheet_loader::StylesheetOwner;
|
||||
use task::TaskOnce;
|
||||
use xml5ever::serialize as xmlSerialize;
|
||||
use xml5ever::serialize::SerializeOpts as XmlSerializeOpts;
|
||||
use xml5ever::serialize::TraversalScope as XmlTraversalScope;
|
||||
use xml5ever::serialize::TraversalScope::ChildrenOnly as XmlChildrenOnly;
|
||||
use xml5ever::serialize::TraversalScope::IncludeNode as XmlIncludeNode;
|
||||
|
||||
// TODO: Update focus state when the top-level browsing context gains or loses system focus,
|
||||
// and when the element enters or leaves a browsing context container.
|
||||
|
@ -1004,6 +1009,19 @@ impl Element {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn xmlSerialize(&self, traversal_scope: XmlTraversalScope) -> Fallible<DOMString> {
|
||||
let mut writer = vec![];
|
||||
match xmlSerialize::serialize(&mut writer,
|
||||
&self.upcast::<Node>(),
|
||||
XmlSerializeOpts {
|
||||
traversal_scope: traversal_scope,
|
||||
..Default::default()
|
||||
}) {
|
||||
Ok(()) => Ok(DOMString::from(String::from_utf8(writer).unwrap())),
|
||||
Err(_) => panic!("Cannot serialize element"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn root_element(&self) -> DomRoot<Element> {
|
||||
if self.node.is_in_doc() {
|
||||
self.upcast::<Node>()
|
||||
|
@ -2047,11 +2065,14 @@ impl ElementMethods for Element {
|
|||
|
||||
/// https://w3c.github.io/DOM-Parsing/#widl-Element-innerHTML
|
||||
fn GetInnerHTML(&self) -> Fallible<DOMString> {
|
||||
// XXX TODO: XML case
|
||||
let qname = QualName::new(self.prefix().clone(),
|
||||
self.namespace().clone(),
|
||||
self.local_name().clone());
|
||||
self.serialize(ChildrenOnly(Some(qname)))
|
||||
if document_from_node(self).is_html_document() {
|
||||
return self.serialize(ChildrenOnly(Some(qname)));
|
||||
} else {
|
||||
return self.xmlSerialize(XmlChildrenOnly(Some(qname)));
|
||||
}
|
||||
}
|
||||
|
||||
/// https://w3c.github.io/DOM-Parsing/#widl-Element-innerHTML
|
||||
|
@ -2071,7 +2092,11 @@ impl ElementMethods for Element {
|
|||
|
||||
// https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#widl-Element-outerHTML
|
||||
fn GetOuterHTML(&self) -> Fallible<DOMString> {
|
||||
self.serialize(IncludeNode)
|
||||
if document_from_node(self).is_html_document() {
|
||||
return self.serialize(IncludeNode);
|
||||
} else {
|
||||
return self.xmlSerialize(XmlIncludeNode);
|
||||
}
|
||||
}
|
||||
|
||||
// https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue