diff --git a/closure/goog/soy/data.js b/closure/goog/soy/data.js index 8c3b84bfdc..1193b97c41 100644 --- a/closure/goog/soy/data.js +++ b/closure/goog/soy/data.js @@ -554,4 +554,22 @@ goog.soy.data.SanitizedCss.prototype.toSafeStyleSheet = function() { 'value.'), value); }; + + +/** + * Converts SanitizedCss into SafeStyle. + * @return {!goog.html.SafeStyle} + */ +goog.soy.data.SanitizedCss.prototype.toSafeStyle = function() { + 'use strict'; + const value = this.toString(); + goog.asserts.assert( + !/{/.test(value), 'value doesn\'t look like style: ' + value); + return goog.html.uncheckedconversions + .safeStyleFromStringKnownToSatisfyTypeContract( + goog.string.Const.from( + 'Soy SanitizedCss produces SafeStyle-contract-compliant value.'), + value); +}; + }); diff --git a/closure/goog/soy/data_test.js b/closure/goog/soy/data_test.js index 4c5c23a9a0..7348792ef2 100644 --- a/closure/goog/soy/data_test.js +++ b/closure/goog/soy/data_test.js @@ -8,6 +8,7 @@ goog.module('goog.soy.dataTest'); goog.setTestOnly(); const SafeHtml = goog.require('goog.html.SafeHtml'); +const SafeStyle = goog.require('goog.html.SafeStyle'); const SafeStyleSheet = goog.require('goog.html.SafeStyleSheet'); const SafeUrl = goog.require('goog.html.SafeUrl'); const TrustedResourceUrl = goog.require('goog.html.TrustedResourceUrl'); @@ -37,9 +38,13 @@ testSuite({ }, testToSafeStyleSheet() { - /** @suppress {checkTypes} suppression added to enable type checking */ - const url = example.sanitizedCssTemplate().toSafeStyleSheet(); - assertEquals('html{display:none}', SafeStyleSheet.unwrap(url)); + const styleSheet = example.sanitizedCssTemplate({}).toSafeStyleSheet(); + assertEquals('html{display:none}', SafeStyleSheet.unwrap(styleSheet)); + }, + + testToSafeStyle() { + const style = example.sanitizedStyleTemplate({}).toSafeStyle(); + assertEquals('display:none;', SafeStyle.unwrap(style)); }, testToTrustedResourceUrl() { diff --git a/closure/goog/soy/soy_testhelper.js b/closure/goog/soy/soy_testhelper.js index d366b62885..503e6316d6 100644 --- a/closure/goog/soy/soy_testhelper.js +++ b/closure/goog/soy/soy_testhelper.js @@ -243,7 +243,7 @@ example.sanitizedTrustedResourceUriTemplate = function(data, opt_injectedData) { /** - * @param {{name: string}} data + * @param {!Object} data * @param {Object=} opt_injectedData * @return {!goog.soy.data.SanitizedCss} */ @@ -253,6 +253,17 @@ example.sanitizedCssTemplate = function(data, opt_injectedData) { }; +/** + * @param {!Object} data + * @param {!Object=} opt_injectedData + * @return {!goog.soy.data.SanitizedCss} + */ +example.sanitizedStyleTemplate = function(data, opt_injectedData) { + 'use strict'; + return new SanitizedCssSubclass('display:none;'); +}; + + /** * @param {{name: string}} data * @param {Object=} opt_injectedData