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

Side by Side Diff: third_party/WebKit/Source/core/dom/Node.cpp

Issue 1934123002: Implement DOM methods: prepend, append, after, before and replaceWith. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync test expectations Created 4 years, 7 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved.
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
7 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo bile.com/) 7 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo bile.com/)
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 409
410 Node* Node::appendChild(Node* newChild, ExceptionState& exceptionState) 410 Node* Node::appendChild(Node* newChild, ExceptionState& exceptionState)
411 { 411 {
412 if (isContainerNode()) 412 if (isContainerNode())
413 return toContainerNode(this)->appendChild(newChild, exceptionState); 413 return toContainerNode(this)->appendChild(newChild, exceptionState);
414 414
415 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method."); 415 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method.");
416 return nullptr; 416 return nullptr;
417 } 417 }
418 418
419 static bool isNodeInNodes(const Node* const node, const HeapVector<NodeOrString> & nodes)
420 {
421 for (const NodeOrString& nodeOrString : nodes) {
422 if (nodeOrString.isNode() && nodeOrString.getAsNode() == node)
423 return true;
424 }
425 return false;
426 }
427
428 static Node* findViablePreviousSibling(const Node& node, const HeapVector<NodeOr String>& nodes)
429 {
430 for (Node* sibling = node.previousSibling(); sibling; sibling = sibling->pre viousSibling()) {
431 if (!isNodeInNodes(sibling, nodes))
432 return sibling;
433 }
434 return nullptr;
435 }
436
437 static Node* findViableNextSibling(const Node& node, const HeapVector<NodeOrStri ng>& nodes)
438 {
439 for (Node* sibling = node.nextSibling(); sibling; sibling = sibling->nextSib ling()) {
440 if (!isNodeInNodes(sibling, nodes))
441 return sibling;
442 }
443 return nullptr;
444 }
445
446 static Node* nodeOrStringToNode(const NodeOrString& nodeOrString, Document& docu ment)
447 {
448 if (nodeOrString.isNode())
449 return nodeOrString.getAsNode();
450 return Text::create(document, nodeOrString.getAsString());
451 }
452
453 static Node* convertNodesIntoNode(const HeapVector<NodeOrString>& nodes, Documen t& document)
454 {
455 if (nodes.size() == 1)
456 return nodeOrStringToNode(nodes[0], document);
457
458 Node* fragment = DocumentFragment::create(document);
459 for (const NodeOrString& nodeOrString : nodes)
460 fragment->appendChild(nodeOrStringToNode(nodeOrString, document), ASSERT _NO_EXCEPTION);
461 return fragment;
462 }
463
464 void Node::prepend(const HeapVector<NodeOrString>& nodes, ExceptionState& except ionState)
465 {
466 Node* node = convertNodesIntoNode(nodes, document());
467 insertBefore(node, firstChild(), exceptionState);
468 }
469
470 void Node::append(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState)
471 {
472 Node* node = convertNodesIntoNode(nodes, document());
473 appendChild(node, exceptionState);
474 }
475
476 void Node::before(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState)
477 {
478 Node* parent = parentNode();
479 if (!parent)
480 return;
481 Node* viablePreviousSibling = findViablePreviousSibling(*this, nodes);
482 Node* node = convertNodesIntoNode(nodes, document());
483 parent->insertBefore(node, viablePreviousSibling ? viablePreviousSibling->ne xtSibling() : parent->firstChild(), exceptionState);
484 }
485
486 void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptio nState)
487 {
488 Node* parent = parentNode();
489 if (!parent)
490 return;
491 Node* viableNextSibling = findViableNextSibling(*this, nodes);
492 Node* node = convertNodesIntoNode(nodes, document());
493 parent->insertBefore(node, viableNextSibling, exceptionState);
494 }
495
496 void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& ex ceptionState)
497 {
498 Node* parent = parentNode();
499 if (!parent)
500 return;
501 Node* viableNextSibling = findViableNextSibling(*this, nodes);
502 Node* node = convertNodesIntoNode(nodes, document());
503 if (parent == parentNode())
504 parent->replaceChild(node, this, exceptionState);
505 else
506 parent->insertBefore(node, viableNextSibling, exceptionState);
507 }
508
419 void Node::remove(ExceptionState& exceptionState) 509 void Node::remove(ExceptionState& exceptionState)
420 { 510 {
421 if (ContainerNode* parent = parentNode()) 511 if (ContainerNode* parent = parentNode())
422 parent->removeChild(this, exceptionState); 512 parent->removeChild(this, exceptionState);
423 } 513 }
424 514
425 void Node::normalize() 515 void Node::normalize()
426 { 516 {
427 updateDistribution(); 517 updateDistribution();
428 518
(...skipping 1903 matching lines...) Expand 10 before | Expand all | Expand 10 after
2332 2422
2333 void showNodePath(const blink::Node* node) 2423 void showNodePath(const blink::Node* node)
2334 { 2424 {
2335 if (node) 2425 if (node)
2336 node->showNodePathForThis(); 2426 node->showNodePathForThis();
2337 else 2427 else
2338 fprintf(stderr, "Cannot showNodePath for (nil)\n"); 2428 fprintf(stderr, "Cannot showNodePath for (nil)\n");
2339 } 2429 }
2340 2430
2341 #endif 2431 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/Node.h ('k') | third_party/WebKit/Source/core/dom/ParentNode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698