1 | /* |
2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "StyleRelations.h" |
28 | |
29 | #include "Element.h" |
30 | #include "NodeRenderStyle.h" |
31 | #include "RenderStyle.h" |
32 | #include "StyleUpdate.h" |
33 | |
34 | namespace WebCore { |
35 | namespace Style { |
36 | |
37 | std::unique_ptr<Relations> commitRelationsToRenderStyle(RenderStyle& style, const Element& element, const Relations& relations) |
38 | { |
39 | std::unique_ptr<Relations> remainingRelations; |
40 | |
41 | auto appendStyleRelation = [&remainingRelations] (const Relation& relation) { |
42 | if (!remainingRelations) |
43 | remainingRelations = std::make_unique<Relations>(); |
44 | remainingRelations->append(relation); |
45 | }; |
46 | |
47 | for (auto& relation : relations) { |
48 | if (relation.element != &element) { |
49 | appendStyleRelation(relation); |
50 | continue; |
51 | } |
52 | switch (relation.type) { |
53 | case Relation::AffectedByActive: |
54 | style.setAffectedByActive(); |
55 | appendStyleRelation(relation); |
56 | break; |
57 | case Relation::AffectedByDrag: |
58 | style.setAffectedByDrag(); |
59 | break; |
60 | case Relation::AffectedByEmpty: |
61 | style.setEmptyState(relation.value); |
62 | appendStyleRelation(relation); |
63 | break; |
64 | case Relation::AffectedByHover: |
65 | style.setAffectedByHover(); |
66 | break; |
67 | case Relation::FirstChild: |
68 | style.setFirstChildState(); |
69 | break; |
70 | case Relation::LastChild: |
71 | style.setLastChildState(); |
72 | break; |
73 | case Relation::Unique: |
74 | style.setUnique(); |
75 | break; |
76 | case Relation::AffectedByFocusWithin: |
77 | case Relation::AffectedByPreviousSibling: |
78 | case Relation::DescendantsAffectedByPreviousSibling: |
79 | case Relation::AffectsNextSibling: |
80 | case Relation::ChildrenAffectedByForwardPositionalRules: |
81 | case Relation::DescendantsAffectedByForwardPositionalRules: |
82 | case Relation::ChildrenAffectedByBackwardPositionalRules: |
83 | case Relation::DescendantsAffectedByBackwardPositionalRules: |
84 | case Relation::ChildrenAffectedByFirstChildRules: |
85 | case Relation::ChildrenAffectedByPropertyBasedBackwardPositionalRules: |
86 | case Relation::ChildrenAffectedByLastChildRules: |
87 | case Relation::NthChildIndex: |
88 | appendStyleRelation(relation); |
89 | break; |
90 | } |
91 | } |
92 | return remainingRelations; |
93 | } |
94 | |
95 | void commitRelations(std::unique_ptr<Relations> relations, Update& update) |
96 | { |
97 | if (!relations) |
98 | return; |
99 | for (auto& relation : *relations) { |
100 | auto& element = const_cast<Element&>(*relation.element); |
101 | switch (relation.type) { |
102 | case Relation::AffectedByActive: |
103 | element.setStyleAffectedByActive(); |
104 | break; |
105 | case Relation::AffectedByDrag: |
106 | element.setChildrenAffectedByDrag(); |
107 | break; |
108 | case Relation::AffectedByEmpty: |
109 | element.setStyleAffectedByEmpty(); |
110 | break; |
111 | case Relation::AffectedByFocusWithin: |
112 | element.setStyleAffectedByFocusWithin(); |
113 | break; |
114 | case Relation::AffectedByHover: |
115 | element.setChildrenAffectedByHover(); |
116 | break; |
117 | case Relation::AffectedByPreviousSibling: |
118 | element.setStyleIsAffectedByPreviousSibling(); |
119 | break; |
120 | case Relation::DescendantsAffectedByPreviousSibling: |
121 | element.setDescendantsAffectedByPreviousSibling(); |
122 | break; |
123 | case Relation::AffectsNextSibling: { |
124 | auto* sibling = &element; |
125 | for (unsigned i = 0; i < relation.value && sibling; ++i, sibling = sibling->nextElementSibling()) |
126 | sibling->setAffectsNextSiblingElementStyle(); |
127 | break; |
128 | } |
129 | case Relation::ChildrenAffectedByForwardPositionalRules: |
130 | element.setChildrenAffectedByForwardPositionalRules(); |
131 | break; |
132 | case Relation::DescendantsAffectedByForwardPositionalRules: |
133 | element.setDescendantsAffectedByForwardPositionalRules(); |
134 | break; |
135 | case Relation::ChildrenAffectedByBackwardPositionalRules: |
136 | element.setChildrenAffectedByBackwardPositionalRules(); |
137 | break; |
138 | case Relation::DescendantsAffectedByBackwardPositionalRules: |
139 | element.setDescendantsAffectedByBackwardPositionalRules(); |
140 | break; |
141 | case Relation::ChildrenAffectedByFirstChildRules: |
142 | element.setChildrenAffectedByFirstChildRules(); |
143 | break; |
144 | case Relation::ChildrenAffectedByPropertyBasedBackwardPositionalRules: |
145 | element.setChildrenAffectedByBackwardPositionalRules(); |
146 | element.setChildrenAffectedByPropertyBasedBackwardPositionalRules(); |
147 | break; |
148 | case Relation::ChildrenAffectedByLastChildRules: |
149 | element.setChildrenAffectedByLastChildRules(); |
150 | break; |
151 | case Relation::FirstChild: |
152 | if (auto* style = update.elementStyle(element)) |
153 | style->setFirstChildState(); |
154 | break; |
155 | case Relation::LastChild: |
156 | if (auto* style = update.elementStyle(element)) |
157 | style->setLastChildState(); |
158 | break; |
159 | case Relation::NthChildIndex: |
160 | if (auto* style = update.elementStyle(element)) |
161 | style->setUnique(); |
162 | element.setChildIndex(relation.value); |
163 | break; |
164 | case Relation::Unique: |
165 | if (auto* style = update.elementStyle(element)) |
166 | style->setUnique(); |
167 | break; |
168 | } |
169 | } |
170 | } |
171 | |
172 | } |
173 | } |
174 | |