/
dom.js
2353 lines (1890 loc) · 88.5 KB
/
dom.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
t.module("glow.dom selectors");
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
t.test("broken", function() {
t.expect(7);
try {
glow.dom.get("[");
t.ok(false, "[ Broken Selector");
} catch (e) {
t.ok(true, "[ Broken Selector");
}
try {
glow.dom.get("(");
t.ok(false, "( Broken Selector");
} catch (e) {
t.ok(true, "( Broken Selector");
}
try {
glow.dom.get("{");
t.ok(false, "{ Broken Selector");
} catch (e) {
t.ok(true, "{ Broken Selector");
}try {
glow.dom.get("<");
t.ok(false, "< Broken Selector");
} catch (e) {
t.ok(true, "< Broken Selector");
}
try {
glow.dom.get("()");
t.ok(false, "() Broken Selector");
} catch (e) {
t.ok(true, "() Broken Selector");
}
try {
glow.dom.get("<>");
t.ok(false, "<> Broken Selector");
} catch (e) {
t.ok(true, "<> Broken Selector");
}
try {
glow.dom.get("{}");
t.ok(false, "{} Broken Selector");
} catch (e) {
t.ok(true, "{} Broken Selector");
}
});
t.test("id", function() {
t.expect(22);
t.selects("#body", ["body"], "ID Selector");
t.selects("body#body", ["body"], "ID Selector w/ Element");
t.selects("ul#firstUL", ["firstUL"], "ID Selector w/ Element");
t.selects("#firstp #simon1", ["simon1"], "ID selector with existing ID descendant");
t.selects("#firstp #foobar", [], "ID selector with non-existant descendant");
t.selects("#台北Táiběi", ["台北Táiběi"], "ID selector using UTF8");
t.selects("#台北Táiběi, #台北", ["台北Táiběi", "台北"], "Multiple ID selectors using UTF8");
t.selects("div #台北", ["台北"], "Descendant ID selector using UTF8");
t.selects("form > #台北", ["台北"], "Child ID selector using UTF8");
t.selects("#foo\\:bar", ["foo:bar"], "Escaped ID");
t.selects("#test\\.foo\\[5\\]bar", ["test.foo[5]bar"], "Escaped ID");
t.selects("div #foo\\:bar", ["foo:bar"], "Descendant escaped ID");
t.selects("div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"], "Descendant escaped ID");
t.selects("form > #foo\\:bar", ["foo:bar"], "Child escaped ID");
t.selects("form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"], "Child escaped ID");
t.selects("#form > #radio1", ["radio1"], "ID Selector, child ID present");
t.selects("#form #first", [], "ID Selector, not an ancestor ID");
t.selects("#form > #option1a", [], "ID Selector, not a child ID");
t.selects("#foo > *", ["sndp", "en", "sap"], "All Children of ID");
t.selects("#firstUL > *", [], "All Children of ID with no children");
t.selects("#asdfasdf #foobar", [], "ID selector with non-existant ancestor");
t.isSet( glow.dom.get("body").get("#form"), q("form"), "ID selector within the context of another element" );
});
t.test("class", function() {
t.expect(16);
t.selects(".blog", ["mark","simon"], "Class Selector");
t.selects(".blog.link", ["simon"], "Class Selector");
t.selects("a.blog", ["mark","simon"], "Class Selector w/ Element");
t.selects("p .blog", ["mark","simon"], "Parent Class Selector");
t.selects(".台北Táiběi", ["utf8class1"], "Class selector using UTF8");
t.selects(".台北", ["utf8class1","utf8class2"], "Class selector using UTF8");
t.selects(".台北Táiběi.台北", ["utf8class1"], "Class selector using UTF8");
t.selects(".台北Táiběi, .台北", ["utf8class1","utf8class2"], "Class selector using UTF8");
t.selects("div .台北Táiběi", ["utf8class1"], "Descendant class selector using UTF8");
t.selects("form > .台北Táiběi", ["utf8class1"], "Child class selector using UTF8");
t.selects(".foo\\:bar", ["foo:bar"], "Escaped Class");
t.selects(".test\\.foo\\[5\\]bar", ["test.foo[5]bar"], "Escaped Class");
t.selects("div .foo\\:bar", ["foo:bar"], "Descendant escaped Class");
t.selects("div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"], "Descendant escaped Class");
t.selects("form > .foo\\:bar", ["foo:bar"], "Child escaped Class");
t.selects("form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"], "Child escaped Class");
});
t.test("multiple", function() {
t.expect(4);
t.selects("a.blog, #main p", ["mark","simon","firstp","ap","sndp","en","sap","first"], "Comma Support");
t.selects("a.blog , #main p", ["mark","simon","firstp","ap","sndp","en","sap","first"], "Comma Support");
t.selects("a.blog ,#main p", ["mark","simon","firstp","ap","sndp","en","sap","first"], "Comma Support");
t.selects("a.blog,#main p", ["mark","simon","firstp","ap","sndp","en","sap","first"], "Comma Support");
});
t.todo("child and adjacent", function() {
t.expect(19);
t.selects("p > a", ["simon1","google","groups","mark","yahoo","simon"], "Child");
t.selects("p> a", ["simon1","google","groups","mark","yahoo","simon"], "Child");
t.selects("p >a", ["simon1","google","groups","mark","yahoo","simon"], "Child");
t.selects("p>a", ["simon1","google","groups","mark","yahoo","simon"], "Child");
t.selects("p > a.blog", ["mark","simon"], "Child w/ Class");
t.selects("code > *", ["anchor1","anchor2"], "All Children");
t.selects("#dl p > * > *", ["anchor1","anchor2"], "All Grandchildren");
t.selects("a + a", ["groups"], "Adjacent");
t.selects("a +a", ["groups"], "Adjacent");
t.selects("a+ a", ["groups"], "Adjacent");
t.selects("a+a", ["groups"], "Adjacent");
t.selects("p + p", ["ap","en","sap"], "Adjacent");
t.selects("a + a, code > a", ["groups","anchor1","anchor2"], "Comma, Child, and Adjacent");
t.selects("p:first-child", ["firstp","sndp"], "First Child");
t.selects("p:nth-child(1)", ["firstp","sndp"], "Nth Child");
t.selects("p:last-child", ["sap"], "Last Child");
t.selects("a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon"], "Last Child");
t.selects("#main form#form > *:nth-child(2)", ["text2"], "Nth-child");
t.selects("#main form#form > :nth-child(2)", ["text2"], "Nth-child");
});
t.todo("attributes", function() {
t.expect(20);
t.selects("a[title]", ["google"], "Attribute Exists");
t.selects("*[title]", ["google"], "Attribute Exists");
t.selects("[title]", ["google"], "Attribute Exists");
t.selects("a[rel='bookmark']", ["simon1"], "Attribute Equals");
t.selects('a[rel="bookmark"]', ["simon1"], "Attribute Equals");
t.selects("a[rel=bookmark]", ["simon1"], "Attribute Equals");
t.selects("#form input[type='hidden'],#form input[type='radio']", ["hidden1","radio1","radio2"], "Multiple Attribute Equals");
t.selects("#form input[type=\"hidden\"],#form input[type='radio']", ["hidden1","radio1","radio2"], "Multiple Attribute Equals");
t.selects("#form input[type=hidden],#form input[type=radio]", ["hidden1","radio1","radio2"], "Multiple Attribute Equals");
t.selects("span[lang=中文]", ["台北"], "Attribute selector using UTF8");
t.selects("a[href ^= 'http://www']", ["google","yahoo"], "Attribute Begins With");
t.selects("a[href $= 'org/']", ["mark"], "Attribute Ends With");
t.selects("a[href *= 'google']", ["google","groups"], "Attribute Contains");
t.selects("#select1 option[selected]", ["option1a"], "Select options via [selected]");
t.selects("#select2 option[selected]", ["option2d"], "Select options via [selected]");
t.selects("#select3 option[selected]", ["option3b", "option3c"], "Select options via [selected]");
t.selects("input[name='foo[bar]']", ["hidden2"], "Grouped Form Elements");
t.selects("#form select:not([multiple])", ["select1", "select2"], ":not() Existing attribute");
t.selects("#form select:not([name=select1])", ["select2", "select3"], ":not() Equals attribute");
t.selects("#form select:not([name='select1'])", ["select2", "select3"], ":not() Equals quoted attribute");
});
t.module("glow.dom.NodeList creation");
t.test("glow.dom.NodeList constructor", function () {
t.expect(1);
var emptyNodeList = new glow.dom.NodeList();
t.equals(emptyNodeList.length, 0, "create empty NodeList");
});
t.test("glow.dom.create", function () {
t.expect(16);
var NodeList = glow.dom.create(' <div id="aNewNode1">test</div> <div id="aNewNode2">test</div> ');
t.ok(NodeList.length == 2, "two element NodeList created with glow.dom.create");
t.ok(NodeList[0].id == "aNewNode1" && NodeList[1].id == "aNewNode2", "nodes created with glow.dom.create as expected");
var error;
try {
var nodes = glow.dom.create(' <div>test</div> hello <div>test</div> ');
} catch (e) {
error = e;
}
t.ok(error.message && error.message.indexOf("Text must be wrapped in an element") > -1, "Throws error with non-whitespace text nodes");
//IE doesn't like link elements inserted via innerHTML
var myLink = glow.dom.create('<link type="text/css" rel="stylesheet" media="screen" href="/static/users/style/id-panel.css" />')
t.ok(myLink[0] && myLink[0].nodeName.toLowerCase() == "link", "Link element created");
// glow.dom.create needs to work with elements that cannot be a child of a DIV
// ***************************************************************************
// CAPTION NODE
var caption = glow.dom.create("<caption></caption>");
t.ok(caption[0] && caption[0].nodeName.toLowerCase() == "caption", "Caption element created");
// THEAD NODE
var thead = glow.dom.create("<thead></thead>");
t.ok(thead[0] && thead[0].nodeName.toLowerCase() == "thead", "Thead element created");
// TH NODE
var th = glow.dom.create("<th></th>");
t.ok(th[0] && th[0].nodeName.toLowerCase() == "th", "Th element created");
// COLGROUP NODE
var colgroup = glow.dom.create("<colgroup />");
t.ok(colgroup[0] && colgroup[0].nodeName.toLowerCase() == "colgroup", "Colgroup element created");
// TBODY NODE
var tbody = glow.dom.create("<tbody></tbody>");
t.ok(tbody[0] && tbody[0].nodeName.toLowerCase() == "tbody", "Tbody element created");
// TR NODE
var tr = glow.dom.create("<tr></tr>");
t.ok(tr[0] && tr[0].nodeName.toLowerCase() == "tr", "Tr element created");
// TD NODE
var td = glow.dom.create("<td></td>");
t.ok(td[0] && td[0].nodeName.toLowerCase() == "td", "Td element created");
// TFOOT NODE
var tfoot = glow.dom.create("<tfoot></tfoot>");
t.ok(tfoot[0] && tfoot[0].nodeName.toLowerCase() == "tfoot", "Tfoot element created");
// OPTION NODE
var option = glow.dom.create("<option></option>");
t.ok(option[0] && option[0].nodeName.toLowerCase() == "option", "Option element created");
// LEGEND NODE
var legend = glow.dom.create("<legend></legend>");
t.ok(legend[0] && legend[0].nodeName.toLowerCase() == "legend", "Legend element created");
// SCRIPT NODE
var script = glow.dom.create("<script></script>");
t.ok(script[0] && script[0].nodeName.toLowerCase() == "script", "Script element created");
// STYLE NODE
var style = glow.dom.create('<style type="text/css">#abc1234 { margin:0; }</style>');
t.equals(style[0].nodeName.toLowerCase(), "style", "Style element created");
});
t.test("glow.dom.create - interpolate", function() {
t.expect(4);
var iNodes = glow.dom.create("<div>{foo}</div>", {
interpolate: {foo: "FOO"}
});
t.equals(iNodes.text(), "FOO", "Basic Interpolation");
var data = {
faveElms: '<strong>strong</strong> & <h1>h1</h1>',
food: 'cake',
lt: '<',
gt: '>'
},
template = '<div>My favourite elements are {faveElms}. Do you have any {food}? Here\'s a {lt}span{gt}. This {<b>placeholder</b>} does not exist.</div>';
var elm = glow.dom.create(template, {
interpolate: data
});
t.equals(elm.get("*").length, 4, "HTML not escaped");
elm = glow.dom.create(template, {
interpolate: data,
escapeHtml: true
});
t.equals(elm.get("span, strong, h1").length, 0, "HTML escaped, no unwanted elements created");
t.equals(elm.text(), 'My favourite elements are <strong>strong</strong> & <h1>h1</h1>. Do you have any cake? Here\'s a <span>. This {placeholder} does not exist.', "HTML escaped, text retained");
});
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
t.test("glow.dom.get - selector", function () {
t.expect(2);
var NodeList = glow.dom.get("#form");
t.ok(NodeList instanceof glow.dom.NodeList, "get returns a NodeList");
t.equals(NodeList[0], document.getElementById("form"), "NodeList contains node for simple id selector");
});
t.test("glow.dom.get - nodes", function () {
t.expect(4);
var singleElementNodeList = glow.dom.get(document.getElementById("option1a"));
t.ok(singleElementNodeList.length == 1, "single element NodeList created with on has one element");
t.equals(
singleElementNodeList[0],
document.getElementById("option1a"),
"single element NodeList has correct element"
);
var twoElementNodeList = glow.dom.get([
document.getElementById("option1a"),
document.getElementById("option1b")
]);
t.ok(twoElementNodeList.length == 2, "two element NodeList created with get has two elements");
t.isSet(
[twoElementNodeList[0], twoElementNodeList[1]],
q("option1a", "option1b"),
"elements in NodeList created by get are as expected"
);
});
t.test("glow.dom.NodeList.get", function() {
t.expect(6);
t.isSet(glow.dom.get("#foo").get("#sndp"), q("sndp"), "find nested with css");
t.isSet(glow.dom.get("#foo").get("#ap"), [], "respects current items (css)");
t.isSet(glow.dom.get("#foo").get(document.getElementById("sndp")), q("sndp"), "find nested with node");
t.isSet(glow.dom.get("#foo").get(document.getElementById("ap")), [], "respects current items (node)");
t.isSet(glow.dom.get("#foo").get(document.getElementById("foo")), [], "Items can't contain themselves");
t.equals(glow.dom.get("#form").get("input").length, 9, "Get form elements within a form");
});
t.module("glow.dom.NodeList array access");
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
// glow.dom.NodeList.length
// Number of content nodes
t.test("glow.dom.NodeList.length", function () {
t.expect(2);
var emptyNodeList = new glow.dom.NodeList();
t.equals(emptyNodeList.length, 0, "zero element NodeList returns zero as the length");
var NodeList = glow.dom.get([
document.getElementById("option1a"),
document.getElementById("option1b")
]);
t.ok(NodeList.length == 2, "two element NodeList returns two as the length");
});
// glow.dom.NodeList.item(Number)
// Get node at index 'Number' (for NodeList compatibility)
t.test("glow.dom.NodeList.item", function () {
t.expect(1);
var option1a = document.getElementById("option1a");
var option1b = document.getElementById("option1b");
var NodeList = glow.dom.get([option1a, option1b]);
t.ok(NodeList.item(0) == option1a && NodeList.item(1) == option1b, "item returns NodeList items");
});
// glow.dom.NodeList.each(Function)
// Runs 'Function' for each context node, arguements[0] is the node
t.test("glow.dom.NodeList.each", function () {
t.expect(1);
var ids = '';
glow.dom.get('#option1a, #option1b').each(function (i) {
ids += this.id + ' ' + i.toString() + ' ';
});
t.equals(ids, "option1a 0 option1b 1 ", "NodeList interated over with each");
});
// glow.dom.NodeList.push(element(s) / array of element(s)) **
// Add elements to the context
t.test("glow.dom.NodeList.push", function () {
t.expect(4);
var NodeList = glow.dom.get("#option1a, #option1b");
NodeList.push(glow.dom.get("#option1c")[0]);
t.equals(NodeList.length, 3, "pushing element increases length");
t.equals(NodeList[2], glow.dom.get("#option1c")[0], "can push a dom node");
NodeList.push([glow.dom.get("#option1d")[0], glow.dom.get("#option2a")[0]]);
t.equals(NodeList.length, 5, "pushing array of nodes increases length");
t.ok(NodeList[3] == glow.dom.get("#option1d")[0] && NodeList[4] == glow.dom.get("#option2a")[0], "can put array of dom nodes");
});
// glow.dom.NodeList.slice(start:Number[, end:Number])
// As you'd expect
t.test("glow.dom.NodeList.slice", function () {
t.expect(3);
var NodeList = glow.dom.get("#option1a, #option1b, #option1c, #option1d").slice(1, -1);
t.equals(NodeList.length, 2, "NodeList length cut by slice");
t.ok(NodeList[0] == glow.dom.get("#option1b")[0] && NodeList[1] == glow.dom.get("#option1c")[0], "expected nodes remain after slice");
t.ok(glow.dom.get("div").slice(0) instanceof glow.dom.NodeList, "Slice returns NodeList");
});
t.test("glow.dom.NodeList.sort", function() {
t.expect(2);
t.isSet(glow.dom.get("#foo p, #ap a").sort(), q("google", "groups", "anchor1", "mark", "sndp", "en", "sap"), "Sort multi selectors into order");
t.isSet(glow.dom.get("#sndp, #foo").sort(), q("foo", "sndp"), "Depth last");
});
t.module("glow.dom.NodeList comparison");
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
// glow.dom.NodeList.eq(element, array) **
// Is the NodeList the same as another? If a single element is provided it is treated as an array of one element
t.test("glow.dom.NodeList.eq", function () {
t.expect(4);
var option1a = glow.dom.get("#option1a");
var option1b = glow.dom.get("#option1b");
t.ok(glow.dom.get(option1a).eq(option1a), "NodeList same as element");
t.ok(! glow.dom.get(option1a).eq(option1b), "NodeList not same as different element");
t.ok(glow.dom.get([option1a, option1b]).eq(glow.dom.get([option1a, option1b])), "NodeList with more than one element same a NodeList");
t.ok(! glow.dom.get([option1a, option1b]).eq(glow.dom.get([option1b, option1a])), "NodeList with more than one element not same as different NodeList");
});
// glow.dom.NodeList.isWithin(element, array - only first element used) **
// Returns true if all of the NodeList nodes are decendants of the parameter.
t.test("glow.dom.NodeList.isWithin", function () {
t.expect(3);
t.ok(glow.dom.get("#area1, #option2a").isWithin(glow.dom.get("#form")), "isWithin for multiple decendents");
t.ok(! glow.dom.get("#area1, #ap").isWithin(glow.dom.get("#form")), "isWithin for mixed decendents and non-decendents");
t.ok(! glow.dom.get("#ap").isWithin(glow.dom.get("#form")), "isWithin for non-decendent element");
});
t.module("glow.dom.NodeList Attribute Methods");
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
// glow.dom.NodeList.attr(String)
// Get attribute 'String' from first context node
t.test("glow.dom.NodeList.attr", function () {
t.expect(14);
t.equals(glow.dom.get("#check1").attr("checked"), "checked", "checked checkbox returns checked as the attribute value");
t.equals(glow.dom.get("#check2").attr("checked"), undefined, "checked checkbox returns empty string as the attribute value");
t.equals(glow.dom.get("#mark").attr("class"), "blog", "can get class attribute");
// TODO - add tests for multiple classes
// cssFloat: styleFloat,
// TODO - how do i test this - is there anything in the html page that is floated
t.equals(glow.dom.get("#text2").attr("disabled"), "disabled", "disabled element returns disabled as the attribute value");
t.equals(glow.dom.get('#text1').attr("disabled"), undefined, "not disabled element returns undefined for disabled attribute value");
// float: styleFloat,
// TODO - how do i test this - see above
var label = glow.dom.get("#testLabel1");
t.equals(label.attr("for"), "text1", "get 'for' with attr");
t.equals(glow.dom.get("#name").attr("maxlength"), "15", "get 'maxlength' with attr");
t.equals(glow.dom.get("#text1").attr("maxlength"), undefined, "get unset 'maxlength' with attr");
t.ok(glow.dom.get("#T2").attr("readonly"), "get 'readonly' with attr");
t.ok(! glow.dom.get("#name").attr("readonly"), "get unset 'readonly' with attr");
t.ok(glow.dom.get("#option2d").attr("selected"), "get 'selected' with attr");
t.ok(! glow.dom.get("#option2c").attr("selected"), "get unset 'selected' with attr");
// TODO - add test that exposes Safari misreporting selected attribute of hidden option
// see jquery for fix for this.
// styleFloat: styleFloat,
// TODO - how do i test this - see above
// value: "value",
t.equals(glow.dom.get("#name").attr("value"), "name", "get 'value' with attr");
t.ok(
glow.dom.get("#idTest").attr("value") == undefined || glow.dom.get("#idTest").attr("value") == "",
"get unset 'value' with attr"
);
});
// glow.dom.NodeList.attr(key:String, value:String)
// Set attribute key = value on context nodes
t.test("glow.dom.NodeList.attr(key:String, value:String)", function () {
t.expect(4);
var node = glow.dom.create("<div></div>");
node.attr("id", "blahblahid");
t.equals(node[0].id, "blahblahid", "set 'id' with attr");
t.equals(node.attr("id"), "blahblahid", "get 'id' with attr");
var nodes = glow.dom.create("<div></div><div></div><div></div>");
nodes.attr("class", "testClass");
t.ok(
nodes.slice(0, 1).attr("class") == "testClass"
&& nodes.slice(1, 2).attr("class") == "testClass"
&& nodes.slice(2, 3).attr("class") == "testClass",
"set 'class' on multiple attributes with attr"
);
glow.dom.get("#check2").attr("checked", "checked");
t.equals(glow.dom.get("#check2").attr("checked"), "checked", "set 'checked' with attr checks checkbox");
glow.dom.get("#check1").attr("checked", "");
// removed because setAttribute("checked", "") has no effect in opera
});
// glow.dom.NodeList.attr(Object)
// Set attributes on context nodes using hash-like Object {attrName: value}
t.test("glow.dom.NodeList.attr(Object)", function () {
t.expect(4);
var nodes = glow.dom.create("<div></div><div></div>");
nodes.attr({
"class" : "blah",
"onclick" : "alert(1);"
});
t.equals(nodes.attr("class"), "blah", "attr with object set 'class' on first node");
t.equals(nodes.attr("onclick"), "alert(1);", "attr with object set 'onclick' on first node");
t.equals(nodes.slice(1, 2).attr("class"), "blah", "attr with object set 'class' on second node");
t.equals(nodes.slice(1, 2).attr("onclick"), "alert(1);", "attr with object set 'onclick' on second node");
});
// glow.dom.NodeList.attr(key:String, value:Function)
// Set attribute key = value(). 'this' in 'value' is the current node, arguments[0] is the index
t.test("glow.dom.NodeList.attr(key:String, value:Function)", function () {
t.expect(2);
var nodes = glow.dom.create("<div></div><div></div>");
var i = 0;
nodes.attr("class", function () {
t.equals(this, nodes[i++], "this in callback is the node for node " + i);
});
});
// glow.dom.NodeList.removeAttr(String)
// Remove attr 'String' from context nodes
t.test("glow.dom.NodeList.removeAttr(String)", function () {
t.expect(2);
var nodes = glow.dom.create("<div class=\"blah\"></div><div class=\"blah\"></div>");
nodes.removeAttr("class");
t.ok(
nodes.slice(0, 1).attr("class") == undefined || nodes.slice(0, 1).attr("class") == "",
"remove attribute from first node"
);
t.ok(
nodes.slice(1, 2).attr("class") == undefined || nodes.slice(1, 2).attr("class") == "",
"remove attribute from second node"
);
});
t.test("glow.dom.NodeList.hasAttr(String)", function () {
t.expect(2);
var nodes = glow.dom.create("<div class=\"blah\"></div><div></div>");
t.ok(nodes.slice(0, 1).hasAttr("class"), "class attribute exists");
t.ok(! nodes.slice(1, 2).hasAttr("class"), "class attribute does not exist");
// don't even bother expecing IE to be sane about empty attributes
});
// glow.dom.NodeList.hasClass(String)
// True if any of the context nodes has class 'String'
t.test("glow.dom.NodeList.hasClass(String)", function () {
t.expect(3);
t.ok(
glow.dom.get("#mark").hasClass("blog")
&& ! glow.dom.get("#mark").hasClass("blah"),
"single class"
);
t.ok(
glow.dom.get("#fadein").hasClass("chain")
&& glow.dom.get("#fadein").hasClass("test")
&& ! glow.dom.get("#fadein").hasClass("blah"),
"mulitple classes"
);
t.ok(
glow.dom.get("#mark, #fadein").hasClass("chain")
&& ! glow.dom.get("#mark, #fadein").hasClass("blah"),
"multiple nodes"
);
});
// glow.dom.NodeList.addClass(String)
// Add class 'String' to context nodes
t.test("glow.dom.NodeList.addClass(String)", function () {
t.expect(3);
var nodes = glow.dom.create("<div></div><div></div>");
t.equals(nodes.addClass("blah"), nodes, "addClass returns the node list");
t.ok(nodes.slice(0,1).hasClass("blah") && nodes.slice(1,2).hasClass("blah"), "add a class");
nodes.addClass("bah");
t.ok(nodes.slice(0,1).hasClass("blah") && nodes.slice(1,2).hasClass("blah") && nodes.slice(0,1).hasClass("bah") && nodes.slice(1,2).hasClass("bah"), "add a second class & retains first class");
});
// glow.dom.NodeList.removeClass(String)
// Remove class 'String' from context nodes
t.test("glow.dom.NodeList.removeClass(String)", function () {
t.expect(6);
var nodes = glow.dom.create('<div class="classa classb"></div><div class="classa"></div>');
t.equals(nodes.removeClass("classb"), nodes, "removeClass returns the node list");
t.ok(!nodes.hasClass("classb"), "remove a class from one node");
var oldClasses = [nodes[0].className, nodes[1].className];
nodes.removeClass("classc");
t.ok(String(oldClasses[0]) == String(nodes[0].className) && String(oldClasses[1]) == String(nodes[1].className), "remove a non-existant class has no effect");
nodes.removeClass("classa");
t.ok(/^\s*$/.test(nodes[0].className) && /^\s*$/.test(nodes[1].className), "remove a class from multiple elements");
// https://devtools.iplayer.bbc.co.uk/bbctrac/jslib/ticket/92 - bug with hyphens in class names
nodes.slice(0,1).addClass("classname-with-hyphen").removeClass("with-hyphen");
t.ok(/\bclassname\-with\-hyphen\b/.test(nodes[0].className), "Partial class name hasn't been removed");
// https://devtools.iplayer.bbc.co.uk/bbctrac/jslib/ticket/98
// removing class from middle of a list collapses its neighbours
nodes.addClass("classx").addClass("classy").addClass("classz").removeClass("classy");
t.ok(!nodes.hasClass("classxclassz"), "remove a class from the middle of a list safely");
});
// glow.dom.NodeList.toggleClass(String)
// Toggle class 'String' on context nodes
t.test("glow.dom.NodeList.toggleClass(String)", function () {
t.expect(3);
var nodes = glow.dom.create("<div class=\"classa classb\"></div><div class=\"classa\"></div>");
t.equals(nodes.toggleClass("classa"), nodes, "toggleClass returns the node list");
t.ok(/^\s*classb\s*$/.test(nodes[0].className) && /^\s*$/.test(nodes[1].className), "toggle class on multiple nodes");
nodes.toggleClass("classb");
t.ok(/^\s*$/.test(nodes[0].className) && /^\s*classb\s*$/.test(nodes[1].className), "toggle class toggles each node");
});
// glow.dom.NodeList.val() **
// Gets the form return value of first context, array for multiple selects, obj for forms
t.test("glow.dom.NodeList.val()", function () {
t.expect(15);
t.equals(
glow.dom.create("<input type=\"text\" name=\"blah\"/>").val(),
"",
"unspecified value returns empty string"
);
t.equals(
glow.dom.create("<input type=\"text\" name=\"blah\" value=\"val\"/>").val(),
"val",
"get value from text input"
);
t.equals(
glow.dom.create("<input type=\"checkbox\" name=\"blah\" value=\"val\" checked=\"checked\"/>").val(),
"val",
"get value from checked checkbox"
);
t.equals(
glow.dom.create("<input type=\"checkbox\" name=\"blah\" value=\"val\"/>").val(),
"",
"Empty value from unchecked checkbox"
);
t.equals(
glow.dom.create("<input type=\"radio\" name=\"blah\" value=\"val\"/>").val(),
"",
"Empty value from unchecked radio"
);
t.equals(
glow.dom.create("<input type=\"radio\" name=\"blah\" value=\"val\" checked=\"checked\"/>").val(),
"val",
"get value from checked radio"
);
t.equals(
glow.dom.create(
"<select name=\"blah\"><option>foo</option>" +
"<option value=\"bah\" selected=\"selected\">bar</option></select>"
).val(),
"bah",
"value for selectd is selected option"
);
t.isSet(
glow.dom.create(
"<select multiple=\"multiple\" name=\"blah\">" +
" <option value=\"blah1\" selected=\"selected\">b1</option>" +
" <option selected=\"selected\" value=\"value2\">blah2</option>" +
" <option>other</option>" +
"</select>"
).val(),
["blah1", "value2"],
"get array of selected items from multiple select"
);
var formVal = glow.dom.create(
"<form>" +
"<fieldset>" +
"<input type=\"hidden\" name=\"hidden1\" value=\"hidden1val\"/>" +
"<input type=\"text\" name=\"nm1\" value=\"val1\"/>" +
"<input type=\"text\" name=\"nm2\" value=\"val2.1\"/>" +
"<input type=\"text\" name=\"nm2\" value=\"val2.2\"/>" +
"</fieldset>" +
"<fieldset>" +
"<input type=\"checkbox\" name=\"ck1\" value=\"val1\"/>" +
"<input type=\"checkbox\" name=\"ck2\" value=\"val2\" checked=\"checked\"/>" +
"<input type=\"checkbox\" name=\"ck3\" value=\"val3.1\"/>" +
"<input type=\"checkbox\" name=\"ck3\" value=\"val3.2\" checked=\"checked\"/>" +
"<input type=\"checkbox\" name=\"ck3\" value=\"val3.3\"/>" +
"<input type=\"checkbox\" name=\"ck3\" value=\"val3.4\" checked=\"checked\"/>" +
"<input type=\"radio\" name=\"myRadios\" value=\"rval1\"/>" +
"<input type=\"radio\" name=\"myRadios\" value=\"rval2\" checked=\"checked\"/>" +
"<input type=\"radio\" name=\"myRadios2\" value=\"rval1\"/>" +
"<input type=\"radio\" name=\"myRadios2\" value=\"rval2\"/>" +
'<input type="text" value="Test" />' +
'<object id="uploadBridge"><param name="quality" value="high" /></object>' +
'<input type="file" name="fileUpload" />' +
'<input type="submit" id="whatever" value="Test" />' +
"</fieldset>" +
"</form>"
).val();
t.equals(formVal.nm1, "val1", "form element in form value");
t.isSet(formVal.nm2, ["val2.1", "val2.2"], "multi-form values result in array");
t.ok(! ('ck1' in formVal), "unchecked checkbox value not in form");
t.equals(formVal.ck2, "val2", "checked checkbox value in form");
t.isSet(formVal.ck3, ["val3.2", "val3.4"], "only checked checkboxes have values");
t.equals(formVal.myRadios, "rval2", "radio has value of checked radio");
t.equals(formVal.myRadios2, undefined, "unchecked radios have undefined value");
});
// glow.dom.NodeList.val(String) **
// Sets value of appropriate context nodes
t.test("glow.dom.NodeList.val(String)", function () {
t.expect(3);
var nodes = glow.dom.create("<input type=\"text\" name=\"blah1\"/><input type=\"text\" name=\"blah2\"/>");
nodes.val("aValue");
t.ok(nodes[0].value == "aValue" && nodes[1].value == "aValue", "set value of text node2");
var select = glow.dom.create("<select><option value=\"1\">blah</option><option value=\"2\">blah</option></select>");
select.val("2");
t.ok(select[0].selectedIndex == 1, "set value for single select");
var multiSelect = glow.dom.create("<select multiple=\"multiple\" name=\"blah\"><option value=\"1\">blah</option><option value=\"2\">blah2</option></select>");
multiSelect.val(["1", "2"]);
t.ok(multiSelect[0].options[0].selected && multiSelect[0].options[1].selected, "multiple select takes array of values");
});
// glow.dom.NodeList.val(Object) **
// Sets value of form elements for each form context node. Object is a {name: value} hash. Value can be an array for multiple selects.
t.test("glow.dom.NodeList.val(Object)", function () {
t.expect(8);
var form = glow.dom.create(
'<form>' +
'<input type="text" name="nm1" value="val1" />' +
'<input type="text" name="nm2" value="val2" />' +
'<input type="text" name="nm3" value="val3" />' +
'<input type="checkbox" name="ck1" value="ck1val1" checked="checked" />' +
'<input type="checkbox" name="ck2" value="ck2val1" checked="checked" />' +
'<input type="checkbox" name="ck2" value="ck2val2" />' +
'<input type="checkbox" name="ck3" value="ck3val3" />' +
'<input type="checkbox" name="ck4" value="ck4val4" checked="checked" />' +
'<input type="radio" name="myRadios" value="rval1" />' +
'<input type="radio" name="myRadios" value="rval2" checked="checked" />' +
'<input type="radio" name="myRadios2" value="rval1" />' +
'<input type="radio" name="myRadios2" value="rval2" />' +
'</form>'
).appendTo(document.body);
form.val({
nm1 : "3val",
nm2 : "2val",
nm3 : "1val",
ck1 : undefined,
ck2 : ["ck2val2"],
ck3 : "ck3val3",
ck4 : "ck4valzz",
myRadios : undefined,
myRadios2 : 'rval1'
});
t.ok(
form[0].elements[0].value == "3val" &&
form[0].elements[1].value == "2val" &&
form[0].elements[2].value == "1val",
"set form values to object"
);
t.ok(! form[0].elements[3].checked, "undefined checkbox is unchecked");
t.ok(! form[0].elements[4].checked, "checkbox without value in array unchecked");
t.ok(form[0].elements[5].checked, "checkbox with value in array checked");
t.ok(form[0].elements[6].checked, "checkbox with value checked");
t.ok(! form[0].elements[7].checked, "checkbox with wrong value value unchecked");
t.ok(! form[0].elements[8].checked && ! form[0].elements[9].checked, "setting radio to undefined unchecks all");
t.ok(form[0].elements[10].checked && ! form[0].elements[11].checked, "setting radio to value checks correct one");
form.remove();
});
t.test("glow.dom.NodeList#prop on empty NodeList", function() {
t.expect(1);
var myNodeList = new glow.dom.NodeList();
// these shouldn't cause an error
myNodeList.prop("hello", "world");
myNodeList.prop({
no: "errors",
please: "thankyou"
});
t.ok(myNodeList.prop("hello") === undefined, "on empty NodeList returns undefined");
});
t.test("glow.dom.NodeList#prop", function() {
t.expect(23);
var elm = glow.dom.create(
'<input type="checkbox" name="ck2" value="val2" checked="checked"/>' +
'<div>Hello</div>' +
'<span>World</span>' +
'');
// getting
t.ok( elm.prop("checked") === true, "Checked property === true" );
t.equals( elm.prop("nodeName").toLowerCase(), "input", "Get the node name" );
t.ok( elm.prop("doesNotExist") === undefined, "Not existant property is undefined" );
// setting
elm.prop("_test1", 10);
var myObj = {hello:"world"};
var returnVal = elm.prop("_test2", myObj);
t.ok(returnVal === elm, "NodeList returned when setting");
// getting what we just set, via api
t.ok( elm.slice(0,1).prop("_test1") === 10, "10 set and got via prop() (elm 1)" );
t.ok( elm.slice(1,2).prop("_test1") === 10, "10 set and got via prop() (elm 2)" );
t.ok( elm.slice(2,3).prop("_test1") === 10, "10 set and got via prop() (elm 3)" );
t.ok( elm.slice(0,1).prop("_test2") === myObj, "myObj set and got via prop() (elm 1)" );
t.ok( elm.slice(1,2).prop("_test2") === myObj, "myObj set and got via prop() (elm 2)" );
t.ok( elm.slice(2,3).prop("_test2") === myObj, "myObj set and got via prop() (elm 3)" );
t.ok( elm.slice(0,1)[0]._test1 === 10, "10 set and got via [0] (elm 1)" );
t.ok( elm.slice(1,2)[0]._test1 === 10, "10 set and got via [0] (elm 2)" );
t.ok( elm.slice(2,3)[0]._test1 === 10, "10 set and got via [0] (elm 3)" );
t.ok( elm.slice(0,1)[0]._test2 === myObj, "myObj set and got via [0] (elm 1)" );
t.ok( elm.slice(1,2)[0]._test2 === myObj, "myObj set and got via [0] (elm 2)" );
t.ok( elm.slice(2,3)[0]._test2 === myObj, "myObj set and got via [0] (elm 3)" );
// more setting - multiple values
var myObj2 = {foo:"bar"};
returnVal = elm.prop({
_test3: 5,
_test4: myObj2
});
t.ok(returnVal === elm, "NodeList returned when setting multiple");
// get those values
t.ok( elm.slice(0,1).prop("_test3") === 5, "5 set and got via prop() (elm 1)" );
t.ok( elm.slice(1,2).prop("_test3") === 5, "5 set and got via prop() (elm 2)" );
t.ok( elm.slice(2,3).prop("_test3") === 5, "5 set and got via prop() (elm 3)" );
t.ok( elm.slice(0,1).prop("_test4") === myObj2, "myObj2 set and got via prop() (elm 1)" );
t.ok( elm.slice(1,2).prop("_test4") === myObj2, "myObj2 set and got via prop() (elm 2)" );
t.ok( elm.slice(2,3).prop("_test4") === myObj2, "myObj2 set and got via prop() (elm 3)" );
elm.destroy();
});
// glow.dom.NodeList.is(CssExpression)
// True if all context nodes match css expression
t.test("glow.dom.NodeList.is(CssExpression)", function () {
t.expect(2);
var nodes = glow.dom.get("#option3a, #option3b, #option3c");
t.ok(nodes.is("#select3 option"), "NodeList is expression");
t.ok(! nodes.is("#option3a, #idTest"), "NodeList isn't expression");
});
t.module("glow.dom.NodeList Altering context selection");
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
t.test("glow.dom.NodeList.filter(function)", function() {
t.expect(4);
var nodes = glow.dom.get("#foo p").filter(function(i) {
return this.innerHTML.indexOf("normal link") != -1;
});
t.isSet(nodes, q("en"), "Filter uses 'this' as context and returns correct item");
nodes = glow.dom.get("#foo p").filter(function(i) {
return glow.dom.get(this).get("> code").length > 0;
});
t.isSet(nodes, q("sndp", "sap"), "Filter returns multiple items");
nodes = glow.dom.get("#foo p").filter(function(i) {
return i % 2;
});
t.isSet(nodes, q("en"), "Filter provides i param");
t.ok(nodes instanceof glow.dom.NodeList, "Returns NodeList");
});
t.test("glow.dom.NodeList.children()", function() {
t.expect(3);
var nodes = glow.dom.get("#foo").children();
t.isSet(nodes, q("sndp", "en", "sap"), "Gets direct children");
nodes = glow.dom.get("#sndp").children();
t.equals(nodes.length, 1, "Gets only elements");
t.ok(nodes instanceof glow.dom.NodeList, "Returns NodeList");