Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1875x 1875x 1875x 1875x 1875x 1875x 1875x 1875x 1815x 1875x 2x 2x 2x 2x 2x 2x 2x 2x 1875x 555x 555x 2x 2x 2x 2x 2x 9432x 9432x 9432x 2x 2x 2x 2x 13058x 13058x 13058x 2x 2x 2x 2x 3500x 3500x 3500x 2x 2x 2x 2x 2x 2x 17448x 17448x 17448x 2x 2x 2x 2x 2x 18649x 18649x 1066x 1066x 9x 9x 1066x 18640x 18640x 18649x 53x 2873x 18649x 18649x 2x 2x 2x 2x 580x 580x 580x 1x 1x 579x 579x 580x 3x 3x 3x 3x 3x 576x 576x 2x 2x 2x 2x 2x 2x 1695x 855x 855x 2x 2x 2x 2x 2x 2x 2x 1695x 555x 555x 2x 2x 2x 2x 2x 1860x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1050x 390x 390x 1050x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1050x 615x 615x 1050x 2x 2x 2x 2x 2x 2x 2x 4213424x 4213424x 4213424x 39042x 39561x 39561x 39561x 7099x 7099x 1872x 39042x 39042x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 41944x 41944x 41944x 41944x 8140x 8140x 33804x 38539x 33772x 1662447x 33772x 41944x 13808x 13808x 19996x 19996x 494664x 19996x 41875x 1791x 1791x 18205x 18205x 728893x 18205x 41859x 1116x 1116x 17089x 17089x 2x 2x 2x 2x 2x 2x 26926x 26926x 26926x 2x 2x 2x 2x 2x 2x 14771x 14771x 14771x 2x 2x 2x 2x 2x 2x 247x 247x 247x 2x 2x 2x 2x 2x 2x 579x 579x 39772x 39772x 634x 102x 102x 102x 102x 11x 634x 39772x 533x 553x 553x 112x 113x 22x 22x 113x 90x 553x 511x 39772x 557x 557x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 19x 19x 1x 19x 3x 3x 15x 19x 5x 5x 19x 6x 6x 19x 10x 19x 5x 3x 3x 5x 3x 5x 2x 2x 5x 19x 2x 2x 13x 13x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 524x 524x 524x 286x 514x 278x 524x 10x 10x 268x 268x 2x 2x 7x 7x 7x 6x 7x 6x 6x 2x 2x 2x 2x 2x 8460x 8460x 7x 8460x 524x 8453x 7929x 7929x 8460x 2x 2x 2x 2x 2x 2x 2x 587x 587x 588x 588x 8x 8x 580x 580x 579x 579x 2x 2x 2x 2x 2x 2x 2x 502x 502x 502x 502x 502x 442x 502x 2x 502x 4x 440x 3x 436x 433x 433x 36x 36x 4x 4x 4x 4x 4x 4x 433x 22x 22x 22x 22x 22x 22x 22x 5x 5x 5x 5x 5x 5x 397x 2x 2x 502x 2x 2x 2x 2x 2x 2x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 2x 2x 2x 2x 26181x 26181x 26181x 24129x 477x 477x 2x 2x 2x 2x 23273x 23273x 23273x 23222x 23222x 2x 2x 2x 2x 2x 8467x 8467x 8467x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8467x 6164x 200x 6164x 964x 5964x 3858x 377x 3858x 3481x 3481x 3481x 21x 21x 3481x 5000x 1142x 636x 1142x 20x 20x 6164x 8461x 8467x 6164x 3858x 3858x 3858x 6164x 503x 1x 1x 1x 503x 503x 503x 1x 1x 1x 503x 503x 1x 1x 503x 503x 503x 503x 503x 503x 502x 502x 502x 502x 502x 502x 502x 503x 503x 503x 503x 503x 503x 8x 503x 1x 1x 503x 3858x 3858x 6164x 581x 1x 1x 1x 581x 581x 581x 578x 580x 580x 580x 580x 24x 580x 2x 2x 2x 580x 580x 580x 580x 38x 38x 580x 38x 38x 580x 580x 580x 580x 580x 580x 580x 2x 2x 580x 580x 580x 580x 580x 579x 580x 557x 557x 555x 555x 555x 555x 555x 4x 4x 4x 4x 4x 4x 4x 4x 4x 555x 557x 580x 580x 580x 580x 580x 580x 580x 580x 169x 580x 160x 22x 160x 160x 22x 22x 160x 580x 580x 580x 580x 60x 580x 46x 46x 580x 580x 580x 580x 580x 64x 64x 64x 580x 48x 48x 580x 578x 581x 3858x 3858x 6164x 1x 1x 3858x 3858x 6164x 10x 10x 3858x 3858x 6164x 1x 1x 3858x 3858x 6164x 33x 33x 33x 1x 1x 33x 6164x 8461x 8461x 8461x 8461x 8461x 8461x 8461x 8467x 1182x 1182x 1182x 1182x 1182x 1182x 1182x 75x 1182x 70x 70x 70x 53x 53x 70x 1182x 8461x 8461x 8467x 8467x 8467x 8467x 8467x 8467x 8467x 6145x 8467x 5911x 5911x 5911x 4x 4x 5911x 8461x 8461x 8467x 2959x 2959x 2959x 2959x 2959x 2959x 1963x 1963x 1963x 179x 59x 179x 120x 120x 179x 1963x 2959x 8461x 8461x 8461x 8467x 8467x 8467x 8411x 8411x 8411x 8467x 3099x 28x 3099x 3099x 10x 10x 3099x 8461x 8461x 8461x 8467x 8467x 8467x 8467x 8467x 8467x 8467x 2920x 8467x 2896x 114x 2896x 2896x 81x 81x 2896x 8461x 8467x 3x 3x 8461x 8467x 3x 3x 8461x 8461x 8461x 8461x 8467x 83x 83x 1x 1x 1x 1x 83x 83x 83x 73x 73x 54x 5x 5x 54x 83x 10x 10x 10x 8x 8x 10x 8467x 8180x 8180x 44x 44x 17x 17x 44x 8180x 8461x 8467x 524x 524x 524x 5x 5x 5x 2x 2x 5x 524x 524x 524x 19x 19x 3x 3x 3x 3x 3x 3x 19x 524x 8461x 8467x 35x 35x 35x 15x 3x 3x 15x 35x 8461x 8467x 69x 69x 68x 68x 68x 68x 68x 68x 523x 523x 523x 522x 522x 523x 65x 523x 458x 458x 523x 68x 68x 68x 69x 69x 3x 3x 69x 8461x 8467x 10x 10x 10x 5x 5x 5x 5x 5x 5x 6x 2x 2x 1x 1x 2x 2x 6x 4x 4x 10x 8456x 8467x 7x 3x 3x 7x 8456x 8467x 9x 35x 35x 14x 9x 9x 9x 9x 9x 1x 1x 9x 8456x 8467x 9x 9x 9x 8456x 8456x 8456x 8463x 8467x 8467x 363x 8467x 7x 7x 8467x 2x 2x 2x 2x 2x 2x 8331x 2x 2x 136x 136x 2x | import { roles as roles_map, aria, elementRoles } from 'aria-query'; // @ts-expect-error package doesn't provide typings import { AXObjects, AXObjectRoles, elementAXObjects } from 'axobject-query'; import { regex_heading_tags, regex_not_whitespace, regex_starts_with_vowel, regex_whitespaces } from '../patterns.js'; import * as w from '../../warnings.js'; import fuzzymatch from '../1-parse/utils/fuzzymatch.js'; import { is_event_attribute, is_text_attribute } from '../../utils/ast.js'; import { ContentEditableBindings } from '../constants.js'; import { walk } from 'zimmerframe'; import { list } from '../../utils/string.js'; const aria_roles = roles_map.keys(); const abstract_roles = aria_roles.filter((role) => roles_map.get(role)?.abstract); const non_abstract_roles = aria_roles.filter((name) => !abstract_roles.includes(name)); const non_interactive_roles = non_abstract_roles .filter((name) => { const role = roles_map.get(name); return ( // 'toolbar' does not descend from widget, but it does support // aria-activedescendant, thus in practice we treat it as a widget. // focusable tabpanel elements are recommended if any panels in a set contain content where the first element in the panel is not focusable. // 'generic' is meant to have no semantic meaning. // 'cell' is treated as CellRole by the AXObject which is interactive, so we treat 'cell' it as interactive as well. !['toolbar', 'tabpanel', 'generic', 'cell'].includes(name) && !role?.superClass.some((classes) => classes.includes('widget')) ); }) .concat( // The `progressbar` is descended from `widget`, but in practice, its // value is always `readonly`, so we treat it as a non-interactive role. 'progressbar' ); const interactive_roles = non_abstract_roles.filter( (name) => !non_interactive_roles.includes(name) && // 'generic' is meant to have no semantic meaning. name !== 'generic' ); /** * @param {import('aria-query').ARIARoleDefinitionKey} role */ function is_non_interactive_roles(role) { return non_interactive_roles.includes(role); } /** * @param {import('aria-query').ARIARoleDefinitionKey} role */ function is_interactive_roles(role) { return interactive_roles.includes(role); } /** * @param {import('aria-query').ARIARoleDefinitionKey} role */ function is_abstract_role(role) { return abstract_roles.includes(role); } const presentation_roles = ['presentation', 'none']; /** * @param {import('aria-query').ARIARoleDefinitionKey} role */ function is_presentation_role(role) { return presentation_roles.includes(role); } /** * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map */ function is_hidden_from_screen_reader(tag_name, attribute_map) { if (tag_name === 'input') { const type = get_static_value(attribute_map.get('type')); if (type === 'hidden') { return true; } } const aria_hidden = attribute_map.get('aria-hidden'); if (!aria_hidden) return false; const aria_hidden_value = get_static_value(aria_hidden); if (aria_hidden_value === null) return true; return aria_hidden_value === true || aria_hidden_value === 'true'; } /** * @param {Map<string, import('#compiler').Attribute>} attribute_map */ function has_disabled_attribute(attribute_map) { const disabled_attr_value = get_static_value(attribute_map.get('disabled')); if (disabled_attr_value) { return true; } const aria_disabled_attr = attribute_map.get('aria-disabled'); if (aria_disabled_attr) { const aria_disabled_attr_value = get_static_value(aria_disabled_attr); if (aria_disabled_attr_value === 'true') { return true; } } return false; } /** * @type {import('aria-query').ARIARoleRelationConcept[]} */ const non_interactive_element_role_schemas = []; elementRoles.entries().forEach(([schema, roles]) => { if ([...roles].every((role) => role !== 'generic' && non_interactive_roles.includes(role))) { non_interactive_element_role_schemas.push(schema); } }); /** * @type {import('aria-query').ARIARoleRelationConcept[]} */ const interactive_element_role_schemas = []; elementRoles.entries().forEach(([schema, roles]) => { if ([...roles].every((role) => interactive_roles.includes(role))) { interactive_element_role_schemas.push(schema); } }); const interactive_ax_objects = [...AXObjects.keys()].filter( (name) => AXObjects.get(name).type === 'widget' ); const non_interactive_ax_objects = [...AXObjects.keys()].filter((name) => ['windows', 'structure'].includes(AXObjects.get(name).type) ); /** * @type {import('aria-query').ARIARoleRelationConcept[]} */ const interactive_element_ax_object_schemas = []; elementAXObjects.entries().forEach( /** * @param {any} _ */ ([schema, ax_object]) => { if ([...ax_object].every((role) => interactive_ax_objects.includes(role))) { interactive_element_ax_object_schemas.push(schema); } } ); /** * @type {import('aria-query').ARIARoleRelationConcept[]} */ const non_interactive_element_ax_object_schemas = []; elementAXObjects.entries().forEach( /** * @param {any} _ */ ([schema, ax_object]) => { if ([...ax_object].every((role) => non_interactive_ax_objects.includes(role))) { non_interactive_element_ax_object_schemas.push(schema); } } ); /** * @param {import('aria-query').ARIARoleRelationConcept} schema * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map */ function match_schema(schema, tag_name, attribute_map) { if (schema.name !== tag_name) return false; if (!schema.attributes) return true; return schema.attributes.every((schema_attribute) => { const attribute = attribute_map.get(schema_attribute.name); if (!attribute) return false; if (schema_attribute.value && schema_attribute.value !== get_static_text_value(attribute)) { return false; } return true; }); } const ElementInteractivity = /** @type {const} */ ({ Interactive: 'interactive', NonInteractive: 'non-interactive', Static: 'static' }); /** * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map * @returns {ElementInteractivity[keyof ElementInteractivity]} */ function element_interactivity(tag_name, attribute_map) { if ( interactive_element_role_schemas.some((schema) => match_schema(schema, tag_name, attribute_map)) ) { return ElementInteractivity.Interactive; } if ( tag_name !== 'header' && non_interactive_element_role_schemas.some((schema) => match_schema(schema, tag_name, attribute_map) ) ) { return ElementInteractivity.NonInteractive; } if ( interactive_element_ax_object_schemas.some((schema) => match_schema(schema, tag_name, attribute_map) ) ) { return ElementInteractivity.Interactive; } if ( non_interactive_element_ax_object_schemas.some((schema) => match_schema(schema, tag_name, attribute_map) ) ) { return ElementInteractivity.NonInteractive; } return ElementInteractivity.Static; } /** * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map * @returns {boolean} */ function is_interactive_element(tag_name, attribute_map) { return element_interactivity(tag_name, attribute_map) === ElementInteractivity.Interactive; } /** * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map * @returns {boolean} */ function is_non_interactive_element(tag_name, attribute_map) { return element_interactivity(tag_name, attribute_map) === ElementInteractivity.NonInteractive; } /** * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map * @returns {boolean} */ function is_static_element(tag_name, attribute_map) { return element_interactivity(tag_name, attribute_map) === ElementInteractivity.Static; } /** * @param {import('aria-query').ARIARoleDefinitionKey} role * @param {string} tag_name * @param {Map<string, import('#compiler').Attribute>} attribute_map */ function is_semantic_role_element(role, tag_name, attribute_map) { for (const [schema, ax_object] of elementAXObjects.entries()) { if ( schema.name === tag_name && (!schema.attributes || schema.attributes.every( /** @param {any} attr */ (attr) => attribute_map.has(attr.name) && get_static_value(attribute_map.get(attr.name)) === attr.value )) ) { for (const name of ax_object) { const roles = AXObjectRoles.get(name); if (roles) { for (const { name } of roles) { if (name === role) { return true; } } } } } } return false; } // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofilling-form-controls:-the-autocomplete-attribute const address_type_tokens = ['shipping', 'billing']; const autofill_field_name_tokens = [ '', 'on', 'off', 'name', 'honorific-prefix', 'given-name', 'additional-name', 'family-name', 'honorific-suffix', 'nickname', 'username', 'new-password', 'current-password', 'one-time-code', 'organization-title', 'organization', 'street-address', 'address-line1', 'address-line2', 'address-line3', 'address-level4', 'address-level3', 'address-level2', 'address-level1', 'country', 'country-name', 'postal-code', 'cc-name', 'cc-given-name', 'cc-additional-name', 'cc-family-name', 'cc-number', 'cc-exp', 'cc-exp-month', 'cc-exp-year', 'cc-csc', 'cc-type', 'transaction-currency', 'transaction-amount', 'language', 'bday', 'bday-day', 'bday-month', 'bday-year', 'sex', 'url', 'photo' ]; const contact_type_tokens = ['home', 'work', 'mobile', 'fax', 'pager']; const autofill_contact_field_name_tokens = [ 'tel', 'tel-country-code', 'tel-national', 'tel-area-code', 'tel-local', 'tel-local-prefix', 'tel-local-suffix', 'tel-extension', 'email', 'impp' ]; /** * @param {null | true | string} autocomplete */ function is_valid_autocomplete(autocomplete) { if (autocomplete === true) { return false; } else if (!autocomplete) { return true; // dynamic value } const tokens = autocomplete.trim().toLowerCase().split(regex_whitespaces); if (typeof tokens[0] === 'string' && tokens[0].startsWith('section-')) { tokens.shift(); } if (address_type_tokens.includes(tokens[0])) { tokens.shift(); } if (autofill_field_name_tokens.includes(tokens[0])) { tokens.shift(); } else { if (contact_type_tokens.includes(tokens[0])) { tokens.shift(); } if (autofill_contact_field_name_tokens.includes(tokens[0])) { tokens.shift(); } else { return false; } } if (tokens[0] === 'webauthn') { tokens.shift(); } return tokens.length === 0; } const aria_attributes = 'activedescendant atomic autocomplete busy checked colcount colindex colspan controls current describedby description details disabled dropeffect errormessage expanded flowto grabbed haspopup hidden invalid keyshortcuts label labelledby level live modal multiline multiselectable orientation owns placeholder posinset pressed readonly relevant required roledescription rowcount rowindex rowspan selected setsize sort valuemax valuemin valuenow valuetext'.split( ' ' ); /** @type {Record<string, string[]>} */ const a11y_required_attributes = { a: ['href'], area: ['alt', 'aria-label', 'aria-labelledby'], // html-has-lang html: ['lang'], // iframe-has-title iframe: ['title'], img: ['alt'], object: ['title', 'aria-label', 'aria-labelledby'] }; const a11y_distracting_elements = ['blink', 'marquee']; const a11y_required_content = [ // anchor-has-content 'a', // heading-has-content 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ]; const a11y_labelable = [ 'button', 'input', 'keygen', 'meter', 'output', 'progress', 'select', 'textarea' ]; const a11y_interactive_handlers = [ // Keyboard events 'keypress', 'keydown', 'keyup', // Click events 'click', 'contextmenu', 'dblclick', 'drag', 'dragend', 'dragenter', 'dragexit', 'dragleave', 'dragover', 'dragstart', 'drop', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseout', 'mouseover', 'mouseup' ]; const a11y_recommended_interactive_handlers = [ 'click', 'mousedown', 'mouseup', 'keypress', 'keydown', 'keyup' ]; const a11y_nested_implicit_semantics = new Map([ ['header', 'banner'], ['footer', 'contentinfo'] ]); const a11y_implicit_semantics = new Map([ ['a', 'link'], ['area', 'link'], ['article', 'article'], ['aside', 'complementary'], ['body', 'document'], ['button', 'button'], ['datalist', 'listbox'], ['dd', 'definition'], ['dfn', 'term'], ['dialog', 'dialog'], ['details', 'group'], ['dt', 'term'], ['fieldset', 'group'], ['figure', 'figure'], ['form', 'form'], ['h1', 'heading'], ['h2', 'heading'], ['h3', 'heading'], ['h4', 'heading'], ['h5', 'heading'], ['h6', 'heading'], ['hr', 'separator'], ['img', 'img'], ['li', 'listitem'], ['link', 'link'], ['main', 'main'], ['menu', 'list'], ['meter', 'progressbar'], ['nav', 'navigation'], ['ol', 'list'], ['option', 'option'], ['optgroup', 'group'], ['output', 'status'], ['progress', 'progressbar'], ['section', 'region'], ['summary', 'button'], ['table', 'table'], ['tbody', 'rowgroup'], ['textarea', 'textbox'], ['tfoot', 'rowgroup'], ['thead', 'rowgroup'], ['tr', 'row'], ['ul', 'list'] ]); const menuitem_type_to_implicit_role = new Map([ ['command', 'menuitem'], ['checkbox', 'menuitemcheckbox'], ['radio', 'menuitemradio'] ]); const input_type_to_implicit_role = new Map([ ['button', 'button'], ['image', 'button'], ['reset', 'button'], ['submit', 'button'], ['checkbox', 'checkbox'], ['radio', 'radio'], ['range', 'slider'], ['number', 'spinbutton'], ['email', 'textbox'], ['search', 'searchbox'], ['tel', 'textbox'], ['text', 'textbox'], ['url', 'textbox'] ]); /** * Exceptions to the rule which follows common A11y conventions * TODO make this configurable by the user * @type {Record<string, string[]>} */ const a11y_non_interactive_element_to_interactive_role_exceptions = { ul: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'], ol: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'], li: ['menuitem', 'option', 'row', 'tab', 'treeitem'], table: ['grid'], td: ['gridcell'], fieldset: ['radiogroup', 'presentation'] }; const combobox_if_list = ['email', 'search', 'tel', 'text', 'url']; /** @param {Map<string, import('#compiler').Attribute>} attribute_map */ function input_implicit_role(attribute_map) { const type_attribute = attribute_map.get('type'); if (!type_attribute) return; const type = get_static_text_value(type_attribute); if (!type) return; const list_attribute_exists = attribute_map.has('list'); if (list_attribute_exists && combobox_if_list.includes(type)) { return 'combobox'; } return input_type_to_implicit_role.get(type); } /** @param {Map<string, import('#compiler').Attribute>} attribute_map */ function menuitem_implicit_role(attribute_map) { const type_attribute = attribute_map.get('type'); if (!type_attribute) return; const type = get_static_text_value(type_attribute); if (!type) return; return menuitem_type_to_implicit_role.get(type); } /** * @param {string} name * @param {Map<string, import('#compiler').Attribute>} attribute_map */ function get_implicit_role(name, attribute_map) { if (name === 'menuitem') { return menuitem_implicit_role(attribute_map); } else if (name === 'input') { return input_implicit_role(attribute_map); } else { return a11y_implicit_semantics.get(name); } } const invisible_elements = ['meta', 'html', 'script', 'style']; /** * @param {import('#compiler').SvelteNode | null} parent * @param {string[]} elements */ function is_parent(parent, elements) { while (parent) { if (parent.type === 'SvelteElement') return true; // unknown, play it safe, so we don't warn if (parent.type === 'RegularElement') { return elements.includes(parent.name); } parent = /** @type {import('#compiler').TemplateNode} */ (parent).parent; } return false; } /** * @param {import('#compiler').Attribute} attribute * @param {import('aria-query').ARIAProperty} name * @param {import('aria-query').ARIAPropertyDefinition} schema * @param {string | true | null} value */ function validate_aria_attribute_value(attribute, name, schema, value) { const type = schema.type; if (value === null) return; if (value === true) value = ''; if (type === 'boolean' && value !== 'true' && value !== 'false') { w.a11y_incorrect_aria_attribute_type_boolean(attribute, name); } else if (type === 'integer' && (value === '' || !Number.isInteger(+value))) { w.a11y_incorrect_aria_attribute_type_integer(attribute, name); } else if (type === 'number' && (value === '' || isNaN(+value))) { w.a11y_incorrect_aria_attribute_type(attribute, name, 'number'); } else if ((type === 'string' || type === 'id') && value === '') { w.a11y_incorrect_aria_attribute_type(attribute, name, 'non-empty string'); } else if (type === 'idlist' && value === '') { w.a11y_incorrect_aria_attribute_type_idlist(attribute, name); } else if (type === 'token') { const values = (schema.values ?? []).map((value) => value.toString()); if (!values.includes(value.toLowerCase())) { w.a11y_incorrect_aria_attribute_type_token( attribute, name, list(values.map((v) => `"${v}"`)) ); } } else if (type === 'tokenlist') { const values = (schema.values ?? []).map((value) => value.toString()); if ( value .toLowerCase() .split(regex_whitespaces) .some((value) => !values.includes(value)) ) { w.a11y_incorrect_aria_attribute_type_tokenlist( attribute, name, list(values.map((v) => `"${v}"`)) ); } } else if (type === 'tristate' && value !== 'true' && value !== 'false' && value !== 'mixed') { w.a11y_incorrect_aria_attribute_type_tristate(attribute, name); } } /** * @param {import('#compiler').RegularElement |import('#compiler').SvelteElement} node * @param {string[]} attributes * @param {string} name */ function warn_missing_attribute(node, attributes, name = node.name) { const article = regex_starts_with_vowel.test(attributes[0]) || attributes[0] === 'href' ? 'an' : 'a'; const sequence = attributes.length > 1 ? attributes.slice(0, -1).join(', ') + ` or ${attributes[attributes.length - 1]}` : attributes[0]; w.a11y_missing_attribute(node, name, article, sequence); } /** * @param {import('#compiler').Attribute | undefined} attribute */ function get_static_value(attribute) { if (!attribute) return null; if (attribute.value === true) return true; if (is_text_attribute(attribute)) return attribute.value[0].data; return null; } /** * @param {import('#compiler').Attribute | undefined} attribute */ function get_static_text_value(attribute) { const value = get_static_value(attribute); if (value === true) return null; return value; } /** * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} node * @param {import('./types.js').AnalysisState} state */ function check_element(node, state) { // foreign namespace means elements can have completely different meanings, therefore we don't check them if (state.options.namespace === 'foreign') return; /** @type {Map<string, import('#compiler').Attribute>} */ const attribute_map = new Map(); /** @type {Set<string>} */ const handlers = new Set(); /** @type {import('#compiler').Attribute[]} */ const attributes = []; const is_dynamic_element = node.type === 'SvelteElement'; let has_spread = false; let has_contenteditable_attr = false; let has_contenteditable_binding = false; for (const attribute of node.attributes) { if (attribute.type === 'SpreadAttribute') { has_spread = true; } else if (attribute.type === 'OnDirective') { handlers.add(attribute.name); } else if (attribute.type === 'Attribute') { if (is_event_attribute(attribute)) { handlers.add(attribute.name.slice(2)); } else { attributes.push(attribute); attribute_map.set(attribute.name, attribute); if (attribute.name === 'contenteditable') { has_contenteditable_attr = true; } } } else if ( attribute.type === 'BindDirective' && ContentEditableBindings.includes(attribute.name) ) { has_contenteditable_binding = true; } } for (const attribute of node.attributes) { if (attribute.type !== 'Attribute') continue; const name = attribute.name.toLowerCase(); // aria-props if (name.startsWith('aria-')) { if (invisible_elements.includes(node.name)) { // aria-unsupported-elements w.a11y_aria_attributes(attribute, node.name); } const type = name.slice(5); if (!aria_attributes.includes(type)) { const match = fuzzymatch(type, aria_attributes); w.a11y_unknown_aria_attribute(attribute, type, match); } if (name === 'aria-hidden' && regex_heading_tags.test(node.name)) { w.a11y_hidden(attribute, node.name); } // aria-proptypes let value = get_static_value(attribute); const schema = aria.get(/** @type {import('aria-query').ARIAProperty} */ (name)); if (schema !== undefined) { validate_aria_attribute_value( attribute, /** @type {import('aria-query').ARIAProperty} */ (name), schema, value ); } // aria-activedescendant-has-tabindex if ( name === 'aria-activedescendant' && !is_dynamic_element && !is_interactive_element(node.name, attribute_map) && !attribute_map.has('tabindex') ) { w.a11y_aria_activedescendant_has_tabindex(attribute); } } // aria-role if (name === 'role') { if (invisible_elements.includes(node.name)) { // aria-unsupported-elements w.a11y_misplaced_role(attribute, node.name); } const value = get_static_value(attribute); if (typeof value === 'string') { for (const c_r of value.split(regex_whitespaces)) { const current_role = /** @type {import('aria-query').ARIARoleDefinitionKey} current_role */ (c_r); if (current_role && is_abstract_role(current_role)) { w.a11y_no_abstract_role(attribute, current_role); } else if (current_role && !aria_roles.includes(current_role)) { const match = fuzzymatch(current_role, aria_roles); w.a11y_unknown_role(attribute, current_role, match); } // no-redundant-roles if ( current_role === get_implicit_role(node.name, attribute_map) && // <ul role="list"> is ok because CSS list-style:none removes the semantics and this is a way to bring them back !['ul', 'ol', 'li'].includes(node.name) ) { w.a11y_no_redundant_roles(attribute, current_role); } // Footers and headers are special cases, and should not have redundant roles unless they are the children of sections or articles. const is_parent_section_or_article = is_parent(node.parent, ['section', 'article']); if (!is_parent_section_or_article) { const has_nested_redundant_role = current_role === a11y_nested_implicit_semantics.get(node.name); if (has_nested_redundant_role) { w.a11y_no_redundant_roles(attribute, current_role); } } // role-has-required-aria-props if ( !is_dynamic_element && !is_semantic_role_element(current_role, node.name, attribute_map) ) { const role = roles_map.get(current_role); if (role) { const required_role_props = Object.keys(role.requiredProps); const has_missing_props = required_role_props.some( (prop) => !attributes.find((a) => a.name === prop) ); if (has_missing_props) { w.a11y_role_has_required_aria_props( attribute, current_role, list( required_role_props.map((v) => `"${v}"`), 'and' ) ); } } } // interactive-supports-focus if ( !has_disabled_attribute(attribute_map) && !is_hidden_from_screen_reader(node.name, attribute_map) && !is_presentation_role(current_role) && is_interactive_roles(current_role) && is_static_element(node.name, attribute_map) && !attribute_map.get('tabindex') ) { const has_interactive_handlers = [...handlers].some((handler) => a11y_interactive_handlers.includes(handler) ); if (has_interactive_handlers) { w.a11y_interactive_supports_focus(node, current_role); } } // no-interactive-element-to-noninteractive-role if ( is_interactive_element(node.name, attribute_map) && (is_non_interactive_roles(current_role) || is_presentation_role(current_role)) ) { w.a11y_no_interactive_element_to_noninteractive_role(node, node.name, current_role); } // no-noninteractive-element-to-interactive-role if ( is_non_interactive_element(node.name, attribute_map) && is_interactive_roles(current_role) && !a11y_non_interactive_element_to_interactive_role_exceptions[node.name]?.includes( current_role ) ) { w.a11y_no_noninteractive_element_to_interactive_role(node, node.name, current_role); } } } } // no-access-key if (name === 'accesskey') { w.a11y_accesskey(attribute); } // no-autofocus if (name === 'autofocus') { w.a11y_autofocus(attribute); } // scope if (name === 'scope' && !is_dynamic_element && node.name !== 'th') { w.a11y_misplaced_scope(attribute); } // tabindex-no-positive if (name === 'tabindex') { const value = get_static_value(attribute); // @ts-ignore todo is tabindex=true correct case? if (!isNaN(value) && +value > 0) { w.a11y_positive_tabindex(attribute); } } } const role = attribute_map.get('role'); const role_static_value = /** @type {import('aria-query').ARIARoleDefinitionKey} */ ( get_static_text_value(role) ); // click-events-have-key-events if (handlers.has('click')) { const is_non_presentation_role = role_static_value !== null && !is_presentation_role(role_static_value); if ( !is_dynamic_element && !is_hidden_from_screen_reader(node.name, attribute_map) && (!role || is_non_presentation_role) && !is_interactive_element(node.name, attribute_map) && !has_spread ) { const has_key_event = handlers.has('keydown') || handlers.has('keyup') || handlers.has('keypress'); if (!has_key_event) { w.a11y_click_events_have_key_events(node); } } } const role_value = /** @type {import('aria-query').ARIARoleDefinitionKey} */ ( role ? role_static_value : get_implicit_role(node.name, attribute_map) ); // no-noninteractive-tabindex if ( !is_dynamic_element && !is_interactive_element(node.name, attribute_map) && !is_interactive_roles(role_static_value) ) { const tab_index = attribute_map.get('tabindex'); const tab_index_value = get_static_text_value(tab_index); if (tab_index && (tab_index_value === null || Number(tab_index_value) >= 0)) { w.a11y_no_noninteractive_tabindex(node); } } // role-supports-aria-props if (typeof role_value === 'string' && roles_map.has(role_value)) { const { props } = /** @type {import('aria-query').ARIARoleDefinition} */ ( roles_map.get(role_value) ); const invalid_aria_props = aria.keys().filter((attribute) => !(attribute in props)); const is_implicit = role_value && role === undefined; for (const attr of attributes) { if ( invalid_aria_props.includes(/** @type {import('aria-query').ARIAProperty} */ (attr.name)) ) { if (is_implicit) { w.a11y_role_supports_aria_props_implicit(attr, attr.name, role_value, node.name); } else { w.a11y_role_supports_aria_props(attr, attr.name, role_value); } } } } // no-noninteractive-element-interactions if ( !has_contenteditable_attr && !is_hidden_from_screen_reader(node.name, attribute_map) && !is_presentation_role(role_static_value) && ((!is_interactive_element(node.name, attribute_map) && is_non_interactive_roles(role_static_value)) || (is_non_interactive_element(node.name, attribute_map) && !role)) ) { const has_interactive_handlers = [...handlers].some((handler) => a11y_recommended_interactive_handlers.includes(handler) ); if (has_interactive_handlers) { w.a11y_no_noninteractive_element_interactions(node, node.name); } } // no-static-element-interactions if ( (!role || role_static_value !== null) && !is_hidden_from_screen_reader(node.name, attribute_map) && !is_presentation_role(role_static_value) && !is_interactive_element(node.name, attribute_map) && !is_interactive_roles(role_static_value) && !is_non_interactive_element(node.name, attribute_map) && !is_non_interactive_roles(role_static_value) && !is_abstract_role(role_static_value) ) { const interactive_handlers = [...handlers].filter((handler) => a11y_interactive_handlers.includes(handler) ); if (interactive_handlers.length > 0) { w.a11y_no_static_element_interactions(node, node.name, list(interactive_handlers)); } } if (handlers.has('mouseover') && !handlers.has('focus')) { w.a11y_mouse_events_have_key_events(node, 'mouseover', 'focus'); } if (handlers.has('mouseout') && !handlers.has('blur')) { w.a11y_mouse_events_have_key_events(node, 'mouseout', 'blur'); } // element-specific checks let contains_a11y_label = false; if (node.name === 'a') { const aria_label_attribute = attribute_map.get('aria-label'); if (aria_label_attribute) { if (get_static_value(aria_label_attribute) !== '') { contains_a11y_label = true; } } const href = attribute_map.get('href') || attribute_map.get('xlink:href'); if (href) { const href_value = get_static_text_value(href); if (href_value !== null) { if (href_value === '' || href_value === '#' || /^\W*javascript:/i.test(href_value)) { w.a11y_invalid_attribute(href, href_value, href.name); } } } else if (!has_spread) { const id_attribute = get_static_value(attribute_map.get('id')); const name_attribute = get_static_value(attribute_map.get('name')); if (!id_attribute && !name_attribute) { warn_missing_attribute(node, ['href']); } } } else if (!has_spread) { const required_attributes = a11y_required_attributes[node.name]; if (required_attributes) { const has_attribute = required_attributes.some((name) => attribute_map.has(name)); if (!has_attribute) { warn_missing_attribute(node, required_attributes); } } } if (node.name === 'input') { const type = attribute_map.get('type'); const type_value = get_static_text_value(type); if (type_value === 'image' && !has_spread) { const required_attributes = ['alt', 'aria-label', 'aria-labelledby']; const has_attribute = required_attributes.some((name) => attribute_map.has(name)); if (!has_attribute) { warn_missing_attribute(node, required_attributes, 'input type="image"'); } } // autocomplete-valid const autocomplete = attribute_map.get('autocomplete'); if (type && autocomplete) { const autocomplete_value = get_static_value(autocomplete); if (!is_valid_autocomplete(autocomplete_value)) { w.a11y_autocomplete_valid( autocomplete, /** @type {string} */ (autocomplete_value), type_value ?? '...' ); } } } if (node.name === 'img') { const alt_attribute = get_static_text_value(attribute_map.get('alt')); const aria_hidden = get_static_value(attribute_map.get('aria-hidden')); if (alt_attribute && !aria_hidden) { if (/\b(image|picture|photo)\b/i.test(alt_attribute)) { w.a11y_img_redundant_alt(node); } } } if (node.name === 'label') { /** @param {import('#compiler').TemplateNode} node */ const has_input_child = (node) => { let has = false; walk( node, {}, { _(node, { next }) { if ( node.type === 'SvelteElement' || node.type === 'SlotElement' || (node.type === 'RegularElement' && (a11y_labelable.includes(node.name) || node.name === 'slot')) ) { has = true; } else { next(); } } } ); return has; }; if (!attribute_map.has('for') && !has_input_child(node)) { w.a11y_label_has_associated_control(node); } } if (node.name === 'video') { const aria_hidden_attribute = attribute_map.get('aria-hidden'); const aria_hidden_exist = aria_hidden_attribute && get_static_value(aria_hidden_attribute); if (attribute_map.has('muted') || aria_hidden_exist === 'true') { return; } let has_caption = false; const track = /** @type {import('#compiler').RegularElement | undefined} */ ( node.fragment.nodes.find((i) => i.type === 'RegularElement' && i.name === 'track') ); if (track) { has_caption = track.attributes.some( (a) => a.type === 'SpreadAttribute' || (a.type === 'Attribute' && a.name === 'kind' && get_static_value(a) === 'captions') ); } if (!has_caption) { w.a11y_media_has_caption(node); } } if (node.name === 'figcaption') { if (!is_parent(node.parent, ['figure'])) { w.a11y_figcaption_parent(node); } } if (node.name === 'figure') { const children = node.fragment.nodes.filter((node) => { if (node.type === 'Comment') return false; if (node.type === 'Text') return regex_not_whitespace.test(node.data); return true; }); const index = children.findIndex( (child) => child.type === 'RegularElement' && child.name === 'figcaption' ); if (index !== -1 && index !== 0 && index !== children.length - 1) { w.a11y_figcaption_index(children[index]); } } if (a11y_distracting_elements.includes(node.name)) { // no-distracting-elements w.a11y_distracting_elements(node, node.name); } // Check content if ( !contains_a11y_label && !has_contenteditable_binding && a11y_required_content.includes(node.name) && node.fragment.nodes.length === 0 ) { w.a11y_missing_content(node, node.name); } } /** * @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} */ export const a11y_validators = { RegularElement(node, context) { check_element(node, context.state); }, SvelteElement(node, context) { check_element(node, context.state); } }; |