Path: blob/trunk/third_party/closure/goog/editor/icontent.js
2868 views
// Copyright 2007 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.13// All Rights Reserved.1415/**16* @fileoverview Static functions for writing the contents of an iframe-based17* editable field. These vary significantly from browser to browser. Uses18* strings and document.write instead of DOM manipulation, because19* iframe-loading is a performance bottleneck.20*21* @author [email protected] (Nick Santos)22*/2324goog.provide('goog.editor.icontent');25goog.provide('goog.editor.icontent.FieldFormatInfo');26goog.provide('goog.editor.icontent.FieldStyleInfo');2728goog.require('goog.dom');29goog.require('goog.editor.BrowserFeature');30goog.require('goog.style');31goog.require('goog.userAgent');32333435/**36* A data structure for storing simple rendering info about a field.37*38* @param {string} fieldId The id of the field.39* @param {boolean} standards Whether the field should be rendered in40* standards mode.41* @param {boolean} blended Whether the field is in blended mode.42* @param {boolean} fixedHeight Whether the field is in fixedHeight mode.43* @param {Object=} opt_extraStyles Other style attributes for the field,44* represented as a map of strings.45* @constructor46* @final47*/48goog.editor.icontent.FieldFormatInfo = function(49fieldId, standards, blended, fixedHeight, opt_extraStyles) {50this.fieldId_ = fieldId;51this.standards_ = standards;52this.blended_ = blended;53this.fixedHeight_ = fixedHeight;54this.extraStyles_ = opt_extraStyles || {};55};56575859/**60* A data structure for storing simple info about the styles of a field.61* Only needed in Firefox/Blended mode.62* @param {Element} wrapper The wrapper div around a field.63* @param {string} css The css for a field.64* @constructor65* @final66*/67goog.editor.icontent.FieldStyleInfo = function(wrapper, css) {68this.wrapper_ = wrapper;69this.css_ = css;70};717273/**74* Whether to always use standards-mode iframes.75* @type {boolean}76* @private77*/78goog.editor.icontent.useStandardsModeIframes_ = false;798081/**82* Sets up goog.editor.icontent to always use standards-mode iframes.83*/84goog.editor.icontent.forceStandardsModeIframes = function() {85goog.editor.icontent.useStandardsModeIframes_ = true;86};878889/**90* Generate the initial iframe content.91* @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about92* the field.93* @param {string} bodyHtml The HTML to insert as the iframe body.94* @param {goog.editor.icontent.FieldStyleInfo?} style Style info about95* the field, if needed.96* @return {string} The initial IFRAME content HTML.97* @private98*/99goog.editor.icontent.getInitialIframeContent_ = function(100info, bodyHtml, style) {101var html = [];102103if (info.blended_ && info.standards_ ||104goog.editor.icontent.useStandardsModeIframes_) {105html.push('<!DOCTYPE HTML>');106}107108// <HTML>109// NOTE(user): Override min-widths that may be set for all110// HTML/BODY nodes. A similar workaround is below for the <body> tag. This111// can happen if the host page includes a rule like this in its CSS:112//113// html, body {min-width: 500px}114//115// In this case, the iframe's <html> and/or <body> may be affected. This was116// part of the problem observed in http://b/5674613. (The other part of that117// problem had to do with the presence of a spurious horizontal scrollbar,118// which caused the editor height to be computed incorrectly.)119html.push('<html style="background:none transparent;min-width:0;');120121// Make sure that the HTML element's height has the122// correct value as the body element's percentage height is made relative123// to the HTML element's height.124// For fixed-height it should be 100% since we want the body to fill the125// whole height. For growing fields it should be auto since we want the126// body to size to its content.127if (info.blended_) {128html.push('height:', info.fixedHeight_ ? '100%' : 'auto');129}130html.push('">');131132// <HEAD><STYLE>133134// IE/Safari whitebox need styles set only iff the client specifically135// requested them.136html.push('<head><style>');137if (style && style.css_) {138html.push(style.css_);139}140141// Firefox blended needs to inherit all the css from the original page.142// Firefox standards mode needs to set extra style for images.143if (goog.userAgent.GECKO && info.standards_) {144// Standards mode will collapse broken images. This means that they145// can never be removed from the field. This style forces the images146// to render as a broken image icon, sized based on the width and height147// of the image.148// TODO(user): Make sure we move this into a contentEditable code149// path if there ever is one for FF.150html.push(' img {-moz-force-broken-image-icon: 1;}');151}152153html.push('</style></head>');154155// <BODY>156// Hidefocus is needed to ensure that IE7 doesn't show the dotted, focus157// border when you tab into the field.158html.push('<body g_editable="true" hidefocus="true" ');159if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {160html.push('contentEditable ');161}162163html.push('class="editable ');164165// TODO: put the field's original ID on the body and stop using ID as a166// way of getting the pointer to the field in the iframe now that it's167// always the body.168html.push('" id="', info.fieldId_, '" style="min-width:0;');169170if (goog.userAgent.GECKO && info.blended_) {171// IMPORTANT: Apply the css from the body then all of the clearing172// CSS to make sure the clearing CSS overrides (e.g. if the body173// has a 3px margin, we want to make sure to override it with 0px.174html.push(175176// margin should not be applied to blended mode because the margin is177// outside the iframe178// In whitebox mode, we want to leave the margin to the default so179// there is a nice margin around the text.180';width:100%;border:0;margin:0;background:none transparent;',181182// In standards-mode, height 100% makes the body size to its183// parent html element, but in quirks mode, we want auto because184// 100% makes it size to the containing window even if the html185// element is smaller.186// TODO: Fixed height, standards mode, CSS_WRITING, with margins on the187// paragraphs has a scrollbar when it doesn't need it. Putting the188// height to auto seems to fix it. Figure out if we should always189// just use auto?190';height:', info.standards_ ? '100%' : 'auto');191192// Only do this for mozilla. IE6 standards mode has a rendering bug when193// there are scrollbars and the body's overflow property is auto194if (info.fixedHeight_) {195html.push(';overflow:auto');196} else {197html.push(';overflow-y:hidden;overflow-x:auto');198}199}200201// Hide the native focus rect in Opera.202if (goog.userAgent.OPERA) {203html.push(';outline:hidden');204}205206for (var key in info.extraStyles_) {207html.push(';' + key + ':' + info.extraStyles_[key]);208}209210html.push('">', bodyHtml, '</body></html>');211212return html.join('');213};214215216/**217* Write the initial iframe content in normal mode.218* @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about219* the field.220* @param {string} bodyHtml The HTML to insert as the iframe body.221* @param {goog.editor.icontent.FieldStyleInfo?} style Style info about222* the field, if needed.223* @param {HTMLIFrameElement} iframe The iframe.224*/225goog.editor.icontent.writeNormalInitialBlendedIframe = function(226info, bodyHtml, style, iframe) {227// Firefox blended needs to inherit all the css from the original page.228// Firefox standards mode needs to set extra style for images.229if (info.blended_) {230var field = style.wrapper_;231// If there is padding on the original field, then the iFrame will be232// positioned inside the padding by default. We don't want this, as it233// causes the contents to appear to shift, and also causes the234// scrollbars to appear inside the padding.235//236// To compensate, we set the iframe margins to offset the padding.237var paddingBox = goog.style.getPaddingBox(field);238if (paddingBox.top || paddingBox.left || paddingBox.right ||239paddingBox.bottom) {240goog.style.setStyle(241iframe, 'margin', (-paddingBox.top) + 'px ' + (-paddingBox.right) +242'px ' + (-paddingBox.bottom) + 'px ' + (-paddingBox.left) + 'px');243}244}245246goog.editor.icontent.writeNormalInitialIframe(info, bodyHtml, style, iframe);247};248249250/**251* Write the initial iframe content in normal mode.252* @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about253* the field.254* @param {string} bodyHtml The HTML to insert as the iframe body.255* @param {goog.editor.icontent.FieldStyleInfo?} style Style info about256* the field, if needed.257* @param {HTMLIFrameElement} iframe The iframe.258*/259goog.editor.icontent.writeNormalInitialIframe = function(260info, bodyHtml, style, iframe) {261262var html =263goog.editor.icontent.getInitialIframeContent_(info, bodyHtml, style);264265var doc = goog.dom.getFrameContentDocument(iframe);266doc.open();267doc.write(html);268doc.close();269};270271272/**273* Write the initial iframe content in IE/HTTPS mode.274* @param {goog.editor.icontent.FieldFormatInfo} info Formatting info about275* the field.276* @param {Document} doc The iframe document.277* @param {string} bodyHtml The HTML to insert as the iframe body.278*/279goog.editor.icontent.writeHttpsInitialIframe = function(info, doc, bodyHtml) {280var body = doc.body;281282// For HTTPS we already have a document with a doc type and a body element283// and don't want to create a new history entry which can cause data loss if284// the user clicks the back button.285if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {286body.contentEditable = true;287}288body.className = 'editable';289body.setAttribute('g_editable', true);290body.hideFocus = true;291body.id = info.fieldId_;292293goog.style.setStyle(body, info.extraStyles_);294body.innerHTML = bodyHtml;295};296297298