From 8c572085c72a48bcb1b639211aa0995bd2cbabc2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 8 Jun 2016 01:43:04 +0200 Subject: [PATCH] Add Element::InsertAdjacentHTML --- components/script/dom/element.rs | 35 ++++++++++++ components/script/dom/webidls/Element.webidl | 2 + .../domparsing/insert-adjacent.html.ini | 6 -- .../domparsing/insert_adjacent_html.html.ini | 57 ------------------- .../domparsing/insert_adjacent_html.xhtml.ini | 54 ------------------ 5 files changed, 37 insertions(+), 117 deletions(-) diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index b454d21c1cba..4539c20d1564 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -2029,6 +2029,41 @@ impl ElementMethods for Element { let where_ = try!(AdjacentPosition::parse(&*where_)); self.insert_adjacent(where_, text.upcast()).map(|_| ()) } + + // https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml + fn InsertAdjacentHTML(&self, position: DOMString, text: DOMString) + -> ErrorResult { + // Step 1. + let position = try!(AdjacentPosition::parse(&*position)); + + let context = match position { + AdjacentPosition::BeforeBegin | AdjacentPosition::AfterEnd => { + match self.upcast::().GetParentNode() { + Some(ref node) if node.is::() => { + return Err(Error::NoModificationAllowed) + } + None => return Err(Error::NoModificationAllowed), + Some(node) => node, + } + } + AdjacentPosition::AfterBegin | AdjacentPosition::BeforeEnd => { + Root::from_ref(self.upcast::()) + } + }; + + // Step 2. + let context = match context.downcast::() { + Some(elem) if elem.local_name() != &atom!("html") || + !elem.html_element_in_html_document() => Root::from_ref(elem), + _ => Root::upcast(HTMLBodyElement::new(atom!("body"), None, &*context.owner_doc())) + }; + + // Step 3. + let fragment = try!(context.upcast::().parse_fragment(text)); + + // Step 4. + context.insert_adjacent(position, fragment.upcast()).map(|_| ()) + } } pub fn fragment_affecting_attributes() -> [Atom; 3] { diff --git a/components/script/dom/webidls/Element.webidl b/components/script/dom/webidls/Element.webidl index ee27b78d4e49..48aeed7fbbb0 100644 --- a/components/script/dom/webidls/Element.webidl +++ b/components/script/dom/webidls/Element.webidl @@ -75,6 +75,8 @@ interface Element : Node { Element? insertAdjacentElement(DOMString where_, Element element); // historical [Throws] void insertAdjacentText(DOMString where_, DOMString data); + [Throws] + void insertAdjacentHTML(DOMString position, DOMString html); }; // http://dev.w3.org/csswg/cssom-view/#extensions-to-the-element-interface diff --git a/tests/wpt/metadata/domparsing/insert-adjacent.html.ini b/tests/wpt/metadata/domparsing/insert-adjacent.html.ini index af6749ad7d2c..10a4345eeb07 100644 --- a/tests/wpt/metadata/domparsing/insert-adjacent.html.ini +++ b/tests/wpt/metadata/domparsing/insert-adjacent.html.ini @@ -3,12 +3,6 @@ [insertAdjacentHTML(beforebegin,

