-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathdom_test.js
1708 lines (1390 loc) · 72.7 KB
/
dom_test.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
var getInnerHTML = function(id) {
return $(id).innerHTML.toString().toLowerCase().gsub(/[\r\n\t]/, '');
};
var createParagraph = function(text) {
var p = document.createElement('p');
p.appendChild(document.createTextNode(text));
return p;
}
function simulateClick(node) {
var oEvent;
if (document.createEvent) {
oEvent = document.createEvent('MouseEvents');
oEvent.initMouseEvent('click', true, true, document.defaultView,
0, 0, 0, 0, 0, false, false, false, false, 0, node);
node.dispatchEvent(oEvent);
} else {
node.click();
}
}
new Test.Unit.Runner({
setup: function() {
if (documentViewportProperties) return;
// Based on properties check from http://www.quirksmode.org/viewport/compatibility.html
documentViewportProperties = {
properties : [
'self.pageXOffset', 'self.pageYOffset',
'self.screenX', 'self.screenY',
'self.innerHeight', 'self.innerWidth',
'self.outerHeight', 'self.outerWidth',
'self.screen.height', 'self.screen.width',
'self.screen.availHeight', 'self.screen.availWidth',
'self.screen.availTop', 'self.screen.availLeft',
'self.screen.Top', 'self.screen.Left',
'self.screenTop', 'self.screenLeft',
'document.body.clientHeight', 'document.body.clientWidth',
'document.body.scrollHeight', 'document.body.scrollWidth',
'document.body.scrollLeft', 'document.body.scrollTop',
'document.body.offsetHeight', 'document.body.offsetWidth',
'document.body.offsetTop', 'document.body.offsetLeft'
].inject([], function(properties, prop) {
if(!self.screen && prop.include('self.screen')) return;
if (!document.body && prop.include('document.body')) return;
properties.push(prop);
if (prop.include('.body') && document.documentElement)
properties.push(prop.sub('.body', '.documentElement'));
return properties;
}),
inspect : function() {
var props = [];
this.properties.each(function(prop) {
if (eval(prop)) props[prop] = eval(prop);
}, this);
return props;
}
};
},
testDollarFunction: function() {
this.assertUndefined($());
this.assertNull(document.getElementById('noWayThisIDExists'),
'nonexistent ID should return null from getElementById');
this.assertNull($('noWayThisIDExists'),
'nonexistent ID should return null from $');
this.assertIdentical(document.getElementById('testdiv'), $('testdiv'),
'getElementById and $ should return the same element');
this.assertEnumEqual([ $('testdiv'), $('container') ], $('testdiv', 'container'));
this.assertEnumEqual([ $('testdiv'), undefined, $('container') ],
$('testdiv', 'noWayThisIDExists', 'container'));
var elt = $('testdiv');
this.assertIdentical(elt, $(elt));
this.assertRespondsTo('hide', elt);
this.assertRespondsTo('childOf', elt);
},
testGetElementsByClassName: function() {
if (document.getElementsByClassName.toString().include('[native code]')) {
this.info("browser uses native getElementsByClassName; skipping tests");
return;
}
var div = $('class_names'), list = $('class_names_ul');
this.assertElementsMatch(document.getElementsByClassName('A'), 'p.A', 'ul#class_names_ul.A', 'li.A.C');
var isElementPrototypeSupported = (function(){
var el = document.createElement('div');
var result = typeof el.show != 'undefined';
el = null;
return result;
})();
if (!isElementPrototypeSupported)
this.assertUndefined(document.getElementById('unextended').show);
this.assertElementsMatch(div.getElementsByClassName('B'), 'ul#class_names_ul.A.B', 'div.B.C.D');
this.assertElementsMatch(div.getElementsByClassName('D C B'), 'div.B.C.D');
this.assertElementsMatch(div.getElementsByClassName(' D\nC\tB '), 'div.B.C.D');
this.assertElementsMatch(div.getElementsByClassName($w('D C B')));
this.assertElementsMatch(list.getElementsByClassName('A'), 'li.A.C');
this.assertElementsMatch(list.getElementsByClassName(' A '), 'li.A.C');
this.assertElementsMatch(list.getElementsByClassName('C A'), 'li.A.C');
this.assertElementsMatch(list.getElementsByClassName("C\nA "), 'li.A.C');
this.assertElementsMatch(list.getElementsByClassName('B'));
this.assertElementsMatch(list.getElementsByClassName('1'), 'li.1');
this.assertElementsMatch(list.getElementsByClassName([1]), 'li.1');
this.assertElementsMatch(list.getElementsByClassName(['1 junk']));
this.assertElementsMatch(list.getElementsByClassName(''));
this.assertElementsMatch(list.getElementsByClassName(' '));
this.assertElementsMatch(list.getElementsByClassName(['']));
this.assertElementsMatch(list.getElementsByClassName([' ', '']));
this.assertElementsMatch(list.getElementsByClassName({}));
// those lookups shouldn't have extended all nodes in document
if (!isElementPrototypeSupported)
this.assertUndefined(document.getElementById('unextended')['show']);
},
testElementInsertWithHTML: function() {
Element.insert('insertions-main', {before:'<p><em>before</em> text</p><p>more testing</p>'});
this.assert(getInnerHTML('insertions-container').startsWith('<p><em>before</em> text</p><p>more testing</p>'));
Element.insert('insertions-main', {after:'<p><em>after</em> text</p><p>more testing</p>'});
this.assert(getInnerHTML('insertions-container').endsWith('<p><em>after</em> text</p><p>more testing</p>'));
Element.insert('insertions-main', {top:'<p><em>top</em> text.</p><p>more testing</p>'});
this.assert(getInnerHTML('insertions-main').startsWith('<p><em>top</em> text.</p><p>more testing</p>'));
Element.insert('insertions-main', {bottom:'<p><em>bottom</em> text.</p><p>more testing</p>'});
this.assert(getInnerHTML('insertions-main').endsWith('<p><em>bottom</em> text.</p><p>more testing</p>'));
},
testElementInsertWithDOMNode: function() {
Element.insert('insertions-node-main', {before: createParagraph('node before')});
this.assert(getInnerHTML('insertions-node-container').startsWith('<p>node before</p>'));
Element.insert('insertions-node-main', {after: createParagraph('node after')});
this.assert(getInnerHTML('insertions-node-container').endsWith('<p>node after</p>'));
Element.insert('insertions-node-main', {top:createParagraph('node top')});
this.assert(getInnerHTML('insertions-node-main').startsWith('<p>node top</p>'));
Element.insert('insertions-node-main', {bottom:createParagraph('node bottom')});
this.assert(getInnerHTML('insertions-node-main').endsWith('<p>node bottom</p>'));
this.assertEqual($('insertions-node-main'), $('insertions-node-main').insert(document.createElement('p')));
},
testElementInsertWithToElementMethod: function() {
Element.insert('insertions-node-main', {toElement: createParagraph.curry('toElement') });
this.assert(getInnerHTML('insertions-node-main').endsWith('<p>toelement</p>'));
Element.insert('insertions-node-main', {bottom: {toElement: createParagraph.curry('bottom toElement') }});
this.assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom toelement</p>'));
},
testElementInsertWithToHTMLMethod: function() {
Element.insert('insertions-node-main', {toHTML: function() { return '<p>toHTML</p>'} });
this.assert(getInnerHTML('insertions-node-main').endsWith('<p>tohtml</p>'));
Element.insert('insertions-node-main', {bottom: {toHTML: function() { return '<p>bottom toHTML</p>'} }});
this.assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom tohtml</p>'));
},
testElementInsertWithNonString: function() {
Element.insert('insertions-main', {bottom:3});
this.assert(getInnerHTML('insertions-main').endsWith('3'));
},
testElementInsertInTables: function() {
Element.insert('second_row', { after:'<tr id="third_row"><td>Third Row</td></tr>' });
this.assert($('second_row').parentNode == $('table'),
'table rows should be inserted correctly');
$('a_cell').insert({ top: 'hello world' });
this.assert($('a_cell').innerHTML.startsWith('hello world'),
'content should be inserted into table cells correctly');
$('a_cell').insert({ after: '<td>hi planet</td>'});
this.assertEqual('hi planet', $('a_cell').next().innerHTML,
'table cells should be inserted after existing table cells correctly');
$('table_for_insertions').insert('<tr><td>a cell!</td></tr>');
this.assert($('table_for_insertions').innerHTML.gsub('\r\n', '').toLowerCase().include('<tr><td>a cell!</td></tr>'),
'complex content should be inserted into a table correctly');
$('row_1').insert({ after:'<tr></tr><tr></tr><tr><td>last</td></tr>' });
this.assertEqual('last', $A($('table_for_row_insertions').getElementsByTagName('tr')).last().lastChild.innerHTML,
'complex content should be inserted after a table row correctly');
},
testElementInsertInSelect: function() {
var selectTop = $('select_for_insert_top'), selectBottom = $('select_for_insert_bottom');
selectBottom.insert('<option value="33">option 33</option><option selected="selected">option 45</option>');
this.assertEqual('option 45', selectBottom.getValue());
selectTop.insert({top:'<option value="A">option A</option><option value="B" selected="selected">option B</option>'});
this.assertEqual(4, selectTop.options.length);
},
testElementMethodInsert: function() {
$('element-insertions-main').insert({before:'some text before'});
this.assert(getInnerHTML('element-insertions-container').startsWith('some text before'));
$('element-insertions-main').insert({after:'some text after'});
this.assert(getInnerHTML('element-insertions-container').endsWith('some text after'));
$('element-insertions-main').insert({top:'some text top'});
this.assert(getInnerHTML('element-insertions-main').startsWith('some text top'));
$('element-insertions-main').insert({bottom:'some text bottom'});
this.assert(getInnerHTML('element-insertions-main').endsWith('some text bottom'));
$('element-insertions-main').insert('some more text at the bottom');
this.assert(getInnerHTML('element-insertions-main').endsWith('some more text at the bottom'));
$('element-insertions-main').insert({TOP:'some text uppercase top'});
this.assert(getInnerHTML('element-insertions-main').startsWith('some text uppercase top'));
$('element-insertions-multiple-main').insert({
top:'1', bottom:2, before: new Element('p').update('3'), after:'4'
});
this.assert(getInnerHTML('element-insertions-multiple-main').startsWith('1'));
this.assert(getInnerHTML('element-insertions-multiple-main').endsWith('2'));
this.assert(getInnerHTML('element-insertions-multiple-container').startsWith('<p>3</p>'));
this.assert(getInnerHTML('element-insertions-multiple-container').endsWith('4'));
$('element-insertions-main').update('test');
$('element-insertions-main').insert(null);
$('element-insertions-main').insert({bottom:null});
this.assertEqual('test', getInnerHTML('element-insertions-main'));
$('element-insertions-main').insert(1337);
this.assertEqual('test1337', getInnerHTML('element-insertions-main'));
},
testNewElementInsert: function() {
var container = new Element('div'), element = new Element('div');
container.insert(element);
element.insert({ before: '<p>a paragraph</p>' });
this.assertEqual('<p>a paragraph</p><div></div>', getInnerHTML(container));
element.insert({ after: 'some text' });
this.assertEqual('<p>a paragraph</p><div></div>some text', getInnerHTML(container));
element.insert({ top: '<p>a paragraph</p>' });
this.assertEqual('<p>a paragraph</p>', getInnerHTML(element));
element.insert('some text');
this.assertEqual('<p>a paragraph</p>some text', getInnerHTML(element));
},
testInsertionBackwardsCompatibility: function() {
new Insertion.Before('element-insertions-main', 'some backward-compatibility testing before');
this.assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing before'));
new Insertion.After('element-insertions-main', 'some backward-compatibility testing after');
this.assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing after'));
new Insertion.Top('element-insertions-main', 'some backward-compatibility testing top');
this.assert(getInnerHTML('element-insertions-main').startsWith('some backward-compatibility testing top'));
new Insertion.Bottom('element-insertions-main', 'some backward-compatibility testing bottom');
this.assert(getInnerHTML('element-insertions-main').endsWith('some backward-compatibility testing bottom'));
},
testElementWrap: function() {
var element = $('wrap'), parent = document.createElement('div');
element.wrap();
this.assert(getInnerHTML('wrap-container').startsWith('<div><p'));
element.wrap('div');
this.assert(getInnerHTML('wrap-container').startsWith('<div><div><p'));
element.wrap(parent);
this.assert(Object.isFunction(parent.setStyle));
this.assert(getInnerHTML('wrap-container').startsWith('<div><div><div><p'));
element.wrap('div', {className: 'wrapper'});
this.assert(element.up().hasClassName('wrapper'));
element.wrap({className: 'other-wrapper'});
this.assert(element.up().hasClassName('other-wrapper'));
element.wrap(new Element('div'), {className: 'yet-other-wrapper'});
this.assert(element.up().hasClassName('yet-other-wrapper'));
var orphan = new Element('p'), div = new Element('div');
orphan.wrap(div);
this.assertEqual(orphan.parentNode, div);
},
testElementWrapReturnsWrapper: function() {
var element = new Element("div");
var wrapper = element.wrap("div");
this.assertNotEqual(element, wrapper);
this.assertEqual(element.up(), wrapper);
},
testElementVisible: function(){
this.assertNotEqual('none', $('test-visible').style.display);
this.assertEqual('none', $('test-hidden').style.display);
},
testElementToggle: function(){
$('test-toggle-visible').toggle();
this.assert(!$('test-toggle-visible').visible(), 'test-toggle-visible 1');
$('test-toggle-visible').toggle();
this.assert($('test-toggle-visible').visible()), 'test-toggle-visible 2';
$('test-toggle-hidden').toggle();
this.assert($('test-toggle-hidden').visible(), 'test-toggle-hidden 1');
$('test-toggle-hidden').toggle();
this.assert(!$('test-toggle-hidden').visible(), 'test-toggle-hidden 2');
},
testElementShow: function(){
$('test-show-visible').show();
this.assert($('test-show-visible').visible());
$('test-show-hidden').show();
this.assert($('test-show-hidden').visible());
},
testElementHide: function(){
$('test-hide-visible').hide();
this.assert(!$('test-hide-visible').visible());
$('test-hide-hidden').hide();
this.assert(!$('test-hide-hidden').visible());
},
testElementRemove: function(){
$('removable').remove();
this.assert($('removable-container').empty());
},
testElementUpdate: function() {
$('testdiv').update('hello from div!');
this.assertEqual('hello from div!', $('testdiv').innerHTML);
Element.update('testdiv', 'another hello from div!');
this.assertEqual('another hello from div!', $('testdiv').innerHTML);
Element.update('testdiv', 123);
this.assertEqual('123', $('testdiv').innerHTML);
Element.update('testdiv');
this.assertEqual('', $('testdiv').innerHTML);
Element.update('testdiv', ' ');
this.assert(!$('testdiv').innerHTML.empty());
},
testElementUpdateWithScript: function() {
$('testdiv').update('hello from div!<script>\ntestVar="hello!";\n</'+'script>');
this.assertEqual('hello from div!',$('testdiv').innerHTML);
this.wait(100,function(){
this.assertEqual('hello!',testVar);
Element.update('testdiv','another hello from div!\n<script>testVar="another hello!"</'+'script>\nhere it goes');
// note: IE normalizes whitespace (like line breaks) to single spaces, thus the match test
this.assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv').innerHTML);
this.wait(100,function(){
this.assertEqual('another hello!',testVar);
Element.update('testdiv','a\n<script>testVar="a"\ntestVar="b"</'+'script>');
this.wait(100,function(){
this.assertEqual('b', testVar);
Element.update('testdiv',
'x<script>testVar2="a"</'+'script>\nblah\n'+
'x<script>testVar2="b"</'+'script>');
this.wait(100,function(){
this.assertEqual('b', testVar2);
});
});
});
});
},
testElementUpdateInTableRow: function() {
$('second_row').update('<td id="i_am_a_td">test</td>');
this.assertEqual('test',$('i_am_a_td').innerHTML);
Element.update('second_row','<td id="i_am_a_td">another <span>test</span></td>');
this.assertEqual('another <span>test</span>',$('i_am_a_td').innerHTML.toLowerCase());
},
testElementUpdateInTableCell: function() {
Element.update('a_cell','another <span>test</span>');
this.assertEqual('another <span>test</span>',$('a_cell').innerHTML.toLowerCase());
},
testElementUpdateInTable: function() {
Element.update('table','<tr><td>boo!</td></tr>');
this.assertMatch(/^<tr>\s*<td>boo!<\/td><\/tr>$/,$('table').innerHTML.toLowerCase());
},
testElementUpdateInSelect: function() {
var select = $('select_for_update');
select.update('<option value="3">option 3</option><option selected="selected">option 4</option>');
this.assertEqual('option 4', select.getValue());
},
testElementUpdateWithLinkTag: function() {
var div = new Element('div');
div.update('<link rel="stylesheet" />');
this.assertEqual(1, div.childNodes.length);
var link = div.down('link');
this.assert(link);
this.assert(link.rel === 'stylesheet');
div.update('<p><link rel="stylesheet"></p>')
this.assertEqual(1, div.childNodes.length);
this.assertEqual(1, div.firstChild.childNodes.length);
var link = div.down('link');
this.assert(link);
this.assert(link.rel === 'stylesheet');
},
testElementUpdateWithDOMNode: function() {
$('testdiv').update(new Element('div').insert('bla'));
this.assertEqual('<div>bla</div>', getInnerHTML('testdiv'));
},
testElementUpdateWithToElementMethod: function() {
$('testdiv').update({toElement: createParagraph.curry('foo')});
this.assertEqual('<p>foo</p>', getInnerHTML('testdiv'));
},
testElementUpdateWithToHTMLMethod: function() {
$('testdiv').update({toHTML: function() { return 'hello world' }});
this.assertEqual('hello world', getInnerHTML('testdiv'));
},
testElementUpdateScriptElement: function() {
var el = new Element('script', {
type: 'text/javascript'
});
this.assertNothingRaised(function(){
el.update('(function(){})');
})
},
testElementReplace: function() {
$('testdiv-replace-1').replace('hello from div!');
this.assertEqual('hello from div!', $('testdiv-replace-container-1').innerHTML);
$('testdiv-replace-2').replace(123);
this.assertEqual('123', $('testdiv-replace-container-2').innerHTML);
$('testdiv-replace-3').replace();
this.assertEqual('', $('testdiv-replace-container-3').innerHTML);
$('testrow-replace').replace('<tr><td>hello</td></tr>');
this.assert(getInnerHTML('testrow-replace-container').include('<tr><td>hello</td></tr>'));
$('testoption-replace').replace('<option>hello</option>');
this.assert($('testoption-replace-container').innerHTML.include('hello'));
Element.replace('testinput-replace', '<p>hello world</p>');
this.assertEqual('<p>hello world</p>', getInnerHTML('testform-replace'));
Element.replace('testform-replace', '<form></form>');
this.assertEqual('<p>some text</p><form></form><p>some text</p>', getInnerHTML('testform-replace-container'));
},
testElementReplaceWithScript: function() {
$('testdiv-replace-4').replace('hello from div!<script>testVarReplace="hello!"</'+'script>');
this.assertEqual('hello from div!', $('testdiv-replace-container-4').innerHTML);
this.wait(100,function(){
this.assertEqual('hello!',testVarReplace);
$('testdiv-replace-5').replace('another hello from div!\n<script>testVarReplace="another hello!"</'+'script>\nhere it goes');
// note: IE normalizes whitespace (like line breaks) to single spaces, thus the match test
this.assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv-replace-container-5').innerHTML);
this.wait(100,function(){
this.assertEqual('another hello!',testVarReplace);
});
});
},
testElementReplaceWithDOMNode: function() {
$('testdiv-replace-element').replace(createParagraph('hello'));
this.assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-element'));
},
testElementReplaceWithToElementMethod: function() {
$('testdiv-replace-toelement').replace({toElement: createParagraph.curry('hello')});
this.assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-toelement'));
},
testElementReplaceWithToHTMLMethod: function() {
$('testdiv-replace-tohtml').replace({toHTML: function() { return 'hello' }});
this.assertEqual('hello', getInnerHTML('testdiv-replace-container-tohtml'));
},
testElementSelectorMethod: function() {
['getElementsBySelector','select'].each(function(method) {
var testSelector = $('container')[method]('p.test');
this.assertEqual(testSelector.length, 4);
this.assertEqual(testSelector[0], $('intended'));
this.assertEqual(testSelector[0], $$('#container p.test')[0]);
}, this);
},
testElementAdjacent: function() {
var elements = $('intended').adjacent('p');
this.assertEqual(elements.length, 3);
elements.each(function(element){
this.assert(element != $('intended'));
}, this);
},
testElementIdentify: function() {
var parent = $('identification');
this.assertEqual(parent.down().identify(), 'predefined_id',
"identify should preserve the IDs of elements that already have them");
this.assert(parent.down(1).identify().startsWith('anonymous_element_'),
"should have #anonymous_element_1");
this.assert(parent.down(2).identify().startsWith('anonymous_element_'),
"should have #anonymous_element_2");
this.assert(parent.down(3).identify().startsWith('anonymous_element_'),
"should have #anonymous_element_3");
this.assert(parent.down(3).id !== parent.down(2).id,
"should not assign duplicate IDs");
},
testElementClassNameMethod: function() {
var testClassNames = $('container').getElementsByClassName('test');
var testSelector = $('container').getElementsBySelector('p.test');
this.assertEqual(testClassNames[0], $('intended'));
this.assertEqual(testClassNames.length, 4);
this.assertEqual(testSelector[3], testClassNames[3]);
this.assertEqual(testClassNames.length, testSelector.length);
},
testElementAncestors: function() {
var ancestors = $('navigation_test_f').ancestors();
this.assertElementsMatch(ancestors, 'ul', 'li', 'ul#navigation_test',
'div#nav_tests_isolator', 'body', 'html');
this.assertElementsMatch(ancestors.last().ancestors());
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[0]).ancestors()[0]['setStyle'] == 'function');
},
testElementDescendants: function() {
this.assertElementsMatch($('navigation_test').descendants(),
'li', 'em', 'li', 'em.dim', 'li', 'em', 'ul', 'li',
'em.dim', 'li#navigation_test_f', 'em', 'li', 'em');
this.assertElementsMatch($('navigation_test_f').descendants(), 'em');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof dummy.descendants()[0].setStyle == 'function');
},
testElementFirstDescendant: function() {
this.assertElementMatches($('navigation_test').firstDescendant(), 'li.first');
this.assertNull($('navigation_test_next_sibling').firstDescendant());
},
testElementChildElements: function() {
this.assertElementsMatch($('navigation_test').childElements(),
'li.first', 'li', 'li#navigation_test_c', 'li.last');
this.assertNotEqual(0, $('navigation_test_next_sibling').childNodes.length);
this.assertEnumEqual([], $('navigation_test_next_sibling').childElements());
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof dummy.childElements()[0].setStyle == 'function');
},
testElementImmediateDescendants: function() {
this.assertIdentical(Element.Methods.childElements, Element.Methods.immediateDescendants);
},
testElementPreviousSiblings: function() {
this.assertElementsMatch($('navigation_test').previousSiblings(),
'span#nav_test_prev_sibling', 'p.test', 'div', 'div#nav_test_first_sibling');
this.assertElementsMatch($('navigation_test_f').previousSiblings(), 'li');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[1]).previousSiblings()[0].setStyle == 'function');
},
testElementNextSiblings: function() {
this.assertElementsMatch($('navigation_test').nextSiblings(),
'div#navigation_test_next_sibling', 'p');
this.assertElementsMatch($('navigation_test_f').nextSiblings());
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[0]).nextSiblings()[0].setStyle == 'function');
},
testElementSiblings: function() {
this.assertElementsMatch($('navigation_test').siblings(),
'div#nav_test_first_sibling', 'div', 'p.test',
'span#nav_test_prev_sibling', 'div#navigation_test_next_sibling', 'p');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[0]).siblings()[0].setStyle == 'function');
},
testElementUp: function() {
var element = $('navigation_test_f');
this.assertElementMatches(element.up(), 'ul');
this.assertElementMatches(element.up(0), 'ul');
this.assertElementMatches(element.up(1), 'li');
this.assertElementMatches(element.up(2), 'ul#navigation_test');
this.assertElementsMatch(element.up('li').siblings(), 'li.first', 'li', 'li.last');
this.assertElementMatches(element.up('ul', 1), 'ul#navigation_test');
this.assertEqual(undefined, element.up('garbage'));
this.assertEqual(undefined, element.up(6));
this.assertElementMatches(element.up('.non-existant, ul'), 'ul');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[0]).up().setStyle == 'function');
},
testElementDown: function() {
var element = $('navigation_test');
this.assertElementMatches(element.down(), 'li.first');
this.assertElementMatches(element.down(0), 'li.first');
this.assertElementMatches(element.down(1), 'em');
this.assertElementMatches(element.down('li', 5), 'li.last');
this.assertElementMatches(element.down('ul').down('li', 1), 'li#navigation_test_f');
this.assertElementMatches(element.down('.non-existant, .first'), 'li.first');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof dummy.down().setStyle == 'function');
var input = $$('input')[0];
this.assertNothingRaised(function(){ input.down('span') });
this.assertUndefined(input.down('span'));
},
testElementPrevious: function() {
var element = $('navigation_test').down('li.last');
this.assertElementMatches(element.previous(), 'li#navigation_test_c');
this.assertElementMatches(element.previous(1), 'li');
this.assertElementMatches(element.previous('.first'), 'li.first');
this.assertEqual(undefined, element.previous(3));
this.assertEqual(undefined, $('navigation_test').down().previous());
this.assertElementMatches(element.previous('.non-existant, .first'), 'li.first');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[1]).previous().setStyle == 'function');
},
testElementNext: function() {
var element = $('navigation_test').down('li.first');
this.assertElementMatches(element.next(), 'li');
this.assertElementMatches(element.next(1), 'li#navigation_test_c');
this.assertElementMatches(element.next(2), 'li.last');
this.assertElementMatches(element.next('.last'), 'li.last');
this.assertEqual(undefined, element.next(3));
this.assertEqual(undefined, element.next(2).next());
this.assertElementMatches(element.next('.non-existant, .last'), 'li.last');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
this.assert(typeof $(dummy.childNodes[0]).next().setStyle == 'function');
},
testElementInspect: function() {
this.assertEqual('<ul id="navigation_test">', $('navigation_test').inspect());
this.assertEqual('<li class="first">', $('navigation_test').down().inspect());
this.assertEqual('<em>', $('navigation_test').down(1).inspect());
},
testElementMakeClipping: function() {
var chained = Element.extend(document.createElement('DIV'));
this.assertEqual(chained, chained.makeClipping());
this.assertEqual(chained, chained.makeClipping());
this.assertEqual(chained, chained.makeClipping().makeClipping());
this.assertEqual(chained, chained.undoClipping());
this.assertEqual(chained, chained.undoClipping());
this.assertEqual(chained, chained.undoClipping().makeClipping());
var items = [
{id: 'hidden', inline: 'hidden', computed: 'hidden'},
{id: 'visible', inline: 'visible', computed: 'visible'},
{id: 'scroll', inline: 'scroll', computed: 'scroll'},
{id: 'auto', inline: 'auto', computed: null},
{id: 'empty', inline: '', computed: 'visible'}
];
items.each(function(item) {
var element = $('element_with_' + item.id + '_overflow');
this.assertEqual(item.inline, element.style.overflow, 'inline style');
this.assertEqual(item.computed, element.getStyle('overflow'), 'computed style');
element.makeClipping();
this.assertEqual('hidden', element.style.overflow);
this.assertEqual('hidden', element.getStyle('overflow'));
element.undoClipping();
this.assertEqual(item.inline, element.style.overflow, 'restored inline style');
this.assertEqual(item.computed, element.getStyle('overflow'), 'restored computed style');
}, this);
},
testElementExtend: function() {
Element.Methods.Simulated.simulatedMethod = function() {
return 'simulated';
};
Element.addMethods();
function testTag(tagName) {
var element = document.createElement(tagName);
this.assertEqual(element, Element.extend(element));
// test method from Methods
this.assertRespondsTo('show', element);
// test method from Simulated
this.assertRespondsTo('simulatedMethod', element);
}
var element = $('element_extend_test');
this.assertRespondsTo('show', element);
var XHTML_TAGS = $w(
'a abbr acronym address applet area '+
'b bdo big blockquote br button caption '+
'cite code col colgroup dd del dfn div dl dt '+
'em fieldset form h1 h2 h3 h4 h5 h6 hr '+
'i iframe img input ins kbd label legend li '+
'map object ol optgroup option p param pre q samp '+
'script select small span strong style sub sup '+
'table tbody td textarea tfoot th thead tr tt ul var');
XHTML_TAGS.each(function(tag) {
var element = document.createElement(tag);
this.assertEqual(element, Element.extend(element));
this.assertRespondsTo('show', element);
}, this);
[null,'','a','aa'].each(function(content) {
var textnode = document.createTextNode(content);
this.assertEqual(textnode, Element.extend(textnode));
this.assert(typeof textnode['show'] == 'undefined');
}, this);
// clean up
delete Element.Methods.Simulated.simulatedMethod;
},
testElementExtendReextendsDiscardedNodes: function() {
this.assertRespondsTo('show', $('discard_1'));
$('element_reextend_test').innerHTML += '<div id="discard_2"></div>';
this.assertRespondsTo('show', $('discard_1'));
},
testElementCleanWhitespace: function() {
Element.cleanWhitespace("test_whitespace");
this.assertEqual(3, $("test_whitespace").childNodes.length);
this.assertEqual(1, $("test_whitespace").firstChild.nodeType);
this.assertEqual('SPAN', $("test_whitespace").firstChild.tagName);
this.assertEqual(1, $("test_whitespace").firstChild.nextSibling.nodeType);
this.assertEqual('DIV', $("test_whitespace").firstChild.nextSibling.tagName);
this.assertEqual(1, $("test_whitespace").firstChild.nextSibling.nextSibling.nodeType);
this.assertEqual('SPAN', $("test_whitespace").firstChild.nextSibling.nextSibling.tagName);
var element = document.createElement('DIV');
element.appendChild(document.createTextNode(''));
element.appendChild(document.createTextNode(''));
this.assertEqual(2, element.childNodes.length);
Element.cleanWhitespace(element);
this.assertEqual(0, element.childNodes.length);
},
testElementEmpty: function() {
this.assert($('test-empty').empty());
this.assert($('test-empty-but-contains-whitespace').empty());
this.assert(!$('test-full').empty());
},
testDescendantOf: function() {
this.assert($('child').descendantOf('ancestor'),
'#child should be descendant of #ancestor');
this.assert($('child').descendantOf($('ancestor')),
'#child should be descendant of #ancestor');
this.assert(!$('ancestor').descendantOf($('child')),
'#ancestor should not be descendant of child');
this.assert($('great-grand-child').descendantOf('ancestor'), 'great-grand-child < ancestor');
this.assert($('grand-child').descendantOf('ancestor'), 'grand-child < ancestor');
this.assert($('great-grand-child').descendantOf('grand-child'), 'great-grand-child < grand-child');
this.assert($('grand-child').descendantOf('child'), 'grand-child < child');
this.assert($('great-grand-child').descendantOf('child'), 'great-grand-child < child');
this.assert($('sibling').descendantOf('ancestor'), 'sibling < ancestor');
this.assert($('grand-sibling').descendantOf('sibling'), 'grand-sibling < sibling');
this.assert($('grand-sibling').descendantOf('ancestor'), 'grand-sibling < ancestor');
this.assert($('grand-sibling').descendantOf(document.body), 'grand-sibling < body');
this.assert(!$('great-grand-child').descendantOf('great-grand-child'), 'great-grand-child < great-grand-child');
this.assert(!$('great-grand-child').descendantOf('sibling'), 'great-grand-child < sibling');
this.assert(!$('sibling').descendantOf('child'), 'sibling < child');
this.assert(!$('great-grand-child').descendantOf('not-in-the-family'), 'great-grand-child < not-in-the-family');
this.assert(!$('child').descendantOf('not-in-the-family'), 'child < not-in-the-family');
this.assert(!$(document.body).descendantOf('great-grand-child'),
'BODY should not be descendant of anything within it');
// dynamically-created elements
$('ancestor').insert(new Element('div', { id: 'weird-uncle' }));
this.assert($('weird-uncle').descendantOf('ancestor'),
'dynamically-created element should work properly');
$(document.body).insert(new Element('div', { id: 'impostor' }));
this.assert(!$('impostor').descendantOf('ancestor'),
'elements inserted elsewhere in the DOM tree should not be descendants');
// test descendantOf document
this.assert($(document.body).descendantOf(document),
'descendantOf(document) should behave predictably');
this.assert($(document.documentElement).descendantOf(document),
'descendantOf(document) should behave predictably');
},
testChildOf: function() {
this.assert($('child').childOf('ancestor'));
this.assert($('child').childOf($('ancestor')));
this.assert($('great-grand-child').childOf('ancestor'));
this.assert(!$('great-grand-child').childOf('not-in-the-family'));
this.assertIdentical(Element.Methods.childOf, Element.Methods.descendantOf);
},
testElementSetStyle: function() {
Element.setStyle('style_test_3',{ 'left': '2px' });
this.assertEqual('2px', $('style_test_3').style.left);
Element.setStyle('style_test_3',{ marginTop: '1px' });
this.assertEqual('1px', $('style_test_3').style.marginTop);
$('style_test_3').setStyle({ marginTop: '2px', left: '-1px' });
this.assertEqual('-1px', $('style_test_3').style.left);
this.assertEqual('2px', $('style_test_3').style.marginTop);
this.assertEqual('none', $('style_test_3').getStyle('float'));
$('style_test_3').setStyle({ 'float': 'left' });
this.assertEqual('left', $('style_test_3').getStyle('float'));
$('style_test_3').setStyle({ cssFloat: 'none' });
this.assertEqual('none', $('style_test_3').getStyle('float'));
this.assertEqual(1, $('style_test_3').getStyle('opacity'),
'#style_test_3 opacity should be 1');
$('style_test_3').setStyle({ opacity: 0.5 });
this.assertEqual(0.5, $('style_test_3').getStyle('opacity'));
$('style_test_3').setStyle({ opacity: '' });
this.assertEqual(1, $('style_test_3').getStyle('opacity'),
'#style_test_3 opacity should be 1');
$('style_test_3').setStyle({ opacity: 0 });
this.assertEqual(0, $('style_test_3').getStyle('opacity'),
'#style_test_3 opacity should be 0');
$('test_csstext_1').setStyle('font-size: 15px');
this.assertEqual('15px', $('test_csstext_1').getStyle('font-size'));
$('test_csstext_2').setStyle({height: '40px'});
$('test_csstext_2').setStyle('font-size: 15px');
this.assertEqual('15px', $('test_csstext_2').getStyle('font-size'));
this.assertEqual('40px', $('test_csstext_2').getStyle('height'));
$('test_csstext_3').setStyle('font-size: 15px');
this.assertEqual('15px', $('test_csstext_3').getStyle('font-size'));
this.assertEqual('1px', $('test_csstext_3').getStyle('border-top-width'));
$('test_csstext_4').setStyle('font-size: 15px');
this.assertEqual('15px', $('test_csstext_4').getStyle('font-size'));
$('test_csstext_4').setStyle('float: right; font-size: 10px');
this.assertEqual('right', $('test_csstext_4').getStyle('float'));
this.assertEqual('10px', $('test_csstext_4').getStyle('font-size'));
$('test_csstext_5').setStyle('float: left; opacity: .5; font-size: 10px');
this.assertEqual(parseFloat('0.5'), parseFloat($('test_csstext_5').getStyle('opacity')));
},
testElementSetStyleCamelized: function() {
this.assertNotEqual('30px', $('style_test_3').style.marginTop);
$('style_test_3').setStyle({ marginTop: '30px'}, true);
this.assertEqual('30px', $('style_test_3').style.marginTop);
},
testElementSetOpacity: function() {
[0, 0.1, 0.5, 0.999].each(function(opacity){
$('style_test_3').setOpacity(opacity);
// b/c of rounding issues on IE special case
var realOpacity = $('style_test_3').getStyle('opacity');
// opera rounds off to two significant digits, so we check for a
// ballpark figure
this.assert(
(Number(realOpacity) - opacity) <= 0.002,
'setting opacity to ' + opacity + ' (actual: ' + realOpacity + ')'
);
}, this);
this.assertEqual(0,
$('style_test_3').setOpacity(0.0000001).getStyle('opacity'));
// for Firefox, we don't set to 1, because of flickering
this.assert(
$('style_test_3').setOpacity(0.9999999).getStyle('opacity') > 0.999
);
/*
IE <= 7 needs a `hasLayout` for opacity ("filter") to function properly
`hasLayout` is triggered by setting `zoom` style to `1`,
In IE8 setting `zoom` does not affect `hasLayout`
and IE8 does not even need `hasLayout` for opacity to work
We do a proper feature test here
*/
var ZOOM_AFFECT_HAS_LAYOUT = (function(){
// IE7
var el = document.createElement('div');
el.style.zoom = 1;
var result = el.hasLayout;
el = null;
return result;
})();
if (ZOOM_AFFECT_HAS_LAYOUT) {
this.assert($('style_test_4').setOpacity(0.5).currentStyle.hasLayout);
this.assert(2, $('style_test_5').setOpacity(0.5).getStyle('zoom'));
this.assert(0.5, new Element('div').setOpacity(0.5).getOpacity());
this.assert(2, new Element('div').setOpacity(0.5).setStyle('zoom: 2;').getStyle('zoom'));
this.assert(2, new Element('div').setStyle('zoom: 2;').setOpacity(0.5).getStyle('zoom'));
}
},
testElementGetStyle: function() {
this.assertEqual("none",
$('style_test_1').getStyle('display'));
// not displayed, so "null" ("auto" is tranlated to "null")
this.assertNull(Element.getStyle('style_test_1', 'width'), 'elements that are hidden should return null on getStyle("width")');
$('style_test_1').show();
// from id rule
this.assertEqual("pointer",
Element.getStyle('style_test_1','cursor'));
this.assertEqual("block",
Element.getStyle('style_test_2','display'));
// we should always get something for width (if displayed)
// firefox and safari automatically send the correct value,
// IE is special-cased to do the same
this.assertEqual($('style_test_2').offsetWidth+'px', Element.getStyle('style_test_2','width'));
this.assertEqual("static",Element.getStyle('style_test_1','position'));
// from style
this.assertEqual("11px",
Element.getStyle('style_test_2','font-size'));
// from class
this.assertEqual("1px",
Element.getStyle('style_test_2','margin-left'));
['not_floating_none','not_floating_style','not_floating_inline'].each(function(element) {
this.assertEqual('none', $(element).getStyle('float'),
'float on ' + element);
this.assertEqual('none', $(element).getStyle('cssFloat'),
'cssFloat on ' + element);
}, this);
['floating_style','floating_inline'].each(function(element) {
this.assertEqual('left', $(element).getStyle('float'));
this.assertEqual('left', $(element).getStyle('cssFloat'));
}, this);
this.assertEqual(0.5, $('op1').getStyle('opacity'), 'get opacity on #op1');
this.assertEqual(0.5, $('op2').getStyle('opacity'), 'get opacity on #op2');
this.assertEqual(1.0, $('op3').getStyle('opacity'), 'get opacity on #op3');
$('op1').setStyle({opacity: '0.3'});
$('op2').setStyle({opacity: '0.3'});
$('op3').setStyle({opacity: '0.3'});
this.assertEqual(0.3, $('op1').getStyle('opacity'), 'get opacity on #op1');
this.assertEqual(0.3, $('op2').getStyle('opacity'), 'get opacity on #op2');
this.assertEqual(0.3, $('op3').getStyle('opacity'), 'get opacity on #op3');
$('op3').setStyle({opacity: 0});
this.assertEqual(0, $('op3').getStyle('opacity'), 'get opacity on #op3');