Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Unified Diff: Source/core/dom/Element.cpp

Issue 105693009: Move insertAdjacentHTML to Element (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Correct base. Should not include patches it depends on. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/dom/Element.h ('k') | Source/core/dom/Element.idl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/dom/Element.cpp
diff --git a/Source/core/dom/Element.cpp b/Source/core/dom/Element.cpp
index f0b7b12554dac75334e698614c4d248274eebca2..57494ee7c70a654ab9e1689fe3099d2c2a6845b8 100644
--- a/Source/core/dom/Element.cpp
+++ b/Source/core/dom/Element.cpp
@@ -2276,6 +2276,69 @@ void Element::setOuterHTML(const String& html, ExceptionState& exceptionState)
mergeWithNextTextNode(prev.release(), exceptionState);
}
+Node* Element::insertAdjacent(const String& where, Node* newChild, ExceptionState& exceptionState)
+{
+ if (equalIgnoringCase(where, "beforeBegin")) {
+ if (ContainerNode* parent = this->parentNode()) {
+ parent->insertBefore(newChild, this, exceptionState);
+ if (!exceptionState.hadException())
+ return newChild;
+ }
+ return 0;
+ }
+
+ if (equalIgnoringCase(where, "afterBegin")) {
+ insertBefore(newChild, firstChild(), exceptionState);
+ return exceptionState.hadException() ? 0 : newChild;
+ }
+
+ if (equalIgnoringCase(where, "beforeEnd")) {
+ appendChild(newChild, exceptionState);
+ return exceptionState.hadException() ? 0 : newChild;
+ }
+
+ if (equalIgnoringCase(where, "afterEnd")) {
+ if (ContainerNode* parent = this->parentNode()) {
+ parent->insertBefore(newChild, nextSibling(), exceptionState);
+ if (!exceptionState.hadException())
+ return newChild;
+ }
+ return 0;
+ }
+
+ exceptionState.throwDOMException(SyntaxError, "The value provided ('" + where + "') is not one of 'beforeBegin', 'afterBegin', 'beforeEnd', or 'afterEnd'.");
+ return 0;
+}
+
+// Step 1 of http://domparsing.spec.whatwg.org/#insertadjacenthtml()
+static Element* contextElementForInsertion(const String& where, Element* element, ExceptionState& exceptionState)
+{
+ if (equalIgnoringCase(where, "beforeBegin") || equalIgnoringCase(where, "afterEnd")) {
+ ContainerNode* parent = element->parentNode();
+ if (!parent || !parent->isElementNode()) {
+ exceptionState.throwDOMException(NoModificationAllowedError, "The element has no parent.");
+ return 0;
+ }
+ return toElement(parent);
+ }
+ if (equalIgnoringCase(where, "afterBegin") || equalIgnoringCase(where, "beforeEnd"))
+ return element;
+ exceptionState.throwDOMException(SyntaxError, "The value provided ('" + where + "') is not one of 'beforeBegin', 'afterBegin', 'beforeEnd', or 'afterEnd'.");
+ return 0;
+}
+
+void Element::insertAdjacentHTML(const String& where, const String& markup, ExceptionState& exceptionState)
+{
+ RefPtr<Element> contextElement = contextElementForInsertion(where, this, exceptionState);
+ if (!contextElement)
+ return;
+
+ RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, contextElement.get(), AllowScriptingContent, "insertAdjacentHTML", exceptionState);
+ if (!fragment)
+ return;
+ insertAdjacent(where, fragment.get(), exceptionState);
+}
+
String Element::innerText()
{
// We need to update layout, since plainText uses line boxes in the render tree.
« no previous file with comments | « Source/core/dom/Element.h ('k') | Source/core/dom/Element.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698