// 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_ = {};