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
|
import * as DomUtils from "domutils";
import * as CSSselect from "../src";
import type { Element, AnyNode, ParentNode, ChildNode } from "domhandler";
import { q, t, createWithFriesXML, loadDoc } from "./tools/sizzle-testinit";
import { parseDocument, parseDOM } from "htmlparser2";
let document = loadDoc();
describe("Sizzle", () => {
beforeEach(() => {
document = loadDoc();
});
it("element", () => {
expect.assertions(37);
// Empty selector returns an empty array
expect(CSSselect.selectAll("", document)).toHaveLength(0);
// Text element as context fails silently
expect(
CSSselect.selectAll("div", document.createTextNode(""))
).toStrictEqual([]);
const form = document.getElementById("form");
// Empty string passed to matchesSelector does not match
expect(CSSselect.is(form, "")).toBe(false);
// Empty selector returns an empty array
expect(CSSselect.selectAll(" ", document)).toHaveLength(0);
// Empty selector returns an empty array
expect(CSSselect.selectAll("\t", document)).toHaveLength(0);
// Select all
expect(CSSselect.selectAll("*", document).length >= 30).toBe(true);
const all = CSSselect.selectAll("*", document);
const good = all.every((el: AnyNode) => el.nodeType !== 8);
// Select all elements, no comment nodes
expect(good).toBe(true);
// Element Selector
t("html", ["html"]);
// Element Selector
t("body", ["body"]);
// Element Selector
t("#qunit-fixture p", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Leading space
t(" #qunit-fixture p", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Leading tab
t("\t#qunit-fixture p", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Leading carriage return
t("\r#qunit-fixture p", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Leading line feed
t("\n#qunit-fixture p", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Leading form feed
t("\f#qunit-fixture p", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Trailing space
t("#qunit-fixture p ", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Trailing tab
t("#qunit-fixture p\t", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Trailing carriage return
t("#qunit-fixture p\r", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Trailing line feed
t("#qunit-fixture p\n", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Trailing form feed
t("#qunit-fixture p\f", ["firstp", "ap", "sndp", "en", "sap", "first"]);
// Parent Element
t("dl ol", ["empty", "listWithTabIndex"]);
// Parent Element (non-space descendant combinator)
t("dl\tol", ["empty", "listWithTabIndex"]);
const obj1 = document.getElementById("object1");
// Object/param as context
expect(CSSselect.selectAll("param", obj1)).toHaveLength(2);
// Finding selects with a context.
t(
"select",
["select1", "select2", "select3", "select4", "select5"],
form
);
/*
* Check for unique-ness and sort order
* Check for duplicates: p, div p
*/
expect(CSSselect.selectAll("p, div p", document)).toStrictEqual(
CSSselect.selectAll("p", document)
);
// Checking sort order
t("h2, h1", ["qunit-header", "qunit-banner", "qunit-userAgent"]);
// Checking sort order
t("#qunit-fixture p, #qunit-fixture p a", [
"firstp",
"simon1",
"ap",
"google",
"groups",
"anchor1",
"mark",
"sndp",
"en",
"yahoo",
"sap",
"anchor2",
"simon",
"first",
]);
// Test Conflict ID
const lengthtest = document.getElementById("lengthtest");
// Finding element with id of ID.
t("#idTest", ["idTest"], lengthtest);
// Finding element with id of ID.
t("[name='id']", ["idTest"], lengthtest);
// Finding elements with id of ID.
t("input[id='idTest']", ["idTest"], lengthtest);
const siblingTest = document.getElementById("siblingTest");
// Element-rooted QSA does not select based on document context
t("div em", [], siblingTest);
// Element-rooted QSA does not select based on document context
t("div em, div em, div em:not(div em)", [], siblingTest);
// Escaped commas do not get treated with an id in element-rooted QSA
t("div em, em\\,", [], siblingTest);
const iframe = document.getElementById("iframe");
iframe.children = parseDOM("<body><p id='foo'>bar</p></body>");
iframe.children.forEach((e) => {
e.parent = iframe;
});
// Other document as context
expect(CSSselect.selectAll("p:contains(bar)", iframe)).toStrictEqual([
DomUtils.getElementById("foo", iframe.children),
]);
iframe.children = [];
let markup = "";
for (let i = 0; i < 100; i++) {
markup = `<div>${markup}</div>`;
}
const [html] = parseDOM(markup);
DomUtils.appendChild(document.body, html);
// No stack or performance problems with large amounts of descendents
expect(
CSSselect.selectAll("body div div div", document).length
).toBeTruthy();
// No stack or performance problems with large amounts of descendents
expect(
CSSselect.selectAll("body>div div div", document).length
).toBeTruthy();
DomUtils.removeElement(html);
// Real use case would be using .watch in browsers with window.watch (see Issue #157)
const elem = document.createElement("toString");
elem.attribs["id"] = "toString";
DomUtils.appendChild(q("qunit-fixture")[0], elem);
// Element name matches Object.prototype property
t("tostring#toString", ["toString"]);
DomUtils.removeElement(elem);
});
it("XML Document Selectors", () => {
let xml = createWithFriesXML();
expect.assertions(11);
// Element Selector with underscore
expect(CSSselect.selectAll("foo_bar", xml)).toHaveLength(1);
// Class selector
expect(CSSselect.selectAll(".component", xml)).toHaveLength(1);
// Attribute selector for class
expect(CSSselect.selectAll("[class*=component]", xml)).toHaveLength(1);
// Attribute selector with name
expect(CSSselect.selectAll("property[name=prop2]", xml)).toHaveLength(
1
);
// Attribute selector with name
expect(CSSselect.selectAll("[name=prop2]", xml)).toHaveLength(1);
// Attribute selector with ID
expect(CSSselect.selectAll("#seite1", xml)).toHaveLength(1);
// Attribute selector with ID
expect(CSSselect.selectAll("component#seite1", xml)).toHaveLength(1);
// Attribute selector filter with ID
expect(
CSSselect.selectAll("component", xml).filter((node) =>
CSSselect.is(node, "#seite1")
)
).toHaveLength(1);
// Descendent selector and dir caching
expect(CSSselect.selectAll("meta property thing", xml)).toHaveLength(2);
// Check for namespaced element
expect(
CSSselect.is(
xml.children.filter((t) => t.type === "tag").pop(),
"soap\\:Envelope",
{
xmlMode: true,
}
)
).toBe(true);
xml = parseDocument(
"<?xml version='1.0' encoding='UTF-8'?><root><elem id='1'/></root>",
{
xmlMode: true,
}
);
// Non-qSA path correctly handles numeric ids (jQuery #14142)
expect(CSSselect.selectAll("elem:not(:has(*))", xml)).toHaveLength(1);
});
it("broken", () => {
expect.assertions(26);
const broken = (selector: string) =>
expect(() => CSSselect.compile(selector)).toThrow(Error);
broken("[");
broken("(");
broken("{");
broken("()");
broken("<>");
broken("{}");
broken(",");
broken(",a");
broken("a,");
// Hangs on IE 9 if regular expression is inefficient
broken("[id=012345678901234567890123456789");
// Doesn't exist
broken(":visble");
broken(":nth-child");
/*
* Sigh again. IE 9 thinks this is also a real selector
* Not super critical that we fix this case
*/
broken(":nth-child(-)");
/*
* Sigh. WebKit thinks this is a real selector in qSA
* They've already fixed this and it'll be coming into
* Current browsers soon. Currently, Safari 5.0 still has this problem
*/
broken(":nth-child(asdf)");
broken(":nth-child(2n+-0)");
broken(":nth-child(2+0)");
broken(":nth-child(- 1n)");
broken(":nth-child(-1 n)");
broken(":first-child(n)");
broken(":last-child(n)");
broken(":only-child(n)");
broken(":nth-last-last-child(1)");
broken(":first-last-child");
broken(":last-last-child");
broken(":only-last-child");
// Make sure attribute value quoting works correctly. See: #6093
parseDOM(
"<input type='hidden' value='2' name='foo.baz' id='attrbad1'/><input type='hidden' value='2' name='foo[baz]' id='attrbad2'/>"
).forEach((node) =>
DomUtils.appendChild(document.getElementById("form"), node)
);
// Shouldn't be matching those inner brackets
broken("input[name=foo[baz]]");
});
it("id", () => {
expect.assertions(34);
// ID Selector
t("#body", ["body"]);
// ID Selector w/ Element
t("body#body", ["body"]);
// ID Selector w/ Element
t("ul#first", []);
// ID selector with existing ID descendant
t("#firstp #simon1", ["simon1"]);
// ID selector with non-existant descendant
t("#firstp #foobar", []);
// ID selector using UTF8
t("#台北Táiběi", ["台北Táiběi"]);
// Multiple ID selectors using UTF8
t("#台北Táiběi, #台北", ["台北Táiběi", "台北"]);
// Descendant ID selector using UTF8
t("div #台北", ["台北"]);
// Child ID selector using UTF8
t("form > #台北", ["台北"]);
// Escaped ID
t("#foo\\:bar", ["foo:bar"]);
// Escaped ID with descendent
t("#foo\\:bar span:not(:input)", ["foo_descendent"]);
// Escaped ID
t("#test\\.foo\\[5\\]bar", ["test.foo[5]bar"]);
// Descendant escaped ID
t("div #foo\\:bar", ["foo:bar"]);
// Descendant escaped ID
t("div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"]);
// Child escaped ID
t("form > #foo\\:bar", ["foo:bar"]);
// Child escaped ID
t("form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"]);
const [fiddle] = parseDOM(
"<div id='fiddle\\Foo'><span id='fiddleSpan'></span></div>"
);
DomUtils.appendChild(document.getElementById("qunit-fixture"), fiddle);
// Escaped ID as context
t(
"> span",
["fiddleSpan"],
CSSselect.selectOne("#fiddle\\\\Foo", document)
);
DomUtils.removeElement(fiddle);
// ID Selector, child ID present
t("#form > #radio1", ["radio1"]); // Bug #267
// ID Selector, not an ancestor ID
t("#form #first", []);
// ID Selector, not a child ID
t("#form > #option1a", []);
// All Children of ID
t("#foo > *", ["sndp", "en", "sap"]);
// All Children of ID with no children
t("#firstUL > *", []);
// ID selector with same value for a name attribute
expect(
(
CSSselect.selectOne<AnyNode, ParentNode>(
"#tName1",
document
) as Element
).attribs["id"]
).toBe("tName1");
// ID selector non-existing but name attribute on an A tag
t("#tName2", []);
// Leading ID selector non-existing but name attribute on an A tag
t("#tName2 span", []);
// Leading ID selector existing, retrieving the child
t("#tName1 span", ["tName1-span"]);
// Ending with ID
expect(
(
CSSselect.selectOne<AnyNode, ParentNode>(
"div > div #tName1",
document
) as Element
).attribs["id"]
).toBe(
(CSSselect.selectOne("#tName1-span", document)?.parent as Element)
.attribs["id"]
);
parseDOM("<a id='backslash\\foo'></a>").forEach((node) =>
DomUtils.appendChild(document.getElementById("form"), node)
);
// ID Selector contains backslash
t("#backslash\\\\foo", ["backslash\\foo"]);
// ID Selector on Form with an input that has a name of 'id'
t("#lengthtest", ["lengthtest"]);
// ID selector with non-existant ancestor
t("#asdfasdf #foobar", []); // Bug #986
// ID selector within the context of another element
t("div#form", [], document.body);
// Underscore ID
t("#types_all", ["types_all"]);
// Dash ID
t("#qunit-fixture", ["qunit-fixture"]);
// ID with weird characters in it
t("#name\\+value", ["name+value"]);
});
it("class", () => {
expect.assertions(27);
// Class Selector
t(".blog", ["mark", "simon"]);
// Class Selector
t(".GROUPS", ["groups"]);
// Class Selector
t(".blog.link", ["simon"]);
// Class Selector w/ Element
t("a.blog", ["mark", "simon"]);
// Parent Class Selector
t("p .blog", ["mark", "simon"]);
// Class selector using UTF8
t(".台北Táiběi", ["utf8class1"]);
// Class selector using UTF8
t(".台北", ["utf8class1", "utf8class2"]);
// Class selector using UTF8
t(".台北Táiběi.台北", ["utf8class1"]);
// Class selector using UTF8
t(".台北Táiběi, .台北", ["utf8class1", "utf8class2"]);
// Descendant class selector using UTF8
t("div .台北Táiběi", ["utf8class1"]);
// Child class selector using UTF8
t("form > .台北Táiběi", ["utf8class1"]);
// Escaped Class
t(".foo\\:bar", ["foo:bar"]);
// Escaped Class
t(".test\\.foo\\[5\\]bar", ["test.foo[5]bar"]);
// Descendant escaped Class
t("div .foo\\:bar", ["foo:bar"]);
// Descendant escaped Class
t("div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"]);
// Child escaped Class
t("form > .foo\\:bar", ["foo:bar"]);
// Child escaped Class
t("form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"]);
const div = document.createElement("div");
div.children = parseDOM(
"<div class='test e'></div><div class='test'></div>"
);
div.children.forEach((e) => {
e.parent = div;
});
// Finding a second class.
expect(CSSselect.selectAll(".e", div)).toStrictEqual([div.children[0]]);
const lastChild = div.children[div.children.length - 1] as Element;
lastChild.attribs["class"] = "e";
// Finding a modified class.
expect(CSSselect.selectAll(".e", div)).toStrictEqual([
div.children[0],
lastChild,
]);
// .null does not match an element with no class
expect(CSSselect.is(div, ".null")).toBe(false);
// .null does not match an element with no class
expect(CSSselect.is(div.children[0], ".null div")).toBe(false);
div.attribs["class"] = "null";
// .null matches element with class 'null'
expect(CSSselect.is(div, ".null")).toBe(true);
// Caching system respects DOM changes
expect(CSSselect.is(div.children[0], ".null div")).toBe(true);
// Testing class on document doesn't error
expect(CSSselect.is(document, ".foo")).toBe(false);
lastChild.attribs["class"] += " hasOwnProperty toString";
// Testing class on global object doesn't error
expect(CSSselect.is(global, ".foo")).toBe(false);
// Classes match Object.prototype properties
expect(
CSSselect.selectAll(".e.hasOwnProperty.toString", div)
).toStrictEqual([lastChild]);
const [div2] = parseDOM(
"<div><svg width='200' height='250' version='1.1' xmlns='http://www.w3.org/2000/svg'><rect x='10' y='10' width='30' height='30' class='foo'></rect></svg></div>"
);
// Class selector against SVG
expect(CSSselect.selectAll(".foo", div2)).toHaveLength(1);
});
it("name", () => {
expect.assertions(13);
// Name selector
t("input[name=action]", ["text1"]);
// Name selector with single quotes
t("input[name='action']", ["text1"]);
// Name selector with double quotes
t('input[name="action"]', ["text1"]);
// Name selector non-input
t("[name=example]", ["name-is-example"]);
// Name selector non-input
t("[name=div]", ["name-is-div"]);
// Name selector non-input
t("*[name=iframe]", ["iframe"]);
// Name selector for grouped input
t("input[name='types[]']", ["types_all", "types_anime", "types_movie"]);
const form1 = document.getElementById("form");
// Name selector within the context of another element
t("input[name=action]", ["text1"], form1);
// Name selector for grouped form element within the context of another element
t("input[name='foo[bar]']", ["hidden2"], form1);
const [form2] = parseDOM("<form><input name='id'/></form>");
DomUtils.appendChild(document.body, form2);
// Make sure that rooted queries on forms (with possible expandos) work.
expect(CSSselect.selectAll("input", form2)).toHaveLength(1);
DomUtils.removeElement(form2);
// Find elements that have similar IDs
t("[name=tName1]", ["tName1ID"]);
// Find elements that have similar IDs
t("[name=tName2]", ["tName2ID"]);
// Find elements that have similar IDs
t("#tName2ID", ["tName2ID"]);
});
it("multiple", () => {
expect.assertions(6);
// Comma Support
t("h2, #qunit-fixture p", [
"qunit-banner",
"qunit-userAgent",
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// Comma Support
t("h2 , #qunit-fixture p", [
"qunit-banner",
"qunit-userAgent",
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// Comma Support
t("h2 , #qunit-fixture p", [
"qunit-banner",
"qunit-userAgent",
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// Comma Support
t("h2,#qunit-fixture p", [
"qunit-banner",
"qunit-userAgent",
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// Comma Support
t("h2,#qunit-fixture p ", [
"qunit-banner",
"qunit-userAgent",
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// Comma Support
t("h2\t,\r#qunit-fixture p\n", [
"qunit-banner",
"qunit-userAgent",
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
});
it("child and adjacent", () => {
expect.assertions(38);
// Child
t("p > a", ["simon1", "google", "groups", "mark", "yahoo", "simon"]);
// Child
t("p> a", ["simon1", "google", "groups", "mark", "yahoo", "simon"]);
// Child
t("p >a", ["simon1", "google", "groups", "mark", "yahoo", "simon"]);
// Child
t("p>a", ["simon1", "google", "groups", "mark", "yahoo", "simon"]);
// Child w/ Class
t("p > a.blog", ["mark", "simon"]);
// All Children
t("code > *", ["anchor1", "anchor2"]);
// All Grandchildren
t("p > * > *", ["anchor1", "anchor2"]);
// Adjacent
t("#qunit-fixture a + a", ["groups", "tName2ID"]);
// Adjacent
t("#qunit-fixture a +a", ["groups", "tName2ID"]);
// Adjacent
t("#qunit-fixture a+ a", ["groups", "tName2ID"]);
// Adjacent
t("#qunit-fixture a+a", ["groups", "tName2ID"]);
// Adjacent
t("p + p", ["ap", "en", "sap"]);
// Adjacent
t("p#firstp + p", ["ap"]);
// Adjacent
t("p[lang=en] + p", ["sap"]);
// Adjacent
t("a.GROUPS + code + a", ["mark"]);
// Comma, Child, and Adjacent
t("#qunit-fixture a + a, code > a", [
"groups",
"anchor1",
"anchor2",
"tName2ID",
]);
// Element Preceded By
t("#qunit-fixture p ~ div", [
"foo",
"nothiddendiv",
"moretests",
"tabindex-tests",
"liveHandlerOrder",
"siblingTest",
]);
// Element Preceded By
t("#first ~ div", [
"moretests",
"tabindex-tests",
"liveHandlerOrder",
"siblingTest",
]);
// Element Preceded By
t("#groups ~ a", ["mark"]);
// Element Preceded By
t("#length ~ input", ["idTest"]);
// Element Preceded By
t("#siblingfirst ~ em", ["siblingnext", "siblingthird"]);
// Element Preceded By (multiple)
t("#siblingTest em ~ em ~ em ~ span", ["siblingspan"]);
// Element Preceded By, Containing
t("#liveHandlerOrder ~ div em:contains('1')", ["siblingfirst"]);
const siblingFirst = document.getElementById("siblingfirst");
// Element Preceded By with a context.
t("~ em", ["siblingnext", "siblingthird"], siblingFirst);
// Element Directly Preceded By with a context.
t("+ em", ["siblingnext"], siblingFirst);
const en = document.getElementById("en");
// Compound selector with context, beginning with sibling test.
t("+ p, a", ["yahoo", "sap"], en);
// Compound selector with context, containing sibling test.
t("a, + p", ["yahoo", "sap"], en);
// Multiple combinators selects all levels
t("#siblingTest em *", [
"siblingchild",
"siblinggrandchild",
"siblinggreatgrandchild",
]);
// Multiple combinators selects all levels
t("#siblingTest > em *", [
"siblingchild",
"siblinggrandchild",
"siblinggreatgrandchild",
]);
// Multiple sibling combinators doesn't miss general siblings
t("#siblingTest > em:first-child + em ~ span", ["siblingspan"]);
// Combinators are not skipped when mixing general and specific
t("#siblingTest > em:contains('x') + em ~ span", []);
// Parent div for next test is found via ID (#8310)
expect(CSSselect.selectAll("#listWithTabIndex", document)).toHaveLength(
1
);
// Make sure the temporary id assigned by sizzle is cleared out (#8310)
expect(CSSselect.selectAll("#__sizzle__", document)).toHaveLength(0);
// Parent div for previous test is still found via ID (#8310)
expect(CSSselect.selectAll("#listWithTabIndex", document)).toHaveLength(
1
);
// Verify deep class selector
t("div.blah > p > a", []);
// No element deep selector
t("div.foo > span > a", []);
// Non-existant ancestors
t(".fototab > .thumbnails > a", []);
// Child of scope
t(
":scope > label",
["scopeTest--child"],
CSSselect.selectOne("#scopeTest", document)
);
});
it("attributes", () => {
expect.assertions(77);
// Attribute Exists
t("#qunit-fixture a[title]", ["google"]);
// Attribute Exists (case-insensitive)
t("#qunit-fixture a[TITLE]", ["google"]);
// Attribute Exists
t("#qunit-fixture *[title]", ["google"]);
// Attribute Exists
t("#qunit-fixture [title]", ["google"]);
// Attribute Exists
t("#qunit-fixture a[ title ]", ["google"]);
// Boolean attribute exists
t("#select2 option[selected]", ["option2d"]);
// Boolean attribute equals
t("#select2 option[selected='selected']", ["option2d"]);
// Attribute Equals
t("#qunit-fixture a[rel='bookmark']", ["simon1"]);
// Attribute Equals
t("#qunit-fixture a[rel='bookmark']", ["simon1"]);
// Attribute Equals
t("#qunit-fixture a[rel=bookmark]", ["simon1"]);
// Attribute Equals
t("#qunit-fixture a[href='http://www.google.com/']", ["google"]);
// Attribute Equals
t("#qunit-fixture a[ rel = 'bookmark' ]", ["simon1"]);
// Attribute Equals Number
t("#qunit-fixture option[value=1]", [
"option1b",
"option2b",
"option3b",
"option4b",
"option5c",
]);
// Attribute Equals Number
t("#qunit-fixture li[tabIndex=-1]", ["foodWithNegativeTabIndex"]);
document.getElementById("anchor2").attribs["href"] = "#2";
// `href` Attribute
t("p a[href^=#]", ["anchor2"]);
t("p a[href*=#]", ["simon1", "anchor2"]);
// `for` Attribute
t("form label[for]", ["label-for"]);
// `for` Attribute in form
t("#form [for=action]", ["label-for"]);
// Attribute containing []
t("input[name^='foo[']", ["hidden2"]);
// Attribute containing []
t("input[name^='foo[bar]']", ["hidden2"]);
// Attribute containing []
t("input[name*='[bar]']", ["hidden2"]);
// Attribute containing []
t("input[name$='bar]']", ["hidden2"]);
// Attribute containing []
t("input[name$='[bar]']", ["hidden2"]);
// Attribute containing []
t("input[name$='foo[bar]']", ["hidden2"]);
// Attribute containing []
t("input[name*='foo[bar]']", ["hidden2"]);
// Without context, single-quoted attribute containing ','
t("input[data-comma='0,1']", ["el12087"], document);
// Without context, double-quoted attribute containing ','
t('input[data-comma="0,1"]', ["el12087"]);
// With context, single-quoted attribute containing ','
t(
"input[data-comma='0,1']",
["el12087"],
document.getElementById("t12087")
);
// With context, double-quoted attribute containing ','
t(
'input[data-comma="0,1"]',
["el12087"],
document.getElementById("t12087")
);
// Multiple Attribute Equals
t("#form input[type='radio'], #form input[type='hidden']", [
"radio1",
"radio2",
"hidden1",
]);
// Multiple Attribute Equals
t("#form input[type='radio'], #form input[type=\"hidden\"]", [
"radio1",
"radio2",
"hidden1",
]);
// Multiple Attribute Equals
t("#form input[type='radio'], #form input[type=hidden]", [
"radio1",
"radio2",
"hidden1",
]);
// Attribute selector using UTF8
t("span[lang=中文]", ["台北"]);
// Attribute Begins With
t("a[href ^= 'http://www']", ["google", "yahoo"]);
// Attribute Ends With
t("a[href $= 'org/']", ["mark"]);
// Attribute Contains
t("a[href *= 'google']", ["google", "groups"]);
// Attribute Is Not Equal
t("#ap a[hreflang!='en']", ["google", "groups", "anchor1"]);
const opt = document.getElementById("option1a");
opt.attribs["test"] = "";
// Attribute Is Not Equal Matches
expect(CSSselect.is(opt, "[id*=option1][type!=checkbox]")).toBe(true);
// Attribute With No Quotes Contains Matches
expect(CSSselect.is(opt, "[id*=option1]")).toBe(true);
// Attribute With No Quotes No Content Matches
expect(CSSselect.is(opt, "[test=]")).toBe(true);
// Attribute with empty string value does not match startsWith selector (^=)
expect(CSSselect.is(opt, "[test^='']")).toBe(false);
// Attribute With No Quotes Equals Matches
expect(CSSselect.is(opt, "[id=option1a]")).toBe(true);
// Attribute With No Quotes Href Contains Matches
expect(
CSSselect.is(document.getElementById("simon1"), "a[href*=#]")
).toBe(true);
// Empty values
t("#select1 option[value='']", ["option1a"]);
// Empty values
t("#select1 option[value!='']", ["option1b", "option1c", "option1d"]);
// Select options via :selected
t("#select1 option:selected", ["option1a"]);
// Select options via :selected
t("#select2 option:selected", ["option2d"]);
// Select options via :selected
t("#select3 option:selected", ["option3b", "option3c"]);
// Select options via :selected
t("select[name='select2'] option:selected", ["option2d"]);
// Grouped Form Elements
t("input[name='foo[bar]']", ["hidden2"]);
const input = document.getElementById("text1");
input.attribs["title"] = "Don't click me";
// Quote within attribute value does not mess up tokenizer
expect(CSSselect.is(input, 'input[title="Don\'t click me"]')).toBe(
true
);
// See jQuery #12303
input.attribs["data-pos"] = ":first";
// POS within attribute value is treated as an attribute value
expect(CSSselect.is(input, "input[data-pos=\\:first]")).toBe(true);
// POS within attribute value is treated as an attribute value
expect(CSSselect.is(input, "input[data-pos=':first']")).toBe(true);
// POS within attribute value after pseudo is treated as an attribute value
expect(CSSselect.is(input, ":input[data-pos=':first']")).toBe(true);
delete input.attribs["data-pos"];
/*
* Make sure attribute value quoting works correctly. See jQuery #6093; #6428; #13894
* Use seeded results to bypass querySelectorAll optimizations
*/
const attrbad = [
...parseDOM(
"<input type='hidden' id='attrbad_space' name='foo bar'/>" +
"<input type='hidden' id='attrbad_dot' value='2' name='foo.baz'/>" +
"<input type='hidden' id='attrbad_brackets' value='2' name='foo[baz]'/>" +
"<input type='hidden' id='attrbad_injection' data-attr='foo_baz']'/>" +
"<input type='hidden' id='attrbad_quote' data-attr='''/>" +
"<input type='hidden' id='attrbad_backslash' data-attr='\'/>" +
"<input type='hidden' id='attrbad_backslash_quote' data-attr='\''/>" +
"<input type='hidden' id='attrbad_backslash_backslash' data-attr='\\'/>" +
"<input type='hidden' id='attrbad_unicode' data-attr='一'/>",
{ decodeEntities: true }
),
] as Element[];
attrbad.forEach((attr) =>
DomUtils.appendChild(document.getElementById("qunit-fixture"), attr)
);
// Underscores don't need escaping
t("input[id=types_all]", ["types_all"]);
// Escaped space
t("input[name=foo\\ bar]", ["attrbad_space"]);
// Escaped dot
t("input[name=foo\\.baz]", ["attrbad_dot"]);
// Escaped brackets
t("input[name=foo\\[baz\\]]", ["attrbad_brackets"]);
// Escaped quote + right bracket
t("input[data-attr='foo_baz\\']']", ["attrbad_injection"]);
// Quoted quote
t("input[data-attr='\\'']", ["attrbad_quote"]);
// Quoted backslash
t("input[data-attr='\\\\']", ["attrbad_backslash"]);
// Quoted backslash quote
t("input[data-attr='\\\\\\'']", ["attrbad_backslash_quote"]);
// Quoted backslash backslash
t("input[data-attr='\\\\\\\\']", ["attrbad_backslash_backslash"]);
// Quoted backslash backslash (numeric escape)
t("input[data-attr='\\5C\\\\']", ["attrbad_backslash_backslash"]);
// Quoted backslash backslash (numeric escape with trailing space)
t("input[data-attr='\\5C \\\\']", ["attrbad_backslash_backslash"]);
// Quoted backslash backslash (numeric escape with trailing tab)
t("input[data-attr='\\5C\t\\\\']", ["attrbad_backslash_backslash"]);
// Long numeric escape (BMP)
t("input[data-attr='\\04e00']", ["attrbad_unicode"]);
document.getElementById("attrbad_unicode").attribs["data-attr"] =
"\uD834\uDF06A";
/*
* It was too much code to fix Safari 5.x Supplemental Plane crashes (see ba5f09fa404379a87370ec905ffa47f8ac40aaa3)
* Long numeric escape (non-BMP)
*/
t("input[data-attr='\\01D306A']", ["attrbad_unicode"]);
attrbad.forEach((attr) => DomUtils.removeElement(attr));
// `input[type=text]`
t("#form input[type=text]", ["text1", "text2", "hidden2", "name"]);
// `input[type=search]`
t("#form input[type=search]", ["search"]);
// `script[src]` (jQuery #13777)
t("#moretests script[src]", ["script-src"]);
// #3279
const div = document.createElement("div");
div.children = parseDOM("<div id='foo' xml:test='something'></div>");
// Finding by attribute with escaped characters.
expect(CSSselect.selectAll("[xml\\:test]", div)).toStrictEqual([
div.children[0],
]);
const foo = document.getElementById("foo");
// Object.prototype property "constructor" (negative)',
t("[constructor]", []);
// Gecko Object.prototype property "watch" (negative)',
t("[watch]", []);
foo.attribs["constructor"] = "foo";
foo.attribs["watch"] = "bar";
// Object.prototype property "constructor"',
t("[constructor='foo']", ["foo"]);
// Gecko Object.prototype property "watch"',
t("[watch='bar']", ["foo"]);
// Value attribute is retrieved correctly
t("input[value=Test]", ["text1", "text2"]);
});
it("pseudo - (parent|empty)", () => {
expect.assertions(3);
// Empty
t("ul:empty", ["firstUL"]);
// Empty with comment node
t("ol:empty", ["empty"]);
// Is A Parent
t("#qunit-fixture p:parent", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
});
it("pseudo - (first|last|only)-(child|of-type)", () => {
expect.assertions(12);
// First Child
t("p:first-child", ["firstp", "sndp"]);
// First Child (leading id)
t("#qunit-fixture p:first-child", ["firstp", "sndp"]);
// First Child (leading class)
t(".nothiddendiv div:first-child", ["nothiddendivchild"]);
// First Child (case-insensitive)
t("#qunit-fixture p:FIRST-CHILD", ["firstp", "sndp"]);
// Last Child
t("p:last-child", ["sap"]);
// Last Child (leading id)
t("#qunit-fixture a:last-child", [
"simon1",
"anchor1",
"mark",
"yahoo",
"anchor2",
"simon",
"liveLink1",
"liveLink2",
]);
// Only Child
t("#qunit-fixture a:only-child", [
"simon1",
"anchor1",
"yahoo",
"anchor2",
"liveLink1",
"liveLink2",
]);
// First-of-type
t("#qunit-fixture > p:first-of-type", ["firstp"]);
// Last-of-type
t("#qunit-fixture > p:last-of-type", ["first"]);
// Only-of-type
t("#qunit-fixture > :only-of-type", [
"name+value",
"firstUL",
"empty",
"floatTest",
"iframe",
"table",
]);
// Verify that the child position isn't being cached improperly
const secondChildren: AnyNode[] = CSSselect.selectAll(
"p:nth-child(2)",
document
);
const newNodes = secondChildren.map((child) => {
const [node] = parseDOM("<div></div>");
DomUtils.prepend(child as ChildNode, node);
return node;
});
// No longer second child
t("p:nth-child(2)", []);
newNodes.forEach((node) => DomUtils.removeElement(node));
// Restored second child
t("p:nth-child(2)", ["ap", "en"]);
});
it("pseudo - nth-child", () => {
expect.assertions(29);
// Nth-child
t("p:nth-child(1)", ["firstp", "sndp"]);
// Nth-child (with whitespace)
t("p:nth-child( 1 )", ["firstp", "sndp"]);
// Nth-child (case-insensitive)
t("#select1 option:NTH-child(3)", ["option1c"]);
// Not nth-child
t("#qunit-fixture p:not(:nth-child(1))", ["ap", "en", "sap", "first"]);
// Nth-child(2)
t("#qunit-fixture form#form > *:nth-child(2)", ["text1"]);
// Nth-child(2)
t("#qunit-fixture form#form > :nth-child(2)", ["text1"]);
// Nth-child(-1)
t("#select1 option:nth-child(-1)", []);
// Nth-child(3)
t("#select1 option:nth-child(3)", ["option1c"]);
// "Nth-child(0n+3)"
t("#select1 option:nth-child(0n+3)", ["option1c"]);
// Nth-child(1n+0)
t("#select1 option:nth-child(1n+0)", [
"option1a",
"option1b",
"option1c",
"option1d",
]);
// Nth-child(1n)
t("#select1 option:nth-child(1n)", [
"option1a",
"option1b",
"option1c",
"option1d",
]);
// Nth-child(n)
t("#select1 option:nth-child(n)", [
"option1a",
"option1b",
"option1c",
"option1d",
]);
// Nth-child(even)
t("#select1 option:nth-child(even)", ["option1b", "option1d"]);
// Nth-child(odd)
t("#select1 option:nth-child(odd)", ["option1a", "option1c"]);
// Nth-child(2n)
t("#select1 option:nth-child(2n)", ["option1b", "option1d"]);
// Nth-child(2n+1)
t("#select1 option:nth-child(2n+1)", ["option1a", "option1c"]);
// Nth-child(2n + 1)
t("#select1 option:nth-child(2n + 1)", ["option1a", "option1c"]);
// Nth-child(+2n + 1)
t("#select1 option:nth-child(+2n + 1)", ["option1a", "option1c"]);
// Nth-child(3n)
t("#select1 option:nth-child(3n)", ["option1c"]);
// Nth-child(3n+1)
t("#select1 option:nth-child(3n+1)", ["option1a", "option1d"]);
// Nth-child(3n+2)
t("#select1 option:nth-child(3n+2)", ["option1b"]);
// Nth-child(3n+3)
t("#select1 option:nth-child(3n+3)", ["option1c"]);
// Nth-child(3n-1)
t("#select1 option:nth-child(3n-1)", ["option1b"]);
// Nth-child(3n-2)
t("#select1 option:nth-child(3n-2)", ["option1a", "option1d"]);
// Nth-child(3n-3)
t("#select1 option:nth-child(3n-3)", ["option1c"]);
// Nth-child(3n+0)
t("#select1 option:nth-child(3n+0)", ["option1c"]);
// Nth-child(-1n+3)
t("#select1 option:nth-child(-1n+3)", [
"option1a",
"option1b",
"option1c",
]);
// Nth-child(-n+3)
t("#select1 option:nth-child(-n+3)", [
"option1a",
"option1b",
"option1c",
]);
// Nth-child(-1n + 3)
t("#select1 option:nth-child(-1n + 3)", [
"option1a",
"option1b",
"option1c",
]);
});
it("pseudo - nth-last-child", () => {
expect.assertions(29);
// Nth-last-child
t("form:nth-last-child(5)", ["testForm"]);
// Nth-last-child (with whitespace)
t("form:nth-last-child( 5 )", ["testForm"]);
// Nth-last-child (case-insensitive)
t("#select1 option:NTH-last-child(3)", ["option1b"]);
// Not nth-last-child
t("#qunit-fixture p:not(:nth-last-child(1))", [
"firstp",
"ap",
"sndp",
"en",
"first",
]);
// Nth-last-child(-1)
t("#select1 option:nth-last-child(-1)", []);
// Nth-last-child(3)
t("#select1 :nth-last-child(3)", ["option1b"]);
// Nth-last-child(3)
t("#select1 *:nth-last-child(3)", ["option1b"]);
// Nth-last-child(3)
t("#select1 option:nth-last-child(3)", ["option1b"]);
// Nth-last-child(0n+3)
t("#select1 option:nth-last-child(0n+3)", ["option1b"]);
// Nth-last-child(1n+0)
t("#select1 option:nth-last-child(1n+0)", [
"option1a",
"option1b",
"option1c",
"option1d",
]);
// Nth-last-child(1n)
t("#select1 option:nth-last-child(1n)", [
"option1a",
"option1b",
"option1c",
"option1d",
]);
// Nth-last-child(n)
t("#select1 option:nth-last-child(n)", [
"option1a",
"option1b",
"option1c",
"option1d",
]);
// Nth-last-child(even)
t("#select1 option:nth-last-child(even)", ["option1a", "option1c"]);
// Nth-last-child(odd)
t("#select1 option:nth-last-child(odd)", ["option1b", "option1d"]);
// Nth-last-child(2n)
t("#select1 option:nth-last-child(2n)", ["option1a", "option1c"]);
// Nth-last-child(2n+1)
t("#select1 option:nth-last-child(2n+1)", ["option1b", "option1d"]);
// Nth-last-child(2n + 1)
t("#select1 option:nth-last-child(2n + 1)", ["option1b", "option1d"]);
// Nth-last-child(+2n + 1)
t("#select1 option:nth-last-child(+2n + 1)", ["option1b", "option1d"]);
// Nth-last-child(3n)
t("#select1 option:nth-last-child(3n)", ["option1b"]);
// Nth-last-child(3n+1)
t("#select1 option:nth-last-child(3n+1)", ["option1a", "option1d"]);
// Nth-last-child(3n+2)
t("#select1 option:nth-last-child(3n+2)", ["option1c"]);
// Nth-last-child(3n+3)
t("#select1 option:nth-last-child(3n+3)", ["option1b"]);
// Nth-last-child(3n-1)
t("#select1 option:nth-last-child(3n-1)", ["option1c"]);
// Nth-last-child(3n-2)
t("#select1 option:nth-last-child(3n-2)", ["option1a", "option1d"]);
// Nth-last-child(3n-3)
t("#select1 option:nth-last-child(3n-3)", ["option1b"]);
// Nth-last-child(3n+0)
t("#select1 option:nth-last-child(3n+0)", ["option1b"]);
// Nth-last-child(-1n+3)
t("#select1 option:nth-last-child(-1n+3)", [
"option1b",
"option1c",
"option1d",
]);
// Nth-last-child(-n+3)
t("#select1 option:nth-last-child(-n+3)", [
"option1b",
"option1c",
"option1d",
]);
// Nth-last-child(-1n + 3)
t("#select1 option:nth-last-child(-1n + 3)", [
"option1b",
"option1c",
"option1d",
]);
});
it("pseudo - nth-of-type", () => {
expect.assertions(9);
// Nth-of-type(-1)
t(":nth-of-type(-1)", []);
// Nth-of-type(3)
t("#ap :nth-of-type(3)", ["mark"]);
// Nth-of-type(n)
t("#ap :nth-of-type(n)", [
"google",
"groups",
"code1",
"anchor1",
"mark",
]);
// Nth-of-type(0n+3)
t("#ap :nth-of-type(0n+3)", ["mark"]);
// Nth-of-type(2n)
t("#ap :nth-of-type(2n)", ["groups"]);
// Nth-of-type(even)
t("#ap :nth-of-type(even)", ["groups"]);
// Nth-of-type(2n+1)
t("#ap :nth-of-type(2n+1)", ["google", "code1", "anchor1", "mark"]);
// Nth-of-type(odd)
t("#ap :nth-of-type(odd)", ["google", "code1", "anchor1", "mark"]);
// Nth-of-type(-n+2)
t("#qunit-fixture > :nth-of-type(-n+2)", [
"firstp",
"ap",
"foo",
"nothiddendiv",
"name+value",
"firstUL",
"empty",
"form",
"floatTest",
"iframe",
"lengthtest",
"table",
]);
});
it("pseudo - nth-last-of-type", () => {
expect.assertions(9);
// Nth-last-of-type(-1)
t(":nth-last-of-type(-1)", []);
// Nth-last-of-type(3)
t("#ap :nth-last-of-type(3)", ["google"]);
// Nth-last-of-type(n)
t("#ap :nth-last-of-type(n)", [
"google",
"groups",
"code1",
"anchor1",
"mark",
]);
// Nth-last-of-type(0n+3)
t("#ap :nth-last-of-type(0n+3)", ["google"]);
// Nth-last-of-type(2n)
t("#ap :nth-last-of-type(2n)", ["groups"]);
// Nth-last-of-type(even)
t("#ap :nth-last-of-type(even)", ["groups"]);
// Nth-last-of-type(2n+1)
t("#ap :nth-last-of-type(2n+1)", [
"google",
"code1",
"anchor1",
"mark",
]);
// Nth-last-of-type(odd)
t("#ap :nth-last-of-type(odd)", ["google", "code1", "anchor1", "mark"]);
// Nth-last-of-type(-n+2)
t("#qunit-fixture > :nth-last-of-type(-n+2)", [
"ap",
"name+value",
"first",
"firstUL",
"empty",
"floatTest",
"iframe",
"table",
"name-tests",
"testForm",
"liveHandlerOrder",
"siblingTest",
]);
});
it("pseudo - has", () => {
expect.assertions(3);
// Basic test
t("p:has(a)", ["firstp", "ap", "en", "sap"]);
// Basic test (irrelevant whitespace)
t("p:has( a )", ["firstp", "ap", "en", "sap"]);
// Nested with overlapping candidates
t("#qunit-fixture div:has(div:has(div:not([id])))", [
"moretests",
"t2037",
]);
});
it("pseudo - misc", () => {
expect.assertions(31);
// Headers
t(":header", ["qunit-header", "qunit-banner", "qunit-userAgent"]);
// Headers(case-insensitive)
t(":Header", ["qunit-header", "qunit-banner", "qunit-userAgent"]);
// Multiple matches with the same context (cache check)
t("#form select:has(option:first-child:contains('o'))", [
"select1",
"select2",
"select3",
"select4",
]);
// All not grandparents
expect(
CSSselect.selectAll("#qunit-fixture :not(:has(:has(*)))", document)
.length
).toBeTruthy();
const select = document.getElementById("select1");
// Has Option Matches
expect(CSSselect.is(select, ":has(option)")).toBe(true);
// Empty string contains
expect(
CSSselect.selectAll("a:contains('')", document).length
).toBeTruthy();
// Text Contains
t("a:contains(Google)", ["google", "groups"]);
// Text Contains
t("a:contains(Google Groups)", ["groups"]);
// Text Contains
t("a:contains('Google Groups (Link)')", ["groups"]);
// Text Contains
t('a:contains("(Link)")', ["groups"]);
// Text Contains
t("a:contains(Google Groups (Link))", ["groups"]);
// Text Contains
t("a:contains((Link))", ["groups"]);
const tmp = document.createElement("div");
tmp.attribs["id"] = "tmp_input";
DomUtils.appendChild(document.body, tmp);
for (const type of ["button", "submit", "reset"]) {
const els = parseDOM(
"<input id='input_%' type='%'/><button id='button_%' type='%'>test</button>".replace(
/%/g,
type
)
).slice(0); // Create a copy of the array, so that `appendChild` doesn't remove the elements.
for (const el of els) {
DomUtils.appendChild(tmp, el);
}
// Input Buttons :${type}
t(`#tmp_input :${type}`, [`input_${type}`, `button_${type}`]);
// Input Matches :${type}
expect(CSSselect.is(els[0], `:${type}`)).toBe(true);
// Button Matches :${type}
expect(CSSselect.is(els[1], `:${type}`)).toBe(true);
}
DomUtils.removeElement(tmp);
// Caching system tolerates recursive selection
t(
"[id='select1'] *:not(:last-child), [id='select2'] *:not(:last-child)",
[
"option1a",
"option1b",
"option1c",
"option2a",
"option2b",
"option2c",
],
document.getElementById("qunit-fixture")
);
/*
* Tokenization edge cases
*/
// Sequential pseudos
t("#qunit-fixture p:has(:contains(mark)):has(code)", ["ap"]);
t(
"#qunit-fixture p:has(:contains(mark)):has(code):contains(This link)",
["ap"]
);
// Pseudo argument containing ')'
t("p:has(>a.GROUPS[src!=')'])", ["ap"]);
t("p:has(>a.GROUPS[src!=')'])", ["ap"]);
// Pseudo followed by token containing ')'
t('p:contains(id="foo")[id!=\\)]', ["sndp"]);
t("p:contains(id=\"foo\")[id!=')']", ["sndp"]);
// Multi-pseudo
t("#ap:has(*), #ap:has(*)", ["ap"]);
// Multi-pseudo with leading nonexistent id
t("#nonexistent:has(*), #ap:has(*)", ["ap"]);
// Tokenization stressor
t(
"a[class*=blog]:not(:has(*, :contains(!)), :contains(!)), br:contains(]), p:contains(]), :not(:empty):not(:parent)",
["ap", "mark", "yahoo", "simon"]
);
});
it("pseudo - :not", () => {
expect.assertions(32);
// Not
t("a.blog:not(.link)", ["mark"]);
// Not - multiple
t("#form option:not(:contains(Nothing),#option1b,:selected)", [
"option1c",
"option1d",
"option2b",
"option2c",
"option3d",
"option3e",
"option4e",
"option5b",
"option5c",
]);
// Not - recursive
t("#form option:not(:not(:selected))[id^='option3']", [
"option3b",
"option3c",
]);
// :not() failing interior
t("#qunit-fixture p:not(.foo)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not() failing interior
t("#qunit-fixture p:not(div.foo)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not() failing interior
t("#qunit-fixture p:not(p.foo)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not() failing interior
t("#qunit-fixture p:not(#blargh)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not() failing interior
t("#qunit-fixture p:not(div#blargh)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not() failing interior
t("#qunit-fixture p:not(p#blargh)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not Multiple
t("#qunit-fixture p:not(a)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not Multiple
t("#qunit-fixture p:not( a )", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not Multiple
t("#qunit-fixture p:not( p )", []);
// :not Multiple
t("#qunit-fixture p:not(a, b)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not Multiple
t("#qunit-fixture p:not(a, b, div)", [
"firstp",
"ap",
"sndp",
"en",
"sap",
"first",
]);
// :not Multiple
t("p:not(p)", []);
// :not Multiple
t("p:not(a,p)", []);
// :not Multiple
t("p:not(p,a)", []);
// :not Multiple
t("p:not(a,p,b)", []);
// :not Multiple
t(":input:not(:image,:input,:submit)", []);
// :not Multiple
t("#qunit-fixture p:not(:has(a), :nth-child(1))", ["first"]);
// No element not selector
t(".container div:not(.excluded) div", []);
// :not() Existing attribute
t("#form select:not([multiple])", ["select1", "select2", "select5"]);
// :not() Equals attribute
t("#form select:not([name=select1])", [
"select2",
"select3",
"select4",
"select5",
]);
// :not() Equals quoted attribute
t("#form select:not([name='select1'])", [
"select2",
"select3",
"select4",
"select5",
]);
// :not() Multiple Class
t("#foo a:not(.blog)", ["yahoo", "anchor2"]);
// :not() Multiple Class
t("#foo a:not(.link)", ["yahoo", "anchor2"]);
// :not() Multiple Class
t("#foo a:not(.blog.link)", ["yahoo", "anchor2"]);
// :not chaining (compound)
t("#qunit-fixture div[id]:not(:has(div, span)):not(:has(*))", [
"nothiddendivchild",
"divWithNoTabIndex",
]);
// :not chaining (with attribute)
t("#qunit-fixture form[id]:not([action$='formaction']):not(:button)", [
"lengthtest",
"name-tests",
"testForm",
]);
// :not chaining (colon in attribute)
t("#qunit-fixture form[id]:not([action='form:action']):not(:button)", [
"form",
"lengthtest",
"name-tests",
"testForm",
]);
// :not chaining (colon in attribute and nested chaining)
t(
"#qunit-fixture form[id]:not([action='form:action']:button):not(:input)",
["form", "lengthtest", "name-tests", "testForm"]
);
// :not chaining
t(
"#form select:not(.select1):contains(Nothing) > option:not(option)",
[]
);
});
it("pseudo - form", () => {
expect.assertions(10);
const extraTexts = [
...parseDOM(
'<input id="impliedText"/><input id="capitalText" type="TEXT">'
),
];
extraTexts.forEach((text) =>
DomUtils.appendChild(document.getElementById("form"), text)
);
// Form element :input
t("#form :input", [
"text1",
"text2",
"radio1",
"radio2",
"check1",
"check2",
"hidden1",
"hidden2",
"name",
"search",
"button",
"area1",
"select1",
"select2",
"select3",
"select4",
"select5",
"impliedText",
"capitalText",
]);
// Form element :radio
t("#form :radio", ["radio1", "radio2"]);
// Form element :checkbox
t("#form :checkbox", ["check1", "check2"]);
// Form element :text
t("#form :text", [
"text1",
"text2",
"hidden2",
"name",
"impliedText",
"capitalText",
]);
// Form element :radio:checked
t("#form :radio:checked", ["radio2"]);
// Form element :checkbox:checked
t("#form :checkbox:checked", ["check1"]);
// Form element :radio:checked, :checkbox:checked
t("#form :radio:checked, #form :checkbox:checked", [
"radio2",
"check1",
]);
// Selected Option Element
t("#form option:selected", [
"option1a",
"option2d",
"option3b",
"option3c",
"option4b",
"option4c",
"option4d",
"option5a",
]);
// Selected Option Element are also :checked
t("#form option:checked", [
"option1a",
"option2d",
"option3b",
"option3c",
"option4b",
"option4c",
"option4d",
"option5a",
]);
// Hidden inputs should be treated as enabled. See QSA test.
t("#hidden1:enabled", ["hidden1"]);
extraTexts.forEach((text) => DomUtils.removeElement(text));
});
it("pseudo - :root", () => {
expect.assertions(1);
// :root selector
expect(CSSselect.selectOne(":root", document)).toBe(
document.documentElement
);
});
it("caching", () => {
expect.assertions(1);
CSSselect.selectAll(":not(code)", document.getElementById("ap"));
// Reusing selector with new context
t(
":not(code)",
["sndp", "en", "yahoo", "sap", "anchor2", "simon"],
document.getElementById("foo")
);
});
it("more specific selector should find less elements", () => {
// Same selector, but with an attribute filter added
expect(
CSSselect.selectAll("#qunit-fixture div div", document).length
).toBeGreaterThanOrEqual(
CSSselect.selectAll("#qunit-fixture div div[id]", document).length
);
});
});
|