dom.html revision c057f16cac82e00e17f15a6597f8538a990ad8d1
<html>
<head>
<title>DOM Test Suite</title>
<style>
h1, h2, h3, h4, h5, h6, p, ul, ol, li {
Xmargin:0;
Xpadding:0;
}
form {
width: 30em;
}
#doc {
border:5px solid #000;
margin:10px;
padding:10px;
}
#hd {
background:#ccc;
}
#foo {
float:left; /* required for getStyle test */
}
#ft {
opacity: 0.75;
}
</style>
</style>
</head>
<body>
<div id="doc">
<div id="hd"><h1>Page Header</h1></div>
<div id="bd">
<div id="main" class="main">
<h2 id="foo">Section Header</h2>
<select id="test-select"><option>choose</option></select>
<em class="lorem ipsum ipsum" id="lorem-ipsum">lorem ipsum</em>
<div class="mod" id="mod1">
<div class="hd">
<h3>Module Header</h3>
</div>
<div class="bd">
<p>Fusce feugiat diam. Vestibulum elementum dui in augue. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris pulvinar.</p>
</div>
<div class="ft"><a href="#">more</a></div>
</div>
<div class="mod" id="mod2">
<div class="hd">
<h3>Module Header</h3>
</div>
<div class="bd">
<form action="#" method="get" id="search">
<fieldset id="search-fields">
<legend>Search</legend>
<label for="search-p" id="search-p-label">Query</label>
<input type="text" name="p" id="search-p">
<input type="submit" value="search">
</fieldset>
</form>
</div>
</div>
</div>
<form id="test-form" class="test-class" action="#">
<input id="test-text-value" value="text value">
<input id="test-text-novalue">
<textarea id="test-textarea-value" value="textarea value"></textarea>
<textarea id="test-textarea-novalue"></textarea>
<textarea id="test-textarea-textvalue">textarea text</textarea>
<button id="test-button-value" value="button value">button</button>
<button id="test-button-novalue"></button>
<button id="test-button-textvalue">button text</button>
<select id="test-select-value">
<option id="test-option-value" value="option value">option text</option>
<option id="test-option-textvalue">option text</option>
<option id="test-option-emptyvalue" value="">empty value</option>
<option id="test-option-emptyvalue-notext" value=""></option>
<option id="test-option-novalue"></option>
</select>
<select id="test-select-textvalue">
<option>option text</option>
</select>
<select id="test-select-emptyvalue">
<option value="">option text</option>
</select>
<select id="test-select-emptyvalue-notext">
<option value=""></option>
</select>
</form>
</div>
<div id="ft">
<p>In hac habitasse platea dictumst. Sed sit amet ligula vitae justo consequat facilisis. Integer tortor. Integer erat. In hac habitasse platea dictumst. Phasellus convallis quam vitae turpis aliquam lobortis. Aliquam scelerisque condimentum lectus. Proin semper adipiscing leo. Nulla facilisi.</p>
</div>
</div>
<script type="text/javascript">
YUI({ base: '/build/', filter: 'debug' }).use('node', function(Y) {
tool = YAHOO.tool,
Assert = YAHOO.util.Assert,
ArrayAssert = YAHOO.util.ArrayAssert,
var logger = new YAHOO.tool.TestLogger(null, { height: '80%' });
var doc = document.getElementById('doc'),
hd = document.getElementById('hd'),
bd = document.getElementById('bd'),
ft = document.getElementById('ft'),
mod1 = document.getElementById('mod1'),
mod2 = document.getElementById('mod2'),
h1 = hd.getElementsByTagName('h1')[0],
main = document.getElementById('main'),
lorem = document.getElementById('lorem-ipsum'),
search = document.getElementById('search'),
search_fields = document.getElementById('search-fields'),
search_p = document.getElementById('search-p'),
foo = document.getElementById('foo'),
tmp = document.createElement('div');
Y.DOM.getStyle(foo, 'float');
name: 'DOM',
test_byId: function() {
Assert.areEqual(doc, Y.DOM.byId('doc'), 'byId("doc")');
Assert.isNull(Y.DOM.byId('fake-id'), 'byId("fake-id")');
Assert.isNull(Y.DOM.byId(null), 'byId(null)');
Assert.isNull(Y.DOM.byId(), 'byId()');
},
test_getText: function() {
Assert.areEqual('', Y.DOM.getText(tmp), 'getText(tmp)');
Assert.areEqual('', Y.DOM.getText(null), 'getText(null)');
Assert.areEqual('', Y.DOM.getText(), 'getText()');
},
test_firstChild: function() {
Assert.isNull(Y.DOM.firstChild(tmp), 'firstChild(tmp)');
Assert.isNull(Y.DOM.firstChild(null), 'firstChild(null)');
Assert.isNull(Y.DOM.firstChild(), 'firstChild()');
Assert.isNull(Y.DOM.firstChild(1), 'firstChild(1)');
Assert.areEqual(hd, Y.DOM.firstChild(doc), 'firstChild(doc)');
Assert.areEqual(bd, Y.DOM.firstChild(doc,
function(node) { return node.id === 'bd'; }
), 'firstChild(doc, fn)');
},
test_lastChild: function() {
Assert.isNull(Y.DOM.lastChild(tmp), 'lastChild(tmp)');
Assert.areEqual(ft, Y.DOM.lastChild(doc), 'lastChild(doc)');
Assert.areEqual(bd, Y.DOM.lastChild(doc,
function(node) { return node.id === 'bd'; }
), 'lastChild(doc, fn)');
},
test_children: function() {
ArrayAssert.itemsAreEqual([], Y.DOM.children(tmp), 'children(tmp)');
ArrayAssert.itemsAreEqual([], Y.DOM.children(null), 'children(null)');
ArrayAssert.itemsAreEqual([], Y.DOM.children(), 'children()');
ArrayAssert.itemsAreEqual([hd, bd, ft], Y.DOM.children(doc), 'children(doc)');
ArrayAssert.itemsAreEqual([mod1, mod2], Y.DOM.children(main, function(node) {
return node.className === 'mod';
}), 'children(main, fn)');
},
test_childrenByTag: function() {
ArrayAssert.itemsAreEqual([], Y.DOM._childrenByTag(tmp), '_childrenByTag(tmp)');
ArrayAssert.itemsAreEqual([], Y.DOM._childrenByTag(null), '_childrenByTag(null)');
ArrayAssert.itemsAreEqual([], Y.DOM._childrenByTag(null, null), '_childrenByTag(null, null)');
ArrayAssert.itemsAreEqual([], Y.DOM._childrenByTag(), '_childrenByTag()');
ArrayAssert.itemsAreEqual([lorem], Y.DOM._childrenByTag(main, 'em'), 'children(main, "em")');
ArrayAssert.itemsAreEqual([search_p], Y.DOM._childrenByTag(search_fields, 'input', function(node) {
return node.type === 'text';
},'_childrenByTag(main, "input", fn)'));
},
test_getStyle: function() {
Assert.areEqual('left',
Y.DOM.getStyle(foo, 'float'), 'getStyle(foo, "float")');
doc.style.marginBottom = '10px';
Assert.areEqual('10px',
Y.DOM.getStyle(doc, 'marginBottom'), 'doc, marginBottom');
Assert.areEqual('static',
Y.DOM.getStyle(doc, 'position'), 'doc, position');
},
test_setStyle: function() {
Y.DOM.setStyle(ft, 'opacity', 0.5);
Assert.areEqual(Y.DOM.getStyle(ft, 'opacity'),
0.5, "Y.DOM.getStyle(ft, 'opacity')");
Y.DOM.setStyle(main, 'float', 'none');
Assert.areEqual('none', Y.DOM.getStyle(main, 'float'),
'wrong style returned');
try {
Y.DOM.setStyle(ft, 'width', '-1');
} catch(e) {
Assert.isFalse(1, 'setStyle invalid width error');
}
try {
Y.DOM.setStyle(ft, 'height', '-1');
} catch(e) {
Assert.isFalse(1, 'setStyle invalid height error');
}
Y.DOM.setStyle(ft, 'display', 'none');
Y.DOM.setStyle(ft, 'display', null);
Y.DOM.setStyle(ft, 'display', 'none');
Y.DOM.setStyle(ft, 'display', '');
Y.DOM.setStyle(ft, 'opacity', null);
Y.DOM.setStyle(ft, 'opacity', 0);
Y.DOM.setStyle(ft, 'opacity', '');
},
test_create: function() {
var html = '<div>fresh</div>';
var el = Y.DOM.create(html);
Assert.areEqual('DIV', el.tagName, html);
html = '<input type="submit">';
el = Y.DOM.create(html);
Assert.areEqual('INPUT', el.tagName, html);
html = '<input name="test-input" type="radio">';
el = Y.DOM.create(html);
Assert.areEqual('radio', el.type, html);
Assert.areEqual('test-input', el.name, html);
html = '<form></form>';
el = Y.DOM.create(html);
Assert.areEqual('FORM', el.tagName, html);
html = '<label>fresh</label>';
el = Y.DOM.create(html);
Assert.areEqual('LABEL', el.tagName, html);
html = '<li>fresh</li>';
el = Y.DOM.create(html);
Assert.areEqual('LI', el.tagName, html);
html = '<ul><li>fresh</li></ul>';
el = Y.DOM.create(html);
Assert.areEqual(1, el.childNodes.length, html);
html = '<tr>fresh</tr>';
el = Y.DOM.create(html);
Assert.areEqual('TR', el.tagName, html);
html = '<td>fresh</td>';
el = Y.DOM.create(html);
Assert.areEqual('TD', el.tagName, html);
html = '<th>fresh</th>';
el = Y.DOM.create(html);
Assert.areEqual('TH', el.tagName, html);
html = '<thead></thead>';
el = Y.DOM.create(html);
Assert.areEqual('THEAD', el.tagName, html);
html = '<caption>fresh</caption>';
el = Y.DOM.create(html);
Assert.areEqual('CAPTION', el.tagName, html);
/*
html = '<col></col>';
el = Y.DOM.create(html);
Assert.areEqual('COL', el.tagName, html);
*/
html = '<colgroup></colgroup>';
el = Y.DOM.create(html);
Assert.areEqual('COLGROUP', el.tagName, html);
html = '<colgroup><col><col></colgroup>';
el = Y.DOM.create(html);
Assert.areEqual('COLGROUP', el.tagName, html);
html = '<button>fresh</button>';
el = Y.DOM.create(html);
Assert.areEqual('BUTTON', el.tagName, html);
html = '<optgroup></optgroup>';
el = Y.DOM.create(html);
Assert.areEqual('OPTGROUP', el.tagName, html);
html = '<optgroup><option>foo</option></optgroup>';
el = Y.DOM.create(html);
Assert.areEqual('OPTGROUP', el.tagName, html);
html = '<dd>fresh</dd>';
el = Y.DOM.create(html);
Assert.areEqual('DD', el.tagName, html);
html = '<dt>fresh</dt>';
el = Y.DOM.create(html);
Assert.areEqual('DT', el.tagName, html);
html = '<select><option>foo</option><option>bar</option></select>';
el = Y.DOM.create(html);
Assert.areEqual('SELECT', el.tagName, html);
Assert.areEqual(2, el.childNodes.length, html);
html = '<table><tr><td>foo</td><td>bar</td></tr></table>';
el = Y.DOM.create(html);
Assert.areEqual('TABLE', el.tagName, html);
Assert.areEqual(1, el.childNodes.length, html);
html = '<tr><td>foo</td><td>bar</td></tr>';
el = Y.DOM.create(html);
Assert.areEqual('TR', el.tagName, html);
Assert.areEqual(2, el.childNodes.length, html);
el = Y.DOM.create(html);
Assert.areEqual('yui-iframetest', el.id, html);
/*
el = Y.DOM.create(html);
Assert.areEqual('SCRIPT', el.tagName, html);
el = Y.DOM.create(html);
Assert.areEqual('LINK', el.tagName, html);
Assert.areEqual(el.rel, 'stylesheet', html);
*/
html = '<option>fresh</option>';
el = Y.DOM.create(html);
Assert.areEqual('OPTION', el.tagName, html);
html = '<legend>fresh</legend>';
el = Y.DOM.create(html);
Assert.areEqual('LEGEND', el.tagName, html);
html = '<fieldset>fresh</fieldset>';
el = Y.DOM.create(html);
Assert.areEqual('FIELDSET', el.tagName, html);
html ='<div id="default-e34f85" class="mod view_default"> <div id="default-e34f85-hd" class="hd"><h2>Yahoo! Mail Preview</h2> <span class="mod-button-wrapper"> </span> </div> <div id="default-e34f85-bd" class="bd type_mail type_mail_default"> <p class="divider-b small text-right"><a href="http://us.lrd.yahoo.com/_ylt=AhQqDwn_HdUEqFrWLI8HTGP1cSkA/SIG=10rfos93v/**http%3A//mail.yahoo.com/">Go to Yahoo! Mail</a></p> <img src="http://l.yimg.com/a/i/ww/met/mail_promo_060608.gif" alt="Yahoo! Mail avatar shows the possibilities of email" class="promo" width="300px"> <p class="x-large sign-in-link"><a href="https://us.lrd.yahoo.com/_ylt=AnGramz3WfSHqLcCWa92OGj1cSkA/SIG=12tb7s4o8/**https%3A//login.yahoo.com/config/mail%3F.intl=us%26.src=ym%26.done=http%3A//mail.yahoo.com">Sign in</a> to preview your email here.</p> <p class="med-small">The all-new Yahoo! Mail:<br>More ways to connect with everyone.</p> </div></div>';
el = Y.DOM.create(html);
Assert.areEqual('default-e34f85', el.id, html);
html = '<thead><tr><td><table><tbody><tr><td>fresh</td></tr></tbody></table></td></tr></thead>';
el = Y.DOM.create(html);
Assert.areEqual('THEAD', el.tagName, html);
html = ' <div>foo</div>';
el = Y.DOM.create(html);
Assert.isTrue(el.tagName === 'DIV', " + html + ");
html = '<option selected>foo</option>';
el = Y.DOM.create(html);
document.getElementById('test-select').appendChild(el);
html = '<li><a href=""></a></li>';
el = Y.DOM.create(html);
Assert.areEqual('A', el.firstChild.tagName, html);
html = '<option>foo</option>';
el = Y.DOM.create(html);
document.getElementById('test-select').appendChild(el);
html = '<thead><tr><td><table><thead></thead><tbody><tr><td>fresh</td></tr></tbody></table></td></tr></thead>';
el = Y.DOM.create(html);
Assert.areEqual('THEAD', el.tagName, html);
},
test_createNodes: function() {
var html = '<div>foo</div><div id="tmp-bar">bar</div><div>baz</div>';
var el = Y.DOM.create(html);
Assert.areEqual(3, el.childNodes.length, html);
Assert.areEqual('tmp-bar', el.childNodes[1].id, html);
html = '<option>foo</option><option id="tmp-bar">bar</option><option>baz</option>';
el = Y.DOM.create(html);
Assert.areEqual(3, el.childNodes.length, html);
Assert.areEqual('tmp-bar', el.childNodes[1].id, html);
html = '<td>foo</td><td id="tmp-bar">bar</td><td>baz</td>';
el = Y.DOM.create(html);
Assert.areEqual(3, el.childNodes.length, html);
Assert.areEqual('tmp-bar', el.childNodes[1].id, html);
html = '<tbody>foo</tbody><tbody id="tmp-bar">bar</tbody><tbody>baz</tbody>';
el = Y.DOM.create(html);
Assert.areEqual(3, el.childNodes.length, html);
Assert.areEqual('tmp-bar', el.childNodes[1].id, html);
html = '<thead><tr><td><table><tbody><tr><td>fresh</td></tr></tbody></table></td></tr></thead><tbody></tbody>';
el = Y.DOM.create(html);
Assert.areEqual(2, el.childNodes.length, html);
},
test_ancestor: function() {
Assert.areEqual(doc, Y.DOM.ancestor(hd), 'ancestor(hd)');
Assert.areEqual(doc, Y.DOM.ancestor(bd), 'ancestor(bd)');
Assert.areEqual(doc, Y.DOM.ancestor(bd), 'ancestor(bd)');
Assert.areEqual(document.body, Y.DOM.ancestor(bd, function(node) { return node.tagName === 'BODY';}), 'ancestor(bd, fn)');
},
test_insertBefore: function() {
//Assert.areEqual(ft, Y.DOM.firstChild(doc), 'insertBefore(ft, hd)');
},
test_insertAfter: function() {
//Assert.areEqual(hd, ft.nextSibling, 'insertAfter(hd, ft)');
},
test_getAttribute: function() {
var node = Y.DOM.byId('doc');
Y.DOM.setAttribute(node, 'class', 'foo');
Assert.areEqual('foo', Y.DOM.getAttribute(node, 'className'), "Y.DOM.getAttribute(node, 'className')");
Y.DOM.setAttribute(node, 'className', 'bar');
node = Y.DOM.byId('search-p-label');
Assert.areEqual('search-p', Y.DOM.getAttribute(node, 'for'), "Y.DOM.setAttribute(node, 'for', 'search-p')");
Assert.areEqual('search-p', Y.DOM.getAttribute(node, 'htmlFor'), "Y.DOM.setAttribute(node, 'htmlFor', 'search-p')");
},
test_getValue: function() {
// text
var id = 'test-text-value',
val = 'text value';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-text-novalue';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
// textarea
id = 'test-textarea-textvalue';
val = 'textarea text';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-textarea-value';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-textarea-novalue';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
// button
id = 'test-button-value';
val = 'button value';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-button-novalue';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-button-textvalue';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
// select
id = 'test-select-value';
val = 'option value';
id = 'test-select-novalue';
val = '';
id = 'test-select-textvalue';
val = 'option text';
id = 'test-select-emptyvalue';
val = '';
id = 'test-select-emptyvalue-notext';
val = '';
// option
id = 'test-option-value';
val = 'option value';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-option-emptyvalue';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-option-novalue';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-option-emptyvalue-notext';
val = '';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
id = 'test-option-textvalue';
val = 'option text';
Y.DOM.setValue(Y.DOM.byId(id), 'new value');
},
test_position: function() { // basic position tests
var node = document.createElement('div');
ArrayAssert.itemsAreEqual([0, 0], Y.DOM.getXY(node), 'off-dom: [0, 0]');
Y.DOM.setXY(node, [100, 200]);
ArrayAssert.itemsAreEqual([100, 200], Y.DOM.getXY(node), 'off-dom: [100, 200]');
document.body.appendChild(node);
Y.DOM.setXY(node, [100, 200]);
ArrayAssert.itemsAreEqual([100, 200], Y.DOM.getXY(node), 'setXY([100, 200]');
},
test_region: function() {
var node = Y.DOM.byId('doc'),
xy = Y.DOM.getXY(node),
region = Y.DOM.region(node);
Assert.areEqual(xy[0], region[0], 'region[0]');
Assert.areEqual(xy[1], region[1], 'region[1]');
},
test_viewportRegion: function() {
var node = Y.DOM.byId('doc'),
scrollX = Y.DOM.docScrollX(node),
scrollY = Y.DOM.docScrollY(node),
winHeight = Y.DOM.winHeight(node),
winWidth = Y.DOM.winWidth(node),
region = Y.DOM.viewportRegion(node);
Assert.areEqual(scrollX, region[0], 'region[0]');
Assert.areEqual(scrollY, region[1], 'region[1]');
region = Y.DOM.viewportRegion();
Assert.areEqual(scrollX, region[0], 'region[0]');
Assert.areEqual(scrollY, region[1], 'region[1]');
}
}));
tool.TestRunner.add(suite);
if (parent && parent != window) {
} else {
}
});
</script>
</body>
</html>