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