void TreeScope::setParentTreeScope(TreeScope& newParentScope) { // A document node cannot be re-parented. ASSERT(!m_rootNode.isDocumentNode()); m_parentTreeScope = &newParentScope; setDocumentScope(newParentScope.documentScope()); }
RelatedNodeRetargeter(Node& relatedNode, TreeScope& targetTreeScope) : m_relatedNode(relatedNode) , m_retargetedRelatedNode(&relatedNode) { TreeScope* currentTreeScope = &m_relatedNode.treeScope(); if (LIKELY(currentTreeScope == &targetTreeScope)) return; if (¤tTreeScope->documentScope() != &targetTreeScope.documentScope()) { m_hasDifferentTreeRoot = true; m_retargetedRelatedNode = nullptr; return; } if (relatedNode.inDocument() != targetTreeScope.rootNode().inDocument()) { m_hasDifferentTreeRoot = true; while (m_retargetedRelatedNode->isInShadowTree()) m_retargetedRelatedNode = downcast<ShadowRoot>(m_retargetedRelatedNode->treeScope().rootNode()).host(); return; } collectTreeScopes(); // FIXME: We should collect this while constructing the event path. Vector<TreeScope*, 8> targetTreeScopeAncestors; for (TreeScope* currentTreeScope = &targetTreeScope; currentTreeScope; currentTreeScope = currentTreeScope->parentTreeScope()) targetTreeScopeAncestors.append(currentTreeScope); ASSERT_WITH_SECURITY_IMPLICATION(!targetTreeScopeAncestors.isEmpty()); unsigned i = m_ancestorTreeScopes.size(); unsigned j = targetTreeScopeAncestors.size(); ASSERT_WITH_SECURITY_IMPLICATION(m_ancestorTreeScopes.last() == targetTreeScopeAncestors.last()); while (m_ancestorTreeScopes[i - 1] == targetTreeScopeAncestors[j - 1]) { i--; j--; if (!i || !j) break; } m_lowestCommonAncestorIndex = i; m_retargetedRelatedNode = nodeInLowestCommonAncestor(); }
RelatedNodeRetargeter::RelatedNodeRetargeter(Node& relatedNode, Node& target) : m_relatedNode(relatedNode) , m_retargetedRelatedNode(&relatedNode) { auto& targetTreeScope = target.treeScope(); TreeScope* currentTreeScope = &m_relatedNode.treeScope(); if (LIKELY(currentTreeScope == &targetTreeScope && target.inDocument() && m_relatedNode.inDocument())) return; if (¤tTreeScope->documentScope() != &targetTreeScope.documentScope()) { m_hasDifferentTreeRoot = true; m_retargetedRelatedNode = nullptr; return; } if (relatedNode.inDocument() != target.inDocument()) { m_hasDifferentTreeRoot = true; m_retargetedRelatedNode = moveOutOfAllShadowRoots(relatedNode); return; } collectTreeScopes(); // FIXME: We should collect this while constructing the event path. Vector<TreeScope*, 8> targetTreeScopeAncestors; for (TreeScope* currentTreeScope = &targetTreeScope; currentTreeScope; currentTreeScope = currentTreeScope->parentTreeScope()) targetTreeScopeAncestors.append(currentTreeScope); ASSERT_WITH_SECURITY_IMPLICATION(!targetTreeScopeAncestors.isEmpty()); unsigned i = m_ancestorTreeScopes.size(); unsigned j = targetTreeScopeAncestors.size(); ASSERT_WITH_SECURITY_IMPLICATION(m_ancestorTreeScopes.last() == targetTreeScopeAncestors.last()); while (m_ancestorTreeScopes[i - 1] == targetTreeScopeAncestors[j - 1]) { i--; j--; if (!i || !j) break; } bool lowestCommonAncestorIsDocumentScope = i + 1 == m_ancestorTreeScopes.size(); if (lowestCommonAncestorIsDocumentScope && !relatedNode.inDocument() && !target.inDocument()) { Node& targetAncestorInDocumentScope = i ? *downcast<ShadowRoot>(m_ancestorTreeScopes[i - 1]->rootNode()).shadowHost() : target; Node& relatedNodeAncestorInDocumentScope = j ? *downcast<ShadowRoot>(targetTreeScopeAncestors[j - 1]->rootNode()).shadowHost() : relatedNode; if (targetAncestorInDocumentScope.rootNode() != relatedNodeAncestorInDocumentScope.rootNode()) { m_hasDifferentTreeRoot = true; m_retargetedRelatedNode = moveOutOfAllShadowRoots(relatedNode); return; } } m_lowestCommonAncestorIndex = i; m_retargetedRelatedNode = nodeInLowestCommonAncestor(); }
void DocumentOrderedMap::add(const AtomicStringImpl& key, Element& element, const TreeScope& treeScope) { ASSERT_WITH_SECURITY_IMPLICATION(element.isInTreeScope()); ASSERT_WITH_SECURITY_IMPLICATION(treeScope.rootNode()->containsIncludingShadowDOM(&element)); if (!element.isInTreeScope() || &element.document() != treeScope.documentScope()) return; Map::AddResult addResult = m_map.add(&key, MapEntry(&element)); if (addResult.isNewEntry) return; MapEntry& entry = addResult.iterator->value; ASSERT_WITH_SECURITY_IMPLICATION(entry.count); entry.element = 0; entry.count++; entry.orderedList.clear(); }
Element* TreeScope::focusedElement() { Document& document = m_rootNode.document(); Element* element = document.focusedElement(); if (!element && document.page()) element = focusedFrameOwnerElement(document.page()->focusController().focusedFrame(), document.frame()); if (!element) return nullptr; TreeScope* treeScope = &element->treeScope(); RELEASE_ASSERT(&document == &treeScope->documentScope()); while (treeScope != this && treeScope != &document) { auto& rootNode = treeScope->rootNode(); if (is<ShadowRoot>(rootNode)) element = downcast<ShadowRoot>(rootNode).host(); else return nullptr; treeScope = &element->treeScope(); } if (this != treeScope) return nullptr; return element; }