OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/custom/CustomElementsRegistry.h" | 5 #include "core/dom/custom/CustomElementsRegistry.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" | 8 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 CustomElementsRegistry* registry = new CustomElementsRegistry(document); | 68 CustomElementsRegistry* registry = new CustomElementsRegistry(document); |
69 if (V0CustomElementRegistrationContext* v0Context = registry->v0()) | 69 if (V0CustomElementRegistrationContext* v0Context = registry->v0()) |
70 v0Context->setV1(registry); | 70 v0Context->setV1(registry); |
71 return registry; | 71 return registry; |
72 } | 72 } |
73 | 73 |
74 CustomElementsRegistry::CustomElementsRegistry(Document* document) | 74 CustomElementsRegistry::CustomElementsRegistry(Document* document) |
75 : m_document(document) | 75 : m_document(document) |
76 , m_upgradeCandidates(new UpgradeCandidateMap()) | 76 , m_upgradeCandidates(new UpgradeCandidateMap()) |
77 { | 77 { |
78 // !contextDocument() just for unit testing. | |
79 DCHECK(document == document->contextDocument() | |
80 || !document->contextDocument()); | |
78 } | 81 } |
79 | 82 |
80 DEFINE_TRACE(CustomElementsRegistry) | 83 DEFINE_TRACE(CustomElementsRegistry) |
81 { | 84 { |
82 visitor->trace(m_definitions); | 85 visitor->trace(m_definitions); |
83 visitor->trace(m_document); | 86 visitor->trace(m_document); |
84 visitor->trace(m_upgradeCandidates); | 87 visitor->trace(m_upgradeCandidates); |
85 visitor->trace(m_whenDefinedPromiseMap); | 88 visitor->trace(m_whenDefinedPromiseMap); |
86 } | 89 } |
87 | 90 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 } | 210 } |
208 | 211 |
209 CustomElementDefinition* CustomElementsRegistry::definitionForName( | 212 CustomElementDefinition* CustomElementsRegistry::definitionForName( |
210 const AtomicString& name) const | 213 const AtomicString& name) const |
211 { | 214 { |
212 return m_definitions.get(name); | 215 return m_definitions.get(name); |
213 } | 216 } |
214 | 217 |
215 void CustomElementsRegistry::addCandidate(Element* candidate) | 218 void CustomElementsRegistry::addCandidate(Element* candidate) |
216 { | 219 { |
220 // !contextDocument() just for unit testing. | |
221 DCHECK(candidate->document().contextDocument() == m_document | |
222 || !candidate->document().contextDocument()); | |
223 | |
217 const AtomicString& name = candidate->localName(); | 224 const AtomicString& name = candidate->localName(); |
218 if (nameIsDefined(name) || v0NameIsDefined(name)) | 225 if (nameIsDefined(name) || v0NameIsDefined(name)) |
219 return; | 226 return; |
220 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(name); | 227 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(name); |
221 UpgradeCandidateSet* set; | 228 UpgradeCandidateSet* set; |
222 if (it != m_upgradeCandidates->end()) { | 229 if (it != m_upgradeCandidates->end()) { |
223 set = it->value; | 230 set = it->value; |
224 } else { | 231 } else { |
225 set = m_upgradeCandidates->add(name, new UpgradeCandidateSet()) | 232 set = m_upgradeCandidates->add(name, new UpgradeCandidateSet()) |
226 .storedValue | 233 .storedValue |
(...skipping 15 matching lines...) Expand all Loading... | |
242 return ScriptPromise::castUndefined(scriptState); | 249 return ScriptPromise::castUndefined(scriptState); |
243 ScriptPromiseResolver* resolver = m_whenDefinedPromiseMap.get(name); | 250 ScriptPromiseResolver* resolver = m_whenDefinedPromiseMap.get(name); |
244 if (resolver) | 251 if (resolver) |
245 return resolver->promise(); | 252 return resolver->promise(); |
246 ScriptPromiseResolver* newResolver = | 253 ScriptPromiseResolver* newResolver = |
247 ScriptPromiseResolver::create(scriptState); | 254 ScriptPromiseResolver::create(scriptState); |
248 m_whenDefinedPromiseMap.add(name, newResolver); | 255 m_whenDefinedPromiseMap.add(name, newResolver); |
249 return newResolver->promise(); | 256 return newResolver->promise(); |
250 } | 257 } |
251 | 258 |
259 using ElementsInDocument = HeapVector<Member<Element>>; | |
260 using ElementsDocumentMap = HeapHashMap<Document*, ElementsInDocument>; | |
dominicc (has gone to gerrit)
2016/07/19 01:57:39
Maybe DocumentElementsMap, since it's a map *from*
| |
261 | |
262 static void addToImports(ElementsDocumentMap& imports, Element* element) | |
263 { | |
264 Document* document = &element->document(); | |
265 const auto& it = imports.find(document); | |
266 ElementsInDocument* list; | |
267 if (it != imports.end()) { | |
268 list = &it->value; | |
269 } else { | |
270 list = &imports.add(document, ElementsInDocument()) | |
271 .storedValue->value; | |
272 } | |
273 list->append(element); | |
274 } | |
275 | |
276 static void collectFromImports(ElementsDocumentMap& imports, HeapVector<Member<E lement>>* elements) | |
277 { | |
278 for (const auto& it : imports) { | |
dominicc (has gone to gerrit)
2016/07/19 01:57:39
I don't think hashtable iteration order is a good
| |
279 CustomElementUpgradeSorter sorter; | |
280 for (Element* element : it.value) | |
281 sorter.add(element); | |
282 sorter.sorted(elements, it.key); | |
283 } | |
284 } | |
285 | |
252 void CustomElementsRegistry::collectCandidates( | 286 void CustomElementsRegistry::collectCandidates( |
253 const CustomElementDescriptor& desc, | 287 const CustomElementDescriptor& desc, |
254 HeapVector<Member<Element>>* elements) | 288 HeapVector<Member<Element>>* elements) |
255 { | 289 { |
256 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); | 290 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); |
257 if (it == m_upgradeCandidates->end()) | 291 if (it == m_upgradeCandidates->end()) |
258 return; | 292 return; |
293 | |
294 ElementsDocumentMap imports; | |
259 CustomElementUpgradeSorter sorter; | 295 CustomElementUpgradeSorter sorter; |
260 for (Element* element : *it.get()->value) { | 296 for (Element* element : *it.get()->value) { |
261 if (!element || !desc.matches(*element)) | 297 if (!element || !desc.matches(*element)) |
262 continue; | 298 continue; |
263 sorter.add(element); | 299 |
300 // Group elements by document if they are in import documents. | |
301 Document& document = element->document(); | |
302 if (document == m_document) | |
303 sorter.add(element); | |
304 else if (document.contextDocument() == m_document) | |
305 addToImports(imports, element); | |
264 } | 306 } |
265 | 307 |
266 m_upgradeCandidates->remove(it); | 308 m_upgradeCandidates->remove(it); |
309 // If there were elements from import documents, put them first. | |
310 if (!imports.isEmpty()) | |
311 collectFromImports(imports, elements); | |
dominicc (has gone to gerrit)
2016/07/19 01:57:39
TL;DR I think you should handle sorting in the cus
| |
267 sorter.sorted(elements, m_document.get()); | 312 sorter.sorted(elements, m_document.get()); |
268 } | 313 } |
269 | 314 |
270 } // namespace blink | 315 } // namespace blink |
OLD | NEW |