icontent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright 2007 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. // All Rights Reserved.
  15. /**
  16. * @fileoverview Static functions for writing the contents of an iframe-based
  17. * editable field. These vary significantly from browser to browser. Uses
  18. * strings and document.write instead of DOM manipulation, because
  19. * iframe-loading is a performance bottleneck.
  20. *
  21. * @author nicksantos@google.com (Nick Santos)
  22. */
  23. goog.provide('goog.editor.icontent');
  24. goog.provide('goog.editor.icontent.FieldFormatInfo');
  25. goog.provide('goog.editor.icontent.FieldStyleInfo');
  26. goog.require('goog.dom');
  27. goog.require('goog.editor.BrowserFeature');
  28. goog.require('goog.style');
  29. goog.require('goog.userAgent');
  30. /**
  31. * A data structure for storing simple rendering info about a field.
  32. *
  33. * @param {string} fieldId The id of the field.
  34. * @param {boolean} standards Whether the field should be rendered in
  35. * standards mode.
  36. * @param {boolean} blended Whether the field is in blended mode.
  37. * @param {boolean} fixedHeight Whether the field is in fixedHeight mode.
  38. * @param {Object=} opt_extraStyles Other style attributes for the field,
  39. * represented as a map of strings.
  40. * @constructor
  41. * @final
  42. */
  43. goog.editor.icontent.FieldFormatInfo = function(
  44. fieldId, standards, blended, fixedHeight, opt_extraStyles) {
  45. this.fieldId_ = fieldId;
  46. this.standards_ = standards;
  47. this.blended_ = blended;
  48. this.fixedHeight_ = fixedHeight;
  49. this.extraStyles_ = opt_extraStyles || {};
  50. };
  51. /**
  52. * A data structure for storing simple info about the styles of a field.
  53. * Only needed in Firefox/Blended mode.
  54. * @param {Element} wrapper The wrapper div around a field.
  55. * @param {string} css The css for a field.
  56. * @constructor
  57. * @final
  58. */
  59. goog.editor.icontent.FieldStyleInfo = function(wrapper, css) {
  60. this.wrapper_ = wrapper;
  61. this.css_ = css;
  62. };
  63. /**
  64. * Whether to always use standards-mode iframes.
  65. * @type {boolean}
  66. * @private
  67. */
  68. goog.editor.icontent.useStandardsModeIframes_ = false;
  69. /**
  70. * Sets up goog.editor.icontent to always use standards-mode iframes.
  71. */
  72. goog.editor.icontent.forceStandardsModeIframes = function() {
  73. goog.editor.icontent.useStandardsModeIframes_ = true;
  74. };
  75. /**
  76. * Generate the initial iframe content.
  77. * @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about
  78. * the field.
  79. * @param {string} bodyHtml The HTML to insert as the iframe body.
  80. * @param {goog.editor.icontent.FieldStyleInfo?} style Style info about
  81. * the field, if needed.
  82. * @return {string} The initial IFRAME content HTML.
  83. * @private
  84. */
  85. goog.editor.icontent.getInitialIframeContent_ = function(
  86. info, bodyHtml, style) {
  87. var html = [];
  88. if (info.blended_ && info.standards_ ||
  89. goog.editor.icontent.useStandardsModeIframes_) {
  90. html.push('<!DOCTYPE HTML>');
  91. }
  92. // <HTML>
  93. // NOTE(user): Override min-widths that may be set for all
  94. // HTML/BODY nodes. A similar workaround is below for the <body> tag. This
  95. // can happen if the host page includes a rule like this in its CSS:
  96. //
  97. // html, body {min-width: 500px}
  98. //
  99. // In this case, the iframe's <html> and/or <body> may be affected. This was
  100. // part of the problem observed in http://b/5674613. (The other part of that
  101. // problem had to do with the presence of a spurious horizontal scrollbar,
  102. // which caused the editor height to be computed incorrectly.)
  103. html.push('<html style="background:none transparent;min-width:0;');
  104. // Make sure that the HTML element's height has the
  105. // correct value as the body element's percentage height is made relative
  106. // to the HTML element's height.
  107. // For fixed-height it should be 100% since we want the body to fill the
  108. // whole height. For growing fields it should be auto since we want the
  109. // body to size to its content.
  110. if (info.blended_) {
  111. html.push('height:', info.fixedHeight_ ? '100%' : 'auto');
  112. }
  113. html.push('">');
  114. // <HEAD><STYLE>
  115. // IE/Safari whitebox need styles set only iff the client specifically
  116. // requested them.
  117. html.push('<head><style>');
  118. if (style && style.css_) {
  119. html.push(style.css_);
  120. }
  121. // Firefox blended needs to inherit all the css from the original page.
  122. // Firefox standards mode needs to set extra style for images.
  123. if (goog.userAgent.GECKO && info.standards_) {
  124. // Standards mode will collapse broken images. This means that they
  125. // can never be removed from the field. This style forces the images
  126. // to render as a broken image icon, sized based on the width and height
  127. // of the image.
  128. // TODO(user): Make sure we move this into a contentEditable code
  129. // path if there ever is one for FF.
  130. html.push(' img {-moz-force-broken-image-icon: 1;}');
  131. }
  132. html.push('</style></head>');
  133. // <BODY>
  134. // Hidefocus is needed to ensure that IE7 doesn't show the dotted, focus
  135. // border when you tab into the field.
  136. html.push('<body g_editable="true" hidefocus="true" ');
  137. if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  138. html.push('contentEditable ');
  139. }
  140. html.push('class="editable ');
  141. // TODO: put the field's original ID on the body and stop using ID as a
  142. // way of getting the pointer to the field in the iframe now that it's
  143. // always the body.
  144. html.push('" id="', info.fieldId_, '" style="min-width:0;');
  145. if (goog.userAgent.GECKO && info.blended_) {
  146. // IMPORTANT: Apply the css from the body then all of the clearing
  147. // CSS to make sure the clearing CSS overrides (e.g. if the body
  148. // has a 3px margin, we want to make sure to override it with 0px.
  149. html.push(
  150. // margin should not be applied to blended mode because the margin is
  151. // outside the iframe
  152. // In whitebox mode, we want to leave the margin to the default so
  153. // there is a nice margin around the text.
  154. ';width:100%;border:0;margin:0;background:none transparent;',
  155. // In standards-mode, height 100% makes the body size to its
  156. // parent html element, but in quirks mode, we want auto because
  157. // 100% makes it size to the containing window even if the html
  158. // element is smaller.
  159. // TODO: Fixed height, standards mode, CSS_WRITING, with margins on the
  160. // paragraphs has a scrollbar when it doesn't need it. Putting the
  161. // height to auto seems to fix it. Figure out if we should always
  162. // just use auto?
  163. ';height:', info.standards_ ? '100%' : 'auto');
  164. // Only do this for mozilla. IE6 standards mode has a rendering bug when
  165. // there are scrollbars and the body's overflow property is auto
  166. if (info.fixedHeight_) {
  167. html.push(';overflow:auto');
  168. } else {
  169. html.push(';overflow-y:hidden;overflow-x:auto');
  170. }
  171. }
  172. // Hide the native focus rect in Opera.
  173. if (goog.userAgent.OPERA) {
  174. html.push(';outline:hidden');
  175. }
  176. for (var key in info.extraStyles_) {
  177. html.push(';' + key + ':' + info.extraStyles_[key]);
  178. }
  179. html.push('">', bodyHtml, '</body></html>');
  180. return html.join('');
  181. };
  182. /**
  183. * Write the initial iframe content in normal mode.
  184. * @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about
  185. * the field.
  186. * @param {string} bodyHtml The HTML to insert as the iframe body.
  187. * @param {goog.editor.icontent.FieldStyleInfo?} style Style info about
  188. * the field, if needed.
  189. * @param {HTMLIFrameElement} iframe The iframe.
  190. */
  191. goog.editor.icontent.writeNormalInitialBlendedIframe = function(
  192. info, bodyHtml, style, iframe) {
  193. // Firefox blended needs to inherit all the css from the original page.
  194. // Firefox standards mode needs to set extra style for images.
  195. if (info.blended_) {
  196. var field = style.wrapper_;
  197. // If there is padding on the original field, then the iFrame will be
  198. // positioned inside the padding by default. We don't want this, as it
  199. // causes the contents to appear to shift, and also causes the
  200. // scrollbars to appear inside the padding.
  201. //
  202. // To compensate, we set the iframe margins to offset the padding.
  203. var paddingBox = goog.style.getPaddingBox(field);
  204. if (paddingBox.top || paddingBox.left || paddingBox.right ||
  205. paddingBox.bottom) {
  206. goog.style.setStyle(
  207. iframe, 'margin', (-paddingBox.top) + 'px ' + (-paddingBox.right) +
  208. 'px ' + (-paddingBox.bottom) + 'px ' + (-paddingBox.left) + 'px');
  209. }
  210. }
  211. goog.editor.icontent.writeNormalInitialIframe(info, bodyHtml, style, iframe);
  212. };
  213. /**
  214. * Write the initial iframe content in normal mode.
  215. * @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about
  216. * the field.
  217. * @param {string} bodyHtml The HTML to insert as the iframe body.
  218. * @param {goog.editor.icontent.FieldStyleInfo?} style Style info about
  219. * the field, if needed.
  220. * @param {HTMLIFrameElement} iframe The iframe.
  221. */
  222. goog.editor.icontent.writeNormalInitialIframe = function(
  223. info, bodyHtml, style, iframe) {
  224. var html =
  225. goog.editor.icontent.getInitialIframeContent_(info, bodyHtml, style);
  226. var doc = goog.dom.getFrameContentDocument(iframe);
  227. doc.open();
  228. doc.write(html);
  229. doc.close();
  230. };
  231. /**
  232. * Write the initial iframe content in IE/HTTPS mode.
  233. * @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about
  234. * the field.
  235. * @param {Document} doc The iframe document.
  236. * @param {string} bodyHtml The HTML to insert as the iframe body.
  237. */
  238. goog.editor.icontent.writeHttpsInitialIframe = function(info, doc, bodyHtml) {
  239. var body = doc.body;
  240. // For HTTPS we already have a document with a doc type and a body element
  241. // and don't want to create a new history entry which can cause data loss if
  242. // the user clicks the back button.
  243. if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  244. body.contentEditable = true;
  245. }
  246. body.className = 'editable';
  247. body.setAttribute('g_editable', true);
  248. body.hideFocus = true;
  249. body.id = info.fieldId_;
  250. goog.style.setStyle(body, info.extraStyles_);
  251. body.innerHTML = bodyHtml;
  252. };