Auto merge of #22891 - georgeroman:implement_cdatasection, r=Manishearth

Implement CDATASection interface and createCDATASection method

<!-- Please describe your changes on the following line: -->

---
<!-- 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
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #22846

<!-- 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/22891)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-03-18 17:22:55 -04:00 committed by GitHub
commit 34fda66dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 106 additions and 112 deletions

View file

@ -40,7 +40,9 @@ use net_traits::image::base::{Image, ImageMetadata};
use range::Range;
use script::layout_exports::NodeFlags;
use script::layout_exports::PendingRestyle;
use script::layout_exports::{CharacterDataTypeId, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use script::layout_exports::{
CharacterDataTypeId, ElementTypeId, HTMLElementTypeId, NodeTypeId, TextTypeId,
};
use script::layout_exports::{Document, Element, Node, Text};
use script::layout_exports::{LayoutCharacterDataHelpers, LayoutDocumentHelpers};
use script::layout_exports::{
@ -153,7 +155,8 @@ impl<'ln> NodeInfo for ServoLayoutNode<'ln> {
}
fn is_text_node(&self) -> bool {
self.script_type_id() == NodeTypeId::CharacterData(CharacterDataTypeId::Text)
self.script_type_id() ==
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text))
}
}
@ -765,7 +768,7 @@ impl<'le> ::selectors::Element for ServoLayoutElement<'le> {
.dom_children()
.all(|node| match node.script_type_id() {
NodeTypeId::Element(..) => false,
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => unsafe {
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => unsafe {
node.node.downcast().unwrap().data_for_layout().is_empty()
},
_ => true,

View file

@ -0,0 +1,32 @@
/* 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::CDATASectionBinding;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::node::Node;
use crate::dom::text::Text;
use dom_struct::dom_struct;
#[dom_struct]
pub struct CDATASection {
text: Text,
}
impl CDATASection {
fn new_inherited(text: DOMString, document: &Document) -> CDATASection {
CDATASection {
text: Text::new_inherited(text, document),
}
}
pub fn new(text: DOMString, document: &Document) -> DomRoot<CDATASection> {
Node::reflect_node(
Box::new(CDATASection::new_inherited(text, document)),
document,
CDATASectionBinding::Wrap,
)
}
}

View file

@ -8,12 +8,13 @@ use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeBinding::NodeMethods;
use crate::dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods;
use crate::dom::bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId};
use crate::dom::bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId, TextTypeId};
use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{DomRoot, LayoutDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::cdatasection::CDATASection;
use crate::dom::comment::Comment;
use crate::dom::document::Document;
use crate::dom::element::Element;
@ -50,7 +51,10 @@ impl CharacterData {
let pi = self.downcast::<ProcessingInstruction>().unwrap();
DomRoot::upcast(ProcessingInstruction::new(pi.Target(), data, &document))
},
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => {
DomRoot::upcast(CDATASection::new(data, &document))
},
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => {
DomRoot::upcast(Text::new(data, &document))
},
_ => unreachable!(),

View file

@ -35,6 +35,7 @@ use crate::dom::bindings::xmlname::XMLName::InvalidXMLName;
use crate::dom::bindings::xmlname::{
namespace_from_domstring, validate_and_extract, xml_name_type,
};
use crate::dom::cdatasection::CDATASection;
use crate::dom::closeevent::CloseEvent;
use crate::dom::comment::Comment;
use crate::dom::compositionevent::CompositionEvent;
@ -3610,6 +3611,22 @@ impl DocumentMethods for Document {
Text::new(data, self)
}
// https://dom.spec.whatwg.org/#dom-document-createcdatasection
fn CreateCDATASection(&self, data: DOMString) -> Fallible<DomRoot<CDATASection>> {
// Step 1
if self.is_html_document {
return Err(Error::NotSupported);
}
// Step 2
if data.contains("]]>") {
return Err(Error::InvalidCharacter);
}
// Step 3
Ok(CDATASection::new(data, self))
}
// https://dom.spec.whatwg.org/#dom-document-createcomment
fn CreateComment(&self, data: DOMString) -> DomRoot<Comment> {
Comment::new(data, self)

View file

@ -245,6 +245,7 @@ pub mod bluetoothuuid;
pub mod canvasgradient;
pub mod canvaspattern;
pub mod canvasrenderingcontext2d;
pub mod cdatasection;
pub mod channelmergernode;
pub mod channelsplitternode;
pub mod characterdata;

View file

@ -19,7 +19,7 @@ use crate::dom::bindings::conversions::{self, DerivedFrom};
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::inheritance::{Castable, CharacterDataTypeId, ElementTypeId};
use crate::dom::bindings::inheritance::{EventTargetTypeId, HTMLElementTypeId, NodeTypeId};
use crate::dom::bindings::inheritance::{SVGElementTypeId, SVGGraphicsElementTypeId};
use crate::dom::bindings::inheritance::{SVGElementTypeId, SVGGraphicsElementTypeId, TextTypeId};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, DomSlice, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
@ -559,7 +559,7 @@ impl Node {
}
match self.type_id() {
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => self
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => self
.parent_node
.get()
.unwrap()
@ -1577,7 +1577,7 @@ impl Node {
// Step 4-5.
match node.type_id() {
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => {
if parent.is::<Document>() {
return Err(Error::HierarchyRequest);
}
@ -2081,7 +2081,12 @@ impl NodeMethods for Node {
// https://dom.spec.whatwg.org/#dom-node-nodetype
fn NodeType(&self) -> u16 {
match self.type_id() {
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => NodeConstants::TEXT_NODE,
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => {
NodeConstants::TEXT_NODE
},
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => {
NodeConstants::CDATA_SECTION_NODE
},
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
NodeConstants::PROCESSING_INSTRUCTION_NODE
},
@ -2097,7 +2102,12 @@ impl NodeMethods for Node {
fn NodeName(&self) -> DOMString {
match self.type_id() {
NodeTypeId::Element(..) => self.downcast::<Element>().unwrap().TagName(),
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => DOMString::from("#text"),
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => {
DOMString::from("#text")
},
NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => {
DOMString::from("#cdata-section")
},
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
self.downcast::<ProcessingInstruction>().unwrap().Target()
},
@ -2253,7 +2263,7 @@ impl NodeMethods for Node {
// Step 4-5.
match node.type_id() {
NodeTypeId::CharacterData(CharacterDataTypeId::Text) if self.is::<Document>() => {
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) if self.is::<Document>() => {
return Err(Error::HierarchyRequest);
},
NodeTypeId::DocumentType if !self.is::<Document>() => {
@ -2476,7 +2486,7 @@ impl NodeMethods for Node {
{
return false;
}
NodeTypeId::CharacterData(CharacterDataTypeId::Text) |
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) |
NodeTypeId::CharacterData(CharacterDataTypeId::Comment)
if !is_equal_characterdata(this, node) =>
{
@ -2931,7 +2941,7 @@ impl Into<LayoutNodeType> for NodeTypeId {
fn into(self) -> LayoutNodeType {
match self {
NodeTypeId::Element(e) => LayoutNodeType::Element(e.into()),
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => LayoutNodeType::Text,
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => LayoutNodeType::Text,
x => unreachable!("Layout should not traverse nodes of type {:?}", x),
}
}

View file

@ -707,14 +707,14 @@ impl RangeMethods for Range {
}
match start_node.type_id() {
// Handled under step 2.
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => (),
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => (),
NodeTypeId::CharacterData(_) => return Err(Error::HierarchyRequest),
_ => (),
}
// Step 2.
let (reference_node, parent) =
if start_node.type_id() == NodeTypeId::CharacterData(CharacterDataTypeId::Text) {
let (reference_node, parent) = match start_node.type_id() {
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => {
// Step 3.
let parent = match start_node.GetParentNode() {
Some(parent) => parent,
@ -723,11 +723,13 @@ impl RangeMethods for Range {
};
// Step 5.
(Some(DomRoot::from_ref(&*start_node)), parent)
} else {
},
_ => {
// Steps 4-5.
let child = start_node.ChildNodes().Item(start_offset);
(child, DomRoot::from_ref(&*start_node))
};
},
};
// Step 6.
Node::ensure_pre_insertion_validity(node, &parent, reference_node.deref())?;
@ -955,7 +957,7 @@ impl RangeMethods for Range {
NodeTypeId::Document(_) | NodeTypeId::DocumentFragment => None,
NodeTypeId::Element(_) => Some(DomRoot::downcast::<Element>(node).unwrap()),
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) |
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => node.GetParentElement(),
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => node.GetParentElement(),
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) |
NodeTypeId::DocumentType => unreachable!(),
};

View file

@ -232,7 +232,7 @@ impl<'a> Serialize for &'a Node {
serializer.write_doctype(&doctype.name())?;
},
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => {
let cdata = n.downcast::<CharacterData>().unwrap();
serializer.write_text(&cdata.data())?;
},

View file

@ -24,7 +24,7 @@ pub struct Text {
}
impl Text {
fn new_inherited(text: DOMString, document: &Document) -> Text {
pub fn new_inherited(text: DOMString, document: &Document) -> Text {
Text {
characterdata: CharacterData::new_inherited(text, document),
}

View file

@ -0,0 +1,10 @@
/* 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://dom.spec.whatwg.org/#interface-cdatasection
*/
interface CDATASection : Text {
};

View file

@ -40,6 +40,8 @@ interface Document : Node {
DocumentFragment createDocumentFragment();
[NewObject]
Text createTextNode(DOMString data);
[NewObject, Throws]
CDATASection createCDATASection(DOMString data);
[NewObject]
Comment createComment(DOMString data);
[NewObject, Throws]

View file

@ -84,7 +84,7 @@ mod webdriver_handlers;
/// TODO(emilio): A few of the FooHelpers can go away, presumably...
pub mod layout_exports {
pub use crate::dom::bindings::inheritance::{CharacterDataTypeId, ElementTypeId};
pub use crate::dom::bindings::inheritance::{HTMLElementTypeId, NodeTypeId};
pub use crate::dom::bindings::inheritance::{HTMLElementTypeId, NodeTypeId, TextTypeId};
pub use crate::dom::bindings::root::LayoutDom;
pub use crate::dom::characterdata::LayoutCharacterDataHelpers;
pub use crate::dom::document::{Document, LayoutDocumentHelpers, PendingRestyle};

View file

@ -99,21 +99,6 @@
[DOMTokenList interface: calling supports(DOMString) on document.body.classList with too few arguments must throw TypeError]
expected: FAIL
[Document interface: operation createCDATASection(DOMString)]
expected: FAIL
[Document interface: new Document() must inherit property "createCDATASection" with the proper type (18)]
expected: FAIL
[Document interface: calling createCDATASection(DOMString) on new Document() with too few arguments must throw TypeError]
expected: FAIL
[Document interface: xmlDoc must inherit property "createCDATASection" with the proper type (18)]
expected: FAIL
[Document interface: calling createCDATASection(DOMString) on xmlDoc with too few arguments must throw TypeError]
expected: FAIL
[Attr interface: existence and properties of interface object]
expected: FAIL
@ -297,21 +282,6 @@
[EventTarget interface: calling dispatchEvent(Event) on document.querySelector("[id\]").attributes[0\] with too few arguments must throw TypeError]
expected: FAIL
[CDATASection interface: existence and properties of interface object]
expected: FAIL
[CDATASection interface object length]
expected: FAIL
[CDATASection interface object name]
expected: FAIL
[CDATASection interface: existence and properties of interface prototype object]
expected: FAIL
[CDATASection interface: existence and properties of interface prototype object's "constructor" property]
expected: FAIL
[Node interface: document.querySelector("[id\]").attributes[0\] must inherit property "getRootNode" with the proper type (17)]
expected: FAIL
@ -504,18 +474,12 @@
[Document interface: new Document() must inherit property "origin" with the proper type]
expected: FAIL
[Document interface: new Document() must inherit property "createCDATASection(DOMString)" with the proper type]
expected: FAIL
[Node interface: new Document() must inherit property "isConnected" with the proper type]
expected: FAIL
[Document interface: xmlDoc must inherit property "origin" with the proper type]
expected: FAIL
[Document interface: xmlDoc must inherit property "createCDATASection(DOMString)" with the proper type]
expected: FAIL
[Node interface: xmlDoc must inherit property "isConnected" with the proper type]
expected: FAIL
@ -795,9 +759,6 @@
[Unscopable handled correctly for remove() on CharacterData]
expected: FAIL
[CDATASection interface: existence and properties of interface prototype object's @@unscopables property]
expected: FAIL
[AbstractRange interface: existence and properties of interface object]
expected: FAIL
@ -1001,9 +962,6 @@
[Document interface: attribute origin]
expected: FAIL
[Document interface: operation createCDATASection(DOMString)]
expected: FAIL
[Unscopable handled correctly for prepend([object Object\],[object Object\]) on Document]
expected: FAIL
@ -1013,21 +971,9 @@
[Document interface: new Document() must inherit property "origin" with the proper type]
expected: FAIL
[Document interface: new Document() must inherit property "createCDATASection(DOMString)" with the proper type]
expected: FAIL
[Document interface: calling createCDATASection(DOMString) on new Document() with too few arguments must throw TypeError]
expected: FAIL
[Document interface: xmlDoc must inherit property "origin" with the proper type]
expected: FAIL
[Document interface: xmlDoc must inherit property "createCDATASection(DOMString)" with the proper type]
expected: FAIL
[Document interface: calling createCDATASection(DOMString) on xmlDoc with too few arguments must throw TypeError]
expected: FAIL
[DocumentType interface: existence and properties of interface prototype object's @@unscopables property]
expected: FAIL
@ -1172,24 +1118,6 @@
[Text interface: document.createTextNode("abc") must inherit property "assignedSlot" with the proper type]
expected: FAIL
[CDATASection interface: existence and properties of interface object]
expected: FAIL
[CDATASection interface object length]
expected: FAIL
[CDATASection interface object name]
expected: FAIL
[CDATASection interface: existence and properties of interface prototype object]
expected: FAIL
[CDATASection interface: existence and properties of interface prototype object's "constructor" property]
expected: FAIL
[CDATASection interface: existence and properties of interface prototype object's @@unscopables property]
expected: FAIL
[AbstractRange interface: existence and properties of interface object]
expected: FAIL

View file

@ -1,4 +0,0 @@
[Node-insertBefore.html]
[Should check the 'parent' type before checking whether 'child' is a child of 'parent']
expected: FAIL

View file

@ -1,7 +0,0 @@
[Node-replaceChild.html]
[If the context node is not a node that can contain children, a HierarchyRequestError exception should be thrown]
expected: FAIL
[Should check the 'parent' type before checking whether 'child' is a child of 'parent']
expected: FAIL

View file

@ -1,5 +0,0 @@
[value-defaultValue-textContent-xhtml.xhtml]
type: testharness
[defaultValue and value include CDATASection Text nodes]
expected: FAIL

View file

@ -13281,7 +13281,7 @@
],
"mozilla/interfaces.html": [
[
"/_mozilla/mozilla/interfaces.html",
"mozilla/interfaces.html",
{}
]
],
@ -20239,7 +20239,7 @@
"testharness"
],
"mozilla/interfaces.html": [
"ab4ead5cfb5afc8b65cb71841e32ee46ec75318b",
"f8d8ab879f84bf03f89b12d5d010c83c1025265a",
"testharness"
],
"mozilla/interfaces.js": [

View file

@ -31,6 +31,7 @@ test_interfaces([
"CanvasGradient",
"CanvasRenderingContext2D",
"CanvasPattern",
"CDATASection",
"ChannelMergerNode",
"ChannelSplitterNode",
"CharacterData",