flash.js 5.5 KB

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