Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp

Issue 2132343002: Make Custom Elements V1 work in HTML imports documents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix tests Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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>;
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) {
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 Document& document = element->document();
301 if (document == m_document) {
302 sorter.add(element);
303 continue;
304 }
dominicc (has gone to gerrit) 2016/07/13 06:12:30 else-if might be clearer than continues.
kojii 2016/07/13 06:47:16 Done.
kojii 2016/07/13 06:47:16 Done.
305
306 // Group elements by document if they are in import documents.
307 if (document.contextDocument() == m_document) {
308 addToImports(imports, element);
309 continue;
310 }
311
312 // The element has moved to other documents, ignore.
264 } 313 }
265 314
266 m_upgradeCandidates->remove(it); 315 m_upgradeCandidates->remove(it);
316 if (!imports.isEmpty())
dominicc (has gone to gerrit) 2016/07/13 06:12:30 I'm not really grokking this change at this point.
kojii 2016/07/13 06:47:16 I'm sorry, I can't understand what "grabble" means
317 collectFromImports(imports, elements);
267 sorter.sorted(elements, m_document.get()); 318 sorter.sorted(elements, m_document.get());
268 } 319 }
269 320
270 } // namespace blink 321 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698