diff --git a/dom/base/Element.cpp b/dom/base/Element.cpp index a3fb9dc1a0d3..44053886eabb 100644 --- a/dom/base/Element.cpp +++ b/dom/base/Element.cpp @@ -1555,36 +1555,31 @@ Element::GetElementsByClassName(const nsAString& aClassNames) void Element::GetElementsWithGrid(nsTArray>& aElements) { - // This helper function is passed to GetElementsByMatching() - // to identify elements with styling which will cause them to - // generate a nsGridContainerFrame during layout. - auto IsDisplayGrid = [](Element* aElement) -> bool - { - RefPtr computedStyle = - nsComputedDOMStyle::GetComputedStyle(aElement, nullptr); - if (computedStyle) { - const nsStyleDisplay* display = computedStyle->StyleDisplay(); - return (display->mDisplay == StyleDisplay::Grid || - display->mDisplay == StyleDisplay::InlineGrid); - } - return false; - }; + nsINode* cur = this; + while (cur) { + if (cur->IsElement()) { + Element* elem = cur->AsElement(); - GetElementsByMatching(IsDisplayGrid, aElements); -} + if (elem->GetPrimaryFrame()) { + // See if this has a GridContainerFrame. Use the same method that + // nsGridContainerFrame uses, which deals with some edge cases. + if (nsGridContainerFrame::GetGridContainerFrame(elem->GetPrimaryFrame())) { + aElements.AppendElement(elem); + } -void -Element::GetElementsByMatching(nsElementMatchFunc aFunc, - nsTArray>& aElements) -{ - for (nsINode* cur = this; cur; cur = cur->GetNextNode(this)) { - if (cur->IsElement() && aFunc(cur->AsElement())) { - aElements.AppendElement(cur->AsElement()); + // This element has a frame, so allow the traversal to go through + // the children. + cur = cur->GetNextNode(this); + continue; + } } + + // Either this isn't an element, or it has no frame. Continue with the + // traversal but ignore all the children. + cur = cur->GetNextNonChildNode(this); } } - /** * Returns the count of descendants (inclusive of aContent) in * the uncomposed document that are explicitly set as editable. diff --git a/dom/base/Element.h b/dom/base/Element.h index 52630edd5770..1a9175497de0 100644 --- a/dom/base/Element.h +++ b/dom/base/Element.h @@ -1167,24 +1167,12 @@ public: /** * Return an array of all elements in the subtree rooted at this - * element that are styled as grid containers. This includes - * elements that don't actually generate any frames (by virtue of - * being in a 'display:none' subtree), but this does not include + * element that have grid container frames. This does not include * pseudo-elements. */ void GetElementsWithGrid(nsTArray>& aElements); private: - /** - * Define a general matching function that can be passed to - * GetElementsByMatching(). Each Element being considered is - * passed in. - */ - typedef bool (*nsElementMatchFunc)(Element* aElement); - - void GetElementsByMatching(nsElementMatchFunc aFunc, - nsTArray>& aElements); - /** * Implement the algorithm specified at * https://dom.spec.whatwg.org/#insert-adjacent for both