Path: blob/trunk/third_party/closure/goog/html/flash.js
2868 views
// Copyright 2014 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview SafeHtml factory methods for creating object and embed tags17* for loading Flash files.18*/1920goog.provide('goog.html.flash');2122goog.require('goog.asserts');23goog.require('goog.html.SafeHtml');242526/**27* Attributes and param tag name attributes not allowed to be overriden28* when calling createObject() and createObjectForOldIe().29*30* While values that should be specified as params are probably not31* recognized as attributes, we block them anyway just to be sure.32* @const {!Array<string>}33* @private34*/35goog.html.flash.FORBIDDEN_ATTRS_AND_PARAMS_ON_FLASH_ = [36'classid', // Used on old IE.37'data', // Used in <object> to specify a URL.38'movie', // Used on old IE.39'type', // Used in <object> on for non-IE/modern IE.40'typemustmatch' // Always set to a fixed value.41];424344goog.html.flash.createEmbed = function(src, opt_attributes) {45var fixedAttributes = {46'src': src,47'type': 'application/x-shockwave-flash',48'pluginspage': 'https://www.macromedia.com/go/getflashplayer'49};50var defaultAttributes = {51'allownetworking': 'none',52'allowscriptaccess': 'never'53};54var attributes = goog.html.SafeHtml.combineAttributes(55fixedAttributes, defaultAttributes, opt_attributes);56return goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(57'embed', attributes);58};596061goog.html.flash.createObject = function(data, opt_params, opt_attributes) {62goog.html.flash.verifyKeysNotInMaps(63goog.html.flash.FORBIDDEN_ATTRS_AND_PARAMS_ON_FLASH_, opt_attributes,64opt_params);6566var paramTags = goog.html.flash.combineParams(67{'allownetworking': 'none', 'allowscriptaccess': 'never'}, opt_params);68var fixedAttributes = {69'data': data,70'type': 'application/x-shockwave-flash',71'typemustmatch': ''72};73var attributes =74goog.html.SafeHtml.combineAttributes(fixedAttributes, {}, opt_attributes);7576return goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(77'object', attributes, paramTags);78};798081goog.html.flash.createObjectForOldIe = function(82movie, opt_params, opt_attributes) {83goog.html.flash.verifyKeysNotInMaps(84goog.html.flash.FORBIDDEN_ATTRS_AND_PARAMS_ON_FLASH_, opt_attributes,85opt_params);8687var paramTags = goog.html.flash.combineParams(88{'allownetworking': 'none', 'allowscriptaccess': 'never', 'movie': movie},89opt_params);90var fixedAttributes = {91'classid': 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'92};93var attributes =94goog.html.SafeHtml.combineAttributes(fixedAttributes, {}, opt_attributes);9596return goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(97'object', attributes, paramTags);98};99100101/**102* @param {!Object<string, string|!goog.string.TypedString>} defaultParams103* @param {?Object<string, string>=} opt_params Optional params passed to104* create*().105* @return {!Array<!goog.html.SafeHtml>} Combined params.106* @throws {Error} If opt_attributes contains an attribute with the same name107* as an attribute in fixedAttributes.108* @package109*/110goog.html.flash.combineParams = function(defaultParams, opt_params) {111var combinedParams = {};112var name;113114for (name in defaultParams) {115goog.asserts.assert(name.toLowerCase() == name, 'Must be lower case');116combinedParams[name] = defaultParams[name];117}118for (name in opt_params) {119var nameLower = name.toLowerCase();120if (nameLower in defaultParams) {121delete combinedParams[nameLower];122}123combinedParams[name] = opt_params[name];124}125126var paramTags = [];127for (name in combinedParams) {128paramTags.push(129goog.html.SafeHtml.createSafeHtmlTagSecurityPrivateDoNotAccessOrElse(130'param', {'name': name, 'value': combinedParams[name]}));131}132return paramTags;133};134135136/**137* Checks that keys are not present as keys in maps.138* @param {!Array<string>} keys Keys that must not be present, lower-case.139* @param {?Object<string, ?goog.html.SafeHtml.AttributeValue>=} opt_attributes140* Optional attributes passed to create*().141* @param {?Object<string, string>=} opt_params Optional params passed to142* createObject*().143* @throws {Error} If any of keys exist as a key, ignoring case, in144* opt_attributes or opt_params.145* @package146*/147goog.html.flash.verifyKeysNotInMaps = function(148keys, opt_attributes, opt_params) {149var verifyNotInMap = function(keys, map, type) {150for (var keyMap in map) {151var keyMapLower = keyMap.toLowerCase();152for (var i = 0; i < keys.length; i++) {153var keyToCheck = keys[i];154goog.asserts.assert(keyToCheck.toLowerCase() == keyToCheck);155if (keyMapLower == keyToCheck) {156throw Error(157'Cannot override "' + keyToCheck + '" ' + type + ', got "' +158keyMap + '" with value "' + map[keyMap] + '"');159}160}161}162};163164verifyNotInMap(keys, opt_attributes, 'attribute');165verifyNotInMap(keys, opt_params, 'param');166};167168169