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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 document ? document->registrationContext() : nullptr) | 72 document ? document->registrationContext() : nullptr) |
73 registry->entangle(v0); | 73 registry->entangle(v0); |
74 return registry; | 74 return registry; |
75 } | 75 } |
76 | 76 |
77 CustomElementsRegistry::CustomElementsRegistry(const LocalDOMWindow* owner) | 77 CustomElementsRegistry::CustomElementsRegistry(const LocalDOMWindow* owner) |
78 : m_owner(owner) | 78 : m_owner(owner) |
79 , m_v0 (new V0RegistrySet()) | 79 , m_v0 (new V0RegistrySet()) |
80 , m_upgradeCandidates(new UpgradeCandidateMap()) | 80 , m_upgradeCandidates(new UpgradeCandidateMap()) |
81 { | 81 { |
82 // !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
| |
83 DCHECK(m_owner->document() == m_owner->document()->contextDocument() | |
84 || !m_owner->document()->contextDocument()); | |
82 } | 85 } |
83 | 86 |
84 DEFINE_TRACE(CustomElementsRegistry) | 87 DEFINE_TRACE(CustomElementsRegistry) |
85 { | 88 { |
86 visitor->trace(m_definitions); | 89 visitor->trace(m_definitions); |
87 visitor->trace(m_owner); | 90 visitor->trace(m_owner); |
88 visitor->trace(m_v0); | 91 visitor->trace(m_v0); |
89 visitor->trace(m_upgradeCandidates); | 92 visitor->trace(m_upgradeCandidates); |
90 visitor->trace(m_whenDefinedPromiseMap); | 93 visitor->trace(m_whenDefinedPromiseMap); |
91 } | 94 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 } | 230 } |
228 | 231 |
229 CustomElementDefinition* CustomElementsRegistry::definitionForName( | 232 CustomElementDefinition* CustomElementsRegistry::definitionForName( |
230 const AtomicString& name) const | 233 const AtomicString& name) const |
231 { | 234 { |
232 return m_definitions.get(name); | 235 return m_definitions.get(name); |
233 } | 236 } |
234 | 237 |
235 void CustomElementsRegistry::addCandidate(Element* candidate) | 238 void CustomElementsRegistry::addCandidate(Element* candidate) |
236 { | 239 { |
240 // !contextDocument() just for unit testing. | |
241 DCHECK(candidate->document().contextDocument() == m_owner->document() | |
242 || !candidate->document().contextDocument()); | |
243 | |
237 const AtomicString& name = candidate->localName(); | 244 const AtomicString& name = candidate->localName(); |
238 if (nameIsDefined(name) || v0NameIsDefined(name)) | 245 if (nameIsDefined(name) || v0NameIsDefined(name)) |
239 return; | 246 return; |
240 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(name); | 247 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(name); |
241 UpgradeCandidateSet* set; | 248 UpgradeCandidateSet* set; |
242 if (it != m_upgradeCandidates->end()) { | 249 if (it != m_upgradeCandidates->end()) { |
243 set = it->value; | 250 set = it->value; |
244 } else { | 251 } else { |
245 set = m_upgradeCandidates->add(name, new UpgradeCandidateSet()) | 252 set = m_upgradeCandidates->add(name, new UpgradeCandidateSet()) |
246 .storedValue | 253 .storedValue |
(...skipping 15 matching lines...) Expand all Loading... | |
262 return ScriptPromise::castUndefined(scriptState); | 269 return ScriptPromise::castUndefined(scriptState); |
263 ScriptPromiseResolver* resolver = m_whenDefinedPromiseMap.get(name); | 270 ScriptPromiseResolver* resolver = m_whenDefinedPromiseMap.get(name); |
264 if (resolver) | 271 if (resolver) |
265 return resolver->promise(); | 272 return resolver->promise(); |
266 ScriptPromiseResolver* newResolver = | 273 ScriptPromiseResolver* newResolver = |
267 ScriptPromiseResolver::create(scriptState); | 274 ScriptPromiseResolver::create(scriptState); |
268 m_whenDefinedPromiseMap.add(name, newResolver); | 275 m_whenDefinedPromiseMap.add(name, newResolver); |
269 return newResolver->promise(); | 276 return newResolver->promise(); |
270 } | 277 } |
271 | 278 |
279 using ElementsInDocument = HeapVector<Member<Element>>; | |
280 using ElementsDocumentMap = HeapHashMap<Document*, ElementsInDocument>; | |
281 | |
282 static void addToImports(ElementsDocumentMap& imports, Element* element) | |
283 { | |
284 Document* document = &element->document(); | |
285 const auto& it = imports.find(document); | |
286 ElementsInDocument* list; | |
287 if (it != imports.end()) { | |
288 list = &it->value; | |
289 } else { | |
290 list = &imports.add(document, ElementsInDocument()) | |
291 .storedValue->value; | |
292 } | |
293 list->append(element); | |
294 } | |
295 | |
296 static void collectFromImports(ElementsDocumentMap& imports, HeapVector<Member<E lement>>* elements) | |
297 { | |
298 for (const auto& it : imports) { | |
299 CustomElementUpgradeSorter sorter; | |
300 for (Element* element : it.value) | |
301 sorter.add(element); | |
302 sorter.sorted(elements, it.key); | |
303 } | |
304 } | |
305 | |
272 void CustomElementsRegistry::collectCandidates( | 306 void CustomElementsRegistry::collectCandidates( |
273 const CustomElementDescriptor& desc, | 307 const CustomElementDescriptor& desc, |
274 HeapVector<Member<Element>>* elements) | 308 HeapVector<Member<Element>>* elements) |
275 { | 309 { |
276 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); | 310 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); |
277 if (it == m_upgradeCandidates->end()) | 311 if (it == m_upgradeCandidates->end()) |
278 return; | 312 return; |
313 | |
314 ElementsDocumentMap imports; | |
279 CustomElementUpgradeSorter sorter; | 315 CustomElementUpgradeSorter sorter; |
280 for (Element* element : *it.get()->value) { | 316 for (Element* element : *it.get()->value) { |
281 if (!element || !desc.matches(*element)) | 317 if (!element || !desc.matches(*element)) |
282 continue; | 318 continue; |
283 sorter.add(element); | 319 |
320 // Group elements by document if they are in import documents. | |
321 Document& document = element->document(); | |
322 if (document == m_owner->document()) | |
323 sorter.add(element); | |
324 else if (document.contextDocument() == m_owner->document()) | |
325 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.
| |
284 } | 326 } |
285 | 327 |
286 m_upgradeCandidates->remove(it); | 328 m_upgradeCandidates->remove(it); |
287 | 329 |
288 Document* document = m_owner->document(); | 330 // 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
| |
289 if (!document) | 331 // if (!document) |
290 return; | 332 // return; |
291 | 333 |
292 sorter.sorted(elements, document); | 334 // If there were elements from import documents, put them first. |
335 if (!imports.isEmpty()) | |
336 collectFromImports(imports, elements); | |
337 | |
338 sorter.sorted(elements, m_owner->document()); | |
293 } | 339 } |
294 | 340 |
295 } // namespace blink | 341 } // namespace blink |
OLD | NEW |