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

Commit db33484

Browse files
vranacopybara-github
authored andcommitted
RELNOTES: Soy: Add toSafeStyle.
It's already present in Java. PiperOrigin-RevId: 509840296 Change-Id: I2172ac90743585448c34ba1e9cbd70a9ee9e4ff1
1 parent ed8e4fb commit db33484

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

closure/goog/soy/data.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,22 @@ goog.soy.data.SanitizedCss.prototype.toSafeStyleSheet = function() {
554554
'value.'),
555555
value);
556556
};
557+
558+
559+
/**
560+
* Converts SanitizedCss into SafeStyle.
561+
* @return {!goog.html.SafeStyle}
562+
*/
563+
goog.soy.data.SanitizedCss.prototype.toSafeStyle = function() {
564+
'use strict';
565+
const value = this.toString();
566+
goog.asserts.assert(
567+
!/{/.test(value), 'value doesn\'t look like style: ' + value);
568+
return goog.html.uncheckedconversions
569+
.safeStyleFromStringKnownToSatisfyTypeContract(
570+
goog.string.Const.from(
571+
'Soy SanitizedCss produces SafeStyle-contract-compliant value.'),
572+
value);
573+
};
574+
557575
});

closure/goog/soy/data_test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ goog.module('goog.soy.dataTest');
88
goog.setTestOnly();
99

1010
const SafeHtml = goog.require('goog.html.SafeHtml');
11+
const SafeStyle = goog.require('goog.html.SafeStyle');
1112
const SafeStyleSheet = goog.require('goog.html.SafeStyleSheet');
1213
const SafeUrl = goog.require('goog.html.SafeUrl');
1314
const TrustedResourceUrl = goog.require('goog.html.TrustedResourceUrl');
@@ -37,9 +38,13 @@ testSuite({
3738
},
3839

3940
testToSafeStyleSheet() {
40-
/** @suppress {checkTypes} suppression added to enable type checking */
41-
const url = example.sanitizedCssTemplate().toSafeStyleSheet();
42-
assertEquals('html{display:none}', SafeStyleSheet.unwrap(url));
41+
const styleSheet = example.sanitizedCssTemplate({}).toSafeStyleSheet();
42+
assertEquals('html{display:none}', SafeStyleSheet.unwrap(styleSheet));
43+
},
44+
45+
testToSafeStyle() {
46+
const style = example.sanitizedStyleTemplate({}).toSafeStyle();
47+
assertEquals('display:none;', SafeStyle.unwrap(style));
4348
},
4449

4550
testToTrustedResourceUrl() {

closure/goog/soy/soy_testhelper.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ example.sanitizedTrustedResourceUriTemplate = function(data, opt_injectedData) {
243243

244244

245245
/**
246-
* @param {{name: string}} data
246+
* @param {!Object<string, *>} data
247247
* @param {Object<string, *>=} opt_injectedData
248248
* @return {!goog.soy.data.SanitizedCss}
249249
*/
@@ -253,6 +253,17 @@ example.sanitizedCssTemplate = function(data, opt_injectedData) {
253253
};
254254

255255

256+
/**
257+
* @param {!Object<string, *>} data
258+
* @param {!Object<string, *>=} opt_injectedData
259+
* @return {!goog.soy.data.SanitizedCss}
260+
*/
261+
example.sanitizedStyleTemplate = function(data, opt_injectedData) {
262+
'use strict';
263+
return new SanitizedCssSubclass('display:none;');
264+
};
265+
266+
256267
/**
257268
* @param {{name: string}} data
258269
* @param {Object<string, *>=} opt_injectedData

0 commit comments

Comments
 (0)