1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*
* Copyright (C) 2016-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CSSCustomPropertyValue.h"
#include "CSSCalcValue.h"
#include "CSSFunctionValue.h"
#include "CSSMarkup.h"
#include "CSSParserIdioms.h"
#include "CSSSerializationContext.h"
#include "CSSTokenizer.h"
#include "ComputedStyleExtractor.h"
#include "RenderStyle.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createEmpty(const AtomString& name)
{
static NeverDestroyed<Ref<CSSVariableData>> empty { CSSVariableData::create({ }) };
return createSyntaxAll(name, Ref { empty.get() });
}
Ref<CSSCustomPropertyValue> CSSCustomPropertyValue::createWithID(const AtomString& name, CSSValueID id)
{
ASSERT(WebCore::isCSSWideKeyword(id) || id == CSSValueInvalid);
return adoptRef(*new CSSCustomPropertyValue(name, { id }));
}
bool CSSCustomPropertyValue::equals(const CSSCustomPropertyValue& other) const
{
if (m_name != other.m_name || m_value.index() != other.m_value.index())
return false;
return WTF::switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>& value) {
auto& otherValue = std::get<Ref<CSSVariableReferenceValue>>(other.m_value);
return value.ptr() == otherValue.ptr() || value.get() == otherValue.get();
}, [&](const CSSValueID& value) {
return value == std::get<CSSValueID>(other.m_value);
}, [&](const Ref<CSSVariableData>& value) {
auto& otherValue = std::get<Ref<CSSVariableData>>(other.m_value);
return value.ptr() == otherValue.ptr() || value.get() == otherValue.get();
}, [&](const SyntaxValue& value) {
return value == std::get<SyntaxValue>(other.m_value);
}, [&](const SyntaxValueList& value) {
return value == std::get<SyntaxValueList>(other.m_value);
});
}
String CSSCustomPropertyValue::customCSSText(const CSS::SerializationContext& context) const
{
auto serializeSyntaxValue = [&](const SyntaxValue& syntaxValue) -> String {
return WTF::switchOn(syntaxValue, [&](const Length& value) {
if (value.type() == LengthType::Calculated) {
// FIXME: Implement serialization for CalculationValue directly.
auto calcValue = CSSCalcValue::create(value.calculationValue(), RenderStyle::defaultStyle());
return calcValue->cssText(context);
}
return CSSPrimitiveValue::create(value, RenderStyle::defaultStyle())->cssText(context);
}, [&](const NumericSyntaxValue& value) {
return CSSPrimitiveValue::create(value.value, value.unitType)->cssText(context);
}, [&](const Style::Color& value) {
return serializationForCSS(context, value);
}, [&](const RefPtr<StyleImage>& value) {
// FIXME: This is not right for gradients that use `currentcolor`. There should be a way preserve it.
return value->computedStyleValue(RenderStyle::defaultStyle())->cssText(context);
}, [&](const URL& value) {
return serializeURL(value.string());
}, [&](const String& value) {
return value;
}, [&](const TransformSyntaxValue& value) {
auto cssValue = transformOperationAsCSSValue(value.transform, RenderStyle::defaultStyle());
if (!cssValue)
return emptyString();
return cssValue->cssText(context);
});
};
auto serialize = [&] {
return WTF::switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>& value) {
return value->cssText(context);
}, [&](const CSSValueID& value) {
return nameString(value).string();
}, [&](const Ref<CSSVariableData>& value) {
return value->serialize();
}, [&](const SyntaxValue& syntaxValue) {
return serializeSyntaxValue(syntaxValue);
}, [&](const SyntaxValueList& syntaxValueList) {
StringBuilder builder;
auto separator = separatorCSSText(syntaxValueList.separator);
for (auto& syntaxValue : syntaxValueList.values) {
if (!builder.isEmpty())
builder.append(separator);
builder.append(serializeSyntaxValue(syntaxValue));
}
return builder.toString();
});
};
if (m_cachedCSSText.isNull())
m_cachedCSSText = serialize();
return m_cachedCSSText;
}
const Vector<CSSParserToken>& CSSCustomPropertyValue::tokens() const
{
static NeverDestroyed<Vector<CSSParserToken>> emptyTokens;
return WTF::switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>&) -> const Vector<CSSParserToken>& {
ASSERT_NOT_REACHED();
return emptyTokens;
}, [&](const CSSValueID&) -> const Vector<CSSParserToken>& {
// Do nothing.
return emptyTokens;
}, [&](const Ref<CSSVariableData>& value) -> const Vector<CSSParserToken>& {
return value->tokens();
}, [&](auto&) -> const Vector<CSSParserToken>& {
if (!m_cachedTokens) {
CSSTokenizer tokenizer { customCSSText(CSS::defaultSerializationContext()) };
m_cachedTokens = CSSVariableData::create(tokenizer.tokenRange());
}
return m_cachedTokens->tokens();
});
}
bool CSSCustomPropertyValue::containsCSSWideKeyword() const
{
return std::holds_alternative<CSSValueID>(m_value) && WebCore::isCSSWideKeyword(std::get<CSSValueID>(m_value));
}
Ref<const CSSVariableData> CSSCustomPropertyValue::asVariableData() const
{
return WTF::switchOn(m_value, [&](const Ref<CSSVariableData>& value) -> Ref<const CSSVariableData> {
return value.get();
}, [&](const Ref<CSSVariableReferenceValue>& value) -> Ref<const CSSVariableData> {
return value->data();
}, [&](auto&) -> Ref<const CSSVariableData> {
return CSSVariableData::create(tokens());
});
}
bool CSSCustomPropertyValue::isCurrentColor() const
{
// FIXME: Registered properties?
auto tokenRange = switchOn(m_value, [&](const Ref<CSSVariableReferenceValue>& variableReferenceValue) {
return variableReferenceValue->data().tokenRange();
}, [&](const Ref<CSSVariableData>& data) {
return data->tokenRange();
}, [&](auto&) {
return CSSParserTokenRange { };
});
if (tokenRange.atEnd())
return false;
auto token = tokenRange.consumeIncludingWhitespace();
if (!tokenRange.atEnd())
return false;
// FIXME: This should probably check all tokens.
return token.id() == CSSValueCurrentcolor;
}
bool CSSCustomPropertyValue::isAnimatable() const
{
return std::holds_alternative<SyntaxValue>(m_value) || std::holds_alternative<SyntaxValueList>(m_value);
}
static bool mayDependOnBaseURL(const CSSCustomPropertyValue::SyntaxValue& syntaxValue)
{
return WTF::switchOn(syntaxValue,
[](const Length&) {
return false;
},
[](const CSSCustomPropertyValue::NumericSyntaxValue&) {
return false;
},
[](const Style::Color&) {
return false;
},
[](const RefPtr<StyleImage>&) {
return true;
},
[](const URL&) {
return true;
},
[](const String&) {
return false;
},
[](const CSSCustomPropertyValue::TransformSyntaxValue&) {
return false;
});
}
bool CSSCustomPropertyValue::customMayDependOnBaseURL() const
{
return WTF::switchOn(m_value,
[](const Ref<CSSVariableReferenceValue>&) {
return false;
},
[](const CSSValueID&) {
return false;
},
[](const Ref<CSSVariableData>&) {
return false;
},
[](const SyntaxValue& syntaxValue) {
return WebCore::mayDependOnBaseURL(syntaxValue);
},
[](const SyntaxValueList& syntaxValueList) {
for (auto& syntaxValue : syntaxValueList.values) {
if (WebCore::mayDependOnBaseURL(syntaxValue))
return true;
}
return false;
});
}
}
|