prototype/test/unit/dom.html
2008-01-18 04:33:34 +00:00

1710 lines
72 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prototype Unit test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="../../dist/prototype.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
<style type="text/css" media="screen">
/* <![CDATA[ */
#testcss1 { font-size:11px; color: #f00; }
#testcss2 { font-size:12px; color: #0f0; display: none; }
#style_test_1 { cursor: pointer; font-size:12px;}
div.style-test { margin-left: 1px }
#style_test_dimensions_container {
position: absolute;
top: 0;
left: 500px;
width: 20px;
height: 30px;
margin: 10px;
padding: 10px;
border: 3px solid red;
}
#not_floating_style { float: none }
#floating_style { float: left }
#op2 { opacity:0.5;filter:alpha(opacity=50)progid:DXImageTransform.Microsoft.Blur(strength=10);}
#scroll_test_1 {
margin: 10px;
padding: 10px;
position: relative;
}
#scroll_test_2 {
position: absolute;
left: 10px;
top: 10px;
}
#dimensions-visible,
#dimensions-display-none,
#dimensions-visible-pos-rel,
#dimensions-display-none-pos-rel,
#dimensions-visible-pos-abs,
#dimensions-display-none-pos-abs {
font-size: 10px;
height: 10em;
width: 20em;
}
#dimensions-visible-pos-abs,
#dimensions-display-none-pos-abs {
position: absolute;
top: 15px;
left: 15px;
}
#dimensions-visible-pos-rel,
#dimensions-display-none-pos-rel {
position: relative;
top: 15px;
left: 15px;
}
#dimensions-display-none, #imensions-display-none-pos-rel, #dimensions-display-none-pos-abs {
display: none;
}
#dimensions-table, #dimensions-tbody, #dimensions-tr, #dimensions-td {
font-size: 10px;
margin: 0;
padding: 0;
border: 0;
border-spacing: 0;
height: 10em;
width: 20em;
}
#notInlineAbsoluted { position: absolute; }
#elementToViewportDimensions {
position: absolute;
top: 0;
left: 0;
height: 10px;
width: 10px;
background: #000;
}
/* for scroll test on really big screens */
body {
height: 40000px;
}
/* ]]> */
</style>
</head>
<body>
<h1>Prototype Unit test file</h1>
<p>
Test of functions in dom.js
</p>
<!-- Log output -->
<div id="scroll_test_1">
<p id="scroll_test_2">Scroll test</p>
</div>
<div id="testlog"> </div>
<div id="test-visible">visible</div>
<div id="test-hidden" style="display:none;">hidden</div>
<div id="test-toggle-visible">visible</div>
<div id="test-toggle-hidden" style="display:none;">hidden</div>
<div id="test-hide-visible">visible</div>
<div id="test-hide-hidden" style="display:none;">hidden</div>
<div id="test-show-visible">visible</div>
<div id="test-show-hidden" style="display:none;">hidden</div>
<div id="removable-container"><div id="removable"></div></div>
<div>
<table>
<tbody id="table">
<tr>
<td>Data</td>
</tr>
<tr>
<td id="a_cell">First Row</td>
</tr>
<tr id="second_row">
<td>Second Row</td>
</tr>
</tbody>
</table>
</div>
<div id="table-container-to-replace">
<table>
<tbody id="table-to-replace">
<tr>
<td>Data</td>
</tr>
<tr>
<td id="a_cell-to-replace">First Row</td>
</tr>
<tr id="second_row-to-replace">
<td>Second Row</td>
</tr>
</tbody>
</table>
</div>
<p class="test">Test paragraph outside of container</p>
<div id="container">
<p class="test" id="intended">Test paragraph 1 inside of container</p>
<p class="test">Test paragraph 2 inside of container</p>
<p class="test">Test paragraph 3 inside of container</p>
<p class="test">Test paragraph 4 inside of container</p>
</div>
<div id="testdiv">to be updated</div>
<div id="testdiv-replace-container-1"><div id="testdiv-replace-1"></div></div>
<div id="testdiv-replace-container-2"><div id="testdiv-replace-2"></div></div>
<div id="testdiv-replace-container-3"><div id="testdiv-replace-3"></div></div>
<div id="testdiv-replace-container-4"><div id="testdiv-replace-4"></div></div>
<div id="testdiv-replace-container-5"><div id="testdiv-replace-5"></div></div>
<div id="testdiv-replace-container-element"><div id="testdiv-replace-element"></div></div>
<div id="testdiv-replace-container-toelement"><div id="testdiv-replace-toelement"></div></div>
<div id="testdiv-replace-container-tohtml"><div id="testdiv-replace-tohtml"></div></div>
<div id="testtable-replace-container"><table id="testtable-replace"></table></div>
<table id="testrow-replace-container"><tr id="testrow-replace"></tr></table>
<select id="testoption-replace-container"><option id="testoption-replace"></option><option>stays</option></select>
<div id="testform-replace-container"><p>some text</p><form id="testform-replace"><input id="testinput-replace" type="text" /></form><p>some text</p></div>
<div id="element_with_visible_overflow" style="overflow:visible">V</div>
<div id="element_with_hidden_overflow" style="overflow:hidden">H</div>
<div id="element_with_scroll_overflow" style="overflow:scroll">S</div>
<div id="element_extend_test"> </div>
<div id="element_reextend_test"><div id="discard_1"></div></div>
<div id="test_whitespace"> <span> </span>
<div><div></div> </div><span> </span>
</div>
<div id="nav_tests_isolator">
<div id="nav_test_first_sibling"></div>
<div></div>
<p id="nav_test_p" class="test"></p>
<span id="nav_test_prev_sibling"></span>
<ul id="navigation_test" style="display: none">
<!-- comment node to screw things up -->
<li class="first"><em>A</em></li>
<li><em class="dim">B</em></li>
<li id="navigation_test_c">
<em>C</em>
<ul>
<li><em class="dim">E</em></li>
<li id="navigation_test_f"><em>F</em></li>
</ul>
</li>
<li class="last"><em>D</em></li>
</ul>
<div id="navigation_test_next_sibling">
<!-- -->
</div>
<p></p>
</div>
<div id="class_names">
<p class="A"></p>
<ul class="A B" id="class_names_ul">
<li class="C"></li>
<li class="A C"></li>
<li class="1"></li>
</ul>
<div class="B C D"></div>
<div id="unextended"></div>
</div>
<div id="style_test_1" style="display:none;"></div>
<div id="style_test_2" class="style-test" style="font-size:11px;"></div>
<div id="style_test_3">blah</div>
<span id="style_test_4">blah</span>
<span id="style_test_5">blah</span>
<div id="style_test_dimensions_container">
<div id="style_test_dimensions" style="background:#ddd;padding:1px;margin:1px;border:1px solid #00f"><div style="height:5px;background:#eee;width:5px;padding:2px;margin:2px;border:2px solid #0f0"> </div>
</div>
</div>
<div id="test_csstext_1">test_csstext_1</div>
<div id="test_csstext_2">test_csstext_2</div>
<div id="test_csstext_3" style="border: 1px solid red">test_csstext_3</div>
<div id="test_csstext_4" style="font-size: 20px">test_csstext_4</div>
<div id="test_csstext_5">test_csstext_5</div>
<div id="custom_attributes">
<div foo="1" bar="2"></div>
<div foo="2"></div>
</div>
<div id="cloned_element_attributes_issue" foo="original"></div>
<a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link" onclick="alert('hello world');"></a>
<a id="attributes_with_issues_2" href="" accesskey="" tabindex="" title=""></a>
<a id="attributes_with_issues_3"></a>
<form id="attributes_with_issues_form" method="post" action="blah">
<input type="checkbox" id="attributes_with_issues_checked" name="a" checked="checked"/>
<input type="checkbox" id="attributes_with_issues_disabled" name="b" checked="checked" disabled="disabled"/>
<input type="text" id="attributes_with_issues_readonly" name="c" readonly="readonly" value="blech"/>
<input type="date" id="attributes_with_issues_type" value="blech" />
<select id="attributes_with_issues_multiple" name="e" multiple="multiple">
<option>blech</option>
<option>blah</option>
</select>
</form>
<!-- writeAttributes -->
<p id="write_attribute_para"></a>
<a id="write_attribute_link" href="test.html"></a>
<form action="/dev/null" id="write_attribute_form" method="get" accept-charset="utf-8">
<label id="write_attribute_label"></label>
<input type="checkbox" name="write_attribute_checkbox" value="" id="write_attribute_checkbox">
<input type="checkbox" checked="checked" name="write_attribute_checked_checkbox" value="" id="write_attribute_checked_checkbox">
<input type="text" name="write_attribute_input" value="" id="write_attribute_input">
<select id="write_attribute_select">
<option>Cat</option>
<option>Dog</option>
</select>
</form>
<table id="write_attribute_table">
<tr><td id="write_attribute_td">A</td><td>B</td></tr>
<tr><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
</table>
<div id="dom_attribute_precedence">
<form action="blech" method="post">
<input type="submit" id="update" />
</form>
</div>
<div id="not_floating_none">NFN</div>
<div id="not_floating_inline" style="float:none">NFI</div>
<div id="not_floating_style">NFS</div>
<div id="floating_inline" style="float:left">FI</div>
<div id="floating_style">FS</div>
<!-- Test Element opacity functions -->
<img id="op1" alt="op2" src="fixtures/logo.gif" style="opacity:0.5;filter:alpha(opacity=50)" />
<img id="op2" alt="op2" src="fixtures/logo.gif"/>
<img id="op3" alt="op3" src="fixtures/logo.gif"/>
<img id="op4-ie" alt="op3" src="fixtures/logo.gif" style="filter:alpha(opacity=30)" />
<div id="dimensions-visible"></div>
<div id="dimensions-display-none"></div>
<div id="dimensions-visible-pos-rel"></div>
<div id="dimensions-display-none-pos-rel"></div>
<div id="dimensions-visible-pos-abs"></div>
<div id="dimensions-display-none-pos-abs"></div>
<table border="0" cellspacing="0" cellpadding="0" id="dimensions-table">
<tbody id="dimensions-tbody">
<tr id="dimensions-tr">
<td id="dimensions-td">Data</td>
</tr>
</tbody>
</table>
<div id="dimensions-nester" style="width: 500px;">
<div id="dimensions-nestee" style="display: none">This is a nested DIV. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
<p id="test-empty"></p>
<p id="test-empty-but-contains-whitespace">
</p>
<p id="test-full">content</p>
<div id="ancestor">
<div id="child">
<div id="grand-child">
<div id="great-grand-child"></div>
</div></div><!-- intentional formatting; don't change this line -->
<div id="sibling"><div id="grand-sibling"></div></div>
</div>
<div id="not-in-the-family"></div>
<div id="insertions-container"><div id="insertions-main"><p>some content.</p></div></div>
<div id="insertions-node-container"><div id="insertions-node-main"><p>some content.</p></div></div>
<div id="element-insertions-container"><div id="element-insertions-main"><p>some content.</p></div></div>
<div id="element-insertions-multiple-container"><div id="element-insertions-multiple-main"><p>some content.</p></div></div>
<table id="table_for_insertions"></table>
<table id="table_for_row_insertions"><tr id="row_1"></tr></table>
<form method="post" action="blah">
<select id="select_for_update" name="select_for_update">
<option>option 1</option>
<option>option 2</option>
</select>
<select id="select_for_insert_bottom" name="select_for_insert_bottom">
<option>option 1</option>
<option>option 2</option>
</select>
<select id="select_for_insert_top" name="select_for_insert_top">
<option>option 1</option>
<option>option 2</option>
</select>
</form>
<div id="wrap-container"><p id="wrap"></p></div>
<!-- Positioning methods bench -->
<div id="body_absolute" style="position: absolute; top: 10px; left: 10px">
<div id="absolute_absolute" style="position: absolute; top: 10px; left:10px"> </div>
<div id="absolute_relative" style="position: relative; top: 10px; left:10px">
<div style="height:10px;font-size:2px">test<span id="inline">test</span></div>
<div id="absolute_relative_undefined">XYZ</div>
</div>
</div>
<div id="notInlineAbsoluted"></div>
<div id="inlineAbsoluted" style="position: absolute"></div>
<div id="unextended"></div>
<div id="identification">
<div id="predefined_id"></div>
<div></div>
<div></div>
<div></div>
<div id="anonymous_element_3"></div>
</div>
<div id='elementToViewportDimensions' style='display: none'></div>
<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
var testVar = 'to be updated', testVar2 = '';
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;
}
Element.addMethods({
hashBrowns: function(element) { return 'hash browns'; }
});
Element.addMethods("LI", {
pancakes: function(element) { return "pancakes"; }
});
Element.addMethods("DIV", {
waffles: function(element) { return "waffles"; }
});
Element.addMethods($w("li div"), {
orangeJuice: function(element) { return "orange juice"; }
});
// Based on properties check from http://www.quirksmode.org/viewport/compatibility.html
var 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);
});
return props;
}
};
new Test.Unit.Runner({
testDollarFunction: function() { with(this) {
assertUndefined($());
assertNull(document.getElementById('noWayThisIDExists'));
assertNull($('noWayThisIDExists'));
assertIdentical(document.getElementById('testdiv'), $('testdiv'));
assertEnumEqual([ $('testdiv'), $('container') ], $('testdiv', 'container'));
assertEnumEqual([ $('testdiv'), undefined, $('container') ],
$('testdiv', 'noWayThisIDExists', 'container'));
var elt = $('testdiv');
assertIdentical(elt, $(elt));
assertRespondsTo('hide', elt);
assertRespondsTo('childOf', elt);
}},
testGetElementsByClassName: function() {with(this) {
if (document.getElementsByClassName.toString().include('[native code]')) {
info("browser uses native getElementsByClassName; skipping tests");
return;
}
var div = $('class_names'), list = $('class_names_ul');
assertElementsMatch(document.getElementsByClassName('A'), 'p.A', 'ul#class_names_ul.A', 'li.A.C');
if (Prototype.Browser.IE)
assertUndefined(document.getElementById('unextended').show);
assertElementsMatch(div.getElementsByClassName('B'), 'ul#class_names_ul.A.B', 'div.B.C.D');
assertElementsMatch(div.getElementsByClassName('D C B'), 'div.B.C.D');
assertElementsMatch(div.getElementsByClassName(' D\nC\tB '), 'div.B.C.D');
assertElementsMatch(div.getElementsByClassName($w('D C B')));
assertElementsMatch(list.getElementsByClassName('A'), 'li.A.C');
assertElementsMatch(list.getElementsByClassName(' A '), 'li.A.C');
assertElementsMatch(list.getElementsByClassName('C A'), 'li.A.C');
assertElementsMatch(list.getElementsByClassName("C\nA "), 'li.A.C');
assertElementsMatch(list.getElementsByClassName('B'));
assertElementsMatch(list.getElementsByClassName('1'), 'li.1');
assertElementsMatch(list.getElementsByClassName([1]), 'li.1');
assertElementsMatch(list.getElementsByClassName(['1 junk']));
assertElementsMatch(list.getElementsByClassName(''));
assertElementsMatch(list.getElementsByClassName(' '));
assertElementsMatch(list.getElementsByClassName(['']));
assertElementsMatch(list.getElementsByClassName([' ', '']));
assertElementsMatch(list.getElementsByClassName({}));
// those lookups shouldn't have extended all nodes in document
if (Prototype.Browser.IE) assertUndefined(document.getElementById('unextended')['show']);
}},
testElementInsertWithHTML: function() {with(this) {
Element.insert('insertions-main', {before:'<p><em>before</em> text</p><p>more testing</p>'});
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>'});
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>'});
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>'});
assert(getInnerHTML('insertions-main').endsWith('<p><em>bottom</em> text.</p><p>more testing</p>'));
}},
testElementInsertWithDOMNode: function() {with(this) {
Element.insert('insertions-node-main', {before: createParagraph('node before')});
assert(getInnerHTML('insertions-node-container').startsWith('<p>node before</p>'));
Element.insert('insertions-node-main', {after: createParagraph('node after')});
assert(getInnerHTML('insertions-node-container').endsWith('<p>node after</p>'));
Element.insert('insertions-node-main', {top:createParagraph('node top')});
assert(getInnerHTML('insertions-node-main').startsWith('<p>node top</p>'));
Element.insert('insertions-node-main', {bottom:createParagraph('node bottom')});
assert(getInnerHTML('insertions-node-main').endsWith('<p>node bottom</p>'));
assertEqual($('insertions-node-main'), $('insertions-node-main').insert(document.createElement('p')));
}},
testElementInsertWithToElementMethod: function() {with(this) {
Element.insert('insertions-node-main', {toElement: createParagraph.curry('toElement') });
assert(getInnerHTML('insertions-node-main').endsWith('<p>toelement</p>'));
Element.insert('insertions-node-main', {bottom: {toElement: createParagraph.curry('bottom toElement') }});
assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom toelement</p>'));
}},
testElementInsertWithToHTMLMethod: function() {with(this) {
Element.insert('insertions-node-main', {toHTML: function() { return '<p>toHTML</p>'} });
assert(getInnerHTML('insertions-node-main').endsWith('<p>tohtml</p>'));
Element.insert('insertions-node-main', {bottom: {toHTML: function() { return '<p>bottom toHTML</p>'} }});
assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom tohtml</p>'));
}},
testElementInsertWithNonString: function() {with(this) {
Element.insert('insertions-main', {bottom:3});
assert(getInnerHTML('insertions-main').endsWith('3'));
}},
testElementInsertInTables: function() {with(this) {
Element.insert('second_row', {after:'<tr id="third_row"><td>Third Row</td></tr>'});
assert($('second_row').descendantOf('table'));
$('a_cell').insert({top:'hello world'});
assert($('a_cell').innerHTML.startsWith('hello world'));
$('a_cell').insert({after:'<td>hi planet</td>'});
assertEqual('hi planet', $('a_cell').next().innerHTML);
$('table_for_insertions').insert('<tr><td>a cell!</td></tr>');
assert($('table_for_insertions').innerHTML.gsub('\r\n', '').toLowerCase().include('<tr><td>a cell!</td></tr>'));
$('row_1').insert({after:'<tr></tr><tr></tr><tr><td>last</td></tr>'});
assertEqual('last', $A($('table_for_row_insertions').getElementsByTagName('tr')).last().lastChild.innerHTML);
}},
testElementInsertInSelect: function() {with(this) {
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>');
assertEqual('option 45', selectBottom.getValue());
selectTop.insert({top:'<option value="A">option A</option><option value="B" selected="selected">option B</option>'});
assertEqual(4, selectTop.options.length);
}},
testElementMethodInsert: function() {with(this) {
$('element-insertions-main').insert({before:'some text before'});
assert(getInnerHTML('element-insertions-container').startsWith('some text before'));
$('element-insertions-main').insert({after:'some text after'});
assert(getInnerHTML('element-insertions-container').endsWith('some text after'));
$('element-insertions-main').insert({top:'some text top'});
assert(getInnerHTML('element-insertions-main').startsWith('some text top'));
$('element-insertions-main').insert({bottom:'some text bottom'});
assert(getInnerHTML('element-insertions-main').endsWith('some text bottom'));
$('element-insertions-main').insert('some more text at the bottom');
assert(getInnerHTML('element-insertions-main').endsWith('some more text at the bottom'));
$('element-insertions-main').insert({TOP:'some text uppercase top'});
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'
});
assert(getInnerHTML('element-insertions-multiple-main').startsWith('1'));
assert(getInnerHTML('element-insertions-multiple-main').endsWith('2'));
assert(getInnerHTML('element-insertions-multiple-container').startsWith('<p>3</p>'));
assert(getInnerHTML('element-insertions-multiple-container').endsWith('4'));
$('element-insertions-main').update('test');
$('element-insertions-main').insert(null);
$('element-insertions-main').insert({bottom:null});
assertEqual('test', getInnerHTML('element-insertions-main'));
$('element-insertions-main').insert(1337);
assertEqual('test1337', getInnerHTML('element-insertions-main'));
}},
testNewElementInsert: function() {with(this) {
var container = new Element('div');
element = new Element('div');
container.insert(element);
element.insert({ before: '<p>a paragraph</p>' });
assertEqual('<p>a paragraph</p><div></div>', getInnerHTML(container));
element.insert({ after: 'some text' });
assertEqual('<p>a paragraph</p><div></div>some text', getInnerHTML(container));
element.insert({ top: '<p>a paragraph</p>' });
assertEqual('<p>a paragraph</p>', getInnerHTML(element));
element.insert('some text');
assertEqual('<p>a paragraph</p>some text', getInnerHTML(element));
}},
testInsertionBackwardsCompatibility: function() {with(this) {
new Insertion.Before('element-insertions-main', 'some backward-compatibility testing before');
assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing before'));
new Insertion.After('element-insertions-main', 'some backward-compatibility testing after');
assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing after'));
new Insertion.Top('element-insertions-main', 'some backward-compatibility testing top');
assert(getInnerHTML('element-insertions-main').startsWith('some backward-compatibility testing top'));
new Insertion.Bottom('element-insertions-main', 'some backward-compatibility testing bottom');
assert(getInnerHTML('element-insertions-main').endsWith('some backward-compatibility testing bottom'));
}},
testElementWrap: function() {with(this) {
var element = $('wrap'), parent = document.createElement('div');
element.wrap();
assert(getInnerHTML('wrap-container').startsWith('<div><p'));
element.wrap('div');
assert(getInnerHTML('wrap-container').startsWith('<div><div><p'));
element.wrap(parent);
assert(Object.isFunction(parent.setStyle));
assert(getInnerHTML('wrap-container').startsWith('<div><div><div><p'));
element.wrap('div', {className: 'wrapper'});
assert(element.up().hasClassName('wrapper'));
element.wrap({className: 'other-wrapper'});
assert(element.up().hasClassName('other-wrapper'));
element.wrap(new Element('div'), {className: 'yet-other-wrapper'});
assert(element.up().hasClassName('yet-other-wrapper'));
var orphan = new Element('p'), div = new Element('div');
orphan.wrap(div);
assertEqual(orphan.parentNode, div);
}},
testElementWrapReturnsWrapper: function() {with(this) {
var element = new Element("div");
var wrapper = element.wrap("div");
assertNotEqual(element, wrapper);
assertEqual(element.up(), wrapper);
}},
testElementVisible: function(){with(this) {
assertNotEqual('none', $('test-visible').style.display);
assertEqual('none', $('test-hidden').style.display);
}},
testElementToggle: function(){with(this) {
$('test-toggle-visible').toggle();
assert(!$('test-toggle-visible').visible());
$('test-toggle-visible').toggle();
assert($('test-toggle-visible').visible());
$('test-toggle-hidden').toggle();
assert($('test-toggle-hidden').visible());
$('test-toggle-hidden').toggle();
assert(!$('test-toggle-hidden').visible());
}},
testElementShow: function(){with(this) {
$('test-show-visible').show();
assert($('test-show-visible').visible());
$('test-show-hidden').show();
assert($('test-show-hidden').visible());
}},
testElementHide: function(){with(this) {
$('test-hide-visible').hide();
assert(!$('test-hide-visible').visible());
$('test-hide-hidden').hide();
assert(!$('test-hide-hidden').visible());
}},
testElementRemove: function(){with(this) {
$('removable').remove();
assert($('removable-container').empty());
}},
testElementUpdate: function() {with(this) {
$('testdiv').update('hello from div!');
assertEqual('hello from div!', $('testdiv').innerHTML);
Element.update('testdiv', 'another hello from div!');
assertEqual('another hello from div!', $('testdiv').innerHTML);
Element.update('testdiv', 123);
assertEqual('123', $('testdiv').innerHTML);
Element.update('testdiv');
assertEqual('', $('testdiv').innerHTML);
Element.update('testdiv', '&nbsp;');
assert(!$('testdiv').innerHTML.empty());
}},
testElementUpdateWithScript: function() {with(this) {
$('testdiv').update('hello from div!<script>\ntestVar="hello!";\n</'+'script>');
assertEqual('hello from div!',$('testdiv').innerHTML);
wait(100,function(){
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
assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv').innerHTML);
wait(100,function(){
assertEqual('another hello!',testVar);
Element.update('testdiv','a\n<script>testVar="a"\ntestVar="b"</'+'script>');
wait(100,function(){
assertEqual('b', testVar);
Element.update('testdiv',
'x<script>testVar2="a"</'+'script>\nblah\n'+
'x<script>testVar2="b"</'+'script>');
wait(100,function(){
assertEqual('b', testVar2);
});
});
});
});
}},
testElementUpdateInTableRow: function() {with(this) {
$('second_row').update('<td id="i_am_a_td">test</td>');
assertEqual('test',$('i_am_a_td').innerHTML);
Element.update('second_row','<td id="i_am_a_td">another <span>test</span></td>');
assertEqual('another <span>test</span>',$('i_am_a_td').innerHTML.toLowerCase());
}},
testElementUpdateInTableCell: function() {with(this) {
Element.update('a_cell','another <span>test</span>');
assertEqual('another <span>test</span>',$('a_cell').innerHTML.toLowerCase());
}},
testElementUpdateInTable: function() {with(this) {
Element.update('table','<tr><td>boo!</td></tr>');
assertMatch(/^<tr>\s*<td>boo!<\/td><\/tr>$/,$('table').innerHTML.toLowerCase());
}},
testElementUpdateInSelect: function() {with(this) {
var select = $('select_for_update');
select.update('<option value="3">option 3</option><option selected="selected">option 4</option>');
assertEqual('option 4', select.getValue());
}},
testElementUpdateWithDOMNode: function() {with(this) {
$('testdiv').update(new Element('div').insert('bla'));
assertEqual('<div>bla</div>', getInnerHTML('testdiv'));
}},
testElementUpdateWithToElementMethod: function() {with(this) {
$('testdiv').update({toElement: createParagraph.curry('foo')});
assertEqual('<p>foo</p>', getInnerHTML('testdiv'));
}},
testElementUpdateWithToHTMLMethod: function() {with(this) {
$('testdiv').update({toHTML: function() { return 'hello world' }});
assertEqual('hello world', getInnerHTML('testdiv'));
}},
testElementReplace: function() {with(this) {
$('testdiv-replace-1').replace('hello from div!');
assertEqual('hello from div!', $('testdiv-replace-container-1').innerHTML);
$('testdiv-replace-2').replace(123);
assertEqual('123', $('testdiv-replace-container-2').innerHTML);
$('testdiv-replace-3').replace();
assertEqual('', $('testdiv-replace-container-3').innerHTML);
$('testrow-replace').replace('<tr><td>hello</td></tr>');
assert(getInnerHTML('testrow-replace-container').include('<tr><td>hello</td></tr>'));
$('testoption-replace').replace('<option>hello</option>');
assert($('testoption-replace-container').innerHTML.include('hello'));
$('testinput-replace').replace('<p>hello world</p>');
assertEqual('<p>hello world</p>', getInnerHTML('testform-replace'));
$('testform-replace').replace('<form></form>');
assertEqual('<p>some text</p><form></form><p>some text</p>', getInnerHTML('testform-replace-container'));
}},
testElementReplaceWithScript: function() {with(this) {
$('testdiv-replace-4').replace('hello from div!<script>testVarReplace="hello!"</'+'script>');
assertEqual('hello from div!', $('testdiv-replace-container-4').innerHTML);
wait(100,function(){
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
assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv-replace-container-5').innerHTML);
wait(100,function(){
assertEqual('another hello!',testVarReplace);
});
});
}},
testElementReplaceWithDOMNode: function() {with(this) {
$('testdiv-replace-element').replace(createParagraph('hello'));
assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-element'));
}},
testElementReplaceWithToElementMethod: function() {with(this) {
$('testdiv-replace-toelement').replace({toElement: createParagraph.curry('hello')});
assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-toelement'));
}},
testElementReplaceWithToHTMLMethod: function() {with(this) {
$('testdiv-replace-tohtml').replace({toHTML: function() { return 'hello' }});
assertEqual('hello', getInnerHTML('testdiv-replace-container-tohtml'));
}},
testElementSelectorMethod: function() {with(this) {
['getElementsBySelector','select'].each(function(method){
var testSelector = $('container')[method]('p.test');
assertEqual(testSelector.length, 4);
assertEqual(testSelector[0], $('intended'));
assertEqual(testSelector[0], $$('#container p.test')[0]);
});
}},
testElementAdjacent: function() {with(this) {
var elements = $('intended').adjacent('p');
assertEqual(elements.length, 3);
elements.each(function(element){
assert(element != $('intended'));
});
}},
testElementIdentify: function() {with(this) {
var parent = $('identification');
assertEqual(parent.down().identify(), 'predefined_id');
assertEqual(parent.down(1).identify(), 'anonymous_element_1');
assertEqual(parent.down(2).identify(), 'anonymous_element_2');
assertEqual(parent.down(3).identify(), 'anonymous_element_4');
}},
testElementClassNameMethod: function() {with(this) {
var testClassNames = $('container').getElementsByClassName('test');
var testSelector = $('container').getElementsBySelector('p.test');
assertEqual(testClassNames[0], $('intended'));
assertEqual(testClassNames.length, 4);
assertEqual(testSelector[3], testClassNames[3]);
assertEqual(testClassNames.length, testSelector.length);
}},
testElementAncestors: function() {with(this) {
var ancestors = $('navigation_test_f').ancestors();
assertElementsMatch(ancestors, 'ul', 'li', 'ul#navigation_test',
'div#nav_tests_isolator', 'body', 'html');
assertElementsMatch(ancestors.last().ancestors());
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof $(dummy.childNodes[0]).ancestors()[0]['setStyle'] == 'function');
}},
testElementDescendants: function() {with(this) {
assertElementsMatch($('navigation_test').descendants(),
'li', 'em', 'li', 'em.dim', 'li', 'em', 'ul', 'li',
'em.dim', 'li#navigation_test_f', 'em', 'li', 'em');
assertElementsMatch($('navigation_test_f').descendants(), 'em');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof dummy.descendants()[0].setStyle == 'function');
}},
testElementFirstDescendant: function() {with(this) {
assertElementMatches($('navigation_test').firstDescendant(), 'li.first');
assertNull($('navigation_test_next_sibling').firstDescendant());
}},
testElementChildElements: function() {with(this) {
assertElementsMatch($('navigation_test').childElements(),
'li.first', 'li', 'li#navigation_test_c', 'li.last');
assertNotEqual(0, $('navigation_test_next_sibling').childNodes.length);
assertEnumEqual([], $('navigation_test_next_sibling').childElements());
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof dummy.childElements()[0].setStyle == 'function');
}},
testElementImmediateDescendants: function() {with(this) {
assertIdentical(Element.Methods.childElements, Element.Methods.immediateDescendants);
}},
testElementPreviousSiblings: function() {with(this) {
assertElementsMatch($('navigation_test').previousSiblings(),
'span#nav_test_prev_sibling', 'p.test', 'div', 'div#nav_test_first_sibling');
assertElementsMatch($('navigation_test_f').previousSiblings(), 'li');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof $(dummy.childNodes[1]).previousSiblings()[0].setStyle == 'function');
}},
testElementNextSiblings: function() {with(this) {
assertElementsMatch($('navigation_test').nextSiblings(),
'div#navigation_test_next_sibling', 'p');
assertElementsMatch($('navigation_test_f').nextSiblings());
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof $(dummy.childNodes[0]).nextSiblings()[0].setStyle == 'function');
}},
testElementSiblings: function() {with(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);
assert(typeof $(dummy.childNodes[0]).siblings()[0].setStyle == 'function');
}},
testElementUp: function() {with(this) {
var element = $('navigation_test_f');
assertElementMatches(element.up(), 'ul');
assertElementMatches(element.up(0), 'ul');
assertElementMatches(element.up(1), 'li');
assertElementMatches(element.up(2), 'ul#navigation_test');
assertElementsMatch(element.up('li').siblings(), 'li.first', 'li', 'li.last');
assertElementMatches(element.up('ul', 1), 'ul#navigation_test');
assertEqual(undefined, element.up('garbage'));
assertEqual(undefined, element.up(6));
assertElementMatches(element.up('.non-existant, ul'), 'ul');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof $(dummy.childNodes[0]).up().setStyle == 'function');
}},
testElementDown: function() {with(this) {
var element = $('navigation_test');
assertElementMatches(element.down(), 'li.first');
assertElementMatches(element.down(0), 'li.first');
assertElementMatches(element.down(1), 'em');
assertElementMatches(element.down('li', 5), 'li.last');
assertElementMatches(element.down('ul').down('li', 1), 'li#navigation_test_f');
assertElementMatches(element.down('.non-existant, .first'), 'li.first');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof dummy.down().setStyle == 'function');
}},
testElementPrevious: function() {with(this) {
var element = $('navigation_test').down('li.last');
assertElementMatches(element.previous(), 'li#navigation_test_c');
assertElementMatches(element.previous(1), 'li');
assertElementMatches(element.previous('.first'), 'li.first');
assertEqual(undefined, element.previous(3));
assertEqual(undefined, $('navigation_test').down().previous());
assertElementMatches(element.previous('.non-existant, .first'), 'li.first');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof $(dummy.childNodes[1]).previous().setStyle == 'function');
}},
testElementNext: function() {with(this) {
var element = $('navigation_test').down('li.first');
assertElementMatches(element.next(), 'li');
assertElementMatches(element.next(1), 'li#navigation_test_c');
assertElementMatches(element.next(2), 'li.last');
assertElementMatches(element.next('.last'), 'li.last');
assertEqual(undefined, element.next(3));
assertEqual(undefined, element.next(2).next());
assertElementMatches(element.next('.non-existant, .last'), 'li.last');
var dummy = $(document.createElement('DIV'));
dummy.innerHTML = '<div></div>'.times(3);
assert(typeof $(dummy.childNodes[0]).next().setStyle == 'function');
}},
testElementInspect: function() {with(this) {
assertEqual('<ul id="navigation_test">', $('navigation_test').inspect());
assertEqual('<li class="first">', $('navigation_test').down().inspect());
assertEqual('<em>', $('navigation_test').down(1).inspect());
}},
testElementMakeClipping: function() {with(this) {
var chained = Element.extend(document.createElement('DIV'));
assertEqual(chained, chained.makeClipping());
assertEqual(chained, chained.makeClipping());
assertEqual(chained, chained.makeClipping().makeClipping());
assertEqual(chained, chained.undoClipping());
assertEqual(chained, chained.undoClipping());
assertEqual(chained, chained.undoClipping().makeClipping());
['hidden','visible','scroll'].each( function(overflowValue) {
var element = $('element_with_'+overflowValue+'_overflow');
assertEqual(overflowValue, element.getStyle('overflow'));
element.makeClipping();
assertEqual('hidden', element.getStyle('overflow'));
element.undoClipping();
assertEqual(overflowValue, element.getStyle('overflow'));
});
}},
testElementExtend: function() {with(this) {
var element = $('element_extend_test');
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);
assertEqual(element, Element.extend(element));
assertRespondsTo('show', element);
});
[null,'','a','aa'].each(function(content) {
var textnode = document.createTextNode(content);
assertEqual(textnode, Element.extend(textnode));
assert(typeof textnode['show'] == 'undefined');
});
}},
testElementExtendReextendsDiscardedNodes: function() {with(this) {
assertRespondsTo('show', $('discard_1'));
$('element_reextend_test').innerHTML += '<div id="discard_2"></div>';
assertRespondsTo('show', $('discard_1'));
}},
testElementCleanWhitespace: function() {with(this) {
Element.cleanWhitespace("test_whitespace");
assertEqual(3, $("test_whitespace").childNodes.length);
assertEqual(1, $("test_whitespace").firstChild.nodeType);
assertEqual('SPAN', $("test_whitespace").firstChild.tagName);
assertEqual(1, $("test_whitespace").firstChild.nextSibling.nodeType);
assertEqual('DIV', $("test_whitespace").firstChild.nextSibling.tagName);
assertEqual(1, $("test_whitespace").firstChild.nextSibling.nextSibling.nodeType);
assertEqual('SPAN', $("test_whitespace").firstChild.nextSibling.nextSibling.tagName);
var element = document.createElement('DIV');
element.appendChild(document.createTextNode(''));
element.appendChild(document.createTextNode(''));
assertEqual(2, element.childNodes.length);
Element.cleanWhitespace(element);
assertEqual(0, element.childNodes.length);
}},
testElementEmpty: function() {with(this) {
assert($('test-empty').empty());
assert($('test-empty-but-contains-whitespace').empty());
assert(!$('test-full').empty());
}},
testDescendantOf: function() {with(this) {
assert($('child').descendantOf('ancestor'));
assert($('child').descendantOf($('ancestor')));
assert(!$('ancestor').descendantOf($('child')));
assert($('great-grand-child').descendantOf('ancestor'), 'great-grand-child < ancestor');
assert($('grand-child').descendantOf('ancestor'), 'grand-child < ancestor');
assert($('great-grand-child').descendantOf('grand-child'), 'great-grand-child < grand-child');
assert($('grand-child').descendantOf('child'), 'grand-child < child');
assert($('great-grand-child').descendantOf('child'), 'great-grand-child < child');
window.debug = true;
assert($('sibling').descendantOf('ancestor'), 'sibling < ancestor');
assert($('grand-sibling').descendantOf('sibling'), 'grand-sibling < sibling');
assert($('grand-sibling').descendantOf('ancestor'), 'grand-sibling < ancestor');
assert($('grand-sibling').descendantOf(document.body), 'grand-sibling < body');
window.debug = false;
assert(!$('great-grand-child').descendantOf('great-grand-child'), 'great-grand-child < great-grand-child');
assert(!$('great-grand-child').descendantOf('sibling'), 'great-grand-child < sibling');
assert(!$('sibling').descendantOf('child'), 'sibling < child');
assert(!$('great-grand-child').descendantOf('not-in-the-family'), 'great-grand-child < not-in-the-family');
assert(!$('child').descendantOf('not-in-the-family'), 'child < not-in-the-family');
assert(!$(document.body).descendantOf('great-grand-child'));
}},
testChildOf: function() {with(this) {
assert($('child').childOf('ancestor'));
assert($('child').childOf($('ancestor')));
assert($('great-grand-child').childOf('ancestor'));
assert(!$('great-grand-child').childOf('not-in-the-family'));
assertIdentical(Element.Methods.childOf, Element.Methods.descendantOf);
}},
testElementSetStyle: function() { with(this) {
Element.setStyle('style_test_3',{ 'left': '2px' });
assertEqual('2px', $('style_test_3').style.left);
Element.setStyle('style_test_3',{ marginTop: '1px' });
assertEqual('1px', $('style_test_3').style.marginTop);
$('style_test_3').setStyle({ marginTop: '2px', left: '-1px' });
assertEqual('-1px', $('style_test_3').style.left);
assertEqual('2px', $('style_test_3').style.marginTop);
assertEqual('none', $('style_test_3').getStyle('float'));
$('style_test_3').setStyle({ 'float': 'left' });
assertEqual('left', $('style_test_3').getStyle('float'));
$('style_test_3').setStyle({ cssFloat: 'none' });
assertEqual('none', $('style_test_3').getStyle('float'));
assertEqual(1, $('style_test_3').getStyle('opacity'));
$('style_test_3').setStyle({ opacity: 0.5 });
assertEqual(0.5, $('style_test_3').getStyle('opacity'));
$('style_test_3').setStyle({ opacity: '' });
assertEqual(1, $('style_test_3').getStyle('opacity'));
$('style_test_3').setStyle({ opacity: 0 });
assertEqual(0, $('style_test_3').getStyle('opacity'));
$('test_csstext_1').setStyle('font-size: 15px');
assertEqual('15px', $('test_csstext_1').getStyle('font-size'));
$('test_csstext_2').setStyle({height: '40px'});
$('test_csstext_2').setStyle('font-size: 15px');
assertEqual('15px', $('test_csstext_2').getStyle('font-size'));
assertEqual('40px', $('test_csstext_2').getStyle('height'));
$('test_csstext_3').setStyle('font-size: 15px');
assertEqual('15px', $('test_csstext_3').getStyle('font-size'));
assertEqual('1px', $('test_csstext_3').getStyle('border-top-width'));
$('test_csstext_4').setStyle('font-size: 15px');
assertEqual('15px', $('test_csstext_4').getStyle('font-size'));
$('test_csstext_4').setStyle('float: right; font-size: 10px');
assertEqual('right', $('test_csstext_4').getStyle('float'));
assertEqual('10px', $('test_csstext_4').getStyle('font-size'));
$('test_csstext_5').setStyle('float: left; opacity: .5; font-size: 10px');
assertEqual(parseFloat('0.5'), parseFloat($('test_csstext_5').getStyle('opacity')));
}},
testElementSetStyleCamelized: function() { with(this) {
assertNotEqual('30px', $('style_test_3').style.marginTop);
$('style_test_3').setStyle({ marginTop: '30px'}, true);
assertEqual('30px', $('style_test_3').style.marginTop);
}},
testElementSetOpacity: function() { with(this) {
[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
assert((Number(realOpacity) - opacity) <= 0.002, 'setting opacity to ' + opacity);
});
assertEqual(0,
$('style_test_3').setOpacity(0.0000001).getStyle('opacity'));
// for Firefox, we don't set to 1, because of flickering
assert(
$('style_test_3').setOpacity(0.9999999).getStyle('opacity') > 0.999
);
if (Prototype.Browser.IE) {
assert($('style_test_4').setOpacity(0.5).currentStyle.hasLayout);
assert(2, $('style_test_5').setOpacity(0.5).getStyle('zoom'));
assert(0.5, new Element('div').setOpacity(0.5).getOpacity());
assert(2, new Element('div').setOpacity(0.5).setStyle('zoom: 2;').getStyle('zoom'));
assert(2, new Element('div').setStyle('zoom: 2;').setOpacity(0.5).getStyle('zoom'));
}
}},
testElementGetStyle: function() { with(this) {
assertEqual("none",
$('style_test_1').getStyle('display'));
// not displayed, so "null" ("auto" is tranlated to "null")
assertNull(Element.getStyle('style_test_1', 'width'), 'elements that are hidden should return null on getStyle("width")');
$('style_test_1').show();
// from id rule
assertEqual("pointer",
Element.getStyle('style_test_1','cursor'));
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
assertEqual($('style_test_2').offsetWidth+'px', Element.getStyle('style_test_2','width'));
assertEqual("static",Element.getStyle('style_test_1','position'));
// from style
assertEqual("11px",
Element.getStyle('style_test_2','font-size'));
// from class
assertEqual("1px",
Element.getStyle('style_test_2','margin-left'));
['not_floating_none','not_floating_style','not_floating_inline'].each(function(element){
assertEqual('none', $(element).getStyle('float'));
assertEqual('none', $(element).getStyle('cssFloat'));
});
['floating_style','floating_inline'].each(function(element){
assertEqual('left', $(element).getStyle('float'));
assertEqual('left', $(element).getStyle('cssFloat'));
});
assertEqual(0.5, $('op1').getStyle('opacity'));
assertEqual(0.5, $('op2').getStyle('opacity'));
assertEqual(1.0, $('op3').getStyle('opacity'));
$('op1').setStyle({opacity: '0.3'});
$('op2').setStyle({opacity: '0.3'});
$('op3').setStyle({opacity: '0.3'});
assertEqual(0.3, $('op1').getStyle('opacity'));
assertEqual(0.3, $('op2').getStyle('opacity'));
assertEqual(0.3, $('op3').getStyle('opacity'));
$('op3').setStyle({opacity: 0});
assertEqual(0, $('op3').getStyle('opacity'));
if(navigator.appVersion.match(/MSIE/)) {
assertEqual('alpha(opacity=30)', $('op1').getStyle('filter'));
assertEqual('progid:DXImageTransform.Microsoft.Blur(strength=10)alpha(opacity=30)', $('op2').getStyle('filter'));
$('op2').setStyle({opacity:''});
assertEqual('progid:DXImageTransform.Microsoft.Blur(strength=10)', $('op2').getStyle('filter'));
assertEqual('alpha(opacity=0)', $('op3').getStyle('filter'));
assertEqual(0.3, $('op4-ie').getStyle('opacity'));
}
// verify that value is still found when using camelized
// strings (function previously used getPropertyValue()
// which expected non-camelized strings)
assertEqual("12px", $('style_test_1').getStyle('fontSize'));
// getStyle on width/height should return values according to
// the CSS box-model, which doesn't include
// margin, padding, or borders
// TODO: This test fails on IE because there seems to be no way
// to calculate this properly (clientWidth/Height returns 0)
if(!navigator.appVersion.match(/MSIE/)) {
assertEqual("14px", $('style_test_dimensions').getStyle('width'));
assertEqual("17px", $('style_test_dimensions').getStyle('height'));
}
}},
testElementGetOpacity: function() {with(this) {
assertEqual(0.45, $('op1').setOpacity(0.45).getOpacity());
}},
testElementReadAttribute: function() {with(this) {
assertEqual('test.html' , $('attributes_with_issues_1').readAttribute('href'));
assertEqual('L' , $('attributes_with_issues_1').readAttribute('accesskey'));
assertEqual('50' , $('attributes_with_issues_1').readAttribute('tabindex'));
assertEqual('a link' , $('attributes_with_issues_1').readAttribute('title'));
$('cloned_element_attributes_issue').readAttribute('foo')
var clone = $('cloned_element_attributes_issue').cloneNode(true);
clone.writeAttribute('foo', 'cloned');
assertEqual('cloned', clone.readAttribute('foo'));
assertEqual('original', $('cloned_element_attributes_issue').readAttribute('foo'));
['href', 'accesskey', 'accesskey', 'title'].each(function(attr){
assertEqual('' , $('attributes_with_issues_2').readAttribute(attr));
});
['checked','disabled','readonly','multiple'].each(function(attr){
assertEqual(attr, $('attributes_with_issues_'+attr).readAttribute(attr));
});
assertEqual("alert('hello world');", $('attributes_with_issues_1').readAttribute('onclick'));
assertNull($('attributes_with_issues_1').readAttribute('onmouseover'));
assertEqual('date', $('attributes_with_issues_type').readAttribute('type'));
assertEqual('text', $('attributes_with_issues_readonly').readAttribute('type'));
var elements = $('custom_attributes').immediateDescendants();
assertEnumEqual(['1', '2'], elements.invoke('readAttribute', 'foo'));
assertEnumEqual(['2', null], elements.invoke('readAttribute', 'bar'));
}},
testElementWriteAttribute: function() {with(this) {
var element = Element.extend(document.body.appendChild(document.createElement('p')));
assertRespondsTo('writeAttribute', element);
assertEqual(element, element.writeAttribute('id', 'write_attribute_test'));
assertEqual('write_attribute_test', element.id);
assertEqual('http://prototypejs.org/', $('write_attribute_link').
writeAttribute({href: 'http://prototypejs.org/', title: 'Home of Prototype'}).href);
assertEqual('Home of Prototype', $('write_attribute_link').title);
var element2 = Element.extend(document.createElement('p'));
element2.writeAttribute('id', 'write_attribute_without_hash');
assertEqual('write_attribute_without_hash', element2.id);
element2.writeAttribute('animal', 'cat');
assertEqual('cat', element2.readAttribute('animal'));
}},
testElementWriteAttributeWithBooleans: function() {with(this) {
var input = $('write_attribute_input'),
select = $('write_attribute_select'),
checkbox = $('write_attribute_checkbox'),
checkedCheckbox = $('write_attribute_checked_checkbox');
assert( input. writeAttribute('readonly'). hasAttribute('readonly'));
assert(!input. writeAttribute('readonly', false). hasAttribute('readonly'));
assert( input. writeAttribute('readonly', true). hasAttribute('readonly'));
assert(!input. writeAttribute('readonly', null). hasAttribute('readonly'));
assert( input. writeAttribute('readonly', 'readonly').hasAttribute('readonly'));
assert( select. writeAttribute('multiple'). hasAttribute('multiple'));
assert( input. writeAttribute('disabled'). hasAttribute('disabled'));
assert( checkbox. writeAttribute('checked'). checked);
assert(!checkedCheckbox.writeAttribute('checked', false). checked);
}},
testElementWriteAttributeWithIssues: function() {with(this) {
var input = $('write_attribute_input').writeAttribute({maxlength: 90, tabindex: 10}),
td = $('write_attribute_td').writeAttribute({valign: 'bottom', colspan: 2, rowspan: 2});
assertEqual(90, input.readAttribute('maxlength'));
assertEqual(10, input.readAttribute('tabindex'));
assertEqual(2, td.readAttribute('colspan'));
assertEqual(2, td.readAttribute('rowspan'));
assertEqual('bottom', td.readAttribute('valign'));
var p = $('write_attribute_para'), label = $('write_attribute_label');
assertEqual('some-class', p. writeAttribute({'class': 'some-class'}). readAttribute('class'));
assertEqual('some-className', p. writeAttribute({className: 'some-className'}).readAttribute('class'));
assertEqual('some-id', label.writeAttribute({'for': 'some-id'}). readAttribute('for'));
assertEqual('some-other-id', label.writeAttribute({htmlFor: 'some-other-id'}). readAttribute('for'));
assert(p.writeAttribute({style: 'width: 5px;'}).readAttribute('style').toLowerCase().include('width'));
}},
testElementWriteAttributeWithCustom: function() {with(this) {
var p = $('write_attribute_para').writeAttribute({name: 'martin', location: 'stockholm', age: 26});
assertEqual('martin', p.readAttribute('name'));
assertEqual('stockholm', p.readAttribute('location'));
assertEqual('26', p.readAttribute('age'));
}},
testNewElement: function() {with(this) {
assert(new Element('h1'));
var XHTML_TAGS = $w(
'a abbr acronym address 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, index) {
var id = tag + '_' + index, element = document.body.appendChild(new Element(tag, {id: id}));
assertEqual(tag, element.tagName.toLowerCase());
assertEqual(element, document.body.lastChild);
assertEqual(id, element.id);
});
assertRespondsTo('update', new Element('div'));
Element.addMethods({
cheeseCake: function(){
return 'Cheese cake';
}
});
assertRespondsTo('cheeseCake', new Element('div'));
/* window.ElementOld = function(tagName, attributes) {
if (Prototype.Browser.IE && attributes && attributes.name) {
tagName = '<' + tagName + ' name="' + attributes.name + '">';
delete attributes.name;
}
return Element.extend(document.createElement(tagName)).writeAttribute(attributes || {});
};
benchmark(function(){
XHTML_TAGS.each(function(tagName){new Element(tagName)});
}, 5);
benchmark(function(){
XHTML_TAGS.each(function(tagName){new ElementOld(tagName)});
}, 5); */
assertEqual('foobar', new Element('a', {custom: 'foobar'}).readAttribute('custom'));
var input = document.body.appendChild(new Element('input',
{id: 'my_input_field_id', name: 'my_input_field'}));
assertEqual(input, document.body.lastChild);
assertEqual('my_input_field', $(document.body.lastChild).name);
if (Prototype.Browser.IE)
assertMatch(/name=["']?my_input_field["']?/, $('my_input_field').outerHTML);
}},
testElementGetHeight: function() {with(this) {
assertIdentical(100, $('dimensions-visible').getHeight());
assertIdentical(100, $('dimensions-display-none').getHeight());
}},
testElementGetWidth: function() {with(this) {
assertIdentical(200, $('dimensions-visible').getWidth());
assertIdentical(200, $('dimensions-display-none').getWidth());
}},
testElementGetDimensions: function() {with(this) {
assertIdentical(100, $('dimensions-visible').getDimensions().height);
assertIdentical(200, $('dimensions-visible').getDimensions().width);
assertIdentical(100, $('dimensions-display-none').getDimensions().height);
assertIdentical(200, $('dimensions-display-none').getDimensions().width);
assertIdentical(100, $('dimensions-visible-pos-rel').getDimensions().height);
assertIdentical(200, $('dimensions-visible-pos-rel').getDimensions().width);
assertIdentical(100, $('dimensions-display-none-pos-rel').getDimensions().height);
assertIdentical(200, $('dimensions-display-none-pos-rel').getDimensions().width);
assertIdentical(100, $('dimensions-visible-pos-abs').getDimensions().height);
assertIdentical(200, $('dimensions-visible-pos-abs').getDimensions().width);
assertIdentical(100, $('dimensions-display-none-pos-abs').getDimensions().height);
assertIdentical(200, $('dimensions-display-none-pos-abs').getDimensions().width);
// known failing issue
// assert($('dimensions-nestee').getDimensions().width <= 500, 'check for proper dimensions of hidden child elements');
$('dimensions-td').hide();
assertIdentical(100, $('dimensions-td').getDimensions().height);
assertIdentical(200, $('dimensions-td').getDimensions().width);
$('dimensions-td').show();
$('dimensions-tr').hide();
assertIdentical(100, $('dimensions-tr').getDimensions().height);
assertIdentical(200, $('dimensions-tr').getDimensions().width);
$('dimensions-tr').show();
$('dimensions-table').hide();
assertIdentical(100, $('dimensions-table').getDimensions().height);
assertIdentical(200, $('dimensions-table').getDimensions().width);
}},
testDOMAttributesHavePrecedenceOverExtendedElementMethods: function() {with(this) {
assertNothingRaised(function() { $('dom_attribute_precedence').down('form') });
assertEqual($('dom_attribute_precedence').down('input'), $('dom_attribute_precedence').down('form').update);
}},
testClassNames: function() {with(this) {
assertEnumEqual([], $('class_names').classNames());
assertEnumEqual(['A'], $('class_names').down().classNames());
assertEnumEqual(['A', 'B'], $('class_names_ul').classNames());
}},
testHasClassName: function() {with(this) {
assertIdentical(false, $('class_names').hasClassName('does_not_exist'));
assertIdentical(true, $('class_names').down().hasClassName('A'));
assertIdentical(false, $('class_names').down().hasClassName('does_not_exist'));
assertIdentical(true, $('class_names_ul').hasClassName('A'));
assertIdentical(true, $('class_names_ul').hasClassName('B'));
assertIdentical(false, $('class_names_ul').hasClassName('does_not_exist'));
}},
testAddClassName: function() {with(this) {
$('class_names').addClassName('added_className');
assertEnumEqual(['added_className'], $('class_names').classNames());
$('class_names').addClassName('added_className'); // verify that className cannot be added twice.
assertEnumEqual(['added_className'], $('class_names').classNames());
$('class_names').addClassName('another_added_className');
assertEnumEqual(['added_className', 'another_added_className'], $('class_names').classNames());
}},
testRemoveClassName: function() {with(this) {
$('class_names').removeClassName('added_className');
assertEnumEqual(['another_added_className'], $('class_names').classNames());
$('class_names').removeClassName('added_className'); // verify that removing a non existent className is safe.
assertEnumEqual(['another_added_className'], $('class_names').classNames());
$('class_names').removeClassName('another_added_className');
assertEnumEqual([], $('class_names').classNames());
}},
testToggleClassName: function() {with(this) {
$('class_names').toggleClassName('toggled_className');
assertEnumEqual(['toggled_className'], $('class_names').classNames());
$('class_names').toggleClassName('toggled_className');
assertEnumEqual([], $('class_names').classNames());
$('class_names_ul').toggleClassName('toggled_className');
assertEnumEqual(['A', 'B', 'toggled_className'], $('class_names_ul').classNames());
$('class_names_ul').toggleClassName('toggled_className');
assertEnumEqual(['A', 'B'], $('class_names_ul').classNames());
}},
testElementScrollTo: function() {with(this) {
var elem = $('scroll_test_2');
Element.scrollTo('scroll_test_2');
assertEqual(Position.page(elem)[1], 0);
window.scrollTo(0, 0);
elem.scrollTo();
assertEqual(Position.page(elem)[1], 0);
window.scrollTo(0, 0);
}},
testCustomElementMethods: function() {with(this) {
var elem = $('navigation_test_f');
assertRespondsTo('hashBrowns', elem);
assertEqual('hash browns', elem.hashBrowns());
assertRespondsTo('hashBrowns', Element);
assertEqual('hash browns', Element.hashBrowns(elem));
}},
testSpecificCustomElementMethods: function() {with(this) {
var elem = $('navigation_test_f');
assert(Element.Methods.ByTag[elem.tagName]);
assertRespondsTo('pancakes', elem);
assertEqual("pancakes", elem.pancakes());
var elem2 = $('test-visible');
assert(Element.Methods.ByTag[elem2.tagName]);
assertUndefined(elem2.pancakes);
assertRespondsTo('waffles', elem2);
assertEqual("waffles", elem2.waffles());
assertRespondsTo('orangeJuice', elem);
assertRespondsTo('orangeJuice', elem2);
assertEqual("orange juice", elem.orangeJuice());
assertEqual("orange juice", elem2.orangeJuice());
assert(typeof Element.orangeJuice == 'undefined');
assert(typeof Element.pancakes == 'undefined');
assert(typeof Element.waffles == 'undefined');
}},
testScriptFragment: function() {with(this) {
var element = document.createElement('div');
// tests an issue with Safari 2.0 crashing when the ScriptFragment
// regular expression is using a pipe-based approach for
// matching any character
['\r','\n',' '].each(function(character){
$(element).update("<script>"+character.times(10000)+"</scr"+"ipt>");
assertEqual('', element.innerHTML);
});
$(element).update("<script>var blah='"+'\\'.times(10000)+"'</scr"+"ipt>");
assertEqual('', element.innerHTML);
}},
testPositionedOffset: function() {with(this) {
assertEnumEqual([10,10],
$('body_absolute').positionedOffset());
assertEnumEqual([10,10],
$('absolute_absolute').positionedOffset());
assertEnumEqual([10,10],
$('absolute_relative').positionedOffset());
assertEnumEqual([0,10],
$('absolute_relative_undefined').positionedOffset());
}},
testViewportOffset: function() {with(this) {
assertEnumEqual([10,10],
$('body_absolute').viewportOffset());
assertEnumEqual([20,20],
$('absolute_absolute').viewportOffset());
assertEnumEqual([20,20],
$('absolute_relative').viewportOffset());
assertEnumEqual([20,30],
$('absolute_relative_undefined').viewportOffset());
}},
testOffsetParent: function() {with(this) {
assertEqual('body_absolute', $('absolute_absolute').getOffsetParent().id);
assertEqual('body_absolute', $('absolute_relative').getOffsetParent().id);
assertEqual('absolute_relative', $('inline').getOffsetParent().id);
assertEqual('absolute_relative', $('absolute_relative_undefined').getOffsetParent().id);
}},
testAbsolutize: function() {with(this) {
$('notInlineAbsoluted', 'inlineAbsoluted').each(function(elt) {
if ('_originalLeft' in elt) delete elt._originalLeft;
elt.absolutize();
assertUndefined(elt._originalLeft, 'absolutize() did not detect absolute positioning');
});
}},
testViewportDimensions: function() {with(this) {
preservingBrowserDimensions(function() {
window.resizeTo(800, 600);
var before = document.viewport.getDimensions();
window.resizeBy(50, 50);
var after = document.viewport.getDimensions();
assertEqual(before.width + 50, after.width, "NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THIS TEST TO PASS");
assertEqual(before.height + 50, after.height, "NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THIS TEST TO PASS");
}.bind(this));
}},
testElementToViewportDimensionsDoesNotAffectDocumentProperties: function() {with(this) {
// No properties on the document should be affected when resizing
// an absolute positioned(0,0) element to viewport dimensions
var vd = document.viewport.getDimensions();
var before = documentViewportProperties.inspect();
$('elementToViewportDimensions').setStyle({ height: vd.height + 'px', width: vd.width + 'px' }).show();
var after = documentViewportProperties.inspect();
$('elementToViewportDimensions').hide();
documentViewportProperties.properties.each(function(prop) {
assertEqual(before[prop], after[prop], prop + ' was affected');
});
}},
testViewportScrollOffsets: function() {with(this) {
preservingBrowserDimensions(function() {
window.scrollTo(0, 0);
assertEqual(0, document.viewport.getScrollOffsets().top);
window.scrollTo(0, 35);
assertEqual(35, document.viewport.getScrollOffsets().top);
window.resizeTo(200, 650);
window.scrollTo(25, 35);
assertEqual(25, document.viewport.getScrollOffsets().left, "NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THESE TESTS TO PASS");
window.resizeTo(850, 650);
}.bind(this));
}},
testNodeConstants: function() {with(this) {
assert(window.Node, 'window.Node is unavailable');
var constants = $H({
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5,
ENTITY_NODE: 6,
PROCESSING_INSTRUCTION_NODE: 7,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_TYPE_NODE: 10,
DOCUMENT_FRAGMENT_NODE: 11,
NOTATION_NODE: 12
});
constants.each(function(pair) {
assertEqual(Node[pair.key], pair.value);
}, this);
}}
});
function preservingBrowserDimensions(callback) {
var original = document.viewport.getDimensions();
window.resizeTo(640, 480);
var resized = document.viewport.getDimensions();
original.width += 640 - resized.width, original.height += 480 - resized.height;
try {
window.resizeTo(original.width, original.height);
callback();
} finally {
window.resizeTo(original.width, original.height);
}
}
// ]]>
</script>
</body>
</html>