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 DCHECK_EQ(document, document->contextDocument()); |
78 } | 79 } |
79 | 80 |
80 DEFINE_TRACE(CustomElementsRegistry) | 81 DEFINE_TRACE(CustomElementsRegistry) |
81 { | 82 { |
82 visitor->trace(m_definitions); | 83 visitor->trace(m_definitions); |
83 visitor->trace(m_document); | 84 visitor->trace(m_document); |
84 visitor->trace(m_upgradeCandidates); | 85 visitor->trace(m_upgradeCandidates); |
85 visitor->trace(m_whenDefinedPromiseMap); | 86 visitor->trace(m_whenDefinedPromiseMap); |
86 } | 87 } |
87 | 88 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 208 } |
208 | 209 |
209 CustomElementDefinition* CustomElementsRegistry::definitionForName( | 210 CustomElementDefinition* CustomElementsRegistry::definitionForName( |
210 const AtomicString& name) const | 211 const AtomicString& name) const |
211 { | 212 { |
212 return m_definitions.get(name); | 213 return m_definitions.get(name); |
213 } | 214 } |
214 | 215 |
215 void CustomElementsRegistry::addCandidate(Element* candidate) | 216 void CustomElementsRegistry::addCandidate(Element* candidate) |
216 { | 217 { |
| 218 DCHECK_EQ(candidate->document().contextDocument(), m_document); |
| 219 |
217 const AtomicString& name = candidate->localName(); | 220 const AtomicString& name = candidate->localName(); |
218 if (nameIsDefined(name) || v0NameIsDefined(name)) | 221 if (nameIsDefined(name) || v0NameIsDefined(name)) |
219 return; | 222 return; |
220 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(name); | 223 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(name); |
221 UpgradeCandidateSet* set; | 224 UpgradeCandidateSet* set; |
222 if (it != m_upgradeCandidates->end()) { | 225 if (it != m_upgradeCandidates->end()) { |
223 set = it->value; | 226 set = it->value; |
224 } else { | 227 } else { |
225 set = m_upgradeCandidates->add(name, new UpgradeCandidateSet()) | 228 set = m_upgradeCandidates->add(name, new UpgradeCandidateSet()) |
226 .storedValue | 229 .storedValue |
(...skipping 15 matching lines...) Expand all Loading... |
242 return ScriptPromise::castUndefined(scriptState); | 245 return ScriptPromise::castUndefined(scriptState); |
243 ScriptPromiseResolver* resolver = m_whenDefinedPromiseMap.get(name); | 246 ScriptPromiseResolver* resolver = m_whenDefinedPromiseMap.get(name); |
244 if (resolver) | 247 if (resolver) |
245 return resolver->promise(); | 248 return resolver->promise(); |
246 ScriptPromiseResolver* newResolver = | 249 ScriptPromiseResolver* newResolver = |
247 ScriptPromiseResolver::create(scriptState); | 250 ScriptPromiseResolver::create(scriptState); |
248 m_whenDefinedPromiseMap.add(name, newResolver); | 251 m_whenDefinedPromiseMap.add(name, newResolver); |
249 return newResolver->promise(); | 252 return newResolver->promise(); |
250 } | 253 } |
251 | 254 |
| 255 using ElementsInDocument = HeapVector<Member<Element>>; |
| 256 using ElementsDocumentMap = HeapHashMap<Document*, ElementsInDocument>; |
| 257 |
| 258 static void addToImports(ElementsDocumentMap& imports, Element* element) |
| 259 { |
| 260 Document* document = &element->document(); |
| 261 const auto& it = imports.find(document); |
| 262 ElementsInDocument* list; |
| 263 if (it != imports.end()) { |
| 264 list = &it->value; |
| 265 } else { |
| 266 list = &imports.add(document, ElementsInDocument()) |
| 267 .storedValue->value; |
| 268 } |
| 269 list->append(element); |
| 270 } |
| 271 |
| 272 static void collectFromImports(ElementsDocumentMap& imports, HeapVector<Member<E
lement>>* elements) |
| 273 { |
| 274 for (const auto& it : imports) { |
| 275 CustomElementUpgradeSorter sorter; |
| 276 for (Element* element : it.value) |
| 277 sorter.add(element); |
| 278 sorter.sorted(elements, it.key); |
| 279 } |
| 280 } |
| 281 |
252 void CustomElementsRegistry::collectCandidates( | 282 void CustomElementsRegistry::collectCandidates( |
253 const CustomElementDescriptor& desc, | 283 const CustomElementDescriptor& desc, |
254 HeapVector<Member<Element>>* elements) | 284 HeapVector<Member<Element>>* elements) |
255 { | 285 { |
256 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); | 286 UpgradeCandidateMap::iterator it = m_upgradeCandidates->find(desc.name()); |
257 if (it == m_upgradeCandidates->end()) | 287 if (it == m_upgradeCandidates->end()) |
258 return; | 288 return; |
| 289 |
| 290 ElementsDocumentMap imports; |
259 CustomElementUpgradeSorter sorter; | 291 CustomElementUpgradeSorter sorter; |
260 for (Element* element : *it.get()->value) { | 292 for (Element* element : *it.get()->value) { |
261 if (!element || !desc.matches(*element)) | 293 if (!element || !desc.matches(*element)) |
262 continue; | 294 continue; |
263 sorter.add(element); | 295 |
| 296 Document& document = element->document(); |
| 297 if (document == m_document) { |
| 298 sorter.add(element); |
| 299 continue; |
| 300 } |
| 301 |
| 302 // Group elements by document if they are in import documents. |
| 303 if (document.contextDocument() == m_document) { |
| 304 addToImports(imports, element); |
| 305 continue; |
| 306 } |
| 307 |
| 308 // The element has moved to other documents, ignore. |
264 } | 309 } |
265 | 310 |
266 m_upgradeCandidates->remove(it); | 311 m_upgradeCandidates->remove(it); |
| 312 if (!imports.isEmpty()) |
| 313 collectFromImports(imports, elements); |
267 sorter.sorted(elements, m_document.get()); | 314 sorter.sorted(elements, m_document.get()); |
268 } | 315 } |
269 | 316 |
270 } // namespace blink | 317 } // namespace blink |
OLD | NEW |