File: soy_testhelper.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (350 lines) | stat: -rw-r--r-- 9,711 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Provides test helpers for Soy tests.
 * @author chrishenry@google.com (Chris Henry)
 */

goog.provide('goog.soy.testHelper');
goog.setTestOnly('goog.soy.testHelper');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.i18n.bidi.Dir');
goog.require('goog.soy.data.SanitizedContent');
goog.require('goog.soy.data.SanitizedContentKind');
goog.require('goog.soy.data.SanitizedCss');
goog.require('goog.soy.data.SanitizedTrustedResourceUri');
goog.require('goog.string');
goog.require('goog.userAgent');



/**
 * Instantiable subclass of SanitizedContent.
 *
 * This is a spoof for sanitized content that isn't robust enough to get
 * through Soy's escaping functions but is good enough for the checks here.
 *
 * @constructor
 * @param {string} content The text.
 * @param {goog.soy.data.SanitizedContentKind} kind The kind of safe content.
 * @extends {goog.soy.data.SanitizedContent}
 * @suppress {missingProvide}
 */
function SanitizedContentSubclass(content, kind) {
  // IMPORTANT! No superclass chaining to avoid exception being thrown.
  this.content = content;
  this.contentKind = kind;
}
goog.inherits(SanitizedContentSubclass, goog.soy.data.SanitizedContent);


/**
 * Instantiable subclass of SanitizedCss.
 * @param {string} content
 * @constructor
 * @extends {goog.soy.data.SanitizedCss}
 * @suppress {missingProvide}
 */
function SanitizedCssSubclass(content) {
  // IMPORTANT! No superclass chaining to avoid exception being thrown.
  this.content = content;
  this.contentKind = goog.soy.data.SanitizedContentKind.CSS;
}
goog.inherits(SanitizedCssSubclass, goog.soy.data.SanitizedCss);


/**
 * @param {string} content The text.
 * @param {goog.soy.data.SanitizedContentKind|string} kind The kind of safe
 *     content.
 * @return {!SanitizedContentSubclass}
 */
function makeSanitizedContent(content, kind) {
  return new SanitizedContentSubclass(
      content,
      /** @type {goog.soy.data.SanitizedContentKind} */ (kind));
}



/**
 * Instantiable subclass of SanitizedTrustedResourceUri.
 *
 * This is a spoof for trusted resource URI that isn't robust enough to get
 * through Soy's escaping functions but is good enough for the checks here.
 *
 * @param {string} content The URI.
 * @constructor
 * @extends {goog.soy.data.SanitizedTrustedResourceUri}
 * @suppress {missingProvide}
 * @final
 */
function SanitizedTrustedResourceUriSubclass(content) {
  // IMPORTANT! No superclass chaining to avoid exception being thrown.
  this.content = content;
  this.contentKind = goog.soy.data.SanitizedContentKind.TRUSTED_RESOURCE_URI;
}
goog.inherits(
    SanitizedTrustedResourceUriSubclass,
    goog.soy.data.SanitizedTrustedResourceUri);



//
// Fake Soy-generated template functions.
//

