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

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

Issue 1085843002: Implement DOM: prepend, append, before, after & replaceWith (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 6 months 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
Index: Source/core/dom/Node.cpp
diff --git a/Source/core/dom/Node.cpp b/Source/core/dom/Node.cpp
index 56fb995d78a7033954fdda8b5b547b0d8d05500c..ab4aabc8ca8e0f453da7cd2d4c16b43617c30896 100644
--- a/Source/core/dom/Node.cpp
+++ b/Source/core/dom/Node.cpp
@@ -382,6 +382,26 @@ void Node::clearRareData()
}
#endif
+PassRefPtrWillBeRawPtr<Node> Node::nodeOrStringToNode(const NodeOrString& nodeOrString)
+{
+ if (nodeOrString.isNode())
+ return nodeOrString.getAsNode();
+ return Text::create(document(), nodeOrString.getAsString());
+}
+
+PassRefPtrWillBeRawPtr<Node> Node::convertNodesIntoNode(const HeapVector<NodeOrString>& nodes)
+{
+ RefPtrWillBeRawPtr<Node> node = nullptr;
+ if (nodes.size() == 1) {
+ node = nodeOrStringToNode(nodes[0]);
philipj_slow 2015/06/15 10:06:58 The spec is on this form, but would this be cleare
Paritosh Kumar 2015/06/23 13:32:09 Thanks. Done.
philipj_slow 2015/06/25 13:13:23 Looks good this way, thanks!
+ } else {
+ node = DocumentFragment::create(document());
+ for (const NodeOrString& nodeOrString : nodes)
+ node->appendChild(nodeOrStringToNode(nodeOrString), ASSERT_NO_EXCEPTION);
+ }
+ return node.release();
+}
+
Node* Node::toNode()
{
return this;
@@ -501,6 +521,45 @@ PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC
return nullptr;
}
+void Node::prepend(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ RefPtrWillBeRawPtr<Node> nodeToPrepend = convertNodesIntoNode(nodes);
+ this->insertBefore(nodeToPrepend, this->firstChild(), exceptionState);
+}
+
+void Node::append(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ RefPtrWillBeRawPtr<Node> nodeToAppend = convertNodesIntoNode(nodes);
+ this->appendChild(nodeToAppend, exceptionState);
+}
+
+void Node::before(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ Node* parent = parentNode();
+ if (!parent)
+ return;
+ RefPtrWillBeRawPtr<Node> nodeToInsert = convertNodesIntoNode(nodes);
+ parent->insertBefore(nodeToInsert, this, exceptionState);
+}
+
+void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ Node* parent = parentNode();
+ if (!parent)
+ return;
+ RefPtrWillBeRawPtr<Node> nodeToInsert = convertNodesIntoNode(nodes);
+ parent->insertBefore(nodeToInsert, this->nextSibling(), exceptionState);
+}
+
+void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ Node* parent = parentNode();
+ if (!parent)
+ return;
+ RefPtrWillBeRawPtr<Node> nodeToReplaceWith = convertNodesIntoNode(nodes);
+ parent->replaceChild(nodeToReplaceWith, this, exceptionState);
+}
+
void Node::remove(ExceptionState& exceptionState)
{
if (ContainerNode* parent = parentNode())

Powered by Google App Engine
This is Rietveld 408576698