tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_normalize.js (2949B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 function run_test() {
      7  /*
      8   * NOTE: [i] is not allowed in this test, since it's done via classinfo and
      9   * we don't have that in xpcshell; the workaround is item(i).  Suck.
     10   */
     11  init();
     12 
     13  test_element();
     14 
     15  // more tests would be nice here (such as for documents), but the primary
     16  // uses of Node.normalize() are in test_element; stuff beyond this is either
     17  // unimplemented or is unlikely to be used all that much within a browser
     18  // DOM implementation
     19 }
     20 
     21 // TEST CODE
     22 
     23 var doc; // cache for use in all tests
     24 
     25 function init() {
     26  doc = ParseFile("empty_document.xml");
     27 }
     28 
     29 function test_element() {
     30  var x = doc.createElement("funk");
     31 
     32  // one empty Text node
     33  x.appendChild(doc.createTextNode(""));
     34  Assert.equal(x.childNodes.length, 1);
     35 
     36  x.normalize();
     37  Assert.equal(x.childNodes.length, 0);
     38 
     39  // multiple empty Text nodes
     40  x.appendChild(doc.createTextNode(""));
     41  x.appendChild(doc.createTextNode(""));
     42  Assert.equal(x.childNodes.length, 2);
     43 
     44  x.normalize();
     45  Assert.equal(x.childNodes.length, 0);
     46 
     47  // empty Text node followed by other Text node
     48  x.appendChild(doc.createTextNode(""));
     49  x.appendChild(doc.createTextNode("Guaraldi"));
     50  Assert.equal(x.childNodes.length, 2);
     51 
     52  x.normalize();
     53  Assert.equal(x.childNodes.length, 1);
     54  Assert.equal(x.childNodes.item(0).nodeValue, "Guaraldi");
     55 
     56  // Text node followed by empty Text node
     57  clearKids(x);
     58  x.appendChild(doc.createTextNode("Guaraldi"));
     59  x.appendChild(doc.createTextNode(""));
     60  Assert.equal(x.childNodes.length, 2);
     61 
     62  x.normalize();
     63  Assert.equal(x.childNodes.item(0).nodeValue, "Guaraldi");
     64 
     65  // Text node followed by empty Text node followed by other Node
     66  clearKids(x);
     67  x.appendChild(doc.createTextNode("Guaraldi"));
     68  x.appendChild(doc.createTextNode(""));
     69  x.appendChild(doc.createElement("jazzy"));
     70  Assert.equal(x.childNodes.length, 3);
     71 
     72  x.normalize();
     73  Assert.equal(x.childNodes.length, 2);
     74  Assert.equal(x.childNodes.item(0).nodeValue, "Guaraldi");
     75  Assert.equal(x.childNodes.item(1).nodeName, "jazzy");
     76 
     77  // Nodes are recursively normalized
     78  clearKids(x);
     79  var kid = doc.createElement("eit");
     80  kid.appendChild(doc.createTextNode(""));
     81 
     82  x.appendChild(doc.createTextNode("Guaraldi"));
     83  x.appendChild(doc.createTextNode(""));
     84  x.appendChild(kid);
     85  Assert.equal(x.childNodes.length, 3);
     86  Assert.equal(x.childNodes.item(2).childNodes.length, 1);
     87 
     88  x.normalize();
     89  Assert.equal(x.childNodes.length, 2);
     90  Assert.equal(x.childNodes.item(0).nodeValue, "Guaraldi");
     91  Assert.equal(x.childNodes.item(1).childNodes.length, 0);
     92 }
     93 
     94 // UTILITY FUNCTIONS
     95 
     96 function clearKids(node) {
     97  while (node.hasChildNodes()) {
     98    node.removeChild(node.childNodes.item(0));
     99  }
    100 }