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 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409
|
/*
* Copyright (C) 2006-2024 Apple Inc. All rights reserved.
* Copyright (C) 2013 Google Inc. All rights reserved.
* Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
* Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies)
*
* 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 "EventHandler.h"
#include "AutoscrollController.h"
#include "BackForwardController.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "CloseWatcherManager.h"
#include "ComposedTreeAncestorIterator.h"
#include "ComposedTreeIterator.h"
#include "CursorList.h"
#include "DocumentInlines.h"
#include "DocumentMarkerController.h"
#include "DragController.h"
#include "DragEvent.h"
#include "DragState.h"
#include "Editing.h"
#include "Editor.h"
#include "EditorClient.h"
#include "ElementInlines.h"
#include "EventNames.h"
#include "FileList.h"
#include "FloatPoint.h"
#include "FloatRect.h"
#include "FocusController.h"
#include "FocusOptions.h"
#include "FrameLoader.h"
#include "FrameSelection.h"
#include "FrameTree.h"
#include "FullscreenManager.h"
#include "HTMLAreaElement.h"
#include "HTMLDialogElement.h"
#include "HTMLDocument.h"
#include "HTMLFrameElement.h"
#include "HTMLFrameSetElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLPlugInElement.h"
#include "HTMLVideoElement.h"
#include "HandleUserInputEventResult.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "Image.h"
#include "ImageOverlay.h"
#include "ImageOverlayController.h"
#include "InspectorInstrumentation.h"
#include "KeyboardEvent.h"
#include "KeyboardScrollingAnimator.h"
#include "LocalFrame.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "MouseEvent.h"
#include "MouseEventWithHitTestResults.h"
#include "NotImplemented.h"
#include "Page.h"
#include "PageOverlayController.h"
#include "Pasteboard.h"
#include "PlatformEvent.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformMouseEvent.h"
#include "PlatformWheelEvent.h"
#include "PluginDocument.h"
#include "PointerCaptureController.h"
#include "PointerEventTypeNames.h"
#include "PseudoClassChangeInvalidation.h"
#include "Quirks.h"
#include "Range.h"
#include "RemoteFrame.h"
#include "RemoteFrameView.h"
#include "RemoteUserInputEventData.h"
#include "RenderFrameSet.h"
#include "RenderImage.h"
#include "RenderLayer.h"
#include "RenderLayerScrollableArea.h"
#include "RenderListBox.h"
#include "RenderTextControlSingleLine.h"
#include "RenderView.h"
#include "RenderWidget.h"
#include "ResourceLoadObserver.h"
#include "SVGDocument.h"
#include "SVGElementTypeHelpers.h"
#include "SVGNames.h"
#include "ScrollAnimator.h"
#include "ScrollLatchingController.h"
#include "Scrollbar.h"
#include "ScrollingCoordinator.h"
#include "ScrollingEffectsController.h"
#include "SelectionRestorationMode.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "SpatialNavigation.h"
#include "StaticPasteboard.h"
#include "StyleCachedImage.h"
#include "TextEvent.h"
#include "TextIterator.h"
#include "TextRecognitionOptions.h"
#include "UserGestureIndicator.h"
#include "UserTypingGestureIndicator.h"
#include "ValidationMessageClient.h"
#include "VisibleUnits.h"
#include "WheelEvent.h"
#include "WheelEventDeltaFilter.h"
#include "WheelEventTestMonitor.h"
#include "WindowsKeyboardCodes.h"
#include <wtf/Assertions.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/RuntimeApplicationChecks.h>
#include <wtf/Scope.h>
#include <wtf/SetForScope.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
#if ENABLE(IOS_TOUCH_EVENTS)
#include "PlatformTouchEventIOS.h"
#endif
#if ENABLE(CONTENT_CHANGE_OBSERVER)
#include "DOMTimerHoldingTank.h"
#endif
#if ENABLE(TOUCH_EVENTS)
#include "TouchEvent.h"
#include "TouchList.h"
#endif
#if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
#include "PlatformTouchEvent.h"
#endif
#if ENABLE(MAC_GESTURE_EVENTS)
#include "PlatformGestureEventMac.h"
#endif
#if ENABLE(POINTER_LOCK)
#include "PointerLockController.h"
#endif
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(EventHandler);
using namespace HTMLNames;
#if ENABLE(DRAG_SUPPORT)
// The link drag hysteresis is much larger than the others because there
// needs to be enough space to cancel the link press without starting a link drag,
// and because dragging links is rare.
const int LinkDragHysteresis = 40;
const int ImageDragHysteresis = 5;
const int TextDragHysteresis = 3;
const int ColorDragHystersis = 3;
const int GeneralDragHysteresis = 3;
#if PLATFORM(MAC)
const Seconds EventHandler::TextDragDelay { 150_ms };
#else
const Seconds EventHandler::TextDragDelay { 0_s };
#endif
#endif // ENABLE(DRAG_SUPPORT)
#if ENABLE(IOS_GESTURE_EVENTS) || ENABLE(MAC_GESTURE_EVENTS)
const float GestureUnknown = 0;
#endif
#if ENABLE(IOS_TOUCH_EVENTS)
// FIXME: Share this constant with EventHandler and SliderThumbElement.
const unsigned InvalidTouchIdentifier = 0;
#endif
// Match key code of composition keydown event on windows.
// IE sends VK_PROCESSKEY which has value 229;
const int CompositionEventKeyCode = 229;
using namespace SVGNames;
#if !ENABLE(IOS_TOUCH_EVENTS)
// The amount of time to wait before sending a fake mouse event, triggered
// during a scroll. The short interval is used if the content responds to the mouse events
// in fakeMouseMoveDurationThreshold or less, otherwise the long interval is used.
const double fakeMouseMoveDurationThreshold = 0.01;
const Seconds fakeMouseMoveShortInterval = { 100_ms };
const Seconds fakeMouseMoveLongInterval = { 250_ms };
#endif
const int maximumCursorSize = 128;
#if ENABLE(MOUSE_CURSOR_SCALE)
// It's pretty unlikely that a scale of less than one would ever be used. But all we really
// need to ensure here is that the scale isn't so small that integer overflow can occur when
// dividing cursor sizes (limited above) by the scale.
const double minimumCursorScale = 0.001;
#endif
class MaximumDurationTracker {
public:
explicit MaximumDurationTracker(double *maxDuration)
: m_maxDuration(maxDuration)
, m_start(MonotonicTime::now())
{
}
~MaximumDurationTracker()
{
*m_maxDuration = std::max(*m_maxDuration, (MonotonicTime::now() - m_start).seconds());
}
private:
double* m_maxDuration;
MonotonicTime m_start;
};
static UserGestureType userGestureTypeForPlatformEvent(const PlatformKeyboardEvent& keyEvent)
{
// https://html.spec.whatwg.org/multipage/interaction.html#activation-triggering-input-event
// An activation triggering input event is any event whose isTrusted attribute is true and whose type is one of:
// * "keydown", provided the key is neither the Esc key nor a shortcut key reserved by the user agent.
if (keyEvent.windowsVirtualKeyCode() == VK_ESCAPE)
return UserGestureType::EscapeKey;
if (keyEvent.type() == PlatformEventType::KeyDown)
return UserGestureType::ActivationTriggering;
// FIXME: This check does not yet handle whether the event represents a "shortcut key reserved by the user agent".
return UserGestureType::Other;
}
static UserGestureType userGestureTypeForPlatformEvent(const PlatformMouseEvent& mouseEvent)
{
// ...
// * "mousedown".
// * "pointerdown", provided the event's pointerType is "mouse".
if (mouseEvent.type() == PlatformEventType::MousePressed)
return UserGestureType::ActivationTriggering;
return UserGestureType::Other;
}
#if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
static UserGestureType userGestureTypeForPlatformEvent(const PlatformTouchEvent& touchEvent)
{
// ...
// * "pointerup", provided the event's pointerType is not "mouse".
// * "touchend".
if (touchEvent.type() == PlatformEventType::TouchEnd)
return UserGestureType::ActivationTriggering;
return UserGestureType::Other;
}
#endif
#if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
class SyntheticTouchPoint : public PlatformTouchPoint {
public:
// The default values are based on http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html
explicit SyntheticTouchPoint(const PlatformMouseEvent& event)
{
static constexpr int idDefaultValue = 0;
static constexpr int radiusYDefaultValue = 1;
static constexpr int radiusXDefaultValue = 1;
static constexpr float rotationAngleDefaultValue = 0.0f;
static constexpr float forceDefaultValue = 1.0f;
m_id = idDefaultValue; // There is only one active TouchPoint.
m_screenPos = event.globalPosition();
m_pos = event.position();
m_radiusY = radiusYDefaultValue;
m_radiusX = radiusXDefaultValue;
m_rotationAngle = rotationAngleDefaultValue;
m_force = forceDefaultValue;
PlatformEvent::Type type = event.type();
ASSERT(type == PlatformEvent::Type::MouseMoved || type == PlatformEvent::Type::MousePressed || type == PlatformEvent::Type::MouseReleased);
switch (type) {
case PlatformEvent::Type::MouseMoved:
m_state = TouchMoved;
break;
case PlatformEvent::Type::MousePressed:
m_state = TouchPressed;
break;
case PlatformEvent::Type::MouseReleased:
m_state = TouchReleased;
break;
default:
ASSERT_NOT_REACHED();
break;
}
}
};
class SyntheticSingleTouchEvent : public PlatformTouchEvent {
public:
explicit SyntheticSingleTouchEvent(const PlatformMouseEvent& event)
{
switch (event.type()) {
case PlatformEvent::Type::MouseMoved:
m_type = Type::TouchMove;
break;
case PlatformEvent::Type::MousePressed:
m_type = Type::TouchStart;
break;
case PlatformEvent::Type::MouseReleased:
m_type = Type::TouchEnd;
break;
default:
ASSERT_NOT_REACHED();
m_type = Type::NoType;
break;
}
m_timestamp = event.timestamp();
m_modifiers = event.modifiers();
m_touchPoints.append(SyntheticTouchPoint(event));
}
};
#endif // ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
static inline ScrollGranularity wheelGranularityToScrollGranularity(unsigned deltaMode)
{
switch (deltaMode) {
case WheelEvent::DOM_DELTA_PAGE:
return ScrollGranularity::Page;
case WheelEvent::DOM_DELTA_LINE:
return ScrollGranularity::Line;
case WheelEvent::DOM_DELTA_PIXEL:
return ScrollGranularity::Pixel;
default:
return ScrollGranularity::Pixel;
}
}
#if (ENABLE(TOUCH_EVENTS) && !PLATFORM(IOS_FAMILY))
static bool shouldGesturesTriggerActive()
{
// If the platform we're on supports GestureTapDown and GestureTapCancel then we'll
// rely on them to set the active state. Unfortunately there's no generic way to
// know in advance what event types are supported.
return false;
}
#endif
#if !PLATFORM(COCOA)
bool EventHandler::eventLoopHandleMouseUp(const MouseEventWithHitTestResults&)
{
return false;
}
#if ENABLE(DRAG_SUPPORT)
bool EventHandler::eventLoopHandleMouseDragged(const MouseEventWithHitTestResults&)
{
return false;
}
#endif
#endif
// Refetch the event target node if it is removed or currently is the shadow node inside an <input> element.
// If a mouse event handler changes the input element type to one that has a widget associated,
// we'd like to EventHandler::handleMousePressEvent to pass the event to the widget and thus the
// event target node can't still be the shadow node.
static inline bool shouldRefetchEventTarget(const MouseEventWithHitTestResults& mouseEvent)
{
RefPtr targetNode = mouseEvent.targetNode();
ASSERT(targetNode);
if (!targetNode->parentNode())
return true;
RefPtr shadowRoot = dynamicDowncast<ShadowRoot>(*targetNode);
return shadowRoot && is<HTMLInputElement>(shadowRoot->host());
}
EventHandler::EventHandler(LocalFrame& frame)
: m_frame(frame)
, m_hoverTimer(*this, &EventHandler::hoverTimerFired)
#if ENABLE(IMAGE_ANALYSIS)
, m_textRecognitionHoverTimer(*this, &EventHandler::textRecognitionHoverTimerFired, 250_ms)
#endif
, m_autoscrollController(makeUnique<AutoscrollController>())
#if !ENABLE(IOS_TOUCH_EVENTS)
, m_fakeMouseMoveEventTimer(*this, &EventHandler::fakeMouseMoveEventTimerFired)
#endif
#if ENABLE(CURSOR_VISIBILITY)
, m_autoHideCursorTimer(*this, &EventHandler::autoHideCursorTimerFired)
#endif
{
}
EventHandler::~EventHandler()
{
#if !ENABLE(IOS_TOUCH_EVENTS)
ASSERT(!m_fakeMouseMoveEventTimer.isActive());
#endif
#if ENABLE(CURSOR_VISIBILITY)
ASSERT(!m_autoHideCursorTimer.isActive());
#endif
}
#if ENABLE(DRAG_SUPPORT)
DragState& EventHandler::dragState()
{
static NeverDestroyed<DragState> state;
return state;
}
Element* EventHandler::draggedElement()
{
return dragState().source.get();
}
RefPtr<Element> EventHandler::protectedDraggedElement()
{
return dragState().source;
}
#endif
void EventHandler::clear()
{
m_hoverTimer.stop();
m_hasScheduledCursorUpdate = false;
#if !ENABLE(IOS_TOUCH_EVENTS)
m_fakeMouseMoveEventTimer.stop();
#endif
#if ENABLE(CURSOR_VISIBILITY)
cancelAutoHideCursorTimer();
#endif
#if ENABLE(IMAGE_ANALYSIS)
m_textRecognitionHoverTimer.stop();
#endif
m_resizeLayer = nullptr;
clearElementUnderMouse();
m_lastElementUnderMouse = nullptr;
m_lastMouseMoveEventSubframe = nullptr;
m_lastScrollbarUnderMouse = nullptr;
m_clickCount = 0;
m_clickNode = nullptr;
#if ENABLE(IOS_GESTURE_EVENTS)
m_gestureInitialDiameter = GestureUnknown;
m_gestureInitialRotation = GestureUnknown;
#endif
#if ENABLE(IOS_GESTURE_EVENTS) || ENABLE(MAC_GESTURE_EVENTS)
m_gestureLastDiameter = GestureUnknown;
m_gestureLastRotation = GestureUnknown;
m_gestureTargets.clear();
#endif
#if ENABLE(IOS_TOUCH_EVENTS)
m_touches.clear();
m_touchLastGlobalPositionAndDeltaMap.clear();
m_firstTouchID = InvalidTouchIdentifier;
m_touchEventTargetSubframe = nullptr;
#endif
m_frameSetBeingResized = nullptr;
#if ENABLE(DRAG_SUPPORT)
m_dragTarget = nullptr;
m_shouldOnlyFireDragOverEvent = false;
#endif
m_lastKnownMousePosition = std::nullopt;
m_lastKnownMouseGlobalPosition = { };
m_mousePressNode = nullptr;
m_mousePressed = false;
m_capturesDragging = false;
resetCapturingMouseEventsElement();
clearLatchedState();
#if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
m_originatingTouchPointTargets.clear();
m_originatingTouchPointDocument = nullptr;
m_originatingTouchPointTargetKey = 0;
m_touchPressed = false;
#endif
m_maxMouseMovedDuration = 0;
m_didStartDrag = false;
}
void EventHandler::nodeWillBeRemoved(Node& nodeToBeRemoved)
{
if (nodeToBeRemoved.containsIncludingShadowDOM(RefPtr { m_clickNode }.get()))
m_clickNode = nullptr;
if (nodeToBeRemoved.containsIncludingShadowDOM(RefPtr { m_lastElementUnderMouse }.get()))
m_lastElementUnderMouse = nullptr;
}
static void setSelectionIfNeeded(FrameSelection& selection, const VisibleSelection& newSelection)
{
if (selection.selection() != newSelection && selection.shouldChangeSelection(newSelection))
selection.setSelection(newSelection, FrameSelection::defaultSetSelectionOptions(UserTriggered::Yes));
}
static inline bool dispatchSelectStart(Node* node)
{
if (!node || !node->renderer())
return true;
auto event = Event::create(eventNames().selectstartEvent, Event::CanBubble::Yes, Event::IsCancelable::Yes);
node->dispatchEvent(event);
return !event->defaultPrevented();
}
static Node* nodeToSelectOnMouseDownForNode(Node& targetNode)
{
if (ImageOverlay::isInsideOverlay(targetNode))
return nullptr;
if (RefPtr rootUserSelectAll = Position::rootUserSelectAllForNode(&targetNode))
return rootUserSelectAll.get();
if (targetNode.shouldSelectOnMouseDown())
return &targetNode;
return nullptr;
}
static VisibleSelection expandSelectionToRespectSelectOnMouseDown(Node& targetNode, const VisibleSelection& selection)
{
RefPtr nodeToSelect = nodeToSelectOnMouseDownForNode(targetNode);
if (!nodeToSelect)
return selection;
VisibleSelection newSelection(selection);
newSelection.setBase(positionBeforeNode(nodeToSelect.get()).upstream(CanCrossEditingBoundary));
newSelection.setExtent(positionAfterNode(nodeToSelect.get()).downstream(CanCrossEditingBoundary));
return newSelection;
}
static bool shouldAvoidExtendingSelectionOnClick(const Node& targetNode, const VisibleSelection& selection)
{
if (is<Text>(targetNode))
return false;
if (selection.isContentEditable())
return false;
auto range = selection.toNormalizedRange();
if (!range)
return false;
if (range->collapsed())
return false;
static constexpr OptionSet plainTextOptions {
TextIteratorBehavior::EmitsObjectReplacementCharacters,
TextIteratorBehavior::EntersTextControls,
};
if (hasAnyPlainText(*range, plainTextOptions, IgnoreCollapsedRanges::Yes))
return false;
return true;
}
bool EventHandler::expandAndUpdateSelectionForMouseDownIfNeeded(Node& targetNode, const VisibleSelection& selection, TextGranularity granularity)
{
auto expandedSelection = expandSelectionToRespectSelectOnMouseDown(targetNode, selection);
if (shouldAvoidExtendingSelectionOnClick(targetNode, expandedSelection))
return false;
return updateSelectionForMouseDownDispatchingSelectStart(&targetNode, expandedSelection, granularity);
}
bool EventHandler::updateSelectionForMouseDownDispatchingSelectStart(Node* targetNode, const VisibleSelection& selection, TextGranularity granularity)
{
if (Position::nodeIsUserSelectNone(targetNode))
return false;
if (!dispatchSelectStart(targetNode)) {
m_mouseDownMayStartSelect = false;
return false;
}
if (selection.isOrphan()) {
m_mouseDownMayStartSelect = false;
return false;
}
if (selection.isRange()) {
m_selectionInitiationState = ExtendedSelection;
#if ENABLE(DRAG_SUPPORT)
m_dragStartSelection = getWeakSimpleRangeFromSelection(selection);
#endif
} else {
granularity = TextGranularity::CharacterGranularity;
m_selectionInitiationState = PlacedCaret;
}
protectedFrame()->selection().setSelectionByMouseIfDifferent(selection, granularity);
return true;
}
void EventHandler::selectClosestWordFromHitTestResult(const HitTestResult& result, AppendTrailingWhitespace appendTrailingWhitespace)
{
RefPtr targetNode = result.targetNode();
VisibleSelection newSelection;
if (targetNode && targetNode->renderer()) {
VisiblePosition pos(targetNode->renderer()->positionForPoint(result.localPoint(), HitTestSource::User, nullptr));
if (pos.isNotNull()) {
newSelection = VisibleSelection(pos);
newSelection.expandUsingGranularity(TextGranularity::WordGranularity);
}
if (appendTrailingWhitespace == ShouldAppendTrailingWhitespace && newSelection.isRange())
newSelection.appendTrailingWhitespace();
expandAndUpdateSelectionForMouseDownIfNeeded(*targetNode, newSelection, TextGranularity::WordGranularity);
}
}
static AppendTrailingWhitespace shouldAppendTrailingWhitespace(const MouseEventWithHitTestResults& result, const LocalFrame& frame)
{
return (result.event().clickCount() == 2 && frame.editor().isSelectTrailingWhitespaceEnabled()) ? ShouldAppendTrailingWhitespace : DontAppendTrailingWhitespace;
}
#if !PLATFORM(COCOA)
VisibleSelection EventHandler::selectClosestWordFromHitTestResultBasedOnLookup(const HitTestResult&)
{
return VisibleSelection();
}
#endif
void EventHandler::selectClosestContextualWordFromHitTestResult(const HitTestResult& result, AppendTrailingWhitespace appendTrailingWhitespace)
{
RefPtr targetNode = result.targetNode();
VisibleSelection newSelection;
if (targetNode && targetNode->renderer()) {
newSelection = selectClosestWordFromHitTestResultBasedOnLookup(result);
if (newSelection.isNone()) {
VisiblePosition pos(targetNode->renderer()->positionForPoint(result.localPoint(), HitTestSource::User, nullptr));
if (pos.isNotNull()) {
newSelection = VisibleSelection(pos);
newSelection.expandUsingGranularity(TextGranularity::WordGranularity);
}
}
if (appendTrailingWhitespace == ShouldAppendTrailingWhitespace && newSelection.isRange())
newSelection.appendTrailingWhitespace();
updateSelectionForMouseDownDispatchingSelectStart(targetNode.get(), expandSelectionToRespectSelectOnMouseDown(*targetNode, newSelection), TextGranularity::WordGranularity);
}
}
void EventHandler::selectClosestContextualWordOrLinkFromHitTestResult(const HitTestResult& result, AppendTrailingWhitespace appendTrailingWhitespace)
{
// FIXME: In the editable case, word selection sometimes selects content that isn't underneath the mouse.
// If the selection is non-editable, we do word selection to make it easier to use the contextual menu items
// available for text selections. But only if we're above text.
if (!m_frame->selection().selection().isContentEditable() && !is<Text>(result.targetNode()))
return;
if (!m_frame->settings().textInteractionEnabled())
return;
RefPtr urlElement = result.URLElement();
if (!urlElement || !isDraggableLink(*urlElement)) {
if (RefPtr targetNode = result.targetNode(); targetNode && isEditableNode(*targetNode)) {
selectClosestWordFromHitTestResult(result, appendTrailingWhitespace);
return;
}
return selectClosestContextualWordFromHitTestResult(result, appendTrailingWhitespace);
}
if (RefPtr targetNode = result.targetNode(); targetNode && targetNode->renderer()) {
VisibleSelection newSelection;
VisiblePosition pos(targetNode->renderer()->positionForPoint(result.localPoint(), HitTestSource::User, nullptr));
if (pos.isNotNull() && pos.deepEquivalent().deprecatedNode()->isDescendantOf(*urlElement))
newSelection = VisibleSelection::selectionFromContentsOfNode(urlElement.get());
updateSelectionForMouseDownDispatchingSelectStart(targetNode.get(), expandSelectionToRespectSelectOnMouseDown(*targetNode, newSelection), TextGranularity::WordGranularity);
}
}
bool EventHandler::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event)
{
if (event.event().button() != MouseButton::Left)
return false;
if (m_frame->selection().isRange()) {
// A double-click when range is already selected
// should not change the selection. So, do not call
// selectClosestWordFromHitTestResult, but do set
// m_beganSelectingText to prevent handleMouseReleaseEvent
// from setting caret selection.
m_selectionInitiationState = ExtendedSelection;
#if ENABLE(DRAG_SUPPORT)
m_dragStartSelection = getWeakSimpleRangeFromSelection(m_frame->selection().selection());
#endif
} else if (mouseDownMayStartSelect())
selectClosestWordFromHitTestResult(event.hitTestResult(), shouldAppendTrailingWhitespace(event, protectedFrame()));
return true;
}
bool EventHandler::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event)
{
if (event.event().button() != MouseButton::Left)
return false;
RefPtr targetNode = event.targetNode();
if (!(targetNode && targetNode->renderer() && mouseDownMayStartSelect()))
return false;
VisibleSelection newSelection;
VisiblePosition pos(targetNode->renderer()->positionForPoint(event.localPoint(), HitTestSource::User, nullptr));
if (pos.isNotNull()) {
newSelection = VisibleSelection(pos);
newSelection.expandUsingGranularity(TextGranularity::ParagraphGranularity);
}
return expandAndUpdateSelectionForMouseDownIfNeeded(*targetNode, newSelection, TextGranularity::ParagraphGranularity);
}
static uint64_t textDistance(const Position& start, const Position& end)
{
auto range = makeSimpleRange(start, end);
if (!range)
return 0;
return characterCount(*range, TextIteratorBehavior::EmitsCharactersBetweenAllVisiblePositions);
}
bool EventHandler::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event)
{
Ref frame = m_frame.get();
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
RefPtr targetNode = event.targetNode();
if (!targetNode || !targetNode->renderer() || !mouseDownMayStartSelect() || m_mouseDownDelegatedFocus)
return false;
// Extend the selection if the Shift key is down, unless the click is in a link.
bool extendSelection = event.event().shiftKey() && !event.isOverLink();
// Don't restart the selection when the mouse is pressed on an
// existing selection so we can allow for text dragging.
if (RefPtr view = frame->view()) {
LayoutPoint vPoint = view->windowToContents(event.event().position());
if (!extendSelection && frame->selection().contains(vPoint)) {
m_mouseDownWasSingleClickInSelection = true;
return false;
}
}
VisiblePosition visiblePosition(targetNode->renderer()->positionForPoint(event.localPoint(), HitTestSource::User, nullptr));
if (visiblePosition.isNull())
visiblePosition = VisiblePosition(firstPositionInOrBeforeNode(targetNode.get()));
Position pos = visiblePosition.deepEquivalent();
VisibleSelection newSelection = frame->selection().selection();
TextGranularity granularity = TextGranularity::CharacterGranularity;
if (!frame->editor().client()->shouldAllowSingleClickToChangeSelection(*targetNode, newSelection))
return true;
if (extendSelection && newSelection.isCaretOrRange()) {
VisibleSelection selectionInUserSelectAll = expandSelectionToRespectSelectOnMouseDown(*targetNode, VisibleSelection(pos));
if (selectionInUserSelectAll.isRange()) {
if (selectionInUserSelectAll.start() < newSelection.start())
pos = selectionInUserSelectAll.start();
else if (newSelection.end() < selectionInUserSelectAll.end())
pos = selectionInUserSelectAll.end();
}
if (!frame->editor().behavior().shouldConsiderSelectionAsDirectional() && pos.isNotNull()) {
// See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection
// was created right-to-left
Position start = newSelection.start();
Position end = newSelection.end();
int distanceToStart = textDistance(start, pos);
int distanceToEnd = textDistance(pos, end);
if (distanceToStart <= distanceToEnd)
newSelection = VisibleSelection(end, pos);
else
newSelection = VisibleSelection(start, pos);
} else {
if (newSelection.directionality() == Directionality::Strong) {
RefPtr baseNode = newSelection.isBaseFirst() ? newSelection.base().computeNodeAfterPosition() : newSelection.base().computeNodeBeforePosition();
if (!baseNode)
baseNode = newSelection.base().containerNode();
if (baseNode) {
auto expandedBaseSelection = expandSelectionToRespectSelectOnMouseDown(*baseNode, VisibleSelection { newSelection.visibleBase() });
expandedBaseSelection.expandUsingGranularity(frame->selection().granularity());
if (expandedBaseSelection.isRange()) {
if (newSelection.isBaseFirst() && pos < newSelection.start())
newSelection.setBase(expandedBaseSelection.end());
else if (!newSelection.isBaseFirst() && newSelection.end() < pos)
newSelection.setBase(expandedBaseSelection.start());
}
}
}
newSelection.setExtent(pos);
}
if (frame->selection().granularity() != TextGranularity::CharacterGranularity) {
granularity = frame->selection().granularity();
newSelection.expandUsingGranularity(frame->selection().granularity());
}
} else {
if (event.event().syntheticClickType() != SyntheticClickType::NoTap) {
auto adjustedVisiblePosition = wordBoundaryForPositionWithoutCrossingLine(visiblePosition).first;
if (adjustedVisiblePosition.isNotNull())
visiblePosition = WTFMove(adjustedVisiblePosition);
}
newSelection = expandSelectionToRespectSelectOnMouseDown(*targetNode, visiblePosition);
}
return updateSelectionForMouseDownDispatchingSelectStart(targetNode.get(), newSelection, granularity);
}
bool EventHandler::canMouseDownStartSelect(const MouseEventWithHitTestResults& event)
{
RefPtr node = event.targetNode();
if (RefPtr page = m_frame->page()) {
if (!page->chrome().client().shouldUseMouseEventForSelection(event.event()))
return false;
}
if (!node || !node->renderer())
return true;
if (node->protectedDocument()->quirks().shouldAvoidStartingSelectionOnMouseDown(*node))
return false;
if (ImageOverlay::isOverlayText(*node))
return node->renderer()->style().usedUserSelect() != UserSelect::None;
return node->canStartSelection() || Position::nodeIsUserSelectAll(node.get());
}
bool EventHandler::mouseDownMayStartSelect() const
{
if (!m_frame->settings().textInteractionEnabled())
return false;
return m_mouseDownMayStartSelect;
}
bool EventHandler::handleMousePressEvent(const MouseEventWithHitTestResults& event)
{
Ref frame = m_frame.get();
#if ENABLE(DRAG_SUPPORT)
// Reset drag state.
setDragStateSource(nullptr);
#endif
#if !ENABLE(IOS_TOUCH_EVENTS)
cancelFakeMouseMoveEvent();
#endif
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
if (RefPtr scrollView = frame->view()) {
if (scrollView->isPointInScrollbarCorner(event.event().position()))
return false;
}
bool singleClick = event.event().clickCount() <= 1;
// If we got the event back, that must mean it wasn't prevented,
// so it's allowed to start a drag or selection if it wasn't in a scrollbar.
m_mouseDownMayStartSelect = canMouseDownStartSelect(event) && !event.scrollbar();
#if ENABLE(DRAG_SUPPORT)
// Careful that the drag starting logic stays in sync with eventMayStartDrag()
// FIXME: eventMayStartDrag() does not check for shift key press, link or image event targets.
// Bug: https://bugs.webkit.org/show_bug.cgi?id=155390
// Single mouse down on links or images can always trigger drag-n-drop.
bool isImageOverlayText = ImageOverlay::isOverlayText(event.protectedTargetNode().get());
bool isMouseDownOnLinkOrImage = event.isOverLink() || (event.hitTestResult().image() && !isImageOverlayText);
m_mouseDownMayStartDrag = singleClick && (!event.event().shiftKey() || isMouseDownOnLinkOrImage) && shouldAllowMouseDownToStartDrag();
#endif
m_mouseDownWasSingleClickInSelection = false;
m_mouseDownEvent = event.event();
if (m_immediateActionStage != ImmediateActionStage::PerformedHitTest)
m_immediateActionStage = ImmediateActionStage::None;
if (event.isOverWidget() && passWidgetMouseDownEventToWidget(event))
return true;
if (RefPtr svgDocument = dynamicDowncast<SVGDocument>(*frame->protectedDocument()); svgDocument && svgDocument->zoomAndPanEnabled()) {
if (event.event().shiftKey() && singleClick) {
m_svgPan = true;
svgDocument->startPan(frame->protectedView()->windowToContents(event.event().position()));
return true;
}
}
// We don't do this at the start of mouse down handling,
// because we don't want to do it until we know we didn't hit a widget.
if (singleClick)
focusDocumentView();
m_mousePressNode = event.targetNode();
frame->protectedDocument()->setFocusNavigationStartingNode(event.protectedTargetNode().get());
#if ENABLE(DRAG_SUPPORT)
m_dragStartPosition = event.event().position();
#endif
m_mousePressed = true;
m_selectionInitiationState = HaveNotStartedSelection;
bool swallowEvent = false;
if (event.event().clickCount() == 2)
swallowEvent = handleMousePressEventDoubleClick(event);
else if (event.event().clickCount() >= 3)
swallowEvent = handleMousePressEventTripleClick(event);
else
swallowEvent = handleMousePressEventSingleClick(event);
m_mouseDownMayStartAutoscroll = mouseDownMayStartSelect()
|| (m_mousePressNode && m_mousePressNode->renderBox() && m_mousePressNode->renderBox()->canBeProgramaticallyScrolled());
return swallowEvent;
}
VisiblePosition EventHandler::selectionExtentRespectingEditingBoundary(const VisibleSelection& selection, const LayoutPoint& localPoint, Node* targetNode)
{
FloatPoint selectionEndPoint = localPoint;
RefPtr editableElement = selection.rootEditableElement();
if (!targetNode || !targetNode->renderer())
return VisiblePosition();
RefPtr adjustedTarget = targetNode;
if (editableElement && !editableElement->contains(targetNode)) {
if (!editableElement->renderer())
return VisiblePosition();
FloatPoint absolutePoint = targetNode->renderer()->localToAbsolute(FloatPoint(selectionEndPoint));
selectionEndPoint = editableElement->renderer()->absoluteToLocal(absolutePoint);
adjustedTarget = editableElement;
}
return adjustedTarget->renderer()->positionForPoint(LayoutPoint(selectionEndPoint), HitTestSource::User, nullptr);
}
#if ENABLE(DRAG_SUPPORT)
#if !PLATFORM(IOS_FAMILY)
bool EventHandler::supportsSelectionUpdatesOnMouseDrag() const
{
return true;
}
bool EventHandler::shouldAllowMouseDownToStartDrag() const
{
return true;
}
#endif
bool EventHandler::handleMouseDraggedEvent(const MouseEventWithHitTestResults& event, CheckDragHysteresis checkDragHysteresis)
{
if (!m_mousePressed)
return false;
Ref frame = m_frame.get();
if (handleDrag(event, checkDragHysteresis))
return true;
RefPtr targetNode = event.targetNode();
if (event.event().button() != MouseButton::Left || !targetNode)
return false;
RenderObject* renderer = targetNode->renderer();
if (!renderer) {
RefPtr parent = targetNode->parentOrShadowHostElement();
if (!parent)
return false;
renderer = parent->renderer();
if (!renderer || !renderer->isRenderListBox())
return false;
}
#if PLATFORM(COCOA) // FIXME: Why does this assertion fire on other platforms?
ASSERT(mouseDownMayStartSelect() || m_mouseDownMayStartAutoscroll);
#endif
m_mouseDownMayStartDrag = false;
if (m_mouseDownMayStartAutoscroll && !panScrollInProgress()) {
m_autoscrollController->startAutoscrollForSelection(renderer);
m_mouseDownMayStartAutoscroll = false;
}
if (m_selectionInitiationState != ExtendedSelection) {
HitTestResult result(m_mouseDownContentsPosition);
frame->protectedDocument()->hitTest(HitTestRequest(), result);
updateSelectionForMouseDrag(result);
} else
event.targetNode()->protectedDocument()->updateStyleIfNeeded();
updateSelectionForMouseDrag(event.hitTestResult());
return true;
}
bool EventHandler::eventMayStartDrag(const PlatformMouseEvent& event) const
{
// This is a pre-flight check of whether the event might lead to a drag being started. Be careful
// that its logic needs to stay in sync with handleMouseMoveEvent() and the way we setMouseDownMayStartDrag
// in handleMousePressEvent
Ref frame = m_frame.get();
RefPtr document = frame->document();
if (!document)
return false;
if (event.button() != MouseButton::Left || event.clickCount() != 1)
return false;
RefPtr view = frame->view();
if (!view)
return false;
RefPtr page = frame->page();
if (!page)
return false;
updateDragSourceActionsAllowed();
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::DisallowUserAgentShadowContent };
HitTestResult result(view->windowToContents(event.position()));
document->hitTest(hitType, result);
DragState state;
RefPtr targetElement = result.targetElement();
return targetElement && page->dragController().draggableElement(frame.ptr(), targetElement.get(), result.roundedPointInInnerNodeFrame(), state);
}
void EventHandler::updateSelectionForMouseDrag()
{
if (!supportsSelectionUpdatesOnMouseDrag())
return;
RefPtr view = m_frame->view();
if (!view)
return;
RefPtr document = m_frame->document();
if (!document)
return;
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::Active, HitTestRequest::Type::Move, HitTestRequest::Type::DisallowUserAgentShadowContent };
HitTestResult result(view->windowToContents(valueOrDefault(m_lastKnownMousePosition)));
document->hitTest(hitType, result);
updateSelectionForMouseDrag(result);
}
void EventHandler::updateSelectionForMouseDrag(const HitTestResult& hitTestResult)
{
if (!supportsSelectionUpdatesOnMouseDrag())
return;
if (!mouseDownMayStartSelect())
return;
RefPtr target = hitTestResult.targetNode();
if (!target)
return;
if (!HTMLElement::shouldExtendSelectionToTargetNode(*target, m_frame->selection().selection()))
return;
VisiblePosition targetPosition = selectionExtentRespectingEditingBoundary(m_frame->selection().selection(), hitTestResult.localPoint(), target.get());
// Don't modify the selection if we're not on a node.
if (targetPosition.isNull())
return;
// Restart the selection if this is the first mouse move. This work is usually
// done in handleMousePressEvent, but not if the mouse press was on an existing selection.
VisibleSelection oldSelection = m_frame->selection().selection();
auto newSelection = oldSelection;
// Special case to limit selection to the containing block for SVG text.
// FIXME: Isn't there a better non-SVG-specific way to do this?
if (RefPtr selectionBaseNode = newSelection.base().deprecatedNode()) {
if (RenderObject* selectionBaseRenderer = selectionBaseNode->renderer()) {
if (selectionBaseRenderer->isRenderSVGText()) {
if (target->renderer()->containingBlock() != selectionBaseRenderer->containingBlock())
return;
}
}
}
if (m_selectionInitiationState == HaveNotStartedSelection && !dispatchSelectStart(target.get())) {
m_mouseDownMayStartSelect = false;
return;
}
bool shouldSetDragStartSelection = false;
if (m_selectionInitiationState != ExtendedSelection) {
// Always extend selection here because it's caused by a mouse drag
m_selectionInitiationState = ExtendedSelection;
newSelection = VisibleSelection(targetPosition);
shouldSetDragStartSelection = true;
}
RefPtr rootUserSelectAllForMousePressNode = Position::rootUserSelectAllForNode(m_mousePressNode.get());
if (rootUserSelectAllForMousePressNode && rootUserSelectAllForMousePressNode == Position::rootUserSelectAllForNode(target.get())) {
newSelection.setBase(positionBeforeNode(rootUserSelectAllForMousePressNode.get()).upstream(CanCrossEditingBoundary));
newSelection.setExtent(positionAfterNode(rootUserSelectAllForMousePressNode.get()).downstream(CanCrossEditingBoundary));
} else {
// Reset base for user select all when base is inside user-select-all area and extent < base.
if (rootUserSelectAllForMousePressNode && target->renderer()->positionForPoint(hitTestResult.localPoint(), HitTestSource::User, nullptr) < m_mousePressNode->renderer()->positionForPoint(m_dragStartPosition, HitTestSource::User, nullptr))
newSelection.setBase(positionAfterNode(rootUserSelectAllForMousePressNode.get()).downstream(CanCrossEditingBoundary));
RefPtr rootUserSelectAllForTarget = Position::rootUserSelectAllForNode(target.get());
if (rootUserSelectAllForTarget && m_mousePressNode->renderer() && target->renderer()->positionForPoint(hitTestResult.localPoint(), HitTestSource::User, nullptr) < m_mousePressNode->renderer()->positionForPoint(m_dragStartPosition, HitTestSource::User, nullptr))
newSelection.setExtent(positionBeforeNode(rootUserSelectAllForTarget.get()).upstream(CanCrossEditingBoundary));
else if (rootUserSelectAllForTarget && m_mousePressNode->renderer())
newSelection.setExtent(positionAfterNode(rootUserSelectAllForTarget.get()).downstream(CanCrossEditingBoundary));
else
newSelection.setExtent(targetPosition);
}
if (m_frame->selection().granularity() != TextGranularity::CharacterGranularity) {
newSelection.expandUsingGranularity(m_frame->selection().granularity());
if (!newSelection.isBaseFirst() && !oldSelection.isBaseFirst() && oldSelection.end() < newSelection.end())
newSelection.setBase(oldSelection.end());
else if (newSelection.isBaseFirst() && !oldSelection.isBaseFirst() && oldSelection.start() < newSelection.start() && m_dragStartSelection && m_dragStartSelection->start.container && m_dragStartSelection->end.container) {
VisibleSelection dragStartSelection { createSimpleRangeFromDragStartSelection() };
dragStartSelection.expandUsingGranularity(m_frame->selection().granularity());
if (!dragStartSelection.isNoneOrOrphaned())
newSelection.setBase(dragStartSelection.start());
}
}
if (shouldSetDragStartSelection)
m_dragStartSelection = getWeakSimpleRangeFromSelection(newSelection);
m_frame->selection().setSelectionByMouseIfDifferent(newSelection, m_frame->selection().granularity(),
FrameSelection::EndPointsAdjustmentMode::AdjustAtBidiBoundary);
if (oldSelection != newSelection && ImageOverlay::isOverlayText(newSelection.start().protectedContainerNode().get()) && ImageOverlay::isOverlayText(newSelection.end().protectedContainerNode().get()))
invalidateClick();
}
SimpleRange EventHandler::createSimpleRangeFromDragStartSelection() const
{
const WeakSimpleRange& range = m_dragStartSelection.value();
return { BoundaryPoint(*(range.start.container), range.start.offset), BoundaryPoint(*(range.end.container), range.end.offset) };
}
std::optional<WeakSimpleRange> EventHandler::getWeakSimpleRangeFromSelection(const VisibleSelection& selection) const
{
if (auto range = selection.range())
return range->makeWeakSimpleRange();
return std::nullopt;
}
#endif // ENABLE(DRAG_SUPPORT)
void EventHandler::lostMouseCapture()
{
protectedFrame()->selection().setCaretBlinkingSuspended(false);
}
bool EventHandler::handleMouseUp(const MouseEventWithHitTestResults& event)
{
if (eventLoopHandleMouseUp(event))
return true;
// If this was the first click in the window, we don't even want to clear the selection.
// This case occurs when the user clicks on a draggable element, since we have to process
// the mouse down and drag events to see if we might start a drag. For other first clicks
// in a window, we just don't acceptFirstMouse, and the whole down-drag-up sequence gets
// ignored upstream of this layer.
return eventActivatedView(event.event());
}
bool EventHandler::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event)
{
if (autoscrollInProgress())
stopAutoscrollTimer();
Ref frame = m_frame.get();
if (handleMouseUp(event))
return true;
// Used to prevent mouseMoveEvent from initiating a drag before
// the mouse is pressed again.
m_mousePressed = false;
m_capturesDragging = false;
#if ENABLE(DRAG_SUPPORT)
m_mouseDownMayStartDrag = false;
#endif
m_mouseDownMayStartSelect = false;
m_mouseDownMayStartAutoscroll = false;
m_mouseDownWasInSubframe = false;
bool handled = false;
// Clear the selection if the mouse didn't move after the last mouse
// press and it's not a context menu click. We do this so when clicking
// on the selection, the selection goes away. However, if we are
// editing, place the caret.
if (m_mouseDownWasSingleClickInSelection && m_selectionInitiationState != ExtendedSelection
#if ENABLE(DRAG_SUPPORT)
&& m_dragStartPosition == event.event().position()
#endif
&& frame->selection().isRange()
&& event.event().button() != MouseButton::Right) {
VisibleSelection newSelection;
RefPtr node = event.targetNode();
bool caretBrowsing = frame->settings().caretBrowsingEnabled();
bool allowSelectionChanges = true;
if (node && node->renderer() && (caretBrowsing || node->hasEditableStyle())) {
VisiblePosition pos = node->renderer()->positionForPoint(event.localPoint(), HitTestSource::User, nullptr);
newSelection = VisibleSelection(pos);
#if PLATFORM(IOS_FAMILY)
// On iOS, selection changes are triggered using platform-specific text interaction gestures rather than
// default behavior on click or mouseup. As such, the only time we should allow click events to change the
// selection on iOS is when we focus a different editable element, in which case the text interaction
// gestures will fail.
allowSelectionChanges = frame->selection().selection().rootEditableElement() != newSelection.rootEditableElement();
#endif
}
if (allowSelectionChanges)
setSelectionIfNeeded(frame->selection(), newSelection);
handled = true;
}
// If the event was a middle click, attempt to copy global selection in after
// the newly set caret position.
//
// There is some debate about when the global selection is pasted:
// xterm: pastes on up.
// GTK: pastes on down.
// Qt: pastes on up.
// Firefox: pastes on up.
// Chromium: pastes on up.
//
// However, WebKitGTK actually needs to paste on up to avoid clashing with
// mouse gestures, https://gitlab.gnome.org/GNOME/epiphany/-/issues/1814. So
// let's always paste on up, and forget about matching GTK.
//
// There is something of a webcompat angle to this well, as highlighted by
// crbug.com/14608. Pages can clear text boxes 'onclick' and, if we paste on
// down then the text is pasted just before the onclick handler runs and
// clears the text box. So it's important this happens after the event
// handlers have been fired.
if (event.event().button() == MouseButton::Middle) {
// Ignore handled, since we want to paste to where the caret was placed anyway.
handled = handlePasteGlobalSelection() || handled;
}
return handled;
}
#if ENABLE(PAN_SCROLLING)
void EventHandler::didPanScrollStart()
{
m_autoscrollController->didPanScrollStart();
}
void EventHandler::didPanScrollStop()
{
m_autoscrollController->didPanScrollStop();
}
void EventHandler::startPanScrolling(RenderElement& renderer)
{
CheckedPtr renderBox = dynamicDowncast<RenderBox>(renderer);
if (!renderBox)
return;
m_autoscrollController->startPanScrolling(*renderBox, lastKnownMousePosition());
invalidateClick();
}
#endif // ENABLE(PAN_SCROLLING)
RenderBox* EventHandler::autoscrollRenderer() const
{
return m_autoscrollController->autoscrollRenderer();
}
void EventHandler::updateAutoscrollRenderer()
{
m_autoscrollController->updateAutoscrollRenderer();
}
bool EventHandler::autoscrollInProgress() const
{
return m_autoscrollController->autoscrollInProgress();
}
bool EventHandler::panScrollInProgress() const
{
return m_autoscrollController->panScrollInProgress();
}
#if ENABLE(DRAG_SUPPORT)
OptionSet<DragSourceAction> EventHandler::updateDragSourceActionsAllowed() const
{
RefPtr page = m_frame->page();
if (!page)
return { };
RefPtr view = m_frame->view();
if (!view)
return { };
return page->dragController().delegateDragSourceAction(view->contentsToRootView(m_mouseDownContentsPosition));
}
#endif // ENABLE(DRAG_SUPPORT)
HitTestResult EventHandler::hitTestResultAtPoint(const LayoutPoint& point, OptionSet<HitTestRequest::Type> hitType) const
{
Ref frame = m_frame.get();
// We always send hitTestResultAtPoint to the main frame if we have one,
// otherwise we might hit areas that are obscured by higher frames.
if (!frame->isMainFrame()) {
if (RefPtr mainFrame = dynamicDowncast<LocalFrame>(frame->mainFrame())) {
if (RefPtr frameView = frame->view(), mainView = mainFrame->view(); frameView && mainView) {
IntPoint mainFramePoint = mainView->rootViewToContents(frameView->contentsToRootView(roundedIntPoint(point)));
return mainFrame->eventHandler().hitTestResultAtPoint(mainFramePoint, hitType);
}
}
}
// We should always start hit testing a clean tree.
if (RefPtr frameView = frame->view())
frameView->updateLayoutAndStyleIfNeededRecursive();
auto result = HitTestResult { point };
RefPtr document = frame->document();
if (!document)
return result;
HitTestRequest request(hitType);
document->hitTest(request, result);
if (!request.readOnly())
frame->protectedDocument()->updateHoverActiveState(request, result.protectedTargetElement().get());
RefPtr innerNode = result.innerNode();
if (request.disallowsUserAgentShadowContent()
|| (request.disallowsUserAgentShadowContentExceptForImageOverlays() && innerNode && !ImageOverlay::isInsideOverlay(*innerNode)))
result.setToNonUserAgentShadowAncestor();
return result;
}
void EventHandler::stopAutoscrollTimer(bool rendererIsBeingDestroyed)
{
m_autoscrollController->stopAutoscrollTimer(rendererIsBeingDestroyed);
}
bool EventHandler::scrollOverflow(ScrollDirection direction, ScrollGranularity granularity, Node* startingNode)
{
RefPtr node = startingNode;
if (!node)
node = m_frame->document()->focusedElement();
if (!node)
node = m_mousePressNode.get();
if (node) {
auto r = node->renderer();
if (r && !r->isRenderListBox() && r->enclosingBox().scroll(direction, granularity)) {
setFrameWasScrolledByUser();
return true;
}
}
return false;
}
bool EventHandler::logicalScrollOverflow(ScrollLogicalDirection direction, ScrollGranularity granularity, Node* startingNode)
{
RefPtr node = startingNode;
if (!node)
node = m_frame->document()->focusedElement();
if (!node)
node = m_mousePressNode.get();
if (node) {
auto r = node->renderer();
if (r && !r->isRenderListBox() && r->enclosingBox().logicalScroll(direction, granularity)) {
setFrameWasScrolledByUser();
return true;
}
}
return false;
}
bool EventHandler::scrollRecursively(ScrollDirection direction, ScrollGranularity granularity, Node* startingNode)
{
// The layout needs to be up to date to determine if we can scroll. We may be
// here because of an onLoad event, in which case the final layout hasn't been performed yet.
Ref frame = m_frame.get();
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
if (scrollOverflow(direction, granularity, startingNode))
return true;
RefPtr view = frame->view();
if (view && view->scroll(direction, granularity))
return true;
RefPtr parent = frame->tree().parent();
if (!parent)
return false;
RefPtr localParent = dynamicDowncast<LocalFrame>(parent.get());
if (!localParent)
return false;
return localParent->checkedEventHandler()->scrollRecursively(direction, granularity, frame->protectedOwnerElement().get());
}
bool EventHandler::logicalScrollRecursively(ScrollLogicalDirection direction, ScrollGranularity granularity, Node* startingNode)
{
Ref frame = m_frame.get();
// The layout needs to be up to date to determine if we can scroll. We may be
// here because of an onLoad event, in which case the final layout hasn't been performed yet.
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
if (logicalScrollOverflow(direction, granularity, startingNode))
return true;
RefPtr view = frame->view();
bool scrolled = false;
#if PLATFORM(COCOA)
// Mac also resets the scroll position in the inline direction.
if (granularity == ScrollGranularity::Document && view && view->logicalScroll(ScrollInlineDirectionBackward, ScrollGranularity::Document))
scrolled = true;
#endif
if (view && view->logicalScroll(direction, granularity))
scrolled = true;
if (scrolled)
return true;
RefPtr parent = frame->tree().parent();
if (!parent)
return false;
RefPtr localParent = dynamicDowncast<LocalFrame>(parent.get());
if (!localParent)
return false;
return localParent->checkedEventHandler()->logicalScrollRecursively(direction, granularity, frame->protectedOwnerElement().get());
}
IntPoint EventHandler::lastKnownMousePosition() const
{
return valueOrDefault(m_lastKnownMousePosition);
}
RefPtr<Frame> EventHandler::subframeForHitTestResult(const MouseEventWithHitTestResults& hitTestResult)
{
if (!hitTestResult.isOverWidget())
return nullptr;
return subframeForTargetNode(hitTestResult.protectedTargetNode().get());
}
RefPtr<Frame> EventHandler::subframeForTargetNode(Node* node)
{
if (!node)
return nullptr;
CheckedPtr renderWidget = dynamicDowncast<RenderWidget>(node->renderer());
if (!renderWidget)
return nullptr;
auto* frameView = dynamicDowncast<FrameView>(renderWidget->widget());
if (!frameView)
return nullptr;
return &frameView->frame();
}
static bool isSubmitImage(Node* node)
{
RefPtr input = dynamicDowncast<HTMLInputElement>(node);
return input && input->isImageButton();
}
// Returns true if the node's editable block is not current focused for editing
static bool nodeIsNotBeingEdited(const Node& node, const LocalFrame& frame)
{
return frame.selection().selection().rootEditableElement() != node.rootEditableElement();
}
bool EventHandler::useHandCursor(Node* node, bool isOverLink, bool shiftKey)
{
if (!node)
return false;
bool editable = node->hasEditableStyle();
bool editableLinkEnabled = false;
// If the link is editable, then we need to check the settings to see whether or not the link should be followed
if (editable) {
switch (m_frame->settings().editableLinkBehavior()) {
case EditableLinkBehavior::Default:
case EditableLinkBehavior::AlwaysLive:
editableLinkEnabled = true;
break;
case EditableLinkBehavior::NeverLive:
editableLinkEnabled = false;
break;
case EditableLinkBehavior::LiveWhenNotFocused:
editableLinkEnabled = nodeIsNotBeingEdited(*node, protectedFrame()) || shiftKey;
break;
case EditableLinkBehavior::OnlyLiveWithShiftKey:
editableLinkEnabled = shiftKey;
break;
}
}
return ((isOverLink || isSubmitImage(node)) && (!editable || editableLinkEnabled));
}
void EventHandler::updateCursorIfNeeded()
{
if (std::exchange(m_hasScheduledCursorUpdate, false))
updateCursor();
}
void EventHandler::updateCursor()
{
if (!m_lastKnownMousePosition)
return;
if (Page* page = m_frame->page()) {
if (!page->chrome().client().supportsSettingCursor())
return;
}
RefPtr view = m_frame->view();
if (!view)
return;
RefPtr document = m_frame->document();
if (!document)
return;
if (!view->shouldSetCursor())
return;
bool shiftKey;
bool ctrlKey;
bool altKey;
bool metaKey;
PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::AllowFrameScrollbars };
HitTestResult result(view->windowToContents(*m_lastKnownMousePosition));
document->hitTest(hitType, result);
updateCursor(*view, result, shiftKey);
}
void EventHandler::updateCursor(LocalFrameView& view, const HitTestResult& result, bool shiftKey)
{
if (auto optionalCursor = selectCursor(result, shiftKey)) {
m_currentMouseCursor = WTFMove(optionalCursor.value());
view.setCursor(m_currentMouseCursor);
}
}
std::optional<Cursor> EventHandler::selectCursor(const HitTestResult& result, bool shiftKey)
{
if (m_resizeLayer && m_resizeLayer->inResizeMode())
return std::nullopt;
if (!m_frame->page())
return std::nullopt;
#if ENABLE(PAN_SCROLLING)
auto* localFrame = dynamicDowncast<LocalFrame>(m_frame->mainFrame());
if (!localFrame)
return std::nullopt;
if (localFrame->eventHandler().panScrollInProgress())
return std::nullopt;
#endif
Ref frame = m_frame.get();
// Use always pointer cursor for scrollbars.
if (result.scrollbar()) {
#if ENABLE(CURSOR_VISIBILITY)
cancelAutoHideCursorTimer();
#endif
return pointerCursor();
}
RefPtr node = result.targetNode();
if (!node)
return std::nullopt;
auto renderer = node->renderer();
auto* style = renderer ? &renderer->style() : nullptr;
bool horizontalText = !style || style->writingMode().isHorizontal();
const Cursor& iBeam = horizontalText ? iBeamCursor() : verticalTextCursor();
// area element has display: none set by default, should use node to get style instead of renderer.
if (is<HTMLAreaElement>(node))
style = node->computedStyle();
#if ENABLE(CURSOR_VISIBILITY)
if (style && style->cursorVisibility() == CursorVisibility::AutoHide)
startAutoHideCursorTimer();
else
cancelAutoHideCursorTimer();
#endif
if (renderer) {
Cursor overrideCursor;
switch (renderer->getCursor(roundedIntPoint(result.localPoint()), overrideCursor)) {
case SetCursorBasedOnStyle:
break;
case SetCursor:
return overrideCursor;
case DoNotSetCursor:
return std::nullopt;
}
}
if (style && style->cursors()) {
const CursorList* cursors = style->cursors();
for (unsigned i = 0; i < cursors->size(); ++i) {
StyleImage* styleImage = (*cursors)[i].image();
if (!styleImage)
continue;
CachedImage* cachedImage = styleImage->cachedImage();
if (!cachedImage)
continue;
float scale = styleImage->imageScaleFactor();
// Get hotspot and convert from logical pixels to physical pixels.
IntPoint hotSpot = (*cursors)[i].hotSpot();
FloatSize size = cachedImage->imageForRenderer(renderer)->size();
if (cachedImage->errorOccurred())
continue;
// Limit the size of cursors (in UI pixels) so that they cannot be
// used to cover UI elements in chrome.
size.scale(1 / scale);
if (size.width() > maximumCursorSize || size.height() > maximumCursorSize)
continue;
Image* image = cachedImage->imageForRenderer(renderer);
#if ENABLE(MOUSE_CURSOR_SCALE)
// Ensure no overflow possible in calculations above.
if (scale < minimumCursorScale)
continue;
return Cursor(image, hotSpot, scale);
#else
ASSERT(scale == 1);
return Cursor(image, hotSpot);
#endif // ENABLE(MOUSE_CURSOR_SCALE)
}
}
switch (style ? style->cursor() : CursorType::Auto) {
case CursorType::Auto: {
if (ImageOverlay::isOverlayText(node.get())) {
auto* renderer = node->renderer();
if (renderer && renderer->style().usedUserSelect() != UserSelect::None)
return iBeam;
}
bool editable = node->hasEditableStyle();
if (useHandCursor(node.get(), result.isOverLink(), shiftKey))
return handCursor();
bool inResizer = false;
if (renderer && renderer->hasLayer()) {
// FIXME: With right-aligned text in a box, the renderer here is usually a RenderText, which prevents showing the resize cursor: webkit.org/b/210935.
auto& layerRenderer = downcast<RenderLayerModelObject>(*renderer);
inResizer = layerRenderer.layer()->isPointInResizeControl(roundedIntPoint(result.localPoint()));
if (inResizer)
return layerRenderer.shouldPlaceVerticalScrollbarOnLeft() ? southWestResizeCursor() : southEastResizeCursor();
}
// During selection, use an I-beam regardless of the content beneath the cursor.
// If a drag may be starting or we're capturing mouse events for a particular node, don't treat this as a selection.
if (m_mousePressed
&& mouseDownMayStartSelect()
#if ENABLE(DRAG_SUPPORT)
&& !m_mouseDownMayStartDrag
#endif
&& frame->selection().isCaretOrRange()
&& !m_capturingMouseEventsElement && renderer && renderer->style().usedUserSelect() != UserSelect::None)
return iBeam;
if ((editable || (renderer && renderer->isRenderText() && node->canStartSelection() && renderer->style().usedUserSelect() != UserSelect::None)) && !inResizer && !result.scrollbar())
return iBeam;
return pointerCursor();
}
case CursorType::Default:
return pointerCursor();
case CursorType::None:
return noneCursor();
case CursorType::ContextMenu:
return contextMenuCursor();
case CursorType::Help:
return helpCursor();
case CursorType::Pointer:
return handCursor();
case CursorType::Progress:
return progressCursor();
case CursorType::Wait:
return waitCursor();
case CursorType::Cell:
return cellCursor();
case CursorType::Crosshair:
return crossCursor();
case CursorType::Text:
return iBeamCursor();
case CursorType::VerticalText:
return verticalTextCursor();
case CursorType::Alias:
return aliasCursor();
case CursorType::Copy:
return copyCursor();
case CursorType::Move:
return moveCursor();
case CursorType::NoDrop:
return noDropCursor();
case CursorType::NotAllowed:
return notAllowedCursor();
case CursorType::Grab:
return grabCursor();
case CursorType::Grabbing:
return grabbingCursor();
case CursorType::EResize:
return eastResizeCursor();
case CursorType::NResize:
return northResizeCursor();
case CursorType::NEResize:
return northEastResizeCursor();
case CursorType::NWResize:
return northWestResizeCursor();
case CursorType::SResize:
return southResizeCursor();
case CursorType::SEResize:
return southEastResizeCursor();
case CursorType::SWResize:
return southWestResizeCursor();
case CursorType::WResize:
return westResizeCursor();
case CursorType::EWResize:
return eastWestResizeCursor();
case CursorType::NSResize:
return northSouthResizeCursor();
case CursorType::NESWResize:
return northEastSouthWestResizeCursor();
case CursorType::NWSEResize:
return northWestSouthEastResizeCursor();
case CursorType::ColumnResize:
return columnResizeCursor();
case CursorType::RowResize:
return rowResizeCursor();
case CursorType::AllScroll:
return moveCursor();
case CursorType::ZoomIn:
return zoomInCursor();
case CursorType::ZoomOut:
return zoomOutCursor();
}
return pointerCursor();
}
#if ENABLE(CURSOR_VISIBILITY)
void EventHandler::startAutoHideCursorTimer()
{
RefPtr page = m_frame->page();
if (!page)
return;
m_autoHideCursorTimer.startOneShot(page->settings().timeWithoutMouseMovementBeforeHidingControls());
#if !ENABLE(IOS_TOUCH_EVENTS)
// The fake mouse move event screws up the auto-hide feature (by resetting the auto-hide timer)
// so cancel any pending fake mouse moves.
if (m_fakeMouseMoveEventTimer.isActive())
m_fakeMouseMoveEventTimer.stop();
#endif
}
void EventHandler::cancelAutoHideCursorTimer()
{
if (m_autoHideCursorTimer.isActive())
m_autoHideCursorTimer.stop();
}
void EventHandler::autoHideCursorTimerFired()
{
RefPtr view = m_frame->view();
if (!view || !view->isActive())
return;
if (RefPtr page = m_frame->page())
page->chrome().setCursorHiddenUntilMouseMoves(true);
}
#endif
static LayoutPoint documentPointForWindowPoint(LocalFrame& frame, const IntPoint& windowPoint)
{
auto* view = frame.view();
// FIXME: Is it really OK to use the wrong coordinates here when view is 0?
// Historically the code would just crash; this is clearly no worse than that.
return view ? view->windowToContents(windowPoint) : windowPoint;
}
std::optional<RemoteUserInputEventData> EventHandler::userInputEventDataForRemoteFrame(const RemoteFrame* remoteFrame, const IntPoint& pointInFrame)
{
if (!remoteFrame)
return std::nullopt;
RefPtr frameView = m_frame->view();
if (!frameView)
return std::nullopt;
RefPtr remoteFrameView = remoteFrame->view();
if (!remoteFrameView)
return std::nullopt;
return RemoteUserInputEventData {
remoteFrame->frameID(),
remoteFrameView->rootViewToContents(frameView->contentsToRootView(pointInFrame))
};
}
static Scrollbar* scrollbarForMouseEvent(const MouseEventWithHitTestResults& mouseEvent, LocalFrameView* view)
{
if (view) {
if (auto* scrollbar = view->scrollbarAtPoint(mouseEvent.event().position()))
return scrollbar;
}
return mouseEvent.scrollbar();
}
HandleUserInputEventResult EventHandler::handleMousePressEvent(const PlatformMouseEvent& platformMouseEvent)
{
Ref frame = m_frame.get();
RefPtr protectedView { frame->view() };
if (InspectorInstrumentation::handleMousePress(frame)) {
invalidateClick();
return true;
}
#if ENABLE(POINTER_LOCK)
if (frame->page()->pointerLockController().isLocked()) {
frame->protectedPage()->pointerLockController().dispatchLockedMouseEvent(platformMouseEvent, eventNames().mousedownEvent);
return true;
}
#endif
if (frame->protectedPage()->pageOverlayController().handleMouseEvent(platformMouseEvent))
return true;
#if ENABLE(TOUCH_EVENTS)
bool defaultPrevented = dispatchSyntheticTouchEventIfEnabled(platformMouseEvent).wasHandled();
if (defaultPrevented)
return true;
#endif
UserGestureIndicator gestureIndicator(IsProcessingUserGesture::Yes, frame->protectedDocument().get(), userGestureTypeForPlatformEvent(platformMouseEvent), UserGestureIndicator::ProcessInteractionStyle::Immediate, platformMouseEvent.authorizationToken());
// FIXME (bug 68185): this call should be made at another abstraction layer
frame->protectedLoader()->resetMultipleFormSubmissionProtection();
#if !ENABLE(IOS_TOUCH_EVENTS)
cancelFakeMouseMoveEvent();
#endif
if (m_eventHandlerWillResetCapturingMouseEventsElement)
resetCapturingMouseEventsElement();
m_mousePressed = true;
m_capturesDragging = true;
setLastKnownMousePosition(platformMouseEvent.position(), platformMouseEvent.globalPosition());
m_mouseDownTimestamp = platformMouseEvent.timestamp();
#if ENABLE(DRAG_SUPPORT)
m_mouseDownMayStartDrag = false;
#endif
m_mouseDownMayStartSelect = false;
m_mouseDownMayStartAutoscroll = false;
if (RefPtr view = frame->view())
m_mouseDownContentsPosition = view->windowToContents(platformMouseEvent.position());
else {
invalidateClick();
return false;
}
m_mouseDownWasInSubframe = false;
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::Active, HitTestRequest::Type::DisallowUserAgentShadowContent };
// Save the document point we generate in case the window coordinate is invalidated by what happens
// when we dispatch the event.
LayoutPoint documentPoint = documentPointForWindowPoint(frame, platformMouseEvent.position());
MouseEventWithHitTestResults mouseEvent = frame->protectedDocument()->prepareMouseEvent(hitType, documentPoint, platformMouseEvent);
if (!mouseEvent.targetNode()) {
invalidateClick();
return false;
}
m_mousePressNode = mouseEvent.targetNode();
frame->protectedDocument()->setFocusNavigationStartingNode(mouseEvent.protectedTargetNode().get());
Scrollbar* scrollbar = scrollbarForMouseEvent(mouseEvent, frame->view());
updateLastScrollbarUnderMouse(scrollbar, SetOrClearLastScrollbar::Set);
bool passedToScrollbar = scrollbar && passMousePressEventToScrollbar(mouseEvent, scrollbar);
if (!passedToScrollbar) {
auto subframe = subframeForHitTestResult(mouseEvent);
if (auto remoteMouseEventData = userInputEventDataForRemoteFrame(dynamicDowncast<RemoteFrame>(subframe).get(), mouseEvent.hitTestResult().roundedPointInInnerNodeFrame()))
return *remoteMouseEventData;
if (RefPtr localSubframe = dynamicDowncast<LocalFrame>(subframe)) {
auto result = passMousePressEventToSubframe(mouseEvent, *localSubframe);
if (auto remoteMouseEventData = result.remoteUserInputEventData())
return *remoteMouseEventData;
if (result.wasHandled()) {
// Start capturing future events for this frame. We only do this if we didn't clear
// the m_mousePressed flag, which may happen if an AppKit widget entered a modal event loop.
m_capturesDragging = localSubframe->eventHandler().capturesDragging();
if (m_mousePressed) {
m_capturingMouseEventsElement = localSubframe->ownerElement();
m_eventHandlerWillResetCapturingMouseEventsElement = true;
if (!m_capturingMouseEventsElement)
m_isCapturingRootElementForMouseEvents = true;
}
invalidateClick();
return true;
}
}
}
#if ENABLE(PAN_SCROLLING)
// We store whether pan scrolling is in progress before calling stopAutoscrollTimer()
// because it will set m_autoscrollType to NoAutoscroll on return.
auto* localFrame = dynamicDowncast<LocalFrame>(frame->mainFrame());
if (!localFrame)
return false;
bool isPanScrollInProgress = Ref(*localFrame)->eventHandler().panScrollInProgress();
stopAutoscrollTimer();
if (isPanScrollInProgress) {
// We invalidate the click when exiting pan scrolling so that we don't inadvertently navigate
// away from the current page (e.g. the click was on a hyperlink). See <rdar://problem/6095023>.
invalidateClick();
return true;
}
#endif
m_clickCount = platformMouseEvent.clickCount();
m_clickNode = mouseEvent.targetNode();
if (!m_clickNode) {
invalidateClick();
return false;
}
RenderLayer* layer = m_clickNode->renderer() ? m_clickNode->renderer()->enclosingLayer() : nullptr;
auto localPoint = roundedIntPoint(mouseEvent.hitTestResult().localPoint());
if (layer && layer->isPointInResizeControl(localPoint)) {
layer->setInResizeMode(true);
m_resizeLayer = *layer;
m_offsetFromResizeCorner = layer->offsetFromResizeCorner(localPoint);
return true;
}
frame->selection().setCaretBlinkingSuspended(true);
bool swallowEvent = !dispatchMouseEvent(eventNames().mousedownEvent, mouseEvent.protectedTargetNode().get(), m_clickCount, platformMouseEvent, FireMouseOverOut::Yes);
if (!swallowEvent || mouseEvent.scrollbar())
m_capturesDragging = true;
else
m_capturesDragging = m_capturesDragging ? CapturesDragging::InabilityReason::Unknown : m_capturesDragging.inabilityReason();
// If the hit testing originally determined the event was in a scrollbar, refetch the MouseEventWithHitTestResults
// in case the scrollbar widget was destroyed when the mouse event was handled.
if (mouseEvent.scrollbar()) {
const bool wasLastScrollBar = mouseEvent.scrollbar() == m_lastScrollbarUnderMouse;
mouseEvent = frame->protectedDocument()->prepareMouseEvent(HitTestRequest(), documentPoint, platformMouseEvent);
if (wasLastScrollBar && mouseEvent.scrollbar() != m_lastScrollbarUnderMouse)
m_lastScrollbarUnderMouse = nullptr;
}
if (!swallowEvent) {
if (shouldRefetchEventTarget(mouseEvent))
mouseEvent = frame->protectedDocument()->prepareMouseEvent(HitTestRequest(), documentPoint, platformMouseEvent);
}
if (!swallowEvent) {
if (passedToScrollbar)
swallowEvent = true;
else
swallowEvent = handleMousePressEvent(mouseEvent);
}
return swallowEvent;
}
// This method only exists for platforms that don't know how to deliver
bool EventHandler::handleMouseDoubleClickEvent(const PlatformMouseEvent& platformMouseEvent)
{
Ref frame = m_frame.get();
RefPtr protectedView { frame->view() };
frame->selection().setCaretBlinkingSuspended(false);
UserGestureIndicator gestureIndicator(IsProcessingUserGesture::Yes, frame->protectedDocument().get(), userGestureTypeForPlatformEvent(platformMouseEvent));
#if ENABLE(POINTER_LOCK)
if (frame->page()->pointerLockController().isLocked()) {
frame->page()->pointerLockController().dispatchLockedMouseEvent(platformMouseEvent, eventNames().mouseupEvent);
return true;
}
#endif
// We get this instead of a second mouse-up
m_mousePressed = false;
setLastKnownMousePosition(platformMouseEvent.position(), platformMouseEvent.globalPosition());
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::Release, HitTestRequest::Type::DisallowUserAgentShadowContent };
MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(hitType, platformMouseEvent);
auto subframe = dynamicDowncast<LocalFrame>(subframeForHitTestResult(mouseEvent));
if (m_eventHandlerWillResetCapturingMouseEventsElement)
resetCapturingMouseEventsElement();
if (subframe && passMousePressEventToSubframe(mouseEvent, *subframe).wasHandled())
return true;
m_clickCount = platformMouseEvent.clickCount();
bool swallowMouseUpEvent = !dispatchMouseEvent(eventNames().mouseupEvent, mouseEvent.protectedTargetNode().get(), m_clickCount, platformMouseEvent, FireMouseOverOut::No);
bool swallowClickEvent = swallowAnyClickEvent(platformMouseEvent, mouseEvent, IgnoreAncestorNodesForClickEvent::Yes);
if (m_lastScrollbarUnderMouse)
swallowMouseUpEvent = m_lastScrollbarUnderMouse->mouseUp(platformMouseEvent);
bool swallowMouseReleaseEvent = !swallowMouseUpEvent && handleMouseReleaseEvent(mouseEvent);
invalidateClick();
return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
}
ScrollableArea* EventHandler::enclosingScrollableArea(Node* node) const
{
for (auto ancestor = node; ancestor; ancestor = ancestor->parentOrShadowHostNode()) {
if (is<HTMLIFrameElement>(*ancestor))
return nullptr;
if (is<HTMLHtmlElement>(*ancestor) || is<HTMLDocument>(*ancestor))
break;
auto renderer = ancestor->renderer();
if (!renderer)
continue;
if (auto* renderListBox = dynamicDowncast<RenderListBox>(*renderer)) {
auto* scrollableArea = static_cast<ScrollableArea*>(renderListBox);
if (scrollableArea->isScrollableOrRubberbandable())
return scrollableArea;
}
if (RefPtr plugin = dynamicDowncast<RenderEmbeddedObject>(renderer)) {
if (auto* scrollableArea = plugin->scrollableArea())
return scrollableArea;
}
auto* layer = renderer->enclosingLayer();
if (!layer)
return nullptr;
if (auto* scrollableLayer = layer->enclosingScrollableLayer(IncludeSelfOrNot::IncludeSelf, CrossFrameBoundaries::No)) {
if (!scrollableLayer->isRenderViewLayer())
return scrollableLayer->scrollableArea();
}
}
return m_frame->view();
}
HandleUserInputEventResult EventHandler::mouseMoved(const PlatformMouseEvent& event)
{
Ref frame = m_frame.get();
RefPtr protectedView { frame->view() };
MaximumDurationTracker maxDurationTracker(&m_maxMouseMovedDuration);
if (frame->page() && frame->protectedPage()->pageOverlayController().handleMouseEvent(event))
return true;
HitTestResult hitTestResult;
auto result = handleMouseMoveEvent(event, &hitTestResult);
RefPtr page = m_frame->page();
if (!page)
return result;
hitTestResult.setToNonUserAgentShadowAncestor();
page->chrome().mouseDidMoveOverElement(hitTestResult, event.modifiers());
#if ENABLE(IMAGE_ANALYSIS)
if (event.syntheticClickType() == SyntheticClickType::NoTap && m_textRecognitionHoverTimer.isActive())
m_textRecognitionHoverTimer.restart();
#endif
return result;
}
bool EventHandler::passMouseMovedEventToScrollbars(const PlatformMouseEvent& event)
{
HitTestResult hitTestResult;
return handleMouseMoveEvent(event, &hitTestResult, true).wasHandled();
}
OptionSet<HitTestRequest::Type> EventHandler::getHitTypeForMouseMoveEvent(const PlatformMouseEvent& platformMouseEvent, bool onlyUpdateScrollbars)
{
OptionSet hitType { HitTestRequest::Type::Move, HitTestRequest::Type::DisallowUserAgentShadowContent, HitTestRequest::Type::AllowFrameScrollbars };
if (m_mousePressed)
hitType.add(HitTestRequest::Type::Active);
else if (onlyUpdateScrollbars) {
// Mouse events should be treated as "read-only" if we're updating only scrollbars. This
// means that :hover and :active freeze in the state they were in, rather than updating
// for nodes the mouse moves while the window is not key (which will be the case if
// onlyUpdateScrollbars is true).
hitType.add(HitTestRequest::Type::ReadOnly);
}
#if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
// Treat any mouse move events as readonly if the user is currently touching the screen.
if (m_touchPressed) {
hitType.add(HitTestRequest::Type::Active);
hitType.add(HitTestRequest::Type::ReadOnly);
}
#endif
#if ENABLE(PENCIL_HOVER)
if (platformMouseEvent.pointerType() == WebCore::penPointerEventType())
hitType.add(WebCore::HitTestRequest::Type::PenEvent);
#else
UNUSED_PARAM(platformMouseEvent);
#endif
return hitType;
}
HitTestResult EventHandler::getHitTestResultForMouseEvent(const PlatformMouseEvent& platformMouseEvent)
{
HitTestRequest request(getHitTypeForMouseMoveEvent(platformMouseEvent));
return prepareMouseEvent(request, platformMouseEvent).hitTestResult();
}
HandleUserInputEventResult EventHandler::handleMouseMoveEvent(const PlatformMouseEvent& platformMouseEvent, HitTestResult* hitTestResult, bool onlyUpdateScrollbars)
{
#if ENABLE(TOUCH_EVENTS)
bool defaultPrevented = dispatchSyntheticTouchEventIfEnabled(platformMouseEvent).wasHandled();
if (defaultPrevented)
return true;
#endif
Ref frame = m_frame.get();
RefPtr protectedView { frame->view() };
#if ENABLE(POINTER_LOCK)
if (frame->page()->pointerLockController().isLocked()) {
frame->protectedPage()->pointerLockController().dispatchLockedMouseEvent(platformMouseEvent, eventNames().mousemoveEvent);
return true;
}
#endif
setLastKnownMousePosition(platformMouseEvent.position(), platformMouseEvent.globalPosition());
if (m_hoverTimer.isActive())
m_hoverTimer.stop();
m_hasScheduledCursorUpdate = false;
#if !ENABLE(IOS_TOUCH_EVENTS)
cancelFakeMouseMoveEvent();
#endif
if (m_svgPan) {
downcast<SVGDocument>(*frame->protectedDocument()).updatePan(frame->protectedView()->windowToContents(valueOrDefault(m_lastKnownMousePosition)));
return true;
}
if (m_frameSetBeingResized)
return !dispatchMouseEvent(eventNames().mousemoveEvent, m_frameSetBeingResized.get(), 0, platformMouseEvent, FireMouseOverOut::No);
// On iOS, our scrollbars are managed by UIKit.
#if !PLATFORM(IOS_FAMILY)
// Send events right to a scrollbar if the mouse is pressed.
if (m_lastScrollbarUnderMouse && m_mousePressed)
return m_lastScrollbarUnderMouse->mouseMoved(platformMouseEvent);
#endif
HitTestRequest request(getHitTypeForMouseMoveEvent(platformMouseEvent, onlyUpdateScrollbars));
MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, platformMouseEvent);
if (hitTestResult)
*hitTestResult = mouseEvent.hitTestResult();
if (m_resizeLayer && m_resizeLayer->inResizeMode()) {
m_resizeLayer->resize(platformMouseEvent, m_offsetFromResizeCorner);
if (m_resizeLayer->renderer().shouldPlaceVerticalScrollbarOnLeft()) {
auto absolutePoint = frame->protectedView()->windowToContents(platformMouseEvent.position());
auto localPoint = roundedIntPoint(m_resizeLayer->absoluteToContents(absolutePoint));
m_offsetFromResizeCorner.setWidth(m_resizeLayer->offsetFromResizeCorner(localPoint).width());
}
} else {
RefPtr scrollbar = mouseEvent.scrollbar();
updateLastScrollbarUnderMouse(scrollbar.get(), m_mousePressed ? SetOrClearLastScrollbar::Clear : SetOrClearLastScrollbar::Set);
// On iOS, our scrollbars are managed by UIKit.
#if !PLATFORM(IOS_FAMILY)
if (!m_mousePressed && scrollbar)
scrollbar->mouseMoved(platformMouseEvent); // Handle hover effects on platforms that support visual feedback on scrollbar hovering.
#endif
if (onlyUpdateScrollbars) {
if (shouldSendMouseEventsToInactiveWindows())
updateMouseEventTargetNode(eventNames().mousemoveEvent, mouseEvent.protectedTargetNode().get(), platformMouseEvent, FireMouseOverOut::Yes);
return true;
}
}
bool swallowEvent = false;
auto subframe = isCapturingMouseEventsElement() ? subframeForTargetNode(m_capturingMouseEventsElement.get()) : subframeForHitTestResult(mouseEvent);
if (auto remoteMouseEventData = userInputEventDataForRemoteFrame(dynamicDowncast<RemoteFrame>(subframe).get(), mouseEvent.hitTestResult().roundedPointInInnerNodeFrame()))
return *remoteMouseEventData;
RefPtr localSubframe = dynamicDowncast<LocalFrame>(subframe.get());
// We want mouseouts to happen first, from the inside out. First send a move event to the last subframe so that it will fire mouseouts.
if (RefPtr lastMouseMoveEventSubframe = m_lastMouseMoveEventSubframe; lastMouseMoveEventSubframe && lastMouseMoveEventSubframe->tree().isDescendantOf(frame.ptr()) && lastMouseMoveEventSubframe != localSubframe)
passMouseMoveEventToSubframe(mouseEvent, *lastMouseMoveEventSubframe);
if (localSubframe) {
// Update over/out state before passing the event to the subframe.
updateMouseEventTargetNode(eventNames().mousemoveEvent, mouseEvent.protectedTargetNode().get(), platformMouseEvent, FireMouseOverOut::Yes);
// Event dispatch in updateMouseEventTargetNode may have caused the subframe of the target
// node to be detached from its FrameView, in which case the event should not be passed.
if (localSubframe->view()) {
auto result = passMouseMoveEventToSubframe(mouseEvent, *localSubframe, hitTestResult);
if (auto remoteMouseEventData = result.remoteUserInputEventData())
return *remoteMouseEventData;
swallowEvent |= result.wasHandled();
}
}
if (!localSubframe || mouseEvent.scrollbar()) {
if (RefPtr view = frame->view())
updateCursor(*view, mouseEvent.hitTestResult(), platformMouseEvent.shiftKey());
}
m_lastMouseMoveEventSubframe = localSubframe;
if (swallowEvent)
return true;
swallowEvent = !dispatchMouseEvent(eventNames().mousemoveEvent, mouseEvent.protectedTargetNode().get(), 0, platformMouseEvent, FireMouseOverOut::Yes);
#if ENABLE(DRAG_SUPPORT)
if (!swallowEvent || m_capturesDragging.inabilityReason() == CapturesDragging::InabilityReason::MouseMoveIsCancelled)
swallowEvent = handleMouseDraggedEvent(mouseEvent);
#endif
return swallowEvent;
}
bool EventHandler::shouldSendMouseEventsToInactiveWindows() const
{
#if PLATFORM(GTK)
return true;
#endif
return false;
}
void EventHandler::invalidateClick()
{
m_clickCount = 0;
m_clickNode = nullptr;
}
static RefPtr<Node> targetNodeForClickEvent(Node* mousePressNode, Node* mouseReleaseNode)
{
if (!mousePressNode || !mouseReleaseNode)
return nullptr;
if (mousePressNode == mouseReleaseNode)
return mouseReleaseNode;
// If mousePressNode and mouseReleaseNode differ, we should fire the event at their common ancestor if there is one.
if (&mousePressNode->document() == &mouseReleaseNode->document()) {
if (auto commonAncestor = commonInclusiveAncestor<ComposedTree>(*mousePressNode, *mouseReleaseNode))
return commonAncestor;
}
auto mouseReleaseShadowHost = mouseReleaseNode->shadowHost();
if (mouseReleaseShadowHost && mouseReleaseShadowHost == mousePressNode->shadowHost()) {
// We want to dispatch the click to the shadow tree host element to give listeners the illusion that the
// shadom tree is a single element. For example, we want to give the illusion that <input type="range">
// is a single element even though it is a composition of multiple shadom tree elements.
return mouseReleaseShadowHost;
}
return nullptr;
}
bool EventHandler::swallowAnyClickEvent(const PlatformMouseEvent& platformMouseEvent, const MouseEventWithHitTestResults& mouseEvent, IgnoreAncestorNodesForClickEvent ignoreAncestorNodesForClickEvent)
{
if (!m_clickCount)
return false;
auto nodeToClick = [&] -> RefPtr<Node> {
if (ignoreAncestorNodesForClickEvent == IgnoreAncestorNodesForClickEvent::Yes) {
if (mouseEvent.targetNode() != m_clickNode)
return nullptr;
return m_clickNode;
}
return targetNodeForClickEvent(RefPtr { m_clickNode }.get(), mouseEvent.protectedTargetNode().get());
}();
if (!nodeToClick)
return false;
bool isPrimaryPointerButton = platformMouseEvent.button() == MouseButton::Left;
if (!isPrimaryPointerButton && !protectedFrame()->settings().auxclickEventEnabled())
return false;
// The auxclick event should only be fired for the non-primary pointer buttons.
// In the case of right button, the auxclick event is dispatched after any contextmenu event.
//
// The click event should only be fired for the primary pointer button.
auto& eventName = isPrimaryPointerButton ? eventNames().clickEvent : eventNames().auxclickEvent;
bool swallowed = !dispatchMouseEvent(eventName, nodeToClick.get(), m_clickCount, platformMouseEvent, FireMouseOverOut::Yes);
if (RefPtr page = m_frame->protectedPage())
page->chrome().client().didDispatchClickEvent(platformMouseEvent, *nodeToClick);
return swallowed;
}
HandleUserInputEventResult EventHandler::handleMouseReleaseEvent(const PlatformMouseEvent& platformMouseEvent)
{
Ref frame = m_frame.get();
RefPtr protectedView { frame->view() };
frame->selection().setCaretBlinkingSuspended(false);
RefPtr page = frame->page();
if (!page)
return false;
#if ENABLE(POINTER_LOCK)
if (auto& pointerLockController = page->pointerLockController(); pointerLockController.isLocked()) {
pointerLockController.dispatchLockedMouseEvent(platformMouseEvent, eventNames().mouseupEvent);
return true;
}
#endif
if (page->pageOverlayController().handleMouseEvent(platformMouseEvent))
return true;
#if ENABLE(TOUCH_EVENTS)
bool defaultPrevented = dispatchSyntheticTouchEventIfEnabled(platformMouseEvent).wasHandled();
if (defaultPrevented)
return true;
#endif
UserGestureIndicator gestureIndicator(IsProcessingUserGesture::Yes, frame->protectedDocument().get(), userGestureTypeForPlatformEvent(platformMouseEvent), UserGestureIndicator::ProcessInteractionStyle::Immediate, platformMouseEvent.authorizationToken());
#if ENABLE(PAN_SCROLLING)
m_autoscrollController->handleMouseReleaseEvent(platformMouseEvent);
#endif
m_mousePressed = false;
setLastKnownMousePosition(platformMouseEvent.position(), platformMouseEvent.globalPosition());
if (m_svgPan) {
m_svgPan = false;
downcast<SVGDocument>(*frame->protectedDocument()).updatePan(frame->protectedView()->windowToContents(valueOrDefault(m_lastKnownMousePosition)));
return true;
}
if (m_frameSetBeingResized)
return !dispatchMouseEvent(eventNames().mouseupEvent, m_frameSetBeingResized.get(), m_clickCount, platformMouseEvent, FireMouseOverOut::No);
// If an immediate action began or was completed using this series of mouse events, then we should send mouseup to
// the DOM and return now so that we don't perform our own default behaviors.
if (immediateActionBeganOrWasCompleted(m_immediateActionStage)) {
// We reset the immediate action stage after event dispatch, and not before, so that DOM event handling can query for the stage if needed.
auto resetImmediateActionStageAfterMouseEventDispatch = makeScopeExit([&] {
m_immediateActionStage = ImmediateActionStage::None;
});
return !dispatchMouseEvent(eventNames().mouseupEvent, m_lastElementUnderMouse.get(), m_clickCount, platformMouseEvent, FireMouseOverOut::No);
}
m_immediateActionStage = ImmediateActionStage::None;
if (m_lastScrollbarUnderMouse) {
invalidateClick();
m_lastScrollbarUnderMouse->mouseUp(platformMouseEvent);
return !dispatchMouseEvent(eventNames().mouseupEvent, m_lastElementUnderMouse.get(), m_clickCount, platformMouseEvent, FireMouseOverOut::No);
}
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::Release, HitTestRequest::Type::DisallowUserAgentShadowContent };
MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(hitType, platformMouseEvent);
auto subframe = isCapturingMouseEventsElement() ? subframeForTargetNode(m_capturingMouseEventsElement.get()) : subframeForHitTestResult(mouseEvent);
if (m_eventHandlerWillResetCapturingMouseEventsElement)
resetCapturingMouseEventsElement();
if (auto remoteMouseEventData = userInputEventDataForRemoteFrame(dynamicDowncast<RemoteFrame>(subframe).get(), mouseEvent.hitTestResult().roundedPointInInnerNodeFrame()))
return *remoteMouseEventData;
if (RefPtr localSubframe = dynamicDowncast<LocalFrame>(subframe)) {
auto result = passMouseReleaseEventToSubframe(mouseEvent, *localSubframe);
if (auto remoteMouseEventData = result.remoteUserInputEventData())
return *remoteMouseEventData;
if (result.wasHandled())
return true;
}
bool swallowMouseUpEvent = !dispatchMouseEvent(eventNames().mouseupEvent, mouseEvent.protectedTargetNode().get(), m_clickCount, platformMouseEvent, FireMouseOverOut::No);
bool swallowClickEvent = swallowAnyClickEvent(platformMouseEvent, mouseEvent, IgnoreAncestorNodesForClickEvent::No);
if (m_resizeLayer) {
m_resizeLayer->setInResizeMode(false);
m_resizeLayer = nullptr;
}
bool swallowMouseReleaseEvent = false;
if (!swallowMouseUpEvent)
swallowMouseReleaseEvent = handleMouseReleaseEvent(mouseEvent);
invalidateClick();
return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
}
bool EventHandler::handleMouseForceEvent(const PlatformMouseEvent& event)
{
Ref frame = m_frame.get();
RefPtr protectedView(frame->view());
#if ENABLE(POINTER_LOCK)
if (frame->page()->pointerLockController().isLocked()) {
m_frame->page()->pointerLockController().dispatchLockedMouseEvent(event, eventNames().webkitmouseforcechangedEvent);
if (event.type() == PlatformEvent::Type::MouseForceDown)
m_frame->page()->pointerLockController().dispatchLockedMouseEvent(event, eventNames().webkitmouseforcedownEvent);
if (event.type() == PlatformEvent::Type::MouseForceUp)
m_frame->page()->pointerLockController().dispatchLockedMouseEvent(event, eventNames().webkitmouseforceupEvent);
return true;
}
#endif
setLastKnownMousePosition(event.position(), event.globalPosition());
OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::DisallowUserAgentShadowContent };
if (event.force())
hitType.add(HitTestRequest::Type::Active);
auto mouseEvent = prepareMouseEvent(hitType, event);
bool swallowedEvent = !dispatchMouseEvent(eventNames().webkitmouseforcechangedEvent, mouseEvent.protectedTargetNode().get(), 0, event, FireMouseOverOut::No);
if (event.type() == PlatformEvent::Type::MouseForceDown)
swallowedEvent |= !dispatchMouseEvent(eventNames().webkitmouseforcedownEvent, mouseEvent.protectedTargetNode().get(), 0, event, FireMouseOverOut::No);
if (event.type() == PlatformEvent::Type::MouseForceUp)
swallowedEvent |= !dispatchMouseEvent(eventNames().webkitmouseforceupEvent, mouseEvent.protectedTargetNode().get(), 0, event, FireMouseOverOut::No);
return swallowedEvent;
}
bool EventHandler::handlePasteGlobalSelection()
{
if (!m_frame->page())
return false;
RefPtr focusFrame = m_frame->page()->checkedFocusController()->focusedOrMainFrame();
// Do not paste here if the focus was moved somewhere else.
if (m_frame.ptr() == focusFrame.get() && m_frame->editor().client()->supportsGlobalSelection())
return protectedFrame()->editor().command("PasteGlobalSelection"_s).execute();
return false;
}
#if ENABLE(DRAG_SUPPORT)
bool EventHandler::dispatchDragEvent(const AtomString& eventType, Element& dragTarget, const PlatformMouseEvent& event, DataTransfer& dataTransfer)
{
Ref frame = m_frame.get();
RefPtr view = frame->view();
// FIXME: We might want to dispatch a dragleave even if the view is gone.
if (!view)
return false;
auto dragEvent = DragEvent::create(eventType, Event::CanBubble::Yes, Event::IsCancelable::Yes, Event::IsComposed::Yes,
event.timestamp().approximateMonotonicTime(), &frame->windowProxy(), 0,
event.globalPosition(), event.position(), event.movementDelta().x(), event.movementDelta().y(),
event.modifiers(), MouseButton::Left, 0, nullptr, event.force(), SyntheticClickType::NoTap, &dataTransfer);
dragTarget.dispatchEvent(dragEvent);
if (CheckedPtr cache = frame->document()->existingAXObjectCache()) {
auto& eventNames = WebCore::eventNames();
if (eventType == eventNames.dragstartEvent)
cache->postNotification(&dragTarget, AXNotification::DraggingStarted);
else if (eventType == eventNames.dragendEvent)
cache->postNotification(&dragTarget, AXNotification::DraggingEnded);
else if (eventType == eventNames.dragenterEvent)
cache->postNotification(&dragTarget, AXNotification::DraggingEnteredDropZone);
else if (eventType == eventNames.dragleaveEvent)
cache->postNotification(&dragTarget, AXNotification::DraggingExitedDropZone);
else if (eventType == eventNames.dropEvent)
cache->postNotification(&dragTarget, AXNotification::DraggingDropped);
}
return dragEvent->defaultPrevented();
}
Element* EventHandler::draggingElement() const
{
return dragState().source.get();
}
void EventHandler::setDragStateSource(Element* element) const
{
RefPtr document = m_frame->document();
if (CheckedPtr cache = document ? document->existingAXObjectCache() : nullptr)
cache->onDragElementChanged(dragState().source.get(), element);
dragState().source = element;
}
bool EventHandler::canDropCurrentlyDraggedImageAsFile() const
{
auto sourceOrigin = dragState().restrictedOriginForImageData;
return !sourceOrigin || m_frame->document()->protectedSecurityOrigin()->canReceiveDragData(*sourceOrigin);
}
static std::pair<bool, RefPtr<LocalFrame>> contentFrameForNode(Node* target)
{
RefPtr frameElement = dynamicDowncast<HTMLFrameElementBase>(target);
if (!frameElement)
return { false, nullptr };
return { true, dynamicDowncast<LocalFrame>(frameElement->contentFrame()) };
}
static std::optional<DragOperation> convertDropZoneOperationToDragOperation(const String& dragOperation)
{
if (dragOperation == "copy"_s)
return DragOperation::Copy;
if (dragOperation == "move"_s)
return DragOperation::Move;
if (dragOperation == "link"_s)
return DragOperation::Link;
return std::nullopt;
}
static String convertDragOperationToDropZoneOperation(std::optional<DragOperation> operation)
{
if (operation) {
switch (*operation) {
case DragOperation::Move:
return "move"_s;
case DragOperation::Link:
return "link"_s;
default:
break;
}
}
return "copy"_s;
}
static bool hasDropZoneType(Document& document, DataTransfer& dataTransfer, const String& keyword)
{
if (keyword.startsWith("file:"_s))
return dataTransfer.hasFileOfType(keyword.substring(5));
if (keyword.startsWith("string:"_s))
return dataTransfer.hasStringOfType(document, keyword.substring(7));
return false;
}
static bool findDropZone(Node& target, DataTransfer& dataTransfer)
{
RefPtr element = dynamicDowncast<Element>(target);
if (!element)
element = target.parentElement();
for (; element; element = element->parentElement()) {
SpaceSplitString keywords(element->attributeWithoutSynchronization(webkitdropzoneAttr), SpaceSplitString::ShouldFoldCase::Yes);
bool matched = false;
std::optional<DragOperation> dragOperation;
for (auto& keyword : keywords) {
if (auto operationFromKeyword = convertDropZoneOperationToDragOperation(keyword)) {
if (!dragOperation)
dragOperation = operationFromKeyword;
} else
matched = matched || hasDropZoneType(target.protectedDocument(), dataTransfer, keyword.string());
if (matched && dragOperation)
break;
}
if (matched) {
dataTransfer.setDropEffect(convertDragOperationToDropZoneOperation(dragOperation));
return true;
}
}
return false;
}
EventHandler::DragTargetResponse EventHandler::dispatchDragEnterOrDragOverEvent(const AtomString& eventType, Element& target, const PlatformMouseEvent& event, std::unique_ptr<Pasteboard>&& pasteboard, OptionSet<DragOperation> sourceOperationMask, bool draggingFiles)
{
auto dataTransfer = DataTransfer::createForUpdatingDropTarget(target.protectedDocument(), WTFMove(pasteboard), sourceOperationMask, draggingFiles);
bool accept = dispatchDragEvent(eventType, target, event, dataTransfer.get());
if (!accept)
accept = findDropZone(target, dataTransfer);
dataTransfer->makeInvalidForSecurity();
if (accept && !dataTransfer->dropEffectIsUninitialized())
return { true, dataTransfer->destinationOperationMask() };
return { accept, std::nullopt };
}
EventHandler::DragTargetResponse EventHandler::updateDragAndDrop(const PlatformMouseEvent& event, const std::function<std::unique_ptr<Pasteboard>()>& makePasteboard, OptionSet<DragOperation> sourceOperationMask, bool draggingFiles)
{
Ref frame = m_frame.get();
if (!frame->view())
return { };
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::DisallowUserAgentShadowContent };
MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(hitType, event);
RefPtr<Element> newTarget;
if (RefPtr targetNode = mouseEvent.targetNode()) {
// Drag events should never go to non-element nodes (following IE, and proper mouseover/out dispatch)
if (!is<Element>(*targetNode))
newTarget = targetNode->parentOrShadowHostElement();
else
newTarget = static_pointer_cast<Element>(WTFMove(targetNode));
}
m_autoscrollController->updateDragAndDrop(newTarget.get(), event.position(), event.timestamp());
DragTargetResponse response;
if (m_dragTarget != newTarget) {
// FIXME: this ordering was explicitly chosen to match WinIE. However,
// it is sometimes incorrect when dragging within subframes, as seen with
// LayoutTests/fast/events/drag-in-frames.html.
//
// Moreover, this ordering conforms to section 7.9.4 of the HTML 5 spec. <http://dev.w3.org/html5/spec/Overview.html#drag-and-drop-processing-model>.
if (auto [isFrameOwner, targetFrame] = contentFrameForNode(newTarget.get()); isFrameOwner) {
if (targetFrame)
response = targetFrame->eventHandler().updateDragAndDrop(event, makePasteboard, sourceOperationMask, draggingFiles);
} else if (newTarget) {
// As per section 7.9.4 of the HTML 5 spec., we must always fire a drag event before firing a dragenter, dragleave, or dragover event.
dispatchEventToDragSourceElement(eventNames().dragEvent, event);
response = dispatchDragEnterOrDragOverEvent(eventNames().dragenterEvent, *newTarget, event, makePasteboard(), sourceOperationMask, draggingFiles);
}
if (auto [isFrameOwner, targetFrame] = contentFrameForNode(m_dragTarget.copyRef().get()); isFrameOwner) {
// FIXME: Recursing again here doesn't make sense if the newTarget and m_dragTarget were in the same frame.
if (targetFrame)
response = targetFrame->eventHandler().updateDragAndDrop(event, makePasteboard, sourceOperationMask, draggingFiles);
} else if (RefPtr dragTarget = m_dragTarget) {
auto dataTransfer = DataTransfer::createForUpdatingDropTarget(dragTarget->protectedDocument(), makePasteboard(), sourceOperationMask, draggingFiles);
dispatchDragEvent(eventNames().dragleaveEvent, *dragTarget, event, dataTransfer.get());
dataTransfer->makeInvalidForSecurity();
}
if (newTarget) {
// We do not explicitly call dispatchDragEvent here because it could ultimately result in the appearance that
// two dragover events fired. So, we mark that we should only fire a dragover event on the next call to this function.
m_shouldOnlyFireDragOverEvent = true;
}
} else {
if (auto [isFrameOwner, targetFrame] = contentFrameForNode(newTarget.get()); isFrameOwner) {
if (targetFrame)
response = targetFrame->eventHandler().updateDragAndDrop(event, makePasteboard, sourceOperationMask, draggingFiles);
} else if (newTarget) {
// Note, when dealing with sub-frames, we may need to fire only a dragover event as a drag event may have been fired earlier.
if (!m_shouldOnlyFireDragOverEvent)
dispatchEventToDragSourceElement(eventNames().dragEvent, event);
response = dispatchDragEnterOrDragOverEvent(eventNames().dragoverEvent, *newTarget, event, makePasteboard(), sourceOperationMask, draggingFiles);
m_shouldOnlyFireDragOverEvent = false;
}
}
m_dragTarget = WTFMove(newTarget);
return response;
}
void EventHandler::cancelDragAndDrop(const PlatformMouseEvent& event, std::unique_ptr<Pasteboard>&& pasteboard, OptionSet<DragOperation> sourceOperationMask, bool draggingFiles)
{
Ref frame = m_frame.get();
if (auto [isFrameOwner, targetFrame] = contentFrameForNode(m_dragTarget.copyRef().get()); isFrameOwner) {
if (targetFrame)
targetFrame->eventHandler().cancelDragAndDrop(event, WTFMove(pasteboard), sourceOperationMask, draggingFiles);
} else if (RefPtr dragTarget = m_dragTarget) {
dispatchEventToDragSourceElement(eventNames().dragEvent, event);
auto dataTransfer = DataTransfer::createForUpdatingDropTarget(dragTarget->protectedDocument(), WTFMove(pasteboard), sourceOperationMask, draggingFiles);
dispatchDragEvent(eventNames().dragleaveEvent, *dragTarget, event, dataTransfer.get());
dataTransfer->makeInvalidForSecurity();
}
clearDragState();
}
bool EventHandler::performDragAndDrop(const PlatformMouseEvent& event, std::unique_ptr<Pasteboard>&& pasteboard, OptionSet<DragOperation> sourceOperationMask, bool draggingFiles)
{
Ref frame = m_frame.get();
bool preventedDefault = false;
if (auto [isFrameOwner, targetFrame] = contentFrameForNode(m_dragTarget.copyRef().get()); isFrameOwner) {
if (targetFrame)
preventedDefault = targetFrame->checkedEventHandler()->performDragAndDrop(event, WTFMove(pasteboard), sourceOperationMask, draggingFiles);
} else if (RefPtr dragTarget = m_dragTarget) {
Ref dataTransfer = DataTransfer::createForDrop(dragTarget->protectedDocument(), WTFMove(pasteboard), sourceOperationMask, draggingFiles);
preventedDefault = dispatchDragEvent(eventNames().dropEvent, *dragTarget, event, dataTransfer);
dataTransfer->makeInvalidForSecurity();
}
clearDragState();
return preventedDefault;
}
void EventHandler::clearDragState()
{
stopAutoscrollTimer();
m_dragStartSelection = std::nullopt;
m_dragTarget = nullptr;
resetCapturingMouseEventsElement();
m_shouldOnlyFireDragOverEvent = false;
#if PLATFORM(COCOA)
m_sendingEventToSubview = false;
#endif
}
#endif // ENABLE(DRAG_SUPPORT)
void EventHandler::setCapturingMouseEventsElement(RefPtr<Element>&& element)
{
m_capturingMouseEventsElement = WTFMove(element);
m_isCapturingRootElementForMouseEvents = false;
m_eventHandlerWillResetCapturingMouseEventsElement = false;
}
void EventHandler::pointerCaptureElementDidChange(Element* element)
{
if (m_capturingMouseEventsElement == element)
return;
setCapturingMouseEventsElement(element);
// Now that we have a new capture element, we need to dispatch boundary mouse events.
updateMouseEventTargetNode(eventNames().gotpointercaptureEvent, element, m_lastPlatformMouseEvent, FireMouseOverOut::Yes);
}
MouseEventWithHitTestResults EventHandler::prepareMouseEvent(const HitTestRequest& request, const PlatformMouseEvent& mouseEvent)
{
m_lastPlatformMouseEvent = mouseEvent;
Ref frame = m_frame.get();
ASSERT(frame->document());
return frame->protectedDocument()->prepareMouseEvent(request, documentPointForWindowPoint(frame, mouseEvent.position()), mouseEvent);
}
static bool hierarchyHasCapturingEventListeners(Element* element, const AtomString& pointerEventName, const AtomString& compatibilityMouseEventName)
{
for (RefPtr<ContainerNode> curr = element; curr; curr = curr->parentInComposedTree()) {
if (curr->hasCapturingEventListeners(pointerEventName) || curr->hasCapturingEventListeners(compatibilityMouseEventName))
return true;
}
return false;
}
#if ENABLE(IMAGE_ANALYSIS)
RefPtr<Element> EventHandler::textRecognitionCandidateElement() const
{
RefPtr candidateElement = m_elementUnderMouse;
if (candidateElement) {
if (auto shadowHost = candidateElement->shadowHost())
candidateElement = shadowHost;
}
if (!candidateElement)
return nullptr;
if (candidateElement->hasEditableStyle())
return nullptr;
auto renderer = candidateElement->renderer();
if (!is<RenderImage>(renderer))
return nullptr;
if (candidateElement->document().settings().textRecognitionInVideosEnabled()) {
if (auto video = dynamicDowncast<HTMLVideoElement>(*candidateElement); video && video->paused())
return candidateElement;
}
#if ENABLE(VIDEO)
if (is<HTMLVideoElement>(*candidateElement))
return nullptr;
#endif // ENABLE(VIDEO)
return candidateElement;
}
#endif // ENABLE(IMAGE_ANALYSIS)
void EventHandler::updateMouseEventTargetNode(const AtomString& eventType, Node* targetNode, const PlatformMouseEvent& platformMouseEvent, FireMouseOverOut fireMouseOverOut)
{
Ref frame = m_frame.get();
RefPtr<Element> targetElement;
// If we're capturing, we always go right to that element.
if (m_capturingMouseEventsElement)
targetElement = m_capturingMouseEventsElement.get();
else {
// If the target node is a non-element, dispatch on the parent. <rdar://problem/4196646>
while (targetNode) {
if (auto* asElement = dynamicDowncast<Element>(*targetNode)) {
targetElement = asElement;
break;
}
targetNode = targetNode->parentInComposedTree();
}
}
m_elementUnderMouse = targetElement;
#if ENABLE(IMAGE_ANALYSIS)
if (!textRecognitionCandidateElement())
m_textRecognitionHoverTimer.stop();
else if (!platformMouseEvent.movementDelta().isZero())
m_textRecognitionHoverTimer.restart();
#endif // ENABLE(IMAGE_ANALYSIS)
if (RefPtr page = frame->page())
page->imageOverlayController().elementUnderMouseDidChange(frame, m_elementUnderMouse.get());
ASSERT_IMPLIES(m_elementUnderMouse, &m_elementUnderMouse->document() == frame->document());
ASSERT_IMPLIES(m_lastElementUnderMouse, &m_lastElementUnderMouse->document() == frame->document());
// Fire mouseout/mouseover if the mouse has shifted to a different node.
if (fireMouseOverOut == FireMouseOverOut::Yes) {
notifyScrollableAreasOfMouseEvents(eventType, m_lastElementUnderMouse.get(), m_elementUnderMouse.get());
if (m_lastElementUnderMouse && &m_lastElementUnderMouse->document() != frame->document()) {
m_lastElementUnderMouse = nullptr;
m_lastScrollbarUnderMouse = nullptr;
}
if (m_lastElementUnderMouse != m_elementUnderMouse) {
// mouseenter and mouseleave events are only dispatched if there is a capturing eventhandler on an ancestor
// or a normal eventhandler on the element itself (they don't bubble).
// This optimization is necessary since these events can cause O(n^2) capturing event-handler checks.
auto& eventNames = WebCore::eventNames();
bool hasCapturingMouseEnterListener = hierarchyHasCapturingEventListeners(m_elementUnderMouse.get(), eventNames.pointerenterEvent, eventNames.mouseenterEvent);
bool hasCapturingMouseLeaveListener = hierarchyHasCapturingEventListeners(m_lastElementUnderMouse.get(), eventNames.pointerleaveEvent, eventNames.mouseleaveEvent);
Vector<Ref<Element>, 32> leftElementsChain;
for (Element* element = m_lastElementUnderMouse.get(); element; element = element->parentElementInComposedTree())
leftElementsChain.append(*element);
Vector<Ref<Element>, 32> enteredElementsChain;
for (Element* element = m_elementUnderMouse.get(); element; element = element->parentElementInComposedTree())
enteredElementsChain.append(*element);
if (!leftElementsChain.isEmpty() && !enteredElementsChain.isEmpty() && leftElementsChain.last().ptr() == enteredElementsChain.last().ptr()) {
size_t minHeight = std::min(leftElementsChain.size(), enteredElementsChain.size());
size_t i;
for (i = 0; i < minHeight; ++i) {
if (leftElementsChain[leftElementsChain.size() - i - 1].ptr() != enteredElementsChain[enteredElementsChain.size() - i - 1].ptr())
break;
}
leftElementsChain.shrink(leftElementsChain.size() - i);
enteredElementsChain.shrink(enteredElementsChain.size() - i);
}
if (auto lastElementUnderMouse = m_lastElementUnderMouse)
lastElementUnderMouse->dispatchMouseEvent(platformMouseEvent, eventNames.mouseoutEvent, 0, m_elementUnderMouse.get());
for (auto& chain : leftElementsChain) {
if (hasCapturingMouseLeaveListener || chain->hasEventListeners(eventNames.pointerleaveEvent) || chain->hasEventListeners(eventNames.mouseleaveEvent))
chain->dispatchMouseEvent(platformMouseEvent, eventNames.mouseleaveEvent, 0, m_elementUnderMouse.get());
}
if (auto elementUnderMouse = m_elementUnderMouse)
elementUnderMouse->dispatchMouseEvent(platformMouseEvent, eventNames.mouseoverEvent, 0, m_lastElementUnderMouse.get());
for (auto& chain : makeReversedRange(enteredElementsChain)) {
if (hasCapturingMouseEnterListener || chain->hasEventListeners(eventNames.pointerenterEvent) || chain->hasEventListeners(eventNames.mouseenterEvent))
chain->dispatchMouseEvent(platformMouseEvent, eventNames.mouseenterEvent, 0, m_lastElementUnderMouse.get());
}
}
// Event handling may have moved the element to a different document.
if (m_elementUnderMouse && &m_elementUnderMouse->document() != frame->document()) {
#if ENABLE(IMAGE_ANALYSIS)
m_textRecognitionHoverTimer.stop();
#endif
clearElementUnderMouse();
}
m_lastElementUnderMouse = m_elementUnderMouse;
}
}
void EventHandler::clearElementUnderMouse()
{
if (!m_elementUnderMouse)
return;
m_elementUnderMouse = nullptr;
RefPtr page = m_frame->page();
if (!page)
return;
auto* imageOverlayController = page->imageOverlayControllerIfExists();
if (!imageOverlayController)
return;
imageOverlayController->elementUnderMouseDidChange(protectedFrame(), nullptr);
}
void EventHandler::notifyScrollableAreasOfMouseEvents(const AtomString& eventType, Element* lastElementUnderMouse, Element* elementUnderMouse)
{
Ref frame = m_frame.get();
RefPtr frameView = frame->view();
if (!frameView)
return;
auto scrollableAreaForLastNode = enclosingScrollableArea(lastElementUnderMouse);
auto scrollableAreaForNodeUnderMouse = enclosingScrollableArea(elementUnderMouse);
if (!!lastElementUnderMouse != !!elementUnderMouse) {
if (elementUnderMouse) {
if (scrollableAreaForNodeUnderMouse != frameView)
frameView->mouseEnteredContentArea();
if (scrollableAreaForNodeUnderMouse)
scrollableAreaForNodeUnderMouse->mouseEnteredContentArea();
} else {
if (scrollableAreaForLastNode)
scrollableAreaForLastNode->mouseExitedContentArea();
if (scrollableAreaForLastNode != frameView)
frameView->mouseExitedContentArea();
}
return;
}
if (!scrollableAreaForLastNode && !scrollableAreaForNodeUnderMouse)
return;
// FIXME: This does doesn't handle nested ScrollableAreas well. It really needs to know
// the hierarchical relationship between scrollableAreaForLastNode and scrollableAreaForNodeUnderMouse.
bool movedBetweenScrollableaAreas = scrollableAreaForLastNode && scrollableAreaForNodeUnderMouse && (scrollableAreaForLastNode != scrollableAreaForNodeUnderMouse);
if (eventType == eventNames().mousemoveEvent) {
frameView->mouseMovedInContentArea();
if (!movedBetweenScrollableaAreas && scrollableAreaForNodeUnderMouse && scrollableAreaForNodeUnderMouse != frameView)
scrollableAreaForNodeUnderMouse->mouseMovedInContentArea();
}
if (!movedBetweenScrollableaAreas)
return;
if (scrollableAreaForLastNode && scrollableAreaForLastNode != frameView)
scrollableAreaForLastNode->mouseExitedContentArea();
if (scrollableAreaForNodeUnderMouse && scrollableAreaForNodeUnderMouse != frameView)
scrollableAreaForNodeUnderMouse->mouseEnteredContentArea();
}
bool EventHandler::dispatchMouseEvent(const AtomString& eventType, Node* targetNode, int clickCount, const PlatformMouseEvent& platformMouseEvent, FireMouseOverOut fireMouseOverOut)
{
Ref frame = m_frame.get();
updateMouseEventTargetNode(eventType, targetNode, platformMouseEvent, fireMouseOverOut);
bool isMouseDownEvent = eventType == eventNames().mousedownEvent;
if (auto elementUnderMouse = m_elementUnderMouse) {
auto [eventIsDispatched, eventIsDefaultPrevented] = elementUnderMouse->dispatchMouseEvent(platformMouseEvent, eventType, clickCount, nullptr, IsSyntheticClick::No);
m_capturesDragging = CapturesDragging::InabilityReason::Unknown;
if (eventIsDefaultPrevented == Element::EventIsDefaultPrevented::Yes) {
if (isMouseDownEvent)
m_capturesDragging = CapturesDragging::InabilityReason::MousePressIsCancelled;
else if (eventType == eventNames().mousemoveEvent)
m_capturesDragging = CapturesDragging::InabilityReason::MouseMoveIsCancelled;
}
if (eventIsDispatched == Element::EventIsDispatched::No)
return false;
}
if (!isMouseDownEvent)
return true;
m_mouseDownDelegatedFocus = false;
// If clicking on a frame scrollbar, do not make any change to which element is focused.
RefPtr view = frame->view();
if (view && view->scrollbarAtPoint(platformMouseEvent.position()))
return true;
// The layout needs to be up to date to determine if an element is focusable.
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
// Remove focus from the currently focused element when a link or button is clicked.
// This is expected by some sites that rely on change event handlers running
// from form fields before the button click is processed, behavior that was inherited
// from the user interface of Windows, where pushing a button moves focus to the button.
// Walk up the DOM tree to search for an element to focus.
RefPtr<Element> element;
for (element = m_elementUnderMouse.get(); element; element = element->parentElementInComposedTree()) {
if (RefPtr shadowRoot = element->shadowRoot()) {
if (shadowRoot->delegatesFocus()) {
element = Element::findFocusDelegateForTarget(*shadowRoot, FocusTrigger::Click);
m_mouseDownDelegatedFocus = true;
break;
}
}
if (element->isMouseFocusable())
break;
}
// To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus an
// element on mouse down if it's selected and inside a focused element. It will be
// focused if the user does a mouseup over it, however, because the mouseup
// will set a selection inside it, which will also set the focused element.
if (element && frame->selection().isRange()) {
if (auto range = frame->selection().selection().toNormalizedRange()) {
if (contains<ComposedTree>(*range, *element) && element->isDescendantOf(frame->document()->protectedFocusedElement().get()))
return true;
}
}
// Only change the focus when clicking scrollbars if it can be transferred to a mouse focusable node.
if (!element && isInsideScrollbar(platformMouseEvent.position()))
return false;
#if (!PLATFORM(GTK) && !PLATFORM(WPE))
// This is a workaround related to :focus-visible (see webkit.org/b/236782).
// Form control elements are not mouse focusable on some platforms (see HTMLFormControlElement::isMouseFocusable())
// which makes us behave differently than other browsers when a button is clicked,
// because the button is not actually focused so we don't set the latest FocusTrigger.
if (m_elementUnderMouse && !m_elementUnderMouse->isMouseFocusable() && is<HTMLFormControlElement>(m_elementUnderMouse))
frame->protectedDocument()->setLatestFocusTrigger(FocusTrigger::Click);
#endif
// If focus shift is blocked, we eat the event.
RefPtr page = frame->page();
if (page && !page->checkedFocusController()->setFocusedElement(element.get(), protectedFrame(), { { }, { }, { }, FocusTrigger::Click, { } }))
return false;
if (element && m_mouseDownDelegatedFocus)
element->findTargetAndUpdateFocusAppearance(SelectionRestorationMode::SelectAll);
return true;
}
bool EventHandler::isInsideScrollbar(const IntPoint& windowPoint) const
{
if (RefPtr document = m_frame->document()) {
HitTestResult result { windowPoint };
document->hitTest(OptionSet<HitTestRequest::Type> { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::DisallowUserAgentShadowContent }, result);
return result.scrollbar();
}
return false;
}
#if !PLATFORM(MAC)
void EventHandler::determineWheelEventTarget(const PlatformWheelEvent&, RefPtr<Element>&, WeakPtr<ScrollableArea>&, bool&)
{
}
bool EventHandler::processWheelEventForScrolling(const PlatformWheelEvent& event, const WeakPtr<ScrollableArea>&, OptionSet<EventHandling> eventHandling)
{
Ref frame = m_frame.get();
// We do another check on the frame view because the event handler can run JS which results in the frame getting destroyed.
RefPtr view = frame->view();
bool didHandleEvent = view ? handleWheelEventInScrollableArea(event, *view, eventHandling) : false;
m_isHandlingWheelEvent = false;
return didHandleEvent;
}
void EventHandler::wheelEventWasProcessedByMainThread(const PlatformWheelEvent& wheelEvent, OptionSet<EventHandling> eventHandling)
{
updateWheelGestureState(wheelEvent, eventHandling);
#if ENABLE(ASYNC_SCROLLING)
if (!m_frame->page())
return;
RefPtr view = m_frame->view();
if (RefPtr scrollingCoordinator = m_frame->page()->scrollingCoordinator()) {
if (scrollingCoordinator->coordinatesScrollingForFrameView(*view))
scrollingCoordinator->wheelEventWasProcessedByMainThread(wheelEvent, m_wheelScrollGestureState);
}
#endif
}
bool EventHandler::platformCompletePlatformWidgetWheelEvent(const PlatformWheelEvent&, const Widget&, const WeakPtr<ScrollableArea>&)
{
return true;
}
void EventHandler::processWheelEventForScrollSnap(const PlatformWheelEvent&, const WeakPtr<ScrollableArea>&)
{
}
#if !PLATFORM(IOS_FAMILY)
IntPoint EventHandler::targetPositionInWindowForSelectionAutoscroll() const
{
return valueOrDefault(m_lastKnownMousePosition);
}
#endif // !PLATFORM(IOS_FAMILY)
#endif // !PLATFORM(MAC)
#if !PLATFORM(IOS_FAMILY)
bool EventHandler::shouldUpdateAutoscroll()
{
return mousePressed();
}
#endif // !PLATFORM(IOS_FAMILY)
Widget* EventHandler::widgetForEventTarget(Element* eventTarget)
{
if (!eventTarget)
return nullptr;
auto* renderWidget = dynamicDowncast<RenderWidget>(eventTarget->renderer());
if (!renderWidget)
return nullptr;
return renderWidget->widget();
}
static RefPtr<Widget> widgetForElement(const Element& element)
{
auto* renderWidget = dynamicDowncast<RenderWidget>(element.renderer());
if (!renderWidget || !renderWidget->widget())
return { };
return renderWidget->widget();
}
bool EventHandler::completeWidgetWheelEvent(const PlatformWheelEvent& event, const SingleThreadWeakPtr<Widget>& widget, const WeakPtr<ScrollableArea>& scrollableArea)
{
m_isHandlingWheelEvent = false;
// We do another check on the widget because the event handler can run JS which results in the frame getting destroyed.
if (!widget)
return false;
if (scrollableArea)
scrollableArea->setScrollShouldClearLatchedState(false);
processWheelEventForScrollSnap(event, scrollableArea);
if (!widget->platformWidget())
return true;
return platformCompletePlatformWidgetWheelEvent(event, *widget.get(), scrollableArea);
}
std::pair<HandleUserInputEventResult, OptionSet<EventHandling>> EventHandler::handleWheelEvent(const PlatformWheelEvent& wheelEvent, OptionSet<WheelEventProcessingSteps> processingSteps)
{
Ref frame = m_frame.get();
#if ENABLE(KINETIC_SCROLLING)
if (wheelEvent.isGestureStart())
m_wheelScrollGestureState = std::nullopt;
#endif
OptionSet<EventHandling> handling;
auto handleWheelEventResult = handleWheelEventInternal(wheelEvent, processingSteps, handling);
// wheelEventWasProcessedByMainThread() may have already been called via performDefaultWheelEventHandling(), but this ensures that it's always called if that code path doesn't run.
wheelEventWasProcessedByMainThread(wheelEvent, handling);
return { handleWheelEventResult, handling };
}
HandleUserInputEventResult EventHandler::handleWheelEventInternal(const PlatformWheelEvent& event, OptionSet<WheelEventProcessingSteps> processingSteps, OptionSet<EventHandling>& handling)
{
Ref frame = m_frame.get();
RefPtr document = frame->document();
if (!document)
return false;
RefPtr view = frame->view();
if (!view)
return false;
if (!frame->page())
return false;
#if ENABLE(POINTER_LOCK)
if (frame->page()->pointerLockController().isLocked()) {
frame->protectedPage()->pointerLockController().dispatchLockedWheelEvent(event);
return true;
}
#endif
#if PLATFORM(COCOA) || PLATFORM(WIN)
LOG_WITH_STREAM(Scrolling, stream << "EventHandler::handleWheelEvent " << event << " processing steps " << processingSteps);
auto monitor = frame->page()->wheelEventTestMonitor();
if (monitor)
monitor->receivedWheelEventWithPhases(event.phase(), event.momentumPhase());
#endif
m_isHandlingWheelEvent = true;
auto allowsScrollingState = SetForScope(m_currentWheelEventAllowsScrolling, processingSteps.contains(WheelEventProcessingSteps::SynchronousScrolling));
setFrameWasScrolledByUser();
setLastKnownMousePosition(event.position(), event.globalPosition());
if (m_frame->isMainFrame()) {
RefPtr page = m_frame->page();
#if ENABLE(WHEEL_EVENT_LATCHING)
page->scrollLatchingController().receivedWheelEvent(event);
#endif
page->wheelEventDeltaFilter()->updateFromEvent(event);
}
HitTestRequest request;
HitTestResult result(view->windowToContents(event.position()));
document->hitTest(request, result);
RefPtr<Element> element = result.targetElement();
WeakPtr<ScrollableArea> scrollableArea;
bool isOverWidget = result.isOverWidget();
// FIXME: Despite doing this up-front search for the correct scrollable area, we dispatch events via elements which
// itself finds and tries to scroll overflow scrollers.
determineWheelEventTarget(event, element, scrollableArea, isOverWidget);
#if PLATFORM(COCOA) || PLATFORM(WIN)
std::unique_ptr<WheelEventTestMonitorCompletionDeferrer> deferrer;
if (scrollableArea)
deferrer = makeUnique<WheelEventTestMonitorCompletionDeferrer>(monitor.get(), scrollableArea->scrollingNodeIDForTesting(), WheelEventTestMonitor::DeferReason::HandlingWheelEventOnMainThread);
#endif
if (element) {
if (isOverWidget) {
if (RefPtr remoteSubframe = dynamicDowncast<RemoteFrame>(subframeForTargetNode(result.protectedTargetNode().get()))) {
if (auto wheelEventDataForRemoteFrame = userInputEventDataForRemoteFrame(remoteSubframe.get(), result.roundedPointInInnerNodeFrame()))
return *wheelEventDataForRemoteFrame;
} else if (RefPtr widget = widgetForElement(*element)) {
if (passWheelEventToWidget(event, *widget, processingSteps))
return completeWidgetWheelEvent(event, widget, scrollableArea);
}
}
auto isCancelable = processingSteps.contains(WheelEventProcessingSteps::BlockingDOMEventDispatch) ? Event::IsCancelable::Yes : Event::IsCancelable::No;
if (!element->dispatchWheelEvent(event, handling, isCancelable)) {
m_isHandlingWheelEvent = false;
if (scrollableArea && scrollableArea->scrollShouldClearLatchedState()) {
// Web developer is controlling scrolling, so don't attempt to latch.
if (handling.containsAll({ EventHandling::DispatchedToDOM, EventHandling::DefaultPrevented }))
clearLatchedState();
scrollableArea->setScrollShouldClearLatchedState(false);
}
processWheelEventForScrollSnap(event, scrollableArea);
return true;
}
}
if (scrollableArea)
scrollableArea->setScrollShouldClearLatchedState(false);
// Event handling may have disconnected m_frame.
if (!m_frame->page())
return false;
bool handledEvent = false;
bool allowScrolling = m_currentWheelEventAllowsScrolling;
#if ENABLE(WHEEL_EVENT_LATCHING)
if (allowScrolling)
allowScrolling = m_frame->page()->scrollLatchingController().latchingAllowsScrollingInFrame(protectedFrame(), scrollableArea);
#endif
auto adjustedWheelEvent = event;
auto filteredDelta = adjustedWheelEvent.delta();
filteredDelta = view->deltaForPropagation(filteredDelta);
if (view->shouldBlockScrollPropagation(filteredDelta))
return true;
if (allowScrolling) {
// FIXME: processWheelEventForScrolling() is only called for FrameView scrolling, not overflow scrolling, which is confusing.
adjustedWheelEvent = adjustedWheelEvent.copyWithDeltaAndVelocity(filteredDelta, adjustedWheelEvent.scrollingVelocity());
handledEvent = processWheelEventForScrolling(adjustedWheelEvent, scrollableArea, handling);
processWheelEventForScrollSnap(adjustedWheelEvent, scrollableArea);
}
return handledEvent;
}
static void handleWheelEventPhaseInScrollableArea(ScrollableArea& scrollableArea, const WheelEvent& wheelEvent)
{
#if PLATFORM(MAC)
if (wheelEvent.phase() == PlatformWheelEventPhase::MayBegin || wheelEvent.phase() == PlatformWheelEventPhase::Cancelled)
scrollableArea.scrollAnimator().handleWheelEventPhase(wheelEvent.phase());
#else
UNUSED_PARAM(scrollableArea);
UNUSED_PARAM(wheelEvent);
#endif
}
static bool scrollViaNonPlatformEvent(ScrollableArea& scrollableArea, const WheelEvent& wheelEvent)
{
auto filteredDelta = FloatSize(wheelEvent.deltaX(), wheelEvent.deltaY());
filteredDelta = scrollableArea.deltaForPropagation(filteredDelta);
ScrollGranularity scrollGranularity = wheelGranularityToScrollGranularity(wheelEvent.deltaMode());
bool didHandleWheelEvent = false;
if (float absoluteDelta = std::abs(filteredDelta.width()))
didHandleWheelEvent |= scrollableArea.scroll(filteredDelta.width() > 0 ? ScrollDirection::ScrollRight : ScrollDirection::ScrollLeft, scrollGranularity, absoluteDelta);
if (float absoluteDelta = std::abs(filteredDelta.height()))
didHandleWheelEvent |= scrollableArea.scroll(filteredDelta.height() > 0 ? ScrollDirection::ScrollDown : ScrollDirection::ScrollUp, scrollGranularity, absoluteDelta);
return didHandleWheelEvent;
}
bool EventHandler::handleWheelEventInAppropriateEnclosingBox(Node* startNode, const WheelEvent& wheelEvent, FloatSize& filteredPlatformDelta, const FloatSize& filteredVelocity, OptionSet<EventHandling> eventHandling)
{
bool shouldHandleEvent = wheelEvent.deltaX() || wheelEvent.deltaY();
#if ENABLE(WHEEL_EVENT_LATCHING)
shouldHandleEvent |= wheelEvent.phase() == PlatformWheelEventPhase::Ended;
shouldHandleEvent |= wheelEvent.momentumPhase() == PlatformWheelEventPhase::Ended;
#endif
if (!startNode->renderer())
return false;
RenderBox& initialEnclosingBox = startNode->renderer()->enclosingBox();
// RenderListBox is special because it's a ScrollableArea that the scrolling tree doesn't know about.
if (CheckedPtr renderListBox = dynamicDowncast<RenderListBox>(initialEnclosingBox))
handleWheelEventPhaseInScrollableArea(*renderListBox, wheelEvent);
if (!shouldHandleEvent)
return false;
auto scrollableAreaForBox = [](RenderBox& box) -> ScrollableArea* {
if (auto* renderListBox = dynamicDowncast<RenderListBox>(box))
return renderListBox;
if (box.hasLayer())
return box.layer()->scrollableArea();
return nullptr;
};
RenderBox* currentEnclosingBox = &initialEnclosingBox;
#if PLATFORM(MAC)
auto biasedDelta = ScrollingEffectsController::wheelDeltaBiasingTowardsVertical(FloatSize(wheelEvent.deltaX(), wheelEvent.deltaY()));
#else
auto biasedDelta = FloatSize(wheelEvent.deltaX(), wheelEvent.deltaY());
#endif
while (currentEnclosingBox) {
if (auto* boxScrollableArea = scrollableAreaForBox(*currentEnclosingBox)) {
auto platformEvent = wheelEvent.underlyingPlatformEvent();
bool scrollingWasHandled;
if (platformEvent) {
auto copiedEvent = platformEvent->copyWithDeltaAndVelocity(filteredPlatformDelta, filteredVelocity);
scrollingWasHandled = scrollableAreaCanHandleEvent(copiedEvent, *boxScrollableArea) && handleWheelEventInScrollableArea(copiedEvent, *boxScrollableArea, eventHandling);
} else
scrollingWasHandled = scrollViaNonPlatformEvent(*boxScrollableArea, wheelEvent);
if (scrollingWasHandled)
return true;
biasedDelta = boxScrollableArea->deltaForPropagation(biasedDelta);
if (boxScrollableArea->shouldBlockScrollPropagation(biasedDelta))
return true;
}
currentEnclosingBox = currentEnclosingBox->containingBlock();
if (!currentEnclosingBox || currentEnclosingBox->isRenderView())
return false;
}
return false;
}
bool EventHandler::scrollableAreaCanHandleEvent(const PlatformWheelEvent& wheelEvent, ScrollableArea& scrollableArea)
{
#if PLATFORM(MAC)
auto biasedDelta = ScrollingEffectsController::wheelDeltaBiasingTowardsVertical(wheelEvent.delta());
#else
auto biasedDelta = wheelEvent.delta();
#endif
auto verticalSide = ScrollableArea::targetSideForScrollDelta(-biasedDelta, ScrollEventAxis::Vertical);
if (verticalSide && !scrollableArea.isPinnedOnSide(*verticalSide))
return true;
auto horizontalSide = ScrollableArea::targetSideForScrollDelta(-biasedDelta, ScrollEventAxis::Horizontal);
if (horizontalSide && !scrollableArea.isPinnedOnSide(*horizontalSide))
return true;
if (scrollableArea.shouldBlockScrollPropagation(biasedDelta) && scrollableArea.overscrollBehaviorAllowsRubberBand())
return true;
return false;
}
bool EventHandler::handleWheelEventInScrollableArea(const PlatformWheelEvent& wheelEvent, ScrollableArea& scrollableArea, OptionSet<EventHandling> eventHandling)
{
auto gestureState = updateWheelGestureState(wheelEvent, eventHandling);
LOG_WITH_STREAM(Scrolling, stream << "EventHandler::handleWheelEventInScrollableArea() " << scrollableArea << " - eventHandling " << eventHandling << " -> gesture state " << gestureState);
return scrollableArea.handleWheelEventForScrolling(wheelEvent, gestureState);
}
std::optional<WheelScrollGestureState> EventHandler::updateWheelGestureState(const PlatformWheelEvent& wheelEvent, OptionSet<EventHandling> eventHandling)
{
#if ENABLE(KINETIC_SCROLLING)
if (!m_wheelScrollGestureState && wheelEvent.isGestureStart() && eventHandling.contains(EventHandling::DispatchedToDOM))
m_wheelScrollGestureState = eventHandling.contains(EventHandling::DefaultPrevented) ? WheelScrollGestureState::Blocking : WheelScrollGestureState::NonBlocking;
return m_wheelScrollGestureState;
#else
UNUSED_PARAM(wheelEvent);
UNUSED_PARAM(eventHandling);
return std::nullopt;
#endif
}
void EventHandler::clearLatchedState()
{
RefPtr<Page> page = m_frame->page();
if (!page)
return;
#if ENABLE(WHEEL_EVENT_LATCHING)
LOG_WITH_STREAM(ScrollLatching, stream << "EventHandler::clearLatchedState()");
if (auto* scrollLatchingController = page->scrollLatchingControllerIfExists())
scrollLatchingController->removeLatchingStateForFrame(Ref<LocalFrame> { m_frame.get() });
#endif
}
void EventHandler::defaultWheelEventHandler(Node* startNode, WheelEvent& wheelEvent)
{
if (!startNode)
return;
if (!m_frame->page())
return;
auto platformEvent = wheelEvent.underlyingPlatformEvent();
bool isUserEvent = platformEvent.has_value();
if (isUserEvent && !m_currentWheelEventAllowsScrolling)
return;
Ref frame = m_frame.get();
FloatSize filteredPlatformDelta(wheelEvent.deltaX(), wheelEvent.deltaY());
FloatSize filteredVelocity;
if (platformEvent)
filteredPlatformDelta = platformEvent->delta();
OptionSet<EventHandling> eventHandling = { EventHandling::DispatchedToDOM };
if (wheelEvent.defaultPrevented())
eventHandling.add(EventHandling::DefaultPrevented);
auto* deltaFilter = frame->page()->wheelEventDeltaFilter();
if (platformEvent && deltaFilter && WheelEventDeltaFilter::shouldApplyFilteringForEvent(*platformEvent)) {
filteredPlatformDelta = deltaFilter->filteredDelta();
filteredVelocity = deltaFilter->filteredVelocity();
}
#if ENABLE(WHEEL_EVENT_LATCHING)
WeakPtr<ScrollableArea> latchedScroller;
if (!frame->page()->scrollLatchingController().latchingAllowsScrollingInFrame(frame, latchedScroller))
return;
if (isUserEvent && latchedScroller) {
if (latchedScroller == frame->view()) {
// FrameView scrolling is handled via processWheelEventForScrolling().
return;
}
if (platformEvent) {
auto copiedEvent = platformEvent->copyWithDeltaAndVelocity(filteredPlatformDelta, filteredVelocity);
if (handleWheelEventInScrollableArea(copiedEvent, *latchedScroller, eventHandling))
wheelEvent.setDefaultHandled();
return;
}
}
#endif
if (handleWheelEventInAppropriateEnclosingBox(startNode, wheelEvent, filteredPlatformDelta, filteredVelocity, eventHandling))
wheelEvent.setDefaultHandled();
}
#if ENABLE(CONTEXT_MENU_EVENT)
bool EventHandler::sendContextMenuEvent(const PlatformMouseEvent& event)
{
Ref frame = m_frame.get();
#if ENABLE(POINTER_LOCK)
// Context menus should not be handled while pointer is locked.
if (auto* page = frame->page(); !page || page->pointerLockController().isLocked())
return false;
#endif
RefPtr doc = frame->document();
RefPtr view = frame->view();
if (!view)
return false;
// Caret blinking is normally un-suspended in handleMouseReleaseEvent, but we
// won't receive that event once the context menu is up.
frame->selection().setCaretBlinkingSuspended(false);
// Clear mouse press state to avoid initiating a drag while context menu is up.
m_mousePressed = false;
bool swallowEvent;
LayoutPoint viewportPos = view->windowToContents(event.position());
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::Active, HitTestRequest::Type::DisallowUserAgentShadowContent };
MouseEventWithHitTestResults mouseEvent = doc->prepareMouseEvent(hitType, viewportPos, event);
// Do not show context menus when clicking on scrollbars.
if (mouseEvent.scrollbar() || view->scrollbarAtPoint(event.position()))
return false;
if (frame->editor().behavior().shouldSelectOnContextualMenuClick()
&& !frame->selection().contains(viewportPos)) {
m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection
selectClosestContextualWordOrLinkFromHitTestResult(mouseEvent.hitTestResult(), shouldAppendTrailingWhitespace(mouseEvent, m_frame));
}
swallowEvent = !dispatchMouseEvent(eventNames().contextmenuEvent, mouseEvent.protectedTargetNode().get(), 0, event, FireMouseOverOut::No);
return swallowEvent;
}
bool EventHandler::sendContextMenuEventForKey()
{
Ref frame = m_frame.get();
RefPtr view = frame->view();
if (!view)
return false;
RefPtr doc = frame->document();
if (!doc)
return false;
// Clear mouse press state to avoid initiating a drag while context menu is up.
m_mousePressed = false;
static const int kContextMenuMargin = 1;
#if OS(WINDOWS)
int rightAligned = ::GetSystemMetrics(SM_MENUDROPALIGNMENT);
#else
int rightAligned = 0;
#endif
IntPoint location { rightAligned ? view->contentsWidth() - kContextMenuMargin : kContextMenuMargin, kContextMenuMargin };
RefPtr focusedElement = doc->focusedElement();
const VisibleSelection& selection = frame->selection().selection();
if (selection.start().deprecatedNode() && (selection.rootEditableElement() || selection.isRange())) {
std::optional<SimpleRange> targetRange;
if (selection.isCaret())
targetRange = selection.toNormalizedRange();
else {
auto endPosition = selection.visibleEnd();
targetRange = VisibleSelection { endPosition.previous(CannotCrossEditingBoundary), endPosition }.toNormalizedRange();
}
if (targetRange) {
IntRect targetRect = frame->editor().firstRectForRange(*targetRange);
int x = rightAligned ? targetRect.maxX() : targetRect.x();
// In a multiline edit, firstRect.maxY() would endup on the next line, so -1.
int y = targetRect.maxY() ? targetRect.maxY() - 1 : 0;
location = IntPoint(x, y);
}
} else if (focusedElement) {
RenderBoxModelObject* box = focusedElement->renderBoxModelObject();
if (!box)
return false;
IntRect boundingBoxRect = box->absoluteBoundingBoxRect(true);
location = IntPoint(boundingBoxRect.x(), boundingBoxRect.maxY() - 1);
} else {
location = IntPoint(
rightAligned ? view->contentsWidth() - kContextMenuMargin : kContextMenuMargin,
kContextMenuMargin);
}
frame->protectedView()->setCursor(pointerCursor());
IntPoint position = view->contentsToRootView(location);
IntPoint globalPosition = view->hostWindow()->rootViewToScreen(IntRect(position, IntSize())).location();
RefPtr<Node> targetNode = doc->focusedElement();
if (!targetNode)
targetNode = doc;
// Use the focused node as the target for hover and active.
HitTestResult result(position);
result.setInnerNode(targetNode.get());
doc->updateHoverActiveState(OptionSet<HitTestRequest::Type> { HitTestRequest::Type::Active, HitTestRequest::Type::DisallowUserAgentShadowContent }, result.targetElement());
// The contextmenu event is a mouse event even when invoked using the keyboard.
// This is required for web compatibility.
#if OS(WINDOWS)
PlatformEvent::Type eventType = PlatformEvent::Type::MouseReleased;
#else
PlatformEvent::Type eventType = PlatformEvent::Type::MousePressed;
#endif
PlatformMouseEvent platformMouseEvent(position, globalPosition, MouseButton::Right, eventType, 1, { }, WallTime::now(), ForceAtClick, SyntheticClickType::NoTap);
return sendContextMenuEvent(platformMouseEvent);
}
#endif // ENABLE(CONTEXT_MENU_EVENT)
void EventHandler::scheduleHoverStateUpdate()
{
if (!m_hoverTimer.isActive())
m_hoverTimer.startOneShot(0_s);
}
void EventHandler::scheduleCursorUpdate()
{
if (m_hasScheduledCursorUpdate)
return;
RefPtr page = m_frame->page();
if (!page)
return;
if (!page->chrome().client().supportsSettingCursor())
return;
m_hasScheduledCursorUpdate = true;
page->scheduleRenderingUpdate(RenderingUpdateStep::CursorUpdate);
}
void EventHandler::dispatchFakeMouseMoveEventSoon()
{
#if !ENABLE(IOS_TOUCH_EVENTS)
if (m_mousePressed)
return;
if (!m_lastKnownMousePosition)
return;
if (RefPtr page = m_frame->page()) {
if (!page->chrome().client().shouldDispatchFakeMouseMoveEvents())
return;
}
// If the content has ever taken longer than fakeMouseMoveShortInterval we
// reschedule the timer and use a longer time. This will cause the content
// to receive these moves only after the user is done scrolling, reducing
// pauses during the scroll.
if (m_fakeMouseMoveEventTimer.isActive())
m_fakeMouseMoveEventTimer.stop();
m_fakeMouseMoveEventTimer.startOneShot(m_maxMouseMovedDuration > fakeMouseMoveDurationThreshold ? fakeMouseMoveLongInterval : fakeMouseMoveShortInterval);
#endif
}
void EventHandler::dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad& quad)
{
#if ENABLE(IOS_TOUCH_EVENTS)
UNUSED_PARAM(quad);
#else
RefPtr view = m_frame->view();
if (!view)
return;
if (!quad.containsPoint(view->windowToContents(valueOrDefault(m_lastKnownMousePosition))))
return;
dispatchFakeMouseMoveEventSoon();
#endif
}
#if !ENABLE(IOS_TOUCH_EVENTS)
void EventHandler::cancelFakeMouseMoveEvent()
{
m_fakeMouseMoveEventTimer.stop();
}
void EventHandler::fakeMouseMoveEventTimerFired()
{
ASSERT(!m_mousePressed);
Ref frame = m_frame.get();
if (!frame->view())
return;
if (!frame->page() || !frame->page()->isVisible() || !frame->page()->focusController().isActive())
return;
auto modifiers = PlatformKeyboardEvent::currentStateOfModifierKeys();
PlatformMouseEvent fakeMouseMoveEvent(valueOrDefault(m_lastKnownMousePosition), m_lastKnownMouseGlobalPosition, MouseButton::None, PlatformEvent::Type::MouseMoved, 0, modifiers, WallTime::now(), 0, SyntheticClickType::NoTap);
mouseMoved(fakeMouseMoveEvent);
}
#endif // !ENABLE(IOS_TOUCH_EVENTS)
void EventHandler::setResizingFrameSet(HTMLFrameSetElement* frameSet)
{
m_frameSetBeingResized = frameSet;
}
void EventHandler::resizeLayerDestroyed()
{
ASSERT(m_resizeLayer);
m_resizeLayer = nullptr;
}
void EventHandler::hoverTimerFired()
{
m_hoverTimer.stop();
ASSERT(m_frame->document());
Ref frame = m_frame.get();
if (RefPtr document = frame->document()) {
if (RefPtr view = frame->view()) {
HitTestResult result(view->windowToContents(valueOrDefault(m_lastKnownMousePosition)));
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::Move, HitTestRequest::Type::DisallowUserAgentShadowContent };
document->hitTest(hitType, result);
document->updateHoverActiveState(hitType, result.targetElement());
}
}
}
#if ENABLE(IMAGE_ANALYSIS)
void EventHandler::textRecognitionHoverTimerFired()
{
RefPtr element = this->textRecognitionCandidateElement();
if (!element)
return;
if (RefPtr page = m_frame->page())
page->chrome().client().requestTextRecognition(*element, { });
}
#endif // ENABLE(IMAGE_ANALYSIS)
bool EventHandler::handleAccessKey(const PlatformKeyboardEvent& event)
{
Ref frame = m_frame.get();
// FIXME: Ignoring the state of Shift key is what neither IE nor Firefox do.
// IE matches lower and upper case access keys regardless of Shift key state - but if both upper and
// lower case variants are present in a document, the correct element is matched based on Shift key state.
// Firefox only matches an access key if Shift is not pressed, and does that case-insensitively.
ASSERT(!accessKeyModifiers().contains(PlatformEvent::Modifier::ShiftKey));
if ((event.modifiers() - PlatformEvent::Modifier::ShiftKey) != accessKeyModifiers())
return false;
RefPtr element = frame->protectedDocument()->elementForAccessKey(event.unmodifiedText());
if (!element)
return false;
element->accessKeyAction(false);
return true;
}
#if !PLATFORM(MAC)
bool EventHandler::needsKeyboardEventDisambiguationQuirks() const
{
return false;
}
#endif
#if ENABLE(FULLSCREEN_API)
bool EventHandler::isKeyEventAllowedInFullScreen(const PlatformKeyboardEvent& keyEvent) const
{
RefPtr document = m_frame->document();
if (document->fullscreenManager().isFullscreenKeyboardInputAllowed())
return true;
if (keyEvent.type() == PlatformKeyboardEvent::Type::Char) {
if (keyEvent.text().length() != 1)
return false;
UChar character = keyEvent.text()[0];
return character == ' ';
}
int keyCode = keyEvent.windowsVirtualKeyCode();
return (keyCode >= VK_BACK && keyCode <= VK_CAPITAL)
|| (keyCode >= VK_SPACE && keyCode <= VK_DELETE)
|| (keyCode >= VK_OEM_1 && keyCode <= VK_OEM_PLUS)
|| (keyCode >= VK_MULTIPLY && keyCode <= VK_OEM_8);
}
#endif
bool EventHandler::keyEvent(const PlatformKeyboardEvent& keyEvent)
{
Ref frame = m_frame.get();
RefPtr page = frame->protectedPage();
RefPtr mainFrameDocument = frame->document() ? frame->document()->protectedMainFrameDocument() : nullptr;
MonotonicTime savedLastHandledUserGestureTimestamp;
bool savedUserDidInteractWithPage = page ? page->userDidInteractWithPage() : false;
if (RefPtr document = frame->document())
savedLastHandledUserGestureTimestamp = document->lastHandledUserGestureTimestamp();
bool wasHandled = internalKeyEvent(keyEvent);
// If the key event was not handled, do not treat it as user interaction with the page.
if (mainFrameDocument) {
if (!wasHandled) {
if (page)
page->setUserDidInteractWithPage(savedUserDidInteractWithPage);
} else
ResourceLoadObserver::shared().logUserInteractionWithReducedTimeResolution(*mainFrameDocument);
}
if (!wasHandled && frame->document())
frame->protectedDocument()->updateLastHandledUserGestureTimestamp(savedLastHandledUserGestureTimestamp);
return wasHandled;
}
void EventHandler::capsLockStateMayHaveChanged() const
{
RefPtr input = dynamicDowncast<HTMLInputElement>(m_frame->document()->focusedElement());
if (!input)
return;
input->capsLockStateMayHaveChanged();
}
bool EventHandler::internalKeyEvent(const PlatformKeyboardEvent& initialKeyEvent)
{
Ref frame = m_frame.get();
RefPtr protectedView { frame->view() };
LOG(Editing, "EventHandler %p keyEvent (text %s keyIdentifier %s)", this, initialKeyEvent.text().utf8().data(), initialKeyEvent.keyIdentifier().utf8().data());
#if ENABLE(POINTER_LOCK)
if (initialKeyEvent.type() == PlatformEvent::Type::KeyDown && initialKeyEvent.windowsVirtualKeyCode() == VK_ESCAPE && frame->page()->pointerLockController().element()) {
frame->protectedPage()->pointerLockController().requestPointerUnlockAndForceCursorVisible();
}
#endif
if (initialKeyEvent.type() == PlatformEvent::Type::KeyDown && initialKeyEvent.windowsVirtualKeyCode() == VK_ESCAPE) {
if (RefPtr page = frame->page()) {
if (auto* validationMessageClient = page->validationMessageClient())
validationMessageClient->hideAnyValidationMessage();
}
}
#if ENABLE(FULLSCREEN_API)
RefPtr document = frame->document();
if (CheckedPtr fullscreenManager = document->fullscreenManagerIfExists(); fullscreenManager && fullscreenManager->isFullscreen()) {
if (initialKeyEvent.type() == PlatformEvent::Type::KeyDown && initialKeyEvent.windowsVirtualKeyCode() == VK_ESCAPE) {
fullscreenManager->fullyExitFullscreen();
return true;
}
if (!isKeyEventAllowedInFullScreen(initialKeyEvent))
return false;
}
#endif
if (initialKeyEvent.windowsVirtualKeyCode() == VK_CAPITAL)
capsLockStateMayHaveChanged();
#if ENABLE(PAN_SCROLLING)
auto* localFrame = dynamicDowncast<LocalFrame>(frame->mainFrame());
if (!localFrame)
return false;
if (Ref(*localFrame)->eventHandler().panScrollInProgress()) {
// If a key is pressed while the panScroll is in progress then we want to stop
if (initialKeyEvent.type() == PlatformEvent::Type::KeyDown || initialKeyEvent.type() == PlatformEvent::Type::RawKeyDown)
stopAutoscrollTimer();
// If we were in panscroll mode, we swallow the key event
return true;
}
#endif
// Check for cases where we are too early for events -- possible unmatched key up
// from pressing return in the location bar.
RefPtr<Element> element = eventTargetElementForDocument(frame->protectedDocument().get());
if (!element)
return false;
UserGestureType gestureType = userGestureTypeForPlatformEvent(initialKeyEvent);
auto canRequestDOMPaste = frame->protectedDocument()->quirks().needsDisableDOMPasteAccessQuirk() ? CanRequestDOMPaste::No : CanRequestDOMPaste::Yes;
UserGestureIndicator gestureIndicator(IsProcessingUserGesture::Yes, frame->protectedDocument().get(), gestureType, UserGestureIndicator::ProcessInteractionStyle::Delayed, initialKeyEvent.authorizationToken(), canRequestDOMPaste);
UserTypingGestureIndicator typingGestureIndicator(frame);
// FIXME (bug 68185): this call should be made at another abstraction layer
frame->protectedLoader()->resetMultipleFormSubmissionProtection();
// In IE, access keys are special, they are handled after default keydown processing, but cannot be canceled - this is hard to match.
// On Mac OS X, we process them before dispatching keydown, as the default keydown handler implements Emacs key bindings, which may conflict
// with access keys. Then we dispatch keydown, but suppress its default handling.
// On Windows, WebKit explicitly calls handleAccessKey() instead of dispatching a keypress event for WM_SYSCHAR messages.
// Other platforms currently match either Mac or Windows behavior, depending on whether they send combined KeyDown events.
bool matchedAnAccessKey = false;
if (initialKeyEvent.type() == PlatformEvent::Type::KeyDown)
matchedAnAccessKey = handleAccessKey(initialKeyEvent);
if (initialKeyEvent.type() == PlatformEvent::Type::KeyUp)
stopKeyboardScrolling();
// FIXME: it would be fair to let an input method handle KeyUp events before DOM dispatch.
if (initialKeyEvent.type() == PlatformEvent::Type::KeyUp || initialKeyEvent.type() == PlatformEvent::Type::Char)
return !element->dispatchKeyEvent(initialKeyEvent);
bool backwardCompatibilityMode = needsKeyboardEventDisambiguationQuirks();
PlatformKeyboardEvent keyDownEvent = initialKeyEvent;
if (keyDownEvent.type() != PlatformEvent::Type::RawKeyDown)
keyDownEvent.disambiguateKeyDownEvent(PlatformEvent::Type::RawKeyDown, backwardCompatibilityMode);
auto keydown = KeyboardEvent::create(keyDownEvent, &frame->windowProxy());
if (matchedAnAccessKey)
keydown->preventDefault();
keydown->setTarget(element.copyRef());
auto setHasFocusVisibleIfNeeded = [initialKeyEvent, keydown](Element& element) {
// If the user interacts with the page via the keyboard, the currently focused element should match :focus-visible.
// Just typing a modifier key is not considered user interaction with the page, but Shift + a (or Caps Lock + a) is considered an interaction.
bool userHasInteractedViaKeyword = keydown->modifierKeys().isEmpty() || ((keydown->shiftKey() || keydown->capsLockKey()) && !initialKeyEvent.text().isEmpty());
if (element.focused() && userHasInteractedViaKeyword) {
Style::PseudoClassChangeInvalidation focusVisibleStyleInvalidation(element, CSSSelector::PseudoClass::FocusVisible, true);
element.setHasFocusVisible(true);
}
};
setHasFocusVisibleIfNeeded(*element);
if (initialKeyEvent.type() == PlatformEvent::Type::RawKeyDown) {
element->dispatchEvent(keydown);
// If frame changed as a result of keydown dispatch, then return true to avoid sending a subsequent keypress message to the new frame.
bool changedFocusedFrame = frame->page() && frame.ptr() != frame->page()->checkedFocusController()->focusedOrMainFrame();
return keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
}
// Run input method in advance of DOM event handling. This may result in the IM
// modifying the page prior the keydown event, but this behaviour is necessary
// in order to match IE:
// 1. preventing default handling of keydown and keypress events has no effect on IM input;
// 2. if an input method handles the event, its keyCode is set to 229 in keydown event.
frame->editor().handleInputMethodKeydown(keydown.get());
bool handledByInputMethod = keydown->defaultHandled();
if (handledByInputMethod) {
keyDownEvent.setWindowsVirtualKeyCode(CompositionEventKeyCode);
keydown = KeyboardEvent::create(keyDownEvent, &frame->windowProxy());
keydown->setTarget(element.copyRef());
keydown->setIsDefaultEventHandlerIgnored();
}
if (accessibilityPreventsEventPropagation(keydown))
keydown->stopPropagation();
#if ENABLE(CONTENT_CHANGE_OBSERVER)
DeferDOMTimersForScope deferralScope { frame->document()->quirks().needsDeferKeyDownAndKeyPressTimersUntilNextEditingCommand() };
#endif
element->dispatchEvent(keydown);
if (handledByInputMethod) {
frame->editor().didDispatchInputMethodKeydown(keydown.get());
return true;
}
// If frame changed as a result of keydown dispatch, then return early to avoid sending a subsequent keypress message to the new frame.
bool changedFocusedFrame = frame->page() && frame.ptr() != frame->page()->checkedFocusController()->focusedOrMainFrame();
bool keydownResult = keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
if (keydownResult && !backwardCompatibilityMode)
return keydownResult;
// Focus may have changed during keydown handling, so refetch element.
// But if we are dispatching a fake backward compatibility keypress, then we pretend that the keypress happened on the original element.
if (!keydownResult) {
element = eventTargetElementForDocument(frame->protectedDocument().get());
if (!element)
return false;
setHasFocusVisibleIfNeeded(*element);
}
PlatformKeyboardEvent keyPressEvent = initialKeyEvent;
keyPressEvent.disambiguateKeyDownEvent(PlatformEvent::Type::Char, backwardCompatibilityMode);
if (keyPressEvent.text().isEmpty())
return keydownResult;
auto keypress = KeyboardEvent::create(keyPressEvent, &frame->windowProxy());
keypress->setTarget(element.copyRef());
if (keydownResult)
keypress->preventDefault();
#if PLATFORM(COCOA)
keypress->keypressCommands() = keydown->keypressCommands();
#endif
element->dispatchEvent(keypress);
return keydownResult || keypress->defaultPrevented() || keypress->defaultHandled();
}
static FocusDirection focusDirectionForKey(const AtomString& keyIdentifier)
{
static MainThreadNeverDestroyed<const AtomString> Down("Down"_s);
static MainThreadNeverDestroyed<const AtomString> Up("Up"_s);
static MainThreadNeverDestroyed<const AtomString> Left("Left"_s);
static MainThreadNeverDestroyed<const AtomString> Right("Right"_s);
FocusDirection retVal = FocusDirection::None;
if (keyIdentifier == Down)
retVal = FocusDirection::Down;
else if (keyIdentifier == Up)
retVal = FocusDirection::Up;
else if (keyIdentifier == Left)
retVal = FocusDirection::Left;
else if (keyIdentifier == Right)
retVal = FocusDirection::Right;
return retVal;
}
static void setInitialKeyboardSelection(LocalFrame& frame, SelectionDirection direction)
{
RefPtr document = frame.document();
if (!document)
return;
FrameSelection& selection = frame.selection();
if (!selection.isNone())
return;
RefPtr focusedElement = document->focusedElement();
VisiblePosition visiblePosition;
switch (direction) {
case SelectionDirection::Backward:
case SelectionDirection::Left:
if (focusedElement)
visiblePosition = VisiblePosition(positionBeforeNode(focusedElement.get()));
else
visiblePosition = endOfDocument(document.get());
break;
case SelectionDirection::Forward:
case SelectionDirection::Right:
if (focusedElement)
visiblePosition = VisiblePosition(positionAfterNode(focusedElement.get()));
else
visiblePosition = startOfDocument(document.get());
break;
}
AXTextStateChangeIntent intent(AXTextStateChangeTypeSelectionMove, AXTextSelection { AXTextSelectionDirectionDiscontiguous, AXTextSelectionGranularityUnknown, false });
selection.setSelection(visiblePosition, FrameSelection::defaultSetSelectionOptions(UserTriggered::Yes), intent);
}
static void handleKeyboardSelectionMovement(LocalFrame& frame, KeyboardEvent& event)
{
FrameSelection& selection = frame.selection();
bool isCommanded = event.getModifierState("Meta"_s);
bool isOptioned = event.getModifierState("Alt"_s);
bool isSelection = !selection.isNone();
FrameSelection::Alteration alternation = event.getModifierState("Shift"_s) ? FrameSelection::Alteration::Extend : FrameSelection::Alteration::Move;
SelectionDirection direction = SelectionDirection::Forward;
TextGranularity granularity = TextGranularity::CharacterGranularity;
switch (focusDirectionForKey(event.keyIdentifier())) {
case FocusDirection::None:
return;
case FocusDirection::Forward:
case FocusDirection::Backward:
ASSERT_NOT_REACHED();
return;
case FocusDirection::Up:
direction = SelectionDirection::Backward;
granularity = isCommanded ? TextGranularity::DocumentBoundary : TextGranularity::LineGranularity;
break;
case FocusDirection::Down:
direction = SelectionDirection::Forward;
granularity = isCommanded ? TextGranularity::DocumentBoundary : TextGranularity::LineGranularity;
break;
case FocusDirection::Left:
direction = SelectionDirection::Left;
granularity = (isCommanded) ? TextGranularity::LineBoundary : (isOptioned) ? TextGranularity::WordGranularity : TextGranularity::CharacterGranularity;
break;
case FocusDirection::Right:
direction = SelectionDirection::Right;
granularity = (isCommanded) ? TextGranularity::LineBoundary : (isOptioned) ? TextGranularity::WordGranularity : TextGranularity::CharacterGranularity;
break;
}
if (isSelection)
selection.modify(alternation, direction, granularity, UserTriggered::Yes);
else
setInitialKeyboardSelection(frame, direction);
event.setDefaultHandled();
}
void EventHandler::handleKeyboardSelectionMovementForAccessibility(KeyboardEvent& event)
{
if (event.type() == eventNames().keydownEvent) {
if (AXObjectCache::accessibilityEnhancedUserInterfaceEnabled())
handleKeyboardSelectionMovement(protectedFrame(), event);
}
}
bool EventHandler::accessibilityPreventsEventPropagation(KeyboardEvent& event)
{
#if PLATFORM(COCOA)
if (!AXObjectCache::accessibilityEnhancedUserInterfaceEnabled())
return false;
if (!m_frame->settings().preventKeyboardDOMEventDispatch())
return false;
// Check for key events that are relevant to accessibility: tab and arrows keys that change focus
if (event.keyIdentifier() == "U+0009"_s)
return true;
FocusDirection direction = focusDirectionForKey(event.keyIdentifier());
if (direction != FocusDirection::None)
return true;
#else
UNUSED_PARAM(event);
#endif
return false;
}
void EventHandler::defaultKeyboardEventHandler(KeyboardEvent& event)
{
Ref frame = m_frame.get();
// 'keyup' is handled preemptively in `EventHandler::internalKeyEvent` so that keyboard scrolls
// can be properly terminated even if the event is default-prevented.
if (event.type() == eventNames().keydownEvent) {
frame->editor().handleKeyboardEvent(event);
if (event.defaultHandled())
return;
if (event.key() == "Escape"_s) {
if (frame->settings().closeWatcherEnabled())
frame->document()->domWindow()->closeWatcherManager().escapeKeyHandler(event);
if (RefPtr activeModalDialog = frame->document()->activeModalDialog())
activeModalDialog->queueCancelTask();
if (RefPtr topmostAutoPopover = frame->document()->topmostAutoPopover())
topmostAutoPopover->hidePopover();
} else if (event.keyIdentifier() == "U+0009"_s)
defaultTabEventHandler(event);
else if (event.keyIdentifier() == "U+0008"_s)
defaultBackspaceEventHandler(event);
else if (event.keyIdentifier() == "PageDown"_s || event.keyIdentifier() == "PageUp"_s)
defaultPageUpDownEventHandler(event);
else if (event.keyIdentifier() == "Home"_s || event.keyIdentifier() == "End"_s)
defaultHomeEndEventHandler(event);
else {
FocusDirection direction = focusDirectionForKey(event.keyIdentifier());
if (direction != FocusDirection::None)
defaultArrowEventHandler(direction, event);
}
handleKeyboardSelectionMovementForAccessibility(event);
}
if (event.type() == eventNames().keypressEvent) {
frame->editor().handleKeyboardEvent(event);
if (event.defaultHandled())
return;
if (event.charCode() == ' ')
defaultSpaceEventHandler(event);
}
}
#if ENABLE(DRAG_SUPPORT)
bool EventHandler::dragHysteresisExceeded(const IntPoint& floatDragViewportLocation) const
{
FloatPoint dragViewportLocation(floatDragViewportLocation.x(), floatDragViewportLocation.y());
return dragHysteresisExceeded(dragViewportLocation);
}
bool EventHandler::dragHysteresisExceeded(const FloatPoint& dragViewportLocation) const
{
auto dragOperation = dragState().type.toSingleValue();
ASSERT(dragOperation);
int threshold = GeneralDragHysteresis;
if (dragOperation) {
switch (*dragOperation) {
case DragSourceAction::Selection:
threshold = TextDragHysteresis;
break;
case DragSourceAction::Image:
#if ENABLE(ATTACHMENT_ELEMENT)
case DragSourceAction::Attachment:
#endif
#if ENABLE(MODEL_ELEMENT)
case DragSourceAction::Model:
#endif
threshold = ImageDragHysteresis;
break;
case DragSourceAction::Link:
threshold = LinkDragHysteresis;
break;
case DragSourceAction::Color:
threshold = ColorDragHystersis;
break;
case DragSourceAction::DHTML:
break;
}
}
return mouseMovementExceedsThreshold(dragViewportLocation, threshold);
}
void EventHandler::invalidateDataTransfer()
{
if (!dragState().dataTransfer)
return;
dragState().dataTransfer->makeInvalidForSecurity();
dragState().dataTransfer = nullptr;
}
static void removeDraggedContentDocumentMarkersFromAllFramesInPage(Page& page)
{
page.forEachDocument([] (Document& document) {
document.markers().removeMarkers(DocumentMarkerType::DraggedContent);
});
if (RefPtr localMainFrame = page.localMainFrame()) {
if (auto* mainFrameRenderer = localMainFrame->contentRenderer())
mainFrameRenderer->repaintRootContents();
}
}
void EventHandler::dragCancelled()
{
#if PLATFORM(IOS_FAMILY)
if (RefPtr page = m_frame->page())
removeDraggedContentDocumentMarkersFromAllFramesInPage(*page);
#endif
}
void EventHandler::didStartDrag()
{
#if PLATFORM(IOS_FAMILY)
RefPtr dragSource = draggedElement();
if (!dragSource)
return;
if (!dragSource->renderer())
return;
std::optional<SimpleRange> draggedContentRange;
if (dragState().type.contains(DragSourceAction::Selection))
draggedContentRange = m_frame->selection().selection().toNormalizedRange();
else
draggedContentRange = makeRangeSelectingNode(*dragSource);
if (draggedContentRange) {
draggedContentRange->start.document().markers().addDraggedContentMarker(*draggedContentRange);
if (CheckedPtr renderer = m_frame->contentRenderer())
renderer->repaintRootContents();
}
#endif
}
std::optional<RemoteUserInputEventData> EventHandler::dragSourceEndedAt(const PlatformMouseEvent& event, OptionSet<DragOperation> dragOperationMask, MayExtendDragSession mayExtendDragSession)
{
// Send a hit test request so that RenderLayer gets a chance to update the :hover and :active pseudoclasses.
auto mouseEvent = prepareMouseEvent(OptionSet<HitTestRequest::Type> { HitTestRequest::Type::Release, HitTestRequest::Type::DisallowUserAgentShadowContent }, event);
if (RefPtr remoteSubframe = dynamicDowncast<RemoteFrame>(subframeForHitTestResult(mouseEvent))) {
// FIXME(264611): These mouse coordinates need to be correctly transformed.
return RemoteUserInputEventData { remoteSubframe->frameID(), mouseEvent.hitTestResult().roundedPointInInnerNodeFrame() };
}
if (shouldDispatchEventsToDragSourceElement()) {
dragState().dataTransfer->setDestinationOperationMask(dragOperationMask);
dispatchEventToDragSourceElement(eventNames().dragendEvent, event);
}
invalidateDataTransfer();
if (mayExtendDragSession == MayExtendDragSession::No) {
if (RefPtr page = m_frame->page())
removeDraggedContentDocumentMarkersFromAllFramesInPage(*page);
}
setDragStateSource(nullptr);
// In case the drag was ended due to an escape key press we need to ensure
// that consecutive mousemove events don't reinitiate the drag and drop.
m_mouseDownMayStartDrag = false;
return std::nullopt;
}
void EventHandler::updateDragStateAfterEditDragIfNeeded(Element& rootEditableElement)
{
// If inserting the dragged contents removed the drag source, we still want to fire dragend at the root editable element.
if (draggedElement() && !draggedElement()->isConnected())
setDragStateSource(&rootEditableElement);
}
bool EventHandler::shouldDispatchEventsToDragSourceElement()
{
return draggedElement() && dragState().dataTransfer && dragState().shouldDispatchEvents;
}
void EventHandler::dispatchEventToDragSourceElement(const AtomString& eventType, const PlatformMouseEvent& event)
{
if (shouldDispatchEventsToDragSourceElement())
dispatchDragEvent(eventType, *protectedDraggedElement(), event, *dragState().dataTransfer);
}
bool EventHandler::dispatchDragStartEventOnSourceElement(DataTransfer& dataTransfer)
{
if (RefPtr page = m_frame->page())
page->dragController().prepareForDragStart(protectedFrame(), dragState().type, *protectedDraggedElement(), dataTransfer, m_mouseDownContentsPosition);
return !dispatchDragEvent(eventNames().dragstartEvent, *protectedDraggedElement(), m_mouseDownEvent, dataTransfer) && !m_frame->selection().selection().isInPasswordField();
}
bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event, CheckDragHysteresis checkDragHysteresis)
{
if (event.event().button() != MouseButton::Left || event.event().type() != PlatformEvent::Type::MouseMoved) {
// If we allowed the other side of the bridge to handle a drag
// last time, then m_mousePressed might still be set. So we
// clear it now to make sure the next move after a drag
// doesn't look like a drag.
m_mousePressed = false;
return false;
}
Ref frame = m_frame.get();
if (eventLoopHandleMouseDragged(event))
return true;
// Careful that the drag starting logic stays in sync with eventMayStartDrag().
if (m_mouseDownMayStartDrag && !draggedElement()) {
dragState().shouldDispatchEvents = updateDragSourceActionsAllowed().contains(DragSourceAction::DHTML);
dragState().restrictedOriginForImageData = nullptr;
// Try to find an element that wants to be dragged.
HitTestResult result(m_mouseDownContentsPosition);
frame->protectedDocument()->hitTest(OptionSet<HitTestRequest::Type> { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::DisallowUserAgentShadowContent }, result);
if (RefPtr page = frame->page())
setDragStateSource(page->dragController().draggableElement(frame.ptr(), result.protectedTargetElement().get(), m_mouseDownContentsPosition, dragState()).get());
if (!draggedElement())
m_mouseDownMayStartDrag = false; // no element is draggable
else
m_dragMayStartSelectionInstead = dragState().type.contains(DragSourceAction::Selection);
}
// For drags starting in the selection, the user must wait between the mousedown and mousedrag,
// or else we bail on the dragging stuff and allow selection to occur
if (m_mouseDownMayStartDrag && m_dragMayStartSelectionInstead && dragState().type.contains(DragSourceAction::Selection) && event.event().timestamp() - m_mouseDownTimestamp < TextDragDelay) {
ASSERT(event.event().type() == PlatformEvent::Type::MouseMoved);
if (dragState().type.contains(DragSourceAction::Image)) {
// ... unless the mouse is over an image, then we start dragging just the image
dragState().type = DragSourceAction::Image;
} else if (!dragState().type.containsAny({ DragSourceAction::DHTML, DragSourceAction::Link })) {
// ... but only bail if we're not over an unselectable element.
m_mouseDownMayStartDrag = false;
setDragStateSource(nullptr);
// ... but if this was the first click in the window, we don't even want to start selection
if (eventActivatedView(event.event()))
m_mouseDownMayStartSelect = false;
} else {
// Prevent the following case from occuring:
// 1. User starts a drag immediately after mouse down over an unselectable element.
// 2. We enter this block and decided that since we're over an unselectable element, don't cancel the drag.
// 3. The drag gets resolved as a potential selection drag below /but/ we haven't exceeded the drag hysteresis yet.
// 4. We enter this block again, and since it's now marked as a selection drag, we cancel the drag.
m_dragMayStartSelectionInstead = false;
}
}
if (!m_mouseDownMayStartDrag)
return !mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll;
ASSERT(draggedElement());
if (!dragState().type.hasExactlyOneBitSet()) {
ASSERT(dragState().type.contains(DragSourceAction::Selection));
#ifndef NDEBUG
auto actionMaskCopy = dragState().type;
actionMaskCopy.remove(DragSourceAction::Selection);
ASSERT(actionMaskCopy.hasExactlyOneBitSet());
#endif
dragState().type = DragSourceAction::Selection;
}
// We are starting a text/image/url drag, so the cursor should be an arrow
if (RefPtr view = frame->view()) {
// FIXME <rdar://7577595>: Custom cursors aren't supported during drag and drop (default to pointer).
view->setCursor(pointerCursor());
}
if (checkDragHysteresis == ShouldCheckDragHysteresis && !dragHysteresisExceeded(event.event().position()))
return true;
// Once we're past the hysteresis point, we don't want to treat this gesture as a click
invalidateClick();
OptionSet<DragOperation> sourceOperationMask;
// This does work only if we missed a dragEnd. Do it anyway, just to make sure the old dataTransfer gets numbed.
// FIXME: Consider doing this earlier in this function as the earliest point we're sure it would be safe to drop an old drag.
invalidateDataTransfer();
RefPtr document = frame->document();
if (!document)
return false;
dragState().dataTransfer = DataTransfer::createForDrag(*document);
auto hasNonDefaultPasteboardData = HasNonDefaultPasteboardData::No;
if (dragState().shouldDispatchEvents) {
ASSERT(draggedElement());
auto dragStartDataTransfer = DataTransfer::createForDragStartEvent(draggedElement()->protectedDocument());
m_mouseDownMayStartDrag = dispatchDragStartEventOnSourceElement(dragStartDataTransfer);
if (downcast<StaticPasteboard>(dragStartDataTransfer->pasteboard()).hasNonDefaultData())
hasNonDefaultPasteboardData = HasNonDefaultPasteboardData::Yes;
dragState().dataTransfer->moveDragState(WTFMove(dragStartDataTransfer));
if (RefPtr draggedElement = this->draggedElement(); draggedElement && dragState().type == DragSourceAction::DHTML && !dragState().dataTransfer->hasDragImage()) {
draggedElement->protectedDocument()->updateStyleIfNeeded();
if (auto* renderer = draggedElement->renderer()) {
auto absolutePosition = renderer->localToAbsolute();
auto delta = m_mouseDownContentsPosition - roundedIntPoint(absolutePosition);
dragState().dataTransfer->setDragImage(draggedElement.releaseNonNull(), delta.width(), delta.height());
} else {
dispatchEventToDragSourceElement(eventNames().dragendEvent, event.event());
m_mouseDownMayStartDrag = false;
invalidateDataTransfer();
setDragStateSource(nullptr);
return true;
}
}
if (draggedElement() && dragState().type.containsAny({ DragSourceAction::DHTML, DragSourceAction::Image })) {
if (auto* renderImage = dynamicDowncast<RenderImage>(draggedElement()->renderer())) {
auto* image = renderImage->cachedImage();
if (image && !image->isCORSSameOrigin())
dragState().restrictedOriginForImageData = SecurityOrigin::create(image->url());
}
}
dragState().dataTransfer->makeInvalidForSecurity();
if (m_mouseDownMayStartDrag) {
// Gather values from DHTML element, if it set any.
sourceOperationMask = dragState().dataTransfer->sourceOperationMask();
// Yuck, a draggedImage:moveTo: message can be fired as a result of kicking off the
// drag with dragImage! Because of that reentrancy, we may think we've not
// started the drag when that happens. So we have to assume it's started before we kick it off.
dragState().dataTransfer->setDragHasStarted();
}
}
if (m_mouseDownMayStartDrag) {
RefPtr page = frame->page();
m_didStartDrag = page && page->dragController().startDrag(frame, dragState(), sourceOperationMask, event.event(), m_mouseDownContentsPosition, hasNonDefaultPasteboardData);
// In WebKit2 we could re-enter this code and start another drag.
// On OS X this causes problems with the ownership of the pasteboard and the promised types.
if (m_didStartDrag) {
m_mouseDownMayStartDrag = false;
return true;
}
if (shouldDispatchEventsToDragSourceElement()) {
// Drag was canned at the last minute. We owe dragSource a dragend event.
dispatchEventToDragSourceElement(eventNames().dragendEvent, event.event());
m_mouseDownMayStartDrag = false;
}
}
if (!m_mouseDownMayStartDrag) {
// Something failed to start the drag, clean up.
invalidateDataTransfer();
setDragStateSource(nullptr);
}
// No more default handling (like selection), whether we're past the hysteresis bounds or not
return true;
}
#endif // ENABLE(DRAG_SUPPORT)
bool EventHandler::mouseMovementExceedsThreshold(const FloatPoint& viewportLocation, int pointsThreshold) const
{
RefPtr view = m_frame->view();
if (!view)
return false;
IntPoint location = view->windowToContents(flooredIntPoint(viewportLocation));
IntSize delta = location - m_mouseDownContentsPosition;
return std::abs(delta.width()) >= pointsThreshold || std::abs(delta.height()) >= pointsThreshold;
}
bool EventHandler::handleTextInputEvent(const String& text, Event* underlyingEvent, TextEventInputType inputType)
{
LOG(Editing, "EventHandler %p handleTextInputEvent (text %s)", this, text.utf8().data());
// Platforms should differentiate real commands like selectAll from text input in disguise (like insertNewline),
// and avoid dispatching text input events from keydown default handlers.
ASSERT(!is<KeyboardEvent>(underlyingEvent) || downcast<KeyboardEvent>(*underlyingEvent).type() == eventNames().keypressEvent);
Ref frame = m_frame.get();
EventTarget* target;
if (underlyingEvent)
target = underlyingEvent->target();
else
target = eventTargetElementForDocument(frame->protectedDocument().get());
if (!target)
return false;
auto event = TextEvent::create(&frame->windowProxy(), text, inputType);
event->setUnderlyingEvent(underlyingEvent);
target->dispatchEvent(event);
return event->defaultHandled();
}
bool EventHandler::isKeyboardOptionTab(KeyboardEvent& event)
{
auto& eventNames = WebCore::eventNames();
return (event.type() == eventNames.keydownEvent || event.type() == eventNames.keypressEvent)
&& event.altKey()
&& event.keyIdentifier() == "U+0009"_s;
}
bool EventHandler::eventInvertsTabsToLinksClientCallResult(KeyboardEvent& event)
{
#if PLATFORM(COCOA)
return isKeyboardOptionTab(event);
#else
UNUSED_PARAM(event);
return false;
#endif
}
bool EventHandler::tabsToLinks(KeyboardEvent* event) const
{
// FIXME: This function needs a better name. It can be called for keypresses other than Tab when spatial navigation is enabled.
RefPtr page = m_frame->page();
if (!page)
return false;
bool tabsToLinksClientCallResult = page->chrome().client().keyboardUIMode() & KeyboardAccessTabsToLinks;
return (event && eventInvertsTabsToLinksClientCallResult(*event)) ? !tabsToLinksClientCallResult : tabsToLinksClientCallResult;
}
bool EventHandler::tabsToAllFormControls(KeyboardEvent* event) const
{
#if PLATFORM(COCOA)
RefPtr page = m_frame->page();
if (!page)
return false;
KeyboardUIMode keyboardUIMode = page->chrome().client().keyboardUIMode();
bool handlingOptionTab = event && isKeyboardOptionTab(*event);
// If tab-to-links is off, option-tab always highlights all controls
if (!(keyboardUIMode & KeyboardAccessTabsToLinks) && handlingOptionTab)
return true;
// If system preferences say to include all controls, we always include all controls
if (keyboardUIMode & KeyboardAccessFull)
return true;
// Otherwise tab-to-links includes all controls, unless the sense is flipped via option-tab.
if (keyboardUIMode & KeyboardAccessTabsToLinks)
return !handlingOptionTab;
return handlingOptionTab;
#else
UNUSED_PARAM(event);
// We always allow tabs to all controls
return true;
#endif
}
void EventHandler::defaultTextInputEventHandler(TextEvent& event)
{
if (m_frame->editor().handleTextEvent(event))
event.setDefaultHandled();
}
bool EventHandler::defaultKeyboardScrollEventHandler(KeyboardEvent& event, ScrollLogicalDirection direction, ScrollGranularity granularity)
{
if (shouldUseSmoothKeyboardScrollingForFocusedScrollableArea())
return keyboardScrollRecursively(scrollDirectionForKeyboardEvent(event), scrollGranularityForKeyboardEvent(event), nullptr, event.repeat());
return logicalScrollRecursively(direction, granularity);
}
void EventHandler::defaultPageUpDownEventHandler(KeyboardEvent& event)
{
#if PLATFORM(GTK) || PLATFORM(WPE) || PLATFORM(WIN)
ASSERT(event.type() == eventNames().keydownEvent);
if (event.ctrlKey() || event.metaKey() || event.altKey() || event.shiftKey())
return;
ScrollLogicalDirection direction = event.keyIdentifier() == "PageUp"_s ? ScrollBlockDirectionBackward : ScrollBlockDirectionForward;
if (defaultKeyboardScrollEventHandler(event, direction, ScrollGranularity::Page))
event.setDefaultHandled();
#else
UNUSED_PARAM(event);
#endif
}
void EventHandler::defaultHomeEndEventHandler(KeyboardEvent& event)
{
#if PLATFORM(GTK) || PLATFORM(WPE) || PLATFORM(WIN)
ASSERT(event.type() == eventNames().keydownEvent);
if (event.ctrlKey() || event.metaKey() || event.altKey() || event.shiftKey())
return;
ScrollLogicalDirection direction = event.keyIdentifier() == "Home"_s ? ScrollBlockDirectionBackward : ScrollBlockDirectionForward;
if (defaultKeyboardScrollEventHandler(event, direction, ScrollGranularity::Document))
event.setDefaultHandled();
#else
UNUSED_PARAM(event);
#endif
}
void EventHandler::defaultSpaceEventHandler(KeyboardEvent& event)
{
Ref frame = m_frame.get();
ASSERT(event.type() == eventNames().keypressEvent);
if (event.ctrlKey() || event.metaKey() || event.altKey())
return;
ScrollLogicalDirection direction = event.shiftKey() ? ScrollBlockDirectionBackward : ScrollBlockDirectionForward;
if (logicalScrollOverflow(direction, ScrollGranularity::Page)) {
event.setDefaultHandled();
return;
}
RefPtr view = frame->view();
if (!view)
return;
bool defaultHandled = false;
if (shouldUseSmoothKeyboardScrollingForFocusedScrollableArea())
defaultHandled = keyboardScroll(scrollDirectionForKeyboardEvent(event), scrollGranularityForKeyboardEvent(event), nullptr, event.repeat());
else
defaultHandled = view->logicalScroll(direction, ScrollGranularity::Page);
if (defaultHandled)
event.setDefaultHandled();
}
void EventHandler::defaultBackspaceEventHandler(KeyboardEvent& event)
{
ASSERT(event.type() == eventNames().keydownEvent);
if (event.ctrlKey() || event.metaKey() || event.altKey())
return;
if (!m_frame->editor().behavior().shouldNavigateBackOnBackspace())
return;
RefPtr page = m_frame->page();
if (!page)
return;
if (!m_frame->settings().backspaceKeyNavigationEnabled())
return;
bool handledEvent = false;
if (event.shiftKey())
handledEvent = page->checkedBackForward()->goForward();
else
handledEvent = page->checkedBackForward()->goBack();
if (handledEvent)
event.setDefaultHandled();
}
void EventHandler::stopKeyboardScrolling()
{
RefPtr page = m_frame->page();
if (!page)
return;
if (auto animator = page->currentKeyboardScrollingAnimator())
animator->handleKeyUpEvent();
}
bool EventHandler::beginKeyboardScrollGesture(KeyboardScrollingAnimator* animator, ScrollDirection direction, ScrollGranularity granularity, bool isKeyRepeat)
{
if (animator && animator->beginKeyboardScrollGesture(direction, granularity, isKeyRepeat)) {
m_frame->protectedPage()->setCurrentKeyboardScrollingAnimator(animator);
return true;
}
return false;
}
bool EventHandler::startKeyboardScrollAnimationOnDocument(ScrollDirection direction, ScrollGranularity granularity, bool isKeyRepeat)
{
RefPtr view = m_frame->view();
if (!view)
return false;
if (RefPtr pluginDocument = dynamicDowncast<PluginDocument>(m_frame->document())) {
if (RefPtr plugin = dynamicDowncast<RenderEmbeddedObject>(pluginDocument->pluginElement()->renderer())) {
if (startKeyboardScrollAnimationOnPlugin(direction, granularity, *plugin, isKeyRepeat))
return true;
}
}
auto* animator = view->scrollAnimator().keyboardScrollingAnimator();
return beginKeyboardScrollGesture(animator, direction, granularity, isKeyRepeat);
}
bool EventHandler::startKeyboardScrollAnimationOnRenderBoxLayer(ScrollDirection direction, ScrollGranularity granularity, RenderBox* renderBox, bool isKeyRepeat)
{
auto* scrollableArea = renderBox->layer() ? renderBox->layer()->scrollableArea() : nullptr;
if (!scrollableArea)
return false;
auto* animator = scrollableArea->scrollAnimator().keyboardScrollingAnimator();
return beginKeyboardScrollGesture(animator, direction, granularity, isKeyRepeat);
}
bool EventHandler::startKeyboardScrollAnimationOnRenderBoxAndItsAncestors(ScrollDirection direction, ScrollGranularity granularity, RenderBox* renderBox, bool isKeyRepeat)
{
while (renderBox && !renderBox->isRenderView()) {
if (startKeyboardScrollAnimationOnRenderBoxLayer(direction, granularity, renderBox, isKeyRepeat))
return true;
renderBox = renderBox->containingBlock();
}
return false;
}
bool EventHandler::startKeyboardScrollAnimationOnPlugin(ScrollDirection direction, ScrollGranularity granularity, RenderEmbeddedObject& pluginRenderer, bool isKeyRepeat)
{
auto* scrollableArea = pluginRenderer.scrollableArea();
if (!scrollableArea)
return false;
auto* animator = scrollableArea->scrollAnimator().keyboardScrollingAnimator();
if (!animator)
return false;
return beginKeyboardScrollGesture(animator, direction, granularity, isKeyRepeat);
}
bool EventHandler::startKeyboardScrollAnimationOnEnclosingScrollableContainer(ScrollDirection direction, ScrollGranularity granularity, Node* startingNode, bool isKeyRepeat)
{
RefPtr node = startingNode;
if (!node)
node = m_frame->document()->focusedElement();
if (!node)
node = m_mousePressNode.get();
if (node) {
auto renderer = node->renderer();
if (!renderer)
return false;
if (RefPtr plugin = dynamicDowncast<RenderEmbeddedObject>(renderer)) {
if (startKeyboardScrollAnimationOnPlugin(direction, granularity, *plugin, isKeyRepeat))
return true;
}
RenderBox& renderBox = renderer->enclosingBox();
if (!renderer->isRenderListBox() && startKeyboardScrollAnimationOnRenderBoxAndItsAncestors(direction, granularity, &renderBox, isKeyRepeat))
return true;
}
return false;
}
ScrollableArea* EventHandler::focusedScrollableArea() const
{
RefPtr<Node> node = m_frame->document()->focusedElement();
if (!node)
node = m_mousePressNode;
return enclosingScrollableArea(node.get());
}
bool EventHandler::shouldUseSmoothKeyboardScrollingForFocusedScrollableArea()
{
if (!m_frame->settings().eventHandlerDrivenSmoothKeyboardScrollingEnabled())
return false;
auto scrollableArea = focusedScrollableArea();
if (!scrollableArea)
return false;
if (scrollableArea->scrollAnimator().usesScrollSnap())
return false;
#if PLATFORM(GTK) || PLATFORM(WPE)
if (!m_frame->settings().asyncFrameScrollingEnabled())
return false;
#endif
if (!scrollableArea->scrollAnimatorEnabled())
return false;
return true;
}
bool EventHandler::keyboardScrollRecursively(std::optional<ScrollDirection> direction, std::optional<ScrollGranularity> granularity, Node* startingNode, bool isKeyRepeat)
{
if (!direction || !granularity)
return false;
Ref frame = m_frame.get();
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
if (startKeyboardScrollAnimationOnEnclosingScrollableContainer(*direction, *granularity, startingNode, isKeyRepeat))
return true;
if (startKeyboardScrollAnimationOnDocument(*direction, *granularity, isKeyRepeat))
return true;
frame = m_frame.get();
RefPtr parent = frame->tree().parent();
if (!parent)
return false;
RefPtr localParent = dynamicDowncast<LocalFrame>(parent.get());
if (!localParent)
return false;
return localParent->checkedEventHandler()->keyboardScrollRecursively(direction, granularity, frame->protectedOwnerElement().get(), isKeyRepeat);
}
bool EventHandler::keyboardScroll(std::optional<ScrollDirection> direction, std::optional<ScrollGranularity> granularity, Node* startingNode, bool isKeyRepeat)
{
if (!direction || !granularity)
return false;
Ref frame = m_frame.get();
frame->protectedDocument()->updateLayoutIgnorePendingStylesheets();
if (startKeyboardScrollAnimationOnEnclosingScrollableContainer(*direction, *granularity, startingNode, isKeyRepeat))
return true;
return startKeyboardScrollAnimationOnDocument(*direction, *granularity, isKeyRepeat);
}
void EventHandler::defaultArrowEventHandler(FocusDirection focusDirection, KeyboardEvent& event)
{
ASSERT(event.type() == eventNames().keydownEvent);
if (!isSpatialNavigationEnabled(protectedFrame().ptr())) {
ScrollLogicalDirection direction;
switch (focusDirection) {
case FocusDirection::Down:
direction = ScrollBlockDirectionForward;
break;
case FocusDirection::Right:
direction = ScrollInlineDirectionForward;
break;
case FocusDirection::Up:
direction = ScrollBlockDirectionBackward;
break;
case FocusDirection::Left:
direction = ScrollInlineDirectionBackward;
break;
case FocusDirection::None:
case FocusDirection::Backward:
case FocusDirection::Forward:
RELEASE_ASSERT_NOT_REACHED();
break;
}
if (defaultKeyboardScrollEventHandler(event, direction, ScrollGranularity::Line))
event.setDefaultHandled();
return;
}
if (event.ctrlKey() || event.metaKey() || event.shiftKey())
return;
RefPtr page = m_frame->page();
if (!page)
return;
// Arrows and other possible directional navigation keys can be used in design
// mode editing.
if (m_frame->document()->inDesignMode())
return;
if (page->checkedFocusController()->advanceFocus(focusDirection, &event))
event.setDefaultHandled();
}
void EventHandler::defaultTabEventHandler(KeyboardEvent& event)
{
Ref frame = m_frame.get();
ASSERT(event.type() == eventNames().keydownEvent);
// We should only advance focus on tabs if no special modifier keys are held down.
if (event.ctrlKey() || event.metaKey())
return;
RefPtr page = frame->page();
if (!page)
return;
// Tabs can be used in design mode editing.
if (frame->document()->inDesignMode())
return;
if (!page->tabKeyCyclesThroughElements())
return;
if (page->checkedFocusController()->advanceFocus(event.shiftKey() ? FocusDirection::Backward : FocusDirection::Forward, &event))
event.setDefaultHandled();
}
void EventHandler::scheduleScrollEvent()
{
Ref frame = m_frame.get();
setFrameWasScrolledByUser();
if (!frame->view())
return;
if (RefPtr document = frame->document())
document->addPendingScrollEventTarget(*document);
}
void EventHandler::setFrameWasScrolledByUser()
{
if (RefPtr view = m_frame->view())
view->setLastUserScrollType(LocalFrameView::UserScrollType::Explicit);
}
bool EventHandler::passMousePressEventToScrollbar(MouseEventWithHitTestResults& mouseEventAndResult, Scrollbar* scrollbar)
{
if (!scrollbar || !scrollbar->enabled())
return false;
setFrameWasScrolledByUser();
return scrollbar->mouseDown(mouseEventAndResult.event());
}
// If scrollbar (under mouse) is different from last, send a mouse exited.
void EventHandler::updateLastScrollbarUnderMouse(Scrollbar* scrollbar, SetOrClearLastScrollbar setOrClear)
{
if (m_lastScrollbarUnderMouse != scrollbar) {
// Send mouse exited to the old scrollbar.
if (m_lastScrollbarUnderMouse)
m_lastScrollbarUnderMouse->mouseExited();
// Send mouse entered if we're setting a new scrollbar.
if (scrollbar && setOrClear == SetOrClearLastScrollbar::Set) {
scrollbar->mouseEntered();
m_lastScrollbarUnderMouse = *scrollbar;
} else
m_lastScrollbarUnderMouse = nullptr;
}
}
#if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
static const AtomString& eventNameForTouchPointState(PlatformTouchPoint::State state)
{
switch (state) {
case PlatformTouchPoint::TouchReleased:
return eventNames().touchendEvent;
case PlatformTouchPoint::TouchCancelled:
return eventNames().touchcancelEvent;
case PlatformTouchPoint::TouchPressed:
return eventNames().touchstartEvent;
case PlatformTouchPoint::TouchMoved:
return eventNames().touchmoveEvent;
case PlatformTouchPoint::TouchStationary:
// TouchStationary state is not converted to touch events, so fall through to assert.
default:
ASSERT_NOT_REACHED();
return emptyAtom();
}
}
static HitTestResult hitTestResultInFrame(LocalFrame* frame, const LayoutPoint& point, OptionSet<HitTestRequest::Type> hitType)
{
HitTestResult result(point);
if (!frame || !frame->contentRenderer())
return result;
if (frame->view()) {
IntRect rect = frame->view()->visibleContentRect();
if (!rect.contains(roundedIntPoint(point)))
return result;
}
frame->protectedDocument()->hitTest(hitType, result);
return result;
}
HandleUserInputEventResult EventHandler::handleTouchEvent(const PlatformTouchEvent& event)
{
Ref frame = m_frame.get();
// First build up the lists to use for the 'touches', 'targetTouches' and 'changedTouches' attributes
// in the JS event. See https://www.sitepen.com/blog/touching-and-gesturing-on-the-iphone/
// for an overview of how these lists fit together.
// Holds the complete set of touches on the screen and will be used as the 'touches' list in the JS event.
RefPtr<TouchList> touches = TouchList::create();
// A different view on the 'touches' list above, filtered and grouped by event target. Used for the
// 'targetTouches' list in the JS event.
typedef UncheckedKeyHashMap<EventTarget*, RefPtr<TouchList>> TargetTouchesMap;
TargetTouchesMap touchesByTarget;
// Array of touches per state, used to assemble the 'changedTouches' list in the JS event.
typedef UncheckedKeyHashSet<RefPtr<EventTarget>> EventTargetSet;
struct Touches {
// The touches corresponding to the particular change state this struct instance represents.
RefPtr<TouchList> m_touches;
// Set of targets involved in m_touches.
EventTargetSet m_targets;
};
std::array<Touches, PlatformTouchPoint::TouchStateEnd> changedTouches;
const Vector<PlatformTouchPoint>& points = event.touchPoints();
UserGestureIndicator gestureIndicator(IsProcessingUserGesture::Yes, frame->protectedDocument().get(), userGestureTypeForPlatformEvent(event), UserGestureIndicator::ProcessInteractionStyle::Immediate, event.authorizationToken());
bool freshTouchEvents = true;
bool allTouchReleased = true;
for (auto& point : points) {
if (point.state() != PlatformTouchPoint::TouchPressed)
freshTouchEvents = false;
if (point.state() != PlatformTouchPoint::TouchReleased && point.state() != PlatformTouchPoint::TouchCancelled)
allTouchReleased = false;
}
for (unsigned index = 0; index < points.size(); index++) {
auto& point = points[index];
PlatformTouchPoint::State pointState = point.state();
LayoutPoint pagePoint = documentPointForWindowPoint(frame, point.pos());
OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::TouchEvent };
// The HitTestRequest types used for mouse events map quite adequately
// to touch events. Note that in addition to meaning that the hit test
// should affect the active state of the current node if necessary,
// HitTestRequest::Type::Active signifies that the hit test is taking place
// with the mouse (or finger in this case) being pressed.
switch (pointState) {
case PlatformTouchPoint::TouchPressed:
hitType.add(HitTestRequest::Type::Active);
break;
case PlatformTouchPoint::TouchMoved:
hitType.add({ HitTestRequest::Type::Active, HitTestRequest::Type::Move, HitTestRequest::Type::ReadOnly });
break;
case PlatformTouchPoint::TouchReleased:
case PlatformTouchPoint::TouchCancelled:
hitType.add(HitTestRequest::Type::Release);
break;
case PlatformTouchPoint::TouchStationary:
hitType.add({ HitTestRequest::Type::Active, HitTestRequest::Type::ReadOnly });
break;
default:
ASSERT_NOT_REACHED();
break;
}
if (shouldGesturesTriggerActive())
hitType.add(HitTestRequest::Type::ReadOnly);
// Increment the platform touch id by 1 to avoid storing a key of 0 in the hashmap.
unsigned touchPointTargetKey = point.id() + 1;
#if PLATFORM(WPE)
bool pointerCancelled = false;
#endif
RefPtr<EventTarget> touchTarget;
RefPtr<EventTarget> pointerTarget;
if (pointState == PlatformTouchPoint::TouchPressed) {
HitTestResult result;
if (freshTouchEvents) {
result = hitTestResultAtPoint(pagePoint, hitType | HitTestRequest::Type::AllowChildFrameContent);
m_originatingTouchPointTargetKey = touchPointTargetKey;
} else if (m_originatingTouchPointDocument && m_originatingTouchPointDocument->frame()) {
Ref frame = *m_originatingTouchPointDocument->frame();
LayoutPoint pagePointInOriginatingDocument = documentPointForWindowPoint(frame, point.pos());
result = hitTestResultInFrame(frame.ptr(), pagePointInOriginatingDocument, hitType);
if (!result.innerNode())
continue;
} else
continue;
RefPtr element = result.targetElement();
ASSERT(element);
if (element && InspectorInstrumentation::handleTouchEvent(frame, *element))
return true;
Ref doc = element->document();
// Record the originating touch document even if it does not have a touch listener.
if (freshTouchEvents) {
m_originatingTouchPointDocument = doc.ptr();
freshTouchEvents = false;
}
if (!doc->hasTouchEventHandlers())
continue;
m_originatingTouchPointTargets.set(touchPointTargetKey, element);
touchTarget = element;
pointerTarget = element;
} else if (pointState == PlatformTouchPoint::TouchReleased || pointState == PlatformTouchPoint::TouchCancelled) {
// No need to perform a hit-test since we only need to unset :hover and :active states.
if (!shouldGesturesTriggerActive() && allTouchReleased)
frame->protectedDocument()->updateHoverActiveState(hitType, 0);
if (touchPointTargetKey == m_originatingTouchPointTargetKey)
m_originatingTouchPointTargetKey = 0;
// The target should be the original target for this touch, so get it from the hashmap. As it's a release or cancel
// we also remove it from the map.
touchTarget = m_originatingTouchPointTargets.take(touchPointTargetKey);
#if PLATFORM(WPE)
HitTestResult result = hitTestResultAtPoint(pagePoint, hitType | HitTestRequest::Type::AllowChildFrameContent);
pointerTarget = result.targetElement();
pointerCancelled = (pointerTarget != touchTarget);
#endif
} else {
// No hittest is performed on move or stationary, since the target is not allowed to change anyway.
touchTarget = m_originatingTouchPointTargets.get(touchPointTargetKey);
HitTestResult result = hitTestResultAtPoint(pagePoint, hitType | HitTestRequest::Type::AllowChildFrameContent);
pointerTarget = result.targetElement();
}
RefPtr touchTargetNode = dynamicDowncast<Node>(touchTarget);
if (!touchTargetNode)
continue;
Ref document = touchTargetNode->document();
if (!document->hasTouchEventHandlers())
continue;
RefPtr targetFrame = document->frame();
if (!targetFrame)
continue;
#if PLATFORM(WPE)
// FIXME: WPE currently does not send touch stationary events, so create a naive TouchReleased PlatformTouchPoint
// on release if the hit test result changed since the previous TouchPressed or TouchMoved
if (pointState == PlatformTouchPoint::TouchReleased && pointerCancelled) {
PlatformTouchEvent cancelEvent = event;
Vector<PlatformTouchPoint> cancelEventPoints = event.touchPoints();
cancelEventPoints.at(index) = PlatformTouchPoint(
point.id(), PlatformTouchPoint::State::TouchCancelled, point.screenPos(), point.pos());
cancelEvent.setTouchPoints(cancelEventPoints);
document->protectedPage()->pointerCaptureController().dispatchEventForTouchAtIndex(
*touchTarget, cancelEvent, index, !index, *document->windowProxy(), { 0, 0 });
}
// FIXME: Pass the touch delta for pointermove events by remembering the position per pointerID similar to
// Apple's m_touchLastGlobalPositionAndDeltaMap
document->protectedPage()->pointerCaptureController().dispatchEventForTouchAtIndex(
*pointerTarget, event, index, !index, *document->windowProxy(), { 0, 0 });
#endif
if (frame.ptr() != targetFrame) {
// pagePoint should always be relative to the target elements containing frame.
pagePoint = documentPointForWindowPoint(*targetFrame, point.pos());
}
float scaleFactor = targetFrame->pageZoomFactor() * targetFrame->frameScaleFactor();
int adjustedPageX = lroundf(pagePoint.x() / scaleFactor);
int adjustedPageY = lroundf(pagePoint.y() / scaleFactor);
auto touch = Touch::create(targetFrame.get(), touchTarget.get(), point.id(),
point.screenPos().x(), point.screenPos().y(), adjustedPageX, adjustedPageY,
point.radiusX(), point.radiusY(), point.rotationAngle(), point.force());
// Ensure this target's touch list exists, even if it ends up empty, so it can always be passed to TouchEvent::Create below.
TargetTouchesMap::iterator targetTouchesIterator = touchesByTarget.find(touchTarget.get());
if (targetTouchesIterator == touchesByTarget.end())
targetTouchesIterator = touchesByTarget.set(touchTarget.get(), TouchList::create()).iterator;
// touches and targetTouches should only contain information about touches still on the screen, so if this point is
// released or cancelled it will only appear in the changedTouches list.
if (pointState != PlatformTouchPoint::TouchReleased && pointState != PlatformTouchPoint::TouchCancelled) {
touches->append(touch.copyRef());
targetTouchesIterator->value->append(touch.copyRef());
}
// Now build up the correct list for changedTouches.
// Note that any touches that are in the TouchStationary state (e.g. if
// the user had several points touched but did not move them all) should
// never be in the changedTouches list so we do not handle them explicitly here.
// See https://bugs.webkit.org/show_bug.cgi?id=37609 for further discussion
// about the TouchStationary state.
if (pointState != PlatformTouchPoint::TouchStationary) {
ASSERT(pointState < PlatformTouchPoint::TouchStateEnd);
if (!changedTouches[pointState].m_touches)
changedTouches[pointState].m_touches = TouchList::create();
changedTouches[pointState].m_touches->append(WTFMove(touch));
changedTouches[pointState].m_targets.add(touchTarget);
}
}
m_touchPressed = touches->length() > 0;
if (allTouchReleased)
m_originatingTouchPointDocument = nullptr;
// Now iterate the changedTouches list and m_targets within it, sending events to the targets as required.
bool swallowedEvent = false;
RefPtr<TouchList> emptyList = TouchList::create();
for (unsigned state = 0; state != PlatformTouchPoint::TouchStateEnd; ++state) {
if (!changedTouches[state].m_touches)
continue;
// When sending a touch cancel event, use empty touches and targetTouches lists.
bool isTouchCancelEvent = (state == PlatformTouchPoint::TouchCancelled);
RefPtr<TouchList>& effectiveTouches(isTouchCancelEvent ? emptyList : touches);
const AtomString& stateName(eventNameForTouchPointState(static_cast<PlatformTouchPoint::State>(state)));
for (auto& target : changedTouches[state].m_targets) {
ASSERT(is<Node>(target));
RefPtr<TouchList> targetTouches(isTouchCancelEvent ? emptyList : touchesByTarget.get(target.get()));
ASSERT(targetTouches);
Ref<TouchEvent> touchEvent = TouchEvent::create(effectiveTouches.get(), targetTouches.get(), changedTouches[state].m_touches.get(),
stateName, downcast<Node>(*target).document().windowProxy(), { }, event.modifiers());
target->dispatchEvent(touchEvent);
swallowedEvent = swallowedEvent || touchEvent->defaultPrevented() || touchEvent->defaultHandled();
}
}
return swallowedEvent;
}
#endif // ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
#if ENABLE(TOUCH_EVENTS)
HandleUserInputEventResult EventHandler::dispatchSyntheticTouchEventIfEnabled(const PlatformMouseEvent& platformMouseEvent)
{
#if ENABLE(IOS_TOUCH_EVENTS)
UNUSED_PARAM(platformMouseEvent);
return false;
#else
if (!m_frame->settings().isTouchEventEmulationEnabled())
return false;
PlatformEvent::Type eventType = platformMouseEvent.type();
if (eventType != PlatformEvent::Type::MouseMoved && eventType != PlatformEvent::Type::MousePressed && eventType != PlatformEvent::Type::MouseReleased)
return false;
constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::Active, HitTestRequest::Type::DisallowUserAgentShadowContent };
MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(hitType, platformMouseEvent);
if (mouseEvent.scrollbar() || subframeForHitTestResult(mouseEvent))
return false;
// The order is important. This check should follow the subframe test: http://webkit.org/b/111292.
if (eventType == PlatformEvent::Type::MouseMoved && !m_touchPressed)
return true;
SyntheticSingleTouchEvent touchEvent(platformMouseEvent);
return handleTouchEvent(touchEvent);
#endif
}
#endif // ENABLE(TOUCH_EVENTS)
void EventHandler::setLastKnownMousePosition(IntPoint position, IntPoint globalPosition)
{
m_lastKnownMousePosition = position;
m_lastKnownMouseGlobalPosition = globalPosition;
}
void EventHandler::setImmediateActionStage(ImmediateActionStage stage)
{
m_immediateActionStage = stage;
}
#if !PLATFORM(COCOA)
OptionSet<PlatformEvent::Modifier> EventHandler::accessKeyModifiers()
{
return PlatformEvent::Modifier::AltKey;
}
HandleUserInputEventResult EventHandler::passMousePressEventToSubframe(MouseEventWithHitTestResults& mouseEventAndResult, LocalFrame& subframe)
{
subframe.eventHandler().handleMousePressEvent(mouseEventAndResult.event());
return true;
}
HandleUserInputEventResult EventHandler::passMouseReleaseEventToSubframe(MouseEventWithHitTestResults& mouseEventAndResult, LocalFrame& subframe)
{
subframe.eventHandler().handleMouseReleaseEvent(mouseEventAndResult.event());
return true;
}
bool EventHandler::passWheelEventToWidget(const PlatformWheelEvent& event, Widget& widget, OptionSet<WheelEventProcessingSteps> processingSteps)
{
RefPtr frameView = dynamicDowncast<LocalFrameView>(widget);
if (!frameView)
return false;
auto [result, _] = frameView->frame().eventHandler().handleWheelEvent(event, processingSteps);
return result.wasHandled();
}
bool EventHandler::passWidgetMouseDownEventToWidget(RenderWidget* renderWidget)
{
return passMouseDownEventToWidget(renderWidget->widget());
}
bool EventHandler::passWidgetMouseDownEventToWidget(const MouseEventWithHitTestResults& event)
{
auto* target = event.targetNode() ? event.targetNode()->renderer() : nullptr;
auto* renderWidget = dynamicDowncast<RenderWidget>(target);
if (!renderWidget)
return false;
return passMouseDownEventToWidget(renderWidget->widget());
}
bool EventHandler::passMouseDownEventToWidget(Widget*)
{
notImplemented();
return false;
}
void EventHandler::focusDocumentView()
{
if (RefPtr page = m_frame->page())
page->checkedFocusController()->setFocusedFrame(protectedFrame().ptr());
}
#endif // !PLATFORM(COCOA)
Ref<LocalFrame> EventHandler::protectedFrame() const
{
return m_frame.get();
}
#if !PLATFORM(COCOA) && !PLATFORM(WIN)
bool EventHandler::eventActivatedView(const PlatformMouseEvent&) const
{
notImplemented();
return false;
}
HandleUserInputEventResult EventHandler::passMouseMoveEventToSubframe(MouseEventWithHitTestResults& mouseEventAndResult, LocalFrame& subframe, HitTestResult* result)
{
subframe.eventHandler().handleMouseMoveEvent(mouseEventAndResult.event(), result);
return true;
}
#endif // !PLATFORM(COCOA) && !PLATFORM(WIN)
} // namespace WebCore
|