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 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
|
/*
* Copyright (C) 2004-2023 Apple Inc. All rights reserved.
* Copyright (C) 2015-2018 Google Inc. All rights reserved.
* Copyright (C) 2005 Alexey Proskuryakov.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* 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 "config.h"
#include "TextIterator.h"
#include "ComposedTreeIterator.h"
#include "Document.h"
#include "Editing.h"
#include "ElementInlines.h"
#include "ElementRareData.h"
#include "FontCascade.h"
#include "HTMLAttachmentElement.h"
#include "HTMLBodyElement.h"
#include "HTMLElement.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLegendElement.h"
#include "HTMLMeterElement.h"
#include "HTMLNames.h"
#include "HTMLParagraphElement.h"
#include "HTMLProgressElement.h"
#include "HTMLSlotElement.h"
#include "HTMLTextAreaElement.h"
#include "HTMLTextFormControlElement.h"
#include "ImageOverlay.h"
#include "LocalFrame.h"
#include "NodeTraversal.h"
#include "Range.h"
#include "RenderBoxInlines.h"
#include "RenderElementInlines.h"
#include "RenderImage.h"
#include "RenderIterator.h"
#include "RenderTableCell.h"
#include "RenderTableRow.h"
#include "RenderTextControl.h"
#include "RenderTextFragment.h"
#include "ShadowRoot.h"
#include "TextBoundaries.h"
#include "TextControlInnerElements.h"
#include "TextPlaceholderElement.h"
#include "VisiblePosition.h"
#include "VisibleUnits.h"
#include <unicode/unorm2.h>
#include <wtf/Function.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/CString.h>
#include <wtf/text/MakeString.h>
#include <wtf/text/ParsingUtilities.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/TextBreakIterator.h>
#include <wtf/unicode/CharacterNames.h>
#include <wtf/unicode/icu/ICUHelpers.h>
#if !UCONFIG_NO_COLLATION
#include <unicode/usearch.h>
#include <wtf/text/TextBreakIteratorInternalICU.h>
#endif
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(TextIterator);
using namespace WTF::Unicode;
using namespace HTMLNames;
// Buffer that knows how to compare with a search target.
// Keeps enough of the previous text to be able to search in the future, but no more.
// Non-breaking spaces are always equal to normal spaces.
// Case folding is also done if the CaseInsensitive option is specified.
// Matches are further filtered if the AtWordStarts option is specified, although some
// matches inside a word are permitted if TreatMedialCapitalAsWordStart is specified as well.
class SearchBuffer {
WTF_MAKE_NONCOPYABLE(SearchBuffer);
public:
SearchBuffer(const String& target, FindOptions);
~SearchBuffer();
// Returns number of characters appended; guaranteed to be in the range [1, length].
size_t append(StringView);
bool needsMoreContext() const;
void prependContext(StringView);
void reachedBreak();
// Result is the size in characters of what was found.
// And <startOffset> is the number of characters back to the start of what was found.
size_t search(size_t& startOffset);
bool atBreak() const;
#if !UCONFIG_NO_COLLATION
private:
bool isBadMatch(const UChar*, size_t length) const;
bool isWordStartMatch(size_t start, size_t length) const;
bool isWordEndMatch(size_t start, size_t length) const;
const String m_target;
const StringView::UpconvertedCharacters m_targetCharacters;
FindOptions m_options;
Vector<UChar> m_buffer;
size_t m_overlap;
size_t m_prefixLength;
bool m_atBreak;
bool m_needsMoreContext;
const bool m_targetRequiresKanaWorkaround;
Vector<UChar> m_normalizedTarget;
mutable Vector<UChar> m_normalizedMatch;
#else
private:
void append(UChar, bool isCharacterStart);
size_t length() const;
String m_target;
FindOptions m_options;
Vector<UChar> m_buffer;
Vector<bool> m_isCharacterStartBuffer;
bool m_isBufferFull;
size_t m_cursor;
#endif
};
// --------
constexpr unsigned bitsInWord = sizeof(unsigned) * 8;
constexpr unsigned bitInWordMask = bitsInWord - 1;
void BitStack::push(bool bit)
{
unsigned index = m_size / bitsInWord;
unsigned shift = m_size & bitInWordMask;
if (!shift && index == m_words.size()) {
m_words.grow(index + 1);
m_words[index] = 0;
}
unsigned& word = m_words[index];
unsigned mask = 1U << shift;
if (bit)
word |= mask;
else
word &= ~mask;
++m_size;
}
void BitStack::pop()
{
if (m_size)
--m_size;
}
bool BitStack::top() const
{
if (!m_size)
return false;
unsigned shift = (m_size - 1) & bitInWordMask;
unsigned index = (m_size - 1) / bitsInWord;
return m_words[index] & (1U << shift);
}
// --------
// This function is like Range::pastLastNode, except for the fact that it can climb up out of shadow trees.
static RefPtr<Node> nextInPreOrderCrossingShadowBoundaries(Node& rangeEndContainer, int rangeEndOffset)
{
if (rangeEndOffset >= 0 && !rangeEndContainer.isCharacterDataNode()) {
if (RefPtr next = rangeEndContainer.traverseToChildAt(rangeEndOffset))
return next;
}
for (RefPtr node = &rangeEndContainer; node; node = node->parentOrShadowHostNode()) {
if (RefPtr next = node->nextSibling())
return next;
}
return nullptr;
}
static inline bool fullyClipsContents(Node& node)
{
CheckedPtr renderer = node.renderer();
if (!renderer) {
RefPtr element = dynamicDowncast<Element>(node);
return element && !element->hasDisplayContents();
}
CheckedPtr box = dynamicDowncast<RenderBox>(*renderer);
if (!box || !box->hasNonVisibleOverflow())
return false;
// Quirk to keep copy/paste in the CodeMirror editor version used in Jenkins working.
if (is<HTMLTextAreaElement>(node))
return box->size().isEmpty();
return box->contentBoxSize().isEmpty();
}
static inline bool ignoresContainerClip(Node& node)
{
CheckedPtr renderer = node.renderer();
if (!renderer || renderer->isRenderTextOrLineBreak())
return false;
return renderer->style().hasOutOfFlowPosition();
}
static void pushFullyClippedState(BitStack& stack, Node& node)
{
// Push true if this node full clips its contents, or if a parent already has fully
// clipped and this is not a node that ignores its container's clip.
stack.push(fullyClipsContents(node) || (stack.top() && !ignoresContainerClip(node)));
}
static void setUpFullyClippedStack(BitStack& stack, Node& node)
{
// Put the nodes in a vector so we can iterate in reverse order.
// FIXME: This (and TextIterator in general) should use ComposedTreeIterator.
Vector<Ref<Node>, 100> ancestry;
for (RefPtr parent = node.parentOrShadowHostNode(); parent; parent = parent->parentOrShadowHostNode())
ancestry.append(*parent);
// Call pushFullyClippedState on each node starting with the earliest ancestor.
size_t size = ancestry.size();
for (size_t i = 0; i < size; ++i)
pushFullyClippedState(stack, ancestry[size - i - 1]);
pushFullyClippedState(stack, node);
}
static bool isClippedByFrameAncestor(const Document& document, TextIteratorBehaviors behaviors)
{
if (!behaviors.contains(TextIteratorBehavior::ClipsToFrameAncestors))
return false;
for (RefPtr owner = document.ownerElement(); owner; owner = owner->document().ownerElement()) {
BitStack ownerClipStack;
setUpFullyClippedStack(ownerClipStack, *owner);
if (ownerClipStack.top())
return true;
}
return false;
}
// FIXME: editingIgnoresContent and isRendererReplacedElement try to do the same job.
// It's not good to have both of them.
bool isRendererReplacedElement(RenderObject* renderer, TextIteratorBehaviors behaviors)
{
if (!renderer)
return false;
bool isAttachment = false;
#if ENABLE(ATTACHMENT_ELEMENT)
isAttachment = renderer->isRenderAttachment();
#endif
if (renderer->isImage() || renderer->isRenderWidget() || renderer->isRenderMedia() || isAttachment)
return true;
if (RefPtr element = dynamicDowncast<Element>(renderer->node())) {
if (is<HTMLFormControlElement>(*element) || is<HTMLLegendElement>(*element) || is<HTMLProgressElement>(*element) || element->hasTagName(meterTag))
return true;
if (equalLettersIgnoringASCIICase(element->attributeWithoutSynchronization(roleAttr), "img"_s))
return true;
#if USE(ATSPI)
// Links are also replaced with object replacement character in ATSPI.
if (behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharacters) && element->isLink())
return true;
#else
UNUSED_PARAM(behaviors);
#endif
}
return false;
}
// --------
inline void TextIteratorCopyableText::reset()
{
m_singleCharacter = 0;
m_string = String();
m_offset = 0;
m_length = 0;
}
inline void TextIteratorCopyableText::set(String&& string)
{
m_singleCharacter = 0;
m_string = WTFMove(string);
m_offset = 0;
m_length = m_string.length();
}
inline void TextIteratorCopyableText::set(String&& string, unsigned offset, unsigned length)
{
ASSERT(offset < string.length());
ASSERT(length);
ASSERT(length <= string.length() - offset);
m_singleCharacter = 0;
m_string = WTFMove(string);
m_offset = offset;
m_length = length;
}
inline void TextIteratorCopyableText::set(UChar singleCharacter)
{
m_singleCharacter = singleCharacter;
m_string = String();
m_offset = 0;
m_length = 0;
}
void TextIteratorCopyableText::appendToStringBuilder(StringBuilder& builder) const
{
if (m_singleCharacter)
builder.append(m_singleCharacter);
else
builder.appendSubstring(m_string, m_offset, m_length);
}
// --------
static Node* firstNode(const BoundaryPoint& point)
{
if (point.container->isCharacterDataNode())
return point.container.ptr();
if (RefPtr child = point.container->traverseToChildAt(point.offset))
return child.get();
if (!point.offset)
return point.container.ptr();
return NodeTraversal::nextSkippingChildren(point.container);
}
TextIterator::TextIterator(const SimpleRange& range, TextIteratorBehaviors behaviors)
: m_behaviors(behaviors)
{
ASSERT(!m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharacters) || !m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharactersForImages));
range.start.protectedDocument()->updateLayoutIgnorePendingStylesheets();
m_startContainer = range.start.container.ptr();
m_startOffset = range.start.offset;
m_endContainer = range.end.container.ptr();
m_endOffset = range.end.offset;
m_currentNode = firstNode(range.start);
if (!m_currentNode)
return;
init();
}
void TextIterator::init()
{
auto currentNode = protectedCurrentNode();
if (isClippedByFrameAncestor(currentNode->protectedDocument(), m_behaviors))
return;
setUpFullyClippedStack(m_fullyClippedStack, *currentNode);
m_offset = currentNode == m_startContainer.get() ? m_startOffset : 0;
m_pastEndNode = nextInPreOrderCrossingShadowBoundaries(*m_endContainer, m_endOffset);
m_positionNode = currentNode.get();
advance();
}
TextIterator::~TextIterator() = default;
// FIXME: Use ComposedTreeIterator instead. These functions are more expensive because they might do O(n) work.
static inline Node* firstChild(TextIteratorBehaviors options, Node& node)
{
if (UNLIKELY(options.contains(TextIteratorBehavior::TraversesFlatTree)))
return firstChildInComposedTreeIgnoringUserAgentShadow(node);
return node.firstChild();
}
static inline Node* nextSibling(TextIteratorBehaviors options, Node& node)
{
if (UNLIKELY(options.contains(TextIteratorBehavior::TraversesFlatTree)))
return nextSiblingInComposedTreeIgnoringUserAgentShadow(node);
return node.nextSibling();
}
static inline Node* nextNode(TextIteratorBehaviors options, Node& node)
{
if (UNLIKELY(options.contains(TextIteratorBehavior::TraversesFlatTree)))
return nextInComposedTreeIgnoringUserAgentShadow(node);
return NodeTraversal::next(node);
}
static inline bool isDescendantOf(TextIteratorBehaviors options, Node& node, Node& possibleAncestor)
{
if (UNLIKELY(options.contains(TextIteratorBehavior::TraversesFlatTree)))
return node.isDescendantOrShadowDescendantOf(&possibleAncestor);
return node.isDescendantOf(&possibleAncestor);
}
static inline Node* parentNodeOrShadowHost(TextIteratorBehaviors options, Node& node)
{
if (UNLIKELY(options.contains(TextIteratorBehavior::TraversesFlatTree)))
return node.parentInComposedTree();
return node.parentOrShadowHostNode();
}
static inline bool hasDisplayContents(Node& node)
{
RefPtr element = dynamicDowncast<Element>(node);
return element && element->hasDisplayContents();
}
static bool isRendererVisible(const RenderObject* renderer, TextIteratorBehaviors behaviors)
{
return renderer && !renderer->isSkippedContent() && !(renderer->style().usedUserSelect() == UserSelect::None && behaviors.contains(TextIteratorBehavior::IgnoresUserSelectNone));
}
void TextIterator::advance()
{
ASSERT(!atEnd());
// reset the run information
m_positionNode = nullptr;
m_copyableText.reset();
m_text = StringView();
// handle remembered node that needed a newline after the text node's newline
if (RefPtr nodeForAdditionalNewline = std::exchange(m_nodeForAdditionalNewline, nullptr).get()) {
// Emit the extra newline, and position it *inside* m_node, after m_node's
// contents, in case it's a block, in the same way that we position the first
// newline. The range for the emitted newline should start where the line
// break begins.
// FIXME: It would be cleaner if we emitted two newlines during the last
// iteration, instead of using m_needsAnotherNewline.
auto parentNode = nodeForAdditionalNewline->protectedParentNode();
emitCharacter('\n', WTFMove(parentNode), WTFMove(nodeForAdditionalNewline), 1, 1);
return;
}
if (!m_textRun && m_remainingTextRun)
revertToRemainingTextRun();
// handle remembered text box
if (m_textRun) {
handleTextRun();
if (m_positionNode)
return;
}
while (m_currentNode && m_currentNode != m_pastEndNode) {
// if the range ends at offset 0 of an element, represent the
// position, but not the content, of that element e.g. if the
// node is a blockflow element, emit a newline that
// precedes the element
if (m_currentNode == m_endContainer && !m_endOffset) {
representNodeOffsetZero();
m_currentNode = nullptr;
return;
}
CheckedPtr renderer = m_currentNode->renderer();
if (!m_handledNode) {
if (!isRendererVisible(renderer.get(), m_behaviors)) {
m_handledNode = true;
m_handledChildren = !hasDisplayContents(*protectedCurrentNode()) && !renderer;
} else if (auto* renderElement = dynamicDowncast<RenderElement>(renderer.get()); renderElement && isSkippedContentRoot(*renderElement))
m_handledChildren = true;
else {
// handle current node according to its type
if (renderer->isRenderText() && m_currentNode->isTextNode())
m_handledNode = handleTextNode();
else if (isRendererReplacedElement(renderer.get(), m_behaviors))
m_handledNode = handleReplacedElement();
else
m_handledNode = handleNonTextNode();
if (m_positionNode)
return;
}
}
// find a new current node to handle in depth-first manner,
// calling exitNode() as we come back thru a parent node
RefPtr next = m_handledChildren ? nullptr : firstChild(m_behaviors, *protectedCurrentNode());
m_offset = 0;
if (!next) {
auto currentNode = protectedCurrentNode();
next = nextSibling(m_behaviors, *currentNode);
if (!next) {
bool pastEnd = nextNode(m_behaviors, *currentNode) == m_pastEndNode;
RefPtr parentNode = parentNodeOrShadowHost(m_behaviors, *currentNode);
while (!next && parentNode) {
if ((pastEnd && parentNode == m_endContainer.get()) || isDescendantOf(m_behaviors, *m_endContainer, *parentNode))
return;
bool haveRenderer = isRendererVisible(currentNode->renderer(), m_behaviors);
RefPtr exitedNode = WTFMove(currentNode);
m_currentNode = WTFMove(parentNode);
currentNode = m_currentNode;
m_fullyClippedStack.pop();
parentNode = parentNodeOrShadowHost(m_behaviors, *currentNode);
if (haveRenderer)
exitNode(exitedNode.get());
if (m_positionNode) {
m_handledNode = true;
m_handledChildren = true;
return;
}
next = nextSibling(m_behaviors, *currentNode);
if (next && isRendererVisible(currentNode->renderer(), m_behaviors))
exitNode(currentNode.get());
}
}
m_fullyClippedStack.pop();
}
// set the new current node
m_currentNode = WTFMove(next);
if (auto currentNode = protectedCurrentNode())
pushFullyClippedState(m_fullyClippedStack, *currentNode);
m_handledNode = false;
m_handledChildren = false;
m_handledFirstLetter = false;
m_firstLetterText = nullptr;
// how would this ever be?
if (m_positionNode)
return;
}
}
static bool hasVisibleTextNode(RenderText& renderer)
{
if (renderer.style().visibility() == Visibility::Visible)
return true;
if (CheckedPtr renderTextFragment = dynamicDowncast<RenderTextFragment>(renderer)) {
if (auto firstLetter = renderTextFragment->firstLetter()) {
if (firstLetter->style().visibility() == Visibility::Visible)
return true;
}
}
return false;
}
bool TextIterator::handleTextNode()
{
Ref textNode = downcast<Text>(protectedCurrentNode().releaseNonNull());
if (m_fullyClippedStack.top() && !m_behaviors.contains(TextIteratorBehavior::IgnoresStyleVisibility))
return false;
CheckedRef renderer = *textNode->renderer();
m_lastTextNode = textNode.ptr();
auto rendererText = rendererTextForBehavior(renderer.get());
// handle pre-formatted text
if (!renderer->style().collapseWhiteSpace()) {
int runStart = m_offset;
if (m_lastTextNodeEndedWithCollapsedSpace && hasVisibleTextNode(renderer)) {
emitCharacter(' ', WTFMove(textNode), nullptr, runStart, runStart);
return false;
}
if (CheckedPtr renderTextFragment = dynamicDowncast<RenderTextFragment>(renderer.get()); renderTextFragment && !m_handledFirstLetter && !m_offset) {
handleTextNodeFirstLetter(*renderTextFragment);
if (m_firstLetterText) {
String firstLetter = m_firstLetterText->text();
emitText(textNode, *m_firstLetterText, m_offset, m_offset + firstLetter.length());
m_firstLetterText = nullptr;
m_textRun = { };
return false;
}
}
if (renderer->style().visibility() != Visibility::Visible && !m_behaviors.contains(TextIteratorBehavior::IgnoresStyleVisibility))
return false;
int rendererTextLength = rendererText.length();
int end = (textNode.ptr() == m_endContainer) ? m_endOffset : INT_MAX;
int runEnd = std::min(rendererTextLength, end);
if (runStart >= runEnd)
return true;
emitText(textNode, renderer, runStart, runEnd);
return true;
}
std::tie(m_textRun, m_textRunLogicalOrderCache) = InlineIterator::firstTextBoxInLogicalOrderFor(renderer.get());
if (CheckedPtr renderTextFragment = dynamicDowncast<RenderTextFragment>(renderer.get()); renderTextFragment && !m_handledFirstLetter && !m_offset)
handleTextNodeFirstLetter(*renderTextFragment);
else if (!m_textRun && rendererText.length()) {
if (renderer->style().visibility() != Visibility::Visible && !m_behaviors.contains(TextIteratorBehavior::IgnoresStyleVisibility))
return false;
m_lastTextNodeEndedWithCollapsedSpace = true; // entire block is collapsed space
return true;
}
handleTextRun();
return true;
}
void TextIterator::handleTextRun()
{
Ref textNode = downcast<Text>(protectedCurrentNode().releaseNonNull());
CheckedRef renderer = m_firstLetterText ? *m_firstLetterText : *textNode->renderer();
if (renderer->style().visibility() != Visibility::Visible && !m_behaviors.contains(TextIteratorBehavior::IgnoresStyleVisibility)) {
m_textRun = { };
return;
}
auto [firstTextRun, orderCache] = InlineIterator::firstTextBoxInLogicalOrderFor(renderer);
auto rendererText = rendererTextForBehavior(renderer.get());
unsigned rangeStart = m_offset;
auto rangeEnd = std::optional<unsigned> { };
if (textNode.ptr() == m_endContainer)
rangeEnd = m_endOffset;
while (m_textRun) {
auto textRunStart = m_textRun->start();
auto textRunEnd = textRunStart + m_textRun->length();
auto runStart = std::max(textRunStart, rangeStart);
auto runEnd = std::min(textRunEnd, rangeEnd.value_or(textRunEnd));
// Check if we need to emit (previously) collapsed whitespace at the start of this run.
auto isAfterRangeEnd = rangeEnd ? runStart > *rangeEnd : false;
auto hasPrecedingCollapsedWhitespace = m_lastTextNodeEndedWithCollapsedSpace || (m_textRun == firstTextRun && textRunStart == runStart && runStart);
auto shouldEmitWhitespace = !isAfterRangeEnd && hasPrecedingCollapsedWhitespace && m_lastCharacter && !renderer->style().isCollapsibleWhiteSpace(m_lastCharacter);
if (shouldEmitWhitespace) {
if (m_lastTextNode == textNode.ptr() && runStart && renderer->style().isCollapsibleWhiteSpace(rendererText[runStart - 1])) {
unsigned spaceRunStart = runStart - 1;
while (spaceRunStart && renderer->style().isCollapsibleWhiteSpace(rendererText[spaceRunStart - 1]))
--spaceRunStart;
emitCharacter(' ', WTFMove(textNode), nullptr, spaceRunStart, spaceRunStart + 1);
} else
emitCharacter(' ', WTFMove(textNode), nullptr, runStart, runStart);
return;
}
// Determine what the next text run will be, but don't advance yet
auto nextTextRun = InlineIterator::nextTextBoxInLogicalOrder(m_textRun, m_textRunLogicalOrderCache);
if (runStart < runEnd) {
auto isNewlineOrTab = [&](UChar character) {
return character == '\n' || character == '\t';
};
// Handle either a single newline or tab character (which becomes a space),
// or a run of characters that does not include newlines or tabs.
// This effectively translates newlines and tabs to spaces without copying the text.
if (isNewlineOrTab(rendererText[runStart])) {
emitCharacter(' ', textNode.copyRef(), nullptr, runStart, runStart + 1);
m_offset = runStart + 1;
} else {
auto subrunEnd = runStart + 1;
for (; subrunEnd < runEnd; ++subrunEnd) {
if (isNewlineOrTab(rendererText[subrunEnd]))
break;
}
if (subrunEnd == runEnd && m_behaviors.contains(TextIteratorBehavior::BehavesAsIfNodesFollowing)) {
bool lastSpaceCollapsedByNextNonTextRun = !nextTextRun && rendererText.length() > subrunEnd && rendererText[subrunEnd] == ' ';
if (lastSpaceCollapsedByNextNonTextRun)
++subrunEnd; // runEnd stopped before last space. Increment by one to restore the space.
}
m_offset = subrunEnd;
emitText(textNode, renderer, runStart, subrunEnd);
}
// If we are doing a subrun that doesn't go to the end of the text box,
// come back again to finish handling this text box; don't advance to the next one.
if (static_cast<unsigned>(m_positionEndOffset) < textRunEnd)
return;
// Advance and return
unsigned nextRunStart = nextTextRun ? nextTextRun->start() : rendererText.length();
if (nextRunStart > runEnd)
m_lastTextNodeEndedWithCollapsedSpace = true; // collapsed space between runs or at the end
m_textRun = nextTextRun;
return;
}
// Advance and continue
m_textRun = nextTextRun;
}
if (!m_textRun && m_remainingTextRun) {
revertToRemainingTextRun();
handleTextRun();
}
}
void TextIterator::revertToRemainingTextRun()
{
ASSERT(!m_textRun && m_remainingTextRun);
m_textRun = m_remainingTextRun;
m_textRunLogicalOrderCache = std::exchange(m_remainingTextRunLogicalOrderCache, { });
m_remainingTextRun = { };
m_firstLetterText = { };
m_offset = 0;
}
static inline RenderText* firstRenderTextInFirstLetter(RenderBoxModelObject* firstLetter)
{
if (!firstLetter)
return nullptr;
// FIXME: Should this check descendent objects?
return childrenOfType<RenderText>(*firstLetter).first();
}
void TextIterator::handleTextNodeFirstLetter(RenderTextFragment& renderer)
{
if (CheckedPtr firstLetter = renderer.firstLetter()) {
if (firstLetter->style().visibility() != Visibility::Visible && !m_behaviors.contains(TextIteratorBehavior::IgnoresStyleVisibility))
return;
if (CheckedPtr firstLetterText = firstRenderTextInFirstLetter(firstLetter.get())) {
m_handledFirstLetter = true;
m_remainingTextRun = m_textRun;
m_remainingTextRunLogicalOrderCache = std::exchange(m_textRunLogicalOrderCache, { });
std::tie(m_textRun, m_textRunLogicalOrderCache) = InlineIterator::firstTextBoxInLogicalOrderFor(*firstLetterText);
m_firstLetterText = firstLetterText.get();
}
}
m_handledFirstLetter = true;
}
bool TextIterator::handleReplacedElement()
{
if (m_fullyClippedStack.top())
return false;
CheckedRef renderer = *m_currentNode->renderer();
if (renderer->style().visibility() != Visibility::Visible && !m_behaviors.contains(TextIteratorBehavior::IgnoresStyleVisibility))
return false;
if (m_lastTextNodeEndedWithCollapsedSpace) {
emitCharacter(' ', m_lastTextNode->protectedParentNode(), m_lastTextNode.copyRef(), 1, 1);
return false;
}
if (CheckedPtr renderTextControl = dynamicDowncast<RenderTextControl>(renderer.get()); renderTextControl && m_behaviors.contains(TextIteratorBehavior::EntersTextControls)) {
if (auto innerTextElement = renderTextControl->textFormControlElement().innerTextElement()) {
m_currentNode = innerTextElement->containingShadowRoot();
pushFullyClippedState(m_fullyClippedStack, *protectedCurrentNode());
m_offset = 0;
return false;
}
}
RefPtr currentElement = dynamicDowncast<HTMLElement>(m_currentNode.get());
if (m_behaviors.contains(TextIteratorBehavior::EntersImageOverlays) && currentElement && ImageOverlay::hasOverlay(*currentElement)) {
if (RefPtr shadowRoot = m_currentNode->shadowRoot()) {
m_currentNode = WTFMove(shadowRoot);
pushFullyClippedState(m_fullyClippedStack, *protectedCurrentNode());
m_offset = 0;
return false;
}
ASSERT_NOT_REACHED();
}
m_hasEmitted = true;
auto shouldEmitObjectReplacementCharacter = [&] {
if (m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharacters))
return true;
if (m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharactersForImages) && is<HTMLImageElement>(m_currentNode.get()))
return true;
#if ENABLE(ATTACHMENT_ELEMENT)
if (m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharactersForAttachments) && is<HTMLAttachmentElement>(m_currentNode.get()))
return true;
#endif
return false;
}();
if (shouldEmitObjectReplacementCharacter) {
emitCharacter(objectReplacementCharacter, m_currentNode->protectedParentNode(), protectedCurrentNode(), 0, 1);
// Don't process subtrees for embedded objects. If the text there is required,
// it must be explicitly asked by specifying a range falling inside its boundaries.
m_handledChildren = true;
return true;
}
if (m_behaviors.contains(TextIteratorBehavior::EmitsCharactersBetweenAllVisiblePositions)) {
// We want replaced elements to behave like punctuation for boundary
// finding, and to simply take up space for the selection preservation
// code in moveParagraphs, so we use a comma.
emitCharacter(',', m_currentNode->protectedParentNode(), protectedCurrentNode(), 0, 1);
return true;
}
m_positionNode = m_currentNode->parentNode();
m_positionOffsetBaseNode = m_currentNode;
m_positionStartOffset = 0;
m_positionEndOffset = 1;
if (CheckedPtr renderImage = dynamicDowncast<RenderImage>(renderer.get()); renderImage && m_behaviors.contains(TextIteratorBehavior::EmitsImageAltText)) {
auto altText = renderImage->altText();
if (unsigned length = altText.length()) {
m_lastCharacter = altText[length - 1];
m_copyableText.set(WTFMove(altText));
m_text = m_copyableText.text();
return true;
}
}
m_copyableText.reset();
m_text = StringView();
m_lastCharacter = 0;
return true;
}
static bool shouldEmitTabBeforeNode(Node& node)
{
CheckedPtr cell = dynamicDowncast<RenderTableCell>(node.renderer());
// Table cells are delimited by tabs.
if (!cell)
return false;
// Want a tab before every cell other than the first one.
CheckedPtr table = cell->table();
return table && (table->cellBefore(cell.get()) || table->cellAbove(cell.get()));
}
static bool shouldEmitNewlineForNode(Node* node, bool emitsOriginalText)
{
CheckedPtr renderer = node->renderer();
if (!(renderer ? renderer->isBR() : node->hasTagName(brTag)))
return false;
return emitsOriginalText || !(node->isInShadowTree() && is<HTMLInputElement>(*node->shadowHost()));
}
static bool hasHeaderTag(HTMLElement& element)
{
return element.hasTagName(h1Tag)
|| element.hasTagName(h2Tag)
|| element.hasTagName(h3Tag)
|| element.hasTagName(h4Tag)
|| element.hasTagName(h5Tag)
|| element.hasTagName(h6Tag);
}
static bool shouldEmitReplacementInsteadOfNode(const Node& node)
{
// Placeholders should eventually disappear, so treating them as a line break doesn't make sense
// as when they are removed the text after it is combined with the text before it.
return is<TextPlaceholderElement>(node);
}
bool shouldEmitNewlinesBeforeAndAfterNode(Node& node)
{
// Block flow (versus inline flow) is represented by having
// a newline both before and after the element.
CheckedPtr renderer = node.renderer();
if (!renderer) {
if (hasDisplayContents(node))
return false;
RefPtr element = dynamicDowncast<HTMLElement>(node);
return element && (hasHeaderTag(*element)
|| element->hasTagName(blockquoteTag)
|| element->hasTagName(ddTag)
|| element->hasTagName(divTag)
|| element->hasTagName(dlTag)
|| element->hasTagName(dtTag)
|| element->hasTagName(hrTag)
|| element->hasTagName(liTag)
|| element->hasTagName(listingTag)
|| element->hasTagName(olTag)
|| element->hasTagName(pTag)
|| element->hasTagName(preTag)
|| element->hasTagName(trTag)
|| element->hasTagName(ulTag));
}
// Need to make an exception for table cells, because they are blocks, but we
// want them tab-delimited rather than having newlines before and after.
if (isTableCell(node))
return false;
// Need to make an exception for table row elements, because they are neither
// "inline" or "RenderBlock", but we want newlines for them.
if (CheckedPtr tableRow = dynamicDowncast<RenderTableRow>(*renderer)) {
CheckedPtr table = tableRow->table();
if (table && !table->isInline())
return true;
}
if (shouldEmitReplacementInsteadOfNode(node))
return false;
return !renderer->isInline()
&& is<RenderBlock>(*renderer)
&& !renderer->isFloatingOrOutOfFlowPositioned()
&& !renderer->isBody();
}
static bool shouldEmitNewlineAfterNode(Node& node, bool emitsCharactersBetweenAllVisiblePositions = false)
{
// FIXME: It should be better but slower to create a VisiblePosition here.
if (!shouldEmitNewlinesBeforeAndAfterNode(node))
return false;
// Don't emit a new line at the end of the document unless we're matching the behavior of VisiblePosition.
if (emitsCharactersBetweenAllVisiblePositions)
return true;
RefPtr subsequentNode = &node;
while ((subsequentNode = NodeTraversal::nextSkippingChildren(*subsequentNode))) {
if (subsequentNode->renderer())
return true;
}
return false;
}
static bool shouldEmitNewlineBeforeNode(Node& node)
{
return shouldEmitNewlinesBeforeAndAfterNode(node);
}
static bool shouldEmitExtraNewlineForNode(Node& node)
{
// When there is a significant collapsed bottom margin, emit an extra
// newline for a more realistic result. We end up getting the right
// result even without margin collapsing. For example: <div><p>text</p></div>
// will work right even if both the <div> and the <p> have bottom margins.
CheckedPtr renderBox = dynamicDowncast<RenderBox>(node.renderer());
if (!renderBox || !renderBox->height())
return false;
// NOTE: We only do this for a select set of nodes, and WinIE appears not to do this at all.
RefPtr element = dynamicDowncast<HTMLElement>(node);
if (!element || (!hasHeaderTag(*element) && !is<HTMLParagraphElement>(*element)))
return false;
auto bottomMargin = renderBox->collapsedMarginAfter();
auto fontSize = renderBox->style().fontDescription().computedSize();
return bottomMargin * 2 >= fontSize;
}
static int collapsedSpaceLength(RenderText& renderer, int textEnd)
{
auto text = renderer.text();
unsigned length = text.length();
for (unsigned i = textEnd; i < length; ++i) {
if (!renderer.style().isCollapsibleWhiteSpace(text[i]))
return i - textEnd;
}
return length - textEnd;
}
static int maxOffsetIncludingCollapsedSpaces(Node& node)
{
int offset = caretMaxOffset(node);
if (CheckedPtr renderText = dynamicDowncast<RenderText>(node.renderer()))
offset += collapsedSpaceLength(*renderText, offset);
return offset;
}
// Whether or not we should emit a character as we enter m_currentNode (if it's a container) or as we hit it (if it's atomic).
bool TextIterator::shouldRepresentNodeOffsetZero()
{
if (m_behaviors.contains(TextIteratorBehavior::EmitsCharactersBetweenAllVisiblePositions)) {
if (CheckedPtr renderer = m_currentNode->renderer(); renderer && renderer->isRenderTable())
return true;
}
// Leave element positioned flush with start of a paragraph
// (e.g. do not insert tab before a table cell at the start of a paragraph)
if (m_lastCharacter == '\n')
return false;
// Otherwise, show the position if we have emitted any characters
if (m_hasEmitted)
return true;
// We've not emitted anything yet. Generally, there is no need for any positioning then.
// The only exception is when the element is visually not in the same line as
// the start of the range (e.g. the range starts at the end of the previous paragraph).
// NOTE: Creating VisiblePositions and comparing them is relatively expensive, so we
// make quicker checks to possibly avoid that. Another check that we could make is
// is whether the inline vs block flow changed since the previous visible element.
// I think we're already in a special enough case that that won't be needed, tho.
// No character needed if this is the first node in the range.
if (m_currentNode == m_startContainer)
return false;
// If we are outside the start container's subtree, assume we need to emit.
// FIXME: m_startContainer could be an inline block
Ref currentNode = *m_currentNode;
if (!currentNode->isDescendantOf(m_startContainer.get()))
return true;
// If we started as m_startContainer offset 0 and the current node is a descendant of
// the start container, we already had enough context to correctly decide whether to
// emit after a preceding block. We chose not to emit (m_hasEmitted is false),
// so don't second guess that now.
// NOTE: Is this really correct when m_currentNode is not a leftmost descendant? Probably
// immaterial since we likely would have already emitted something by now.
if (m_startOffset == 0)
return false;
// If this node is unrendered or invisible the VisiblePosition checks below won't have much meaning.
// Additionally, if the range we are iterating over contains huge sections of unrendered content,
// we would create VisiblePositions on every call to this function without this check.
if (!currentNode->renderer() || currentNode->renderer()->style().visibility() != Visibility::Visible)
return false;
if (CheckedPtr renderBlockFlow = dynamicDowncast<RenderBlockFlow>(*currentNode->renderer())) {
if (!renderBlockFlow->height() && !is<HTMLBodyElement>(currentNode))
return false;
}
// The startPos.isNotNull() check is needed because the start could be before the body,
// and in that case we'll get null. We don't want to put in newlines at the start in that case.
// The currPos.isNotNull() check is needed because positions in non-HTML content
// (like SVG) do not have visible positions, and we don't want to emit for them either.
VisiblePosition startPos = VisiblePosition(Position(protectedStartContainer(), m_startOffset, Position::PositionIsOffsetInAnchor));
VisiblePosition currPos = VisiblePosition(positionBeforeNode(currentNode.ptr()));
return startPos.isNotNull() && currPos.isNotNull() && !inSameLine(startPos, currPos);
}
bool TextIterator::shouldEmitSpaceBeforeAndAfterNode(Node& node)
{
return node.renderer() && node.renderer()->isRenderTable() && (node.renderer()->isInline() || m_behaviors.contains(TextIteratorBehavior::EmitsCharactersBetweenAllVisiblePositions));
}
void TextIterator::representNodeOffsetZero()
{
// Emit a character to show the positioning of m_currentNode.
// When we haven't been emitting any characters, shouldRepresentNodeOffsetZero() can
// create VisiblePositions, which is expensive. So, we perform the inexpensive checks
// on m_currentNode to see if it necessitates emitting a character first and will early return
// before encountering shouldRepresentNodeOffsetZero()s worse case behavior.
auto currentNode = protectedCurrentNode();
if (shouldEmitTabBeforeNode(*currentNode)) {
if (shouldRepresentNodeOffsetZero()) {
auto parentNode = currentNode->protectedParentNode();
emitCharacter('\t', WTFMove(parentNode), WTFMove(currentNode), 0, 0);
}
} else if (shouldEmitNewlineBeforeNode(*currentNode)) {
if (shouldRepresentNodeOffsetZero()) {
auto parentNode = currentNode->protectedParentNode();
emitCharacter('\n', WTFMove(parentNode), WTFMove(currentNode), 0, 0);
}
} else if (shouldEmitSpaceBeforeAndAfterNode(*currentNode)) {
if (shouldRepresentNodeOffsetZero()) {
auto parentNode = currentNode->protectedParentNode();
emitCharacter(' ', WTFMove(parentNode), WTFMove(currentNode), 0, 0);
}
} else if (shouldEmitReplacementInsteadOfNode(*currentNode)) {
if (shouldRepresentNodeOffsetZero()) {
auto parentNode = currentNode->protectedParentNode();
emitCharacter(objectReplacementCharacter, WTFMove(parentNode), WTFMove(currentNode), 0, 0);
}
}
}
bool TextIterator::handleNonTextNode()
{
auto currentNode = protectedCurrentNode();
if (shouldEmitNewlineForNode(currentNode.get(), m_behaviors.contains(TextIteratorBehavior::EmitsOriginalText))) {
auto parentNode = currentNode->protectedParentNode();
emitCharacter('\n', WTFMove(parentNode), WTFMove(currentNode), 0, 1);
} else if (m_behaviors.contains(TextIteratorBehavior::EmitsCharactersBetweenAllVisiblePositions) && currentNode->renderer() && currentNode->renderer()->isHR()) {
auto parentNode = currentNode->protectedParentNode();
emitCharacter(' ', WTFMove(parentNode), WTFMove(currentNode), 0, 1);
} else
representNodeOffsetZero();
return true;
}
void TextIterator::exitNode(Node* exitedNode)
{
// prevent emitting a newline when exiting a collapsed block at beginning of the range
// FIXME: !m_hasEmitted does not necessarily mean there was a collapsed block... it could
// have been an hr (e.g.). Also, a collapsed block could have height (e.g. a table) and
// therefore look like a blank line.
if (!m_hasEmitted)
return;
// Emit with a position *inside* m_currentNode, after m_currentNode's contents, in
// case it is a block, because the run should start where the
// emitted character is positioned visually.
RefPtr baseNode = exitedNode;
// FIXME: This shouldn't require the m_lastTextNode to be true, but we can't change that without making
// the logic in _web_attributedStringFromRange match. We'll get that for free when we switch to use
// TextIterator in _web_attributedStringFromRange.
// See <rdar://problem/5428427> for an example of how this mismatch will cause problems.
if (m_lastTextNode && shouldEmitNewlineAfterNode(*protectedCurrentNode(), m_behaviors.contains(TextIteratorBehavior::EmitsCharactersBetweenAllVisiblePositions))) {
// use extra newline to represent margin bottom, as needed
bool addNewline = shouldEmitExtraNewlineForNode(*protectedCurrentNode());
// FIXME: We need to emit a '\n' as we leave an empty block(s) that
// contain a VisiblePosition when doing selection preservation.
if (m_lastCharacter != '\n') {
// insert a newline with a position following this block's contents.
emitCharacter('\n', baseNode->protectedParentNode(), baseNode.copyRef(), 1, 1);
// remember whether to later add a newline for the current node
ASSERT(!m_nodeForAdditionalNewline);
if (addNewline)
m_nodeForAdditionalNewline = baseNode.get();
} else if (addNewline)
// insert a newline with a position following this block's contents.
emitCharacter('\n', baseNode->protectedParentNode(), baseNode.copyRef(), 1, 1);
}
// If nothing was emitted, see if we need to emit a space.
if (!m_positionNode && shouldEmitSpaceBeforeAndAfterNode(*protectedCurrentNode())) {
auto parentNode = baseNode->protectedParentNode();
emitCharacter(' ', WTFMove(parentNode), WTFMove(baseNode), 1, 1);
}
}
void TextIterator::emitCharacter(UChar character, RefPtr<Node>&& characterNode, RefPtr<Node>&& offsetBaseNode, int textStartOffset, int textEndOffset)
{
ASSERT(characterNode);
m_hasEmitted = true;
// remember information with which to construct the TextIterator::range()
m_positionNode = WTFMove(characterNode);
m_positionOffsetBaseNode = WTFMove(offsetBaseNode);
m_positionStartOffset = textStartOffset;
m_positionEndOffset = textEndOffset;
m_copyableText.set(character);
m_text = m_copyableText.text();
m_lastCharacter = character;
m_lastTextNodeEndedWithCollapsedSpace = false;
}
void TextIterator::emitText(Text& textNode, RenderText& renderer, int textStartOffset, int textEndOffset)
{
ASSERT(textStartOffset >= 0);
ASSERT(textEndOffset >= 0);
ASSERT(textStartOffset <= textEndOffset);
bool shouldIgnoreFullSizeKana = m_behaviors.contains(TextIteratorBehavior::IgnoresFullSizeKana) && renderer.style().textTransform().contains(TextTransform::FullSizeKana);
// FIXME: This probably yields the wrong offsets when text-transform: lowercase turns a single character into two characters.
String string = m_behaviors.contains(TextIteratorBehavior::EmitsOriginalText) || shouldIgnoreFullSizeKana ? renderer.originalText()
: (m_behaviors.contains(TextIteratorBehavior::EmitsTextsWithoutTranscoding) ? renderer.textWithoutConvertingBackslashToYenSymbol() : renderer.text());
ASSERT(m_behaviors.contains(TextIteratorBehavior::EmitsOriginalText) || string.length() >= static_cast<unsigned>(textEndOffset));
textEndOffset = std::min(string.length(), static_cast<unsigned>(textEndOffset));
m_positionNode = &textNode;
m_positionOffsetBaseNode = nullptr;
m_positionStartOffset = textStartOffset;
m_positionEndOffset = textEndOffset;
m_lastCharacter = string[textEndOffset - 1];
m_copyableText.set(WTFMove(string), textStartOffset, textEndOffset - textStartOffset);
m_text = m_copyableText.text();
m_lastTextNodeEndedWithCollapsedSpace = false;
m_hasEmitted = true;
}
SimpleRange TextIterator::range() const
{
ASSERT(!atEnd());
// Use the current run information, if we have it.
if (m_positionOffsetBaseNode) {
unsigned index = m_positionOffsetBaseNode->computeNodeIndex();
m_positionStartOffset += index;
m_positionEndOffset += index;
m_positionOffsetBaseNode = nullptr;
}
return { { *m_positionNode, static_cast<unsigned>(m_positionStartOffset) }, { *m_positionNode, static_cast<unsigned>(m_positionEndOffset) } };
}
Node* TextIterator::node() const
{
auto start = this->range().start;
if (start.container->isCharacterDataNode())
return start.container.ptr();
return start.container->traverseToChildAt(start.offset);
}
RefPtr<Node> TextIterator::protectedCurrentNode() const
{
return m_currentNode;
}
#if ENABLE(TREE_DEBUGGING)
void TextIterator::showTreeForThis() const
{
if (m_currentNode)
m_currentNode->showTreeForThis();
fprintf(stderr, "offset: %d\n", m_offset);
}
#endif
// --------
SimplifiedBackwardsTextIterator::SimplifiedBackwardsTextIterator(const SimpleRange& range)
{
range.start.protectedDocument()->updateLayoutIgnorePendingStylesheets();
RefPtr startNode = range.start.container.ptr();
RefPtr endNode = range.end.container.ptr();
unsigned startOffset = range.start.offset;
unsigned endOffset = range.end.offset;
if (!startNode->isCharacterDataNode()) {
if (startOffset < startNode->countChildNodes()) {
startNode = startNode->traverseToChildAt(startOffset);
startOffset = 0;
}
}
if (!endNode->isCharacterDataNode()) {
if (endOffset > 0 && endOffset <= endNode->countChildNodes()) {
endNode = endNode->traverseToChildAt(endOffset - 1);
endOffset = endNode->length();
}
}
m_node = endNode;
setUpFullyClippedStack(m_fullyClippedStack, *m_node);
m_offset = endOffset;
m_handledNode = false;
m_handledChildren = endOffset == 0;
m_startContainer = WTFMove(startNode);
m_startOffset = startOffset;
m_endContainer = endNode;
m_endOffset = endOffset;
m_positionNode = WTFMove(endNode);
m_lastTextNode = nullptr;
m_lastCharacter = '\n';
m_havePassedStartContainer = false;
advance();
}
void SimplifiedBackwardsTextIterator::advance()
{
ASSERT(!atEnd());
m_positionNode = nullptr;
m_copyableText.reset();
m_text = StringView();
while (m_node && !m_havePassedStartContainer) {
// Don't handle node if we start iterating at [node, 0].
if (!m_handledNode && !(m_node == m_endContainer && !m_endOffset)) {
CheckedPtr renderer = m_node->renderer();
if (renderer && renderer->isRenderText() && m_node->isTextNode()) {
if (renderer->style().visibility() == Visibility::Visible && m_offset > 0)
m_handledNode = handleTextNode();
} else if (isRendererReplacedElement(renderer.get(), m_behaviors)) {
if (renderer->style().visibility() == Visibility::Visible && m_offset > 0)
m_handledNode = handleReplacedElement();
} else
m_handledNode = handleNonTextNode();
if (m_positionNode)
return;
}
if (!m_handledChildren && m_node->hasChildNodes()) {
m_node = m_node->lastChild();
pushFullyClippedState(m_fullyClippedStack, *protectedNode());
} else {
// Exit empty containers as we pass over them or containers
// where [container, 0] is where we started iterating.
if (!m_handledNode && canHaveChildrenForEditing(*protectedNode()) && m_node->parentNode() && (!m_node->lastChild() || (m_node == m_endContainer && !m_endOffset))) {
exitNode();
if (m_positionNode) {
m_handledNode = true;
m_handledChildren = true;
return;
}
}
// Exit all other containers.
while (!m_node->previousSibling()) {
if (!advanceRespectingRange(m_node->protectedParentOrShadowHostNode().get()))
break;
m_fullyClippedStack.pop();
exitNode();
if (m_positionNode) {
m_handledNode = true;
m_handledChildren = true;
return;
}
}
m_fullyClippedStack.pop();
if (advanceRespectingRange(m_node->protectedPreviousSibling().get()))
pushFullyClippedState(m_fullyClippedStack, *protectedNode());
else
m_node = nullptr;
}
// For the purpose of word boundary detection,
// we should iterate all visible text and trailing (collapsed) whitespaces.
m_offset = m_node ? maxOffsetIncludingCollapsedSpaces(*protectedNode()) : 0;
m_handledNode = false;
m_handledChildren = false;
if (m_positionNode)
return;
}
}
bool SimplifiedBackwardsTextIterator::handleTextNode()
{
m_lastTextNode = downcast<Text>(m_node.get());
int startOffset;
int offsetInNode;
CheckedPtr renderer = handleFirstLetter(startOffset, offsetInNode);
if (!renderer)
return true;
String text = renderer->text();
if (!renderer->hasRenderedText() && text.length())
return true;
if (startOffset + offsetInNode == m_offset) {
ASSERT(!m_shouldHandleFirstLetter);
return true;
}
m_positionEndOffset = m_offset;
m_offset = startOffset + offsetInNode;
m_positionNode = m_node;
m_positionStartOffset = m_offset;
ASSERT(m_positionStartOffset < m_positionEndOffset);
ASSERT(m_positionStartOffset - offsetInNode >= 0);
ASSERT(m_positionEndOffset - offsetInNode > 0);
ASSERT(m_positionEndOffset - offsetInNode <= static_cast<int>(text.length()));
m_lastCharacter = text[m_positionEndOffset - offsetInNode - 1];
m_copyableText.set(WTFMove(text), m_positionStartOffset - offsetInNode, m_positionEndOffset - m_positionStartOffset);
m_text = m_copyableText.text();
return !m_shouldHandleFirstLetter;
}
RenderText* SimplifiedBackwardsTextIterator::handleFirstLetter(int& startOffset, int& offsetInNode)
{
CheckedRef renderer = downcast<RenderText>(*m_node->renderer());
startOffset = (m_node == m_startContainer) ? m_startOffset : 0;
CheckedPtr fragment = dynamicDowncast<RenderTextFragment>(renderer.get());
if (!fragment) {
offsetInNode = 0;
return renderer.ptr();
}
int offsetAfterFirstLetter = fragment->start();
if (startOffset >= offsetAfterFirstLetter) {
ASSERT(!m_shouldHandleFirstLetter);
offsetInNode = offsetAfterFirstLetter;
return renderer.ptr();
}
if (!m_shouldHandleFirstLetter && startOffset + offsetAfterFirstLetter < m_offset) {
m_shouldHandleFirstLetter = true;
offsetInNode = offsetAfterFirstLetter;
return renderer.ptr();
}
m_shouldHandleFirstLetter = false;
offsetInNode = 0;
CheckedPtr firstLetterRenderer = firstRenderTextInFirstLetter(fragment->firstLetter());
m_offset = firstLetterRenderer->caretMaxOffset();
m_offset += collapsedSpaceLength(*firstLetterRenderer, m_offset);
return firstLetterRenderer.get();
}
bool SimplifiedBackwardsTextIterator::handleReplacedElement()
{
unsigned index = m_node->computeNodeIndex();
// We want replaced elements to behave like punctuation for boundary
// finding, and to simply take up space for the selection preservation
// code in moveParagraphs, so we use a comma. Unconditionally emit
// here because this iterator is only used for boundary finding.
emitCharacter(',', m_node->protectedParentNode(), index, index + 1);
return true;
}
bool SimplifiedBackwardsTextIterator::handleNonTextNode()
{
auto currentNode = protectedNode();
if (shouldEmitTabBeforeNode(*currentNode)) {
unsigned index = currentNode->computeNodeIndex();
emitCharacter('\t', currentNode->protectedParentNode(), index + 1, index + 1);
} else if (shouldEmitNewlineForNode(currentNode.get(), m_behaviors.contains(TextIteratorBehavior::EmitsOriginalText)) || shouldEmitNewlineAfterNode(*m_node)) {
if (m_lastCharacter != '\n') {
// Corresponds to the same check in TextIterator::exitNode.
unsigned index = currentNode->computeNodeIndex();
// The start of this emitted range is wrong. Ensuring correctness would require
// VisiblePositions and so would be slow. previousBoundary expects this.
emitCharacter('\n', currentNode->protectedParentNode(), index + 1, index + 1);
}
}
return true;
}
void SimplifiedBackwardsTextIterator::exitNode()
{
auto node = protectedNode();
if (shouldEmitTabBeforeNode(*node))
emitCharacter('\t', WTFMove(node), 0, 0);
else if (shouldEmitNewlineForNode(node.get(), m_behaviors.contains(TextIteratorBehavior::EmitsOriginalText)) || shouldEmitNewlineBeforeNode(*m_node)) {
// The start of this emitted range is wrong. Ensuring correctness would require
// VisiblePositions and so would be slow. previousBoundary expects this.
emitCharacter('\n', WTFMove(node), 0, 0);
}
}
void SimplifiedBackwardsTextIterator::emitCharacter(UChar c, RefPtr<Node>&& node, int startOffset, int endOffset)
{
ASSERT(node);
m_positionNode = WTFMove(node);
m_positionStartOffset = startOffset;
m_positionEndOffset = endOffset;
m_copyableText.set(c);
m_text = m_copyableText.text();
m_lastCharacter = c;
}
bool SimplifiedBackwardsTextIterator::advanceRespectingRange(Node* next)
{
if (!next)
return false;
m_havePassedStartContainer |= m_node == m_startContainer;
if (m_havePassedStartContainer)
return false;
m_node = next;
return true;
}
SimpleRange SimplifiedBackwardsTextIterator::range() const
{
ASSERT(!atEnd());
Ref positionNode = *m_positionNode;
return { { positionNode.copyRef(), static_cast<unsigned>(m_positionStartOffset) }, { positionNode.copyRef(), static_cast<unsigned>(m_positionEndOffset) } };
}
// --------
CharacterIterator::CharacterIterator(const SimpleRange& range, TextIteratorBehaviors behaviors)
: m_underlyingIterator(range, behaviors)
{
while (!atEnd() && !m_underlyingIterator.text().length())
m_underlyingIterator.advance();
}
SimpleRange CharacterIterator::range() const
{
SimpleRange range = m_underlyingIterator.range();
if (!m_underlyingIterator.atEnd()) {
if (m_underlyingIterator.text().length() <= 1)
ASSERT(m_runOffset == 0);
else {
unsigned offset = range.startOffset() + m_runOffset;
range = { { range.start.container.copyRef(), offset }, { range.start.container.copyRef(), offset + 1 } };
}
}
return range;
}
void CharacterIterator::advance(int count)
{
if (count <= 0) {
ASSERT(count == 0);
return;
}
m_atBreak = false;
// easy if there is enough left in the current m_underlyingIterator run
int remaining = m_underlyingIterator.text().length() - m_runOffset;
if (count < remaining) {
m_runOffset += count;
m_offset += count;
return;
}
// exhaust the current m_underlyingIterator run
count -= remaining;
m_offset += remaining;
// move to a subsequent m_underlyingIterator run
for (m_underlyingIterator.advance(); !atEnd(); m_underlyingIterator.advance()) {
int runLength = m_underlyingIterator.text().length();
if (!runLength)
m_atBreak = true;
else {
// see whether this is m_underlyingIterator to use
if (count < runLength) {
m_runOffset = count;
m_offset += count;
return;
}
// exhaust this m_underlyingIterator run
count -= runLength;
m_offset += runLength;
}
}
// ran to the end of the m_underlyingIterator... no more runs left
m_atBreak = true;
m_runOffset = 0;
}
BackwardsCharacterIterator::BackwardsCharacterIterator(const SimpleRange& range)
: m_underlyingIterator(range)
{
while (!atEnd() && !m_underlyingIterator.text().length())
m_underlyingIterator.advance();
}
SimpleRange BackwardsCharacterIterator::range() const
{
auto range = m_underlyingIterator.range();
if (!m_underlyingIterator.atEnd()) {
if (m_underlyingIterator.text().length() <= 1)
ASSERT(m_runOffset == 0);
else {
unsigned offset = range.end.offset - m_runOffset;
range = { { range.start.container.copyRef(), offset - 1 }, { range.start.container.copyRef(), offset } };
}
}
return range;
}
void BackwardsCharacterIterator::advance(int count)
{
if (count <= 0) {
ASSERT(!count);
return;
}
m_atBreak = false;
int remaining = m_underlyingIterator.text().length() - m_runOffset;
if (count < remaining) {
m_runOffset += count;
m_offset += count;
return;
}
count -= remaining;
m_offset += remaining;
for (m_underlyingIterator.advance(); !atEnd(); m_underlyingIterator.advance()) {
int runLength = m_underlyingIterator.text().length();
if (runLength == 0)
m_atBreak = true;
else {
if (count < runLength) {
m_runOffset = count;
m_offset += count;
return;
}
count -= runLength;
m_offset += runLength;
}
}
m_atBreak = true;
m_runOffset = 0;
}
// --------
WordAwareIterator::WordAwareIterator(const SimpleRange& range)
: m_underlyingIterator(range)
{
advance(); // get in position over the first chunk of text
}
// We're always in one of these modes:
// - The current chunk in the text iterator is our current chunk
// (typically its a piece of whitespace, or text that ended with whitespace)
// - The previous chunk in the text iterator is our current chunk
// (we looked ahead to the next chunk and found a word boundary)
// - We built up our own chunk of text from many chunks from the text iterator
// FIXME: Performance could be bad for huge spans next to each other that don't fall on word boundaries.
void WordAwareIterator::advance()
{
m_previousText.reset();
m_buffer.clear();
// If last time we did a look-ahead, start with that looked-ahead chunk now
if (!m_didLookAhead) {
ASSERT(!m_underlyingIterator.atEnd());
m_underlyingIterator.advance();
}
m_didLookAhead = false;
// Go to next non-empty chunk
while (!m_underlyingIterator.atEnd() && !m_underlyingIterator.text().length())
m_underlyingIterator.advance();
if (m_underlyingIterator.atEnd())
return;
while (1) {
// If this chunk ends in whitespace we can just use it as our chunk.
if (deprecatedIsSpaceOrNewline(m_underlyingIterator.text()[m_underlyingIterator.text().length() - 1]))
return;
// If this is the first chunk that failed, save it in previousText before look ahead
if (m_buffer.isEmpty())
m_previousText = m_underlyingIterator.copyableText();
// Look ahead to next chunk. If it is whitespace or a break, we can use the previous stuff
m_underlyingIterator.advance();
if (m_underlyingIterator.atEnd() || !m_underlyingIterator.text().length() || deprecatedIsSpaceOrNewline(m_underlyingIterator.text()[0])) {
m_didLookAhead = true;
return;
}
if (m_buffer.isEmpty()) {
// Start gobbling chunks until we get to a suitable stopping point.
append(m_buffer, m_previousText.text());
m_previousText.reset();
}
append(m_buffer, m_underlyingIterator.text());
}
}
StringView WordAwareIterator::text() const
{
if (!m_buffer.isEmpty())
return m_buffer.span();
if (m_previousText.text().length())
return m_previousText.text();
return m_underlyingIterator.text();
}
// --------
static inline UChar foldQuoteMarkAndReplaceNoBreakSpace(UChar c)
{
switch (c) {
case hebrewPunctuationGershayim:
case leftDoubleQuotationMark:
case leftLowDoubleQuotationMark:
case rightDoubleQuotationMark:
case leftPointingDoubleAngleQuotationMark:
case rightPointingDoubleAngleQuotationMark:
case doubleHighReversed9QuotationMark:
case doubleLowReversed9QuotationMark:
case reversedDoublePrimeQuotationMark:
case doublePrimeQuotationMark:
case lowDoublePrimeQuotationMark:
case fullwidthQuotationMark:
return '"';
case hebrewPunctuationGeresh:
case leftSingleQuotationMark:
case leftLowSingleQuotationMark:
case rightSingleQuotationMark:
case singleLow9QuotationMark:
case singleLeftPointingAngleQuotationMark:
case singleRightPointingAngleQuotationMark:
case leftCornerBracket:
case rightCornerBracket:
case leftWhiteCornerBracket:
case rightWhiteCornerBracket:
case presentationFormForVerticalLeftCornerBracket:
case presentationFormForVerticalRightCornerBracket:
case presentationFormForVerticalLeftWhiteCornerBracket:
case presentationFormForVerticalRightWhiteCornerBracket:
case fullwidthApostrophe:
case halfwidthLeftCornerBracket:
case halfwidthRightCornerBracket:
return '\'';
case noBreakSpace:
return ' ';
default:
return c;
}
}
// FIXME: We'd like to tailor the searcher to fold quote marks for us instead
// of doing it in a separate replacement pass here, but ICU doesn't offer a way
// to add tailoring on top of the locale-specific tailoring as of this writing.
String foldQuoteMarks(const String& stringToFold)
{
String result = makeStringByReplacingAll(stringToFold, hebrewPunctuationGeresh, '\'');
result = makeStringByReplacingAll(result, hebrewPunctuationGershayim, '"');
result = makeStringByReplacingAll(result, leftDoubleQuotationMark, '"');
result = makeStringByReplacingAll(result, leftLowDoubleQuotationMark, '"');
result = makeStringByReplacingAll(result, leftSingleQuotationMark, '\'');
result = makeStringByReplacingAll(result, leftLowSingleQuotationMark, '\'');
result = makeStringByReplacingAll(result, rightDoubleQuotationMark, '"');
result = makeStringByReplacingAll(result, singleLow9QuotationMark, '\'');
result = makeStringByReplacingAll(result, singleLeftPointingAngleQuotationMark, '\'');
result = makeStringByReplacingAll(result, singleRightPointingAngleQuotationMark, '\'');
result = makeStringByReplacingAll(result, leftCornerBracket, '\'');
result = makeStringByReplacingAll(result, rightCornerBracket, '\'');
result = makeStringByReplacingAll(result, leftWhiteCornerBracket, '\'');
result = makeStringByReplacingAll(result, rightWhiteCornerBracket, '\'');
result = makeStringByReplacingAll(result, presentationFormForVerticalLeftCornerBracket, '\'');
result = makeStringByReplacingAll(result, presentationFormForVerticalRightCornerBracket, '\'');
result = makeStringByReplacingAll(result, presentationFormForVerticalLeftWhiteCornerBracket, '\'');
result = makeStringByReplacingAll(result, presentationFormForVerticalRightWhiteCornerBracket, '\'');
result = makeStringByReplacingAll(result, fullwidthApostrophe, '\'');
result = makeStringByReplacingAll(result, halfwidthLeftCornerBracket, '\'');
result = makeStringByReplacingAll(result, halfwidthRightCornerBracket, '\'');
result = makeStringByReplacingAll(result, leftPointingDoubleAngleQuotationMark, '"');
result = makeStringByReplacingAll(result, rightPointingDoubleAngleQuotationMark, '"');
result = makeStringByReplacingAll(result, doubleHighReversed9QuotationMark, '"');
result = makeStringByReplacingAll(result, doubleLowReversed9QuotationMark, '"');
result = makeStringByReplacingAll(result, reversedDoublePrimeQuotationMark, '"');
result = makeStringByReplacingAll(result, doublePrimeQuotationMark, '"');
result = makeStringByReplacingAll(result, lowDoublePrimeQuotationMark, '"');
result = makeStringByReplacingAll(result, fullwidthQuotationMark, '"');
return makeStringByReplacingAll(result, rightSingleQuotationMark, '\'');
}
#if !UCONFIG_NO_COLLATION
constexpr size_t minimumSearchBufferSize = 8192;
#ifndef NDEBUG
static bool searcherInUse;
#endif
static UStringSearch* createSearcher()
{
// Provide a non-empty pattern and non-empty text so usearch_open will not fail,
// but it doesn't matter exactly what it is, since we don't perform any searches
// without setting both the pattern and the text.
UErrorCode status = U_ZERO_ERROR;
auto searchCollatorName = makeString(unsafeSpan(currentSearchLocaleID()), "@collation=search"_s);
UStringSearch* searcher = usearch_open(&newlineCharacter, 1, &newlineCharacter, 1, searchCollatorName.utf8().data(), 0, &status);
ASSERT(U_SUCCESS(status) || status == U_USING_FALLBACK_WARNING || status == U_USING_DEFAULT_WARNING);
return searcher;
}
static UStringSearch* searcher()
{
static UStringSearch* searcher = createSearcher();
return searcher;
}
static inline void lockSearcher()
{
#ifndef NDEBUG
ASSERT(!searcherInUse);
searcherInUse = true;
#endif
}
static inline void unlockSearcher()
{
#ifndef NDEBUG
ASSERT(searcherInUse);
searcherInUse = false;
#endif
}
// ICU's search ignores the distinction between small kana letters and ones
// that are not small, and also characters that differ only in the voicing
// marks when considering only primary collation strength differences.
// This is not helpful for end users, since these differences make words
// distinct, so for our purposes we need these to be considered.
// The Unicode folks do not think the collation algorithm should be
// changed. To work around this, we would like to tailor the ICU searcher,
// but we can't get that to work yet. So instead, we check for cases where
// these differences occur, and skip those matches.
// We refer to the above technique as the "kana workaround". The next few
// functions are helper functinos for the kana workaround.
static inline bool isKanaLetter(UChar character)
{
// Hiragana letters.
if (character >= 0x3041 && character <= 0x3096)
return true;
// Katakana letters.
if (character >= 0x30A1 && character <= 0x30FA)
return true;
if (character >= 0x31F0 && character <= 0x31FF)
return true;
// Halfwidth katakana letters.
if (character >= 0xFF66 && character <= 0xFF9D && character != 0xFF70)
return true;
return false;
}
static inline bool isSmallKanaLetter(UChar character)
{
ASSERT(isKanaLetter(character));
switch (character) {
case 0x3041: // HIRAGANA LETTER SMALL A
case 0x3043: // HIRAGANA LETTER SMALL I
case 0x3045: // HIRAGANA LETTER SMALL U
case 0x3047: // HIRAGANA LETTER SMALL E
case 0x3049: // HIRAGANA LETTER SMALL O
case 0x3063: // HIRAGANA LETTER SMALL TU
case 0x3083: // HIRAGANA LETTER SMALL YA
case 0x3085: // HIRAGANA LETTER SMALL YU
case 0x3087: // HIRAGANA LETTER SMALL YO
case 0x308E: // HIRAGANA LETTER SMALL WA
case 0x3095: // HIRAGANA LETTER SMALL KA
case 0x3096: // HIRAGANA LETTER SMALL KE
case 0x30A1: // KATAKANA LETTER SMALL A
case 0x30A3: // KATAKANA LETTER SMALL I
case 0x30A5: // KATAKANA LETTER SMALL U
case 0x30A7: // KATAKANA LETTER SMALL E
case 0x30A9: // KATAKANA LETTER SMALL O
case 0x30C3: // KATAKANA LETTER SMALL TU
case 0x30E3: // KATAKANA LETTER SMALL YA
case 0x30E5: // KATAKANA LETTER SMALL YU
case 0x30E7: // KATAKANA LETTER SMALL YO
case 0x30EE: // KATAKANA LETTER SMALL WA
case 0x30F5: // KATAKANA LETTER SMALL KA
case 0x30F6: // KATAKANA LETTER SMALL KE
case 0x31F0: // KATAKANA LETTER SMALL KU
case 0x31F1: // KATAKANA LETTER SMALL SI
case 0x31F2: // KATAKANA LETTER SMALL SU
case 0x31F3: // KATAKANA LETTER SMALL TO
case 0x31F4: // KATAKANA LETTER SMALL NU
case 0x31F5: // KATAKANA LETTER SMALL HA
case 0x31F6: // KATAKANA LETTER SMALL HI
case 0x31F7: // KATAKANA LETTER SMALL HU
case 0x31F8: // KATAKANA LETTER SMALL HE
case 0x31F9: // KATAKANA LETTER SMALL HO
case 0x31FA: // KATAKANA LETTER SMALL MU
case 0x31FB: // KATAKANA LETTER SMALL RA
case 0x31FC: // KATAKANA LETTER SMALL RI
case 0x31FD: // KATAKANA LETTER SMALL RU
case 0x31FE: // KATAKANA LETTER SMALL RE
case 0x31FF: // KATAKANA LETTER SMALL RO
case 0xFF67: // HALFWIDTH KATAKANA LETTER SMALL A
case 0xFF68: // HALFWIDTH KATAKANA LETTER SMALL I
case 0xFF69: // HALFWIDTH KATAKANA LETTER SMALL U
case 0xFF6A: // HALFWIDTH KATAKANA LETTER SMALL E
case 0xFF6B: // HALFWIDTH KATAKANA LETTER SMALL O
case 0xFF6C: // HALFWIDTH KATAKANA LETTER SMALL YA
case 0xFF6D: // HALFWIDTH KATAKANA LETTER SMALL YU
case 0xFF6E: // HALFWIDTH KATAKANA LETTER SMALL YO
case 0xFF6F: // HALFWIDTH KATAKANA LETTER SMALL TU
return true;
}
return false;
}
enum VoicedSoundMarkType { NoVoicedSoundMark, VoicedSoundMark, SemiVoicedSoundMark };
static inline VoicedSoundMarkType composedVoicedSoundMark(UChar character)
{
ASSERT(isKanaLetter(character));
switch (character) {
case 0x304C: // HIRAGANA LETTER GA
case 0x304E: // HIRAGANA LETTER GI
case 0x3050: // HIRAGANA LETTER GU
case 0x3052: // HIRAGANA LETTER GE
case 0x3054: // HIRAGANA LETTER GO
case 0x3056: // HIRAGANA LETTER ZA
case 0x3058: // HIRAGANA LETTER ZI
case 0x305A: // HIRAGANA LETTER ZU
case 0x305C: // HIRAGANA LETTER ZE
case 0x305E: // HIRAGANA LETTER ZO
case 0x3060: // HIRAGANA LETTER DA
case 0x3062: // HIRAGANA LETTER DI
case 0x3065: // HIRAGANA LETTER DU
case 0x3067: // HIRAGANA LETTER DE
case 0x3069: // HIRAGANA LETTER DO
case 0x3070: // HIRAGANA LETTER BA
case 0x3073: // HIRAGANA LETTER BI
case 0x3076: // HIRAGANA LETTER BU
case 0x3079: // HIRAGANA LETTER BE
case 0x307C: // HIRAGANA LETTER BO
case 0x3094: // HIRAGANA LETTER VU
case 0x30AC: // KATAKANA LETTER GA
case 0x30AE: // KATAKANA LETTER GI
case 0x30B0: // KATAKANA LETTER GU
case 0x30B2: // KATAKANA LETTER GE
case 0x30B4: // KATAKANA LETTER GO
case 0x30B6: // KATAKANA LETTER ZA
case 0x30B8: // KATAKANA LETTER ZI
case 0x30BA: // KATAKANA LETTER ZU
case 0x30BC: // KATAKANA LETTER ZE
case 0x30BE: // KATAKANA LETTER ZO
case 0x30C0: // KATAKANA LETTER DA
case 0x30C2: // KATAKANA LETTER DI
case 0x30C5: // KATAKANA LETTER DU
case 0x30C7: // KATAKANA LETTER DE
case 0x30C9: // KATAKANA LETTER DO
case 0x30D0: // KATAKANA LETTER BA
case 0x30D3: // KATAKANA LETTER BI
case 0x30D6: // KATAKANA LETTER BU
case 0x30D9: // KATAKANA LETTER BE
case 0x30DC: // KATAKANA LETTER BO
case 0x30F4: // KATAKANA LETTER VU
case 0x30F7: // KATAKANA LETTER VA
case 0x30F8: // KATAKANA LETTER VI
case 0x30F9: // KATAKANA LETTER VE
case 0x30FA: // KATAKANA LETTER VO
return VoicedSoundMark;
case 0x3071: // HIRAGANA LETTER PA
case 0x3074: // HIRAGANA LETTER PI
case 0x3077: // HIRAGANA LETTER PU
case 0x307A: // HIRAGANA LETTER PE
case 0x307D: // HIRAGANA LETTER PO
case 0x30D1: // KATAKANA LETTER PA
case 0x30D4: // KATAKANA LETTER PI
case 0x30D7: // KATAKANA LETTER PU
case 0x30DA: // KATAKANA LETTER PE
case 0x30DD: // KATAKANA LETTER PO
return SemiVoicedSoundMark;
}
return NoVoicedSoundMark;
}
static inline bool isCombiningVoicedSoundMark(UChar character)
{
switch (character) {
case 0x3099: // COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK
case 0x309A: // COMBINING KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
return true;
}
return false;
}
static inline bool containsKanaLetters(const String& pattern)
{
if (pattern.is8Bit())
return false;
for (auto character : pattern.span16()) {
if (isKanaLetter(character))
return true;
}
return false;
}
static void normalizeCharacters(const UChar* characters, unsigned length, Vector<UChar>& buffer)
{
UErrorCode status = U_ZERO_ERROR;
auto* normalizer = unorm2_getNFCInstance(&status);
ASSERT(U_SUCCESS(status));
buffer.reserveCapacity(length);
status = callBufferProducingFunction(unorm2_normalize, normalizer, characters, length, buffer);
ASSERT(U_SUCCESS(status));
}
static bool isNonLatin1Separator(char32_t character)
{
ASSERT_ARG(character, !isLatin1(character));
return U_GET_GC_MASK(character) & (U_GC_S_MASK | U_GC_P_MASK | U_GC_Z_MASK | U_GC_CF_MASK);
}
static inline bool isSeparator(char32_t character)
{
static constexpr std::array<bool, 256> latin1SeparatorTable {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // space ! " # $ % & ' ( ) * + , - . /
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, // : ; < = > ?
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // @
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, // [ \ ] ^ _
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // `
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, // { | } ~
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
};
if (isLatin1(character))
return latin1SeparatorTable[character];
return isNonLatin1Separator(character);
}
inline SearchBuffer::SearchBuffer(const String& target, FindOptions options)
: m_target(foldQuoteMarks(target))
, m_targetCharacters(StringView(m_target).upconvertedCharacters())
, m_options(options)
, m_prefixLength(0)
, m_atBreak(true)
, m_needsMoreContext(options.contains(FindOption::AtWordStarts))
, m_targetRequiresKanaWorkaround(containsKanaLetters(m_target))
{
ASSERT(!m_target.isEmpty());
size_t targetLength = m_target.length();
m_buffer.reserveInitialCapacity(std::max(targetLength * 8, minimumSearchBufferSize));
m_overlap = m_buffer.capacity() / 4;
if (m_options.contains(FindOption::AtWordStarts) && targetLength) {
char32_t targetFirstCharacter;
U16_GET(m_target, 0, 0u, targetLength, targetFirstCharacter);
// Characters in the separator category never really occur at the beginning of a word,
// so if the target begins with such a character, we just ignore the AtWordStart option.
if (isSeparator(targetFirstCharacter)) {
m_options.remove(FindOption::AtWordStarts);
m_needsMoreContext = false;
}
}
// Grab the single global searcher.
// If we ever have a reason to do more than once search buffer at once, we'll have
// to move to multiple searchers.
lockSearcher();
UStringSearch* searcher = WebCore::searcher();
UCollator* collator = usearch_getCollator(searcher);
UCollationStrength strength;
USearchAttributeValue comparator;
if (m_options.contains(FindOption::CaseInsensitive)) {
// Without loss of generality, have 'e' match {'e', 'E', 'é', 'É'} and 'é' match {'é', 'É'}.
strength = UCOL_SECONDARY;
comparator = USEARCH_PATTERN_BASE_WEIGHT_IS_WILDCARD;
} else {
// Without loss of generality, have 'e' match {'e'} and 'é' match {'é'}.
strength = UCOL_TERTIARY;
comparator = USEARCH_STANDARD_ELEMENT_COMPARISON;
}
if (ucol_getStrength(collator) != strength) {
ucol_setStrength(collator, strength);
usearch_reset(searcher);
}
UErrorCode status = U_ZERO_ERROR;
usearch_setAttribute(searcher, USEARCH_ELEMENT_COMPARISON, comparator, &status);
ASSERT(U_SUCCESS(status));
usearch_setPattern(searcher, m_targetCharacters, targetLength, &status);
ASSERT(U_SUCCESS(status));
// The kana workaround requires a normalized copy of the target string.
if (m_targetRequiresKanaWorkaround)
normalizeCharacters(m_targetCharacters, targetLength, m_normalizedTarget);
}
inline SearchBuffer::~SearchBuffer()
{
// Leave the static object pointing to a valid string.
UErrorCode status = U_ZERO_ERROR;
usearch_setPattern(WebCore::searcher(), &newlineCharacter, 1, &status);
ASSERT(U_SUCCESS(status));
usearch_setText(WebCore::searcher(), &newlineCharacter, 1, &status);
ASSERT(U_SUCCESS(status));
unlockSearcher();
}
inline size_t SearchBuffer::append(StringView text)
{
ASSERT(text.length());
if (m_atBreak) {
m_buffer.shrink(0);
m_prefixLength = 0;
m_atBreak = false;
} else if (m_buffer.size() == m_buffer.capacity()) {
memcpySpan(m_buffer.mutableSpan(), m_buffer.span().last(m_overlap));
m_prefixLength -= std::min(m_prefixLength, m_buffer.size() - m_overlap);
m_buffer.shrink(m_overlap);
}
size_t oldLength = m_buffer.size();
size_t usableLength = std::min<size_t>(m_buffer.capacity() - oldLength, text.length());
ASSERT(usableLength);
m_buffer.grow(oldLength + usableLength);
for (unsigned i = 0; i < usableLength; ++i)
m_buffer[oldLength + i] = foldQuoteMarkAndReplaceNoBreakSpace(text[i]);
return usableLength;
}
inline bool SearchBuffer::needsMoreContext() const
{
return m_needsMoreContext;
}
inline void SearchBuffer::prependContext(StringView text)
{
ASSERT(m_needsMoreContext);
ASSERT(m_prefixLength == m_buffer.size());
if (!text.length())
return;
m_atBreak = false;
size_t wordBoundaryContextStart = text.length();
if (wordBoundaryContextStart) {
U16_BACK_1(text, 0, wordBoundaryContextStart);
wordBoundaryContextStart = startOfLastWordBoundaryContext(text.left(wordBoundaryContextStart));
}
size_t usableLength = std::min(m_buffer.capacity() - m_prefixLength, text.length() - wordBoundaryContextStart);
WTF::append(m_buffer, text.substring(text.length() - usableLength, usableLength));
m_prefixLength += usableLength;
if (wordBoundaryContextStart || m_prefixLength == m_buffer.capacity())
m_needsMoreContext = false;
}
inline bool SearchBuffer::atBreak() const
{
return m_atBreak;
}
inline void SearchBuffer::reachedBreak()
{
m_atBreak = true;
}
inline bool SearchBuffer::isBadMatch(const UChar* match, size_t matchLength) const
{
// This function implements the kana workaround. If usearch treats
// it as a match, but we do not want to, then it's a "bad match".
if (!m_targetRequiresKanaWorkaround)
return false;
// Normalize into a match buffer. We reuse a single buffer rather than
// creating a new one each time.
normalizeCharacters(match, matchLength, m_normalizedMatch);
auto a = m_normalizedTarget.span();
auto b = m_normalizedMatch.span();
while (true) {
// Skip runs of non-kana-letter characters. This is necessary so we can
// correctly handle strings where the target and match have different-length
// runs of characters that match, while still double checking the correctness
// of matches of kana letters with other kana letters.
skipUntil<isKanaLetter>(a);
skipUntil<isKanaLetter>(b);
// If we reached the end of either the target or the match, we should have
// reached the end of both; both should have the same number of kana letters.
if (a.empty() || b.empty()) {
ASSERT(a.empty());
ASSERT(b.empty());
return false;
}
// Check for differences in the kana letter character itself.
if (isSmallKanaLetter(a.front()) != isSmallKanaLetter(b.front()))
return true;
if (composedVoicedSoundMark(a.front()) != composedVoicedSoundMark(b.front()))
return true;
skip(a, 1);
skip(b, 1);
// Check for differences in combining voiced sound marks found after the letter.
while (1) {
if (!(!a.empty() && isCombiningVoicedSoundMark(a.front()))) {
if (!b.empty() && isCombiningVoicedSoundMark(b.front()))
return true;
break;
}
if (!(!b.empty() && isCombiningVoicedSoundMark(b.front())))
return true;
if (a.front() != b.front())
return true;
skip(a, 1);
skip(b, 1);
}
}
}
inline bool SearchBuffer::isWordEndMatch(size_t start, size_t length) const
{
ASSERT(length);
ASSERT(m_options.contains(FindOption::AtWordEnds));
// Start searching at the end of matched search, so that multiple word matches succeed.
int endWord;
findEndWordBoundary(m_buffer.span(), start + length - 1, &endWord);
return static_cast<size_t>(endWord) == start + length;
}
inline bool SearchBuffer::isWordStartMatch(size_t start, size_t length) const
{
ASSERT(m_options.contains(FindOption::AtWordStarts));
if (!start)
return true;
int size = m_buffer.size();
int offset = start;
char32_t firstCharacter;
auto buffer = m_buffer.span();
U16_GET(buffer, 0, offset, size, firstCharacter);
if (m_options.contains(FindOption::TreatMedialCapitalAsWordStart)) {
char32_t previousCharacter;
U16_PREV(buffer, 0, offset, previousCharacter);
if (isSeparator(firstCharacter)) {
// The start of a separator run is a word start (".org" in "webkit.org").
if (!isSeparator(previousCharacter))
return true;
} else if (isASCIIUpper(firstCharacter)) {
// The start of an uppercase run is a word start ("Kit" in "WebKit").
if (!isASCIIUpper(previousCharacter))
return true;
// The last character of an uppercase run followed by a non-separator, non-digit
// is a word start ("Request" in "XMLHTTPRequest").
offset = start;
U16_FWD_1(buffer, offset, size);
char32_t nextCharacter = 0;
if (offset < size)
U16_GET(buffer, 0, offset, size, nextCharacter);
if (!isASCIIUpper(nextCharacter) && !isASCIIDigit(nextCharacter) && !isSeparator(nextCharacter))
return true;
} else if (isASCIIDigit(firstCharacter)) {
// The start of a digit run is a word start ("2" in "WebKit2").
if (!isASCIIDigit(previousCharacter))
return true;
} else if (isSeparator(previousCharacter) || isASCIIDigit(previousCharacter)) {
// The start of a non-separator, non-uppercase, non-digit run is a word start,
// except after an uppercase. ("org" in "webkit.org", but not "ore" in "WebCore").
return true;
}
}
// Chinese and Japanese lack word boundary marks, and there is no clear agreement on what constitutes
// a word, so treat the position before any CJK character as a word start.
if (FontCascade::isCJKIdeographOrSymbol(firstCharacter))
return true;
size_t wordBreakSearchStart = start + length;
while (wordBreakSearchStart > start)
wordBreakSearchStart = findNextWordFromIndex(buffer, wordBreakSearchStart, false /* backwards */);
return wordBreakSearchStart == start;
}
inline size_t SearchBuffer::search(size_t& start)
{
size_t size = m_buffer.size();
if (m_atBreak) {
if (!size)
return 0;
} else {
if (size != m_buffer.capacity())
return 0;
}
UStringSearch* searcher = WebCore::searcher();
UErrorCode status = U_ZERO_ERROR;
usearch_setText(searcher, m_buffer.data(), size, &status);
ASSERT(U_SUCCESS(status));
usearch_setOffset(searcher, m_prefixLength, &status);
ASSERT(U_SUCCESS(status));
int matchStart = usearch_next(searcher, &status);
ASSERT(U_SUCCESS(status));
nextMatch:
if (!(matchStart >= 0 && static_cast<size_t>(matchStart) < size)) {
ASSERT(matchStart == USEARCH_DONE);
return 0;
}
// Matches that start in the overlap area are only tentative.
// The same match may appear later, matching more characters,
// possibly including a combining character that's not yet in the buffer.
if (!m_atBreak && static_cast<size_t>(matchStart) >= size - m_overlap) {
size_t overlap = m_overlap;
if (m_options.contains(FindOption::AtWordStarts)) {
// Ensure that there is sufficient context before matchStart the next time around for
// determining if it is at a word boundary.
unsigned wordBoundaryContextStart = matchStart;
U16_BACK_1(m_buffer.span(), 0, wordBoundaryContextStart);
wordBoundaryContextStart = startOfLastWordBoundaryContext(m_buffer.subspan(0, wordBoundaryContextStart));
overlap = std::min(size - 1, std::max(overlap, size - wordBoundaryContextStart));
}
memcpySpan(m_buffer.mutableSpan(), m_buffer.span().last(overlap));
m_prefixLength -= std::min(m_prefixLength, size - overlap);
m_buffer.shrink(overlap);
return 0;
}
size_t matchedLength = usearch_getMatchedLength(searcher);
ASSERT_WITH_SECURITY_IMPLICATION(matchStart + matchedLength <= size);
// If this match is "bad", move on to the next match.
if (isBadMatch(m_buffer.subspan(matchStart).data(), matchedLength)
|| (m_options.contains(FindOption::AtWordStarts) && !isWordStartMatch(matchStart, matchedLength))
|| (m_options.contains(FindOption::AtWordEnds) && !isWordEndMatch(matchStart, matchedLength))) {
matchStart = usearch_next(searcher, &status);
ASSERT(U_SUCCESS(status));
goto nextMatch;
}
size_t newSize = size - (matchStart + 1);
memmoveSpan(m_buffer.mutableSpan(), m_buffer.subspan(matchStart + 1, newSize));
m_prefixLength -= std::min<size_t>(m_prefixLength, matchStart + 1);
m_buffer.shrink(newSize);
start = size - matchStart;
return matchedLength;
}
#else
inline SearchBuffer::SearchBuffer(const String& target, FindOptions options)
: m_target(foldQuoteMarks(options & CaseInsensitive ? target.foldCase() : target))
, m_options(options)
, m_buffer(m_target.length())
, m_isCharacterStartBuffer(m_target.length())
, m_isBufferFull(false)
, m_cursor(0)
{
ASSERT(!m_target.isEmpty());
m_target.replace(noBreakSpace, ' ');
}
inline SearchBuffer::~SearchBuffer() = default;
inline void SearchBuffer::reachedBreak()
{
m_cursor = 0;
m_isBufferFull = false;
}
inline bool SearchBuffer::atBreak() const
{
return !m_cursor && !m_isBufferFull;
}
inline void SearchBuffer::append(UChar c, bool isStart)
{
m_buffer[m_cursor] = foldQuoteMarkAndReplaceNoBreakSpace(c);
m_isCharacterStartBuffer[m_cursor] = isStart;
if (++m_cursor == m_target.length()) {
m_cursor = 0;
m_isBufferFull = true;
}
}
inline size_t SearchBuffer::append(const UChar* characters, size_t length)
{
ASSERT(length);
if (!(m_options & CaseInsensitive)) {
append(characters[0], true);
return 1;
}
constexpr int maxFoldedCharacters = 16; // sensible maximum is 3, this should be more than enough
UChar foldedCharacters[maxFoldedCharacters];
UErrorCode status = U_ZERO_ERROR;
int numFoldedCharacters = u_strFoldCase(foldedCharacters, maxFoldedCharacters, characters, 1, U_FOLD_CASE_DEFAULT, &status);
ASSERT(U_SUCCESS(status));
ASSERT(numFoldedCharacters);
ASSERT(numFoldedCharacters <= maxFoldedCharacters);
if (U_SUCCESS(status) && numFoldedCharacters) {
numFoldedCharacters = std::min(numFoldedCharacters, maxFoldedCharacters);
append(foldedCharacters[0], true);
for (int i = 1; i < numFoldedCharacters; ++i)
append(foldedCharacters[i], false);
}
return 1;
}
inline bool SearchBuffer::needsMoreContext() const
{
return false;
}
void SearchBuffer::prependContext(const UChar*, size_t)
{
ASSERT_NOT_REACHED();
}
inline size_t SearchBuffer::search(size_t& start)
{
if (!m_isBufferFull)
return 0;
if (!m_isCharacterStartBuffer[m_cursor])
return 0;
size_t tailSpace = m_target.length() - m_cursor;
if (memcmp(&m_buffer[m_cursor], m_target.characters(), tailSpace * sizeof(UChar)) != 0)
return 0;
if (memcmp(&m_buffer[0], m_target.characters() + tailSpace, m_cursor * sizeof(UChar)) != 0)
return 0;
start = length();
// Now that we've found a match once, we don't want to find it again, because those
// are the SearchBuffer semantics, allowing for a buffer where you append more than one
// character at a time. To do this we take advantage of m_isCharacterStartBuffer, but if
// we want to get rid of that in the future we could track this with a separate boolean
// or even move the characters to the start of the buffer and set m_isBufferFull to false.
m_isCharacterStartBuffer[m_cursor] = false;
return start;
}
// Returns the number of characters that were appended to the buffer (what we are searching in).
// That's not necessarily the same length as the passed-in target string, because case folding
// can make two strings match even though they're not the same length.
size_t SearchBuffer::length() const
{
size_t bufferSize = m_target.length();
size_t length = 0;
for (size_t i = 0; i < bufferSize; ++i)
length += m_isCharacterStartBuffer[i];
return length;
}
#endif
// --------
uint64_t characterCount(const SimpleRange& range, TextIteratorBehaviors behaviors)
{
auto adjustedRange = range;
auto ordering = treeOrder<ComposedTree>(range.start, range.end);
if (is_gt(ordering))
std::swap(adjustedRange.start, adjustedRange.end);
else if (!is_lt(ordering))
return 0;
uint64_t length = 0;
for (TextIterator it(adjustedRange, behaviors); !it.atEnd(); it.advance())
length += it.text().length();
return length;
}
static inline bool isInsideReplacedElement(TextIterator& iterator, TextIteratorBehaviors behaviors)
{
ASSERT(!iterator.atEnd());
ASSERT(iterator.text().length() == 1);
RefPtr node = iterator.node();
return node && isRendererReplacedElement(node->renderer(), behaviors);
}
constexpr uint64_t clampedAdd(uint64_t a, uint64_t b)
{
auto sum = a + b;
return sum >= a ? sum : std::numeric_limits<uint64_t>::max();
}
SimpleRange resolveCharacterRange(const SimpleRange& scope, CharacterRange range, TextIteratorBehaviors behaviors)
{
auto resultRange = SimpleRange { range.location ? scope.end : scope.start, (range.location || range.length) ? scope.end : scope.start };
auto rangeEnd = clampedAdd(range.location, range.length);
uint64_t location = 0;
for (TextIterator it(scope, behaviors); !it.atEnd(); it.advance()) {
unsigned length = it.text().length();
auto textRunRange = it.range();
auto found = [&] (uint64_t targetLocation) -> bool {
return targetLocation >= location && targetLocation - location <= length;
};
bool foundStart = found(range.location);
bool foundEnd = found(rangeEnd);
if (foundEnd) {
// FIXME: This is a workaround for the fact that the end of a run is often at the wrong position for emitted '\n's or if the renderer of the current node is a replaced element.
// FIXME: consider controlling this with TextIteratorBehavior instead of doing it unconditionally to help us eventually phase it out everywhere.
if (length == 1 && (it.text()[0] == '\n' || isInsideReplacedElement(it, behaviors))) {
it.advance();
if (!it.atEnd())
textRunRange.end = it.range().start;
else {
if (auto end = makeBoundaryPoint(VisiblePosition(makeDeprecatedLegacyPosition(textRunRange.start)).next().deepEquivalent()))
textRunRange.end = *end;
}
}
}
auto boundary = [&] (uint64_t targetLocation) -> BoundaryPoint {
if (is<Text>(textRunRange.start.container)) {
ASSERT(targetLocation - location <= downcast<Text>(textRunRange.start.container.get()).length());
unsigned offset = textRunRange.start.offset + targetLocation - location;
return { textRunRange.start.container.copyRef(), offset };
}
return targetLocation == location ? textRunRange.start : textRunRange.end;
};
if (foundStart)
resultRange.start = boundary(range.location);
if (foundEnd) {
resultRange.end = boundary(rangeEnd);
break;
}
location += length;
}
return resultRange;
}
// --------
bool hasAnyPlainText(const SimpleRange& range, TextIteratorBehaviors behaviors, IgnoreCollapsedRanges ignoreCollapsedRanges)
{
for (TextIterator iterator { range, behaviors }; !iterator.atEnd(); iterator.advance()) {
if (ignoreCollapsedRanges == IgnoreCollapsedRanges::Yes && iterator.range().collapsed())
continue;
if (!iterator.text().isEmpty())
return true;
}
return false;
}
String plainText(const SimpleRange& range, TextIteratorBehaviors defaultBehavior, bool isDisplayString)
{
// The initial buffer size can be critical for performance: https://bugs.webkit.org/show_bug.cgi?id=81192
constexpr unsigned initialCapacity = 1 << 15;
Ref document = range.start.document();
unsigned bufferLength = 0;
StringBuilder builder;
builder.reserveCapacity(initialCapacity);
TextIteratorBehaviors behaviors = defaultBehavior;
if (!isDisplayString)
behaviors.add(TextIteratorBehavior::EmitsTextsWithoutTranscoding);
for (TextIterator it(range, behaviors); !it.atEnd(); it.advance()) {
it.appendTextToStringBuilder(builder);
bufferLength += it.text().length();
}
if (!bufferLength)
return emptyString();
String result = builder.toString();
if (isDisplayString)
document->displayStringModifiedByEncoding(result);
return result;
}
String plainTextReplacingNoBreakSpace(const SimpleRange& range, TextIteratorBehaviors defaultBehaviors, bool isDisplayString)
{
return makeStringByReplacingAll(plainText(range, defaultBehaviors, isDisplayString), noBreakSpace, ' ');
}
static void forEachMatch(const SimpleRange& range, const String& target, FindOptions options, NOESCAPE const Function<bool(CharacterRange)>& match)
{
SearchBuffer buffer(target, options);
if (buffer.needsMoreContext()) {
auto beforeStartRange = SimpleRange { makeBoundaryPointBeforeNodeContents(range.start.document()), range.start };
for (SimplifiedBackwardsTextIterator backwardsIterator(beforeStartRange); !backwardsIterator.atEnd(); backwardsIterator.advance()) {
buffer.prependContext(backwardsIterator.text());
if (!buffer.needsMoreContext())
break;
}
}
CharacterIterator findIterator(range, findIteratorOptions(options));
while (!findIterator.atEnd()) {
findIterator.advance(buffer.append(findIterator.text()));
while (1) {
size_t matchStartOffset;
size_t newMatchLength = buffer.search(matchStartOffset);
if (!newMatchLength) {
if (findIterator.atBreak() && !buffer.atBreak()) {
buffer.reachedBreak();
continue;
}
break;
}
size_t lastCharacterInBufferOffset = findIterator.characterOffset();
ASSERT(lastCharacterInBufferOffset >= matchStartOffset);
if (match(CharacterRange(lastCharacterInBufferOffset - matchStartOffset, newMatchLength)))
return;
}
}
}
static SimpleRange rangeForMatch(const SimpleRange& range, FindOptions options, CharacterRange match)
{
auto noMatchResult = [&] () {
auto& boundary = options.contains(FindOption::Backwards) ? range.start : range.end;
return SimpleRange { boundary, boundary };
};
if (!match.length)
return noMatchResult();
CharacterIterator it(range, findIteratorOptions(options));
it.advance(match.location);
if (it.atEnd())
return noMatchResult();
auto start = it.range().start;
it.advance(match.length - 1);
if (it.atEnd())
return noMatchResult();
return { WTFMove(start), it.range().end };
}
SimpleRange findClosestPlainText(const SimpleRange& range, const String& target, FindOptions options, uint64_t targetOffset)
{
CharacterRange closestMatch;
uint64_t closestMatchDistance = std::numeric_limits<uint64_t>::max();
forEachMatch(range, target, options, [&] (CharacterRange match) {
auto distance = [] (uint64_t a, uint64_t b) -> uint64_t {
return std::abs(static_cast<int64_t>(a - b));
};
auto matchDistance = std::min(distance(match.location, targetOffset), distance(match.location + match.length, targetOffset));
if (matchDistance > closestMatchDistance)
return false;
if (matchDistance == closestMatchDistance && !options.contains(FindOption::Backwards))
return false;
closestMatch = match;
if (!matchDistance && !options.contains(FindOption::Backwards))
return true;
closestMatchDistance = matchDistance;
return false;
});
return rangeForMatch(range, options, closestMatch);
}
SimpleRange findPlainText(const SimpleRange& range, const String& target, FindOptions options)
{
// When searching forward stop since we want the first match.
// When searching backward keep going since we want the last match.
bool stopAfterFindingMatch = !options.contains(FindOption::Backwards);
CharacterRange lastMatchFound;
forEachMatch(range, target, options, [&] (CharacterRange match) {
lastMatchFound = match;
return stopAfterFindingMatch;
});
return rangeForMatch(range, options, lastMatchFound);
}
bool containsPlainText(const String& document, const String& target, FindOptions options)
{
SearchBuffer buffer { target, options };
StringView remainingText { document };
while (!remainingText.isEmpty()) {
size_t charactersAppended = buffer.append(document);
remainingText = remainingText.substring(charactersAppended);
if (remainingText.isEmpty())
buffer.reachedBreak();
size_t matchStartOffset;
if (buffer.search(matchStartOffset))
return true;
}
return false;
}
}
#if ENABLE(TREE_DEBUGGING)
void showTree(const WebCore::TextIterator& pos)
{
pos.showTreeForThis();
}
void showTree(const WebCore::TextIterator* pos)
{
if (pos)
pos->showTreeForThis();
}
#endif
|