1 | /*
|
---|
2 | * Copyright (C) 2022 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. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
20 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
22 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "config.h"
|
---|
26 | #include "ContainerQuery.h"
|
---|
27 |
|
---|
28 | #include "CSSMarkup.h"
|
---|
29 | #include "CSSValue.h"
|
---|
30 | #include <wtf/NeverDestroyed.h>
|
---|
31 | #include <wtf/text/StringBuilder.h>
|
---|
32 |
|
---|
33 | namespace WebCore {
|
---|
34 | namespace CQ {
|
---|
35 | namespace FeatureNames {
|
---|
36 |
|
---|
37 | const AtomString& width()
|
---|
38 | {
|
---|
39 | static MainThreadNeverDestroyed<AtomString> name { "width"_s };
|
---|
40 | return name;
|
---|
41 | }
|
---|
42 |
|
---|
43 | const AtomString& height()
|
---|
44 | {
|
---|
45 | static MainThreadNeverDestroyed<AtomString> name { "height"_s };
|
---|
46 | return name;
|
---|
47 | }
|
---|
48 |
|
---|
49 | const AtomString& inlineSize()
|
---|
50 | {
|
---|
51 | static MainThreadNeverDestroyed<AtomString> name { "inline-size"_s };
|
---|
52 | return name;
|
---|
53 | }
|
---|
54 |
|
---|
55 | const AtomString& blockSize()
|
---|
56 | {
|
---|
57 | static MainThreadNeverDestroyed<AtomString> name { "block-size"_s };
|
---|
58 | return name;
|
---|
59 | }
|
---|
60 |
|
---|
61 | const AtomString& aspectRatio()
|
---|
62 | {
|
---|
63 | static MainThreadNeverDestroyed<AtomString> name { "aspect-ratio"_s };
|
---|
64 | return name;
|
---|
65 | }
|
---|
66 |
|
---|
67 | const AtomString& orientation()
|
---|
68 | {
|
---|
69 | static MainThreadNeverDestroyed<AtomString> name { "orientation"_s };
|
---|
70 | return name;
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | OptionSet<Axis> requiredAxesForFeature(const AtomString& featureName)
|
---|
76 | {
|
---|
77 | if (featureName == FeatureNames::width())
|
---|
78 | return { Axis::Width };
|
---|
79 | if (featureName == FeatureNames::height())
|
---|
80 | return { Axis::Height };
|
---|
81 | if (featureName == FeatureNames::inlineSize())
|
---|
82 | return { Axis::Inline };
|
---|
83 | if (featureName == FeatureNames::blockSize())
|
---|
84 | return { Axis::Block };
|
---|
85 | if (featureName == FeatureNames::aspectRatio() || featureName == FeatureNames::orientation())
|
---|
86 | return { Axis::Inline, Axis::Block };
|
---|
87 | return { };
|
---|
88 | }
|
---|
89 |
|
---|
90 | void serialize(StringBuilder&, const SizeFeature&);
|
---|
91 | template<typename ConditionType> void serialize(StringBuilder&, const ConditionType&);
|
---|
92 |
|
---|
93 | static void serialize(StringBuilder& builder, const ContainerQuery& containerQuery)
|
---|
94 | {
|
---|
95 | WTF::switchOn(containerQuery, [&](auto& node) {
|
---|
96 | builder.append('(');
|
---|
97 | serialize(builder, node);
|
---|
98 | builder.append(')');
|
---|
99 | }, [&](const CQ::UnknownQuery& unknownQuery) {
|
---|
100 | builder.append(unknownQuery.name);
|
---|
101 | builder.append('(');
|
---|
102 | builder.append(unknownQuery.text);
|
---|
103 | builder.append(')');
|
---|
104 | });
|
---|
105 | }
|
---|
106 |
|
---|
107 | void serialize(StringBuilder& builder, const SizeFeature& sizeFeature)
|
---|
108 | {
|
---|
109 | auto serializeRangeComparisonOperator = [&](ComparisonOperator op) {
|
---|
110 | builder.append(' ');
|
---|
111 | switch (op) {
|
---|
112 | case ComparisonOperator::LessThan:
|
---|
113 | builder.append('<');
|
---|
114 | break;
|
---|
115 | case ComparisonOperator::LessThanOrEqual:
|
---|
116 | builder.append("<=");
|
---|
117 | break;
|
---|
118 | case ComparisonOperator::Equal:
|
---|
119 | builder.append('=');
|
---|
120 | break;
|
---|
121 | case ComparisonOperator::GreaterThan:
|
---|
122 | builder.append('>');
|
---|
123 | break;
|
---|
124 | case ComparisonOperator::GreaterThanOrEqual:
|
---|
125 | builder.append(">=");
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | builder.append(' ');
|
---|
129 | };
|
---|
130 |
|
---|
131 | switch (sizeFeature.syntax) {
|
---|
132 | case Syntax::Boolean:
|
---|
133 | serializeIdentifier(sizeFeature.name, builder);
|
---|
134 | break;
|
---|
135 |
|
---|
136 | case Syntax::Colon:
|
---|
137 | switch (sizeFeature.rightComparison->op) {
|
---|
138 | case ComparisonOperator::LessThanOrEqual:
|
---|
139 | builder.append("max-");
|
---|
140 | break;
|
---|
141 | case ComparisonOperator::Equal:
|
---|
142 | break;
|
---|
143 | case ComparisonOperator::GreaterThanOrEqual:
|
---|
144 | builder.append("min-");
|
---|
145 | break;
|
---|
146 | case ComparisonOperator::LessThan:
|
---|
147 | case ComparisonOperator::GreaterThan:
|
---|
148 | ASSERT_NOT_REACHED();
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | serializeIdentifier(sizeFeature.name, builder);
|
---|
152 |
|
---|
153 | builder.append(": ");
|
---|
154 | builder.append(sizeFeature.rightComparison->value->cssText());
|
---|
155 | break;
|
---|
156 |
|
---|
157 | case Syntax::Range:
|
---|
158 | if (sizeFeature.leftComparison) {
|
---|
159 | builder.append(sizeFeature.leftComparison->value->cssText());
|
---|
160 | serializeRangeComparisonOperator(sizeFeature.leftComparison->op);
|
---|
161 | }
|
---|
162 |
|
---|
163 | serializeIdentifier(sizeFeature.name, builder);
|
---|
164 |
|
---|
165 | if (sizeFeature.rightComparison) {
|
---|
166 | serializeRangeComparisonOperator(sizeFeature.rightComparison->op);
|
---|
167 | builder.append(sizeFeature.rightComparison->value->cssText());
|
---|
168 | }
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | template<typename ConditionType>
|
---|
174 | void serialize(StringBuilder& builder, const ConditionType& condition)
|
---|
175 | {
|
---|
176 | if (condition.queries.size() == 1 && condition.logicalOperator == LogicalOperator::Not) {
|
---|
177 | builder.append("not ");
|
---|
178 | serialize(builder, condition.queries.first());
|
---|
179 | return;
|
---|
180 | }
|
---|
181 |
|
---|
182 | for (auto& query : condition.queries) {
|
---|
183 | if (&query != &condition.queries.first())
|
---|
184 | builder.append(condition.logicalOperator == LogicalOperator::And ? " and " : " or ");
|
---|
185 | serialize(builder, query);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 | void serialize(StringBuilder& builder, const ContainerQuery& query)
|
---|
192 | {
|
---|
193 | CQ::serialize(builder, query);
|
---|
194 | }
|
---|
195 |
|
---|
196 | }
|
---|
197 |
|
---|