PageRenderTime 87ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 1ms

/dom/tests/mochitest/ajax/prototype/test/unit/dom_test.js

http://github.com/zpao/v8monkey
JavaScript | 1403 lines | 1098 code | 252 blank | 53 comment | 33 complexity | 930eebb5aadc609a6fcf9770d7690c34 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD

Large files files are truncated, but you can click here to view the full file

  1. var getInnerHTML = function(id) {
  2. return $(id).innerHTML.toString().toLowerCase().gsub(/[\r\n\t]/, '');
  3. };
  4. var createParagraph = function(text) {
  5. var p = document.createElement('p');
  6. p.appendChild(document.createTextNode(text));
  7. return p;
  8. }
  9. new Test.Unit.Runner({
  10. setup: function() {
  11. if (documentViewportProperties) return;
  12. // Based on properties check from http://www.quirksmode.org/viewport/compatibility.html
  13. documentViewportProperties = {
  14. properties : [
  15. 'self.pageXOffset', 'self.pageYOffset',
  16. 'self.screenX', 'self.screenY',
  17. 'self.innerHeight', 'self.innerWidth',
  18. 'self.outerHeight', 'self.outerWidth',
  19. 'self.screen.height', 'self.screen.width',
  20. 'self.screen.availHeight', 'self.screen.availWidth',
  21. 'self.screen.availTop', 'self.screen.availLeft',
  22. 'self.screen.Top', 'self.screen.Left',
  23. 'self.screenTop', 'self.screenLeft',
  24. 'document.body.clientHeight', 'document.body.clientWidth',
  25. 'document.body.scrollHeight', 'document.body.scrollWidth',
  26. 'document.body.scrollLeft', 'document.body.scrollTop',
  27. 'document.body.offsetHeight', 'document.body.offsetWidth',
  28. 'document.body.offsetTop', 'document.body.offsetLeft'
  29. ].inject([], function(properties, prop) {
  30. if (!self.screen && prop.include('self.screen')) return;
  31. if (!document.body && prop.include('document.body')) return;
  32. properties.push(prop);
  33. if (prop.include('.body') && document.documentElement)
  34. properties.push(prop.sub('.body', '.documentElement'));
  35. return properties;
  36. }),
  37. inspect : function() {
  38. var props = [];
  39. this.properties.each(function(prop) {
  40. if (eval(prop)) props[prop] = eval(prop);
  41. }, this);
  42. return props;
  43. }
  44. };
  45. },
  46. testDollarFunction: function() {
  47. this.assertUndefined($());
  48. this.assertNull(document.getElementById('noWayThisIDExists'));
  49. this.assertNull($('noWayThisIDExists'));
  50. this.assertIdentical(document.getElementById('testdiv'), $('testdiv'));
  51. this.assertEnumEqual([ $('testdiv'), $('container') ], $('testdiv', 'container'));
  52. this.assertEnumEqual([ $('testdiv'), undefined, $('container') ],
  53. $('testdiv', 'noWayThisIDExists', 'container'));
  54. var elt = $('testdiv');
  55. this.assertIdentical(elt, $(elt));
  56. this.assertRespondsTo('hide', elt);
  57. this.assertRespondsTo('childOf', elt);
  58. },
  59. testGetElementsByClassName: function() {
  60. if (document.getElementsByClassName.toString().include('[native code]')) {
  61. this.info("browser uses native getElementsByClassName; skipping tests");
  62. return;
  63. }
  64. var div = $('class_names'), list = $('class_names_ul');
  65. this.assertElementsMatch(document.getElementsByClassName('A'), 'p.A', 'ul#class_names_ul.A', 'li.A.C');
  66. if (Prototype.Browser.IE)
  67. this.assertUndefined(document.getElementById('unextended').show);
  68. this.assertElementsMatch(div.getElementsByClassName('B'), 'ul#class_names_ul.A.B', 'div.B.C.D');
  69. this.assertElementsMatch(div.getElementsByClassName('D C B'), 'div.B.C.D');
  70. this.assertElementsMatch(div.getElementsByClassName(' D\nC\tB '), 'div.B.C.D');
  71. this.assertElementsMatch(div.getElementsByClassName($w('D C B')));
  72. this.assertElementsMatch(list.getElementsByClassName('A'), 'li.A.C');
  73. this.assertElementsMatch(list.getElementsByClassName(' A '), 'li.A.C');
  74. this.assertElementsMatch(list.getElementsByClassName('C A'), 'li.A.C');
  75. this.assertElementsMatch(list.getElementsByClassName("C\nA "), 'li.A.C');
  76. this.assertElementsMatch(list.getElementsByClassName('B'));
  77. this.assertElementsMatch(list.getElementsByClassName('1'), 'li.1');
  78. this.assertElementsMatch(list.getElementsByClassName([1]), 'li.1');
  79. this.assertElementsMatch(list.getElementsByClassName(['1 junk']));
  80. this.assertElementsMatch(list.getElementsByClassName(''));
  81. this.assertElementsMatch(list.getElementsByClassName(' '));
  82. this.assertElementsMatch(list.getElementsByClassName(['']));
  83. this.assertElementsMatch(list.getElementsByClassName([' ', '']));
  84. this.assertElementsMatch(list.getElementsByClassName({}));
  85. // those lookups shouldn't have extended all nodes in document
  86. if (Prototype.Browser.IE) this.assertUndefined(document.getElementById('unextended')['show']);
  87. },
  88. testElementInsertWithHTML: function() {
  89. Element.insert('insertions-main', {before:'<p><em>before</em> text</p><p>more testing</p>'});
  90. this.assert(getInnerHTML('insertions-container').startsWith('<p><em>before</em> text</p><p>more testing</p>'));
  91. Element.insert('insertions-main', {after:'<p><em>after</em> text</p><p>more testing</p>'});
  92. this.assert(getInnerHTML('insertions-container').endsWith('<p><em>after</em> text</p><p>more testing</p>'));
  93. Element.insert('insertions-main', {top:'<p><em>top</em> text.</p><p>more testing</p>'});
  94. this.assert(getInnerHTML('insertions-main').startsWith('<p><em>top</em> text.</p><p>more testing</p>'));
  95. Element.insert('insertions-main', {bottom:'<p><em>bottom</em> text.</p><p>more testing</p>'});
  96. this.assert(getInnerHTML('insertions-main').endsWith('<p><em>bottom</em> text.</p><p>more testing</p>'));
  97. },
  98. testElementInsertWithDOMNode: function() {
  99. Element.insert('insertions-node-main', {before: createParagraph('node before')});
  100. this.assert(getInnerHTML('insertions-node-container').startsWith('<p>node before</p>'));
  101. Element.insert('insertions-node-main', {after: createParagraph('node after')});
  102. this.assert(getInnerHTML('insertions-node-container').endsWith('<p>node after</p>'));
  103. Element.insert('insertions-node-main', {top:createParagraph('node top')});
  104. this.assert(getInnerHTML('insertions-node-main').startsWith('<p>node top</p>'));
  105. Element.insert('insertions-node-main', {bottom:createParagraph('node bottom')});
  106. this.assert(getInnerHTML('insertions-node-main').endsWith('<p>node bottom</p>'));
  107. this.assertEqual($('insertions-node-main'), $('insertions-node-main').insert(document.createElement('p')));
  108. },
  109. testElementInsertWithToElementMethod: function() {
  110. Element.insert('insertions-node-main', {toElement: createParagraph.curry('toElement') });
  111. this.assert(getInnerHTML('insertions-node-main').endsWith('<p>toelement</p>'));
  112. Element.insert('insertions-node-main', {bottom: {toElement: createParagraph.curry('bottom toElement') }});
  113. this.assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom toelement</p>'));
  114. },
  115. testElementInsertWithToHTMLMethod: function() {
  116. Element.insert('insertions-node-main', {toHTML: function() { return '<p>toHTML</p>'} });
  117. this.assert(getInnerHTML('insertions-node-main').endsWith('<p>tohtml</p>'));
  118. Element.insert('insertions-node-main', {bottom: {toHTML: function() { return '<p>bottom toHTML</p>'} }});
  119. this.assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom tohtml</p>'));
  120. },
  121. testElementInsertWithNonString: function() {
  122. Element.insert('insertions-main', {bottom:3});
  123. this.assert(getInnerHTML('insertions-main').endsWith('3'));
  124. },
  125. testElementInsertInTables: function() {
  126. Element.insert('second_row', {after:'<tr id="third_row"><td>Third Row</td></tr>'});
  127. this.assert($('second_row').descendantOf('table'));
  128. $('a_cell').insert({top:'hello world'});
  129. this.assert($('a_cell').innerHTML.startsWith('hello world'));
  130. $('a_cell').insert({after:'<td>hi planet</td>'});
  131. this.assertEqual('hi planet', $('a_cell').next().innerHTML);
  132. $('table_for_insertions').insert('<tr><td>a cell!</td></tr>');
  133. this.assert($('table_for_insertions').innerHTML.gsub('\r\n', '').toLowerCase().include('<tr><td>a cell!</td></tr>'));
  134. $('row_1').insert({after:'<tr></tr><tr></tr><tr><td>last</td></tr>'});
  135. this.assertEqual('last', $A($('table_for_row_insertions').getElementsByTagName('tr')).last().lastChild.innerHTML);
  136. },
  137. testElementInsertInSelect: function() {
  138. var selectTop = $('select_for_insert_top'), selectBottom = $('select_for_insert_bottom');
  139. selectBottom.insert('<option value="33">option 33</option><option selected="selected">option 45</option>');
  140. this.assertEqual('option 45', selectBottom.getValue());
  141. selectTop.insert({top:'<option value="A">option A</option><option value="B" selected="selected">option B</option>'});
  142. this.assertEqual(4, selectTop.options.length);
  143. },
  144. testElementMethodInsert: function() {
  145. $('element-insertions-main').insert({before:'some text before'});
  146. this.assert(getInnerHTML('element-insertions-container').startsWith('some text before'));
  147. $('element-insertions-main').insert({after:'some text after'});
  148. this.assert(getInnerHTML('element-insertions-container').endsWith('some text after'));
  149. $('element-insertions-main').insert({top:'some text top'});
  150. this.assert(getInnerHTML('element-insertions-main').startsWith('some text top'));
  151. $('element-insertions-main').insert({bottom:'some text bottom'});
  152. this.assert(getInnerHTML('element-insertions-main').endsWith('some text bottom'));
  153. $('element-insertions-main').insert('some more text at the bottom');
  154. this.assert(getInnerHTML('element-insertions-main').endsWith('some more text at the bottom'));
  155. $('element-insertions-main').insert({TOP:'some text uppercase top'});
  156. this.assert(getInnerHTML('element-insertions-main').startsWith('some text uppercase top'));
  157. $('element-insertions-multiple-main').insert({
  158. top:'1', bottom:2, before: new Element('p').update('3'), after:'4'
  159. });
  160. this.assert(getInnerHTML('element-insertions-multiple-main').startsWith('1'));
  161. this.assert(getInnerHTML('element-insertions-multiple-main').endsWith('2'));
  162. this.assert(getInnerHTML('element-insertions-multiple-container').startsWith('<p>3</p>'));
  163. this.assert(getInnerHTML('element-insertions-multiple-container').endsWith('4'));
  164. $('element-insertions-main').update('test');
  165. $('element-insertions-main').insert(null);
  166. $('element-insertions-main').insert({bottom:null});
  167. this.assertEqual('test', getInnerHTML('element-insertions-main'));
  168. $('element-insertions-main').insert(1337);
  169. this.assertEqual('test1337', getInnerHTML('element-insertions-main'));
  170. },
  171. testNewElementInsert: function() {
  172. var container = new Element('div');
  173. element = new Element('div');
  174. container.insert(element);
  175. element.insert({ before: '<p>a paragraph</p>' });
  176. this.assertEqual('<p>a paragraph</p><div></div>', getInnerHTML(container));
  177. element.insert({ after: 'some text' });
  178. this.assertEqual('<p>a paragraph</p><div></div>some text', getInnerHTML(container));
  179. element.insert({ top: '<p>a paragraph</p>' });
  180. this.assertEqual('<p>a paragraph</p>', getInnerHTML(element));
  181. element.insert('some text');
  182. this.assertEqual('<p>a paragraph</p>some text', getInnerHTML(element));
  183. },
  184. testInsertionBackwardsCompatibility: function() {
  185. new Insertion.Before('element-insertions-main', 'some backward-compatibility testing before');
  186. this.assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing before'));
  187. new Insertion.After('element-insertions-main', 'some backward-compatibility testing after');
  188. this.assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing after'));
  189. new Insertion.Top('element-insertions-main', 'some backward-compatibility testing top');
  190. this.assert(getInnerHTML('element-insertions-main').startsWith('some backward-compatibility testing top'));
  191. new Insertion.Bottom('element-insertions-main', 'some backward-compatibility testing bottom');
  192. this.assert(getInnerHTML('element-insertions-main').endsWith('some backward-compatibility testing bottom'));
  193. },
  194. testElementWrap: function() {
  195. var element = $('wrap'), parent = document.createElement('div');
  196. element.wrap();
  197. this.assert(getInnerHTML('wrap-container').startsWith('<div><p'));
  198. element.wrap('div');
  199. this.assert(getInnerHTML('wrap-container').startsWith('<div><div><p'));
  200. element.wrap(parent);
  201. this.assert(Object.isFunction(parent.setStyle));
  202. this.assert(getInnerHTML('wrap-container').startsWith('<div><div><div><p'));
  203. element.wrap('div', {className: 'wrapper'});
  204. this.assert(element.up().hasClassName('wrapper'));
  205. element.wrap({className: 'other-wrapper'});
  206. this.assert(element.up().hasClassName('other-wrapper'));
  207. element.wrap(new Element('div'), {className: 'yet-other-wrapper'});
  208. this.assert(element.up().hasClassName('yet-other-wrapper'));
  209. var orphan = new Element('p'), div = new Element('div');
  210. orphan.wrap(div);
  211. this.assertEqual(orphan.parentNode, div);
  212. },
  213. testElementWrapReturnsWrapper: function() {
  214. var element = new Element("div");
  215. var wrapper = element.wrap("div");
  216. this.assertNotEqual(element, wrapper);
  217. this.assertEqual(element.up(), wrapper);
  218. },
  219. testElementVisible: function(){
  220. this.assertNotEqual('none', $('test-visible').style.display);
  221. this.assertEqual('none', $('test-hidden').style.display);
  222. },
  223. testElementToggle: function(){
  224. $('test-toggle-visible').toggle();
  225. this.assert(!$('test-toggle-visible').visible());
  226. $('test-toggle-visible').toggle();
  227. this.assert($('test-toggle-visible').visible());
  228. $('test-toggle-hidden').toggle();
  229. this.assert($('test-toggle-hidden').visible());
  230. $('test-toggle-hidden').toggle();
  231. this.assert(!$('test-toggle-hidden').visible());
  232. },
  233. testElementShow: function(){
  234. $('test-show-visible').show();
  235. this.assert($('test-show-visible').visible());
  236. $('test-show-hidden').show();
  237. this.assert($('test-show-hidden').visible());
  238. },
  239. testElementHide: function(){
  240. $('test-hide-visible').hide();
  241. this.assert(!$('test-hide-visible').visible());
  242. $('test-hide-hidden').hide();
  243. this.assert(!$('test-hide-hidden').visible());
  244. },
  245. testElementRemove: function(){
  246. $('removable').remove();
  247. this.assert($('removable-container').empty());
  248. },
  249. testElementUpdate: function() {
  250. $('testdiv').update('hello from div!');
  251. this.assertEqual('hello from div!', $('testdiv').innerHTML);
  252. Element.update('testdiv', 'another hello from div!');
  253. this.assertEqual('another hello from div!', $('testdiv').innerHTML);
  254. Element.update('testdiv', 123);
  255. this.assertEqual('123', $('testdiv').innerHTML);
  256. Element.update('testdiv');
  257. this.assertEqual('', $('testdiv').innerHTML);
  258. Element.update('testdiv', '&nbsp;');
  259. this.assert(!$('testdiv').innerHTML.empty());
  260. },
  261. testElementUpdateWithScript: function() {
  262. $('testdiv').update('hello from div!<script>\ntestVar="hello!";\n</'+'script>');
  263. this.assertEqual('hello from div!',$('testdiv').innerHTML);
  264. this.wait(100,function(){
  265. this.assertEqual('hello!',testVar);
  266. Element.update('testdiv','another hello from div!\n<script>testVar="another hello!"</'+'script>\nhere it goes');
  267. // note: IE normalizes whitespace (like line breaks) to single spaces, thus the match test
  268. this.assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv').innerHTML);
  269. this.wait(100,function(){
  270. this.assertEqual('another hello!',testVar);
  271. Element.update('testdiv','a\n<script>testVar="a"\ntestVar="b"</'+'script>');
  272. this.wait(100,function(){
  273. this.assertEqual('b', testVar);
  274. Element.update('testdiv',
  275. 'x<script>testVar2="a"</'+'script>\nblah\n'+
  276. 'x<script>testVar2="b"</'+'script>');
  277. this.wait(100,function(){
  278. this.assertEqual('b', testVar2);
  279. });
  280. });
  281. });
  282. });
  283. },
  284. testElementUpdateInTableRow: function() {
  285. $('second_row').update('<td id="i_am_a_td">test</td>');
  286. this.assertEqual('test',$('i_am_a_td').innerHTML);
  287. Element.update('second_row','<td id="i_am_a_td">another <span>test</span></td>');
  288. this.assertEqual('another <span>test</span>',$('i_am_a_td').innerHTML.toLowerCase());
  289. },
  290. testElementUpdateInTableCell: function() {
  291. Element.update('a_cell','another <span>test</span>');
  292. this.assertEqual('another <span>test</span>',$('a_cell').innerHTML.toLowerCase());
  293. },
  294. testElementUpdateInTable: function() {
  295. Element.update('table','<tr><td>boo!</td></tr>');
  296. this.assertMatch(/^<tr>\s*<td>boo!<\/td><\/tr>$/,$('table').innerHTML.toLowerCase());
  297. },
  298. testElementUpdateInSelect: function() {
  299. var select = $('select_for_update');
  300. select.update('<option value="3">option 3</option><option selected="selected">option 4</option>');
  301. this.assertEqual('option 4', select.getValue());
  302. },
  303. testElementUpdateWithDOMNode: function() {
  304. $('testdiv').update(new Element('div').insert('bla'));
  305. this.assertEqual('<div>bla</div>', getInnerHTML('testdiv'));
  306. },
  307. testElementUpdateWithToElementMethod: function() {
  308. $('testdiv').update({toElement: createParagraph.curry('foo')});
  309. this.assertEqual('<p>foo</p>', getInnerHTML('testdiv'));
  310. },
  311. testElementUpdateWithToHTMLMethod: function() {
  312. $('testdiv').update({toHTML: function() { return 'hello world' }});
  313. this.assertEqual('hello world', getInnerHTML('testdiv'));
  314. },
  315. testElementReplace: function() {
  316. $('testdiv-replace-1').replace('hello from div!');
  317. this.assertEqual('hello from div!', $('testdiv-replace-container-1').innerHTML);
  318. $('testdiv-replace-2').replace(123);
  319. this.assertEqual('123', $('testdiv-replace-container-2').innerHTML);
  320. $('testdiv-replace-3').replace();
  321. this.assertEqual('', $('testdiv-replace-container-3').innerHTML);
  322. $('testrow-replace').replace('<tr><td>hello</td></tr>');
  323. this.assert(getInnerHTML('testrow-replace-container').include('<tr><td>hello</td></tr>'));
  324. $('testoption-replace').replace('<option>hello</option>');
  325. this.assert($('testoption-replace-container').innerHTML.include('hello'));
  326. Element.replace('testinput-replace', '<p>hello world</p>');
  327. this.assertEqual('<p>hello world</p>', getInnerHTML('testform-replace'));
  328. Element.replace('testform-replace', '<form></form>');
  329. this.assertEqual('<p>some text</p><form></form><p>some text</p>', getInnerHTML('testform-replace-container'));
  330. },
  331. testElementReplaceWithScript: function() {
  332. $('testdiv-replace-4').replace('hello from div!<script>testVarReplace="hello!"</'+'script>');
  333. this.assertEqual('hello from div!', $('testdiv-replace-container-4').innerHTML);
  334. this.wait(100,function(){
  335. this.assertEqual('hello!',testVarReplace);
  336. $('testdiv-replace-5').replace('another hello from div!\n<script>testVarReplace="another hello!"</'+'script>\nhere it goes');
  337. // note: IE normalizes whitespace (like line breaks) to single spaces, thus the match test
  338. this.assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv-replace-container-5').innerHTML);
  339. this.wait(100,function(){
  340. this.assertEqual('another hello!',testVarReplace);
  341. });
  342. });
  343. },
  344. testElementReplaceWithDOMNode: function() {
  345. $('testdiv-replace-element').replace(createParagraph('hello'));
  346. this.assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-element'));
  347. },
  348. testElementReplaceWithToElementMethod: function() {
  349. $('testdiv-replace-toelement').replace({toElement: createParagraph.curry('hello')});
  350. this.assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-toelement'));
  351. },
  352. testElementReplaceWithToHTMLMethod: function() {
  353. $('testdiv-replace-tohtml').replace({toHTML: function() { return 'hello' }});
  354. this.assertEqual('hello', getInnerHTML('testdiv-replace-container-tohtml'));
  355. },
  356. testElementSelectorMethod: function() {
  357. ['getElementsBySelector','select'].each(function(method) {
  358. var testSelector = $('container')[method]('p.test');
  359. this.assertEqual(testSelector.length, 4);
  360. this.assertEqual(testSelector[0], $('intended'));
  361. this.assertEqual(testSelector[0], $$('#container p.test')[0]);
  362. }, this);
  363. },
  364. testElementAdjacent: function() {
  365. var elements = $('intended').adjacent('p');
  366. this.assertEqual(elements.length, 3);
  367. elements.each(function(element){
  368. this.assert(element != $('intended'));
  369. }, this);
  370. },
  371. testElementIdentify: function() {
  372. var parent = $('identification');
  373. this.assertEqual(parent.down().identify(), 'predefined_id');
  374. this.assert(parent.down(1).identify().startsWith('anonymous_element_'));
  375. this.assert(parent.down(2).identify().startsWith('anonymous_element_'));
  376. this.assert(parent.down(3).identify().startsWith('anonymous_element_'));
  377. this.assert(parent.down(3).id !== parent.down(2).id);
  378. },
  379. testElementClassNameMethod: function() {
  380. var testClassNames = $('container').getElementsByClassName('test');
  381. var testSelector = $('container').getElementsBySelector('p.test');
  382. this.assertEqual(testClassNames[0], $('intended'));
  383. this.assertEqual(testClassNames.length, 4);
  384. this.assertEqual(testSelector[3], testClassNames[3]);
  385. this.assertEqual(testClassNames.length, testSelector.length);
  386. },
  387. testElementAncestors: function() {
  388. var ancestors = $('navigation_test_f').ancestors();
  389. this.assertElementsMatch(ancestors, 'ul', 'li', 'ul#navigation_test',
  390. 'div#nav_tests_isolator', 'body', 'html');
  391. this.assertElementsMatch(ancestors.last().ancestors());
  392. var dummy = $(document.createElement('DIV'));
  393. dummy.innerHTML = '<div></div>'.times(3);
  394. this.assert(typeof $(dummy.childNodes[0]).ancestors()[0]['setStyle'] == 'function');
  395. },
  396. testElementDescendants: function() {
  397. this.assertElementsMatch($('navigation_test').descendants(),
  398. 'li', 'em', 'li', 'em.dim', 'li', 'em', 'ul', 'li',
  399. 'em.dim', 'li#navigation_test_f', 'em', 'li', 'em');
  400. this.assertElementsMatch($('navigation_test_f').descendants(), 'em');
  401. var dummy = $(document.createElement('DIV'));
  402. dummy.innerHTML = '<div></div>'.times(3);
  403. this.assert(typeof dummy.descendants()[0].setStyle == 'function');
  404. },
  405. testElementFirstDescendant: function() {
  406. this.assertElementMatches($('navigation_test').firstDescendant(), 'li.first');
  407. this.assertNull($('navigation_test_next_sibling').firstDescendant());
  408. },
  409. testElementChildElements: function() {
  410. this.assertElementsMatch($('navigation_test').childElements(),
  411. 'li.first', 'li', 'li#navigation_test_c', 'li.last');
  412. this.assertNotEqual(0, $('navigation_test_next_sibling').childNodes.length);
  413. this.assertEnumEqual([], $('navigation_test_next_sibling').childElements());
  414. var dummy = $(document.createElement('DIV'));
  415. dummy.innerHTML = '<div></div>'.times(3);
  416. this.assert(typeof dummy.childElements()[0].setStyle == 'function');
  417. },
  418. testElementImmediateDescendants: function() {
  419. this.assertIdentical(Element.Methods.childElements, Element.Methods.immediateDescendants);
  420. },
  421. testElementPreviousSiblings: function() {
  422. this.assertElementsMatch($('navigation_test').previousSiblings(),
  423. 'span#nav_test_prev_sibling', 'p.test', 'div', 'div#nav_test_first_sibling');
  424. this.assertElementsMatch($('navigation_test_f').previousSiblings(), 'li');
  425. var dummy = $(document.createElement('DIV'));
  426. dummy.innerHTML = '<div></div>'.times(3);
  427. this.assert(typeof $(dummy.childNodes[1]).previousSiblings()[0].setStyle == 'function');
  428. },
  429. testElementNextSiblings: function() {
  430. this.assertElementsMatch($('navigation_test').nextSiblings(),
  431. 'div#navigation_test_next_sibling', 'p');
  432. this.assertElementsMatch($('navigation_test_f').nextSiblings());
  433. var dummy = $(document.createElement('DIV'));
  434. dummy.innerHTML = '<div></div>'.times(3);
  435. this.assert(typeof $(dummy.childNodes[0]).nextSiblings()[0].setStyle == 'function');
  436. },
  437. testElementSiblings: function() {
  438. this.assertElementsMatch($('navigation_test').siblings(),
  439. 'div#nav_test_first_sibling', 'div', 'p.test',
  440. 'span#nav_test_prev_sibling', 'div#navigation_test_next_sibling', 'p');
  441. var dummy = $(document.createElement('DIV'));
  442. dummy.innerHTML = '<div></div>'.times(3);
  443. this.assert(typeof $(dummy.childNodes[0]).siblings()[0].setStyle == 'function');
  444. },
  445. testElementUp: function() {
  446. var element = $('navigation_test_f');
  447. this.assertElementMatches(element.up(), 'ul');
  448. this.assertElementMatches(element.up(0), 'ul');
  449. this.assertElementMatches(element.up(1), 'li');
  450. this.assertElementMatches(element.up(2), 'ul#navigation_test');
  451. this.assertElementsMatch(element.up('li').siblings(), 'li.first', 'li', 'li.last');
  452. this.assertElementMatches(element.up('ul', 1), 'ul#navigation_test');
  453. this.assertEqual(undefined, element.up('garbage'));
  454. this.assertEqual(undefined, element.up(6));
  455. this.assertElementMatches(element.up('.non-existant, ul'), 'ul');
  456. var dummy = $(document.createElement('DIV'));
  457. dummy.innerHTML = '<div></div>'.times(3);
  458. this.assert(typeof $(dummy.childNodes[0]).up().setStyle == 'function');
  459. },
  460. testElementDown: function() {
  461. var element = $('navigation_test');
  462. this.assertElementMatches(element.down(), 'li.first');
  463. this.assertElementMatches(element.down(0), 'li.first');
  464. this.assertElementMatches(element.down(1), 'em');
  465. this.assertElementMatches(element.down('li', 5), 'li.last');
  466. this.assertElementMatches(element.down('ul').down('li', 1), 'li#navigation_test_f');
  467. this.assertElementMatches(element.down('.non-existant, .first'), 'li.first');
  468. var dummy = $(document.createElement('DIV'));
  469. dummy.innerHTML = '<div></div>'.times(3);
  470. this.assert(typeof dummy.down().setStyle == 'function');
  471. var input = $$('input')[0];
  472. this.assertNothingRaised(function(){ input.down('span') });
  473. this.assertUndefined(input.down('span'));
  474. },
  475. testElementPrevious: function() {
  476. var element = $('navigation_test').down('li.last');
  477. this.assertElementMatches(element.previous(), 'li#navigation_test_c');
  478. this.assertElementMatches(element.previous(1), 'li');
  479. this.assertElementMatches(element.previous('.first'), 'li.first');
  480. this.assertEqual(undefined, element.previous(3));
  481. this.assertEqual(undefined, $('navigation_test').down().previous());
  482. this.assertElementMatches(element.previous('.non-existant, .first'), 'li.first');
  483. var dummy = $(document.createElement('DIV'));
  484. dummy.innerHTML = '<div></div>'.times(3);
  485. this.assert(typeof $(dummy.childNodes[1]).previous().setStyle == 'function');
  486. },
  487. testElementNext: function() {
  488. var element = $('navigation_test').down('li.first');
  489. this.assertElementMatches(element.next(), 'li');
  490. this.assertElementMatches(element.next(1), 'li#navigation_test_c');
  491. this.assertElementMatches(element.next(2), 'li.last');
  492. this.assertElementMatches(element.next('.last'), 'li.last');
  493. this.assertEqual(undefined, element.next(3));
  494. this.assertEqual(undefined, element.next(2).next());
  495. this.assertElementMatches(element.next('.non-existant, .last'), 'li.last');
  496. var dummy = $(document.createElement('DIV'));
  497. dummy.innerHTML = '<div></div>'.times(3);
  498. this.assert(typeof $(dummy.childNodes[0]).next().setStyle == 'function');
  499. },
  500. testElementInspect: function() {
  501. this.assertEqual('<ul id="navigation_test">', $('navigation_test').inspect());
  502. this.assertEqual('<li class="first">', $('navigation_test').down().inspect());
  503. this.assertEqual('<em>', $('navigation_test').down(1).inspect());
  504. },
  505. testElementMakeClipping: function() {
  506. var chained = Element.extend(document.createElement('DIV'));
  507. this.assertEqual(chained, chained.makeClipping());
  508. this.assertEqual(chained, chained.makeClipping());
  509. this.assertEqual(chained, chained.makeClipping().makeClipping());
  510. this.assertEqual(chained, chained.undoClipping());
  511. this.assertEqual(chained, chained.undoClipping());
  512. this.assertEqual(chained, chained.undoClipping().makeClipping());
  513. ['hidden','visible','scroll'].each( function(overflowValue) {
  514. var element = $('element_with_'+overflowValue+'_overflow');
  515. this.assertEqual(overflowValue, element.getStyle('overflow'));
  516. element.makeClipping();
  517. this.assertEqual('hidden', element.getStyle('overflow'));
  518. element.undoClipping();
  519. this.assertEqual(overflowValue, element.getStyle('overflow'));
  520. }, this);
  521. },
  522. testElementExtend: function() {
  523. var element = $('element_extend_test');
  524. this.assertRespondsTo('show', element);
  525. var XHTML_TAGS = $w(
  526. 'a abbr acronym address applet area '+
  527. 'b bdo big blockquote br button caption '+
  528. 'cite code col colgroup dd del dfn div dl dt '+
  529. 'em fieldset form h1 h2 h3 h4 h5 h6 hr '+
  530. 'i iframe img input ins kbd label legend li '+
  531. 'map object ol optgroup option p param pre q samp '+
  532. 'script select small span strong style sub sup '+
  533. 'table tbody td textarea tfoot th thead tr tt ul var');
  534. XHTML_TAGS.each(function(tag) {
  535. var element = document.createElement(tag);
  536. this.assertEqual(element, Element.extend(element));
  537. this.assertRespondsTo('show', element);
  538. }, this);
  539. [null,'','a','aa'].each(function(content) {
  540. var textnode = document.createTextNode(content);
  541. this.assertEqual(textnode, Element.extend(textnode));
  542. this.assert(typeof textnode['show'] == 'undefined');
  543. }, this);
  544. },
  545. testElementExtendReextendsDiscardedNodes: function() {
  546. this.assertRespondsTo('show', $('discard_1'));
  547. $('element_reextend_test').innerHTML += '<div id="discard_2"></div>';
  548. this.assertRespondsTo('show', $('discard_1'));
  549. },
  550. testElementCleanWhitespace: function() {
  551. Element.cleanWhitespace("test_whitespace");
  552. this.assertEqual(3, $("test_whitespace").childNodes.length);
  553. this.assertEqual(1, $("test_whitespace").firstChild.nodeType);
  554. this.assertEqual('SPAN', $("test_whitespace").firstChild.tagName);
  555. this.assertEqual(1, $("test_whitespace").firstChild.nextSibling.nodeType);
  556. this.assertEqual('DIV', $("test_whitespace").firstChild.nextSibling.tagName);
  557. this.assertEqual(1, $("test_whitespace").firstChild.nextSibling.nextSibling.nodeType);
  558. this.assertEqual('SPAN', $("test_whitespace").firstChild.nextSibling.nextSibling.tagName);
  559. var element = document.createElement('DIV');
  560. element.appendChild(document.createTextNode(''));
  561. element.appendChild(document.createTextNode(''));
  562. this.assertEqual(2, element.childNodes.length);
  563. Element.cleanWhitespace(element);
  564. this.assertEqual(0, element.childNodes.length);
  565. },
  566. testElementEmpty: function() {
  567. this.assert($('test-empty').empty());
  568. this.assert($('test-empty-but-contains-whitespace').empty());
  569. this.assert(!$('test-full').empty());
  570. },
  571. testDescendantOf: function() {
  572. this.assert($('child').descendantOf('ancestor'));
  573. this.assert($('child').descendantOf($('ancestor')));
  574. this.assert(!$('ancestor').descendantOf($('child')));
  575. this.assert($('great-grand-child').descendantOf('ancestor'), 'great-grand-child < ancestor');
  576. this.assert($('grand-child').descendantOf('ancestor'), 'grand-child < ancestor');
  577. this.assert($('great-grand-child').descendantOf('grand-child'), 'great-grand-child < grand-child');
  578. this.assert($('grand-child').descendantOf('child'), 'grand-child < child');
  579. this.assert($('great-grand-child').descendantOf('child'), 'great-grand-child < child');
  580. this.assert($('sibling').descendantOf('ancestor'), 'sibling < ancestor');
  581. this.assert($('grand-sibling').descendantOf('sibling'), 'grand-sibling < sibling');
  582. this.assert($('grand-sibling').descendantOf('ancestor'), 'grand-sibling < ancestor');
  583. this.assert($('grand-sibling').descendantOf(document.body), 'grand-sibling < body');
  584. this.assert(!$('great-grand-child').descendantOf('great-grand-child'), 'great-grand-child < great-grand-child');
  585. this.assert(!$('great-grand-child').descendantOf('sibling'), 'great-grand-child < sibling');
  586. this.assert(!$('sibling').descendantOf('child'), 'sibling < child');
  587. this.assert(!$('great-grand-child').descendantOf('not-in-the-family'), 'great-grand-child < not-in-the-family');
  588. this.assert(!$('child').descendantOf('not-in-the-family'), 'child < not-in-the-family');
  589. this.assert(!$(document.body).descendantOf('great-grand-child'));
  590. // dynamically-created elements
  591. $('ancestor').insert(new Element('div', { id: 'weird-uncle' }));
  592. this.assert($('weird-uncle').descendantOf('ancestor'));
  593. $(document.body).insert(new Element('div', { id: 'impostor' }));
  594. this.assert(!$('impostor').descendantOf('ancestor'));
  595. // test descendantOf document
  596. this.assert($(document.body).descendantOf(document));
  597. this.assert($(document.documentElement).descendantOf(document));
  598. },
  599. testChildOf: function() {
  600. this.assert($('child').childOf('ancestor'));
  601. this.assert($('child').childOf($('ancestor')));
  602. this.assert($('great-grand-child').childOf('ancestor'));
  603. this.assert(!$('great-grand-child').childOf('not-in-the-family'));
  604. this.assertIdentical(Element.Methods.childOf, Element.Methods.descendantOf);
  605. },
  606. testElementSetStyle: function() {
  607. Element.setStyle('style_test_3',{ 'left': '2px' });
  608. this.assertEqual('2px', $('style_test_3').style.left);
  609. Element.setStyle('style_test_3',{ marginTop: '1px' });
  610. this.assertEqual('1px', $('style_test_3').style.marginTop);
  611. $('style_test_3').setStyle({ marginTop: '2px', left: '-1px' });
  612. this.assertEqual('-1px', $('style_test_3').style.left);
  613. this.assertEqual('2px', $('style_test_3').style.marginTop);
  614. this.assertEqual('none', $('style_test_3').getStyle('float'));
  615. $('style_test_3').setStyle({ 'float': 'left' });
  616. this.assertEqual('left', $('style_test_3').getStyle('float'));
  617. $('style_test_3').setStyle({ cssFloat: 'none' });
  618. this.assertEqual('none', $('style_test_3').getStyle('float'));
  619. this.assertEqual(1, $('style_test_3').getStyle('opacity'));
  620. $('style_test_3').setStyle({ opacity: 0.5 });
  621. this.assertEqual(0.5, $('style_test_3').getStyle('opacity'));
  622. $('style_test_3').setStyle({ opacity: '' });
  623. this.assertEqual(1, $('style_test_3').getStyle('opacity'));
  624. $('style_test_3').setStyle({ opacity: 0 });
  625. this.assertEqual(0, $('style_test_3').getStyle('opacity'));
  626. $('test_csstext_1').setStyle('font-size: 15px');
  627. this.assertEqual('15px', $('test_csstext_1').getStyle('font-size'));
  628. $('test_csstext_2').setStyle({height: '40px'});
  629. $('test_csstext_2').setStyle('font-size: 15px');
  630. this.assertEqual('15px', $('test_csstext_2').getStyle('font-size'));
  631. this.assertEqual('40px', $('test_csstext_2').getStyle('height'));
  632. $('test_csstext_3').setStyle('font-size: 15px');
  633. this.assertEqual('15px', $('test_csstext_3').getStyle('font-size'));
  634. this.assertEqual('1px', $('test_csstext_3').getStyle('border-top-width'));
  635. $('test_csstext_4').setStyle('font-size: 15px');
  636. this.assertEqual('15px', $('test_csstext_4').getStyle('font-size'));
  637. $('test_csstext_4').setStyle('float: right; font-size: 10px');
  638. this.assertEqual('right', $('test_csstext_4').getStyle('float'));
  639. this.assertEqual('10px', $('test_csstext_4').getStyle('font-size'));
  640. $('test_csstext_5').setStyle('float: left; opacity: .5; font-size: 10px');
  641. this.assertEqual(parseFloat('0.5'), parseFloat($('test_csstext_5').getStyle('opacity')));
  642. },
  643. testElementSetStyleCamelized: function() {
  644. this.assertNotEqual('30px', $('style_test_3').style.marginTop);
  645. $('style_test_3').setStyle({ marginTop: '30px'}, true);
  646. this.assertEqual('30px', $('style_test_3').style.marginTop);
  647. },
  648. testElementSetOpacity: function() {
  649. [0,0.1,0.5,0.999].each(function(opacity){
  650. $('style_test_3').setOpacity(opacity);
  651. // b/c of rounding issues on IE special case
  652. var realOpacity = $('style_test_3').getStyle('opacity');
  653. // opera rounds off to two significant digits, so we check for a
  654. // ballpark figure
  655. this.assert((Number(realOpacity) - opacity) <= 0.002, 'setting opacity to ' + opacity);
  656. }, this);
  657. this.assertEqual(0,
  658. $('style_test_3').setOpacity(0.0000001).getStyle('opacity'));
  659. // for Firefox, we don't set to 1, because of flickering
  660. this.assert(
  661. $('style_test_3').setOpacity(0.9999999).getStyle('opacity') > 0.999
  662. );
  663. if (Prototype.Browser.IE) {
  664. this.assert($('style_test_4').setOpacity(0.5).currentStyle.hasLayout);
  665. this.assert(2, $('style_test_5').setOpacity(0.5).getStyle('zoom'));
  666. this.assert(0.5, new Element('div').setOpacity(0.5).getOpacity());
  667. this.assert(2, new Element('div').setOpacity(0.5).setStyle('zoom: 2;').getStyle('zoom'));
  668. this.assert(2, new Element('div').setStyle('zoom: 2;').setOpacity(0.5).getStyle('zoom'));
  669. }
  670. },
  671. testElementGetStyle: function() {
  672. this.assertEqual("none",
  673. $('style_test_1').getStyle('display'));
  674. // not displayed, so "null" ("auto" is tranlated to "null")
  675. this.assertNull(Element.getStyle('style_test_1', 'width'), 'elements that are hidden should return null on getStyle("width")');
  676. $('style_test_1').show();
  677. // from id rule
  678. this.assertEqual("pointer",
  679. Element.getStyle('style_test_1','cursor'));
  680. this.assertEqual("block",
  681. Element.getStyle('style_test_2','display'));
  682. // we should always get something for width (if displayed)
  683. // firefox and safari automatically send the correct value,
  684. // IE is special-cased to do the same
  685. this.assertEqual($('style_test_2').offsetWidth+'px', Element.getStyle('style_test_2','width'));
  686. this.assertEqual("static",Element.getStyle('style_test_1','position'));
  687. // from style
  688. this.assertEqual("11px",
  689. Element.getStyle('style_test_2','font-size'));
  690. // from class
  691. this.assertEqual("1px",
  692. Element.getStyle('style_test_2','margin-left'));
  693. ['not_floating_none','not_floating_style','not_floating_inline'].each(function(element) {
  694. this.assertEqual('none', $(element).getStyle('float'));
  695. this.assertEqual('none', $(element).getStyle('cssFloat'));
  696. }, this);
  697. ['floating_style','floating_inline'].each(function(element) {
  698. this.assertEqual('left', $(element).getStyle('float'));
  699. this.assertEqual('left', $(element).getStyle('cssFloat'));
  700. }, this);
  701. this.assertEqual(0.5, $('op1').getStyle('opacity'));
  702. this.assertEqual(0.5, $('op2').getStyle('opacity'));
  703. this.assertEqual(1.0, $('op3').getStyle('opacity'));
  704. $('op1').setStyle({opacity: '0.3'});
  705. $('op2').setStyle({opacity: '0.3'});
  706. $('op3').setStyle({opacity: '0.3'});
  707. this.assertEqual(0.3, $('op1').getStyle('opacity'));
  708. this.assertEqual(0.3, $('op2').getStyle('opacity'));
  709. this.assertEqual(0.3, $('op3').getStyle('opacity'));
  710. $('op3').setStyle({opacity: 0});
  711. this.assertEqual(0, $('op3').getStyle('opacity'));
  712. if (navigator.appVersion.match(/MSIE/)) {
  713. this.assertEqual('alpha(opacity=30)', $('op1').getStyle('filter'));
  714. this.assertEqual('progid:DXImageTransform.Microsoft.Blur(strength=10)alpha(opacity=30)', $('op2').getStyle('filter'));
  715. $('op2').setStyle({opacity:''});
  716. this.assertEqual('progid:DXImageTransform.Microsoft.Blur(strength=10)', $('op2').getStyle('filter'));
  717. this.assertEqual('alpha(opacity=0)', $('op3').getStyle('filter'));
  718. this.assertEqual(0.3, $('op4-ie').getStyle('opacity'));
  719. }
  720. // verify that value is still found when using camelized
  721. // strings (function previously used getPropertyValue()
  722. // which expected non-camelized strings)
  723. this.assertEqual("12px", $('style_test_1').getStyle('fontSize'));
  724. // getStyle on width/height should return values according to
  725. // the CSS box-model, which doesn't include
  726. // margin, padding, or borders
  727. // TODO: This test fails on IE because there seems to be no way
  728. // to calculate this properly (clientWidth/Height returns 0)
  729. if (!navigator.appVersion.match(/MSIE/)) {
  730. this.assertEqual("14px", $('style_test_dimensions').getStyle('width'));
  731. this.assertEqual("17px", $('style_test_dimensions').getStyle('height'));
  732. }
  733. // height/width could always be calculated if it's set to "auto" (Firefox)
  734. this.assertNotNull($('auto_dimensions').getStyle('height'));
  735. this.assertNotNull($('auto_dimensions').getStyle('width'));
  736. },
  737. testElementGetOpacity: function() {
  738. this.assertEqual(0.45, $('op1').setOpacity(0.45).getOpacity());
  739. },
  740. testElementReadAttribute: function() {
  741. var attribFormIssues = $('attributes_with_issues_form');
  742. this.assertEqual('blah-class', attribFormIssues.readAttribute('class'));
  743. this.assertEqual('post', attribFormIssues.readAttribute('method'));
  744. this.assertEqual('string', typeof(attribFormIssues.readAttribute('action')));
  745. this.assertEqual('string', typeof(attribFormIssues.readAttribute('id')));
  746. $(document.body).insert('<div id="ie_href_test_div"></div>');
  747. $('ie_href_test_div').insert('<p>blah blah</p><a id="ie_href_test" href="test.html">blah</a>');
  748. this.assertEqual('test.html', $('ie_href_test').readAttribute('href'));
  749. this.assertEqual('test.html' , $('attributes_with_issues_1').readAttribute('href'));
  750. this.assertEqual('L' , $('attributes_with_issues_1').readAttribute('accesskey'));
  751. this.assertEqual('50' , $('attributes_with_issues_1').readAttribute('tabindex'));
  752. this.assertEqual('a link' , $('attributes_with_issues_1').readAttribute('title'));
  753. $('cloned_element_attributes_issue').readAttribute('foo')
  754. var clone = $('cloned_element_attributes_issue').cloneNode(true);
  755. clone.writeAttribute('foo', 'cloned');
  756. this.assertEqual('cloned', clone.readAttribute('foo'));
  757. this.assertEqual('original', $('cloned_element_attributes_issue').readAttribute('foo'));
  758. ['href', 'accesskey', 'accesskey', 'title'].each(function(attr) {
  759. this.assertEqual('' , $('attributes_with_issues_2').readAttribute(attr));
  760. }, this);
  761. ['checked','disabled','readonly','multiple'].each(function(attr) {
  762. this.assertEqual(attr, $('attributes_with_issues_'+attr).readAttribute(attr));
  763. }, this);
  764. this.assertEqual("alert('hello world');", $('attributes_with_issues_1').readAttribute('onclick'));
  765. this.assertNull($('attributes_with_issues_1').readAttribute('onmouseover'));
  766. this.assertEqual('date', $('attributes_with_issues_type').readAttribute('type'));
  767. this.assertEqual('text', $('attributes_with_issues_readonly').readAttribute('type'));
  768. var elements = $('custom_attributes').immediateDescendants();
  769. this.assertEnumEqual(['1', '2'], elements.invoke('readAttribute', 'foo'));
  770. this.assertEnumEqual(['2', null], elements.invoke('readAttribute', 'bar'));
  771. var table = $('write_attribute_table');
  772. this.assertEqual('4', table.readAttribute('cellspacing'));
  773. this.assertEqual('6', table.readAttribute('cellpadding'));
  774. // test for consistent flag value across browsers
  775. ["true", true, " ", 'rEadOnLy'].each(function(value) {
  776. $('attributes_with_issues_readonly').writeAttribute('readonly', value);
  777. this.assertEqual('readonly', $('attributes_with_issues_readonly').readAttribute('readonly'));
  778. }, this);
  779. },
  780. testElementWriteAttribute: function() {
  781. var element = Element.extend(document.body.appendChild(document.createElement('p')));
  782. this.assertRespondsTo('writeAttribute', element);
  783. this.assertEqual(element, element.writeAttribute('id', 'write_attribute_test'));
  784. this.assertEqual('write_attribute_test', element.id);
  785. this.assertEqual('http://prototypejs.org/', $('write_attribute_link').
  786. writeAttribute({href: 'http://prototypejs.org/', title: 'Home of Prototype'}).href);
  787. this.assertEqual('Home of Prototype', $('write_attribute_link').title);
  788. var element2 = Element.extend(document.createElement('p'));
  789. element2.writeAttribute('id', 'write_attribute_without_hash');
  790. this.assertEqual('write_attribute_without_hash', element2.id);
  791. element2.writeAttribute('animal', 'cat');
  792. this.assertEqual('cat', element2.readAttribute('animal'));
  793. },
  794. testElementWriteAttributeWithBooleans: function() {
  795. var input = $('write_attribute_input'),
  796. select = $('write_attribute_select'),
  797. checkbox = $('write_attribute_checkbox'),
  798. checkedCheckbox = $('write_attribute_checked_checkbox');
  799. this.assert( input. writeAttribute('readonly'). hasAttribute('readonly'));
  800. this.assert(!input. writeAttribute('readonly', false). hasAttribute('readonly'));
  801. this.assert( input. writeAttribute('readonly', true). hasAttribute('readonly'));
  802. this.assert(!input. writeAttribute('readonly', null). hasAttribute('readonly'));
  803. this.assert( input. writeAttribute('readonly', 'readonly').hasAttribute('readonly'));
  804. this.assert( select. writeAttribute('multiple'). hasAttribute('multiple'));
  805. this.assert( input. writeAttribute('disabled'). hasAttribute('disabled'));
  806. this.assert( checkbox. writeAttribute('checked'). checked);
  807. this.assert(!checkedCheckbox.writeAttribute('checked', false). checked);
  808. },
  809. testElementWriteAttributeWithIssues: function() {
  810. var input = $('write_attribute_input').writeAttribute({maxlength: 90, tabindex: 10}),
  811. td = $('write_attribute_td').writeAttribute({valign: 'bottom', colspan: 2, rowspan: 2});
  812. this.assertEqual(90, input.readAttribute('maxlength'));
  813. this.assertEqual(10, input.readAttribute('tabindex'));
  814. this.assertEqual(2, td.readAttribute('colspan'));
  815. this.assertEqual(2, td.readAttribute('rowspan'));
  816. this.assertEqual('bottom', td.readAttribute('valign'));
  817. var p = $('write_attribute_para'), label = $('write_attribute_label');
  818. this.assertEqual('some-class', p. writeAttribute({'class': 'some-class'}). readAttribute('class'));
  819. this.assertEqual('some-className', p. writeAttribute({className: 'some-className'}).readAttribute('class'));
  820. this.assertEqual('some-id', label.writeAttribute({'for': 'some-id'}). readAttribute('for'));
  821. this.assertEqual('some-other-id', label.writeAttribute({htmlFor: 'some-other-id'}). readAttribute('for'));
  822. this.assert(p.writeAttribute({style: 'width: 5px;'}).readAttribute('style').toLowerCase().include('width'));
  823. var table = $('write_attribute_table');
  824. table.writeAttribute('cellspacing', '2')
  825. table.writeAttribute('cellpadding', '3')
  826. this.assertEqual('2', table.readAttribute('cellspacing'));
  827. this.assertEqual('3', table.readAttribute('cellpadding'));
  828. var iframe = new Element('iframe', { frameborder: 0 });
  829. this.assertIdentical(0, parseInt(iframe.readAttribute('frameborder')));
  830. },
  831. testElementWriteAttributeWithCustom: function() {
  832. var p = $('write_attribute_para').writeAttribute({name: 'martin', location: 'stockholm', age: 26});
  833. this.assertEqual('martin', p.readAttribute('name'));
  834. this.assertEqual('stockholm', p.readAttribute('location'));
  835. this.assertEqual('26', p.readAttribute('age'));
  836. },
  837. testElementHasAttribute: function() {
  838. var label = $('write_attribute_label');
  839. this.assertIdentical(true, label.hasAttribute('for'));
  840. this.assertIdentical(false, label.hasAttribute('htmlFor'));
  841. this.assertIdentical(false, label.hasAttribute('className'));
  842. this.assertIdentical(false, label.hasAttribute('rainbows'));
  843. var input = $('write_attribute_input');
  844. this.assertNotIdentical(null, input.hasAttribute('readonly'));
  845. this.assertNotIdentical(null, input.hasAttribute('readOnly'));
  846. },
  847. testNewElement: function() {
  848. this.assert(new Element('h1'));
  849. var XHTML_TAGS = $w(
  850. 'a abbr acronym address area '+
  851. 'b bdo big blockquote br button caption '+
  852. 'cite code col colgroup dd del dfn div dl dt '+
  853. 'em fieldset form h1 h2 h3 h4 h5 h6 hr '+
  854. 'i iframe img input ins kbd label legend li '+
  855. 'map object ol optgroup option p param pre q samp '+
  856. 'script select small span strong style sub sup '+
  857. 'table tbody td textarea tfoot th thead tr tt ul var');
  858. XHTML_TAGS.each(function(tag, index) {
  859. var id = tag + '_' + index, element = document.body.appendChild(new Element(tag, {id: id}));
  860. this.assertEqual(tag, element.tagName.toLowerCase());
  861. this.assertEqual(element, document.body.lastChild);
  862. this.assertEqual(id, element.id);
  863. }, this);
  864. this.assertRespondsTo('update', new Element('div'));
  865. Element.addMethods({
  866. cheeseCake: function(){
  867. return 'Cheese cake';
  868. }
  869. });
  870. this.assertRespondsTo('cheeseCake', new Element('div'));
  871. /* window.ElementOld = function(tagName, attributes) {
  872. if (Prototype.Browser.IE && attributes && attributes.name) {
  873. tagName = '<' + tagName + ' name="' + attributes.name + '">';
  874. delete attributes.name;
  875. }
  876. return Element.extend(document.createElement(tagName)).writeAttribute(attributes || {});
  877. };
  878. this.benchmark(function(){
  879. XHTML_TAGS.each(function(tagName) { new Element(tagName) });
  880. }, 5);
  881. this.benchmark(function(){
  882. XHTML_TAGS.each(fu…

Large files files are truncated, but you can click here to view the full file