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

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
« no previous file with comments | « Source/core/dom/Node.h ('k') | Source/core/dom/ParentNode.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 485
486 PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC hild, ExceptionState& exceptionState) 486 PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC hild, ExceptionState& exceptionState)
487 { 487 {
488 if (isContainerNode()) 488 if (isContainerNode())
489 return toContainerNode(this)->appendChild(newChild, exceptionState); 489 return toContainerNode(this)->appendChild(newChild, exceptionState);
490 490
491 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method."); 491 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method.");
492 return nullptr; 492 return nullptr;
493 } 493 }
494 494
495 static bool isNodeInNodes(const Node& node, const HeapVector<NodeOrString>& node s)
496 {
497 for (const NodeOrString& nodeOrString : nodes) {
498 if (nodeOrString.isNode() && nodeOrString.getAsNode() == &node)
499 return true;
500 }
501 return false;
502 }
503
504 static Node* findViablePreviousSibling(const Node& node, const HeapVector<NodeOr String>& nodes)
505 {
506 for (Node* sibling = node.previousSibling(); sibling; sibling = sibling->pre viousSibling()) {
507 if (!isNodeInNodes(*sibling, nodes))
508 return sibling;
509 }
510 return nullptr;
511 }
512
513 static Node* findViableNextSibling(const Node& node, const HeapVector<NodeOrStri ng>& nodes)
514 {
515 for (Node* sibling = node.nextSibling(); sibling; sibling = sibling->nextSib ling()) {
516 if (!isNodeInNodes(*sibling, nodes))
517 return sibling;
518 }
519 return nullptr;
520 }
521
522 static PassRefPtrWillBeRawPtr<Node> nodeOrStringToNode(const NodeOrString& nodeO rString, Document& document)
523 {
524 if (nodeOrString.isNode())
525 return nodeOrString.getAsNode();
526 return Text::create(document, nodeOrString.getAsString());
527 }
528
529 static PassRefPtrWillBeRawPtr<Node> convertNodesIntoNode(const HeapVector<NodeOr String>& nodes, Document& document)
530 {
531 if (nodes.size() == 1)
532 return nodeOrStringToNode(nodes[0], document);
533
534 RefPtrWillBeRawPtr<Node> fragment = DocumentFragment::create(document);
535 for (const NodeOrString& nodeOrString : nodes)
536 fragment->appendChild(nodeOrStringToNode(nodeOrString, document), ASSERT _NO_EXCEPTION);
537 return fragment.release();
538 }
539
540 void Node::prepend(const HeapVector<NodeOrString>& nodes, ExceptionState& except ionState)
541 {
542 RefPtrWillBeRawPtr<Node> node = convertNodesIntoNode(nodes, document());
543 insertBefore(node, firstChild(), exceptionState);
544 }
545
546 void Node::append(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState)
547 {
548 RefPtrWillBeRawPtr<Node> node = convertNodesIntoNode(nodes, document());
549 appendChild(node, exceptionState);
550 }
551
552 void Node::before(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState)
553 {
554 Node* parent = parentNode();
555 if (!parent)
556 return;
557 Node* viablePreviousSibling = findViablePreviousSibling(*this, nodes);
558 RefPtrWillBeRawPtr<Node> node = convertNodesIntoNode(nodes, document());
559 parent->insertBefore(node, viablePreviousSibling ? viablePreviousSibling->ne xtSibling() : parent->firstChild(), exceptionState);
560 }
561
562 void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptio nState)
563 {
564 Node* parent = parentNode();
565 if (!parent)
566 return;
567 Node* viableNextSibling = findViableNextSibling(*this, nodes);
568 RefPtrWillBeRawPtr<Node> node = convertNodesIntoNode(nodes, document());
569 parent->insertBefore(node, viableNextSibling, exceptionState);
570 }
571
572 void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& ex ceptionState)
573 {
574 Node* parent = parentNode();
575 if (!parent)
576 return;
577 Node* viableNextSibling = findViableNextSibling(*this, nodes);
578 RefPtrWillBeRawPtr<Node> node = convertNodesIntoNode(nodes, document());
579 if (parent == parentNode())
580 parent->replaceChild(node, this, exceptionState);
581 else
582 parent->insertBefore(node, viableNextSibling, exceptionState);
583 }
584
495 void Node::remove(ExceptionState& exceptionState) 585 void Node::remove(ExceptionState& exceptionState)
496 { 586 {
497 if (ContainerNode* parent = parentNode()) 587 if (ContainerNode* parent = parentNode())
498 parent->removeChild(this, exceptionState); 588 parent->removeChild(this, exceptionState);
499 } 589 }
500 590
501 void Node::normalize() 591 void Node::normalize()
502 { 592 {
503 updateDistribution(); 593 updateDistribution();
504 594
(...skipping 1912 matching lines...) Expand 10 before | Expand all | Expand 10 after
2417 2507
2418 void showNodePath(const blink::Node* node) 2508 void showNodePath(const blink::Node* node)
2419 { 2509 {
2420 if (node) 2510 if (node)
2421 node->showNodePathForThis(); 2511 node->showNodePathForThis();
2422 else 2512 else
2423 fprintf(stderr, "Cannot showNodePath for (nil)\n"); 2513 fprintf(stderr, "Cannot showNodePath for (nil)\n");
2424 } 2514 }
2425 2515
2426 #endif 2516 #endif
OLDNEW
« 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