diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2019-03-18 17:22:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-18 17:22:55 -0400 |
commit | 34fda66dfa5528f2b10a873e9a67417c6986c712 (patch) | |
tree | 8994cc26cc41a45bbc189622871b4a1c0b378f18 /components/script/dom | |
parent | 0fac8f2f62aff22897a0ca657a840333827cabf0 (diff) | |
parent | 4b8282b3b164771e3351c2a85890167ab6d0ab7f (diff) | |
download | servo-34fda66dfa5528f2b10a873e9a67417c6986c712.tar.gz servo-34fda66dfa5528f2b10a873e9a67417c6986c712.zip |
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 -->
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/cdatasection.rs | 32 | ||||
-rw-r--r-- | components/script/dom/characterdata.rs | 8 | ||||
-rw-r--r-- | components/script/dom/document.rs | 17 | ||||
-rw-r--r-- | components/script/dom/mod.rs | 1 | ||||
-rw-r--r-- | components/script/dom/node.rs | 26 | ||||
-rw-r--r-- | components/script/dom/range.rs | 14 | ||||
-rw-r--r-- | components/script/dom/servoparser/html.rs | 2 | ||||
-rw-r--r-- | components/script/dom/text.rs | 2 | ||||
-rw-r--r-- | components/script/dom/webidls/CDATASection.webidl | 10 | ||||
-rw-r--r-- | components/script/dom/webidls/Document.webidl | 2 |
10 files changed, 96 insertions, 18 deletions
diff --git a/components/script/dom/cdatasection.rs b/components/script/dom/cdatasection.rs new file mode 100644 index 00000000000..f4c788fd722 --- /dev/null +++ b/components/script/dom/cdatasection.rs @@ -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, + ) + } +} diff --git a/components/script/dom/characterdata.rs b/components/script/dom/characterdata.rs index 24d042a3c12..54f3ab0e360 100644 --- a/components/script/dom/characterdata.rs +++ b/components/script/dom/characterdata.rs @@ -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!(), diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 91ccc829719..b589d4e5b79 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -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) diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index b8f288a7e4e..e7ea25c565f 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -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; diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 7b5d4157bc0..3331686eef4 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -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), } } diff --git a/components/script/dom/range.rs b/components/script/dom/range.rs index 5a81ba595b6..630c4acd245 100644 --- a/components/script/dom/range.rs +++ b/components/script/dom/range.rs @@ -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!(), }; diff --git a/components/script/dom/servoparser/html.rs b/components/script/dom/servoparser/html.rs index 45ef625a5ee..fe6d06064f4 100644 --- a/components/script/dom/servoparser/html.rs +++ b/components/script/dom/servoparser/html.rs @@ -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())?; }, diff --git a/components/script/dom/text.rs b/components/script/dom/text.rs index 49deffb071a..a3b66583d83 100644 --- a/components/script/dom/text.rs +++ b/components/script/dom/text.rs @@ -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), } diff --git a/components/script/dom/webidls/CDATASection.webidl b/components/script/dom/webidls/CDATASection.webidl new file mode 100644 index 00000000000..cddd6edef6f --- /dev/null +++ b/components/script/dom/webidls/CDATASection.webidl @@ -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 { +}; diff --git a/components/script/dom/webidls/Document.webidl b/components/script/dom/webidls/Document.webidl index 91e82cfcd65..ccc3d339377 100644 --- a/components/script/dom/webidls/Document.webidl +++ b/components/script/dom/webidls/Document.webidl @@ -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] |