Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit d06d214

Browse files
Closure Teamcopybara-github
authored andcommitted
Make it possible to render a Soy template as text without constructing a renderer.
RELNOTES: Make it possible to render a Soy template as text without constructing a renderer. PiperOrigin-RevId: 540025428 Change-Id: I45ec5fe78a07c7c42e038ef1c33b66e6ecb13b9f
1 parent fd880ed commit d06d214

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

closure/goog/soy/renderer.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ class Renderer {
128128
* @template ARG_TYPES
129129
*/
130130
renderText(template, templateData = undefined) {
131-
const result = template(templateData || {}, this.getInjectedData_());
132-
asserts.assertString(
133-
result,
134-
'renderText was called with a template of kind other than "text"');
135-
return String(result);
131+
return soy.renderAsText(template, templateData, this.getInjectedData_());
136132
}
137133

138134
/**

closure/goog/soy/soy.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,25 @@ function assertFirstTagValid(html) {
303303
const INVALID_TAG_TO_RENDER =
304304
/^<(body|caption|col|colgroup|head|html|tr|td|th|tbody|thead|tfoot)>/i;
305305

306+
/**
307+
* Renders a Soy template as test.
308+
* @param {function(ARG_TYPES, ?CompatibleIj=): *} template The Soy template to
309+
* render.
310+
* @param {ARG_TYPES=} templateData The data for the template.
311+
* @param {?Object=} injectedData The injected data for the template.
312+
* @return {string}
313+
* @template ARG_TYPES
314+
*/
315+
function renderAsText(
316+
template, templateData = undefined, injectedData = undefined) {
317+
const result = template(templateData || defaultTemplateData, injectedData);
318+
asserts.assertString(
319+
result,
320+
'renderText was called with a template of kind other than "text"');
321+
return String(result);
322+
}
323+
exports.renderAsText = renderAsText;
324+
306325
/**
307326
* Immutable object that is passed into templates that are rendered
308327
* without any data.

closure/goog/soy/soy_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,20 @@ testSuite({
250250
'Expect grandchildDiv to still be contained in childDiv.', childDiv,
251251
grandchildDiv.parentElement);
252252
},
253+
254+
testRenderAsText() {
255+
// RenderText works on string templates.
256+
assertEquals('<b>XSS</b>', soy.renderAsText(example.stringTemplate));
257+
// RenderText on non-text template fails.
258+
assertEquals(
259+
'Assertion failed: ' +
260+
'renderText was called with a template of kind other than "text"',
261+
assertThrows(/**
262+
* @suppress {checkTypes} suppression added to enable type
263+
* checking.
264+
*/
265+
() => void soy.renderAsText(
266+
example.sanitizedHtmlTemplate, {}))
267+
.message);
268+
},
253269
});

0 commit comments

Comments
 (0)