content/test/unit/test_normalize.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/test/unit/test_normalize.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +function run_test()
    1.10 +{
    1.11 +  /*
    1.12 +   * NOTE: [i] is not allowed in this test, since it's done via classinfo and
    1.13 +   * we don't have that in xpcshell; the workaround is item(i).  Suck.
    1.14 +   */
    1.15 +  init();
    1.16 +
    1.17 +  test_element();
    1.18 +
    1.19 +  // more tests would be nice here (such as for documents), but the primary
    1.20 +  // uses of Node.normalize() are in test_element; stuff beyond this is either
    1.21 +  // unimplemented or is unlikely to be used all that much within a browser
    1.22 +  // DOM implementation
    1.23 +}
    1.24 +
    1.25 +// TEST CODE
    1.26 +
    1.27 +var doc; // cache for use in all tests
    1.28 +
    1.29 +function init()
    1.30 +{
    1.31 +  doc = ParseFile("empty_document.xml");
    1.32 +}
    1.33 +
    1.34 +function test_element()
    1.35 +{
    1.36 +  var x = doc.createElement("funk");
    1.37 +
    1.38 +  // one empty Text node
    1.39 +  x.appendChild(doc.createTextNode(""));
    1.40 +  do_check_eq(x.childNodes.length, 1);
    1.41 +
    1.42 +  x.normalize();
    1.43 +  do_check_eq(x.childNodes.length, 0);
    1.44 +
    1.45 +
    1.46 +  // multiple empty Text nodes
    1.47 +  x.appendChild(doc.createTextNode(""));
    1.48 +  x.appendChild(doc.createTextNode(""));
    1.49 +  do_check_eq(x.childNodes.length, 2);
    1.50 +
    1.51 +  x.normalize();
    1.52 +  do_check_eq(x.childNodes.length, 0);
    1.53 +
    1.54 +
    1.55 +  // empty Text node followed by other Text node
    1.56 +  x.appendChild(doc.createTextNode(""));
    1.57 +  x.appendChild(doc.createTextNode("Guaraldi"));
    1.58 +  do_check_eq(x.childNodes.length, 2);
    1.59 +
    1.60 +  x.normalize();
    1.61 +  do_check_eq(x.childNodes.length, 1);
    1.62 +  do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    1.63 +
    1.64 +
    1.65 +  // Text node followed by empty Text node
    1.66 +  clearKids(x);
    1.67 +  x.appendChild(doc.createTextNode("Guaraldi"));
    1.68 +  x.appendChild(doc.createTextNode(""));
    1.69 +  do_check_eq(x.childNodes.length, 2);
    1.70 +
    1.71 +  x.normalize();
    1.72 +  do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    1.73 +
    1.74 +
    1.75 +  // Text node followed by empty Text node followed by other Node
    1.76 +  clearKids(x);
    1.77 +  x.appendChild(doc.createTextNode("Guaraldi"));
    1.78 +  x.appendChild(doc.createTextNode(""));
    1.79 +  x.appendChild(doc.createElement("jazzy"));
    1.80 +  do_check_eq(x.childNodes.length, 3);
    1.81 +
    1.82 +  x.normalize();
    1.83 +  do_check_eq(x.childNodes.length, 2);
    1.84 +  do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    1.85 +  do_check_eq(x.childNodes.item(1).nodeName, "jazzy");
    1.86 +
    1.87 +
    1.88 +  // Nodes are recursively normalized
    1.89 +  clearKids(x);
    1.90 +  var kid = doc.createElement("eit");
    1.91 +  kid.appendChild(doc.createTextNode(""));
    1.92 +
    1.93 +  x.appendChild(doc.createTextNode("Guaraldi"));
    1.94 +  x.appendChild(doc.createTextNode(""));
    1.95 +  x.appendChild(kid);
    1.96 +  do_check_eq(x.childNodes.length, 3);
    1.97 +  do_check_eq(x.childNodes.item(2).childNodes.length, 1);
    1.98 +
    1.99 +  x.normalize();
   1.100 +  do_check_eq(x.childNodes.length, 2);
   1.101 +  do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
   1.102 +  do_check_eq(x.childNodes.item(1).childNodes.length, 0);
   1.103 +}
   1.104 +
   1.105 +
   1.106 +// UTILITY FUNCTIONS
   1.107 +
   1.108 +function clearKids(node)
   1.109 +{
   1.110 +  while (node.hasChildNodes())
   1.111 +    node.removeChild(node.childNodes.item(0));
   1.112 +}

mercurial