Properly determine if css custom property values are computationally independent
[WebKit-https.git] / Source / WebCore / css / CSSVariableData.cpp
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Copyright (C) 2016 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 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 "CSSVariableData.h"
32
33 #include "CSSCustomPropertyValue.h"
34 #include "CSSParserTokenRange.h"
35 #include "CSSValuePool.h"
36 #include "RenderStyle.h"
37 #include <wtf/text/AtomicStringHash.h>
38 #include <wtf/text/StringBuilder.h>
39 #include <wtf/text/StringView.h>
40
41 namespace WebCore {
42
43 template<typename CharacterType> void CSSVariableData::updateTokens(const CSSParserTokenRange& range)
44 {
45     const CharacterType* currentOffset = m_backingString.characters<CharacterType>();
46     for (const CSSParserToken& token : range) {
47         if (token.hasStringBacking()) {
48             unsigned length = token.value().length();
49             StringView string(currentOffset, length);
50             m_tokens.append(token.copyWithUpdatedString(string));
51             currentOffset += length;
52         } else
53             m_tokens.append(token);
54     }
55     ASSERT(currentOffset == m_backingString.characters<CharacterType>() + m_backingString.length());
56 }
57
58 bool CSSVariableData::operator==(const CSSVariableData& other) const
59 {
60     return tokens() == other.tokens();
61 }
62
63 void CSSVariableData::consumeAndUpdateTokens(const CSSParserTokenRange& range)
64 {
65     StringBuilder stringBuilder;
66     CSSParserTokenRange localRange = range;
67
68     while (!localRange.atEnd()) {
69         CSSParserToken token = localRange.consume();
70         if (token.hasStringBacking())
71             stringBuilder.append(token.value());
72     }
73     m_backingString = stringBuilder.toString();
74     if (m_backingString.is8Bit())
75         updateTokens<LChar>(range);
76     else
77         updateTokens<UChar>(range);
78 }
79
80 CSSVariableData::CSSVariableData(const CSSParserTokenRange& range, bool needsVariableResolution)
81     : m_needsVariableResolution(needsVariableResolution)
82 {
83     consumeAndUpdateTokens(range);
84 }
85
86 bool CSSVariableData::checkVariablesForCycles(const AtomicString& name, const RenderStyle& style, HashSet<AtomicString>& seenProperties, HashSet<AtomicString>& invalidProperties) const
87 {
88     if (invalidProperties.contains(name))
89         return false;
90     
91     HashSet<AtomicString> newSeenProperties = seenProperties;
92     newSeenProperties.add(name);
93     
94     bool valid = checkVariablesForCyclesWithRange(m_tokens, style, newSeenProperties, invalidProperties);
95     if (!valid)
96         invalidProperties.add(name);
97     
98     return valid;
99 }
100     
101 bool CSSVariableData::checkVariablesForCyclesWithRange(CSSParserTokenRange range, const RenderStyle& style, HashSet<AtomicString>& seenProperties, HashSet<AtomicString>& invalidProperties) const
102 {
103     while (!range.atEnd()) {
104         if (range.peek().functionId() == CSSValueVar || range.peek().functionId() == CSSValueEnv) {
105             CSSParserTokenRange block = range.consumeBlock();
106             
107             block.consumeWhitespace();
108             ASSERT(block.peek().type() == IdentToken);
109             AtomicString variableName = block.consumeIncludingWhitespace().value().toAtomicString();
110             ASSERT(block.atEnd() || block.peek().type() == CommaToken);
111             if (seenProperties.contains(variableName))
112                 return false;
113
114             auto* value = style.getCustomProperty(variableName);
115             if (value && value->containsVariables() && !value->checkVariablesForCycles(variableName, style, seenProperties, invalidProperties))
116                 return false;
117
118             if (range.peek().type() == CommaToken) {
119                 // Fallback.
120                 range.consume();
121                 return checkVariablesForCyclesWithRange(block, style, seenProperties, invalidProperties);
122             }
123         } else
124             range.consume();
125     }
126     return true;
127 }
128
129 bool CSSVariableData::resolveVariableFallback(const CSSRegisteredCustomPropertySet& registeredProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result, const RenderStyle& style) const
130 {
131     if (range.atEnd())
132         return false;
133     ASSERT(range.peek().type() == CommaToken);
134     range.consume();
135     return resolveTokenRange(registeredProperties, range, result, style);
136 }
137
138 bool CSSVariableData::resolveVariableReference(const CSSRegisteredCustomPropertySet& registeredProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result, const RenderStyle& style) const
139 {
140     range.consumeWhitespace();
141     ASSERT(range.peek().type() == IdentToken);
142     AtomicString variableName = range.consumeIncludingWhitespace().value().toAtomicString();
143     ASSERT(range.atEnd() || (range.peek().type() == CommaToken));
144     
145     auto* property = style.getCustomProperty(variableName);
146     if (property && property->resolvedTypedValue()) {
147         result.appendVector(property->tokens(registeredProperties, style));
148         return true;
149     }
150
151     if (!property || !property->value()) {
152         auto* registered = registeredProperties.get(variableName);
153         if (registered && registered->initialValue())
154             property = registered->initialValue();
155         else
156             return resolveVariableFallback(registeredProperties, range, result, style);
157     }
158     ASSERT(property);
159     result.appendVector(property->tokens(registeredProperties, style));
160     
161     return true;
162 }
163
164 RefPtr<CSSVariableData> CSSVariableData::resolveVariableReferences(const CSSRegisteredCustomPropertySet& registeredProperties, const RenderStyle& style) const
165 {
166     Vector<CSSParserToken> resolvedTokens;
167     CSSParserTokenRange range = m_tokens;
168     if (!resolveTokenRange(registeredProperties, range, resolvedTokens, style))
169         return nullptr;
170     return CSSVariableData::createResolved(resolvedTokens, *this);
171 }
172     
173 bool CSSVariableData::resolveTokenRange(const CSSRegisteredCustomPropertySet& registeredProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result, const RenderStyle& style) const
174 {
175     bool success = true;
176     while (!range.atEnd()) {
177         if (range.peek().functionId() == CSSValueVar || range.peek().functionId() == CSSValueEnv)
178             success &= resolveVariableReference(registeredProperties, range.consumeBlock(), result, style);
179         else
180             result.append(range.consume());
181     }
182     return success;
183 }
184
185 } // namespace WebCore