Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(778)

Unified Diff: Source/core/dom/ContainerNode.cpp

Issue 251633002: Add support for DocumentFragment.getElementById() (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Use isInTreeScope() instead of isInDocument() Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/dom/ContainerNode.cpp
diff --git a/Source/core/dom/ContainerNode.cpp b/Source/core/dom/ContainerNode.cpp
index f355f618b05a5d2abd7d3807fca55973c52daf0d..f4be545c83d489d59034f896d7663954a12dc620 100644
--- a/Source/core/dom/ContainerNode.cpp
+++ b/Source/core/dom/ContainerNode.cpp
@@ -1162,6 +1162,26 @@ PassRefPtr<RadioNodeList> ContainerNode::radioNodeList(const AtomicString& name,
return ensureRareData().ensureNodeLists().addCache<RadioNodeList>(*this, type, name);
}
+Element* ContainerNode::getElementById(const AtomicString& id) const
+{
+ if (isInTreeScope()) {
+ // Fast path if we are in a tree scope: call getElementById() on tree scope
+ // and check if the matching element is in our subtree.
+ Element* element = treeScope().getElementById(id);
+ if (!element)
+ return 0;
+ if (element->isDescendantOf(this))
arv (Not doing code reviews) 2014/04/25 15:28:59 Can this ever be false?
Inactive 2014/04/25 15:31:30 Yes, just because there is an element in the Docum
arv (Not doing code reviews) 2014/04/25 15:37:42 I see. The SVGSVGElement makes this != tree scope.
+ return element;
+ }
+
+ // Fall back to traversing our subtree. In case of duplicate ids, the first element found will be returned.
+ for (Element* element = ElementTraversal::firstWithin(*this); element; element = ElementTraversal::next(*element, this)) {
+ if (element->getIdAttribute() == id)
+ return element;
+ }
+ return 0;
+}
+
#ifndef NDEBUG
bool childAttachedAllowedWhenAttachingChildren(ContainerNode* node)
{

Powered by Google App Engine
This is Rietveld 408576698