hyatt@apple.com | faa1d7b | 2016-09-13 19:08:30 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
darin@apple.com | 61b455d | 2021-09-14 19:59:52 +0000 | [diff] [blame] | 2 | // Copyright (C) 2016-2021 Apple Inc. All rights reserved. |
hyatt@apple.com | faa1d7b | 2016-09-13 19:08:30 +0000 | [diff] [blame] | 3 | // |
| 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.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 33 | #include "CSSVariableData.h" |
emilio@crisal.io | 1448584 | 2020-04-19 18:01:18 +0000 | [diff] [blame] | 34 | #include "ConstantPropertyMap.h" |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 35 | #include "RenderStyle.h" |
antti@apple.com | 3c83b83 | 2019-10-31 18:55:34 +0000 | [diff] [blame] | 36 | #include "StyleBuilder.h" |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 37 | #include "StyleResolver.h" |
| 38 | |
hyatt@apple.com | faa1d7b | 2016-09-13 19:08:30 +0000 | [diff] [blame] | 39 | namespace WebCore { |
| 40 | |
darin@apple.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 41 | static bool resolveTokenRange(CSSParserTokenRange, Vector<CSSParserToken>&, Style::BuilderState&); |
| 42 | |
darin@apple.com | 61b455d | 2021-09-14 19:59:52 +0000 | [diff] [blame] | 43 | CSSVariableReferenceValue::CSSVariableReferenceValue(Ref<CSSVariableData>&& data, const CSSParserContext& context) |
darin@apple.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 44 | : CSSValue(VariableReferenceClass) |
| 45 | , m_data(WTFMove(data)) |
darin@apple.com | 61b455d | 2021-09-14 19:59:52 +0000 | [diff] [blame] | 46 | , m_context(context) |
hyatt@apple.com | faa1d7b | 2016-09-13 19:08:30 +0000 | [diff] [blame] | 47 | { |
hyatt@apple.com | 9e09f63 | 2016-10-27 22:05:01 +0000 | [diff] [blame] | 48 | } |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 49 | |
darin@apple.com | 61b455d | 2021-09-14 19:59:52 +0000 | [diff] [blame] | 50 | Ref<CSSVariableReferenceValue> CSSVariableReferenceValue::create(const CSSParserTokenRange& range, const CSSParserContext& context) |
darin@apple.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 51 | { |
darin@apple.com | 61b455d | 2021-09-14 19:59:52 +0000 | [diff] [blame] | 52 | return adoptRef(*new CSSVariableReferenceValue(CSSVariableData::create(range), context)); |
darin@apple.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 53 | } |
| 54 | |
achristensen@apple.com | e7dbfd3 | 2021-09-20 17:41:00 +0000 | [diff] [blame] | 55 | Ref<CSSVariableReferenceValue> CSSVariableReferenceValue::create(Ref<CSSVariableData>&& data, const CSSParserContext& context) |
| 56 | { |
| 57 | return adoptRef(*new CSSVariableReferenceValue(WTFMove(data), context)); |
| 58 | } |
| 59 | |
darin@apple.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 60 | bool CSSVariableReferenceValue::equals(const CSSVariableReferenceValue& other) const |
| 61 | { |
| 62 | return m_data.get() == other.m_data.get(); |
| 63 | } |
| 64 | |
| 65 | String CSSVariableReferenceValue::customCSSText() const |
| 66 | { |
| 67 | if (m_stringValue.isNull()) |
| 68 | m_stringValue = m_data->tokenRange().serialize(); |
| 69 | return m_stringValue; |
| 70 | } |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 71 | |
antti@apple.com | 60abb7b | 2019-10-30 20:13:30 +0000 | [diff] [blame] | 72 | static bool resolveVariableFallback(CSSParserTokenRange range, Vector<CSSParserToken>& result, Style::BuilderState& builderState) |
hyatt@apple.com | 9e09f63 | 2016-10-27 22:05:01 +0000 | [diff] [blame] | 73 | { |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 74 | if (range.atEnd()) |
| 75 | return false; |
| 76 | ASSERT(range.peek().type() == CommaToken); |
darin@apple.com | a073f72 | 2021-06-29 01:34:20 +0000 | [diff] [blame] | 77 | range.consumeIncludingWhitespace(); |
antti@apple.com | 60abb7b | 2019-10-30 20:13:30 +0000 | [diff] [blame] | 78 | return resolveTokenRange(range, result, builderState); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
emilio@crisal.io | 1448584 | 2020-04-19 18:01:18 +0000 | [diff] [blame] | 81 | static bool resolveVariableReference(CSSParserTokenRange range, CSSValueID functionId, Vector<CSSParserToken>& result, Style::BuilderState& builderState) |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 82 | { |
emilio@crisal.io | 1448584 | 2020-04-19 18:01:18 +0000 | [diff] [blame] | 83 | ASSERT(functionId == CSSValueVar || functionId == CSSValueEnv); |
| 84 | |
antti@apple.com | 60abb7b | 2019-10-30 20:13:30 +0000 | [diff] [blame] | 85 | auto& registeredProperties = builderState.document().getCSSRegisteredCustomPropertySet(); |
| 86 | auto& style = builderState.style(); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 87 | |
| 88 | range.consumeWhitespace(); |
| 89 | ASSERT(range.peek().type() == IdentToken); |
cdumez@apple.com | 30939fd | 2022-04-28 04:50:39 +0000 | [diff] [blame] | 90 | auto variableName = range.consumeIncludingWhitespace().value().toAtomString(); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 91 | ASSERT(range.atEnd() || (range.peek().type() == CommaToken)); |
| 92 | |
| 93 | // Apply this variable first, in case it is still unresolved |
antti@apple.com | 3c83b83 | 2019-10-31 18:55:34 +0000 | [diff] [blame] | 94 | builderState.builder().applyCustomProperty(variableName); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 95 | |
| 96 | // Apply fallback to detect cycles |
| 97 | Vector<CSSParserToken> fallbackResult; |
antti@apple.com | 60abb7b | 2019-10-30 20:13:30 +0000 | [diff] [blame] | 98 | bool fallbackReturn = resolveVariableFallback(CSSParserTokenRange(range), fallbackResult, builderState); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 99 | |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 100 | |
emilio@crisal.io | 1448584 | 2020-04-19 18:01:18 +0000 | [diff] [blame] | 101 | auto* property = functionId == CSSValueVar |
| 102 | ? style.getCustomProperty(variableName) |
| 103 | : builderState.document().constantProperties().values().get(variableName); |
justin_michaud@apple.com | b863bdf | 2018-12-19 03:21:15 +0000 | [diff] [blame] | 104 | if (!property || property->isUnset()) { |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 105 | auto* registered = registeredProperties.get(variableName); |
| 106 | if (registered && registered->initialValue()) |
| 107 | property = registered->initialValue(); |
justin_michaud@apple.com | b863bdf | 2018-12-19 03:21:15 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (!property || property->isInvalid()) { |
commit-queue@webkit.org | c1cc2af | 2020-09-13 06:39:34 +0000 | [diff] [blame] | 111 | if (fallbackResult.size() > CSSVariableReferenceValue::maxSubstitutionTokens) |
| 112 | return false; |
| 113 | |
justin_michaud@apple.com | b863bdf | 2018-12-19 03:21:15 +0000 | [diff] [blame] | 114 | if (fallbackReturn) |
| 115 | result.appendVector(fallbackResult); |
| 116 | return fallbackReturn; |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | ASSERT(property->isResolved()); |
commit-queue@webkit.org | c1cc2af | 2020-09-13 06:39:34 +0000 | [diff] [blame] | 120 | if (property->tokens().size() > CSSVariableReferenceValue::maxSubstitutionTokens) |
| 121 | return false; |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 122 | |
commit-queue@webkit.org | c1cc2af | 2020-09-13 06:39:34 +0000 | [diff] [blame] | 123 | result.appendVector(property->tokens()); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
antti@apple.com | 60abb7b | 2019-10-30 20:13:30 +0000 | [diff] [blame] | 127 | static bool resolveTokenRange(CSSParserTokenRange range, Vector<CSSParserToken>& result, Style::BuilderState& builderState) |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 128 | { |
| 129 | bool success = true; |
| 130 | while (!range.atEnd()) { |
emilio@crisal.io | 1448584 | 2020-04-19 18:01:18 +0000 | [diff] [blame] | 131 | auto functionId = range.peek().functionId(); |
| 132 | if (functionId == CSSValueVar || functionId == CSSValueEnv) |
| 133 | success &= resolveVariableReference(range.consumeBlock(), functionId, result, builderState); |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 134 | else |
| 135 | result.append(range.consume()); |
| 136 | } |
| 137 | return success; |
| 138 | } |
| 139 | |
antti@apple.com | 60abb7b | 2019-10-30 20:13:30 +0000 | [diff] [blame] | 140 | RefPtr<CSSVariableData> CSSVariableReferenceValue::resolveVariableReferences(Style::BuilderState& builderState) const |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 141 | { |
| 142 | Vector<CSSParserToken> resolvedTokens; |
darin@apple.com | eb02608 | 2020-04-12 19:33:30 +0000 | [diff] [blame] | 143 | if (!resolveTokenRange(m_data->tokenRange(), resolvedTokens, builderState)) |
justin_michaud@apple.com | ea578c3 | 2018-11-01 20:35:27 +0000 | [diff] [blame] | 144 | return nullptr; |
| 145 | |
| 146 | return CSSVariableData::create(resolvedTokens); |
hyatt@apple.com | faa1d7b | 2016-09-13 19:08:30 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } // namespace WebCore |