summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_getTranslationNodes.html
blob: aa8b1229da0c1c86b942f3a835a6bfdfab4c1051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for nsIDOMWindowUtils.getTranslationNodes</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="runTest()">
<script type="application/javascript">
  var utils = SpecialPowers.getDOMWindowUtils(window);


  function testTranslationRoot(rootNode) {
    var translationNodes = utils.getTranslationNodes(rootNode);

    var expectedResult = rootNode.getAttribute("expected");
    var expectedLength = expectedResult.split(" ").length;

    is(translationNodes.length, expectedLength,
       "Correct number of translation nodes for testcase " + rootNode.id);

    var resultList = [];
    for (var i = 0; i < translationNodes.length; i++) {
      var node = translationNodes.item(i).localName;
      if (translationNodes.isTranslationRootAtIndex(i)) {
        node += "[root]"
      }
      resultList.push(node);
    }

    is(resultList.length, translationNodes.length,
       "Correct number of translation nodes for testcase " + rootNode.id);

    is(resultList.join(" "), expectedResult,
       "Correct list of translation nodes for testcase " + rootNode.id);
  }

  function runTest() {
    isnot(utils, null, "nsIDOMWindowUtils");

    var testcases = document.querySelectorAll("div[expected]");
    for (var testcase of testcases) {
      testTranslationRoot(testcase);
    }

    var testiframe = document.getElementById("testiframe");
    var iframediv = testiframe.contentDocument.querySelector("div");
    try {
      var foo = utils.getTranslationNodes(iframediv);
      ok(false, "Cannot use a node from a different document");
    } catch (e) {
      is(e.name, "WrongDocumentError", "Cannot use a node from a different document");
    }

    SimpleTest.finish();
  }

  SimpleTest.waitForExplicitFinish();
</script>

<!-- Test that an inline element inside a root is not a root -->
<div id="testcase1"
     expected="div[root] span">
  <div>
    lorem ipsum <span>dolor</span> sit amet
  </div>
</div>

<!-- Test that a usually inline element becomes a root if it is
     displayed as a block -->
<div id="testcase2"
     expected="div[root] span[root]">
  <div>
    lorem ipsum <span style="display: block;">dolor</span> sit amet
  </div>
</div>

<!-- Test that the content-less <div> is ignored and only the
     <p> with content is returned -->
<div id="testcase3"
     expected="p[root]">
  <div>
    <p>lorem ipsum</p>
  </div>
</div>

<!-- Test that an inline element which the parent is not a root
     becomes a root -->
<div id="testcase4"
     expected="span[root]">
  <div>
    <span>lorem ipsum</span>
  </div>
</div>

<!-- Test siblings -->
<div id="testcase5"
     expected="li[root] li[root]">
  <ul>
    <li>lorem</li>
    <li>ipsum</li>
  </ul>
</div>

<!-- Test <ul> with content outside li -->
<div id="testcase6"
     expected="ul[root] li[root] li[root]">
  <ul>Lorem
    <li>lorem</li>
    <li>ipsum</li>
  </ul>
</div>

<!-- Test inline siblings -->
<div id="testcase7"
     expected="ul[root] li li">
  <ul>Lorem
    <li style="display: inline">lorem</li>
    <li style="display: inline">ipsum</li>
  </ul>
</div>

<!-- Test inline siblings becoming roots -->
<div id="testcase8"
     expected="li[root] li[root]">
  <ul>
    <li style="display: inline">lorem</li>
    <li style="display: inline">ipsum</li>
  </ul>
</div>

<!-- Test that nodes with only punctuation, whitespace
     or numbers are ignored -->
<div id="testcase9"
     expected="li[root] li[root]">
  <ul>
    <li>lorem</li>
    <li>ipsum</li>
    <li>-.,;'/!@#$%^*()</li>
    <li>0123456789</li>
    <li>
          </li>
  </ul>
</div>

<!-- Test paragraphs -->
<div id="testcase10"
     expected="p[root] a b p[root] a b">
  <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b>amet</b>, consetetur</p>
  <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b>amet</b>, consetetur</p>
</div>

<!-- Test that a display:none element is not ignored -->
<div id="testcase11"
     expected="p[root] a b">
  <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b style="display:none">amet</b>, consetetur</p>
</div>

<!-- Test that deep nesting does not cause useless content to be returned -->
<div id="testcase12"
     expected="p[root]">
  <div>
    <div>
      <div>
        <p>Lorem ipsum</p>
      </div>
    </div>
  </div>
</div>

<!-- Test that deep nesting does not cause useless content to be returned -->
<div id="testcase13"
     expected="div[root] p[root]">
  <div>Lorem ipsum
    <div>
      <div>
        <p>Lorem ipsum</p>
      </div>
    </div>
  </div>
</div>

<!-- Test that non-html elements and elements that usually have non-translatable
     content are ignored -->
<div id="testcase14"
     expected="div[root]">
  <div>
    Lorem Ipsum
    <noscript>Lorem Ipsum</noscript>
    <style>.dummyClass { color: blue; }</style>
    <script> /* script tag */ </script>
    <code> code </code>
    <iframe id="testiframe"
            srcdoc="<div>Lorem ipsum</div>">
    </iframe>
    <svg>lorem</svg>
    <math>ipsum</math>
  </div>
</div>

<!-- Test that nesting of inline elements won't produce roots as long as
     the parents are in the list of translation nodes -->
<div id="testcase15"
     expected="p[root] a b span em">
  <p>Lorem <a>ipsum <b>dolor <span>sit</span> amet</b></a>, <em>consetetur</em></p>
</div>

<!-- Test that comment nodes are not considered for translation -->
<div id="testcase16"
     expected="p[root] p[root]">
  <p>Lorem ipsum</p>
  <div>  <!-- Comment -->  </div>
  <p>Lorem ipsum</p>
</div>

<!-- Test that comment nodes are not considered for translation -->
<div id="testcase17"
     expected="p[root]">
  <div>
    <!-- Comment -->
    <p>Lorem Ipsum</p>
  </div>
</div>
</body>
</html>