selector.html revision b3190f6ee8689b5059b56dbe5b12fc04ba371679
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai.yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai background: #070;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai color: #fff;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai.yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai background: #700;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai color: #fff;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<script type="text/javascript" src="/build/yui/yui.js"></script>
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desaionload = function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var SELECTOR_MODULE = 'selector-css3';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai YUI({filter: 'raw'}).use('node', SELECTOR_MODULE, 'console', 'test', 'dom-deprecated', function(Y) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // simulate Moo monkey-patch
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return false;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.Test.Runner.setName('Selector Tests');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai get: function (id) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getChildren: function (el) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai el = typeof el === 'string' ? document.getElementById(el) : el;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var children = [],
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = 0, len = el.childNodes.length; i < len; ++i) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getElementsByClassName: function (clz, tag, root) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai document.getElementById(root) : root || document;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var els = root.getElementsByTagName(tag || '*'),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if ((S + els[i].className + S).indexOf(clz)) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return nodes;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getFirstChild: function (el) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai el = typeof el === 'string' ? document.getElementById(el) : el;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai while (node && node.nodeType !== 1) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return node;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getLastChild: function (el) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai el = typeof el === 'string' ? document.getElementById(el) : el;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai while (node && node.nodeType !== 1) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return node;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai addClass: function (els, clz) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var S = ' ',
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (typeof els === 'string') {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai els = [ document.getElementById( els ) ];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai } else if (els.nodeType === 1) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai els = [ els ];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai } else if (({}).toString.call(els) === '[object Array]') {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = els.length - 1; i >= 0; --i) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (typeof els[i] === 'string') {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai els[i] = document.getElementById(els[i]);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (!els[i]) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai clz = S + clz + S;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var suite = new Y.Test.Suite("Selector Suite");
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var demoLastChild = children[children.length - 1];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var all = Y.Dom.get('test-inputs').getElementsByTagName('input');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai ArrayAssert.itemsAreEqual([all[0], all[1], all[2]], Selector.filter(all, '[type=checkbox]'), '[type=checkbox]');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai ArrayAssert.itemsAreEqual([], Selector.filter(null, '[type=checkbox]'), 'null input');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai //ArrayAssert.itemsAreEqual([document.getElementById('test-inputs')], Selector.filter(['root-test', 'test-inputs'], 'form'), 'form (string inputs)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // no longer supporting string input for element
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai //ArrayAssert.itemsAreEqual([document.getElementById('test-inputs')], Selector.filter(['root-test', document.getElementById('test-inputs'), document.createTextNode('foo')], 'form'), 'form (mixed inputs)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), '[type=checkbox], button'), '[type=checkbox], button');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), 'button, [type=checkbox]'), 'button, [type=checkbox]');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), 'foo, button'), 'foo, button');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(null, '#foo'), ';ull input');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(document.createTextNode('foo'), '#foo'), 'textNode input');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('test-lang-en-us'), '[lang|=en]'), '[lang|=en] (lang="en-us")');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('test-lang-en'), '[lang|=en]'), '[lang|=en] (lang="en")');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.Dom.get('test-lang-none'), '[lang|=en]'), '[lang|=en] false pos');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), 'for [type=checkbox]'), 'for [type=checkbox] false pos');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), 'form [type=checkbox]'), 'form [type=checkbox]');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), 'for [type=checkbox]'), 'for [type=checkbox] false pos');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('checkbox-checked'), '[type=checkbox]:checked'), 'type=checkbox:checked');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('radio-checked'), ':checked'), ':checked (radio)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.Dom.get('radio-unchecked'), ':checked'), ':checked (radio) false pos');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.Dom.get('checkbox-unchecked'), '[type=checkbox]:checked'), 'type=checkbox:checked false pos');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(Y.Dom.get('checkbox-unchecked'), '[type=checkbox]:not(:checked)'), 'type=checkbox:not(:checked)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[0], 'dd'), 'dd (dd1)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[1], 'dd'), 'dd (dd2)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(document.getElementsByTagName('dd')[0], '.test-dd2'), '.test-dd2 (dd1)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(document.getElementsByTagName('dd')[1], '.test-dd1'), '.test-dd1 (dd2)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[0], '.test-dd1'), '.test-dd1');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[1], '.test-dd2'), '.test-dd2');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[0], 'dd'), 'dd (dd1)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[1], 'dd'), 'dd (dd2)');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var input = form.getElementsByTagName('input')[0];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isTrue(Selector.test(input, 'input', form));
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Assert.isFalse(Selector.test(Y.DOM.byId('foo-bar'), 'input#foo-bar', form));
"$('h2', Y.DOM.byId('test:colon'), true)");
"$('.foo #demo-last-child', true, document.body)");
//ArrayAssert.itemsAreEqual(document.body.getElementsByTagName('p'), $('body p', document.body), "$('body p', document.body)");
ArrayAssert.itemsAreEqual([], $('#root-test li', Y.Dom.get('nth-test')), 'id selector w/root false pos');
ArrayAssert.itemsAreEqual(Y.Dom.get('demo').getElementsByTagName('p'), $('#demo.foo p'), '#demo.foo p');
//ArrayAssert.itemsAreEqual(Y.Dom.getElementsByClassName('para'), $('[class~=para]'), '[class~=para]');
//ArrayAssert.itemsAreEqual(Y.Dom.getElementsByClassName('para'), $('[class~="para"]'), '[class~="para"]');
//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');
Assert.areEqual(document.getElementById('label-checkbox-unchecked'), $('label[for=checkbox-unchecked]', null, true), 'for attribute');
Assert.areEqual($('.not-button', root).length, $('input:not([type=button])', root).length, '.not-button = input:not([type=button]) - root query');
//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)");
ArrayAssert.itemsAreEqual([demoFirstChild, Y.DOM.next(demoFirstChild)], $('#demo > p:not(.last)'), '#demo > p:not(.last)');
Assert.areEqual(Y.DOM.byId('test-custom-attr'), $("#test-custom-attr[foo=bar]", null, true), "#test-custom-attr[foo=bar], null, true");
Assert.areEqual(Y.DOM.byId('test-custom-attr'), $("div[foo=bar]", null, true), "div[foo=bar], null, true");
Assert.areEqual(Y.DOM.byId('test-custom-attr'), $("div#test-custom-attr[foo=bar]", null, true), "div#test-custom-attr[foo=bar], null, true");
Assert.areEqual(Y.DOM.byId('href-test'), $('[href$=".html"]', null, true), "$('[href$=\".html\"]')");
Assert.isNull($('[href$="?html"]', null, true), "$('[href$=\?html\]')");
var simpleTest = new Y.Test.Case({
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-of-type'), 'only-of-type false pos');
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)');
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');
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');
Assert.isTrue(Selector.test(Y.Dom.getFirstChild('demo'), 'p.first.para'), 'tag & multiple class match');
Assert.areEqual(Y.DOM.byId('disabled-button'), $('#test-inputs :disabled', null, true), "$('#test-inputs :disabled')");
Assert.areEqual(Y.DOM.byId('checkbox-unchecked'), $('#test-inputs input:enabled', null, true), "$('#test-inputs input:enabled')");
Assert.areEqual('iframe foo', Y.Lang.trim($('#demo li', frameDoc, true).innerHTML), "Y.get('#demo li', frameDoc, true)");
var demo = Y.DOM.byId('demo'),
if (el.parentNode === demo) {
paraChildren.push(el);
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');
var node = Y.DOM.byId('demo-first-child');
var nodes = document.getElementsByTagName('div'),
filtered = Y.Selector.filter(nodes, function(n) {
return n.tagName == 'SPAN';
ArrayAssert.itemsAreEqual([], filtered);
var node = document.getElementById('1');
Assert.isNotNull(node);
var node = document.getElementById('1');
Assert.isNotNull(node);
var node = document.getElementById('foo:bar');
Assert.isNotNull(node);
var node = document.getElementById('foo:bar');
Assert.isNotNull(node);
var node = document.getElementById('foo:bar');
Assert.isNotNull(node);
var node = document.getElementById('1');
Assert.isNotNull(node);
var node = document.getElementById('test-dl');
Y.Selector.query(':last-child', node, true));
suite.add(selectorQueryAll);
suite.add(simpleTest);
Y.Test.Runner.add(suite);