This repository was archived by the owner on Aug 1, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff line change @@ -303,6 +303,25 @@ function assertFirstTagValid(html) {
303303const INVALID_TAG_TO_RENDER =
304304 / ^ < ( b o d y | c a p t i o n | c o l | c o l g r o u p | h e a d | h t m l | t r | t d | t h | t b o d y | t h e a d | t f o o t ) > / 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.
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments