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 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
|
/*
* Copyright (C) 2007, 2008, 2009 Apple Computer, Inc.
* Copyright (C) 2010, 2011 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/core/editing/editing_style.h"
#include "base/memory/values_equivalent.h"
#include "base/stl_util.h"
#include "mojo/public/mojom/base/text_direction.mojom-blink.h"
#include "third_party/blink/renderer/core/css/css_color.h"
#include "third_party/blink/renderer/core/css/css_computed_style_declaration.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_identifier_value_mappings.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/css/css_property_value_set.h"
#include "third_party/blink/renderer/core/css/css_rule_list.h"
#include "third_party/blink/renderer/core/css/css_style_rule.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/font_size_functions.h"
#include "third_party/blink/renderer/core/css/parser/css_parser.h"
#include "third_party/blink/renderer/core/css/properties/css_property.h"
#include "third_party/blink/renderer/core/css/properties/longhands.h"
#include "third_party/blink/renderer/core/css/properties/shorthands.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver.h"
#include "third_party/blink/renderer/core/css/style_rule.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/node.h"
#include "third_party/blink/renderer/core/dom/node_traversal.h"
#include "third_party/blink/renderer/core/dom/qualified_name.h"
#include "third_party/blink/renderer/core/editing/commands/apply_style_command.h"
#include "third_party/blink/renderer/core/editing/editing_style_utilities.h"
#include "third_party/blink/renderer/core/editing/editing_tri_state.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/editor.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/editing/serializers/html_interchange.h"
#include "third_party/blink/renderer/core/editing/visible_selection.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/html/html_font_element.h"
#include "third_party/blink/renderer/core/html/html_span_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/style_property_shorthand.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/disallow_new_wrapper.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
namespace blink {
// Editing style properties must be preserved during editing operation.
// e.g. when a user inserts a new paragraph, all properties listed here must be
// copied to the new paragraph.
// NOTE: Use either allEditingProperties() or inheritableEditingProperties() to
// respect runtime enabling of properties.
static const CSSPropertyID kStaticEditingProperties[] = {
CSSPropertyID::kBackgroundColor,
CSSPropertyID::kColor,
CSSPropertyID::kFontFamily,
CSSPropertyID::kFontSize,
CSSPropertyID::kFontStyle,
CSSPropertyID::kFontVariantLigatures,
CSSPropertyID::kFontVariantCaps,
CSSPropertyID::kFontWeight,
CSSPropertyID::kLetterSpacing,
CSSPropertyID::kOrphans,
CSSPropertyID::kTextAlign,
CSSPropertyID::kTextDecorationLine,
CSSPropertyID::kTextIndent,
CSSPropertyID::kTextTransform,
CSSPropertyID::kWidows,
CSSPropertyID::kWordSpacing,
CSSPropertyID::kWebkitTextDecorationsInEffect,
CSSPropertyID::kWebkitTextFillColor,
CSSPropertyID::kWebkitTextStrokeColor,
CSSPropertyID::kWebkitTextStrokeWidth,
CSSPropertyID::kCaretColor,
CSSPropertyID::kTextWrapMode,
CSSPropertyID::kWhiteSpaceCollapse,
};
enum EditingPropertiesType {
kOnlyInheritableEditingProperties,
kAllEditingProperties
};
static const Vector<const CSSProperty*>& AllEditingProperties(
const ExecutionContext* execution_context) {
DEFINE_STATIC_LOCAL(Vector<const CSSProperty*>, properties, ());
if (properties.empty()) {
properties.ReserveInitialCapacity(std::size(kStaticEditingProperties) + 2);
CSSProperty::FilterWebExposedCSSPropertiesIntoVector(
execution_context, kStaticEditingProperties,
std::size(kStaticEditingProperties), properties);
}
return properties;
}
static const Vector<const CSSProperty*>& InheritableEditingProperties(
const ExecutionContext* execution_context) {
DEFINE_STATIC_LOCAL(Vector<const CSSProperty*>, properties, ());
if (properties.empty()) {
const Vector<const CSSProperty*>& all =
AllEditingProperties(execution_context);
properties.ReserveInitialCapacity(all.size());
for (const CSSProperty* property : all) {
if (property->IsInherited()) {
properties.push_back(property);
}
}
}
return properties;
}
template <class StyleDeclarationType>
static MutableCSSPropertyValueSet* CopyEditingProperties(
const ExecutionContext* execution_context,
StyleDeclarationType* style,
EditingPropertiesType type = kOnlyInheritableEditingProperties) {
if (type == kAllEditingProperties)
return style->CopyPropertiesInSet(AllEditingProperties(execution_context));
return style->CopyPropertiesInSet(
InheritableEditingProperties(execution_context));
}
static inline bool IsEditingProperty(ExecutionContext* execution_context,
CSSPropertyID id) {
static const Vector<const CSSProperty*>& properties =
AllEditingProperties(execution_context);
for (wtf_size_t index = 0; index < properties.size(); index++) {
if (properties[index]->IDEquals(id))
return true;
}
return false;
}
static MutableCSSPropertyValueSet* GetPropertiesNotIn(
CSSPropertyValueSet* style_with_redundant_properties,
Node*,
CSSStyleDeclaration* base_style,
SecureContextMode);
enum LegacyFontSizeMode {
kAlwaysUseLegacyFontSize,
kUseLegacyFontSizeOnlyIfPixelValuesMatch
};
static int LegacyFontSizeFromCSSValue(Document*,
const CSSValue*,
bool,
LegacyFontSizeMode);
class HTMLElementEquivalent : public GarbageCollected<HTMLElementEquivalent> {
public:
HTMLElementEquivalent(CSSPropertyID);
HTMLElementEquivalent(CSSPropertyID, const HTMLQualifiedName& tag_name);
HTMLElementEquivalent(CSSPropertyID,
CSSValueID primitive_value,
const HTMLQualifiedName& tag_name);
virtual bool Matches(const Element* element) const {
return !tag_name_ || element->HasTagName(*tag_name_);
}
virtual bool HasAttribute() const { return false; }
virtual bool PropertyExistsInStyle(const CSSPropertyValueSet* style) const {
return style->GetPropertyCSSValue(property_id_);
}
virtual bool ValueIsPresentInStyle(HTMLElement*, CSSPropertyValueSet*) const;
virtual void AddToStyle(Element*, EditingStyle*) const;
virtual void Trace(Visitor* visitor) const {
visitor->Trace(identifier_value_);
}
protected:
const CSSPropertyID property_id_;
const Member<CSSIdentifierValue> identifier_value_;
// We can store a pointer because HTML tag names are const global.
const HTMLQualifiedName* tag_name_;
};
HTMLElementEquivalent::HTMLElementEquivalent(CSSPropertyID id)
: property_id_(id), tag_name_(nullptr) {}
HTMLElementEquivalent::HTMLElementEquivalent(CSSPropertyID id,
const HTMLQualifiedName& tag_name)
: property_id_(id), tag_name_(&tag_name) {}
HTMLElementEquivalent::HTMLElementEquivalent(CSSPropertyID id,
CSSValueID value_id,
const HTMLQualifiedName& tag_name)
: property_id_(id),
identifier_value_(CSSIdentifierValue::Create(value_id)),
tag_name_(&tag_name) {
DCHECK(IsValidCSSValueID(value_id));
}
bool HTMLElementEquivalent::ValueIsPresentInStyle(
HTMLElement* element,
CSSPropertyValueSet* style) const {
const CSSValue* value = style->GetPropertyCSSValue(property_id_);
// TODO: Does this work on style or computed style? The code here, but we
// might need to do something here to match CSSPrimitiveValues. if
// (property_id_ == CSSPropertyID::kFontWeight &&
// identifier_value_->GetValueID() == CSSValueID::kBold) {
// auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value);
// if (primitive_value &&
// primitive_value->GetFloatValue() >= BoldThreshold()) {
// LOG(INFO) << "weight match in HTMLElementEquivalent for primitive
// value"; return true;
// } else {
// LOG(INFO) << "weight match in HTMLElementEquivalent for identifier
// value";
// }
// }
if (!Matches(element))
return false;
auto* identifier_value = DynamicTo<CSSIdentifierValue>(value);
return identifier_value &&
identifier_value->GetValueID() == identifier_value_->GetValueID();
}
void HTMLElementEquivalent::AddToStyle(Element* element,
EditingStyle* style) const {
style->SetProperty(property_id_, identifier_value_->CssText(),
/* important */ false,
element->GetExecutionContext()->GetSecureContextMode());
}
class HTMLTextDecorationEquivalent final : public HTMLElementEquivalent {
public:
static HTMLElementEquivalent* Create(CSSValueID primitive_value,
const HTMLQualifiedName& tag_name) {
return MakeGarbageCollected<HTMLTextDecorationEquivalent>(primitive_value,
tag_name);
}
HTMLTextDecorationEquivalent(CSSValueID primitive_value,
const HTMLQualifiedName& tag_name);
bool PropertyExistsInStyle(const CSSPropertyValueSet*) const override;
bool ValueIsPresentInStyle(HTMLElement*, CSSPropertyValueSet*) const override;
void Trace(Visitor* visitor) const override {
HTMLElementEquivalent::Trace(visitor);
}
};
HTMLTextDecorationEquivalent::HTMLTextDecorationEquivalent(
CSSValueID primitive_value,
const HTMLQualifiedName& tag_name)
: HTMLElementEquivalent(CSSPropertyID::kTextDecorationLine,
primitive_value,
tag_name)
// CSSPropertyID::kTextDecorationLine is used in
// HTMLElementEquivalent::AddToStyle
{}
bool HTMLTextDecorationEquivalent::PropertyExistsInStyle(
const CSSPropertyValueSet* style) const {
return style->GetPropertyCSSValue(
CSSPropertyID::kWebkitTextDecorationsInEffect) ||
style->GetPropertyCSSValue(CSSPropertyID::kTextDecorationLine);
}
bool HTMLTextDecorationEquivalent::ValueIsPresentInStyle(
HTMLElement* element,
CSSPropertyValueSet* style) const {
const CSSValue* style_value =
style->GetPropertyCSSValue(CSSPropertyID::kWebkitTextDecorationsInEffect);
if (!style_value) {
style_value =
style->GetPropertyCSSValue(CSSPropertyID::kTextDecorationLine);
}
if (!Matches(element))
return false;
auto* style_value_list = DynamicTo<CSSValueList>(style_value);
return style_value_list && style_value_list->HasValue(*identifier_value_);
}
class HTMLAttributeEquivalent : public HTMLElementEquivalent {
public:
HTMLAttributeEquivalent(CSSPropertyID,
const HTMLQualifiedName& tag_name,
const QualifiedName& attr_name);
HTMLAttributeEquivalent(CSSPropertyID, const QualifiedName& attr_name);
bool Matches(const Element* element) const override {
return HTMLElementEquivalent::Matches(element) &&
element->hasAttribute(attr_name_);
}
bool HasAttribute() const override { return true; }
bool ValueIsPresentInStyle(HTMLElement*, CSSPropertyValueSet*) const override;
void AddToStyle(Element*, EditingStyle*) const override;
virtual const CSSValue* AttributeValueAsCSSValue(Element*) const;
inline const QualifiedName& AttributeName() const { return attr_name_; }
void Trace(Visitor* visitor) const override {
HTMLElementEquivalent::Trace(visitor);
}
protected:
// We can store a reference because HTML attribute names are const global.
const QualifiedName& attr_name_;
};
HTMLAttributeEquivalent::HTMLAttributeEquivalent(
CSSPropertyID id,
const HTMLQualifiedName& tag_name,
const QualifiedName& attr_name)
: HTMLElementEquivalent(id, tag_name), attr_name_(attr_name) {}
HTMLAttributeEquivalent::HTMLAttributeEquivalent(CSSPropertyID id,
const QualifiedName& attr_name)
: HTMLElementEquivalent(id), attr_name_(attr_name) {}
bool HTMLAttributeEquivalent::ValueIsPresentInStyle(
HTMLElement* element,
CSSPropertyValueSet* style) const {
const CSSValue* value = AttributeValueAsCSSValue(element);
const CSSValue* style_value = style->GetPropertyCSSValue(property_id_);
return base::ValuesEquivalent(value, style_value);
}
void HTMLAttributeEquivalent::AddToStyle(Element* element,
EditingStyle* style) const {
if (const CSSValue* value = AttributeValueAsCSSValue(element)) {
style->SetProperty(property_id_, value->CssText(), /* important */ false,
element->GetExecutionContext()->GetSecureContextMode());
}
}
const CSSValue* HTMLAttributeEquivalent::AttributeValueAsCSSValue(
Element* element) const {
DCHECK(element);
const AtomicString& value = element->getAttribute(attr_name_);
if (value.IsNull())
return nullptr;
auto* dummy_style =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
dummy_style->ParseAndSetProperty(
property_id_, value, /* important */ false,
element->GetExecutionContext()->GetSecureContextMode());
return dummy_style->GetPropertyCSSValue(property_id_);
}
class HTMLFontSizeEquivalent final : public HTMLAttributeEquivalent {
public:
static HTMLFontSizeEquivalent* Create() {
return MakeGarbageCollected<HTMLFontSizeEquivalent>();
}
HTMLFontSizeEquivalent();
const CSSValue* AttributeValueAsCSSValue(Element*) const override;
void Trace(Visitor* visitor) const override {
HTMLAttributeEquivalent::Trace(visitor);
}
};
HTMLFontSizeEquivalent::HTMLFontSizeEquivalent()
: HTMLAttributeEquivalent(CSSPropertyID::kFontSize,
html_names::kFontTag,
html_names::kSizeAttr) {}
const CSSValue* HTMLFontSizeEquivalent::AttributeValueAsCSSValue(
Element* element) const {
DCHECK(element);
const AtomicString& value = element->getAttribute(attr_name_);
if (value.IsNull())
return nullptr;
std::optional<CSSValueID> size =
HTMLFontElement::CssValueFromFontSizeNumber(value);
if (!size) {
return nullptr;
}
return CSSIdentifierValue::Create(*size);
}
EditingStyle::EditingStyle(Element* element,
PropertiesToInclude properties_to_include) {
Init(element, properties_to_include);
}
EditingStyle::EditingStyle(const Position& position,
PropertiesToInclude properties_to_include) {
Init(position.AnchorNode(), properties_to_include);
}
EditingStyle::EditingStyle(const CSSPropertyValueSet* style)
: mutable_style_(style ? style->MutableCopy() : nullptr) {
ExtractFontSizeDelta();
}
EditingStyle::EditingStyle(CSSPropertyID property_id,
const String& value,
SecureContextMode secure_context_mode)
: mutable_style_(nullptr) {
SetProperty(property_id, value, /* important */ false, secure_context_mode);
is_vertical_align_ = property_id == CSSPropertyID::kVerticalAlign &&
(value == "sub" || value == "super");
}
static Color CssValueToColor(const CSSValue* value) {
if (!value)
return Color::kTransparent;
auto* color_value = DynamicTo<cssvalue::CSSColor>(value);
if (!color_value && !value->IsPrimitiveValue() && !value->IsIdentifierValue())
return Color::kTransparent;
if (color_value)
return color_value->Value();
Color color = Color::kTransparent;
// FIXME: Why ignore the return value?
CSSParser::ParseColor(color, value->CssText());
return color;
}
static inline Color GetFontColor(CSSStyleDeclaration* style) {
return CssValueToColor(
style->GetPropertyCSSValueInternal(CSSPropertyID::kColor));
}
static inline Color GetFontColor(CSSPropertyValueSet* style) {
return CssValueToColor(style->GetPropertyCSSValue(CSSPropertyID::kColor));
}
static inline Color GetBackgroundColor(CSSStyleDeclaration* style) {
return CssValueToColor(
style->GetPropertyCSSValueInternal(CSSPropertyID::kBackgroundColor));
}
static inline Color GetBackgroundColor(CSSPropertyValueSet* style) {
return CssValueToColor(
style->GetPropertyCSSValue(CSSPropertyID::kBackgroundColor));
}
static inline Color BackgroundColorInEffect(Node* node) {
return CssValueToColor(
EditingStyleUtilities::BackgroundColorValueInEffect(node));
}
static CSSValueID NormalizeTextAlign(CSSValueID text_align) {
switch (text_align) {
case CSSValueID::kCenter:
case CSSValueID::kWebkitCenter:
return CSSValueID::kCenter;
case CSSValueID::kJustify:
return CSSValueID::kJustify;
case CSSValueID::kLeft:
case CSSValueID::kWebkitLeft:
return CSSValueID::kLeft;
case CSSValueID::kRight:
case CSSValueID::kWebkitRight:
return CSSValueID::kRight;
case CSSValueID::kStart:
case CSSValueID::kEnd:
return text_align;
default:
return CSSValueID::kInvalid;
}
}
static CSSValueID TextAlignResolvingStartAndEnd(CSSValueID text_align,
TextDirection direction) {
const CSSValueID normalized = NormalizeTextAlign(text_align);
switch (normalized) {
case CSSValueID::kStart:
return IsLtr(direction) ? CSSValueID::kLeft : CSSValueID::kRight;
case CSSValueID::kEnd:
return IsLtr(direction) ? CSSValueID::kRight : CSSValueID::kLeft;
default:
return normalized;
}
}
// Returns true "text-align" property of |style| is redundant when applying
// |style| inheriting from |base_style| to |node|.
// Note: direction for "text-align:start" and "text-align:end" are taken
// from |node|.
template <typename T>
static bool IsRedundantTextAlign(MutableCSSPropertyValueSet* style,
T* base_style,
Node* node) {
DCHECK(node);
const CSSValueID base_text_align = NormalizeTextAlign(
GetIdentifierValue(base_style, CSSPropertyID::kTextAlign));
if (base_text_align == CSSValueID::kInvalid)
return false;
const CSSValueID text_align =
NormalizeTextAlign(GetIdentifierValue(style, CSSPropertyID::kTextAlign));
if (text_align == CSSValueID::kInvalid)
return false;
if (text_align == base_text_align)
return true;
const ComputedStyle* node_style =
GetComputedStyleForElementOrLayoutObject(*node);
if (!node_style) {
return true;
}
TextDirection node_direction = node_style->Direction();
if (base_text_align == CSSValueID::kStart ||
base_text_align == CSSValueID::kEnd) {
// Returns true for "text-align:left" of <p>
// <div style="text-align:start"><p dir="ltr" style="text-align:left">
// because meaning of "text-align:start" in <p> is identical to
// "text-align:left".
//
// Returns false for "text-align:left" of <p>
// <div style="text-align:start"><p dir="rtl" style="text-align:left">
// because meaning of "text-align:start" in <p> is identical to
// "text-align:right".
return TextAlignResolvingStartAndEnd(base_text_align, node_direction) ==
text_align;
}
if (text_align == CSSValueID::kStart || text_align == CSSValueID::kEnd) {
// Returns true for "text-align:start" of <p>
// <div style="text-align:left"><p dir="ltr" style="text-align:start">
// <div style="text-align:right"><p dir="rtl" style="text-align:start">
// Returns false for "text-align:start" of <p>
// <div style="text-align:left"><p dir="rtl" style="text-align:start">
// <div style="text-align:right"><p dir="ltr" style="text-align:start">
return TextAlignResolvingStartAndEnd(text_align, node_direction) ==
base_text_align;
}
return false;
}
namespace {
Element* ElementFromStyledNode(Node* node) {
if (Element* element = DynamicTo<Element>(node)) {
return element;
}
if (node) {
// This should probably be FlatTreeTraversal::ParentElement() instead, but
// it breaks tests.
return node->ParentOrShadowHostElement();
}
return nullptr;
}
} // namespace
void EditingStyle::Init(Node* node, PropertiesToInclude properties_to_include) {
if (IsTabHTMLSpanElementTextNode(node))
node = TabSpanElement(node)->parentNode();
else if (IsTabHTMLSpanElement(node))
node = node->parentNode();
node_ = node;
auto* computed_style_at_position =
MakeGarbageCollected<CSSComputedStyleDeclaration>(
ElementFromStyledNode(node));
mutable_style_ =
properties_to_include == kAllProperties && computed_style_at_position
? computed_style_at_position->CopyProperties()
: CopyEditingProperties(node ? node->GetExecutionContext() : nullptr,
computed_style_at_position);
if (properties_to_include == kEditingPropertiesInEffect) {
if (const CSSValue* value =
EditingStyleUtilities::BackgroundColorValueInEffect(node)) {
mutable_style_->ParseAndSetProperty(
CSSPropertyID::kBackgroundColor, value->CssText(),
/* important */ false,
node->GetExecutionContext()->GetSecureContextMode());
}
if (const CSSValue* value = computed_style_at_position->GetPropertyCSSValue(
CSSPropertyID::kWebkitTextDecorationsInEffect)) {
mutable_style_->ParseAndSetProperty(
CSSPropertyID::kTextDecoration, value->CssText(),
/* important */ false,
node->GetExecutionContext()->GetSecureContextMode());
}
}
const ComputedStyle* computed_style =
node ? GetComputedStyleForElementOrLayoutObject(*node) : nullptr;
if (computed_style) {
// Fix for crbug.com/768261: due to text-autosizing, reading the current
// computed font size and re-writing it to an element may actually cause the
// font size to become larger (since the autosizer will run again on the new
// computed size). The fix is to toss out the computed size property here
// and use ComputedStyle::SpecifiedFontSize().
if (computed_style->ComputedFontSize() !=
computed_style->SpecifiedFontSize()) {
// ReplaceSelectionCommandTest_TextAutosizingDoesntInflateText gets here.
mutable_style_->ParseAndSetProperty(
CSSPropertyID::kFontSize,
CSSNumericLiteralValue::Create(computed_style->SpecifiedFontSize(),
CSSPrimitiveValue::UnitType::kPixels)
->CssText(),
/* important */ false,
node->GetExecutionContext()->GetSecureContextMode());
}
RemoveForcedColorsIfNeeded(computed_style);
RemoveInheritedColorsIfNeeded(computed_style);
ReplaceFontSizeByKeywordIfPossible(
computed_style, node->GetExecutionContext()->GetSecureContextMode(),
computed_style_at_position);
}
is_monospace_font_ = computed_style_at_position->IsMonospaceFont();
ExtractFontSizeDelta();
}
void EditingStyle::RemoveForcedColorsIfNeeded(
const ComputedStyle* computed_style) {
if (!computed_style->InForcedColorsMode()) {
return;
}
mutable_style_->RemoveProperty(CSSPropertyID::kColor);
mutable_style_->RemoveProperty(CSSPropertyID::kBackgroundColor);
mutable_style_->RemoveProperty(CSSPropertyID::kTextDecorationColor);
}
void EditingStyle::RemoveInheritedColorsIfNeeded(
const ComputedStyle* computed_style) {
// If a node's text fill color is currentColor, then its children use
// their font-color as their text fill color (they don't
// inherit it). Likewise for stroke color.
// Similar thing happens for caret-color if it's auto or currentColor.
if (computed_style->TextFillColor().IsCurrentColor())
mutable_style_->RemoveProperty(CSSPropertyID::kWebkitTextFillColor);
if (computed_style->TextStrokeColor().IsCurrentColor())
mutable_style_->RemoveProperty(CSSPropertyID::kWebkitTextStrokeColor);
if (computed_style->CaretColor().IsAutoColor() ||
computed_style->CaretColor().IsCurrentColor())
mutable_style_->RemoveProperty(CSSPropertyID::kCaretColor);
}
CSSValueID EditingStyle::GetProperty(CSSPropertyID property_id) const {
return GetIdentifierValue(mutable_style_.Get(), property_id);
}
void EditingStyle::SetProperty(CSSPropertyID property_id,
const String& value,
bool important,
SecureContextMode secure_context_mode) {
if (!mutable_style_) {
mutable_style_ =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
}
mutable_style_->ParseAndSetProperty(property_id, value, important,
secure_context_mode);
}
void EditingStyle::ReplaceFontSizeByKeywordIfPossible(
const ComputedStyle* computed_style,
SecureContextMode secure_context_mode,
CSSComputedStyleDeclaration* css_computed_style) {
DCHECK(computed_style);
if (computed_style->GetFontDescription().KeywordSize()) {
if (const CSSValue* keyword =
css_computed_style->GetFontSizeCSSValuePreferringKeyword()) {
mutable_style_->ParseAndSetProperty(
CSSPropertyID::kFontSize, keyword->CssText(),
/* important */ false, secure_context_mode);
}
}
}
void EditingStyle::ExtractFontSizeDelta() {
if (!mutable_style_)
return;
if (mutable_style_->GetPropertyCSSValue(CSSPropertyID::kFontSize)) {
// Explicit font size overrides any delta.
mutable_style_->RemoveProperty(CSSPropertyID::kInternalFontSizeDelta);
return;
}
// Get the adjustment amount out of the style.
const CSSValue* value = mutable_style_->GetPropertyCSSValue(
CSSPropertyID::kInternalFontSizeDelta);
const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value);
// Only PX handled now. If we handle more types in the future, perhaps
// a switch statement here would be more appropriate.
if (!primitive_value || !primitive_value->IsPx()) {
return;
}
std::optional<double> font_size_delta = primitive_value->GetValueIfKnown();
if (font_size_delta.has_value()) {
font_size_delta_ = ClampTo<float>(font_size_delta.value());
mutable_style_->RemoveProperty(CSSPropertyID::kInternalFontSizeDelta);
}
}
bool EditingStyle::IsEmpty() const {
return (!mutable_style_ || mutable_style_->IsEmpty()) &&
font_size_delta_ == kNoFontDelta;
}
bool EditingStyle::GetTextDirection(
mojo_base::mojom::blink::TextDirection& writing_direction) const {
if (!mutable_style_)
return false;
const CSSValue* unicode_bidi =
mutable_style_->GetPropertyCSSValue(CSSPropertyID::kUnicodeBidi);
auto* unicode_bidi_identifier_value =
DynamicTo<CSSIdentifierValue>(unicode_bidi);
if (!unicode_bidi_identifier_value)
return false;
CSSValueID unicode_bidi_value = unicode_bidi_identifier_value->GetValueID();
if (EditingStyleUtilities::IsEmbedOrIsolate(unicode_bidi_value)) {
const CSSValue* direction =
mutable_style_->GetPropertyCSSValue(CSSPropertyID::kDirection);
auto* direction_identifier_value = DynamicTo<CSSIdentifierValue>(direction);
if (!direction_identifier_value)
return false;
writing_direction =
direction_identifier_value->GetValueID() == CSSValueID::kLtr
? mojo_base::mojom::blink::TextDirection::LEFT_TO_RIGHT
: mojo_base::mojom::blink::TextDirection::RIGHT_TO_LEFT;
return true;
}
if (unicode_bidi_value == CSSValueID::kNormal) {
writing_direction =
mojo_base::mojom::blink::TextDirection::UNKNOWN_DIRECTION;
return true;
}
return false;
}
void EditingStyle::OverrideWithStyle(const CSSPropertyValueSet* style) {
if (!style || style->IsEmpty())
return;
if (!mutable_style_) {
mutable_style_ =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
}
mutable_style_->MergeAndOverrideOnConflict(style);
ExtractFontSizeDelta();
}
void EditingStyle::Clear() {
mutable_style_.Clear();
is_monospace_font_ = false;
font_size_delta_ = kNoFontDelta;
}
EditingStyle* EditingStyle::Copy() const {
EditingStyle* copy = MakeGarbageCollected<EditingStyle>();
if (mutable_style_)
copy->mutable_style_ = mutable_style_->MutableCopy();
copy->is_monospace_font_ = is_monospace_font_;
copy->font_size_delta_ = font_size_delta_;
return copy;
}
// This is the list of CSS properties that apply specially to block-level
// elements.
static const CSSPropertyID kStaticBlockProperties[] = {
CSSPropertyID::kBreakAfter, CSSPropertyID::kBreakBefore,
CSSPropertyID::kBreakInside, CSSPropertyID::kOrphans,
CSSPropertyID::kOverflow, // This can be also be applied to replaced
// elements
CSSPropertyID::kColumnCount, CSSPropertyID::kColumnGap,
CSSPropertyID::kColumnRuleColor, CSSPropertyID::kColumnRuleStyle,
CSSPropertyID::kColumnRuleWidth, CSSPropertyID::kRowRuleWidth,
CSSPropertyID::kWebkitColumnBreakBefore,
CSSPropertyID::kWebkitColumnBreakAfter,
CSSPropertyID::kWebkitColumnBreakInside, CSSPropertyID::kColumnWidth,
CSSPropertyID::kPageBreakAfter, CSSPropertyID::kPageBreakBefore,
CSSPropertyID::kPageBreakInside, CSSPropertyID::kTextAlign,
CSSPropertyID::kTextAlignLast, CSSPropertyID::kTextIndent,
CSSPropertyID::kWidows};
static const Vector<const CSSProperty*>& BlockPropertiesVector(
const ExecutionContext* execution_context) {
DEFINE_STATIC_LOCAL(Vector<const CSSProperty*>, properties, ());
if (properties.empty()) {
CSSProperty::FilterWebExposedCSSPropertiesIntoVector(
execution_context, kStaticBlockProperties,
std::size(kStaticBlockProperties), properties);
}
return properties;
}
EditingStyle* EditingStyle::ExtractAndRemoveBlockProperties(
const ExecutionContext* execution_context) {
EditingStyle* block_properties = MakeGarbageCollected<EditingStyle>();
if (!mutable_style_)
return block_properties;
block_properties->mutable_style_ = mutable_style_->CopyPropertiesInSet(
BlockPropertiesVector(execution_context));
RemoveBlockProperties(execution_context);
return block_properties;
}
EditingStyle* EditingStyle::ExtractAndRemoveTextDirection(
SecureContextMode secure_context_mode) {
EditingStyle* text_direction = MakeGarbageCollected<EditingStyle>();
text_direction->mutable_style_ =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
text_direction->mutable_style_->SetLonghandProperty(
CSSPropertyID::kUnicodeBidi, CSSValueID::kIsolate,
mutable_style_->PropertyIsImportant(CSSPropertyID::kUnicodeBidi));
text_direction->mutable_style_->ParseAndSetProperty(
CSSPropertyID::kDirection,
mutable_style_->GetPropertyValue(CSSPropertyID::kDirection),
mutable_style_->PropertyIsImportant(CSSPropertyID::kDirection),
secure_context_mode);
mutable_style_->RemoveProperty(CSSPropertyID::kUnicodeBidi);
mutable_style_->RemoveProperty(CSSPropertyID::kDirection);
return text_direction;
}
void EditingStyle::RemoveBlockProperties(
const ExecutionContext* execution_context) {
if (!mutable_style_)
return;
mutable_style_->RemovePropertiesInSet(
BlockPropertiesVector(execution_context));
}
void EditingStyle::RemoveStyleAddedByElement(Element* element) {
if (!element || !element->parentElement()) {
return;
}
MutableCSSPropertyValueSet* parent_style =
CopyEditingProperties(element->parentElement()->GetExecutionContext(),
MakeGarbageCollected<CSSComputedStyleDeclaration>(
element->parentElement()),
kAllEditingProperties);
MutableCSSPropertyValueSet* element_style = CopyEditingProperties(
element->GetExecutionContext(),
MakeGarbageCollected<CSSComputedStyleDeclaration>(element),
kAllEditingProperties);
element_style->RemoveEquivalentProperties(parent_style);
mutable_style_->RemoveEquivalentProperties(element_style);
}
void EditingStyle::RemoveStyleConflictingWithStyleOfElement(Element* element) {
if (!element || !element->parentElement() || !mutable_style_) {
return;
}
MutableCSSPropertyValueSet* parent_style =
CopyEditingProperties(element->parentElement()->GetExecutionContext(),
MakeGarbageCollected<CSSComputedStyleDeclaration>(
element->parentElement()),
kAllEditingProperties);
MutableCSSPropertyValueSet* element_style = CopyEditingProperties(
element->GetExecutionContext(),
MakeGarbageCollected<CSSComputedStyleDeclaration>(element),
kAllEditingProperties);
element_style->RemoveEquivalentProperties(parent_style);
for (const CSSPropertyValue& property : element_style->Properties()) {
mutable_style_->RemoveProperty(property.PropertyID());
}
}
void EditingStyle::CollapseTextDecorationProperties(
SecureContextMode secure_context_mode) {
if (!mutable_style_)
return;
const CSSValue* text_decorations_in_effect =
mutable_style_->GetPropertyCSSValue(
CSSPropertyID::kWebkitTextDecorationsInEffect);
if (!text_decorations_in_effect)
return;
if (text_decorations_in_effect->IsValueList()) {
mutable_style_->ParseAndSetProperty(
CSSPropertyID::kTextDecorationLine,
text_decorations_in_effect->CssText(),
mutable_style_->PropertyIsImportant(CSSPropertyID::kTextDecorationLine),
secure_context_mode);
} else {
mutable_style_->RemoveProperty(CSSPropertyID::kTextDecorationLine);
}
mutable_style_->RemoveProperty(CSSPropertyID::kWebkitTextDecorationsInEffect);
}
EditingTriState EditingStyle::TriStateOfStyle(
ExecutionContext* execution_context,
EditingStyle* style,
SecureContextMode secure_context_mode) const {
if (!style || !style->mutable_style_)
return EditingTriState::kFalse;
DCHECK(style->node_);
return TriStateOfStyle(
style->mutable_style_->EnsureCSSStyleDeclaration(execution_context),
style->node_, kDoNotIgnoreTextOnlyProperties, secure_context_mode);
}
EditingTriState EditingStyle::TriStateOfStyle(
CSSStyleDeclaration* style_to_compare,
Node* node,
ShouldIgnoreTextOnlyProperties should_ignore_text_only_properties,
SecureContextMode secure_context_mode) const {
// editing/execCommand/query-text-alignment.html requires |node|.
DCHECK(node);
MutableCSSPropertyValueSet* difference = GetPropertiesNotIn(
mutable_style_.Get(), node, style_to_compare, secure_context_mode);
// CSS properties that create a visual difference only when applied to text.
static const CSSProperty* kTextOnlyProperties[] = {
// FIXME: CSSPropertyID::kTextDecoration needs to be removed when CSS3
// Text
// Decoration feature is no longer experimental.
&GetCSSPropertyTextDecoration(),
&GetCSSPropertyTextDecorationLine(),
&GetCSSPropertyWebkitTextDecorationsInEffect(),
&GetCSSPropertyFontStyle(),
&GetCSSPropertyFontWeight(),
&GetCSSPropertyColor(),
};
if (should_ignore_text_only_properties == kIgnoreTextOnlyProperties) {
difference->RemovePropertiesInSet(kTextOnlyProperties);
}
if (difference->IsEmpty())
return EditingTriState::kTrue;
if (difference->PropertyCount() == mutable_style_->PropertyCount())
return EditingTriState::kFalse;
return EditingTriState::kMixed;
}
EditingTriState EditingStyle::TriStateOfStyle(
const VisibleSelection& selection,
SecureContextMode secure_context_mode) const {
if (selection.IsNone())
return EditingTriState::kFalse;
if (selection.IsCaret()) {
return TriStateOfStyle(
selection.Start().AnchorNode()->GetExecutionContext(),
EditingStyleUtilities::CreateStyleAtSelectionStart(selection),
secure_context_mode);
}
EditingTriState state = EditingTriState::kFalse;
bool node_is_start = true;
for (Node& node : NodeTraversal::StartsAt(*selection.Start().AnchorNode())) {
if (node.GetLayoutObject() && IsEditable(node)) {
auto* computed_style = MakeGarbageCollected<CSSComputedStyleDeclaration>(
ElementFromStyledNode(&node));
CSSStyleDeclaration* node_style = computed_style;
if (computed_style) {
// If the selected element has <sub> or <sup> ancestor element, apply
// the corresponding style(vertical-align) to it so that
// document.queryCommandState() works with the style. See bug
// http://crbug.com/582225.
if (is_vertical_align_ &&
GetIdentifierValue(computed_style, CSSPropertyID::kVerticalAlign) ==
CSSValueID::kBaseline) {
const auto* vertical_align =
To<CSSIdentifierValue>(mutable_style_->GetPropertyCSSValue(
CSSPropertyID::kVerticalAlign));
if (EditingStyleUtilities::HasAncestorVerticalAlignStyle(
node, vertical_align->GetValueID())) {
auto* mutable_style = computed_style->CopyProperties();
mutable_style->SetProperty(CSSPropertyID::kVerticalAlign,
*vertical_align);
node_style = mutable_style->EnsureCSSStyleDeclaration(
node.GetExecutionContext());
}
}
// Pass EditingStyle::DoNotIgnoreTextOnlyProperties without checking if
// node.isTextNode() because the node can be an element node. See bug
// http://crbug.com/584939.
EditingTriState node_state = TriStateOfStyle(
node_style, &node, EditingStyle::kDoNotIgnoreTextOnlyProperties,
secure_context_mode);
if (node_is_start) {
state = node_state;
node_is_start = false;
} else if (state != node_state && node.IsTextNode()) {
state = EditingTriState::kMixed;
break;
}
}
}
if (&node == selection.End().AnchorNode())
break;
}
return state;
}
bool EditingStyle::ConflictsWithInlineStyleOfElement(
HTMLElement* element,
EditingStyle* extracted_style,
Vector<CSSPropertyID>* conflicting_properties) const {
DCHECK(element);
DCHECK(!conflicting_properties || conflicting_properties->empty());
const CSSPropertyValueSet* inline_style = element->InlineStyle();
if (!mutable_style_ || !inline_style)
return false;
for (const CSSPropertyValue& property : mutable_style_->Properties()) {
CSSPropertyID property_id = property.PropertyID();
// We don't override `white-space-collapse` property of a tab span because
// that would collapse the tab into a space.
//
// Logically speaking, only `white-space-collapse` is needed (i.e.,
// `text-wrap` is not needed.) But including other longhands helps producing
// `white-space` instead of `white-space-collapse`. Because the snippet
// produced by this logic may be sent to other browsers by copy&paste,
// e-mail, etc., `white-space` is more interoperable when
// `white-space-collapse` is not broadly supported. See crbug.com/1417543
// and `editing/pasteboard/pasting-tabs.html`.
#if EXPENSIVE_DCHECKS_ARE_ON()
DCHECK_NE(property_id, CSSPropertyID::kWhiteSpace);
DCHECK_EQ(whiteSpaceShorthand().length(), 2u);
DCHECK_EQ(whiteSpaceShorthand().properties()[0]->PropertyID(),
CSSPropertyID::kWhiteSpaceCollapse);
DCHECK_EQ(whiteSpaceShorthand().properties()[1]->PropertyID(),
CSSPropertyID::kTextWrapMode);
#endif // EXPENSIVE_DCHECKS_ARE_ON()
const bool is_whitespace_property =
property_id == CSSPropertyID::kWhiteSpaceCollapse ||
property_id == CSSPropertyID::kTextWrapMode;
if (is_whitespace_property && IsTabHTMLSpanElement(element)) {
continue;
}
if (property_id == CSSPropertyID::kWebkitTextDecorationsInEffect &&
inline_style->GetPropertyCSSValue(CSSPropertyID::kTextDecorationLine)) {
if (!conflicting_properties)
return true;
conflicting_properties->push_back(CSSPropertyID::kTextDecoration);
// Because text-decoration expands to text-decoration-line,
// we also state it as conflicting.
conflicting_properties->push_back(CSSPropertyID::kTextDecorationLine);
if (extracted_style) {
extracted_style->SetProperty(
CSSPropertyID::kTextDecorationLine,
inline_style->GetPropertyValue(CSSPropertyID::kTextDecorationLine),
inline_style->PropertyIsImportant(
CSSPropertyID::kTextDecorationLine),
element->GetExecutionContext()->GetSecureContextMode());
}
continue;
}
if (!inline_style->GetPropertyCSSValue(property_id))
continue;
if (property_id == CSSPropertyID::kUnicodeBidi &&
inline_style->GetPropertyCSSValue(CSSPropertyID::kDirection)) {
if (!conflicting_properties)
return true;
conflicting_properties->push_back(CSSPropertyID::kDirection);
if (extracted_style) {
extracted_style->SetProperty(
property_id, inline_style->GetPropertyValue(property_id),
inline_style->PropertyIsImportant(property_id),
element->GetExecutionContext()->GetSecureContextMode());
}
}
if (!conflicting_properties)
return true;
conflicting_properties->push_back(property_id);
if (extracted_style) {
extracted_style->SetProperty(
property_id, inline_style->GetPropertyValue(property_id),
inline_style->PropertyIsImportant(property_id),
element->GetExecutionContext()->GetSecureContextMode());
}
}
return conflicting_properties && !conflicting_properties->empty();
}
static const HeapVector<Member<HTMLElementEquivalent>>&
HtmlElementEquivalents() {
using Holder = DisallowNewWrapper<HeapVector<Member<HTMLElementEquivalent>>>;
DEFINE_STATIC_LOCAL(Persistent<Holder>, html_element_equivalents_holder,
(MakeGarbageCollected<Holder>()));
HeapVector<Member<HTMLElementEquivalent>>* html_element_equivalents =
&html_element_equivalents_holder->Value();
if (!html_element_equivalents->size()) {
html_element_equivalents->push_back(
MakeGarbageCollected<HTMLElementEquivalent>(
CSSPropertyID::kFontWeight, CSSValueID::kBold, html_names::kBTag));
html_element_equivalents->push_back(
MakeGarbageCollected<HTMLElementEquivalent>(CSSPropertyID::kFontWeight,
CSSValueID::kBold,
html_names::kStrongTag));
html_element_equivalents->push_back(
MakeGarbageCollected<HTMLElementEquivalent>(
CSSPropertyID::kVerticalAlign, CSSValueID::kSub,
html_names::kSubTag));
html_element_equivalents->push_back(
MakeGarbageCollected<HTMLElementEquivalent>(
CSSPropertyID::kVerticalAlign, CSSValueID::kSuper,
html_names::kSupTag));
html_element_equivalents->push_back(
MakeGarbageCollected<HTMLElementEquivalent>(
CSSPropertyID::kFontStyle, CSSValueID::kItalic, html_names::kITag));
html_element_equivalents->push_back(
MakeGarbageCollected<HTMLElementEquivalent>(CSSPropertyID::kFontStyle,
CSSValueID::kItalic,
html_names::kEmTag));
html_element_equivalents->push_back(HTMLTextDecorationEquivalent::Create(
CSSValueID::kUnderline, html_names::kUTag));
html_element_equivalents->push_back(HTMLTextDecorationEquivalent::Create(
CSSValueID::kLineThrough, html_names::kSTag));
html_element_equivalents->push_back(HTMLTextDecorationEquivalent::Create(
CSSValueID::kLineThrough, html_names::kStrikeTag));
}
return *html_element_equivalents;
}
bool EditingStyle::ConflictsWithImplicitStyleOfElement(
HTMLElement* element,
EditingStyle* extracted_style,
ShouldExtractMatchingStyle should_extract_matching_style) const {
if (!mutable_style_)
return false;
const HeapVector<Member<HTMLElementEquivalent>>& html_element_equivalents =
HtmlElementEquivalents();
for (wtf_size_t i = 0; i < html_element_equivalents.size(); ++i) {
const HTMLElementEquivalent* equivalent = html_element_equivalents[i].Get();
if (equivalent->Matches(element) &&
equivalent->PropertyExistsInStyle(mutable_style_.Get()) &&
(should_extract_matching_style == kExtractMatchingStyle ||
!equivalent->ValueIsPresentInStyle(element, mutable_style_.Get()))) {
if (extracted_style)
equivalent->AddToStyle(element, extracted_style);
return true;
}
}
return false;
}
static const HeapVector<Member<HTMLAttributeEquivalent>>&
HtmlAttributeEquivalents() {
using Holder =
DisallowNewWrapper<HeapVector<Member<HTMLAttributeEquivalent>>>;
DEFINE_STATIC_LOCAL(Persistent<Holder>, html_attribute_equivalents_holder,
(MakeGarbageCollected<Holder>()));
HeapVector<Member<HTMLAttributeEquivalent>>* html_attribute_equivalents =
&html_attribute_equivalents_holder->Value();
if (!html_attribute_equivalents->size()) {
// elementIsStyledSpanOrHTMLEquivalent depends on the fact each
// HTMLAttriuteEquivalent matches exactly one attribute of exactly one
// element except dirAttr.
html_attribute_equivalents->push_back(
MakeGarbageCollected<HTMLAttributeEquivalent>(CSSPropertyID::kColor,
html_names::kFontTag,
html_names::kColorAttr));
html_attribute_equivalents->push_back(
MakeGarbageCollected<HTMLAttributeEquivalent>(
CSSPropertyID::kFontFamily, html_names::kFontTag,
html_names::kFaceAttr));
html_attribute_equivalents->push_back(HTMLFontSizeEquivalent::Create());
html_attribute_equivalents->push_back(
MakeGarbageCollected<HTMLAttributeEquivalent>(CSSPropertyID::kDirection,
html_names::kDirAttr));
html_attribute_equivalents->push_back(
MakeGarbageCollected<HTMLAttributeEquivalent>(
CSSPropertyID::kUnicodeBidi, html_names::kDirAttr));
}
return *html_attribute_equivalents;
}
bool EditingStyle::ConflictsWithImplicitStyleOfAttributes(
HTMLElement* element) const {
DCHECK(element);
if (!mutable_style_)
return false;
const HeapVector<Member<HTMLAttributeEquivalent>>&
html_attribute_equivalents = HtmlAttributeEquivalents();
for (const auto& equivalent : html_attribute_equivalents) {
if (equivalent->Matches(element) &&
equivalent->PropertyExistsInStyle(mutable_style_.Get()) &&
!equivalent->ValueIsPresentInStyle(element, mutable_style_.Get()))
return true;
}
return false;
}
bool EditingStyle::ExtractConflictingImplicitStyleOfAttributes(
HTMLElement* element,
ShouldPreserveWritingDirection should_preserve_writing_direction,
EditingStyle* extracted_style,
Vector<QualifiedName>& conflicting_attributes,
ShouldExtractMatchingStyle should_extract_matching_style) const {
DCHECK(element);
// HTMLAttributeEquivalent::addToStyle doesn't support unicode-bidi and
// direction properties
if (extracted_style)
DCHECK_EQ(should_preserve_writing_direction, kPreserveWritingDirection);
if (!mutable_style_)
return false;
const HeapVector<Member<HTMLAttributeEquivalent>>&
html_attribute_equivalents = HtmlAttributeEquivalents();
bool removed = false;
for (const auto& attribute : html_attribute_equivalents) {
const HTMLAttributeEquivalent* equivalent = attribute.Get();
// unicode-bidi and direction are pushed down separately so don't push down
// with other styles.
if (should_preserve_writing_direction == kPreserveWritingDirection &&
equivalent->AttributeName() == html_names::kDirAttr)
continue;
if (!equivalent->Matches(element) ||
!equivalent->PropertyExistsInStyle(mutable_style_.Get()) ||
(should_extract_matching_style == kDoNotExtractMatchingStyle &&
equivalent->ValueIsPresentInStyle(element, mutable_style_.Get())))
continue;
if (extracted_style)
equivalent->AddToStyle(element, extracted_style);
conflicting_attributes.push_back(equivalent->AttributeName());
removed = true;
}
return removed;
}
bool EditingStyle::StyleIsPresentInComputedStyleOfNode(Node* node) const {
return !mutable_style_ ||
GetPropertiesNotIn(mutable_style_.Get(), node,
MakeGarbageCollected<CSSComputedStyleDeclaration>(
ElementFromStyledNode(node)),
node->GetExecutionContext()->GetSecureContextMode())
->IsEmpty();
}
bool EditingStyle::ElementIsStyledSpanOrHTMLEquivalent(
const HTMLElement* element) {
DCHECK(element);
bool element_is_span_or_element_equivalent = false;
if (IsA<HTMLSpanElement>(*element)) {
element_is_span_or_element_equivalent = true;
} else {
const HeapVector<Member<HTMLElementEquivalent>>& html_element_equivalents =
HtmlElementEquivalents();
wtf_size_t i;
for (i = 0; i < html_element_equivalents.size(); ++i) {
if (html_element_equivalents[i]->Matches(element)) {
element_is_span_or_element_equivalent = true;
break;
}
}
}
AttributeCollection attributes = element->Attributes();
if (attributes.IsEmpty()) {
// span, b, etc... without any attributes
return element_is_span_or_element_equivalent;
}
unsigned matched_attributes = 0;
const HeapVector<Member<HTMLAttributeEquivalent>>&
html_attribute_equivalents = HtmlAttributeEquivalents();
for (const auto& equivalent : html_attribute_equivalents) {
if (equivalent->Matches(element) &&
equivalent->AttributeName() != html_names::kDirAttr)
matched_attributes++;
}
if (!element_is_span_or_element_equivalent && !matched_attributes) {
// element is not a span, a html element equivalent, or font element.
return false;
}
if (element->hasAttribute(html_names::kStyleAttr)) {
if (const CSSPropertyValueSet* style = element->InlineStyle()) {
for (const CSSPropertyValue& property : style->Properties()) {
if (!IsEditingProperty(element->GetExecutionContext(),
property.PropertyID())) {
return false;
}
}
}
matched_attributes++;
}
// font with color attribute, span with style attribute, etc...
DCHECK_LE(matched_attributes, attributes.size());
return matched_attributes >= attributes.size();
}
void EditingStyle::PrepareToApplyAt(
const Position& position,
ShouldPreserveWritingDirection should_preserve_writing_direction) {
if (!mutable_style_)
return;
DCHECK(position.IsNotNull());
// ReplaceSelectionCommand::handleStyleSpans() requires that this function
// only removes the editing style. If this function was modified in the future
// to delete all redundant properties, then add a boolean value to indicate
// which one of editingStyleAtPosition or computedStyle is called.
EditingStyle* editing_style_at_position =
MakeGarbageCollected<EditingStyle>(position, kEditingPropertiesInEffect);
CSSPropertyValueSet* style_at_position =
editing_style_at_position->mutable_style_.Get();
const CSSValue* unicode_bidi = nullptr;
const CSSValue* direction = nullptr;
if (should_preserve_writing_direction == kPreserveWritingDirection) {
unicode_bidi =
mutable_style_->GetPropertyCSSValue(CSSPropertyID::kUnicodeBidi);
direction = mutable_style_->GetPropertyCSSValue(CSSPropertyID::kDirection);
}
mutable_style_->RemoveEquivalentProperties(style_at_position);
DCHECK(editing_style_at_position->node_);
if (IsRedundantTextAlign(mutable_style_.Get(), style_at_position,
editing_style_at_position->node_))
mutable_style_->RemoveProperty(CSSPropertyID::kTextAlign);
if (GetFontColor(mutable_style_.Get()) == GetFontColor(style_at_position))
mutable_style_->RemoveProperty(CSSPropertyID::kColor);
if (EditingStyleUtilities::HasTransparentBackgroundColor(
mutable_style_.Get()) ||
CssValueToColor(mutable_style_->GetPropertyCSSValue(
CSSPropertyID::kBackgroundColor)) ==
BackgroundColorInEffect(position.ComputeContainerNode()))
mutable_style_->RemoveProperty(CSSPropertyID::kBackgroundColor);
if (auto* unicode_bidi_identifier_value =
DynamicTo<CSSIdentifierValue>(unicode_bidi)) {
mutable_style_->SetLonghandProperty(
CSSPropertyID::kUnicodeBidi,
unicode_bidi_identifier_value->GetValueID());
if (auto* direction_identifier_value =
DynamicTo<CSSIdentifierValue>(direction)) {
mutable_style_->SetLonghandProperty(
CSSPropertyID::kDirection, direction_identifier_value->GetValueID());
}
}
}
void EditingStyle::MergeTypingStyle(Document* document) {
DCHECK(document);
EditingStyle* typing_style = document->GetFrame()->GetEditor().TypingStyle();
if (!typing_style || typing_style == this)
return;
MergeStyle(typing_style->Style(), kOverrideValues);
}
void EditingStyle::MergeInlineStyleOfElement(
HTMLElement* element,
CSSPropertyOverrideMode mode,
PropertiesToInclude properties_to_include) {
DCHECK(element);
if (!element->InlineStyle())
return;
switch (properties_to_include) {
case kAllProperties:
MergeStyle(element->InlineStyle(), mode);
return;
case kOnlyEditingInheritableProperties:
MergeStyle(CopyEditingProperties(element->GetExecutionContext(),
element->InlineStyle(),
kOnlyInheritableEditingProperties),
mode);
return;
case kEditingPropertiesInEffect:
MergeStyle(
CopyEditingProperties(element->GetExecutionContext(),
element->InlineStyle(), kAllEditingProperties),
mode);
return;
}
}
static inline bool ElementMatchesAndPropertyIsNotInInlineStyleDecl(
const HTMLElementEquivalent* equivalent,
const Element* element,
EditingStyle::CSSPropertyOverrideMode mode,
CSSPropertyValueSet* style) {
return equivalent->Matches(element) &&
(!element->InlineStyle() ||
!equivalent->PropertyExistsInStyle(element->InlineStyle())) &&
(mode == EditingStyle::kOverrideValues ||
!equivalent->PropertyExistsInStyle(style));
}
static MutableCSSPropertyValueSet* ExtractEditingProperties(
const ExecutionContext* execution_context,
const CSSPropertyValueSet* style,
EditingStyle::PropertiesToInclude properties_to_include) {
if (!style)
return nullptr;
switch (properties_to_include) {
case EditingStyle::kAllProperties:
case EditingStyle::kEditingPropertiesInEffect:
return CopyEditingProperties(execution_context, style,
kAllEditingProperties);
case EditingStyle::kOnlyEditingInheritableProperties:
return CopyEditingProperties(execution_context, style,
kOnlyInheritableEditingProperties);
}
NOTREACHED();
}
void EditingStyle::MergeInlineAndImplicitStyleOfElement(
Element* element,
CSSPropertyOverrideMode mode,
PropertiesToInclude properties_to_include) {
EditingStyle* style_from_rules = MakeGarbageCollected<EditingStyle>();
style_from_rules->MergeStyleFromRulesForSerialization(element);
if (element->InlineStyle())
style_from_rules->mutable_style_->MergeAndOverrideOnConflict(
element->InlineStyle());
style_from_rules->mutable_style_ = ExtractEditingProperties(
element->GetExecutionContext(), style_from_rules->mutable_style_.Get(),
properties_to_include);
MergeStyle(style_from_rules->mutable_style_.Get(), mode);
const HeapVector<Member<HTMLElementEquivalent>>& element_equivalents =
HtmlElementEquivalents();
for (const auto& equivalent : element_equivalents) {
if (ElementMatchesAndPropertyIsNotInInlineStyleDecl(
equivalent.Get(), element, mode, mutable_style_.Get()))
equivalent->AddToStyle(element, this);
}
const HeapVector<Member<HTMLAttributeEquivalent>>& attribute_equivalents =
HtmlAttributeEquivalents();
for (const auto& attribute : attribute_equivalents) {
if (attribute->AttributeName() == html_names::kDirAttr)
continue; // We don't want to include directionality
if (ElementMatchesAndPropertyIsNotInInlineStyleDecl(
attribute.Get(), element, mode, mutable_style_.Get()))
attribute->AddToStyle(element, this);
}
}
static const CSSValueList& MergeTextDecorationValues(
const CSSValueList& merged_value,
const CSSValueList& value_to_merge) {
DEFINE_STATIC_LOCAL(Persistent<CSSIdentifierValue>, underline,
(CSSIdentifierValue::Create(CSSValueID::kUnderline)));
DEFINE_STATIC_LOCAL(Persistent<CSSIdentifierValue>, line_through,
(CSSIdentifierValue::Create(CSSValueID::kLineThrough)));
CSSValueList& result = *merged_value.Copy();
if (value_to_merge.HasValue(*underline) && !merged_value.HasValue(*underline))
result.Append(*underline);
if (value_to_merge.HasValue(*line_through) &&
!merged_value.HasValue(*line_through))
result.Append(*line_through);
return result;
}
void EditingStyle::MergeStyle(const CSSPropertyValueSet* style,
CSSPropertyOverrideMode mode) {
if (!style)
return;
if (!mutable_style_) {
mutable_style_ = style->MutableCopy();
return;
}
unsigned property_count = style->PropertyCount();
for (unsigned i = 0; i < property_count; ++i) {
const CSSPropertyValue& property = style->PropertyAt(i);
const CSSValue* value =
mutable_style_->GetPropertyCSSValue(property.PropertyID());
// text decorations never override values
const auto* property_value_list = DynamicTo<CSSValueList>(property.Value());
if ((property.PropertyID() == CSSPropertyID::kTextDecorationLine ||
property.PropertyID() ==
CSSPropertyID::kWebkitTextDecorationsInEffect) &&
property_value_list && value) {
if (const auto* value_list = DynamicTo<CSSValueList>(value)) {
const CSSValueList& result =
MergeTextDecorationValues(*value_list, *property_value_list);
mutable_style_->SetProperty(property.PropertyID(), result,
property.IsImportant());
continue;
}
// text-decoration: none is equivalent to not having the property
value = nullptr;
}
if (mode == kOverrideValues || (mode == kDoNotOverrideValues && !value)) {
mutable_style_->SetLonghandProperty(property);
}
}
}
static MutableCSSPropertyValueSet* StyleFromMatchedRulesForElement(
Element* element,
unsigned rules_to_include) {
auto* style =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
StyleRuleList* matched_rules =
element->GetDocument().GetStyleResolver().StyleRulesForElement(
element, rules_to_include);
if (matched_rules) {
for (unsigned i = 0; i < matched_rules->size(); ++i)
style->MergeAndOverrideOnConflict(&matched_rules->at(i)->Properties());
}
return style;
}
void EditingStyle::MergeStyleFromRules(Element* element) {
MutableCSSPropertyValueSet* style_from_matched_rules =
StyleFromMatchedRulesForElement(element, StyleResolver::kAuthorCSSRules);
// Styles from the inline style declaration, held in the variable "style",
// take precedence over those from matched rules.
if (mutable_style_)
style_from_matched_rules->MergeAndOverrideOnConflict(mutable_style_.Get());
Clear();
mutable_style_ = style_from_matched_rules;
}
void EditingStyle::MergeStyleFromRulesForSerialization(Element* element) {
MergeStyleFromRules(element);
// The property value, if it's a percentage, may not reflect the actual
// computed value.
// For example: style="height: 1%; overflow: visible;" in quirksmode
// FIXME: There are others like this, see <rdar://problem/5195123> Slashdot
// copy/paste fidelity problem
auto* computed_style_for_element =
MakeGarbageCollected<CSSComputedStyleDeclaration>(element);
auto* from_computed_style =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
{
unsigned property_count = mutable_style_->PropertyCount();
for (unsigned i = 0; i < property_count; ++i) {
const CSSPropertyValue& property = mutable_style_->PropertyAt(i);
const CSSValue& value = property.Value();
const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value);
if (!primitive_value)
continue;
if (primitive_value->IsPercentage()) {
CSSPropertyName name = property.Name();
if (const CSSValue* computed_property_value =
computed_style_for_element->GetPropertyCSSValue(name)) {
from_computed_style->AddRespectingCascade(
CSSPropertyValue(name, *computed_property_value));
}
}
}
}
mutable_style_->MergeAndOverrideOnConflict(from_computed_style);
// There are some scenarios, like when copying rich text while in ForcedColors
// mode where we don't want to keep the ForcedColors styling, so that if it is
// pasted and sent to someone with no ForcedColors applied it does not affect
// their styling.
if (element->GetDocument().InForcedColorsMode()) {
mutable_style_->SetLonghandProperty(CSSPropertyID::kBackgroundColor,
CSSValueID::kInitial, false);
mutable_style_->SetLonghandProperty(CSSPropertyID::kColor,
CSSValueID::kInitial, false);
mutable_style_->SetLonghandProperty(CSSPropertyID::kTextDecorationColor,
CSSValueID::kInitial, false);
}
if (RuntimeEnabledFeatures::ResolveVarStylesOnCopyEnabled()) {
ComputeValues(element);
}
}
static void RemovePropertiesInStyle(
MutableCSSPropertyValueSet* style_to_remove_properties_from,
CSSPropertyValueSet* style) {
unsigned property_count = style->PropertyCount();
Vector<const CSSProperty*> properties_to_remove(property_count);
for (unsigned i = 0; i < property_count; ++i) {
// TODO(crbug.com/980160): Remove access to static Variable instance.
properties_to_remove[i] =
&CSSProperty::Get(style->PropertyAt(i).PropertyID());
}
style_to_remove_properties_from->RemovePropertiesInSet(properties_to_remove);
}
void EditingStyle::RemoveStyleFromRulesAndContext(Element* element,
Element* context) {
DCHECK(element);
if (!mutable_style_)
return;
// StyleResolver requires clean style.
DCHECK_GE(element->GetDocument().Lifecycle().GetState(),
DocumentLifecycle::kStyleClean);
DCHECK(element->GetDocument().IsActive());
SecureContextMode secure_context_mode =
element->GetExecutionContext()->GetSecureContextMode();
// 1. Remove style from matched rules because style remain without repeating
// it in inline style declaration
MutableCSSPropertyValueSet* style_from_matched_rules =
StyleFromMatchedRulesForElement(element, StyleResolver::kAllCSSRules);
if (style_from_matched_rules && !style_from_matched_rules->IsEmpty()) {
mutable_style_ =
GetPropertiesNotIn(mutable_style_.Get(), element,
style_from_matched_rules->EnsureCSSStyleDeclaration(
element->GetExecutionContext()),
secure_context_mode);
}
// 2. Remove style present in context and not overriden by matched rules.
EditingStyle* computed_style =
MakeGarbageCollected<EditingStyle>(context, kEditingPropertiesInEffect);
if (computed_style->mutable_style_) {
if (!computed_style->mutable_style_->GetPropertyCSSValue(
CSSPropertyID::kBackgroundColor)) {
computed_style->mutable_style_->SetLonghandProperty(
CSSPropertyID::kBackgroundColor, CSSValueID::kTransparent);
}
RemovePropertiesInStyle(computed_style->mutable_style_.Get(),
style_from_matched_rules);
mutable_style_ = GetPropertiesNotIn(
mutable_style_.Get(), element,
computed_style->mutable_style_->EnsureCSSStyleDeclaration(
element->GetExecutionContext()),
secure_context_mode);
}
// 3. If this element is a span and has display: inline or float: none, remove
// them unless they are overriden by rules. These rules are added by
// serialization code to wrap text nodes.
if (IsStyleSpanOrSpanWithOnlyStyleAttribute(element)) {
if (!style_from_matched_rules->GetPropertyCSSValue(
CSSPropertyID::kDisplay) &&
GetProperty(CSSPropertyID::kDisplay) == CSSValueID::kInline)
mutable_style_->RemoveProperty(CSSPropertyID::kDisplay);
if (!style_from_matched_rules->GetPropertyCSSValue(CSSPropertyID::kFloat) &&
GetProperty(CSSPropertyID::kFloat) == CSSValueID::kNone)
mutable_style_->RemoveProperty(CSSPropertyID::kFloat);
}
}
void EditingStyle::RemovePropertiesInElementDefaultStyle(Element* element) {
if (!mutable_style_ || mutable_style_->IsEmpty())
return;
CSSPropertyValueSet* default_style = StyleFromMatchedRulesForElement(
element, StyleResolver::kUAAndUserCSSRules);
RemovePropertiesInStyle(mutable_style_.Get(), default_style);
}
void EditingStyle::ForceInline() {
if (!mutable_style_) {
mutable_style_ =
MakeGarbageCollected<MutableCSSPropertyValueSet>(kHTMLQuirksMode);
}
const bool kPropertyIsImportant = true;
mutable_style_->SetLonghandProperty(
CSSPropertyID::kDisplay, CSSValueID::kInline, kPropertyIsImportant);
}
int EditingStyle::LegacyFontSize(Document* document) const {
const CSSValue* css_value =
mutable_style_->GetPropertyCSSValue(CSSPropertyID::kFontSize);
if (!css_value ||
!(css_value->IsPrimitiveValue() || css_value->IsIdentifierValue()))
return 0;
return LegacyFontSizeFromCSSValue(document, css_value, is_monospace_font_,
kAlwaysUseLegacyFontSize);
}
void EditingStyle::Trace(Visitor* visitor) const {
visitor->Trace(mutable_style_);
visitor->Trace(node_);
}
static void ReconcileTextDecorationProperties(
MutableCSSPropertyValueSet* style,
SecureContextMode secure_context_mode) {
const CSSValue* text_decorations_in_effect =
style->GetPropertyCSSValue(CSSPropertyID::kWebkitTextDecorationsInEffect);
const CSSValue* text_decoration =
style->GetPropertyCSSValue(CSSPropertyID::kTextDecorationLine);
// "web_tests/editing/execCommand/insert-list-and-strikethrough.html" makes
// both |textDecorationsInEffect| and |textDecoration| non-null.
if (text_decorations_in_effect) {
style->ParseAndSetProperty(CSSPropertyID::kTextDecorationLine,
text_decorations_in_effect->CssText(),
/* important */ false, secure_context_mode);
style->RemoveProperty(CSSPropertyID::kWebkitTextDecorationsInEffect);
text_decoration = text_decorations_in_effect;
}
// If text-decoration is set to "none", remove the property because we don't
// want to add redundant "text-decoration: none".
if (text_decoration && !text_decoration->IsValueList())
style->RemoveProperty(CSSPropertyID::kTextDecorationLine);
}
StyleChange::StyleChange(EditingStyle* style, const Position& position)
: apply_bold_(false),
apply_italic_(false),
apply_underline_(false),
apply_line_through_(false),
apply_subscript_(false),
apply_superscript_(false) {
Document* document = position.GetDocument();
if (!style || !style->Style() || !document || !document->GetFrame())
return;
Element* const element = AssociatedElementOf(position);
if (!element)
return;
CSSComputedStyleDeclaration* const computed_style =
MakeGarbageCollected<CSSComputedStyleDeclaration>(element);
// FIXME: take care of background-color in effect
// Note: editing/undo/redo-selection-modify-crash.html needs to pass
// |element| to |GetPropertiesNotIn()| to remove "text-align:left".
MutableCSSPropertyValueSet* mutable_style = GetPropertiesNotIn(
style->Style(), element, computed_style,
document->GetExecutionContext()->GetSecureContextMode());
DCHECK(mutable_style);
ReconcileTextDecorationProperties(
mutable_style, document->GetExecutionContext()->GetSecureContextMode());
if (!document->GetFrame()->GetEditor().ShouldStyleWithCSS())
ExtractTextStyles(document, mutable_style,
computed_style->IsMonospaceFont());
// If unicode-bidi is present in mutableStyle and direction is not, then add
// direction to mutableStyle.
// FIXME: Shouldn't this be done in getPropertiesNotIn?
if (mutable_style->GetPropertyCSSValue(CSSPropertyID::kUnicodeBidi) &&
!style->Style()->GetPropertyCSSValue(CSSPropertyID::kDirection)) {
mutable_style->ParseAndSetProperty(
CSSPropertyID::kDirection,
style->Style()->GetPropertyValue(CSSPropertyID::kDirection),
/* important */ false,
document->GetExecutionContext()->GetSecureContextMode());
}
// Save the result for later
css_style_ = mutable_style->AsText().StripWhiteSpace();
}
static void SetTextDecorationProperty(MutableCSSPropertyValueSet* style,
const CSSValueList* new_text_decoration,
CSSPropertyID property_id,
SecureContextMode secure_context_mode) {
if (new_text_decoration->length()) {
style->ParseAndSetProperty(property_id, new_text_decoration->CssText(),
style->PropertyIsImportant(property_id),
secure_context_mode);
} else {
// text-decoration: none is redundant since it does not remove any text
// decorations.
style->RemoveProperty(property_id);
}
}
static std::optional<double> GetPrimitiveValueNumber(
CSSPropertyValueSet* style,
CSSPropertyID property_id) {
if (!style) {
return std::nullopt;
}
const CSSValue* value = style->GetPropertyCSSValue(property_id);
if (const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value)) {
return primitive_value->GetValueIfKnown();
}
return std::nullopt;
}
void StyleChange::ExtractTextStyles(Document* document,
MutableCSSPropertyValueSet* style,
bool is_monospace_font) {
CHECK(style);
std::optional<double> weight =
GetPrimitiveValueNumber(style, CSSPropertyID::kFontWeight);
if ((weight.has_value() && weight.value() >= kBoldThreshold) ||
GetIdentifierValue(style, CSSPropertyID::kFontWeight) ==
CSSValueID::kBold) {
style->RemoveProperty(CSSPropertyID::kFontWeight);
apply_bold_ = true;
}
CSSValueID font_style = GetIdentifierValue(style, CSSPropertyID::kFontStyle);
if (font_style == CSSValueID::kItalic || font_style == CSSValueID::kOblique) {
style->RemoveProperty(CSSPropertyID::kFontStyle);
apply_italic_ = true;
}
// Assuming reconcileTextDecorationProperties has been called, there should
// not be -webkit-text-decorations-in-effect
// Furthermore, text-decoration: none has been trimmed so that text-decoration
// property is always a CSSValueList.
const CSSValue* text_decoration =
style->GetPropertyCSSValue(CSSPropertyID::kTextDecorationLine);
if (const auto* text_decoration_value_list =
DynamicTo<CSSValueList>(text_decoration)) {
DEFINE_STATIC_LOCAL(Persistent<CSSIdentifierValue>, underline,
(CSSIdentifierValue::Create(CSSValueID::kUnderline)));
DEFINE_STATIC_LOCAL(Persistent<CSSIdentifierValue>, line_through,
(CSSIdentifierValue::Create(CSSValueID::kLineThrough)));
CSSValueList* new_text_decoration = text_decoration_value_list->Copy();
if (new_text_decoration->RemoveAll(*underline))
apply_underline_ = true;
if (new_text_decoration->RemoveAll(*line_through))
apply_line_through_ = true;
// If trimTextDecorations, delete underline and line-through
SetTextDecorationProperty(
style, new_text_decoration, CSSPropertyID::kTextDecorationLine,
document->GetExecutionContext()->GetSecureContextMode());
}
CSSValueID vertical_align =
GetIdentifierValue(style, CSSPropertyID::kVerticalAlign);
switch (vertical_align) {
case CSSValueID::kSub:
style->RemoveProperty(CSSPropertyID::kVerticalAlign);
apply_subscript_ = true;
break;
case CSSValueID::kSuper:
style->RemoveProperty(CSSPropertyID::kVerticalAlign);
apply_superscript_ = true;
break;
default:
break;
}
if (style->GetPropertyCSSValue(CSSPropertyID::kColor)) {
// The <font> tag cannot handle rgb colors, so we need to serialize as hex
// here in order to continue supporting it.
apply_font_color_ = GetFontColor(style).SerializeAsCanvasColor();
style->RemoveProperty(CSSPropertyID::kColor);
}
apply_font_face_ = style->GetPropertyValue(CSSPropertyID::kFontFamily);
// Remove double quotes for Outlook 2007 compatibility. See
// https://bugs.webkit.org/show_bug.cgi?id=79448
apply_font_face_.Replace('"', "");
style->RemoveProperty(CSSPropertyID::kFontFamily);
if (const CSSValue* font_size =
style->GetPropertyCSSValue(CSSPropertyID::kFontSize)) {
if (!font_size->IsPrimitiveValue() && !font_size->IsIdentifierValue()) {
// Can't make sense of the number. Put no font size.
style->RemoveProperty(CSSPropertyID::kFontSize);
} else if (int legacy_font_size = LegacyFontSizeFromCSSValue(
document, font_size, is_monospace_font,
kUseLegacyFontSizeOnlyIfPixelValuesMatch)) {
apply_font_size_ = String::Number(legacy_font_size);
style->RemoveProperty(CSSPropertyID::kFontSize);
}
}
}
static void DiffTextDecorations(MutableCSSPropertyValueSet* style,
CSSPropertyID property_id,
const CSSValue* ref_text_decoration,
SecureContextMode secure_context_mode) {
const CSSValue* text_decoration = style->GetPropertyCSSValue(property_id);
const auto* values_in_text_decoration =
DynamicTo<CSSValueList>(text_decoration);
const auto* values_in_ref_text_decoration =
DynamicTo<CSSValueList>(ref_text_decoration);
if (!values_in_text_decoration || !values_in_ref_text_decoration)
return;
CSSValueList* new_text_decoration = values_in_text_decoration->Copy();
for (wtf_size_t i = 0; i < values_in_ref_text_decoration->length(); i++)
new_text_decoration->RemoveAll(values_in_ref_text_decoration->Item(i));
SetTextDecorationProperty(style, new_text_decoration, property_id,
secure_context_mode);
}
static bool FontWeightIsBold(const CSSValue* font_weight) {
if (auto* font_weight_identifier_value =
DynamicTo<CSSIdentifierValue>(font_weight)) {
// Because b tag can only bold text, there are only two states in plain
// html: bold and not bold. Collapse all other values to either one of these
// two states for editing purposes.
switch (font_weight_identifier_value->GetValueID()) {
case CSSValueID::kNormal:
return false;
case CSSValueID::kBold:
return true;
default:
break;
}
}
if (const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(font_weight)) {
CHECK(primitive_value->IsNumber());
std::optional<double> weight = primitive_value->GetValueIfKnown();
return weight.has_value() && weight.value() >= kBoldThreshold;
}
return false;
}
static bool FontWeightNeedsResolving(const CSSValue* font_weight) {
if (font_weight->IsPrimitiveValue())
return false;
auto* font_weight_identifier_value =
DynamicTo<CSSIdentifierValue>(font_weight);
if (!font_weight_identifier_value)
return true;
const CSSValueID value = font_weight_identifier_value->GetValueID();
return value == CSSValueID::kLighter || value == CSSValueID::kBolder;
}
void EditingStyle::ComputeValues(Element* element) {
DCHECK(element);
DCHECK(mutable_style_);
if (!element || !mutable_style_) {
return;
}
if (!element->GetComputedStyle()) {
return;
}
StyleResolver& resolver = element->GetDocument().GetStyleResolver();
// Create a new mutable CSS property value set for resolved styles.
MutableCSSPropertyValueSet* resolved_property_value_set =
MakeGarbageCollected<MutableCSSPropertyValueSet>(
mutable_style_->CssParserMode());
// Resolve each property in the current mutable_style_.
for (const CSSPropertyValue& property : mutable_style_->Properties()) {
const CSSValue& value = property.Value();
if (value.IsUnparsedDeclaration() || value.IsPendingSubstitutionValue()) {
if (const CSSValue* resolved_value =
resolver.ComputeValue(element, property.Name(), value)) {
resolved_property_value_set->SetProperty(
property.Name(), *resolved_value, property.IsImportant());
continue;
}
}
resolved_property_value_set->SetProperty(property.Name(), value,
property.IsImportant());
}
// Update the mutable style with the resolved style set.
mutable_style_ = resolved_property_value_set;
}
MutableCSSPropertyValueSet* GetPropertiesNotIn(
CSSPropertyValueSet* style_with_redundant_properties,
Node* node,
CSSStyleDeclaration* base_style,
SecureContextMode secure_context_mode) {
DCHECK(style_with_redundant_properties);
DCHECK(node);
DCHECK(base_style);
MutableCSSPropertyValueSet* result =
style_with_redundant_properties->MutableCopy();
result->RemoveEquivalentProperties(base_style);
const CSSValue* base_text_decorations_in_effect =
base_style->GetPropertyCSSValueInternal(
CSSPropertyID::kWebkitTextDecorationsInEffect);
DiffTextDecorations(result, CSSPropertyID::kTextDecorationLine,
base_text_decorations_in_effect, secure_context_mode);
DiffTextDecorations(result, CSSPropertyID::kWebkitTextDecorationsInEffect,
base_text_decorations_in_effect, secure_context_mode);
if (const CSSValue* base_font_weight =
base_style->GetPropertyCSSValueInternal(CSSPropertyID::kFontWeight)) {
if (const CSSValue* font_weight =
result->GetPropertyCSSValue(CSSPropertyID::kFontWeight)) {
if (!FontWeightNeedsResolving(font_weight) &&
!FontWeightNeedsResolving(base_font_weight) &&
(FontWeightIsBold(font_weight) == FontWeightIsBold(base_font_weight)))
result->RemoveProperty(CSSPropertyID::kFontWeight);
}
}
if (base_style->GetPropertyCSSValueInternal(CSSPropertyID::kColor) &&
GetFontColor(result) == GetFontColor(base_style))
result->RemoveProperty(CSSPropertyID::kColor);
if (IsRedundantTextAlign(result, base_style, node))
result->RemoveProperty(CSSPropertyID::kTextAlign);
if (base_style->GetPropertyCSSValueInternal(
CSSPropertyID::kBackgroundColor) &&
GetBackgroundColor(result) == GetBackgroundColor(base_style))
result->RemoveProperty(CSSPropertyID::kBackgroundColor);
return result;
}
CSSValueID GetIdentifierValue(CSSPropertyValueSet* style,
CSSPropertyID property_id) {
if (!style)
return CSSValueID::kInvalid;
const CSSValue* value = style->GetPropertyCSSValue(property_id);
auto* identifier_value = DynamicTo<CSSIdentifierValue>(value);
if (!identifier_value)
return CSSValueID::kInvalid;
return identifier_value->GetValueID();
}
CSSValueID GetIdentifierValue(CSSStyleDeclaration* style,
CSSPropertyID property_id) {
if (!style)
return CSSValueID::kInvalid;
const CSSValue* value = style->GetPropertyCSSValueInternal(property_id);
auto* identifier_value = DynamicTo<CSSIdentifierValue>(value);
if (!identifier_value)
return CSSValueID::kInvalid;
return identifier_value->GetValueID();
}
int LegacyFontSizeFromCSSValue(Document* document,
const CSSValue* value,
bool is_monospace_font,
LegacyFontSizeMode mode) {
if (const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value)) {
if (primitive_value->IsLength()) {
// TODO(crbug.com/40634280): This doesn't seem to be handle math functions
// correctly. This is the result of a refactoring, and may have revealed
// an existing bug. Fix it if necessary.
CSSPrimitiveValue::UnitType length_unit =
primitive_value->IsNumericLiteralValue()
? To<CSSNumericLiteralValue>(primitive_value)->GetType()
: CSSPrimitiveValue::UnitType::kPixels;
if (!CSSPrimitiveValue::IsRelativeUnit(length_unit)) {
std::optional<double> number = primitive_value->GetValueIfKnown();
if (!number.has_value()) {
return 0;
}
double conversion =
CSSPrimitiveValue::ConversionToCanonicalUnitsScaleFactor(
length_unit);
int pixel_font_size = ClampTo<int>(number.value() * conversion);
int legacy_font_size = FontSizeFunctions::LegacyFontSize(
document, pixel_font_size, is_monospace_font);
// Use legacy font size only if pixel value matches exactly to that of
// legacy font size.
if (mode == kAlwaysUseLegacyFontSize ||
FontSizeFunctions::FontSizeForKeyword(document, legacy_font_size,
is_monospace_font) ==
pixel_font_size)
return legacy_font_size;
return 0;
}
}
}
if (const auto* identifier_value = DynamicTo<CSSIdentifierValue>(value)) {
if (identifier_value->GetValueID() == CSSValueID::kWebkitXxxLarge)
return FontSizeFunctions::KeywordSize(CSSValueID::kXxxLarge) - 1;
if (CSSValueID::kXSmall <= identifier_value->GetValueID() &&
identifier_value->GetValueID() <= CSSValueID::kXxxLarge)
return FontSizeFunctions::KeywordSize(identifier_value->GetValueID()) - 1;
}
return 0;
}
EditingTriState EditingStyle::SelectionHasStyle(const LocalFrame& frame,
CSSPropertyID property_id,
const String& value) {
const SecureContextMode secure_context_mode =
frame.DomWindow()->GetSecureContextMode();
// TODO(editing-dev): The use of UpdateStyleAndLayout
// needs to be audited. See http://crbug.com/590369 for more details.
frame.GetDocument()->UpdateStyleAndLayout(DocumentUpdateReason::kSelection);
return MakeGarbageCollected<EditingStyle>(property_id, value,
secure_context_mode)
->TriStateOfStyle(frame.Selection().ComputeVisibleSelectionInDOMTree(),
secure_context_mode);
}
} // namespace blink
|