Debian Codegraph @

closure-library/closure/goog/editor/icontent.js

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