| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.soyTest'); |
| 16 |
goog.setTestOnly('goog.soyTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.NodeType'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.functions'); |
| 22 |
goog.require('goog.soy'); |
| 23 |
/** @suppress {extraRequire} */ |
| 24 |
goog.require('goog.soy.testHelper'); |
| 25 |
goog.require('goog.testing.PropertyReplacer'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
|
| 28 |
var stubs; |
| 29 |
|
| 30 |
function setUp() { |
| 31 |
stubs = new goog.testing.PropertyReplacer(); |
| 32 |
} |
| 33 |
|
| 34 |
function tearDown() { |
| 35 |
stubs.reset(); |
| 36 |
} |
| 37 |
|
| 38 |
function testRenderHtml() { |
| 39 |
var testDiv = goog.dom.createElement(goog.dom.TagName.DIV); |
| 40 |
goog.soy.renderHtml(testDiv, example.sanitizedHtmlTemplate()); |
| 41 |
assertEquals('hello <b>world</b>', testDiv.innerHTML.toLowerCase()); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
function testRenderElement() { |
| 46 |
var testDiv = goog.dom.createElement(goog.dom.TagName.DIV); |
| 47 |
goog.soy.renderElement(testDiv, example.multiRootTemplate, {name: 'Boo'}); |
| 48 |
assertEquals('<div>Hello</div><div>Boo</div>', elementToInnerHtml(testDiv)); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function testRenderElementWithNoTemplateData() { |
| 53 |
var testDiv = goog.dom.createElement(goog.dom.TagName.DIV); |
| 54 |
goog.soy.renderElement(testDiv, example.noDataTemplate); |
| 55 |
assertEquals('<div>Hello</div>', elementToInnerHtml(testDiv)); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
function testRenderAsFragmentTextNode() { |
| 60 |
var fragment = |
| 61 |
goog.soy.renderAsFragment(example.textNodeTemplate, {name: 'Boo'}); |
| 62 |
assertEquals(goog.dom.NodeType.TEXT, fragment.nodeType); |
| 63 |
assertEquals('Boo', fragmentToHtml(fragment)); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
function testRenderAsFragmentInjectedData() { |
| 68 |
var fragment = goog.soy.renderAsFragment( |
| 69 |
example.injectedDataTemplate, {name: 'Boo'}, {name: 'ijBoo'}); |
| 70 |
assertEquals(goog.dom.NodeType.TEXT, fragment.nodeType); |
| 71 |
assertEquals('BooijBoo', fragmentToHtml(fragment)); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
function testRenderAsFragmentSingleRoot() { |
| 76 |
var fragment = |
| 77 |
goog.soy.renderAsFragment(example.singleRootTemplate, {name: 'Boo'}); |
| 78 |
assertEquals(goog.dom.NodeType.ELEMENT, fragment.nodeType); |
| 79 |
assertEquals(String(goog.dom.TagName.SPAN), fragment.tagName); |
| 80 |
assertEquals('Boo', fragment.innerHTML); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
function testRenderAsFragmentMultiRoot() { |
| 85 |
var fragment = |
| 86 |
goog.soy.renderAsFragment(example.multiRootTemplate, {name: 'Boo'}); |
| 87 |
assertEquals(goog.dom.NodeType.DOCUMENT_FRAGMENT, fragment.nodeType); |
| 88 |
assertEquals('<div>Hello</div><div>Boo</div>', fragmentToHtml(fragment)); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
function testRenderAsFragmentNoData() { |
| 93 |
var fragment = goog.soy.renderAsFragment(example.noDataTemplate); |
| 94 |
assertEquals(goog.dom.NodeType.ELEMENT, fragment.nodeType); |
| 95 |
assertEquals('<div>Hello</div>', fragmentToHtml(fragment)); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
function testRenderAsElementTextNode() { |
| 100 |
var elem = goog.soy.renderAsElement(example.textNodeTemplate, {name: 'Boo'}); |
| 101 |
assertEquals(goog.dom.NodeType.ELEMENT, elem.nodeType); |
| 102 |
assertEquals(String(goog.dom.TagName.DIV), elem.tagName); |
| 103 |
assertEquals('Boo', elementToInnerHtml(elem)); |
| 104 |
} |
| 105 |
|
| 106 |
function testRenderAsElementInjectedData() { |
| 107 |
var elem = goog.soy.renderAsElement( |
| 108 |
example.injectedDataTemplate, {name: 'Boo'}, {name: 'ijBoo'}); |
| 109 |
assertEquals(goog.dom.NodeType.ELEMENT, elem.nodeType); |
| 110 |
assertEquals(String(goog.dom.TagName.DIV), elem.tagName); |
| 111 |
assertEquals('BooijBoo', elementToInnerHtml(elem)); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
function testRenderAsElementSingleRoot() { |
| 116 |
var elem = |
| 117 |
goog.soy.renderAsElement(example.singleRootTemplate, {name: 'Boo'}); |
| 118 |
assertEquals(goog.dom.NodeType.ELEMENT, elem.nodeType); |
| 119 |
assertEquals(String(goog.dom.TagName.SPAN), elem.tagName); |
| 120 |
assertEquals('Boo', elementToInnerHtml(elem)); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
function testRenderAsElementMultiRoot() { |
| 125 |
var elem = goog.soy.renderAsElement(example.multiRootTemplate, {name: 'Boo'}); |
| 126 |
assertEquals(goog.dom.NodeType.ELEMENT, elem.nodeType); |
| 127 |
assertEquals(String(goog.dom.TagName.DIV), elem.tagName); |
| 128 |
assertEquals('<div>Hello</div><div>Boo</div>', elementToInnerHtml(elem)); |
| 129 |
} |
| 130 |
|
| 131 |
function testRenderAsElementWithNoData() { |
| 132 |
var elem = goog.soy.renderAsElement(example.noDataTemplate); |
| 133 |
assertEquals('Hello', elementToInnerHtml(elem)); |
| 134 |
} |
| 135 |
|
| 136 |
function testConvertToElement() { |
| 137 |
var elem = goog.soy.convertToElement(example.sanitizedHtmlTemplate()); |
| 138 |
assertEquals(goog.dom.NodeType.ELEMENT, elem.nodeType); |
| 139 |
assertEquals(String(goog.dom.TagName.DIV), elem.tagName); |
| 140 |
assertEquals('hello <b>world</b>', elem.innerHTML.toLowerCase()); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Asserts that the function throws an error for unsafe templates. |
| 146 |
* @param {Function} function Callback to test. |
| 147 |
*/ |
| 148 |
function assertUnsafeTemplateOutputErrorThrown(func) { |
| 149 |
stubs.set(goog.asserts, 'ENABLE_ASSERTS', true); |
| 150 |
assertContains( |
| 151 |
'Soy template output is unsafe for use as HTML', |
| 152 |
assertThrows(func).message); |
| 153 |
stubs.set(goog.asserts, 'ENABLE_ASSERTS', false); |
| 154 |
assertEquals('zSoyz', func()); |
| 155 |
} |
| 156 |
|
| 157 |
function testAllowButEscapeUnsanitizedText() { |
| 158 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 159 |
goog.soy.renderElement(div, example.unsanitizedTextTemplate); |
| 160 |
assertEquals('I <3 Puppies & Kittens', div.innerHTML); |
| 161 |
var fragment = goog.soy.renderAsFragment(example.unsanitizedTextTemplate); |
| 162 |
assertEquals('I <3 Puppies & Kittens', fragment.nodeValue); |
| 163 |
assertEquals( |
| 164 |
'I <3 Puppies & Kittens', |
| 165 |
goog.soy.renderAsElement(example.unsanitizedTextTemplate).innerHTML); |
| 166 |
} |
| 167 |
|
| 168 |
function testRejectSanitizedCss() { |
| 169 |
assertUnsafeTemplateOutputErrorThrown(function() { |
| 170 |
goog.soy.renderAsElement(example.sanitizedCssTemplate); |
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
function testRejectSanitizedCss() { |
| 175 |
assertUnsafeTemplateOutputErrorThrown(function() { |
| 176 |
return goog.soy |
| 177 |
.renderAsElement(example.templateSpoofingSanitizedContentString) |
| 178 |
.innerHTML; |
| 179 |
}); |
| 180 |
} |
| 181 |
|
| 182 |
function testRejectStringTemplatesWhenModeIsSet() { |
| 183 |
stubs.set(goog.soy, 'REQUIRE_STRICT_AUTOESCAPE', true); |
| 184 |
assertUnsafeTemplateOutputErrorThrown(function() { |
| 185 |
return goog.soy.renderAsElement(example.noDataTemplate).innerHTML; |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
function testAcceptSanitizedHtml() { |
| 190 |
assertEquals( |
| 191 |
'Hello World', |
| 192 |
goog.dom.getTextContent( |
| 193 |
goog.soy.renderAsElement(example.sanitizedHtmlTemplate))); |
| 194 |
} |
| 195 |
|
| 196 |
function testRejectSanitizedHtmlAttributes() { |
| 197 |
// Attributes context has nothing to do with html. |
| 198 |
assertUnsafeTemplateOutputErrorThrown(function() { |
| 199 |
return goog.dom.getTextContent( |
| 200 |
goog.soy.renderAsElement(example.sanitizedHtmlAttributesTemplate)); |
| 201 |
}); |
| 202 |
} |
| 203 |
|
| 204 |
function testAcceptNonObject() { |
| 205 |
// Some templates, or things that spoof templates in unit tests, might return |
| 206 |
// non-strings in unusual cases. |
| 207 |
assertEquals( |
| 208 |
'null', goog.dom.getTextContent( |
| 209 |
goog.soy.renderAsElement(goog.functions.constant(null)))); |
| 210 |
} |
| 211 |
|
| 212 |
function testDebugAssertionWithBadFirstTag() { |
| 213 |
try { |
| 214 |
goog.soy.renderAsElement(example.tableRowTemplate); |
| 215 |
// Expect no exception in production code. |
| 216 |
assert(!goog.DEBUG); |
| 217 |
} catch (e) { |
| 218 |
// Expect exception in debug code. |
| 219 |
assert(goog.DEBUG); |
| 220 |
// Make sure to let the developer know which tag caused the problem. |
| 221 |
assertContains('<tr>', e.message); |
| 222 |
} |
| 223 |
|
| 224 |
try { |
| 225 |
goog.soy.renderAsFragment(example.tableRowTemplate); |
| 226 |
// Expect no exception in production code. |
| 227 |
assert(!goog.DEBUG); |
| 228 |
} catch (e) { |
| 229 |
// Expect exception in debug code. |
| 230 |
assert(goog.DEBUG); |
| 231 |
// Make sure to let the developer know which tag caused the problem. |
| 232 |
assertContains('<tr>', e.message); |
| 233 |
} |
| 234 |
|
| 235 |
try { |
| 236 |
goog.soy.renderAsElement(example.colGroupTemplateCaps); |
| 237 |
// Expect no exception in production code. |
| 238 |
assert(!goog.DEBUG); |
| 239 |
} catch (e) { |
| 240 |
// Expect exception in debug code. |
| 241 |
assert(goog.DEBUG); |
| 242 |
// Make sure to let the developer know which tag caused the problem. |
| 243 |
assertContains('<COLGROUP>', e.message); |
| 244 |
} |
| 245 |
} |