Chromium Code Reviews| 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 6ecc7a8ea89b49ab40328c7981fe22f933eed729..9e30d75ba957311f47aadf13154e69ec4aadab95 100644 |
| --- a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp |
| +++ b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp |
| @@ -79,6 +79,9 @@ CustomElementsRegistry::CustomElementsRegistry(const LocalDOMWindow* owner) |
| , m_v0 (new V0RegistrySet()) |
| , m_upgradeCandidates(new UpgradeCandidateMap()) |
| { |
| + // !contextDocument() just for unit testing. |
|
dominicc (has gone to gerrit)
2016/08/19 04:54:11
Can you explain what you're thinking here? Which u
kochi
2016/08/19 10:51:48
Will update the comment.
kochi
2016/08/22 07:37:34
Looks the comment is obsolete, and the latter cond
|
| + DCHECK(m_owner->document() == m_owner->document()->contextDocument() |
| + || !m_owner->document()->contextDocument()); |
| } |
| DEFINE_TRACE(CustomElementsRegistry) |
| @@ -234,6 +237,10 @@ CustomElementDefinition* CustomElementsRegistry::definitionForName( |
| void CustomElementsRegistry::addCandidate(Element* candidate) |
| { |
| + // !contextDocument() just for unit testing. |
| + DCHECK(candidate->document().contextDocument() == m_owner->document() |
| + || !candidate->document().contextDocument()); |
| + |
| const AtomicString& name = candidate->localName(); |
| if (nameIsDefined(name) || v0NameIsDefined(name)) |
| return; |
| @@ -269,6 +276,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) |
| @@ -276,20 +310,32 @@ 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); |
| + |
| + // Group elements by document if they are in import documents. |
| + Document& document = element->document(); |
| + if (document == m_owner->document()) |
| + sorter.add(element); |
| + else if (document.contextDocument() == m_owner->document()) |
| + addToImports(imports, element); |
|
dominicc (has gone to gerrit)
2016/08/19 04:54:11
Why not just put them all in the sorter? Sorter ca
kochi
2016/08/22 07:37:34
Done.
|
| } |
| m_upgradeCandidates->remove(it); |
| - Document* document = m_owner->document(); |
| - if (!document) |
| - return; |
| + // Document* document = m_owner->document(); |
|
dominicc (has gone to gerrit)
2016/08/19 04:54:11
Let's not commit commented-out code like this.
kochi
2016/08/19 10:51:48
Done. This was a tentative commentout and uninten
|
| + // if (!document) |
| + // return; |
| + |
| + // If there were elements from import documents, put them first. |
| + if (!imports.isEmpty()) |
| + collectFromImports(imports, elements); |
| - sorter.sorted(elements, document); |
| + sorter.sorted(elements, m_owner->document()); |
| } |
| } // namespace blink |