2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
7 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "core/dom/Node.h"
28 #include "bindings/v8/ExceptionState.h"
29 #include "bindings/v8/ScriptCallStackFactory.h"
30 #include "core/HTMLNames.h"
31 #include "core/XMLNames.h"
32 #include "core/accessibility/AXObjectCache.h"
33 #include "core/dom/Attr.h"
34 #include "core/dom/Attribute.h"
35 #include "core/dom/ChildListMutationScope.h"
36 #include "core/dom/ChildNodeList.h"
37 #include "core/dom/DOMImplementation.h"
38 #include "core/dom/Document.h"
39 #include "core/dom/DocumentFragment.h"
40 #include "core/dom/DocumentMarkerController.h"
41 #include "core/dom/DocumentType.h"
42 #include "core/dom/Element.h"
43 #include "core/dom/ElementRareData.h"
44 #include "core/dom/ElementTraversal.h"
45 #include "core/dom/ExceptionCode.h"
46 #include "core/dom/LiveNodeList.h"
47 #include "core/dom/NoEventDispatchAssertion.h"
48 #include "core/dom/NodeRareData.h"
49 #include "core/dom/NodeRenderingTraversal.h"
50 #include "core/dom/NodeTraversal.h"
51 #include "core/dom/ProcessingInstruction.h"
52 #include "core/dom/Range.h"
53 #include "core/dom/StaticNodeList.h"
54 #include "core/dom/TemplateContentDocumentFragment.h"
55 #include "core/dom/Text.h"
56 #include "core/dom/TreeScopeAdopter.h"
57 #include "core/dom/UserActionElementSet.h"
58 #include "core/dom/WeakNodeMap.h"
59 #include "core/dom/shadow/ElementShadow.h"
60 #include "core/dom/shadow/InsertionPoint.h"
61 #include "core/dom/shadow/ShadowRoot.h"
62 #include "core/editing/htmlediting.h"
63 #include "core/editing/markup.h"
64 #include "core/events/Event.h"
65 #include "core/events/EventDispatchMediator.h"
66 #include "core/events/EventDispatcher.h"
67 #include "core/events/EventListener.h"
68 #include "core/events/GestureEvent.h"
69 #include "core/events/KeyboardEvent.h"
70 #include "core/events/MouseEvent.h"
71 #include "core/events/MutationEvent.h"
72 #include "core/events/TextEvent.h"
73 #include "core/events/TouchEvent.h"
74 #include "core/events/UIEvent.h"
75 #include "core/events/WheelEvent.h"
76 #include "core/frame/EventHandlerRegistry.h"
77 #include "core/frame/LocalFrame.h"
78 #include "core/html/HTMLAnchorElement.h"
79 #include "core/html/HTMLDialogElement.h"
80 #include "core/html/HTMLFrameOwnerElement.h"
81 #include "core/html/HTMLStyleElement.h"
82 #include "core/page/ContextMenuController.h"
83 #include "core/page/EventHandler.h"
84 #include "core/page/Page.h"
85 #include "core/frame/Settings.h"
86 #include "core/rendering/FlowThreadController.h"
87 #include "core/rendering/RenderBox.h"
88 #include "core/svg/graphics/SVGImage.h"
89 #include "platform/Partitions.h"
90 #include "platform/TraceEvent.h"
91 #include "platform/TracedValue.h"
92 #include "wtf/HashSet.h"
93 #include "wtf/PassOwnPtr.h"
94 #include "wtf/RefCountedLeakCounter.h"
95 #include "wtf/Vector.h"
96 #include "wtf/text/CString.h"
97 #include "wtf/text/StringBuilder.h"
101 using namespace HTMLNames;
104 void* Node::operator new(size_t size)
106 ASSERT(isMainThread());
107 return partitionAlloc(Partitions::getObjectModelPartition(), size);
110 void Node::operator delete(void* ptr)
112 ASSERT(isMainThread());
117 #if DUMP_NODE_STATISTICS
118 static HashSet<Node*>& liveNodeSet()
120 DEFINE_STATIC_LOCAL(HashSet<Node*>, s_liveNodeSet, ());
121 return s_liveNodeSet;
125 void Node::dumpStatistics()
127 #if DUMP_NODE_STATISTICS
128 size_t nodesWithRareData = 0;
130 size_t elementNodes = 0;
131 size_t attrNodes = 0;
132 size_t textNodes = 0;
133 size_t cdataNodes = 0;
134 size_t commentNodes = 0;
136 size_t documentNodes = 0;
137 size_t docTypeNodes = 0;
138 size_t fragmentNodes = 0;
139 size_t shadowRootNodes = 0;
141 HashMap<String, size_t> perTagCount;
143 size_t attributes = 0;
144 size_t elementsWithAttributeStorage = 0;
145 size_t elementsWithRareData = 0;
146 size_t elementsWithNamedNodeMap = 0;
148 for (HashSet<Node*>::iterator it = liveNodeSet().begin(); it != liveNodeSet().end(); ++it) {
151 if (node->hasRareData()) {
153 if (node->isElementNode()) {
154 ++elementsWithRareData;
155 if (toElement(node)->hasNamedNodeMap())
156 ++elementsWithNamedNodeMap;
160 switch (node->nodeType()) {
165 Element* element = toElement(node);
166 HashMap<String, size_t>::AddResult result = perTagCount.add(element->tagName(), 1);
167 if (!result.isNewEntry)
168 result.storedValue->value++;
170 if (const ElementData* elementData = element->elementData()) {
171 attributes += elementData->length();
172 ++elementsWithAttributeStorage;
176 case ATTRIBUTE_NODE: {
184 case CDATA_SECTION_NODE: {
192 case PROCESSING_INSTRUCTION_NODE: {
196 case DOCUMENT_NODE: {
200 case DOCUMENT_TYPE_NODE: {
204 case DOCUMENT_FRAGMENT_NODE: {
205 if (node->isShadowRoot())
214 printf("Number of Nodes: %d\n\n", liveNodeSet().size());
215 printf("Number of Nodes with RareData: %zu\n\n", nodesWithRareData);
217 printf("NodeType distribution:\n");
218 printf(" Number of Element nodes: %zu\n", elementNodes);
219 printf(" Number of Attribute nodes: %zu\n", attrNodes);
220 printf(" Number of Text nodes: %zu\n", textNodes);
221 printf(" Number of CDATASection nodes: %zu\n", cdataNodes);
222 printf(" Number of Comment nodes: %zu\n", commentNodes);
223 printf(" Number of ProcessingInstruction nodes: %zu\n", piNodes);
224 printf(" Number of Document nodes: %zu\n", documentNodes);
225 printf(" Number of DocumentType nodes: %zu\n", docTypeNodes);
226 printf(" Number of DocumentFragment nodes: %zu\n", fragmentNodes);
227 printf(" Number of ShadowRoot nodes: %zu\n", shadowRootNodes);
229 printf("Element tag name distibution:\n");
230 for (HashMap<String, size_t>::iterator it = perTagCount.begin(); it != perTagCount.end(); ++it)
231 printf(" Number of <%s> tags: %zu\n", it->key.utf8().data(), it->value);
233 printf("Attributes:\n");
234 printf(" Number of Attributes (non-Node and Node): %zu [%zu]\n", attributes, sizeof(Attribute));
235 printf(" Number of Elements with attribute storage: %zu [%zu]\n", elementsWithAttributeStorage, sizeof(ElementData));
236 printf(" Number of Elements with RareData: %zu\n", elementsWithRareData);
237 printf(" Number of Elements with NamedNodeMap: %zu [%zu]\n", elementsWithNamedNodeMap, sizeof(NamedNodeMap));
241 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, nodeCounter, ("WebCoreNode"));
243 void Node::trackForDebugging()
246 nodeCounter.increment();
249 #if DUMP_NODE_STATISTICS
250 liveNodeSet().add(this);
254 Node::Node(TreeScope* treeScope, ConstructionType type)
256 , m_parentOrShadowHostNode(nullptr)
257 , m_treeScope(treeScope)
258 , m_previous(nullptr)
261 ASSERT(m_treeScope || type == CreateDocument || type == CreateShadowRoot);
262 ScriptWrappable::init(this);
265 m_treeScope->guardRef();
268 #if !defined(NDEBUG) || (defined(DUMP_NODE_STATISTICS) && DUMP_NODE_STATISTICS)
271 InspectorCounters::incrementCounter(InspectorCounters::NodeCounter);
277 nodeCounter.decrement();
280 #if DUMP_NODE_STATISTICS
281 liveNodeSet().remove(this);
288 RELEASE_ASSERT(!renderer());
290 if (!isContainerNode())
291 willBeDeletedFromDocument();
294 m_previous->setNextSibling(0);
296 m_next->setPreviousSibling(0);
299 m_treeScope->guardDeref();
301 // With Oilpan, the rare data finalizer also asserts for
302 // this condition (we cannot directly access it here.)
303 RELEASE_ASSERT(hasRareData() || !renderer());
306 InspectorCounters::decrementCounter(InspectorCounters::NodeCounter);
308 if (getFlag(HasWeakReferencesFlag))
309 WeakNodeMap::notifyNodeDestroyed(this);
313 // With Oilpan all of this is handled with weak processing of the document.
314 void Node::willBeDeletedFromDocument()
316 if (!isTreeScopeInitialized())
319 Document& document = this->document();
321 if (hasEventTargetData()) {
322 clearEventTargetData();
323 document.didClearTouchEventHandlers(this);
324 if (document.frameHost())
325 document.frameHost()->eventHandlerRegistry().didRemoveAllEventHandlers(*this);
328 if (AXObjectCache* cache = document.existingAXObjectCache())
331 document.markers().removeMarkers(this);
335 NodeRareData* Node::rareData() const
337 ASSERT_WITH_SECURITY_IMPLICATION(hasRareData());
338 return static_cast<NodeRareData*>(m_data.m_rareData);
341 NodeRareData& Node::ensureRareData()
347 m_data.m_rareData = ElementRareData::create(m_data.m_renderer);
349 m_data.m_rareData = NodeRareData::create(m_data.m_renderer);
351 ASSERT(m_data.m_rareData);
353 setFlag(HasRareDataFlag);
358 void Node::clearRareData()
360 ASSERT(hasRareData());
361 ASSERT(!transientMutationObserverRegistry() || transientMutationObserverRegistry()->isEmpty());
363 RenderObject* renderer = m_data.m_rareData->renderer();
365 delete static_cast<ElementRareData*>(m_data.m_rareData);
367 delete static_cast<NodeRareData*>(m_data.m_rareData);
368 m_data.m_renderer = renderer;
369 clearFlag(HasRareDataFlag);
378 short Node::tabIndex() const
383 String Node::nodeValue() const
388 void Node::setNodeValue(const String&)
390 // By default, setting nodeValue has no effect.
393 PassRefPtrWillBeRawPtr<NodeList> Node::childNodes()
395 if (isContainerNode())
396 return ensureRareData().ensureNodeLists().ensureChildNodeList(toContainerNode(*this));
397 return ensureRareData().ensureNodeLists().ensureEmptyChildNodeList(*this);
400 Node& Node::lastDescendantOrSelf() const
402 Node* n = const_cast<Node*>(this);
403 while (n && n->lastChild())
409 Node* Node::pseudoAwarePreviousSibling() const
411 if (parentElement() && !previousSibling()) {
412 Element* parent = parentElement();
413 if (isAfterPseudoElement() && parent->lastChild())
414 return parent->lastChild();
415 if (!isBeforePseudoElement())
416 return parent->pseudoElement(BEFORE);
418 return previousSibling();
421 Node* Node::pseudoAwareNextSibling() const
423 if (parentElement() && !nextSibling()) {
424 Element* parent = parentElement();
425 if (isBeforePseudoElement() && parent->firstChild())
426 return parent->firstChild();
427 if (!isAfterPseudoElement())
428 return parent->pseudoElement(AFTER);
430 return nextSibling();
433 Node* Node::pseudoAwareFirstChild() const
435 if (isElementNode()) {
436 const Element* currentElement = toElement(this);
437 Node* first = currentElement->pseudoElement(BEFORE);
440 first = currentElement->firstChild();
442 first = currentElement->pseudoElement(AFTER);
449 Node* Node::pseudoAwareLastChild() const
451 if (isElementNode()) {
452 const Element* currentElement = toElement(this);
453 Node* last = currentElement->pseudoElement(AFTER);
456 last = currentElement->lastChild();
458 last = currentElement->pseudoElement(BEFORE);
465 void Node::insertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node* refChild, ExceptionState& exceptionState)
467 if (isContainerNode())
468 toContainerNode(this)->insertBefore(newChild, refChild, exceptionState);
470 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method.");
473 void Node::replaceChild(PassRefPtrWillBeRawPtr<Node> newChild, Node* oldChild, ExceptionState& exceptionState)
475 if (isContainerNode())
476 toContainerNode(this)->replaceChild(newChild, oldChild, exceptionState);
478 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method.");
481 void Node::removeChild(Node* oldChild, ExceptionState& exceptionState)
483 if (isContainerNode())
484 toContainerNode(this)->removeChild(oldChild, exceptionState);
486 exceptionState.throwDOMException(NotFoundError, "This node type does not support this method.");
489 void Node::appendChild(PassRefPtrWillBeRawPtr<Node> newChild, ExceptionState& exceptionState)
491 if (isContainerNode())
492 toContainerNode(this)->appendChild(newChild, exceptionState);
494 exceptionState.throwDOMException(HierarchyRequestError, "This node type does not support this method.");
497 void Node::remove(ExceptionState& exceptionState)
499 if (ContainerNode* parent = parentNode())
500 parent->removeChild(this, exceptionState);
503 void Node::normalize()
505 // Go through the subtree beneath us, normalizing all nodes. This means that
506 // any two adjacent text nodes are merged and any empty text nodes are removed.
508 RefPtrWillBeRawPtr<Node> node = this;
509 while (Node* firstChild = node->firstChild())
512 if (node->isElementNode())
513 toElement(node)->normalizeAttributes();
518 if (node->nodeType() == TEXT_NODE)
519 node = toText(node)->mergeNextSiblingNodesIfPossible();
521 node = NodeTraversal::nextPostOrder(*node);
525 const AtomicString& Node::localName() const
530 const AtomicString& Node::namespaceURI() const
535 bool Node::isContentEditable(UserSelectAllTreatment treatment)
537 document().updateRenderTreeIfNeeded();
538 return rendererIsEditable(Editable, treatment);
541 bool Node::isContentRichlyEditable()
543 document().updateRenderTreeIfNeeded();
544 return rendererIsEditable(RichlyEditable, UserSelectAllIsAlwaysNonEditable);
547 bool Node::rendererIsEditable(EditableLevel editableLevel, UserSelectAllTreatment treatment) const
549 if (isPseudoElement())
552 // Ideally we'd call ASSERT(!needsStyleRecalc()) here, but
553 // ContainerNode::setFocus() calls setNeedsStyleRecalc(), so the assertion
554 // would fire in the middle of Document::setFocusedNode().
556 for (const Node* node = this; node; node = node->parentNode()) {
557 if ((node->isHTMLElement() || node->isDocumentNode()) && node->renderer()) {
558 // Elements with user-select: all style are considered atomic
559 // therefore non editable.
560 if (Position::nodeIsUserSelectAll(node) && treatment == UserSelectAllIsAlwaysNonEditable)
562 switch (node->renderer()->style()->userModify()) {
567 case READ_WRITE_PLAINTEXT_ONLY:
568 return editableLevel != RichlyEditable;
570 ASSERT_NOT_REACHED();
578 bool Node::isEditableToAccessibility(EditableLevel editableLevel) const
580 if (rendererIsEditable(editableLevel))
583 // FIXME: Respect editableLevel for ARIA editable elements.
584 if (editableLevel == RichlyEditable)
587 ASSERT(AXObjectCache::accessibilityEnabled());
588 ASSERT(document().existingAXObjectCache());
590 if (AXObjectCache* cache = document().existingAXObjectCache())
591 return cache->rootAXEditableElement(this);
596 bool Node::shouldUseInputMethod()
598 return isContentEditable(UserSelectAllIsAlwaysNonEditable);
601 RenderBox* Node::renderBox() const
603 RenderObject* renderer = this->renderer();
604 return renderer && renderer->isBox() ? toRenderBox(renderer) : 0;
607 RenderBoxModelObject* Node::renderBoxModelObject() const
609 RenderObject* renderer = this->renderer();
610 return renderer && renderer->isBoxModelObject() ? toRenderBoxModelObject(renderer) : 0;
613 LayoutRect Node::boundingBox() const
616 return renderer()->absoluteBoundingBoxRect();
620 bool Node::hasNonEmptyBoundingBox() const
622 // Before calling absoluteRects, check for the common case where the renderer
623 // is non-empty, since this is a faster check and almost always returns true.
624 RenderBoxModelObject* box = renderBoxModelObject();
627 if (!box->borderBoundingBox().isEmpty())
630 Vector<IntRect> rects;
631 FloatPoint absPos = renderer()->localToAbsolute();
632 renderer()->absoluteRects(rects, flooredLayoutPoint(absPos));
633 size_t n = rects.size();
634 for (size_t i = 0; i < n; ++i)
635 if (!rects[i].isEmpty())
642 inline static ShadowRoot* oldestShadowRootFor(const Node* node)
644 if (!node->isElementNode())
646 if (ElementShadow* shadow = toElement(node)->shadow())
647 return shadow->oldestShadowRoot();
652 void Node::recalcDistribution()
654 if (isElementNode()) {
655 if (ElementShadow* shadow = toElement(this)->shadow())
656 shadow->distributeIfNeeded();
659 for (Node* child = firstChild(); child; child = child->nextSibling()) {
660 if (child->childNeedsDistributionRecalc())
661 child->recalcDistribution();
664 for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot()) {
665 if (root->childNeedsDistributionRecalc())
666 root->recalcDistribution();
669 clearChildNeedsDistributionRecalc();
672 void Node::setIsLink(bool isLink)
674 setFlag(isLink && !SVGImage::isInSVGImage(toElement(this)), IsLinkFlag);
677 void Node::setNeedsStyleInvalidation()
679 setFlag(NeedsStyleInvalidationFlag);
680 markAncestorsWithChildNeedsStyleInvalidation();
683 void Node::markAncestorsWithChildNeedsStyleInvalidation()
685 for (Node* node = parentOrShadowHostNode(); node && !node->childNeedsStyleInvalidation(); node = node->parentOrShadowHostNode())
686 node->setChildNeedsStyleInvalidation();
687 document().scheduleRenderTreeUpdateIfNeeded();
690 void Node::markAncestorsWithChildNeedsDistributionRecalc()
692 for (Node* node = this; node && !node->childNeedsDistributionRecalc(); node = node->parentOrShadowHostNode())
693 node->setChildNeedsDistributionRecalc();
694 document().scheduleRenderTreeUpdateIfNeeded();
699 PassRefPtr<JSONArray> jsStackAsJSONArray()
701 RefPtr<JSONArray> jsonArray = JSONArray::create();
702 RefPtrWillBeRawPtr<ScriptCallStack> stack = createScriptCallStack(10);
704 return jsonArray.release();
705 for (size_t i = 0; i < stack->size(); i++)
706 jsonArray->pushString(stack->at(i).functionName());
707 return jsonArray.release();
710 PassRefPtr<JSONObject> jsonObjectForStyleInvalidation(unsigned nodeCount, const Node* rootNode)
712 RefPtr<JSONObject> jsonObject = JSONObject::create();
713 jsonObject->setNumber("node_count", nodeCount);
714 jsonObject->setString("root_node", rootNode->debugName());
715 jsonObject->setArray("js_stack", jsStackAsJSONArray());
716 return jsonObject.release();
719 } // anonymous namespace'd functions supporting traceStyleChange
721 unsigned Node::styledSubtreeSize() const
723 unsigned nodeCount = 0;
725 for (const Node* node = this; node; node = NodeTraversal::next(*node, this)) {
726 if (node->isTextNode() || node->isElementNode())
728 for (ShadowRoot* root = node->youngestShadowRoot(); root; root = root->olderShadowRoot())
729 nodeCount += root->styledSubtreeSize();
735 void Node::traceStyleChange(StyleChangeType changeType)
737 static const unsigned kMinLoggedSize = 100;
738 unsigned nodeCount = styledSubtreeSize();
739 if (nodeCount < kMinLoggedSize)
742 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("style.debug"),
743 "Node::setNeedsStyleRecalc",
744 "data", TracedValue::fromJSONValue(jsonObjectForStyleInvalidation(nodeCount, this))
748 void Node::traceStyleChangeIfNeeded(StyleChangeType changeType)
750 // TRACE_EVENT_CATEGORY_GROUP_ENABLED macro loads a global static bool into our local bool.
751 bool styleTracingEnabled;
752 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("style.debug"), &styleTracingEnabled);
753 if (UNLIKELY(styleTracingEnabled))
754 traceStyleChange(changeType);
757 inline void Node::setStyleChange(StyleChangeType changeType)
759 m_nodeFlags = (m_nodeFlags & ~StyleChangeMask) | changeType;
762 void Node::markAncestorsWithChildNeedsStyleRecalc()
764 for (ContainerNode* p = parentOrShadowHostNode(); p && !p->childNeedsStyleRecalc(); p = p->parentOrShadowHostNode())
765 p->setChildNeedsStyleRecalc();
766 document().scheduleRenderTreeUpdateIfNeeded();
769 void Node::setNeedsStyleRecalc(StyleChangeType changeType)
771 ASSERT(changeType != NoStyleChange);
772 if (!inActiveDocument())
775 StyleChangeType existingChangeType = styleChangeType();
776 if (changeType > existingChangeType) {
777 setStyleChange(changeType);
778 if (changeType >= SubtreeStyleChange)
779 traceStyleChangeIfNeeded(changeType);
782 if (existingChangeType == NoStyleChange)
783 markAncestorsWithChildNeedsStyleRecalc();
785 if (isElementNode() && hasRareData())
786 toElement(*this).setAnimationStyleChange(false);
789 void Node::clearNeedsStyleRecalc()
791 m_nodeFlags &= ~StyleChangeMask;
793 clearSVGFilterNeedsLayerUpdate();
795 if (isElementNode() && hasRareData())
796 toElement(*this).setAnimationStyleChange(false);
799 bool Node::inActiveDocument() const
801 return inDocument() && document().isActive();
804 Node* Node::focusDelegate()
809 bool Node::shouldHaveFocusAppearance() const
815 bool Node::isInert() const
817 const HTMLDialogElement* dialog = document().activeModalDialog();
818 if (dialog && this != document() && !NodeRenderingTraversal::contains(dialog, this))
820 return document().ownerElement() && document().ownerElement()->isInert();
823 unsigned Node::nodeIndex() const
825 Node *_tempNode = previousSibling();
827 for ( count=0; _tempNode; count++ )
828 _tempNode = _tempNode->previousSibling();
832 void Node::invalidateNodeListCachesInAncestors(const QualifiedName* attrName, Element* attributeOwnerElement)
834 if (hasRareData() && (!attrName || isAttributeNode())) {
835 if (NodeListsNodeData* lists = rareData()->nodeLists())
836 lists->clearChildNodeListCache();
839 // Modifications to attributes that are not associated with an Element can't invalidate NodeList caches.
840 if (attrName && !attributeOwnerElement)
843 if (!document().shouldInvalidateNodeListCaches(attrName))
846 document().invalidateNodeListCaches(attrName);
848 for (Node* node = this; node; node = node->parentNode()) {
849 if (NodeListsNodeData* lists = node->nodeLists())
850 lists->invalidateCaches(attrName);
854 NodeListsNodeData* Node::nodeLists()
856 return hasRareData() ? rareData()->nodeLists() : 0;
859 void Node::clearNodeLists()
861 rareData()->clearNodeLists();
864 bool Node::isDescendantOf(const Node *other) const
866 // Return true if other is an ancestor of this, otherwise false
867 if (!other || !other->hasChildren() || inDocument() != other->inDocument())
869 if (other->treeScope() != treeScope())
871 if (other->isTreeScope())
872 return !isTreeScope();
873 for (const ContainerNode* n = parentNode(); n; n = n->parentNode()) {
880 bool Node::contains(const Node* node) const
884 return this == node || node->isDescendantOf(this);
887 bool Node::containsIncludingShadowDOM(const Node* node) const
895 if (document() != node->document())
898 if (inDocument() != node->inDocument())
901 bool hasChildren = isContainerNode() && toContainerNode(this)->hasChildren();
902 bool hasShadow = isElementNode() && toElement(this)->shadow();
903 if (!hasChildren && !hasShadow)
906 for (; node; node = node->shadowHost()) {
907 if (treeScope() == node->treeScope())
908 return contains(node);
914 bool Node::containsIncludingHostElements(const Node& node) const
916 const Node* current = &node;
920 if (current->isDocumentFragment() && toDocumentFragment(current)->isTemplateContent())
921 current = static_cast<const TemplateContentDocumentFragment*>(current)->host();
923 current = current->parentOrShadowHostNode();
928 Node* Node::commonAncestor(const Node& other, Node* (*parent)(const Node&))
932 if (document() != other.document())
935 for (Node* node = this; node; node = parent(*node)) {
941 for (const Node* node = &other; node; node = parent(*node)) {
946 Node* thisIterator = this;
947 const Node* otherIterator = &other;
948 if (thisDepth > otherDepth) {
949 for (int i = thisDepth; i > otherDepth; --i)
950 thisIterator = parent(*thisIterator);
951 } else if (otherDepth > thisDepth) {
952 for (int i = otherDepth; i > thisDepth; --i)
953 otherIterator = parent(*otherIterator);
955 while (thisIterator) {
956 if (thisIterator == otherIterator)
958 thisIterator = parent(*thisIterator);
959 otherIterator = parent(*otherIterator);
961 ASSERT(!otherIterator);
965 void Node::reattach(const AttachContext& context)
967 AttachContext reattachContext(context);
968 reattachContext.performingReattach = true;
970 // We only need to detach if the node has already been through attach().
971 if (styleChangeType() < NeedsReattachStyleChange)
972 detach(reattachContext);
973 attach(reattachContext);
976 void Node::attach(const AttachContext&)
978 ASSERT(document().inStyleRecalc() || isDocumentNode());
979 ASSERT(needsAttach());
980 ASSERT(!renderer() || (renderer()->style() && (renderer()->parent() || renderer()->isRenderView())));
982 clearNeedsStyleRecalc();
984 if (AXObjectCache* cache = document().axObjectCache())
985 cache->updateCacheAfterNodeIsAttached(this);
989 static Node* detachingNode;
991 bool Node::inDetach() const
993 return detachingNode == this;
997 void Node::detach(const AttachContext& context)
999 ASSERT(document().lifecycle().stateAllowsDetach());
1000 DocumentLifecycle::DetachScope willDetach(document().lifecycle());
1003 ASSERT(!detachingNode);
1004 detachingNode = this;
1008 renderer()->destroyAndCleanupAnonymousWrappers();
1011 // Do not remove the element's hovered and active status
1012 // if performing a reattach.
1013 if (!context.performingReattach) {
1014 Document& doc = document();
1015 if (isUserActionElement()) {
1017 doc.hoveredNodeDetached(this);
1018 if (inActiveChain())
1019 doc.activeChainNodeDetached(this);
1020 doc.userActionElements().didDetach(this);
1024 setStyleChange(NeedsReattachStyleChange);
1025 setChildNeedsStyleRecalc();
1027 if (StyleResolver* resolver = document().styleResolver())
1028 resolver->ruleFeatureSet().styleInvalidator().clearInvalidation(*this);
1029 clearChildNeedsStyleInvalidation();
1030 clearNeedsStyleInvalidation();
1037 void Node::reattachWhitespaceSiblings(Text* start)
1039 for (Node* sibling = start; sibling; sibling = sibling->nextSibling()) {
1040 if (sibling->isTextNode() && toText(sibling)->containsOnlyWhitespace()) {
1041 bool hadRenderer = !!sibling->renderer();
1042 sibling->reattach();
1043 // If the reattach didn't toggle the visibility of the whitespace we don't
1044 // need to continue reattaching siblings since they won't toggle visibility
1046 if (hadRenderer == !!sibling->renderer())
1048 } else if (sibling->renderer()) {
1054 // FIXME: This code is used by editing. Seems like it could move over there and not pollute Node.
1055 Node *Node::previousNodeConsideringAtomicNodes() const
1057 if (previousSibling()) {
1058 Node *n = previousSibling();
1059 while (!isAtomicNode(n) && n->lastChild())
1063 else if (parentNode()) {
1064 return parentNode();
1071 Node *Node::nextNodeConsideringAtomicNodes() const
1073 if (!isAtomicNode(this) && firstChild())
1074 return firstChild();
1076 return nextSibling();
1077 const Node *n = this;
1078 while (n && !n->nextSibling())
1079 n = n->parentNode();
1081 return n->nextSibling();
1085 Node *Node::previousLeafNode() const
1087 Node *node = previousNodeConsideringAtomicNodes();
1089 if (isAtomicNode(node))
1091 node = node->previousNodeConsideringAtomicNodes();
1096 Node *Node::nextLeafNode() const
1098 Node *node = nextNodeConsideringAtomicNodes();
1100 if (isAtomicNode(node))
1102 node = node->nextNodeConsideringAtomicNodes();
1107 RenderStyle* Node::virtualComputedStyle(PseudoId pseudoElementSpecifier)
1109 return parentOrShadowHostNode() ? parentOrShadowHostNode()->computedStyle(pseudoElementSpecifier) : 0;
1112 int Node::maxCharacterOffset() const
1114 ASSERT_NOT_REACHED();
1118 // FIXME: Shouldn't these functions be in the editing code? Code that asks questions about HTML in the core DOM class
1119 // is obviously misplaced.
1120 bool Node::canStartSelection() const
1122 if (rendererIsEditable())
1126 RenderStyle* style = renderer()->style();
1127 // We allow selections to begin within an element that has -webkit-user-select: none set,
1128 // but if the element is draggable then dragging should take priority over selection.
1129 if (style->userDrag() == DRAG_ELEMENT && style->userSelect() == SELECT_NONE)
1132 return parentOrShadowHostNode() ? parentOrShadowHostNode()->canStartSelection() : true;
1135 Element* Node::shadowHost() const
1137 if (ShadowRoot* root = containingShadowRoot())
1138 return root->host();
1142 Node* Node::deprecatedShadowAncestorNode() const
1144 if (ShadowRoot* root = containingShadowRoot())
1145 return root->host();
1147 return const_cast<Node*>(this);
1150 ShadowRoot* Node::containingShadowRoot() const
1152 Node& root = treeScope().rootNode();
1153 return root.isShadowRoot() ? toShadowRoot(&root) : 0;
1156 Node* Node::nonBoundaryShadowTreeRootNode()
1158 ASSERT(!isShadowRoot());
1161 if (root->isShadowRoot())
1163 Node* parent = root->parentOrShadowHostNode();
1164 if (parent && parent->isShadowRoot())
1171 ContainerNode* Node::nonShadowBoundaryParentNode() const
1173 ContainerNode* parent = parentNode();
1174 return parent && !parent->isShadowRoot() ? parent : 0;
1177 Element* Node::parentOrShadowHostElement() const
1179 ContainerNode* parent = parentOrShadowHostNode();
1183 if (parent->isShadowRoot())
1184 return toShadowRoot(parent)->host();
1186 if (!parent->isElementNode())
1189 return toElement(parent);
1192 ContainerNode* Node::parentOrShadowHostOrTemplateHostNode() const
1194 if (isDocumentFragment() && toDocumentFragment(this)->isTemplateContent())
1195 return static_cast<const TemplateContentDocumentFragment*>(this)->host();
1196 return parentOrShadowHostNode();
1199 bool Node::isBlockFlowElement() const
1201 return isElementNode() && renderer() && renderer()->isRenderBlockFlow();
1204 Element *Node::enclosingBlockFlowElement() const
1206 Node *n = const_cast<Node *>(this);
1207 if (isBlockFlowElement())
1208 return toElement(n);
1211 n = n->parentNode();
1214 if (n->isBlockFlowElement() || isHTMLBodyElement(*n))
1215 return toElement(n);
1220 bool Node::isRootEditableElement() const
1222 return rendererIsEditable() && isElementNode() && (!parentNode() || !parentNode()->rendererIsEditable()
1223 || !parentNode()->isElementNode() || isHTMLBodyElement((*this)));
1226 Element* Node::rootEditableElement(EditableType editableType) const
1228 if (editableType == HasEditableAXRole) {
1229 if (AXObjectCache* cache = document().existingAXObjectCache())
1230 return const_cast<Element*>(cache->rootAXEditableElement(this));
1233 return rootEditableElement();
1236 Element* Node::rootEditableElement() const
1238 Element* result = 0;
1239 for (Node* n = const_cast<Node*>(this); n && n->rendererIsEditable(); n = n->parentNode()) {
1240 if (n->isElementNode())
1241 result = toElement(n);
1242 if (isHTMLBodyElement(*n))
1248 bool Node::inSameContainingBlockFlowElement(Node *n)
1250 return n ? enclosingBlockFlowElement() == n->enclosingBlockFlowElement() : false;
1253 // FIXME: End of obviously misplaced HTML editing functions. Try to move these out of Node.
1255 Document* Node::ownerDocument() const
1257 Document* doc = &document();
1258 return doc == this ? 0 : doc;
1261 KURL Node::baseURI() const
1263 return parentNode() ? parentNode()->baseURI() : KURL();
1266 bool Node::isEqualNode(Node* other) const
1271 NodeType nodeType = this->nodeType();
1272 if (nodeType != other->nodeType())
1275 if (nodeName() != other->nodeName())
1278 if (localName() != other->localName())
1281 if (namespaceURI() != other->namespaceURI())
1284 if (nodeValue() != other->nodeValue())
1287 if (isElementNode() && !toElement(this)->hasEquivalentAttributes(toElement(other)))
1290 Node* child = firstChild();
1291 Node* otherChild = other->firstChild();
1294 if (!child->isEqualNode(otherChild))
1297 child = child->nextSibling();
1298 otherChild = otherChild->nextSibling();
1304 if (isDocumentTypeNode()) {
1305 const DocumentType* documentTypeThis = toDocumentType(this);
1306 const DocumentType* documentTypeOther = toDocumentType(other);
1308 if (documentTypeThis->publicId() != documentTypeOther->publicId())
1311 if (documentTypeThis->systemId() != documentTypeOther->systemId())
1318 bool Node::isDefaultNamespace(const AtomicString& namespaceURIMaybeEmpty) const
1320 const AtomicString& namespaceURI = namespaceURIMaybeEmpty.isEmpty() ? nullAtom : namespaceURIMaybeEmpty;
1322 switch (nodeType()) {
1323 case ELEMENT_NODE: {
1324 const Element& element = toElement(*this);
1326 if (element.prefix().isNull())
1327 return element.namespaceURI() == namespaceURI;
1329 if (element.hasAttributes()) {
1330 AttributeCollection attributes = element.attributes();
1331 AttributeCollection::const_iterator end = attributes.end();
1332 for (AttributeCollection::const_iterator it = attributes.begin(); it != end; ++it) {
1333 if (it->localName() == xmlnsAtom)
1334 return it->value() == namespaceURI;
1338 if (Element* parent = parentElement())
1339 return parent->isDefaultNamespace(namespaceURI);
1344 if (Element* de = toDocument(this)->documentElement())
1345 return de->isDefaultNamespace(namespaceURI);
1347 case DOCUMENT_TYPE_NODE:
1348 case DOCUMENT_FRAGMENT_NODE:
1350 case ATTRIBUTE_NODE: {
1351 const Attr* attr = toAttr(this);
1352 if (attr->ownerElement())
1353 return attr->ownerElement()->isDefaultNamespace(namespaceURI);
1357 if (Element* parent = parentElement())
1358 return parent->isDefaultNamespace(namespaceURI);
1363 const AtomicString& Node::lookupPrefix(const AtomicString& namespaceURI) const
1365 // Implemented according to
1366 // http://dom.spec.whatwg.org/#dom-node-lookupprefix
1368 if (namespaceURI.isEmpty() || namespaceURI.isNull())
1371 const Element* context;
1373 switch (nodeType()) {
1375 context = toElement(this);
1378 context = toDocument(this)->documentElement();
1380 case DOCUMENT_FRAGMENT_NODE:
1381 case DOCUMENT_TYPE_NODE:
1384 // FIXME: Remove this when Attr no longer extends Node (CR305105)
1385 case ATTRIBUTE_NODE:
1386 context = toAttr(this)->ownerElement();
1389 context = parentElement();
1396 return context->locateNamespacePrefix(namespaceURI);
1399 const AtomicString& Node::lookupNamespaceURI(const String& prefix) const
1401 // Implemented according to
1402 // http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/namespaces-algorithms.html#lookupNamespaceURIAlgo
1404 if (!prefix.isNull() && prefix.isEmpty())
1407 switch (nodeType()) {
1408 case ELEMENT_NODE: {
1409 const Element& element = toElement(*this);
1411 if (!element.namespaceURI().isNull() && element.prefix() == prefix)
1412 return element.namespaceURI();
1414 if (element.hasAttributes()) {
1415 AttributeCollection attributes = element.attributes();
1416 AttributeCollection::const_iterator end = attributes.end();
1417 for (AttributeCollection::const_iterator it = attributes.begin(); it != end; ++it) {
1418 if (it->prefix() == xmlnsAtom && it->localName() == prefix) {
1419 if (!it->value().isEmpty())
1423 if (it->localName() == xmlnsAtom && prefix.isNull()) {
1424 if (!it->value().isEmpty())
1430 if (Element* parent = parentElement())
1431 return parent->lookupNamespaceURI(prefix);
1435 if (Element* de = toDocument(this)->documentElement())
1436 return de->lookupNamespaceURI(prefix);
1438 case DOCUMENT_TYPE_NODE:
1439 case DOCUMENT_FRAGMENT_NODE:
1441 case ATTRIBUTE_NODE: {
1442 const Attr *attr = toAttr(this);
1443 if (attr->ownerElement())
1444 return attr->ownerElement()->lookupNamespaceURI(prefix);
1449 if (Element* parent = parentElement())
1450 return parent->lookupNamespaceURI(prefix);
1455 static void appendTextContent(const Node* node, bool convertBRsToNewlines, bool& isNullString, StringBuilder& content)
1457 switch (node->nodeType()) {
1458 case Node::TEXT_NODE:
1459 case Node::CDATA_SECTION_NODE:
1460 case Node::COMMENT_NODE:
1461 isNullString = false;
1462 content.append(toCharacterData(node)->data());
1465 case Node::PROCESSING_INSTRUCTION_NODE:
1466 isNullString = false;
1467 content.append(toProcessingInstruction(node)->data());
1470 case Node::ELEMENT_NODE:
1471 if (isHTMLBRElement(*node) && convertBRsToNewlines) {
1472 isNullString = false;
1473 content.append('\n');
1477 case Node::ATTRIBUTE_NODE:
1478 case Node::DOCUMENT_FRAGMENT_NODE:
1479 isNullString = false;
1480 for (Node* child = toContainerNode(node)->firstChild(); child; child = child->nextSibling()) {
1481 Node::NodeType childNodeType = child->nodeType();
1482 if (childNodeType == Node::COMMENT_NODE || childNodeType == Node::PROCESSING_INSTRUCTION_NODE)
1484 appendTextContent(child, convertBRsToNewlines, isNullString, content);
1488 case Node::DOCUMENT_NODE:
1489 case Node::DOCUMENT_TYPE_NODE:
1494 String Node::textContent(bool convertBRsToNewlines) const
1496 StringBuilder content;
1497 bool isNullString = true;
1498 appendTextContent(this, convertBRsToNewlines, isNullString, content);
1499 return isNullString ? String() : content.toString();
1502 void Node::setTextContent(const String& text)
1504 switch (nodeType()) {
1506 case CDATA_SECTION_NODE:
1508 case PROCESSING_INSTRUCTION_NODE:
1512 case ATTRIBUTE_NODE:
1513 case DOCUMENT_FRAGMENT_NODE: {
1514 // FIXME: Merge this logic into replaceChildrenWithText.
1515 RefPtrWillBeRawPtr<ContainerNode> container = toContainerNode(this);
1516 // No need to do anything if the text is identical.
1517 if (container->hasOneTextChild() && toText(container->firstChild())->data() == text)
1519 ChildListMutationScope mutation(*this);
1520 container->removeChildren();
1521 // Note: This API will not insert empty text nodes:
1522 // http://dom.spec.whatwg.org/#dom-node-textcontent
1523 if (!text.isEmpty())
1524 container->appendChild(document().createTextNode(text), ASSERT_NO_EXCEPTION);
1528 case DOCUMENT_TYPE_NODE:
1532 ASSERT_NOT_REACHED();
1535 bool Node::offsetInCharacters() const
1540 unsigned short Node::compareDocumentPosition(const Node* otherNode) const
1542 return compareDocumentPositionInternal(otherNode, TreatShadowTreesAsDisconnected);
1545 unsigned short Node::compareDocumentPositionInternal(const Node* otherNode, ShadowTreesTreatment treatment) const
1547 // It is not clear what should be done if |otherNode| is 0.
1549 return DOCUMENT_POSITION_DISCONNECTED;
1551 if (otherNode == this)
1552 return DOCUMENT_POSITION_EQUIVALENT;
1554 const Attr* attr1 = nodeType() == ATTRIBUTE_NODE ? toAttr(this) : 0;
1555 const Attr* attr2 = otherNode->nodeType() == ATTRIBUTE_NODE ? toAttr(otherNode) : 0;
1557 const Node* start1 = attr1 ? attr1->ownerElement() : this;
1558 const Node* start2 = attr2 ? attr2->ownerElement() : otherNode;
1560 // If either of start1 or start2 is null, then we are disconnected, since one of the nodes is
1561 // an orphaned attribute node.
1562 if (!start1 || !start2) {
1563 unsigned short direction = (this > otherNode) ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
1564 return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | direction;
1567 Vector<const Node*, 16> chain1;
1568 Vector<const Node*, 16> chain2;
1570 chain1.append(attr1);
1572 chain2.append(attr2);
1574 if (attr1 && attr2 && start1 == start2 && start1) {
1575 // We are comparing two attributes on the same node. Crawl our attribute map and see which one we hit first.
1576 const Element* owner1 = attr1->ownerElement();
1577 owner1->synchronizeAllAttributes();
1578 AttributeCollection attributes = owner1->attributes();
1579 AttributeCollection::const_iterator end = attributes.end();
1580 for (AttributeCollection::const_iterator it = attributes.begin(); it != end; ++it) {
1581 // If neither of the two determining nodes is a child node and nodeType is the same for both determining nodes, then an
1582 // implementation-dependent order between the determining nodes is returned. This order is stable as long as no nodes of
1583 // the same nodeType are inserted into or removed from the direct container. This would be the case, for example,
1584 // when comparing two attributes of the same element, and inserting or removing additional attributes might change
1585 // the order between existing attributes.
1586 if (attr1->qualifiedName() == it->name())
1587 return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_FOLLOWING;
1588 if (attr2->qualifiedName() == it->name())
1589 return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_PRECEDING;
1592 ASSERT_NOT_REACHED();
1593 return DOCUMENT_POSITION_DISCONNECTED;
1596 // If one node is in the document and the other is not, we must be disconnected.
1597 // If the nodes have different owning documents, they must be disconnected. Note that we avoid
1598 // comparing Attr nodes here, since they return false from inDocument() all the time (which seems like a bug).
1599 if (start1->inDocument() != start2->inDocument() || (treatment == TreatShadowTreesAsDisconnected && start1->treeScope() != start2->treeScope())) {
1600 unsigned short direction = (this > otherNode) ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
1601 return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | direction;
1604 // We need to find a common ancestor container, and then compare the indices of the two immediate children.
1605 const Node* current;
1606 for (current = start1; current; current = current->parentOrShadowHostNode())
1607 chain1.append(current);
1608 for (current = start2; current; current = current->parentOrShadowHostNode())
1609 chain2.append(current);
1611 unsigned index1 = chain1.size();
1612 unsigned index2 = chain2.size();
1614 // If the two elements don't have a common root, they're not in the same tree.
1615 if (chain1[index1 - 1] != chain2[index2 - 1]) {
1616 unsigned short direction = (this > otherNode) ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
1617 return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | direction;
1620 unsigned connection = start1->treeScope() != start2->treeScope() ? DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC : 0;
1622 // Walk the two chains backwards and look for the first difference.
1623 for (unsigned i = std::min(index1, index2); i; --i) {
1624 const Node* child1 = chain1[--index1];
1625 const Node* child2 = chain2[--index2];
1626 if (child1 != child2) {
1627 // If one of the children is an attribute, it wins.
1628 if (child1->nodeType() == ATTRIBUTE_NODE)
1629 return DOCUMENT_POSITION_FOLLOWING | connection;
1630 if (child2->nodeType() == ATTRIBUTE_NODE)
1631 return DOCUMENT_POSITION_PRECEDING | connection;
1633 // If one of the children is a shadow root,
1634 if (child1->isShadowRoot() || child2->isShadowRoot()) {
1635 if (!child2->isShadowRoot())
1636 return Node::DOCUMENT_POSITION_FOLLOWING | connection;
1637 if (!child1->isShadowRoot())
1638 return Node::DOCUMENT_POSITION_PRECEDING | connection;
1640 for (ShadowRoot* child = toShadowRoot(child2)->olderShadowRoot(); child; child = child->olderShadowRoot())
1641 if (child == child1)
1642 return Node::DOCUMENT_POSITION_FOLLOWING | connection;
1644 return Node::DOCUMENT_POSITION_PRECEDING | connection;
1647 if (!child2->nextSibling())
1648 return DOCUMENT_POSITION_FOLLOWING | connection;
1649 if (!child1->nextSibling())
1650 return DOCUMENT_POSITION_PRECEDING | connection;
1652 // Otherwise we need to see which node occurs first. Crawl backwards from child2 looking for child1.
1653 for (Node* child = child2->previousSibling(); child; child = child->previousSibling()) {
1654 if (child == child1)
1655 return DOCUMENT_POSITION_FOLLOWING | connection;
1657 return DOCUMENT_POSITION_PRECEDING | connection;
1661 // There was no difference between the two parent chains, i.e., one was a subset of the other. The shorter
1662 // chain is the ancestor.
1663 return index1 < index2 ?
1664 DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_CONTAINED_BY | connection :
1665 DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_CONTAINS | connection;
1668 FloatPoint Node::convertToPage(const FloatPoint& p) const
1670 // If there is a renderer, just ask it to do the conversion
1672 return renderer()->localToAbsolute(p, UseTransforms);
1674 // Otherwise go up the tree looking for a renderer
1675 if (Element* parent = parentElement())
1676 return parent->convertToPage(p);
1678 // No parent - no conversion needed
1682 FloatPoint Node::convertFromPage(const FloatPoint& p) const
1684 // If there is a renderer, just ask it to do the conversion
1686 return renderer()->absoluteToLocal(p, UseTransforms);
1688 // Otherwise go up the tree looking for a renderer
1689 if (Element* parent = parentElement())
1690 return parent->convertFromPage(p);
1692 // No parent - no conversion needed
1696 String Node::debugName() const
1699 name.append(nodeName());
1702 name.appendLiteral(" id=\'");
1703 name.append(toElement(this)->getIdAttribute());
1708 name.appendLiteral(" class=\'");
1709 for (size_t i = 0; i < toElement(this)->classNames().size(); ++i) {
1712 name.append(toElement(this)->classNames()[i]);
1717 return name.toString();
1722 static void appendAttributeDesc(const Node* node, StringBuilder& stringBuilder, const QualifiedName& name, const char* attrDesc)
1724 if (!node->isElementNode())
1727 String attr = toElement(node)->getAttribute(name);
1731 stringBuilder.append(attrDesc);
1732 stringBuilder.append("=\"");
1733 stringBuilder.append(attr);
1734 stringBuilder.append("\"");
1737 void Node::showNode(const char* prefix) const
1742 String value = nodeValue();
1743 value.replaceWithLiteral('\\', "\\\\");
1744 value.replaceWithLiteral('\n', "\\n");
1745 fprintf(stderr, "%s%s\t%p \"%s\"\n", prefix, nodeName().utf8().data(), this, value.utf8().data());
1747 StringBuilder attrs;
1748 appendAttributeDesc(this, attrs, idAttr, " ID");
1749 appendAttributeDesc(this, attrs, classAttr, " CLASS");
1750 appendAttributeDesc(this, attrs, styleAttr, " STYLE");
1751 fprintf(stderr, "%s%s\t%p%s\n", prefix, nodeName().utf8().data(), this, attrs.toString().utf8().data());
1755 void Node::showTreeForThis() const
1757 showTreeAndMark(this, "*");
1760 void Node::showNodePathForThis() const
1762 Vector<const Node*, 16> chain;
1763 const Node* node = this;
1764 while (node->parentOrShadowHostNode()) {
1766 node = node->parentOrShadowHostNode();
1768 for (unsigned index = chain.size(); index > 0; --index) {
1769 const Node* node = chain[index - 1];
1770 if (node->isShadowRoot()) {
1772 for (ShadowRoot* shadowRoot = toShadowRoot(node)->olderShadowRoot(); shadowRoot; shadowRoot = shadowRoot->olderShadowRoot())
1774 fprintf(stderr, "/#shadow-root[%d]", count);
1778 switch (node->nodeType()) {
1779 case ELEMENT_NODE: {
1780 fprintf(stderr, "/%s", node->nodeName().utf8().data());
1782 const Element* element = toElement(node);
1783 const AtomicString& idattr = element->getIdAttribute();
1784 bool hasIdAttr = !idattr.isNull() && !idattr.isEmpty();
1785 if (node->previousSibling() || node->nextSibling()) {
1787 for (Node* previous = node->previousSibling(); previous; previous = previous->previousSibling())
1788 if (previous->nodeName() == node->nodeName())
1791 fprintf(stderr, "[@id=\"%s\" and position()=%d]", idattr.utf8().data(), count);
1793 fprintf(stderr, "[%d]", count);
1794 } else if (hasIdAttr) {
1795 fprintf(stderr, "[@id=\"%s\"]", idattr.utf8().data());
1800 fprintf(stderr, "/text()");
1802 case ATTRIBUTE_NODE:
1803 fprintf(stderr, "/@%s", node->nodeName().utf8().data());
1809 fprintf(stderr, "\n");
1812 static void traverseTreeAndMark(const String& baseIndent, const Node* rootNode, const Node* markedNode1, const char* markedLabel1, const Node* markedNode2, const char* markedLabel2)
1814 for (const Node* node = rootNode; node; node = NodeTraversal::next(*node)) {
1815 if (node == markedNode1)
1816 fprintf(stderr, "%s", markedLabel1);
1817 if (node == markedNode2)
1818 fprintf(stderr, "%s", markedLabel2);
1820 StringBuilder indent;
1821 indent.append(baseIndent);
1822 for (const Node* tmpNode = node; tmpNode && tmpNode != rootNode; tmpNode = tmpNode->parentOrShadowHostNode())
1823 indent.append('\t');
1824 fprintf(stderr, "%s", indent.toString().utf8().data());
1826 indent.append('\t');
1827 if (node->isShadowRoot()) {
1828 if (ShadowRoot* youngerShadowRoot = toShadowRoot(node)->youngerShadowRoot())
1829 traverseTreeAndMark(indent.toString(), youngerShadowRoot, markedNode1, markedLabel1, markedNode2, markedLabel2);
1830 } else if (ShadowRoot* oldestShadowRoot = oldestShadowRootFor(node))
1831 traverseTreeAndMark(indent.toString(), oldestShadowRoot, markedNode1, markedLabel1, markedNode2, markedLabel2);
1835 void Node::showTreeAndMark(const Node* markedNode1, const char* markedLabel1, const Node* markedNode2, const char* markedLabel2) const
1837 const Node* rootNode;
1838 const Node* node = this;
1839 while (node->parentOrShadowHostNode() && !isHTMLBodyElement(*node))
1840 node = node->parentOrShadowHostNode();
1843 String startingIndent;
1844 traverseTreeAndMark(startingIndent, rootNode, markedNode1, markedLabel1, markedNode2, markedLabel2);
1847 void Node::formatForDebugger(char* buffer, unsigned length) const
1858 strncpy(buffer, result.utf8().data(), length - 1);
1861 static ContainerNode* parentOrShadowHostOrFrameOwner(const Node* node)
1863 ContainerNode* parent = node->parentOrShadowHostNode();
1864 if (!parent && node->document().frame())
1865 parent = node->document().frame()->deprecatedLocalOwner();
1869 static void showSubTreeAcrossFrame(const Node* node, const Node* markedNode, const String& indent)
1871 if (node == markedNode)
1873 fputs(indent.utf8().data(), stderr);
1875 if (node->isShadowRoot()) {
1876 if (ShadowRoot* youngerShadowRoot = toShadowRoot(node)->youngerShadowRoot())
1877 showSubTreeAcrossFrame(youngerShadowRoot, markedNode, indent + "\t");
1879 if (node->isFrameOwnerElement())
1880 showSubTreeAcrossFrame(toHTMLFrameOwnerElement(node)->contentDocument(), markedNode, indent + "\t");
1881 if (ShadowRoot* oldestShadowRoot = oldestShadowRootFor(node))
1882 showSubTreeAcrossFrame(oldestShadowRoot, markedNode, indent + "\t");
1884 for (Node* child = node->firstChild(); child; child = child->nextSibling())
1885 showSubTreeAcrossFrame(child, markedNode, indent + "\t");
1888 void Node::showTreeForThisAcrossFrame() const
1890 Node* rootNode = const_cast<Node*>(this);
1891 while (parentOrShadowHostOrFrameOwner(rootNode))
1892 rootNode = parentOrShadowHostOrFrameOwner(rootNode);
1893 showSubTreeAcrossFrame(rootNode, this, "");
1900 Node* Node::enclosingLinkEventParentOrSelf()
1902 for (Node* node = this; node; node = NodeRenderingTraversal::parent(node)) {
1903 // For imagemaps, the enclosing link node is the associated area element not the image itself.
1904 // So we don't let images be the enclosingLinkNode, even though isLink sometimes returns true
1906 if (node->isLink() && !isHTMLImageElement(*node))
1913 const AtomicString& Node::interfaceName() const
1915 return EventTargetNames::Node;
1918 ExecutionContext* Node::executionContext() const
1920 return document().contextDocument().get();
1923 void Node::didMoveToNewDocument(Document& oldDocument)
1925 TreeScopeAdopter::ensureDidMoveToNewDocumentWasCalled(oldDocument);
1927 if (const EventTargetData* eventTargetData = this->eventTargetData()) {
1928 const EventListenerMap& listenerMap = eventTargetData->eventListenerMap;
1929 if (!listenerMap.isEmpty()) {
1930 Vector<AtomicString> types = listenerMap.eventTypes();
1931 for (unsigned i = 0; i < types.size(); ++i)
1932 document().addListenerTypeIfNeeded(types[i]);
1936 if (AXObjectCache::accessibilityEnabled()) {
1937 if (AXObjectCache* cache = oldDocument.existingAXObjectCache())
1938 cache->remove(this);
1941 oldDocument.markers().removeMarkers(this);
1942 oldDocument.updateRangesAfterNodeMovedToAnotherDocument(*this);
1944 if (const TouchEventTargetSet* touchHandlers = oldDocument.touchEventTargets()) {
1945 while (touchHandlers->contains(this)) {
1946 oldDocument.didRemoveTouchEventHandler(this);
1947 document().didAddTouchEventHandler(this);
1950 if (oldDocument.frameHost() != document().frameHost()) {
1951 if (oldDocument.frameHost())
1952 oldDocument.frameHost()->eventHandlerRegistry().didMoveOutOfFrameHost(*this);
1953 if (document().frameHost())
1954 document().frameHost()->eventHandlerRegistry().didMoveIntoFrameHost(*this);
1957 if (WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> >* registry = mutationObserverRegistry()) {
1958 for (size_t i = 0; i < registry->size(); ++i) {
1959 document().addMutationObserverTypes(registry->at(i)->mutationTypes());
1963 if (WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> >* transientRegistry = transientMutationObserverRegistry()) {
1964 for (WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> >::iterator iter = transientRegistry->begin(); iter != transientRegistry->end(); ++iter) {
1965 document().addMutationObserverTypes((*iter)->mutationTypes());
1970 static inline bool tryAddEventListener(Node* targetNode, const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
1972 if (!targetNode->EventTarget::addEventListener(eventType, listener, useCapture))
1975 Document& document = targetNode->document();
1976 document.addListenerTypeIfNeeded(eventType);
1977 if (isTouchEventType(eventType))
1978 document.didAddTouchEventHandler(targetNode);
1979 if (document.frameHost())
1980 document.frameHost()->eventHandlerRegistry().didAddEventHandler(*targetNode, eventType);
1985 bool Node::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
1987 return tryAddEventListener(this, eventType, listener, useCapture);
1990 static inline bool tryRemoveEventListener(Node* targetNode, const AtomicString& eventType, EventListener* listener, bool useCapture)
1992 if (!targetNode->EventTarget::removeEventListener(eventType, listener, useCapture))
1995 // FIXME: Notify Document that the listener has vanished. We need to keep track of a number of
1996 // listeners for each type, not just a bool - see https://bugs.webkit.org/show_bug.cgi?id=33861
1997 Document& document = targetNode->document();
1998 if (isTouchEventType(eventType))
1999 document.didRemoveTouchEventHandler(targetNode);
2000 if (document.frameHost())
2001 document.frameHost()->eventHandlerRegistry().didRemoveEventHandler(*targetNode, eventType);
2006 bool Node::removeEventListener(const AtomicString& eventType, EventListener* listener, bool useCapture)
2008 return tryRemoveEventListener(this, eventType, listener, useCapture);
2011 void Node::removeAllEventListeners()
2013 if (hasEventListeners() && document().frameHost())
2014 document().frameHost()->eventHandlerRegistry().didRemoveAllEventHandlers(*this);
2015 EventTarget::removeAllEventListeners();
2016 document().didClearTouchEventHandlers(this);
2019 void Node::removeAllEventListenersRecursively()
2021 for (Node* node = this; node; node = NodeTraversal::next(*node)) {
2022 node->removeAllEventListeners();
2023 for (ShadowRoot* root = node->youngestShadowRoot(); root; root = root->olderShadowRoot())
2024 root->removeAllEventListenersRecursively();
2028 typedef WillBeHeapHashMap<RawPtrWillBeWeakMember<Node>, OwnPtr<EventTargetData> > EventTargetDataMap;
2030 static EventTargetDataMap& eventTargetDataMap()
2033 DEFINE_STATIC_LOCAL(Persistent<EventTargetDataMap>, map, (new EventTargetDataMap()));
2036 DEFINE_STATIC_LOCAL(EventTargetDataMap, map, ());
2041 EventTargetData* Node::eventTargetData()
2043 return hasEventTargetData() ? eventTargetDataMap().get(this) : 0;
2046 EventTargetData& Node::ensureEventTargetData()
2048 if (hasEventTargetData())
2049 return *eventTargetDataMap().get(this);
2050 setHasEventTargetData(true);
2051 EventTargetData* data = new EventTargetData;
2052 eventTargetDataMap().set(this, adoptPtr(data));
2057 void Node::clearEventTargetData()
2059 eventTargetDataMap().remove(this);
2063 WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> >* Node::mutationObserverRegistry()
2067 NodeMutationObserverData* data = rareData()->mutationObserverData();
2070 return &data->registry;
2073 WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> >* Node::transientMutationObserverRegistry()
2077 NodeMutationObserverData* data = rareData()->mutationObserverData();
2080 return &data->transientRegistry;
2083 template<typename Registry>
2084 static inline void collectMatchingObserversForMutation(WillBeHeapHashMap<RawPtrWillBeMember<MutationObserver>, MutationRecordDeliveryOptions>& observers, Registry* registry, Node& target, MutationObserver::MutationType type, const QualifiedName* attributeName)
2088 for (typename Registry::iterator iter = registry->begin(); iter != registry->end(); ++iter) {
2089 const MutationObserverRegistration& registration = **iter;
2090 if (registration.shouldReceiveMutationFrom(target, type, attributeName)) {
2091 MutationRecordDeliveryOptions deliveryOptions = registration.deliveryOptions();
2092 WillBeHeapHashMap<RawPtrWillBeMember<MutationObserver>, MutationRecordDeliveryOptions>::AddResult result = observers.add(®istration.observer(), deliveryOptions);
2093 if (!result.isNewEntry)
2094 result.storedValue->value |= deliveryOptions;
2099 void Node::getRegisteredMutationObserversOfType(WillBeHeapHashMap<RawPtrWillBeMember<MutationObserver>, MutationRecordDeliveryOptions>& observers, MutationObserver::MutationType type, const QualifiedName* attributeName)
2101 ASSERT((type == MutationObserver::Attributes && attributeName) || !attributeName);
2102 collectMatchingObserversForMutation(observers, mutationObserverRegistry(), *this, type, attributeName);
2103 collectMatchingObserversForMutation(observers, transientMutationObserverRegistry(), *this, type, attributeName);
2104 for (Node* node = parentNode(); node; node = node->parentNode()) {
2105 collectMatchingObserversForMutation(observers, node->mutationObserverRegistry(), *this, type, attributeName);
2106 collectMatchingObserversForMutation(observers, node->transientMutationObserverRegistry(), *this, type, attributeName);
2110 void Node::registerMutationObserver(MutationObserver& observer, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
2112 MutationObserverRegistration* registration = 0;
2113 WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> >& registry = ensureRareData().ensureMutationObserverData().registry;
2114 for (size_t i = 0; i < registry.size(); ++i) {
2115 if (®istry[i]->observer() == &observer) {
2116 registration = registry[i].get();
2117 registration->resetObservation(options, attributeFilter);
2121 if (!registration) {
2122 registry.append(MutationObserverRegistration::create(observer, this, options, attributeFilter));
2123 registration = registry.last().get();
2126 document().addMutationObserverTypes(registration->mutationTypes());
2129 void Node::unregisterMutationObserver(MutationObserverRegistration* registration)
2131 WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> >* registry = mutationObserverRegistry();
2136 size_t index = registry->find(registration);
2137 ASSERT(index != kNotFound);
2138 if (index == kNotFound)
2141 // Deleting the registration may cause this node to be derefed, so we must make sure the Vector operation completes
2142 // before that, in case |this| is destroyed (see MutationObserverRegistration::m_registrationNodeKeepAlive).
2143 // FIXME: Simplify the registration/transient registration logic to make this understandable by humans.
2144 RefPtrWillBeRawPtr<Node> protect(this);
2146 // The explicit dispose() is needed to have the registration
2147 // object unregister itself promptly.
2148 registration->dispose();
2150 registry->remove(index);
2153 void Node::registerTransientMutationObserver(MutationObserverRegistration* registration)
2155 ensureRareData().ensureMutationObserverData().transientRegistry.add(registration);
2158 void Node::unregisterTransientMutationObserver(MutationObserverRegistration* registration)
2160 WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> >* transientRegistry = transientMutationObserverRegistry();
2161 ASSERT(transientRegistry);
2162 if (!transientRegistry)
2165 ASSERT(transientRegistry->contains(registration));
2166 transientRegistry->remove(registration);
2169 void Node::notifyMutationObserversNodeWillDetach()
2171 if (!document().hasMutationObservers())
2174 for (Node* node = parentNode(); node; node = node->parentNode()) {
2175 if (WillBeHeapVector<OwnPtrWillBeMember<MutationObserverRegistration> >* registry = node->mutationObserverRegistry()) {
2176 const size_t size = registry->size();
2177 for (size_t i = 0; i < size; ++i)
2178 registry->at(i)->observedSubtreeNodeWillDetach(*this);
2181 if (WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> >* transientRegistry = node->transientMutationObserverRegistry()) {
2182 for (WillBeHeapHashSet<RawPtrWillBeMember<MutationObserverRegistration> >::iterator iter = transientRegistry->begin(); iter != transientRegistry->end(); ++iter)
2183 (*iter)->observedSubtreeNodeWillDetach(*this);
2188 void Node::handleLocalEvents(Event* event)
2190 if (!hasEventTargetData())
2193 if (isDisabledFormControl(this) && event->isMouseEvent())
2196 fireEventListeners(event);
2199 void Node::dispatchScopedEvent(PassRefPtrWillBeRawPtr<Event> event)
2201 dispatchScopedEventDispatchMediator(EventDispatchMediator::create(event));
2204 void Node::dispatchScopedEventDispatchMediator(PassRefPtrWillBeRawPtr<EventDispatchMediator> eventDispatchMediator)
2206 EventDispatcher::dispatchScopedEvent(this, eventDispatchMediator);
2209 bool Node::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
2211 if (event->isMouseEvent())
2212 return EventDispatcher::dispatchEvent(this, MouseEventDispatchMediator::create(static_pointer_cast<MouseEvent>(event), MouseEventDispatchMediator::SyntheticMouseEvent));
2213 if (event->isTouchEvent())
2214 return dispatchTouchEvent(static_pointer_cast<TouchEvent>(event));
2215 return EventDispatcher::dispatchEvent(this, EventDispatchMediator::create(event));
2218 void Node::dispatchSubtreeModifiedEvent()
2220 if (isInShadowTree())
2223 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
2225 if (!document().hasListenerType(Document::DOMSUBTREEMODIFIED_LISTENER))
2228 dispatchScopedEvent(MutationEvent::create(EventTypeNames::DOMSubtreeModified, true));
2231 bool Node::dispatchDOMActivateEvent(int detail, PassRefPtrWillBeRawPtr<Event> underlyingEvent)
2233 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
2234 RefPtrWillBeRawPtr<UIEvent> event = UIEvent::create(EventTypeNames::DOMActivate, true, true, document().domWindow(), detail);
2235 event->setUnderlyingEvent(underlyingEvent);
2236 dispatchScopedEvent(event);
2237 return event->defaultHandled();
2240 bool Node::dispatchKeyEvent(const PlatformKeyboardEvent& event)
2242 return EventDispatcher::dispatchEvent(this, KeyboardEventDispatchMediator::create(KeyboardEvent::create(event, document().domWindow())));
2245 bool Node::dispatchMouseEvent(const PlatformMouseEvent& event, const AtomicString& eventType,
2246 int detail, Node* relatedTarget)
2248 return EventDispatcher::dispatchEvent(this, MouseEventDispatchMediator::create(MouseEvent::create(eventType, document().domWindow(), event, detail, relatedTarget)));
2251 bool Node::dispatchGestureEvent(const PlatformGestureEvent& event)
2253 RefPtrWillBeRawPtr<GestureEvent> gestureEvent = GestureEvent::create(document().domWindow(), event);
2254 if (!gestureEvent.get())
2256 return EventDispatcher::dispatchEvent(this, GestureEventDispatchMediator::create(gestureEvent));
2259 bool Node::dispatchTouchEvent(PassRefPtrWillBeRawPtr<TouchEvent> event)
2261 return EventDispatcher::dispatchEvent(this, TouchEventDispatchMediator::create(event));
2264 void Node::dispatchSimulatedClick(Event* underlyingEvent, SimulatedClickMouseEventOptions eventOptions)
2266 EventDispatcher::dispatchSimulatedClick(this, underlyingEvent, eventOptions);
2269 bool Node::dispatchWheelEvent(const PlatformWheelEvent& event)
2271 return EventDispatcher::dispatchEvent(this, WheelEventDispatchMediator::create(event, document().domWindow()));
2274 void Node::dispatchInputEvent()
2276 dispatchScopedEvent(Event::createBubble(EventTypeNames::input));
2279 void Node::defaultEventHandler(Event* event)
2281 if (event->target() != this)
2283 const AtomicString& eventType = event->type();
2284 if (eventType == EventTypeNames::keydown || eventType == EventTypeNames::keypress) {
2285 if (event->isKeyboardEvent()) {
2286 if (LocalFrame* frame = document().frame())
2287 frame->eventHandler().defaultKeyboardEventHandler(toKeyboardEvent(event));
2289 } else if (eventType == EventTypeNames::click) {
2290 int detail = event->isUIEvent() ? static_cast<UIEvent*>(event)->detail() : 0;
2291 if (dispatchDOMActivateEvent(detail, event))
2292 event->setDefaultHandled();
2293 } else if (eventType == EventTypeNames::contextmenu) {
2294 if (Page* page = document().page())
2295 page->contextMenuController().handleContextMenuEvent(event);
2296 } else if (eventType == EventTypeNames::textInput) {
2297 if (event->hasInterface(EventNames::TextEvent)) {
2298 if (LocalFrame* frame = document().frame())
2299 frame->eventHandler().defaultTextInputEventHandler(toTextEvent(event));
2302 } else if (eventType == EventTypeNames::mousedown && event->isMouseEvent()) {
2303 MouseEvent* mouseEvent = toMouseEvent(event);
2304 if (mouseEvent->button() == MiddleButton) {
2305 if (enclosingLinkEventParentOrSelf())
2308 RenderObject* renderer = this->renderer();
2309 while (renderer && (!renderer->isBox() || !toRenderBox(renderer)->canBeScrolledAndHasScrollableArea()))
2310 renderer = renderer->parent();
2313 if (LocalFrame* frame = document().frame())
2314 frame->eventHandler().startPanScrolling(renderer);
2318 } else if ((eventType == EventTypeNames::wheel || eventType == EventTypeNames::mousewheel) && event->hasInterface(EventNames::WheelEvent)) {
2319 WheelEvent* wheelEvent = toWheelEvent(event);
2321 // If we don't have a renderer, send the wheel event to the first node we find with a renderer.
2322 // This is needed for <option> and <optgroup> elements so that <select>s get a wheel scroll.
2323 Node* startNode = this;
2324 while (startNode && !startNode->renderer())
2325 startNode = startNode->parentOrShadowHostNode();
2327 if (startNode && startNode->renderer()) {
2328 if (LocalFrame* frame = document().frame())
2329 frame->eventHandler().defaultWheelEventHandler(startNode, wheelEvent);
2331 } else if (event->type() == EventTypeNames::webkitEditableContentChanged) {
2332 dispatchInputEvent();
2336 void Node::willCallDefaultEventHandler(const Event&)
2340 bool Node::willRespondToMouseMoveEvents()
2342 if (isDisabledFormControl(this))
2344 return hasEventListeners(EventTypeNames::mousemove) || hasEventListeners(EventTypeNames::mouseover) || hasEventListeners(EventTypeNames::mouseout);
2347 bool Node::willRespondToMouseClickEvents()
2349 if (isDisabledFormControl(this))
2351 return isContentEditable(UserSelectAllIsAlwaysNonEditable) || hasEventListeners(EventTypeNames::mouseup) || hasEventListeners(EventTypeNames::mousedown) || hasEventListeners(EventTypeNames::click) || hasEventListeners(EventTypeNames::DOMActivate);
2354 bool Node::willRespondToTouchEvents()
2356 if (isDisabledFormControl(this))
2358 return hasEventListeners(EventTypeNames::touchstart) || hasEventListeners(EventTypeNames::touchmove) || hasEventListeners(EventTypeNames::touchcancel) || hasEventListeners(EventTypeNames::touchend);
2362 // This is here for inlining
2363 inline void TreeScope::removedLastRefToScope()
2365 ASSERT_WITH_SECURITY_IMPLICATION(!deletionHasBegun());
2366 if (m_guardRefCount) {
2367 // If removing a child removes the last self-only ref, we don't
2368 // want the scope to be destructed until after
2369 // removeDetachedChildren returns, so we guard ourselves with an
2370 // extra self-only ref.
2374 // We need to do this right now since guardDeref() can delete this.
2375 rootNode().m_inRemovedLastRefFunction = false;
2380 rootNode().m_inRemovedLastRefFunction = false;
2382 #if SECURITY_ASSERT_ENABLED
2389 // It's important not to inline removedLastRef, because we don't want to inline the code to
2390 // delete a Node at each deref call site.
2391 void Node::removedLastRef()
2393 // An explicit check for Document here is better than a virtual function since it is
2394 // faster for non-Document nodes, and because the call to removedLastRef that is inlined
2395 // at all deref call sites is smaller if it's a non-virtual function.
2396 if (isTreeScope()) {
2397 treeScope().removedLastRefToScope();
2401 #if SECURITY_ASSERT_ENABLED
2402 m_deletionHasBegun = true;
2408 unsigned Node::connectedSubframeCount() const
2410 return hasRareData() ? rareData()->connectedSubframeCount() : 0;
2413 void Node::incrementConnectedSubframeCount(unsigned amount)
2415 ASSERT(isContainerNode());
2416 ensureRareData().incrementConnectedSubframeCount(amount);
2419 void Node::decrementConnectedSubframeCount(unsigned amount)
2421 rareData()->decrementConnectedSubframeCount(amount);
2424 void Node::updateAncestorConnectedSubframeCountForRemoval() const
2426 unsigned count = connectedSubframeCount();
2431 for (Node* node = parentOrShadowHostNode(); node; node = node->parentOrShadowHostNode())
2432 node->decrementConnectedSubframeCount(count);
2435 void Node::updateAncestorConnectedSubframeCountForInsertion() const
2437 unsigned count = connectedSubframeCount();
2442 for (Node* node = parentOrShadowHostNode(); node; node = node->parentOrShadowHostNode())
2443 node->incrementConnectedSubframeCount(count);
2446 PassRefPtrWillBeRawPtr<StaticNodeList> Node::getDestinationInsertionPoints()
2448 document().updateDistributionForNodeIfNeeded(this);
2449 WillBeHeapVector<RawPtrWillBeMember<InsertionPoint>, 8> insertionPoints;
2450 collectDestinationInsertionPoints(*this, insertionPoints);
2451 WillBeHeapVector<RefPtrWillBeMember<Node> > filteredInsertionPoints;
2452 for (size_t i = 0; i < insertionPoints.size(); ++i) {
2453 InsertionPoint* insertionPoint = insertionPoints[i];
2454 ASSERT(insertionPoint->containingShadowRoot());
2455 if (insertionPoint->containingShadowRoot()->type() != ShadowRoot::UserAgentShadowRoot)
2456 filteredInsertionPoints.append(insertionPoint);
2458 return StaticNodeList::adopt(filteredInsertionPoints);
2461 void Node::setFocus(bool flag)
2463 document().userActionElements().setFocused(this, flag);
2466 void Node::setActive(bool flag)
2468 document().userActionElements().setActive(this, flag);
2471 void Node::setHovered(bool flag)
2473 document().userActionElements().setHovered(this, flag);
2476 bool Node::isUserActionElementActive() const
2478 ASSERT(isUserActionElement());
2479 return document().userActionElements().isActive(this);
2482 bool Node::isUserActionElementInActiveChain() const
2484 ASSERT(isUserActionElement());
2485 return document().userActionElements().isInActiveChain(this);
2488 bool Node::isUserActionElementHovered() const
2490 ASSERT(isUserActionElement());
2491 return document().userActionElements().isHovered(this);
2494 bool Node::isUserActionElementFocused() const
2496 ASSERT(isUserActionElement());
2497 return document().userActionElements().isFocused(this);
2500 void Node::setCustomElementState(CustomElementState newState)
2502 CustomElementState oldState = customElementState();
2505 case NotCustomElement:
2506 ASSERT_NOT_REACHED(); // Everything starts in this state
2509 case WaitingForUpgrade:
2510 ASSERT(NotCustomElement == oldState);
2514 ASSERT(WaitingForUpgrade == oldState);
2518 ASSERT(isHTMLElement() || isSVGElement());
2519 setFlag(CustomElementFlag);
2520 setFlag(newState == Upgraded, CustomElementUpgradedFlag);
2522 if (oldState == NotCustomElement || newState == Upgraded)
2523 setNeedsStyleRecalc(SubtreeStyleChange); // :unresolved has changed
2526 void Node::trace(Visitor* visitor)
2528 visitor->trace(m_parentOrShadowHostNode);
2529 visitor->trace(m_previous);
2530 visitor->trace(m_next);
2532 visitor->trace(rareData());
2533 visitor->trace(m_treeScope);
2534 EventTarget::trace(visitor);
2537 unsigned Node::lengthOfContents() const
2539 // This switch statement must be consistent with that of Range::processContentsBetweenOffsets.
2540 switch (nodeType()) {
2541 case Node::TEXT_NODE:
2542 case Node::CDATA_SECTION_NODE:
2543 case Node::COMMENT_NODE:
2544 return toCharacterData(this)->length();
2545 case Node::PROCESSING_INSTRUCTION_NODE:
2546 return toProcessingInstruction(this)->data().length();
2547 case Node::ELEMENT_NODE:
2548 case Node::ATTRIBUTE_NODE:
2549 case Node::DOCUMENT_NODE:
2550 case Node::DOCUMENT_FRAGMENT_NODE:
2551 return toContainerNode(this)->countChildren();
2552 case Node::DOCUMENT_TYPE_NODE:
2555 ASSERT_NOT_REACHED();
2559 } // namespace WebCore
2563 void showNode(const WebCore::Node* node)
2569 void showTree(const WebCore::Node* node)
2572 node->showTreeForThis();
2575 void showNodePath(const WebCore::Node* node)
2578 node->showNodePathForThis();