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

Side by Side 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, 5 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 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 494
495 PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC hild, ExceptionState& exceptionState) 495 PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC hild, ExceptionState& exceptionState)
496 { 496 {
497 if (isContainerNode()) 497 if (isContainerNode())
498 return toContainerNode(this)->appendChild(newChild, exceptionState); 498 return toContainerNode(this)->appendChild(newChild, exceptionState);
499 499
500 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method."); 500 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method.");
501 return nullptr; 501 return nullptr;
502 } 502 }
503 503
504 static bool isNodeInNodes(const Node& node, const HeapVector<NodeOrString>& node s)
505 {
506 for (const NodeOrString& nodeOrString : nodes) {
507 if (nodeOrString.isNode() && nodeOrString.getAsNode() == &node)
508 return true;
509 }
510 return false;
511 }
512
513 static Node* findViablePreviousSibling(const Node& node, const HeapVector<NodeOr String>& nodes)
514 {
515 for (Node* sibling = node.previousSibling(); sibling; sibling = sibling->pre viousSibling()) {
516 if (!isNodeInNodes(*sibling, nodes))
517 return sibling;
518 }
519 return nullptr;
520 }
521
522 static Node* findViableNextSibling(const Node& node, const HeapVector<NodeOrStri ng>& nodes)
523 {
524 for (Node* sibling = node.nextSibling(); sibling; sibling = sibling->nextSib ling()) {
525 if (!isNodeInNodes(*sibling, nodes))
526 return sibling;
527 }
528 return nullptr;
529 }
530
531 static PassRefPtrWillBeRawPtr<Node> nodeOrStringToNode(const NodeOrString& nodeO rString, Document& document)
532 {
533 if (nodeOrString.isNode())
534 return nodeOrString.getAsNode();
535 return Text::create(document, nodeOrString.getAsString());
536 }
537
538 static PassRefPtrWillBeRawPtr<Node> convertNodesIntoNode(const HeapVector<NodeOr String>& nodes, Document& document)
539 {
540 if (nodes.size() == 1)
541 return nodeOrStringToNode(nodes[0], document);
542
543 RefPtrWillBeRawPtr<Node> fragment = DocumentFragment::create(document);
544 for (const NodeOrString& nodeOrString : nodes)
545 fragment->appendChild(nodeOrStringToNode(nodeOrString, document), ASSERT _NO_EXCEPTION);
546 return fragment.release();
547 }
548
549 void Node::prepend(const HeapVector<NodeOrString>& nodes, ExceptionState& except ionState)
550 {
551 insertBefore(convertNodesIntoNode(nodes, document()), firstChild(), exceptio nState);
552 }
553
554 void Node::append(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState)
555 {
556 appendChild(convertNodesIntoNode(nodes, document()), exceptionState);
557 }
558
559 void Node::before(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState)
560 {
561 Node* parent = parentNode();
562 if (!parent)
563 return;
564 Node* viablePreviousSibling = findViablePreviousSibling(*this, nodes);
565 parent->insertBefore(convertNodesIntoNode(nodes, document()), viablePrevious Sibling ? viablePreviousSibling->nextSibling() : parent->firstChild(), exception State);
566 }
567
568 void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptio nState)
569 {
570 Node* parent = parentNode();
571 if (!parent)
572 return;
573 Node* viableNextSibling = findViableNextSibling(*this, nodes);
574 parent->insertBefore(convertNodesIntoNode(nodes, document()), viableNextSibl ing, exceptionState);
575 }
576
577 void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& ex ceptionState)
578 {
579 Node* parent = parentNode();
580 if (!parent)
581 return;
582 Node* viableNextSibling = findViableNextSibling(*this, nodes);
583 RefPtrWillBeRawPtr<Node> node = convertNodesIntoNode(nodes, document());
584 if (parent == parentNode())
585 parent->replaceChild(node, this, exceptionState);
586 else
587 parent->insertBefore(node, viableNextSibling, exceptionState);
588 }
589
504 void Node::remove(ExceptionState& exceptionState) 590 void Node::remove(ExceptionState& exceptionState)
505 { 591 {
506 if (ContainerNode* parent = parentNode()) 592 if (ContainerNode* parent = parentNode())
507 parent->removeChild(this, exceptionState); 593 parent->removeChild(this, exceptionState);
508 } 594 }
509 595
510 void Node::normalize() 596 void Node::normalize()
511 { 597 {
512 updateDistribution(); 598 updateDistribution();
513 599
(...skipping 1988 matching lines...) Expand 10 before | Expand all | Expand 10 after
2502 2588
2503 void showNodePath(const blink::Node* node) 2589 void showNodePath(const blink::Node* node)
2504 { 2590 {
2505 if (node) 2591 if (node)
2506 node->showNodePathForThis(); 2592 node->showNodePathForThis();
2507 else 2593 else
2508 fprintf(stderr, "Cannot showNodePath for (nil)\n"); 2594 fprintf(stderr, "Cannot showNodePath for (nil)\n");
2509 } 2595 }
2510 2596
2511 #endif 2597 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698