1 | /*
|
---|
2 | * Copyright (C) 2021 Tyler Wilcock <[email protected]>.
|
---|
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 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "CSSCounterStyleRule.h"
|
---|
28 |
|
---|
29 | #include "CSSPropertyParser.h"
|
---|
30 | #include "CSSStyleSheet.h"
|
---|
31 | #include "CSSTokenizer.h"
|
---|
32 | #include "Pair.h"
|
---|
33 | #include <wtf/text/StringBuilder.h>
|
---|
34 |
|
---|
35 | namespace WebCore {
|
---|
36 |
|
---|
37 | StyleRuleCounterStyle::StyleRuleCounterStyle(const AtomString& name, Ref<StyleProperties>&& properties)
|
---|
38 | : StyleRuleBase(StyleRuleType::CounterStyle)
|
---|
39 | , m_name(name)
|
---|
40 | , m_properties(WTFMove(properties))
|
---|
41 | {
|
---|
42 | }
|
---|
43 |
|
---|
44 | Ref<StyleRuleCounterStyle> StyleRuleCounterStyle::create(const AtomString& name, Ref<StyleProperties>&& properties)
|
---|
45 | {
|
---|
46 | return adoptRef(*new StyleRuleCounterStyle(name, WTFMove(properties)));
|
---|
47 | }
|
---|
48 |
|
---|
49 | static CounterStyleSystem toCounterStyleSystemEnum(RefPtr<CSSValue> system)
|
---|
50 | {
|
---|
51 | if (!system || !system->isPrimitiveValue())
|
---|
52 | return CounterStyleSystem::Symbolic;
|
---|
53 |
|
---|
54 | auto& primitiveSystemValue = downcast<CSSPrimitiveValue>(*system);
|
---|
55 | ASSERT(primitiveSystemValue.isValueID() || primitiveSystemValue.isPair());
|
---|
56 | CSSValueID systemKeyword = CSSValueInvalid;
|
---|
57 | if (primitiveSystemValue.isValueID())
|
---|
58 | systemKeyword = primitiveSystemValue.valueID();
|
---|
59 | else if (auto* pair = primitiveSystemValue.pairValue()) {
|
---|
60 | // This value must be `fixed` or `extends`, both of which can or must have an additional component.
|
---|
61 | auto firstValue = pair->first();
|
---|
62 | ASSERT(firstValue && firstValue->isValueID());
|
---|
63 | if (firstValue)
|
---|
64 | systemKeyword = firstValue->valueID();
|
---|
65 | }
|
---|
66 |
|
---|
67 | switch (systemKeyword) {
|
---|
68 | case CSSValueCyclic:
|
---|
69 | return CounterStyleSystem::Cyclic;
|
---|
70 | case CSSValueFixed:
|
---|
71 | return CounterStyleSystem::Fixed;
|
---|
72 | case CSSValueSymbolic:
|
---|
73 | return CounterStyleSystem::Symbolic;
|
---|
74 | case CSSValueAlphabetic:
|
---|
75 | return CounterStyleSystem::Alphabetic;
|
---|
76 | case CSSValueNumeric:
|
---|
77 | return CounterStyleSystem::Numeric;
|
---|
78 | case CSSValueAdditive:
|
---|
79 | return CounterStyleSystem::Additive;
|
---|
80 | case CSSValueExtends:
|
---|
81 | return CounterStyleSystem::Extends;
|
---|
82 | default:
|
---|
83 | ASSERT_NOT_REACHED();
|
---|
84 | return CounterStyleSystem::Symbolic;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | static bool symbolsValidForSystem(CounterStyleSystem system, RefPtr<CSSValue> symbols, RefPtr<CSSValue> additiveSymbols)
|
---|
89 | {
|
---|
90 | switch (system) {
|
---|
91 | case CounterStyleSystem::Cyclic:
|
---|
92 | case CounterStyleSystem::Fixed:
|
---|
93 | case CounterStyleSystem::Symbolic:
|
---|
94 | return symbols && symbols->isValueList() && downcast<CSSValueList>(*symbols).length();
|
---|
95 | case CounterStyleSystem::Alphabetic:
|
---|
96 | case CounterStyleSystem::Numeric:
|
---|
97 | return symbols && symbols->isValueList() && downcast<CSSValueList>(*symbols).length() >= 2u;
|
---|
98 | case CounterStyleSystem::Additive:
|
---|
99 | return additiveSymbols && additiveSymbols->isValueList() && downcast<CSSValueList>(*additiveSymbols).length();
|
---|
100 | case CounterStyleSystem::Extends:
|
---|
101 | return !symbols && !additiveSymbols;
|
---|
102 | default:
|
---|
103 | ASSERT_NOT_REACHED();
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | bool StyleRuleCounterStyle::newValueInvalidOrEqual(CSSPropertyID propertyID, const RefPtr<CSSValue> newValue) const
|
---|
109 | {
|
---|
110 | auto currentValue = m_properties->getPropertyCSSValue(propertyID);
|
---|
111 | if (compareCSSValuePtr(currentValue, newValue))
|
---|
112 | return true;
|
---|
113 |
|
---|
114 | RefPtr<CSSValue> symbols;
|
---|
115 | RefPtr<CSSValue> additiveSymbols;
|
---|
116 | switch (propertyID) {
|
---|
117 | case CSSPropertySystem:
|
---|
118 | // If the attribute being set is `system`, and the new value would change the algorithm used, do nothing
|
---|
119 | // and abort these steps.
|
---|
120 | // (It's okay to change an aspect of the algorithm, like the first symbol value of a `fixed` system.)
|
---|
121 | return toCounterStyleSystemEnum(currentValue) != toCounterStyleSystemEnum(newValue);
|
---|
122 | case CSSPropertySymbols:
|
---|
123 | symbols = newValue;
|
---|
124 | additiveSymbols = m_properties->getPropertyCSSValue(CSSPropertyAdditiveSymbols);
|
---|
125 | break;
|
---|
126 | case CSSPropertyAdditiveSymbols:
|
---|
127 | symbols = m_properties->getPropertyCSSValue(CSSPropertySymbols);
|
---|
128 | additiveSymbols = newValue;
|
---|
129 | break;
|
---|
130 | default:
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 | auto system = m_properties->getPropertyCSSValue(CSSPropertySystem);
|
---|
134 | return symbolsValidForSystem(toCounterStyleSystemEnum(system), symbols, additiveSymbols);
|
---|
135 | }
|
---|
136 |
|
---|
137 | StyleRuleCounterStyle::~StyleRuleCounterStyle() = default;
|
---|
138 |
|
---|
139 | MutableStyleProperties& StyleRuleCounterStyle::mutableProperties()
|
---|
140 | {
|
---|
141 | if (!is<MutableStyleProperties>(m_properties))
|
---|
142 | m_properties = m_properties->mutableCopy();
|
---|
143 | return downcast<MutableStyleProperties>(m_properties.get());
|
---|
144 | }
|
---|
145 |
|
---|
146 | Ref<CSSCounterStyleRule> CSSCounterStyleRule::create(StyleRuleCounterStyle& rule, CSSStyleSheet* sheet)
|
---|
147 | {
|
---|
148 | return adoptRef(*new CSSCounterStyleRule(rule, sheet));
|
---|
149 | }
|
---|
150 |
|
---|
151 | CSSCounterStyleRule::CSSCounterStyleRule(StyleRuleCounterStyle& counterStyleRule, CSSStyleSheet* parent)
|
---|
152 | : CSSRule(parent)
|
---|
153 | , m_counterStyleRule(counterStyleRule)
|
---|
154 | {
|
---|
155 | }
|
---|
156 |
|
---|
157 | CSSCounterStyleRule::~CSSCounterStyleRule() = default;
|
---|
158 |
|
---|
159 | String CSSCounterStyleRule::cssText() const
|
---|
160 | {
|
---|
161 | String systemText = system();
|
---|
162 | const char* systemPrefix = systemText.isEmpty() ? "" : " system: ";
|
---|
163 | const char* systemSuffix = systemText.isEmpty() ? "" : ";";
|
---|
164 |
|
---|
165 | String symbolsText = symbols();
|
---|
166 | const char* symbolsPrefix = symbolsText.isEmpty() ? "" : " symbols: ";
|
---|
167 | const char* symbolsSuffix = symbolsText.isEmpty() ? "" : ";";
|
---|
168 |
|
---|
169 | String additiveSymbolsText = additiveSymbols();
|
---|
170 | const char* additiveSymbolsPrefix = additiveSymbolsText.isEmpty() ? "" : " additive-symbols: ";
|
---|
171 | const char* additiveSymbolsSuffix = additiveSymbolsText.isEmpty() ? "" : ";";
|
---|
172 |
|
---|
173 | String negativeText = negative();
|
---|
174 | const char* negativePrefix = negativeText.isEmpty() ? "" : " negative: ";
|
---|
175 | const char* negativeSuffix = negativeText.isEmpty() ? "" : ";";
|
---|
176 |
|
---|
177 | String prefixText = prefix();
|
---|
178 | const char* prefixTextPrefix = prefixText.isEmpty() ? "" : " prefix: ";
|
---|
179 | const char* prefixTextSuffix = prefixText.isEmpty() ? "" : ";";
|
---|
180 |
|
---|
181 | String suffixText = suffix();
|
---|
182 | const char* suffixTextPrefix = suffixText.isEmpty() ? "" : " suffix: ";
|
---|
183 | const char* suffixTextSuffix = suffixText.isEmpty() ? "" : ";";
|
---|
184 |
|
---|
185 | String padText = pad();
|
---|
186 | const char* padPrefix = padText.isEmpty() ? "" : " pad: ";
|
---|
187 | const char* padSuffix = padText.isEmpty() ? "" : ";";
|
---|
188 |
|
---|
189 | String rangeText = range();
|
---|
190 | const char* rangePrefix = rangeText.isEmpty() ? "" : " range: ";
|
---|
191 | const char* rangeSuffix = rangeText.isEmpty() ? "" : ";";
|
---|
192 |
|
---|
193 | String fallbackText = fallback();
|
---|
194 | const char* fallbackPrefix = fallbackText.isEmpty() ? "" : " fallback: ";
|
---|
195 | const char* fallbackSuffix = fallbackText.isEmpty() ? "" : ";";
|
---|
196 |
|
---|
197 | String speakAsText = speakAs();
|
---|
198 | const char* speakAsPrefix = speakAsText.isEmpty() ? "" : " speak-as: ";
|
---|
199 | const char* speakAsSuffix = speakAsText.isEmpty() ? "" : ";";
|
---|
200 |
|
---|
201 | return makeString("@counter-style ", name(), " {",
|
---|
202 | systemPrefix, systemText, systemSuffix,
|
---|
203 | symbolsPrefix, symbolsText, symbolsSuffix,
|
---|
204 | additiveSymbolsPrefix, additiveSymbolsText, additiveSymbolsSuffix,
|
---|
205 | negativePrefix, negativeText, negativeSuffix,
|
---|
206 | prefixTextPrefix, prefixText, prefixTextSuffix,
|
---|
207 | suffixTextPrefix, suffixText, suffixTextSuffix,
|
---|
208 | padPrefix, padText, padSuffix,
|
---|
209 | rangePrefix, rangeText, rangeSuffix,
|
---|
210 | fallbackPrefix, fallbackText, fallbackSuffix,
|
---|
211 | speakAsPrefix, speakAsText, speakAsSuffix,
|
---|
212 | " }");
|
---|
213 | }
|
---|
214 |
|
---|
215 | void CSSCounterStyleRule::reattach(StyleRuleBase& rule)
|
---|
216 | {
|
---|
217 | m_counterStyleRule = static_cast<StyleRuleCounterStyle&>(rule);
|
---|
218 | }
|
---|
219 |
|
---|
220 | // https://drafts.csswg.org/css-counter-styles-3/#dom-csscounterstylerule-name
|
---|
221 | void CSSCounterStyleRule::setName(const String& text)
|
---|
222 | {
|
---|
223 | auto tokenizer = CSSTokenizer(text);
|
---|
224 | auto tokenRange = tokenizer.tokenRange();
|
---|
225 | auto name = CSSPropertyParserHelpers::consumeCounterStyleNameInPrelude(tokenRange);
|
---|
226 | if (name.isNull() || name == m_counterStyleRule->name())
|
---|
227 | return;
|
---|
228 |
|
---|
229 | CSSStyleSheet::RuleMutationScope mutationScope(this);
|
---|
230 | m_counterStyleRule->setName(name);
|
---|
231 | }
|
---|
232 |
|
---|
233 | void CSSCounterStyleRule::setterInternal(CSSPropertyID propertyID, const String& valueText)
|
---|
234 | {
|
---|
235 | auto tokenizer = CSSTokenizer(valueText);
|
---|
236 | auto tokenRange = tokenizer.tokenRange();
|
---|
237 | auto newValue = CSSPropertyParser::parseCounterStyleDescriptor(propertyID, tokenRange, parserContext());
|
---|
238 | if (m_counterStyleRule->newValueInvalidOrEqual(propertyID, newValue))
|
---|
239 | return;
|
---|
240 |
|
---|
241 | CSSStyleSheet::RuleMutationScope mutationScope(this);
|
---|
242 | m_counterStyleRule->mutableProperties().setProperty(propertyID, WTFMove(newValue));
|
---|
243 | }
|
---|
244 |
|
---|
245 | void CSSCounterStyleRule::setSystem(const String& text)
|
---|
246 | {
|
---|
247 | setterInternal(CSSPropertySystem, text);
|
---|
248 | }
|
---|
249 |
|
---|
250 | void CSSCounterStyleRule::setNegative(const String& text)
|
---|
251 | {
|
---|
252 | setterInternal(CSSPropertyNegative, text);
|
---|
253 | }
|
---|
254 |
|
---|
255 | void CSSCounterStyleRule::setPrefix(const String& text)
|
---|
256 | {
|
---|
257 | setterInternal(CSSPropertyPrefix, text);
|
---|
258 | }
|
---|
259 |
|
---|
260 | void CSSCounterStyleRule::setSuffix(const String& text)
|
---|
261 | {
|
---|
262 | setterInternal(CSSPropertySuffix, text);
|
---|
263 | }
|
---|
264 |
|
---|
265 | void CSSCounterStyleRule::setRange(const String& text)
|
---|
266 | {
|
---|
267 | setterInternal(CSSPropertyRange, text);
|
---|
268 | }
|
---|
269 |
|
---|
270 | void CSSCounterStyleRule::setPad(const String& text)
|
---|
271 | {
|
---|
272 | setterInternal(CSSPropertyPad, text);
|
---|
273 | }
|
---|
274 |
|
---|
275 | void CSSCounterStyleRule::setFallback(const String& text)
|
---|
276 | {
|
---|
277 | setterInternal(CSSPropertyFallback, text);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void CSSCounterStyleRule::setSymbols(const String& text)
|
---|
281 | {
|
---|
282 | setterInternal(CSSPropertySymbols, text);
|
---|
283 | }
|
---|
284 |
|
---|
285 | void CSSCounterStyleRule::setAdditiveSymbols(const String& text)
|
---|
286 | {
|
---|
287 | setterInternal(CSSPropertyAdditiveSymbols, text);
|
---|
288 | }
|
---|
289 |
|
---|
290 | void CSSCounterStyleRule::setSpeakAs(const String& text)
|
---|
291 | {
|
---|
292 | setterInternal(CSSPropertySpeakAs, text);
|
---|
293 | }
|
---|
294 |
|
---|
295 | } // namespace WebCore
|
---|