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

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: Add comment 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
« no previous file with comments | « Source/core/dom/ContainerNode.h ('k') | Source/core/dom/Document.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/dom/ContainerNode.cpp
diff --git a/Source/core/dom/ContainerNode.cpp b/Source/core/dom/ContainerNode.cpp
index f355f618b05a5d2abd7d3807fca55973c52daf0d..07f4210845da661c3964387fb2ddcc35da445466 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 (inDocument()) {
arv (Not doing code reviews) 2014/04/25 13:36:19 I don't think this is required. It should be suffi
Inactive 2014/04/25 14:41:20 Yes, this is not strictly required. I thought it w
Inactive 2014/04/25 15:13:40 I think I should be using isInTreeScope() instead
+ // Fast path if we are in a document: call getElementById() on document
+ // and check if the matching element is in our subtree.
+ Element* element = treeScope().getElementById(id);
+ if (!element)
Inactive 2014/04/25 14:41:20 i.e. Early return here is now possible as we know
arv (Not doing code reviews) 2014/04/25 15:04:04 I was thinking of the case where we are in a disco
+ return 0;
+ if (element->isDescendantOf(this))
+ 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)
{
« no previous file with comments | « Source/core/dom/ContainerNode.h ('k') | Source/core/dom/Document.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698