This source file includes following definitions.
- collectChildrenAndRemoveFromOldParent
- removeDetachedChildren
- parserTakeAllChildrenFrom
- isChildTypeAllowed
- containsConsideringHostElements
- checkAcceptChild
- checkAcceptChildGuaranteedNodeTypes
- insertBefore
- insertBeforeCommon
- parserInsertBefore
- replaceChild
- willRemoveChild
- willRemoveChildren
- disconnectDescendantFrames
- removeChild
- removeBetween
- parserRemoveChild
- removeChildren
- appendChild
- parserAppendChild
- attach
- detach
- childrenChanged
- cloneChildNodes
- getUpperLeftCorner
- getLowerRightCorner
- boundingBox
- focusStateChanged
- setFocus
- setActive
- setHovered
- children
- countChildren
- traverseToChildAt
- querySelector
- querySelectorAll
- dispatchChildInsertionEvents
- dispatchChildRemovalEvents
- updateTreeAfterInsertion
- hasRestyleFlagInternal
- hasRestyleFlagsInternal
- setRestyleFlag
- checkForChildrenAdjacentRuleChanges
- getElementsByTagName
- getElementsByTagNameNS
- getElementsByName
- getElementsByClassName
- radioNodeList
- childAttachedAllowedWhenAttachingChildren
#include "config.h"
#include "core/dom/ContainerNode.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ChildListMutationScope.h"
#include "core/dom/ClassCollection.h"
#include "core/dom/ContainerNodeAlgorithms.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/FullscreenElementStack.h"
#include "core/dom/NameNodeList.h"
#include "core/dom/NodeChildRemovalTracker.h"
#include "core/dom/NodeRareData.h"
#include "core/dom/NodeRenderStyle.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/SelectorQuery.h"
#include "core/events/MutationEvent.h"
#include "core/html/HTMLCollection.h"
#include "core/html/HTMLFrameOwnerElement.h"
#include "core/html/RadioNodeList.h"
#include "core/rendering/InlineTextBox.h"
#include "core/rendering/RenderText.h"
#include "core/rendering/RenderTheme.h"
#include "core/rendering/RenderView.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
static void dispatchChildInsertionEvents(Node&);
static void dispatchChildRemovalEvents(Node&);
ChildNodesLazySnapshot* ChildNodesLazySnapshot::latestSnapshot = 0;
#ifndef NDEBUG
unsigned NoEventDispatchAssertion::s_count = 0;
#endif
static void collectChildrenAndRemoveFromOldParent(Node& node, NodeVector& nodes, ExceptionState& exceptionState)
{
if (!node.isDocumentFragment()) {
nodes.append(&node);
if (ContainerNode* oldParent = node.parentNode())
oldParent->removeChild(&node, exceptionState);
return;
}
getChildNodes(node, nodes);
toContainerNode(node).removeChildren();
}
void ContainerNode::removeDetachedChildren()
{
if (connectedSubframeCount()) {
for (Node* child = firstChild(); child; child = child->nextSibling())
child->updateAncestorConnectedSubframeCountForRemoval();
}
ASSERT(needsAttach());
removeDetachedChildrenInContainer<Node, ContainerNode>(*this);
}
void ContainerNode::parserTakeAllChildrenFrom(ContainerNode& oldParent)
{
while (RefPtr<Node> child = oldParent.firstChild()) {
oldParent.parserRemoveChild(*child);
treeScope().adoptIfNeeded(*child);
parserAppendChild(child.get());
}
}
ContainerNode::~ContainerNode()
{
willBeDeletedFromDocument();
removeDetachedChildren();
}
bool ContainerNode::isChildTypeAllowed(const Node& child) const
{
if (!child.isDocumentFragment())
return childTypeAllowed(child.nodeType());
for (Node* node = child.firstChild(); node; node = node->nextSibling()) {
if (!childTypeAllowed(node->nodeType()))
return false;
}
return true;
}
bool ContainerNode::containsConsideringHostElements(const Node& newChild) const
{
if (isInShadowTree() || document().isTemplateDocument())
return newChild.containsIncludingHostElements(*this);
return newChild.contains(this);
}
bool ContainerNode::checkAcceptChild(const Node* newChild, const Node* oldChild, ExceptionState& exceptionState) const
{
if (!newChild) {
exceptionState.throwDOMException(NotFoundError, "The new child element is null.");
return false;
}
if ((newChild->isElementNode() || newChild->isTextNode()) && isElementNode()) {
ASSERT(isChildTypeAllowed(*newChild));
if (containsConsideringHostElements(*newChild)) {
exceptionState.throwDOMException(HierarchyRequestError, "The new child element contains the parent.");
return false;
}
return true;
}
ASSERT(!newChild->isPseudoElement());
if (newChild->isPseudoElement()) {
exceptionState.throwDOMException(HierarchyRequestError, "The new child element is a pseudo-element.");
return false;
}
if (containsConsideringHostElements(*newChild)) {
exceptionState.throwDOMException(HierarchyRequestError, "The new child element contains the parent.");
return false;
}
if (oldChild && isDocumentNode()) {
if (!toDocument(this)->canReplaceChild(*newChild, *oldChild)) {
exceptionState.throwDOMException(HierarchyRequestError, "Failed to replace child.");
return false;
}
} else if (!isChildTypeAllowed(*newChild)) {
exceptionState.throwDOMException(HierarchyRequestError, "Nodes of type '" + newChild->nodeName() + "' may not be inserted inside nodes of type '" + nodeName() + "'.");
return false;
}
return true;
}
bool ContainerNode::checkAcceptChildGuaranteedNodeTypes(const Node& newChild, ExceptionState& exceptionState) const
{
ASSERT(isChildTypeAllowed(newChild));
if (newChild.contains(this)) {
exceptionState.throwDOMException(HierarchyRequestError, "The new child element contains the parent.");
return false;
}
return true;
}
void ContainerNode::insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionState& exceptionState)
{
#if !ENABLE(OILPAN)
ASSERT(refCount() || parentOrShadowHostNode());
#endif
RefPtr<Node> protect(this);
if (!refChild) {
appendChild(newChild, exceptionState);
return;
}
if (!checkAcceptChild(newChild.get(), 0, exceptionState))
return;
ASSERT(newChild);
if (refChild->parentNode() != this) {
exceptionState.throwDOMException(NotFoundError, "The node before which the new node is to be inserted is not a child of this node.");
return;
}
if (refChild->previousSibling() == newChild || refChild == newChild)
return;
RefPtr<Node> next = refChild;
NodeVector targets;
collectChildrenAndRemoveFromOldParent(*newChild, targets, exceptionState);
if (exceptionState.hadException())
return;
if (targets.isEmpty())
return;
if (!checkAcceptChildGuaranteedNodeTypes(*newChild, exceptionState))
return;
InspectorInstrumentation::willInsertDOMNode(this);
ChildListMutationScope mutation(*this);
for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
ASSERT(*it);
Node& child = **it;
if (next->parentNode() != this)
break;
if (child.parentNode())
break;
treeScope().adoptIfNeeded(child);
insertBeforeCommon(*next, child);
updateTreeAfterInsertion(child);
}
dispatchSubtreeModifiedEvent();
}
void ContainerNode::insertBeforeCommon(Node& nextChild, Node& newChild)
{
NoEventDispatchAssertion assertNoEventDispatch;
ASSERT(!newChild.parentNode());
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.parentNode() == this);
ASSERT(!newChild->isDocumentFragment());
ASSERT(!isHTMLTemplateElement(this));
if (nextChild.previousSibling() == newChild || nextChild == newChild)
return;
if (document() != newChild->document())
document().adoptNode(newChild.get(), ASSERT_NO_EXCEPTION);
insertBeforeCommon(nextChild, *newChild);
newChild->updateAncestorConnectedSubframeCountForInsertion();
ChildListMutationScope(*this).childAdded(*newChild);
childrenChanged(true, newChild->previousSibling(), &nextChild, 1);
ChildNodeInsertionNotifier(*this).notify(*newChild);
}
void ContainerNode::replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionState& exceptionState)
{
#if !ENABLE(OILPAN)
ASSERT(refCount() || parentOrShadowHostNode());
#endif
RefPtr<Node> protect(this);
if (oldChild == newChild)
return;
if (!oldChild) {
exceptionState.throwDOMException(NotFoundError, "The node to be replaced is null.");
return;
}
if (!checkAcceptChild(newChild.get(), oldChild, exceptionState))
return;
if (oldChild->parentNode() != this) {
exceptionState.throwDOMException(NotFoundError, "The node to be replaced is not a child of this node.");
return;
}
ChildListMutationScope mutation(*this);
RefPtr<Node> next = oldChild->nextSibling();
RefPtr<Node> removedChild = oldChild;
removeChild(oldChild, exceptionState);
if (exceptionState.hadException())
return;
if (next && (next->previousSibling() == newChild || next == newChild))
return;
if (!checkAcceptChild(newChild.get(), oldChild, exceptionState))
return;
NodeVector targets;
collectChildrenAndRemoveFromOldParent(*newChild, targets, exceptionState);
if (exceptionState.hadException())
return;
if (!checkAcceptChild(newChild.get(), oldChild, exceptionState))
return;
InspectorInstrumentation::willInsertDOMNode(this);
for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
ASSERT(*it);
Node& child = **it;
if (next && next->parentNode() != this)
break;
if (child.parentNode())
break;
treeScope().adoptIfNeeded(child);
{
NoEventDispatchAssertion assertNoEventDispatch;
if (next)
insertBeforeCommon(*next, child);
else
appendChildToContainer(child, *this);
}
updateTreeAfterInsertion(child);
}
dispatchSubtreeModifiedEvent();
}
void ContainerNode::willRemoveChild(Node& child)
{
ASSERT(child.parentNode() == this);
ChildListMutationScope(*this).willRemoveChild(child);
child.notifyMutationObserversNodeWillDetach();
dispatchChildRemovalEvents(child);
document().nodeWillBeRemoved(child);
ChildFrameDisconnector(child).disconnect();
}
void ContainerNode::willRemoveChildren()
{
NodeVector children;
getChildNodes(*this, children);
ChildListMutationScope mutation(*this);
for (NodeVector::const_iterator it = children.begin(); it != children.end(); ++it) {
ASSERT(*it);
Node& child = **it;
mutation.willRemoveChild(child);
child.notifyMutationObserversNodeWillDetach();
dispatchChildRemovalEvents(child);
}
ChildFrameDisconnector(*this).disconnect(ChildFrameDisconnector::DescendantsOnly);
}
void ContainerNode::disconnectDescendantFrames()
{
ChildFrameDisconnector(*this).disconnect();
}
void ContainerNode::removeChild(Node* oldChild, ExceptionState& exceptionState)
{
#if !ENABLE(OILPAN)
ASSERT(refCount() || parentOrShadowHostNode());
#endif
RefPtr<Node> protect(this);
if (!oldChild || oldChild->parentNode() != this || oldChild->isPseudoElement()) {
exceptionState.throwDOMException(NotFoundError, "The node to be removed is not a child of this node.");
return;
}
RefPtr<Node> child = oldChild;
document().removeFocusedElementOfSubtree(child.get());
if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(document()))
fullscreen->removeFullScreenElementOfSubtree(child.get());
if (child->parentNode() != this) {
exceptionState.throwDOMException(NotFoundError, "The node to be removed is no longer a child of this node. Perhaps it was moved in a 'blur' event handler?");
return;
}
willRemoveChild(*child);
if (child->parentNode() != this) {
exceptionState.throwDOMException(NotFoundError, "The node to be removed is no longer a child of this node. Perhaps it was moved in response to a mutation?");
return;
}
{
HTMLFrameOwnerElement::UpdateSuspendScope suspendWidgetHierarchyUpdates;
Node* prev = child->previousSibling();
Node* next = child->nextSibling();
removeBetween(prev, next, *child);
childrenChanged(false, prev, next, -1);
ChildNodeRemovalNotifier(*this).notify(*child);
}
dispatchSubtreeModifiedEvent();
}
void ContainerNode::removeBetween(Node* previousChild, Node* nextChild, Node& oldChild)
{
NoEventDispatchAssertion assertNoEventDispatch;
ASSERT(oldChild.parentNode() == this);
if (!oldChild.needsAttach())
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.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);
}
void ContainerNode::removeChildren()
{
if (!m_firstChild)
return;
RefPtr<ContainerNode> protect(this);
if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(document()))
fullscreen->removeFullScreenElementOfSubtree(this, true);
willRemoveChildren();
{
SubframeLoadingDisabler disabler(*this);
document().removeFocusedElementOfSubtree(this, true);
document().nodeChildrenWillBeRemoved(*this);
}
NodeVector removedChildren;
{
HTMLFrameOwnerElement::UpdateSuspendScope suspendWidgetHierarchyUpdates;
{
NoEventDispatchAssertion assertNoEventDispatch;
removedChildren.reserveInitialCapacity(countChildren());
while (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]);
}
dispatchSubtreeModifiedEvent();
}
void ContainerNode::appendChild(PassRefPtr<Node> newChild, ExceptionState& exceptionState)
{
RefPtr<ContainerNode> protect(this);
ASSERT(refCount() || parentOrShadowHostNode());
if (!checkAcceptChild(newChild.get(), 0, exceptionState))
return;
ASSERT(newChild);
if (newChild == m_lastChild)
return;
NodeVector targets;
collectChildrenAndRemoveFromOldParent(*newChild, targets, exceptionState);
if (exceptionState.hadException())
return;
if (targets.isEmpty())
return;
if (!checkAcceptChildGuaranteedNodeTypes(*newChild, exceptionState))
return;
InspectorInstrumentation::willInsertDOMNode(this);
ChildListMutationScope mutation(*this);
for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
ASSERT(*it);
Node& child = **it;
if (child.parentNode())
break;
treeScope().adoptIfNeeded(child);
{
NoEventDispatchAssertion assertNoEventDispatch;
appendChildToContainer(child, *this);
}
updateTreeAfterInsertion(child);
}
dispatchSubtreeModifiedEvent();
}
void ContainerNode::parserAppendChild(PassRefPtr<Node> newChild)
{
ASSERT(newChild);
ASSERT(!newChild->parentNode());
ASSERT(!newChild->isDocumentFragment());
ASSERT(!isHTMLTemplateElement(this));
if (document() != newChild->document())
document().adoptNode(newChild.get(), ASSERT_NO_EXCEPTION);
Node* last = m_lastChild;
{
NoEventDispatchAssertion assertNoEventDispatch;
appendChildToContainer(*newChild, *this);
treeScope().adoptIfNeeded(*newChild);
}
newChild->updateAncestorConnectedSubframeCountForInsertion();
ChildListMutationScope(*this).childAdded(*newChild);
childrenChanged(true, last, 0, 1);
ChildNodeInsertionNotifier(*this).notify(*newChild);
}
void ContainerNode::attach(const AttachContext& context)
{
attachChildren(context);
clearChildNeedsStyleRecalc();
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();
if (childCountDelta > 0 && !childNeedsStyleRecalc()) {
setChildNeedsStyleRecalc();
markAncestorsWithChildNeedsStyleRecalc();
}
}
void ContainerNode::cloneChildNodes(ContainerNode *clone)
{
TrackExceptionState exceptionState;
for (Node* n = firstChild(); n && !exceptionState.hadException(); n = n->nextSibling())
clone->appendChild(n->cloneNode(true), exceptionState);
}
bool ContainerNode::getUpperLeftCorner(FloatPoint& point) const
{
if (!renderer())
return false;
RenderObject* o = renderer();
if (!o->isInline() || o->isReplaced()) {
point = o->localToAbsolute(FloatPoint(), UseTransforms);
return true;
}
while (o) {
RenderObject* 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()) {
} 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().toFloat());
} else if (o->isBox()) {
RenderBox* box = toRenderBox(o);
point.moveBy(box->location());
}
point = o->container()->localToAbsolute(point, UseTransforms);
return true;
}
}
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;
}
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 (foundUpperLeft != foundLowerRight) {
if (foundUpperLeft)
lowerRight = upperLeft;
else
upperLeft = lowerRight;
}
return enclosingLayoutRect(FloatRect(upperLeft, lowerRight.expandedTo(upperLeft) - upperLeft));
}
void ContainerNode::focusStateChanged()
{
if (!renderer())
return;
if ((isElementNode() && toElement(this)->childrenAffectedByFocus())
|| (renderStyle()->affectedByFocus() && renderStyle()->hasPseudoStyle(FIRST_LETTER)))
setNeedsStyleRecalc(SubtreeStyleChange);
else if (renderStyle()->affectedByFocus())
setNeedsStyleRecalc(LocalStyleChange);
if (renderer() && renderer()->style()->hasAppearance())
RenderTheme::theme().stateChanged(renderer(), FocusState);
}
void ContainerNode::setFocus(bool received)
{
if (focused() == received)
return;
Node::setFocus(received);
focusStateChanged();
if (renderer() || received)
return;
if (isElementNode() && toElement(this)->childrenAffectedByFocus())
setNeedsStyleRecalc(SubtreeStyleChange);
else
setNeedsStyleRecalc(LocalStyleChange);
}
void ContainerNode::setActive(bool down)
{
if (down == active())
return;
Node::setActive(down);
if (renderer()) {
if ((isElementNode() && toElement(this)->childrenAffectedByActive())
|| (renderStyle()->affectedByActive() && renderStyle()->hasPseudoStyle(FIRST_LETTER)))
setNeedsStyleRecalc(SubtreeStyleChange);
else if (renderStyle()->affectedByActive())
setNeedsStyleRecalc(LocalStyleChange);
if (renderStyle()->hasAppearance())
RenderTheme::theme().stateChanged(renderer(), PressedState);
}
}
void ContainerNode::setHovered(bool over)
{
if (over == hovered())
return;
Node::setHovered(over);
if (!renderer()) {
if (over)
return;
if (isElementNode() && toElement(this)->childrenAffectedByHover())
setNeedsStyleRecalc(SubtreeStyleChange);
else
setNeedsStyleRecalc(LocalStyleChange);
return;
}
if ((isElementNode() && toElement(this)->childrenAffectedByHover())
|| (renderStyle()->affectedByHover() && renderStyle()->hasPseudoStyle(FIRST_LETTER)))
setNeedsStyleRecalc(SubtreeStyleChange);
else if (renderStyle()->affectedByHover())
setNeedsStyleRecalc(LocalStyleChange);
if (renderer()->style()->hasAppearance())
RenderTheme::theme().stateChanged(renderer(), HoverState);
}
PassRefPtr<HTMLCollection> ContainerNode::children()
{
return ensureRareData().ensureNodeLists().addCache<HTMLCollection>(*this, NodeChildren);
}
unsigned ContainerNode::countChildren() const
{
unsigned count = 0;
Node *n;
for (n = firstChild(); n; n = n->nextSibling())
count++;
return count;
}
Node* ContainerNode::traverseToChildAt(unsigned index) const
{
unsigned i;
Node *n = firstChild();
for (i = 0; n != 0 && i < index; i++)
n = n->nextSibling();
return n;
}
PassRefPtr<Element> ContainerNode::querySelector(const AtomicString& selectors, ExceptionState& exceptionState)
{
if (selectors.isEmpty()) {
exceptionState.throwDOMException(SyntaxError, "The provided selector is empty.");
return nullptr;
}
SelectorQuery* selectorQuery = document().selectorQueryCache().add(selectors, document(), exceptionState);
if (!selectorQuery)
return nullptr;
return selectorQuery->queryFirst(*this);
}
PassRefPtr<NodeList> ContainerNode::querySelectorAll(const AtomicString& selectors, ExceptionState& exceptionState)
{
if (selectors.isEmpty()) {
exceptionState.throwDOMException(SyntaxError, "The provided selector is empty.");
return nullptr;
}
SelectorQuery* selectorQuery = document().selectorQueryCache().add(selectors, document(), exceptionState);
if (!selectorQuery)
return nullptr;
return selectorQuery->queryAll(*this);
}
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(EventTypeNames::DOMNodeInserted, true, c->parentNode()));
if (c->inDocument() && document->hasListenerType(Document::DOMNODEINSERTEDINTODOCUMENT_LISTENER)) {
for (; c; c = NodeTraversal::next(*c, &child))
c->dispatchScopedEvent(MutationEvent::create(EventTypeNames::DOMNodeInsertedIntoDocument, false));
}
}
static void dispatchChildRemovalEvents(Node& child)
{
if (child.isInShadowTree()) {
InspectorInstrumentation::willRemoveDOMNode(&child);
return;
}
ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
InspectorInstrumentation::willRemoveDOMNode(&child);
RefPtr<Node> c(child);
RefPtr<Document> document(child.document());
if (c->parentNode() && document->hasListenerType(Document::DOMNODEREMOVED_LISTENER)) {
NodeChildRemovalTracker scope(child);
c->dispatchScopedEvent(MutationEvent::create(EventTypeNames::DOMNodeRemoved, true, c->parentNode()));
}
if (c->inDocument() && document->hasListenerType(Document::DOMNODEREMOVEDFROMDOCUMENT_LISTENER)) {
NodeChildRemovalTracker scope(child);
for (; c; c = NodeTraversal::next(*c, &child))
c->dispatchScopedEvent(MutationEvent::create(EventTypeNames::DOMNodeRemovedFromDocument, false));
}
}
void ContainerNode::updateTreeAfterInsertion(Node& child)
{
ASSERT(refCount());
ASSERT(child.refCount());
ChildListMutationScope(*this).childAdded(child);
childrenChanged(false, child.previousSibling(), child.nextSibling(), 1);
ChildNodeInsertionNotifier(*this).notify(child);
dispatchChildInsertionEvents(child);
}
bool ContainerNode::hasRestyleFlagInternal(DynamicRestyleFlags mask) const
{
return rareData()->hasRestyleFlag(mask);
}
bool ContainerNode::hasRestyleFlagsInternal() const
{
return rareData()->hasRestyleFlags();
}
void ContainerNode::setRestyleFlag(DynamicRestyleFlags mask)
{
ASSERT(isElementNode() || isShadowRoot());
ensureRareData().setRestyleFlag(mask);
}
void ContainerNode::checkForChildrenAdjacentRuleChanges()
{
bool hasDirectAdjacentRules = childrenAffectedByDirectAdjacentRules();
bool hasIndirectAdjacentRules = childrenAffectedByIndirectAdjacentRules();
if (!hasDirectAdjacentRules && !hasIndirectAdjacentRules)
return;
unsigned forceCheckOfNextElementCount = 0;
bool forceCheckOfAnyElementSibling = false;
Document& document = this->document();
for (Node* child = firstChild(); child; child = child->nextSibling()) {
if (!child->isElementNode())
continue;
Element* element = toElement(child);
bool childRulesChanged = element->needsStyleRecalc() && element->styleChangeType() >= SubtreeStyleChange;
if (forceCheckOfNextElementCount || forceCheckOfAnyElementSibling)
element->setNeedsStyleRecalc(SubtreeStyleChange);
if (forceCheckOfNextElementCount)
forceCheckOfNextElementCount--;
if (childRulesChanged && hasDirectAdjacentRules)
forceCheckOfNextElementCount = document.styleEngine()->maxDirectAdjacentSelectors();
forceCheckOfAnyElementSibling = forceCheckOfAnyElementSibling || (childRulesChanged && hasIndirectAdjacentRules);
}
}
PassRefPtr<HTMLCollection> ContainerNode::getElementsByTagName(const AtomicString& localName)
{
if (localName.isNull())
return nullptr;
if (document().isHTMLDocument())
return ensureRareData().ensureNodeLists().addCache<HTMLTagCollection>(*this, HTMLTagCollectionType, localName);
return ensureRareData().ensureNodeLists().addCache<TagCollection>(*this, TagCollectionType, localName);
}
PassRefPtr<HTMLCollection> ContainerNode::getElementsByTagNameNS(const AtomicString& namespaceURI, const AtomicString& localName)
{
if (localName.isNull())
return nullptr;
if (namespaceURI == starAtom)
return getElementsByTagName(localName);
return ensureRareData().ensureNodeLists().addCache(*this, namespaceURI.isEmpty() ? nullAtom : namespaceURI, localName);
}
PassRefPtr<NodeList> ContainerNode::getElementsByName(const AtomicString& elementName)
{
return ensureRareData().ensureNodeLists().addCache<NameNodeList>(*this, NameNodeListType, elementName);
}
PassRefPtr<HTMLCollection> ContainerNode::getElementsByClassName(const AtomicString& classNames)
{
return ensureRareData().ensureNodeLists().addCache<ClassCollection>(*this, ClassCollectionType, classNames);
}
PassRefPtr<RadioNodeList> ContainerNode::radioNodeList(const AtomicString& name, bool onlyMatchImgElements)
{
ASSERT(isHTMLFormElement(this) || isHTMLFieldSetElement(this));
CollectionType type = onlyMatchImgElements ? RadioImgNodeListType : RadioNodeListType;
return ensureRareData().ensureNodeLists().addCache<RadioNodeList>(*this, type, name);
}
#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
}