Home | History | Annotate | Download | only in Node
      1 <html>
      2 
      3 <head>
      4 
      5 <title>Test of DOM Node.normalize()</title>
      6 
      7 <script>
      8 
      9 function logLine(message)
     10 {
     11     var console = document.getElementById("console");
     12     console.appendChild(document.createTextNode(message));
     13     console.appendChild(document.createElement('br'));
     14 }
     15 
     16 function log(message)
     17 {
     18     var console = document.getElementById("console");
     19     console.appendChild(document.createTextNode(message));
     20 }
     21 
     22 
     23 function prepare_test1(testDiv)
     24 {
     25     testDiv.firstChild.splitText(4);
     26     return testDiv.childNodes.length == 2;
     27 }
     28 function check_test1(testDiv)
     29 {
     30     return testDiv.childNodes.length == 1;
     31 }
     32 
     33 function prepare_test2(testDiv)
     34 {
     35     testDiv.firstChild.splitText(9);
     36     testDiv.firstChild.splitText(4);
     37     return testDiv.childNodes.length == 3;
     38 }
     39 function check_test2(testDiv)
     40 {
     41     return testDiv.childNodes.length == 1;
     42 }
     43 
     44 function prepare_test3(testDiv)
     45 {
     46     testDiv.childNodes[1].splitText(4);
     47     testDiv.childNodes[4].splitText(3);
     48     return testDiv.childNodes.length == 6;
     49 }
     50 function check_test3(testDiv)
     51 {
     52     return testDiv.childNodes.length == 4;
     53 }
     54 
     55 function prepare_test4(testDiv)
     56 {
     57     testDiv.childNodes[0].data = "";
     58     return testDiv.childNodes.length == 1;
     59 }
     60 function check_test4(testDiv)
     61 {
     62     return testDiv.childNodes.length == 0;
     63 }
     64 
     65 function prepare_test5(testDiv)
     66 {
     67     testDiv.childNodes[1].data = "";
     68     return testDiv.childNodes.length == 3;
     69 }
     70 function check_test5(testDiv)
     71 {
     72     return testDiv.childNodes.length == 2;
     73 }
     74 
     75 function prepare_test6(testDiv)
     76 {
     77     testDiv.childNodes[0].splitText(0);
     78     testDiv.childNodes[0].splitText(0);
     79     return testDiv.childNodes.length == 3;
     80 }
     81 function check_test6(testDiv)
     82 {
     83     return testDiv.childNodes.length == 1;
     84 }
     85 
     86 function prepare_test7(testDiv)
     87 {
     88     testDiv.childNodes[0].splitText(4);
     89     testDiv.childNodes[0].splitText(4);
     90     testDiv.childNodes[0].splitText(4);
     91     return testDiv.childNodes.length == 4;
     92 }
     93 function check_test7(testDiv)
     94 {
     95     return testDiv.childNodes.length == 1;
     96 }
     97 
     98 function prepare_test8(testDiv)
     99 {
    100     testDiv.childNodes[0].splitText(4);
    101     testDiv.childNodes[0].splitText(4);
    102     return testDiv.childNodes.length == 3;
    103 }
    104 function check_test8(testDiv)
    105 {
    106     return testDiv.childNodes.length == 1;
    107 }
    108 
    109 function prepare_test9(testDiv)
    110 {
    111     testDiv.childNodes[1].splitText(4);
    112     testDiv.childNodes[1].splitText(0);    // empty text node before other text nodes
    113     testDiv.childNodes[5].splitText(3);
    114     testDiv.childNodes[5].splitText(3);    // empty text node between other text nodes
    115     testDiv.childNodes[7].splitText(2);    // empty text node after other text nodes
    116     return testDiv.childNodes.length == 9;
    117 }
    118 function check_test9(testDiv)
    119 {
    120     return testDiv.childNodes.length == 4;
    121 }
    122 
    123 function prepare_test10(testDiv)
    124 {
    125     testDiv.childNodes[0].childNodes[0].splitText(2);
    126     testDiv.childNodes[1].splitText(4);
    127     testDiv.childNodes[3].childNodes[0].data = "";    // empty first text node of the second bold node
    128     testDiv.childNodes[3].childNodes[1].childNodes[0].data = "";    // empty text node of the italic node
    129     testDiv.childNodes[4].splitText(1);
    130     return testDiv.childNodes.length == 6;
    131 }
    132 function check_test10(testDiv)
    133 {
    134     return testDiv.childNodes.length == 4
    135         && testDiv.childNodes[0].childNodes.length == 1        // first bold node must have single text node child
    136         && testDiv.childNodes[2].childNodes.length == 1        // first bold node must have single italic node child
    137         && testDiv.childNodes[2].childNodes[0].childNodes.length == 0;    // italic node must be empty
    138 }
    139 
    140 function runTest(testDiv, testName)
    141 {
    142     if (self["prepare_"+testName](testDiv)) {
    143         var oldHTML = testDiv.innerHTML;
    144         testDiv.normalize();
    145         if (testDiv.innerHTML != oldHTML) {
    146             log("FAILED: innerHTML changed from \"" + oldHTML + "\" to \"" + testDiv.innerHTML + "\"");
    147         } else {
    148             if (self["check_"+testName](testDiv))
    149                 log("PASSED");
    150             else
    151                 log("FAILED");
    152         }        
    153     } else {
    154         log("FAILED in test preparation");
    155     }
    156 }
    157 
    158 function runTests()
    159 {
    160     if (window.layoutTestController)
    161         layoutTestController.dumpAsText();
    162 
    163     try {
    164         var tests = document.getElementById("tests").childNodes;
    165         for (i = 0; i < tests.length; i++) {
    166             var testDiv = tests[i];
    167             // Skip formatting text nodes
    168             if (testDiv.nodeType == Node.ELEMENT_NODE) {
    169                 var testName = testDiv.getAttribute("name");
    170                 
    171                 log(testName + " (" + testDiv.getAttribute("description") + "): ");
    172                 
    173                 try {
    174                     runTest(testDiv, testName);
    175                 } catch(e) {
    176                     log("FAILED with exception: " + e);
    177                 }
    178                 
    179                 logLine("")
    180             }
    181         }
    182 
    183     } catch(e) {
    184         logLine("FAILED, exception thrown during tests: " + e);
    185     }
    186 }
    187 
    188 </script>
    189 
    190 </head>
    191 
    192 <body onload="runTests()">
    193 
    194 <div id="description">Several tests of the DOM normalize() function.</div>
    195 
    196 <div id="tests" style="display:none">
    197     <div name="test1" description="two non-empty text nodes">some text</div>
    198     <div name="test2" description="three non-empty text nodes">some more text</div>
    199     <div name="test3" description="non-empty text nodes mixed with elements"><b></b>some more<b></b> text</div>
    200     <div name="test4" description="single empty text node">text</div>
    201     <div name="test5" description="empty text node between elements"><b></b>text<i></i></div>
    202     <div name="test6" description="empty text nodes before non-empty node">text</div>
    203     <div name="test7" description="empty text nodes after non-empty node">text</div>
    204     <div name="test8" description="empty text nodes between non-empty nodes">some text</div>
    205     <div name="test9" description="empty and non-empty text nodes mixed with elements"><b></b>some more<b></b> text</div>
    206     <div name="test10" description="mixed cases including deeper nested text nodes"><b>text</b>text <b>text<i>text</i></b> text</div>
    207 </div>
    208 
    209 <div id="console"></div>
    210 
    211 </body>
    212 
    213 </html>
    214