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

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
« no previous file with comments | « Source/core/dom/Node.h ('k') | Source/core/dom/ParentNode.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/dom/Node.cpp
diff --git a/Source/core/dom/Node.cpp b/Source/core/dom/Node.cpp
index 56fb995d78a7033954fdda8b5b547b0d8d05500c..5dbb22b24756493c29a6191fa4cbccf96ce88e48 100644
--- a/Source/core/dom/Node.cpp
+++ b/Source/core/dom/Node.cpp
@@ -382,6 +382,43 @@ void Node::clearRareData()
}
#endif
+PassRefPtrWillBeRawPtr<Node> Node::nodeOrStringToNode(const NodeOrString& nodeOrString)
+{
+ if (nodeOrString.isNode())
+ return nodeOrString.getAsNode();
+ return Text::create(document(), nodeOrString.getAsString());
+}
+
+Node* Node::viablePreviosOrNextSibling(const HeapVector<NodeOrString>& nodes, bool next)
philipj_slow 2015/06/25 13:13:23 The bool argument isn't great. I applied your patc
Paritosh Kumar 2015/06/26 13:14:05 Yes, It looks great. Thanks.
+{
+ Node* node = next ? this->nextSibling() : this->previousSibling();
+ bool notFound;
+ while (node) {
+ notFound = true;
+ for (NodeOrString nodeOrString : nodes) {
+ if (nodeOrStringToNode(nodeOrString)->isSameNode(node)) {
philipj_slow 2015/06/25 13:13:23 It's a waste to convert string to nodes here, inst
Paritosh Kumar 2015/06/26 13:14:06 Done.
+ notFound = false;
+ break;
+ }
+ }
+ if (notFound)
+ return node;
+ node = next ? node->nextSibling() : node->previousSibling();
+ }
+ return nullptr;
+}
+
+PassRefPtrWillBeRawPtr<Node> Node::convertNodesIntoNode(const HeapVector<NodeOrString>& nodes)
+{
+ if (nodes.size() == 1)
+ return nodeOrStringToNode(nodes[0]);
+
+ RefPtrWillBeRawPtr<Node> 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 +538,54 @@ 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;
+ Node* viablePreviousSibling = viablePreviosOrNextSibling(nodes, false /* Previous */);
+ RefPtrWillBeRawPtr<Node> nodeToInsert = convertNodesIntoNode(nodes);
+ if (!viablePreviousSibling)
+ parent->insertBefore(nodeToInsert, parent->firstChild(), exceptionState);
+ else
+ parent->insertBefore(nodeToInsert, viablePreviousSibling, exceptionState);
+}
+
+void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ Node* parent = parentNode();
+ if (!parent)
+ return;
+ Node* viableNextSibling = viablePreviosOrNextSibling(nodes, true /* Next */);
+ RefPtrWillBeRawPtr<Node> nodeToInsert = convertNodesIntoNode(nodes);
+ parent->insertBefore(nodeToInsert, viableNextSibling, exceptionState);
+}
+
+void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState)
+{
+ Node* parent = parentNode();
+ if (!parent)
+ return;
+ Node* viableNextSibling = viablePreviosOrNextSibling(nodes, true /* Next */);
philipj_slow 2015/06/25 13:13:23 Here viableNextSibling and nodeToReplaceWith can b
Paritosh Kumar 2015/06/26 13:14:06 Done.
+ RefPtrWillBeRawPtr<Node> nodeToReplaceWith = convertNodesIntoNode(nodes);
+ if (parent == parentNode())
+ parent->replaceChild(nodeToReplaceWith, this, exceptionState);
+ else
+ parent->insertBefore(nodeToReplaceWith, viableNextSibling, exceptionState);
+}
+
void Node::remove(ExceptionState& exceptionState)
{
if (ContainerNode* parent = parentNode())
« no previous file with comments | « Source/core/dom/Node.h ('k') | Source/core/dom/ParentNode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698