closure-builder
Version:
Simple Closure, Soy and JavaScript Build system
306 lines (275 loc) • 12.2 kB
JavaScript
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview
* Value converters for protocol buffers in Soy that are semantically
* similar to Soy builtin types.
*
* <p>Calls to these are generated by
* com.google.template.soy.types.proto.SoyProtoTypeImpl.
*
*/
goog.provide('soydata.packSanitizedCssToSafeStyleProtoSoyRuntimeOnly');
goog.provide('soydata.packSanitizedCssToSafeStyleSheetProtoSoyRuntimeOnly');
goog.provide('soydata.packSanitizedHtmlToProtoSoyRuntimeOnly');
goog.provide('soydata.packSanitizedJsToProtoSoyRuntimeOnly');
goog.provide('soydata.packSanitizedTrustedResourceUriToProtoSoyRuntimeOnly');
goog.provide('soydata.packSanitizedUriToProtoSoyRuntimeOnly');
goog.provide('soydata.unpackProtoToSanitizedCss');
goog.provide('soydata.unpackProtoToSanitizedHtml');
goog.provide('soydata.unpackProtoToSanitizedJs');
goog.provide('soydata.unpackProtoToSanitizedTrustedResourceUri');
goog.provide('soydata.unpackProtoToSanitizedUri');
goog.require('goog.debug');
goog.require('goog.html.SafeHtml');
goog.require('goog.html.SafeScript');
goog.require('goog.html.SafeStyle');
goog.require('goog.html.SafeStyleSheet');
goog.require('goog.html.SafeUrl');
goog.require('goog.html.TrustedResourceUrl');
goog.require('goog.html.uncheckedconversions');
goog.require('goog.soy.data.SanitizedCss');
goog.require('goog.soy.data.SanitizedHtml');
goog.require('goog.soy.data.SanitizedJs');
goog.require('goog.soy.data.SanitizedTrustedResourceUri');
goog.require('goog.soy.data.SanitizedUri');
goog.require('goog.string');
goog.require('goog.string.Const');
goog.require('proto.webutil.html.types.SafeHtmlProto');
goog.require('proto.webutil.html.types.SafeScriptProto');
goog.require('proto.webutil.html.types.SafeStyleProto');
goog.require('proto.webutil.html.types.SafeStyleSheetProto');
goog.require('proto.webutil.html.types.SafeUrlProto');
goog.require('proto.webutil.html.types.TrustedResourceUrlProto');
goog.require('security.html.jspbconversions');
goog.require('soydata.VERY_UNSAFE');
/**
* Converts a CSS Sanitized Content object to a corresponding Safe Style Proto.
* @param {!goog.soy.data.SanitizedCss|string} sanitizedCss
* @return {!proto.webutil.html.types.SafeStyleProto}
*/
soydata.packSanitizedCssToSafeStyleProtoSoyRuntimeOnly = function(
sanitizedCss) {
if (sanitizedCss !== '' &&
!(sanitizedCss instanceof goog.soy.data.SanitizedCss)) {
throw new Error(
'expected SanitizedCss, got ' + goog.debug.runtimeType(sanitizedCss));
}
// Sanity check: Try to prevent accidental misuse when this is a full
// stylesheet rather than a declaration list. The error may trigger
// incorrectly if the content contains curly brackets inside comments or
// quoted strings.
//
// This is a best-effort attempt to preserve SafeStyle's semantic guarantees.
if (sanitizedCss && goog.string.contains(sanitizedCss.getContent(), '{')) {
throw new Error('Consider using packSanitizedCssToSafeStyleSheetProto().');
}
var safeStyle =
goog.html.uncheckedconversions
.safeStyleFromStringKnownToSatisfyTypeContract(
goog.string.Const.from('from Soy SanitizedCss object'),
sanitizedCss ? sanitizedCss.getContent() : '');
return security.html.jspbconversions.safeStyleToProto(safeStyle);
};
/**
* Converts a CSS Sanitized Content object to a corresponding Safe Style Sheet
* Proto.
* @param {!goog.soy.data.SanitizedCss|string} sanitizedCss
* @return {!proto.webutil.html.types.SafeStyleSheetProto}
*/
soydata.packSanitizedCssToSafeStyleSheetProtoSoyRuntimeOnly = function(
sanitizedCss) {
if (sanitizedCss !== '' &&
!(sanitizedCss instanceof goog.soy.data.SanitizedCss)) {
throw new Error(
'expected SanitizedCss, got ' + goog.debug.runtimeType(sanitizedCss));
}
// Sanity check: Try to prevent accidental misuse when this is a declaration
// list rather than a full stylesheet. The error may trigger incorrectly if
// the content contains curly brackets inside comments or quoted strings.
//
// This is a best-effort attempt to preserve SafeStyleSheet's semantic
// guarantees.
if (sanitizedCss && sanitizedCss.getContent().length > 0 &&
!goog.string.contains(sanitizedCss.getContent(), '{')) {
throw new Error('Consider using packSanitizedCssToSafeStyleProto().');
}
var safeStyleSheet =
goog.html.uncheckedconversions
.safeStyleSheetFromStringKnownToSatisfyTypeContract(
goog.string.Const.from('from Soy SanitizedCss object'),
sanitizedCss ? sanitizedCss.getContent() : '');
return security.html.jspbconversions.safeStyleSheetToProto(safeStyleSheet);
};
/**
* Converts an HTML Sanitized Content object to a corresponding
* Safe String Proto.
* @param {!goog.soy.data.SanitizedHtml|string|!soydata.IdomFunction|!Function}
* sanitizedHtml
* @return {!proto.webutil.html.types.SafeHtmlProto}
*/
soydata.packSanitizedHtmlToProtoSoyRuntimeOnly = function(sanitizedHtml) {
if (sanitizedHtml !== '' &&
!(sanitizedHtml instanceof goog.soy.data.SanitizedHtml)) {
throw new Error(
'expected SanitizedHtml, got ' + goog.debug.runtimeType(sanitizedHtml));
}
var content = sanitizedHtml ? sanitizedHtml.getContent() : '';
var contentDir = sanitizedHtml ? sanitizedHtml.contentDir : null;
var safeHtml =
goog.html.uncheckedconversions
.safeHtmlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from('from Soy SanitizedHtml object'), content,
contentDir);
return security.html.jspbconversions.safeHtmlToProto(safeHtml);
};
/**
* Converts a JS Sanitized Content object to a corresponding Safe Script Proto.
* @param {!goog.soy.data.SanitizedJs|string} sanitizedJs
* @return {!proto.webutil.html.types.SafeScriptProto}
*/
soydata.packSanitizedJsToProtoSoyRuntimeOnly = function(sanitizedJs) {
if (sanitizedJs !== '' &&
!(sanitizedJs instanceof goog.soy.data.SanitizedJs)) {
throw new Error(
'expected SanitizedJs, got ' + goog.debug.runtimeType(sanitizedJs));
}
var safeScript =
goog.html.uncheckedconversions
.safeScriptFromStringKnownToSatisfyTypeContract(
goog.string.Const.from('from Soy SanitizedJs object'),
sanitizedJs ? sanitizedJs.getContent() : '');
return security.html.jspbconversions.safeScriptToProto(safeScript);
};
/**
* Converts a Trusted Resource URI Sanitized Content object to a corresponding
* Trusted Resource URL Proto.
* @param {!goog.soy.data.SanitizedTrustedResourceUri|string}
* sanitizedTrustedResourceUri
* @return {!proto.webutil.html.types.TrustedResourceUrlProto}
*/
soydata.packSanitizedTrustedResourceUriToProtoSoyRuntimeOnly = function(
sanitizedTrustedResourceUri) {
if (sanitizedTrustedResourceUri !== '' &&
!(sanitizedTrustedResourceUri instanceof
goog.soy.data.SanitizedTrustedResourceUri)) {
throw new Error(
'expected SanitizedTrustedResourceUri, got ' +
goog.debug.runtimeType(sanitizedTrustedResourceUri));
}
var trustedResourceUrl =
goog.html.uncheckedconversions
.trustedResourceUrlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from(
'from Soy SanitizedTrustedResourceUri object'),
sanitizedTrustedResourceUri ?
sanitizedTrustedResourceUri.getContent() :
'');
return security.html.jspbconversions.trustedResourceUrlToProto(
trustedResourceUrl);
};
/**
* Converts a URI Sanitized Content object to a corresponding Safe URL Proto.
* @param {!goog.soy.data.SanitizedUri|string} sanitizedUri
* @return {!proto.webutil.html.types.SafeUrlProto}
*/
soydata.packSanitizedUriToProtoSoyRuntimeOnly = function(sanitizedUri) {
if (sanitizedUri !== '' &&
!(sanitizedUri instanceof goog.soy.data.SanitizedUri)) {
throw new Error(
'expected SanitizedUri, got ' + goog.debug.runtimeType(sanitizedUri));
}
var safeUrl = goog.html.uncheckedconversions
.safeUrlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from('from Soy SanitizedUri object'),
sanitizedUri ? sanitizedUri.getContent() : '');
return security.html.jspbconversions.safeUrlToProto(safeUrl);
};
/**
* Converts a Safe String Proto to HTML Sanitized Content.
* @param {?proto.webutil.html.types.SafeHtmlProto|undefined} x null or a safe string proto.
* @return {?goog.soy.data.SanitizedHtml}
*/
soydata.unpackProtoToSanitizedHtml = function(x) {
if (x instanceof proto.webutil.html.types.SafeHtmlProto) {
var safeHtml = security.html.jspbconversions.safeHtmlFromProto(x);
return soydata.VERY_UNSAFE.ordainSanitizedHtml(
goog.html.SafeHtml.unwrap(safeHtml), safeHtml.getDirection());
}
return null;
};
/**
* Converts a Safe String Proto to CSS Sanitized Content.
* @param {?proto.webutil.html.types.SafeStyleProto | ?proto.webutil.html.types.SafeStyleSheetProto} x
* null or a safe string proto.
* @return {?goog.soy.data.SanitizedCss}
*/
soydata.unpackProtoToSanitizedCss = function(x) {
var safeCss;
if (x instanceof proto.webutil.html.types.SafeStyleProto) {
safeCss = security.html.jspbconversions.safeStyleFromProto(x);
return soydata.VERY_UNSAFE.ordainSanitizedCss(
goog.html.SafeStyle.unwrap(safeCss));
} else if (x instanceof proto.webutil.html.types.SafeStyleSheetProto) {
safeCss = security.html.jspbconversions.safeStyleSheetFromProto(x);
return soydata.VERY_UNSAFE.ordainSanitizedCss(
goog.html.SafeStyleSheet.unwrap(safeCss));
}
return null;
};
/**
* Converts a Safe String Proto to JS Sanitized Content.
* @param {?proto.webutil.html.types.SafeScriptProto} x null or a safe string proto.
* @return {?goog.soy.data.SanitizedJs}
*/
soydata.unpackProtoToSanitizedJs = function(x) {
if (x instanceof proto.webutil.html.types.SafeScriptProto) {
var safeJs = security.html.jspbconversions.safeScriptFromProto(x);
return soydata.VERY_UNSAFE.ordainSanitizedJs(
goog.html.SafeScript.unwrap(safeJs));
}
return null;
};
/**
* Converts a Safe String Proto to URI Sanitized Content.
* @param {?proto.webutil.html.types.SafeUrlProto | ?proto.webutil.html.types.TrustedResourceUrlProto} x
* null or a safe string proto.
* @return {?goog.soy.data.SanitizedUri}
*/
soydata.unpackProtoToSanitizedUri = function(x) {
var safeUrl;
if (x instanceof proto.webutil.html.types.SafeUrlProto) {
safeUrl = security.html.jspbconversions.safeUrlFromProto(x);
return soydata.VERY_UNSAFE.ordainSanitizedUri(
goog.html.SafeUrl.unwrap(safeUrl));
}
return null;
};
/**
* Converts a Safe String Proto to a Trusted Resource URI Sanitized Content.
* @param {?proto.webutil.html.types.TrustedResourceUrlProto} x
* null or a safe string proto.
* @return {?goog.soy.data.SanitizedTrustedResourceUri}
*/
soydata.unpackProtoToSanitizedTrustedResourceUri = function(x) {
var safeUrl;
if (x instanceof proto.webutil.html.types.TrustedResourceUrlProto) {
safeUrl = security.html.jspbconversions.trustedResourceUrlFromProto(x);
return soydata.VERY_UNSAFE.ordainSanitizedTrustedResourceUri(
goog.html.TrustedResourceUrl.unwrap(safeUrl));
}
return null;
};