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 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/layout/length_utils.h"
#include <algorithm>
#include <optional>
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/layout/block_node.h"
#include "third_party/blink/renderer/core/layout/constraint_space.h"
#include "third_party/blink/renderer/core/layout/constraint_space_builder.h"
#include "third_party/blink/renderer/core/layout/fragmentation_utils.h"
#include "third_party/blink/renderer/core/layout/geometry/box_strut.h"
#include "third_party/blink/renderer/core/layout/geometry/logical_size.h"
#include "third_party/blink/renderer/core/layout/layout_box.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/layout/space_utils.h"
#include "third_party/blink/renderer/core/layout/svg/layout_svg_root.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/geometry/length.h"
#include "third_party/blink/renderer/platform/geometry/length_functions.h"
namespace blink {
LayoutUnit ResolveInlineLengthInternal(
const ConstraintSpace& constraint_space,
const ComputedStyle& style,
const BoxStrut& border_padding,
MinMaxSizesFunctionRef min_max_sizes_func,
const Length& original_length,
const Length* auto_length,
LengthTypeInternal length_type,
FitContentMode fit_content_mode,
LayoutUnit override_available_size,
CalcSizeKeywordBehavior calc_size_keyword_behavior) {
DCHECK_EQ(constraint_space.GetWritingMode(), style.GetWritingMode());
// For min-inline-size, this might still be 'auto'.
const Length& length =
original_length.IsAuto() && auto_length ? *auto_length : original_length;
switch (length.GetType()) {
case Length::kFillAvailable:
case Length::kStretch: {
const LayoutUnit available_size =
override_available_size == kIndefiniteSize
? constraint_space.AvailableSize().inline_size
: override_available_size;
if (available_size == kIndefiniteSize) {
return kIndefiniteSize;
}
DCHECK_GE(available_size, LayoutUnit());
const BoxStrut margins = ComputeMarginsForSelf(constraint_space, style);
LayoutUnit margins_to_subtract = margins.InlineSum();
if (length.GetType() == Length::kStretch) [[unlikely]] {
const LogicalBoxSides& ignore_margin_sides =
constraint_space.IgnoreMarginsForStretch();
margins_to_subtract = ignore_margin_sides.inline_start
? LayoutUnit()
: margins.inline_start;
margins_to_subtract +=
ignore_margin_sides.inline_end ? LayoutUnit() : margins.inline_end;
}
return std::max(border_padding.InlineSum(),
available_size - margins_to_subtract);
}
case Length::kPercent:
case Length::kFixed:
case Length::kCalculated: {
LayoutUnit percentage_resolution_size =
constraint_space.PercentageResolutionInlineSize();
if (length.HasPercent() &&
percentage_resolution_size == kIndefiniteSize) {
if (RuntimeEnabledFeatures::LayoutMinSizeIndefiniteEnabled()) {
if (length_type != LengthTypeInternal::kMin) {
return kIndefiniteSize;
}
percentage_resolution_size = LayoutUnit();
} else {
return kIndefiniteSize;
}
}
bool evaluated_indefinite = false;
LayoutUnit value = MinimumValueForLength(
length, percentage_resolution_size,
{.intrinsic_evaluator =
[&](const Length& length_to_evaluate) {
LayoutUnit result = ResolveInlineLengthInternal(
constraint_space, style, border_padding,
min_max_sizes_func, length_to_evaluate, auto_length,
length_type, fit_content_mode, override_available_size,
calc_size_keyword_behavior);
if (result == kIndefiniteSize) {
evaluated_indefinite = true;
return kIndefiniteSize;
}
if (style.BoxSizing() == EBoxSizing::kContentBox) {
result -= border_padding.InlineSum();
}
DCHECK_GE(result, LayoutUnit());
return result;
},
.calc_size_keyword_behavior = calc_size_keyword_behavior});
if (evaluated_indefinite) {
return kIndefiniteSize;
}
if (style.BoxSizing() == EBoxSizing::kBorderBox)
value = std::max(border_padding.InlineSum(), value);
else
value += border_padding.InlineSum();
return value;
}
case Length::kContent:
case Length::kMaxContent:
return min_max_sizes_func(SizeType::kContent).sizes.max_size;
case Length::kMinContent:
return min_max_sizes_func(SizeType::kContent).sizes.min_size;
case Length::kMinIntrinsic:
return min_max_sizes_func(SizeType::kIntrinsic).sizes.min_size;
case Length::kFitContent: {
const LayoutUnit available_size =
override_available_size == kIndefiniteSize
? constraint_space.AvailableSize().inline_size
: override_available_size;
// fit-content resolves differently depending on the type of length.
if (available_size == kIndefiniteSize) {
switch (fit_content_mode) {
case FitContentMode::kNormal:
switch (length_type) {
case LengthTypeInternal::kMin:
return min_max_sizes_func(SizeType::kContent).sizes.min_size;
case LengthTypeInternal::kMain:
return kIndefiniteSize;
case LengthTypeInternal::kMax:
return min_max_sizes_func(SizeType::kContent).sizes.max_size;
}
case FitContentMode::kMinContribution:
return min_max_sizes_func(SizeType::kContent).sizes.min_size;
case FitContentMode::kMaxContribution:
return min_max_sizes_func(SizeType::kContent).sizes.max_size;
}
}
DCHECK_GE(available_size, LayoutUnit());
const BoxStrut margins = ComputeMarginsForSelf(constraint_space, style);
return min_max_sizes_func(SizeType::kContent)
.sizes.ShrinkToFit(
(available_size - margins.InlineSum()).ClampNegativeToZero());
}
case Length::kAuto:
case Length::kNone:
return kIndefiniteSize;
case Length::kFlex:
NOTREACHED() << "Should only be used for grid.";
case Length::kDeviceWidth:
case Length::kDeviceHeight:
case Length::kExtendToZoom:
NOTREACHED() << "Should only be used for viewport definitions.";
}
}
LayoutUnit ResolveBlockLengthInternal(
const ConstraintSpace& constraint_space,
const ComputedStyle& style,
const BoxStrut& border_padding,
const Length& original_length,
const Length* auto_length,
LengthTypeInternal length_type,
LayoutUnit override_available_size,
const LayoutUnit* override_percentage_resolution_size,
BlockSizeFunctionRef block_size_func) {
DCHECK_EQ(constraint_space.GetWritingMode(), style.GetWritingMode());
// For min-block-size, this might still be 'auto'.
const Length& length =
original_length.IsAuto() && auto_length ? *auto_length : original_length;
switch (length.GetType()) {
case Length::kFillAvailable:
case Length::kStretch: {
const LayoutUnit available_size =
override_available_size == kIndefiniteSize
? constraint_space.AvailableSize().block_size
: override_available_size;
if (available_size == kIndefiniteSize) {
return length_type == LengthTypeInternal::kMain
? block_size_func(SizeType::kContent)
: kIndefiniteSize;
}
DCHECK_GE(available_size, LayoutUnit());
const BoxStrut margins = ComputeMarginsForSelf(constraint_space, style);
LayoutUnit margins_to_subtract = margins.BlockSum();
if (length.GetType() == Length::kStretch) [[unlikely]] {
const LogicalBoxSides& ignore_margin_sides =
constraint_space.IgnoreMarginsForStretch();
margins_to_subtract = ignore_margin_sides.block_start
? LayoutUnit()
: margins.block_start;
margins_to_subtract +=
ignore_margin_sides.block_end ? LayoutUnit() : margins.block_end;
}
return std::max(border_padding.BlockSum(),
available_size - margins_to_subtract);
}
case Length::kPercent:
case Length::kFixed:
case Length::kCalculated: {
LayoutUnit percentage_resolution_size =
override_percentage_resolution_size
? *override_percentage_resolution_size
: constraint_space.PercentageResolutionBlockSize();
if (length.HasPercent() &&
percentage_resolution_size == kIndefiniteSize) {
switch (length_type) {
case LengthTypeInternal::kMin: {
if (RuntimeEnabledFeatures::LayoutMinSizeIndefiniteEnabled()) {
percentage_resolution_size = LayoutUnit();
} else {
return kIndefiniteSize;
}
break;
}
case LengthTypeInternal::kMain:
return block_size_func(SizeType::kContent);
case LengthTypeInternal::kMax:
return kIndefiniteSize;
}
}
bool evaluated_indefinite = false;
LayoutUnit value = MinimumValueForLength(
length, percentage_resolution_size,
{.intrinsic_evaluator = [&](const Length& length_to_evaluate) {
LayoutUnit result = ResolveBlockLengthInternal(
constraint_space, style, border_padding, length_to_evaluate,
auto_length, length_type, override_available_size,
override_percentage_resolution_size, block_size_func);
if (result == kIndefiniteSize) {
evaluated_indefinite = true;
return kIndefiniteSize;
}
if (style.BoxSizing() == EBoxSizing::kContentBox) {
result -= border_padding.BlockSum();
}
DCHECK_GE(result, LayoutUnit());
return result;
}});
if (evaluated_indefinite) {
return kIndefiniteSize;
}
if (style.BoxSizing() == EBoxSizing::kBorderBox)
value = std::max(border_padding.BlockSum(), value);
else
value += border_padding.BlockSum();
return value;
}
case Length::kContent:
case Length::kMinContent:
case Length::kMaxContent:
case Length::kMinIntrinsic:
case Length::kFitContent: {
const LayoutUnit intrinsic_size = block_size_func(
length.IsMinIntrinsic() ? SizeType::kIntrinsic : SizeType::kContent);
#if DCHECK_IS_ON()
// Due to how intrinsic_size is calculated, it should always include
// border and padding. We cannot check for this if we are
// block-fragmented, though, because then the block-start border/padding
// may be in a different fragmentainer than the block-end border/padding.
if (intrinsic_size != kIndefiniteSize &&
!constraint_space.HasBlockFragmentation())
DCHECK_GE(intrinsic_size, border_padding.BlockSum());
#endif // DCHECK_IS_ON()
return intrinsic_size;
}
case Length::kAuto:
case Length::kNone:
return kIndefiniteSize;
case Length::kFlex:
NOTREACHED() << "Should only be used for grid.";
case Length::kDeviceWidth:
case Length::kDeviceHeight:
case Length::kExtendToZoom:
NOTREACHED() << "Should only be used for viewport definitions.";
}
}
LayoutUnit InlineSizeFromAspectRatio(const BoxStrut& border_padding,
const LogicalSize& aspect_ratio,
EBoxSizing box_sizing,
LayoutUnit block_size) {
if (box_sizing == EBoxSizing::kBorderBox) {
return std::max(
border_padding.InlineSum(),
block_size.MulDiv(aspect_ratio.inline_size, aspect_ratio.block_size));
}
block_size -= border_padding.BlockSum();
return block_size.MulDiv(aspect_ratio.inline_size, aspect_ratio.block_size) +
border_padding.InlineSum();
}
LayoutUnit BlockSizeFromAspectRatio(const BoxStrut& border_padding,
const LogicalSize& aspect_ratio,
EBoxSizing box_sizing,
LayoutUnit inline_size) {
DCHECK_GE(inline_size, border_padding.InlineSum());
if (box_sizing == EBoxSizing::kBorderBox) {
return std::max(
border_padding.BlockSum(),
inline_size.MulDiv(aspect_ratio.block_size, aspect_ratio.inline_size));
}
inline_size -= border_padding.InlineSum();
return inline_size.MulDiv(aspect_ratio.block_size, aspect_ratio.inline_size) +
border_padding.BlockSum();
}
namespace {
// Currently this simply sets the correct override sizes for the replaced
// element, and lets legacy layout do the result.
MinMaxSizesResult ComputeMinAndMaxContentContributionForReplaced(
const BlockNode& child,
const ConstraintSpace& space) {
const auto& child_style = child.Style();
const BoxStrut border_padding =
ComputeBorders(space, child) + ComputePadding(space, child_style);
MinMaxSizes result;
result = ComputeReplacedSize(child, space, border_padding).inline_size;
if (child_style.LogicalWidth().HasPercent() ||
child_style.LogicalMaxWidth().HasPercent()) {
// TODO(ikilpatrick): No browser does this today, but we'd get slightly
// better results here if we also considered the min-block size, and
// transferred through the aspect-ratio (if available).
result.min_size = ResolveMinInlineLength(
space, child_style, border_padding,
[&](SizeType) -> MinMaxSizesResult {
// Behave the same as if we couldn't resolve the min-inline size.
MinMaxSizes sizes;
sizes = border_padding.InlineSum();
return {sizes, /* depends_on_block_constraints */ false};
},
child_style.LogicalMinWidth());
}
// Replaced elements which have a percentage block-size always depend on
// their block constraints (as they have an aspect-ratio which changes their
// min/max content size).
// TODO(https://crbug.com/40339056): These should also check for 'stretch'
// values. (We could add Length::MayHaveStretchOrPercentDependence or
// similar.)
const bool depends_on_block_constraints =
child_style.LogicalHeight().MayHavePercentDependence() ||
child_style.LogicalMinHeight().MayHavePercentDependence() ||
child_style.LogicalMaxHeight().MayHavePercentDependence() ||
(child_style.LogicalHeight().HasAuto() &&
space.IsBlockAutoBehaviorStretch());
return MinMaxSizesResult(result, depends_on_block_constraints);
}
} // namespace
MinMaxSizesResult ComputeMinAndMaxContentContributionInternal(
WritingMode parent_writing_mode,
const BlockNode& child,
const ConstraintSpace& space,
MinMaxSizesFunctionRef original_min_max_sizes_func) {
const auto& style = child.Style();
const auto border_padding =
ComputeBorders(space, child) + ComputePadding(space, style);
// First check if we are an orthogonal writing-mode root, then attempt to
// resolve the block-size.
if (!IsParallelWritingMode(parent_writing_mode, style.GetWritingMode())) {
const LayoutUnit block_size = ComputeBlockSizeForFragment(
space, child, border_padding, /* intrinsic_size */ kIndefiniteSize,
/* inline_size */ kIndefiniteSize);
// If we weren't able to resolve the block-size, or we might have intrinsic
// constraints, just perform a full layout via the callback.
if (block_size == kIndefiniteSize ||
style.LogicalMinHeight().HasContentOrIntrinsic() ||
style.LogicalMaxHeight().HasContentOrIntrinsic() || child.IsTable()) {
return original_min_max_sizes_func(SizeType::kContent);
}
return {{block_size, block_size}, /* depends_on_block_constraints */ false};
}
// Intercept the min/max sizes function so we can access both the
// `depends_on_block_constraints` and `applied_aspect_ratio` variables.
bool depends_on_block_constraints = false;
bool applied_aspect_ratio = false;
auto min_max_sizes_func = [&](SizeType type) {
const MinMaxSizesResult result = original_min_max_sizes_func(type);
depends_on_block_constraints |= result.depends_on_block_constraints;
applied_aspect_ratio |= result.applied_aspect_ratio;
return result;
};
DCHECK_EQ(space.AvailableSize().inline_size, kIndefiniteSize);
// First attempt to resolve the main-length, if we can't resolve (e.g. a
// percentage, or similar) it'll return a kIndefiniteSize.
const Length& main_length = style.LogicalWidth();
const LayoutUnit extent =
ResolveMainInlineLength(space, style, border_padding, min_max_sizes_func,
main_length, &Length::FitContent());
// If we successfully resolved our main size, just use that as the
// contribution, otherwise invoke the callback.
MinMaxSizes sizes = (extent == kIndefiniteSize)
? min_max_sizes_func(SizeType::kContent).sizes
: MinMaxSizes{extent, extent};
// If we have calc-size() with a sizing-keyword of auto/fit-content/stretch
// we need to perform an additional step. Treat the sizing-keyword as auto,
// then resolve auto as both min-content, and max-content.
if (main_length.IsCalculated() &&
(main_length.HasAuto() || main_length.HasFitContent() ||
main_length.HasStretch())) {
sizes.min_size = ResolveMainInlineLength(
space, style, border_padding, min_max_sizes_func, main_length,
/* auto_length */ &Length::MinContent(),
/* override_available_size */ kIndefiniteSize,
CalcSizeKeywordBehavior::kAsAuto);
sizes.max_size = ResolveMainInlineLength(
space, style, border_padding, min_max_sizes_func, main_length,
/* auto_length */ &Length::MaxContent(),
/* override_available_size */ kIndefiniteSize,
CalcSizeKeywordBehavior::kAsAuto);
}
// Check if we should apply the automatic minimum size.
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
const Length* auto_min_length =
(!style.IsScrollContainer() && applied_aspect_ratio)
? &Length::MinIntrinsic()
: nullptr;
// If fit-content is present we need to resolve the min/max sizes twice, once
// assuming its min-content, and max-content. See:
// https://github.com/w3c/csswg-drafts/issues/10721
if (style.LogicalMinWidth().HasFitContent() ||
style.LogicalMaxWidth().HasFitContent()) {
const MinMaxSizes min_sizes = ComputeMinMaxInlineSizes(
space, child, border_padding, auto_min_length, min_max_sizes_func,
TransferredSizesMode::kNormal, FitContentMode::kMinContribution);
const MinMaxSizes max_sizes = ComputeMinMaxInlineSizes(
space, child, border_padding, auto_min_length, min_max_sizes_func,
TransferredSizesMode::kNormal, FitContentMode::kMaxContribution);
sizes.min_size = min_sizes.ClampSizeToMinAndMax(sizes.min_size);
sizes.max_size = max_sizes.ClampSizeToMinAndMax(sizes.max_size);
} else {
const MinMaxSizes min_max_sizes = ComputeMinMaxInlineSizes(
space, child, border_padding, auto_min_length, min_max_sizes_func);
sizes.Constrain(min_max_sizes.max_size);
sizes.Encompass(min_max_sizes.min_size);
}
return {sizes, depends_on_block_constraints};
}
MinMaxSizesResult ComputeMinAndMaxContentContribution(
const ComputedStyle& parent_style,
const BlockNode& child,
const ConstraintSpace& space,
const MinMaxSizesFloatInput float_input) {
const auto& child_style = child.Style();
const auto parent_writing_mode = parent_style.GetWritingMode();
const auto child_writing_mode = child_style.GetWritingMode();
if (IsParallelWritingMode(parent_writing_mode, child_writing_mode)) {
if (child.IsReplaced())
return ComputeMinAndMaxContentContributionForReplaced(child, space);
}
auto MinMaxSizesFunc = [&](SizeType type) -> MinMaxSizesResult {
return child.ComputeMinMaxSizes(parent_writing_mode, type, space,
float_input);
};
return ComputeMinAndMaxContentContributionInternal(parent_writing_mode, child,
space, MinMaxSizesFunc);
}
MinMaxSizesResult ComputeMinAndMaxContentContributionForSelf(
const BlockNode& child,
const ConstraintSpace& space) {
DCHECK(child.CreatesNewFormattingContext());
const ComputedStyle& child_style = child.Style();
WritingMode writing_mode = child_style.GetWritingMode();
if (child.IsReplaced())
return ComputeMinAndMaxContentContributionForReplaced(child, space);
auto MinMaxSizesFunc = [&](SizeType type) -> MinMaxSizesResult {
return child.ComputeMinMaxSizes(writing_mode, type, space);
};
return ComputeMinAndMaxContentContributionInternal(writing_mode, child, space,
MinMaxSizesFunc);
}
MinMaxSizesResult ComputeMinAndMaxContentContributionForSelf(
const BlockNode& child,
const ConstraintSpace& space,
MinMaxSizesFunctionRef min_max_sizes_func) {
DCHECK(child.CreatesNewFormattingContext());
return child.IsReplaced()
? ComputeMinAndMaxContentContributionForReplaced(child, space)
: ComputeMinAndMaxContentContributionInternal(
child.Style().GetWritingMode(), child, space,
min_max_sizes_func);
}
MinMaxSizes ComputeMinAndMaxContentContributionForTest(
WritingMode parent_writing_mode,
const BlockNode& child,
const ConstraintSpace& space,
const MinMaxSizes& min_max_sizes) {
auto MinMaxSizesFunc = [&](SizeType) -> MinMaxSizesResult {
return MinMaxSizesResult(min_max_sizes,
/* depends_on_block_constraints */ false);
};
return ComputeMinAndMaxContentContributionInternal(parent_writing_mode, child,
space, MinMaxSizesFunc)
.sizes;
}
LayoutUnit ComputeInlineSizeForFragmentInternal(
const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
MinMaxSizesFunctionRef min_max_sizes_func) {
const auto& style = node.Style();
const Length& logical_width = style.LogicalWidth();
const bool may_apply_aspect_ratio = ([&]() {
if (style.AspectRatio().IsAuto()) {
return false;
}
// Even though an implicit stretch will resolve - we prefer the inline-axis
// size for this case.
if (style.LogicalHeight().HasAuto() &&
space.BlockAutoBehavior() != AutoSizeBehavior::kStretchExplicit) {
return false;
}
// If we can resolve our block-size with no intrinsic-size we can use our
// aspect-ratio.
return ComputeBlockSizeForFragment(space, node, border_padding,
/* intrinsic_size */ kIndefiniteSize,
/* inline_size */ kIndefiniteSize) !=
kIndefiniteSize;
})();
const Length& auto_length = ([&]() {
if (space.AvailableSize().inline_size == kIndefiniteSize) {
return Length::MinContent();
}
if (space.InlineAutoBehavior() == AutoSizeBehavior::kStretchExplicit) {
return Length::FillAvailable();
}
if (may_apply_aspect_ratio) {
return Length::FitContent();
}
if (space.InlineAutoBehavior() == AutoSizeBehavior::kStretchImplicit) {
return Length::FillAvailable();
}
DCHECK_EQ(space.InlineAutoBehavior(), AutoSizeBehavior::kFitContent);
return Length::FitContent();
})();
// Check if we should apply the automatic minimum size.
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
bool apply_automatic_min_size = ([&]() {
if (style.IsScrollContainer()) {
return false;
}
if (!may_apply_aspect_ratio) {
return false;
}
if (logical_width.HasContentOrIntrinsic()) {
return true;
}
if (logical_width.HasAuto() && auto_length.HasContentOrIntrinsic()) {
return true;
}
return false;
})();
const LayoutUnit extent =
ResolveMainInlineLength(space, style, border_padding, min_max_sizes_func,
logical_width, &auto_length);
return ComputeMinMaxInlineSizes(
space, node, border_padding,
apply_automatic_min_size ? &Length::MinIntrinsic() : nullptr,
min_max_sizes_func)
.ClampSizeToMinAndMax(extent);
}
LayoutUnit ComputeInlineSizeForFragment(
const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
MinMaxSizesFunctionRef min_max_sizes_func) {
if (space.IsFixedInlineSize() || space.IsAnonymous()) {
return space.AvailableSize().inline_size;
}
if (node.IsTable()) {
return To<TableNode>(node).ComputeTableInlineSize(space, border_padding);
}
return ComputeInlineSizeForFragmentInternal(space, node, border_padding,
min_max_sizes_func);
}
LayoutUnit ComputeUsedInlineSizeForTableFragment(
const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
const MinMaxSizes& table_grid_min_max_sizes) {
DCHECK(!space.IsFixedInlineSize());
auto MinMaxSizesFunc = [&](SizeType type) -> MinMaxSizesResult {
const auto& style = node.Style();
const bool has_aspect_ratio = !style.AspectRatio().IsAuto();
// Check if we have an aspect-ratio.
if (has_aspect_ratio && type == SizeType::kContent) {
const LayoutUnit block_size =
ComputeBlockSizeForFragment(space, node, border_padding,
/* intrinsic_size */ kIndefiniteSize,
/* inline_size */ kIndefiniteSize);
if (block_size != kIndefiniteSize) {
const LayoutUnit inline_size = InlineSizeFromAspectRatio(
border_padding, style.LogicalAspectRatio(),
style.BoxSizingForAspectRatio(), block_size);
return MinMaxSizesResult({inline_size, inline_size},
/* depends_on_block_constraints */ false);
}
}
return MinMaxSizesResult(table_grid_min_max_sizes,
/* depends_on_block_constraints */ false);
};
return ComputeInlineSizeForFragmentInternal(space, node, border_padding,
MinMaxSizesFunc);
}
MinMaxSizes ComputeInitialMinMaxBlockSizes(const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
LayoutUnit override_available_size) {
const ComputedStyle& style = node.Style();
MinMaxSizes sizes = {ResolveInitialMinBlockLength(
space, style, border_padding,
style.LogicalMinHeight(), override_available_size),
ResolveInitialMaxBlockLength(
space, style, border_padding,
style.LogicalMaxHeight(), override_available_size)};
sizes.max_size = std::max(sizes.max_size, sizes.min_size);
return sizes;
}
MinMaxSizes ComputeMinMaxBlockSizes(const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
const Length* auto_min_length,
BlockSizeFunctionRef block_size_func,
LayoutUnit override_available_size) {
const ComputedStyle& style = node.Style();
MinMaxSizes sizes = {
ResolveMinBlockLength(space, style, border_padding, block_size_func,
style.LogicalMinHeight(), auto_min_length,
override_available_size),
ResolveMaxBlockLength(space, style, border_padding,
style.LogicalMaxHeight(), block_size_func,
override_available_size)};
// Clamp the auto min-size by the max-size.
if (auto_min_length && style.LogicalMinHeight().HasAuto()) {
sizes.min_size = std::min(sizes.min_size, sizes.max_size);
}
// Tables can't shrink below their min-intrinsic size.
if (node.IsTable()) {
sizes.Encompass(block_size_func(SizeType::kIntrinsic));
}
sizes.max_size = std::max(sizes.max_size, sizes.min_size);
return sizes;
}
MinMaxSizes ComputeTransferredMinMaxInlineSizes(
const LogicalSize& ratio,
const MinMaxSizes& block_min_max,
const BoxStrut& border_padding,
const EBoxSizing sizing) {
MinMaxSizes transferred_min_max = {LayoutUnit(), LayoutUnit::Max()};
if (block_min_max.min_size > LayoutUnit()) {
transferred_min_max.min_size = InlineSizeFromAspectRatio(
border_padding, ratio, sizing, block_min_max.min_size);
}
if (block_min_max.max_size != LayoutUnit::Max()) {
transferred_min_max.max_size = InlineSizeFromAspectRatio(
border_padding, ratio, sizing, block_min_max.max_size);
}
// Minimum size wins over maximum size.
transferred_min_max.max_size =
std::max(transferred_min_max.max_size, transferred_min_max.min_size);
return transferred_min_max;
}
MinMaxSizes ComputeTransferredMinMaxBlockSizes(
const LogicalSize& ratio,
const MinMaxSizes& inline_min_max,
const BoxStrut& border_padding,
const EBoxSizing sizing) {
MinMaxSizes transferred_min_max = {LayoutUnit(), LayoutUnit::Max()};
if (inline_min_max.min_size > LayoutUnit()) {
transferred_min_max.min_size = BlockSizeFromAspectRatio(
border_padding, ratio, sizing, inline_min_max.min_size);
}
if (inline_min_max.max_size != LayoutUnit::Max()) {
transferred_min_max.max_size = BlockSizeFromAspectRatio(
border_padding, ratio, sizing, inline_min_max.max_size);
}
// Minimum size wins over maximum size.
transferred_min_max.max_size =
std::max(transferred_min_max.max_size, transferred_min_max.min_size);
return transferred_min_max;
}
MinMaxSizes ComputeMinMaxInlineSizesFromAspectRatio(
const ConstraintSpace& constraint_space,
const BlockNode& node,
const BoxStrut& border_padding) {
// The spec requires us to clamp these by the specified size (it calls it the
// preferred size). However, we actually don't need to worry about that,
// because we only use this if the width is indefinite.
// We do not need to compute the min/max inline sizes; as long as we always
// apply the transferred min/max size before the explicit min/max size, the
// result will be identical.
const ComputedStyle& style = node.Style();
DCHECK(!style.AspectRatio().IsAuto());
const MinMaxSizes block_min_max =
ComputeInitialMinMaxBlockSizes(constraint_space, node, border_padding);
return ComputeTransferredMinMaxInlineSizes(style.LogicalAspectRatio(),
block_min_max, border_padding,
style.BoxSizingForAspectRatio());
}
MinMaxSizes ComputeMinMaxInlineSizes(
const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
const Length* auto_min_length,
MinMaxSizesFunctionRef min_max_sizes_func,
TransferredSizesMode transferred_sizes_mode,
FitContentMode fit_content_mode,
LayoutUnit override_available_size) {
const ComputedStyle& style = node.Style();
MinMaxSizes sizes = {
ResolveMinInlineLength(space, style, border_padding, min_max_sizes_func,
style.LogicalMinWidth(), auto_min_length,
override_available_size, fit_content_mode),
ResolveMaxInlineLength(space, style, border_padding, min_max_sizes_func,
style.LogicalMaxWidth(), override_available_size,
fit_content_mode)};
// Clamp the auto min-size by the max-size.
if (auto_min_length && style.LogicalMinWidth().HasAuto()) {
sizes.min_size = std::min(sizes.min_size, sizes.max_size);
}
// This implements the transferred min/max sizes per:
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-size-transfers
if (transferred_sizes_mode == TransferredSizesMode::kNormal &&
!style.AspectRatio().IsAuto() && style.LogicalWidth().HasAuto() &&
space.InlineAutoBehavior() != AutoSizeBehavior::kStretchExplicit) {
MinMaxSizes transferred_sizes =
ComputeMinMaxInlineSizesFromAspectRatio(space, node, border_padding);
sizes.min_size = std::max(
sizes.min_size, std::min(transferred_sizes.min_size, sizes.max_size));
sizes.max_size = std::min(sizes.max_size, transferred_sizes.max_size);
}
// Tables can't shrink below their min-intrinsic size.
if (node.IsTable()) {
sizes.Encompass(min_max_sizes_func(SizeType::kIntrinsic).sizes.min_size);
}
sizes.max_size = std::max(sizes.max_size, sizes.min_size);
return sizes;
}
namespace {
// Computes the block-size for a fragment, ignoring the fixed block-size if set.
LayoutUnit ComputeBlockSizeForFragmentInternal(
const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
LayoutUnit intrinsic_size,
LayoutUnit inline_size,
LayoutUnit override_available_size = kIndefiniteSize) {
const ComputedStyle& style = node.Style();
// Scrollable percentage-sized children of table cells (sometimes) are sized
// to their initial min-size.
// See: https://drafts.csswg.org/css-tables-3/#row-layout
if (space.IsRestrictedBlockSizeTableCellChild()) {
return ResolveInitialMinBlockLength(space, style, border_padding,
style.LogicalMinHeight(),
override_available_size);
}
const Length& logical_height = style.LogicalHeight();
const bool has_aspect_ratio = !style.AspectRatio().IsAuto();
const bool may_apply_aspect_ratio =
has_aspect_ratio && inline_size != kIndefiniteSize;
const Length& auto_length = ([&]() {
if (space.AvailableSize().block_size == kIndefiniteSize) {
return Length::FitContent();
}
if (space.BlockAutoBehavior() == AutoSizeBehavior::kStretchExplicit) {
return Length::FillAvailable();
}
if (may_apply_aspect_ratio) {
return Length::FitContent();
}
if (space.BlockAutoBehavior() == AutoSizeBehavior::kStretchImplicit) {
return Length::FillAvailable();
}
DCHECK_EQ(space.BlockAutoBehavior(), AutoSizeBehavior::kFitContent);
return Length::FitContent();
})();
// Check if we should apply the automatic minimum size.
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
bool apply_automatic_min_size = ([&]() {
// We check for LayoutUnit::Max() as flexbox uses this as a "placeholder"
// to compute the flex line length while still respecting max-block-size.
if (intrinsic_size == kIndefiniteSize ||
intrinsic_size == LayoutUnit::Max()) {
return false;
}
if (style.IsScrollContainer()) {
return false;
}
if (!may_apply_aspect_ratio) {
return false;
}
if (logical_height.HasContentOrIntrinsic()) {
return true;
}
if (logical_height.HasAuto() && auto_length.HasContentOrIntrinsic()) {
return true;
}
return false;
})();
auto BlockSizeFunc = [&](SizeType type) {
if (type == SizeType::kContent && has_aspect_ratio &&
inline_size != kIndefiniteSize) {
return BlockSizeFromAspectRatio(
border_padding, style.LogicalAspectRatio(),
style.BoxSizingForAspectRatio(), inline_size);
}
return intrinsic_size;
};
const LayoutUnit extent = ResolveMainBlockLength(
space, style, border_padding, logical_height, &auto_length, BlockSizeFunc,
override_available_size);
if (extent == kIndefiniteSize) {
DCHECK_EQ(intrinsic_size, kIndefiniteSize);
return extent;
}
MinMaxSizes min_max = ComputeMinMaxBlockSizes(
space, node, border_padding,
apply_automatic_min_size ? &Length::MinIntrinsic() : nullptr,
BlockSizeFunc, override_available_size);
// When fragmentation is present often want to encompass the intrinsic size.
if (space.MinBlockSizeShouldEncompassIntrinsicSize() &&
intrinsic_size != kIndefiniteSize) {
min_max.Encompass(std::min(intrinsic_size, min_max.max_size));
}
return min_max.ClampSizeToMinAndMax(extent);
}
} // namespace
LayoutUnit ComputeBlockSizeForFragment(const ConstraintSpace& constraint_space,
const BlockNode& node,
const BoxStrut& border_padding,
LayoutUnit intrinsic_size,
LayoutUnit inline_size,
LayoutUnit override_available_size) {
// The |override_available_size| should only be used for <table>s.
DCHECK(override_available_size == kIndefiniteSize || node.IsTable());
if (constraint_space.IsFixedBlockSize()) {
LayoutUnit block_size = override_available_size == kIndefiniteSize
? constraint_space.AvailableSize().block_size
: override_available_size;
if (constraint_space.MinBlockSizeShouldEncompassIntrinsicSize())
return std::max(intrinsic_size, block_size);
return block_size;
}
if (constraint_space.IsTableCell() && intrinsic_size != kIndefiniteSize)
return intrinsic_size;
if (constraint_space.IsAnonymous())
return intrinsic_size;
return ComputeBlockSizeForFragmentInternal(
constraint_space, node, border_padding, intrinsic_size, inline_size,
override_available_size);
}
LayoutUnit ComputeInitialBlockSizeForFragment(
const ConstraintSpace& space,
const BlockNode& node,
const BoxStrut& border_padding,
LayoutUnit intrinsic_size,
LayoutUnit inline_size,
LayoutUnit override_available_size) {
if (space.IsInitialBlockSizeIndefinite())
return intrinsic_size;
return ComputeBlockSizeForFragment(space, node, border_padding,
intrinsic_size, inline_size,
override_available_size);
}
namespace {
// Returns the default natural size.
LogicalSize ComputeDefaultNaturalSize(const BlockNode& node) {
const auto& style = node.Style();
PhysicalSize natural_size(LayoutUnit(300), LayoutUnit(150));
natural_size.Scale(style.EffectiveZoom());
return ToLogicalSize(natural_size, style.GetWritingMode());
}
// This takes the aspect-ratio, and natural-sizes and normalizes them returning
// the border-box natural-size.
//
// The following combinations are possible:
// - an aspect-ratio with a natural-size
// - an aspect-ratio with no natural-size
// - no aspect-ratio with a natural-size
//
// It is not possible to have no aspect-ratio with no natural-size (as we'll
// use the default replaced size of 300x150 as a last resort).
// https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width
std::optional<LogicalSize> ComputeNormalizedNaturalSize(
const BlockNode& node,
const BoxStrut& border_padding,
const EBoxSizing box_sizing,
const LogicalSize& aspect_ratio) {
std::optional<LayoutUnit> intrinsic_inline;
std::optional<LayoutUnit> intrinsic_block;
node.IntrinsicSize(&intrinsic_inline, &intrinsic_block);
// Add the border-padding. If we *don't* have an aspect-ratio use the default
// natural size (300x150).
if (intrinsic_inline) {
intrinsic_inline = *intrinsic_inline + border_padding.InlineSum();
} else if (aspect_ratio.IsEmpty()) {
intrinsic_inline = ComputeDefaultNaturalSize(node).inline_size +
border_padding.InlineSum();
}
if (intrinsic_block) {
intrinsic_block = *intrinsic_block + border_padding.BlockSum();
} else if (aspect_ratio.IsEmpty()) {
intrinsic_block =
ComputeDefaultNaturalSize(node).block_size + border_padding.BlockSum();
}
// If we have one natural size reflect via. the aspect-ratio.
if (!intrinsic_inline && intrinsic_block) {
DCHECK(!aspect_ratio.IsEmpty());
intrinsic_inline = InlineSizeFromAspectRatio(border_padding, aspect_ratio,
box_sizing, *intrinsic_block);
}
// There are cases where the natural-size wont match the aspect-ratio. Always
// coerce the natural block-size to respect the aspect-ratio when present.
if (intrinsic_inline && (!intrinsic_block || !aspect_ratio.IsEmpty())) {
DCHECK(!aspect_ratio.IsEmpty());
intrinsic_block = BlockSizeFromAspectRatio(border_padding, aspect_ratio,
box_sizing, *intrinsic_inline);
}
DCHECK(intrinsic_inline.has_value() == intrinsic_block.has_value());
if (intrinsic_inline && intrinsic_block)
return LogicalSize(*intrinsic_inline, *intrinsic_block);
return std::nullopt;
}
// The main part of ComputeReplacedSize(). This function doesn't handle a
// case of <svg> as the documentElement.
LogicalSize ComputeReplacedSizeInternal(const BlockNode& node,
const ConstraintSpace& space,
const BoxStrut& border_padding,
ReplacedSizeMode mode) {
DCHECK(node.IsReplaced());
const ComputedStyle& style = node.Style();
const EBoxSizing box_sizing = style.BoxSizingForAspectRatio();
const LogicalSize aspect_ratio = node.GetReplacedAspectRatio();
const std::optional<LogicalSize> natural_size = ComputeNormalizedNaturalSize(
node, border_padding, box_sizing, aspect_ratio);
const Length& block_length = style.LogicalHeight();
auto BlockSizeFunc = [&](SizeType) -> LayoutUnit {
if (aspect_ratio.IsEmpty()) {
DCHECK(natural_size);
return natural_size->block_size;
}
if (mode == ReplacedSizeMode::kNormal) {
return ComputeReplacedSize(node, space, border_padding,
ReplacedSizeMode::kIgnoreBlockLengths)
.block_size;
}
return kIndefiniteSize;
};
MinMaxSizes block_min_max_sizes;
std::optional<LayoutUnit> replaced_block;
if (mode == ReplacedSizeMode::kIgnoreBlockLengths) {
// Don't resolve any block lengths or constraints.
block_min_max_sizes = {LayoutUnit(), LayoutUnit::Max()};
} else {
// Replaced elements in quirks-mode resolve their min/max block-sizes
// against a different size than the main size. See:
// - https://www.w3.org/TR/CSS21/visudet.html#min-max-heights
// - https://bugs.chromium.org/p/chromium/issues/detail?id=385877
// For the history on this behavior. Fortunately if this is the case we can
// just use the given available size to resolve these sizes against.
const LayoutUnit min_max_percentage_resolution_size =
node.GetDocument().InQuirksMode() && !node.IsOutOfFlowPositioned()
? space.AvailableSize().block_size
: space.PercentageResolutionBlockSize();
block_min_max_sizes = {
ResolveMinBlockLength(space, style, border_padding, BlockSizeFunc,
style.LogicalMinHeight(),
/* auto_length */ nullptr,
/* override_available_size */ kIndefiniteSize,
&min_max_percentage_resolution_size),
ResolveMaxBlockLength(space, style, border_padding,
style.LogicalMaxHeight(), BlockSizeFunc,
/* override_available_size */ kIndefiniteSize,
&min_max_percentage_resolution_size)};
if (space.IsFixedBlockSize()) {
replaced_block = space.AvailableSize().block_size;
DCHECK_GE(*replaced_block, 0);
} else {
const Length& auto_block_length = space.IsBlockAutoBehaviorStretch()
? Length::FillAvailable()
: Length::Auto();
const LayoutUnit block_size = ResolveMainBlockLength(
space, style, border_padding, block_length, &auto_block_length,
/* intrinsic_size */ kIndefiniteSize);
if (block_size != kIndefiniteSize) {
DCHECK_GE(block_size, LayoutUnit());
replaced_block = block_min_max_sizes.ClampSizeToMinAndMax(block_size);
}
}
}
const Length& inline_length = style.LogicalWidth();
auto StretchFit = [&]() -> LayoutUnit {
LayoutUnit size;
if (space.AvailableSize().inline_size == kIndefiniteSize) {
size = border_padding.InlineSum();
// TODO(crbug.com/1218055): Instead of using the default natural size, we
// should be using the initial containing block size. When doing this
// we'll need to invalidated (sparingly) on window resize.
// TODO(https://crbug.com/313072): Values with intrinsic sizing or
// content sizing keywords should perhaps also get the natural size here
// (or be zero).
if (inline_length.HasPercent()) {
size += ComputeDefaultNaturalSize(node).inline_size;
}
} else {
// Stretch to the available-size if it is definite.
size = ResolveMainInlineLength(
space, style, border_padding,
[](SizeType) -> MinMaxSizesResult { NOTREACHED(); },
Length::FillAvailable(), /* auto_length */ nullptr,
/* override_available_size */ kIndefiniteSize);
}
// If stretch-fit applies we must have an aspect-ratio.
DCHECK(!aspect_ratio.IsEmpty());
// Apply the transferred min/max sizes.
const MinMaxSizes transferred_min_max_sizes =
ComputeTransferredMinMaxInlineSizes(aspect_ratio, block_min_max_sizes,
border_padding, box_sizing);
size = transferred_min_max_sizes.ClampSizeToMinAndMax(size);
return size;
};
auto MinMaxSizesFunc = [&](SizeType) -> MinMaxSizesResult {
LayoutUnit size;
if (aspect_ratio.IsEmpty()) {
DCHECK(natural_size);
size = natural_size->inline_size;
} else if (replaced_block) {
size = InlineSizeFromAspectRatio(border_padding, aspect_ratio, box_sizing,
*replaced_block);
} else if (natural_size) {
DCHECK_NE(mode, ReplacedSizeMode::kIgnoreInlineLengths);
size = ComputeReplacedSize(node, space, border_padding,
ReplacedSizeMode::kIgnoreInlineLengths)
.inline_size;
} else {
// We don't have a natural size - default to stretching.
size = StretchFit();
}
// |depends_on_block_constraints| doesn't matter in this context.
MinMaxSizes sizes;
sizes += size;
return {sizes, /* depends_on_block_constraints */ false};
};
MinMaxSizes inline_min_max_sizes;
std::optional<LayoutUnit> replaced_inline;
if (mode == ReplacedSizeMode::kIgnoreInlineLengths) {
// Don't resolve any inline lengths or constraints.
inline_min_max_sizes = {LayoutUnit(), LayoutUnit::Max()};
} else {
inline_min_max_sizes = {
ResolveMinInlineLength(space, style, border_padding, MinMaxSizesFunc,
style.LogicalMinWidth()),
ResolveMaxInlineLength(space, style, border_padding, MinMaxSizesFunc,
style.LogicalMaxWidth())};
if (space.IsFixedInlineSize()) {
replaced_inline = space.AvailableSize().inline_size;
DCHECK_GE(*replaced_inline, 0);
} else {
const Length& auto_length = space.IsInlineAutoBehaviorStretch()
? Length::FillAvailable()
: Length::Auto();
const LayoutUnit inline_size =
ResolveMainInlineLength(space, style, border_padding, MinMaxSizesFunc,
inline_length, &auto_length);
if (inline_size != kIndefiniteSize) {
DCHECK_GE(inline_size, LayoutUnit());
replaced_inline =
inline_min_max_sizes.ClampSizeToMinAndMax(inline_size);
}
}
}
if (replaced_inline && replaced_block)
return LogicalSize(*replaced_inline, *replaced_block);
// We have *only* an aspect-ratio with no sizes (natural or otherwise), we
// default to stretching.
if (!natural_size && !replaced_inline && !replaced_block) {
replaced_inline = StretchFit();
replaced_inline =
inline_min_max_sizes.ClampSizeToMinAndMax(*replaced_inline);
}
// We only know one size, the other gets computed via the aspect-ratio (if
// present), or defaults to the natural-size.
if (replaced_inline) {
DCHECK(!replaced_block);
DCHECK(natural_size || !aspect_ratio.IsEmpty());
replaced_block = aspect_ratio.IsEmpty() ? natural_size->block_size
: BlockSizeFromAspectRatio(
border_padding, aspect_ratio,
box_sizing, *replaced_inline);
replaced_block = block_min_max_sizes.ClampSizeToMinAndMax(*replaced_block);
return LogicalSize(*replaced_inline, *replaced_block);
}
if (replaced_block) {
DCHECK(!replaced_inline);
DCHECK(natural_size || !aspect_ratio.IsEmpty());
replaced_inline = aspect_ratio.IsEmpty() ? natural_size->inline_size
: InlineSizeFromAspectRatio(
border_padding, aspect_ratio,
box_sizing, *replaced_block);
replaced_inline =
inline_min_max_sizes.ClampSizeToMinAndMax(*replaced_inline);
return LogicalSize(*replaced_inline, *replaced_block);
}
// Both lengths are unknown, start with the natural-size.
DCHECK(!replaced_inline);
DCHECK(!replaced_block);
replaced_inline = natural_size->inline_size;
replaced_block = natural_size->block_size;
// Apply the min/max sizes to the natural-size.
const LayoutUnit constrained_inline =
inline_min_max_sizes.ClampSizeToMinAndMax(*replaced_inline);
const LayoutUnit constrained_block =
block_min_max_sizes.ClampSizeToMinAndMax(*replaced_block);
// If the min/max sizes had no effect, just return the natural-size.
if (constrained_inline == replaced_inline &&
constrained_block == replaced_block)
return LogicalSize(*replaced_inline, *replaced_block);
// If we have no aspect-ratio, use both constrained sizes.
if (aspect_ratio.IsEmpty())
return {constrained_inline, constrained_block};
// The min/max sizes have applied, try to respect the aspect-ratio.
// The following implements the table from section 10.4 at:
// https://www.w3.org/TR/CSS22/visudet.html#min-max-widths
const bool is_min_inline_constrained = constrained_inline > *replaced_inline;
const bool is_max_inline_constrained = constrained_inline < *replaced_inline;
const bool is_min_block_constrained = constrained_block > *replaced_block;
const bool is_max_block_constrained = constrained_block < *replaced_block;
// Constraints caused us to grow in one dimension and shrink in the other.
// Use both constrained sizes.
if ((is_max_inline_constrained && is_min_block_constrained) ||
(is_min_inline_constrained && is_max_block_constrained))
return {constrained_inline, constrained_block};
const LayoutUnit hypothetical_block = BlockSizeFromAspectRatio(
border_padding, aspect_ratio, box_sizing, constrained_inline);
const LayoutUnit hypothetical_inline = InlineSizeFromAspectRatio(
border_padding, aspect_ratio, box_sizing, constrained_block);
// If the inline-size got constrained more extremely than the block-size, use
// the constrained inline-size, and recalculate the block-size.
if (constrained_block == *replaced_block ||
(is_max_inline_constrained && hypothetical_block <= constrained_block) ||
(is_min_inline_constrained &&
constrained_inline >= hypothetical_inline)) {
return {constrained_inline,
block_min_max_sizes.ClampSizeToMinAndMax(hypothetical_block)};
}
// If the block-size got constrained more extremely than the inline-size, use
// the constrained block-size, and recalculate the inline-size.
return {inline_min_max_sizes.ClampSizeToMinAndMax(hypothetical_inline),
constrained_block};
}
} // namespace
// Computes size for a replaced element.
LogicalSize ComputeReplacedSize(const BlockNode& node,
const ConstraintSpace& space,
const BoxStrut& border_padding,
ReplacedSizeMode mode) {
DCHECK(node.IsReplaced());
const auto* svg_root = DynamicTo<LayoutSVGRoot>(node.GetLayoutBox());
if (!svg_root || !svg_root->IsDocumentElement()) {
return ComputeReplacedSizeInternal(node, space, border_padding, mode);
}
PhysicalSize container_size(svg_root->GetContainerSize());
if (!container_size.IsEmpty()) {
LogicalSize size =
ToLogicalSize(container_size, node.Style().GetWritingMode());
size.inline_size += border_padding.InlineSum();
size.block_size += border_padding.BlockSum();
return size;
}
if (svg_root->IsEmbeddedThroughFrameContainingSVGDocument()) {
LogicalSize size = space.AvailableSize();
size.block_size = node.Style().IsHorizontalWritingMode()
? node.InitialContainingBlockSize().height
: node.InitialContainingBlockSize().width;
return size;
}
LogicalSize size =
ComputeReplacedSizeInternal(node, space, border_padding, mode);
if (node.Style().LogicalWidth().HasPercent()) {
double factor = svg_root->LogicalSizeScaleFactorForPercentageLengths();
if (factor != 1.0) {
// TODO(https://crbug.com/313072): Just because a calc *has* percentages
// doesn't mean *all* the lengths are percentages.
size.inline_size *= factor;
}
}
const Length& logical_height = node.Style().LogicalHeight();
if (logical_height.HasPercent()) {
// TODO(https://crbug.com/313072): Might this also be needed for intrinsic
// sizing keywords?
LayoutUnit height = ValueForLength(
logical_height,
node.GetDocument().GetLayoutView()->ViewLogicalHeightForPercentages());
double factor = svg_root->LogicalSizeScaleFactorForPercentageLengths();
if (factor != 1.0) {
// TODO(https://crbug.com/313072): Just because a calc *has* percentages
// doesn't mean *all* the lengths are percentages.
height *= factor;
}
size.block_size = height;
}
return size;
}
int ResolveUsedColumnCount(int computed_count,
LayoutUnit computed_size,
LayoutUnit used_gap,
LayoutUnit available_size) {
if (computed_size == kIndefiniteSize) {
if (!computed_count) {
// Both `column-width` and `column-count` are auto. We're here because
// `column-height` is non-auto. Set column count to 1.
return 1;
}
return computed_count;
}
DCHECK_GT(computed_size, LayoutUnit());
int count_from_width =
((available_size + used_gap) / (computed_size + used_gap)).ToInt();
count_from_width = std::max(1, count_from_width);
if (!computed_count)
return count_from_width;
return std::max(1, std::min(computed_count, count_from_width));
}
int ResolveUsedColumnCount(const ComputedStyle& style,
LayoutUnit available_size) {
LayoutUnit computed_column_inline_size =
style.HasAutoColumnWidth()
? kIndefiniteSize
: std::max(LayoutUnit(1), LayoutUnit(style.ColumnWidth()));
LayoutUnit gap = ResolveColumnGapForMulticol(style, available_size);
int computed_count = style.HasAutoColumnCount() ? 0 : style.ColumnCount();
return ResolveUsedColumnCount(computed_count, computed_column_inline_size,
gap, available_size);
}
LayoutUnit ResolveUsedColumnInlineSize(int computed_count,
LayoutUnit computed_size,
LayoutUnit used_gap,
LayoutUnit available_size) {
int used_count = ResolveUsedColumnCount(computed_count, computed_size,
used_gap, available_size);
return std::max(((available_size + used_gap) / used_count) - used_gap,
LayoutUnit());
}
LayoutUnit ResolveUsedColumnInlineSize(const ComputedStyle& style,
LayoutUnit available_size) {
// Should only attempt to resolve this if columns != auto.
DCHECK(style.SpecifiesColumns());
LayoutUnit computed_size =
style.HasAutoColumnWidth()
? kIndefiniteSize
: std::max(LayoutUnit(1), LayoutUnit(style.ColumnWidth()));
int computed_count = style.HasAutoColumnCount() ? 0 : style.ColumnCount();
LayoutUnit used_gap = ResolveColumnGapForMulticol(style, available_size);
return ResolveUsedColumnInlineSize(computed_count, computed_size, used_gap,
available_size);
}
std::optional<LayoutUnit> ResolveColumnGapLength(const ComputedStyle& style,
LayoutUnit available_size) {
if (const std::optional<Length>& gap = style.ColumnGap()) {
return MinimumValueForLength(*gap, available_size.ClampIndefiniteToZero());
}
return std::nullopt;
}
LayoutUnit ResolveColumnGapForMulticol(const ComputedStyle& style,
LayoutUnit available_size) {
return ResolveColumnGapLength(style, available_size)
.value_or(LayoutUnit(style.GetFontDescription().ComputedPixelSize()));
}
std::optional<LayoutUnit> ResolveRowGapLength(const ComputedStyle& style,
LayoutUnit available_size) {
if (const std::optional<Length>& gap = style.RowGap()) {
return MinimumValueForLength(*gap, available_size.ClampIndefiniteToZero());
}
return std::nullopt;
}
LayoutUnit ResolveRowGapForMulticol(const ComputedStyle& style,
LayoutUnit available_size) {
return ResolveRowGapLength(style, available_size)
.value_or(LayoutUnit(style.GetFontDescription().ComputedPixelSize()));
}
std::optional<LayoutUnit> ResolveItemToleranceLength(
const ComputedStyle& style,
LayoutUnit available_size) {
if (const auto& item_tolerance = style.ItemTolerance()) {
return MinimumValueForLength(*item_tolerance,
available_size.ClampIndefiniteToZero());
}
return std::nullopt;
}
LayoutUnit ResolveItemToleranceForMasonry(const ComputedStyle& style,
const LogicalSize& available_size) {
return ResolveItemToleranceLength(
style, (style.MasonryTrackSizingDirection() == kForColumns)
? available_size.block_size
: available_size.inline_size)
.value_or(LayoutUnit(style.GetFontDescription().ComputedPixelSize()));
}
LayoutUnit ColumnInlineProgression(const ComputedStyle& style,
LayoutUnit available_size) {
return ResolveUsedColumnInlineSize(style, available_size) +
ResolveColumnGapForMulticol(style, available_size);
}
PhysicalBoxStrut ComputePhysicalMargins(
const ComputedStyle& style,
PhysicalSize percentage_resolution_size) {
if (!style.MayHaveMargin())
return PhysicalBoxStrut();
return PhysicalBoxStrut(
MinimumValueForLength(style.MarginTop(),
percentage_resolution_size.height),
MinimumValueForLength(style.MarginRight(),
percentage_resolution_size.width),
MinimumValueForLength(style.MarginBottom(),
percentage_resolution_size.height),
MinimumValueForLength(style.MarginLeft(),
percentage_resolution_size.width));
}
BoxStrut ComputeMarginsFor(const ConstraintSpace& constraint_space,
const ComputedStyle& style,
const ConstraintSpace& compute_for) {
if (!style.MayHaveMargin() || constraint_space.IsAnonymous())
return BoxStrut();
LogicalSize percentage_resolution_size =
constraint_space.MarginPaddingPercentageResolutionSize();
return ComputePhysicalMargins(style, percentage_resolution_size)
.ConvertToLogical(compute_for.GetWritingDirection());
}
namespace {
BoxStrut ComputeBordersInternal(const ComputedStyle& style) {
return PhysicalBoxStrut(style.BorderTopWidth(), style.BorderRightWidth(),
style.BorderBottomWidth(), style.BorderLeftWidth())
.ConvertToLogical(style.GetWritingDirection());
}
} // namespace
BoxStrut ComputeBorders(const ConstraintSpace& constraint_space,
const BlockNode& node) {
// If we are producing an anonymous fragment (e.g. a column), it has no
// borders, padding or scrollbars. Using the ones from the container can only
// cause trouble.
if (constraint_space.IsAnonymous())
return BoxStrut();
// If we are a table cell we just access the values set by the parent table
// layout as border may be collapsed etc.
if (constraint_space.IsTableCell())
return constraint_space.TableCellBorders();
if (node.IsTable()) {
return To<TableNode>(node).GetTableBorders()->TableBorder();
}
return ComputeBordersInternal(node.Style());
}
BoxStrut ComputeBordersForInline(const ComputedStyle& style) {
return ComputeBordersInternal(style);
}
BoxStrut ComputeNonCollapsedTableBorders(const ComputedStyle& style) {
return ComputeBordersInternal(style);
}
BoxStrut ComputeBordersForTest(const ComputedStyle& style) {
return ComputeBordersInternal(style);
}
BoxStrut ComputePadding(const ConstraintSpace& constraint_space,
const ComputedStyle& style) {
// If we are producing an anonymous fragment (e.g. a column) we shouldn't
// have any padding.
if (!style.MayHavePadding() || constraint_space.IsAnonymous())
return BoxStrut();
// Tables with collapsed borders don't have any padding.
if (style.IsDisplayTableBox() &&
style.BorderCollapse() == EBorderCollapse::kCollapse) {
return BoxStrut();
}
// This function may be called for determining intrinsic padding, clamp
// indefinite %-sizes to zero. See:
// https://drafts.csswg.org/css-sizing-3/#min-percentage-contribution
LogicalSize percentage_resolution_size =
constraint_space.MarginPaddingPercentageResolutionSize()
.ClampIndefiniteToZero();
return {MinimumValueForLength(style.PaddingInlineStart(),
percentage_resolution_size.inline_size),
MinimumValueForLength(style.PaddingInlineEnd(),
percentage_resolution_size.inline_size),
MinimumValueForLength(style.PaddingBlockStart(),
percentage_resolution_size.block_size),
MinimumValueForLength(style.PaddingBlockEnd(),
percentage_resolution_size.block_size)};
}
BoxStrut ComputeScrollbarsForNonAnonymous(const BlockNode& node) {
const ComputedStyle& style = node.Style();
if (!style.IsScrollContainer() && style.IsScrollbarGutterAuto())
return BoxStrut();
const LayoutBox* layout_box = node.GetLayoutBox();
return layout_box->ComputeLogicalScrollbars();
}
void ResolveInlineAutoMargins(const ComputedStyle& style,
const ComputedStyle& container_style,
LayoutUnit available_inline_size,
LayoutUnit inline_size,
BoxStrut* margins) {
const LayoutUnit used_space = inline_size + margins->InlineSum();
const LayoutUnit available_space = available_inline_size - used_space;
bool is_start_auto = style.MarginInlineStartUsing(container_style).IsAuto();
bool is_end_auto = style.MarginInlineEndUsing(container_style).IsAuto();
if (is_start_auto && is_end_auto) {
margins->inline_start = (available_space / 2).ClampNegativeToZero();
margins->inline_end =
available_inline_size - inline_size - margins->inline_start;
} else if (is_start_auto) {
margins->inline_start = available_space.ClampNegativeToZero();
} else if (is_end_auto) {
margins->inline_end =
available_inline_size - inline_size - margins->inline_start;
}
}
void ResolveAutoMargins(Length start_length,
Length end_length,
LayoutUnit additional_space,
LayoutUnit* start_result,
LayoutUnit* end_result) {
bool start_is_auto = start_length.IsAuto();
bool end_is_auto = end_length.IsAuto();
if (start_is_auto) {
if (end_is_auto) {
*start_result = additional_space / 2;
additional_space -= *start_result;
} else {
*start_result = additional_space;
}
}
if (end_is_auto) {
*end_result = additional_space;
}
}
void ResolveAutoMargins(Length inline_start_length,
Length inline_end_length,
Length block_start_length,
Length block_end_length,
LayoutUnit additional_inline_space,
LayoutUnit additional_block_space,
BoxStrut* margins) {
ResolveAutoMargins(inline_start_length, inline_end_length,
additional_inline_space, &margins->inline_start,
&margins->inline_end);
ResolveAutoMargins(block_start_length, block_end_length,
additional_block_space, &margins->block_start,
&margins->block_end);
}
LayoutUnit LineOffsetForTextAlign(ETextAlign text_align,
TextDirection direction,
LayoutUnit space_left) {
bool is_ltr = IsLtr(direction);
if (text_align == ETextAlign::kStart || text_align == ETextAlign::kJustify)
text_align = is_ltr ? ETextAlign::kLeft : ETextAlign::kRight;
else if (text_align == ETextAlign::kEnd)
text_align = is_ltr ? ETextAlign::kRight : ETextAlign::kLeft;
switch (text_align) {
case ETextAlign::kLeft:
case ETextAlign::kWebkitLeft: {
// The direction of the block should determine what happens with wide
// lines. In particular with RTL blocks, wide lines should still spill
// out to the left.
if (is_ltr)
return LayoutUnit();
return space_left.ClampPositiveToZero();
}
case ETextAlign::kRight:
case ETextAlign::kWebkitRight: {
// In RTL, trailing spaces appear on the left of the line.
if (!is_ltr) [[unlikely]] {
return space_left;
}
// Wide lines spill out of the block based off direction.
// So even if text-align is right, if direction is LTR, wide lines
// should overflow out of the right side of the block.
if (space_left > LayoutUnit())
return space_left;
return LayoutUnit();
}
case ETextAlign::kCenter:
case ETextAlign::kWebkitCenter: {
if (is_ltr)
return (space_left / 2).ClampNegativeToZero();
// In RTL, trailing spaces appear on the left of the line.
if (space_left > LayoutUnit())
return (space_left / 2).ClampNegativeToZero();
// In RTL, wide lines should spill out to the left, same as kRight.
return space_left;
}
default:
NOTREACHED();
}
}
// Calculates default content size for html and body elements in quirks mode.
// Returns |kIndefiniteSize| in all other cases.
LayoutUnit CalculateDefaultBlockSize(const ConstraintSpace& space,
const BlockNode& node,
const BlockBreakToken* break_token,
const BoxStrut& border_scrollbar_padding) {
// In quirks mode, html and body elements will completely fill the ICB, block
// percentages should resolve against this size.
if (node.IsQuirkyAndFillsViewport() && !IsBreakInside(break_token)) {
LayoutUnit block_size = space.AvailableSize().block_size;
block_size -= ComputeMarginsForSelf(space, node.Style()).BlockSum();
return std::max(block_size.ClampNegativeToZero(),
border_scrollbar_padding.BlockSum());
}
return kIndefiniteSize;
}
FragmentGeometry CalculateInitialFragmentGeometry(
const ConstraintSpace& space,
const BlockNode& node,
const BlockBreakToken* break_token,
MinMaxSizesFunctionRef min_max_sizes_func,
bool is_intrinsic) {
const auto& style = node.Style();
if (node.IsFrameSet()) {
if (node.IsParentNGFrameSet()) {
const auto size = space.AvailableSize();
DCHECK_NE(size.inline_size, kIndefiniteSize);
DCHECK_NE(size.block_size, kIndefiniteSize);
DCHECK(space.IsFixedInlineSize());
DCHECK(space.IsFixedBlockSize());
return {size, {}, {}, {}};
}
const auto size = node.InitialContainingBlockSize();
return {ToLogicalSize(size, style.GetWritingMode()), {}, {}, {}};
}
const auto border = ComputeBorders(space, node);
const auto padding = ComputePadding(space, style);
auto scrollbar = ComputeScrollbars(space, node);
const auto border_padding = border + padding;
const auto border_scrollbar_padding = border_padding + scrollbar;
if (node.IsReplaced()) {
const auto border_box_size = ComputeReplacedSize(
node, space, border_padding,
is_intrinsic ? ReplacedSizeMode::kIgnoreInlineLengths
: ReplacedSizeMode::kNormal);
return {border_box_size, border, scrollbar, padding};
}
const LayoutUnit inline_size =
is_intrinsic ? kIndefiniteSize
: ComputeInlineSizeForFragment(space, node, border_padding,
min_max_sizes_func);
if (inline_size != kIndefiniteSize &&
inline_size < border_scrollbar_padding.InlineSum() &&
scrollbar.InlineSum() && !space.IsAnonymous()) [[unlikely]] {
// Clamp the inline size of the scrollbar, unless it's larger than the
// inline size of the content box, in which case we'll return that instead.
// Scrollbar handling is quite bad in such situations, and this method here
// is just to make sure that left-hand scrollbars don't mess up scrollWidth.
// For the full story, visit http://crbug.com/724255.
const auto content_box_inline_size =
inline_size - border_padding.InlineSum();
if (scrollbar.InlineSum() > content_box_inline_size) {
if (scrollbar.inline_end) {
DCHECK(!scrollbar.inline_start);
scrollbar.inline_end = content_box_inline_size;
} else {
DCHECK(scrollbar.inline_start);
scrollbar.inline_start = content_box_inline_size;
}
}
}
const auto default_block_size = CalculateDefaultBlockSize(
space, node, break_token, border_scrollbar_padding);
const auto block_size = ComputeInitialBlockSizeForFragment(
space, node, border_padding, default_block_size, inline_size);
return {LogicalSize(inline_size, block_size), border, scrollbar, padding};
}
FragmentGeometry CalculateInitialFragmentGeometry(
const ConstraintSpace& space,
const BlockNode& node,
const BlockBreakToken* break_token,
bool is_intrinsic) {
auto MinMaxSizesFunc = [&](SizeType type) -> MinMaxSizesResult {
return node.ComputeMinMaxSizes(space.GetWritingMode(), type, space);
};
return CalculateInitialFragmentGeometry(space, node, break_token,
MinMaxSizesFunc, is_intrinsic);
}
LogicalSize ShrinkLogicalSize(LogicalSize size, const BoxStrut& insets) {
if (size.inline_size != kIndefiniteSize) {
size.inline_size =
(size.inline_size - insets.InlineSum()).ClampNegativeToZero();
}
if (size.block_size != kIndefiniteSize) {
size.block_size =
(size.block_size - insets.BlockSum()).ClampNegativeToZero();
}
return size;
}
LogicalSize CalculateChildAvailableSize(
const ConstraintSpace& space,
const BlockNode& node,
const LogicalSize border_box_size,
const BoxStrut& border_scrollbar_padding) {
LogicalSize child_available_size =
ShrinkLogicalSize(border_box_size, border_scrollbar_padding);
if (space.IsAnonymous() ||
(node.IsAnonymousBlockFlow() &&
child_available_size.block_size == kIndefiniteSize)) {
child_available_size.block_size = space.AvailableSize().block_size;
}
return child_available_size;
}
namespace {
// Implements the common part of the child percentage size calculation. Deals
// with how percentages are propagated from parent to child in quirks mode.
LogicalSize AdjustChildPercentageSize(const ConstraintSpace& space,
const BlockNode node,
LogicalSize child_percentage_size,
LayoutUnit parent_percentage_block_size) {
// In quirks mode the percentage resolution height is passed from parent to
// child.
// https://quirks.spec.whatwg.org/#the-percentage-height-calculation-quirk
if (child_percentage_size.block_size == kIndefiniteSize &&
node.UseParentPercentageResolutionBlockSizeForChildren())
child_percentage_size.block_size = parent_percentage_block_size;
return child_percentage_size;
}
} // namespace
LogicalSize CalculateChildPercentageSize(
const ConstraintSpace& space,
const BlockNode node,
const LogicalSize child_available_size) {
// Anonymous block or spaces should use the parent percent block-size.
if (space.IsAnonymous() || node.IsAnonymousBlockFlow()) {
return {child_available_size.inline_size,
space.PercentageResolutionBlockSize()};
}
// Table cell children don't apply the "percentage-quirk". I.e. if their
// percentage resolution block-size is indefinite, they don't pass through
// their parent's percentage resolution block-size.
if (space.IsTableCellChild())
return child_available_size;
return AdjustChildPercentageSize(space, node, child_available_size,
space.PercentageResolutionBlockSize());
}
LogicalSize CalculateReplacedChildPercentageSize(
const ConstraintSpace& space,
const BlockNode node,
const LogicalSize child_available_size,
const BoxStrut& border_scrollbar_padding,
const BoxStrut& border_padding) {
// Anonymous block or spaces should use the parent percent block-size.
if (space.IsAnonymous() || node.IsAnonymousBlockFlow()) {
return {child_available_size.inline_size,
space.ReplacedChildPercentageResolutionBlockSize()};
}
// Table cell children don't apply the "percentage-quirk". I.e. if their
// percentage resolution block-size is indefinite, they don't pass through
// their parent's percentage resolution block-size.
if (space.IsTableCellChild())
return child_available_size;
// Replaced descendants of a table-cell which has a definite block-size,
// always resolve their percentages against this size (even during the
// "layout" pass where the fixed block-size may be different).
//
// This ensures that between the table-cell "measure" and "layout" passes
// the replaced descendants remain the same size.
if (space.IsTableCell() && node.Style().LogicalHeight().IsFixed()) {
LayoutUnit block_size = ComputeBlockSizeForFragmentInternal(
space, node, border_padding, kIndefiniteSize /* intrinsic_size */,
kIndefiniteSize /* inline_size */);
DCHECK_NE(block_size, kIndefiniteSize);
return {child_available_size.inline_size,
(block_size - border_scrollbar_padding.BlockSum())
.ClampNegativeToZero()};
}
return AdjustChildPercentageSize(
space, node, child_available_size,
space.ReplacedChildPercentageResolutionBlockSize());
}
LayoutUnit ClampIntrinsicBlockSize(
const ConstraintSpace& space,
const BlockNode& node,
const BlockBreakToken* break_token,
const BoxStrut& border_scrollbar_padding,
LayoutUnit current_intrinsic_block_size,
std::optional<LayoutUnit> body_margin_block_sum) {
// Tables don't respect size containment, or apply the "fill viewport" quirk.
DCHECK(!node.IsTable());
const LayoutUnit intrinsic_block_size =
CalculateIntrinsicBlockSizeIgnoringChildren(node,
border_scrollbar_padding);
if (intrinsic_block_size != kIndefiniteSize) {
return intrinsic_block_size;
}
// Apply the "fills viewport" quirk if needed.
const ComputedStyle& style = node.Style();
if (!IsBreakInside(break_token) && node.IsQuirkyAndFillsViewport() &&
style.LogicalHeight().IsAuto() &&
space.AvailableSize().block_size != kIndefiniteSize) {
DCHECK_EQ(node.IsBody() && !node.CreatesNewFormattingContext(),
body_margin_block_sum.has_value());
LayoutUnit margin_sum = body_margin_block_sum.value_or(
ComputeMarginsForSelf(space, style).BlockSum());
current_intrinsic_block_size = std::max(
current_intrinsic_block_size,
(space.AvailableSize().block_size - margin_sum).ClampNegativeToZero());
}
return current_intrinsic_block_size;
}
std::optional<MinMaxSizesResult> CalculateMinMaxSizesIgnoringChildren(
const BlockNode& node,
const BoxStrut& border_scrollbar_padding) {
MinMaxSizes sizes;
sizes += border_scrollbar_padding.InlineSum();
// Check if the intrinsic size was overridden.
const LayoutUnit override_size = node.OverrideIntrinsicContentInlineSize();
if (override_size != kIndefiniteSize) {
sizes += override_size;
return MinMaxSizesResult{sizes, /* depends_on_block_constraints */ false};
}
// Check if we have a "default" size (a <textarea>).
const LayoutUnit default_size = node.DefaultIntrinsicContentInlineSize();
if (default_size != kIndefiniteSize) {
sizes += default_size;
// <textarea>'s intrinsic size should ignore scrollbar existence.
if (node.IsTextArea()) {
sizes -= ComputeScrollbarsForNonAnonymous(node).InlineSum();
}
return MinMaxSizesResult{sizes,
/* depends_on_block_constraints */ false};
}
// Size contained elements don't consider children for intrinsic sizing.
// Also, if we don't have children, we can determine the size immediately.
if (node.ShouldApplyInlineSizeContainment() || !node.FirstChild()) {
return MinMaxSizesResult{sizes,
/* depends_on_block_constraints */ false};
}
return std::nullopt;
}
LayoutUnit CalculateIntrinsicBlockSizeIgnoringChildren(
const BlockNode& node,
const BoxStrut& border_scrollbar_padding) {
// Check if the intrinsic size was overridden.
const LayoutUnit override_size = node.OverrideIntrinsicContentBlockSize();
if (override_size != kIndefiniteSize) {
return override_size + border_scrollbar_padding.BlockSum();
}
// Check if we have a "default" size (a <textarea>).
const LayoutUnit default_block_size = node.DefaultIntrinsicContentBlockSize();
if (default_block_size != kIndefiniteSize) {
// <textarea>'s intrinsic size should ignore scrollbar existence.
if (node.IsTextArea()) {
return default_block_size -
ComputeScrollbarsForNonAnonymous(node).BlockSum() +
border_scrollbar_padding.BlockSum();
}
return default_block_size + border_scrollbar_padding.BlockSum();
}
if (node.ShouldApplyBlockSizeContainment()) {
return border_scrollbar_padding.BlockSum();
}
return kIndefiniteSize;
}
void AddScrollbarFreeze(const BoxStrut& scrollbars_before,
const BoxStrut& scrollbars_after,
WritingDirectionMode writing_direction,
bool* freeze_horizontal,
bool* freeze_vertical) {
PhysicalBoxStrut physical_before =
scrollbars_before.ConvertToPhysical(writing_direction);
PhysicalBoxStrut physical_after =
scrollbars_after.ConvertToPhysical(writing_direction);
*freeze_horizontal |= (!physical_before.top && physical_after.top) ||
(!physical_before.bottom && physical_after.bottom);
*freeze_vertical |= (!physical_before.left && physical_after.left) ||
(!physical_before.right && physical_after.right);
}
} // namespace blink
|