| 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 "CSSVariableReferenceValue.h" |
| 32 | |
| 33 | #include "RenderStyle.h" |
| 34 | #include "StyleResolver.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | String CSSVariableReferenceValue::customCSSText() const |
| 39 | { |
| 40 | if (!m_serialized) { |
| 41 | m_serialized = true; |
| 42 | m_stringValue = m_data->tokenRange().serialize(); |
| 43 | } |
| 44 | return m_stringValue; |
| 45 | } |
| 46 | |
| 47 | static bool resolveTokenRange(CSSParserTokenRange, Vector<CSSParserToken>&, ApplyCascadedPropertyState&); |
| 48 | |
| 49 | static bool resolveVariableFallback(CSSParserTokenRange range, Vector<CSSParserToken>& result, ApplyCascadedPropertyState& state) |
| 50 | { |
| 51 | if (range.atEnd()) |
| 52 | return false; |
| 53 | ASSERT(range.peek().type() == CommaToken); |
| 54 | range.consume(); |
| 55 | return resolveTokenRange(range, result, state); |
| 56 | } |
| 57 | |
| 58 | static bool resolveVariableReference(CSSParserTokenRange range, Vector<CSSParserToken>& result, ApplyCascadedPropertyState& state) |
| 59 | { |
| 60 | auto& registeredProperties = state.styleResolver->document().getCSSRegisteredCustomPropertySet(); |
| 61 | auto& style = *state.styleResolver->style(); |
| 62 | |
| 63 | range.consumeWhitespace(); |
| 64 | ASSERT(range.peek().type() == IdentToken); |
| 65 | String variableName = range.consumeIncludingWhitespace().value().toString(); |
| 66 | ASSERT(range.atEnd() || (range.peek().type() == CommaToken)); |
| 67 | |
| 68 | // Apply this variable first, in case it is still unresolved |
| 69 | state.styleResolver->applyCascadedCustomProperty(variableName, state); |
| 70 | |
| 71 | // Apply fallback to detect cycles |
| 72 | Vector<CSSParserToken> fallbackResult; |
| 73 | bool fallbackReturn = resolveVariableFallback(CSSParserTokenRange(range), fallbackResult, state); |
| 74 | |
| 75 | auto* property = style.getCustomProperty(variableName); |
| 76 | |
| 77 | if (!property || property->isUnset()) { |
| 78 | auto* registered = registeredProperties.get(variableName); |
| 79 | if (registered && registered->initialValue()) |
| 80 | property = registered->initialValue(); |
| 81 | } |
| 82 | |
| 83 | if (!property || property->isInvalid()) { |
| 84 | if (fallbackReturn) |
| 85 | result.appendVector(fallbackResult); |
| 86 | return fallbackReturn; |
| 87 | } |
| 88 | |
| 89 | ASSERT(property->isResolved()); |
| 90 | result.appendVector(property->tokens()); |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | static bool resolveTokenRange(CSSParserTokenRange range, Vector<CSSParserToken>& result, ApplyCascadedPropertyState& state) |
| 96 | { |
| 97 | bool success = true; |
| 98 | while (!range.atEnd()) { |
| 99 | if (range.peek().functionId() == CSSValueVar || range.peek().functionId() == CSSValueEnv) |
| 100 | success &= resolveVariableReference(range.consumeBlock(), result, state); |
| 101 | else |
| 102 | result.append(range.consume()); |
| 103 | } |
| 104 | return success; |
| 105 | } |
| 106 | |
| 107 | RefPtr<CSSVariableData> CSSVariableReferenceValue::resolveVariableReferences(ApplyCascadedPropertyState& state) const |
| 108 | { |
| 109 | Vector<CSSParserToken> resolvedTokens; |
| 110 | CSSParserTokenRange range = m_data->tokenRange(); |
| 111 | |
| 112 | if (!resolveTokenRange(range, resolvedTokens, state)) |
| 113 | return nullptr; |
| 114 | |
| 115 | return CSSVariableData::create(resolvedTokens); |
| 116 | } |
| 117 | |
| 118 | } // namespace WebCore |
| 119 | |