Debian Codegraph @

closure-library/closure/goog/soy/soy_testhelper.js

raw
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 /**
16 * @fileoverview Provides test helpers for Soy tests.
17 * @author [email protected] (Chris Henry)
18 */
19
20 goog.provide('goog.soy.testHelper');
21 goog.setTestOnly('goog.soy.testHelper');
22
23 goog.require('goog.dom');
24 goog.require('goog.dom.TagName');
25 goog.require('goog.i18n.bidi.Dir');
26 goog.require('goog.soy.data.SanitizedContent');
27 goog.require('goog.soy.data.SanitizedContentKind');
28 goog.require('goog.soy.data.SanitizedCss');
29 goog.require('goog.soy.data.SanitizedTrustedResourceUri');
30 goog.require('goog.string');
31 goog.require('goog.userAgent');
32
33
34
35 /**
36 * Instantiable subclass of SanitizedContent.
37 *
38 * This is a spoof for sanitized content that isn't robust enough to get
39 * through Soy's escaping functions but is good enough for the checks here.
40 *
41 * @constructor
42 * @param {string} content The text.
43 * @param {goog.soy.data.SanitizedContentKind} kind The kind of safe content.
44 * @extends {goog.soy.data.SanitizedContent}
45 * @suppress {missingProvide}
46 */
47 function SanitizedContentSubclass(content, kind) {
48 // IMPORTANT! No superclass chaining to avoid exception being thrown.
49 this.content = content;
50 this.contentKind = kind;
51 }
52 goog.inherits(SanitizedContentSubclass, goog.soy.data.SanitizedContent);
53
54
55 /**
56 * Instantiable subclass of SanitizedCss.
57 * @param {string} content
58 * @constructor
59 * @extends {goog.soy.data.SanitizedCss}
60 * @suppress {missingProvide}
61 */
62 function SanitizedCssSubclass(content) {
63 // IMPORTANT! No superclass chaining to avoid exception being thrown.
64 this.content = content;
65 this.contentKind = goog.soy.data.SanitizedContentKind.CSS;
66 }
67 goog.inherits(SanitizedCssSubclass, goog.soy.data.SanitizedCss);
68
69
70 /**
71 * @param {string} content The text.
72 * @param {goog.soy.data.SanitizedContentKind|string} kind The kind of safe
73 * content.
74 * @return {!SanitizedContentSubclass}
75 */
76 function makeSanitizedContent(content, kind) {
77 return new SanitizedContentSubclass(
78 content,
79 /** @type {goog.soy.data.SanitizedContentKind} */ (kind));
80 }
81
82
83
84 /**
85 * Instantiable subclass of SanitizedTrustedResourceUri.
86 *
87 * This is a spoof for trusted resource URI that isn't robust enough to get
88 * through Soy's escaping functions but is good enough for the checks here.
89 *
90 * @param {string} content The URI.
91 * @constructor
92 * @extends {goog.soy.data.SanitizedTrustedResourceUri}
93 * @suppress {missingProvide}
94 * @final
95 */
96 function SanitizedTrustedResourceUriSubclass(content) {
97 // IMPORTANT! No superclass chaining to avoid exception being thrown.
98 this.content = content;
99 this.contentKind = goog.soy.data.SanitizedContentKind.TRUSTED_RESOURCE_URI;
100 }
101 goog.inherits(
102 SanitizedTrustedResourceUriSubclass,
103 goog.soy.data.SanitizedTrustedResourceUri);
104
105
106
107 //
108 // Fake Soy-generated template functions.
109 //
110
111 var example = {};
112
113
114 /**
115 * @param {{name: string}} data
116 * @param {null=} opt_sb
117 * @param {?Object<string, *>=} opt_injectedData
118 * @return {string}
119 */
120 example.textNodeTemplate = function(data, opt_sb, opt_injectedData) {
121 assertNotNull(data);
122 assertNotUndefined(data);
123 return goog.string.htmlEscape(data.name);
124 };
125
126
127 /**
128 * @param {{name: string}} data
129 * @param {null=} opt_sb
130 * @param {?Object<string, *>=} opt_injectedData
131 * @return {string}
132 */
133 example.singleRootTemplate = function(data, opt_sb, opt_injectedData) {
134 assertNotNull(data);
135 assertNotUndefined(data);
136 return '<span>' + goog.string.htmlEscape(data.name) + '</span>';
137 };
138
139
140 /**
141 * @param {{name: string}} data
142 * @param {null=} opt_sb
143 * @param {?Object<string, *>=} opt_injectedData
144 * @return {string}
145 */
146 example.multiRootTemplate = function(data, opt_sb, opt_injectedData) {
147 assertNotNull(data);
148 assertNotUndefined(data);
149 return '<div>Hello</div><div>' + goog.string.htmlEscape(data.name) + '</div>';
150 };
151
152
153 /**
154 * @param {{name: string}} data
155 * @param {null=} opt_sb
156 * @param {?Object<string, *>=} opt_injectedData
157 * @return {string}
158 */
159 example.injectedDataTemplate = function(data, opt_sb, opt_injectedData) {
160 assertNotNull(data);
161 assertNotUndefined(data);
162 return goog.string.htmlEscape(data.name) +
163 goog.string.htmlEscape(opt_injectedData.name);
164 };
165
166
167 /**
168 * @param {{name: string}} data
169 * @param {null=} opt_sb
170 * @param {Object<string, *>=} opt_injectedData
171 * @return {string}
172 */
173 example.noDataTemplate = function(data, opt_sb, opt_injectedData) {
174 assertNotNull(data);
175 assertNotUndefined(data);
176 return '<div>Hello</div>';
177 };
178
179
180 /**
181 * @param {{name: string}} data
182 * @param {null=} opt_sb
183 * @param {Object<string, *>=} opt_injectedData
184 * @return {!SanitizedContentSubclass}
185 */
186 example.sanitizedHtmlTemplate = function(data, opt_sb, opt_injectedData) {
187 // Test the SanitizedContent constructor.
188 var sanitized = makeSanitizedContent(
189 'Hello <b>World</b>', goog.soy.data.SanitizedContentKind.HTML);
190 sanitized.contentDir = goog.i18n.bidi.Dir.LTR;
191 return sanitized;
192 };
193
194
195 /**
196 * @param {{name: string}} data
197 * @param {null=} opt_sb
198 * @param {Object<string, *>=} opt_injectedData
199 * @return {!SanitizedContentSubclass}
200 */
201 example.sanitizedHtmlAttributesTemplate = function(
202 data, opt_sb, opt_injectedData) {
203 return makeSanitizedContent(
204 'foo="bar"', goog.soy.data.SanitizedContentKind.ATTRIBUTES);
205 };
206
207
208 /**
209 * @param {{name: string}} data
210 * @param {null=} opt_sb
211 * @param {?Object<string, *>=} opt_injectedData
212 * @return {!SanitizedContentSubclass}
213 */
214 example.sanitizedSmsUrlTemplate = function(data, opt_sb, opt_injectedData) {
215 // Test the SanitizedContent constructor.
216 var sanitized = makeSanitizedContent(
217 'sms:123456789', goog.soy.data.SanitizedContentKind.URI);
218 return sanitized;
219 };
220
221
222 /**
223 * @param {{name: string}} data
224 * @param {null=} opt_sb
225 * @param {?Object<string, *>=} opt_injectedData
226 * @return {!SanitizedContentSubclass}
227 */
228 example.sanitizedHttpUrlTemplate = function(data, opt_sb, opt_injectedData) {
229 // Test the SanitizedContent constructor.
230 var sanitized = makeSanitizedContent(
231 'https://google.com/foo?n=917', goog.soy.data.SanitizedContentKind.URI);
232 return sanitized;
233 };
234
235
236 /**
237 * @param {{name: string}} data
238 * @param {null=} opt_sb
239 * @param {?Object<string, *>=} opt_injectedData
240 * @return {!goog.soy.data.SanitizedTrustedResourceUri}
241 */
242 example.sanitizedTrustedResourceUriTemplate = function(
243 data, opt_sb, opt_injectedData) {
244 return new SanitizedTrustedResourceUriSubclass('https://google.com/a.js');
245 };
246
247
248 /**
249 * @param {{name: string}} data
250 * @param {null=} opt_sb
251 * @param {Object<string, *>=} opt_injectedData
252 * @return {!goog.soy.data.SanitizedCss}
253 */
254 example.sanitizedCssTemplate = function(data, opt_sb, opt_injectedData) {
255 return new SanitizedCssSubclass('html{display:none}');
256 };
257
258
259 /**
260 * @param {{name: string}} data
261 * @param {null=} opt_sb
262 * @param {Object<string, *>=} opt_injectedData
263 * @return {!SanitizedContentSubclass}
264 */
265 example.unsanitizedTextTemplate = function(data, opt_sb, opt_injectedData) {
266 return makeSanitizedContent(
267 'I <3 Puppies & Kittens', goog.soy.data.SanitizedContentKind.TEXT);
268 };
269
270
271 /**
272 * @param {{name: string}} data
273 * @param {null=} opt_sb
274 * @param {?Object<string, *>=} opt_injectedData
275 * @return {!SanitizedContentSubclass}
276 */
277 example.sanitizedUriTemplate = function(data, opt_sb, opt_injectedData) {
278 return makeSanitizedContent(
279 'https://example.com', goog.soy.data.SanitizedContentKind.URI);
280 };
281
282
283 /**
284 * @param {{name: string}} data
285 * @param {null=} opt_sb
286 * @param {Object<string, *>=} opt_injectedData
287 * @return {!SanitizedContentSubclass}
288 */
289 example.templateSpoofingSanitizedContentString = function(
290 data, opt_sb, opt_injectedData) {
291 return makeSanitizedContent(
292 'Hello World',
293 // This is to ensure we're using triple-equals against a unique Javascript
294 // object. For example, in Javascript, consider ({}) == '[Object object]'
295 // is true.
296 goog.soy.data.SanitizedContentKind.HTML.toString());
297 };
298
299
300 /**
301 * @param {{name: string}} data
302 * @param {null=} opt_sb
303 * @param {Object<string, *>=} opt_injectedData
304 * @return {string}
305 */
306 example.tableRowTemplate = function(data, opt_sb, opt_injectedData) {
307 return '<tr><td></td></tr>';
308 };
309
310
311 /**
312 * @param {{name: string}} data
313 * @param {null=} opt_sb
314 * @param {Object<string, *>=} opt_injectedData
315 * @return {string}
316 */
317 example.colGroupTemplateCaps = function(data, opt_sb, opt_injectedData) {
318 return '<COLGROUP></COLGROUP>';
319 };
320
321
322 //
323 // Test helper functions.
324 //
325
326
327 /**
328 * Retrieves the content of document fragment as HTML.
329 * @param {Node} fragment The document fragment.
330 * @return {string} Content of the document fragment as HTML.
331 */
332 function fragmentToHtml(fragment) {
333 var testDiv = goog.dom.createElement(goog.dom.TagName.DIV);
334 testDiv.appendChild(fragment);
335 return elementToInnerHtml(testDiv);
336 }
337
338
339 /**
340 * Retrieves the content of an element as HTML.
341 * @param {Element} elem The element.
342 * @return {string} Content of the element as HTML.
343 */
344 function elementToInnerHtml(elem) {
345 var innerHtml = elem.innerHTML;
346 if (goog.userAgent.IE) {
347 innerHtml = innerHtml.replace(/DIV/g, 'div').replace(/\s/g, '');
348 }
349 return innerHtml;
350 }