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
|
// 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 utility methods to render soy template.
*/
goog.provide('goog.soy');
goog.require('goog.dom');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.TagName');
/**
* Renders a Soy template and then set the output string as
* the innerHTML of an element. It is recommended to use this helper function
* instead of directly setting innerHTML in your hand-written code, so that it
* will be easier to audit the code for cross-site scripting vulnerabilities.
*
* @param {Element} element The element whose content we are rendering into.
* @param {Function} template The Soy template defining the element's content.
* @param {Object=} opt_templateData The data for the template.
* @param {Object=} opt_injectedData The injected data for the template.
*/
goog.soy.renderElement = function(element, template, opt_templateData,
opt_injectedData) {
element.innerHTML = template(
opt_templateData || goog.soy.defaultTemplateData_, undefined,
opt_injectedData);
};
/**
* Renders a Soy template into a single node or a document
* fragment. If the rendered HTML string represents a single node, then that
* node is returned (note that this is *not* a fragment, despite them name of
* the method). Otherwise a document fragment is returned containing the
* rendered nodes.
*
* @param {Function} template The Soy template defining the element's content.
* @param {Object=} opt_templateData The data for the template.
* @param {Object=} opt_injectedData The injected data for the template.
* @param {goog.dom.DomHelper=} opt_domHelper The DOM helper used to
* create DOM nodes; defaults to {@code goog.dom.getDomHelper}.
* @return {!Node} The resulting node or document fragment.
*/
goog.soy.renderAsFragment = function(template, opt_templateData,
opt_injectedData, opt_domHelper) {
var dom = opt_domHelper || goog.dom.getDomHelper();
return dom.htmlToDocumentFragment(
template(opt_templateData || goog.soy.defaultTemplateData_,
undefined, opt_injectedData));
};
/**
* Renders a Soy template into a single node. If the rendered
* HTML string represents a single node, then that node is returned. Otherwise,
* a DIV element is returned containing the rendered nodes.
*
* @param {Function} template The Soy template defining the element's content.
* @param {Object=} opt_templateData The data for the template.
* @param {Object=} opt_injectedData The injected data for the template.
* @param {goog.dom.DomHelper=} opt_domHelper The DOM helper used to
* create DOM nodes; defaults to {@code goog.dom.getDomHelper}.
* @return {!Element} Rendered template contents, wrapped in a parent DIV
* element if necessary.
*/
goog.soy.renderAsElement = function(template, opt_templateData,
opt_injectedData, opt_domHelper) {
var dom = opt_domHelper || goog.dom.getDomHelper();
var wrapper = dom.createElement(goog.dom.TagName.DIV);
wrapper.innerHTML = template(
opt_templateData || goog.soy.defaultTemplateData_,
undefined, opt_injectedData);
// If the template renders as a single element, return it.
if (wrapper.childNodes.length == 1) {
var firstChild = wrapper.firstChild;
if (firstChild.nodeType == goog.dom.NodeType.ELEMENT) {
return /** @type {!Element} */ (firstChild);
}
}
// Otherwise, return the wrapper DIV.
return wrapper;
};
/**
* Immutable object that is passed into templates that are rendered
* without any data.
* @type {Object}
* @private
*/
goog.soy.defaultTemplateData_ = {};
|