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/CustomElementUpgradeSorter.h" | 5 #include "core/dom/custom/CustomElementUpgradeSorter.h" |
6 | 6 |
7 #include "core/dom/Element.h" | 7 #include "core/dom/Element.h" |
8 #include "core/dom/ElementTraversal.h" | 8 #include "core/dom/ElementTraversal.h" |
9 #include "core/dom/Node.h" | 9 #include "core/dom/Node.h" |
10 #include "core/dom/shadow/ShadowRoot.h" | 10 #include "core/dom/shadow/ShadowRoot.h" |
11 #include "core/html/HTMLLinkElement.h" | |
12 #include "core/html/imports/HTMLImportChild.h" | |
13 #include "core/html/imports/HTMLImportLoader.h" | |
11 | 14 |
12 namespace blink { | 15 namespace blink { |
13 | 16 |
14 CustomElementUpgradeSorter::CustomElementUpgradeSorter() | 17 CustomElementUpgradeSorter::CustomElementUpgradeSorter() |
15 : m_elements(new HeapHashSet<Member<Element>>()) | 18 : m_elements(new HeapHashSet<Member<Element>>()) |
16 , m_parentChildMap(new ParentChildMap()) | 19 , m_parentChildMap(new ParentChildMap()) |
17 { | 20 { |
18 } | 21 } |
19 | 22 |
23 static HTMLLinkElement* getLinkElement(Document* document) | |
24 { | |
25 DCHECK(document); | |
26 HTMLImportLoader* loader = document->importLoader(); | |
27 if (!loader) | |
28 return nullptr; | |
29 | |
30 return loader->firstImport()->link(); | |
31 } | |
32 | |
33 CustomElementUpgradeSorter::AddResult CustomElementUpgradeSorter::addToParentChi ldMap( | |
34 Node* parent, | |
35 Node* child) | |
36 { | |
37 ParentChildMap::iterator it = m_parentChildMap->find(parent); | |
38 if (it != m_parentChildMap->end()) { | |
39 it->value.add(child); | |
40 // The entry for the parent exists; so must its parents. | |
41 return kParentAlreadyExistsInMap; | |
42 } | |
43 ParentChildMap::AddResult result = | |
44 m_parentChildMap->add(parent, HeapHashSet<Member<Node>>()); | |
45 result.storedValue->value.add(child); | |
46 return kParentAddedToMap; | |
47 } | |
48 | |
20 void CustomElementUpgradeSorter::add(Element* element) | 49 void CustomElementUpgradeSorter::add(Element* element) |
21 { | 50 { |
22 m_elements->add(element); | 51 m_elements->add(element); |
23 | 52 |
24 for (Node* n = element, *parent = n->parentOrShadowHostNode(); | 53 // Note: handling of |parent| here is tricky; At the first iteration |
25 parent; | 54 // |parent| can never be nullptr because the input is |element| and this |
dominicc (has gone to gerrit)
2016/08/22 12:05:58
Actually this isn't true; the parent can be nullpt
kochi
2016/08/24 11:26:03
Refactored this code and I think the loop look bet
| |
26 n = parent, parent = parent->parentOrShadowHostNode()) { | 55 // loop traverses up to document. Therefore the <link> check in the middle |
56 // of the loop will be run whenever a document is found. | |
57 for (Node* n = element, *parent = n->parentOrShadowHostNode(); parent;) { | |
58 if (addToParentChildMap(parent, n) == kParentAlreadyExistsInMap) | |
59 break; | |
27 | 60 |
28 ParentChildMap::iterator it = m_parentChildMap->find(parent); | 61 n = parent; |
29 if (it == m_parentChildMap->end()) { | 62 parent = parent->parentOrShadowHostNode(); |
30 ParentChildMap::AddResult result = | 63 |
31 m_parentChildMap->add(parent, HeapHashSet<Member<Node>>()); | 64 // Check if this is an imported document, and if it is, register its |
32 result.storedValue->value.add(n); | 65 // corresponding <link rel="import"> element as its parent. |
33 } else { | 66 if (!parent && n->isDocumentNode()) { |
34 it->value.add(n); | 67 Document* d = toDocument(n); |
35 // The entry for the parent exists; so must its parents. | 68 Element* link = getLinkElement(d); |
36 break; | 69 if (!link || addToParentChildMap(link, n) == kParentAlreadyExistsInM ap) |
70 break; | |
71 | |
72 n = link; | |
73 parent = link->parentOrShadowHostNode(); | |
37 } | 74 } |
38 } | 75 } |
39 } | 76 } |
40 | 77 |
41 void CustomElementUpgradeSorter::visit( | 78 void CustomElementUpgradeSorter::visit( |
42 HeapVector<Member<Element>>* result, | 79 HeapVector<Member<Element>>* result, |
43 ChildSet& children, | 80 ChildSet& children, |
44 const ChildSet::iterator& it) | 81 const ChildSet::iterator& it) |
45 { | 82 { |
46 if (it == children.end()) | 83 if (it == children.end()) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 visit(result, children, children.find(e)); | 118 visit(result, children, children.find(e)); |
82 } | 119 } |
83 | 120 |
84 if (children.size() == 1) | 121 if (children.size() == 1) |
85 visit(result, children, children.begin()); | 122 visit(result, children, children.begin()); |
86 | 123 |
87 DCHECK(children.isEmpty()); | 124 DCHECK(children.isEmpty()); |
88 } | 125 } |
89 | 126 |
90 } // namespace blink | 127 } // namespace blink |
OLD | NEW |