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 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! [Calc expressions][calc].
//!
//! [calc]: https://drafts.csswg.org/css-values/#calc-notation
use num_traits::Zero;
use smallvec::SmallVec;
use std::fmt::{self, Write};
use std::ops::{Add, Mul, Neg, Rem, Sub};
use std::{cmp, mem};
use style_traits::{CssWriter, ToCss};
/// Whether we're a `min` or `max` function.
#[derive(
Clone,
Copy,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
Serialize,
ToAnimatedZero,
ToResolvedValue,
ToShmem,
)]
#[repr(u8)]
pub enum MinMaxOp {
/// `min()`
Min,
/// `max()`
Max,
}
/// Whether we're a `mod` or `rem` function.
#[derive(
Clone,
Copy,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
Serialize,
ToAnimatedZero,
ToResolvedValue,
ToShmem,
)]
#[repr(u8)]
pub enum ModRemOp {
/// `mod()`
Mod,
/// `rem()`
Rem,
}
impl ModRemOp {
fn apply(self, dividend: f32, divisor: f32) -> f32 {
// In mod(A, B) only, if B is infinite and A has opposite sign to B
// (including an oppositely-signed zero), the result is NaN.
// https://drafts.csswg.org/css-values/#round-infinities
if matches!(self, Self::Mod) &&
divisor.is_infinite() &&
dividend.is_sign_negative() != divisor.is_sign_negative()
{
return f32::NAN;
}
let (r, same_sign_as) = match self {
Self::Mod => (dividend - divisor * (dividend / divisor).floor(), divisor),
Self::Rem => (dividend - divisor * (dividend / divisor).trunc(), dividend),
};
if r == 0.0 && same_sign_as.is_sign_negative() {
-0.0
} else {
r
}
}
}
/// The strategy used in `round()`
#[derive(
Clone,
Copy,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
Serialize,
ToAnimatedZero,
ToResolvedValue,
ToShmem,
)]
#[repr(u8)]
pub enum RoundingStrategy {
/// `round(nearest, a, b)`
/// round a to the nearest multiple of b
Nearest,
/// `round(up, a, b)`
/// round a up to the nearest multiple of b
Up,
/// `round(down, a, b)`
/// round a down to the nearest multiple of b
Down,
/// `round(to-zero, a, b)`
/// round a to the nearest multiple of b that is towards zero
ToZero,
}
/// This determines the order in which we serialize members of a calc() sum.
///
/// See https://drafts.csswg.org/css-values-4/#sort-a-calculations-children
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[allow(missing_docs)]
pub enum SortKey {
Number,
Percentage,
Cap,
Ch,
Cqb,
Cqh,
Cqi,
Cqmax,
Cqmin,
Cqw,
Deg,
Dppx,
Dvb,
Dvh,
Dvi,
Dvmax,
Dvmin,
Dvw,
Em,
Ex,
Ic,
Lh,
Lvb,
Lvh,
Lvi,
Lvmax,
Lvmin,
Lvw,
Px,
Rem,
Rlh,
Sec,
Svb,
Svh,
Svi,
Svmax,
Svmin,
Svw,
Vb,
Vh,
Vi,
Vmax,
Vmin,
Vw,
Other,
}
/// A generic node in a calc expression.
///
/// FIXME: This would be much more elegant if we used `Self` in the types below,
/// but we can't because of https://github.com/serde-rs/serde/issues/1565.
///
/// FIXME: The following annotations are to workaround an LLVM inlining bug, see
/// bug 1631929.
///
/// cbindgen:destructor-attributes=MOZ_NEVER_INLINE
/// cbindgen:copy-constructor-attributes=MOZ_NEVER_INLINE
/// cbindgen:eq-attributes=MOZ_NEVER_INLINE
#[repr(u8)]
#[derive(
Clone,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
Serialize,
ToAnimatedZero,
ToResolvedValue,
ToShmem,
)]
pub enum GenericCalcNode<L> {
/// A leaf node.
Leaf(L),
/// A node that negates its child, e.g. Negate(1) == -1.
Negate(Box<GenericCalcNode<L>>),
/// A node that inverts its child, e.g. Invert(10) == 1 / 10 == 0.1. The child must always
/// resolve to a number unit.
Invert(Box<GenericCalcNode<L>>),
/// A sum node, representing `a + b + c` where a, b, and c are the
/// arguments.
Sum(crate::OwnedSlice<GenericCalcNode<L>>),
/// A product node, representing `a * b * c` where a, b, and c are the
/// arguments.
Product(crate::OwnedSlice<GenericCalcNode<L>>),
/// A `min` or `max` function.
MinMax(crate::OwnedSlice<GenericCalcNode<L>>, MinMaxOp),
/// A `clamp()` function.
Clamp {
/// The minimum value.
min: Box<GenericCalcNode<L>>,
/// The central value.
center: Box<GenericCalcNode<L>>,
/// The maximum value.
max: Box<GenericCalcNode<L>>,
},
/// A `round()` function.
Round {
/// The rounding strategy.
strategy: RoundingStrategy,
/// The value to round.
value: Box<GenericCalcNode<L>>,
/// The step value.
step: Box<GenericCalcNode<L>>,
},
/// A `mod()` or `rem()` function.
ModRem {
/// The dividend calculation.
dividend: Box<GenericCalcNode<L>>,
/// The divisor calculation.
divisor: Box<GenericCalcNode<L>>,
/// Is the function mod or rem?
op: ModRemOp,
},
/// A `hypot()` function
Hypot(crate::OwnedSlice<GenericCalcNode<L>>),
/// An `abs()` function.
Abs(Box<GenericCalcNode<L>>),
/// A `sign()` function.
Sign(Box<GenericCalcNode<L>>),
}
pub use self::GenericCalcNode as CalcNode;
bitflags! {
/// Expected units we allow parsing within a `calc()` expression.
///
/// This is used as a hint for the parser to fast-reject invalid
/// expressions. Numbers are always allowed because they multiply other
/// units.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct CalcUnits: u8 {
/// <length>
const LENGTH = 1 << 0;
/// <percentage>
const PERCENTAGE = 1 << 1;
/// <angle>
const ANGLE = 1 << 2;
/// <time>
const TIME = 1 << 3;
/// <resolution>
const RESOLUTION = 1 << 4;
/// A component of a color (r, g, b, h, s, l, alpha, etc.)
const COLOR_COMPONENT = 1 << 5;
/// <length-percentage>
const LENGTH_PERCENTAGE = Self::LENGTH.bits() | Self::PERCENTAGE.bits();
// NOTE: When you add to this, make sure to make Atan2 deal with these.
/// Allow all units.
const ALL = Self::LENGTH.bits() | Self::PERCENTAGE.bits() | Self::ANGLE.bits() |
Self::TIME.bits() | Self::RESOLUTION.bits() | Self::COLOR_COMPONENT.bits();
}
}
impl CalcUnits {
/// Returns whether the flags only represent a single unit. This will return true for 0, which
/// is a "number" this is also fine.
#[inline]
fn is_single_unit(&self) -> bool {
self.bits() == 0 || self.bits() & (self.bits() - 1) == 0
}
/// Returns true if this unit is allowed to be summed with the given unit, otherwise false.
#[inline]
fn can_sum_with(&self, other: Self) -> bool {
match *self {
Self::LENGTH => other.intersects(Self::LENGTH | Self::PERCENTAGE),
Self::PERCENTAGE => other.intersects(Self::LENGTH | Self::PERCENTAGE),
Self::LENGTH_PERCENTAGE => other.intersects(Self::LENGTH | Self::PERCENTAGE),
u => u.is_single_unit() && other == u,
}
}
}
/// For percentage resolution, sometimes we can't assume that the percentage basis is positive (so
/// we don't know whether a percentage is larger than another).
pub enum PositivePercentageBasis {
/// The percent basis is not known-positive, we can't compare percentages.
Unknown,
/// The percent basis is known-positive, we assume larger percentages are larger.
Yes,
}
macro_rules! compare_helpers {
() => {
/// Return whether a leaf is greater than another.
#[allow(unused)]
fn gt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool {
self.compare(other, basis_positive) == Some(cmp::Ordering::Greater)
}
/// Return whether a leaf is less than another.
fn lt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool {
self.compare(other, basis_positive) == Some(cmp::Ordering::Less)
}
/// Return whether a leaf is smaller or equal than another.
fn lte(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool {
match self.compare(other, basis_positive) {
Some(cmp::Ordering::Less) => true,
Some(cmp::Ordering::Equal) => true,
Some(cmp::Ordering::Greater) => false,
None => false,
}
}
};
}
/// A trait that represents all the stuff a valid leaf of a calc expression.
pub trait CalcNodeLeaf: Clone + Sized + PartialEq + ToCss {
/// Returns the unit of the leaf.
fn unit(&self) -> CalcUnits;
/// Returns the unitless value of this leaf if one is available.
fn unitless_value(&self) -> Option<f32>;
/// Return true if the units of both leaves are equal. (NOTE: Does not take
/// the values into account)
fn is_same_unit_as(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
/// Do a partial comparison of these values.
fn compare(
&self,
other: &Self,
base_is_positive: PositivePercentageBasis,
) -> Option<cmp::Ordering>;
compare_helpers!();
/// Create a new leaf with a number value.
fn new_number(value: f32) -> Self;
/// Returns a float value if the leaf is a number.
fn as_number(&self) -> Option<f32>;
/// Whether this value is known-negative.
fn is_negative(&self) -> Result<bool, ()> {
self.unitless_value()
.map(|v| Ok(v.is_sign_negative()))
.unwrap_or_else(|| Err(()))
}
/// Whether this value is infinite.
fn is_infinite(&self) -> Result<bool, ()> {
self.unitless_value()
.map(|v| Ok(v.is_infinite()))
.unwrap_or_else(|| Err(()))
}
/// Whether this value is zero.
fn is_zero(&self) -> Result<bool, ()> {
self.unitless_value()
.map(|v| Ok(v.is_zero()))
.unwrap_or_else(|| Err(()))
}
/// Whether this value is NaN.
fn is_nan(&self) -> Result<bool, ()> {
self.unitless_value()
.map(|v| Ok(v.is_nan()))
.unwrap_or_else(|| Err(()))
}
/// Tries to merge one leaf into another using the sum, that is, perform `x` + `y`.
fn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()>;
/// Try to merge the right leaf into the left by using a multiplication. Return true if the
/// merge was successful, otherwise false.
fn try_product_in_place(&mut self, other: &mut Self) -> bool;
/// Tries a generic arithmetic operation.
fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>
where
O: Fn(f32, f32) -> f32;
/// Map the value of this node with the given operation.
fn map(&mut self, op: impl FnMut(f32) -> f32) -> Result<(), ()>;
/// Canonicalizes the expression if necessary.
fn simplify(&mut self);
/// Returns the sort key for simplification.
fn sort_key(&self) -> SortKey;
/// Create a new leaf containing the sign() result of the given leaf.
fn sign_from(leaf: &impl CalcNodeLeaf) -> Result<Self, ()> {
let Some(value) = leaf.unitless_value() else {
return Err(());
};
Ok(Self::new_number(if value.is_nan() {
f32::NAN
} else if value.is_zero() {
value
} else if value.is_sign_negative() {
-1.0
} else {
1.0
}))
}
}
/// The level of any argument being serialized in `to_css_impl`.
enum ArgumentLevel {
/// The root of a calculation tree.
CalculationRoot,
/// The root of an operand node's argument, e.g. `min(10, 20)`, `10` and `20` will have this
/// level, but min in this case will have `TopMost`.
ArgumentRoot,
/// Any other values serialized in the tree.
Nested,
}
impl<L: CalcNodeLeaf> CalcNode<L> {
/// Create a dummy CalcNode that can be used to do replacements of other nodes.
fn dummy() -> Self {
Self::MinMax(Default::default(), MinMaxOp::Max)
}
/// Change all the leaf nodes to have the given value. This is useful when
/// you have `calc(1px * nan)` and you want to replace the product node with
/// `calc(nan)`, in which case the unit will be retained.
fn coerce_to_value(&mut self, value: f32) -> Result<(), ()> {
self.map(|_| value)
}
/// Return true if a product is distributive over this node.
/// Is distributive: (2 + 3) * 4 = 8 + 12
/// Not distributive: sign(2 + 3) * 4 != sign(8 + 12)
#[inline]
pub fn is_product_distributive(&self) -> bool {
match self {
Self::Leaf(l) => l.unit() != CalcUnits::COLOR_COMPONENT,
Self::Sum(children) => children.iter().all(|c| c.is_product_distributive()),
_ => false,
}
}
/// If the node has a valid unit outcome, then return it, otherwise fail.
pub fn unit(&self) -> Result<CalcUnits, ()> {
Ok(match self {
CalcNode::Leaf(l) => l.unit(),
CalcNode::Negate(child) | CalcNode::Invert(child) | CalcNode::Abs(child) => {
child.unit()?
},
CalcNode::Sum(children) => {
let mut unit = children.first().unwrap().unit()?;
for child in children.iter().skip(1) {
let child_unit = child.unit()?;
if !child_unit.can_sum_with(unit) {
return Err(());
}
unit |= child_unit;
}
unit
},
CalcNode::Product(children) => {
// Only one node is allowed to have a unit, the rest must be numbers.
let mut unit = None;
for child in children.iter() {
let child_unit = child.unit()?;
if child_unit.is_empty() {
// Numbers are always allowed in a product, so continue with the next.
continue;
}
if unit.is_some() {
// We already have a unit for the node, so another unit node is invalid.
return Err(());
}
// We have the unit for the node.
unit = Some(child_unit);
}
// We only keep track of specified units, so if we end up with a None and no failure
// so far, then we have a number.
unit.unwrap_or(CalcUnits::empty())
},
CalcNode::MinMax(children, _) | CalcNode::Hypot(children) => {
let mut unit = children.first().unwrap().unit()?;
for child in children.iter().skip(1) {
let child_unit = child.unit()?;
if !child_unit.can_sum_with(unit) {
return Err(());
}
unit |= child_unit;
}
unit
},
CalcNode::Clamp { min, center, max } => {
let min_unit = min.unit()?;
let center_unit = center.unit()?;
if !min_unit.can_sum_with(center_unit) {
return Err(());
}
let max_unit = max.unit()?;
if !center_unit.can_sum_with(max_unit) {
return Err(());
}
min_unit | center_unit | max_unit
},
CalcNode::Round { value, step, .. } => {
let value_unit = value.unit()?;
let step_unit = step.unit()?;
if !step_unit.can_sum_with(value_unit) {
return Err(());
}
value_unit | step_unit
},
CalcNode::ModRem {
dividend, divisor, ..
} => {
let dividend_unit = dividend.unit()?;
let divisor_unit = divisor.unit()?;
if !divisor_unit.can_sum_with(dividend_unit) {
return Err(());
}
dividend_unit | divisor_unit
},
CalcNode::Sign(ref child) => {
// sign() always resolves to a number, but we still need to make sure that the
// child units make sense.
let _ = child.unit()?;
CalcUnits::empty()
},
})
}
/// Negate the node inline. If the node is distributive, it is replaced by the result,
/// otherwise the node is wrapped in a [`Negate`] node.
pub fn negate(&mut self) {
/// Node(params) -> Negate(Node(params))
fn wrap_self_in_negate<L: CalcNodeLeaf>(s: &mut CalcNode<L>) {
let result = mem::replace(s, CalcNode::dummy());
*s = CalcNode::Negate(Box::new(result));
}
match *self {
CalcNode::Leaf(ref mut leaf) => {
if leaf.map(std::ops::Neg::neg).is_err() {
wrap_self_in_negate(self)
}
},
CalcNode::Negate(ref mut value) => {
// Don't negate the value here. Replace `self` with it's child.
let result = mem::replace(value.as_mut(), Self::dummy());
*self = result;
},
CalcNode::Invert(_) => {
// -(1 / -10) == -(-0.1) == 0.1
wrap_self_in_negate(self)
},
CalcNode::Sum(ref mut children) => {
for child in children.iter_mut() {
child.negate();
}
},
CalcNode::Product(_) => {
// -(2 * 3 / 4) == -(1.5)
wrap_self_in_negate(self);
},
CalcNode::MinMax(ref mut children, ref mut op) => {
for child in children.iter_mut() {
child.negate();
}
// Negating min-max means the operation is swapped.
*op = match *op {
MinMaxOp::Min => MinMaxOp::Max,
MinMaxOp::Max => MinMaxOp::Min,
};
},
CalcNode::Clamp {
ref mut min,
ref mut center,
ref mut max,
} => {
if min.lte(max, PositivePercentageBasis::Unknown) {
min.negate();
center.negate();
max.negate();
mem::swap(min, max);
} else {
wrap_self_in_negate(self);
}
},
CalcNode::Round {
ref mut strategy,
ref mut value,
ref mut step,
} => {
match *strategy {
RoundingStrategy::Nearest => {
// Nearest is tricky because we'd have to swap the
// behavior at the half-way point from using the upper
// to lower bound.
// Simpler to just wrap self in a negate node.
wrap_self_in_negate(self);
return;
},
RoundingStrategy::Up => *strategy = RoundingStrategy::Down,
RoundingStrategy::Down => *strategy = RoundingStrategy::Up,
RoundingStrategy::ToZero => (),
}
value.negate();
step.negate();
},
CalcNode::ModRem {
ref mut dividend,
ref mut divisor,
..
} => {
dividend.negate();
divisor.negate();
},
CalcNode::Hypot(ref mut children) => {
for child in children.iter_mut() {
child.negate();
}
},
CalcNode::Abs(_) => {
wrap_self_in_negate(self);
},
CalcNode::Sign(ref mut child) => {
child.negate();
},
}
}
fn sort_key(&self) -> SortKey {
match *self {
Self::Leaf(ref l) => l.sort_key(),
_ => SortKey::Other,
}
}
/// Returns the leaf if we can (if simplification has allowed it).
pub fn as_leaf(&self) -> Option<&L> {
match *self {
Self::Leaf(ref l) => Some(l),
_ => None,
}
}
/// Tries to merge one node into another using the sum, that is, perform `x` + `y`.
fn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()> {
match (self, other) {
(&mut CalcNode::Leaf(ref mut one), &CalcNode::Leaf(ref other)) => {
one.try_sum_in_place(other)
},
_ => Err(()),
}
}
/// Tries to merge one node into another using the product, that is, perform `x` * `y`.
pub fn try_product_in_place(&mut self, other: &mut Self) -> bool {
if let Ok(resolved) = other.resolve() {
if let Some(number) = resolved.as_number() {
if number == 1.0 {
return true;
}
if self.is_product_distributive() {
if self.map(|v| v * number).is_err() {
return false;
}
return true;
}
}
}
if let Ok(resolved) = self.resolve() {
if let Some(number) = resolved.as_number() {
if number == 1.0 {
std::mem::swap(self, other);
return true;
}
if other.is_product_distributive() {
if other.map(|v| v * number).is_err() {
return false;
}
std::mem::swap(self, other);
return true;
}
}
}
false
}
/// Tries to apply a generic arithmetic operator
fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>
where
O: Fn(f32, f32) -> f32,
{
match (self, other) {
(&CalcNode::Leaf(ref one), &CalcNode::Leaf(ref other)) => {
Ok(CalcNode::Leaf(one.try_op(other, op)?))
},
_ => Err(()),
}
}
/// Map the value of this node with the given operation.
pub fn map(&mut self, mut op: impl FnMut(f32) -> f32) -> Result<(), ()> {
fn map_internal<L: CalcNodeLeaf>(
node: &mut CalcNode<L>,
op: &mut impl FnMut(f32) -> f32,
) -> Result<(), ()> {
match node {
CalcNode::Leaf(l) => l.map(op),
CalcNode::Negate(v) | CalcNode::Invert(v) => map_internal(v, op),
CalcNode::Sum(children) | CalcNode::Product(children) => {
for node in &mut **children {
map_internal(node, op)?;
}
Ok(())
},
CalcNode::MinMax(children, _) => {
for node in &mut **children {
map_internal(node, op)?;
}
Ok(())
},
CalcNode::Clamp { min, center, max } => {
map_internal(min, op)?;
map_internal(center, op)?;
map_internal(max, op)
},
CalcNode::Round { value, step, .. } => {
map_internal(value, op)?;
map_internal(step, op)
},
CalcNode::ModRem {
dividend, divisor, ..
} => {
map_internal(dividend, op)?;
map_internal(divisor, op)
},
CalcNode::Hypot(children) => {
for node in &mut **children {
map_internal(node, op)?;
}
Ok(())
},
CalcNode::Abs(child) | CalcNode::Sign(child) => map_internal(child, op),
}
}
map_internal(self, &mut op)
}
/// Convert this `CalcNode` into a `CalcNode` with a different leaf kind.
pub fn map_leaves<O, F>(&self, mut map: F) -> CalcNode<O>
where
O: CalcNodeLeaf,
F: FnMut(&L) -> O,
{
self.map_leaves_internal(&mut map)
}
fn map_leaves_internal<O, F>(&self, map: &mut F) -> CalcNode<O>
where
O: CalcNodeLeaf,
F: FnMut(&L) -> O,
{
fn map_children<L, O, F>(
children: &[CalcNode<L>],
map: &mut F,
) -> crate::OwnedSlice<CalcNode<O>>
where
L: CalcNodeLeaf,
O: CalcNodeLeaf,
F: FnMut(&L) -> O,
{
children
.iter()
.map(|c| c.map_leaves_internal(map))
.collect()
}
match *self {
Self::Leaf(ref l) => CalcNode::Leaf(map(l)),
Self::Negate(ref c) => CalcNode::Negate(Box::new(c.map_leaves_internal(map))),
Self::Invert(ref c) => CalcNode::Invert(Box::new(c.map_leaves_internal(map))),
Self::Sum(ref c) => CalcNode::Sum(map_children(c, map)),
Self::Product(ref c) => CalcNode::Product(map_children(c, map)),
Self::MinMax(ref c, op) => CalcNode::MinMax(map_children(c, map), op),
Self::Clamp {
ref min,
ref center,
ref max,
} => {
let min = Box::new(min.map_leaves_internal(map));
let center = Box::new(center.map_leaves_internal(map));
let max = Box::new(max.map_leaves_internal(map));
CalcNode::Clamp { min, center, max }
},
Self::Round {
strategy,
ref value,
ref step,
} => {
let value = Box::new(value.map_leaves_internal(map));
let step = Box::new(step.map_leaves_internal(map));
CalcNode::Round {
strategy,
value,
step,
}
},
Self::ModRem {
ref dividend,
ref divisor,
op,
} => {
let dividend = Box::new(dividend.map_leaves_internal(map));
let divisor = Box::new(divisor.map_leaves_internal(map));
CalcNode::ModRem {
dividend,
divisor,
op,
}
},
Self::Hypot(ref c) => CalcNode::Hypot(map_children(c, map)),
Self::Abs(ref c) => CalcNode::Abs(Box::new(c.map_leaves_internal(map))),
Self::Sign(ref c) => CalcNode::Sign(Box::new(c.map_leaves_internal(map))),
}
}
/// Resolve this node into a value.
pub fn resolve(&self) -> Result<L, ()> {
self.resolve_map(|l| Ok(l.clone()))
}
/// Resolve this node into a value, given a function that maps the leaf values.
pub fn resolve_map<F>(&self, mut leaf_to_output_fn: F) -> Result<L, ()>
where
F: FnMut(&L) -> Result<L, ()>,
{
self.resolve_internal(&mut leaf_to_output_fn)
}
fn resolve_internal<F>(&self, leaf_to_output_fn: &mut F) -> Result<L, ()>
where
F: FnMut(&L) -> Result<L, ()>,
{
match self {
Self::Leaf(l) => leaf_to_output_fn(l),
Self::Negate(child) => {
let mut result = child.resolve_internal(leaf_to_output_fn)?;
result.map(|v| v.neg())?;
Ok(result)
},
Self::Invert(child) => {
let mut result = child.resolve_internal(leaf_to_output_fn)?;
result.map(|v| 1.0 / v)?;
Ok(result)
},
Self::Sum(children) => {
let mut result = children[0].resolve_internal(leaf_to_output_fn)?;
for child in children.iter().skip(1) {
let right = child.resolve_internal(leaf_to_output_fn)?;
// try_op will make sure we only sum leaves with the same type.
result = result.try_op(&right, |left, right| left + right)?;
}
Ok(result)
},
Self::Product(children) => {
let mut result = children[0].resolve_internal(leaf_to_output_fn)?;
for child in children.iter().skip(1) {
let right = child.resolve_internal(leaf_to_output_fn)?;
// Mutliply only allowed when either side is a number.
match result.as_number() {
Some(left) => {
// Left side is a number, so we use the right node as the result.
result = right;
result.map(|v| v * left)?;
},
None => {
// Left side is not a number, so check if the right side is.
match right.as_number() {
Some(right) => {
result.map(|v| v * right)?;
},
None => {
// Multiplying with both sides having units.
return Err(());
},
}
},
}
}
Ok(result)
},
Self::MinMax(children, op) => {
let mut result = children[0].resolve_internal(leaf_to_output_fn)?;
if result.is_nan()? {
return Ok(result);
}
for child in children.iter().skip(1) {
let candidate = child.resolve_internal(leaf_to_output_fn)?;
// Leaf types must match for each child.
if !result.is_same_unit_as(&candidate) {
return Err(());
}
if candidate.is_nan()? {
result = candidate;
break;
}
let candidate_wins = match op {
MinMaxOp::Min => candidate.lt(&result, PositivePercentageBasis::Yes),
MinMaxOp::Max => candidate.gt(&result, PositivePercentageBasis::Yes),
};
if candidate_wins {
result = candidate;
}
}
Ok(result)
},
Self::Clamp { min, center, max } => {
let min = min.resolve_internal(leaf_to_output_fn)?;
let center = center.resolve_internal(leaf_to_output_fn)?;
let max = max.resolve_internal(leaf_to_output_fn)?;
if !min.is_same_unit_as(¢er) || !max.is_same_unit_as(¢er) {
return Err(());
}
if min.is_nan()? {
return Ok(min);
}
if center.is_nan()? {
return Ok(center);
}
if max.is_nan()? {
return Ok(max);
}
let mut result = center;
if result.gt(&max, PositivePercentageBasis::Yes) {
result = max;
}
if result.lt(&min, PositivePercentageBasis::Yes) {
result = min
}
Ok(result)
},
Self::Round {
strategy,
value,
step,
} => {
let mut value = value.resolve_internal(leaf_to_output_fn)?;
let step = step.resolve_internal(leaf_to_output_fn)?;
if !value.is_same_unit_as(&step) {
return Err(());
}
let Some(step) = step.unitless_value() else {
return Err(());
};
let step = step.abs();
value.map(|value| {
// TODO(emilio): Seems like at least a few of these
// special-cases could be removed if we do the math in a
// particular order.
if step.is_zero() {
return f32::NAN;
}
if value.is_infinite() {
if step.is_infinite() {
return f32::NAN;
}
return value;
}
if step.is_infinite() {
match strategy {
RoundingStrategy::Nearest | RoundingStrategy::ToZero => {
return if value.is_sign_negative() { -0.0 } else { 0.0 }
},
RoundingStrategy::Up => {
return if !value.is_sign_negative() && !value.is_zero() {
f32::INFINITY
} else if !value.is_sign_negative() && value.is_zero() {
value
} else {
-0.0
}
},
RoundingStrategy::Down => {
return if value.is_sign_negative() && !value.is_zero() {
-f32::INFINITY
} else if value.is_sign_negative() && value.is_zero() {
value
} else {
0.0
}
},
}
}
let div = value / step;
let lower_bound = div.floor() * step;
let upper_bound = div.ceil() * step;
match strategy {
RoundingStrategy::Nearest => {
// In case of a tie, use the upper bound
if value - lower_bound < upper_bound - value {
lower_bound
} else {
upper_bound
}
},
RoundingStrategy::Up => upper_bound,
RoundingStrategy::Down => lower_bound,
RoundingStrategy::ToZero => {
// In case of a tie, use the upper bound
if lower_bound.abs() < upper_bound.abs() {
lower_bound
} else {
upper_bound
}
},
}
})?;
Ok(value)
},
Self::ModRem {
dividend,
divisor,
op,
} => {
let mut dividend = dividend.resolve_internal(leaf_to_output_fn)?;
let divisor = divisor.resolve_internal(leaf_to_output_fn)?;
if !dividend.is_same_unit_as(&divisor) {
return Err(());
}
let Some(divisor) = divisor.unitless_value() else {
return Err(());
};
dividend.map(|dividend| op.apply(dividend, divisor))?;
Ok(dividend)
},
Self::Hypot(children) => {
let mut result = children[0].resolve_internal(leaf_to_output_fn)?;
result.map(|v| v.powi(2))?;
for child in children.iter().skip(1) {
let child_value = child.resolve_internal(leaf_to_output_fn)?;
if !result.is_same_unit_as(&child_value) {
return Err(());
}
let Some(child_value) = child_value.unitless_value() else {
return Err(());
};
result.map(|v| v + child_value.powi(2))?;
}
result.map(|v| v.sqrt())?;
Ok(result)
},
Self::Abs(ref c) => {
let mut result = c.resolve_internal(leaf_to_output_fn)?;
result.map(|v| v.abs())?;
Ok(result)
},
Self::Sign(ref c) => {
let result = c.resolve_internal(leaf_to_output_fn)?;
Ok(L::sign_from(&result)?)
},
}
}
fn is_negative_leaf(&self) -> Result<bool, ()> {
Ok(match *self {
Self::Leaf(ref l) => l.is_negative()?,
_ => false,
})
}
fn is_zero_leaf(&self) -> Result<bool, ()> {
Ok(match *self {
Self::Leaf(ref l) => l.is_zero()?,
_ => false,
})
}
fn is_infinite_leaf(&self) -> Result<bool, ()> {
Ok(match *self {
Self::Leaf(ref l) => l.is_infinite()?,
_ => false,
})
}
fn is_nan_leaf(&self) -> Result<bool, ()> {
Ok(match *self {
Self::Leaf(ref l) => l.is_nan()?,
_ => false,
})
}
/// Visits all the nodes in this calculation tree recursively, starting by
/// the leaves and bubbling all the way up.
///
/// This is useful for simplification, but can also be used for validation
/// and such.
pub fn visit_depth_first(&mut self, mut f: impl FnMut(&mut Self)) {
self.visit_depth_first_internal(&mut f)
}
fn visit_depth_first_internal(&mut self, f: &mut impl FnMut(&mut Self)) {
match *self {
Self::Clamp {
ref mut min,
ref mut center,
ref mut max,
} => {
min.visit_depth_first_internal(f);
center.visit_depth_first_internal(f);
max.visit_depth_first_internal(f);
},
Self::Round {
ref mut value,
ref mut step,
..
} => {
value.visit_depth_first_internal(f);
step.visit_depth_first_internal(f);
},
Self::ModRem {
ref mut dividend,
ref mut divisor,
..
} => {
dividend.visit_depth_first_internal(f);
divisor.visit_depth_first_internal(f);
},
Self::Sum(ref mut children) |
Self::Product(ref mut children) |
Self::MinMax(ref mut children, _) |
Self::Hypot(ref mut children) => {
for child in &mut **children {
child.visit_depth_first_internal(f);
}
},
Self::Negate(ref mut value) | Self::Invert(ref mut value) => {
value.visit_depth_first_internal(f);
},
Self::Abs(ref mut value) | Self::Sign(ref mut value) => {
value.visit_depth_first_internal(f);
},
Self::Leaf(..) => {},
}
f(self);
}
/// This function simplifies and sorts the calculation of the specified node. It simplifies
/// directly nested nodes while assuming that all nodes below it have already been simplified.
/// It is recommended to use this function in combination with `visit_depth_first()`.
///
/// This function is necessary only if the node needs to be preserved after parsing,
/// specifically for `<length-percentage>` cases where the calculation contains percentages or
/// relative units. Otherwise, the node can be evaluated using `resolve()`, which will
/// automatically provide a simplified value.
///
/// <https://drafts.csswg.org/css-values-4/#calc-simplification>
pub fn simplify_and_sort_direct_children(&mut self) {
macro_rules! replace_self_with {
($slot:expr) => {{
let result = mem::replace($slot, Self::dummy());
*self = result;
}};
}
macro_rules! value_or_stop {
($op:expr) => {{
match $op {
Ok(value) => value,
Err(_) => return,
}
}};
}
match *self {
Self::Clamp {
ref mut min,
ref mut center,
ref mut max,
} => {
// NOTE: clamp() is max(min, min(center, max))
let min_cmp_center = match min.compare(¢er, PositivePercentageBasis::Unknown) {
Some(o) => o,
None => return,
};
// So if we can prove that min is more than center, then we won,
// as that's what we should always return.
if matches!(min_cmp_center, cmp::Ordering::Greater) {
replace_self_with!(&mut **min);
return;
}
// Otherwise try with max.
let max_cmp_center = match max.compare(¢er, PositivePercentageBasis::Unknown) {
Some(o) => o,
None => return,
};
if matches!(max_cmp_center, cmp::Ordering::Less) {
// max is less than center, so we need to return effectively
// `max(min, max)`.
let max_cmp_min = match max.compare(&min, PositivePercentageBasis::Unknown) {
Some(o) => o,
None => {
debug_assert!(
false,
"We compared center with min and max, how are \
min / max not comparable with each other?"
);
return;
},
};
if matches!(max_cmp_min, cmp::Ordering::Less) {
replace_self_with!(&mut **min);
return;
}
replace_self_with!(&mut **max);
return;
}
// Otherwise we're the center node.
replace_self_with!(&mut **center);
},
Self::Round {
strategy,
ref mut value,
ref mut step,
} => {
if value_or_stop!(step.is_zero_leaf()) {
value_or_stop!(value.coerce_to_value(f32::NAN));
replace_self_with!(&mut **value);
return;
}
if value_or_stop!(value.is_infinite_leaf()) &&
value_or_stop!(step.is_infinite_leaf())
{
value_or_stop!(value.coerce_to_value(f32::NAN));
replace_self_with!(&mut **value);
return;
}
if value_or_stop!(value.is_infinite_leaf()) {
replace_self_with!(&mut **value);
return;
}
if value_or_stop!(step.is_infinite_leaf()) {
match strategy {
RoundingStrategy::Nearest | RoundingStrategy::ToZero => {
value_or_stop!(value.coerce_to_value(0.0));
replace_self_with!(&mut **value);
return;
},
RoundingStrategy::Up => {
if !value_or_stop!(value.is_negative_leaf()) &&
!value_or_stop!(value.is_zero_leaf())
{
value_or_stop!(value.coerce_to_value(f32::INFINITY));
replace_self_with!(&mut **value);
return;
} else if !value_or_stop!(value.is_negative_leaf()) &&
value_or_stop!(value.is_zero_leaf())
{
replace_self_with!(&mut **value);
return;
} else {
value_or_stop!(value.coerce_to_value(0.0));
replace_self_with!(&mut **value);
return;
}
},
RoundingStrategy::Down => {
if value_or_stop!(value.is_negative_leaf()) &&
!value_or_stop!(value.is_zero_leaf())
{
value_or_stop!(value.coerce_to_value(f32::INFINITY));
replace_self_with!(&mut **value);
return;
} else if value_or_stop!(value.is_negative_leaf()) &&
value_or_stop!(value.is_zero_leaf())
{
replace_self_with!(&mut **value);
return;
} else {
value_or_stop!(value.coerce_to_value(0.0));
replace_self_with!(&mut **value);
return;
}
},
}
}
if value_or_stop!(step.is_negative_leaf()) {
step.negate();
}
let remainder = value_or_stop!(value.try_op(step, Rem::rem));
if value_or_stop!(remainder.is_zero_leaf()) {
replace_self_with!(&mut **value);
return;
}
let (mut lower_bound, mut upper_bound) = if value_or_stop!(value.is_negative_leaf())
{
let upper_bound = value_or_stop!(value.try_op(&remainder, Sub::sub));
let lower_bound = value_or_stop!(upper_bound.try_op(&step, Sub::sub));
(lower_bound, upper_bound)
} else {
let lower_bound = value_or_stop!(value.try_op(&remainder, Sub::sub));
let upper_bound = value_or_stop!(lower_bound.try_op(&step, Add::add));
(lower_bound, upper_bound)
};
match strategy {
RoundingStrategy::Nearest => {
let lower_diff = value_or_stop!(value.try_op(&lower_bound, Sub::sub));
let upper_diff = value_or_stop!(upper_bound.try_op(value, Sub::sub));
// In case of a tie, use the upper bound
if lower_diff.lt(&upper_diff, PositivePercentageBasis::Unknown) {
replace_self_with!(&mut lower_bound);
} else {
replace_self_with!(&mut upper_bound);
}
},
RoundingStrategy::Up => {
replace_self_with!(&mut upper_bound);
},
RoundingStrategy::Down => {
replace_self_with!(&mut lower_bound);
},
RoundingStrategy::ToZero => {
let mut lower_diff = lower_bound.clone();
let mut upper_diff = upper_bound.clone();
if value_or_stop!(lower_diff.is_negative_leaf()) {
lower_diff.negate();
}
if value_or_stop!(upper_diff.is_negative_leaf()) {
upper_diff.negate();
}
// In case of a tie, use the upper bound
if lower_diff.lt(&upper_diff, PositivePercentageBasis::Unknown) {
replace_self_with!(&mut lower_bound);
} else {
replace_self_with!(&mut upper_bound);
}
},
};
},
Self::ModRem {
ref dividend,
ref divisor,
op,
} => {
let mut result = value_or_stop!(dividend.try_op(divisor, |a, b| op.apply(a, b)));
replace_self_with!(&mut result);
},
Self::MinMax(ref mut children, op) => {
let winning_order = match op {
MinMaxOp::Min => cmp::Ordering::Less,
MinMaxOp::Max => cmp::Ordering::Greater,
};
if value_or_stop!(children[0].is_nan_leaf()) {
replace_self_with!(&mut children[0]);
return
}
let mut result = 0;
for i in 1..children.len() {
if value_or_stop!(children[i].is_nan_leaf()) {
replace_self_with!(&mut children[i]);
return
}
let o = match children[i]
.compare(&children[result], PositivePercentageBasis::Unknown)
{
// We can't compare all the children, so we can't
// know which one will actually win. Bail out and
// keep ourselves as a min / max function.
//
// TODO: Maybe we could simplify compatible children,
// see https://github.com/w3c/csswg-drafts/issues/4756
None => return,
Some(o) => o,
};
if o == winning_order {
result = i;
}
}
replace_self_with!(&mut children[result]);
},
Self::Sum(ref mut children_slot) => {
let mut sums_to_merge = SmallVec::<[_; 3]>::new();
let mut extra_kids = 0;
for (i, child) in children_slot.iter().enumerate() {
if let Self::Sum(ref children) = *child {
extra_kids += children.len();
sums_to_merge.push(i);
}
}
// If we only have one kid, we've already simplified it, and it
// doesn't really matter whether it's a sum already or not, so
// lift it up and continue.
if children_slot.len() == 1 {
replace_self_with!(&mut children_slot[0]);
return;
}
let mut children = mem::take(children_slot).into_vec();
if !sums_to_merge.is_empty() {
children.reserve(extra_kids - sums_to_merge.len());
// Merge all our nested sums, in reverse order so that the
// list indices are not invalidated.
for i in sums_to_merge.drain(..).rev() {
let kid_children = match children.swap_remove(i) {
Self::Sum(c) => c,
_ => unreachable!(),
};
// This would be nicer with
// https://github.com/rust-lang/rust/issues/59878 fixed.
children.extend(kid_children.into_vec());
}
}
debug_assert!(children.len() >= 2, "Should still have multiple kids!");
// Sort by spec order.
children.sort_unstable_by_key(|c| c.sort_key());
// NOTE: if the function returns true, by the docs of dedup_by,
// a is removed.
children.dedup_by(|a, b| b.try_sum_in_place(a).is_ok());
if children.len() == 1 {
// If only one children remains, lift it up, and carry on.
replace_self_with!(&mut children[0]);
} else {
// Else put our simplified children back.
*children_slot = children.into_boxed_slice().into();
}
},
Self::Product(ref mut children_slot) => {
let mut products_to_merge = SmallVec::<[_; 3]>::new();
let mut extra_kids = 0;
for (i, child) in children_slot.iter().enumerate() {
if let Self::Product(ref children) = *child {
extra_kids += children.len();
products_to_merge.push(i);
}
}
// If we only have one kid, we've already simplified it, and it
// doesn't really matter whether it's a product already or not,
// so lift it up and continue.
if children_slot.len() == 1 {
replace_self_with!(&mut children_slot[0]);
return;
}
let mut children = mem::take(children_slot).into_vec();
if !products_to_merge.is_empty() {
children.reserve(extra_kids - products_to_merge.len());
// Merge all our nested sums, in reverse order so that the
// list indices are not invalidated.
for i in products_to_merge.drain(..).rev() {
let kid_children = match children.swap_remove(i) {
Self::Product(c) => c,
_ => unreachable!(),
};
// This would be nicer with
// https://github.com/rust-lang/rust/issues/59878 fixed.
children.extend(kid_children.into_vec());
}
}
debug_assert!(children.len() >= 2, "Should still have multiple kids!");
// NOTE: if the function returns true, by the docs of dedup_by,
// a is removed.
children.dedup_by(|right, left| left.try_product_in_place(right));
if children.len() == 1 {
// If only one children remains, lift it up, and carry on.
replace_self_with!(&mut children[0]);
} else {
// Else put our simplified children back.
*children_slot = children.into_boxed_slice().into();
}
},
Self::Hypot(ref children) => {
let mut result = value_or_stop!(children[0].try_op(&children[0], Mul::mul));
for child in children.iter().skip(1) {
let square = value_or_stop!(child.try_op(&child, Mul::mul));
result = value_or_stop!(result.try_op(&square, Add::add));
}
result = value_or_stop!(result.try_op(&result, |a, _| a.sqrt()));
replace_self_with!(&mut result);
},
Self::Abs(ref mut child) => {
if let CalcNode::Leaf(leaf) = child.as_mut() {
value_or_stop!(leaf.map(|v| v.abs()));
replace_self_with!(&mut **child);
}
},
Self::Sign(ref mut child) => {
if let CalcNode::Leaf(leaf) = child.as_mut() {
let mut result = Self::Leaf(value_or_stop!(L::sign_from(leaf)));
replace_self_with!(&mut result);
}
},
Self::Negate(ref mut child) => {
// Step 6.
match &mut **child {
CalcNode::Leaf(_) => {
// 1. If root’s child is a numeric value, return an equivalent numeric value, but
// with the value negated (0 - value).
child.negate();
replace_self_with!(&mut **child);
},
CalcNode::Negate(value) => {
// 2. If root’s child is a Negate node, return the child’s child.
replace_self_with!(&mut **value);
},
_ => {
// 3. Return root.
},
}
},
Self::Invert(ref mut child) => {
// Step 7.
match &mut **child {
CalcNode::Leaf(leaf) => {
// 1. If root’s child is a number (not a percentage or dimension) return the
// reciprocal of the child’s value.
if leaf.unit().is_empty() {
value_or_stop!(child.map(|v| 1.0 / v));
replace_self_with!(&mut **child);
}
},
CalcNode::Invert(value) => {
// 2. If root’s child is an Invert node, return the child’s child.
replace_self_with!(&mut **value);
},
_ => {
// 3. Return root.
},
}
},
Self::Leaf(ref mut l) => {
l.simplify();
},
}
}
/// Simplifies and sorts the kids in the whole calculation subtree.
pub fn simplify_and_sort(&mut self) {
self.visit_depth_first(|node| node.simplify_and_sort_direct_children())
}
fn to_css_impl<W>(&self, dest: &mut CssWriter<W>, level: ArgumentLevel) -> fmt::Result
where
W: Write,
{
let write_closing_paren = match *self {
Self::MinMax(_, op) => {
dest.write_str(match op {
MinMaxOp::Max => "max(",
MinMaxOp::Min => "min(",
})?;
true
},
Self::Clamp { .. } => {
dest.write_str("clamp(")?;
true
},
Self::Round { strategy, .. } => {
match strategy {
RoundingStrategy::Nearest => dest.write_str("round("),
RoundingStrategy::Up => dest.write_str("round(up, "),
RoundingStrategy::Down => dest.write_str("round(down, "),
RoundingStrategy::ToZero => dest.write_str("round(to-zero, "),
}?;
true
},
Self::ModRem { op, .. } => {
dest.write_str(match op {
ModRemOp::Mod => "mod(",
ModRemOp::Rem => "rem(",
})?;
true
},
Self::Hypot(_) => {
dest.write_str("hypot(")?;
true
},
Self::Abs(_) => {
dest.write_str("abs(")?;
true
},
Self::Sign(_) => {
dest.write_str("sign(")?;
true
},
Self::Negate(_) => {
// We never generate a [`Negate`] node as the root of a calculation, only inside
// [`Sum`] nodes as a child. Because negate nodes are handled by the [`Sum`] node
// directly (see below), this node will never be serialized.
debug_assert!(
false,
"We never serialize Negate nodes as they are handled inside Sum nodes."
);
dest.write_str("(-1 * ")?;
true
},
Self::Invert(_) => {
dest.write_str("(1 / ")?;
true
},
Self::Sum(_) | Self::Product(_) => match level {
ArgumentLevel::CalculationRoot => {
dest.write_str("calc(")?;
true
},
ArgumentLevel::ArgumentRoot => false,
ArgumentLevel::Nested => {
dest.write_str("(")?;
true
},
},
Self::Leaf(_) => match level {
ArgumentLevel::CalculationRoot => {
dest.write_str("calc(")?;
true
},
ArgumentLevel::ArgumentRoot | ArgumentLevel::Nested => false,
},
};
match *self {
Self::MinMax(ref children, _) | Self::Hypot(ref children) => {
let mut first = true;
for child in &**children {
if !first {
dest.write_str(", ")?;
}
first = false;
child.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
}
},
Self::Negate(ref value) | Self::Invert(ref value) => {
value.to_css_impl(dest, ArgumentLevel::Nested)?
},
Self::Sum(ref children) => {
let mut first = true;
for child in &**children {
if !first {
match child {
Self::Leaf(l) => {
if let Ok(true) = l.is_negative() {
dest.write_str(" - ")?;
let mut negated = l.clone();
// We can unwrap here, because we already
// checked if the value inside is negative.
negated.map(std::ops::Neg::neg).unwrap();
negated.to_css(dest)?;
} else {
dest.write_str(" + ")?;
l.to_css(dest)?;
}
},
Self::Negate(n) => {
dest.write_str(" - ")?;
n.to_css_impl(dest, ArgumentLevel::Nested)?;
},
_ => {
dest.write_str(" + ")?;
child.to_css_impl(dest, ArgumentLevel::Nested)?;
},
}
} else {
first = false;
child.to_css_impl(dest, ArgumentLevel::Nested)?;
}
}
},
Self::Product(ref children) => {
let mut first = true;
for child in &**children {
if !first {
match child {
Self::Invert(n) => {
dest.write_str(" / ")?;
n.to_css_impl(dest, ArgumentLevel::Nested)?;
},
_ => {
dest.write_str(" * ")?;
child.to_css_impl(dest, ArgumentLevel::Nested)?;
},
}
} else {
first = false;
child.to_css_impl(dest, ArgumentLevel::Nested)?;
}
}
},
Self::Clamp {
ref min,
ref center,
ref max,
} => {
min.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
dest.write_str(", ")?;
center.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
dest.write_str(", ")?;
max.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
},
Self::Round {
ref value,
ref step,
..
} => {
value.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
dest.write_str(", ")?;
step.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
},
Self::ModRem {
ref dividend,
ref divisor,
..
} => {
dividend.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
dest.write_str(", ")?;
divisor.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?;
},
Self::Abs(ref v) | Self::Sign(ref v) => {
v.to_css_impl(dest, ArgumentLevel::ArgumentRoot)?
},
Self::Leaf(ref l) => l.to_css(dest)?,
}
if write_closing_paren {
dest.write_char(')')?;
}
Ok(())
}
fn compare(
&self,
other: &Self,
basis_positive: PositivePercentageBasis,
) -> Option<cmp::Ordering> {
match (self, other) {
(&CalcNode::Leaf(ref one), &CalcNode::Leaf(ref other)) => {
one.compare(other, basis_positive)
},
_ => None,
}
}
compare_helpers!();
}
impl<L: CalcNodeLeaf> ToCss for CalcNode<L> {
/// <https://drafts.csswg.org/css-values/#calc-serialize>
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
self.to_css_impl(dest, ArgumentLevel::CalculationRoot)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_sum_with_checks() {
assert!(CalcUnits::LENGTH.can_sum_with(CalcUnits::LENGTH));
assert!(CalcUnits::LENGTH.can_sum_with(CalcUnits::PERCENTAGE));
assert!(CalcUnits::LENGTH.can_sum_with(CalcUnits::LENGTH_PERCENTAGE));
assert!(CalcUnits::PERCENTAGE.can_sum_with(CalcUnits::LENGTH));
assert!(CalcUnits::PERCENTAGE.can_sum_with(CalcUnits::PERCENTAGE));
assert!(CalcUnits::PERCENTAGE.can_sum_with(CalcUnits::LENGTH_PERCENTAGE));
assert!(CalcUnits::LENGTH_PERCENTAGE.can_sum_with(CalcUnits::LENGTH));
assert!(CalcUnits::LENGTH_PERCENTAGE.can_sum_with(CalcUnits::PERCENTAGE));
assert!(CalcUnits::LENGTH_PERCENTAGE.can_sum_with(CalcUnits::LENGTH_PERCENTAGE));
assert!(!CalcUnits::ANGLE.can_sum_with(CalcUnits::TIME));
assert!(CalcUnits::ANGLE.can_sum_with(CalcUnits::ANGLE));
assert!(!(CalcUnits::ANGLE | CalcUnits::TIME).can_sum_with(CalcUnits::ANGLE));
assert!(!CalcUnits::ANGLE.can_sum_with(CalcUnits::ANGLE | CalcUnits::TIME));
assert!(
!(CalcUnits::ANGLE | CalcUnits::TIME).can_sum_with(CalcUnits::ANGLE | CalcUnits::TIME)
);
}
}