1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "ContainerNode.h"
#include "AXObjectCache.h"
#include "ChildListMutationScope.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "ContainerNodeAlgorithms.h"
#include "Editor.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameView.h"
#include "HTMLNames.h"
#include "InlineTextBox.h"
#include "InsertionPoint.h"
#include "InspectorInstrumentation.h"
#include "JSNode.h"
#include "LoaderStrategy.h"
#include "MemoryCache.h"
#include "MutationEvent.h"
#include "NodeRenderStyle.h"
#include "NodeTraversal.h"
#include "Page.h"
#include "PlatformStrategies.h"
#include "RenderBox.h"
#include "RenderTheme.h"
#include "RenderWidget.h"
#include "ResourceLoadScheduler.h"
#include "RootInlineBox.h"
#include "TemplateContentDocumentFragment.h"
#include <wtf/CurrentTime.h>
#include <wtf/Vector.h>
#if ENABLE(DELETION_UI)
#include "DeleteButtonController.h"
#endif
using namespace std;
namespace WebCore {
static void dispatchChildInsertionEvents(Node*);
static void dispatchChildRemovalEvents(Node*);
static void updateTreeAfterInsertion(ContainerNode*, Node*, AttachBehavior);
typedef pair<RefPtr<Node>, unsigned> CallbackParameters;
typedef pair<NodeCallback, CallbackParameters> CallbackInfo;
typedef Vector<CallbackInfo> NodeCallbackQueue;
static NodeCallbackQueue* s_postAttachCallbackQueue;
static size_t s_attachDepth;
static bool s_shouldReEnableMemoryCacheCallsAfterAttach;
ChildNodesLazySnapshot* ChildNodesLazySnapshot::latestSnapshot = 0;
#ifndef NDEBUG
unsigned NoEventDispatchAssertion::s_count = 0;
#endif
static void collectChildrenAndRemoveFromOldParent(Node* node, NodeVector& nodes, ExceptionCode& ec)
{
if (node->nodeType() != Node::DOCUMENT_FRAGMENT_NODE) {
nodes.append(node);
if (ContainerNode* oldParent = node->parentNode())
oldParent->removeChild(node, ec);
return;
}
getChildNodes(node, nodes);
toContainerNode(node)->removeChildren();
}
void ContainerNode::removeDetachedChildren()
{
if (connectedSubframeCount()) {
for (Node* child = firstChild(); child; child = child->nextSibling())
child->updateAncestorConnectedSubframeCountForRemoval();
}
// FIXME: We should be able to ASSERT(!attached()) here: https://bugs.webkit.org/show_bug.cgi?id=107801
removeDetachedChildrenInContainer<Node, ContainerNode>(this);
}
void ContainerNode::takeAllChildrenFrom(ContainerNode* oldParent)
{
NodeVector children;
getChildNodes(oldParent, children);
if (oldParent->document()->hasMutationObserversOfType(MutationObserver::ChildList)) {
ChildListMutationScope mutation(oldParent);
for (unsigned i = 0; i < children.size(); ++i)
mutation.willRemoveChild(children[i].get());
}
// FIXME: We need to do notifyMutationObserversNodeWillDetach() for each child,
// probably inside removeDetachedChildrenInContainer.
oldParent->removeDetachedChildren();
for (unsigned i = 0; i < children.size(); ++i) {
if (children[i]->attached())
children[i]->detach();
// FIXME: We need a no mutation event version of adoptNode.
RefPtr<Node> child = document()->adoptNode(children[i].release(), ASSERT_NO_EXCEPTION);
parserAppendChild(child.get());
// FIXME: Together with adoptNode above, the tree scope might get updated recursively twice
// (if the document changed or oldParent was in a shadow tree, AND *this is in a shadow tree).
// Can we do better?
treeScope()->adoptIfNeeded(child.get());
if (attached() && !child->attached())
child->attach();
}
}
ContainerNode::~ContainerNode()
{
if (Document* document = documentInternal())
willBeDeletedFrom(document);
removeDetachedChildren();
}
static inline bool isChildTypeAllowed(ContainerNode* newParent, Node* child)
{
if (!child->isDocumentFragment())
return newParent->childTypeAllowed(child->nodeType());
for (Node* node = child->firstChild(); node; node = node->nextSibling()) {
if (!newParent->childTypeAllowed(node->nodeType()))
return false;
}
return true;
}
static inline bool isInTemplateContent(const Node* node)
{
#if ENABLE(TEMPLATE_ELEMENT)
Document* document = node->document();
return document && document == document->templateDocument();
#else
UNUSED_PARAM(node);
return false;
#endif
}
static inline bool containsConsideringHostElements(const Node* newChild, const Node* newParent)
{
return (newParent->isInShadowTree() || isInTemplateContent(newParent))
? newChild->containsIncludingHostElements(newParent)
: newChild->contains(newParent);
}
static inline ExceptionCode checkAcceptChild(ContainerNode* newParent, Node* newChild, Node* oldChild)
{
// Not mentioned in spec: throw NOT_FOUND_ERR if newChild is null
if (!newChild)
return NOT_FOUND_ERR;
// Use common case fast path if possible.
if ((newChild->isElementNode() || newChild->isTextNode()) && newParent->isElementNode()) {
ASSERT(!newParent->isReadOnlyNode());
ASSERT(!newParent->isDocumentTypeNode());
ASSERT(isChildTypeAllowed(newParent, newChild));
if (containsConsideringHostElements(newChild, newParent))
return HIERARCHY_REQUEST_ERR;
return 0;
}
// This should never happen, but also protect release builds from tree corruption.
ASSERT(!newChild->isPseudoElement());
if (newChild->isPseudoElement())
return HIERARCHY_REQUEST_ERR;
if (newParent->isReadOnlyNode())
return NO_MODIFICATION_ALLOWED_ERR;
if (newChild->inDocument() && newChild->isDocumentTypeNode())
return HIERARCHY_REQUEST_ERR;
if (containsConsideringHostElements(newChild, newParent))
return HIERARCHY_REQUEST_ERR;
if (oldChild && newParent->isDocumentNode()) {
if (!toDocument(newParent)->canReplaceChild(newChild, oldChild))
return HIERARCHY_REQUEST_ERR;
} else if (!isChildTypeAllowed(newParent, newChild))
return HIERARCHY_REQUEST_ERR;
return 0;
}
static inline bool checkAcceptChildGuaranteedNodeTypes(ContainerNode* newParent, Node* newChild, ExceptionCode& ec)
{
ASSERT(!newParent->isReadOnlyNode());
ASSERT(!newParent->isDocumentTypeNode());
ASSERT(isChildTypeAllowed(newParent, newChild));
if (newChild->contains(newParent)) {
ec = HIERARCHY_REQUEST_ERR;
return false;
}
return true;
}
static inline bool checkAddChild(ContainerNode* newParent, Node* newChild, ExceptionCode& ec)
{
ec = checkAcceptChild(newParent, newChild, 0);
if (ec)
return false;
return true;
}
static inline bool checkReplaceChild(ContainerNode* newParent, Node* newChild, Node* oldChild, ExceptionCode& ec)
{
ec = checkAcceptChild(newParent, newChild, oldChild);
if (ec)
return false;
return true;
}
bool ContainerNode::insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode& ec, AttachBehavior attachBehavior)
{
// Check that this node is not "floating".
// If it is, it can be deleted as a side effect of sending mutation events.
ASSERT(refCount() || parentOrShadowHostNode());
RefPtr<Node> protect(this);
ec = 0;
// insertBefore(node, 0) is equivalent to appendChild(node)
if (!refChild)
return appendChild(newChild, ec, attachBehavior);
// Make sure adding the new child is OK.
if (!checkAddChild(this, newChild.get(), ec))
return false;
// NOT_FOUND_ERR: Raised if refChild is not a child of this node
if (refChild->parentNode() != this) {
ec = NOT_FOUND_ERR;
return false;
}
if (refChild->previousSibling() == newChild || refChild == newChild) // nothing to do
return true;
RefPtr<Node> next = refChild;
NodeVector targets;
collectChildrenAndRemoveFromOldParent(newChild.get(), targets, ec);
if (ec)
return false;
if (targets.isEmpty())
return true;
// We need this extra check because collectChildrenAndRemoveFromOldParent() can fire mutation events.
if (!checkAcceptChildGuaranteedNodeTypes(this, newChild.get(), ec))
return false;
InspectorInstrumentation::willInsertDOMNode(document(), this);
ChildListMutationScope mutation(this);
for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
Node* child = it->get();
// Due to arbitrary code running in response to a DOM mutation event it's
// possible that "next" is no longer a child of "this".
// It's also possible that "child" has been inserted elsewhere.
// In either of those cases, we'll just stop.
if (next->parentNode() != this)
break;
if (child->parentNode())
break;
treeScope()->adoptIfNeeded(child);
insertBeforeCommon(next.get(), child);
updateTreeAfterInsertion(this, child, attachBehavior);
}
dispatchSubtreeModifiedEvent();
return true;
}
void ContainerNode::insertBeforeCommon(Node* nextChild, Node* newChild)
{
NoEventDispatchAssertion assertNoEventDispatch;
ASSERT(newChild);
ASSERT(!newChild->parentNode()); // Use insertBefore if you need to handle reparenting (and want DOM mutation events).
ASSERT(!newChild->nextSibling());
ASSERT(!newChild->previousSibling());
ASSERT(!newChild->isShadowRoot());
Node* prev = nextChild->previousSibling();
ASSERT(m_lastChild != prev);
nextChild->setPreviousSibling(newChild);
if (prev) {
ASSERT(m_firstChild != nextChild);
ASSERT(prev->nextSibling() == nextChild);
prev->setNextSibling(newChild);
} else {
ASSERT(m_firstChild == nextChild);
m_firstChild = newChild;
}
newChild->setParentOrShadowHostNode(this);
newChild->setPreviousSibling(prev);
newChild->setNextSibling(nextChild);
}
void ContainerNode::parserInsertBefore(PassRefPtr<Node> newChild, Node* nextChild)
{
ASSERT(newChild);
ASSERT(nextChild);
ASSERT(nextChild->parentNode() == this);
ASSERT(!newChild->isDocumentFragment());
#if ENABLE(TEMPLATE_ELEMENT)
ASSERT(!hasTagName(HTMLNames::templateTag));
#endif
if (nextChild->previousSibling() == newChild || nextChild == newChild) // nothing to do
return;
if (document() != newChild->document())
document()->adoptNode(newChild.get(), ASSERT_NO_EXCEPTION);
insertBeforeCommon(nextChild, newChild.get());
newChild->updateAncestorConnectedSubframeCountForInsertion();
ChildListMutationScope(this).childAdded(newChild.get());
childrenChanged(true, newChild->previousSibling(), nextChild, 1);
ChildNodeInsertionNotifier(this).notify(newChild.get());
}
bool ContainerNode::replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode& ec, AttachBehavior attachBehavior)
{
// Check that this node is not "floating".
// If it is, it can be deleted as a side effect of sending mutation events.
ASSERT(refCount() || parentOrShadowHostNode());
RefPtr<Node> protect(this);
ec = 0;
if (oldChild == newChild) // nothing to do
return true;
if (!oldChild) {
ec = NOT_FOUND_ERR;
return false;
}
// Make sure replacing the old child with the new is ok
if (!checkReplaceChild(this, newChild.get(), oldChild, ec))
return false;
// NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
if (oldChild->parentNode() != this) {
ec = NOT_FOUND_ERR;
return false;
}
ChildListMutationScope mutation(this);
RefPtr<Node> next = oldChild->nextSibling();
// Remove the node we're replacing
RefPtr<Node> removedChild = oldChild;
removeChild(oldChild, ec);
if (ec)
return false;
if (next && (next->previousSibling() == newChild || next == newChild)) // nothing to do
return true;
// Does this one more time because removeChild() fires a MutationEvent.
if (!checkReplaceChild(this, newChild.get(), oldChild, ec))
return false;
NodeVector targets;
collectChildrenAndRemoveFromOldParent(newChild.get(), targets, ec);
if (ec)
return false;
// Does this yet another check because collectChildrenAndRemoveFromOldParent() fires a MutationEvent.
if (!checkReplaceChild(this, newChild.get(), oldChild, ec))
return false;
InspectorInstrumentation::willInsertDOMNode(document(), this);
// Add the new child(ren)
for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
Node* child = it->get();
// Due to arbitrary code running in response to a DOM mutation event it's
// possible that "next" is no longer a child of "this".
// It's also possible that "child" has been inserted elsewhere.
// In either of those cases, we'll just stop.
if (next && next->parentNode() != this)
break;
if (child->parentNode())
break;
treeScope()->adoptIfNeeded(child);
// Add child before "next".
{
NoEventDispatchAssertion assertNoEventDispatch;
if (next)
insertBeforeCommon(next.get(), child);
else
appendChildToContainer(child, this);
}
updateTreeAfterInsertion(this, child, attachBehavior);
}
dispatchSubtreeModifiedEvent();
return true;
}
static void willRemoveChild(Node* child)
{
ASSERT(child->parentNode());
ChildListMutationScope(child->parentNode()).willRemoveChild(child);
child->notifyMutationObserversNodeWillDetach();
dispatchChildRemovalEvents(child);
child->document()->nodeWillBeRemoved(child); // e.g. mutation event listener can create a new range.
ChildFrameDisconnector(child).disconnect();
}
static void willRemoveChildren(ContainerNode* container)
{
NodeVector children;
getChildNodes(container, children);
container->document()->nodeChildrenWillBeRemoved(container);
ChildListMutationScope mutation(container);
for (NodeVector::const_iterator it = children.begin(); it != children.end(); ++it) {
Node* child = it->get();
mutation.willRemoveChild(child);
child->notifyMutationObserversNodeWillDetach();
// fire removed from document mutation events.
dispatchChildRemovalEvents(child);
}
ChildFrameDisconnector(container).disconnect(ChildFrameDisconnector::DescendantsOnly);
}
void ContainerNode::disconnectDescendantFrames()
{
ChildFrameDisconnector(this).disconnect();
}
bool ContainerNode::removeChild(Node* oldChild, ExceptionCode& ec)
{
// Check that this node is not "floating".
// If it is, it can be deleted as a side effect of sending mutation events.
ASSERT(refCount() || parentOrShadowHostNode());
RefPtr<Node> protect(this);
ec = 0;
// NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
if (isReadOnlyNode()) {
ec = NO_MODIFICATION_ALLOWED_ERR;
return false;
}
// NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
if (!oldChild || oldChild->parentNode() != this) {
ec = NOT_FOUND_ERR;
return false;
}
RefPtr<Node> child = oldChild;
document()->removeFocusedNodeOfSubtree(child.get());
#if ENABLE(FULLSCREEN_API)
document()->removeFullScreenElementOfSubtree(child.get());
#endif
// Events fired when blurring currently focused node might have moved this
// child into a different parent.
if (child->parentNode() != this) {
ec = NOT_FOUND_ERR;
return false;
}
willRemoveChild(child.get());
// Mutation events might have moved this child into a different parent.
if (child->parentNode() != this) {
ec = NOT_FOUND_ERR;
return false;
}
{
WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
Node* prev = child->previousSibling();
Node* next = child->nextSibling();
removeBetween(prev, next, child.get());
childrenChanged(false, prev, next, -1);
ChildNodeRemovalNotifier(this).notify(child.get());
}
dispatchSubtreeModifiedEvent();
return child;
}
void ContainerNode::removeBetween(Node* previousChild, Node* nextChild, Node* oldChild)
{
NoEventDispatchAssertion assertNoEventDispatch;
ASSERT(oldChild);
ASSERT(oldChild->parentNode() == this);
// Remove from rendering tree
if (oldChild->attached())
oldChild->detach();
if (nextChild)
nextChild->setPreviousSibling(previousChild);
if (previousChild)
previousChild->setNextSibling(nextChild);
if (m_firstChild == oldChild)
m_firstChild = nextChild;
if (m_lastChild == oldChild)
m_lastChild = previousChild;
oldChild->setPreviousSibling(0);
oldChild->setNextSibling(0);
oldChild->setParentOrShadowHostNode(0);
document()->adoptIfNeeded(oldChild);
}
void ContainerNode::parserRemoveChild(Node* oldChild)
{
ASSERT(oldChild);
ASSERT(oldChild->parentNode() == this);
ASSERT(!oldChild->isDocumentFragment());
Node* prev = oldChild->previousSibling();
Node* next = oldChild->nextSibling();
oldChild->updateAncestorConnectedSubframeCountForRemoval();
ChildListMutationScope(this).willRemoveChild(oldChild);
oldChild->notifyMutationObserversNodeWillDetach();
removeBetween(prev, next, oldChild);
childrenChanged(true, prev, next, -1);
ChildNodeRemovalNotifier(this).notify(oldChild);
}
// this differs from other remove functions because it forcibly removes all the children,
// regardless of read-only status or event exceptions, e.g.
void ContainerNode::removeChildren()
{
if (!m_firstChild)
return;
// The container node can be removed from event handlers.
RefPtr<ContainerNode> protect(this);
// exclude this node when looking for removed focusedNode since only children will be removed
document()->removeFocusedNodeOfSubtree(this, true);
#if ENABLE(FULLSCREEN_API)
document()->removeFullScreenElementOfSubtree(this, true);
#endif
// Do any prep work needed before actually starting to detach
// and remove... e.g. stop loading frames, fire unload events.
willRemoveChildren(protect.get());
NodeVector removedChildren;
{
WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
{
NoEventDispatchAssertion assertNoEventDispatch;
removedChildren.reserveInitialCapacity(childNodeCount());
while (RefPtr<Node> n = m_firstChild) {
removedChildren.append(m_firstChild);
removeBetween(0, m_firstChild->nextSibling(), m_firstChild);
}
}
childrenChanged(false, 0, 0, -static_cast<int>(removedChildren.size()));
for (size_t i = 0; i < removedChildren.size(); ++i)
ChildNodeRemovalNotifier(this).notify(removedChildren[i].get());
}
dispatchSubtreeModifiedEvent();
}
bool ContainerNode::appendChild(PassRefPtr<Node> newChild, ExceptionCode& ec, AttachBehavior attachBehavior)
{
RefPtr<ContainerNode> protect(this);
// Check that this node is not "floating".
// If it is, it can be deleted as a side effect of sending mutation events.
ASSERT(refCount() || parentOrShadowHostNode());
ec = 0;
// Make sure adding the new child is ok
if (!checkAddChild(this, newChild.get(), ec))
return false;
if (newChild == m_lastChild) // nothing to do
return newChild;
NodeVector targets;
collectChildrenAndRemoveFromOldParent(newChild.get(), targets, ec);
if (ec)
return false;
if (targets.isEmpty())
return true;
// We need this extra check because collectChildrenAndRemoveFromOldParent() can fire mutation events.
if (!checkAcceptChildGuaranteedNodeTypes(this, newChild.get(), ec))
return false;
InspectorInstrumentation::willInsertDOMNode(document(), this);
// Now actually add the child(ren)
ChildListMutationScope mutation(this);
for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
Node* child = it->get();
// If the child has a parent again, just stop what we're doing, because
// that means someone is doing something with DOM mutation -- can't re-parent
// a child that already has a parent.
if (child->parentNode())
break;
treeScope()->adoptIfNeeded(child);
// Append child to the end of the list
{
NoEventDispatchAssertion assertNoEventDispatch;
appendChildToContainer(child, this);
}
updateTreeAfterInsertion(this, child, attachBehavior);
}
dispatchSubtreeModifiedEvent();
return true;
}
void ContainerNode::parserAppendChild(PassRefPtr<Node> newChild)
{
ASSERT(newChild);
ASSERT(!newChild->parentNode()); // Use appendChild if you need to handle reparenting (and want DOM mutation events).
ASSERT(!newChild->isDocumentFragment());
#if ENABLE(TEMPLATE_ELEMENT)
ASSERT(!hasTagName(HTMLNames::templateTag));
#endif
if (document() != newChild->document())
document()->adoptNode(newChild.get(), ASSERT_NO_EXCEPTION);
Node* last = m_lastChild;
{
NoEventDispatchAssertion assertNoEventDispatch;
// FIXME: This method should take a PassRefPtr.
appendChildToContainer(newChild.get(), this);
treeScope()->adoptIfNeeded(newChild.get());
}
newChild->updateAncestorConnectedSubframeCountForInsertion();
ChildListMutationScope(this).childAdded(newChild.get());
childrenChanged(true, last, 0, 1);
ChildNodeInsertionNotifier(this).notify(newChild.get());
}
void ContainerNode::suspendPostAttachCallbacks()
{
if (!s_attachDepth) {
ASSERT(!s_shouldReEnableMemoryCacheCallsAfterAttach);
if (Page* page = document()->page()) {
// FIXME: How can this call be specific to one Page, while the
// s_attachDepth is a global? Doesn't make sense.
if (page->areMemoryCacheClientCallsEnabled()) {
page->setMemoryCacheClientCallsEnabled(false);
s_shouldReEnableMemoryCacheCallsAfterAttach = true;
}
}
platformStrategies()->loaderStrategy()->resourceLoadScheduler()->suspendPendingRequests();
}
++s_attachDepth;
}
void ContainerNode::resumePostAttachCallbacks()
{
if (s_attachDepth == 1) {
RefPtr<ContainerNode> protect(this);
if (s_postAttachCallbackQueue)
dispatchPostAttachCallbacks();
if (s_shouldReEnableMemoryCacheCallsAfterAttach) {
s_shouldReEnableMemoryCacheCallsAfterAttach = false;
if (Page* page = document()->page())
page->setMemoryCacheClientCallsEnabled(true);
}
platformStrategies()->loaderStrategy()->resourceLoadScheduler()->resumePendingRequests();
}
--s_attachDepth;
}
void ContainerNode::queuePostAttachCallback(NodeCallback callback, Node* node, unsigned callbackData)
{
if (!s_postAttachCallbackQueue)
s_postAttachCallbackQueue = new NodeCallbackQueue;
s_postAttachCallbackQueue->append(CallbackInfo(callback, CallbackParameters(node, callbackData)));
}
bool ContainerNode::postAttachCallbacksAreSuspended()
{
return s_attachDepth;
}
void ContainerNode::dispatchPostAttachCallbacks()
{
// We recalculate size() each time through the loop because a callback
// can add more callbacks to the end of the queue.
for (size_t i = 0; i < s_postAttachCallbackQueue->size(); ++i) {
const CallbackInfo& info = (*s_postAttachCallbackQueue)[i];
NodeCallback callback = info.first;
CallbackParameters params = info.second;
callback(params.first.get(), params.second);
}
s_postAttachCallbackQueue->clear();
}
static void needsStyleRecalcCallback(Node* node, unsigned data)
{
node->setNeedsStyleRecalc(static_cast<StyleChangeType>(data));
}
void ContainerNode::scheduleSetNeedsStyleRecalc(StyleChangeType changeType)
{
if (postAttachCallbacksAreSuspended())
queuePostAttachCallback(needsStyleRecalcCallback, this, static_cast<unsigned>(changeType));
else
setNeedsStyleRecalc(changeType);
}
void ContainerNode::attach(const AttachContext& context)
{
attachChildren(context);
Node::attach(context);
}
void ContainerNode::detach(const AttachContext& context)
{
detachChildren(context);
clearChildNeedsStyleRecalc();
Node::detach(context);
}
void ContainerNode::childrenChanged(bool changedByParser, Node*, Node*, int childCountDelta)
{
document()->incDOMTreeVersion();
if (!changedByParser && childCountDelta)
document()->updateRangesAfterChildrenChanged(this);
invalidateNodeListCachesInAncestors();
}
inline static void cloneChildNodesAvoidingDeleteButton(ContainerNode* parent, ContainerNode* clonedParent, HTMLElement* deleteButtonContainerElement)
{
ExceptionCode ec = 0;
for (Node* child = parent->firstChild(); child && !ec; child = child->nextSibling()) {
#if ENABLE(DELETION_UI)
if (child == deleteButtonContainerElement)
continue;
#else
UNUSED_PARAM(deleteButtonContainerElement);
#endif
RefPtr<Node> clonedChild = child->cloneNode(false);
clonedParent->appendChild(clonedChild, ec);
if (!ec && child->isContainerNode())
cloneChildNodesAvoidingDeleteButton(toContainerNode(child), toContainerNode(clonedChild.get()), deleteButtonContainerElement);
}
}
void ContainerNode::cloneChildNodes(ContainerNode *clone)
{
#if ENABLE(DELETION_UI)
HTMLElement* deleteButtonContainerElement = 0;
if (Frame* frame = document()->frame())
deleteButtonContainerElement = frame->editor().deleteButtonController()->containerElement();
cloneChildNodesAvoidingDeleteButton(this, clone, deleteButtonContainerElement);
#else
cloneChildNodesAvoidingDeleteButton(this, clone, 0);
#endif
}
bool ContainerNode::getUpperLeftCorner(FloatPoint& point) const
{
if (!renderer())
return false;
// What is this code really trying to do?
RenderObject* o = renderer();
RenderObject* p = o;
if (!o->isInline() || o->isReplaced()) {
point = o->localToAbsolute(FloatPoint(), UseTransforms);
return true;
}
// find the next text/image child, to get a position
while (o) {
p = o;
if (o->firstChild())
o = o->firstChild();
else if (o->nextSibling())
o = o->nextSibling();
else {
RenderObject* next = 0;
while (!next && o->parent()) {
o = o->parent();
next = o->nextSibling();
}
o = next;
if (!o)
break;
}
ASSERT(o);
if (!o->isInline() || o->isReplaced()) {
point = o->localToAbsolute(FloatPoint(), UseTransforms);
return true;
}
if (p->node() && p->node() == this && o->isText() && !o->isBR() && !toRenderText(o)->firstTextBox()) {
// do nothing - skip unrendered whitespace that is a child or next sibling of the anchor
} else if ((o->isText() && !o->isBR()) || o->isReplaced()) {
point = FloatPoint();
if (o->isText() && toRenderText(o)->firstTextBox()) {
point.move(toRenderText(o)->linesBoundingBox().x(), toRenderText(o)->firstTextBox()->root()->lineTop());
} else if (o->isBox()) {
RenderBox* box = toRenderBox(o);
point.moveBy(box->location());
}
point = o->container()->localToAbsolute(point, UseTransforms);
return true;
}
}
// If the target doesn't have any children or siblings that could be used to calculate the scroll position, we must be
// at the end of the document. Scroll to the bottom. FIXME: who said anything about scrolling?
if (!o && document()->view()) {
point = FloatPoint(0, document()->view()->contentsHeight());
return true;
}
return false;
}
bool ContainerNode::getLowerRightCorner(FloatPoint& point) const
{
if (!renderer())
return false;
RenderObject* o = renderer();
if (!o->isInline() || o->isReplaced()) {
RenderBox* box = toRenderBox(o);
point = o->localToAbsolute(LayoutPoint(box->size()), UseTransforms);
return true;
}
// find the last text/image child, to get a position
while (o) {
if (o->lastChild())
o = o->lastChild();
else if (o->previousSibling())
o = o->previousSibling();
else {
RenderObject* prev = 0;
while (!prev) {
o = o->parent();
if (!o)
return false;
prev = o->previousSibling();
}
o = prev;
}
ASSERT(o);
if (o->isText() || o->isReplaced()) {
point = FloatPoint();
if (o->isText()) {
RenderText* text = toRenderText(o);
IntRect linesBox = text->linesBoundingBox();
if (!linesBox.maxX() && !linesBox.maxY())
continue;
point.moveBy(linesBox.maxXMaxYCorner());
} else {
RenderBox* box = toRenderBox(o);
point.moveBy(box->frameRect().maxXMaxYCorner());
}
point = o->container()->localToAbsolute(point, UseTransforms);
return true;
}
}
return true;
}
LayoutRect ContainerNode::boundingBox() const
{
FloatPoint upperLeft, lowerRight;
bool foundUpperLeft = getUpperLeftCorner(upperLeft);
bool foundLowerRight = getLowerRightCorner(lowerRight);
// If we've found one corner, but not the other,
// then we should just return a point at the corner that we did find.
if (foundUpperLeft != foundLowerRight) {
if (foundUpperLeft)
lowerRight = upperLeft;
else
upperLeft = lowerRight;
}
return enclosingLayoutRect(FloatRect(upperLeft, lowerRight.expandedTo(upperLeft) - upperLeft));
}
unsigned ContainerNode::childNodeCount() const
{
unsigned count = 0;
Node *n;
for (n = firstChild(); n; n = n->nextSibling())
count++;
return count;
}
Node *ContainerNode::childNode(unsigned index) const
{
unsigned i;
Node *n = firstChild();
for (i = 0; n != 0 && i < index; i++)
n = n->nextSibling();
return n;
}
static void dispatchChildInsertionEvents(Node* child)
{
if (child->isInShadowTree())
return;
ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
RefPtr<Node> c = child;
RefPtr<Document> document = child->document();
if (c->parentNode() && document->hasListenerType(Document::DOMNODEINSERTED_LISTENER))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeInsertedEvent, true, c->parentNode()));
// dispatch the DOMNodeInsertedIntoDocument event to all descendants
if (c->inDocument() && document->hasListenerType(Document::DOMNODEINSERTEDINTODOCUMENT_LISTENER)) {
for (; c; c = NodeTraversal::next(c.get(), child))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeInsertedIntoDocumentEvent, false));
}
}
static void dispatchChildRemovalEvents(Node* child)
{
if (child->isInShadowTree()) {
InspectorInstrumentation::willRemoveDOMNode(child->document(), child);
return;
}
ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
willCreatePossiblyOrphanedTreeByRemoval(child);
InspectorInstrumentation::willRemoveDOMNode(child->document(), child);
RefPtr<Node> c = child;
RefPtr<Document> document = child->document();
// dispatch pre-removal mutation events
if (c->parentNode() && document->hasListenerType(Document::DOMNODEREMOVED_LISTENER))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeRemovedEvent, true, c->parentNode()));
// dispatch the DOMNodeRemovedFromDocument event to all descendants
if (c->inDocument() && document->hasListenerType(Document::DOMNODEREMOVEDFROMDOCUMENT_LISTENER)) {
for (; c; c = NodeTraversal::next(c.get(), child))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeRemovedFromDocumentEvent, false));
}
}
static void updateTreeAfterInsertion(ContainerNode* parent, Node* child, AttachBehavior attachBehavior)
{
ASSERT(parent->refCount());
ASSERT(child->refCount());
ChildListMutationScope(parent).childAdded(child);
parent->childrenChanged(false, child->previousSibling(), child->nextSibling(), 1);
ChildNodeInsertionNotifier(parent).notify(child);
// FIXME: Attachment should be the first operation in this function, but some code
// (for example, HTMLFormControlElement's autofocus support) requires this ordering.
if (parent->attached() && !child->attached() && child->parentNode() == parent) {
if (attachBehavior == AttachLazily)
child->lazyAttach();
else
child->attach();
}
dispatchChildInsertionEvents(child);
}
#ifndef NDEBUG
bool childAttachedAllowedWhenAttachingChildren(ContainerNode* node)
{
if (node->isShadowRoot())
return true;
if (node->isInsertionPoint())
return true;
if (node->isElementNode() && toElement(node)->shadow())
return true;
return false;
}
#endif
} // namespace WebCore
|