beforebegin

)] expected: FAIL - [insertAdjacentHTML(afterbegin,

afterbegin

)] - expected: FAIL - - [insertAdjacentHTML(beforeend,

beforeend

)] - expected: FAIL - [insertAdjacentHTML(afterend,

afterend

)] expected: FAIL diff --git a/tests/wpt/metadata/domparsing/insert_adjacent_html.html.ini b/tests/wpt/metadata/domparsing/insert_adjacent_html.html.ini index a1028c07ebe1..552aaec582f3 100644 --- a/tests/wpt/metadata/domparsing/insert_adjacent_html.html.ini +++ b/tests/wpt/metadata/domparsing/insert_adjacent_html.html.ini @@ -3,87 +3,30 @@ [beforeBegin content without next sibling] expected: FAIL - [Afterbegin content without next sibling] - expected: FAIL - - [BeforeEnd content without next sibling] - expected: FAIL - [afterend content without next sibling] expected: FAIL [beforeBegin content again, with next sibling] expected: FAIL - [Afterbegin content again, with next sibling] - expected: FAIL - - [BeforeEnd content again, with next sibling] - expected: FAIL - [afterend content again, with next sibling] expected: FAIL - [Should throw when inserting with invalid position string] - expected: FAIL - - [When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (text)] - expected: FAIL - - [When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (comments)] - expected: FAIL - - [When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (elements)] - expected: FAIL - - [When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (text)] - expected: FAIL - - [When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (comments)] - expected: FAIL - - [When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (elements)] - expected: FAIL - - [Inserting after being and before end should order things correctly] - expected: FAIL - [beforeBegin child node not in tree but has parent] expected: FAIL - [Afterbegin child node not in tree but has parent] - expected: FAIL - - [BeforeEnd child node not in tree but has parent] - expected: FAIL - [afterend child node not in tree but has parent] expected: FAIL [beforeBegin content2 without next sibling] expected: FAIL - [Afterbegin content2 without next sibling] - expected: FAIL - - [BeforeEnd content2 without next sibling] - expected: FAIL - [afterend content2 without next sibling] expected: FAIL [beforeBegin content2 test again, now that there's a next sibling] expected: FAIL - [Afterbegin content2 test again, now that there's a next sibling] - expected: FAIL - - [BeforeEnd content2 test again, now that there's a next sibling] - expected: FAIL - [afterend content2 test again, now that there's a next sibling] expected: FAIL - [Inserting kids of the element should not do weird things with implied / tags] - expected: FAIL - diff --git a/tests/wpt/metadata/domparsing/insert_adjacent_html.xhtml.ini b/tests/wpt/metadata/domparsing/insert_adjacent_html.xhtml.ini index 5ceb4830cb7e..9312a8de81a5 100644 --- a/tests/wpt/metadata/domparsing/insert_adjacent_html.xhtml.ini +++ b/tests/wpt/metadata/domparsing/insert_adjacent_html.xhtml.ini @@ -3,84 +3,30 @@ [beforeBegin content without next sibling] expected: FAIL - [Afterbegin content without next sibling] - expected: FAIL - - [BeforeEnd content without next sibling] - expected: FAIL - [afterend content without next sibling] expected: FAIL [beforeBegin content again, with next sibling] expected: FAIL - [Afterbegin content again, with next sibling] - expected: FAIL - - [BeforeEnd content again, with next sibling] - expected: FAIL - [afterend content again, with next sibling] expected: FAIL - [Should throw when inserting with invalid position string] - expected: FAIL - - [When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (text)] - expected: FAIL - - [When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (comments)] - expected: FAIL - - [When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (elements)] - expected: FAIL - - [When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (text)] - expected: FAIL - - [When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (comments)] - expected: FAIL - - [When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (elements)] - expected: FAIL - - [Inserting after being and before end should order things correctly] - expected: FAIL - [beforeBegin child node not in tree but has parent] expected: FAIL - [Afterbegin child node not in tree but has parent] - expected: FAIL - - [BeforeEnd child node not in tree but has parent] - expected: FAIL - [afterend child node not in tree but has parent] expected: FAIL [beforeBegin content2 without next sibling] expected: FAIL - [Afterbegin content2 without next sibling] - expected: FAIL - - [BeforeEnd content2 without next sibling] - expected: FAIL - [afterend content2 without next sibling] expected: FAIL [beforeBegin content2 test again, now that there's a next sibling] expected: FAIL - [Afterbegin content2 test again, now that there's a next sibling] - expected: FAIL - - [BeforeEnd content2 test again, now that there's a next sibling] - expected: FAIL - [afterend content2 test again, now that there's a next sibling] expected: FAIL