Index: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp |
diff --git a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp |
index 1bd3aad2ffc571283db758477872f8e23179e72b..21349ab2b4fb8837a6eb5ba778d0c3c7b806f067 100644 |
--- a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp |
+++ b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp |
@@ -75,6 +75,7 @@ CustomElementsRegistry::CustomElementsRegistry(Document* document) |
: m_document(document) |
, m_upgradeCandidates(new UpgradeCandidateMap()) |
{ |
+ DCHECK_EQ(document, document->contextDocument()); |
} |
DEFINE_TRACE(CustomElementsRegistry) |
@@ -214,6 +215,8 @@ CustomElementDefinition* CustomElementsRegistry::definitionForName( |
void CustomElementsRegistry::addCandidate(Element* candidate) |
{ |
+ DCHECK_EQ(candidate->document().contextDocument(), m_document); |
+ |
const AtomicString& name = candidate->localName(); |
if (nameIsDefined(name) || v0NameIsDefined(name)) |
return; |
@@ -249,6 +252,33 @@ ScriptPromise CustomElementsRegistry::whenDefined( |
return newResolver->promise(); |
} |
+using ElementsInDocument = HeapVector<Member<Element>>; |
+using ElementsDocumentMap = HeapHashMap<Document*, ElementsInDocument>; |
+ |
+static void addToImports(ElementsDocumentMap& imports, Element* element) |
+{ |
+ Document* document = &element->document(); |
+ const auto& it = imports.find(document); |
+ ElementsInDocument* list; |
+ if (it != imports.end()) { |
+ list = &it->value; |
+ } else { |
+ list = &imports.add(document, ElementsInDocument()) |
+ .storedValue->value; |
+ } |
+ list->append(element); |
+} |
+ |
+static void collectFromImports(ElementsDocumentMap& imports, HeapVector<Member<Element>>* elements) |
+{ |
+ for (const auto& it : imports) { |
+ CustomElementUpgradeSorter sorter; |
+ for (Element* element : it.value) |
+ sorter.add(element); |
+ sorter.sorted(elements, it.key); |
+ } |
+} |
+ |
void CustomElementsRegistry::collectCandidates( |
const CustomElementDescriptor& desc, |
HeapVector<Member<Element>>* elements) |
@@ -256,14 +286,31 @@ void CustomElementsRegistry::collectCandidates( |
UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); |
if (it == m_upgradeCandidates->end()) |
return; |
+ |
+ ElementsDocumentMap imports; |
CustomElementUpgradeSorter sorter; |
for (Element* element : *it.get()->value) { |
if (!element || !desc.matches(*element)) |
continue; |
- sorter.add(element); |
+ |
+ Document& document = element->document(); |
+ if (document == m_document) { |
+ sorter.add(element); |
+ continue; |
+ } |
+ |
+ // Group elements by document if they are in import documents. |
+ if (document.contextDocument() == m_document) { |
+ addToImports(imports, element); |
+ continue; |
+ } |
+ |
+ // The element has moved to other documents, ignore. |
} |
m_upgradeCandidates->remove(it); |
+ if (!imports.isEmpty()) |
+ collectFromImports(imports, elements); |
sorter.sorted(elements, m_document.get()); |
} |