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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004-2023 Apple Inc. All rights reserved.
* Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "CSSAttrValue.h"
#include "CSSPrimitiveNumericUnits.h"
#include "CSSPropertyNames.h"
#include "CSSValue.h"
#include "CSSValueKeywords.h"
#include "ExceptionOr.h"
#include "LayoutUnit.h"
#include <utility>
#include <wtf/Forward.h>
#include <wtf/MathExtras.h>
namespace WebCore {
class CSSCalcValue;
class CSSToLengthConversionData;
class FontCascade;
class RenderStyle;
class RenderView;
struct Length;
// Max/min values for CSS, needs to slightly smaller/larger than the true max/min values to allow for rounding without overflowing.
// Subtract two (rather than one) to allow for values to be converted to float and back without exceeding the LayoutUnit::max.
constexpr float maxValueForCssLength = static_cast<float>(intMaxForLayoutUnit - 2);
constexpr float minValueForCssLength = static_cast<float>(intMinForLayoutUnit + 2);
// Dimension calculations are imprecise, often resulting in values of e.g.
// 44.99998. We need to round if we're really close to the next integer value.
template<typename T> inline T roundForImpreciseConversion(double value)
{
value += (value < 0) ? -0.01 : +0.01;
return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
}
template<> inline float roundForImpreciseConversion(double value)
{
double ceiledValue = ceil(value);
double proximityToNextInt = ceiledValue - value;
if (proximityToNextInt <= 0.01 && value > 0)
return static_cast<float>(ceiledValue);
if (proximityToNextInt >= 0.99 && value < 0)
return static_cast<float>(floor(value));
return static_cast<float>(value);
}
class CSSPrimitiveValue final : public CSSValue {
public:
static constexpr bool isLength(CSSUnitType);
template<CSS::AngleUnit, typename T = double> static T computeAngle(CSSUnitType, T angle);
template<CSS::TimeUnit, typename T = double> static T computeTime(CSSUnitType, T time);
template<CSS::FrequencyUnit, typename T = double> static T computeFrequency(CSSUnitType, T frequency);
template<CSS::ResolutionUnit, typename T = double> static T computeResolution(CSSUnitType, T resolution);
// FIXME: Some of these use primitiveUnitType() and some use primitiveType(). Many that use primitiveUnitType() are likely broken with calc().
bool isAngle() const { return unitCategory(primitiveType()) == CSSUnitCategory::Angle; }
bool isAttr() const { return primitiveUnitType() == CSSUnitType::CSS_ATTR; }
bool isFontIndependentLength() const { return isFontIndependentLength(primitiveUnitType()); }
bool isFontRelativeLength() const { return isFontRelativeLength(primitiveUnitType()); }
bool isParentFontRelativeLength() const { return isPercentage() || (isFontRelativeLength() && !isRootFontRelativeLength()); }
bool isRootFontRelativeLength() const { return isRootFontRelativeLength(primitiveUnitType()); }
bool isQuirkyEms() const { return primitiveType() == CSSUnitType::CSS_QUIRKY_EM; }
bool isLength() const { return isLength(static_cast<CSSUnitType>(primitiveType())); }
bool isNumber() const { return primitiveType() == CSSUnitType::CSS_NUMBER; }
bool isInteger() const { return primitiveType() == CSSUnitType::CSS_INTEGER; }
bool isNumberOrInteger() const { return isNumber() || isInteger(); }
bool isPercentage() const { return primitiveType() == CSSUnitType::CSS_PERCENTAGE; }
bool isPx() const { return primitiveType() == CSSUnitType::CSS_PX; }
bool isTime() const { return unitCategory(primitiveType()) == CSSUnitCategory::Time; }
bool isFrequency() const { return unitCategory(primitiveType()) == CSSUnitCategory::Frequency; }
bool isCalculated() const { return primitiveUnitType() == CSSUnitType::CSS_CALC; }
bool isCalculatedPercentageWithLength() const { return primitiveType() == CSSUnitType::CSS_CALC_PERCENTAGE_WITH_LENGTH; }
bool isDotsPerInch() const { return primitiveType() == CSSUnitType::CSS_DPI; }
bool isDotsPerPixel() const { return primitiveType() == CSSUnitType::CSS_DPPX; }
bool isDotsPerCentimeter() const { return primitiveType() == CSSUnitType::CSS_DPCM; }
bool isX() const { return primitiveType() == CSSUnitType::CSS_X; }
bool isResolution() const { return unitCategory(primitiveType()) == CSSUnitCategory::Resolution; }
bool isViewportPercentageLength() const { return isViewportPercentageLength(primitiveUnitType()); }
bool isContainerPercentageLength() const { return isContainerPercentageLength(primitiveUnitType()); }
bool isFlex() const { return primitiveType() == CSSUnitType::CSS_FR; }
bool conversionToCanonicalUnitRequiresConversionData() const;
static Ref<CSSPrimitiveValue> create(double);
static Ref<CSSPrimitiveValue> create(double, CSSUnitType);
static Ref<CSSPrimitiveValue> createInteger(double);
static Ref<CSSPrimitiveValue> create(const Length&);
static Ref<CSSPrimitiveValue> create(const Length&, const RenderStyle&);
static Ref<CSSPrimitiveValue> create(Ref<CSSCalcValue>);
static Ref<CSSPrimitiveValue> create(Ref<CSSAttrValue>);
static inline Ref<CSSPrimitiveValue> create(CSSValueID);
bool isValueID() const { return primitiveUnitType() == CSSUnitType::CSS_VALUE_ID; }
CSSValueID valueID() const { return isValueID() ? m_value.valueID : CSSValueInvalid; }
static Ref<CSSPrimitiveValue> create(CSSPropertyID);
bool isPropertyID() const { return primitiveUnitType() == CSSUnitType::CSS_PROPERTY_ID; }
CSSPropertyID propertyID() const { return isPropertyID() ? m_value.propertyID : CSSPropertyInvalid; }
bool isString() const { return primitiveUnitType() == CSSUnitType::CSS_STRING; }
static Ref<CSSPrimitiveValue> create(String);
static Ref<CSSPrimitiveValue> createCustomIdent(String);
bool isCustomIdent() const { return primitiveUnitType() == CSSUnitType::CustomIdent; }
static Ref<CSSPrimitiveValue> createFontFamily(String);
bool isFontFamily() const { return primitiveUnitType() == CSSUnitType::CSS_FONT_FAMILY; }
static Ref<CSSPrimitiveValue> createURI(String);
bool isURI() const { return primitiveUnitType() == CSSUnitType::CSS_URI; }
static inline CSSPrimitiveValue& implicitInitialValue();
~CSSPrimitiveValue();
WEBCORE_EXPORT CSSUnitType primitiveType() const;
// Exposed for DeprecatedCSSOMPrimitiveValue. Throws if conversion to `targetUnit` is not allowed.
ExceptionOr<float> getFloatValueDeprecated(CSSUnitType targetUnit) const;
// MARK: Integer (requires `isInteger() == true`)
template<typename T = int> T resolveAsInteger(const CSSToLengthConversionData&) const;
template<typename T = int> T resolveAsIntegerNoConversionDataRequired() const;
template<typename T = int> T resolveAsIntegerDeprecated() const;
template<typename T = int> std::optional<T> resolveAsIntegerIfNotCalculated() const;
// MARK: Number (requires `isNumberOrInteger() == true`)
template<typename T = double> T resolveAsNumber(const CSSToLengthConversionData&) const;
template<typename T = double> T resolveAsNumberNoConversionDataRequired() const;
template<typename T = double> T resolveAsNumberDeprecated() const;
template<typename T = double> std::optional<T> resolveAsNumberIfNotCalculated() const;
// MARK: Percentage (requires `isPercentage() == true`)
template<typename T = double> T resolveAsPercentage(const CSSToLengthConversionData&) const;
template<typename T = double> T resolveAsPercentageNoConversionDataRequired() const;
template<typename T = double> T resolveAsPercentageDeprecated() const;
template<typename T = double> std::optional<T> resolveAsPercentageIfNotCalculated() const;
// MARK: Angle (requires `isAngle() == true`)
template<typename T = double, CSS::AngleUnit = CSS::UnitTraits<CSS::AngleUnit>::canonical> T resolveAsAngle(const CSSToLengthConversionData&) const;
template<typename T = double, CSS::AngleUnit = CSS::UnitTraits<CSS::AngleUnit>::canonical> T resolveAsAngleNoConversionDataRequired() const;
template<typename T = double, CSS::AngleUnit = CSS::UnitTraits<CSS::AngleUnit>::canonical> T resolveAsAngleDeprecated() const;
// MARK: Time (requires `isTime() == true`)
template<typename T = double, CSS::TimeUnit = CSS::UnitTraits<CSS::TimeUnit>::canonical> T resolveAsTime(const CSSToLengthConversionData&) const;
template<typename T = double, CSS::TimeUnit = CSS::UnitTraits<CSS::TimeUnit>::canonical> T resolveAsTimeNoConversionDataRequired() const;
// MARK: Resolution (requires `isResolution() == true`)
template<typename T = double, CSS::ResolutionUnit = CSS::UnitTraits<CSS::ResolutionUnit>::canonical> T resolveAsResolution(const CSSToLengthConversionData&) const;
template<typename T = double, CSS::ResolutionUnit = CSS::UnitTraits<CSS::ResolutionUnit>::canonical> T resolveAsResolutionNoConversionDataRequired() const;
template<typename T = double, CSS::ResolutionUnit = CSS::UnitTraits<CSS::ResolutionUnit>::canonical> T resolveAsResolutionDeprecated() const;
// MARK: Flex (requires `isFlex() == true`)
template<typename T = double> T resolveAsFlex(const CSSToLengthConversionData&) const;
template<typename T = double> T resolveAsFlexNoConversionDataRequired() const;
// MARK: Length (requires `isLength() == true`)
template<typename T = double> T resolveAsLength(const CSSToLengthConversionData&) const;
template<typename T = double> T resolveAsLengthNoConversionDataRequired() const;
template<typename T = double> T resolveAsLengthDeprecated() const;
bool convertingToLengthHasRequiredConversionData(int lengthConversion, const CSSToLengthConversionData&) const;
template<int> Length convertToLength(const CSSToLengthConversionData&) const;
// MARK: Non-converting
template<typename T = double> T value(const CSSToLengthConversionData& conversionData) const { return clampTo<T>(doubleValue(conversionData)); }
template<typename T = double> T valueNoConversionDataRequired() const { return clampTo<T>(doubleValueNoConversionDataRequired()); }
template<typename T = double> std::optional<T> valueIfNotCalculated() const;
// MARK: Divides value by 100 if percentage.
template<typename T = double> T valueDividingBy100IfPercentage(const CSSToLengthConversionData& conversionData) const { return clampTo<T>(doubleValueDividingBy100IfPercentage(conversionData)); }
template<typename T = double> T valueDividingBy100IfPercentageNoConversionDataRequired() const { return clampTo<T>(doubleValueDividingBy100IfPercentageNoConversionDataRequired()); }
template<typename T = double> T valueDividingBy100IfPercentageDeprecated() const { return clampTo<T>(doubleValueDividingBy100IfPercentageDeprecated()); }
// These return nullopt for calc, for which range checking is not done at parse time: <https://www.w3.org/TR/css3-values/#calc-range>.
std::optional<bool> isZero() const;
std::optional<bool> isOne() const;
std::optional<bool> isPositive() const;
std::optional<bool> isNegative() const;
WEBCORE_EXPORT String stringValue() const;
const CSSCalcValue* cssCalcValue() const { return isCalculated() ? m_value.calc : nullptr; }
const CSSAttrValue* cssAttrValue() const { return isAttr() ? m_value.attr : nullptr; }
String customCSSText(const CSS::SerializationContext&) const;
bool equals(const CSSPrimitiveValue&) const;
static ASCIILiteral unitTypeString(CSSUnitType);
void collectComputedStyleDependencies(ComputedStyleDependencies&) const;
IterationStatus customVisitChildren(NOESCAPE const Function<IterationStatus(CSSValue&)>&) const;
private:
friend class CSSValuePool;
friend class StaticCSSValuePool;
friend LazyNeverDestroyed<CSSPrimitiveValue>;
friend bool CSSValue::addHash(Hasher&) const;
explicit CSSPrimitiveValue(CSSPropertyID);
explicit CSSPrimitiveValue(const Length&);
CSSPrimitiveValue(const Length&, const RenderStyle&);
CSSPrimitiveValue(const String&, CSSUnitType);
CSSPrimitiveValue(double, CSSUnitType);
explicit CSSPrimitiveValue(Ref<CSSCalcValue>);
explicit CSSPrimitiveValue(Ref<CSSAttrValue>);
CSSPrimitiveValue(StaticCSSValueTag, CSSValueID);
CSSPrimitiveValue(StaticCSSValueTag, double, CSSUnitType);
enum ImplicitInitialValueTag { ImplicitInitialValue };
CSSPrimitiveValue(StaticCSSValueTag, ImplicitInitialValueTag);
CSSUnitType primitiveUnitType() const { return static_cast<CSSUnitType>(m_primitiveUnitType); }
void setPrimitiveUnitType(CSSUnitType type) { m_primitiveUnitType = enumToUnderlyingType(type); }
// MARK: Length converting
double resolveAsLengthDouble(const CSSToLengthConversionData&) const;
// MARK: Arbitrarily converting
double doubleValue(CSSUnitType targetUnit, const CSSToLengthConversionData&) const;
double doubleValueNoConversionDataRequired(CSSUnitType targetUnit) const;
double doubleValueDeprecated(CSSUnitType targetUnit) const;
template<typename T = double> inline T value(CSSUnitType targetUnit, const CSSToLengthConversionData& conversionData) const { return clampTo<T>(doubleValue(targetUnit, conversionData)); }
template<typename T = double> inline T valueNoConversionDataRequired(CSSUnitType targetUnit) const { return clampTo<T>(doubleValueNoConversionDataRequired(targetUnit)); }
template<typename T = double> inline T valueDeprecated(CSSUnitType targetUnit) const { return clampTo<T>(doubleValueDeprecated(targetUnit)); }
// MARK: Non-converting
double doubleValue(const CSSToLengthConversionData&) const;
double doubleValueNoConversionDataRequired() const { ASSERT(!isCalculated()); return m_value.number; }
double doubleValueDeprecated() const;
double doubleValueDividingBy100IfPercentage(const CSSToLengthConversionData&) const;
double doubleValueDividingBy100IfPercentageNoConversionDataRequired() const;
double doubleValueDividingBy100IfPercentageDeprecated() const;
template<typename T = double> inline T valueDeprecated() const { return clampTo<T>(doubleValueDeprecated()); }
static std::optional<double> conversionToCanonicalUnitsScaleFactor(CSSUnitType);
std::optional<double> doubleValueInternal(CSSUnitType targetUnit, const CSSToLengthConversionData&) const;
std::optional<double> doubleValueInternalDeprecated(CSSUnitType targetUnit) const;
bool addDerivedHash(Hasher&) const;
ALWAYS_INLINE String serializeInternal(const CSS::SerializationContext&) const;
NEVER_INLINE String formatNumberValue(ASCIILiteral suffix) const;
NEVER_INLINE String formatIntegerValue(ASCIILiteral suffix) const;
static constexpr bool isFontIndependentLength(CSSUnitType);
static constexpr bool isFontRelativeLength(CSSUnitType);
static constexpr bool isRootFontRelativeLength(CSSUnitType);
static constexpr bool isContainerPercentageLength(CSSUnitType);
static constexpr bool isViewportPercentageLength(CSSUnitType);
union {
CSSPropertyID propertyID;
CSSValueID valueID;
double number;
StringImpl* string;
const CSSCalcValue* calc;
const CSSAttrValue* attr;
} m_value;
};
template<typename TargetType> constexpr TargetType fromCSSValueID(CSSValueID);
constexpr bool CSSPrimitiveValue::isFontIndependentLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_PX
|| type == CSSUnitType::CSS_CM
|| type == CSSUnitType::CSS_MM
|| type == CSSUnitType::CSS_IN
|| type == CSSUnitType::CSS_PT
|| type == CSSUnitType::CSS_PC;
}
constexpr bool CSSPrimitiveValue::isRootFontRelativeLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_RCAP
|| type == CSSUnitType::CSS_RCH
|| type == CSSUnitType::CSS_REM
|| type == CSSUnitType::CSS_REX
|| type == CSSUnitType::CSS_RIC
|| type == CSSUnitType::CSS_RLH;
}
constexpr bool CSSPrimitiveValue::isFontRelativeLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_EM
|| type == CSSUnitType::CSS_EX
|| type == CSSUnitType::CSS_LH
|| type == CSSUnitType::CSS_CAP
|| type == CSSUnitType::CSS_CH
|| type == CSSUnitType::CSS_IC
|| type == CSSUnitType::CSS_QUIRKY_EM
|| isRootFontRelativeLength(type);
}
constexpr bool CSSPrimitiveValue::isContainerPercentageLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_CQW
|| type == CSSUnitType::CSS_CQH
|| type == CSSUnitType::CSS_CQI
|| type == CSSUnitType::CSS_CQB
|| type == CSSUnitType::CSS_CQMIN
|| type == CSSUnitType::CSS_CQMAX;
}
constexpr bool CSSPrimitiveValue::isLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_EM
|| type == CSSUnitType::CSS_EX
|| type == CSSUnitType::CSS_PX
|| type == CSSUnitType::CSS_CM
|| type == CSSUnitType::CSS_MM
|| type == CSSUnitType::CSS_IN
|| type == CSSUnitType::CSS_PT
|| type == CSSUnitType::CSS_PC
|| type == CSSUnitType::CSS_Q
|| isFontRelativeLength(type)
|| isViewportPercentageLength(type)
|| isContainerPercentageLength(type)
|| type == CSSUnitType::CSS_QUIRKY_EM;
}
constexpr bool CSSPrimitiveValue::isViewportPercentageLength(CSSUnitType type)
{
return type >= CSSUnitType::FirstViewportCSSUnitType && type <= CSSUnitType::LastViewportCSSUnitType;
}
template<typename T> std::optional<T> CSSPrimitiveValue::valueIfNotCalculated() const
{
if (isCalculated())
return std::nullopt;
return m_value.number;
}
// MARK: Integer
template<typename T> T CSSPrimitiveValue::resolveAsInteger(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isInteger());
return value<T>(conversionData);
}
template<typename T> T CSSPrimitiveValue::resolveAsIntegerNoConversionDataRequired() const
{
ASSERT(isInteger());
return valueNoConversionDataRequired<T>();
}
template<typename T> T CSSPrimitiveValue::resolveAsIntegerDeprecated() const
{
ASSERT(isInteger());
return valueDeprecated<T>();
}
template<typename T> std::optional<T> CSSPrimitiveValue::resolveAsIntegerIfNotCalculated() const
{
ASSERT(isInteger());
return valueIfNotCalculated<T>();
}
// MARK: Number
template<typename T> T CSSPrimitiveValue::resolveAsNumber(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isNumberOrInteger());
return value<T>(CSSUnitType::CSS_NUMBER, conversionData);
}
template<typename T> T CSSPrimitiveValue::resolveAsNumberNoConversionDataRequired() const
{
ASSERT(isNumberOrInteger());
return valueNoConversionDataRequired<T>(CSSUnitType::CSS_NUMBER);
}
template<typename T> T CSSPrimitiveValue::resolveAsNumberDeprecated() const
{
ASSERT(isNumberOrInteger());
return valueDeprecated<T>(CSSUnitType::CSS_NUMBER);
}
template<typename T> std::optional<T> CSSPrimitiveValue::resolveAsNumberIfNotCalculated() const
{
ASSERT(isNumberOrInteger());
return valueIfNotCalculated<T>();
}
// MARK: Percentage
template<typename T> T CSSPrimitiveValue::resolveAsPercentage(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isPercentage());
return value<T>(conversionData);
}
template<typename T> T CSSPrimitiveValue::resolveAsPercentageNoConversionDataRequired() const
{
ASSERT(isPercentage());
return valueNoConversionDataRequired<T>();
}
template<typename T> T CSSPrimitiveValue::resolveAsPercentageDeprecated() const
{
ASSERT(isPercentage());
return valueDeprecated<T>();
}
template<typename T> std::optional<T> CSSPrimitiveValue::resolveAsPercentageIfNotCalculated() const
{
ASSERT(isPercentage());
return valueIfNotCalculated<T>();
}
// MARK: Angle
template<CSS::AngleUnit angleUnit, typename T> T CSSPrimitiveValue::computeAngle(CSSUnitType type, T angle)
{
if constexpr (angleUnit == CSS::AngleUnit::Deg) {
switch (type) {
case CSSUnitType::CSS_DEG:
return angle;
case CSSUnitType::CSS_RAD:
return rad2deg(angle);
case CSSUnitType::CSS_GRAD:
return grad2deg(angle);
case CSSUnitType::CSS_TURN:
return turn2deg(angle);
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (angleUnit == CSS::AngleUnit::Rad) {
switch (type) {
case CSSUnitType::CSS_DEG:
return deg2rad(angle);
case CSSUnitType::CSS_RAD:
return angle;
case CSSUnitType::CSS_GRAD:
return grad2rad(angle);
case CSSUnitType::CSS_TURN:
return turn2rad(angle);
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (angleUnit == CSS::AngleUnit::Grad) {
switch (type) {
case CSSUnitType::CSS_DEG:
return deg2grad(angle);
case CSSUnitType::CSS_RAD:
return rad2grad(angle);
case CSSUnitType::CSS_GRAD:
return angle;
case CSSUnitType::CSS_TURN:
return turn2grad(angle);
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (angleUnit == CSS::AngleUnit::Turn) {
switch (type) {
case CSSUnitType::CSS_DEG:
return deg2turn(angle);
case CSSUnitType::CSS_RAD:
return rad2Turn(angle);
case CSSUnitType::CSS_GRAD:
return grad2turn(angle);
case CSSUnitType::CSS_TURN:
return angle;
default:
ASSERT_NOT_REACHED();
return 0;
}
}
}
template<typename T, CSS::AngleUnit angleUnit> T CSSPrimitiveValue::resolveAsAngle(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isAngle());
return clampTo<T>(computeAngle<angleUnit>(primitiveType(), value<double>(conversionData)));
}
template<typename T, CSS::AngleUnit angleUnit> T CSSPrimitiveValue::resolveAsAngleNoConversionDataRequired() const
{
ASSERT(isAngle());
return clampTo<T>(computeAngle<angleUnit>(primitiveType(), valueNoConversionDataRequired<double>()));
}
template<typename T, CSS::AngleUnit angleUnit> T CSSPrimitiveValue::resolveAsAngleDeprecated() const
{
ASSERT(isAngle());
return clampTo<T>(computeAngle<angleUnit>(primitiveType(), valueDeprecated<double>()));
}
// MARK: Time
template<CSS::TimeUnit timeUnit, typename T> inline T CSSPrimitiveValue::computeTime(CSSUnitType type, T value)
{
if constexpr (timeUnit == CSS::TimeUnit::S) {
switch (type) {
case CSSUnitType::CSS_S:
return value;
case CSSUnitType::CSS_MS:
return value * CSS::secondsPerMillisecond;
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (timeUnit == CSS::TimeUnit::Ms) {
switch (type) {
case CSSUnitType::CSS_S:
return value / CSS::secondsPerMillisecond;
case CSSUnitType::CSS_MS:
return value;
default:
ASSERT_NOT_REACHED();
return 0;
}
}
}
template<typename T, CSS::TimeUnit timeUnit> T CSSPrimitiveValue::resolveAsTime(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isTime());
return clampTo<T>(computeTime<timeUnit>(primitiveType(), value<double>(conversionData)));
}
template<typename T, CSS::TimeUnit timeUnit> T CSSPrimitiveValue::resolveAsTimeNoConversionDataRequired() const
{
ASSERT(isTime());
return clampTo<T>(computeTime<timeUnit>(primitiveType(), valueNoConversionDataRequired<double>()));
}
// MARK: Frequency
template<CSS::FrequencyUnit frequencyUnit, typename T> inline T CSSPrimitiveValue::computeFrequency(CSSUnitType type, T value)
{
if constexpr (frequencyUnit == CSS::FrequencyUnit::Hz) {
switch (type) {
case CSSUnitType::CSS_HZ:
return value;
case CSSUnitType::CSS_KHZ:
return value * CSS::hertzPerKilohertz;
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (frequencyUnit == CSS::FrequencyUnit::Khz) {
switch (type) {
case CSSUnitType::CSS_HZ:
return value / CSS::hertzPerKilohertz;
case CSSUnitType::CSS_KHZ:
return value;
default:
ASSERT_NOT_REACHED();
return 0;
}
}
}
// MARK: Resolution
template<CSS::ResolutionUnit resolutionUnit, typename T> inline T CSSPrimitiveValue::computeResolution(CSSUnitType type, T resolution)
{
if constexpr (resolutionUnit == CSS::ResolutionUnit::Dppx) {
switch (type) {
case CSSUnitType::CSS_DPPX:
return resolution;
case CSSUnitType::CSS_X:
return resolution * CSS::dppxPerX;
case CSSUnitType::CSS_DPI:
return resolution * CSS::dppxPerDpi;
case CSSUnitType::CSS_DPCM:
return resolution * CSS::dppxPerDpcm;
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (resolutionUnit == CSS::ResolutionUnit::X) {
switch (type) {
case CSSUnitType::CSS_DPPX:
return resolution / CSS::dppxPerX;
case CSSUnitType::CSS_X:
return resolution;
case CSSUnitType::CSS_DPI:
return resolution * CSS::dppxPerDpi / CSS::dppxPerX;
case CSSUnitType::CSS_DPCM:
return resolution * CSS::dppxPerDpcm / CSS::dppxPerX;
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (resolutionUnit == CSS::ResolutionUnit::Dpi) {
switch (type) {
case CSSUnitType::CSS_DPPX:
return resolution / CSS::dppxPerDpi;
case CSSUnitType::CSS_X:
return resolution * CSS::dppxPerX / CSS::dppxPerDpi;
case CSSUnitType::CSS_DPI:
return resolution;
case CSSUnitType::CSS_DPCM:
return resolution * CSS::dppxPerDpcm / CSS::dppxPerDpi;
default:
ASSERT_NOT_REACHED();
return 0;
}
} else if constexpr (resolutionUnit == CSS::ResolutionUnit::Dpcm) {
switch (type) {
case CSSUnitType::CSS_DPPX:
return resolution / CSS::dppxPerDpcm;
case CSSUnitType::CSS_X:
return resolution * CSS::dppxPerX / CSS::dppxPerDpcm;
case CSSUnitType::CSS_DPI:
return resolution * CSS::dppxPerDpi / CSS::dppxPerDpcm;
case CSSUnitType::CSS_DPCM:
return resolution;
default:
ASSERT_NOT_REACHED();
return 0;
}
}
}
template<typename T, CSS::ResolutionUnit resolutionUnit> T CSSPrimitiveValue::resolveAsResolution(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isResolution());
return clampTo<T>(computeResolution<resolutionUnit>(primitiveType(), value<double>(conversionData)));
}
template<typename T, CSS::ResolutionUnit resolutionUnit> T CSSPrimitiveValue::resolveAsResolutionNoConversionDataRequired() const
{
ASSERT(isResolution());
return clampTo<T>(computeResolution<resolutionUnit>(primitiveType(), valueNoConversionDataRequired<double>()));
}
template<typename T, CSS::ResolutionUnit resolutionUnit> T CSSPrimitiveValue::resolveAsResolutionDeprecated() const
{
ASSERT(isResolution());
return clampTo<T>(computeResolution<resolutionUnit>(primitiveType(), valueDeprecated<double>()));
}
// MARK: Flex
template<typename T> T CSSPrimitiveValue::resolveAsFlex(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isFlex());
return value<T>(conversionData);
}
template<typename T> T CSSPrimitiveValue::resolveAsFlexNoConversionDataRequired() const
{
ASSERT(isFlex());
return valueNoConversionDataRequired<T>();
}
// MARK: Length
template<typename T> T CSSPrimitiveValue::resolveAsLengthNoConversionDataRequired() const
{
ASSERT(isLength());
return valueNoConversionDataRequired<T>(CSSUnitType::CSS_PX);
}
template<typename T> T CSSPrimitiveValue::resolveAsLengthDeprecated() const
{
ASSERT(isLength());
return valueDeprecated<T>(CSSUnitType::CSS_PX);
}
// MARK: valueID(...)
inline CSSValueID valueID(const CSSPrimitiveValue& value)
{
return value.valueID();
}
inline CSSValueID valueID(const CSSPrimitiveValue* value)
{
return value ? valueID(*value) : CSSValueInvalid;
}
inline CSSValueID valueID(const CSSValue& value)
{
auto* primitiveValue = dynamicDowncast<CSSPrimitiveValue>(value);
return primitiveValue ? valueID(*primitiveValue) : CSSValueInvalid;
}
inline CSSValueID valueID(const CSSValue* value)
{
return value ? valueID(*value) : CSSValueInvalid;
}
inline bool isValueID(const CSSPrimitiveValue& value, CSSValueID id)
{
return valueID(value) == id;
}
inline bool isValueID(const CSSPrimitiveValue* value, CSSValueID id)
{
return valueID(value) == id;
}
inline bool isValueID(const RefPtr<CSSPrimitiveValue>& value, CSSValueID id)
{
return valueID(value.get()) == id;
}
inline bool isValueID(const Ref<CSSPrimitiveValue>& value, CSSValueID id)
{
return valueID(value.get()) == id;
}
inline bool isValueID(const CSSValue& value, CSSValueID id)
{
return valueID(value) == id;
}
inline bool isValueID(const CSSValue* value, CSSValueID id)
{
return valueID(value) == id;
}
inline bool isValueID(const RefPtr<CSSValue>& value, CSSValueID id)
{
return isValueID(value.get(), id);
}
inline bool isValueID(const Ref<CSSValue>& value, CSSValueID id)
{
return isValueID(value.get(), id);
}
inline bool CSSValue::isValueID() const
{
auto* value = dynamicDowncast<CSSPrimitiveValue>(*this);
return value && value->isValueID();
}
inline CSSValueID CSSValue::valueID() const
{
auto* value = dynamicDowncast<CSSPrimitiveValue>(*this);
return value ? value->valueID() : CSSValueInvalid;
}
inline bool CSSValue::isCustomIdent() const
{
auto* value = dynamicDowncast<CSSPrimitiveValue>(*this);
return value && value->isCustomIdent();
}
inline String CSSValue::customIdent() const
{
ASSERT(isCustomIdent());
return downcast<CSSPrimitiveValue>(*this).stringValue();
}
inline bool CSSValue::isInteger() const
{
auto* value = dynamicDowncast<CSSPrimitiveValue>(*this);
return value && value->isInteger();
}
inline int CSSValue::integer(const CSSToLengthConversionData& conversionData) const
{
ASSERT(isInteger());
return downcast<CSSPrimitiveValue>(*this).resolveAsInteger(conversionData);
}
inline int CSSValue::integerDeprecated() const
{
ASSERT(isInteger());
return downcast<CSSPrimitiveValue>(*this).resolveAsIntegerDeprecated();
}
void add(Hasher&, const CSSPrimitiveValue&);
} // namespace WebCore
SPECIALIZE_TYPE_TRAITS_CSS_VALUE(CSSPrimitiveValue, isPrimitiveValue())
|