TEST_F(CustomElementUpgradeSorterTest, sorter_shadow) { // A* // + {ShadowRoot} // | + B // | + C* // + D* Element* a = createElementWithId("a-a", "a"); Element* b = createElementWithId("a-a", "b"); Element* c = createElementWithId("a-a", "c"); Element* d = createElementWithId("a-a", "d"); document()->documentElement()->appendChild(a); ShadowRoot* s = attachShadowTo(a); a->appendChild(d); s->appendChild(b); b->appendChild(c); CustomElementUpgradeSorter sort; sort.add(a); sort.add(c); sort.add(d); HeapVector<Member<Element>> elements; sort.sorted(&elements, document()); EXPECT_EQ(3u, elements.size()); EXPECT_EQ(a, elements[0].get()); EXPECT_EQ(c, elements[1].get()); EXPECT_EQ(d, elements[2].get()); }
TEST_F(CustomElementUpgradeSorterTest, oneCandidate) { NonThrowableExceptionState noExceptions; Element* element = document()->createElement("a-a", StringOrDictionary(), noExceptions); document()->documentElement()->appendChild(element); CustomElementUpgradeSorter sorter; sorter.add(element); HeapVector<Member<Element>> elements; sorter.sorted(&elements, document()); EXPECT_EQ(1u, elements.size()) << "exactly one candidate should be in the result set"; EXPECT_TRUE(elements.contains(element)) << "the candidate should be the element that was added"; }
TEST_F(CustomElementUpgradeSorterTest, inOtherDocument_notInSet) { NonThrowableExceptionState noExceptions; Element* element = document()->createElement("a-a", StringOrDictionary(), noExceptions); Document* otherDocument = HTMLDocument::create(); otherDocument->appendChild(element); EXPECT_EQ(otherDocument, element->ownerDocument()) << "sanity: another document should have adopted an element on append"; CustomElementUpgradeSorter sorter; sorter.add(element); HeapVector<Member<Element>> elements; sorter.sorted(&elements, document()); EXPECT_EQ(0u, elements.size()) << "the adopted-away candidate should not have been included"; }
void CustomElementRegistry::collectCandidates( const CustomElementDescriptor& desc, HeapVector<Member<Element>>* elements) { UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); if (it == m_upgradeCandidates->end()) return; CustomElementUpgradeSorter sorter; for (Element* element : *it.get()->value) { if (!element || !desc.matches(*element)) continue; sorter.add(element); } m_upgradeCandidates->remove(it); Document* document = m_owner->document(); if (!document) return; sorter.sorted(elements, document); }
TEST_F(CustomElementUpgradeSorterTest, candidatesInDocumentOrder) { Element* a = createElementWithId("a-a", "a"); Element* b = createElementWithId("a-a", "b"); Element* c = createElementWithId("a-a", "c"); document()->documentElement()->appendChild(a); a->appendChild(b); document()->documentElement()->appendChild(c); CustomElementUpgradeSorter sorter; sorter.add(b); sorter.add(a); sorter.add(c); HeapVector<Member<Element>> elements; sorter.sorted(&elements, document()); EXPECT_EQ(3u, elements.size()); EXPECT_EQ(a, elements[0].get()); EXPECT_EQ(b, elements[1].get()); EXPECT_EQ(c, elements[2].get()); }
TEST_F(CustomElementUpgradeSorterTest, sorter_shallowDeep) { // A* // B // + C* Element* a = createElementWithId("a-a", "a"); Element* b = createElementWithId("a-a", "b"); Element* c = createElementWithId("a-a", "c"); document()->documentElement()->appendChild(a); document()->documentElement()->appendChild(b); b->appendChild(c); CustomElementUpgradeSorter sort; sort.add(a); sort.add(c); HeapVector<Member<Element>> elements; sort.sorted(&elements, document()); EXPECT_EQ(2u, elements.size()); EXPECT_EQ(a, elements[0].get()); EXPECT_EQ(c, elements[1].get()); }