mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
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:
commit
34fda66dfa
18 changed files with 106 additions and 112 deletions
32
components/script/dom/cdatasection.rs
Normal file
32
components/script/dom/cdatasection.rs
Normal 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,
|
||||
)
|
||||
}
|
||||
}
|
|
@ -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!(),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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!(),
|
||||
};
|
||||
|
|
|
@ -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())?;
|
||||
},
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
|
|
10
components/script/dom/webidls/CDATASection.webidl
Normal file
10
components/script/dom/webidls/CDATASection.webidl
Normal 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 {
|
||||
};
|
|
@ -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]
|
||||
|
|
|
@ -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};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue