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

Commit

Permalink
RELNOTES: Soy: Add toSafeStyle.
Browse files Browse the repository at this point in the history
It's already present in Java.

PiperOrigin-RevId: 509840296
Change-Id: I2172ac90743585448c34ba1e9cbd70a9ee9e4ff1
  • Loading branch information
vrana authored and copybara-github committed Feb 15, 2023
1 parent ed8e4fb commit db33484
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
18 changes: 18 additions & 0 deletions closure/goog/soy/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

});
11 changes: 8 additions & 3 deletions closure/goog/soy/data_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 12 additions & 1 deletion closure/goog/soy/soy_testhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ example.sanitizedTrustedResourceUriTemplate = function(data, opt_injectedData) {


/**
* @param {{name: string}} data
* @param {!Object<string, *>} data
* @param {Object<string, *>=} opt_injectedData
* @return {!goog.soy.data.SanitizedCss}
*/
Expand All @@ -253,6 +253,17 @@ example.sanitizedCssTemplate = function(data, opt_injectedData) {
};


/**
* @param {!Object<string, *>} data
* @param {!Object<string, *>=} 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<string, *>=} opt_injectedData
Expand Down

0 comments on commit db33484

Please sign in to comment.