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

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

Issue 1995933005: CustomElementUpgradeSorter puts elements in shadow-including tree order. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "core/dom/custom/CustomElementUpgradeSorter.h"
6
7 #include "core/dom/Element.h"
8 #include "core/dom/ElementTraversal.h"
9 #include "core/dom/Node.h"
10 #include "core/dom/shadow/ShadowRoot.h"
11
12 namespace blink {
13
14 CustomElementUpgradeSorter::CustomElementUpgradeSorter()
15 : m_elements(new HeapHashSet<Member<Element>>())
16 , m_parentChildMap(new ParentChildMap())
17 {
18 }
19
20 void CustomElementUpgradeSorter::add(Element* element)
21 {
22 m_elements->add(element);
23
24 for (Node* n = element, *parent = n->parentOrShadowHostNode();
25 parent;
26 n = parent, parent = parent->parentOrShadowHostNode()) {
27
28 ParentChildMap::iterator it = m_parentChildMap->find(parent);
29 if (it == m_parentChildMap->end()) {
30 ParentChildMap::AddResult result =
31 m_parentChildMap->add(parent, HeapHashSet<Member<Node>>());
32 result.storedValue->value.add(n);
33 } else {
34 it->value.add(n);
35 // The entry for the parent exists; so must its parents.
36 break;
37 }
38 }
39 }
40
41 void CustomElementUpgradeSorter::visit(
42 HeapVector<Member<Element>>* result,
43 ChildSet& children,
44 const ChildSet::iterator& it)
45 {
46 if (it == children.end())
47 return;
48 if (it->get()->isElementNode() && m_elements->contains(toElement(*it)))
49 result->append(toElement(*it));
50 sorted(result, *it);
51 children.remove(it);
52 }
53
54 void CustomElementUpgradeSorter::sorted(
55 HeapVector<Member<Element>>* result,
56 Node* parent)
57 {
58 ParentChildMap::iterator childrenIterator = m_parentChildMap->find(parent);
59 if (childrenIterator == m_parentChildMap->end())
60 return;
61
62 ChildSet& children = childrenIterator->value;
63
64 if (children.size() == 1) {
65 visit(result, children, children.begin());
66 return;
67 }
68
69 // TODO(dominicc): When custom elements are used in UA shadow
70 // roots, expand this to include UA shadow roots.
71 ShadowRoot* shadowRoot = parent->isElementNode()
72 ? toElement(parent)->authorShadowRoot()
73 : nullptr;
74 if (shadowRoot)
75 visit(result, children, children.find(shadowRoot));
76
77 for (Element* e = ElementTraversal::firstChild(*parent);
78 e && children.size() > 1;
79 e = ElementTraversal::nextSibling(*e)) {
80
81 visit(result, children, children.find(e));
82 }
83
84 if (children.size() == 1)
85 visit(result, children, children.begin());
86
87 DCHECK(children.isEmpty());
88 }
89
90 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698