 Chromium Code Reviews
 Chromium Code Reviews Issue 1085843002:
  Implement DOM: prepend, append, before, after & replaceWith  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/blink.git@master
    
  
    Issue 1085843002:
  Implement DOM: prepend, append, before, after & replaceWith  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/blink.git@master| OLD | NEW | 
|---|---|
| 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 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 LayoutObject* layoutObject = m_data.m_rareData->layoutObject(); | 375 LayoutObject* layoutObject = m_data.m_rareData->layoutObject(); | 
| 376 if (isElementNode()) | 376 if (isElementNode()) | 
| 377 delete static_cast<ElementRareData*>(m_data.m_rareData); | 377 delete static_cast<ElementRareData*>(m_data.m_rareData); | 
| 378 else | 378 else | 
| 379 delete static_cast<NodeRareData*>(m_data.m_rareData); | 379 delete static_cast<NodeRareData*>(m_data.m_rareData); | 
| 380 m_data.m_layoutObject = layoutObject; | 380 m_data.m_layoutObject = layoutObject; | 
| 381 clearFlag(HasRareDataFlag); | 381 clearFlag(HasRareDataFlag); | 
| 382 } | 382 } | 
| 383 #endif | 383 #endif | 
| 384 | 384 | 
| 385 PassRefPtrWillBeRawPtr<Node> Node::nodeOrStringToNode(const NodeOrString& nodeOr String) | |
| 386 { | |
| 387 if (nodeOrString.isNode()) | |
| 388 return nodeOrString.getAsNode(); | |
| 389 return Text::create(document(), nodeOrString.getAsString()); | |
| 390 } | |
| 391 | |
| 392 Node* Node::viablePreviosOrNextSibling(const HeapVector<NodeOrString>& nodes, bo ol 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.
 | |
| 393 { | |
| 394 Node* node = next ? this->nextSibling() : this->previousSibling(); | |
| 395 bool notFound; | |
| 396 while (node) { | |
| 397 notFound = true; | |
| 398 for (NodeOrString nodeOrString : nodes) { | |
| 399 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.
 | |
| 400 notFound = false; | |
| 401 break; | |
| 402 } | |
| 403 } | |
| 404 if (notFound) | |
| 405 return node; | |
| 406 node = next ? node->nextSibling() : node->previousSibling(); | |
| 407 } | |
| 408 return nullptr; | |
| 409 } | |
| 410 | |
| 411 PassRefPtrWillBeRawPtr<Node> Node::convertNodesIntoNode(const HeapVector<NodeOrS tring>& nodes) | |
| 412 { | |
| 413 if (nodes.size() == 1) | |
| 414 return nodeOrStringToNode(nodes[0]); | |
| 415 | |
| 416 RefPtrWillBeRawPtr<Node> node = DocumentFragment::create(document()); | |
| 417 for (const NodeOrString& nodeOrString : nodes) | |
| 418 node->appendChild(nodeOrStringToNode(nodeOrString), ASSERT_NO_EXCEPTION) ; | |
| 419 return node.release(); | |
| 420 } | |
| 421 | |
| 385 Node* Node::toNode() | 422 Node* Node::toNode() | 
| 386 { | 423 { | 
| 387 return this; | 424 return this; | 
| 388 } | 425 } | 
| 389 | 426 | 
| 390 short Node::tabIndex() const | 427 short Node::tabIndex() const | 
| 391 { | 428 { | 
| 392 return 0; | 429 return 0; | 
| 393 } | 430 } | 
| 394 | 431 | 
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 | 531 | 
| 495 PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC hild, ExceptionState& exceptionState) | 532 PassRefPtrWillBeRawPtr<Node> Node::appendChild(PassRefPtrWillBeRawPtr<Node> newC hild, ExceptionState& exceptionState) | 
| 496 { | 533 { | 
| 497 if (isContainerNode()) | 534 if (isContainerNode()) | 
| 498 return toContainerNode(this)->appendChild(newChild, exceptionState); | 535 return toContainerNode(this)->appendChild(newChild, exceptionState); | 
| 499 | 536 | 
| 500 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method."); | 537 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method."); | 
| 501 return nullptr; | 538 return nullptr; | 
| 502 } | 539 } | 
| 503 | 540 | 
| 541 void Node::prepend(const HeapVector<NodeOrString>& nodes, ExceptionState& except ionState) | |
| 542 { | |
| 543 RefPtrWillBeRawPtr<Node> nodeToPrepend = convertNodesIntoNode(nodes); | |
| 544 this->insertBefore(nodeToPrepend, this->firstChild(), exceptionState); | |
| 545 } | |
| 546 | |
| 547 void Node::append(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState) | |
| 548 { | |
| 549 RefPtrWillBeRawPtr<Node> nodeToAppend = convertNodesIntoNode(nodes); | |
| 550 this->appendChild(nodeToAppend, exceptionState); | |
| 551 } | |
| 552 | |
| 553 void Node::before(const HeapVector<NodeOrString>& nodes, ExceptionState& excepti onState) | |
| 554 { | |
| 555 Node* parent = parentNode(); | |
| 556 if (!parent) | |
| 557 return; | |
| 558 Node* viablePreviousSibling = viablePreviosOrNextSibling(nodes, false /* Pre vious */); | |
| 559 RefPtrWillBeRawPtr<Node> nodeToInsert = convertNodesIntoNode(nodes); | |
| 560 if (!viablePreviousSibling) | |
| 561 parent->insertBefore(nodeToInsert, parent->firstChild(), exceptionState) ; | |
| 562 else | |
| 563 parent->insertBefore(nodeToInsert, viablePreviousSibling, exceptionState ); | |
| 564 } | |
| 565 | |
| 566 void Node::after(const HeapVector<NodeOrString>& nodes, ExceptionState& exceptio nState) | |
| 567 { | |
| 568 Node* parent = parentNode(); | |
| 569 if (!parent) | |
| 570 return; | |
| 571 Node* viableNextSibling = viablePreviosOrNextSibling(nodes, true /* Next */) ; | |
| 572 RefPtrWillBeRawPtr<Node> nodeToInsert = convertNodesIntoNode(nodes); | |
| 573 parent->insertBefore(nodeToInsert, viableNextSibling, exceptionState); | |
| 574 } | |
| 575 | |
| 576 void Node::replaceWith(const HeapVector<NodeOrString>& nodes, ExceptionState& ex ceptionState) | |
| 577 { | |
| 578 Node* parent = parentNode(); | |
| 579 if (!parent) | |
| 580 return; | |
| 581 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.
 | |
| 582 RefPtrWillBeRawPtr<Node> nodeToReplaceWith = convertNodesIntoNode(nodes); | |
| 583 if (parent == parentNode()) | |
| 584 parent->replaceChild(nodeToReplaceWith, this, exceptionState); | |
| 585 else | |
| 586 parent->insertBefore(nodeToReplaceWith, viableNextSibling, exceptionStat e); | |
| 587 } | |
| 588 | |
| 504 void Node::remove(ExceptionState& exceptionState) | 589 void Node::remove(ExceptionState& exceptionState) | 
| 505 { | 590 { | 
| 506 if (ContainerNode* parent = parentNode()) | 591 if (ContainerNode* parent = parentNode()) | 
| 507 parent->removeChild(this, exceptionState); | 592 parent->removeChild(this, exceptionState); | 
| 508 } | 593 } | 
| 509 | 594 | 
| 510 void Node::normalize() | 595 void Node::normalize() | 
| 511 { | 596 { | 
| 512 updateDistribution(); | 597 updateDistribution(); | 
| 513 | 598 | 
| (...skipping 1988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2502 | 2587 | 
| 2503 void showNodePath(const blink::Node* node) | 2588 void showNodePath(const blink::Node* node) | 
| 2504 { | 2589 { | 
| 2505 if (node) | 2590 if (node) | 
| 2506 node->showNodePathForThis(); | 2591 node->showNodePathForThis(); | 
| 2507 else | 2592 else | 
| 2508 fprintf(stderr, "Cannot showNodePath for (nil)\n"); | 2593 fprintf(stderr, "Cannot showNodePath for (nil)\n"); | 
| 2509 } | 2594 } | 
| 2510 | 2595 | 
| 2511 #endif | 2596 #endif | 
| OLD | NEW |