tor-browser

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

test_getTranslationNodes.html (5782B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test for nsIDOMWindowUtils.getTranslationNodes</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
      7 </head>
      8 <body onload="runTest()">
      9 <script type="application/javascript">
     10  var utils = SpecialPowers.getDOMWindowUtils(window);
     11 
     12 
     13  function testTranslationRoot(rootNode) {
     14    var translationNodes = utils.getTranslationNodes(rootNode);
     15 
     16    var expectedResult = rootNode.getAttribute("expected");
     17    var expectedLength = expectedResult.split(" ").length;
     18 
     19    is(translationNodes.length, expectedLength,
     20       "Correct number of translation nodes for testcase " + rootNode.id);
     21 
     22    var resultList = [];
     23    for (var i = 0; i < translationNodes.length; i++) {
     24      var node = translationNodes.item(i).localName;
     25      if (translationNodes.isTranslationRootAtIndex(i)) {
     26        node += "[root]"
     27      }
     28      resultList.push(node);
     29    }
     30 
     31    is(resultList.length, translationNodes.length,
     32       "Correct number of translation nodes for testcase " + rootNode.id);
     33 
     34    is(resultList.join(" "), expectedResult,
     35       "Correct list of translation nodes for testcase " + rootNode.id);
     36  }
     37 
     38  function runTest() {
     39    isnot(utils, null, "nsIDOMWindowUtils");
     40 
     41    var testcases = document.querySelectorAll("div[expected]");
     42    for (var testcase of testcases) {
     43      testTranslationRoot(testcase);
     44    }
     45 
     46    var testiframe = document.getElementById("testiframe");
     47    var iframediv = testiframe.contentDocument.querySelector("div");
     48    try {
     49      var foo = utils.getTranslationNodes(iframediv);
     50      ok(false, "Cannot use a node from a different document");
     51    } catch (e) {
     52      is(e.name, "WrongDocumentError", "Cannot use a node from a different document");
     53    }
     54 
     55    SimpleTest.finish();
     56  }
     57 
     58  SimpleTest.waitForExplicitFinish();
     59 </script>
     60 
     61 <!-- Test that an inline element inside a root is not a root -->
     62 <div id="testcase1"
     63     expected="div[root] span">
     64  <div>
     65    lorem ipsum <span>dolor</span> sit amet
     66  </div>
     67 </div>
     68 
     69 <!-- Test that a usually inline element becomes a root if it is
     70     displayed as a block -->
     71 <div id="testcase2"
     72     expected="div[root] span[root]">
     73  <div>
     74    lorem ipsum <span style="display: block;">dolor</span> sit amet
     75  </div>
     76 </div>
     77 
     78 <!-- Test that the content-less <div> is ignored and only the
     79     <p> with content is returned -->
     80 <div id="testcase3"
     81     expected="p[root]">
     82  <div>
     83    <p>lorem ipsum</p>
     84  </div>
     85 </div>
     86 
     87 <!-- Test that an inline element which the parent is not a root
     88     becomes a root -->
     89 <div id="testcase4"
     90     expected="span[root]">
     91  <div>
     92    <span>lorem ipsum</span>
     93  </div>
     94 </div>
     95 
     96 <!-- Test siblings -->
     97 <div id="testcase5"
     98     expected="li[root] li[root]">
     99  <ul>
    100    <li>lorem</li>
    101    <li>ipsum</li>
    102  </ul>
    103 </div>
    104 
    105 <!-- Test <ul> with content outside li -->
    106 <div id="testcase6"
    107     expected="ul[root] li[root] li[root]">
    108  <ul>Lorem
    109    <li>lorem</li>
    110    <li>ipsum</li>
    111  </ul>
    112 </div>
    113 
    114 <!-- Test inline siblings -->
    115 <div id="testcase7"
    116     expected="ul[root] li li">
    117  <ul>Lorem
    118    <li style="display: inline">lorem</li>
    119    <li style="display: inline">ipsum</li>
    120  </ul>
    121 </div>
    122 
    123 <!-- Test inline siblings becoming roots -->
    124 <div id="testcase8"
    125     expected="li[root] li[root]">
    126  <ul>
    127    <li style="display: inline">lorem</li>
    128    <li style="display: inline">ipsum</li>
    129  </ul>
    130 </div>
    131 
    132 <!-- Test that nodes with only punctuation, whitespace
    133     or numbers are ignored -->
    134 <div id="testcase9"
    135     expected="li[root] li[root]">
    136  <ul>
    137    <li>lorem</li>
    138    <li>ipsum</li>
    139    <li>-.,;'/!@#$%^*()</li>
    140    <li>0123456789</li>
    141    <li>
    142          </li>
    143  </ul>
    144 </div>
    145 
    146 <!-- Test paragraphs -->
    147 <div id="testcase10"
    148     expected="p[root] a b p[root] a b">
    149  <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b>amet</b>, consetetur</p>
    150  <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b>amet</b>, consetetur</p>
    151 </div>
    152 
    153 <!-- Test that a display:none element is not ignored -->
    154 <div id="testcase11"
    155     expected="p[root] a b">
    156  <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b style="display:none">amet</b>, consetetur</p>
    157 </div>
    158 
    159 <!-- Test that deep nesting does not cause useless content to be returned -->
    160 <div id="testcase12"
    161     expected="p[root]">
    162  <div>
    163    <div>
    164      <div>
    165        <p>Lorem ipsum</p>
    166      </div>
    167    </div>
    168  </div>
    169 </div>
    170 
    171 <!-- Test that deep nesting does not cause useless content to be returned -->
    172 <div id="testcase13"
    173     expected="div[root] p[root]">
    174  <div>Lorem ipsum
    175    <div>
    176      <div>
    177        <p>Lorem ipsum</p>
    178      </div>
    179    </div>
    180  </div>
    181 </div>
    182 
    183 <!-- Test that non-html elements and elements that usually have non-translatable
    184     content are ignored -->
    185 <div id="testcase14"
    186     expected="div[root]">
    187  <div>
    188    Lorem Ipsum
    189    <noscript>Lorem Ipsum</noscript>
    190    <style>.dummyClass { color: blue; }</style>
    191    <script> /* script tag */ </script>
    192    <code> code </code>
    193    <iframe id="testiframe"
    194            srcdoc="<div>Lorem ipsum</div>">
    195    </iframe>
    196    <svg>lorem</svg>
    197    <math>ipsum</math>
    198  </div>
    199 </div>
    200 
    201 <!-- Test that nesting of inline elements won't produce roots as long as
    202     the parents are in the list of translation nodes -->
    203 <div id="testcase15"
    204     expected="p[root] a b span em">
    205  <p>Lorem <a>ipsum <b>dolor <span>sit</span> amet</b></a>, <em>consetetur</em></p>
    206 </div>
    207 
    208 <!-- Test that comment nodes are not considered for translation -->
    209 <div id="testcase16"
    210     expected="p[root] p[root]">
    211  <p>Lorem ipsum</p>
    212  <div>  <!-- Comment -->  </div>
    213  <p>Lorem ipsum</p>
    214 </div>
    215 
    216 <!-- Test that comment nodes are not considered for translation -->
    217 <div id="testcase17"
    218     expected="p[root]">
    219  <div>
    220    <!-- Comment -->
    221    <p>Lorem Ipsum</p>
    222  </div>
    223 </div>
    224 </body>
    225 </html>