Skip to content

Commit

Permalink
Auto merge of #22891 - georgeroman:implement_cdatasection, r=Manishearth
Browse files Browse the repository at this point in the history
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 -->
  • Loading branch information
bors-servo committed Mar 14, 2019
2 parents 85ed181 + 2983d9d commit 71d86b9
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 96 deletions.
9 changes: 6 additions & 3 deletions components/layout_thread/dom_wrapper.rs
Expand Up @@ -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::{
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions 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,
)
}
}
8 changes: 6 additions & 2 deletions components/script/dom/characterdata.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(),
Expand Down
17 changes: 17 additions & 0 deletions components/script/dom/document.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -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;
Expand Down
26 changes: 18 additions & 8 deletions components/script/dom/node.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
},
Expand All @@ -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()
},
Expand Down Expand Up @@ -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>() => {
Expand Down Expand Up @@ -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) =>
{
Expand Down Expand Up @@ -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),
}
}
Expand Down
14 changes: 8 additions & 6 deletions components/script/dom/range.rs
Expand Up @@ -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,
Expand All @@ -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())?;
Expand Down Expand Up @@ -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!(),
};
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/servoparser/html.rs
Expand Up @@ -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())?;
},
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/text.rs
Expand Up @@ -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),
}
Expand Down
10 changes: 10 additions & 0 deletions 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 {
};
2 changes: 2 additions & 0 deletions components/script/dom/webidls/Document.webidl
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion components/script/lib.rs
Expand Up @@ -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};
Expand Down

0 comments on commit 71d86b9

Please sign in to comment.