iframe.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2008 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 Utilities for creating and working with iframes
  16. * cross-browser.
  17. * @author gboyer@google.com (Garry Boyer)
  18. */
  19. goog.provide('goog.dom.iframe');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.dom.safe');
  23. goog.require('goog.html.SafeHtml');
  24. goog.require('goog.html.SafeStyle');
  25. goog.require('goog.html.TrustedResourceUrl');
  26. goog.require('goog.string.Const');
  27. goog.require('goog.userAgent');
  28. /**
  29. * Safe source for a blank iframe.
  30. *
  31. * Intentionally not about:blank for IE, which gives mixed content warnings in
  32. * IE6 over HTTPS. Using 'about:blank' for all other browsers to support Content
  33. * Security Policy (CSP). According to http://www.w3.org/TR/CSP/ CSP does not
  34. * allow inline javascript by default.
  35. *
  36. * @const {!goog.html.TrustedResourceUrl}
  37. */
  38. goog.dom.iframe.BLANK_SOURCE_URL = goog.userAgent.IE ?
  39. goog.html.TrustedResourceUrl.fromConstant(
  40. goog.string.Const.from('javascript:""')) :
  41. goog.html.TrustedResourceUrl.fromConstant(
  42. goog.string.Const.from('about:blank'));
  43. /**
  44. * Legacy version of goog.dom.iframe.BLANK_SOURCE_URL.
  45. * @const {string}
  46. */
  47. goog.dom.iframe.BLANK_SOURCE =
  48. goog.html.TrustedResourceUrl.unwrap(goog.dom.iframe.BLANK_SOURCE_URL);
  49. /**
  50. * Safe source for a new blank iframe that may not cause a new load of the
  51. * iframe. This is different from {@code goog.dom.iframe.BLANK_SOURCE} in that
  52. * it will allow an iframe to be loaded synchronously in more browsers, notably
  53. * Gecko, following the javascript protocol spec.
  54. *
  55. * NOTE: This should not be used to replace the source of an existing iframe.
  56. * The new src value will be ignored, per the spec.
  57. *
  58. * Due to cross-browser differences, the load is not guaranteed to be
  59. * synchronous. If code depends on the load of the iframe,
  60. * then {@code goog.net.IframeLoadMonitor} or a similar technique should be
  61. * used.
  62. *
  63. * According to
  64. * http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#javascript-protocol
  65. * the 'javascript:""' URL should trigger a new load of the iframe, which may be
  66. * asynchronous. A void src, such as 'javascript:undefined', does not change
  67. * the browsing context document's, and thus should not trigger another load.
  68. *
  69. * Intentionally not about:blank, which also triggers a load.
  70. *
  71. * NOTE: 'javascript:' URL handling spec compliance varies per browser. IE
  72. * throws an error with 'javascript:undefined'. Webkit browsers will reload the
  73. * iframe when setting this source on an existing iframe.
  74. *
  75. * @const {!goog.html.TrustedResourceUrl}
  76. */
  77. goog.dom.iframe.BLANK_SOURCE_NEW_FRAME_URL = goog.userAgent.IE ?
  78. goog.html.TrustedResourceUrl.fromConstant(
  79. goog.string.Const.from('javascript:""')) :
  80. goog.html.TrustedResourceUrl.fromConstant(
  81. goog.string.Const.from('javascript:undefined'));
  82. /**
  83. * Legacy version of goog.dom.iframe.BLANK_SOURCE_NEW_FRAME_URL.
  84. * @const {string}
  85. */
  86. goog.dom.iframe.BLANK_SOURCE_NEW_FRAME = goog.html.TrustedResourceUrl.unwrap(
  87. goog.dom.iframe.BLANK_SOURCE_NEW_FRAME_URL);
  88. /**
  89. * Styles to help ensure an undecorated iframe.
  90. * @const {string}
  91. * @private
  92. */
  93. goog.dom.iframe.STYLES_ = 'border:0;vertical-align:bottom;';
  94. /**
  95. * Creates a completely blank iframe element.
  96. *
  97. * The iframe will not caused mixed-content warnings for IE6 under HTTPS.
  98. * The iframe will also have no borders or padding, so that the styled width
  99. * and height will be the actual width and height of the iframe.
  100. *
  101. * This function currently only attempts to create a blank iframe. There
  102. * are no guarantees to the contents of the iframe or whether it is rendered
  103. * in quirks mode.
  104. *
  105. * @param {goog.dom.DomHelper} domHelper The dom helper to use.
  106. * @param {!goog.html.SafeStyle=} opt_styles CSS styles for the iframe.
  107. * @return {!HTMLIFrameElement} A completely blank iframe.
  108. */
  109. goog.dom.iframe.createBlank = function(domHelper, opt_styles) {
  110. var styles;
  111. if (opt_styles) {
  112. // SafeStyle has to be converted back to a string for now, since there's
  113. // no safe alternative to createDom().
  114. styles = goog.html.SafeStyle.unwrap(opt_styles);
  115. } else { // undefined.
  116. styles = '';
  117. }
  118. return domHelper.createDom(goog.dom.TagName.IFRAME, {
  119. 'frameborder': 0,
  120. // Since iframes are inline elements, we must align to bottom to
  121. // compensate for the line descent.
  122. 'style': goog.dom.iframe.STYLES_ + styles,
  123. 'src': goog.dom.iframe.BLANK_SOURCE
  124. });
  125. };
  126. /**
  127. * Writes the contents of a blank iframe that has already been inserted
  128. * into the document.
  129. * @param {!HTMLIFrameElement} iframe An iframe with no contents, such as
  130. * one created by {@link #createBlank}, but already appended to
  131. * a parent document.
  132. * @param {!goog.html.SafeHtml} content Content to write to the iframe,
  133. * from doctype to the HTML close tag.
  134. */
  135. goog.dom.iframe.writeSafeContent = function(iframe, content) {
  136. var doc = goog.dom.getFrameContentDocument(iframe);
  137. doc.open();
  138. goog.dom.safe.documentWrite(doc, content);
  139. doc.close();
  140. };
  141. // TODO(gboyer): Provide a higher-level API for the most common use case, so
  142. // that you can just provide a list of stylesheets and some content HTML.
  143. /**
  144. * Creates a same-domain iframe containing preloaded content.
  145. *
  146. * This is primarily useful for DOM sandboxing. One use case is to embed
  147. * a trusted Javascript app with potentially conflicting CSS styles. The
  148. * second case is to reduce the cost of layout passes by the browser -- for
  149. * example, you can perform sandbox sizing of characters in an iframe while
  150. * manipulating a heavy DOM in the main window. The iframe and parent frame
  151. * can access each others' properties and functions without restriction.
  152. *
  153. * @param {!Element} parentElement The parent element in which to append the
  154. * iframe.
  155. * @param {!goog.html.SafeHtml=} opt_headContents Contents to go into the
  156. * iframe's head.
  157. * @param {!goog.html.SafeHtml=} opt_bodyContents Contents to go into the
  158. * iframe's body.
  159. * @param {!goog.html.SafeStyle=} opt_styles CSS styles for the iframe itself,
  160. * before adding to the parent element.
  161. * @param {boolean=} opt_quirks Whether to use quirks mode (false by default).
  162. * @return {!HTMLIFrameElement} An iframe that has the specified contents.
  163. */
  164. goog.dom.iframe.createWithContent = function(
  165. parentElement, opt_headContents, opt_bodyContents, opt_styles, opt_quirks) {
  166. var domHelper = goog.dom.getDomHelper(parentElement);
  167. var content = goog.html.SafeHtml.create(
  168. 'html', {}, goog.html.SafeHtml.concat(
  169. goog.html.SafeHtml.create('head', {}, opt_headContents),
  170. goog.html.SafeHtml.create('body', {}, opt_bodyContents)));
  171. if (!opt_quirks) {
  172. content =
  173. goog.html.SafeHtml.concat(goog.html.SafeHtml.DOCTYPE_HTML, content);
  174. }
  175. var iframe = goog.dom.iframe.createBlank(domHelper, opt_styles);
  176. // Cannot manipulate iframe content until it is in a document.
  177. parentElement.appendChild(iframe);
  178. goog.dom.iframe.writeSafeContent(iframe, content);
  179. return iframe;
  180. };