1/*
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
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 "CSSCustomPropertyValue.h"
28#include "CSSTokenizer.h"
29
30namespace WebCore {
31
32bool CSSCustomPropertyValue::equals(const CSSCustomPropertyValue& other) const
33{
34 if (m_name != other.m_name || m_value.index() != other.m_value.index())
35 return false;
36 return WTF::switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>& value) {
37 return value.get() == WTF::get<Ref<CSSVariableReferenceValue>>(other.m_value).get();
38 }, [&](const CSSValueID& value) {
39 return value == WTF::get<CSSValueID>(other.m_value);
40 }, [&](const Ref<CSSVariableData>& value) {
41 return value.get() == WTF::get<Ref<CSSVariableData>>(other.m_value).get();
42 }, [&](const Length& value) {
43 return value == WTF::get<Length>(other.m_value);
44 }, [&](const Ref<StyleImage>& value) {
45 return value.get() == WTF::get<Ref<StyleImage>>(other.m_value).get();
46 });
47}
48
49String CSSCustomPropertyValue::customCSSText() const
50{
51 if (!m_serialized) {
52 m_serialized = true;
53
54 WTF::switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>& value) {
55 m_stringValue = value->cssText();
56 }, [&](const CSSValueID& value) {
57 m_stringValue = getValueName(value);
58 }, [&](const Ref<CSSVariableData>& value) {
59 m_stringValue = value->tokenRange().serialize();
60 }, [&](const Length& value) {
61 m_stringValue = CSSPrimitiveValue::create(value.value(), CSSPrimitiveValue::CSS_PX)->cssText();
62 }, [&](const Ref<StyleImage>& value) {
63 m_stringValue = value->cssValue()->cssText();
64 });
65 }
66 return m_stringValue;
67}
68
69Vector<CSSParserToken> CSSCustomPropertyValue::tokens() const
70{
71 Vector<CSSParserToken> result;
72
73 WTF::switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>&) {
74 ASSERT_NOT_REACHED();
75 }, [&](const CSSValueID&) {
76 // Do nothing
77 }, [&](const Ref<CSSVariableData>& value) {
78 result.appendVector(value->tokens());
79 }, [&](const Length&) {
80 CSSTokenizer tokenizer(cssText());
81
82 auto tokenizerRange = tokenizer.tokenRange();
83 while (!tokenizerRange.atEnd())
84 result.append(tokenizerRange.consume());
85 }, [&](const Ref<StyleImage>&) {
86 CSSTokenizer tokenizer(cssText());
87
88 auto tokenizerRange = tokenizer.tokenRange();
89 while (!tokenizerRange.atEnd())
90 result.append(tokenizerRange.consume());
91 });
92
93 return result;
94}
95
96}
97