michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function run_test() michael@0: { michael@0: /* michael@0: * NOTE: [i] is not allowed in this test, since it's done via classinfo and michael@0: * we don't have that in xpcshell; the workaround is item(i). Suck. michael@0: */ michael@0: init(); michael@0: michael@0: test_element(); michael@0: michael@0: // more tests would be nice here (such as for documents), but the primary michael@0: // uses of Node.normalize() are in test_element; stuff beyond this is either michael@0: // unimplemented or is unlikely to be used all that much within a browser michael@0: // DOM implementation michael@0: } michael@0: michael@0: // TEST CODE michael@0: michael@0: var doc; // cache for use in all tests michael@0: michael@0: function init() michael@0: { michael@0: doc = ParseFile("empty_document.xml"); michael@0: } michael@0: michael@0: function test_element() michael@0: { michael@0: var x = doc.createElement("funk"); michael@0: michael@0: // one empty Text node michael@0: x.appendChild(doc.createTextNode("")); michael@0: do_check_eq(x.childNodes.length, 1); michael@0: michael@0: x.normalize(); michael@0: do_check_eq(x.childNodes.length, 0); michael@0: michael@0: michael@0: // multiple empty Text nodes michael@0: x.appendChild(doc.createTextNode("")); michael@0: x.appendChild(doc.createTextNode("")); michael@0: do_check_eq(x.childNodes.length, 2); michael@0: michael@0: x.normalize(); michael@0: do_check_eq(x.childNodes.length, 0); michael@0: michael@0: michael@0: // empty Text node followed by other Text node michael@0: x.appendChild(doc.createTextNode("")); michael@0: x.appendChild(doc.createTextNode("Guaraldi")); michael@0: do_check_eq(x.childNodes.length, 2); michael@0: michael@0: x.normalize(); michael@0: do_check_eq(x.childNodes.length, 1); michael@0: do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); michael@0: michael@0: michael@0: // Text node followed by empty Text node michael@0: clearKids(x); michael@0: x.appendChild(doc.createTextNode("Guaraldi")); michael@0: x.appendChild(doc.createTextNode("")); michael@0: do_check_eq(x.childNodes.length, 2); michael@0: michael@0: x.normalize(); michael@0: do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); michael@0: michael@0: michael@0: // Text node followed by empty Text node followed by other Node michael@0: clearKids(x); michael@0: x.appendChild(doc.createTextNode("Guaraldi")); michael@0: x.appendChild(doc.createTextNode("")); michael@0: x.appendChild(doc.createElement("jazzy")); michael@0: do_check_eq(x.childNodes.length, 3); michael@0: michael@0: x.normalize(); michael@0: do_check_eq(x.childNodes.length, 2); michael@0: do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); michael@0: do_check_eq(x.childNodes.item(1).nodeName, "jazzy"); michael@0: michael@0: michael@0: // Nodes are recursively normalized michael@0: clearKids(x); michael@0: var kid = doc.createElement("eit"); michael@0: kid.appendChild(doc.createTextNode("")); michael@0: michael@0: x.appendChild(doc.createTextNode("Guaraldi")); michael@0: x.appendChild(doc.createTextNode("")); michael@0: x.appendChild(kid); michael@0: do_check_eq(x.childNodes.length, 3); michael@0: do_check_eq(x.childNodes.item(2).childNodes.length, 1); michael@0: michael@0: x.normalize(); michael@0: do_check_eq(x.childNodes.length, 2); michael@0: do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); michael@0: do_check_eq(x.childNodes.item(1).childNodes.length, 0); michael@0: } michael@0: michael@0: michael@0: // UTILITY FUNCTIONS michael@0: michael@0: function clearKids(node) michael@0: { michael@0: while (node.hasChildNodes()) michael@0: node.removeChild(node.childNodes.item(0)); michael@0: }