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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview A collection of JavaScript utilities used to simplify working
* with the DOM.
*/
goog.provide('cvox.DomUtil');
goog.require('cvox.AbstractTts');
goog.require('cvox.AriaUtil');
goog.require('cvox.ChromeVox');
goog.require('cvox.DomPredicates');
goog.require('cvox.Memoize');
goog.require('cvox.NodeState');
goog.require('cvox.XpathUtil');
/**
* Create the namespace
* @constructor
*/
cvox.DomUtil = function() {
};
/**
* Note: If you are adding a new mapping, the new message identifier needs a
* corresponding braille message. For example, a message id 'tag_button'
* requires another message 'tag_button_brl' within messages.js.
* @type {Object}
*/
cvox.DomUtil.INPUT_TYPE_TO_INFORMATION_TABLE_MSG = {
'button' : 'input_type_button',
'checkbox' : 'input_type_checkbox',
'color' : 'input_type_color',
'datetime' : 'input_type_datetime',
'datetime-local' : 'input_type_datetime_local',
'date' : 'input_type_date',
'email' : 'input_type_email',
'file' : 'input_type_file',
'image' : 'input_type_image',
'month' : 'input_type_month',
'number' : 'input_type_number',
'password' : 'input_type_password',
'radio' : 'input_type_radio',
'range' : 'input_type_range',
'reset' : 'input_type_reset',
'search' : 'input_type_search',
'submit' : 'input_type_submit',
'tel' : 'input_type_tel',
'text' : 'input_type_text',
'url' : 'input_type_url',
'week' : 'input_type_week'
};
/**
* Note: If you are adding a new mapping, the new message identifier needs a
* corresponding braille message. For example, a message id 'tag_button'
* requires another message 'tag_button_brl' within messages.js.
* @type {Object}
*/
cvox.DomUtil.TAG_TO_INFORMATION_TABLE_VERBOSE_MSG = {
'A' : 'tag_link',
'ARTICLE' : 'tag_article',
'ASIDE' : 'tag_aside',
'AUDIO' : 'tag_audio',
'BUTTON' : 'tag_button',
'FOOTER' : 'tag_footer',
'H1' : 'tag_h1',
'H2' : 'tag_h2',
'H3' : 'tag_h3',
'H4' : 'tag_h4',
'H5' : 'tag_h5',
'H6' : 'tag_h6',
'HEADER' : 'tag_header',
'HGROUP' : 'tag_hgroup',
'LI' : 'tag_li',
'MARK' : 'tag_mark',
'NAV' : 'tag_nav',
'OL' : 'tag_ol',
'SECTION' : 'tag_section',
'SELECT' : 'tag_select',
'TABLE' : 'tag_table',
'TEXTAREA' : 'tag_textarea',
'TIME' : 'tag_time',
'UL' : 'tag_ul',
'VIDEO' : 'tag_video'
};
/**
* ChromeVox does not speak the omitted tags.
* @type {Object}
*/
cvox.DomUtil.TAG_TO_INFORMATION_TABLE_BRIEF_MSG = {
'AUDIO' : 'tag_audio',
'BUTTON' : 'tag_button',
'SELECT' : 'tag_select',
'TABLE' : 'tag_table',
'TEXTAREA' : 'tag_textarea',
'VIDEO' : 'tag_video'
};
/**
* These tags are treated as text formatters.
* @type {Array.<string>}
*/
cvox.DomUtil.FORMATTING_TAGS =
['B', 'BIG', 'CITE', 'CODE', 'DFN', 'EM', 'I', 'KBD', 'SAMP', 'SMALL',
'SPAN', 'STRIKE', 'STRONG', 'SUB', 'SUP', 'U', 'VAR'];
/**
* Determine if the given node is visible on the page. This does not check if
* it is inside the document view-port as some sites try to communicate with
* screen readers with such elements.
* @param {Node} node The node to determine as visible or not.
* @param {{checkAncestors: (boolean|undefined),
checkDescendants: (boolean|undefined)}=} opt_options
* In certain cases, we already have information
* on the context of the node. To improve performance and avoid redundant
* operations, you may wish to turn certain visibility checks off by
* passing in an options object. The following properties are configurable:
* checkAncestors: {boolean=} True if we should check the ancestor chain
* for forced invisibility traits of descendants. True by default.
* checkDescendants: {boolean=} True if we should consider descendants of
* the given node for visible elements. True by default.
* @return {boolean} True if the node is visible.
*/
cvox.DomUtil.isVisible = function(node, opt_options) {
var checkAncestors = true;
var checkDescendants = true;
if (opt_options) {
if (opt_options.checkAncestors !== undefined) {
checkAncestors = opt_options.checkAncestors;
}
if (opt_options.checkDescendants !== undefined) {
checkDescendants = opt_options.checkDescendants;
}
}
// Generate a unique function name based on the arguments, and
// memoize the result of the internal visibility computation so that
// within the same call stack, we don't need to recompute the visibility
// of the same node.
var fname = 'isVisible-' + checkAncestors + '-' + checkDescendants;
return /** @type {boolean} */ (cvox.Memoize.memoize(
cvox.DomUtil.computeIsVisible_.bind(
this, node, checkAncestors, checkDescendants), fname, node));
};
/**
* Implementation of |cvox.DomUtil.isVisible|.
* @param {Node} node The node to determine as visible or not.
* @param {boolean} checkAncestors True if we should check the ancestor chain
* for forced invisibility traits of descendants.
* @param {boolean} checkDescendants True if we should consider descendants of
* the given node for visible elements.
* @return {boolean} True if the node is visible.
* @private
*/
cvox.DomUtil.computeIsVisible_ = function(
node, checkAncestors, checkDescendants) {
// If the node is an iframe that we can never inject into, consider it hidden.
if (node.tagName == 'IFRAME' && !node.src) {
return false;
}
// If the node is being forced visible by ARIA, ARIA wins.
if (cvox.AriaUtil.isForcedVisibleRecursive(node)) {
return true;
}
// Confirm that no subtree containing node is invisible.
if (checkAncestors &&
cvox.DomUtil.hasInvisibleAncestor_(node)) {
return false;
}
// If the node's subtree has a visible node, we declare it as visible.
if (cvox.DomUtil.hasVisibleNodeSubtree_(node, checkDescendants)) {
return true;
}
return false;
};
/**
* Checks the ancestor chain for the given node for invisibility. If an
* ancestor is invisible and this cannot be overriden by a descendant,
* we return true. If the element is not a descendant of the document
* element it will return true (invisible).
* @param {Node} node The node to check the ancestor chain for.
* @return {boolean} True if a descendant is invisible.
* @private
*/
cvox.DomUtil.hasInvisibleAncestor_ = function(node) {
var ancestor = node;
while (ancestor = ancestor.parentElement) {
var style = document.defaultView.getComputedStyle(ancestor, null);
if (cvox.DomUtil.isInvisibleStyle(style, true)) {
return true;
}
// Once we reach the document element and we haven't found anything
// invisible yet, we're done. If we exit the while loop and never found
// the document element, the element wasn't part of the DOM and thus it's
// invisible.
if (ancestor == document.documentElement) {
return false;
}
}
return true;
};
/**
* Checks for a visible node in the subtree defined by root.
* @param {Node} root The root of the subtree to check.
* @param {boolean} recursive Whether or not to check beyond the root of the
* subtree for visible nodes. This option exists for performance tuning.
* Sometimes we already have information about the descendants, and we do
* not need to check them again.
* @return {boolean} True if the subtree contains a visible node.
* @private
*/
cvox.DomUtil.hasVisibleNodeSubtree_ = function(root, recursive) {
if (!(root instanceof Element)) {
var parentStyle = document.defaultView
.getComputedStyle(root.parentElement, null);
var isVisibleParent = !cvox.DomUtil.isInvisibleStyle(parentStyle);
return isVisibleParent;
}
var rootStyle = document.defaultView.getComputedStyle(root, null);
var isRootVisible = !cvox.DomUtil.isInvisibleStyle(rootStyle);
if (isRootVisible) {
return true;
}
var isSubtreeInvisible = cvox.DomUtil.isInvisibleStyle(rootStyle, true);
if (!recursive || isSubtreeInvisible) {
return false;
}
// Carry on with a recursive check of the descendants.
var children = root.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (cvox.DomUtil.hasVisibleNodeSubtree_(child, recursive)) {
return true;
}
}
return false;
};
/**
* Determines whether or a node is not visible according to any CSS criteria
* that can hide it.
* @param {CSSStyleDeclaration} style The style of the node to determine as
* invsible or not.
* @param {boolean=} opt_strict If set to true, we do not check the visibility
* style attribute. False by default.
* CAUTION: Checking the visibility style attribute can result in returning
* true (invisible) even when an element has have visible descendants. This
* is because an element with visibility:hidden can have descendants that
* are visible.
* @return {boolean} True if the node is invisible.
*/
cvox.DomUtil.isInvisibleStyle = function(style, opt_strict) {
if (!style) {
return false;
}
if (style.display == 'none') {
return true;
}
// Opacity values range from 0.0 (transparent) to 1.0 (fully opaque).
if (parseFloat(style.opacity) == 0) {
return true;
}
// Visibility style tests for non-strict checking.
if (!opt_strict &&
(style.visibility == 'hidden' || style.visibility == 'collapse')) {
return true;
}
return false;
};
/**
* Determines whether a control should be announced as disabled.
*
* @param {Node} node The node to be examined.
* @return {boolean} Whether or not the node is disabled.
*/
cvox.DomUtil.isDisabled = function(node) {
if (node.disabled) {
return true;
}
var ancestor = node;
while (ancestor = ancestor.parentElement) {
if (ancestor.tagName == 'FIELDSET' && ancestor.disabled) {
return true;
}
}
return false;
};
/**
* Determines whether a node is an HTML5 semantic element
*
* @param {Node} node The node to be checked.
* @return {boolean} True if the node is an HTML5 semantic element.
*/
cvox.DomUtil.isSemanticElt = function(node) {
if (node.tagName) {
var tag = node.tagName;
if ((tag == 'SECTION') || (tag == 'NAV') || (tag == 'ARTICLE') ||
(tag == 'ASIDE') || (tag == 'HGROUP') || (tag == 'HEADER') ||
(tag == 'FOOTER') || (tag == 'TIME') || (tag == 'MARK')) {
return true;
}
}
return false;
};
/**
* Determines whether or not a node is a leaf node.
* TODO (adu): This function is doing a lot more than just checking for the
* presence of descendants. We should be more precise in the documentation
* about what we mean by leaf node.
*
* @param {Node} node The node to be checked.
* @param {boolean=} opt_allowHidden Allows hidden nodes during descent.
* @return {boolean} True if the node is a leaf node.
*/
cvox.DomUtil.isLeafNode = function(node, opt_allowHidden) {
// If it's not an Element, then it's a leaf if it has no first child.
if (!(node instanceof Element)) {
return (node.firstChild == null);
}
// Now we know for sure it's an element.
var element = /** @type {Element} */(node);
if (!opt_allowHidden &&
!cvox.DomUtil.isVisible(element, {checkAncestors: false})) {
return true;
}
if (!opt_allowHidden && cvox.AriaUtil.isHidden(element)) {
return true;
}
if (cvox.AriaUtil.isLeafElement(element)) {
return true;
}
switch (element.tagName) {
case 'OBJECT':
case 'EMBED':
case 'VIDEO':
case 'AUDIO':
case 'IFRAME':
case 'FRAME':
return true;
}
if (!!cvox.DomPredicates.linkPredicate([element])) {
return !cvox.DomUtil.findNode(element, function(node) {
return !!cvox.DomPredicates.headingPredicate([node]);
});
}
if (cvox.DomUtil.isLeafLevelControl(element)) {
return true;
}
if (!element.firstChild) {
return true;
}
if (cvox.DomUtil.isMath(element)) {
return true;
}
if (cvox.DomPredicates.headingPredicate([element])) {
return !cvox.DomUtil.findNode(element, function(n) {
return !!cvox.DomPredicates.controlPredicate([n]);
});
}
return false;
};
/**
* Determines whether or not a node is or is the descendant of a node
* with a particular tag or class name.
*
* @param {Node} node The node to be checked.
* @param {?string} tagName The tag to check for, or null if the tag
* doesn't matter.
* @param {?string=} className The class to check for, or null if the class
* doesn't matter.
* @return {boolean} True if the node or one of its ancestor has the specified
* tag.
*/
cvox.DomUtil.isDescendantOf = function(node, tagName, className) {
while (node) {
if (tagName && className &&
(node.tagName && (node.tagName == tagName)) &&
(node.className && (node.className == className))) {
return true;
} else if (tagName && !className &&
(node.tagName && (node.tagName == tagName))) {
return true;
} else if (!tagName && className &&
(node.className && (node.className == className))) {
return true;
}
node = node.parentNode;
}
return false;
};
/**
* Determines whether or not a node is or is the descendant of another node.
*
* @param {Object} node The node to be checked.
* @param {Object} ancestor The node to see if it's a descendant of.
* @return {boolean} True if the node is ancestor or is a descendant of it.
*/
cvox.DomUtil.isDescendantOfNode = function(node, ancestor) {
while (node && ancestor) {
if (node.isSameNode(ancestor)) {
return true;
}
node = node.parentNode;
}
return false;
};
/**
* Remove all whitespace from the beginning and end, and collapse all
* inner strings of whitespace to a single space.
* @param {string} str The input string.
* @return {string} The string with whitespace collapsed.
*/
cvox.DomUtil.collapseWhitespace = function(str) {
return str.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '');
};
/**
* Gets the base label of a node. I don't know exactly what this is.
*
* @param {Node} node The node to get the label from.
* @param {boolean=} recursive Whether or not the element's subtree
* should be used; true by default.
* @param {boolean=} includeControls Whether or not controls in the subtree
* should be included; true by default.
* @return {string} The base label of the node.
* @private
*/
cvox.DomUtil.getBaseLabel_ = function(node, recursive, includeControls) {
var label = '';
if (node.hasAttribute) {
if (node.hasAttribute('aria-labelledby')) {
var labelNodeIds = node.getAttribute('aria-labelledby').split(' ');
for (var labelNodeId, i = 0; labelNodeId = labelNodeIds[i]; i++) {
var labelNode = document.getElementById(labelNodeId);
if (labelNode) {
label += ' ' + cvox.DomUtil.getName(
labelNode, true, includeControls, true);
}
}
} else if (node.hasAttribute('aria-label')) {
label = node.getAttribute('aria-label');
} else if (node.constructor == HTMLImageElement) {
label = cvox.DomUtil.getImageTitle(node);
} else if (node.tagName == 'FIELDSET') {
// Other labels will trump fieldset legend with this implementation.
// Depending on how this works out on the web, we may later switch this
// to appending the fieldset legend to any existing label.
var legends = node.getElementsByTagName('LEGEND');
label = '';
for (var legend, i = 0; legend = legends[i]; i++) {
label += ' ' + cvox.DomUtil.getName(legend, true, includeControls);
}
}
if (label.length == 0 && node && node.id) {
var labelFor = document.querySelector('label[for="' + node.id + '"]');
if (labelFor) {
label = cvox.DomUtil.getName(labelFor, recursive, includeControls);
}
}
}
return cvox.DomUtil.collapseWhitespace(label);
};
/**
* Gets the nearest label in the ancestor chain, if one exists.
* @param {Node} node The node to start from.
* @return {string} The label.
* @private
*/
cvox.DomUtil.getNearestAncestorLabel_ = function(node) {
var label = '';
var enclosingLabel = node;
while (enclosingLabel && enclosingLabel.tagName != 'LABEL') {
enclosingLabel = enclosingLabel.parentElement;
}
if (enclosingLabel && !enclosingLabel.hasAttribute('for')) {
// Get all text from the label but don't include any controls.
label = cvox.DomUtil.getName(enclosingLabel, true, false);
}
return label;
};
/**
* Gets the name for an input element.
* @param {Node} node The node.
* @return {string} The name.
* @private
*/
cvox.DomUtil.getInputName_ = function(node) {
var label = '';
if (node.type == 'image') {
label = cvox.DomUtil.getImageTitle(node);
} else if (node.type == 'submit') {
if (node.hasAttribute('value')) {
label = node.getAttribute('value');
} else {
label = 'Submit';
}
} else if (node.type == 'reset') {
if (node.hasAttribute('value')) {
label = node.getAttribute('value');
} else {
label = 'Reset';
}
} else if (node.type == 'button') {
if (node.hasAttribute('value')) {
label = node.getAttribute('value');
}
}
return label;
};
/**
* Wraps getName_ with marking and unmarking nodes so that infinite loops
* don't occur. This is the ugly way to solve this; getName should not ever
* do a recursive call somewhere above it in the tree.
* @param {Node} node See getName_.
* @param {boolean=} recursive See getName_.
* @param {boolean=} includeControls See getName_.
* @param {boolean=} opt_allowHidden Allows hidden nodes in name computation.
* @return {string} See getName_.
*/
cvox.DomUtil.getName = function(
node, recursive, includeControls, opt_allowHidden) {
if (!node || node.cvoxGetNameMarked == true) {
return '';
}
node.cvoxGetNameMarked = true;
var ret =
cvox.DomUtil.getName_(node, recursive, includeControls, opt_allowHidden);
node.cvoxGetNameMarked = false;
var prefix = cvox.DomUtil.getPrefixText(node);
return prefix + ret;
};
// TODO(dtseng): Seems like this list should be longer...
/**
* Determines if a node has a name obtained from concatinating the names of its
* children.
* @param {!Node} node The node under consideration.
* @param {boolean=} opt_allowHidden Allows hidden nodes in name computation.
* @return {boolean} True if node has name based on children.
* @private
*/
cvox.DomUtil.hasChildrenBasedName_ = function(node, opt_allowHidden) {
if (!!cvox.DomPredicates.linkPredicate([node]) ||
!!cvox.DomPredicates.headingPredicate([node]) ||
node.tagName == 'BUTTON' ||
cvox.AriaUtil.isControlWidget(node) ||
!cvox.DomUtil.isLeafNode(node, opt_allowHidden)) {
return true;
} else {
return false;
}
};
/**
* Get the name of a node: this includes all static text content and any
* HTML-author-specified label, title, alt text, aria-label, etc. - but
* does not include:
* - the user-generated control value (use getValue)
* - the current state (use getState)
* - the role (use getRole)
*
* Order of precedence:
* Text content if it's a text node.
* aria-labelledby
* aria-label
* alt (for an image)
* title
* label (for a control)
* placeholder (for an input element)
* recursive calls to getName on all children
*
* @param {Node} node The node to get the name from.
* @param {boolean=} recursive Whether or not the element's subtree should
* be used; true by default.
* @param {boolean=} includeControls Whether or not controls in the subtree
* should be included; true by default.
* @param {boolean=} opt_allowHidden Allows hidden nodes in name computation.
* @return {string} The name of the node.
* @private
*/
cvox.DomUtil.getName_ = function(
node, recursive, includeControls, opt_allowHidden) {
if (typeof(recursive) === 'undefined') {
recursive = true;
}
if (typeof(includeControls) === 'undefined') {
includeControls = true;
}
if (node.constructor == Text) {
return node.data;
}
var label = cvox.DomUtil.getBaseLabel_(node, recursive, includeControls);
if (label.length == 0 && cvox.DomUtil.isControl(node)) {
label = cvox.DomUtil.getNearestAncestorLabel_(node);
}
if (label.length == 0 && node.constructor == HTMLInputElement) {
label = cvox.DomUtil.getInputName_(node);
}
if (cvox.DomUtil.isInputTypeText(node) && node.hasAttribute('placeholder')) {
var placeholder = node.getAttribute('placeholder');
if (label.length > 0) {
if (cvox.DomUtil.getValue(node).length > 0) {
return label;
} else {
return label + ' with hint ' + placeholder;
}
} else {
return placeholder;
}
}
if (label.length > 0) {
return label;
}
// Fall back to naming via title only if there is no text content.
if (cvox.DomUtil.collapseWhitespace(node.textContent).length == 0 &&
node.hasAttribute &&
node.hasAttribute('title')) {
return node.getAttribute('title');
}
if (!recursive) {
return '';
}
if (cvox.AriaUtil.isCompositeControl(node)) {
return '';
}
if (cvox.DomUtil.hasChildrenBasedName_(node, opt_allowHidden)) {
return cvox.DomUtil.getNameFromChildren(
node, includeControls, opt_allowHidden);
}
return '';
};
/**
* Get the name from the children of a node, not including the node itself.
*
* @param {Node} node The node to get the name from.
* @param {boolean=} includeControls Whether or not controls in the subtree
* should be included; true by default.
* @param {boolean=} opt_allowHidden Allow hidden nodes in name computation.
* @return {string} The concatenated text of all child nodes.
*/
cvox.DomUtil.getNameFromChildren = function(
node, includeControls, opt_allowHidden) {
if (includeControls == undefined) {
includeControls = true;
}
var name = '';
var delimiter = '';
for (var i = 0; i < node.childNodes.length; i++) {
var child = node.childNodes[i];
var prevChild = node.childNodes[i - 1] || child;
if (!includeControls && cvox.DomUtil.isControl(child)) {
continue;
}
var isVisible = cvox.DomUtil.isVisible(child, {checkAncestors: false});
if (opt_allowHidden || (isVisible && !cvox.AriaUtil.isHidden(child))) {
delimiter = (prevChild.tagName == 'SPAN' ||
child.tagName == 'SPAN' ||
child.parentNode.tagName == 'SPAN') ?
'' : ' ';
name += delimiter + cvox.DomUtil.getName(child, true, includeControls);
}
}
return name;
};
/**
* Get any prefix text for the given node.
* This includes list style text for the leftmost leaf node under a listitem.
* @param {Node} node Compute prefix for this node.
* @param {number=} opt_index Starting offset into the given node's text.
* @return {string} Prefix text, if any.
*/
cvox.DomUtil.getPrefixText = function(node, opt_index) {
opt_index = opt_index || 0;
// Generate list style text.
var ancestors = cvox.DomUtil.getAncestors(node);
var prefix = '';
var firstListitem = cvox.DomPredicates.listItemPredicate(ancestors);
var leftmost = firstListitem;
while (leftmost && leftmost.firstChild) {
leftmost = leftmost.firstChild;
}
// Do nothing if we're not at the leftmost leaf.
if (firstListitem &&
firstListitem.parentNode &&
opt_index == 0 &&
firstListitem.parentNode.tagName == 'OL' &&
node == leftmost &&
document.defaultView.getComputedStyle(firstListitem.parentNode)
.listStyleType != 'none') {
var items = cvox.DomUtil.toArray(firstListitem.parentNode.children).filter(
function(li) { return li.tagName == 'LI'; });
var position = items.indexOf(firstListitem) + 1;
// TODO(dtseng): Support all list style types.
if (document.defaultView.getComputedStyle(
firstListitem.parentNode).listStyleType.indexOf('latin') != -1) {
position--;
prefix = String.fromCharCode('A'.charCodeAt(0) + position % 26);
} else {
prefix = position;
}
prefix += '. ';
}
return prefix;
};
/**
* Use heuristics to guess at the label of a control, to be used if one
* is not explicitly set in the DOM. This is useful when a control
* field gets focus, but probably not useful when browsing the page
* element at a time.
* @param {Node} node The node to get the label from.
* @return {string} The name of the control, using heuristics.
*/
cvox.DomUtil.getControlLabelHeuristics = function(node) {
// If the node explicitly has aria-label or title set to '',
// treat it the same way as alt='' and do not guess - just assume
// the web developer knew what they were doing and wanted
// no title/label for that control.
if (node.hasAttribute &&
((node.hasAttribute('aria-label') &&
(node.getAttribute('aria-label') == '')) ||
(node.hasAttribute('aria-title') &&
(node.getAttribute('aria-title') == '')))) {
return '';
}
// TODO (clchen, rshearer): Implement heuristics for getting the label
// information from the table headers once the code for getting table
// headers quickly is implemented.
// If no description has been found yet and heuristics are enabled,
// then try getting the content from the closest node.
var prevNode = cvox.DomUtil.previousLeafNode(node);
var prevTraversalCount = 0;
while (prevNode && (!cvox.DomUtil.hasContent(prevNode) ||
cvox.DomUtil.isControl(prevNode))) {
prevNode = cvox.DomUtil.previousLeafNode(prevNode);
prevTraversalCount++;
}
var nextNode = cvox.DomUtil.directedNextLeafNode(node);
var nextTraversalCount = 0;
while (nextNode && (!cvox.DomUtil.hasContent(nextNode) ||
cvox.DomUtil.isControl(nextNode))) {
nextNode = cvox.DomUtil.directedNextLeafNode(nextNode);
nextTraversalCount++;
}
var guessedLabelNode;
if (prevNode && nextNode) {
var parentNode = node;
// Count the number of parent nodes until there is a shared parent; the
// label is most likely in the same branch of the DOM as the control.
// TODO (chaitanyag): Try to generalize this algorithm and move it to
// its own function in DOM Utils.
var prevCount = 0;
while (parentNode) {
if (cvox.DomUtil.isDescendantOfNode(prevNode, parentNode)) {
break;
}
parentNode = parentNode.parentNode;
prevCount++;
}
parentNode = node;
var nextCount = 0;
while (parentNode) {
if (cvox.DomUtil.isDescendantOfNode(nextNode, parentNode)) {
break;
}
parentNode = parentNode.parentNode;
nextCount++;
}
guessedLabelNode = nextCount < prevCount ? nextNode : prevNode;
} else {
guessedLabelNode = prevNode || nextNode;
}
if (guessedLabelNode) {
return cvox.DomUtil.collapseWhitespace(
cvox.DomUtil.getValue(guessedLabelNode) + ' ' +
cvox.DomUtil.getName(guessedLabelNode));
}
return '';
};
/**
* Get the text value of a node: the selected value of a select control or the
* current text of a text control. Does not return the state of a checkbox
* or radio button.
*
* Not recursive.
*
* @param {Node} node The node to get the value from.
* @return {string} The value of the node.
*/
cvox.DomUtil.getValue = function(node) {
var activeDescendant = cvox.AriaUtil.getActiveDescendant(node);
if (activeDescendant) {
return cvox.DomUtil.collapseWhitespace(
cvox.DomUtil.getValue(activeDescendant) + ' ' +
cvox.DomUtil.getName(activeDescendant));
}
if (node.constructor == HTMLSelectElement) {
node = /** @type {HTMLSelectElement} */(node);
var value = '';
var start = node.selectedOptions ? node.selectedOptions[0] : null;
var end = node.selectedOptions ?
node.selectedOptions[node.selectedOptions.length - 1] : null;
// TODO(dtseng): Keeping this stateless means we describe the start and end
// of the selection only since we don't know which was added or
// removed. Once we keep the previous selection, we can read the diff.
if (start && end && start != end) {
value = cvox.ChromeVox.msgs.getMsg(
'selected_options_value', [start.text, end.text]);
} else if (start) {
value = start.text + '';
}
return value;
}
if (node.constructor == HTMLTextAreaElement) {
return node.value;
}
if (node.constructor == HTMLInputElement) {
switch (node.type) {
// Returning '' for inputs that are covered by getName.
case 'hidden':
case 'image':
case 'submit':
case 'reset':
case 'button':
case 'checkbox':
case 'radio':
return '';
case 'password':
return node.value.replace(/./g, 'dot ');
default:
return node.value;
}
}
if (node.isContentEditable) {
return cvox.DomUtil.getNameFromChildren(node, true);
}
return '';
};
/**
* Given an image node, return its title as a string. The preferred title
* is always the alt text, and if that's not available, then the title
* attribute. If neither of those are available, it attempts to construct
* a title from the filename, and if all else fails returns the word Image.
* @param {Node} node The image node.
* @return {string} The title of the image.
*/
cvox.DomUtil.getImageTitle = function(node) {
var text;
if (node.hasAttribute('alt')) {
text = node.alt;
} else if (node.hasAttribute('title')) {
text = node.title;
} else {
var url = node.src;
if (url.substring(0, 4) != 'data') {
var filename = url.substring(
url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
// Hack to not speak the filename if it's ridiculously long.
if (filename.length >= 1 && filename.length <= 16) {
text = filename + ' Image';
} else {
text = 'Image';
}
} else {
text = 'Image';
}
}
return text;
};
/**
* Search the whole page for any aria-labelledby attributes and collect
* the complete set of ids they map to, so that we can skip elements that
* just label other elements and not double-speak them. We cache this
* result and then throw it away at the next event loop.
* @return {Object.<string, boolean>} Set of all ids that are mapped
* by aria-labelledby.
*/
cvox.DomUtil.getLabelledByTargets = function() {
if (cvox.labelledByTargets) {
return cvox.labelledByTargets;
}
// Start by getting all elements with
// aria-labelledby on the page since that's probably a short list,
// then see if any of those ids overlap with an id in this element's
// ancestor chain.
var labelledByElements = document.querySelectorAll('[aria-labelledby]');
var labelledByTargets = {};
for (var i = 0; i < labelledByElements.length; ++i) {
var element = labelledByElements[i];
var attrValue = element.getAttribute('aria-labelledby');
var ids = attrValue.split(/ +/);
for (var j = 0; j < ids.length; j++) {
labelledByTargets[ids[j]] = true;
}
}
cvox.labelledByTargets = labelledByTargets;
window.setTimeout(function() {
cvox.labelledByTargets = null;
}, 0);
return labelledByTargets;
};
/**
* Determines whether or not a node has content.
*
* @param {Node} node The node to be checked.
* @return {boolean} True if the node has content.
*/
cvox.DomUtil.hasContent = function(node) {
// Memoize the result of the internal content computation so that
// within the same call stack, we don't need to redo the computation
// on the same node twice.
return /** @type {boolean} */ (cvox.Memoize.memoize(
cvox.DomUtil.computeHasContent_.bind(this), 'hasContent', node));
};
/**
* Internal implementation of |cvox.DomUtil.hasContent|.
*
* @param {Node} node The node to be checked.
* @return {boolean} True if the node has content.
* @private
*/
cvox.DomUtil.computeHasContent_ = function(node) {
// nodeType:8 == COMMENT_NODE
if (node.nodeType == 8) {
return false;
}
// Exclude anything in the head
if (cvox.DomUtil.isDescendantOf(node, 'HEAD')) {
return false;
}
// Exclude script nodes
if (cvox.DomUtil.isDescendantOf(node, 'SCRIPT')) {
return false;
}
// Exclude noscript nodes
if (cvox.DomUtil.isDescendantOf(node, 'NOSCRIPT')) {
return false;
}
// Exclude noembed nodes since NOEMBED is deprecated. We treat
// noembed as having not content rather than try to get its content since
// Chrome will return raw HTML content rather than a valid DOM subtree.
if (cvox.DomUtil.isDescendantOf(node, 'NOEMBED')) {
return false;
}
// Exclude style nodes that have been dumped into the body.
if (cvox.DomUtil.isDescendantOf(node, 'STYLE')) {
return false;
}
// Check the style to exclude undisplayed/hidden nodes.
if (!cvox.DomUtil.isVisible(node)) {
return false;
}
// Ignore anything that is hidden by ARIA.
if (cvox.AriaUtil.isHidden(node)) {
return false;
}
// We need to speak controls, including those with no value entered. We
// therefore treat visible controls as if they had content, and return true
// below.
if (cvox.DomUtil.isControl(node)) {
return true;
}
// Videos are always considered to have content so that we can navigate to
// and use the controls of the video widget.
if (cvox.DomUtil.isDescendantOf(node, 'VIDEO')) {
return true;
}
// Audio elements are always considered to have content so that we can
// navigate to and use the controls of the audio widget.
if (cvox.DomUtil.isDescendantOf(node, 'AUDIO')) {
return true;
}
// We want to try to jump into an iframe iff it has a src attribute.
// For right now, we will avoid iframes without any content in their src since
// ChromeVox is not being injected in those cases and will cause the user to
// get stuck.
// TODO (clchen, dmazzoni): Manually inject ChromeVox for iframes without src.
if ((node.tagName == 'IFRAME') && (node.src) &&
(node.src.indexOf('javascript:') != 0)) {
return true;
}
var controlQuery = 'button,input,select,textarea';
// Skip any non-control content inside of a label if the label is
// correctly associated with a control, the label text will get spoken
// when the control is reached.
var enclosingLabel = node.parentElement;
while (enclosingLabel && enclosingLabel.tagName != 'LABEL') {
enclosingLabel = enclosingLabel.parentElement;
}
if (enclosingLabel) {
var embeddedControl = enclosingLabel.querySelector(controlQuery);
if (enclosingLabel.hasAttribute('for')) {
var targetId = enclosingLabel.getAttribute('for');
var targetNode = document.getElementById(targetId);
if (targetNode &&
cvox.DomUtil.isControl(targetNode) &&
!embeddedControl) {
return false;
}
} else if (embeddedControl) {
return false;
}
}
// Skip any non-control content inside of a legend if the legend is correctly
// nested within a fieldset. The legend text will get spoken when the fieldset
// is reached.
var enclosingLegend = node.parentElement;
while (enclosingLegend && enclosingLegend.tagName != 'LEGEND') {
enclosingLegend = enclosingLegend.parentElement;
}
if (enclosingLegend) {
var legendAncestor = enclosingLegend.parentElement;
while (legendAncestor && legendAncestor.tagName != 'FIELDSET') {
legendAncestor = legendAncestor.parentElement;
}
var embeddedControl =
legendAncestor && legendAncestor.querySelector(controlQuery);
if (legendAncestor && !embeddedControl) {
return false;
}
}
if (!!cvox.DomPredicates.linkPredicate([node])) {
return true;
}
// At this point, any non-layout tables are considered to have content.
// For layout tables, it is safe to consider them as without content since the
// sync operation would select a descendant of a layout table if possible. The
// only instance where |hasContent| gets called on a layout table is if no
// descendants have content (see |AbstractNodeWalker.next|).
if (node.tagName == 'TABLE' && !cvox.DomUtil.isLayoutTable(node)) {
return true;
}
// Math is always considered to have content.
if (cvox.DomUtil.isMath(node)) {
return true;
}
if (cvox.DomPredicates.headingPredicate([node])) {
return true;
}
if (cvox.DomUtil.isFocusable(node)) {
return true;
}
// Skip anything referenced by another element on the page
// via aria-labelledby.
var labelledByTargets = cvox.DomUtil.getLabelledByTargets();
var enclosingNodeWithId = node;
while (enclosingNodeWithId) {
if (enclosingNodeWithId.id &&
labelledByTargets[enclosingNodeWithId.id]) {
// If we got here, some element on this page has an aria-labelledby
// attribute listing this node as its id. As long as that "some" element
// is not this element, we should return false, indicating this element
// should be skipped.
var attrValue = enclosingNodeWithId.getAttribute('aria-labelledby');
if (attrValue) {
var ids = attrValue.split(/ +/);
if (ids.indexOf(enclosingNodeWithId.id) == -1) {
return false;
}
} else {
return false;
}
}
enclosingNodeWithId = enclosingNodeWithId.parentElement;
}
var text = cvox.DomUtil.getValue(node) + ' ' + cvox.DomUtil.getName(node);
var state = cvox.DomUtil.getState(node, true);
if (text.match(/^\s+$/) && state === '') {
// Text only contains whitespace
return false;
}
return true;
};
/**
* Returns a list of all the ancestors of a given node. The last element
* is the current node.
*
* @param {Node} targetNode The node to get ancestors for.
* @return {Array.<Node>} An array of ancestors for the targetNode.
*/
cvox.DomUtil.getAncestors = function(targetNode) {
var ancestors = new Array();
while (targetNode) {
ancestors.push(targetNode);
targetNode = targetNode.parentNode;
}
ancestors.reverse();
while (ancestors.length && !ancestors[0].tagName && !ancestors[0].nodeValue) {
ancestors.shift();
}
return ancestors;
};
/**
* Compares Ancestors of A with Ancestors of B and returns
* the index value in B at which B diverges from A.
* If there is no divergence, the result will be -1.
* Note that if B is the same as A except B has more nodes
* even after A has ended, that is considered a divergence.
* The first node that B has which A does not have will
* be treated as the divergence point.
*
* @param {Object} ancestorsA The array of ancestors for Node A.
* @param {Object} ancestorsB The array of ancestors for Node B.
* @return {number} The index of the divergence point (the first node that B has
* which A does not have in B's list of ancestors).
*/
cvox.DomUtil.compareAncestors = function(ancestorsA, ancestorsB) {
var i = 0;
while (ancestorsA[i] && ancestorsB[i] && (ancestorsA[i] == ancestorsB[i])) {
i++;
}
if (!ancestorsA[i] && !ancestorsB[i]) {
i = -1;
}
return i;
};
/**
* Returns an array of ancestors that are unique for the currentNode when
* compared to the previousNode. Having such an array is useful in generating
* the node information (identifying when interesting node boundaries have been
* crossed, etc.).
*
* @param {Node} previousNode The previous node.
* @param {Node} currentNode The current node.
* @param {boolean=} opt_fallback True returns node's ancestors in the case
* where node's ancestors is a subset of previousNode's ancestors.
* @return {Array.<Node>} An array of unique ancestors for the current node
* (inclusive).
*/
cvox.DomUtil.getUniqueAncestors = function(
previousNode, currentNode, opt_fallback) {
var prevAncestors = cvox.DomUtil.getAncestors(previousNode);
var currentAncestors = cvox.DomUtil.getAncestors(currentNode);
var divergence = cvox.DomUtil.compareAncestors(prevAncestors,
currentAncestors);
var diff = currentAncestors.slice(divergence);
return (diff.length == 0 && opt_fallback) ? currentAncestors : diff;
};
/**
* Returns a role message identifier for a node.
* For a localized string, see cvox.DomUtil.getRole.
* @param {Node} targetNode The node to get the role name for.
* @param {number} verbosity The verbosity setting to use.
* @return {string} The role message identifier for the targetNode.
*/
cvox.DomUtil.getRoleMsg = function(targetNode, verbosity) {
var info;
info = cvox.AriaUtil.getRoleNameMsg(targetNode);
if (!info) {
if (targetNode.tagName == 'INPUT') {
info = cvox.DomUtil.INPUT_TYPE_TO_INFORMATION_TABLE_MSG[targetNode.type];
} else if (targetNode.tagName == 'A' &&
cvox.DomUtil.isInternalLink(targetNode)) {
info = 'internal_link';
} else if (targetNode.tagName == 'A' &&
targetNode.getAttribute('href') &&
cvox.ChromeVox.visitedUrls[targetNode.href]) {
info = 'visited_link';
} else if (targetNode.tagName == 'A' &&
targetNode.getAttribute('name')) {
info = ''; // Don't want to add any role to anchors.
} else if (targetNode.isContentEditable) {
info = 'input_type_text';
} else if (cvox.DomUtil.isMath(targetNode)) {
info = 'math_expr';
} else if (targetNode.tagName == 'TABLE' &&
cvox.DomUtil.isLayoutTable(targetNode)) {
info = '';
} else {
if (verbosity == cvox.VERBOSITY_BRIEF) {
info =
cvox.DomUtil.TAG_TO_INFORMATION_TABLE_BRIEF_MSG[targetNode.tagName];
} else {
info = cvox.DomUtil.TAG_TO_INFORMATION_TABLE_VERBOSE_MSG[
targetNode.tagName];
if (cvox.DomUtil.hasLongDesc(targetNode)) {
info = 'image_with_long_desc';
}
if (!info && targetNode.onclick) {
info = 'clickable';
}
}
}
}
return info;
};
/**
* Returns a string to be presented to the user that identifies what the
* targetNode's role is.
* ARIA roles are given priority; if there is no ARIA role set, the role
* will be determined by the HTML tag for the node.
*
* @param {Node} targetNode The node to get the role name for.
* @param {number} verbosity The verbosity setting to use.
* @return {string} The role name for the targetNode.
*/
cvox.DomUtil.getRole = function(targetNode, verbosity) {
var roleMsg = cvox.DomUtil.getRoleMsg(targetNode, verbosity) || '';
var role = roleMsg && roleMsg != ' ' ?
cvox.ChromeVox.msgs.getMsg(roleMsg) : '';
return role ? role : roleMsg;
};
/**
* Count the number of items in a list node.
*
* @param {Node} targetNode The list node.
* @return {number} The number of items in the list.
*/
cvox.DomUtil.getListLength = function(targetNode) {
var count = 0;
for (var node = targetNode.firstChild;
node;
node = node.nextSibling) {
if (cvox.DomUtil.isVisible(node) &&
(node.tagName == 'LI' ||
(node.getAttribute && node.getAttribute('role') == 'listitem'))) {
if (node.hasAttribute('aria-setsize')) {
var ariaLength = parseInt(node.getAttribute('aria-setsize'), 10);
if (!isNaN(ariaLength)) {
return ariaLength;
}
}
count++;
}
}
return count;
};
/**
* Returns a NodeState that gives information about the state of the targetNode.
*
* @param {Node} targetNode The node to get the state information for.
* @param {boolean} primary Whether this is the primary node we're
* interested in, where we might want extra information - as
* opposed to an ancestor, where we might be more brief.
* @return {cvox.NodeState} The status information about the node.
*/
cvox.DomUtil.getStateMsgs = function(targetNode, primary) {
var activeDescendant = cvox.AriaUtil.getActiveDescendant(targetNode);
if (activeDescendant) {
return cvox.DomUtil.getStateMsgs(activeDescendant, primary);
}
var info = [];
var role = targetNode.getAttribute ? targetNode.getAttribute('role') : '';
info = cvox.AriaUtil.getStateMsgs(targetNode, primary);
if (!info) {
info = [];
}
if (targetNode.tagName == 'INPUT') {
if (!targetNode.hasAttribute('aria-checked')) {
var INPUT_MSGS = {
'checkbox-true': 'checkbox_checked_state',
'checkbox-false': 'checkbox_unchecked_state',
'radio-true': 'radio_selected_state',
'radio-false': 'radio_unselected_state' };
var msgId = INPUT_MSGS[targetNode.type + '-' + !!targetNode.checked];
if (msgId) {
info.push([msgId]);
}
}
} else if (targetNode.tagName == 'SELECT') {
if (targetNode.selectedOptions && targetNode.selectedOptions.length <= 1) {
info.push(['list_position',
cvox.ChromeVox.msgs.getNumber(targetNode.selectedIndex + 1),
cvox.ChromeVox.msgs.getNumber(targetNode.options.length)]);
} else {
info.push(['selected_options_state',
cvox.ChromeVox.msgs.getNumber(targetNode.selectedOptions.length)]);
}
} else if (targetNode.tagName == 'UL' ||
targetNode.tagName == 'OL' ||
role == 'list') {
info.push(['list_with_items',
cvox.ChromeVox.msgs.getNumber(
cvox.DomUtil.getListLength(targetNode))]);
}
if (cvox.DomUtil.isDisabled(targetNode)) {
info.push(['aria_disabled_true']);
}
if (targetNode.accessKey) {
info.push(['access_key', targetNode.accessKey]);
}
return info;
};
/**
* Returns a string that gives information about the state of the targetNode.
*
* @param {Node} targetNode The node to get the state information for.
* @param {boolean} primary Whether this is the primary node we're
* interested in, where we might want extra information - as
* opposed to an ancestor, where we might be more brief.
* @return {string} The status information about the node.
*/
cvox.DomUtil.getState = function(targetNode, primary) {
return cvox.NodeStateUtil.expand(
cvox.DomUtil.getStateMsgs(targetNode, primary));
};
/**
* Return whether a node is focusable. This includes nodes whose tabindex
* attribute is set to "-1" explicitly - these nodes are not in the tab
* order, but they should still be focused if the user navigates to them
* using linear or smart DOM navigation.
*
* Note that when the tabIndex property of an Element is -1, that doesn't
* tell us whether the tabIndex attribute is missing or set to "-1" explicitly,
* so we have to check the attribute.
*
* @param {Object} targetNode The node to check if it's focusable.
* @return {boolean} True if the node is focusable.
*/
cvox.DomUtil.isFocusable = function(targetNode) {
if (!targetNode || typeof(targetNode.tabIndex) != 'number') {
return false;
}
// Workaround for http://code.google.com/p/chromium/issues/detail?id=153904
if ((targetNode.tagName == 'A') && !targetNode.hasAttribute('href') &&
!targetNode.hasAttribute('tabindex')) {
return false;
}
if (targetNode.tabIndex >= 0) {
return true;
}
if (targetNode.hasAttribute &&
targetNode.hasAttribute('tabindex') &&
targetNode.getAttribute('tabindex') == '-1') {
return true;
}
return false;
};
/**
* Find a focusable descendant of a given node. This includes nodes whose
* tabindex attribute is set to "-1" explicitly - these nodes are not in the
* tab order, but they should still be focused if the user navigates to them
* using linear or smart DOM navigation.
*
* @param {Node} targetNode The node whose descendants to check if focusable.
* @return {Node} The focusable descendant node. Null if no descendant node
* was found.
*/
cvox.DomUtil.findFocusableDescendant = function(targetNode) {
// Search down the descendants chain until a focusable node is found
if (targetNode) {
var focusableNode =
cvox.DomUtil.findNode(targetNode, cvox.DomUtil.isFocusable);
if (focusableNode) {
return focusableNode;
}
}
return null;
};
/**
* Returns the number of focusable nodes in root's subtree. The count does not
* include root.
*
* @param {Node} targetNode The node whose descendants to check are focusable.
* @return {number} The number of focusable descendants.
*/
cvox.DomUtil.countFocusableDescendants = function(targetNode) {
return targetNode ?
cvox.DomUtil.countNodes(targetNode, cvox.DomUtil.isFocusable) : 0;
};
/**
* Checks if the targetNode is still attached to the document.
* A node can become detached because of AJAX changes.
*
* @param {Object} targetNode The node to check.
* @return {boolean} True if the targetNode is still attached.
*/
cvox.DomUtil.isAttachedToDocument = function(targetNode) {
while (targetNode) {
if (targetNode.tagName && (targetNode.tagName == 'HTML')) {
return true;
}
targetNode = targetNode.parentNode;
}
return false;
};
/**
* Dispatches a left click event on the element that is the targetNode.
* Clicks go in the sequence of mousedown, mouseup, and click.
* @param {Node} targetNode The target node of this operation.
* @param {boolean} shiftKey Specifies if shift is held down.
* @param {boolean} callOnClickDirectly Specifies whether or not to directly
* invoke the onclick method if there is one.
* @param {boolean=} opt_double True to issue a double click.
* @param {boolean=} opt_handleOwnEvents Whether to handle the generated
* events through the normal event processing.
*/
cvox.DomUtil.clickElem = function(
targetNode, shiftKey, callOnClickDirectly, opt_double,
opt_handleOwnEvents) {
// If there is an activeDescendant of the targetNode, then that is where the
// click should actually be targeted.
var activeDescendant = cvox.AriaUtil.getActiveDescendant(targetNode);
if (activeDescendant) {
targetNode = activeDescendant;
}
if (callOnClickDirectly) {
var onClickFunction = null;
if (targetNode.onclick) {
onClickFunction = targetNode.onclick;
}
if (!onClickFunction && (targetNode.nodeType != 1) &&
targetNode.parentNode && targetNode.parentNode.onclick) {
onClickFunction = targetNode.parentNode.onclick;
}
var keepGoing = true;
if (onClickFunction) {
try {
keepGoing = onClickFunction();
} catch (exception) {
// Something went very wrong with the onclick method; we'll ignore it
// and just dispatch a click event normally.
}
}
if (!keepGoing) {
// The onclick method ran successfully and returned false, meaning the
// event should not bubble up, so we will return here.
return;
}
}
// Send a mousedown (or simply a double click if requested).
var evt = document.createEvent('MouseEvents');
var evtType = opt_double ? 'dblclick' : 'mousedown';
evt.initMouseEvent(evtType, true, true, document.defaultView,
1, 0, 0, 0, 0, false, false, shiftKey, false, 0, null);
// Unless asked not to, Mark any events we generate so we don't try to
// process our own events.
evt.fromCvox = !opt_handleOwnEvents;
try {
targetNode.dispatchEvent(evt);
} catch (e) {}
//Send a mouse up
evt = document.createEvent('MouseEvents');
evt.initMouseEvent('mouseup', true, true, document.defaultView,
1, 0, 0, 0, 0, false, false, shiftKey, false, 0, null);
evt.fromCvox = !opt_handleOwnEvents;
try {
targetNode.dispatchEvent(evt);
} catch (e) {}
//Send a click
evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, document.defaultView,
1, 0, 0, 0, 0, false, false, shiftKey, false, 0, null);
evt.fromCvox = !opt_handleOwnEvents;
try {
targetNode.dispatchEvent(evt);
} catch (e) {}
if (cvox.DomUtil.isInternalLink(targetNode)) {
cvox.DomUtil.syncInternalLink(targetNode);
}
};
/**
* Syncs to an internal link.
* @param {Node} node A link whose href's target we want to sync.
*/
cvox.DomUtil.syncInternalLink = function(node) {
var targetNode;
var targetId = node.href.split('#')[1];
targetNode = document.getElementById(targetId);
if (!targetNode) {
var nodes = document.getElementsByName(targetId);
if (nodes.length > 0) {
targetNode = nodes[0];
}
}
if (targetNode) {
// Insert a dummy node to adjust next Tab focus location.
var parent = targetNode.parentNode;
var dummyNode = document.createElement('div');
dummyNode.setAttribute('tabindex', '-1');
parent.insertBefore(dummyNode, targetNode);
dummyNode.setAttribute('chromevoxignoreariahidden', 1);
dummyNode.focus();
cvox.ChromeVox.syncToNode(targetNode, false);
}
};
/**
* Given an HTMLInputElement, returns true if it's an editable text type.
* This includes input type='text' and input type='password' and a few
* others.
*
* @param {Node} node The node to check.
* @return {boolean} True if the node is an INPUT with an editable text type.
*/
cvox.DomUtil.isInputTypeText = function(node) {
if (!node || node.constructor != HTMLInputElement) {
return false;
}
switch (node.type) {
case 'email':
case 'number':
case 'password':
case 'search':
case 'text':
case 'tel':
case 'url':
case '':
return true;
default:
return false;
}
};
/**
* Given a node, returns true if it's a control. Controls are *not necessarily*
* leaf-level given that some composite controls may have focusable children
* if they are managing focus with tabindex:
* ( http://www.w3.org/TR/2010/WD-wai-aria-practices-20100916/#visualfocus ).
*
* @param {Node} node The node to check.
* @return {boolean} True if the node is a control.
*/
cvox.DomUtil.isControl = function(node) {
if (cvox.AriaUtil.isControlWidget(node) &&
cvox.DomUtil.isFocusable(node)) {
return true;
}
if (node.tagName) {
switch (node.tagName) {
case 'BUTTON':
case 'TEXTAREA':
case 'SELECT':
return true;
case 'INPUT':
return node.type != 'hidden';
}
}
if (node.isContentEditable) {
return true;
}
return false;
};
/**
* Given a node, returns true if it's a leaf-level control. This includes
* composite controls thare are managing focus for children with
* activedescendant, but not composite controls with focusable children:
* ( http://www.w3.org/TR/2010/WD-wai-aria-practices-20100916/#visualfocus ).
*
* @param {Node} node The node to check.
* @return {boolean} True if the node is a leaf-level control.
*/
cvox.DomUtil.isLeafLevelControl = function(node) {
if (cvox.DomUtil.isControl(node)) {
return !(cvox.AriaUtil.isCompositeControl(node) &&
cvox.DomUtil.findFocusableDescendant(node));
}
return false;
};
/**
* Given a node that might be inside of a composite control like a listbox,
* return the surrounding control.
* @param {Node} node The node from which to start looking.
* @return {Node} The surrounding composite control node, or null if none.
*/
cvox.DomUtil.getSurroundingControl = function(node) {
var surroundingControl = null;
if (!cvox.DomUtil.isControl(node) && node.hasAttribute &&
node.hasAttribute('role')) {
surroundingControl = node.parentElement;
while (surroundingControl &&
!cvox.AriaUtil.isCompositeControl(surroundingControl)) {
surroundingControl = surroundingControl.parentElement;
}
}
return surroundingControl;
};
/**
* Given a node and a function for determining when to stop
* descent, return the next leaf-like node.
*
* @param {!Node} node The node from which to start looking,
* this node *must not* be above document.body.
* @param {boolean} r True if reversed. False by default.
* @param {function(!Node):boolean} isLeaf A function that
* returns true if we should stop descending.
* @return {Node} The next leaf-like node or null if there is no next
* leaf-like node. This function will always return a node below
* document.body and never document.body itself.
*/
cvox.DomUtil.directedNextLeafLikeNode = function(node, r, isLeaf) {
if (node != document.body) {
// if not at the top of the tree, we want to find the next possible
// branch forward in the dom, so we climb up the parents until we find a
// node that has a nextSibling
while (!cvox.DomUtil.directedNextSibling(node, r)) {
if (!node) {
return null;
}
// since node is never above document.body, it always has a parent.
// so node.parentNode will never be null.
node = /** @type {!Node} */(node.parentNode);
if (node == document.body) {
// we've readed the end of the document.
return null;
}
}
if (cvox.DomUtil.directedNextSibling(node, r)) {
// we just checked that next sibling is non-null.
node = /** @type {!Node} */(cvox.DomUtil.directedNextSibling(node, r));
}
}
// once we're at our next sibling, we want to descend down into it as
// far as the child class will allow
while (cvox.DomUtil.directedFirstChild(node, r) && !isLeaf(node)) {
node = /** @type {!Node} */(cvox.DomUtil.directedFirstChild(node, r));
}
// after we've done all that, if we are still at document.body, this must
// be an empty document.
if (node == document.body) {
return null;
}
return node;
};
/**
* Given a node, returns the next leaf node.
*
* @param {!Node} node The node from which to start looking
* for the next leaf node.
* @param {boolean=} reverse True if reversed. False by default.
* @return {Node} The next leaf node.
* Null if there is no next leaf node.
*/
cvox.DomUtil.directedNextLeafNode = function(node, reverse) {
reverse = !!reverse;
return cvox.DomUtil.directedNextLeafLikeNode(
node, reverse, cvox.DomUtil.isLeafNode);
};
/**
* Given a node, returns the previous leaf node.
*
* @param {!Node} node The node from which to start looking
* for the previous leaf node.
* @return {Node} The previous leaf node.
* Null if there is no previous leaf node.
*/
cvox.DomUtil.previousLeafNode = function(node) {
return cvox.DomUtil.directedNextLeafNode(node, true);
};
/**
* Computes the outer most leaf node of a given node, depending on value
* of the reverse flag r.
* @param {!Node} node in the DOM.
* @param {boolean} r True if reversed. False by default.
* @param {function(!Node):boolean} pred Predicate to decide
* what we consider a leaf.
* @return {Node} The outer most leaf node of that node.
*/
cvox.DomUtil.directedFindFirstNode = function(node, r, pred) {
var child = cvox.DomUtil.directedFirstChild(node, r);
while (child) {
if (pred(child)) {
return child;
} else {
var leaf = cvox.DomUtil.directedFindFirstNode(child, r, pred);
if (leaf) {
return leaf;
}
}
child = cvox.DomUtil.directedNextSibling(child, r);
}
return null;
};
/**
* Moves to the deepest node satisfying a given predicate under the given node.
* @param {!Node} node in the DOM.
* @param {boolean} r True if reversed. False by default.
* @param {function(!Node):boolean} pred Predicate deciding what a leaf is.
* @return {Node} The deepest node satisfying pred.
*/
cvox.DomUtil.directedFindDeepestNode = function(node, r, pred) {
var next = cvox.DomUtil.directedFindFirstNode(node, r, pred);
if (!next) {
if (pred(node)) {
return node;
} else {
return null;
}
} else {
return cvox.DomUtil.directedFindDeepestNode(next, r, pred);
}
};
/**
* Computes the next node wrt. a predicate that is a descendant of ancestor.
* @param {!Node} node in the DOM.
* @param {!Node} ancestor of the given node.
* @param {boolean} r True if reversed. False by default.
* @param {function(!Node):boolean} pred Predicate to decide
* what we consider a leaf.
* @param {boolean=} above True if the next node can live in the subtree
* directly above the start node. False by default.
* @param {boolean=} deep True if we are looking for the next node that is
* deepest in the tree. Otherwise the next shallow node is returned.
* False by default.
* @return {Node} The next node in the DOM that satisfies the predicate.
*/
cvox.DomUtil.directedFindNextNode = function(
node, ancestor, r, pred, above, deep) {
above = !!above;
deep = !!deep;
if (!cvox.DomUtil.isDescendantOfNode(node, ancestor) || node == ancestor) {
return null;
}
var next = cvox.DomUtil.directedNextSibling(node, r);
while (next) {
if (!deep && pred(next)) {
return next;
}
var leaf = (deep ?
cvox.DomUtil.directedFindDeepestNode :
cvox.DomUtil.directedFindFirstNode)(next, r, pred);
if (leaf) {
return leaf;
}
if (deep && pred(next)) {
return next;
}
next = cvox.DomUtil.directedNextSibling(next, r);
}
var parent = /** @type {!Node} */(node.parentNode);
if (above && pred(parent)) {
return parent;
}
return cvox.DomUtil.directedFindNextNode(
parent, ancestor, r, pred, above, deep);
};
/**
* Get a string representing a control's value and state, i.e. the part
* that changes while interacting with the control
* @param {Element} control A control.
* @return {string} The value and state string.
*/
cvox.DomUtil.getControlValueAndStateString = function(control) {
var parentControl = cvox.DomUtil.getSurroundingControl(control);
if (parentControl) {
return cvox.DomUtil.collapseWhitespace(
cvox.DomUtil.getValue(control) + ' ' +
cvox.DomUtil.getName(control) + ' ' +
cvox.DomUtil.getState(control, true));
} else {
return cvox.DomUtil.collapseWhitespace(
cvox.DomUtil.getValue(control) + ' ' +
cvox.DomUtil.getState(control, true));
}
};
/**
* Determine whether the given node is an internal link.
* @param {Node} node The node to be examined.
* @return {boolean} True if the node is an internal link, false otherwise.
*/
cvox.DomUtil.isInternalLink = function(node) {
if (node.nodeType == 1) { // Element nodes only.
var href = node.getAttribute('href');
if (href && href.indexOf('#') != -1) {
var path = href.split('#')[0];
return path == '' || path == window.location.pathname;
}
}
return false;
};
/**
* Get a string containing the currently selected link's URL.
* @param {Node} node The link from which URL needs to be extracted.
* @return {string} The value of the URL.
*/
cvox.DomUtil.getLinkURL = function(node) {
if (node.tagName == 'A') {
if (node.getAttribute('href')) {
if (cvox.DomUtil.isInternalLink(node)) {
return cvox.ChromeVox.msgs.getMsg('internal_link');
} else {
return node.getAttribute('href');
}
} else {
return '';
}
} else if (cvox.AriaUtil.getRoleName(node) ==
cvox.ChromeVox.msgs.getMsg('aria_role_link')) {
return cvox.ChromeVox.msgs.getMsg('unknown_link');
}
return '';
};
/**
* Checks if a given node is inside a table and returns the table node if it is
* @param {Node} node The node.
* @param {{allowCaptions: (undefined|boolean)}=} kwargs Optional named args.
* allowCaptions: If true, will return true even if inside a caption. False
* by default.
* @return {Node} If the node is inside a table, the table node. Null if it
* is not.
*/
cvox.DomUtil.getContainingTable = function(node, kwargs) {
var ancestors = cvox.DomUtil.getAncestors(node);
return cvox.DomUtil.findTableNodeInList(ancestors, kwargs);
};
/**
* Extracts a table node from a list of nodes.
* @param {Array.<Node>} nodes The list of nodes.
* @param {{allowCaptions: (undefined|boolean)}=} kwargs Optional named args.
* allowCaptions: If true, will return true even if inside a caption. False
* by default.
* @return {Node} The table node if the list of nodes contains a table node.
* Null if it does not.
*/
cvox.DomUtil.findTableNodeInList = function(nodes, kwargs) {
kwargs = kwargs || {allowCaptions: false};
// Don't include the caption node because it is actually rendered outside
// of the table.
for (var i = nodes.length - 1, node; node = nodes[i]; i--) {
if (node.constructor != Text) {
if (!kwargs.allowCaptions && node.tagName == 'CAPTION') {
return null;
}
if ((node.tagName == 'TABLE') || cvox.AriaUtil.isGrid(node)) {
return node;
}
}
}
return null;
};
/**
* Determines whether a given table is a data table or a layout table
* @param {Node} tableNode The table node.
* @return {boolean} If the table is a layout table, returns true. False
* otherwise.
*/
cvox.DomUtil.isLayoutTable = function(tableNode) {
// TODO(stoarca): Why are we returning based on this inaccurate heuristic
// instead of first trying the better heuristics below?
if (tableNode.rows && (tableNode.rows.length <= 1 ||
(tableNode.rows[0].childElementCount == 1))) {
// This table has either 0 or one rows, or only "one" column.
// This is a quick check for column count and may not be accurate. See
// TraverseTable.getW3CColCount_ for a more accurate
// (but more complicated) way to determine column count.
return true;
}
// These heuristics are adapted from the Firefox data and layout table.
// heuristics: http://asurkov.blogspot.com/2011/10/data-vs-layout-table.html
if (cvox.AriaUtil.isGrid(tableNode)) {
// This table has an ARIA role identifying it as a grid.
// Not a layout table.
return false;
}
if (cvox.AriaUtil.isLandmark(tableNode)) {
// This table has an ARIA landmark role - not a layout table.
return false;
}
if (tableNode.caption || tableNode.summary) {
// This table has a caption or a summary - not a layout table.
return false;
}
if ((cvox.XpathUtil.evalXPath('tbody/tr/th', tableNode).length > 0) &&
(cvox.XpathUtil.evalXPath('tbody/tr/td', tableNode).length > 0)) {
// This table at least one column and at least one column header.
// Not a layout table.
return false;
}
if (cvox.XpathUtil.evalXPath('colgroup', tableNode).length > 0) {
// This table specifies column groups - not a layout table.
return false;
}
if ((cvox.XpathUtil.evalXPath('thead', tableNode).length > 0) ||
(cvox.XpathUtil.evalXPath('tfoot', tableNode).length > 0)) {
// This table has header or footer rows - not a layout table.
return false;
}
if ((cvox.XpathUtil.evalXPath('tbody/tr/td/embed', tableNode).length > 0) ||
(cvox.XpathUtil.evalXPath('tbody/tr/td/object', tableNode).length > 0) ||
(cvox.XpathUtil.evalXPath('tbody/tr/td/iframe', tableNode).length > 0) ||
(cvox.XpathUtil.evalXPath('tbody/tr/td/applet', tableNode).length > 0)) {
// This table contains embed, object, applet, or iframe elements. It is
// a layout table.
return true;
}
// These heuristics are loosely based on Okada and Miura's "Detection of
// Layout-Purpose TABLE Tags Based on Machine Learning" (2007).
// http://books.google.com/books?id=kUbmdqasONwC&lpg=PA116&ots=Lb3HJ7dISZ&lr&pg=PA116
// Increase the points for each heuristic. If there are 3 or more points,
// this is probably a layout table.
var points = 0;
if (! cvox.DomUtil.hasBorder(tableNode)) {
// This table has no border.
points++;
}
if (tableNode.rows.length <= 6) {
// This table has a limited number of rows.
points++;
}
if (cvox.DomUtil.countPreviousTags(tableNode) <= 12) {
// This table has a limited number of previous tags.
points++;
}
if (cvox.XpathUtil.evalXPath('tbody/tr/td/table', tableNode).length > 0) {
// This table has nested tables.
points++;
}
return (points >= 3);
};
/**
* Count previous tags, which we dfine as the number of HTML tags that
* appear before the given node.
* @param {Node} node The given node.
* @return {number} The number of previous tags.
*/
cvox.DomUtil.countPreviousTags = function(node) {
var ancestors = cvox.DomUtil.getAncestors(node);
return ancestors.length + cvox.DomUtil.countPreviousSiblings(node);
};
/**
* Counts previous siblings, not including text nodes.
* @param {Node} node The given node.
* @return {number} The number of previous siblings.
*/
cvox.DomUtil.countPreviousSiblings = function(node) {
var count = 0;
var prev = node.previousSibling;
while (prev != null) {
if (prev.constructor != Text) {
count++;
}
prev = prev.previousSibling;
}
return count;
};
/**
* Whether a given table has a border or not.
* @param {Node} tableNode The table node.
* @return {boolean} If the table has a border, return true. False otherwise.
*/
cvox.DomUtil.hasBorder = function(tableNode) {
// If .frame contains "void" there is no border.
if (tableNode.frame) {
return (tableNode.frame.indexOf('void') == -1);
}
// If .border is defined and == "0" then there is no border.
if (tableNode.border) {
if (tableNode.border.length == 1) {
return (tableNode.border != '0');
} else {
return (tableNode.border.slice(0, -2) != 0);
}
}
// If .style.border-style is 'none' there is no border.
if (tableNode.style.borderStyle && tableNode.style.borderStyle == 'none') {
return false;
}
// If .style.border-width is specified in units of length
// ( https://developer.mozilla.org/en/CSS/border-width ) then we need
// to check if .style.border-width starts with 0[px,em,etc]
if (tableNode.style.borderWidth) {
return (tableNode.style.borderWidth.slice(0, -2) != 0);
}
// If .style.border-color is defined, then there is a border
if (tableNode.style.borderColor) {
return true;
}
return false;
};
/**
* Return the first leaf node, starting at the top of the document.
* @return {Node?} The first leaf node in the document, if found.
*/
cvox.DomUtil.getFirstLeafNode = function() {
var node = document.body;
while (node && node.firstChild) {
node = node.firstChild;
}
while (node && !cvox.DomUtil.hasContent(node)) {
node = cvox.DomUtil.directedNextLeafNode(node);
}
return node;
};
/**
* Finds the first descendant node that matches the filter function, using
* a depth first search. This function offers the most general purpose way
* of finding a matching element. You may also wish to consider
* {@code goog.dom.query} which can express many matching criteria using
* CSS selector expressions. These expressions often result in a more
* compact representation of the desired result.
* This is the findNode function from goog.dom:
* http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/dom/dom.js
*
* @param {Node} root The root of the tree to search.
* @param {function(Node) : boolean} p The filter function.
* @return {Node|undefined} The found node or undefined if none is found.
*/
cvox.DomUtil.findNode = function(root, p) {
var rv = [];
var found = cvox.DomUtil.findNodes_(root, p, rv, true, 10000);
return found ? rv[0] : undefined;
};
/**
* Finds the number of nodes matching the filter.
* @param {Node} root The root of the tree to search.
* @param {function(Node) : boolean} p The filter function.
* @return {number} The number of nodes selected by filter.
*/
cvox.DomUtil.countNodes = function(root, p) {
var rv = [];
cvox.DomUtil.findNodes_(root, p, rv, false, 10000);
return rv.length;
};
/**
* Finds the first or all the descendant nodes that match the filter function,
* using a depth first search.
* @param {Node} root The root of the tree to search.
* @param {function(Node) : boolean} p The filter function.
* @param {Array.<Node>} rv The found nodes are added to this array.
* @param {boolean} findOne If true we exit after the first found node.
* @param {number} maxChildCount The max child count. This is used as a kill
* switch - if there are more nodes than this, terminate the search.
* @return {boolean} Whether the search is complete or not. True in case
* findOne is true and the node is found. False otherwise. This is the
* findNodes_ function from goog.dom:
* http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/dom/dom.js.
* @private
*/
cvox.DomUtil.findNodes_ = function(root, p, rv, findOne, maxChildCount) {
if ((root != null) || (maxChildCount == 0)) {
var child = root.firstChild;
while (child) {
if (p(child)) {
rv.push(child);
if (findOne) {
return true;
}
}
maxChildCount = maxChildCount - 1;
if (cvox.DomUtil.findNodes_(child, p, rv, findOne, maxChildCount)) {
return true;
}
child = child.nextSibling;
}
}
return false;
};
/**
* Converts a NodeList into an array
* @param {NodeList} nodeList The nodeList.
* @return {Array} The array of nodes in the nodeList.
*/
cvox.DomUtil.toArray = function(nodeList) {
var nodeArray = [];
for (var i = 0; i < nodeList.length; i++) {
nodeArray.push(nodeList[i]);
}
return nodeArray;
};
/**
* Creates a new element with the same attributes and no children.
* @param {Node|Text} node A node to clone.
* @param {Object.<string, boolean>} skipattrs Set the attribute to true to
* skip it during cloning.
* @return {Node|Text} The cloned node.
*/
cvox.DomUtil.shallowChildlessClone = function(node, skipattrs) {
if (node.nodeName == '#text') {
return document.createTextNode(node.nodeValue);
}
if (node.nodeName == '#comment') {
return document.createComment(node.nodeValue);
}
var ret = document.createElement(node.nodeName);
for (var i = 0; i < node.attributes.length; ++i) {
var attr = node.attributes[i];
if (skipattrs && skipattrs[attr.nodeName]) {
continue;
}
ret.setAttribute(attr.nodeName, attr.nodeValue);
}
return ret;
};
/**
* Creates a new element with the same attributes and clones of children.
* @param {Node|Text} node A node to clone.
* @param {Object.<string, boolean>} skipattrs Set the attribute to true to
* skip it during cloning.
* @return {Node|Text} The cloned node.
*/
cvox.DomUtil.deepClone = function(node, skipattrs) {
var ret = cvox.DomUtil.shallowChildlessClone(node, skipattrs);
for (var i = 0; i < node.childNodes.length; ++i) {
ret.appendChild(cvox.DomUtil.deepClone(node.childNodes[i], skipattrs));
}
return ret;
};
/**
* Returns either node.firstChild or node.lastChild, depending on direction.
* @param {Node|Text} node The node.
* @param {boolean} reverse If reversed.
* @return {Node|Text} The directed first child or null if the node has
* no children.
*/
cvox.DomUtil.directedFirstChild = function(node, reverse) {
if (reverse) {
return node.lastChild;
}
return node.firstChild;
};
/**
* Returns either node.nextSibling or node.previousSibling, depending on
* direction.
* @param {Node|Text} node The node.
* @param {boolean=} reverse If reversed.
* @return {Node|Text} The directed next sibling or null if there are
* no more siblings in that direction.
*/
cvox.DomUtil.directedNextSibling = function(node, reverse) {
if (!node) {
return null;
}
if (reverse) {
return node.previousSibling;
}
return node.nextSibling;
};
/**
* Creates a function that sends a click. This is because loop closures
* are dangerous.
* See: http://joust.kano.net/weblog/archive/2005/08/08/
* a-huge-gotcha-with-javascript-closures/
* @param {Node} targetNode The target node to click on.
* @return {function()} A function that will click on the given targetNode.
*/
cvox.DomUtil.createSimpleClickFunction = function(targetNode) {
var target = targetNode.cloneNode(true);
return function() { cvox.DomUtil.clickElem(target, false, false); };
};
/**
* Adds a node to document.head if that node has not already been added.
* If document.head does not exist, this will add the node to the body.
* @param {Node} node The node to add.
* @param {string=} opt_id The id of the node to ensure the node is only
* added once.
*/
cvox.DomUtil.addNodeToHead = function(node, opt_id) {
if (opt_id && document.getElementById(opt_id)) {
return;
}
var p = document.head || document.body;
p.appendChild(node);
};
/**
* Checks if a given node is inside a math expressions and
* returns the math node if one exists.
* @param {Node} node The node.
* @return {Node} The math node, if the node is inside a math expression.
* Null if it is not.
*/
cvox.DomUtil.getContainingMath = function(node) {
var ancestors = cvox.DomUtil.getAncestors(node);
return cvox.DomUtil.findMathNodeInList(ancestors);
};
/**
* Extracts a math node from a list of nodes.
* @param {Array.<Node>} nodes The list of nodes.
* @return {Node} The math node if the list of nodes contains a math node.
* Null if it does not.
*/
cvox.DomUtil.findMathNodeInList = function(nodes) {
for (var i = 0, node; node = nodes[i]; i++) {
if (cvox.DomUtil.isMath(node)) {
return node;
}
}
return null;
};
/**
* Checks to see wether a node is a math node.
* @param {Node} node The node to be tested.
* @return {boolean} Whether or not a node is a math node.
*/
cvox.DomUtil.isMath = function(node) {
return cvox.DomUtil.isMathml(node) ||
cvox.DomUtil.isMathJax(node) ||
cvox.DomUtil.isMathImg(node) ||
cvox.AriaUtil.isMath(node);
};
/**
* Specifies node classes in which we expect maths expressions a alt text.
* @type {{tex: Array.<string>,
* asciimath: Array.<string>}}
*/
// These are the classes for which we assume they contain Maths in the ALT or
// TITLE attribute.
// tex: Wikipedia;
// latex: Wordpress;
// numberedequation, inlineformula, displayformula: MathWorld;
cvox.DomUtil.ALT_MATH_CLASSES = {
tex: ['tex', 'latex'],
asciimath: ['numberedequation', 'inlineformula', 'displayformula']
};
/**
* Composes a query selector string for image nodes with alt math content by
* type of content.
* @param {string} contentType The content type, e.g., tex, asciimath.
* @return {!string} The query elector string.
*/
cvox.DomUtil.altMathQuerySelector = function(contentType) {
var classes = cvox.DomUtil.ALT_MATH_CLASSES[contentType];
if (classes) {
return classes.map(function(x) {return 'img.' + x;}).join(', ');
}
return '';
};
/**
* Check if a given node is potentially a math image with alternative text in
* LaTeX.
* @param {Node} node The node to be tested.
* @return {boolean} Whether or not a node has an image with class TeX or LaTeX.
*/
cvox.DomUtil.isMathImg = function(node) {
if (!node || !node.tagName || !node.className) {
return false;
}
if (node.tagName != 'IMG') {
return false;
}
var className = node.className.toLowerCase();
return cvox.DomUtil.ALT_MATH_CLASSES.tex.indexOf(className) != -1 ||
cvox.DomUtil.ALT_MATH_CLASSES.asciimath.indexOf(className) != -1;
};
/**
* Checks to see whether a node is a MathML node.
* !! This is necessary as Chrome currently does not upperCase Math tags !!
* @param {Node} node The node to be tested.
* @return {boolean} Whether or not a node is a MathML node.
*/
cvox.DomUtil.isMathml = function(node) {
if (!node || !node.tagName) {
return false;
}
return node.tagName.toLowerCase() == 'math';
};
/**
* Checks to see wether a node is a MathJax node.
* @param {Node} node The node to be tested.
* @return {boolean} Whether or not a node is a MathJax node.
*/
cvox.DomUtil.isMathJax = function(node) {
if (!node || !node.tagName || !node.className) {
return false;
}
function isSpanWithClass(n, cl) {
return (n.tagName == 'SPAN' &&
n.className.split(' ').some(function(x) {
return x.toLowerCase() == cl;}));
};
if (isSpanWithClass(node, 'math')) {
var ancestors = cvox.DomUtil.getAncestors(node);
return ancestors.some(function(x) {return isSpanWithClass(x, 'mathjax');});
}
return false;
};
/**
* Computes the id of the math span in a MathJax DOM element.
* @param {string} jaxId The id of the MathJax node.
* @return {string} The id of the span node.
*/
cvox.DomUtil.getMathSpanId = function(jaxId) {
var node = document.getElementById(jaxId + '-Frame');
if (node) {
var span = node.querySelector('span.math');
if (span) {
return span.id;
}
}
};
/**
* Returns true if the node has a longDesc.
* @param {Node} node The node to be tested.
* @return {boolean} Whether or not a node has a longDesc.
*/
cvox.DomUtil.hasLongDesc = function(node) {
if (node && node.longDesc) {
return true;
}
return false;
};
/**
* Returns tag name of a node if it has one.
* @param {Node} node A node.
* @return {string} A the tag name of the node.
*/
cvox.DomUtil.getNodeTagName = function(node) {
if (node.nodeType == Node.ELEMENT_NODE) {
return node.tagName;
}
return '';
};
/**
* Cleaning up a list of nodes to remove empty text nodes.
* @param {NodeList} nodes The nodes list.
* @return {!Array.<Node|string|null>} The cleaned up list of nodes.
*/
cvox.DomUtil.purgeNodes = function(nodes) {
return cvox.DomUtil.toArray(nodes).
filter(function(node) {
return node.nodeType != Node.TEXT_NODE ||
!node.textContent.match(/^\s+$/);});
};
/**
* Calculates a hit point for a given node.
* @return {{x:(number), y:(number)}} The position.
*/
cvox.DomUtil.elementToPoint = function(node) {
if (!node) {
return {x: 0, y: 0};
}
if (node.constructor == Text) {
node = node.parentNode;
}
var r = node.getBoundingClientRect();
return {
x: r.left + (r.width / 2),
y: r.top + (r.height / 2)
};
};
/**
* Checks if an input node supports HTML5 selection.
* If the node is not an input element, returns false.
* @param {Node} node The node to check.
* @return {boolean} True if HTML5 selection supported.
*/
cvox.DomUtil.doesInputSupportSelection = function(node) {
return goog.isDef(node) &&
node.tagName == 'INPUT' &&
node.type != 'email' &&
node.type != 'number';
};
/**
* Gets the hint text for a given element.
* @param {Node} node The target node.
* @return {string} The hint text.
*/
cvox.DomUtil.getHint = function(node) {
var desc = '';
if (node.hasAttribute) {
if (node.hasAttribute('aria-describedby')) {
var describedByIds = node.getAttribute('aria-describedby').split(' ');
for (var describedById, i = 0; describedById = describedByIds[i]; i++) {
var describedNode = document.getElementById(describedById);
if (describedNode) {
desc += ' ' + cvox.DomUtil.getName(
describedNode, true, true, true);
}
}
}
}
return desc;
};
|