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; } 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')); this.assertNull($('noWayThisIDExists')); this.assertIdentical(document.getElementById('testdiv'), $('testdiv')); 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'); if (Prototype.Browser.IE) 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 (Prototype.Browser.IE) this.assertUndefined(document.getElementById('unextended')['show']); }, testElementInsertWithHTML: function() { Element.insert('insertions-main', {before:'

before text

more testing

'}); this.assert(getInnerHTML('insertions-container').startsWith('

before text

more testing

')); Element.insert('insertions-main', {after:'

after text

more testing

'}); this.assert(getInnerHTML('insertions-container').endsWith('

after text

more testing

')); Element.insert('insertions-main', {top:'

top text.

more testing

'}); this.assert(getInnerHTML('insertions-main').startsWith('

top text.

more testing

')); Element.insert('insertions-main', {bottom:'

bottom text.

more testing

'}); this.assert(getInnerHTML('insertions-main').endsWith('

bottom text.

more testing

')); }, testElementInsertWithDOMNode: function() { Element.insert('insertions-node-main', {before: createParagraph('node before')}); this.assert(getInnerHTML('insertions-node-container').startsWith('

node before

')); Element.insert('insertions-node-main', {after: createParagraph('node after')}); this.assert(getInnerHTML('insertions-node-container').endsWith('

node after

')); Element.insert('insertions-node-main', {top:createParagraph('node top')}); this.assert(getInnerHTML('insertions-node-main').startsWith('

node top

')); Element.insert('insertions-node-main', {bottom:createParagraph('node bottom')}); this.assert(getInnerHTML('insertions-node-main').endsWith('

node bottom

')); 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('

toelement

')); Element.insert('insertions-node-main', {bottom: {toElement: createParagraph.curry('bottom toElement') }}); this.assert(getInnerHTML('insertions-node-main').endsWith('

bottom toelement

')); }, testElementInsertWithToHTMLMethod: function() { Element.insert('insertions-node-main', {toHTML: function() { return '

toHTML

'} }); this.assert(getInnerHTML('insertions-node-main').endsWith('

tohtml

')); Element.insert('insertions-node-main', {bottom: {toHTML: function() { return '

bottom toHTML

'} }}); this.assert(getInnerHTML('insertions-node-main').endsWith('

bottom tohtml

')); }, testElementInsertWithNonString: function() { Element.insert('insertions-main', {bottom:3}); this.assert(getInnerHTML('insertions-main').endsWith('3')); }, testElementInsertInTables: function() { Element.insert('second_row', {after:'Third Row'}); this.assert($('second_row').descendantOf('table')); $('a_cell').insert({top:'hello world'}); this.assert($('a_cell').innerHTML.startsWith('hello world')); $('a_cell').insert({after:'hi planet'}); this.assertEqual('hi planet', $('a_cell').next().innerHTML); $('table_for_insertions').insert('a cell!'); this.assert($('table_for_insertions').innerHTML.gsub('\r\n', '').toLowerCase().include('a cell!')); $('row_1').insert({after:'last'}); this.assertEqual('last', $A($('table_for_row_insertions').getElementsByTagName('tr')).last().lastChild.innerHTML); }, testElementInsertInSelect: function() { var selectTop = $('select_for_insert_top'), selectBottom = $('select_for_insert_bottom'); selectBottom.insert(''); this.assertEqual('option 45', selectBottom.getValue()); selectTop.insert({top:''}); 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('

3

')); 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: '

a paragraph

' }); this.assertEqual('

a paragraph

', getInnerHTML(container)); element.insert({ after: 'some text' }); this.assertEqual('

a paragraph

some text', getInnerHTML(container)); element.insert({ top: '

a paragraph

' }); this.assertEqual('

a paragraph

', getInnerHTML(element)); element.insert('some text'); this.assertEqual('

a paragraph

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('
\ntestVar="hello!";\n'); this.assertEqual('hello from div!',$('testdiv').innerHTML); this.wait(100,function(){ this.assertEqual('hello!',testVar); Element.update('testdiv','another hello from div!\n