var example = {};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.textNodeTemplate = function(data, opt_sb, opt_injectedData) {
  assertNotNull(data);
  assertNotUndefined(data);
  return goog.string.htmlEscape(data.name);
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.singleRootTemplate = function(data, opt_sb, opt_injectedData) {
  assertNotNull(data);
  assertNotUndefined(data);
  return '<span>' + goog.string.htmlEscape(data.name) + '</span>';
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.multiRootTemplate = function(data, opt_sb, opt_injectedData) {
  assertNotNull(data);
  assertNotUndefined(data);
  return '<div>Hello</div><div>' + goog.string.htmlEscape(data.name) + '</div>';
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.injectedDataTemplate = function(data, opt_sb, opt_injectedData) {
  assertNotNull(data);
  assertNotUndefined(data);
  return goog.string.htmlEscape(data.name) +
      goog.string.htmlEscape(opt_injectedData.name);
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.noDataTemplate = function(data, opt_sb, opt_injectedData) {
  assertNotNull(data);
  assertNotUndefined(data);
  return '<div>Hello</div>';
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.sanitizedHtmlTemplate = function(data, opt_sb, opt_injectedData) {
  // Test the SanitizedContent constructor.
  var sanitized = makeSanitizedContent(
      'Hello <b>World</b>', goog.soy.data.SanitizedContentKind.HTML);
  sanitized.contentDir = goog.i18n.bidi.Dir.LTR;
  return sanitized;
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.sanitizedHtmlAttributesTemplate = function(
    data, opt_sb, opt_injectedData) {
  return makeSanitizedContent(
      'foo="bar"', goog.soy.data.SanitizedContentKind.ATTRIBUTES);
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.sanitizedSmsUrlTemplate = function(data, opt_sb, opt_injectedData) {
  // Test the SanitizedContent constructor.
  var sanitized = makeSanitizedContent(
      'sms:123456789', goog.soy.data.SanitizedContentKind.URI);
  return sanitized;
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.sanitizedHttpUrlTemplate = function(data, opt_sb, opt_injectedData) {
  // Test the SanitizedContent constructor.
  var sanitized = makeSanitizedContent(
      'https://google.com/foo?n=917', goog.soy.data.SanitizedContentKind.URI);
  return sanitized;
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {!goog.soy.data.SanitizedTrustedResourceUri}
 */
example.sanitizedTrustedResourceUriTemplate = function(
    data, opt_sb, opt_injectedData) {
  return new SanitizedTrustedResourceUriSubclass('https://google.com/a.js');
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {!goog.soy.data.SanitizedCss}
 */
example.sanitizedCssTemplate = function(data, opt_sb, opt_injectedData) {
  return new SanitizedCssSubclass('html{display:none}');
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.unsanitizedTextTemplate = function(data, opt_sb, opt_injectedData) {
  return makeSanitizedContent(
      'I <3 Puppies & Kittens', goog.soy.data.SanitizedContentKind.TEXT);
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {?Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.sanitizedUriTemplate = function(data, opt_sb, opt_injectedData) {
  return makeSanitizedContent(
      'https://example.com', goog.soy.data.SanitizedContentKind.URI);
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {!SanitizedContentSubclass}
 */
example.templateSpoofingSanitizedContentString = function(
    data, opt_sb, opt_injectedData) {
  return makeSanitizedContent(
      'Hello World',
      // This is to ensure we're using triple-equals against a unique Javascript
      // object.  For example, in Javascript, consider ({}) == '[Object object]'
      // is true.
      goog.soy.data.SanitizedContentKind.HTML.toString());
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.tableRowTemplate = function(data, opt_sb, opt_injectedData) {
  return '<tr><td></td></tr>';
};


/**
 * @param {{name: string}} data
 * @param {null=} opt_sb
 * @param {Object<string, *>=} opt_injectedData
 * @return {string}
 */
example.colGroupTemplateCaps = function(data, opt_sb, opt_injectedData) {
  return '<COLGROUP></COLGROUP>';
};


//
// Test helper functions.
//


/**
 * Retrieves the content of document fragment as HTML.
 * @param {Node} fragment The document fragment.
 * @return {string} Content of the document fragment as HTML.
 */
function fragmentToHtml(fragment) {
  var testDiv = goog.dom.createElement(goog.dom.TagName.DIV);
  testDiv.appendChild(fragment);
  return elementToInnerHtml(testDiv);
}


/**
 * Retrieves the content of an element as HTML.
 * @param {Element} elem The element.
 * @return {string} Content of the element as HTML.
 */
function elementToInnerHtml(elem) {
  var innerHtml = elem.innerHTML;
  if (goog.userAgent.IE) {
    innerHtml = innerHtml.replace(/DIV/g, 'div').replace(/\s/g, '');
  }
  return innerHtml;
}