2 <script src=
"../../../resources/js-test.js"></script>
4 var parser
= new DOMParser();
5 var serializer
= new XMLSerializer();
6 var doc
, newChild
, fragment
;
8 function dumpDocument() {
9 debug(escapeHTML(serializer
.serializeToString(doc
)) + '<br>');
12 function test(name
, fn
) {
18 description('This tests that various combinations of replaceChild on the document works as specified.');
20 test('replacing element with element', function() {
21 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
22 newChild
= doc
.createElement('div');
24 shouldNotThrow('doc.replaceChild(newChild, doc.documentElement)');
27 test('replacing element with element in fragment', function() {
28 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
29 fragment
= doc
.createDocumentFragment();
30 fragment
.appendChild(doc
.createElement('div'));
32 shouldNotThrow('doc.replaceChild(fragment, doc.documentElement);');
35 test('replacing element with multiple elements in fragment', function() {
36 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
37 fragment
= doc
.createDocumentFragment();
38 fragment
.appendChild(doc
.createElement('div'));
39 fragment
.appendChild(doc
.createElement('span'));
41 shouldThrow('doc.replaceChild(fragment, doc.documentElement);');
44 test('replacing element with doctype', function() {
45 doc
= parser
.parseFromString('<body/>', 'text/xml');
46 newChild
= doc
.implementation
.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
48 shouldNotThrow('doc.replaceChild(newChild, doc.documentElement)');
51 test('replacing element with doctype when a doctype already exists', function() {
52 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
53 newChild
= doc
.implementation
.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
55 shouldThrow('doc.replaceChild(newChild, doc.documentElement)');
58 test('replacing doctype with doctype', function() {
59 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
60 newChild
= doc
.implementation
.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
62 shouldNotThrow('doc.replaceChild(newChild, doc.doctype)');
65 test('replacing doctype with element', function() {
66 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
67 doc
.removeChild(doc
.documentElement
);
68 newChild
= doc
.createElement('bar');
70 shouldNotThrow('doc.replaceChild(newChild, doc.doctype)');
73 test('replacing element with doctype when an element already exists', function() {
74 doc
= parser
.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
75 newChild
= doc
.implementation
.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
77 shouldThrow('doc.replaceChild(newChild, doc.documentElement)');