source: webkit/trunk/Source/WebCore/css/ContainerQuery.cpp

Last change on this file was 292375, checked in by Antti Koivisto, 3 years ago

[CSS Container Queries] Simplify grammar
https://bugs.webkit.org/show_bug.cgi?id=238732

Reviewed by Sam Weinig.

Because size() function syntax is no longer supported, separate SizeQuery and SizeCondition productions
are no longer needed.

Some slightly unnecessary templatization is retained to help with future work.

  • css/ContainerQuery.cpp:
  • css/ContainerQuery.h:
  • css/ContainerQueryParser.cpp:

(WebCore::ContainerQueryParser::consumeContainerQuery):
(WebCore::ContainerQueryParser::consumeCondition):
(WebCore::ContainerQueryParser::consumeSizeFeature):
(WebCore::ContainerQueryParser::consumePlainSizeFeature):
(WebCore::ContainerQueryParser::consumeSizeQuery): Deleted.

  • css/ContainerQueryParser.h:
  • style/ContainerQueryEvaluator.cpp:

(WebCore::Style::ContainerQueryEvaluator::evaluateQuery const):

  • style/ContainerQueryEvaluator.h:
File size: 5.9 KB
Line 
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
33namespace WebCore {
34namespace CQ {
35namespace FeatureNames {
36
37const AtomString& width()
38{
39 static MainThreadNeverDestroyed<AtomString> name { "width"_s };
40 return name;
41}
42
43const AtomString& height()
44{
45 static MainThreadNeverDestroyed<AtomString> name { "height"_s };
46 return name;
47}
48
49const AtomString& inlineSize()
50{
51 static MainThreadNeverDestroyed<AtomString> name { "inline-size"_s };
52 return name;
53}
54
55const AtomString& blockSize()
56{
57 static MainThreadNeverDestroyed<AtomString> name { "block-size"_s };
58 return name;
59}
60
61const AtomString& aspectRatio()
62{
63 static MainThreadNeverDestroyed<AtomString> name { "aspect-ratio"_s };
64 return name;
65}
66
67const AtomString& orientation()
68{
69 static MainThreadNeverDestroyed<AtomString> name { "orientation"_s };
70 return name;
71}
72
73}
74
75OptionSet<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
90void serialize(StringBuilder&, const SizeFeature&);
91template<typename ConditionType> void serialize(StringBuilder&, const ConditionType&);
92
93static 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
107void 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
173template<typename ConditionType>
174void 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
191void serialize(StringBuilder& builder, const ContainerQuery& query)
192{
193 CQ::serialize(builder, query);
194}
195
196}
197
Note: See TracBrowser for help on using the repository browser.