blob: 0b2ffcb60e8dc9bff14d818c5163f77b93082eb8 [file] [log] [blame]
hyatt@apple.comfaa1d7b2016-09-13 19:08:30 +00001// Copyright 2015 The Chromium Authors. All rights reserved.
darin@apple.com61b455d2021-09-14 19:59:52 +00002// Copyright (C) 2016-2021 Apple Inc. All rights reserved.
hyatt@apple.comfaa1d7b2016-09-13 19:08:30 +00003//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "config.h"
31#include "CSSVariableReferenceValue.h"
32
darin@apple.comeb026082020-04-12 19:33:30 +000033#include "CSSVariableData.h"
emilio@crisal.io14485842020-04-19 18:01:18 +000034#include "ConstantPropertyMap.h"
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000035#include "RenderStyle.h"
antti@apple.com3c83b832019-10-31 18:55:34 +000036#include "StyleBuilder.h"
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000037#include "StyleResolver.h"
38
hyatt@apple.comfaa1d7b2016-09-13 19:08:30 +000039namespace WebCore {
40
darin@apple.comeb026082020-04-12 19:33:30 +000041static bool resolveTokenRange(CSSParserTokenRange, Vector<CSSParserToken>&, Style::BuilderState&);
42
darin@apple.com61b455d2021-09-14 19:59:52 +000043CSSVariableReferenceValue::CSSVariableReferenceValue(Ref<CSSVariableData>&& data, const CSSParserContext& context)
darin@apple.comeb026082020-04-12 19:33:30 +000044 : CSSValue(VariableReferenceClass)
45 , m_data(WTFMove(data))
darin@apple.com61b455d2021-09-14 19:59:52 +000046 , m_context(context)
hyatt@apple.comfaa1d7b2016-09-13 19:08:30 +000047{
hyatt@apple.com9e09f632016-10-27 22:05:01 +000048}
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000049
darin@apple.com61b455d2021-09-14 19:59:52 +000050Ref<CSSVariableReferenceValue> CSSVariableReferenceValue::create(const CSSParserTokenRange& range, const CSSParserContext& context)
darin@apple.comeb026082020-04-12 19:33:30 +000051{
darin@apple.com61b455d2021-09-14 19:59:52 +000052 return adoptRef(*new CSSVariableReferenceValue(CSSVariableData::create(range), context));
darin@apple.comeb026082020-04-12 19:33:30 +000053}
54
achristensen@apple.come7dbfd32021-09-20 17:41:00 +000055Ref<CSSVariableReferenceValue> CSSVariableReferenceValue::create(Ref<CSSVariableData>&& data, const CSSParserContext& context)
56{
57 return adoptRef(*new CSSVariableReferenceValue(WTFMove(data), context));
58}
59
darin@apple.comeb026082020-04-12 19:33:30 +000060bool CSSVariableReferenceValue::equals(const CSSVariableReferenceValue& other) const
61{
62 return m_data.get() == other.m_data.get();
63}
64
65String CSSVariableReferenceValue::customCSSText() const
66{
67 if (m_stringValue.isNull())
68 m_stringValue = m_data->tokenRange().serialize();
69 return m_stringValue;
70}
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000071
antti@apple.com60abb7b2019-10-30 20:13:30 +000072static bool resolveVariableFallback(CSSParserTokenRange range, Vector<CSSParserToken>& result, Style::BuilderState& builderState)
hyatt@apple.com9e09f632016-10-27 22:05:01 +000073{
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000074 if (range.atEnd())
75 return false;
76 ASSERT(range.peek().type() == CommaToken);
darin@apple.coma073f722021-06-29 01:34:20 +000077 range.consumeIncludingWhitespace();
antti@apple.com60abb7b2019-10-30 20:13:30 +000078 return resolveTokenRange(range, result, builderState);
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000079}
80
emilio@crisal.io14485842020-04-19 18:01:18 +000081static bool resolveVariableReference(CSSParserTokenRange range, CSSValueID functionId, Vector<CSSParserToken>& result, Style::BuilderState& builderState)
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000082{
emilio@crisal.io14485842020-04-19 18:01:18 +000083 ASSERT(functionId == CSSValueVar || functionId == CSSValueEnv);
84
antti@apple.com60abb7b2019-10-30 20:13:30 +000085 auto& registeredProperties = builderState.document().getCSSRegisteredCustomPropertySet();
86 auto& style = builderState.style();
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000087
88 range.consumeWhitespace();
89 ASSERT(range.peek().type() == IdentToken);
cdumez@apple.com30939fd2022-04-28 04:50:39 +000090 auto variableName = range.consumeIncludingWhitespace().value().toAtomString();
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000091 ASSERT(range.atEnd() || (range.peek().type() == CommaToken));
92
93 // Apply this variable first, in case it is still unresolved
antti@apple.com3c83b832019-10-31 18:55:34 +000094 builderState.builder().applyCustomProperty(variableName);
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000095
96 // Apply fallback to detect cycles
97 Vector<CSSParserToken> fallbackResult;
antti@apple.com60abb7b2019-10-30 20:13:30 +000098 bool fallbackReturn = resolveVariableFallback(CSSParserTokenRange(range), fallbackResult, builderState);
justin_michaud@apple.comea578c32018-11-01 20:35:27 +000099
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000100
emilio@crisal.io14485842020-04-19 18:01:18 +0000101 auto* property = functionId == CSSValueVar
102 ? style.getCustomProperty(variableName)
103 : builderState.document().constantProperties().values().get(variableName);
justin_michaud@apple.comb863bdf2018-12-19 03:21:15 +0000104 if (!property || property->isUnset()) {
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000105 auto* registered = registeredProperties.get(variableName);
106 if (registered && registered->initialValue())
107 property = registered->initialValue();
justin_michaud@apple.comb863bdf2018-12-19 03:21:15 +0000108 }
109
110 if (!property || property->isInvalid()) {
commit-queue@webkit.orgc1cc2af2020-09-13 06:39:34 +0000111 if (fallbackResult.size() > CSSVariableReferenceValue::maxSubstitutionTokens)
112 return false;
113
justin_michaud@apple.comb863bdf2018-12-19 03:21:15 +0000114 if (fallbackReturn)
115 result.appendVector(fallbackResult);
116 return fallbackReturn;
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000117 }
118
119 ASSERT(property->isResolved());
commit-queue@webkit.orgc1cc2af2020-09-13 06:39:34 +0000120 if (property->tokens().size() > CSSVariableReferenceValue::maxSubstitutionTokens)
121 return false;
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000122
commit-queue@webkit.orgc1cc2af2020-09-13 06:39:34 +0000123 result.appendVector(property->tokens());
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000124 return true;
125}
126
antti@apple.com60abb7b2019-10-30 20:13:30 +0000127static bool resolveTokenRange(CSSParserTokenRange range, Vector<CSSParserToken>& result, Style::BuilderState& builderState)
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000128{
129 bool success = true;
130 while (!range.atEnd()) {
emilio@crisal.io14485842020-04-19 18:01:18 +0000131 auto functionId = range.peek().functionId();
132 if (functionId == CSSValueVar || functionId == CSSValueEnv)
133 success &= resolveVariableReference(range.consumeBlock(), functionId, result, builderState);
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000134 else
135 result.append(range.consume());
136 }
137 return success;
138}
139
antti@apple.com60abb7b2019-10-30 20:13:30 +0000140RefPtr<CSSVariableData> CSSVariableReferenceValue::resolveVariableReferences(Style::BuilderState& builderState) const
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000141{
142 Vector<CSSParserToken> resolvedTokens;
darin@apple.comeb026082020-04-12 19:33:30 +0000143 if (!resolveTokenRange(m_data->tokenRange(), resolvedTokens, builderState))
justin_michaud@apple.comea578c32018-11-01 20:35:27 +0000144 return nullptr;
145
146 return CSSVariableData::create(resolvedTokens);
hyatt@apple.comfaa1d7b2016-09-13 19:08:30 +0000147}
148
149} // namespace WebCore