forked from yui/nodejs-yui3
-
Notifications
You must be signed in to change notification settings - Fork 36
/
selector.js
executable file
·507 lines (414 loc) · 28.1 KB
/
selector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/env node
var sys = require('sys'),
fs = require('fs');
var YUI = require("../lib/node-yui3").YUI;
// TODO: This should pass, but currently doesn't.
// This will work for YUI core, but any submodules are in different files
// This will work better once the 3.1.0 version of YUI is available
// And you can combo handle all the submodules into one request.
//require("assert").equal( global.YUI, undefined, "global yui created");
//Now use non-DOM related YUI utilities
YUI({
//Only set these if you want to load locally
loaderPath: 'loader/loader-debug.js',
base: './yui3/build/',
filter: 'debug',
logExclude: {
'Selector': true,
'attribute': true,
'base': true,
'get': true,
'loader': true,
'yui': true,
'widget': true,
'event': true
},
debug: true
}).use('nodejs-dom', 'event', 'node-base', 'tabview', 'test', 'selector-css3', function(Y) {
var document = Y.Browser.document;
var window = Y.Browser.window;
/* {{{ Selector Test Suite */
var runTests = function() {
//Y.Selector.query('[href$="^html"]');
String.prototype.test = function() {
// simulate Moo monkey-patch
return false;
};
Y.Dom = {
get: function (id) {
return document.getElementById(id);
},
getChildren: function (el) {
el = typeof el === 'string' ? document.getElementById(el) : el;
var children = [],
i, len;
for (i = 0, len = el.childNodes.length; i < len; ++i) {
if (el.childNodes[i].nodeType === 1) {
children.push(el.childNodes[i]);
}
}
return children;
},
getElementsByClassName: function (clz, tag, root) {
root = typeof root === 'string' ?
document.getElementById(root) : root || document;
var els = root.getElementsByTagName(tag || '*'),
nodes = [],
S = ' ',
i, len;
clz = S + clz + S;
for (i = 0, len = els.length; i < len; ++i) {
if ((S + els[i].className + S).indexOf(clz)) {
nodes.push(els[i]);
}
}
return nodes;
},
getFirstChild: function (el) {
el = typeof el === 'string' ? document.getElementById(el) : el;
var node = el.firstChild;
while (node && node.nodeType !== 1) {
node = node.nextSibling;
}
return node;
},
getLastChild: function (el) {
el = typeof el === 'string' ? document.getElementById(el) : el;
var node = el.lastChild;
while (node && node.nodeType !== 1) {
node = node.previousSibling;
}
return node;
},
addClass: function (els, clz) {
var S = ' ',
i, len;
if (typeof els === 'string') {
els = [ document.getElementById( els ) ];
} else if (els.nodeType === 1) {
els = [ els ];
} else if (({}).toString.call(els) === '[object Array]') {
for (i = els.length - 1; i >= 0; --i) {
if (typeof els[i] === 'string') {
els[i] = document.getElementById(els[i]);
if (!els[i]) {
els.splice(i,1);
}
}
}
} else {
els = [];
}
clz = S + clz + S;
for (i = 0, len = els.length; i < len; ++i) {
if ((S + els[i].className + S).indexOf(clz)) {
els[i].className += clz.replace(/\s+$/,'');
}
}
}
};
var suite = new Y.Test.Suite("Selector Suite");
var Selector = Y.Selector;
var Assert = Y.Assert;
var ArrayAssert = Y.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 Y.Test.Case({
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)');
Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[0], 'dd'), 'dd (dd1)');
Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[1], 'dd'), 'dd (dd2)');
Assert.isFalse(Selector.test(document.getElementsByTagName('dd')[0], '.test-dd2'), '.test-dd2 (dd1)');
Assert.isFalse(Selector.test(document.getElementsByTagName('dd')[1], '.test-dd1'), '.test-dd1 (dd2)');
Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[0], '.test-dd1'), '.test-dd1');
Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[1], '.test-dd2'), '.test-dd2');
Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[0], 'dd'), 'dd (dd1)');
Assert.isTrue(Selector.test(document.getElementsByTagName('dd')[1], 'dd'), 'dd (dd2)');
// with optional root arg
var form = Y.DOM.byId('form-root');
var input = form.getElementsByTagName('input')[0];
Assert.isTrue(Selector.test(input, 'input', form));
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');
//sys.puts('##------------------------------------------------------');
//sys.puts(sys.inspect(Y.DOM.byId('mod1')));
//sys.puts(sys.inspect($('a span, a', Y.DOM.byId('mod1'))));
//sys.puts(sys.inspect(Y.DOM.byId('mod1')));
//sys.puts('##------------------------------------------------------');
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)");
// 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');
},
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
},
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, div', null, true), "$('body, div', 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\]')");
/*Selector fails this already..
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', $('#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()");
}
});
suite.add(selectorQueryAll);
suite.add(simpleTest);
Y.Test.Runner.add(suite);
Y.Test.Runner.run();
};
/* }}} */
Y.log('JSDom testing..');
//sys.puts('Inside1: ' + sys.inspect(process.memoryUsage()));
fs.readFile('./selector.html', encoding="utf-8", function(err, data) {
///Y.log(data);
document.body.innerHTML = data;
//Y.log(document.body.outerHTML);
Y.log('Document loaded, run tests..');
runTests();
});
});