Skip to content

Commit

Permalink
Auto merge of #20054 - ferjm:innertext.setter, r=emilio
Browse files Browse the repository at this point in the history
Implement element.innerText setter

- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #16107
- [X] There are tests for these changes

<!-- 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/20054)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Feb 16, 2018
2 parents ff58cb0 + d64bf62 commit a6113af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 370 deletions.
60 changes: 58 additions & 2 deletions components/script/dom/htmlelement.rs
Expand Up @@ -17,17 +17,20 @@ 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};
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;
Expand Down Expand Up @@ -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-";
Expand Down
2 changes: 1 addition & 1 deletion tests/wpt/metadata/MANIFEST.json
Expand Up @@ -565038,7 +565038,7 @@
"support"
],
"innerText/setter.html": [
"6a9362dc28615ae2667131e44fcdb2589f87d9d2",
"8e78f2b7641a609b692a55ad8f4979dd852b706c",
"testharness"
],
"input-events/OWNERS": [
Expand Down

0 comments on commit a6113af

Please sign in to comment.