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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
* Copyright (C) 2025 Samuel Weinig <sam@webkit.org>
*
* 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,
* 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.
*/
#pragma once
#include "CSSCalcSymbolsAllowed.h"
#include "CSSCalcValue.h"
#include "CSSParserTokenRange.h"
#include "CSSPrimitiveNumericTypes.h"
#include "CSSPropertyParserOptions.h"
namespace WebCore {
struct CSSParserContext;
namespace CSSPropertyParserHelpers {
// MARK: - Generic Consumer Definition
template<typename> struct ConsumerDefinition;
inline bool shouldAcceptUnitlessValue(double value, CSSPropertyParserOptions options)
{
// FIXME: Presentational HTML attributes shouldn't use the CSS parser for lengths.
if (!value && options.unitlessZero == UnitlessZeroQuirk::Allow)
return true;
if (isUnitlessValueParsingEnabledForMode(options.parserMode))
return true;
return options.parserMode == HTMLQuirksMode && options.unitless == UnitlessQuirk::Allow;
}
// Used to check that a specialization of ConsumerDefinition exists.
struct HasConsumerDefinition {
private:
template<typename T, typename U = decltype(ConsumerDefinition<T>{})>
static constexpr bool exists(int) { return true; }
template<typename T>
static constexpr bool exists(char) { return false; }
public:
template<typename T>
static constexpr bool check() { return exists<T>(0); }
};
// FIXME: Bailing on infinity during validation does not seem to match the intent of the spec,
// though due to the use of "implementation-defined" it may still be conforming. The spec states:
//
// "When a value cannot be explicitly supported due to range/precision limitations, it must
// be converted to the closest value supported by the implementation, but how the implementation
// defines "closest" is implementation-defined as well."
//
// Angles have the additional restriction that:
//
// "If an <angle> must be converted due to exceeding the implementation-defined range of supported
// values, it must be clamped to the nearest supported multiple of 360deg."
//
// (https://drafts.csswg.org/css-values-4/#numeric-types)
//
// The infinity here is produced by the parser when a parsed number is no representable in
// as a double. A potentially more appropriate behavior would be to have the parser use
// std::numeric_limits<double>::max() instead. For angles, this would require further integration
// with the fast_float library (or whatever is currently being used to parse the number) to
// extract the correct modulo 360deg value.
// Shared validator for types dimensional types that need to canonicalize to support range
// constraints other than 0 and +/-∞.
template<typename Raw, typename F> bool isValidDimensionValue(Raw raw, F&& functor)
{
if (std::isinf(raw.value))
return false;
if constexpr (raw.range.min == -CSS::Range::infinity && raw.range.max == CSS::Range::infinity)
return true;
else if constexpr (raw.range.min == 0 && raw.range.max == CSS::Range::infinity)
return raw.value >= 0;
else if constexpr (raw.range.min == -CSS::Range::infinity && raw.range.max == 0)
return raw.value <= 0;
else
return functor();
}
// Shared validator for types that only support 0 and +/-∞ as valid range constraints.
template<typename Raw> bool isValidNonCanonicalizableDimensionValue(Raw raw)
{
if (std::isinf(raw.value))
return false;
if constexpr (raw.range.min == -CSS::Range::infinity && raw.range.max == CSS::Range::infinity)
return true;
else if constexpr (raw.range.min == 0 && raw.range.max == CSS::Range::infinity)
return raw.value >= 0;
else if constexpr (raw.range.min == -CSS::Range::infinity && raw.range.max == 0)
return raw.value <= 0;
}
// Shared validator for types that always have their value in canonical units (number, percentage, flex).
template<typename Raw> bool isValidCanonicalValue(Raw raw)
{
if (std::isinf(raw.value))
return false;
if constexpr (raw.range.min == -CSS::Range::infinity && raw.range.max == CSS::Range::infinity)
return true;
else if constexpr (raw.range.max == CSS::Range::infinity)
return raw.value >= raw.range.min;
else if constexpr (raw.range.min == -CSS::Range::infinity)
return raw.value <= raw.range.max;
else
return raw.value >= raw.range.min && raw.value <= raw.range.max;
}
// Shared clamping utility.
template<typename Raw> Raw performParseTimeClamp(Raw raw)
{
static_assert(raw.range.options != CSS::RangeOptions::Default);
if constexpr (raw.range.options == CSS::RangeOptions::ClampLower)
return { std::max<typename Raw::ResolvedValueType>(raw.value, raw.range.min) };
else if constexpr (raw.range.options == CSS::RangeOptions::ClampUpper)
return { std::min<typename Raw::ResolvedValueType>(raw.value, raw.range.max) };
else if constexpr (raw.range.options == CSS::RangeOptions::ClampBoth)
return { std::clamp<typename Raw::ResolvedValueType>(raw.value, raw.range.min, raw.range.max) };
}
// Shared consumer for `Dimension` tokens.
template<typename Primitive, typename Validator> struct DimensionConsumer {
static constexpr CSSParserTokenType tokenType = DimensionToken;
static std::optional<typename Primitive::Raw> consume(CSSParserTokenRange& range, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions options)
{
ASSERT(range.peek().type() == DimensionToken);
auto& token = range.peek();
auto validatedUnit = Validator::validate(token.unitType(), options);
if (!validatedUnit)
return std::nullopt;
auto rawValue = typename Primitive::Raw { *validatedUnit, token.numericValue() };
if constexpr (rawValue.range.options != CSS::RangeOptions::Default)
rawValue = performParseTimeClamp(rawValue);
if (!Validator::isValid(rawValue, options))
return std::nullopt;
range.consumeIncludingWhitespace();
return rawValue;
}
};
// Shared consumer for `Percentage` tokens.
template<typename Primitive, typename Validator> struct PercentageConsumer {
static constexpr CSSParserTokenType tokenType = PercentageToken;
static std::optional<typename Primitive::Raw> consume(CSSParserTokenRange& range, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions options)
{
ASSERT(range.peek().type() == PercentageToken);
auto rawValue = typename Primitive::Raw { CSS::PercentageUnit::Percentage, range.peek().numericValue() };
if constexpr (rawValue.range.options != CSS::RangeOptions::Default)
rawValue = performParseTimeClamp(rawValue);
if (!Validator::isValid(rawValue, options))
return std::nullopt;
range.consumeIncludingWhitespace();
return rawValue;
}
};
// Shared consumer for `Number` tokens.
template<typename Primitive, typename Validator> struct NumberConsumer {
static constexpr CSSParserTokenType tokenType = NumberToken;
static std::optional<typename Primitive::Raw> consume(CSSParserTokenRange& range, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions options)
{
ASSERT(range.peek().type() == NumberToken);
auto rawValue = typename Primitive::Raw { CSS::NumberUnit::Number, range.peek().numericValue() };
if constexpr (rawValue.range.options != CSS::RangeOptions::Default)
rawValue = performParseTimeClamp(rawValue);
if (!Validator::isValid(rawValue, options))
return std::nullopt;
range.consumeIncludingWhitespace();
return rawValue;
}
};
// Shared consumer for `Number` tokens for use by dimensional primitives that support "unitless" values.
template<typename Primitive, typename Validator, auto unit> struct NumberConsumerForUnitlessValues {
static constexpr CSSParserTokenType tokenType = NumberToken;
static std::optional<typename Primitive::Raw> consume(CSSParserTokenRange& range, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions options)
{
ASSERT(range.peek().type() == NumberToken);
auto numericValue = range.peek().numericValue();
if (!shouldAcceptUnitlessValue(numericValue, options))
return std::nullopt;
auto rawValue = typename Primitive::Raw { unit, numericValue };
if constexpr (rawValue.range.options != CSS::RangeOptions::Default)
rawValue = performParseTimeClamp(rawValue);
if (!Validator::isValid(rawValue, options))
return std::nullopt;
range.consumeIncludingWhitespace();
return rawValue;
}
};
// Shared consumer for `Function` tokens that processes `calc()` for the provided primitive.
template<typename Primitive> struct FunctionConsumerForCalcValues {
static constexpr CSSParserTokenType tokenType = FunctionToken;
static std::optional<typename Primitive::Calc> consume(CSSParserTokenRange& range, const CSSParserContext& context, CSSCalcSymbolsAllowed symbolsAllowed, CSSPropertyParserOptions options)
{
ASSERT(range.peek().type() == FunctionToken);
auto rangeCopy = range;
if (RefPtr value = CSSCalcValue::parse(rangeCopy, context, Primitive::category, Primitive::range, WTFMove(symbolsAllowed), options)) {
range = rangeCopy;
return {{ value.releaseNonNull() }};
}
return std::nullopt;
}
};
template<typename T> struct KeywordConsumer {
static constexpr CSSParserTokenType tokenType = IdentToken;
static std::optional<T> consume(CSSParserTokenRange& range, const CSSParserContext&, CSSCalcSymbolsAllowed, CSSPropertyParserOptions)
{
ASSERT(range.peek().type() == IdentToken);
if (range.peek().id() == T::value) {
range.consumeIncludingWhitespace();
return T { };
}
return std::nullopt;
}
};
} // namespace CSSPropertyParserHelpers
} // namespace WebCore
|