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 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
|
/*
* Copyright (C) 2024-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.
*/
#include "config.h"
#include "CSSCalcTree+Simplification.h"
#include "AnchorPositionEvaluator.h"
#include "CSSCalcSymbolTable.h"
#include "CSSCalcTree+ContainerProgressEvaluator.h"
#include "CSSCalcTree+Copy.h"
#include "CSSCalcTree+Evaluation.h"
#include "CSSCalcTree+Mappings.h"
#include "CSSCalcTree+MediaProgressEvaluator.h"
#include "CSSCalcTree+NumericIdentity.h"
#include "CSSCalcTree+Traversal.h"
#include "CSSCalcTree.h"
#include "CSSPrimitiveValue.h"
#include "CalculationCategory.h"
#include "CalculationExecutor.h"
#include "ContainerQueryFeatures.h"
#include "MediaQueryFeatures.h"
#include "RenderStyle.h"
#include "RenderStyleInlines.h"
#include "StyleBuilderState.h"
#include "StyleLengthResolution.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
namespace CSSCalc {
static auto copyAndSimplify(const MQ::MediaProgressProviding*, const SimplificationOptions&) -> const MQ::MediaProgressProviding*;
static auto copyAndSimplify(const CQ::ContainerProgressProviding*, const SimplificationOptions&) -> const CQ::ContainerProgressProviding*;
static auto copyAndSimplify(const Random::CachingOptions&, const SimplificationOptions&) -> Random::CachingOptions;
static auto copyAndSimplify(const AtomString&, const SimplificationOptions&) -> AtomString;
static auto copyAndSimplify(const CSS::Keyword::None&, const SimplificationOptions&) -> CSS::Keyword::None;
static auto copyAndSimplify(const Children&, const SimplificationOptions&) -> Children;
static auto copyAndSimplify(const ChildOrNone&, const SimplificationOptions&) -> ChildOrNone;
template<typename T>
static auto copyAndSimplify(const std::optional<T>&, const SimplificationOptions&) -> std::optional<T>;
template<typename Op, typename... Args> static double executeMathOperation(Args&&... args)
{
return Calculation::executeOperation<ToCalculationTreeOp<Op>>(std::forward<Args>(args)...);
}
template<typename... F> static decltype(auto) switchTogether(const Child& a, const Child& b, F&&... f)
{
auto visitor = WTF::makeVisitor(std::forward<F>(f)...);
using ResultType = decltype(visitor(std::declval<Number>(), std::declval<Number>()));
if (a.index() != b.index())
return visitor(std::nullopt, std::nullopt);
return WTF::switchOn(a,
[&]<typename T>(const T& aT) -> ResultType {
return visitor(aT, get<T>(b));
}
);
}
// MARK: Predicate: percentageResolveToDimension
static bool percentageResolveToDimension(const SimplificationOptions& options)
{
switch (options.category) {
case Calculation::Category::Integer:
case Calculation::Category::Number:
case Calculation::Category::Length:
case Calculation::Category::Percentage:
case Calculation::Category::Angle:
case Calculation::Category::Time:
case Calculation::Category::Frequency:
case Calculation::Category::Resolution:
case Calculation::Category::Flex:
return false;
case Calculation::Category::AnglePercentage:
case Calculation::Category::LengthPercentage:
return true;
}
ASSERT_NOT_REACHED();
return false;
}
// MARK: Predicate: unitsMatch
constexpr bool unitsMatch(const Number&, const Number&, const SimplificationOptions&)
{
return true;
}
constexpr bool unitsMatch(const Percentage&, const Percentage&, const SimplificationOptions&)
{
return true;
}
static bool unitsMatch(const CanonicalDimension& a, const CanonicalDimension& b, const SimplificationOptions&)
{
return a.dimension == b.dimension;
}
static bool unitsMatch(const NonCanonicalDimension& a, const NonCanonicalDimension& b, const SimplificationOptions&)
{
return a.unit == b.unit;
}
// MARK: Predicate: magnitudeComparable
constexpr bool magnitudeComparable(const Number&, const SimplificationOptions&)
{
return true;
}
static bool magnitudeComparable(const Percentage&, const SimplificationOptions& options)
{
return !percentageResolveToDimension(options);
}
constexpr bool magnitudeComparable(const CanonicalDimension&, const SimplificationOptions&)
{
return true;
}
constexpr bool magnitudeComparable(const NonCanonicalDimension&, const SimplificationOptions&)
{
return true;
}
// MARK: Predicate: fullyResolved
constexpr bool fullyResolved(const Number&, const SimplificationOptions&)
{
return true;
}
static bool fullyResolved(const Percentage&, const SimplificationOptions& options)
{
return !percentageResolveToDimension(options);
}
constexpr bool fullyResolved(const CanonicalDimension&, const SimplificationOptions&)
{
return true;
}
constexpr bool fullyResolved(const NonCanonicalDimension&, const SimplificationOptions&)
{
return false;
}
std::optional<CanonicalDimension> canonicalize(NonCanonicalDimension root, const std::optional<CSSToLengthConversionData>& conversionData)
{
auto makeCanonical = [&](double value, CanonicalDimension::Dimension dimension) -> std::optional<CanonicalDimension> {
return CanonicalDimension { .value = value, .dimension = dimension };
};
auto tryMakeCanonical = [&](double value, CSS::LengthUnit lengthUnit) -> std::optional<CanonicalDimension> {
if (conversionData) {
// We are only interested in canonicalizing to `px`, not adjusting for zoom, which will be handled later. When computing font-size, zoom is not applied in the same way, so must be special cased here.
if (conversionData->computingFontSize())
return CanonicalDimension { .value = Style::computeNonCalcLengthDouble(value, lengthUnit, *conversionData), .dimension = CanonicalDimension::Dimension::Length };
return CanonicalDimension { .value = Style::computeNonCalcLengthDouble(value, lengthUnit, *conversionData) / conversionData->style()->usedZoom(), .dimension = CanonicalDimension::Dimension::Length };
}
return { };
};
switch (root.unit) {
// Absolute Lengths (can be canonicalized without conversion data).
case CSSUnitType::CSS_CM:
return makeCanonical(root.value * CSS::pixelsPerCm, CanonicalDimension::Dimension::Length);
case CSSUnitType::CSS_MM:
return makeCanonical(root.value * CSS::pixelsPerMm, CanonicalDimension::Dimension::Length);
case CSSUnitType::CSS_Q:
return makeCanonical(root.value * CSS::pixelsPerQ, CanonicalDimension::Dimension::Length);
case CSSUnitType::CSS_IN:
return makeCanonical(root.value * CSS::pixelsPerInch, CanonicalDimension::Dimension::Length);
case CSSUnitType::CSS_PT:
return makeCanonical(root.value * CSS::pixelsPerPt, CanonicalDimension::Dimension::Length);
case CSSUnitType::CSS_PC:
return makeCanonical(root.value * CSS::pixelsPerPc, CanonicalDimension::Dimension::Length);
// Font, Viewport and Container relative Lengths (require conversion data for canonicalization).
case CSSUnitType::CSS_EM:
case CSSUnitType::CSS_EX:
case CSSUnitType::CSS_LH:
case CSSUnitType::CSS_CAP:
case CSSUnitType::CSS_CH:
case CSSUnitType::CSS_IC:
case CSSUnitType::CSS_RCAP:
case CSSUnitType::CSS_RCH:
case CSSUnitType::CSS_REM:
case CSSUnitType::CSS_REX:
case CSSUnitType::CSS_RIC:
case CSSUnitType::CSS_RLH:
case CSSUnitType::CSS_VW:
case CSSUnitType::CSS_VH:
case CSSUnitType::CSS_VMIN:
case CSSUnitType::CSS_VMAX:
case CSSUnitType::CSS_VB:
case CSSUnitType::CSS_VI:
case CSSUnitType::CSS_SVW:
case CSSUnitType::CSS_SVH:
case CSSUnitType::CSS_SVMIN:
case CSSUnitType::CSS_SVMAX:
case CSSUnitType::CSS_SVB:
case CSSUnitType::CSS_SVI:
case CSSUnitType::CSS_LVW:
case CSSUnitType::CSS_LVH:
case CSSUnitType::CSS_LVMIN:
case CSSUnitType::CSS_LVMAX:
case CSSUnitType::CSS_LVB:
case CSSUnitType::CSS_LVI:
case CSSUnitType::CSS_DVW:
case CSSUnitType::CSS_DVH:
case CSSUnitType::CSS_DVMIN:
case CSSUnitType::CSS_DVMAX:
case CSSUnitType::CSS_DVB:
case CSSUnitType::CSS_DVI:
case CSSUnitType::CSS_CQW:
case CSSUnitType::CSS_CQH:
case CSSUnitType::CSS_CQI:
case CSSUnitType::CSS_CQB:
case CSSUnitType::CSS_CQMIN:
case CSSUnitType::CSS_CQMAX:
return tryMakeCanonical(root.value, *CSS::toLengthUnit(root.unit));
// <angle>
case CSSUnitType::CSS_RAD:
return makeCanonical(root.value * degreesPerRadianDouble, CanonicalDimension::Dimension::Angle);
case CSSUnitType::CSS_GRAD:
return makeCanonical(root.value * degreesPerGradientDouble, CanonicalDimension::Dimension::Angle);
case CSSUnitType::CSS_TURN:
return makeCanonical(root.value * degreesPerTurnDouble, CanonicalDimension::Dimension::Angle);
// <time>
case CSSUnitType::CSS_MS:
return makeCanonical(root.value * CSS::secondsPerMillisecond, CanonicalDimension::Dimension::Time);
// <frequency>
case CSSUnitType::CSS_KHZ:
return makeCanonical(root.value * CSS::hertzPerKilohertz, CanonicalDimension::Dimension::Frequency);
// <resolution>
case CSSUnitType::CSS_X:
return makeCanonical(root.value * CSS::dppxPerX, CanonicalDimension::Dimension::Resolution);
case CSSUnitType::CSS_DPI:
return makeCanonical(root.value * CSS::dppxPerDpi, CanonicalDimension::Dimension::Resolution);
case CSSUnitType::CSS_DPCM:
return makeCanonical(root.value * CSS::dppxPerDpcm, CanonicalDimension::Dimension::Resolution);
// Canonical dimensional types should never be stored in a NonCanonicalDimension.
case CSSUnitType::CSS_PX:
case CSSUnitType::CSS_DEG:
case CSSUnitType::CSS_S:
case CSSUnitType::CSS_HZ:
case CSSUnitType::CSS_DPPX:
case CSSUnitType::CSS_FR:
// Non-dimensional types should never be stored in a NonCanonicalDimension.
case CSSUnitType::CSS_NUMBER:
case CSSUnitType::CSS_INTEGER:
case CSSUnitType::CSS_PERCENTAGE:
// Non-numeric types should never be stored in a NonCanonicalDimension.
case CSSUnitType::CSS_ATTR:
case CSSUnitType::CSS_CALC:
case CSSUnitType::CSS_CALC_PERCENTAGE_WITH_ANGLE:
case CSSUnitType::CSS_CALC_PERCENTAGE_WITH_LENGTH:
case CSSUnitType::CSS_DIMENSION:
case CSSUnitType::CSS_FONT_FAMILY:
case CSSUnitType::CSS_IDENT:
case CSSUnitType::CSS_PROPERTY_ID:
case CSSUnitType::CSS_QUIRKY_EM:
case CSSUnitType::CSS_STRING:
case CSSUnitType::CSS_UNKNOWN:
case CSSUnitType::CSS_URI:
case CSSUnitType::CSS_VALUE_ID:
case CSSUnitType::CustomIdent:
break;
}
ASSERT_NOT_REACHED();
return { };
}
// MARK: Generic partial evaluation functions
template<typename Op> static std::optional<Child> simplifyForOperation(Child& a, Child& b, const SimplificationOptions& options)
{
return switchTogether(a, b,
[&]<Numeric T>(const T& numericA, const T& numericB) -> std::optional<Child> {
if (!unitsMatch(numericA, numericB, options) || !fullyResolved(numericA, options))
return { };
return makeChildWithValueBasedOn(executeMathOperation<Op>(numericA.value, numericB.value), numericA);
},
[](const auto&, const auto&) -> std::optional<Child> {
return { };
}
);
}
template<typename Op, typename Completion> static std::optional<Child> simplifyForOperationWithCompletion(Child& a, Child& b, const SimplificationOptions& options, Completion&& completion)
{
return switchTogether(a, b,
[&]<Numeric T>(const T& numericA, const T& numericB) -> std::optional<Child> {
if (!unitsMatch(numericA, numericB, options) || !fullyResolved(numericA, options))
return { };
return completion(executeMathOperation<Op>(numericA.value, numericB.value));
},
[](const auto&, const auto&) -> std::optional<Child> {
return { };
}
);
}
template<typename Op> static std::optional<Child> simplifyForRound(Op& root, const SimplificationOptions& options)
{
if (root.b)
return simplifyForOperation<Op>(root.a, *root.b, options);
if (auto* numberA = get_if<Number>(&root.a))
return makeChild(Number { .value = executeMathOperation<Op>(numberA->value, 1.0) });
return { };
}
template<typename Op> static std::optional<Child> simplifyForTrig(Op& root, const SimplificationOptions&)
{
// NOTE: `a` has been type checked by this point to be `<number>` or an `<angle>`, though they may not
// be able to be fully resolved yet. If its an `<angle>`, it is also already been converted to canonical
// units via earlier simplification.
return WTF::switchOn(root.a,
[&](const Number& a) -> std::optional<Child> {
return makeChild(Number { .value = executeMathOperation<Op>(a.value) });
},
[&](const CanonicalDimension& a) -> std::optional<Child> {
ASSERT(a.dimension == CanonicalDimension::Dimension::Angle);
return makeChild(Number { .value = executeMathOperation<Op>(deg2rad(a.value)) });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
template<typename Op> static std::optional<Child> simplifyForArcTrig(Op& root, const SimplificationOptions&)
{
// NOTE: `a` has been type checked by this point to be `<number>`, though they may not
// be able to be fully resolved yet.
return WTF::switchOn(root.a,
[&](const Number& a) -> std::optional<Child> {
return makeChild(CanonicalDimension { .value = executeMathOperation<Op>(a.value), .dimension = CanonicalDimension::Dimension::Angle });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
template<typename Op> static std::optional<Child> simplifyForMinMax(Op& root, const SimplificationOptions& options)
{
ASSERT(!root.children.isEmpty());
// This function implements shared logic for Min and Max simplification:
// 5.1. For each node child of root’s children:
// If child is a numeric value with enough information to compare magnitudes with another child of the same unit (see note in previous step), and there are other children of root that are numeric values with the same unit, combine all such children with the appropriate operator per root, and replace child with the result, removing all other child nodes involved.
// 5.2. If root has only one child, return the child.
// 5.3. Otherwise, return root.
// --
// These steps are implemented as a two phase procedure.
// 1. Iterate children to find "merge opportunities", counting the total number of merges that will happen, and storing the index of the first child of each merge type in a lookup table.
// 2. Perform merges based on data from step 1.
//
// By splitting it up, we can perform two optimizations:
// 1. If the result of step 1 shows that the number of "merge opportunities" will lead to only one remaining child, we can avoid allocating a new Children Vector, and just merge directly into the child.
// 2. If the result of step 1 shows that the number of "merge opportunities" will lead to more than one remaining child, we can precisely allocate the Children Vector to be (existing children - "merge opportunities").
auto evaluate = [](const Child& a, const Child& b) -> Child {
ASSERT(a.index() == b.index());
return WTF::switchOn(a,
[&]<Numeric T>(const T& aNumeric) -> Child {
ASSERT(toNumericIdentity(aNumeric) == toNumericIdentity(get<T>(b)));
return makeChildWithValueBasedOn(executeMathOperation<Op>(aNumeric.value, get<T>(b).value), aNumeric);
},
[](const auto&) -> Child {
ASSERT_NOT_REACHED();
return makeChild(Number { .value = 0 });
}
);
};
// Special case a root with one child to avoid doing any work at all, and just returning the child.
if (root.children.size() == 1)
return { WTFMove(root.children[0]) };
// Map of unit types (via NumericIdentity) to the first index in `root.children` where a value with that unit can be found.
// More specifically, it maps the unit to the index + 1, as 0 is used to indicate no units of that type have been found.
// FIXME: This should be turned into a type with an interface that doesn't require explicit use of static_cast<uint8_t> by the caller.
std::array<size_t, numberOfNumericIdentityTypes> offsetOfFirstInstance { };
bool canMergePercentages = !percentageResolveToDimension(options);
unsigned numberOfMergeOpportunities = 0;
for (size_t i = 0; i < root.children.size(); ++i) {
numberOfMergeOpportunities += WTF::switchOn(root.children[i],
[&]<Numeric T>(const T& child) {
auto id = toNumericIdentity(child);
if (id == NumericIdentity::Percentage && !canMergePercentages)
return 0;
if (auto offset = offsetOfFirstInstance[static_cast<uint8_t>(id)]) {
// There has already been an instance of this type. This is a merge opportunity.
// Merge the value into first instance.
root.children[offset - 1] = evaluate(root.children[offset - 1], root.children[i]);
// Return 1 to increment the number of merge opportunities observed.
return 1;
}
// First instance of this. Store the index (well, index + 1, since 0 is the unset value).
offsetOfFirstInstance[static_cast<uint8_t>(id)] = i + 1;
// Give this was the first instance, it is not yet a merge opportunity.
return 0;
},
[](const auto&) {
return 0;
}
);
}
// If there are no merge opportunities, no further simplification is possible.
if (!numberOfMergeOpportunities)
return { };
auto combinedChildrenSize = root.children.size() - numberOfMergeOpportunities;
// If all the removal from merges leaves a single child, that means everything merged into the first child.
if (combinedChildrenSize == 1)
return { WTFMove(root.children[0]) };
Vector<Child> combinedChildren;
combinedChildren.reserveInitialCapacity(combinedChildrenSize);
for (size_t i = 0; i < root.children.size(); ++i) {
WTF::switchOn(root.children[i],
[&]<Numeric T>(const T& child) {
auto offset = offsetOfFirstInstance[static_cast<uint8_t>(toNumericIdentity(child))];
// If the stored offset for this type is unset (as it would be for percentages if merging them is disallowed) or is set to this index (as it would be for the first instance of a merged type), append the child as normal.
if (!offset || (offset - 1) == i) {
combinedChildren.append(WTFMove(root.children[i]));
return;
}
// Otherwise, it's one that can be dropped.
},
[&](const auto&) {
combinedChildren.append(WTFMove(root.children[i]));
}
);
}
root.children = WTFMove(combinedChildren);
return { };
}
// MARK: In-place simplification / replacement finding.
std::optional<Child> simplify(Number&, const SimplificationOptions&)
{
// No further simplification possible for <number>.
return { };
}
std::optional<Child> simplify(Percentage&, const SimplificationOptions&)
{
// 1.1. If root is a percentage that will be resolved against another value, and there is enough information available to resolve it, do so, and express the resulting numeric value in the appropriate canonical unit. Return the value.
// NOTE: Handled by the Calculation::Tree / CalculationValue types at use time.
return { };
}
std::optional<Child> simplify(CanonicalDimension&, const SimplificationOptions&)
{
// No further simplification possible for canonical <dimension>.
return { };
}
std::optional<Child> simplify(NonCanonicalDimension& root, const SimplificationOptions& options)
{
// NOTE: This implements the non-canonical dimension relevant parts of the numeric value simplification steps.
// 1.2. If root is a dimension that is not expressed in its canonical unit, and there is enough information available to convert it to the canonical unit, do so, and return the value.
if (auto canonical = canonicalize(root, options.conversionData))
return makeChild(WTFMove(*canonical));
return { };
}
std::optional<Child> simplify(Symbol& root, const SimplificationOptions& options)
{
// NOTE: This implements the keyword relevant parts of the numeric value simplification steps.
// 1.3. If root is a <calc-keyword> that can be resolved, return what it resolves to, simplified.
if (auto value = options.symbolTable.get(root.id))
return copyAndSimplify(makeNumeric(value->value, root.unit), options);
return { };
}
std::optional<Child> simplify(Sum& root, const SimplificationOptions& options)
{
ASSERT(!root.children.isEmpty());
// 8. If root is a Sum node:
// 8.1. For each of root’s children that are Sum nodes, replace them with their children.
if (std::ranges::any_of(root.children, [](auto& child) { return WTF::holdsAlternative<IndirectNode<Sum>>(child); })) {
Vector<Child> newChildren;
for (auto& child : root.children) {
if (auto* childSum = get_if<IndirectNode<Sum>>(&child))
newChildren.appendVector(WTFMove((*childSum)->children.value));
else
newChildren.append(WTFMove(child));
}
root.children = WTFMove(newChildren);
}
// 8.2. For each set of root’s children that are numeric values with identical units, remove those children and replace them with a single numeric value containing the sum of the removed nodes, and with the same unit. (E.g. combine numbers, combine percentages, combine px values, etc.)
// 8.3. If root has only a single child at this point, return the child.
// 8.4. Otherwise, return root
// These steps are implemented as a two phase procedure.
// 1. Iterate children to find "merge/removal opportunities", counting the total number of opportunities that will happen, and storing the index of the first child of each type in a lookup table.
// 2. Perform merges and removals based on data from step 1.
//
// By splitting it up, we can perform two optimizations:
// 1. If the result of step 1 shows that the number of "merge/removal opportunities" will lead to only one remaining child, we can avoid allocating a new Children Vector, and just merge directly into the child.
// 2. If the result of step 1 shows that the number of "merge/removal opportunities" will lead to more than one remaining child, we can precisely allocate the Children Vector to be (existing children - "merge/removal opportunities").
auto evaluate = [](const Child& a, const Child& b) -> std::pair<Child, double> {
ASSERT(a.index() == b.index());
return WTF::switchOn(a,
[&]<Numeric T>(const T& aNumeric) -> std::pair<Child, double> {
ASSERT(toNumericIdentity(aNumeric) == toNumericIdentity(get<T>(b)));
auto result = executeMathOperation<Sum>(aNumeric.value, get<T>(b).value);
return { makeChildWithValueBasedOn(result, aNumeric), result };
},
[](const auto&) -> std::pair<Child, double> {
ASSERT_NOT_REACHED();
return { makeChild(Number { .value = 0 }), 0 };
}
);
};
// Special case a root with one child to avoid doing any work at all, and just returning the child.
if (root.children.size() == 1)
return { WTFMove(root.children[0]) };
// Map of unit types (via NumericIdentity) to the first index in `root.children` where a value with that unit can be found.
// More specifically, it maps the unit to the index + 1, as 0 is used to indicate no units of that type have been found.
// FIXME: This should be turned into a type with an interface that doesn't require explicit use of static_cast<uint8_t> by the caller.
struct FirstInstance {
size_t offset = 0;
unsigned merges = 0;
bool canRemove = false;
};
std::array<FirstInstance, numberOfNumericIdentityTypes> firstInstances { };
for (size_t i = 0; i < root.children.size(); ++i) {
WTF::switchOn(root.children[i],
[&]<Numeric T>(const T& child) {
auto id = toNumericIdentity(child);
bool canRemoveIfZero = isLength(id) && options.allowZeroValueLengthRemovalFromSum;
if (auto& firstInstance = firstInstances[static_cast<uint8_t>(id)]; firstInstance.offset) {
// There has already been an instance of this type. This is a merge opportunity.
// Calculate the merged value.
auto [mergedChild, mergedValue] = evaluate(root.children[firstInstance.offset - 1], root.children[i]);
// Store the merged value in the original array.
root.children[firstInstance.offset - 1] = WTFMove(mergedChild);
// Update the `merges` count and `canRemove` bit for the new merged value.
firstInstance.merges += 1;
firstInstance.canRemove = canRemoveIfZero && !mergedValue;
return;
}
// First instance of this. Store the index (well, index + 1, since 0 is the unset value) and the canRemove bit.
firstInstances[static_cast<uint8_t>(id)] = {
.offset = i + 1,
.merges = 0,
.canRemove = canRemoveIfZero && !child.value
};
},
[](const auto&) {
// Non-numeric values are not eligible for merge or removal.
}
);
}
// Calculate the total number of children we will be able to remove from merges and removals.
unsigned childrenToRemoveFromMerges = 0;
unsigned childrenToRemoveTotal = 0;
for (auto& firstInstance : firstInstances) {
if (firstInstance.offset) {
childrenToRemoveFromMerges += firstInstance.merges;
childrenToRemoveTotal += firstInstance.merges + (firstInstance.canRemove ? 1 : 0);
}
}
// If there are no merge/removal opportunities, no further simplification is possible.
if (!childrenToRemoveTotal)
return { };
// If all the removal from merges leaves a single child, that means everything merged into the first child.
if ((root.children.size() - childrenToRemoveFromMerges) == 1)
return { WTFMove(root.children[0]) };
auto combinedChildrenSize = root.children.size() - childrenToRemoveTotal;
// If the new size is 0, we removed too much. Return a single 0 value of type `length` to keep things valid. A value of type `length` is returned because the only kind of node that can be removed is of type `length`.
if (!combinedChildrenSize)
return { makeChild(CanonicalDimension { .value = 0, .dimension = CanonicalDimension::Dimension::Length }) };
// If the new size is 1, we know there is one child, we just don't know which one yet.
if (combinedChildrenSize == 1) {
for (size_t i = 0; i < root.children.size(); ++i) {
auto replacement = WTF::switchOn(root.children[i],
[&]<Numeric T>(const T& child) -> std::optional<Child> {
auto& firstInstance = firstInstances[static_cast<uint8_t>(toNumericIdentity(child))];
ASSERT(firstInstance.offset);
// If the stored offset for this type is set to this index and it's not one that can be removed, this is the 1 child to return.
if ((firstInstance.offset - 1) == i && !firstInstance.canRemove)
return { WTFMove(root.children[i]) };
// Otherwise, it's one that can be dropped.
return { };
},
[&](const auto&) -> std::optional<Child> {
return { WTFMove(root.children[i]) };
}
);
if (replacement)
return { WTFMove(*replacement) };
}
}
Vector<Child> combinedChildren;
combinedChildren.reserveInitialCapacity(combinedChildrenSize);
for (size_t i = 0; i < root.children.size(); ++i) {
WTF::switchOn(root.children[i],
[&]<Numeric T>(const T& child) {
auto& firstInstance = firstInstances[static_cast<uint8_t>(toNumericIdentity(child))];
ASSERT(firstInstance.offset);
// If the stored offset for this type is set to this index and it's not one that can be removed, append the child as normal
if ((firstInstance.offset - 1) == i && !firstInstance.canRemove) {
combinedChildren.append(WTFMove(root.children[i]));
return;
}
// Otherwise, it's one that can be dropped.
},
[&](const auto&) {
combinedChildren.append(WTFMove(root.children[i]));
}
);
}
root.children = WTFMove(combinedChildren);
return { };
}
std::optional<Child> simplify(Product& root, const SimplificationOptions& options)
{
ASSERT(!root.children.isEmpty());
// 9. If root is a Product node:
// NOTE: We merge steps 9.1. and 9.2, as they have significant overlap.
// 9.1. For each of root’s children that are Product nodes, replace them with their children.
//
// -- and --
//
// 9.2. If root has multiple children that are numbers (not percentages or dimensions), remove them and replace them with a single number containing the product of the removed nodes.
Vector<Child> newChildren;
std::optional<Number> numericProduct;
auto processChild = [&newChildren, &numericProduct](Child& child) {
if (auto* childValue = get_if<Number>(&child)) {
if (numericProduct)
numericProduct = Number { .value = childValue->value * numericProduct->value };
else
numericProduct = Number { .value = childValue->value };
} else
newChildren.append(WTFMove(child));
};
for (auto& child : root.children) {
if (auto* childProduct = get_if<IndirectNode<Product>>(&child)) {
for (auto& childProductChild : (*childProduct)->children)
processChild(childProductChild);
} else
processChild(child);
}
// If `numericProduct` has a value and `newChildren` is empty, that means all the children were numbers and the product can be returned directly.
if (numericProduct) {
if (newChildren.isEmpty())
return makeChild(*numericProduct);
// 9.3. If root contains only two children, one of which is a number (not a percentage or dimension) and the other of which is a Sum whose children are all numeric values, multiply all of the Sum’s children by the number, then return the Sum.
// We extend this step to include numeric and Invert children for the non-number child as an optimization taking advantage of step 9.4, but for the case where the check is cheaper.
// NOTE: Since we just merged all numeric values into `numericProduct`, we know that if `numericProduct` is not std::nullopt the last child is a singular `number` child. Therefore, we only need to check if there is one child and is a Sum (or Numeric or Invert).
if (newChildren.size() == 1) {
auto replacement = WTF::switchOn(newChildren[0],
[&]<Numeric T>(T& numeric) -> std::optional<Child> {
return makeChildWithValueBasedOn(numeric.value * numericProduct->value, numeric);
},
[&](IndirectNode<Sum>& sum) -> std::optional<Child> {
if (!std::ranges::all_of(sum->children, [](auto& child) { return isNumeric(child); }))
return { };
for (auto& child : sum->children) {
WTF::switchOn(child,
[&]<Numeric T>(T& child) { child.value *= numericProduct->value; },
[](auto&) { }
);
}
return { Child { WTFMove(sum) } };
},
[&](IndirectNode<Invert>& invert) -> std::optional<Child> {
return WTF::switchOn(invert->a,
[&]<Numeric T>(const T& child) -> std::optional<Child> {
return makeChildWithValueBasedOn(child.value * numericProduct->value, child);
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
},
[](auto&) -> std::optional<Child> {
return { };
}
);
if (replacement)
return { WTFMove(*replacement) };
}
// If there was more than one child or no replacement was found, append the product from step 9.2 into the newChildren array.
newChildren.append(makeChild(*numericProduct));
}
root.children = WTFMove(newChildren);
// 9.4. If root contains only numeric values and/or Invert nodes containing numeric values, and multiplying the types of all the children (noting that the type of an Invert node is the inverse of its child’s type) results in a type that matches any of the types that a math function can resolve to, return the result of multiplying all the values of the children (noting that the value of an Invert node is the reciprocal of its child’s value), expressed in the result’s canonical unit.
struct ProductResult {
double value;
Type type;
};
auto productResult = ProductResult { .value = 1, .type = Type { } };
bool success = false;
for (auto& child : root.children) {
success = WTF::switchOn(child,
[&](const Number& number) -> bool {
// <number> is the identity type, so multiplying by it has no effect.
productResult.value *= number.value;
return true;
},
[&](const Percentage& percentage) -> bool {
auto multipliedType = Type::multiply(productResult.type, getType(percentage));
if (!multipliedType)
return false;
productResult.type = *multipliedType;
productResult.value *= percentage.value;
return true;
},
[&](const CanonicalDimension& canonicalDimension) -> bool {
auto multipliedType = Type::multiply(productResult.type, getType(canonicalDimension.dimension));
if (!multipliedType)
return false;
productResult.type = *multipliedType;
productResult.value *= canonicalDimension.value;
return true;
},
[&](IndirectNode<Invert>& invertChild) -> bool {
return WTF::switchOn(invertChild->a,
[&](const Number& number) -> bool {
// <number> is the identity type, so multiplying / inverting by it has no effect.
productResult.value /= number.value;
return true;
},
[&](const Percentage& percentage) -> bool {
auto invertedPercentageChildType = Type::invert(getType(percentage));
auto multipliedType = Type::multiply(productResult.type, invertedPercentageChildType);
if (!multipliedType)
return false;
productResult.type = *multipliedType;
productResult.value /= percentage.value;
return true;
},
[&](const CanonicalDimension& canonicalDimension) -> bool {
auto invertedCanonicalDimensionType = Type::invert(getType(canonicalDimension));
auto multipliedType = Type::multiply(productResult.type, invertedCanonicalDimensionType);
if (!multipliedType)
return false;
productResult.type = *multipliedType;
productResult.value /= canonicalDimension.value;
return true;
},
[](const auto&) -> bool {
return false;
}
);
},
[](const auto&) -> bool {
return false;
}
);
if (!success)
break;
}
if (success) {
if (auto category = productResult.type.calculationCategory()) {
switch (*category) {
case Calculation::Category::Integer:
case Calculation::Category::Number:
return makeChild(Number { .value = productResult.value });
case Calculation::Category::Percentage:
return makeChild(Percentage { .value = productResult.value, .hint = Type::determinePercentHint(options.category) });
case Calculation::Category::LengthPercentage:
return makeChild(Percentage { .value = productResult.value, .hint = PercentHint::Length });
case Calculation::Category::Length:
return makeChild(CanonicalDimension { .value = productResult.value, .dimension = CanonicalDimension::Dimension::Length });
case Calculation::Category::Angle:
return makeChild(CanonicalDimension { .value = productResult.value, .dimension = CanonicalDimension::Dimension::Angle });
case Calculation::Category::AnglePercentage:
return makeChild(Percentage { .value = productResult.value, .hint = PercentHint::Angle });
case Calculation::Category::Time:
return makeChild(CanonicalDimension { .value = productResult.value, .dimension = CanonicalDimension::Dimension::Time });
case Calculation::Category::Frequency:
return makeChild(CanonicalDimension { .value = productResult.value, .dimension = CanonicalDimension::Dimension::Frequency });
case Calculation::Category::Resolution:
return makeChild(CanonicalDimension { .value = productResult.value, .dimension = CanonicalDimension::Dimension::Resolution });
case Calculation::Category::Flex:
return makeChild(CanonicalDimension { .value = productResult.value, .dimension = CanonicalDimension::Dimension::Flex });
}
}
}
// 9.5. Return root.
return { };
}
std::optional<Child> simplify(Negate& root, const SimplificationOptions&)
{
// 6. If root is a Negate node:
return WTF::switchOn(root.a,
[&]<Numeric T>(T& a) -> std::optional<Child> {
// 6.1. If root’s child is a numeric value, return an equivalent numeric value, but with the value negated (0 - value).
return makeChildWithValueBasedOn(0.0 - a.value, a);
},
[](IndirectNode<Negate>& a) -> std::optional<Child> {
// 6.2. If root’s child is a Negate node, return the child’s child.
return { WTFMove(a->a) };
},
[](IndirectNode<Sum>& a) -> std::optional<Child> {
// Not stated in spec, but needed for tests.
if (!std::ranges::all_of(a->children, [](auto& child) { return isNumeric(child); }))
return { };
for (auto& child : a->children) {
WTF::switchOn(child,
[&]<Numeric T>(T& child) { child.value = -child.value; },
[](auto&) { }
);
}
return { Child { WTFMove(a) } };
},
[](IndirectNode<Product>& a) -> std::optional<Child> {
// Not stated in spec, but needed for tests.
if (!std::ranges::all_of(a->children, [](auto& child) { return isNumeric(child); }))
return { };
for (auto& child : a->children) {
WTF::switchOn(child,
[&]<Numeric T>(T& child) { child.value = -child.value; },
[](auto&) { }
);
}
return { Child { WTFMove(a) } };
},
[](auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Invert& root, const SimplificationOptions&)
{
// 7. If root is an Invert node:
return WTF::switchOn(root.a,
[&](Number& a) -> std::optional<Child> {
// 7.1. If root’s child is a number (not a percentage or dimension) return the reciprocal of the child’s value.
return makeChild(Number { .value = (1.0 / a.value) });
},
[](IndirectNode<Invert>& a) -> std::optional<Child> {
// 7.2. If root’s child is an Invert node, return the child’s child.
return { WTFMove(a->a) };
},
[](auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Min& root, const SimplificationOptions& options)
{
return simplifyForMinMax(root, options);
}
std::optional<Child> simplify(Max& root, const SimplificationOptions& options)
{
return simplifyForMinMax(root, options);
}
std::optional<Child> simplify(Clamp& root, const SimplificationOptions& options)
{
auto minIsNone = WTF::holdsAlternative<CSS::Keyword::None>(root.min);
auto maxIsNone = WTF::holdsAlternative<CSS::Keyword::None>(root.max);
if (minIsNone && maxIsNone) {
// - clamp(none, VAL, none) is equivalent to just calc(VAL).
return { WTFMove(root.val) };
}
// FIXME: Are any of these transforms kosher?
// If only MIN and VAL have matching units, we can transform clamp(MIN, VAL, MAX) aka (max(MIN, min(VAL, MAX)) into a min(newVAL, MAX).
// If only VAL and MAX have matching units, we can transform clamp(MIN, VAL, MAX) aka (max(MIN, min(VAL, MAX)) into a max(MIN, newVAL).
return WTF::switchOn(root.val,
[&]<Numeric T>(T& val) -> std::optional<Child> {
if (minIsNone) {
auto& maxChild = get<Child>(root.max);
if (!WTF::holdsAlternative<T>(maxChild))
return { };
auto& max = get<T>(maxChild);
if (!unitsMatch(val, max, options))
return { };
// As units already match, we only have to check that one of the arguments is `magnitudeComparable`.
if (!magnitudeComparable(val, options))
return { };
// - clamp(none, VAL, MAX) is equivalent to min(VAL, MAX)
return makeChildWithValueBasedOn(executeMathOperation<Min>(val.value, max.value), val);
} else if (maxIsNone) {
auto& minChild = get<Child>(root.min);
if (!WTF::holdsAlternative<T>(minChild))
return { };
auto& min = get<T>(minChild);
if (!unitsMatch(min, val, options))
return { };
// As units already match, we only have to check that one of the arguments is `magnitudeComparable`.
if (!magnitudeComparable(val, options))
return { };
// - clamp(MIN, VAL, none) is equivalent to max(MIN, VAL)
return makeChildWithValueBasedOn(executeMathOperation<Max>(min.value, val.value), val);
} else {
auto& minChild = get<Child>(root.min);
auto& maxChild = get<Child>(root.max);
// If all three parameters have the same unit, we can perform the clamp in full.
if (!WTF::holdsAlternative<T>(minChild) || !WTF::holdsAlternative<T>(maxChild))
return { };
auto& min = get<T>(minChild);
auto& max = get<T>(maxChild);
if (!unitsMatch(min, val, options) || !unitsMatch(val, max, options))
return { };
// As units already match, we only have to check that one of the arguments is `magnitudeComparable`.
if (!magnitudeComparable(val, options))
return { };
return makeChildWithValueBasedOn(executeMathOperation<Clamp>(min.value, val.value, max.value), val);
}
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(RoundNearest& root, const SimplificationOptions& options)
{
return simplifyForRound(root, options);
}
std::optional<Child> simplify(RoundUp& root, const SimplificationOptions& options)
{
return simplifyForRound(root, options);
}
std::optional<Child> simplify(RoundDown& root, const SimplificationOptions& options)
{
return simplifyForRound(root, options);
}
std::optional<Child> simplify(RoundToZero& root, const SimplificationOptions& options)
{
return simplifyForRound(root, options);
}
std::optional<Child> simplify(Mod& root, const SimplificationOptions& options)
{
return simplifyForOperation<Mod>(root.a, root.b, options);
}
std::optional<Child> simplify(Rem& root, const SimplificationOptions& options)
{
return simplifyForOperation<Rem>(root.a, root.b, options);
}
std::optional<Child> simplify(Sin& root, const SimplificationOptions& options)
{
return simplifyForTrig(root, options);
}
std::optional<Child> simplify(Cos& root, const SimplificationOptions& options)
{
return simplifyForTrig(root, options);
}
std::optional<Child> simplify(Tan& root, const SimplificationOptions& options)
{
return simplifyForTrig(root, options);
}
std::optional<Child> simplify(Asin& root, const SimplificationOptions& options)
{
return simplifyForArcTrig(root, options);
}
std::optional<Child> simplify(Acos& root, const SimplificationOptions& options)
{
return simplifyForArcTrig(root, options);
}
std::optional<Child> simplify(Atan& root, const SimplificationOptions& options)
{
return simplifyForArcTrig(root, options);
}
std::optional<Child> simplify(Atan2& root, const SimplificationOptions& options)
{
return simplifyForOperationWithCompletion<Atan2>(root.a, root.b, options, [](double value) {
return makeChild(CanonicalDimension { .value = value, .dimension = CanonicalDimension::Dimension::Angle });
});
}
std::optional<Child> simplify(Pow& root, const SimplificationOptions&)
{
// NOTE: `a` and `b` have been type checked by this point to be `<number>`, though they may not
// be able to be fully resolved yet.
return switchTogether(root.a, root.b,
[&](const Number& a, const Number& b) -> std::optional<Child> {
return makeChild(Number { .value = executeMathOperation<Pow>(a.value, b.value) });
},
[](const auto&, const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Sqrt& root, const SimplificationOptions&)
{
// NOTE: `a` has been type checked by this point to be `<number>`, though they may not
// be able to be fully resolved yet.
return WTF::switchOn(root.a,
[&](const Number& a) -> std::optional<Child> {
return makeChild(Number { .value = executeMathOperation<Sqrt>(a.value) });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Hypot& root, const SimplificationOptions& options)
{
// Hypot can be simplified if all its children are the same type, and it is both canonical (for lengths) and fully resolved (for percentages). We optimistically assume that the children fit this criteria, and execute the operation over the children, checking each one as it is requested. If we find out our assumption was incorrect (e.g. a child is non-canonical or non-resolved), we set a flag indicating the evaluation failed, but due to the evaluation API's interface, must evaluate all the remaining children. Once the evaluation is complete, if the fail bit is set, we failed to simplify, if it is not, we can return the new numeric result.
struct NumberTag { };
struct PercentageTag { };
struct DimensionTag { CanonicalDimension::Dimension dimension; };
struct FailureTag { };
std::variant<std::monostate, NumberTag, PercentageTag, DimensionTag, FailureTag> result;
double value = executeMathOperation<Hypot>(root.children.value, [&](const auto& child) {
return WTF::switchOn(result,
[&](const std::monostate&) -> double {
// First iteration.
return WTF::switchOn(child,
[&](const Number& number) -> double {
result = NumberTag { };
return number.value;
},
[&](const Percentage& percentage) -> double {
if (percentageResolveToDimension(options)) {
result = FailureTag { };
return std::numeric_limits<double>::quiet_NaN();
}
result = PercentageTag { };
return percentage.value;
},
[&](const CanonicalDimension& dimension) -> double {
result = DimensionTag { dimension.dimension };
return dimension.value;
},
[&](const auto&) -> double {
result = FailureTag { };
return std::numeric_limits<double>::quiet_NaN();
}
);
},
[&](const NumberTag&) -> double {
if (auto* numberChild = get_if<Number>(&child))
return numberChild->value;
result = FailureTag { };
return std::numeric_limits<double>::quiet_NaN();
},
[&](const PercentageTag&) -> double {
if (auto* percentageChild = get_if<Percentage>(&child))
return percentageChild->value;
result = FailureTag { };
return std::numeric_limits<double>::quiet_NaN();
},
[&](const DimensionTag& tag) -> double {
if (auto* dimensionChild = get_if<CanonicalDimension>(&child); dimensionChild && dimensionChild->dimension == tag.dimension)
return dimensionChild->value;
result = FailureTag { };
return std::numeric_limits<double>::quiet_NaN();
},
[&](const FailureTag&) -> double {
return std::numeric_limits<double>::quiet_NaN();
}
);
});
return WTF::switchOn(result,
[&](const NumberTag&) -> std::optional<Child> {
return makeChild(Number { .value = value });
},
[&](const PercentageTag&) -> std::optional<Child> {
return makeChild(Percentage { .value = value, .hint = Type::determinePercentHint(options.category) });
},
[&](const DimensionTag& tag) -> std::optional<Child> {
return makeChild(CanonicalDimension { .value = value, .dimension = tag.dimension });
},
[&](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Log& root, const SimplificationOptions&)
{
// NOTE: `a` and `b` have been type checked by this point to be `<number>`, though they may not
// be able to be fully resolved yet.
if (root.b) {
return switchTogether(root.a, *root.b,
[&](const Number& a, const Number& b) -> std::optional<Child> {
return makeChild(Number { .value = executeMathOperation<Log>(a.value, b.value) });
},
[](const auto&, const auto&) -> std::optional<Child> {
return { };
}
);
}
return WTF::switchOn(root.a,
[](const Number& a) -> std::optional<Child> {
return makeChild(Number { .value = executeMathOperation<Log>(a.value) });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Exp& root, const SimplificationOptions&)
{
// NOTE: `a` has been type checked by this point to be `<number>`, though they may not
// be able to be fully resolved yet.
return WTF::switchOn(root.a,
[](const Number& a) -> std::optional<Child> {
return makeChild(Number { .value = executeMathOperation<Exp>(a.value) });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Abs& root, const SimplificationOptions& options)
{
return WTF::switchOn(root.a,
[&]<Numeric T>(const T& a) -> std::optional<Child> {
if (!magnitudeComparable(a, options))
return { };
return makeChildWithValueBasedOn(executeMathOperation<Abs>(a.value), a);
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Sign& root, const SimplificationOptions& options)
{
return WTF::switchOn(root.a,
[&]<Numeric T>(const T& a) -> std::optional<Child> {
if (!magnitudeComparable(a, options))
return { };
return makeChild(Number { .value = executeMathOperation<Sign>(a.value) });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Random& root, const SimplificationOptions& options)
{
if (!options.conversionData || !options.conversionData->styleBuilderState())
return { };
if (root.cachingOptions.perElement && !options.conversionData->styleBuilderState()->element())
return { };
if (root.min.index() != root.max.index() || (root.step && root.step->index() != root.min.index()))
return { };
return WTF::switchOn(root.min,
[&]<Numeric T>(const T& numericMin) -> std::optional<Child> {
auto numericMax = get<T>(root.max);
if (!unitsMatch(numericMin, numericMax, options) || !fullyResolved(numericMin, options))
return { };
std::optional<double> valueStep;
if (root.step) {
auto numericStep = get<T>(*root.step);
if (!unitsMatch(numericMin, numericStep, options))
return { };
valueStep = numericStep.value;
}
auto valueMin = numericMin.value;
auto valueMax = numericMax.value;
// RandomKeyMap relies on using NaN for HashTable deleted/empty values but
// the result is always NaN if either is NaN, so we can return early here.
if (std::isnan(valueMin) || std::isnan(valueMax))
return makeChildWithValueBasedOn(std::numeric_limits<double>::quiet_NaN(), numericMin);
auto keyMap = options.conversionData->styleBuilderState()->randomKeyMap(
root.cachingOptions.perElement
);
auto randomUnitInterval = keyMap->lookupUnitInterval(
root.cachingOptions.identifier,
valueMin,
valueMax,
valueStep
);
auto result = Calculation::executeOperation<ToCalculationTreeOp<Random>>(
randomUnitInterval,
valueMin,
valueMax,
valueStep
);
return makeChildWithValueBasedOn(result, numericMin);
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
return { };
}
std::optional<Child> simplify(Progress& root, const SimplificationOptions& options)
{
if (root.value.index() != root.start.index() || root.start.index() != root.end.index())
return { };
return WTF::switchOn(root.value,
[&]<Numeric T>(const T& numericValue) -> std::optional<Child> {
const auto& numericStart = get<T>(root.start);
const auto& numericEnd = get<T>(root.end);
if (!unitsMatch(numericValue, numericStart, options) || !unitsMatch(numericStart, numericEnd, options) || !fullyResolved(numericValue, options))
return { };
return makeChild(Number { .value = executeMathOperation<Progress>(numericValue.value, numericStart.value, numericEnd.value) });
},
[](const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(MediaProgress& root, const SimplificationOptions& options)
{
ASSERT(root.feature->category() == options.category);
if (!options.conversionData || !options.conversionData->styleBuilderState())
return { };
return switchTogether(root.start, root.end,
[&]<Numeric T>(const T& start, const T& end) -> std::optional<Child> {
if (!unitsMatch(start, end, options) || !fullyResolved(start, options))
return { };
Ref document = options.conversionData->styleBuilderState()->document();
auto value = evaluateMediaProgress(root, document, *options.conversionData);
return makeChild(Number { .value = executeMathOperation<Progress>(value, start.value, end.value) });
},
[](const auto&, const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(ContainerProgress& root, const SimplificationOptions& options)
{
ASSERT(root.feature->category() == options.category);
if (!options.conversionData || !options.conversionData->styleBuilderState() || !options.conversionData->styleBuilderState()->element())
return { };
return switchTogether(root.start, root.end,
[&]<Numeric T>(const T& start, const T& end) -> std::optional<Child> {
if (!unitsMatch(start, end, options) || !fullyResolved(start, options))
return { };
Ref element = *options.conversionData->styleBuilderState()->element();
auto value = evaluateContainerProgress(root, element, *options.conversionData);
if (!value)
return { };
return makeChild(Number { .value = executeMathOperation<Progress>(*value, start.value, end.value) });
},
[](const auto&, const auto&) -> std::optional<Child> {
return { };
}
);
}
std::optional<Child> simplify(Anchor& anchor, const SimplificationOptions& options)
{
if (!options.conversionData || !options.conversionData->styleBuilderState())
return { };
auto evaluationOptions = EvaluationOptions {
.category = options.category,
.range = CSS::All,
.conversionData = options.conversionData,
.symbolTable = options.symbolTable
};
auto result = evaluateWithoutFallback(anchor, evaluationOptions);
if (!result) {
// https://drafts.csswg.org/css-anchor-position-1/#anchor-valid
// "If any of these conditions are false, the anchor() function resolves to its specified fallback value.
// If no fallback value is specified, it makes the declaration referencing it invalid at computed-value time."
if (!anchor.fallback)
options.conversionData->styleBuilderState()->setCurrentPropertyInvalidAtComputedValueTime();
// Replace the anchor node with the fallback node.
return std::exchange(anchor.fallback, { });
}
return CanonicalDimension { .value = *result, .dimension = CanonicalDimension::Dimension::Length };
}
std::optional<Child> simplify(AnchorSize& anchorSize, const SimplificationOptions& options)
{
if (!options.conversionData || !options.conversionData->styleBuilderState())
return { };
auto& builderState = *options.conversionData->styleBuilderState();
std::optional<Style::ScopedName> anchorSizeScopedName;
if (!anchorSize.elementName.isNull()) {
anchorSizeScopedName = Style::ScopedName {
.name = anchorSize.elementName,
.scopeOrdinal = builderState.styleScopeOrdinal()
};
}
auto result = Style::AnchorPositionEvaluator::evaluateSize(builderState, anchorSizeScopedName, anchorSize.dimension);
if (!result) {
if (!anchorSize.fallback)
options.conversionData->styleBuilderState()->setCurrentPropertyInvalidAtComputedValueTime();
return std::exchange(anchorSize.fallback, { });
}
return CanonicalDimension { .value = *result, .dimension = CanonicalDimension::Dimension::Length };
}
// MARK: Copy & Simplify.
const MQ::MediaProgressProviding* copyAndSimplify(const MQ::MediaProgressProviding* root, const SimplificationOptions&)
{
return root;
}
const CQ::ContainerProgressProviding* copyAndSimplify(const CQ::ContainerProgressProviding* root, const SimplificationOptions&)
{
return root;
}
Random::CachingOptions copyAndSimplify(const Random::CachingOptions& root, const SimplificationOptions&)
{
return root;
}
AtomString copyAndSimplify(const AtomString& root, const SimplificationOptions&)
{
return root;
}
CSS::Keyword::None copyAndSimplify(const CSS::Keyword::None& root, const SimplificationOptions&)
{
return root;
}
Children copyAndSimplify(const Children& children, const SimplificationOptions& options)
{
return WTF::map(children, [&](auto& child) { return copyAndSimplify(child, options); });
}
auto copyAndSimplify(const ChildOrNone& root, const SimplificationOptions& options) -> ChildOrNone
{
return WTF::switchOn(root, [&](auto& root) { return ChildOrNone { copyAndSimplify(root, options) }; });
}
template<typename T> auto copyAndSimplify(const std::optional<T>& root, const SimplificationOptions& options) -> std::optional<T>
{
if (root)
return copyAndSimplify(*root, options);
return { };
}
template<Leaf Op> static auto copyAndSimplifyChildren(const Op& op, const SimplificationOptions&) -> Op
{
return op;
}
template<typename Op> static auto copyAndSimplifyChildren(const IndirectNode<Op>& root, const SimplificationOptions& options) -> Op
{
return WTF::apply([&](const auto& ...x) { return Op { copyAndSimplify(x, options)... }; } , *root);
}
static auto copyAndSimplifyChildren(const IndirectNode<MediaProgress>& root, const SimplificationOptions& options) -> MediaProgress
{
// Modify the category to match the media-progress() category following non-"math function" rules.
// FIXME: Catching cases like this would be a good reason to make non-"math function" nodes distinct, perhaps even using an explicitly nested Tree in some fashion.
SimplificationOptions nestedOptions = options;
nestedOptions.category = root->feature->category();
return WTF::apply([&](const auto& ...x) { return MediaProgress { copyAndSimplify(x, nestedOptions)... }; } , *root);
}
static auto copyAndSimplifyChildren(const IndirectNode<ContainerProgress>& root, const SimplificationOptions& options) -> ContainerProgress
{
// Modify the category to match the container-progress() category following non-"math function" rules.
// FIXME: Catching cases like this would be a good reason to make non-"math function" nodes distinct, perhaps even using an explicitly nested Tree in some fashion.
SimplificationOptions nestedOptions = options;
nestedOptions.category = root->feature->category();
return WTF::apply([&](const auto& ...x) { return ContainerProgress { copyAndSimplify(x, nestedOptions)... }; } , *root);
}
static auto copyAndSimplifyChildren(const IndirectNode<Anchor>& anchor, const SimplificationOptions& options) -> Anchor
{
return Anchor { .elementName = anchor->elementName, .side = copy(anchor->side), .fallback = copyAndSimplify(anchor->fallback, options) };
}
static auto copyAndSimplifyChildren(const IndirectNode<AnchorSize>& anchorSize, const SimplificationOptions& options) -> AnchorSize
{
return AnchorSize {
.elementName = anchorSize->elementName,
.dimension = anchorSize->dimension,
.fallback = copyAndSimplify(anchorSize->fallback, options)
};
}
Child copyAndSimplify(const Child& root, const SimplificationOptions& options)
{
return WTF::switchOn(root,
[&](const auto& root) -> Child {
// Create a simplified copy by recursively calling simplify on all children.
auto simplified = copyAndSimplifyChildren(root, options);
// Attempt to simplify the term itself, using the result as a replacement if successful.
if (auto replacement = simplify(simplified, options))
return WTFMove(*replacement);
return makeChild(WTFMove(simplified), getType(root));
}
);
}
Tree copyAndSimplify(const Tree& tree, const SimplificationOptions& options)
{
return Tree {
.root = copyAndSimplify(tree.root, options),
.type = tree.type,
.stage = tree.stage,
.requiresConversionData = tree.requiresConversionData,
.unique = tree.unique,
};
}
// MARK: - Can Simplify
bool canSimplify(const Tree& tree, const SimplificationOptions&)
{
// NOTE: This is a simple and conservative implementation of `canSimplify`. A more precise implementation
// is possible by utilizing the provided `SimplificationOptions` if that should be necessary.
return WTF::switchOn(tree.root,
[&](const Number&) -> bool {
return false;
},
[&](const Percentage&) -> bool {
return false;
},
[&](const CanonicalDimension&) -> bool {
return false;
},
[&](auto const&) -> bool {
return true;
}
);
}
} // namespace CSSCalc
} // namespace WebCore
|