Chromium Code Reviews| Index: Source/core/dom/Node.cpp |
| diff --git a/Source/core/dom/Node.cpp b/Source/core/dom/Node.cpp |
| index 56fb995d78a7033954fdda8b5b547b0d8d05500c..5b6e77c2d4760f72c2572e272c282390c1feca01 100644 |
| --- a/Source/core/dom/Node.cpp |
| +++ b/Source/core/dom/Node.cpp |
| @@ -501,6 +501,92 @@ PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC |
| return nullptr; |
| } |
| +static bool isNodeInNodes(const Node& node, const HeapVector<NodeOrString>& nodes) |
| +{ |
| + for (const NodeOrString& nodeOrString : nodes) { |
| + if (nodeOrString.isNode() && nodeOrString.getAsNode() == &node) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +static Node* findViablePreviousSibling(const Node& node, const HeapVector<NodeOrString>& nodes) |
| +{ |
| + for (Node* sibling = node.previousSibling(); sibling; sibling = sibling->previousSibling()) { |
| + if (!isNodeInNodes(*sibling, nodes)) |
| + return sibling; |
| + } |
| + return nullptr; |
| +} |
| + |
| +static Node* findViableNextSibling(const Node& node, const HeapVector<NodeOrString>& nodes) |
| +{ |
| + for (Node* sibling = node.nextSibling(); sibling; sibling = sibling->nextSibling()) { |
| + if (!isNodeInNodes(*sibling, nodes)) |
| + return sibling; |
| + } |
| + return nullptr; |
| +} |
| + |
| +static PassRefPtrWillBeRawPtr<Node> nodeOrStringToNode(const NodeOrString& nodeOrString, Document& document) |
| +{ |
| + if (nodeOrString.isNode()) |
| + return nodeOrString.getAsNode(); |
| + return Text::create(document, nodeOrString.getAsString()); |
| +} |
| + |
| +static PassRefPtrWillBeRawPtr<Node> convertNodesIntoNode(const HeapVector<NodeOrString>& nodes, Document& document) |
| +{ |
| + if (nodes.size() == 1) |
| + return nodeOrStringToNode(nodes[0], document); |
| + |
| + RefPtrWillBeRawPtr<Node> fragment = DocumentFragment::create(document); |
| + for (const NodeOrString& nodeOrString : nodes) |
| + fragment->appendChild(nodeOrStringToNode(nodeOrString, document), ASSERT_NO_EXCEPTION); |
| + return fragment.release(); |
| +} |
| + |
| +void Node::prepend(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState) |
| +{ |
| + insertBefore(convertNodesIntoNode(nodes, document()), firstChild(), exceptionState); |
| +} |
| + |
| +void Node::append(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState) |
| +{ |
| + appendChild(convertNodesIntoNode(nodes, document()), exceptionState); |
| +} |
| + |
| +void Node::before(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState) |
| +{ |
| + Node* parent = parentNode(); |
| + if (!parent) |
| + return; |
| + Node* viablePreviousSibling = findViablePreviousSibling(*this, nodes); |
| + parent->insertBefore(convertNodesIntoNode(nodes, document()), viablePreviousSibling ? viablePreviousSibling->nextSibling() : parent->firstChild(), exceptionState); |
| +} |
| + |
| +void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState) |
| +{ |
| + Node* parent = parentNode(); |
| + if (!parent) |
| + return; |
| + Node* viableNextSibling = findViableNextSibling(*this, nodes); |
| + parent->insertBefore(convertNodesIntoNode(nodes, document()), viableNextSibling, exceptionState); |
| +} |
| + |
| +void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptionState) |
| +{ |
| + Node* parent = parentNode(); |
| + if (!parent) |
| + return; |
| + Node* viableNextSibling = findViableNextSibling(*this, nodes); |
| + RefPtrWillBeRawPtr<Node> nodeToReplaceWith = convertNodesIntoNode(nodes, document()); |
|
philipj_slow
2015/07/03 21:52:06
The node is either replaced with or inserted befor
Paritosh Kumar
2015/07/06 09:02:27
Replaced.
|
| + if (parent == parentNode()) |
| + parent->replaceChild(nodeToReplaceWith, this, exceptionState); |
| + else |
| + parent->insertBefore(nodeToReplaceWith, viableNextSibling, exceptionState); |
| +} |
| + |
| void Node::remove(ExceptionState& exceptionState) |
| { |
| if (ContainerNode* parent = parentNode()) |