diff options
author | Fernando Jiménez Moreno <ferjmoreno@gmail.com> | 2018-02-15 10:19:49 +0100 |
---|---|---|
committer | Fernando Jiménez Moreno <ferjmoreno@gmail.com> | 2018-02-16 11:20:53 +0100 |
commit | d64bf623f6e7d825aff8a59a6f220db2fbb15f29 (patch) | |
tree | 1715fc75a96c77f3c0bee88651a35478db315038 /components/script/dom/htmlelement.rs | |
parent | 7945dff8ea18cd4084c1baa075d506837359ebe5 (diff) | |
download | servo-d64bf623f6e7d825aff8a59a6f220db2fbb15f29.tar.gz servo-d64bf623f6e7d825aff8a59a6f220db2fbb15f29.zip |
Implement element.innerText setter
Diffstat (limited to 'components/script/dom/htmlelement.rs')
-rw-r--r-- | components/script/dom/htmlelement.rs | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/components/script/dom/htmlelement.rs b/components/script/dom/htmlelement.rs index ec9b3973ca1..12dc0d1c125 100644 --- a/components/script/dom/htmlelement.rs +++ b/components/script/dom/htmlelement.rs @@ -17,10 +17,12 @@ use dom::bindings::root::{Dom, DomRoot, MutNullableDom, RootedReference}; use dom::bindings::str::DOMString; use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner}; use dom::document::{Document, FocusType}; +use dom::documentfragment::DocumentFragment; use dom::domstringmap::DOMStringMap; use dom::element::{AttributeMutation, Element}; use dom::eventtarget::EventTarget; use dom::htmlbodyelement::HTMLBodyElement; +use dom::htmlbrelement::HTMLBRElement; use dom::htmlframesetelement::HTMLFrameSetElement; use dom::htmlhtmlelement::HTMLHtmlElement; use dom::htmlinputelement::{HTMLInputElement, InputType}; @@ -28,6 +30,7 @@ use dom::htmllabelelement::HTMLLabelElement; use dom::node::{Node, NodeFlags}; use dom::node::{document_from_node, window_from_node}; use dom::nodelist::NodeList; +use dom::text::Text; use dom::virtualmethods::VirtualMethods; use dom::window::ReflowReason; use dom_struct::dom_struct; @@ -421,11 +424,64 @@ impl HTMLElementMethods for HTMLElement { } // https://html.spec.whatwg.org/multipage/#the-innertext-idl-attribute - fn SetInnerText(&self, _: DOMString) { - // XXX (ferjm) implement this. + fn SetInnerText(&self, input: DOMString) { + // Step 1. + let document = document_from_node(self); + + // Step 2. + let fragment = DocumentFragment::new(&document); + + // Step 3. The given value is already named 'input'. + + // Step 4. + let mut position = input.chars().peekable(); + + // Step 5. + let mut text = String::new(); + + // Step 6. + while let Some(ch) = position.next() { + match ch { + '\u{000A}' | '\u{000D}' => { + if ch == '\u{000D}' && position.peek() == Some(&'\u{000A}') { + // a \r\n pair should only generate one <br>, + // so just skip the \r. + position.next(); + } + + if !text.is_empty() { + append_text_node_to_fragment(&document, &fragment, text); + text = String::new(); + } + + let br = HTMLBRElement::new(local_name!("br"), None, &document); + fragment.upcast::<Node>().AppendChild(&br.upcast()).unwrap(); + }, + _ => { + text.push(ch); + } + } + } + + if !text.is_empty() { + append_text_node_to_fragment(&document, &fragment, text); + } + + // Step 7. + Node::replace_all(Some(fragment.upcast()), self.upcast::<Node>()); } } +fn append_text_node_to_fragment( + document: &Document, + fragment: &DocumentFragment, + text: String +) { + let text = Text::new(DOMString::from(text), document); + let node = DomRoot::upcast::<Node>(text); + fragment.upcast::<Node>().AppendChild(&node).unwrap(); +} + // https://html.spec.whatwg.org/multipage/#attr-data-* static DATA_PREFIX: &str = "data-"; |