Bug 1464433 - Cleaning up nsINode::GetElementById. r=emilio
authorrahul0379 <rahul0379@gmail.com>
Sat, 26 May 2018 20:15:11 +0530
changeset 476787 2263ea88eb0622434836392f3a922b172a66b2d9
parent 476786 2e7a4d376aff00ae43f8051aa53af8be65a112a3
child 476788 ea82a59465a958b93f2cd61cb6b59415a990a46a
push id1757
push userffxbld-merge
push dateFri, 24 Aug 2018 17:02:43 +0000
treeherdermozilla-release@736023aebdb1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
bugs1464433
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1464433 - Cleaning up nsINode::GetElementById. r=emilio
dom/base/nsINode.cpp
--- a/dom/base/nsINode.cpp
+++ b/dom/base/nsINode.cpp
@@ -2431,74 +2431,51 @@ nsINode::ParseSelectorList(const nsAStri
   return ret;
 }
 
 namespace {
 struct SelectorMatchInfo {
 };
 } // namespace
 
-// Given an id, find elements with that id under aRoot that match aMatchInfo if
-// any is provided.  If no SelectorMatchInfo is provided, just find the ones
-// with the given id.  aRoot must be in the document.
-template<bool onlyFirstMatch, class T>
-inline static void
-FindMatchingElementsWithId(const nsAString& aId, nsINode* aRoot,
-                           SelectorMatchInfo* aMatchInfo,
-                           T& aList)
+// Given an id, find first element with that id under aRoot.
+// If none found, return nullptr. aRoot must be in the document.
+inline static Element*
+FindMatchingElementWithId(const nsAString& aId, nsINode* aRoot)
 {
   MOZ_ASSERT(aRoot->IsInUncomposedDoc(),
              "Don't call me if the root is not in the document");
   // FIXME(emilio): It'd be nice to optimize this for shadow roots too.
   MOZ_ASSERT(aRoot->IsElement() || aRoot->IsDocument(),
              "The optimization below to check ContentIsDescendantOf only for "
              "elements depends on aRoot being either an element or a "
              "document if it's in the document.  Note that document fragments "
              "can't be IsInUncomposedDoc(), so should never show up here.");
 
   const nsTArray<Element*>* elements = aRoot->OwnerDoc()->GetAllElementsForId(aId);
   if (!elements) {
     // Nothing to do; we're done
-    return;
+    return nullptr;
   }
 
   // XXXbz: Should we fall back to the tree walk if aRoot is not the
   // document and |elements| is long, for some value of "long"?
   for (size_t i = 0; i < elements->Length(); ++i) {
     Element* element = (*elements)[i];
     if (!aRoot->IsElement() ||
         (element != aRoot &&
            nsContentUtils::ContentIsDescendantOf(element, aRoot))) {
       // We have an element with the right id and it's a strict descendant
-      // of aRoot.  Make sure it really matches the selector.
-      if (!aMatchInfo
-      ) {
-        aList.AppendElement(element);
-        if (onlyFirstMatch) {
-          return;
-        }
-      }
+      // of aRoot.
+      return element;
     }
   }
+  return nullptr;
 }
 
-
-struct ElementHolder {
-  ElementHolder() : mElement(nullptr) {}
-  void AppendElement(Element* aElement) {
-    MOZ_ASSERT(!mElement, "Should only get one element");
-    mElement = aElement;
-  }
-  void SetCapacity(uint32_t aCapacity) { MOZ_CRASH("Don't call me!"); }
-  uint32_t Length() { return 0; }
-  Element* ElementAt(uint32_t aIndex) { return nullptr; }
-
-  Element* mElement;
-};
-
 Element*
 nsINode::QuerySelector(const nsAString& aSelector, ErrorResult& aResult)
 {
   const RawServoSelectorList* list = ParseSelectorList(aSelector, aResult);
   if (!list) {
     return nullptr;
   }
   const bool useInvalidation = false;
@@ -2521,19 +2498,17 @@ nsINode::QuerySelectorAll(const nsAStrin
 }
 
 Element*
 nsINode::GetElementById(const nsAString& aId)
 {
   MOZ_ASSERT(IsElement() || IsDocumentFragment(),
              "Bogus this object for GetElementById call");
   if (IsInUncomposedDoc()) {
-    ElementHolder holder;
-    FindMatchingElementsWithId<true>(aId, this, nullptr, holder);
-    return holder.mElement;
+    return FindMatchingElementWithId(aId, this);
   }
 
   for (nsIContent* kid = GetFirstChild(); kid; kid = kid->GetNextNode(this)) {
     if (!kid->IsElement()) {
       continue;
     }
     nsAtom* id = kid->AsElement()->GetID();
     if (id && id->Equals(aId)) {