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<html>
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<head>
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<title>Selector Test Suite</title>
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<style type="text/css">
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai.yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai background: #070;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai color: #fff;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai.yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai background: #700;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai color: #fff;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai</style>
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<script type="text/javascript" src="/build/yui/yui.js"></script>
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai<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 String.prototype.test = function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // simulate Moo monkey-patch
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return false;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai };
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.Test.Runner.setName('Selector Tests');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.Dom = {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai get: function (id) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return document.getElementById(id);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getChildren: function (el) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai el = typeof el === 'string' ? document.getElementById(el) : el;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var children = [],
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i, len;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = 0, len = el.childNodes.length; i < len; ++i) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (el.childNodes[i].nodeType === 1) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai children.push(el.childNodes[i]);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return children;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getElementsByClassName: function (clz, tag, root) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai root = typeof root === 'string' ?
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai document.getElementById(root) : root || document;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var els = root.getElementsByTagName(tag || '*'),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai nodes = [],
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai S = ' ',
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i, len;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai clz = S + clz + S;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = 0, len = els.length; i < len; ++i) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if ((S + els[i].className + S).indexOf(clz)) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai nodes.push(els[i]);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return nodes;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getFirstChild: function (el) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai el = typeof el === 'string' ? document.getElementById(el) : el;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var node = el.firstChild;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai while (node && node.nodeType !== 1) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai node = node.nextSibling;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return node;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai getLastChild: function (el) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai el = typeof el === 'string' ? document.getElementById(el) : el;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var node = el.lastChild;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai while (node && node.nodeType !== 1) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai node = node.previousSibling;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return node;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai addClass: function (els, clz) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var S = ' ',
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i, len;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
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 els.splice(i,1);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai } else {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai els = [];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai clz = S + clz + S;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = 0, len = els.length; i < len; ++i) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if ((S + els[i].className + S).indexOf(clz)) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai els[i].className += clz.replace(/\s+$/,'');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai };
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai new Y.Console({height:'50em'}).render();
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var suite = new Y.Test.Suite("Selector Suite");
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var Selector = Y.Selector;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var Assert = Y.Assert;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var ArrayAssert = Y.ArrayAssert;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var $ = Selector.query;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var demo = Y.Dom.get('demo');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai children = Y.Dom.getChildren(demo);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var demoFirstChild = children[0];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var demoLastChild = children[children.length - 1];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var selectorQueryAll = new Y.Test.Case({
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai name: 'Query All',
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai testFilter: function() {
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 },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai testTest: function() {
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
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
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
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
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
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
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // with optional root arg
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var form = Y.DOM.byId('form-root');
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));
var form = Y.DOM.byId('test-inputs');
Assert.isFalse(Selector.test(input, 'input', form));
Assert.isFalse(Selector.test(form, 'form', form));
Assert.isTrue(Selector.test(form, 'form', form.parentNode));
Assert.isTrue(Selector.test(Y.DOM.byId('foo-bar'), 'input#foo-bar', form));
Assert.isFalse(Selector.test(Y.DOM.byId('foo-bar'), '#test-inputs input#foo-bar', form));
Assert.isTrue(Selector.test(Y.DOM.byId('foo-bar'), '#test-inputs input#foo-bar', form.parentNode));
},
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');
Assert.areEqual(
$('a span, a', Y.DOM.byId('mod1')).length,
$('a, a span', Y.DOM.byId('mod1')).length,
"$('a, a span') === $('a span, a')");
var node = document.createElement('div');
node.innerHTML = '<li><em>foo</em></li>';
Assert.areEqual(node.getElementsByTagName('em')[0], $('li em', node, true), 'off-dom: li em');
Assert.isNull($('div li em', node, true), 'off-dom: div li em');
Assert.areEqual(Y.DOM.byId('test:colon').getElementsByTagName('h2')[0],
$('h2', Y.DOM.byId('test:colon'), true),
"$('h2', Y.DOM.byId('test:colon'), true)");
Assert.areEqual(Y.DOM.byId('demo-last-child'), $('.foo #demo-last-child', document.body, true),
"$('.foo #demo-last-child', true, document.body)");
// standard: no element-scoped query
//ArrayAssert.itemsAreEqual(document.body.getElementsByTagName('p'), $('body p', document.body), "$('body p', document.body)");
// 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');
all = Y.Dom.get('nth-type-test').getElementsByTagName('div');
even = Y.Dom.getElementsByClassName('even', 'div', 'nth-type-test');
//ArrayAssert.itemsAreEqual(all[2], $('#nth-type-test div:nth-of-type(3)'),
//"$('#nth-type-test div:nth-of-type(3)')");
//ArrayAssert.itemsAreEqual(even, $('#nth-type-test div:nth-of-type(even)'),
//"$('#nth-type-test div:nth-of-type(even)')");
//ArrayAssert.itemsAreEqual(even, $('#nth-type-test div:nth-of-type(2n)'),
//"$('#nth-type-test div:nth-of-type(2n)')");
},
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([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');
var root = Y.DOM.byId('test-inputs');
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('foo-bar'), $("[id^='foo-']", null, true), "[id^='foo-'], null, true");
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('test-custom-attr'), $("div[foo]", null, true), "div[foo], null, true");
ArrayAssert.itemsAreEqual([Y.DOM.byId('test-custom-attr')], $("[foo]"), "[foo]");
Assert.areEqual(document.body, $('body, span', null, true), "$('body, span', null, true)");
Assert.areEqual(document.getElementById('foo-bar'),
$('[id=foo-bar]', null, true),
"$('[id=foo-bar]', null, true)");
Assert.areEqual(document.getElementById('test-custom-attr'),
$('[foo-bar]', null, true),
"$('[foo-bar]', null, true)");
Assert.areEqual(document.getElementById('foo-bar'),
$('[aria-controls]', null, true),
"$('[aria-controls]', null, true)");
Assert.areEqual(document.getElementById('foo-bar'),
$('[-hyphen-start]', null, true),
"$('[-hyphen-start]', null, true)");
Assert.areEqual(Y.DOM.byId('test-lang-en-us'), $('[lang|=en]', null, true), "$('[lang|=en]')");
Assert.areEqual(Y.DOM.byId('href-test'), $('[href$=".html"]', null, true), "$('[href$=\".html\"]')");
Assert.isNull($('[href$="?html"]', null, true), "$('[href$=\?html\]')");
/* failing in brute (ticket 2528057)
Assert.areEqual(Y.DOM.byId('test:colon'),
$('#test\\:colon', null, true),
"$('#test\\:colon', null, true)");
*/
}
});
var simpleTest = new Y.Test.Case({
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.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');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), ':not(.foo)'), 'not(.foo)');
/* 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.isTrue(Selector.test(Y.Dom.get('href-test'), '[href$=".html"]'), 'href$=".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'), 'div');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), 'body div'), 'body div');
Assert.isFalse(Selector.test(Y.Dom.get('demo'), 'span'), 'tag false positive');
Assert.isTrue(Selector.test(Y.Dom.get('demo'), '*'), 'universal tag');
},
testDisabled: function() {
Assert.areEqual(Y.DOM.byId('disabled-button'), $('#test-inputs :disabled', null, true), "$('#test-inputs :disabled')");
},
testEnabled: function() {
Assert.areEqual(Y.DOM.byId('checkbox-unchecked'), $('#test-inputs input:enabled', null, true), "$('#test-inputs input:enabled')");
},
testIframe: function() {
var frameDoc = Y.DOM.byId('test-frame').contentWindow.document;
Assert.areEqual('iframe foo', Y.Lang.trim($('#demo li', frameDoc, true).innerHTML), "Y.get('#demo li', frameDoc, true)");
},
// to support:
// $('> p', myNode) === ('#my-node-id > p');
// $('+ p', myNode) === ('#my-node-id + p');
// $('~ p', myNode) === ('#my-node-id ~ p');
testScoped: function() {
var demo = Y.DOM.byId('demo'),
paraChildren = [];
Y.each(demo.getElementsByTagName('p'), function(el) {
if (el.parentNode === demo) {
paraChildren.push(el);
}
});
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');
},
testAncestor: function() {
var node = Y.DOM.byId('demo-first-child');
Assert.isNull(Y.Selector.ancestor(node, 'p'));
Assert.areEqual(node, Y.Selector.ancestor(node, 'p', true));
Assert.areEqual(document.body, Y.Selector.ancestor(node, 'body', true));
Assert.areEqual(node.parentNode, Y.Selector.ancestor(node, 'div'));
},
testChecked: function() {
Assert.areEqual(4, Y.all('#test-inputs :checked').size(), "Y.all('#test-inputs :checked').size()");
},
'should apply function for filter test': function() {
var nodes = document.getElementsByTagName('div'),
filtered = Y.Selector.filter(nodes, function(n) {
return n.tagName == 'SPAN';
});
ArrayAssert.itemsAreEqual([], filtered);
},
'should find element with quoted number ID': function() {
var node = document.getElementById('1');
Assert.isNotNull(node);
Assert.areEqual(node, Y.Selector.query('[id="1"]', null, true));
},
'should find element with number ID': function() {
var node = document.getElementById('1');
Assert.isNotNull(node);
Assert.areEqual(node, Y.Selector.query('[id=1]', null, true));
},
'should find element with colon in quoted ID': function() {
var node = document.getElementById('foo:bar');
Assert.isNotNull(node);
Assert.areEqual(node, Y.Selector.query('[id="foo:bar"]', null, true));
},
'should find element with colon in ID': function() {
var node = document.getElementById('foo:bar');
Assert.isNotNull(node);
Assert.areEqual(node, Y.Selector.query('[id=foo:bar]', null, true));
},
'should find element with colon in ID shorthand': function() {
var node = document.getElementById('foo:bar');
Assert.isNotNull(node);
Assert.areEqual(node, Y.Selector.query('#foo\\:bar', null, true));
},
'should find element with number ID shorthand': function() {
var node = document.getElementById('1');
Assert.isNotNull(node);
Assert.areEqual(node, Y.Selector.query('#1', null, true));
},
'should omit comments from children': function() {
var node = document.getElementById('test-dl');
debugger;
Assert.areEqual(document.getElementById('last-dd'),
Y.Selector.query(':last-child', node, true));
}
});
suite.add(selectorQueryAll);
suite.add(simpleTest);
Y.Test.Runner.add(suite);
Y.Test.Runner.run();
});
};
</script>
<style type="text/css">
</style>
</head>
<body class="yui3-skin-sam">
<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>
<div id="nth-type-test">
<span>span</span>
<div>first div</div>
<span>span</span>
<div class="even">second div</div>
<div>third div</div>
<div class="even">fourth div</div>
<span>span</span>
</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" disabled id="disabled-button">
<input id="foo-bar" name="foo.bar" class="not-button" aria-controls="foo-bar" -hyphen-start="true">
<select>
<option>foo</option>
<option selected>bar</option>
</select>
</form>
<form id="form-root">
<input type="button" value="foo">
<input name="foo" class="text-input">
</form>
<div id="mod1">
<div><h3>H3 - Title</h3></div>
<div><p>lorem ipsum dolor sit <a href="#"><span>link</span></a></p></div>
</div>
<div class="Bar" id="class-bar"></div>
<div id="contains-special">contains "' & ]</div>
<div id="test-rel-div" rel="foo2"></div>
<iframe src="test-frame.html" id="test-frame"></iframe>
<dl id="test-dl">
<dt>Window size</dt>
<dd class="test-dd1">dd1</dd>
<dt>Document size</dt>
<dd id="last-dd" class="test-dd2">dd2</dd>
<!-- test comment -->
</dl>
<div id="test:colon"><h2>test</h2></div>
<div id="test-custom-attr" foo="bar" foo-bar="bar">custom attribute</div>
<div id="1"></div>
<div id="foo:bar"></div>
</body>
</html>