blob: 4ba0268339468706e7ff876ca8e3cd668b9feacb [file] [log] [blame]
<html>
<script>
var parser = new DOMParser();
var serializer = new XMLSerializer();
var successCount = 0;
function debug(str) {
var console = document.getElementById('console');
var li = document.createElement('li');
li.appendChild(document.createTextNode(str));
console.appendChild(li);
}
// Replacing element tests
function replaceElementWithElement() {
debug('replacing element with element')
try {
var doc = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><foo/>', "text/xml")
var newChild = doc.createElement('bar');
doc.replaceChild(newChild, doc.documentElement)
} catch (ex) {
debug('FAILED: ' + ex)
return;
}
debug('SUCCESS: ' + serializer.serializeToString(doc));
successCount++;
}
function replaceElementWithDoctype() {
debug('replacing element with doctype')
try {
var doc = parser.parseFromString('<foo/>', "text/xml")
var newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd')
doc.replaceChild(newChild, doc.documentElement)
} catch (ex) {
debug('FAILED: ' + ex)
return;
}
debug('SUCCESS: ' + serializer.serializeToString(doc));
successCount++;
}
function replaceElementWithDoctypeWhenDoctypeAlreadyExists() {
debug('replacing element with doctype when a doctype already exists')
try {
var doc = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><foo/>', "text/xml")
var newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd')
doc.replaceChild(newChild, doc.documentElement)
} catch (ex) {
debug('SUCCESS: failed with exception ' + ex)
successCount++;
return;
}
debug('FAILED: succeeded when test should have failed ' + serializer.serializeToString(doc));
}
function replaceDoctypeWithDoctype() {
debug('replacing doctype with doctype')
try {
var doc = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><foo/>', "text/xml")
var newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd')
doc.replaceChild(newChild, doc.doctype)
} catch (ex) {
debug('FAILED: ' + ex)
return;
}
debug('SUCCESS: ' + serializer.serializeToString(doc));
successCount++;
}
function replaceDoctypeWithElement() {
debug('replacing doctype with element')
try {
var doc = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><foo/>', "text/xml")
// Remove the document element
doc.removeChild(doc.documentElement);
var newChild = doc.createElement('bar');
alert(serializer.serializeToString(doc))
doc.replaceChild(newChild, doc.doctype)
} catch (ex) {
debug('FAILED: ' + ex)
return;
}
debug('SUCCESS: ' + serializer.serializeToString(doc));
successCount++;
}
function replaceDoctypeWithElementWhenElementAlreadyExists() {
debug('replacing element with doctype when an element already exists')
try {
var doc = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><foo/>', "text/xml")
alert(serializer.serializeToString(doc));
var newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd')
doc.replaceChild(newChild, doc.documentElement)
} catch (ex) {
debug('SUCCESS: failed with exception ' + ex)
successCount++;
return;
}
debug('FAILED: succeeded when test should have failed ' + serializer.serializeToString(doc));
}
function runTest() {
if (window.layoutTestController)
layoutTestController.dumpAsText();
replaceElementWithElement();
replaceElementWithDoctype();
// FIXME: Once DocumentType nodes are exposed as children of the document we can add these checks back.
// See http://bugs.webkit.org/show_bug.cgi?id=12751
if (0) {
replaceElementWithDoctypeWhenDoctypeAlreadyExists();
replaceDoctypeWithDoctype();
replaceDoctypeWithElement();
}
if (successCount == 2)
debug('SUCCESS - All tests succeeded!')
else
debug('FAILED - One or more tests failed')
}
</script>
<body onload="runTest();">
This tests that various combinations of replaceChild on the document works as specified. If this test succeeds, there will be a number of "SUCCESS" lines below followed by "SUCCESS - All tests succeeded".
<ul id="console">
</ul>
</body>
</html>