yellow-brick-road 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2233 @@
1
+ /*
2
+ * Copyright 2008 Google Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * @fileoverview
19
+ * Utility functions and classes for Soy.
20
+ *
21
+ * <p>
22
+ * The top portion of this file contains utilities for Soy users:<ul>
23
+ * <li> soy.StringBuilder: Compatible with the 'stringbuilder' code style.
24
+ * <li> soy.renderElement: Render template and set as innerHTML of an element.
25
+ * <li> soy.renderAsFragment: Render template and return as HTML fragment.
26
+ * </ul>
27
+ *
28
+ * <p>
29
+ * The bottom portion of this file contains utilities that should only be called
30
+ * by Soy-generated JS code. Please do not use these functions directly from
31
+ * your hand-writen code. Their names all start with '$$'.
32
+ *
33
+ */
34
+
35
+
36
+ // COPIED FROM nogoog_shim.js
37
+
38
+ // Create closure namespaces.
39
+ var goog = goog || {};
40
+
41
+
42
+ goog.inherits = function(childCtor, parentCtor) {
43
+ /** @constructor */
44
+ function tempCtor() {}
45
+ tempCtor.prototype = parentCtor.prototype;
46
+ childCtor.superClass_ = parentCtor.prototype;
47
+ childCtor.prototype = new tempCtor();
48
+ childCtor.prototype.constructor = childCtor;
49
+ };
50
+
51
+
52
+ // Just enough browser detection for this file.
53
+ if (!goog.userAgent) {
54
+ goog.userAgent = (function() {
55
+ var userAgent = "";
56
+ if ("undefined" !== typeof navigator && navigator
57
+ && "string" == typeof navigator.userAgent) {
58
+ userAgent = navigator.userAgent;
59
+ }
60
+ var isOpera = userAgent.indexOf('Opera') == 0;
61
+ return {
62
+ /**
63
+ * @type {boolean}
64
+ */
65
+ HAS_JSCRIPT: typeof 'ScriptEngine' in this,
66
+ /**
67
+ * @type {boolean}
68
+ */
69
+ IS_OPERA: isOpera,
70
+ /**
71
+ * @type {boolean}
72
+ */
73
+ IS_IE: !isOpera && userAgent.indexOf('MSIE') != -1,
74
+ /**
75
+ * @type {boolean}
76
+ */
77
+ IS_WEBKIT: !isOpera && userAgent.indexOf('WebKit') != -1
78
+ };
79
+ })();
80
+ }
81
+
82
+ if (!goog.asserts) {
83
+ goog.asserts = {
84
+ /**
85
+ * @param {...*} var_args
86
+ */
87
+ fail: function (var_args) {}
88
+ };
89
+ }
90
+
91
+
92
+ // Stub out the document wrapper used by renderAs*.
93
+ if (!goog.dom) {
94
+ goog.dom = {};
95
+ /**
96
+ * @param {Document=} d
97
+ * @constructor
98
+ */
99
+ goog.dom.DomHelper = function(d) {
100
+ this.document_ = d || document;
101
+ };
102
+ /**
103
+ * @return {!Document}
104
+ */
105
+ goog.dom.DomHelper.prototype.getDocument = function() {
106
+ return this.document_;
107
+ };
108
+ /**
109
+ * Creates a new element.
110
+ * @param {string} name Tag name.
111
+ * @return {!Element}
112
+ */
113
+ goog.dom.DomHelper.prototype.createElement = function(name) {
114
+ return this.document_.createElement(name);
115
+ };
116
+ /**
117
+ * Creates a new document fragment.
118
+ * @return {!DocumentFragment}
119
+ */
120
+ goog.dom.DomHelper.prototype.createDocumentFragment = function() {
121
+ return this.document_.createDocumentFragment();
122
+ };
123
+ }
124
+
125
+
126
+ if (!goog.format) {
127
+ goog.format = {
128
+ insertWordBreaks: function(str, maxCharsBetweenWordBreaks) {
129
+ str = String(str);
130
+
131
+ var resultArr = [];
132
+ var resultArrLen = 0;
133
+
134
+ // These variables keep track of important state inside str.
135
+ var isInTag = false; // whether we're inside an HTML tag
136
+ var isMaybeInEntity = false; // whether we might be inside an HTML entity
137
+ var numCharsWithoutBreak = 0; // number of chars since last word break
138
+ var flushIndex = 0; // index of first char not yet flushed to resultArr
139
+
140
+ for (var i = 0, n = str.length; i < n; ++i) {
141
+ var charCode = str.charCodeAt(i);
142
+
143
+ // If hit maxCharsBetweenWordBreaks, and not space next, then add <wbr>.
144
+ if (numCharsWithoutBreak >= maxCharsBetweenWordBreaks &&
145
+ // space
146
+ charCode != 32) {
147
+ resultArr[resultArrLen++] = str.substring(flushIndex, i);
148
+ flushIndex = i;
149
+ resultArr[resultArrLen++] = goog.format.WORD_BREAK;
150
+ numCharsWithoutBreak = 0;
151
+ }
152
+
153
+ if (isInTag) {
154
+ // If inside an HTML tag and we see '>', it's the end of the tag.
155
+ if (charCode == 62) {
156
+ isInTag = false;
157
+ }
158
+
159
+ } else if (isMaybeInEntity) {
160
+ switch (charCode) {
161
+ // Inside an entity, a ';' is the end of the entity.
162
+ // The entity that just ended counts as one char, so increment
163
+ // numCharsWithoutBreak.
164
+ case 59: // ';'
165
+ isMaybeInEntity = false;
166
+ ++numCharsWithoutBreak;
167
+ break;
168
+ // If maybe inside an entity and we see '<', we weren't actually in
169
+ // an entity. But now we're inside and HTML tag.
170
+ case 60: // '<'
171
+ isMaybeInEntity = false;
172
+ isInTag = true;
173
+ break;
174
+ // If maybe inside an entity and we see ' ', we weren't actually in
175
+ // an entity. Just correct the state and reset the
176
+ // numCharsWithoutBreak since we just saw a space.
177
+ case 32: // ' '
178
+ isMaybeInEntity = false;
179
+ numCharsWithoutBreak = 0;
180
+ break;
181
+ }
182
+
183
+ } else { // !isInTag && !isInEntity
184
+ switch (charCode) {
185
+ // When not within a tag or an entity and we see '<', we're now
186
+ // inside an HTML tag.
187
+ case 60: // '<'
188
+ isInTag = true;
189
+ break;
190
+ // When not within a tag or an entity and we see '&', we might be
191
+ // inside an entity.
192
+ case 38: // '&'
193
+ isMaybeInEntity = true;
194
+ break;
195
+ // When we see a space, reset the numCharsWithoutBreak count.
196
+ case 32: // ' '
197
+ numCharsWithoutBreak = 0;
198
+ break;
199
+ // When we see a non-space, increment the numCharsWithoutBreak.
200
+ default:
201
+ ++numCharsWithoutBreak;
202
+ break;
203
+ }
204
+ }
205
+ }
206
+
207
+ // Flush the remaining chars at the end of the string.
208
+ resultArr[resultArrLen++] = str.substring(flushIndex);
209
+
210
+ return resultArr.join('');
211
+ },
212
+ /**
213
+ * String inserted as a word break by insertWordBreaks(). Safari requires
214
+ * <wbr></wbr>, Opera needs the 'shy' entity, though this will give a
215
+ * visible hyphen at breaks. Other browsers just use <wbr>.
216
+ * @type {string}
217
+ * @private
218
+ */
219
+ WORD_BREAK: goog.userAgent.IS_WEBKIT
220
+ ? '<wbr></wbr>' : goog.userAgent.IS_OPERA ? '&shy;' : '<wbr>'
221
+ };
222
+ }
223
+
224
+
225
+ if (!goog.i18n) {
226
+ goog.i18n = {
227
+ bidi: {
228
+ /**
229
+ * Check the directionality of a piece of text, return true if the piece
230
+ * of text should be laid out in RTL direction.
231
+ * @param {string} text The piece of text that need to be detected.
232
+ * @param {boolean=} opt_isHtml Whether {@code text} is HTML/HTML-escaped.
233
+ * Default: false.
234
+ * @return {boolean}
235
+ * @private
236
+ */
237
+ detectRtlDirectionality: function(text, opt_isHtml) {
238
+ text = soyshim.$$bidiStripHtmlIfNecessary_(text, opt_isHtml);
239
+ return soyshim.$$bidiRtlWordRatio_(text)
240
+ > soyshim.$$bidiRtlDetectionThreshold_;
241
+ }
242
+ }
243
+ };
244
+ }
245
+
246
+ /**
247
+ * Directionality enum.
248
+ * @enum {number}
249
+ */
250
+ goog.i18n.bidi.Dir = {
251
+ RTL: -1,
252
+ UNKNOWN: 0,
253
+ LTR: 1
254
+ };
255
+
256
+
257
+ /**
258
+ * Convert a directionality given in various formats to a goog.i18n.bidi.Dir
259
+ * constant. Useful for interaction with different standards of directionality
260
+ * representation.
261
+ *
262
+ * @param {goog.i18n.bidi.Dir|number|boolean} givenDir Directionality given in
263
+ * one of the following formats:
264
+ * 1. A goog.i18n.bidi.Dir constant.
265
+ * 2. A number (positive = LRT, negative = RTL, 0 = unknown).
266
+ * 3. A boolean (true = RTL, false = LTR).
267
+ * @return {goog.i18n.bidi.Dir} A goog.i18n.bidi.Dir constant matching the given
268
+ * directionality.
269
+ */
270
+ goog.i18n.bidi.toDir = function(givenDir) {
271
+ if (typeof givenDir == 'number') {
272
+ return givenDir > 0 ? goog.i18n.bidi.Dir.LTR :
273
+ givenDir < 0 ? goog.i18n.bidi.Dir.RTL : goog.i18n.bidi.Dir.UNKNOWN;
274
+ } else {
275
+ return givenDir ? goog.i18n.bidi.Dir.RTL : goog.i18n.bidi.Dir.LTR;
276
+ }
277
+ };
278
+
279
+
280
+ /**
281
+ * Utility class for formatting text for display in a potentially
282
+ * opposite-directionality context without garbling. Provides the following
283
+ * functionality:
284
+ *
285
+ * @param {goog.i18n.bidi.Dir|number|boolean} dir The context
286
+ * directionality as a number
287
+ * (positive = LRT, negative = RTL, 0 = unknown).
288
+ * @constructor
289
+ */
290
+ goog.i18n.BidiFormatter = function(dir) {
291
+ this.dir_ = goog.i18n.bidi.toDir(dir);
292
+ };
293
+
294
+
295
+ /**
296
+ * Returns "dir=ltr" or "dir=rtl", depending on {@code text}'s estimated
297
+ * directionality, if it is not the same as the context directionality.
298
+ * Otherwise, returns the empty string.
299
+ *
300
+ * @param {string} text Text whose directionality is to be estimated.
301
+ * @param {boolean=} opt_isHtml Whether {@code text} is HTML / HTML-escaped.
302
+ * Default: false.
303
+ * @return {string} "dir=rtl" for RTL text in non-RTL context; "dir=ltr" for LTR
304
+ * text in non-LTR context; else, the empty string.
305
+ */
306
+ goog.i18n.BidiFormatter.prototype.dirAttr = function (text, opt_isHtml) {
307
+ var dir = soy.$$bidiTextDir(text, opt_isHtml);
308
+ return dir && dir != this.dir_ ? dir < 0 ? 'dir=rtl' : 'dir=ltr' : '';
309
+ };
310
+
311
+ /**
312
+ * Returns the trailing horizontal edge, i.e. "right" or "left", depending on
313
+ * the global bidi directionality.
314
+ * @return {string} "left" for RTL context and "right" otherwise.
315
+ */
316
+ goog.i18n.BidiFormatter.prototype.endEdge = function () {
317
+ return this.dir_ < 0 ? 'left' : 'right';
318
+ };
319
+
320
+ /**
321
+ * Returns the Unicode BiDi mark matching the context directionality (LRM for
322
+ * LTR context directionality, RLM for RTL context directionality), or the
323
+ * empty string for neutral / unknown context directionality.
324
+ *
325
+ * @return {string} LRM for LTR context directionality and RLM for RTL context
326
+ * directionality.
327
+ */
328
+ goog.i18n.BidiFormatter.prototype.mark = function () {
329
+ return (
330
+ (this.dir_ > 0) ? '\u200E' /*LRM*/ :
331
+ (this.dir_ < 0) ? '\u200F' /*RLM*/ :
332
+ '');
333
+ };
334
+
335
+ /**
336
+ * Returns a Unicode BiDi mark matching the context directionality (LRM or RLM)
337
+ * if the directionality or the exit directionality of {@code text} are opposite
338
+ * to the context directionality. Otherwise returns the empty string.
339
+ *
340
+ * @param {string} text The input text.
341
+ * @param {boolean=} opt_isHtml Whether {@code text} is HTML / HTML-escaped.
342
+ * Default: false.
343
+ * @return {string} A Unicode bidi mark matching the global directionality or
344
+ * the empty string.
345
+ */
346
+ goog.i18n.BidiFormatter.prototype.markAfter = function (text, opt_isHtml) {
347
+ var dir = soy.$$bidiTextDir(text, opt_isHtml);
348
+ return soyshim.$$bidiMarkAfterKnownDir_(this.dir_, dir, text, opt_isHtml);
349
+ };
350
+
351
+ /**
352
+ * Formats a string of unknown directionality for use in HTML output of the
353
+ * context directionality, so an opposite-directionality string is neither
354
+ * garbled nor garbles what follows it.
355
+ *
356
+ * @param {string} str The input text.
357
+ * @param {boolean=} placeholder This argument exists for consistency with the
358
+ * Closure Library. Specifying it has no effect.
359
+ * @return {string} Input text after applying the above processing.
360
+ */
361
+ goog.i18n.BidiFormatter.prototype.spanWrap = function(str, placeholder) {
362
+ str = String(str);
363
+ var textDir = soy.$$bidiTextDir(str, true);
364
+ var reset = soyshim.$$bidiMarkAfterKnownDir_(this.dir_, textDir, str, true);
365
+ if (textDir > 0 && this.dir_ <= 0) {
366
+ str = '<span dir=ltr>' + str + '</span>';
367
+ } else if (textDir < 0 && this.dir_ >= 0) {
368
+ str = '<span dir=rtl>' + str + '</span>';
369
+ }
370
+ return str + reset;
371
+ };
372
+
373
+ /**
374
+ * Returns the leading horizontal edge, i.e. "left" or "right", depending on
375
+ * the global bidi directionality.
376
+ * @return {string} "right" for RTL context and "left" otherwise.
377
+ */
378
+ goog.i18n.BidiFormatter.prototype.startEdge = function () {
379
+ return this.dir_ < 0 ? 'right' : 'left';
380
+ };
381
+
382
+ /**
383
+ * Formats a string of unknown directionality for use in plain-text output of
384
+ * the context directionality, so an opposite-directionality string is neither
385
+ * garbled nor garbles what follows it.
386
+ * As opposed to {@link #spanWrap}, this makes use of unicode BiDi formatting
387
+ * characters. In HTML, its *only* valid use is inside of elements that do not
388
+ * allow mark-up, e.g. an 'option' tag.
389
+ *
390
+ * @param {string} str The input text.
391
+ * @param {boolean=} placeholder This argument exists for consistency with the
392
+ * Closure Library. Specifying it has no effect.
393
+ * @return {string} Input text after applying the above processing.
394
+ */
395
+ goog.i18n.BidiFormatter.prototype.unicodeWrap = function(str, placeholder) {
396
+ str = String(str);
397
+ var textDir = soy.$$bidiTextDir(str, true);
398
+ var reset = soyshim.$$bidiMarkAfterKnownDir_(this.dir_, textDir, str, true);
399
+ if (textDir > 0 && this.dir_ <= 0) {
400
+ str = '\u202A' + str + '\u202C';
401
+ } else if (textDir < 0 && this.dir_ >= 0) {
402
+ str = '\u202B' + str + '\u202C';
403
+ }
404
+ return str + reset;
405
+ };
406
+
407
+
408
+ goog.string = {
409
+
410
+ /**
411
+ * Converts \r\n, \r, and \n to <br>s
412
+ * @param {*} str The string in which to convert newlines.
413
+ * @param {boolean=} opt_xml Whether to use XML compatible tags.
414
+ * @return {string} A copy of {@code str} with converted newlines.
415
+ */
416
+ newLineToBr: function(str, opt_xml) {
417
+
418
+ str = String(str);
419
+
420
+ // This quick test helps in the case when there are no chars to replace,
421
+ // in the worst case this makes barely a difference to the time taken.
422
+ if (!goog.string.NEWLINE_TO_BR_RE_.test(str)) {
423
+ return str;
424
+ }
425
+
426
+ return str.replace(/(\r\n|\r|\n)/g, opt_xml ? '<br />' : '<br>');
427
+ },
428
+ urlEncode: encodeURIComponent,
429
+ /**
430
+ * Regular expression used within newlineToBr().
431
+ * @type {RegExp}
432
+ * @private
433
+ */
434
+ NEWLINE_TO_BR_RE_: /[\r\n]/
435
+ };
436
+
437
+
438
+ /**
439
+ * Utility class to facilitate much faster string concatenation in IE,
440
+ * using Array.join() rather than the '+' operator. For other browsers
441
+ * we simply use the '+' operator.
442
+ *
443
+ * @param {Object|number|string|boolean=} opt_a1 Optional first initial item
444
+ * to append.
445
+ * @param {...Object|number|string|boolean} var_args Other initial items to
446
+ * append, e.g., new goog.string.StringBuffer('foo', 'bar').
447
+ * @constructor
448
+ */
449
+ goog.string.StringBuffer = function(opt_a1, var_args) {
450
+ /**
451
+ * Internal buffer for the string to be concatenated.
452
+ * @type {string|Array}
453
+ * @private
454
+ */
455
+ this.buffer_ = goog.userAgent.HAS_JSCRIPT ? [] : '';
456
+
457
+ if (opt_a1 != null) {
458
+ this.append.apply(this, arguments);
459
+ }
460
+ };
461
+
462
+
463
+ /**
464
+ * Length of internal buffer (faster than calling buffer_.length).
465
+ * Only used for IE.
466
+ * @type {number}
467
+ * @private
468
+ */
469
+ goog.string.StringBuffer.prototype.bufferLength_ = 0;
470
+
471
+ /**
472
+ * Appends one or more items to the string.
473
+ *
474
+ * Calling this with null, undefined, or empty arguments is an error.
475
+ *
476
+ * @param {Object|number|string|boolean} a1 Required first string.
477
+ * @param {Object|number|string|boolean=} opt_a2 Optional second string.
478
+ * @param {...Object|number|string|boolean} var_args Other items to append,
479
+ * e.g., sb.append('foo', 'bar', 'baz').
480
+ * @return {goog.string.StringBuffer} This same StringBuilder object.
481
+ */
482
+ goog.string.StringBuffer.prototype.append = function(a1, opt_a2, var_args) {
483
+
484
+ if (goog.userAgent.HAS_JSCRIPT) {
485
+ if (opt_a2 == null) { // no second argument (note: undefined == null)
486
+ // Array assignment is 2x faster than Array push. Also, use a1
487
+ // directly to avoid arguments instantiation, another 2x improvement.
488
+ this.buffer_[this.bufferLength_++] = a1;
489
+ } else {
490
+ var arr = /**@type {Array.<number|string|boolean>}*/this.buffer_;
491
+ arr.push.apply(arr, arguments);
492
+ this.bufferLength_ = this.buffer_.length;
493
+ }
494
+
495
+ } else {
496
+
497
+ // Use a1 directly to avoid arguments instantiation for single-arg case.
498
+ this.buffer_ += a1;
499
+ if (opt_a2 != null) { // no second argument (note: undefined == null)
500
+ for (var i = 1; i < arguments.length; i++) {
501
+ this.buffer_ += arguments[i];
502
+ }
503
+ }
504
+ }
505
+
506
+ return this;
507
+ };
508
+
509
+
510
+ /**
511
+ * Clears the string.
512
+ */
513
+ goog.string.StringBuffer.prototype.clear = function() {
514
+
515
+ if (goog.userAgent.HAS_JSCRIPT) {
516
+ this.buffer_.length = 0; // reuse array to avoid creating new object
517
+ this.bufferLength_ = 0;
518
+
519
+ } else {
520
+ this.buffer_ = '';
521
+ }
522
+ };
523
+
524
+
525
+ /**
526
+ * Returns the concatenated string.
527
+ *
528
+ * @return {string} The concatenated string.
529
+ */
530
+ goog.string.StringBuffer.prototype.toString = function() {
531
+
532
+ if (goog.userAgent.HAS_JSCRIPT) {
533
+ var str = this.buffer_.join('');
534
+ // Given a string with the entire contents, simplify the StringBuilder by
535
+ // setting its contents to only be this string, rather than many fragments.
536
+ this.clear();
537
+ if (str) {
538
+ this.append(str);
539
+ }
540
+ return str;
541
+
542
+ } else {
543
+ return /** @type {string} */ (this.buffer_);
544
+ }
545
+ };
546
+
547
+
548
+ if (!goog.soy) goog.soy = {
549
+ /**
550
+ * Helper function to render a Soy template and then set the
551
+ * output string as the innerHTML of an element. It is recommended
552
+ * to use this helper function instead of directly setting
553
+ * innerHTML in your hand-written code, so that it will be easier
554
+ * to audit the code for cross-site scripting vulnerabilities.
555
+ *
556
+ * @param {Function} template The Soy template defining element's content.
557
+ * @param {Object=} opt_templateData The data for the template.
558
+ * @param {Object=} opt_injectedData The injected data for the template.
559
+ * @param {(goog.dom.DomHelper|Document)=} opt_dom The context in which DOM
560
+ * nodes will be created.
561
+ */
562
+ renderAsElement: function(
563
+ template, opt_templateData, opt_injectedData, opt_dom) {
564
+ return /** @type {!Element} */ (soyshim.$$renderWithWrapper_(
565
+ template, opt_templateData, opt_dom, true /* asElement */,
566
+ opt_injectedData));
567
+ },
568
+ /**
569
+ * Helper function to render a Soy template into a single node or
570
+ * a document fragment. If the rendered HTML string represents a
571
+ * single node, then that node is returned (note that this is
572
+ * *not* a fragment, despite them name of the method). Otherwise a
573
+ * document fragment is returned containing the rendered nodes.
574
+ *
575
+ * @param {Function} template The Soy template defining element's content.
576
+ * @param {Object=} opt_templateData The data for the template.
577
+ * @param {Object=} opt_injectedData The injected data for the template.
578
+ * @param {(goog.dom.DomHelper|Document)=} opt_dom The context in which DOM
579
+ * nodes will be created.
580
+ * @return {!Node} The resulting node or document fragment.
581
+ */
582
+ renderAsFragment: function(
583
+ template, opt_templateData, opt_injectedData, opt_dom) {
584
+ return soyshim.$$renderWithWrapper_(
585
+ template, opt_templateData, opt_dom, false /* asElement */,
586
+ opt_injectedData);
587
+ },
588
+ /**
589
+ * Helper function to render a Soy template and then set the output string as
590
+ * the innerHTML of an element. It is recommended to use this helper function
591
+ * instead of directly setting innerHTML in your hand-written code, so that it
592
+ * will be easier to audit the code for cross-site scripting vulnerabilities.
593
+ *
594
+ * NOTE: New code should consider using goog.soy.renderElement instead.
595
+ *
596
+ * @param {Element} element The element whose content we are rendering.
597
+ * @param {Function} template The Soy template defining the element's content.
598
+ * @param {Object=} opt_templateData The data for the template.
599
+ * @param {Object=} opt_injectedData The injected data for the template.
600
+ */
601
+ renderElement: function(
602
+ element, template, opt_templateData, opt_injectedData) {
603
+ element.innerHTML = template(opt_templateData, null, opt_injectedData);
604
+ }
605
+ };
606
+
607
+
608
+ var soy = { esc: {} };
609
+ var soydata = {};
610
+ var soyshim = { $$DEFAULT_TEMPLATE_DATA_: {} };
611
+ /**
612
+ * Helper function to render a Soy template into a single node or a document
613
+ * fragment. If the rendered HTML string represents a single node, then that
614
+ * node is returned. Otherwise a document fragment is created and returned
615
+ * (wrapped in a DIV element if #opt_singleNode is true).
616
+ *
617
+ * @param {Function} template The Soy template defining the element's content.
618
+ * @param {Object=} opt_templateData The data for the template.
619
+ * @param {(goog.dom.DomHelper|Document)=} opt_dom The context in which DOM
620
+ * nodes will be created.
621
+ * @param {boolean=} opt_asElement Whether to wrap the fragment in an
622
+ * element if the template does not render a single element. If true,
623
+ * result is always an Element.
624
+ * @param {Object=} opt_injectedData The injected data for the template.
625
+ * @return {!Node} The resulting node or document fragment.
626
+ * @private
627
+ */
628
+ soyshim.$$renderWithWrapper_ = function(
629
+ template, opt_templateData, opt_dom, opt_asElement, opt_injectedData) {
630
+
631
+ var dom = opt_dom || document;
632
+ var wrapper = dom.createElement('div');
633
+ wrapper.innerHTML = template(
634
+ opt_templateData || soyshim.$$DEFAULT_TEMPLATE_DATA_, undefined,
635
+ opt_injectedData);
636
+
637
+ // If the template renders as a single element, return it.
638
+ if (wrapper.childNodes.length == 1) {
639
+ var firstChild = wrapper.firstChild;
640
+ if (!opt_asElement || firstChild.nodeType == 1 /* Element */) {
641
+ return /** @type {!Node} */ (firstChild);
642
+ }
643
+ }
644
+
645
+ // If we're forcing it to be a single element, return the wrapper DIV.
646
+ if (opt_asElement) {
647
+ return wrapper;
648
+ }
649
+
650
+ // Otherwise, create and return a fragment.
651
+ var fragment = dom.createDocumentFragment();
652
+ while (wrapper.firstChild) {
653
+ fragment.appendChild(wrapper.firstChild);
654
+ }
655
+ return fragment;
656
+ };
657
+
658
+
659
+ /**
660
+ * Returns a Unicode BiDi mark matching bidiGlobalDir (LRM or RLM) if the
661
+ * directionality or the exit directionality of text are opposite to
662
+ * bidiGlobalDir. Otherwise returns the empty string.
663
+ * If opt_isHtml, makes sure to ignore the LTR nature of the mark-up and escapes
664
+ * in text, making the logic suitable for HTML and HTML-escaped text.
665
+ * @param {number} bidiGlobalDir The global directionality context: 1 if ltr, -1
666
+ * if rtl, 0 if unknown.
667
+ * @param {number} dir text's directionality: 1 if ltr, -1 if rtl, 0 if unknown.
668
+ * @param {string} text The text whose directionality is to be estimated.
669
+ * @param {boolean=} opt_isHtml Whether text is HTML/HTML-escaped.
670
+ * Default: false.
671
+ * @return {string} A Unicode bidi mark matching bidiGlobalDir, or
672
+ * the empty string when text's overall and exit directionalities both match
673
+ * bidiGlobalDir, or bidiGlobalDir is 0 (unknown).
674
+ * @private
675
+ */
676
+ soyshim.$$bidiMarkAfterKnownDir_ = function(
677
+ bidiGlobalDir, dir, text, opt_isHtml) {
678
+ return (
679
+ bidiGlobalDir > 0 && (dir < 0 ||
680
+ soyshim.$$bidiIsRtlExitText_(text, opt_isHtml)) ? '\u200E' : // LRM
681
+ bidiGlobalDir < 0 && (dir > 0 ||
682
+ soyshim.$$bidiIsLtrExitText_(text, opt_isHtml)) ? '\u200F' : // RLM
683
+ '');
684
+ };
685
+
686
+
687
+ /**
688
+ * Strips str of any HTML mark-up and escapes. Imprecise in several ways, but
689
+ * precision is not very important, since the result is only meant to be used
690
+ * for directionality detection.
691
+ * @param {string} str The string to be stripped.
692
+ * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
693
+ * Default: false.
694
+ * @return {string} The stripped string.
695
+ * @private
696
+ */
697
+ soyshim.$$bidiStripHtmlIfNecessary_ = function(str, opt_isHtml) {
698
+ return opt_isHtml ? str.replace(soyshim.$$BIDI_HTML_SKIP_RE_, ' ') : str;
699
+ };
700
+
701
+
702
+ /**
703
+ * Simplified regular expression for am HTML tag (opening or closing) or an HTML
704
+ * escape - the things we want to skip over in order to ignore their ltr
705
+ * characters.
706
+ * @type {RegExp}
707
+ * @private
708
+ */
709
+ soyshim.$$BIDI_HTML_SKIP_RE_ = /<[^>]*>|&[^;]+;/g;
710
+
711
+
712
+ /**
713
+ * A practical pattern to identify strong LTR character. This pattern is not
714
+ * theoretically correct according to unicode standard. It is simplified for
715
+ * performance and small code size.
716
+ * @type {string}
717
+ * @private
718
+ */
719
+ soyshim.$$bidiLtrChars_ =
720
+ 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF' +
721
+ '\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF';
722
+
723
+
724
+ /**
725
+ * A practical pattern to identify strong neutral and weak character. This
726
+ * pattern is not theoretically correct according to unicode standard. It is
727
+ * simplified for performance and small code size.
728
+ * @type {string}
729
+ * @private
730
+ */
731
+ soyshim.$$bidiNeutralChars_ =
732
+ '\u0000-\u0020!-@[-`{-\u00BF\u00D7\u00F7\u02B9-\u02FF\u2000-\u2BFF';
733
+
734
+
735
+ /**
736
+ * A practical pattern to identify strong RTL character. This pattern is not
737
+ * theoretically correct according to unicode standard. It is simplified for
738
+ * performance and small code size.
739
+ * @type {string}
740
+ * @private
741
+ */
742
+ soyshim.$$bidiRtlChars_ = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC';
743
+
744
+
745
+ /**
746
+ * Regular expressions to check if a piece of text is of RTL directionality
747
+ * on first character with strong directionality.
748
+ * @type {RegExp}
749
+ * @private
750
+ */
751
+ soyshim.$$bidiRtlDirCheckRe_ = new RegExp(
752
+ '^[^' + soyshim.$$bidiLtrChars_ + ']*[' + soyshim.$$bidiRtlChars_ + ']');
753
+
754
+
755
+ /**
756
+ * Regular expressions to check if a piece of text is of neutral directionality.
757
+ * Url are considered as neutral.
758
+ * @type {RegExp}
759
+ * @private
760
+ */
761
+ soyshim.$$bidiNeutralDirCheckRe_ = new RegExp(
762
+ '^[' + soyshim.$$bidiNeutralChars_ + ']*$|^http://');
763
+
764
+
765
+ /**
766
+ * Check the directionality of the a piece of text based on the first character
767
+ * with strong directionality.
768
+ * @param {string} str string being checked.
769
+ * @return {boolean} return true if rtl directionality is being detected.
770
+ * @private
771
+ */
772
+ soyshim.$$bidiIsRtlText_ = function(str) {
773
+ return soyshim.$$bidiRtlDirCheckRe_.test(str);
774
+ };
775
+
776
+
777
+ /**
778
+ * Check the directionality of the a piece of text based on the first character
779
+ * with strong directionality.
780
+ * @param {string} str string being checked.
781
+ * @return {boolean} true if all characters have neutral directionality.
782
+ * @private
783
+ */
784
+ soyshim.$$bidiIsNeutralText_ = function(str) {
785
+ return soyshim.$$bidiNeutralDirCheckRe_.test(str);
786
+ };
787
+
788
+
789
+ /**
790
+ * This constant controls threshold of rtl directionality.
791
+ * @type {number}
792
+ * @private
793
+ */
794
+ soyshim.$$bidiRtlDetectionThreshold_ = 0.40;
795
+
796
+
797
+ /**
798
+ * Returns the RTL ratio based on word count.
799
+ * @param {string} str the string that need to be checked.
800
+ * @return {number} the ratio of RTL words among all words with directionality.
801
+ * @private
802
+ */
803
+ soyshim.$$bidiRtlWordRatio_ = function(str) {
804
+ var rtlCount = 0;
805
+ var totalCount = 0;
806
+ var tokens = str.split(' ');
807
+ for (var i = 0; i < tokens.length; i++) {
808
+ if (soyshim.$$bidiIsRtlText_(tokens[i])) {
809
+ rtlCount++;
810
+ totalCount++;
811
+ } else if (!soyshim.$$bidiIsNeutralText_(tokens[i])) {
812
+ totalCount++;
813
+ }
814
+ }
815
+
816
+ return totalCount == 0 ? 0 : rtlCount / totalCount;
817
+ };
818
+
819
+
820
+ /**
821
+ * Regular expressions to check if the last strongly-directional character in a
822
+ * piece of text is LTR.
823
+ * @type {RegExp}
824
+ * @private
825
+ */
826
+ soyshim.$$bidiLtrExitDirCheckRe_ = new RegExp(
827
+ '[' + soyshim.$$bidiLtrChars_ + '][^' + soyshim.$$bidiRtlChars_ + ']*$');
828
+
829
+
830
+ /**
831
+ * Regular expressions to check if the last strongly-directional character in a
832
+ * piece of text is RTL.
833
+ * @type {RegExp}
834
+ * @private
835
+ */
836
+ soyshim.$$bidiRtlExitDirCheckRe_ = new RegExp(
837
+ '[' + soyshim.$$bidiRtlChars_ + '][^' + soyshim.$$bidiLtrChars_ + ']*$');
838
+
839
+
840
+ /**
841
+ * Check if the exit directionality a piece of text is LTR, i.e. if the last
842
+ * strongly-directional character in the string is LTR.
843
+ * @param {string} str string being checked.
844
+ * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
845
+ * Default: false.
846
+ * @return {boolean} Whether LTR exit directionality was detected.
847
+ * @private
848
+ */
849
+ soyshim.$$bidiIsLtrExitText_ = function(str, opt_isHtml) {
850
+ str = soyshim.$$bidiStripHtmlIfNecessary_(str, opt_isHtml);
851
+ return soyshim.$$bidiLtrExitDirCheckRe_.test(str);
852
+ };
853
+
854
+
855
+ /**
856
+ * Check if the exit directionality a piece of text is RTL, i.e. if the last
857
+ * strongly-directional character in the string is RTL.
858
+ * @param {string} str string being checked.
859
+ * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
860
+ * Default: false.
861
+ * @return {boolean} Whether RTL exit directionality was detected.
862
+ * @private
863
+ */
864
+ soyshim.$$bidiIsRtlExitText_ = function(str, opt_isHtml) {
865
+ str = soyshim.$$bidiStripHtmlIfNecessary_(str, opt_isHtml);
866
+ return soyshim.$$bidiRtlExitDirCheckRe_.test(str);
867
+ };
868
+
869
+
870
+ // =============================================================================
871
+ // COPIED FROM soyutils_usegoog.js
872
+
873
+
874
+ // -----------------------------------------------------------------------------
875
+ // StringBuilder (compatible with the 'stringbuilder' code style).
876
+
877
+
878
+ /**
879
+ * Utility class to facilitate much faster string concatenation in IE,
880
+ * using Array.join() rather than the '+' operator. For other browsers
881
+ * we simply use the '+' operator.
882
+ *
883
+ * @param {Object} var_args Initial items to append,
884
+ * e.g., new soy.StringBuilder('foo', 'bar').
885
+ * @constructor
886
+ */
887
+ soy.StringBuilder = goog.string.StringBuffer;
888
+
889
+
890
+ // -----------------------------------------------------------------------------
891
+ // soydata: Defines typed strings, e.g. an HTML string {@code "a<b>c"} is
892
+ // semantically distinct from the plain text string {@code "a<b>c"} and smart
893
+ // templates can take that distinction into account.
894
+
895
+ /**
896
+ * A type of textual content.
897
+ * @enum {number}
898
+ */
899
+ soydata.SanitizedContentKind = {
900
+
901
+ /**
902
+ * A snippet of HTML that does not start or end inside a tag, comment, entity,
903
+ * or DOCTYPE; and that does not contain any executable code
904
+ * (JS, {@code <object>}s, etc.) from a different trust domain.
905
+ */
906
+ HTML: 0,
907
+
908
+ /**
909
+ * A sequence of code units that can appear between quotes (either kind) in a
910
+ * JS program without causing a parse error, and without causing any side
911
+ * effects.
912
+ * <p>
913
+ * The content should not contain unescaped quotes, newlines, or anything else
914
+ * that would cause parsing to fail or to cause a JS parser to finish the
915
+ * string its parsing inside the content.
916
+ * <p>
917
+ * The content must also not end inside an escape sequence ; no partial octal
918
+ * escape sequences or odd number of '{@code \}'s at the end.
919
+ */
920
+ JS_STR_CHARS: 1,
921
+
922
+ /** A properly encoded portion of a URI. */
923
+ URI: 2,
924
+
925
+ /** An attribute name and value such as {@code dir="ltr"}. */
926
+ HTML_ATTRIBUTE: 3
927
+ };
928
+
929
+
930
+ /**
931
+ * A string-like object that carries a content-type.
932
+ * @param {string} content
933
+ * @constructor
934
+ * @private
935
+ */
936
+ soydata.SanitizedContent = function(content) {
937
+ /**
938
+ * The textual content.
939
+ * @type {string}
940
+ */
941
+ this.content = content;
942
+ };
943
+
944
+ /** @type {soydata.SanitizedContentKind} */
945
+ soydata.SanitizedContent.prototype.contentKind;
946
+
947
+ /** @override */
948
+ soydata.SanitizedContent.prototype.toString = function() {
949
+ return this.content;
950
+ };
951
+
952
+
953
+ /**
954
+ * Content of type {@link soydata.SanitizedContentKind.HTML}.
955
+ * @param {string} content A string of HTML that can safely be embedded in
956
+ * a PCDATA context in your app. If you would be surprised to find that an
957
+ * HTML sanitizer produced {@code s} (e.g. it runs code or fetches bad URLs)
958
+ * and you wouldn't write a template that produces {@code s} on security or
959
+ * privacy grounds, then don't pass {@code s} here.
960
+ * @constructor
961
+ * @extends {soydata.SanitizedContent}
962
+ */
963
+ soydata.SanitizedHtml = function(content) {
964
+ soydata.SanitizedContent.call(this, content);
965
+ };
966
+ goog.inherits(soydata.SanitizedHtml, soydata.SanitizedContent);
967
+
968
+ /** @override */
969
+ soydata.SanitizedHtml.prototype.contentKind = soydata.SanitizedContentKind.HTML;
970
+
971
+
972
+ /**
973
+ * Content of type {@link soydata.SanitizedContentKind.JS_STR_CHARS}.
974
+ * @param {string} content A string of JS that when evaled, produces a
975
+ * value that does not depend on any sensitive data and has no side effects
976
+ * <b>OR</b> a string of JS that does not reference any variables or have
977
+ * any side effects not known statically to the app authors.
978
+ * @constructor
979
+ * @extends {soydata.SanitizedContent}
980
+ */
981
+ soydata.SanitizedJsStrChars = function(content) {
982
+ soydata.SanitizedContent.call(this, content);
983
+ };
984
+ goog.inherits(soydata.SanitizedJsStrChars, soydata.SanitizedContent);
985
+
986
+ /** @override */
987
+ soydata.SanitizedJsStrChars.prototype.contentKind =
988
+ soydata.SanitizedContentKind.JS_STR_CHARS;
989
+
990
+
991
+ /**
992
+ * Content of type {@link soydata.SanitizedContentKind.URI}.
993
+ * @param {string} content A chunk of URI that the caller knows is safe to
994
+ * emit in a template.
995
+ * @constructor
996
+ * @extends {soydata.SanitizedContent}
997
+ */
998
+ soydata.SanitizedUri = function(content) {
999
+ soydata.SanitizedContent.call(this, content);
1000
+ };
1001
+ goog.inherits(soydata.SanitizedUri, soydata.SanitizedContent);
1002
+
1003
+ /** @override */
1004
+ soydata.SanitizedUri.prototype.contentKind = soydata.SanitizedContentKind.URI;
1005
+
1006
+
1007
+ /**
1008
+ * Content of type {@link soydata.SanitizedContentKind.HTML_ATTRIBUTE}.
1009
+ * @param {string} content An attribute name and value, such as
1010
+ * {@code dir="ltr"}.
1011
+ * @constructor
1012
+ * @extends {soydata.SanitizedContent}
1013
+ */
1014
+ soydata.SanitizedHtmlAttribute = function(content) {
1015
+ soydata.SanitizedContent.call(this, content);
1016
+ };
1017
+ goog.inherits(soydata.SanitizedHtmlAttribute, soydata.SanitizedContent);
1018
+
1019
+ /** @override */
1020
+ soydata.SanitizedHtmlAttribute.prototype.contentKind =
1021
+ soydata.SanitizedContentKind.HTML_ATTRIBUTE;
1022
+
1023
+
1024
+ // -----------------------------------------------------------------------------
1025
+ // Public utilities.
1026
+
1027
+
1028
+ /**
1029
+ * Helper function to render a Soy template and then set the output string as
1030
+ * the innerHTML of an element. It is recommended to use this helper function
1031
+ * instead of directly setting innerHTML in your hand-written code, so that it
1032
+ * will be easier to audit the code for cross-site scripting vulnerabilities.
1033
+ *
1034
+ * NOTE: New code should consider using goog.soy.renderElement instead.
1035
+ *
1036
+ * @param {Element} element The element whose content we are rendering.
1037
+ * @param {Function} template The Soy template defining the element's content.
1038
+ * @param {Object=} opt_templateData The data for the template.
1039
+ * @param {Object=} opt_injectedData The injected data for the template.
1040
+ */
1041
+ soy.renderElement = goog.soy.renderElement;
1042
+
1043
+
1044
+ /**
1045
+ * Helper function to render a Soy template into a single node or a document
1046
+ * fragment. If the rendered HTML string represents a single node, then that
1047
+ * node is returned (note that this is *not* a fragment, despite them name of
1048
+ * the method). Otherwise a document fragment is returned containing the
1049
+ * rendered nodes.
1050
+ *
1051
+ * NOTE: New code should consider using goog.soy.renderAsFragment
1052
+ * instead (note that the arguments are different).
1053
+ *
1054
+ * @param {Function} template The Soy template defining the element's content.
1055
+ * @param {Object=} opt_templateData The data for the template.
1056
+ * @param {Document=} opt_document The document used to create DOM nodes. If not
1057
+ * specified, global document object is used.
1058
+ * @param {Object=} opt_injectedData The injected data for the template.
1059
+ * @return {!Node} The resulting node or document fragment.
1060
+ */
1061
+ soy.renderAsFragment = function(
1062
+ template, opt_templateData, opt_document, opt_injectedData) {
1063
+ return goog.soy.renderAsFragment(
1064
+ template, opt_templateData, opt_injectedData,
1065
+ new goog.dom.DomHelper(opt_document));
1066
+ };
1067
+
1068
+
1069
+ /**
1070
+ * Helper function to render a Soy template into a single node. If the rendered
1071
+ * HTML string represents a single node, then that node is returned. Otherwise,
1072
+ * a DIV element is returned containing the rendered nodes.
1073
+ *
1074
+ * NOTE: New code should consider using goog.soy.renderAsElement
1075
+ * instead (note that the arguments are different).
1076
+ *
1077
+ * @param {Function} template The Soy template defining the element's content.
1078
+ * @param {Object=} opt_templateData The data for the template.
1079
+ * @param {Document=} opt_document The document used to create DOM nodes. If not
1080
+ * specified, global document object is used.
1081
+ * @param {Object=} opt_injectedData The injected data for the template.
1082
+ * @return {!Element} Rendered template contents, wrapped in a parent DIV
1083
+ * element if necessary.
1084
+ */
1085
+ soy.renderAsElement = function(
1086
+ template, opt_templateData, opt_document, opt_injectedData) {
1087
+ return goog.soy.renderAsElement(
1088
+ template, opt_templateData, opt_injectedData,
1089
+ new goog.dom.DomHelper(opt_document));
1090
+ };
1091
+
1092
+
1093
+ // -----------------------------------------------------------------------------
1094
+ // Below are private utilities to be used by Soy-generated code only.
1095
+
1096
+
1097
+ /**
1098
+ * Builds an augmented data object to be passed when a template calls another,
1099
+ * and needs to pass both original data and additional params. The returned
1100
+ * object will contain both the original data and the additional params. If the
1101
+ * same key appears in both, then the value from the additional params will be
1102
+ * visible, while the value from the original data will be hidden. The original
1103
+ * data object will be used, but not modified.
1104
+ *
1105
+ * @param {!Object} origData The original data to pass.
1106
+ * @param {Object} additionalParams The additional params to pass.
1107
+ * @return {Object} An augmented data object containing both the original data
1108
+ * and the additional params.
1109
+ */
1110
+ soy.$$augmentData = function(origData, additionalParams) {
1111
+
1112
+ // Create a new object whose '__proto__' field is set to origData.
1113
+ /** @constructor */
1114
+ function TempCtor() {}
1115
+ TempCtor.prototype = origData;
1116
+ var newData = new TempCtor();
1117
+
1118
+ // Add the additional params to the new object.
1119
+ for (var key in additionalParams) {
1120
+ newData[key] = additionalParams[key];
1121
+ }
1122
+
1123
+ return newData;
1124
+ };
1125
+
1126
+
1127
+ /**
1128
+ * Gets the keys in a map as an array. There are no guarantees on the order.
1129
+ * @param {Object} map The map to get the keys of.
1130
+ * @return {Array.<string>} The array of keys in the given map.
1131
+ */
1132
+ soy.$$getMapKeys = function(map) {
1133
+ var mapKeys = [];
1134
+ for (var key in map) {
1135
+ mapKeys.push(key);
1136
+ }
1137
+ return mapKeys;
1138
+ };
1139
+
1140
+
1141
+ /**
1142
+ * Gets a consistent unique id for the given delegate template name. Two calls
1143
+ * to this function will return the same id if and only if the input names are
1144
+ * the same.
1145
+ *
1146
+ * <p> Important: This function must always be called with a string constant.
1147
+ *
1148
+ * <p> If Closure Compiler is not being used, then this is just this identity
1149
+ * function. If Closure Compiler is being used, then each call to this function
1150
+ * will be replaced with a short string constant, which will be consistent per
1151
+ * input name.
1152
+ *
1153
+ * @param {string} delTemplateName The delegate template name for which to get a
1154
+ * consistent unique id.
1155
+ * @return {string} A unique id that is consistent per input name.
1156
+ *
1157
+ * @consistentIdGenerator
1158
+ */
1159
+ soy.$$getDelegateId = function(delTemplateName) {
1160
+ return delTemplateName;
1161
+ };
1162
+
1163
+
1164
+ /**
1165
+ * Map from registered delegate template id/name to the priority of the
1166
+ * implementation.
1167
+ * @type {Object}
1168
+ * @private
1169
+ */
1170
+ soy.$$DELEGATE_REGISTRY_PRIORITIES_ = {};
1171
+
1172
+ /**
1173
+ * Map from registered delegate template id/name to the implementation function.
1174
+ * @type {Object}
1175
+ * @private
1176
+ */
1177
+ soy.$$DELEGATE_REGISTRY_FUNCTIONS_ = {};
1178
+
1179
+
1180
+ /**
1181
+ * Registers a delegate implementation. If the same delegate template id/name
1182
+ * has been registered previously, then priority values are compared and only
1183
+ * the higher priority implementation is stored (if priorities are equal, an
1184
+ * error is thrown).
1185
+ *
1186
+ * @param {string} delTemplateId The delegate template id/name to register.
1187
+ * @param {number} delPriority The implementation's priority value.
1188
+ * @param {Function} delFn The implementation function.
1189
+ */
1190
+ soy.$$registerDelegateFn = function(delTemplateId, delPriority, delFn) {
1191
+ var mapKey = 'key_' + delTemplateId;
1192
+ var currPriority = soy.$$DELEGATE_REGISTRY_PRIORITIES_[mapKey];
1193
+ if (currPriority === undefined || delPriority > currPriority) {
1194
+ // Registering new or higher-priority function: replace registry entry.
1195
+ soy.$$DELEGATE_REGISTRY_PRIORITIES_[mapKey] = delPriority;
1196
+ soy.$$DELEGATE_REGISTRY_FUNCTIONS_[mapKey] = delFn;
1197
+ } else if (delPriority == currPriority) {
1198
+ // Registering same-priority function: error.
1199
+ throw Error(
1200
+ 'Encountered two active delegates with same priority (id/name "' +
1201
+ delTemplateId + '").');
1202
+ } else {
1203
+ // Registering lower-priority function: do nothing.
1204
+ }
1205
+ };
1206
+
1207
+
1208
+ /**
1209
+ * Retrieves the (highest-priority) implementation that has been registered for
1210
+ * a given delegate template id/name. If no implementation has been registered
1211
+ * for the id/name, then returns an implementation that is equivalent to an
1212
+ * empty template (i.e. rendered output would be empty string).
1213
+ *
1214
+ * @param {string} delTemplateId The delegate template id/name to get.
1215
+ * @return {Function} The retrieved implementation function.
1216
+ */
1217
+ soy.$$getDelegateFn = function(delTemplateId) {
1218
+ var delFn = soy.$$DELEGATE_REGISTRY_FUNCTIONS_['key_' + delTemplateId];
1219
+ return delFn ? delFn : soy.$$EMPTY_TEMPLATE_FN_;
1220
+ };
1221
+
1222
+
1223
+ /**
1224
+ * Private helper soy.$$getDelegateFn(). This is the empty template function
1225
+ * that is returned whenever there's no delegate implementation found.
1226
+ *
1227
+ * @param {Object.<string, *>=} opt_data
1228
+ * @param {soy.StringBuilder=} opt_sb
1229
+ * @param {Object.<string, *>=} opt_ijData
1230
+ * @return {string}
1231
+ * @private
1232
+ */
1233
+ soy.$$EMPTY_TEMPLATE_FN_ = function(opt_data, opt_sb, opt_ijData) {
1234
+ return '';
1235
+ };
1236
+
1237
+
1238
+ // -----------------------------------------------------------------------------
1239
+ // Escape/filter/normalize.
1240
+
1241
+
1242
+ /**
1243
+ * Escapes HTML special characters in a string. Escapes double quote '"' in
1244
+ * addition to '&', '<', and '>' so that a string can be included in an HTML
1245
+ * tag attribute value within double quotes.
1246
+ * Will emit known safe HTML as-is.
1247
+ *
1248
+ * @param {*} value The string-like value to be escaped. May not be a string,
1249
+ * but the value will be coerced to a string.
1250
+ * @return {string} An escaped version of value.
1251
+ */
1252
+ soy.$$escapeHtml = function(value) {
1253
+ if (typeof value === 'object' && value &&
1254
+ value.contentKind === soydata.SanitizedContentKind.HTML) {
1255
+ return value.content;
1256
+ }
1257
+ return soy.esc.$$escapeHtmlHelper(value);
1258
+ };
1259
+
1260
+
1261
+ /**
1262
+ * Escapes HTML special characters in a string so that it can be embedded in
1263
+ * RCDATA.
1264
+ * <p>
1265
+ * Escapes HTML special characters so that the value will not prematurely end
1266
+ * the body of a tag like {@code <textarea>} or {@code <title>}. RCDATA tags
1267
+ * cannot contain other HTML entities, so it is not strictly necessary to escape
1268
+ * HTML special characters except when part of that text looks like an HTML
1269
+ * entity or like a close tag : {@code </textarea>}.
1270
+ * <p>
1271
+ * Will normalize known safe HTML to make sure that sanitized HTML (which could
1272
+ * contain an innocuous {@code </textarea>} don't prematurely end an RCDATA
1273
+ * element.
1274
+ *
1275
+ * @param {*} value The string-like value to be escaped. May not be a string,
1276
+ * but the value will be coerced to a string.
1277
+ * @return {string} An escaped version of value.
1278
+ */
1279
+ soy.$$escapeHtmlRcdata = function(value) {
1280
+ if (typeof value === 'object' && value &&
1281
+ value.contentKind === soydata.SanitizedContentKind.HTML) {
1282
+ return soy.esc.$$normalizeHtmlHelper(value.content);
1283
+ }
1284
+ return soy.esc.$$escapeHtmlHelper(value);
1285
+ };
1286
+
1287
+
1288
+ /**
1289
+ * Removes HTML tags from a string of known safe HTML so it can be used as an
1290
+ * attribute value.
1291
+ *
1292
+ * @param {*} value The HTML to be escaped. May not be a string, but the
1293
+ * value will be coerced to a string.
1294
+ * @return {string} A representation of value without tags, HTML comments, or
1295
+ * other content.
1296
+ */
1297
+ soy.$$stripHtmlTags = function(value) {
1298
+ return String(value).replace(soy.esc.$$HTML_TAG_REGEX_, '');
1299
+ };
1300
+
1301
+
1302
+ /**
1303
+ * Escapes HTML special characters in an HTML attribute value.
1304
+ *
1305
+ * @param {*} value The HTML to be escaped. May not be a string, but the
1306
+ * value will be coerced to a string.
1307
+ * @return {string} An escaped version of value.
1308
+ */
1309
+ soy.$$escapeHtmlAttribute = function(value) {
1310
+ if (typeof value === 'object' && value &&
1311
+ value.contentKind === soydata.SanitizedContentKind.HTML) {
1312
+ return soy.esc.$$normalizeHtmlHelper(soy.$$stripHtmlTags(value.content));
1313
+ }
1314
+ return soy.esc.$$escapeHtmlHelper(value);
1315
+ };
1316
+
1317
+
1318
+ /**
1319
+ * Escapes HTML special characters in a string including space and other
1320
+ * characters that can end an unquoted HTML attribute value.
1321
+ *
1322
+ * @param {*} value The HTML to be escaped. May not be a string, but the
1323
+ * value will be coerced to a string.
1324
+ * @return {string} An escaped version of value.
1325
+ */
1326
+ soy.$$escapeHtmlAttributeNospace = function(value) {
1327
+ if (typeof value === 'object' && value &&
1328
+ value.contentKind === soydata.SanitizedContentKind.HTML) {
1329
+ return soy.esc.$$normalizeHtmlNospaceHelper(
1330
+ soy.$$stripHtmlTags(value.content));
1331
+ }
1332
+ return soy.esc.$$escapeHtmlNospaceHelper(value);
1333
+ };
1334
+
1335
+
1336
+ /**
1337
+ * Filters out strings that cannot be a substring of a valid HTML attribute.
1338
+ *
1339
+ * @param {*} value The value to escape. May not be a string, but the value
1340
+ * will be coerced to a string.
1341
+ * @return {string} A valid HTML attribute name part or name/value pair.
1342
+ * {@code "zSoyz"} if the input is invalid.
1343
+ */
1344
+ soy.$$filterHtmlAttribute = function(value) {
1345
+ if (typeof value === 'object' && value &&
1346
+ value.contentKind === soydata.SanitizedContentKind.HTML_ATTRIBUTE) {
1347
+ return value.content.replace(/=([^"']*)$/, '="$1"');
1348
+ }
1349
+ return soy.esc.$$filterHtmlAttributeHelper(value);
1350
+ };
1351
+
1352
+
1353
+ /**
1354
+ * Filters out strings that cannot be a substring of a valid HTML element name.
1355
+ *
1356
+ * @param {*} value The value to escape. May not be a string, but the value
1357
+ * will be coerced to a string.
1358
+ * @return {string} A valid HTML element name part.
1359
+ * {@code "zSoyz"} if the input is invalid.
1360
+ */
1361
+ soy.$$filterHtmlElementName = function(value) {
1362
+ return soy.esc.$$filterHtmlElementNameHelper(value);
1363
+ };
1364
+
1365
+
1366
+ /**
1367
+ * Escapes characters in the value to make it valid content for a JS string
1368
+ * literal.
1369
+ *
1370
+ * @param {*} value The value to escape. May not be a string, but the value
1371
+ * will be coerced to a string.
1372
+ * @return {string} An escaped version of value.
1373
+ * @deprecated
1374
+ */
1375
+ soy.$$escapeJs = function(value) {
1376
+ return soy.$$escapeJsString(value);
1377
+ };
1378
+
1379
+
1380
+ /**
1381
+ * Escapes characters in the value to make it valid content for a JS string
1382
+ * literal.
1383
+ *
1384
+ * @param {*} value The value to escape. May not be a string, but the value
1385
+ * will be coerced to a string.
1386
+ * @return {string} An escaped version of value.
1387
+ */
1388
+ soy.$$escapeJsString = function(value) {
1389
+ if (typeof value === 'object' &&
1390
+ value.contentKind === soydata.SanitizedContentKind.JS_STR_CHARS) {
1391
+ return value.content;
1392
+ }
1393
+ return soy.esc.$$escapeJsStringHelper(value);
1394
+ };
1395
+
1396
+
1397
+ /**
1398
+ * Encodes a value as a JavaScript literal.
1399
+ *
1400
+ * @param {*} value The value to escape. May not be a string, but the value
1401
+ * will be coerced to a string.
1402
+ * @return {string} A JavaScript code representation of the input.
1403
+ */
1404
+ soy.$$escapeJsValue = function(value) {
1405
+ // We surround values with spaces so that they can't be interpolated into
1406
+ // identifiers by accident.
1407
+ // We could use parentheses but those might be interpreted as a function call.
1408
+ if (value == null) { // Intentionally matches undefined.
1409
+ // Java returns null from maps where there is no corresponding key while
1410
+ // JS returns undefined.
1411
+ // We always output null for compatibility with Java which does not have a
1412
+ // distinct undefined value.
1413
+ return ' null ';
1414
+ }
1415
+ switch (typeof value) {
1416
+ case 'boolean': case 'number':
1417
+ return ' ' + value + ' ';
1418
+ default:
1419
+ return "'" + soy.esc.$$escapeJsStringHelper(String(value)) + "'";
1420
+ }
1421
+ };
1422
+
1423
+
1424
+ /**
1425
+ * Escapes characters in the string to make it valid content for a JS regular
1426
+ * expression literal.
1427
+ *
1428
+ * @param {*} value The value to escape. May not be a string, but the value
1429
+ * will be coerced to a string.
1430
+ * @return {string} An escaped version of value.
1431
+ */
1432
+ soy.$$escapeJsRegex = function(value) {
1433
+ return soy.esc.$$escapeJsRegexHelper(value);
1434
+ };
1435
+
1436
+
1437
+ /**
1438
+ * Matches all URI mark characters that conflict with HTML attribute delimiters
1439
+ * or that cannot appear in a CSS uri.
1440
+ * From <a href="http://www.w3.org/TR/CSS2/grammar.html">G.2: CSS grammar</a>
1441
+ * <pre>
1442
+ * url ([!#$%&*-~]|{nonascii}|{escape})*
1443
+ * </pre>
1444
+ *
1445
+ * @type {RegExp}
1446
+ * @private
1447
+ */
1448
+ soy.$$problematicUriMarks_ = /['()]/g;
1449
+
1450
+ /**
1451
+ * @param {string} ch A single character in {@link soy.$$problematicUriMarks_}.
1452
+ * @return {string}
1453
+ * @private
1454
+ */
1455
+ soy.$$pctEncode_ = function(ch) {
1456
+ return '%' + ch.charCodeAt(0).toString(16);
1457
+ };
1458
+
1459
+ /**
1460
+ * Escapes a string so that it can be safely included in a URI.
1461
+ *
1462
+ * @param {*} value The value to escape. May not be a string, but the value
1463
+ * will be coerced to a string.
1464
+ * @return {string} An escaped version of value.
1465
+ */
1466
+ soy.$$escapeUri = function(value) {
1467
+ if (typeof value === 'object' &&
1468
+ value.contentKind === soydata.SanitizedContentKind.URI) {
1469
+ return soy.$$normalizeUri(value);
1470
+ }
1471
+ // Apostophes and parentheses are not matched by encodeURIComponent.
1472
+ // They are technically special in URIs, but only appear in the obsolete mark
1473
+ // production in Appendix D.2 of RFC 3986, so can be encoded without changing
1474
+ // semantics.
1475
+ var encoded = soy.esc.$$escapeUriHelper(value);
1476
+ soy.$$problematicUriMarks_.lastIndex = 0;
1477
+ if (soy.$$problematicUriMarks_.test(encoded)) {
1478
+ return encoded.replace(soy.$$problematicUriMarks_, soy.$$pctEncode_);
1479
+ }
1480
+ return encoded;
1481
+ };
1482
+
1483
+
1484
+ /**
1485
+ * Removes rough edges from a URI by escaping any raw HTML/JS string delimiters.
1486
+ *
1487
+ * @param {*} value The value to escape. May not be a string, but the value
1488
+ * will be coerced to a string.
1489
+ * @return {string} An escaped version of value.
1490
+ */
1491
+ soy.$$normalizeUri = function(value) {
1492
+ return soy.esc.$$normalizeUriHelper(value);
1493
+ };
1494
+
1495
+
1496
+ /**
1497
+ * Vets a URI's protocol and removes rough edges from a URI by escaping
1498
+ * any raw HTML/JS string delimiters.
1499
+ *
1500
+ * @param {*} value The value to escape. May not be a string, but the value
1501
+ * will be coerced to a string.
1502
+ * @return {string} An escaped version of value.
1503
+ */
1504
+ soy.$$filterNormalizeUri = function(value) {
1505
+ return soy.esc.$$filterNormalizeUriHelper(value);
1506
+ };
1507
+
1508
+
1509
+ /**
1510
+ * Escapes a string so it can safely be included inside a quoted CSS string.
1511
+ *
1512
+ * @param {*} value The value to escape. May not be a string, but the value
1513
+ * will be coerced to a string.
1514
+ * @return {string} An escaped version of value.
1515
+ */
1516
+ soy.$$escapeCssString = function(value) {
1517
+ return soy.esc.$$escapeCssStringHelper(value);
1518
+ };
1519
+
1520
+
1521
+ /**
1522
+ * Encodes a value as a CSS identifier part, keyword, or quantity.
1523
+ *
1524
+ * @param {*} value The value to escape. May not be a string, but the value
1525
+ * will be coerced to a string.
1526
+ * @return {string} A safe CSS identifier part, keyword, or quanitity.
1527
+ */
1528
+ soy.$$filterCssValue = function(value) {
1529
+ // Uses == to intentionally match null and undefined for Java compatibility.
1530
+ if (value == null) {
1531
+ return '';
1532
+ }
1533
+ return soy.esc.$$filterCssValueHelper(value);
1534
+ };
1535
+
1536
+
1537
+ // -----------------------------------------------------------------------------
1538
+ // Basic directives/functions.
1539
+
1540
+
1541
+ /**
1542
+ * Converts \r\n, \r, and \n to <br>s
1543
+ * @param {*} str The string in which to convert newlines.
1544
+ * @return {string} A copy of {@code str} with converted newlines.
1545
+ */
1546
+ soy.$$changeNewlineToBr = function(str) {
1547
+ return goog.string.newLineToBr(String(str), false);
1548
+ };
1549
+
1550
+
1551
+ /**
1552
+ * Inserts word breaks ('wbr' tags) into a HTML string at a given interval. The
1553
+ * counter is reset if a space is encountered. Word breaks aren't inserted into
1554
+ * HTML tags or entities. Entites count towards the character count; HTML tags
1555
+ * do not.
1556
+ *
1557
+ * @param {*} str The HTML string to insert word breaks into. Can be other
1558
+ * types, but the value will be coerced to a string.
1559
+ * @param {number} maxCharsBetweenWordBreaks Maximum number of non-space
1560
+ * characters to allow before adding a word break.
1561
+ * @return {string} The string including word breaks.
1562
+ */
1563
+ soy.$$insertWordBreaks = function(str, maxCharsBetweenWordBreaks) {
1564
+ return goog.format.insertWordBreaks(String(str), maxCharsBetweenWordBreaks);
1565
+ };
1566
+
1567
+
1568
+ /**
1569
+ * Truncates a string to a given max length (if it's currently longer),
1570
+ * optionally adding ellipsis at the end.
1571
+ *
1572
+ * @param {*} str The string to truncate. Can be other types, but the value will
1573
+ * be coerced to a string.
1574
+ * @param {number} maxLen The maximum length of the string after truncation
1575
+ * (including ellipsis, if applicable).
1576
+ * @param {boolean} doAddEllipsis Whether to add ellipsis if the string needs
1577
+ * truncation.
1578
+ * @return {string} The string after truncation.
1579
+ */
1580
+ soy.$$truncate = function(str, maxLen, doAddEllipsis) {
1581
+
1582
+ str = String(str);
1583
+ if (str.length <= maxLen) {
1584
+ return str; // no need to truncate
1585
+ }
1586
+
1587
+ // If doAddEllipsis, either reduce maxLen to compensate, or else if maxLen is
1588
+ // too small, just turn off doAddEllipsis.
1589
+ if (doAddEllipsis) {
1590
+ if (maxLen > 3) {
1591
+ maxLen -= 3;
1592
+ } else {
1593
+ doAddEllipsis = false;
1594
+ }
1595
+ }
1596
+
1597
+ // Make sure truncating at maxLen doesn't cut up a unicode surrogate pair.
1598
+ if (soy.$$isHighSurrogate_(str.charAt(maxLen - 1)) &&
1599
+ soy.$$isLowSurrogate_(str.charAt(maxLen))) {
1600
+ maxLen -= 1;
1601
+ }
1602
+
1603
+ // Truncate.
1604
+ str = str.substring(0, maxLen);
1605
+
1606
+ // Add ellipsis.
1607
+ if (doAddEllipsis) {
1608
+ str += '...';
1609
+ }
1610
+
1611
+ return str;
1612
+ };
1613
+
1614
+ /**
1615
+ * Private helper for $$truncate() to check whether a char is a high surrogate.
1616
+ * @param {string} ch The char to check.
1617
+ * @return {boolean} Whether the given char is a unicode high surrogate.
1618
+ * @private
1619
+ */
1620
+ soy.$$isHighSurrogate_ = function(ch) {
1621
+ return 0xD800 <= ch && ch <= 0xDBFF;
1622
+ };
1623
+
1624
+ /**
1625
+ * Private helper for $$truncate() to check whether a char is a low surrogate.
1626
+ * @param {string} ch The char to check.
1627
+ * @return {boolean} Whether the given char is a unicode low surrogate.
1628
+ * @private
1629
+ */
1630
+ soy.$$isLowSurrogate_ = function(ch) {
1631
+ return 0xDC00 <= ch && ch <= 0xDFFF;
1632
+ };
1633
+
1634
+
1635
+ // -----------------------------------------------------------------------------
1636
+ // Bidi directives/functions.
1637
+
1638
+
1639
+ /**
1640
+ * Cache of bidi formatter by context directionality, so we don't keep on
1641
+ * creating new objects.
1642
+ * @type {!Object.<!goog.i18n.BidiFormatter>}
1643
+ * @private
1644
+ */
1645
+ soy.$$bidiFormatterCache_ = {};
1646
+
1647
+
1648
+ /**
1649
+ * Returns cached bidi formatter for bidiGlobalDir, or creates a new one.
1650
+ * @param {number} bidiGlobalDir The global directionality context: 1 if ltr, -1
1651
+ * if rtl, 0 if unknown.
1652
+ * @return {goog.i18n.BidiFormatter} A formatter for bidiGlobalDir.
1653
+ * @private
1654
+ */
1655
+ soy.$$getBidiFormatterInstance_ = function(bidiGlobalDir) {
1656
+ return soy.$$bidiFormatterCache_[bidiGlobalDir] ||
1657
+ (soy.$$bidiFormatterCache_[bidiGlobalDir] =
1658
+ new goog.i18n.BidiFormatter(bidiGlobalDir));
1659
+ };
1660
+
1661
+
1662
+ /**
1663
+ * Estimate the overall directionality of text. If opt_isHtml, makes sure to
1664
+ * ignore the LTR nature of the mark-up and escapes in text, making the logic
1665
+ * suitable for HTML and HTML-escaped text.
1666
+ * @param {string} text The text whose directionality is to be estimated.
1667
+ * @param {boolean=} opt_isHtml Whether text is HTML/HTML-escaped.
1668
+ * Default: false.
1669
+ * @return {number} 1 if text is LTR, -1 if it is RTL, and 0 if it is neutral.
1670
+ */
1671
+ soy.$$bidiTextDir = function(text, opt_isHtml) {
1672
+ if (!text) {
1673
+ return 0;
1674
+ }
1675
+ return goog.i18n.bidi.detectRtlDirectionality(text, opt_isHtml) ? -1 : 1;
1676
+ };
1677
+
1678
+
1679
+ /**
1680
+ * Returns "dir=ltr" or "dir=rtl", depending on text's estimated
1681
+ * directionality, if it is not the same as bidiGlobalDir.
1682
+ * Otherwise, returns the empty string.
1683
+ * If opt_isHtml, makes sure to ignore the LTR nature of the mark-up and escapes
1684
+ * in text, making the logic suitable for HTML and HTML-escaped text.
1685
+ * @param {number} bidiGlobalDir The global directionality context: 1 if ltr, -1
1686
+ * if rtl, 0 if unknown.
1687
+ * @param {string} text The text whose directionality is to be estimated.
1688
+ * @param {boolean=} opt_isHtml Whether text is HTML/HTML-escaped.
1689
+ * Default: false.
1690
+ * @return {soydata.SanitizedHtmlAttribute} "dir=rtl" for RTL text in non-RTL
1691
+ * context; "dir=ltr" for LTR text in non-LTR context;
1692
+ * else, the empty string.
1693
+ */
1694
+ soy.$$bidiDirAttr = function(bidiGlobalDir, text, opt_isHtml) {
1695
+ return new soydata.SanitizedHtmlAttribute(
1696
+ soy.$$getBidiFormatterInstance_(bidiGlobalDir).dirAttr(text, opt_isHtml));
1697
+ };
1698
+
1699
+
1700
+ /**
1701
+ * Returns a Unicode BiDi mark matching bidiGlobalDir (LRM or RLM) if the
1702
+ * directionality or the exit directionality of text are opposite to
1703
+ * bidiGlobalDir. Otherwise returns the empty string.
1704
+ * If opt_isHtml, makes sure to ignore the LTR nature of the mark-up and escapes
1705
+ * in text, making the logic suitable for HTML and HTML-escaped text.
1706
+ * @param {number} bidiGlobalDir The global directionality context: 1 if ltr, -1
1707
+ * if rtl, 0 if unknown.
1708
+ * @param {string} text The text whose directionality is to be estimated.
1709
+ * @param {boolean=} opt_isHtml Whether text is HTML/HTML-escaped.
1710
+ * Default: false.
1711
+ * @return {string} A Unicode bidi mark matching bidiGlobalDir, or the empty
1712
+ * string when text's overall and exit directionalities both match
1713
+ * bidiGlobalDir, or bidiGlobalDir is 0 (unknown).
1714
+ */
1715
+ soy.$$bidiMarkAfter = function(bidiGlobalDir, text, opt_isHtml) {
1716
+ var formatter = soy.$$getBidiFormatterInstance_(bidiGlobalDir);
1717
+ return formatter.markAfter(text, opt_isHtml);
1718
+ };
1719
+
1720
+
1721
+ /**
1722
+ * Returns str wrapped in a <span dir=ltr|rtl> according to its directionality -
1723
+ * but only if that is neither neutral nor the same as the global context.
1724
+ * Otherwise, returns str unchanged.
1725
+ * Always treats str as HTML/HTML-escaped, i.e. ignores mark-up and escapes when
1726
+ * estimating str's directionality.
1727
+ * @param {number} bidiGlobalDir The global directionality context: 1 if ltr, -1
1728
+ * if rtl, 0 if unknown.
1729
+ * @param {*} str The string to be wrapped. Can be other types, but the value
1730
+ * will be coerced to a string.
1731
+ * @return {string} The wrapped string.
1732
+ */
1733
+ soy.$$bidiSpanWrap = function(bidiGlobalDir, str) {
1734
+ var formatter = soy.$$getBidiFormatterInstance_(bidiGlobalDir);
1735
+ return formatter.spanWrap(str + '', true);
1736
+ };
1737
+
1738
+
1739
+ /**
1740
+ * Returns str wrapped in Unicode BiDi formatting characters according to its
1741
+ * directionality, i.e. either LRE or RLE at the beginning and PDF at the end -
1742
+ * but only if str's directionality is neither neutral nor the same as the
1743
+ * global context. Otherwise, returns str unchanged.
1744
+ * Always treats str as HTML/HTML-escaped, i.e. ignores mark-up and escapes when
1745
+ * estimating str's directionality.
1746
+ * @param {number} bidiGlobalDir The global directionality context: 1 if ltr, -1
1747
+ * if rtl, 0 if unknown.
1748
+ * @param {*} str The string to be wrapped. Can be other types, but the value
1749
+ * will be coerced to a string.
1750
+ * @return {string} The wrapped string.
1751
+ */
1752
+ soy.$$bidiUnicodeWrap = function(bidiGlobalDir, str) {
1753
+ var formatter = soy.$$getBidiFormatterInstance_(bidiGlobalDir);
1754
+ return formatter.unicodeWrap(str + '', true);
1755
+ };
1756
+
1757
+
1758
+ // -----------------------------------------------------------------------------
1759
+ // Generated code.
1760
+
1761
+
1762
+
1763
+
1764
+ // START GENERATED CODE FOR ESCAPERS.
1765
+
1766
+ /**
1767
+ * @type {function (*) : string}
1768
+ */
1769
+ soy.esc.$$escapeUriHelper = function(v) {
1770
+ return encodeURIComponent(String(v));
1771
+ };
1772
+
1773
+ /**
1774
+ * Maps charcters to the escaped versions for the named escape directives.
1775
+ * @type {Object.<string, string>}
1776
+ * @private
1777
+ */
1778
+ soy.esc.$$ESCAPE_MAP_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_ = {
1779
+ '\x00': '\x26#0;',
1780
+ '\x22': '\x26quot;',
1781
+ '\x26': '\x26amp;',
1782
+ '\x27': '\x26#39;',
1783
+ '\x3c': '\x26lt;',
1784
+ '\x3e': '\x26gt;',
1785
+ '\x09': '\x26#9;',
1786
+ '\x0a': '\x26#10;',
1787
+ '\x0b': '\x26#11;',
1788
+ '\x0c': '\x26#12;',
1789
+ '\x0d': '\x26#13;',
1790
+ ' ': '\x26#32;',
1791
+ '-': '\x26#45;',
1792
+ '\/': '\x26#47;',
1793
+ '\x3d': '\x26#61;',
1794
+ '`': '\x26#96;',
1795
+ '\x85': '\x26#133;',
1796
+ '\xa0': '\x26#160;',
1797
+ '\u2028': '\x26#8232;',
1798
+ '\u2029': '\x26#8233;'
1799
+ };
1800
+
1801
+ /**
1802
+ * A function that can be used with String.replace..
1803
+ * @param {string} ch A single character matched by a compatible matcher.
1804
+ * @return {string} A token in the output language.
1805
+ * @private
1806
+ */
1807
+ soy.esc.$$REPLACER_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_ = function(ch) {
1808
+ return soy.esc.$$ESCAPE_MAP_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_[ch];
1809
+ };
1810
+
1811
+ /**
1812
+ * Maps charcters to the escaped versions for the named escape directives.
1813
+ * @type {Object.<string, string>}
1814
+ * @private
1815
+ */
1816
+ soy.esc.$$ESCAPE_MAP_FOR_ESCAPE_JS_STRING__AND__ESCAPE_JS_REGEX_ = {
1817
+ '\x00': '\\x00',
1818
+ '\x08': '\\x08',
1819
+ '\x09': '\\t',
1820
+ '\x0a': '\\n',
1821
+ '\x0b': '\\x0b',
1822
+ '\x0c': '\\f',
1823
+ '\x0d': '\\r',
1824
+ '\x22': '\\x22',
1825
+ '\x26': '\\x26',
1826
+ '\x27': '\\x27',
1827
+ '\/': '\\\/',
1828
+ '\x3c': '\\x3c',
1829
+ '\x3d': '\\x3d',
1830
+ '\x3e': '\\x3e',
1831
+ '\\': '\\\\',
1832
+ '\x85': '\\x85',
1833
+ '\u2028': '\\u2028',
1834
+ '\u2029': '\\u2029',
1835
+ '$': '\\x24',
1836
+ '(': '\\x28',
1837
+ ')': '\\x29',
1838
+ '*': '\\x2a',
1839
+ '+': '\\x2b',
1840
+ ',': '\\x2c',
1841
+ '-': '\\x2d',
1842
+ '.': '\\x2e',
1843
+ ':': '\\x3a',
1844
+ '?': '\\x3f',
1845
+ '[': '\\x5b',
1846
+ ']': '\\x5d',
1847
+ '^': '\\x5e',
1848
+ '{': '\\x7b',
1849
+ '|': '\\x7c',
1850
+ '}': '\\x7d'
1851
+ };
1852
+
1853
+ /**
1854
+ * A function that can be used with String.replace..
1855
+ * @param {string} ch A single character matched by a compatible matcher.
1856
+ * @return {string} A token in the output language.
1857
+ * @private
1858
+ */
1859
+ soy.esc.$$REPLACER_FOR_ESCAPE_JS_STRING__AND__ESCAPE_JS_REGEX_ = function(ch) {
1860
+ return soy.esc.$$ESCAPE_MAP_FOR_ESCAPE_JS_STRING__AND__ESCAPE_JS_REGEX_[ch];
1861
+ };
1862
+
1863
+ /**
1864
+ * Maps charcters to the escaped versions for the named escape directives.
1865
+ * @type {Object.<string, string>}
1866
+ * @private
1867
+ */
1868
+ soy.esc.$$ESCAPE_MAP_FOR_ESCAPE_CSS_STRING_ = {
1869
+ '\x00': '\\0 ',
1870
+ '\x08': '\\8 ',
1871
+ '\x09': '\\9 ',
1872
+ '\x0a': '\\a ',
1873
+ '\x0b': '\\b ',
1874
+ '\x0c': '\\c ',
1875
+ '\x0d': '\\d ',
1876
+ '\x22': '\\22 ',
1877
+ '\x26': '\\26 ',
1878
+ '\x27': '\\27 ',
1879
+ '(': '\\28 ',
1880
+ ')': '\\29 ',
1881
+ '*': '\\2a ',
1882
+ '\/': '\\2f ',
1883
+ ':': '\\3a ',
1884
+ ';': '\\3b ',
1885
+ '\x3c': '\\3c ',
1886
+ '\x3d': '\\3d ',
1887
+ '\x3e': '\\3e ',
1888
+ '@': '\\40 ',
1889
+ '\\': '\\5c ',
1890
+ '{': '\\7b ',
1891
+ '}': '\\7d ',
1892
+ '\x85': '\\85 ',
1893
+ '\xa0': '\\a0 ',
1894
+ '\u2028': '\\2028 ',
1895
+ '\u2029': '\\2029 '
1896
+ };
1897
+
1898
+ /**
1899
+ * A function that can be used with String.replace..
1900
+ * @param {string} ch A single character matched by a compatible matcher.
1901
+ * @return {string} A token in the output language.
1902
+ * @private
1903
+ */
1904
+ soy.esc.$$REPLACER_FOR_ESCAPE_CSS_STRING_ = function(ch) {
1905
+ return soy.esc.$$ESCAPE_MAP_FOR_ESCAPE_CSS_STRING_[ch];
1906
+ };
1907
+
1908
+ /**
1909
+ * Maps charcters to the escaped versions for the named escape directives.
1910
+ * @type {Object.<string, string>}
1911
+ * @private
1912
+ */
1913
+ soy.esc.$$ESCAPE_MAP_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_ = {
1914
+ '\x00': '%00',
1915
+ '\x01': '%01',
1916
+ '\x02': '%02',
1917
+ '\x03': '%03',
1918
+ '\x04': '%04',
1919
+ '\x05': '%05',
1920
+ '\x06': '%06',
1921
+ '\x07': '%07',
1922
+ '\x08': '%08',
1923
+ '\x09': '%09',
1924
+ '\x0a': '%0A',
1925
+ '\x0b': '%0B',
1926
+ '\x0c': '%0C',
1927
+ '\x0d': '%0D',
1928
+ '\x0e': '%0E',
1929
+ '\x0f': '%0F',
1930
+ '\x10': '%10',
1931
+ '\x11': '%11',
1932
+ '\x12': '%12',
1933
+ '\x13': '%13',
1934
+ '\x14': '%14',
1935
+ '\x15': '%15',
1936
+ '\x16': '%16',
1937
+ '\x17': '%17',
1938
+ '\x18': '%18',
1939
+ '\x19': '%19',
1940
+ '\x1a': '%1A',
1941
+ '\x1b': '%1B',
1942
+ '\x1c': '%1C',
1943
+ '\x1d': '%1D',
1944
+ '\x1e': '%1E',
1945
+ '\x1f': '%1F',
1946
+ ' ': '%20',
1947
+ '\x22': '%22',
1948
+ '\x27': '%27',
1949
+ '(': '%28',
1950
+ ')': '%29',
1951
+ '\x3c': '%3C',
1952
+ '\x3e': '%3E',
1953
+ '\\': '%5C',
1954
+ '{': '%7B',
1955
+ '}': '%7D',
1956
+ '\x7f': '%7F',
1957
+ '\x85': '%C2%85',
1958
+ '\xa0': '%C2%A0',
1959
+ '\u2028': '%E2%80%A8',
1960
+ '\u2029': '%E2%80%A9',
1961
+ '\uff01': '%EF%BC%81',
1962
+ '\uff03': '%EF%BC%83',
1963
+ '\uff04': '%EF%BC%84',
1964
+ '\uff06': '%EF%BC%86',
1965
+ '\uff07': '%EF%BC%87',
1966
+ '\uff08': '%EF%BC%88',
1967
+ '\uff09': '%EF%BC%89',
1968
+ '\uff0a': '%EF%BC%8A',
1969
+ '\uff0b': '%EF%BC%8B',
1970
+ '\uff0c': '%EF%BC%8C',
1971
+ '\uff0f': '%EF%BC%8F',
1972
+ '\uff1a': '%EF%BC%9A',
1973
+ '\uff1b': '%EF%BC%9B',
1974
+ '\uff1d': '%EF%BC%9D',
1975
+ '\uff1f': '%EF%BC%9F',
1976
+ '\uff20': '%EF%BC%A0',
1977
+ '\uff3b': '%EF%BC%BB',
1978
+ '\uff3d': '%EF%BC%BD'
1979
+ };
1980
+
1981
+ /**
1982
+ * A function that can be used with String.replace..
1983
+ * @param {string} ch A single character matched by a compatible matcher.
1984
+ * @return {string} A token in the output language.
1985
+ * @private
1986
+ */
1987
+ soy.esc.$$REPLACER_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_ = function(ch) {
1988
+ return soy.esc.$$ESCAPE_MAP_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_[ch];
1989
+ };
1990
+
1991
+ /**
1992
+ * Matches characters that need to be escaped for the named directives.
1993
+ * @type RegExp
1994
+ * @private
1995
+ */
1996
+ soy.esc.$$MATCHER_FOR_ESCAPE_HTML_ = /[\x00\x22\x26\x27\x3c\x3e]/g;
1997
+
1998
+ /**
1999
+ * Matches characters that need to be escaped for the named directives.
2000
+ * @type RegExp
2001
+ * @private
2002
+ */
2003
+ soy.esc.$$MATCHER_FOR_NORMALIZE_HTML_ = /[\x00\x22\x27\x3c\x3e]/g;
2004
+
2005
+ /**
2006
+ * Matches characters that need to be escaped for the named directives.
2007
+ * @type RegExp
2008
+ * @private
2009
+ */
2010
+ soy.esc.$$MATCHER_FOR_ESCAPE_HTML_NOSPACE_ = /[\x00\x09-\x0d \x22\x26\x27\x2d\/\x3c-\x3e`\x85\xa0\u2028\u2029]/g;
2011
+
2012
+ /**
2013
+ * Matches characters that need to be escaped for the named directives.
2014
+ * @type RegExp
2015
+ * @private
2016
+ */
2017
+ soy.esc.$$MATCHER_FOR_NORMALIZE_HTML_NOSPACE_ = /[\x00\x09-\x0d \x22\x27\x2d\/\x3c-\x3e`\x85\xa0\u2028\u2029]/g;
2018
+
2019
+ /**
2020
+ * Matches characters that need to be escaped for the named directives.
2021
+ * @type RegExp
2022
+ * @private
2023
+ */
2024
+ soy.esc.$$MATCHER_FOR_ESCAPE_JS_STRING_ = /[\x00\x08-\x0d\x22\x26\x27\/\x3c-\x3e\\\x85\u2028\u2029]/g;
2025
+
2026
+ /**
2027
+ * Matches characters that need to be escaped for the named directives.
2028
+ * @type RegExp
2029
+ * @private
2030
+ */
2031
+ soy.esc.$$MATCHER_FOR_ESCAPE_JS_REGEX_ = /[\x00\x08-\x0d\x22\x24\x26-\/\x3a\x3c-\x3f\x5b-\x5e\x7b-\x7d\x85\u2028\u2029]/g;
2032
+
2033
+ /**
2034
+ * Matches characters that need to be escaped for the named directives.
2035
+ * @type RegExp
2036
+ * @private
2037
+ */
2038
+ soy.esc.$$MATCHER_FOR_ESCAPE_CSS_STRING_ = /[\x00\x08-\x0d\x22\x26-\x2a\/\x3a-\x3e@\\\x7b\x7d\x85\xa0\u2028\u2029]/g;
2039
+
2040
+ /**
2041
+ * Matches characters that need to be escaped for the named directives.
2042
+ * @type RegExp
2043
+ * @private
2044
+ */
2045
+ soy.esc.$$MATCHER_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_ = /[\x00- \x22\x27-\x29\x3c\x3e\\\x7b\x7d\x7f\x85\xa0\u2028\u2029\uff01\uff03\uff04\uff06-\uff0c\uff0f\uff1a\uff1b\uff1d\uff1f\uff20\uff3b\uff3d]/g;
2046
+
2047
+ /**
2048
+ * A pattern that vets values produced by the named directives.
2049
+ * @type RegExp
2050
+ * @private
2051
+ */
2052
+ soy.esc.$$FILTER_FOR_FILTER_CSS_VALUE_ = /^(?!-*(?:expression|(?:moz-)?binding))(?:[.#]?-?(?:[_a-z0-9-]+)(?:-[_a-z0-9-]+)*-?|-?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[a-z]{1,2}|%)?|!important|)$/i;
2053
+
2054
+ /**
2055
+ * A pattern that vets values produced by the named directives.
2056
+ * @type RegExp
2057
+ * @private
2058
+ */
2059
+ soy.esc.$$FILTER_FOR_FILTER_NORMALIZE_URI_ = /^(?:(?:https?|mailto):|[^&:\/?#]*(?:[\/?#]|$))/i;
2060
+
2061
+ /**
2062
+ * A pattern that vets values produced by the named directives.
2063
+ * @type RegExp
2064
+ * @private
2065
+ */
2066
+ soy.esc.$$FILTER_FOR_FILTER_HTML_ATTRIBUTE_ = /^(?!style|on|action|archive|background|cite|classid|codebase|data|dsync|href|longdesc|src|usemap)(?:[a-z0-9_$:-]*)$/i;
2067
+
2068
+ /**
2069
+ * A pattern that vets values produced by the named directives.
2070
+ * @type RegExp
2071
+ * @private
2072
+ */
2073
+ soy.esc.$$FILTER_FOR_FILTER_HTML_ELEMENT_NAME_ = /^(?!script|style|title|textarea|xmp|no)[a-z0-9_$:-]*$/i;
2074
+
2075
+ /**
2076
+ * A helper for the Soy directive |escapeHtml
2077
+ * @param {*} value Can be of any type but will be coerced to a string.
2078
+ * @return {string} The escaped text.
2079
+ */
2080
+ soy.esc.$$escapeHtmlHelper = function(value) {
2081
+ var str = String(value);
2082
+ return str.replace(
2083
+ soy.esc.$$MATCHER_FOR_ESCAPE_HTML_,
2084
+ soy.esc.$$REPLACER_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_);
2085
+ };
2086
+
2087
+ /**
2088
+ * A helper for the Soy directive |normalizeHtml
2089
+ * @param {*} value Can be of any type but will be coerced to a string.
2090
+ * @return {string} The escaped text.
2091
+ */
2092
+ soy.esc.$$normalizeHtmlHelper = function(value) {
2093
+ var str = String(value);
2094
+ return str.replace(
2095
+ soy.esc.$$MATCHER_FOR_NORMALIZE_HTML_,
2096
+ soy.esc.$$REPLACER_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_);
2097
+ };
2098
+
2099
+ /**
2100
+ * A helper for the Soy directive |escapeHtmlNospace
2101
+ * @param {*} value Can be of any type but will be coerced to a string.
2102
+ * @return {string} The escaped text.
2103
+ */
2104
+ soy.esc.$$escapeHtmlNospaceHelper = function(value) {
2105
+ var str = String(value);
2106
+ return str.replace(
2107
+ soy.esc.$$MATCHER_FOR_ESCAPE_HTML_NOSPACE_,
2108
+ soy.esc.$$REPLACER_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_);
2109
+ };
2110
+
2111
+ /**
2112
+ * A helper for the Soy directive |normalizeHtmlNospace
2113
+ * @param {*} value Can be of any type but will be coerced to a string.
2114
+ * @return {string} The escaped text.
2115
+ */
2116
+ soy.esc.$$normalizeHtmlNospaceHelper = function(value) {
2117
+ var str = String(value);
2118
+ return str.replace(
2119
+ soy.esc.$$MATCHER_FOR_NORMALIZE_HTML_NOSPACE_,
2120
+ soy.esc.$$REPLACER_FOR_ESCAPE_HTML__AND__NORMALIZE_HTML__AND__ESCAPE_HTML_NOSPACE__AND__NORMALIZE_HTML_NOSPACE_);
2121
+ };
2122
+
2123
+ /**
2124
+ * A helper for the Soy directive |escapeJsString
2125
+ * @param {*} value Can be of any type but will be coerced to a string.
2126
+ * @return {string} The escaped text.
2127
+ */
2128
+ soy.esc.$$escapeJsStringHelper = function(value) {
2129
+ var str = String(value);
2130
+ return str.replace(
2131
+ soy.esc.$$MATCHER_FOR_ESCAPE_JS_STRING_,
2132
+ soy.esc.$$REPLACER_FOR_ESCAPE_JS_STRING__AND__ESCAPE_JS_REGEX_);
2133
+ };
2134
+
2135
+ /**
2136
+ * A helper for the Soy directive |escapeJsRegex
2137
+ * @param {*} value Can be of any type but will be coerced to a string.
2138
+ * @return {string} The escaped text.
2139
+ */
2140
+ soy.esc.$$escapeJsRegexHelper = function(value) {
2141
+ var str = String(value);
2142
+ return str.replace(
2143
+ soy.esc.$$MATCHER_FOR_ESCAPE_JS_REGEX_,
2144
+ soy.esc.$$REPLACER_FOR_ESCAPE_JS_STRING__AND__ESCAPE_JS_REGEX_);
2145
+ };
2146
+
2147
+ /**
2148
+ * A helper for the Soy directive |escapeCssString
2149
+ * @param {*} value Can be of any type but will be coerced to a string.
2150
+ * @return {string} The escaped text.
2151
+ */
2152
+ soy.esc.$$escapeCssStringHelper = function(value) {
2153
+ var str = String(value);
2154
+ return str.replace(
2155
+ soy.esc.$$MATCHER_FOR_ESCAPE_CSS_STRING_,
2156
+ soy.esc.$$REPLACER_FOR_ESCAPE_CSS_STRING_);
2157
+ };
2158
+
2159
+ /**
2160
+ * A helper for the Soy directive |filterCssValue
2161
+ * @param {*} value Can be of any type but will be coerced to a string.
2162
+ * @return {string} The escaped text.
2163
+ */
2164
+ soy.esc.$$filterCssValueHelper = function(value) {
2165
+ var str = String(value);
2166
+ if (!soy.esc.$$FILTER_FOR_FILTER_CSS_VALUE_.test(str)) {
2167
+ return 'zSoyz';
2168
+ }
2169
+ return str;
2170
+ };
2171
+
2172
+ /**
2173
+ * A helper for the Soy directive |normalizeUri
2174
+ * @param {*} value Can be of any type but will be coerced to a string.
2175
+ * @return {string} The escaped text.
2176
+ */
2177
+ soy.esc.$$normalizeUriHelper = function(value) {
2178
+ var str = String(value);
2179
+ return str.replace(
2180
+ soy.esc.$$MATCHER_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_,
2181
+ soy.esc.$$REPLACER_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_);
2182
+ };
2183
+
2184
+ /**
2185
+ * A helper for the Soy directive |filterNormalizeUri
2186
+ * @param {*} value Can be of any type but will be coerced to a string.
2187
+ * @return {string} The escaped text.
2188
+ */
2189
+ soy.esc.$$filterNormalizeUriHelper = function(value) {
2190
+ var str = String(value);
2191
+ if (!soy.esc.$$FILTER_FOR_FILTER_NORMALIZE_URI_.test(str)) {
2192
+ return 'zSoyz';
2193
+ }
2194
+ return str.replace(
2195
+ soy.esc.$$MATCHER_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_,
2196
+ soy.esc.$$REPLACER_FOR_NORMALIZE_URI__AND__FILTER_NORMALIZE_URI_);
2197
+ };
2198
+
2199
+ /**
2200
+ * A helper for the Soy directive |filterHtmlAttribute
2201
+ * @param {*} value Can be of any type but will be coerced to a string.
2202
+ * @return {string} The escaped text.
2203
+ */
2204
+ soy.esc.$$filterHtmlAttributeHelper = function(value) {
2205
+ var str = String(value);
2206
+ if (!soy.esc.$$FILTER_FOR_FILTER_HTML_ATTRIBUTE_.test(str)) {
2207
+ return 'zSoyz';
2208
+ }
2209
+ return str;
2210
+ };
2211
+
2212
+ /**
2213
+ * A helper for the Soy directive |filterHtmlElementName
2214
+ * @param {*} value Can be of any type but will be coerced to a string.
2215
+ * @return {string} The escaped text.
2216
+ */
2217
+ soy.esc.$$filterHtmlElementNameHelper = function(value) {
2218
+ var str = String(value);
2219
+ if (!soy.esc.$$FILTER_FOR_FILTER_HTML_ELEMENT_NAME_.test(str)) {
2220
+ return 'zSoyz';
2221
+ }
2222
+ return str;
2223
+ };
2224
+
2225
+ /**
2226
+ * Matches all tags, HTML comments, and DOCTYPEs in tag soup HTML.
2227
+ *
2228
+ * @type {RegExp}
2229
+ * @private
2230
+ */
2231
+ soy.esc.$$HTML_TAG_REGEX_ = /<(?:!|\/?[a-zA-Z])(?:[^>'"]|"[^"]*"|'[^']*')*>/g;
2232
+
2233
+ // END GENERATED CODE