source: webkit/trunk/Source/WebCore/css/CSSCustomPropertyValue.cpp

Last change on this file was 285714, checked in by [email protected], 4 years ago

Re-use isCSSWideKeyword in CSSCustomPropertyValue::createWithID
https://bugs.webkit.org/show_bug.cgi?id=233035

Reviewed by Antti Koivisto.

  • css/CSSCustomPropertyValue.cpp:

(WebCore::CSSCustomPropertyValue::createWithID):

  • css/CSSCustomPropertyValue.h:
File size: 4.1 KB
Line 
1/*
2 * Copyright (C) 2016-2021 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
29#include "CSSParserIdioms.h"
30#include "CSSTokenizer.h"
31
32namespace WebCore {
33
34Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createEmpty(const AtomString& name)
35{
36 return adoptRef(*new CSSCustomPropertyValue(name, std::monostate { }));
37}
38
39Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createWithID(const AtomString& name, CSSValueID id)
40{
41 ASSERT(WebCore::isCSSWideKeyword(id) || id == CSSValueInvalid);
42 return adoptRef(*new CSSCustomPropertyValue(name, { id }));
43}
44
45bool CSSCustomPropertyValue::equals(const CSSCustomPropertyValue& other) const
46{
47 if (m_name != other.m_name || m_value.index() != other.m_value.index())
48 return false;
49 return WTF::switchOn(m_value, [&](const std::monostate&) {
50 return true;
51 }, [&](const Ref<CSSVariableReferenceValue>& value) {
52 return value.get() == std::get<Ref<CSSVariableReferenceValue>>(other.m_value).get();
53 }, [&](const CSSValueID& value) {
54 return value == std::get<CSSValueID>(other.m_value);
55 }, [&](const Ref<CSSVariableData>& value) {
56 return value.get() == std::get<Ref<CSSVariableData>>(other.m_value).get();
57 }, [&](const Length& value) {
58 return value == std::get<Length>(other.m_value);
59 }, [&](const Ref<StyleImage>& value) {
60 return value.get() == std::get<Ref<StyleImage>>(other.m_value).get();
61 });
62}
63
64String CSSCustomPropertyValue::customCSSText() const
65{
66 if (m_stringValue.isNull()) {
67 WTF::switchOn(m_value, [&](const std::monostate&) {
68 m_stringValue = emptyString();
69 }, [&](const Ref<CSSVariableReferenceValue>& value) {
70 m_stringValue = value->cssText();
71 }, [&](const CSSValueID& value) {
72 m_stringValue = getValueName(value);
73 }, [&](const Ref<CSSVariableData>& value) {
74 m_stringValue = value->tokenRange().serialize();
75 }, [&](const Length& value) {
76 m_stringValue = CSSPrimitiveValue::create(value.value(), CSSUnitType::CSS_PX)->cssText();
77 }, [&](const Ref<StyleImage>& value) {
78 m_stringValue = value->cssValue()->cssText();
79 });
80 }
81 return m_stringValue;
82}
83
84Vector<CSSParserToken> CSSCustomPropertyValue::tokens() const
85{
86 Vector<CSSParserToken> result;
87 WTF::switchOn(m_value, [&](const std::monostate&) {
88 // Do nothing.
89 }, [&](const Ref<CSSVariableReferenceValue>&) {
90 ASSERT_NOT_REACHED();
91 }, [&](const CSSValueID&) {
92 // Do nothing.
93 }, [&](const Ref<CSSVariableData>& value) {
94 result.appendVector(value->tokens());
95 }, [&](auto&) {
96 CSSTokenizer tokenizer(customCSSText());
97 auto tokenizerRange = tokenizer.tokenRange();
98 while (!tokenizerRange.atEnd())
99 result.append(tokenizerRange.consume());
100 });
101 return result;
102}
103
104}
Note: See TracBrowser for help on using the repository browser.