selector.html revision 08318b3df0963de4f820fc177d4d895588bdfe1d
<html>
<head>
<title>Selector Test Suite</title>
<script>
onload = function() {
YUI().use('*', function(Y) {
var logger = new YAHOO.tool.TestLogger(null, {height:'2000px'});
var suite = new YAHOO.tool.TestSuite("Selector Suite");
var Selector = Y.Selector;
var Assert = YAHOO.util.Assert;
var ArrayAssert = YAHOO.util.ArrayAssert;
var $ = Selector.query;
var demo = Y.Dom.get('demo');
children = Y.Dom.getChildren(demo);
var demoFirstChild = children[0];
var demoLastChild = children[children.length - 1];
var selectorQueryAll = new YAHOO.tool.TestCase({
name: 'Query All',
testFilter: function() {
var all = Y.Dom.get('test-inputs').getElementsByTagName('input');
ArrayAssert.itemsAreEqual([all[0], all[1], all[2]], Selector.filter(all, '[type=checkbox]'), '[type=checkbox]');
ArrayAssert.itemsAreEqual([], Selector.filter(null, '[type=checkbox]'), 'null input');
//ArrayAssert.itemsAreEqual([document.getElementById('test-inputs')], Selector.filter(['root-test', 'test-inputs'], 'form'), 'form (string inputs)');
// no longer supporting string input for element
//ArrayAssert.itemsAreEqual([document.getElementById('test-inputs')], Selector.filter(['root-test', document.getElementById('test-inputs'), document.createTextNode('foo')], 'form'), 'form (mixed inputs)');
},
testTest: function() {
Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), '[type=checkbox], button'), '[type=checkbox], button');
Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), 'button, [type=checkbox]'), 'button, [type=checkbox]');
Assert.isFalse(Selector.test(null, '#foo'), ';ull input');
Assert.isTrue(Selector.test(Y.Dom.get('test-lang-en-us'), '[lang|=en]'), '[lang|=en] (lang="en-us")');
Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), 'for [type=checkbox]'), 'for [type=checkbox] false pos');
Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), 'form [type=checkbox]'), 'form [type=checkbox]');
Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), 'for [type=checkbox]'), 'for [type=checkbox] false pos');
Assert.isTrue(Selector.test(Y.Dom.get('checkbox-checked'), '[type=checkbox]:checked'), 'type=checkbox:checked');
Assert.isFalse(Selector.test(Y.Dom.get('radio-unchecked'), ':checked'), ':checked (radio) false pos');
Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), '[type=checkbox]:checked'), 'type=checkbox:checked false pos');
Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), '[type=checkbox]:not(:checked)'), 'type=checkbox:not(:checked)');
},
testRootQuery: function() {
var all = Y.Dom.get('nth-test').getElementsByTagName('li');
ArrayAssert.itemsAreEqual(all, $('li', document), 'document');
ArrayAssert.itemsAreEqual(all, $('#root-test li'), 'document');
ArrayAssert.itemsAreEqual([], $('#root-tes li', Y.DOM.byId('demo')), 'false id document');
// standard: no element-scoped query
//ArrayAssert.itemsAreEqual(document.body.getElementsByTagName('p'), $('body p', document.body), "$('body p', document.body)");
/* no longer supporting string input for element
ArrayAssert.itemsAreEqual([], $('li', 'nth-tes'), 'id as root false pos');
ArrayAssert.itemsAreEqual(all, $('li', Y.Dom.get('nth-test')), 'node as root');
ArrayAssert.itemsAreEqual(all, $('#nth-test li', Y.Dom.get('root-test')), 'id selector w/root node ref');
Assert.areEqual(all[0], $('#nth-test li', 'root-test', true), 'id selector w/root string, firstOnly');
*/
// based on non-standard behavior
ArrayAssert.itemsAreEqual([], $('#root-test li', Y.Dom.get('nth-test')), 'id selector w/root false pos');
},
testNthLastChild: function() {
var all = Y.Dom.get('nth-test').getElementsByTagName('li');
var odd = Y.Dom.getElementsByClassName('even', 'li', 'nth-test');
var even = Y.Dom.getElementsByClassName('odd', 'li', 'nth-test');
var four1 = Y.Dom.getElementsByClassName('last-four-1', 'li', 'nth-test');
ArrayAssert.itemsAreEqual(odd, $('li:nth-last-child(odd)'), 'odd');
ArrayAssert.itemsAreEqual(even, $('li:nth-last-child(2n)'), '2n');
ArrayAssert.itemsAreEqual(even, $('li:nth-last-child(even)'), 'even');
ArrayAssert.itemsAreEqual(even, $('li:nth-last-child(2n+0)'), '2n+0');
ArrayAssert.itemsAreEqual(odd, $('li:nth-last-child(2n+1)'), '2n+1');
ArrayAssert.itemsAreEqual(four1, $('li:nth-last-child(4n+1)'), '4n+1');
},
testNthType: function() {
var all = Y.Dom.get('nth-test').getElementsByTagName('li');
var odd = Y.Dom.getElementsByClassName('odd', 'li', 'nth-test');
var even = Y.Dom.getElementsByClassName('even', 'li', 'nth-test');
var three1 = Y.Dom.getElementsByClassName('three-1', 'li', 'nth-test');
var four1 = Y.Dom.getElementsByClassName('four-1', 'li', 'nth-test');
var four2 = Y.Dom.getElementsByClassName('four-2', 'li', 'nth-test');
var four3 = Y.Dom.getElementsByClassName('four-3', 'li', 'nth-test');
var four4 = Y.Dom.getElementsByClassName('four-4', 'li', 'nth-test');
ArrayAssert.itemsAreEqual(odd, $('li:nth-of-type(odd)'), 'odd');
},
testNthChild: function() {
var all = Y.Dom.get('nth-test').getElementsByTagName('li');
var odd = Y.Dom.getElementsByClassName('odd', 'li', 'nth-test');
var even = Y.Dom.getElementsByClassName('even', 'li', 'nth-test');
var three1 = Y.Dom.getElementsByClassName('three-1', 'li', 'nth-test');
var four1 = Y.Dom.getElementsByClassName('four-1', 'li', 'nth-test');
var four2 = Y.Dom.getElementsByClassName('four-2', 'li', 'nth-test');
var four3 = Y.Dom.getElementsByClassName('four-3', 'li', 'nth-test');
var four4 = Y.Dom.getElementsByClassName('four-4', 'li', 'nth-test');
ArrayAssert.itemsAreEqual(even[1], $('li:nth-child(2)'), '2');
ArrayAssert.itemsAreEqual(even[1], $('li:nth-child(0n+2)'), '0n+2');
ArrayAssert.itemsAreEqual(three1, $('li:nth-child(3n+1)'), '3n+1');
ArrayAssert.itemsAreEqual(all, $('li:nth-child(n+1)'), 'n+1');
//from http://www.w3.org/TR/css3-selectors/#nth-child-pseudo examples
ArrayAssert.itemsAreEqual(odd, $('li:nth-child(2n+1)'), '2n+1');
ArrayAssert.itemsAreEqual(odd, $('li:nth-child(odd)'), 'odd');
ArrayAssert.itemsAreEqual(even, $('li:nth-child(2n+0)'), '2n+0');
ArrayAssert.itemsAreEqual(even, $('li:nth-child(2n)'), '2n');
ArrayAssert.itemsAreEqual(even, $('li:nth-child(even)'), 'even');
ArrayAssert.itemsAreEqual(four1, $('li:nth-child(4n+1)'), '4n+1');
ArrayAssert.itemsAreEqual(four2, $('li:nth-child(4n+2)'), '4n+2');
ArrayAssert.itemsAreEqual(four3, $('li:nth-child(4n+3)'), '4n+3');
ArrayAssert.itemsAreEqual(four4, $('li:nth-child(4n+4)'), '4n+4');
ArrayAssert.itemsAreEqual(even[0], $('li:nth-child(0n+1)'), '0n+1');
ArrayAssert.itemsAreEqual(even[0], $('li:nth-child(1)'), '1');
ArrayAssert.itemsAreEqual(all, $('li:nth-child(1n+0)'), '1n+0');
ArrayAssert.itemsAreEqual(all, $('li:nth-child(n+0)'), 'n+0');
},
testQuery: function() {
ArrayAssert.itemsAreEqual(document.getElementsByTagName('p'), $('p, p'), 'p, p');
Assert.areEqual(document.getElementsByTagName('p')[0], $('p', null, true), 'p (firstOnly)');
ArrayAssert.itemsAreEqual([], $('.Foo'), '.Foo');
ArrayAssert.itemsAreEqual([document.getElementById('root-test')], $('#root-test'), 'id only');
ArrayAssert.itemsAreEqual(Y.Dom.get('demo').getElementsByTagName('p'), $('#demo.foo p'), '#demo.foo p');
ArrayAssert.itemsAreEqual(Y.Dom.get('demo').getElementsByTagName('p'), $('.foo p'), '.foo p');
ArrayAssert.itemsAreEqual(Y.Dom.get('demo').getElementsByTagName('p'), $('#demo p'), '#demo p');
ArrayAssert.itemsAreEqual($('p > em'), [Y.Dom.getFirstChild('demo-first-child')], 'p > em');
ArrayAssert.itemsAreEqual(Y.Dom.getElementsByClassName('para'), $('[class~=para]'), '[class~=para]');
ArrayAssert.itemsAreEqual(Y.Dom.getElementsByClassName('para'), $('[class~="para"]'), '[class~="para"]');
ArrayAssert.itemsAreEqual(document.body.getElementsByTagName('p'), $('body div p'), 'body div p');
ArrayAssert.itemsAreEqual([], $('#demo .madeup'), '#demo .madeup');
ArrayAssert.itemsAreEqual(Y.Dom.getElementsByClassName('para', 'p'), $('div .para'), 'div .para');
ArrayAssert.itemsAreEqual(Y.Dom.getElementsByClassName('first', null, Y.DOM.byId(demo)), $('#demo .first'), '#demo .first');
ArrayAssert.itemsAreEqual(document.getElementsByTagName('div'), $('div'), 'div');
ArrayAssert.itemsAreEqual(document.body.getElementsByTagName('div'), $('body div'), 'body div');
ArrayAssert.itemsAreEqual([demoFirstChild, Y.DOM.next(demoFirstChild)], $('#demo > p:not(.last)'), '#demo > p:not(.last)');
ArrayAssert.itemsAreEqual([Y.Dom.get('class-bar')], $('.Bar'), '.Bar');
//ArrayAssert.itemsAreEqual([], $(null), 'null input');
ArrayAssert.itemsAreEqual([], $('#fake-id-not-in-doc'), 'id false positive');
Y.Dom.addClass($('li'), 'first-child');
Assert.areEqual(document.getElementById('label-checkbox-unchecked'), $('label[for=checkbox-unchecked]', null, true), 'for attribute');
ArrayAssert.itemsAreEqual($('.not-button', Y.DOM.byId('test-inputs')), $('input:not([type=button])', Y.DOM.byId('test-inputs')), 'input[type=radio], input[type=checkbox] = input:not([type=button])');
//ArrayAssert.itemsAreEqual(Y.Dom.get('demo2').getElementsByTagName('div'), $('div:contains(child of demo2)', Y.DOM.byId('demo2')), 'div:contains:(child of demo2) ');
//Assert.areEqual(document.getElementById('contains-special'), $(':contains(contains "\' & ])'), 'contains "\' & ]');
}
});
var simpleTest = new YAHOO.tool.TestCase({
name: 'Simple Node Test',
testPseudo: function() {
Assert.isFalse(Selector.test(Y.Dom.getFirstChild('demo'), ':first-child.last'), 'first-child class false pos');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), ':first-child.first'), 'first-child class');
Assert.isTrue(Selector.test(demoFirstChild, ':not(.last)'), 'not(.last)');
Assert.isFalse(Selector.test(Y.Dom.getFirstChild('demo'), ':only-of-type'), 'only-of-type false pos');
/* non-standard
Assert.isFalse(Selector.test(Y.Dom.get('demo2'), ':not(:contains(demo2))'), ':not(:contains(demo2))');
Assert.isTrue(Selector.test(Y.Dom.get('demo2'), ':not(:contains(demo1))'), ':not(:contains(demo1))');
Assert.isTrue(Selector.test(Y.Dom.get('demo2'), ':contains(child of demo2)'), 'contains(child of demo2)');
*/
},
testAttr: function() {
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'div[id=demo][title~=demo]'), 'tag & multiple attributes');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'div[title="this is a demo"]'), 'quoted attribute with spaces');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), "div[title='this is a demo']"), 'single quoted attribute with spaces');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), '[id=demo][title=demo]'), 'multiple attributes false pos');
/* non-standard
alert(Y.Dom.get('demo').querySelector('[id!=bar]'));
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'div[title=this is a demo]'), 'attribute with spaces');
*/
},
testClass: function() {
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), 'p.first.para'), 'tag & multiple class match');
},
testId: function() {
},
testTag: function() {
},
// to support:
// $('> p', myNode) === ('#my-node-id > p');
// $('+ p', myNode) === ('#my-node-id + p');
// $('~ p', myNode) === ('#my-node-id ~ p');
testScoped: function() {
var paraChildren = Y.DOM._childrenByTag(Y.DOM.byId('demo'), 'p');
var someChildren = Y.DOM.children(Y.DOM.byId('demo'), function(child) {
return (child.tagName === 'P' &&
Y.DOM.firstChild(child.parentNode) !== child);
});
var bodyDivs = Y.DOM.children(document.body, function(child) {
return (child.tagName === 'DIV' && child.id != 'demo');
});
ArrayAssert.itemsAreEqual(paraChildren, $('> p', Y.DOM.byId('demo')), '#demo > p');
ArrayAssert.itemsAreEqual($('#demo > p'), $('foo, > p', Y.DOM.byId('demo')), '#demo foo, #demo > p');
//ArrayAssert.itemsAreEqual($('#demo-first-child ~ p'), $('~ p', Y.DOM.byId('demo-first-child')), '#demo-first-child ~ div');
//ArrayAssert.itemsAreEqual($('#demo-first-child ~ p'), someChildren, 'someChildren');
//ArrayAssert.itemsAreEqual(someChildren, $('~ p', Y.DOM.byId('demo-first-child')), '~ p');
//ArrayAssert.itemsAreEqual(bodyDivs, $('~ div', Y.DOM.byId('demo')), '#demo ~ div');
}
});
suite.add(selectorQueryAll);
suite.add(simpleTest);
YAHOO.tool.TestRunner.add(suite);
});
};
</script>
</style>
</head>
<body>
<div id="demo" class="foo" title="this is a demo">
<p class="para first" id="demo-first-child"><em>lorem ipsum</em></p>
<p class="para">lorem ipsum</p>
<p class="para last">lorem ipsum</p>
<div><p>div lorem</p></div>
<div id="demo-last-child"><p>last child</p></div>
</div>
<div id="demo2">
<div>child of demo2</div>
</div>
<div id="empty"></div>
<div id="root-test">
<ol id="nth-test">
<li class="odd three-1 four-1">foo</li>
<li class="even four-2 last-four-1">foo</li>
<li class="odd four-3">foo</li>
<li class="even three-1 four-4">foo</li>
<li class="odd four-1">foo</li>
<li class="even four-2 last-four-1">foo</li>
<li class="odd three-1 four-3">foo</li>
<li class="even four-4" id="test-lang-none">foo</li>
<li class="odd four-1" lang="en-US" id="test-lang-en-us">foo</li>
<li class="even three-1 four-2 last-four-1" lang="en" id="test-lang-en">foo</li>
</ol>
</div>
<form id="test-inputs">
<label for="checkbox-unchecked" id="label-checkbox-unchecked">label</label>
<input type="checkbox" id="checkbox-unchecked" class="not-button">
<input type="checkbox" checked id="checkbox-checked-noval" class="not-button">
<input type="checkbox" checked="true" id="checkbox-checked" class="not-button">
<input type="radio" id="radio-unchecked" class="not-button">
<input type="radio" checked="true" id="radio-checked" class="not-button">
<input type="button" value="foo">
</form>
<div class="Bar" id="class-bar"></div>
<div id="contains-special">contains "' & ]</div>
</body>
</html>