selector.html revision 5d829590558aa225fef50b0d3d22e35ff361d431
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Selector Test Suite</title>
<link type="text/css" rel="stylesheet" href="/yui2/build/logger/assets/logger.css">
<link type="text/css" rel="stylesheet" href="/yui2/build/yuitest/assets/testlogger.css">
<script type="text/javascript" src="/build/yui/yui.js"></script>
<script type="text/javascript" src="/build/event-custom/event-custom.js"></script>
<script type="text/javascript" src="/build/event/event.js"></script>
<script type="text/javascript" src="/build/oop/oop.js"></script>
<script type="text/javascript" src="/build/dom/dom-debug.js"></script>
<script type="text/javascript" src="/build/dom/selector-css3.js"></script>
<script type="text/javascript" src="/build/base/base-debug.js"></script>
<script type="text/javascript" src="/build/attribute/attribute-debug.js"></script>
<script type="text/javascript" src="/build/node/node-debug.js"></script>
<script type="text/javascript" src="/yui2/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="/yui2/build/dom/dom-min.js"></script>
<script type="text/javascript" src="/yui2/build/event/event.js"></script>
<script type="text/javascript" src="/yui2/build/logger/logger-min.js"></script>
<script type="text/javascript" src="/yui2/build/yuitest/yuitest.js"></script>
<script>
onload = function() {
YUI().use('*', function(Y) {
Y.Dom = YAHOO.util.Dom;
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(Y.Dom.get('checkbox-unchecked'), 'foo, button'), 'foo, button');
Assert.isFalse(Selector.test(null, '#foo'), ';ull input');
Assert.isFalse(Selector.test(document.createTextNode('foo'), '#foo'), 'textNode input');
Assert.isTrue(Selector.test(Y.Dom.get('test-lang-en-us'), '[lang|=en]'), '[lang|=en] (lang="en-us")');
Assert.isTrue(Selector.test(Y.Dom.get('test-lang-en'), '[lang|=en]'), '[lang|=en] (lang="en")');
Assert.isFalse(Selector.test(Y.Dom.get('test-lang-none'), '[lang|=en]'), '[lang|=en] false pos');
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.isTrue(Selector.test(Y.Dom.get('radio-checked'), ':checked'), ':checked (radio)');
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([], $('body p', document.body), "$('body p', document.body)");
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([], $('#demo.bar p'), '#demo.bar p');
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 "\' & ]');
Assert.areEqual(Y.DOM.byId('test-select'), $('#test-select', null, true), "($('#test-select'), null, true)");
Assert.areEqual(document.getElementsByTagName('link')[0], $('[rel^=style]', null, true), "($('[rel^=style]), null, true)");
Assert.areEqual(Y.DOM.byId('test-rel-div'), $('div[rel^=foo2]', null, true), "($('[rel^=foo2]), null, true)");
Assert.areEqual(Y.DOM.byId('test-rel-div'), $("div[rel^='foo2']", null, true), "($('[rel^=\'foo2\']), null, true)");
Assert.areEqual(Y.DOM.byId('foo-bar'), $("input[name='foo.bar']", null, true), "input[name='foo.bar'], null, true)");
}
});
var simpleTest = new YAHOO.tool.TestCase({
name: 'Simple Node Test',
testPseudo: function() {
Assert.isTrue(Selector.test(Y.Dom.getLastChild('demo'), ':last-child'), 'last-child');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), 'p:first-child'), 'first-child tag');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), ':first-child'), 'first-child');
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.isFalse(Selector.test(Y.Dom.getFirstChild('demo'), ':only-child'), 'only-child');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), ':not(.foo)'), 'not(.foo)');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), ':not(p)'), 'not(p)');
Assert.isTrue(Selector.test(demoFirstChild, ':not(.last)'), 'not(.last)');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), ':first-of-type'), 'first-of-type');
Assert.isTrue(Selector.test(Y.Dom.getLastChild('demo'), ':last-of-type'), 'last-of-type');
Assert.isFalse(Selector.test(Y.Dom.getFirstChild('demo'), ':only-of-type'), 'only-of-type false pos');
Assert.isFalse(Selector.test(Y.Dom.get('demo2'), ':empty'), 'empty false pos');
Assert.isTrue(Selector.test(Y.Dom.get('empty'), ':empty'), 'empty');
/* non-standard
Assert.isTrue(Selector.test(Y.Dom.get('demo2'), ':contains(demo2)'), 'contains(demo2)');
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'), '[title]'), 'title exists');
Assert.isFalse(Selector.test(Y.Dom.getFirstChild('demo'), '[title]'), 'title exists false pos');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[id=demo]'), 'id equality');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), '[id|=me]'), 'id starts with false pos');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[id~=demo]'), 'id includes');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[title~=demo]'), 'title includes');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[id^=de]'), 'id starts with');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[id$=mo]'), 'id ends with');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), '[id$=m]'), 'id ends with false pos');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[id*=em]'), 'id substr');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), '[id*=ex]'), 'id substr false pos');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '[id=demo][title~=demo]'), 'multiple attributes');
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.isTrue(Selector.test(Y.Dom.get('href-test'), '[href="foo.html"]'), 'href="foo.html"');
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('href-test'), '[href=foo.html]'), 'href=foo.html');
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.get('demo'), '.foo'), 'class match');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'div.foo'), 'tag match');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), 'span.foo'), 'tag false positive');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '#demo.foo'), 'id match');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), '.baz'), 'class false positive');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), '.first.para'), 'multiple class match');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), 'p.first.para'), 'tag & multiple class match');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), '.foo.bar'), 'multiple class false pos');
},
testId: function() {
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '#demo'), 'id match');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'div#demo'), 'tag match');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), 'div#dmo'), 'id false positive');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), 'span#demo'), 'tag false positive');
},
testTag: function() {
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'div'), 'tag match');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), 'span'), 'tag false positive');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '*'), 'universal tag');
},
// 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([Y.Dom.get('demo2')], $('+ div', Y.DOM.byId('demo')), '#demo + div');
//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);
YAHOO.tool.TestRunner.run();
});
};
</script>
<style type="text/css">
</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>
<select id="test-select" name="test-select">
<option value="0">foo</option>
<option value="1">bar</option>
<option>baz</option>
</select>
<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>
<a id="href-test" href="foo.html">foo</a>
<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">
<input id="foo-bar" name="foo.bar" class="not-button">
</form>
<div class="Bar" id="class-bar"></div>
<div id="contains-special">contains "' & ]</div>
<div id="test-rel-div" rel="foo2"></div>
</body>
</html>