123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- goog.provide('goog.html.SafeStyle');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.html.SafeUrl');
- goog.require('goog.string');
- goog.require('goog.string.Const');
- goog.require('goog.string.TypedString');
- goog.html.SafeStyle = function() {
-
- this.privateDoNotAccessOrElseSafeStyleWrappedValue_ = '';
-
- this.SAFE_STYLE_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ =
- goog.html.SafeStyle.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_;
- };
- goog.html.SafeStyle.prototype.implementsGoogStringTypedString = true;
- goog.html.SafeStyle.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ = {};
- goog.html.SafeStyle.fromConstant = function(style) {
- var styleString = goog.string.Const.unwrap(style);
- if (styleString.length === 0) {
- return goog.html.SafeStyle.EMPTY;
- }
- goog.html.SafeStyle.checkStyle_(styleString);
- goog.asserts.assert(
- goog.string.endsWith(styleString, ';'),
- 'Last character of style string is not \';\': ' + styleString);
- goog.asserts.assert(
- goog.string.contains(styleString, ':'),
- 'Style string must contain at least one \':\', to ' +
- 'specify a "name: value" pair: ' + styleString);
- return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
- styleString);
- };
- goog.html.SafeStyle.checkStyle_ = function(style) {
- goog.asserts.assert(
- !/[<>]/.test(style), 'Forbidden characters in style string: ' + style);
- };
- goog.html.SafeStyle.prototype.getTypedStringValue = function() {
- return this.privateDoNotAccessOrElseSafeStyleWrappedValue_;
- };
- if (goog.DEBUG) {
-
- goog.html.SafeStyle.prototype.toString = function() {
- return 'SafeStyle{' + this.privateDoNotAccessOrElseSafeStyleWrappedValue_ +
- '}';
- };
- }
- goog.html.SafeStyle.unwrap = function(safeStyle) {
-
-
-
-
-
-
-
-
-
-
- if (safeStyle instanceof goog.html.SafeStyle &&
- safeStyle.constructor === goog.html.SafeStyle &&
- safeStyle.SAFE_STYLE_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ ===
- goog.html.SafeStyle.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_) {
- return safeStyle.privateDoNotAccessOrElseSafeStyleWrappedValue_;
- } else {
- goog.asserts.fail('expected object of type SafeStyle, got \'' +
- safeStyle + '\' of type ' + goog.typeOf(safeStyle));
- return 'type_error:SafeStyle';
- }
- };
- goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse = function(
- style) {
- return new goog.html.SafeStyle().initSecurityPrivateDoNotAccessOrElse_(style);
- };
- goog.html.SafeStyle.prototype.initSecurityPrivateDoNotAccessOrElse_ = function(
- style) {
- this.privateDoNotAccessOrElseSafeStyleWrappedValue_ = style;
- return this;
- };
- goog.html.SafeStyle.EMPTY =
- goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse('');
- goog.html.SafeStyle.INNOCUOUS_STRING = 'zClosurez';
- goog.html.SafeStyle.PropertyMap;
- goog.html.SafeStyle.create = function(map) {
- var style = '';
- for (var name in map) {
- if (!/^[-_a-zA-Z0-9]+$/.test(name)) {
- throw Error('Name allows only [-_a-zA-Z0-9], got: ' + name);
- }
- var value = map[name];
- if (value == null) {
- continue;
- }
- if (value instanceof goog.string.Const) {
- value = goog.string.Const.unwrap(value);
-
-
- goog.asserts.assert(!/[{;}]/.test(value), 'Value does not allow [{;}].');
- } else {
- value = String(value);
- if (!goog.html.SafeStyle.VALUE_RE_.test(
- value.replace(goog.html.SafeUrl.URL_RE_, 'url'))) {
- goog.asserts.fail(
- 'String value allows only [-,."\'%_!# a-zA-Z0-9] and simple ' +
- 'functions, got: ' + value);
- value = goog.html.SafeStyle.INNOCUOUS_STRING;
- } else if (!goog.html.SafeStyle.hasBalancedQuotes_(value)) {
- goog.asserts.fail(
- 'String value requires balanced quotes, got: ' + value);
- value = goog.html.SafeStyle.INNOCUOUS_STRING;
- } else {
- value = goog.html.SafeStyle.sanitizeUrl_(value);
- }
- }
- style += name + ':' + value + ';';
- }
- if (!style) {
- return goog.html.SafeStyle.EMPTY;
- }
- goog.html.SafeStyle.checkStyle_(style);
- return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
- style);
- };
- goog.html.SafeStyle.hasBalancedQuotes_ = function(value) {
- var outsideSingle = true;
- var outsideDouble = true;
- for (var i = 0; i < value.length; i++) {
- var c = value.charAt(i);
- if (c == "'" && outsideDouble) {
- outsideSingle = !outsideSingle;
- } else if (c == '"' && outsideSingle) {
- outsideDouble = !outsideDouble;
- }
- }
- return outsideSingle && outsideDouble;
- };
- goog.html.SafeStyle.VALUE_RE_ = new RegExp(
- '^([-,."\'%_!# a-zA-Z0-9]+|(hsl|hsla|rgb|rgba' +
- '|(rotate|scale|translate)(X|Y|Z|3d)?)' +
- '\\([-0-9a-z.%, ]+\\))$');
- goog.html.SafeUrl.URL_RE_ = new RegExp(
- '\\b(url\\([ \t\n]*)(' +
- '\'[ -&(-\\[\\]-~]*\'' +
- '|"[ !#-\\[\\]-~]*"' +
- '|[!#-&*-\\[\\]-~]*' +
- ')([ \t\n]*\\))',
- 'g');
- goog.html.SafeStyle.sanitizeUrl_ = function(value) {
- return value.replace(
- goog.html.SafeUrl.URL_RE_, function(match, before, url, after) {
- var quote = '';
- url = url.replace(/^(['"])(.*)\1$/, function(match, start, inside) {
- quote = start;
- return inside;
- });
- var sanitized = goog.html.SafeUrl.sanitize(url).getTypedStringValue();
- return before + quote + sanitized + quote + after;
- });
- };
- goog.html.SafeStyle.concat = function(var_args) {
- var style = '';
-
- var addArgument = function(argument) {
- if (goog.isArray(argument)) {
- goog.array.forEach(argument, addArgument);
- } else {
- style += goog.html.SafeStyle.unwrap(argument);
- }
- };
- goog.array.forEach(arguments, addArgument);
- if (!style) {
- return goog.html.SafeStyle.EMPTY;
- }
- return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
- style);
- };
|