#include "config.h"
#include "ContainerNode.h"
#include "AXObjectCache.h"
#include "AllDescendantsCollection.h"
#include "ChildListMutationScope.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "ClassCollection.h"
#include "CommonVM.h"
#include "ContainerNodeAlgorithms.h"
#include "Editor.h"
#include "EventNames.h"
#include "FloatRect.h"
#include "FrameView.h"
#include "GenericCachedHTMLCollection.h"
#include "HTMLFormControlsCollection.h"
#include "HTMLOptionsCollection.h"
#include "HTMLSlotElement.h"
#include "HTMLTableRowsCollection.h"
#include "InlineTextBox.h"
#include "InspectorInstrumentation.h"
#include "JSNode.h"
#include "LabelsNodeList.h"
#include "MutationEvent.h"
#include "NameNodeList.h"
#include "NoEventDispatchAssertion.h"
#include "NodeRareData.h"
#include "NodeRenderStyle.h"
#include "RadioNodeList.h"
#include "RenderBox.h"
#include "RenderTheme.h"
#include "RenderTreeUpdater.h"
#include "RenderWidget.h"
#include "RootInlineBox.h"
#include "SVGDocumentExtensions.h"
#include "SVGElement.h"
#include "SVGNames.h"
#include "SelectorQuery.h"
#include "TemplateContentDocumentFragment.h"
#include <algorithm>
#include <wtf/CurrentTime.h>
#include <wtf/Variant.h>
namespace WebCore {
static void dispatchChildInsertionEvents(Node&);
static void dispatchChildRemovalEvents(Node&);
ChildNodesLazySnapshot* ChildNodesLazySnapshot::latestSnapshot;
#ifndef NDEBUG
unsigned NoEventDispatchAssertion::s_count = 0;
NoEventDispatchAssertion::EventAllowedScope* NoEventDispatchAssertion::EventAllowedScope::s_currentScope = nullptr;
#endif
static ExceptionOr<void> collectChildrenAndRemoveFromOldParent(Node& node, NodeVector& nodes)
{
if (!is<DocumentFragment>(node)) {
nodes.append(node);
auto* oldParent = node.parentNode();
if (!oldParent)
return { };
return oldParent->removeChild(node);
}
getChildNodes(node, nodes);
downcast<DocumentFragment>(node).removeChildren();
return { };
}
void ContainerNode::removeDetachedChildren()
{
if (connectedSubframeCount()) {
for (Node* child = firstChild(); child; child = child->nextSibling())
child->updateAncestorConnectedSubframeCountForRemoval();
}
removeDetachedChildrenInContainer(*this);
}
static inline void destroyRenderTreeIfNeeded(Node& child)
{
bool childIsHTMLSlotElement = false;
childIsHTMLSlotElement = is<HTMLSlotElement>(child);
bool isElement = is<Element>(child);
if (!child.renderer() && !childIsHTMLSlotElement
&& !(isElement && downcast<Element>(child).isNamedFlowContentElement()))
return;
if (isElement)
RenderTreeUpdater::tearDownRenderers(downcast<Element>(child));
else if (is<Text>(child))
RenderTreeUpdater::tearDownRenderer(downcast<Text>(child));
}
void ContainerNode::takeAllChildrenFrom(ContainerNode* oldParent)
{
ASSERT(oldParent);
NodeVector children;
getChildNodes(*oldParent, children);
if (oldParent->document().hasMutationObserversOfType(MutationObserver::ChildList)) {
ChildListMutationScope mutation(*oldParent);
for (auto& child : children)
mutation.willRemoveChild(child);
}
disconnectSubframesIfNeeded(*oldParent, DescendantsOnly);
{
NoEventDispatchAssertion assertNoEventDispatch;
oldParent->document().nodeChildrenWillBeRemoved(*oldParent);
WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
while (RefPtr<Node> child = oldParent->m_firstChild) {
oldParent->removeBetween(nullptr, child->nextSibling(), *child);
notifyChildNodeRemoved(*oldParent, *child);
}
ChildChange change = { AllChildrenRemoved, nullptr, nullptr, ChildChangeSourceParser };
childrenChanged(change);
}
document().notifyRemovePendingSheetIfNeeded();
for (auto& child : children) {
RELEASE_ASSERT(!child->parentNode() && &child->treeScope() == &treeScope());
ASSERT(!ensurePreInsertionValidity(child, nullptr).hasException());
treeScope().adoptIfNeeded(child);
parserAppendChild(child);
}
}
ContainerNode::~ContainerNode()
{
if (!isDocumentNode())
willBeDeletedFrom(document());
removeDetachedChildren();
}
static inline bool isChildTypeAllowed(ContainerNode& newParent, Node& child)
{
if (!child.isDocumentFragment())
return newParent.childTypeAllowed(child.nodeType());
for (Node* node = child.firstChild(); node; node = node->nextSibling()) {
if (!newParent.childTypeAllowed(node->nodeType()))
return false;
}
return true;
}
static inline bool isInTemplateContent(const Node* node)
{
Document& document = node->document();
return &document == document.templateDocument();
}
static inline bool containsConsideringHostElements(const Node& newChild, const Node& newParent)
{
return (newParent.isInShadowTree() || isInTemplateContent(&newParent))
? newChild.containsIncludingHostElements(&newParent)
: newChild.contains(&newParent);
}
static inline ExceptionOr<void> checkAcceptChild(ContainerNode& newParent, Node& newChild, const Node* refChild, Document::AcceptChildOperation operation)
{
if ((newChild.isElementNode() || newChild.isTextNode()) && newParent.isElementNode()) {
ASSERT(!newParent.isDocumentTypeNode());
ASSERT(isChildTypeAllowed(newParent, newChild));
if (containsConsideringHostElements(newChild, newParent))
return Exception { HIERARCHY_REQUEST_ERR };
if (operation == Document::AcceptChildOperation::InsertOrAdd && refChild && refChild->parentNode() != &newParent)
return Exception { NOT_FOUND_ERR };
return { };
}
ASSERT(!newChild.isPseudoElement());
if (newChild.isPseudoElement())
return Exception { HIERARCHY_REQUEST_ERR };
if (containsConsideringHostElements(newChild, newParent))
return Exception { HIERARCHY_REQUEST_ERR };
if (operation == Document::AcceptChildOperation::InsertOrAdd && refChild && refChild->parentNode() != &newParent)
return Exception { NOT_FOUND_ERR };
if (is<Document>(newParent)) {
if (!downcast<Document>(newParent).canAcceptChild(newChild, refChild, operation))
return Exception { HIERARCHY_REQUEST_ERR };
} else if (!isChildTypeAllowed(newParent, newChild))
return Exception { HIERARCHY_REQUEST_ERR };
return { };
}
static inline ExceptionOr<void> checkAcceptChildGuaranteedNodeTypes(ContainerNode& newParent, Node& newChild)
{
ASSERT(!newParent.isDocumentTypeNode());
ASSERT(isChildTypeAllowed(newParent, newChild));
if (newChild.contains(&newParent))
return Exception { HIERARCHY_REQUEST_ERR };
return { };
}
ExceptionOr<void> ContainerNode::ensurePreInsertionValidity(Node& newChild, Node* refChild)
{
return checkAcceptChild(*this, newChild, refChild, Document::AcceptChildOperation::InsertOrAdd);
}
static inline ExceptionOr<void> checkPreReplacementValidity(ContainerNode& newParent, Node& newChild, Node& oldChild)
{
return checkAcceptChild(newParent, newChild, &oldChild, Document::AcceptChildOperation::Replace);
}
ExceptionOr<void> ContainerNode::insertBefore(Node& newChild, Node* refChild)
{
ASSERT(refCount() || parentOrShadowHostNode());
auto validityCheckResult = ensurePreInsertionValidity(newChild, refChild);
if (validityCheckResult.hasException())
return validityCheckResult.releaseException();
if (refChild == &newChild)
refChild = newChild.nextSibling();
if (!refChild)
return appendChildWithoutPreInsertionValidityCheck(newChild);
Ref<ContainerNode> protectedThis(*this);
Ref<Node> next(*refChild);
NodeVector targets;
auto removeResult = collectChildrenAndRemoveFromOldParent(newChild, targets);
if (removeResult.hasException())
return removeResult.releaseException();
if (targets.isEmpty())
return { };
auto checkAcceptResult = checkAcceptChildGuaranteedNodeTypes(*this, newChild);
if (checkAcceptResult.hasException())
return checkAcceptResult.releaseException();
InspectorInstrumentation::willInsertDOMNode(document(), *this);
ChildListMutationScope mutation(*this);
for (auto& child : targets) {
if (next->parentNode() != this)
break;
if (child->parentNode())
break;
{
NoEventDispatchAssertion assertNoEventDispatch;
treeScope().adoptIfNeeded(child);
insertBeforeCommon(next, child);
}
updateTreeAfterInsertion(child);
}
dispatchSubtreeModifiedEvent();
return { };
}
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.setParentNode(this);
newChild.setPreviousSibling(prev);
newChild.setNextSibling(&nextChild);
}
void ContainerNode::appendChildCommon(Node& child)
{
NoEventDispatchAssertion assertNoEventDispatch;
child.setParentNode(this);
if (m_lastChild) {
child.setPreviousSibling(m_lastChild);
m_lastChild->setNextSibling(&child);
} else
m_firstChild = &child;
m_lastChild = &child;
}
void ContainerNode::notifyChildInserted(Node& child, ChildChangeSource source)
{
ChildListMutationScope(*this).childAdded(child);
NodeVector postInsertionNotificationTargets;
notifyChildNodeInserted(*this, child, postInsertionNotificationTargets);
ChildChange change;
change.type = child.isElementNode() ? ElementInserted : child.isTextNode() ? TextInserted : NonContentsChildInserted;
change.previousSiblingElement = ElementTraversal::previousSibling(child);
change.nextSiblingElement = ElementTraversal::nextSibling(child);
change.source = source;
childrenChanged(change);
document().notifyRemovePendingSheetIfNeeded();
for (auto& target : postInsertionNotificationTargets)
target->finishedInsertingSubtree();
}
void ContainerNode::notifyChildRemoved(Node& child, Node* previousSibling, Node* nextSibling, ChildChangeSource source)
{
NoEventDispatchAssertion assertNoEventDispatch;
notifyChildNodeRemoved(*this, child);
ChildChange change;
change.type = is<Element>(child) ? ElementRemoved : is<Text>(child) ? TextRemoved : NonContentsChildRemoved;
change.previousSiblingElement = (!previousSibling || is<Element>(*previousSibling)) ? downcast<Element>(previousSibling) : ElementTraversal::previousSibling(*previousSibling);
change.nextSiblingElement = (!nextSibling || is<Element>(*nextSibling)) ? downcast<Element>(nextSibling) : ElementTraversal::nextSibling(*nextSibling);
change.source = source;
childrenChanged(change);
}
void ContainerNode::parserInsertBefore(Node& newChild, Node& nextChild)
{
ASSERT(nextChild.parentNode() == this);
ASSERT(!newChild.isDocumentFragment());
ASSERT(!hasTagName(HTMLNames::templateTag));
if (nextChild.previousSibling() == &newChild || &nextChild == &newChild) return;
if (&document() != &newChild.document())
document().adoptNode(newChild);
insertBeforeCommon(nextChild, newChild);
newChild.updateAncestorConnectedSubframeCountForInsertion();
notifyChildInserted(newChild, ChildChangeSourceParser);
}
ExceptionOr<void> ContainerNode::replaceChild(Node& newChild, Node& oldChild)
{
ASSERT(refCount() || parentOrShadowHostNode());
Ref<ContainerNode> protectedThis(*this);
auto validityResult = checkPreReplacementValidity(*this, newChild, oldChild);
if (validityResult.hasException())
return validityResult.releaseException();
if (oldChild.parentNode() != this)
return Exception { NOT_FOUND_ERR };
RefPtr<Node> refChild = oldChild.nextSibling();
if (refChild.get() == &newChild)
refChild = refChild->nextSibling();
NodeVector targets;
{
ChildListMutationScope mutation(*this);
auto collectResult = collectChildrenAndRemoveFromOldParent(newChild, targets);
if (collectResult.hasException())
return collectResult.releaseException();
}
validityResult = checkPreReplacementValidity(*this, newChild, oldChild);
if (validityResult.hasException())
return validityResult.releaseException();
Ref<Node> protectOldChild(oldChild);
ChildListMutationScope mutation(*this);
if (oldChild.parentNode()) {
auto removeResult = removeChild(oldChild);
if (removeResult.hasException())
return removeResult.releaseException();
validityResult = checkPreReplacementValidity(*this, newChild, oldChild);
if (validityResult.hasException())
return validityResult.releaseException();
}
InspectorInstrumentation::willInsertDOMNode(document(), *this);
for (auto& child : targets) {
if (refChild && refChild->parentNode() != this)
break;
if (child->parentNode())
break;
{
NoEventDispatchAssertion assertNoEventDispatch;
treeScope().adoptIfNeeded(child);
if (refChild)
insertBeforeCommon(*refChild, child.get());
else
appendChildCommon(child);
}
updateTreeAfterInsertion(child.get());
}
dispatchSubtreeModifiedEvent();
return { };
}
static void willRemoveChild(ContainerNode& container, Node& child)
{
ASSERT(child.parentNode());
ChildListMutationScope(*child.parentNode()).willRemoveChild(child);
child.notifyMutationObserversNodeWillDetach();
dispatchChildRemovalEvents(child);
if (child.parentNode() != &container)
return;
if (is<ContainerNode>(child))
disconnectSubframesIfNeeded(downcast<ContainerNode>(child), RootAndDescendants);
}
static void willRemoveChildren(ContainerNode& container)
{
NodeVector children;
getChildNodes(container, children);
ChildListMutationScope mutation(container);
for (auto& child : children) {
mutation.willRemoveChild(child.get());
child->notifyMutationObserversNodeWillDetach();
dispatchChildRemovalEvents(child.get());
}
disconnectSubframesIfNeeded(container, DescendantsOnly);
}
void ContainerNode::disconnectDescendantFrames()
{
disconnectSubframesIfNeeded(*this, RootAndDescendants);
}
ExceptionOr<void> ContainerNode::removeChild(Node& oldChild)
{
ASSERT(refCount() || parentOrShadowHostNode());
Ref<ContainerNode> protectedThis(*this);
if (oldChild.parentNode() != this)
return Exception { NOT_FOUND_ERR };
Ref<Node> child(oldChild);
willRemoveChild(*this, child);
if (child->parentNode() != this)
return Exception { NOT_FOUND_ERR };
{
WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
NoEventDispatchAssertion assertNoEventDispatch;
document().nodeWillBeRemoved(child);
Node* prev = child->previousSibling();
Node* next = child->nextSibling();
removeBetween(prev, next, child);
notifyChildRemoved(child, prev, next, ChildChangeSourceAPI);
}
if (document().svgExtensions()) {
Element* shadowHost = this->shadowHost();
if (!shadowHost || !shadowHost->hasTagName(SVGNames::useTag))
document().accessSVGExtensions().rebuildElements();
}
document().notifyRemovePendingSheetIfNeeded();
dispatchSubtreeModifiedEvent();
return { };
}
void ContainerNode::removeBetween(Node* previousChild, Node* nextChild, Node& oldChild)
{
InspectorInstrumentation::didRemoveDOMNode(oldChild.document(), oldChild);
NoEventDispatchAssertion assertNoEventDispatch;
ASSERT(oldChild.parentNode() == this);
destroyRenderTreeIfNeeded(oldChild);
if (nextChild) {
nextChild->setPreviousSibling(previousChild);
oldChild.setNextSibling(nullptr);
} else {
ASSERT(m_lastChild == &oldChild);
m_lastChild = previousChild;
}
if (previousChild) {
previousChild->setNextSibling(nextChild);
oldChild.setPreviousSibling(nullptr);
} else {
ASSERT(m_firstChild == &oldChild);
m_firstChild = nextChild;
}
ASSERT(m_firstChild != &oldChild);
ASSERT(m_lastChild != &oldChild);
ASSERT(!oldChild.previousSibling());
ASSERT(!oldChild.nextSibling());
oldChild.setParentNode(nullptr);
document().adoptIfNeeded(oldChild);
}
void ContainerNode::parserRemoveChild(Node& oldChild)
{
disconnectSubframesIfNeeded(*this, DescendantsOnly);
if (oldChild.parentNode() != this)
return;
{
NoEventDispatchAssertion assertNoEventDispatch;
document().nodeChildrenWillBeRemoved(*this);
ASSERT(oldChild.parentNode() == this);
ASSERT(!oldChild.isDocumentFragment());
Node* prev = oldChild.previousSibling();
Node* next = oldChild.nextSibling();
ChildListMutationScope(*this).willRemoveChild(oldChild);
oldChild.notifyMutationObserversNodeWillDetach();
removeBetween(prev, next, oldChild);
notifyChildRemoved(oldChild, prev, next, ChildChangeSourceParser);
}
document().notifyRemovePendingSheetIfNeeded();
}
void ContainerNode::removeChildren()
{
if (!m_firstChild)
return;
Ref<ContainerNode> protectedThis(*this);
willRemoveChildren(*this);
{
WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
NoEventDispatchAssertion assertNoEventDispatch;
document().nodeChildrenWillBeRemoved(*this);
while (RefPtr<Node> child = m_firstChild) {
removeBetween(0, child->nextSibling(), *child);
notifyChildNodeRemoved(*this, *child);
}
ChildChange change = { AllChildrenRemoved, nullptr, nullptr, ChildChangeSourceAPI };
childrenChanged(change);
}
if (document().svgExtensions()) {
Element* shadowHost = this->shadowHost();
if (!shadowHost || !shadowHost->hasTagName(SVGNames::useTag))
document().accessSVGExtensions().rebuildElements();
}
document().notifyRemovePendingSheetIfNeeded();
dispatchSubtreeModifiedEvent();
}
ExceptionOr<void> ContainerNode::appendChild(Node& newChild)
{
ASSERT(refCount() || parentOrShadowHostNode());
auto validityCheckResult = ensurePreInsertionValidity(newChild, nullptr);
if (validityCheckResult.hasException())
return validityCheckResult.releaseException();
return appendChildWithoutPreInsertionValidityCheck(newChild);
}
ExceptionOr<void> ContainerNode::appendChildWithoutPreInsertionValidityCheck(Node& newChild)
{
Ref<ContainerNode> protectedThis(*this);
NodeVector targets;
auto removeResult = collectChildrenAndRemoveFromOldParent(newChild, targets);
if (removeResult.hasException())
return removeResult.releaseException();
if (targets.isEmpty())
return { };
auto nodeTypeResult = checkAcceptChildGuaranteedNodeTypes(*this, newChild);
if (nodeTypeResult.hasException())
return nodeTypeResult.releaseException();
InspectorInstrumentation::willInsertDOMNode(document(), *this);
ChildListMutationScope mutation(*this);
for (auto& child : targets) {
if (child->parentNode())
break;
{
NoEventDispatchAssertion assertNoEventDispatch;
treeScope().adoptIfNeeded(child);
appendChildCommon(child);
}
updateTreeAfterInsertion(child.get());
}
dispatchSubtreeModifiedEvent();
return { };
}
void ContainerNode::parserAppendChild(Node& newChild)
{
ASSERT(!newChild.parentNode()); ASSERT(!newChild.isDocumentFragment());
ASSERT(!hasTagName(HTMLNames::templateTag));
{
NoEventDispatchAssertion assertNoEventDispatch;
if (&document() != &newChild.document())
document().adoptNode(newChild);
appendChildCommon(newChild);
treeScope().adoptIfNeeded(newChild);
}
newChild.updateAncestorConnectedSubframeCountForInsertion();
notifyChildInserted(newChild, ChildChangeSourceParser);
}
void ContainerNode::childrenChanged(const ChildChange& change)
{
document().incDOMTreeVersion();
if (change.source == ChildChangeSourceAPI && change.type != TextChanged)
document().updateRangesAfterChildrenChanged(*this);
invalidateNodeListAndCollectionCachesInAncestors();
}
void ContainerNode::cloneChildNodes(ContainerNode& clone)
{
Document& targetDocument = clone.document();
for (Node* child = firstChild(); child; child = child->nextSibling()) {
auto clonedChild = child->cloneNodeInternal(targetDocument, CloningOperation::SelfWithTemplateContent);
if (!clone.appendChild(clonedChild).hasException() && is<ContainerNode>(*child))
downcast<ContainerNode>(*child).cloneChildNodes(downcast<ContainerNode>(clonedChild.get()));
}
}
unsigned ContainerNode::countChildNodes() const
{
unsigned count = 0;
for (Node* child = firstChild(); child; child = child->nextSibling())
++count;
return count;
}
Node* ContainerNode::traverseToChildAt(unsigned index) const
{
Node* child = firstChild();
for (; child && index > 0; --index)
child = child->nextSibling();
return child;
}
static void dispatchChildInsertionEvents(Node& child)
{
if (child.isInShadowTree())
return;
ASSERT_WITH_SECURITY_IMPLICATION(NoEventDispatchAssertion::isEventDispatchAllowedInSubtree(child));
RefPtr<Node> c = &child;
Ref<Document> document(child.document());
if (c->parentNode() && document->hasListenerType(Document::DOMNODEINSERTED_LISTENER))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeInsertedEvent, true, c->parentNode()));
if (c->inDocument() && document->hasListenerType(Document::DOMNODEINSERTEDINTODOCUMENT_LISTENER)) {
for (; c; c = NodeTraversal::next(*c, &child))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeInsertedIntoDocumentEvent, false));
}
}
static void dispatchChildRemovalEvents(Node& child)
{
if (child.isInShadowTree()) {
InspectorInstrumentation::willRemoveDOMNode(child.document(), child);
return;
}
ASSERT_WITH_SECURITY_IMPLICATION(NoEventDispatchAssertion::isEventDispatchAllowedInSubtree(child));
willCreatePossiblyOrphanedTreeByRemoval(&child);
InspectorInstrumentation::willRemoveDOMNode(child.document(), child);
RefPtr<Node> c = &child;
Ref<Document> document(child.document());
if (c->parentNode() && document->hasListenerType(Document::DOMNODEREMOVED_LISTENER))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeRemovedEvent, true, c->parentNode()));
if (c->inDocument() && document->hasListenerType(Document::DOMNODEREMOVEDFROMDOCUMENT_LISTENER)) {
for (; c; c = NodeTraversal::next(*c, &child))
c->dispatchScopedEvent(MutationEvent::create(eventNames().DOMNodeRemovedFromDocumentEvent, false));
}
}
void ContainerNode::updateTreeAfterInsertion(Node& child)
{
ASSERT(child.refCount());
notifyChildInserted(child, ChildChangeSourceAPI);
dispatchChildInsertionEvents(child);
}
ExceptionOr<Element*> ContainerNode::querySelector(const String& selectors)
{
auto query = document().selectorQueryForString(selectors);
if (query.hasException())
return query.releaseException();
return query.releaseReturnValue().queryFirst(*this);
}
ExceptionOr<Ref<NodeList>> ContainerNode::querySelectorAll(const String& selectors)
{
auto query = document().selectorQueryForString(selectors);
if (query.hasException())
return query.releaseException();
return query.releaseReturnValue().queryAll(*this);
}
Ref<HTMLCollection> ContainerNode::getElementsByTagName(const AtomicString& qualifiedName)
{
ASSERT(!qualifiedName.isNull());
if (qualifiedName == starAtom)
return ensureRareData().ensureNodeLists().addCachedCollection<AllDescendantsCollection>(*this, AllDescendants);
if (document().isHTMLDocument())
return ensureRareData().ensureNodeLists().addCachedCollection<HTMLTagCollection>(*this, ByHTMLTag, qualifiedName);
return ensureRareData().ensureNodeLists().addCachedCollection<TagCollection>(*this, ByTag, qualifiedName);
}
Ref<HTMLCollection> ContainerNode::getElementsByTagNameNS(const AtomicString& namespaceURI, const AtomicString& localName)
{
ASSERT(!localName.isNull());
return ensureRareData().ensureNodeLists().addCachedTagCollectionNS(*this, namespaceURI.isEmpty() ? nullAtom : namespaceURI, localName);
}
Ref<NodeList> ContainerNode::getElementsByName(const String& elementName)
{
return ensureRareData().ensureNodeLists().addCacheWithAtomicName<NameNodeList>(*this, elementName);
}
Ref<HTMLCollection> ContainerNode::getElementsByClassName(const AtomicString& classNames)
{
return ensureRareData().ensureNodeLists().addCachedCollection<ClassCollection>(*this, ByClass, classNames);
}
Ref<RadioNodeList> ContainerNode::radioNodeList(const AtomicString& name)
{
ASSERT(hasTagName(HTMLNames::formTag) || hasTagName(HTMLNames::fieldsetTag));
return ensureRareData().ensureNodeLists().addCacheWithAtomicName<RadioNodeList>(*this, name);
}
Ref<HTMLCollection> ContainerNode::children()
{
return ensureRareData().ensureNodeLists().addCachedCollection<GenericCachedHTMLCollection<CollectionTypeTraits<NodeChildren>::traversalType>>(*this, NodeChildren);
}
Element* ContainerNode::firstElementChild() const
{
return ElementTraversal::firstChild(*this);
}
Element* ContainerNode::lastElementChild() const
{
return ElementTraversal::lastChild(*this);
}
unsigned ContainerNode::childElementCount() const
{
auto children = childrenOfType<Element>(*this);
return std::distance(children.begin(), children.end());
}
ExceptionOr<void> ContainerNode::append(Vector<NodeOrString>&& vector)
{
auto result = convertNodesOrStringsIntoNode(WTFMove(vector));
if (result.hasException())
return result.releaseException();
auto node = result.releaseReturnValue();
if (!node)
return { };
return appendChild(*node);
}
ExceptionOr<void> ContainerNode::prepend(Vector<NodeOrString>&& vector)
{
auto result = convertNodesOrStringsIntoNode(WTFMove(vector));
if (result.hasException())
return result.releaseException();
auto node = result.releaseReturnValue();
if (!node)
return { };
return insertBefore(*node, firstChild());
}
HTMLCollection* ContainerNode::cachedHTMLCollection(CollectionType type)
{
return hasRareData() && rareData()->nodeLists() ? rareData()->nodeLists()->cachedCollection<HTMLCollection>(type) : nullptr;
}
}