1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright 2014 The Closure Library Authors. All Rights Reserved.
//
// 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 SafeHtml factory methods for creating object and embed tags
* for loading Flash files.
*/
goog.provide('goog.html.flash');
goog.require('goog.asserts');
goog.require('goog.html.SafeHtml');
/**
* Attributes and param tag name attributes not allowed to be overriden
* when calling createObject() and createObjectForOldIe().
*
* While values that should be specified as params are probably not
* recognized as attributes, we block them anyway just to be sure.
* @const {!Array<string>}
* @private
*/
goog.html.flash.FORBIDDEN_ATTRS_AND_PARAMS_ON_FLASH_ = [
'classid', // Used on old IE.
'data', // Used in <object> to specify a URL.
'movie', // Used on old IE.
'type', // Used in <object> on for non-IE/modern IE.
'typemustmatch' // Always set to a fixed value.
];
goog.html.flash.createEmbed = function(src, opt_attributes) {
var fixedAttributes = {
'src': src,
'type': 'application/x-shockwave-flash',
'pluginspage': 'https://www.macromedia.com/go/getflashplayer'
};
var defaultAttributes = {
'allownetworking': 'none',
'allowscriptaccess': 'never'
};
var attributes = goog.html.SafeHtml.combineAttributes(
fixedAttributes, defaultAttributes, opt_attributes);
return goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(
'embed', attributes);
};
goog.html.flash.createObject = function(data, opt_params, opt_attributes) {
goog.html.flash.verifyKeysNotInMaps(
goog.html.flash.FORBIDDEN_ATTRS_AND_PARAMS_ON_FLASH_, opt_attributes,
opt_params);
var paramTags = goog.html.flash.combineParams(
{'allownetworking': 'none', 'allowscriptaccess': 'never'}, opt_params);
var fixedAttributes = {
'data': data,
'type': 'application/x-shockwave-flash',
'typemustmatch': ''
};
var attributes =
goog.html.SafeHtml.combineAttributes(fixedAttributes, {}, opt_attributes);
return goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(
'object', attributes, paramTags);
};
goog.html.flash.createObjectForOldIe = function(
movie, opt_params, opt_attributes) {
goog.html.flash.verifyKeysNotInMaps(
goog.html.flash.FORBIDDEN_ATTRS_AND_PARAMS_ON_FLASH_, opt_attributes,
opt_params);
var paramTags = goog.html.flash.combineParams(
{'allownetworking': 'none', 'allowscriptaccess': 'never', 'movie': movie},
opt_params);
var fixedAttributes = {
'classid': 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'
};
var attributes =
goog.html.SafeHtml.combineAttributes(fixedAttributes, {}, opt_attributes);
return goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(
'object', attributes, paramTags);
};
/**
* @param {!Object<string, string|!goog.string.TypedString>} defaultParams
* @param {?Object<string, string>=} opt_params Optional params passed to
* create*().
* @return {!Array<!goog.html.SafeHtml>} Combined params.
* @throws {Error} If opt_attributes contains an attribute with the same name
* as an attribute in fixedAttributes.
* @package
*/
goog.html.flash.combineParams = function(defaultParams, opt_params) {
var combinedParams = {};
var name;
for (name in defaultParams) {
goog.asserts.assert(name.toLowerCase() == name, 'Must be lower case');
combinedParams[name] = defaultParams[name];
}
for (name in opt_params) {
var nameLower = name.toLowerCase();
if (nameLower in defaultParams) {
delete combinedParams[nameLower];
}
combinedParams[name] = opt_params[name];
}
var paramTags = [];
for (name in combinedParams) {
paramTags.push(
goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(
'param', {'name': name, 'value': combinedParams[name]}));
}
return paramTags;
};
/**
* Checks that keys are not present as keys in maps.
* @param {!Array<string>} keys Keys that must not be present, lower-case.
* @param {?Object<string, ?goog.html.SafeHtml.AttributeValue>=} opt_attributes
* Optional attributes passed to create*().
* @param {?Object<string, string>=} opt_params Optional params passed to
* createObject*().
* @throws {Error} If any of keys exist as a key, ignoring case, in
* opt_attributes or opt_params.
* @package
*/
goog.html.flash.verifyKeysNotInMaps = function(
keys, opt_attributes, opt_params) {
var verifyNotInMap = function(keys, map, type) {
for (var keyMap in map) {
var keyMapLower = keyMap.toLowerCase();
for (var i = 0; i < keys.length; i++) {
var keyToCheck = keys[i];
goog.asserts.assert(keyToCheck.toLowerCase() == keyToCheck);
if (keyMapLower == keyToCheck) {
throw new Error(
'Cannot override "' + keyToCheck + '" ' + type + ', got "' +
keyMap + '" with value "' + map[keyMap] + '"');
}
}
}
};
verifyNotInMap(keys, opt_attributes, 'attribute');
verifyNotInMap(keys, opt_params, 'param');
};
|