mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Implement innerHTML getter for HTML documents
XML case is not yet implemented.
This commit is contained in:
parent
fc76107a92
commit
7aee1cae84
5 changed files with 239 additions and 9 deletions
|
@ -185,7 +185,7 @@ DOMInterfaces = {
|
|||
'Element': {
|
||||
'nativeType': 'AbstractNode',
|
||||
'pointerType': '',
|
||||
'needsAbstract': ['getClientRects', 'getBoundingClientRect', 'setAttribute', 'setAttributeNS', 'removeAttribute', 'removeAttributeNS', 'id', 'attributes']
|
||||
'needsAbstract': ['getClientRects', 'getBoundingClientRect', 'setAttribute', 'setAttributeNS', 'removeAttribute', 'removeAttributeNS', 'id', 'attributes', 'innerHTML', 'outerHTML']
|
||||
},
|
||||
|
||||
'Event': {
|
||||
|
|
|
@ -13,10 +13,11 @@ use dom::htmlcollection::HTMLCollection;
|
|||
use dom::clientrect::ClientRect;
|
||||
use dom::clientrectlist::ClientRectList;
|
||||
use dom::document::AbstractDocument;
|
||||
use dom::node::{AbstractNode, ElementNodeTypeId, Node};
|
||||
use dom::node::{AbstractNode, ElementNodeTypeId, Node, NodeIterator};
|
||||
use dom::document;
|
||||
use dom::namespace;
|
||||
use dom::namespace::{Namespace, Null};
|
||||
use dom::htmlserializer::serialize;
|
||||
use layout_interface::{ContentBoxQuery, ContentBoxResponse, ContentBoxesQuery};
|
||||
use layout_interface::{ContentBoxesResponse, ContentChangedDocumentDamage};
|
||||
use layout_interface::{MatchSelectorsDocumentDamage};
|
||||
|
@ -323,6 +324,20 @@ impl Element {
|
|||
document.document().damage_and_reflow(damage);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_void(&self) -> bool {
|
||||
if self.namespace != namespace::HTML {
|
||||
return false
|
||||
}
|
||||
match self.tag_name.as_slice() {
|
||||
/* List of void elements from
|
||||
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#html-fragment-serialization-algorithm */
|
||||
"area" | "base" | "basefont" | "bgsound" | "br" | "col" | "embed" |
|
||||
"frame" | "hr" | "img" | "input" | "keygen" | "link" | "menuitem" |
|
||||
"meta" | "param" | "source" | "track" | "wbr" => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// http://www.whatwg.org/html/#reflecting-content-attributes-in-idl-attributes
|
||||
|
@ -535,19 +550,20 @@ impl Element {
|
|||
0
|
||||
}
|
||||
|
||||
pub fn GetInnerHTML(&self) -> Fallible<DOMString> {
|
||||
Ok(~"")
|
||||
pub fn GetInnerHTML(&self, abstract_self: AbstractNode) -> Fallible<DOMString> {
|
||||
//XXX TODO: XML case
|
||||
Ok(serialize(&mut NodeIterator::new(abstract_self, false, false)))
|
||||
}
|
||||
|
||||
pub fn SetInnerHTML(&mut self, _value: DOMString) -> ErrorResult {
|
||||
pub fn SetInnerHTML(&mut self, _abstract_self: AbstractNode, _value: DOMString) -> ErrorResult {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn GetOuterHTML(&self) -> Fallible<DOMString> {
|
||||
Ok(~"")
|
||||
pub fn GetOuterHTML(&self, abstract_self:AbstractNode) -> Fallible<DOMString> {
|
||||
Ok(serialize(&mut NodeIterator::new(abstract_self, true, false)))
|
||||
}
|
||||
|
||||
pub fn SetOuterHTML(&mut self, _value: DOMString) -> ErrorResult {
|
||||
pub fn SetOuterHTML(&mut self, _abstract_self: AbstractNode, _value: DOMString) -> ErrorResult {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -560,7 +576,6 @@ impl Element {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
fn get_attribute_parts(name: DOMString) -> (Option<~str>, ~str) {
|
||||
//FIXME: Throw for XML-invalid names
|
||||
//FIXME: Throw for XMLNS-invalid names
|
||||
|
|
135
src/components/script/dom/htmlserializer.rs
Normal file
135
src/components/script/dom/htmlserializer.rs
Normal file
|
@ -0,0 +1,135 @@
|
|||
/* 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 http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::namespace;
|
||||
use dom::attr::Attr;
|
||||
use dom::node::NodeIterator;
|
||||
use dom::node::{DoctypeNodeTypeId, DocumentFragmentNodeTypeId, CommentNodeTypeId, DocumentNodeTypeId, ElementNodeTypeId, TextNodeTypeId, AbstractNode};
|
||||
|
||||
pub fn serialize(iterator: &mut NodeIterator) -> ~str {
|
||||
let mut html = ~"";
|
||||
let mut open_elements: ~[~str] = ~[];
|
||||
|
||||
for node in *iterator {
|
||||
while open_elements.len() > iterator.depth {
|
||||
html.push_str(~"</" + open_elements.pop() + ">");
|
||||
}
|
||||
html.push_str(
|
||||
match node.type_id() {
|
||||
ElementNodeTypeId(..) => {
|
||||
serialize_elem(node, &mut open_elements)
|
||||
}
|
||||
CommentNodeTypeId => {
|
||||
serialize_comment(node)
|
||||
}
|
||||
TextNodeTypeId => {
|
||||
serialize_text(node)
|
||||
}
|
||||
DoctypeNodeTypeId => {
|
||||
serialize_doctype(node)
|
||||
}
|
||||
DocumentFragmentNodeTypeId => {
|
||||
~""
|
||||
}
|
||||
DocumentNodeTypeId(_) => {
|
||||
fail!("It shouldn't be possible to serialize a document node")
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
while open_elements.len() > 0 {
|
||||
html.push_str(~"</" + open_elements.pop() + ">");
|
||||
}
|
||||
html
|
||||
}
|
||||
|
||||
fn serialize_comment(node: AbstractNode) -> ~str {
|
||||
node.with_imm_characterdata(|comment| {
|
||||
~"<!--" + comment.data + "-->"
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_text(node: AbstractNode) -> ~str {
|
||||
node.with_imm_characterdata(|text| {
|
||||
match node.parent_node() {
|
||||
Some(parent) if parent.is_element() => {
|
||||
parent.with_imm_element(|elem| {
|
||||
match elem.tag_name.as_slice() {
|
||||
"style" | "script" | "xmp" | "iframe" |
|
||||
"noembed" | "noframes" | "plaintext" |
|
||||
"noscript" if elem.namespace == namespace::HTML => {
|
||||
text.data.clone()
|
||||
},
|
||||
_ => escape(text.data, false)
|
||||
}
|
||||
})
|
||||
},
|
||||
_ => escape(text.data, false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_doctype(node: AbstractNode) -> ~str {
|
||||
node.with_imm_doctype(|doctype| {
|
||||
~"<!DOCTYPE" + doctype.name + ">"
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_elem(node: AbstractNode, open_elements: &mut ~[~str]) -> ~str {
|
||||
node.with_imm_element(|elem| {
|
||||
let mut rv = ~"<" + elem.tag_name;
|
||||
for attr in elem.attrs.iter() {
|
||||
rv.push_str(serialize_attr(attr));
|
||||
};
|
||||
rv.push_str(">");
|
||||
match elem.tag_name.as_slice() {
|
||||
"pre" | "listing" | "textarea" if
|
||||
elem.namespace == namespace::HTML => {
|
||||
match node.first_child() {
|
||||
Some(child) if child.is_text() => {
|
||||
child.with_imm_characterdata(|text| {
|
||||
if text.data[0] == 0x0A as u8 {
|
||||
rv.push_str("\x0A");
|
||||
}
|
||||
})
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
if !elem.is_void() {
|
||||
open_elements.push(elem.tag_name.clone());
|
||||
}
|
||||
rv
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_attr(attr: &@mut Attr) -> ~str {
|
||||
let attr_name = if attr.namespace == namespace::XML {
|
||||
~"xml:" + attr.local_name.clone()
|
||||
} else if attr.namespace == namespace::XMLNS &&
|
||||
attr.local_name.as_slice() == "xmlns" {
|
||||
~"xmlns"
|
||||
} else if attr.namespace == namespace::XMLNS {
|
||||
~"xmlns:" + attr.local_name.clone()
|
||||
} else if attr.namespace == namespace::XLink {
|
||||
~"xlink:" + attr.local_name.clone()
|
||||
} else {
|
||||
attr.name.clone()
|
||||
};
|
||||
~" " + attr_name + "=\"" + escape(attr.value, true) + "\""
|
||||
}
|
||||
|
||||
fn escape(string: &str, attr_mode: bool) -> ~str {
|
||||
let replaced = string.replace("&", "&").replace("\xA0", " ");
|
||||
match attr_mode {
|
||||
true => {
|
||||
replaced.replace("\"", """)
|
||||
},
|
||||
false => {
|
||||
replaced.replace("<", "<").replace(">", ">")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -684,6 +684,85 @@ impl Iterator<AbstractNode> for TreeIterator {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct NodeIterator {
|
||||
start_node: AbstractNode,
|
||||
current_node: Option<AbstractNode>,
|
||||
depth: uint,
|
||||
priv include_start: bool,
|
||||
priv include_descendants_of_void: bool
|
||||
}
|
||||
|
||||
impl NodeIterator {
|
||||
pub fn new(start_node: AbstractNode, include_start: bool, include_descendants_of_void: bool) -> NodeIterator {
|
||||
NodeIterator {
|
||||
start_node: start_node,
|
||||
current_node: None,
|
||||
depth: 0,
|
||||
include_start: include_start,
|
||||
include_descendants_of_void: include_descendants_of_void
|
||||
}
|
||||
}
|
||||
|
||||
fn next_child(&self, node: AbstractNode) -> Option<AbstractNode> {
|
||||
if !self.include_descendants_of_void &&
|
||||
node.is_element() {
|
||||
node.with_imm_element(|elem| {
|
||||
if elem.is_void() {
|
||||
None
|
||||
} else {
|
||||
node.first_child()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
node.first_child()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<AbstractNode> for NodeIterator {
|
||||
fn next(&mut self) -> Option<AbstractNode> {
|
||||
self.current_node = match self.current_node {
|
||||
None => {
|
||||
if self.include_start {
|
||||
Some(self.start_node)
|
||||
} else {
|
||||
self.next_child(self.start_node)
|
||||
}
|
||||
},
|
||||
Some(node) => {
|
||||
match self.next_child(node) {
|
||||
Some(child) => {
|
||||
self.depth += 1;
|
||||
Some(child)
|
||||
},
|
||||
None if node == self.start_node => None,
|
||||
None => {
|
||||
match node.next_sibling() {
|
||||
Some(sibling) => Some(sibling),
|
||||
None => {
|
||||
let mut candidate = node;
|
||||
while candidate.next_sibling().is_none() {
|
||||
candidate = candidate.parent_node().expect("Got to root without reaching start node");
|
||||
self.depth -= 1;
|
||||
if candidate == self.start_node {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if candidate != self.start_node {
|
||||
candidate.next_sibling()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
self.current_node
|
||||
}
|
||||
}
|
||||
|
||||
fn gather_abstract_nodes(cur: &AbstractNode, refs: &mut ~[AbstractNode], postorder: bool) {
|
||||
if !postorder {
|
||||
refs.push(cur.clone());
|
||||
|
|
|
@ -113,6 +113,7 @@ pub mod dom {
|
|||
pub mod htmlquoteelement;
|
||||
pub mod htmlscriptelement;
|
||||
pub mod htmlselectelement;
|
||||
pub mod htmlserializer;
|
||||
pub mod htmlspanelement;
|
||||
pub mod htmlsourceelement;
|
||||
pub mod htmlstyleelement;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue