dom/tests/mochitest/ajax/jquery/test/unit/selector.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/ajax/jquery/test/unit/selector.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,224 @@
     1.4 +module("selector");
     1.5 +
     1.6 +test("element", function() {
     1.7 +	expect(9);
     1.8 +	ok( $("*").size() >= 30, "Select all" );
     1.9 +	var all = $("*"), good = true;
    1.10 +	for ( var i = 0; i < all.length; i++ )
    1.11 +		if ( all[i].nodeType == 8 )
    1.12 +			good = false;
    1.13 +	ok( good, "Select all elements, no comment nodes" );
    1.14 +	t( "Element Selector", "p", ["firstp","ap","sndp","en","sap","first"] );
    1.15 +	t( "Element Selector", "body", ["body"] );
    1.16 +	t( "Element Selector", "html", ["html"] );
    1.17 +	t( "Parent Element", "div p", ["firstp","ap","sndp","en","sap","first"] );
    1.18 +	equals( $("param", "#object1").length, 2, "Object/param as context" );
    1.19 +	
    1.20 +	ok( $("#length").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
    1.21 +	ok( $("#lengthtest input").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
    1.22 +});
    1.23 +
    1.24 +if ( location.protocol != "file:" ) {
    1.25 +	test("Element Selector with underscore", function() {
    1.26 +		expect(1);
    1.27 +		stop();
    1.28 +		$.get("data/with_fries.xml", function(xml) {
    1.29 +			equals( $("foo_bar", xml).length, 1, "Element Selector with underscore" );
    1.30 +			start();
    1.31 +		});
    1.32 +	});
    1.33 +}
    1.34 +
    1.35 +test("broken", function() {
    1.36 +	expect(7);
    1.37 +	t( "Broken Selector", "[", [] );
    1.38 +	t( "Broken Selector", "(", [] );
    1.39 +	t( "Broken Selector", "{", [] );
    1.40 +	t( "Broken Selector", "<", [] );
    1.41 +	t( "Broken Selector", "()", [] );
    1.42 +	t( "Broken Selector", "<>", [] );
    1.43 +	t( "Broken Selector", "{}", [] );
    1.44 +});
    1.45 +
    1.46 +test("id", function() {
    1.47 +	expect(25);
    1.48 +	t( "ID Selector", "#body", ["body"] );
    1.49 +	t( "ID Selector w/ Element", "body#body", ["body"] );
    1.50 +	t( "ID Selector w/ Element", "ul#first", [] );
    1.51 +	t( "ID selector with existing ID descendant", "#firstp #simon1", ["simon1"] );
    1.52 +	t( "ID selector with non-existant descendant", "#firstp #foobar", [] );
    1.53 +	t( "ID selector using UTF8", "#台北Táiběi", ["台北Táiběi"] );
    1.54 +	t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", ["台北Táiběi","台北"] );
    1.55 +	t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
    1.56 +	t( "Child ID selector using UTF8", "form > #台北", ["台北"] );
    1.57 +	
    1.58 +	t( "Escaped ID", "#foo\\:bar", ["foo:bar"] );
    1.59 +	t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
    1.60 +	t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] );
    1.61 +	t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
    1.62 +	t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] );
    1.63 +	t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
    1.64 +	
    1.65 +	t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
    1.66 +	t( "ID Selector, not an ancestor ID", "#form #first", [] );
    1.67 +	t( "ID Selector, not a child ID", "#form > #option1a", [] );
    1.68 +	
    1.69 +	t( "All Children of ID", "#foo > *", ["sndp", "en", "sap"] );
    1.70 +	t( "All Children of ID with no children", "#firstUL/*", [] );
    1.71 +	
    1.72 +	$('<a name="tName1">tName1 A</a><a name="tName2">tName2 A</a><div id="tName1">tName1 Div</div>').appendTo('#main');
    1.73 +	equals( $("#tName1")[0].id, 'tName1', "ID selector with same value for a name attribute" );
    1.74 +	equals( $("#tName2").length, 0, "ID selector non-existing but name attribute on an A tag" );
    1.75 +	t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", ["lengthtest"] );
    1.76 +	
    1.77 +	t( "ID selector with non-existant ancestor", "#asdfasdf #foobar", [] ); // bug #986
    1.78 +
    1.79 +	isSet( $("body").find("div#form"), [], "ID selector within the context of another element" );
    1.80 +});
    1.81 +
    1.82 +test("class", function() {
    1.83 +	expect(16);
    1.84 +	t( "Class Selector", ".blog", ["mark","simon"] );
    1.85 +	t( "Class Selector", ".blog.link", ["simon"] );
    1.86 +	t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
    1.87 +	t( "Parent Class Selector", "p .blog", ["mark","simon"] );
    1.88 +	
    1.89 +	t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
    1.90 +	t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );
    1.91 +	t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] );
    1.92 +	t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
    1.93 +	t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
    1.94 +	t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] );
    1.95 +	
    1.96 +	t( "Escaped Class", ".foo\\:bar", ["foo:bar"] );
    1.97 +	t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
    1.98 +	t( "Descendant scaped Class", "div .foo\\:bar", ["foo:bar"] );
    1.99 +	t( "Descendant scaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
   1.100 +	t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] );
   1.101 +	t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
   1.102 +});
   1.103 +
   1.104 +test("multiple", function() {
   1.105 +	expect(4);
   1.106 +	t( "Comma Support", "a.blog, p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
   1.107 +	t( "Comma Support", "a.blog , p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
   1.108 +	t( "Comma Support", "a.blog ,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
   1.109 +	t( "Comma Support", "a.blog,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
   1.110 +});
   1.111 +
   1.112 +test("child and adjacent", function() {
   1.113 +	expect(37);
   1.114 +	t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
   1.115 +	t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
   1.116 +	t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
   1.117 +	t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );
   1.118 +	t( "Child w/ Class", "p > a.blog", ["mark","simon"] );
   1.119 +	t( "All Children", "code > *", ["anchor1","anchor2"] );
   1.120 +	t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );
   1.121 +	t( "Adjacent", "a + a", ["groups"] );
   1.122 +	t( "Adjacent", "a +a", ["groups"] );
   1.123 +	t( "Adjacent", "a+ a", ["groups"] );
   1.124 +	t( "Adjacent", "a+a", ["groups"] );
   1.125 +	t( "Adjacent", "p + p", ["ap","en","sap"] );
   1.126 +	t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] );
   1.127 +	
   1.128 +	t( "First Child", "p:first-child", ["firstp","sndp"] );
   1.129 +	t( "Nth Child", "p:nth-child(1)", ["firstp","sndp"] );
   1.130 +	
   1.131 +	t( "Last Child", "p:last-child", ["sap"] );
   1.132 +	t( "Last Child", "a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon"] );
   1.133 +	
   1.134 +	t( "Nth-child", "#main form#form > *:nth-child(2)", ["text2"] );
   1.135 +	t( "Nth-child", "#main form#form > :nth-child(2)", ["text2"] );
   1.136 +
   1.137 +	t( "Nth-child", "#form select:first option:nth-child(3)", ["option1c"] );
   1.138 +	t( "Nth-child", "#form select:first option:nth-child(0n+3)", ["option1c"] );
   1.139 +	t( "Nth-child", "#form select:first option:nth-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] );
   1.140 +	t( "Nth-child", "#form select:first option:nth-child(1n)", ["option1a", "option1b", "option1c", "option1d"] );
   1.141 +	t( "Nth-child", "#form select:first option:nth-child(n)", ["option1a", "option1b", "option1c", "option1d"] );
   1.142 +	t( "Nth-child", "#form select:first option:nth-child(even)", ["option1b", "option1d"] );
   1.143 +	t( "Nth-child", "#form select:first option:nth-child(odd)", ["option1a", "option1c"] );
   1.144 +	t( "Nth-child", "#form select:first option:nth-child(2n)", ["option1b", "option1d"] );
   1.145 +	t( "Nth-child", "#form select:first option:nth-child(2n+1)", ["option1a", "option1c"] );
   1.146 +	t( "Nth-child", "#form select:first option:nth-child(3n)", ["option1c"] );
   1.147 +	t( "Nth-child", "#form select:first option:nth-child(3n+1)", ["option1a", "option1d"] );
   1.148 +	t( "Nth-child", "#form select:first option:nth-child(3n+2)", ["option1b"] );
   1.149 +	t( "Nth-child", "#form select:first option:nth-child(3n+3)", ["option1c"] );
   1.150 +	t( "Nth-child", "#form select:first option:nth-child(3n-1)", ["option1b"] );
   1.151 +	t( "Nth-child", "#form select:first option:nth-child(3n-2)", ["option1a", "option1d"] );
   1.152 +	t( "Nth-child", "#form select:first option:nth-child(3n-3)", ["option1c"] );
   1.153 +	t( "Nth-child", "#form select:first option:nth-child(3n+0)", ["option1c"] );
   1.154 +	t( "Nth-child", "#form select:first option:nth-child(-n+3)", ["option1a", "option1b", "option1c"] );
   1.155 +});
   1.156 +
   1.157 +test("attributes", function() {
   1.158 +	expect(20);
   1.159 +	t( "Attribute Exists", "a[title]", ["google"] );
   1.160 +	t( "Attribute Exists", "*[title]", ["google"] );
   1.161 +	t( "Attribute Exists", "[title]", ["google"] );
   1.162 +	
   1.163 +	t( "Attribute Equals", "a[rel='bookmark']", ["simon1"] );
   1.164 +	t( "Attribute Equals", 'a[rel="bookmark"]', ["simon1"] );
   1.165 +	t( "Attribute Equals", "a[rel=bookmark]", ["simon1"] );
   1.166 +	t( "Multiple Attribute Equals", "#form input[type='hidden'],#form input[type='radio']", ["hidden1","radio1","radio2"] );
   1.167 +	t( "Multiple Attribute Equals", "#form input[type=\"hidden\"],#form input[type='radio']", ["hidden1","radio1","radio2"] );
   1.168 +	t( "Multiple Attribute Equals", "#form input[type=hidden],#form input[type=radio]", ["hidden1","radio1","radio2"] );
   1.169 +	
   1.170 +	t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] );
   1.171 +	
   1.172 +	t( "Attribute Begins With", "a[href ^= 'http://www']", ["google","yahoo"] );
   1.173 +	t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] );
   1.174 +	t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] );
   1.175 +	
   1.176 +	t("Select options via [selected]", "#select1 option[selected]", ["option1a"] );
   1.177 +	t("Select options via [selected]", "#select2 option[selected]", ["option2d"] );
   1.178 +	t("Select options via [selected]", "#select3 option[selected]", ["option3b", "option3c"] );
   1.179 +	
   1.180 +	t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] );
   1.181 +	
   1.182 +	t( ":not() Existing attribute", "#form select:not([multiple])", ["select1", "select2"]);
   1.183 +	t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3"]);
   1.184 +	t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3"]);
   1.185 +});
   1.186 +
   1.187 +test("pseudo (:) selectors", function() {
   1.188 +	expect(35);
   1.189 +	t( "First Child", "p:first-child", ["firstp","sndp"] );
   1.190 +	t( "Last Child", "p:last-child", ["sap"] );
   1.191 +	t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );
   1.192 +	t( "Empty", "ul:empty", ["firstUL"] );
   1.193 +	t( "Enabled UI Element", "#form input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2","name"] );
   1.194 +	t( "Disabled UI Element", "#form input:disabled", ["text2"] );
   1.195 +	t( "Checked UI Element", "#form input:checked", ["radio2","check1"] );
   1.196 +	t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c"] );
   1.197 +	t( "Text Contains", "a:contains('Google')", ["google","groups"] );
   1.198 +	t( "Text Contains", "a:contains('Google Groups')", ["groups"] );
   1.199 +	t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests", "moretests"] );
   1.200 +	t( "Not", "a.blog:not(.link)", ["mark"] );
   1.201 +	t( "Not - multiple", "#form option:not(:contains('Nothing'),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d"] );
   1.202 +	t( "Not - complex", "#form option:not([id^='opt']:gt(0):nth-child(-n+3))", [ "option1a", "option1d", "option2d", "option3d"] );
   1.203 +	t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
   1.204 +	
   1.205 +	t( "nth Element", "p:nth(1)", ["ap"] );
   1.206 +	t( "First Element", "p:first", ["firstp"] );
   1.207 +	t( "Last Element", "p:last", ["first"] );
   1.208 +	t( "Even Elements", "p:even", ["firstp","sndp","sap"] );
   1.209 +	t( "Odd Elements", "p:odd", ["ap","en","first"] );
   1.210 +	t( "Position Equals", "p:eq(1)", ["ap"] );
   1.211 +	t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] );
   1.212 +	t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] );
   1.213 +	t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] );
   1.214 +	t( "Is Visible", "#form input:visible", ["text1","text2","radio1","radio2","check1","check2","name"] );
   1.215 +	t( "Is Hidden", "#form input:hidden", ["hidden1","hidden2"] );
   1.216 +	
   1.217 +	t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "button", "area1", "select1", "select2", "select3"] );
   1.218 +	t( "Form element :radio", "#form :radio", ["radio1", "radio2"] );
   1.219 +	t( "Form element :checkbox", "#form :checkbox", ["check1", "check2"] );
   1.220 +	t( "Form element :text", "#form :text", ["text1", "text2", "hidden2", "name"] );
   1.221 +	t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] );
   1.222 +	t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] );
   1.223 +	t( "Form element :checkbox:checked, :radio:checked", "#form :checkbox:checked, #form :radio:checked", ["check1", "radio2"] );
   1.224 +
   1.225 +	t( "Headers", ":header", ["header", "banner", "userAgent"] );
   1.226 +	t( "Has Children - :has()", "p:has(a)", ["firstp","ap","en","sap"] );
   1.227 +});

mercurial