Path: blob/trunk/third_party/closure/goog/editor/field.js
2868 views
// Copyright 2006 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 Class to encapsulate an editable field. Always uses an17* iframe to contain the editable area, never inherits the style of the18* surrounding page, and is always a fixed height.19*20* @author [email protected] (Nick Santos)21* @see ../demos/editor/editor.html22* @see ../demos/editor/field_basic.html23*/2425goog.provide('goog.editor.Field');26goog.provide('goog.editor.Field.EventType');2728goog.require('goog.a11y.aria');29goog.require('goog.a11y.aria.Role');30goog.require('goog.array');31goog.require('goog.asserts');32goog.require('goog.async.Delay');33goog.require('goog.dom');34goog.require('goog.dom.Range');35goog.require('goog.dom.TagName');36goog.require('goog.dom.classlist');37goog.require('goog.editor.BrowserFeature');38goog.require('goog.editor.Command');39goog.require('goog.editor.Plugin');40goog.require('goog.editor.icontent');41goog.require('goog.editor.icontent.FieldFormatInfo');42goog.require('goog.editor.icontent.FieldStyleInfo');43goog.require('goog.editor.node');44goog.require('goog.editor.range');45goog.require('goog.events');46goog.require('goog.events.EventHandler');47goog.require('goog.events.EventTarget');48goog.require('goog.events.EventType');49goog.require('goog.events.KeyCodes');50goog.require('goog.functions');51goog.require('goog.html.SafeHtml');52goog.require('goog.html.legacyconversions');53goog.require('goog.log');54goog.require('goog.log.Level');55goog.require('goog.string');56goog.require('goog.string.Unicode');57goog.require('goog.style');58goog.require('goog.userAgent');59goog.require('goog.userAgent.product');60616263/**64* This class encapsulates an editable field.65*66* event: load Fires when the field is loaded67* event: unload Fires when the field is unloaded (made not editable)68*69* event: beforechange Fires before the content of the field might change70*71* event: delayedchange Fires a short time after field has changed. If multiple72* change events happen really close to each other only73* the last one will trigger the delayedchange event.74*75* event: beforefocus Fires before the field becomes active76* event: focus Fires when the field becomes active. Fires after the blur event77* event: blur Fires when the field becomes inactive78*79* TODO: figure out if blur or beforefocus fires first in IE and make FF match80*81* @param {string} id An identifer for the field. This is used to find the82* field and the element associated with this field.83* @param {Document=} opt_doc The document that the element with the given84* id can be found in. If not provided, the default document is used.85* @constructor86* @extends {goog.events.EventTarget}87*/88goog.editor.Field = function(id, opt_doc) {89goog.events.EventTarget.call(this);9091/**92* The id for this editable field, which must match the id of the element93* associated with this field.94* @type {string}95*/96this.id = id;9798/**99* The hash code for this field. Should be equal to the id.100* @type {string}101* @private102*/103this.hashCode_ = id;104105/**106* Dom helper for the editable node.107* @type {goog.dom.DomHelper}108* @protected109*/110this.editableDomHelper = null;111112/**113* Map of class id to registered plugin.114* @type {Object}115* @private116*/117this.plugins_ = {};118119120/**121* Plugins registered on this field, indexed by the goog.editor.Plugin.Op122* that they support.123* @type {Object<Array<goog.editor.Plugin>>}124* @private125*/126this.indexedPlugins_ = {};127128for (var op in goog.editor.Plugin.OPCODE) {129this.indexedPlugins_[op] = [];130}131132133/**134* Additional styles to install for the editable field.135* @type {string}136* @protected137*/138this.cssStyles = '';139140// The field will not listen to change events until it has finished loading141/** @private */142this.stoppedEvents_ = {};143this.stopEvent(goog.editor.Field.EventType.CHANGE);144this.stopEvent(goog.editor.Field.EventType.DELAYEDCHANGE);145/** @private */146this.isModified_ = false;147/** @private */148this.isEverModified_ = false;149/** @private */150this.delayedChangeTimer_ = new goog.async.Delay(151this.dispatchDelayedChange_, goog.editor.Field.DELAYED_CHANGE_FREQUENCY,152this);153154/** @private */155this.debouncedEvents_ = {};156for (var key in goog.editor.Field.EventType) {157this.debouncedEvents_[goog.editor.Field.EventType[key]] = 0;158}159160if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {161/** @private */162this.changeTimerGecko_ = new goog.async.Delay(163this.handleChange, goog.editor.Field.CHANGE_FREQUENCY, this);164}165166/**167* @type {goog.events.EventHandler<!goog.editor.Field>}168* @protected169*/170this.eventRegister = new goog.events.EventHandler(this);171172// Wrappers around this field, to be disposed when the field is disposed.173/** @private */174this.wrappers_ = [];175176/** @private */177this.loadState_ = goog.editor.Field.LoadState_.UNEDITABLE;178179var doc = opt_doc || document;180181/**182* The dom helper for the node to be made editable.183* @type {goog.dom.DomHelper}184* @protected185*/186this.originalDomHelper = goog.dom.getDomHelper(doc);187188/**189* The original node that is being made editable, or null if it has190* not yet been found.191* @type {Element}192* @protected193*/194this.originalElement = this.originalDomHelper.getElement(this.id);195196/**197* @private {boolean}198*/199this.followLinkInNewWindow_ =200goog.editor.BrowserFeature.FOLLOWS_EDITABLE_LINKS;201202// Default to the same window as the field is in.203/** @private */204this.appWindow_ = this.originalDomHelper.getWindow();205};206goog.inherits(goog.editor.Field, goog.events.EventTarget);207208209/**210* The editable dom node.211* @type {Element}212* TODO(user): Make this private!213*/214goog.editor.Field.prototype.field = null;215216217/**218* Logging object.219* @type {goog.log.Logger}220* @protected221*/222goog.editor.Field.prototype.logger = goog.log.getLogger('goog.editor.Field');223224225/**226* Event types that can be stopped/started.227* @enum {string}228*/229goog.editor.Field.EventType = {230/**231* Dispatched when the command state of the selection may have changed. This232* event should be listened to for updating toolbar state.233*/234COMMAND_VALUE_CHANGE: 'cvc',235/**236* Dispatched when the field is loaded and ready to use.237*/238LOAD: 'load',239/**240* Dispatched when the field is fully unloaded and uneditable.241*/242UNLOAD: 'unload',243/**244* Dispatched before the field contents are changed.245*/246BEFORECHANGE: 'beforechange',247/**248* Dispatched when the field contents change, in FF only.249* Used for internal resizing, please do not use.250*/251CHANGE: 'change',252/**253* Dispatched on a slight delay after changes are made.254* Use for autosave, or other times your app needs to know255* that the field contents changed.256*/257DELAYEDCHANGE: 'delayedchange',258/**259* Dispatched before focus in moved into the field.260*/261BEFOREFOCUS: 'beforefocus',262/**263* Dispatched when focus is moved into the field.264*/265FOCUS: 'focus',266/**267* Dispatched when the field is blurred.268*/269BLUR: 'blur',270/**271* Dispatched before tab is handled by the field. This is a legacy way272* of controlling tab behavior. Use trog.plugins.AbstractTabHandler now.273*/274BEFORETAB: 'beforetab',275/**276* Dispatched after the iframe containing the field is resized, so that UI277* components which contain it can respond.278*/279IFRAME_RESIZED: 'ifrsz',280/**281* Dispatched after a user action that will eventually fire a SELECTIONCHANGE282* event. For mouseups, this is fired immediately before SELECTIONCHANGE,283* since {@link #handleMouseUp_} fires SELECTIONCHANGE immediately. May be284* fired up to {@link #SELECTION_CHANGE_FREQUENCY_} ms before SELECTIONCHANGE285* is fired in the case of keyup events, since they use286* {@link #selectionChangeTimer_}.287*/288BEFORESELECTIONCHANGE: 'beforeselectionchange',289/**290* Dispatched when the selection changes.291* Use handleSelectionChange from plugin API instead of listening292* directly to this event.293*/294SELECTIONCHANGE: 'selectionchange'295};296297298/**299* The load state of the field.300* @enum {number}301* @private302*/303goog.editor.Field.LoadState_ = {304UNEDITABLE: 0,305LOADING: 1,306EDITABLE: 2307};308309310/**311* The amount of time that a debounce blocks an event.312* TODO(nicksantos): As of 9/30/07, this is only used for blocking313* a keyup event after a keydown. We might need to tweak this for other314* types of events. Maybe have a per-event debounce time?315* @type {number}316* @private317*/318goog.editor.Field.DEBOUNCE_TIME_MS_ = 500;319320321/**322* There is at most one "active" field at a time. By "active" field, we mean323* a field that has focus and is being used.324* @type {?string}325* @private326*/327goog.editor.Field.activeFieldId_ = null;328329330/**331* Whether this field is in "modal interaction" mode. This usually332* means that it's being edited by a dialog.333* @type {boolean}334* @private335*/336goog.editor.Field.prototype.inModalMode_ = false;337338339/**340* The window where dialogs and bubbles should be rendered.341* @type {!Window}342* @private343*/344goog.editor.Field.prototype.appWindow_;345346347/**348* Target node to be used when dispatching SELECTIONCHANGE asynchronously on349* mouseup (to avoid IE quirk). Should be set just before starting the timer and350* nulled right after consuming.351* @type {Node}352* @private353*/354goog.editor.Field.prototype.selectionChangeTarget_;355356357/**358* Flag controlling wether to capture mouse up events on the window or not.359* @type {boolean}360* @private361*/362goog.editor.Field.prototype.useWindowMouseUp_ = false;363364365/**366* FLag indicating the handling of a mouse event sequence.367* @type {boolean}368* @private369*/370goog.editor.Field.prototype.waitingForMouseUp_ = false;371372373/**374* Sets the active field id.375* @param {?string} fieldId The active field id.376*/377goog.editor.Field.setActiveFieldId = function(fieldId) {378goog.editor.Field.activeFieldId_ = fieldId;379};380381382/**383* @return {?string} The id of the active field.384*/385goog.editor.Field.getActiveFieldId = function() {386return goog.editor.Field.activeFieldId_;387};388389390/**391* Sets flag to control whether to use window mouse up after seeing392* a mouse down operation on the field.393* @param {boolean} flag True to track window mouse up.394*/395goog.editor.Field.prototype.setUseWindowMouseUp = function(flag) {396goog.asserts.assert(397!flag || !this.usesIframe(),398'procssing window mouse up should only be enabled when not using iframe');399this.useWindowMouseUp_ = flag;400};401402403/**404* @return {boolean} Whether we're in modal interaction mode. When this405* returns true, another plugin is interacting with the field contents406* in a synchronous way, and expects you not to make changes to407* the field's DOM structure or selection.408*/409goog.editor.Field.prototype.inModalMode = function() {410return this.inModalMode_;411};412413414/**415* @param {boolean} inModalMode Sets whether we're in modal interaction mode.416*/417goog.editor.Field.prototype.setModalMode = function(inModalMode) {418this.inModalMode_ = inModalMode;419};420421422/**423* Returns a string usable as a hash code for this field. For field's424* that were created with an id, the hash code is guaranteed to be the id.425* TODO(user): I think we can get rid of this. Seems only used from editor.426* @return {string} The hash code for this editable field.427*/428goog.editor.Field.prototype.getHashCode = function() {429return this.hashCode_;430};431432433/**434* Returns the editable DOM element or null if this field435* is not editable.436* <p>On IE or Safari this is the element with contentEditable=true437* (in whitebox mode, the iFrame body).438* <p>On Gecko this is the iFrame body439* TODO(user): How do we word this for subclass version?440* @return {Element} The editable DOM element, defined as above.441*/442goog.editor.Field.prototype.getElement = function() {443return this.field;444};445446447/**448* Returns original DOM element that is being made editable by Trogedit or449* null if that element has not yet been found in the appropriate document.450* @return {Element} The original element.451*/452goog.editor.Field.prototype.getOriginalElement = function() {453return this.originalElement;454};455456457/**458* Registers a keyboard event listener on the field. This is necessary for459* Gecko since the fields are contained in an iFrame and there is no way to460* auto-propagate key events up to the main window.461* @param {string|Array<string>} type Event type to listen for or array of462* event types, for example goog.events.EventType.KEYDOWN.463* @param {Function} listener Function to be used as the listener.464* @param {boolean=} opt_capture Whether to use capture phase (optional,465* defaults to false).466* @param {Object=} opt_handler Object in whose scope to call the listener.467*/468goog.editor.Field.prototype.addListener = function(469type, listener, opt_capture, opt_handler) {470var elem = this.getElement();471// On Gecko, keyboard events only reliably fire on the document element when472// using an iframe.473if (goog.editor.BrowserFeature.USE_DOCUMENT_FOR_KEY_EVENTS && elem &&474this.usesIframe()) {475elem = elem.ownerDocument;476}477if (opt_handler) {478this.eventRegister.listenWithScope(479elem, type, listener, opt_capture, opt_handler);480} else {481this.eventRegister.listen(elem, type, listener, opt_capture);482}483};484485486/**487* Returns the registered plugin with the given classId.488* @param {string} classId classId of the plugin.489* @return {goog.editor.Plugin} Registered plugin with the given classId.490*/491goog.editor.Field.prototype.getPluginByClassId = function(classId) {492return this.plugins_[classId];493};494495496/**497* Registers the plugin with the editable field.498* @param {goog.editor.Plugin} plugin The plugin to register.499*/500goog.editor.Field.prototype.registerPlugin = function(plugin) {501var classId = plugin.getTrogClassId();502if (this.plugins_[classId]) {503goog.log.error(504this.logger, 'Cannot register the same class of plugin twice.');505}506this.plugins_[classId] = plugin;507508// Only key events and execute should have these has* functions with a custom509// handler array since they need to be very careful about performance.510// The rest of the plugin hooks should be event-based.511for (var op in goog.editor.Plugin.OPCODE) {512var opcode = goog.editor.Plugin.OPCODE[op];513if (plugin[opcode]) {514this.indexedPlugins_[op].push(plugin);515}516}517plugin.registerFieldObject(this);518519// By default we enable all plugins for fields that are currently loaded.520if (this.isLoaded()) {521plugin.enable(this);522}523};524525526/**527* Unregisters the plugin with this field.528* @param {goog.editor.Plugin} plugin The plugin to unregister.529*/530goog.editor.Field.prototype.unregisterPlugin = function(plugin) {531var classId = plugin.getTrogClassId();532if (!this.plugins_[classId]) {533goog.log.error(534this.logger, 'Cannot unregister a plugin that isn\'t registered.');535}536delete this.plugins_[classId];537538for (var op in goog.editor.Plugin.OPCODE) {539var opcode = goog.editor.Plugin.OPCODE[op];540if (plugin[opcode]) {541goog.array.remove(this.indexedPlugins_[op], plugin);542}543}544545plugin.unregisterFieldObject(this);546};547548549/**550* Sets the value that will replace the style attribute of this field's551* element when the field is made non-editable. This method is called with the552* current value of the style attribute when the field is made editable.553* @param {string} cssText The value of the style attribute.554*/555goog.editor.Field.prototype.setInitialStyle = function(cssText) {556this.cssText = cssText;557};558559560/**561* Reset the properties on the original field element to how it was before562* it was made editable.563*/564goog.editor.Field.prototype.resetOriginalElemProperties = function() {565var field = this.getOriginalElement();566field.removeAttribute('contentEditable');567field.removeAttribute('g_editable');568field.removeAttribute('role');569570if (!this.id) {571field.removeAttribute('id');572} else {573field.id = this.id;574}575576field.className = this.savedClassName_ || '';577578var cssText = this.cssText;579if (!cssText) {580field.removeAttribute('style');581} else {582goog.dom.setProperties(field, {'style': cssText});583}584585if (goog.isString(this.originalFieldLineHeight_)) {586goog.style.setStyle(field, 'lineHeight', this.originalFieldLineHeight_);587this.originalFieldLineHeight_ = null;588}589};590591592/**593* Checks the modified state of the field.594* Note: Changes that take place while the goog.editor.Field.EventType.CHANGE595* event is stopped do not effect the modified state.596* @param {boolean=} opt_useIsEverModified Set to true to check if the field597* has ever been modified since it was created, otherwise checks if the field598* has been modified since the last goog.editor.Field.EventType.DELAYEDCHANGE599* event was dispatched.600* @return {boolean} Whether the field has been modified.601*/602goog.editor.Field.prototype.isModified = function(opt_useIsEverModified) {603return opt_useIsEverModified ? this.isEverModified_ : this.isModified_;604};605606607/**608* Number of milliseconds after a change when the change event should be fired.609* @type {number}610*/611goog.editor.Field.CHANGE_FREQUENCY = 15;612613614/**615* Number of milliseconds between delayed change events.616* @type {number}617*/618goog.editor.Field.DELAYED_CHANGE_FREQUENCY = 250;619620621/**622* @return {boolean} Whether the field is implemented as an iframe.623*/624goog.editor.Field.prototype.usesIframe = goog.functions.TRUE;625626627/**628* @return {boolean} Whether the field should be rendered with a fixed629* height, or should expand to fit its contents.630*/631goog.editor.Field.prototype.isFixedHeight = goog.functions.TRUE;632633634/**635* @return {boolean} Whether the field should be refocused on input.636* This is a workaround for the iOS bug that text input doesn't work637* when the main window listens touch events.638*/639goog.editor.Field.prototype.shouldRefocusOnInputMobileSafari =640goog.functions.FALSE;641642643/**644* Map of keyCodes (not charCodes) that cause changes in the field contents.645* @type {Object}646* @private647*/648goog.editor.Field.KEYS_CAUSING_CHANGES_ = {64946: true, // DEL6508: true // BACKSPACE651};652653if (!goog.userAgent.IE) {654// Only IE doesn't change the field by default upon tab.655// TODO(user): This really isn't right now that we have tab plugins.656goog.editor.Field.KEYS_CAUSING_CHANGES_[9] = true; // TAB657}658659660/**661* Map of keyCodes (not charCodes) that when used in conjunction with the662* Ctrl key cause changes in the field contents. These are the keys that are663* not handled by basic formatting trogedit plugins.664* @type {Object}665* @private666*/667goog.editor.Field.CTRL_KEYS_CAUSING_CHANGES_ = {66886: true, // V66988: true // X670};671672if (goog.userAgent.WINDOWS && !goog.userAgent.GECKO) {673// In IE and Webkit, input from IME (Input Method Editor) does not generate a674// keypress event so we have to rely on the keydown event. This way we have675// false positives while the user is using keyboard to select the676// character to input, but it is still better than the false negatives677// that ignores user's final input at all.678goog.editor.Field.KEYS_CAUSING_CHANGES_[229] = true; // from IME;679}680681682/**683* Returns true if the keypress generates a change in contents.684* @param {goog.events.BrowserEvent} e The event.685* @param {boolean} testAllKeys True to test for all types of generating keys.686* False to test for only the keys found in687* goog.editor.Field.KEYS_CAUSING_CHANGES_.688* @return {boolean} Whether the keypress generates a change in contents.689* @private690*/691goog.editor.Field.isGeneratingKey_ = function(e, testAllKeys) {692if (goog.editor.Field.isSpecialGeneratingKey_(e)) {693return true;694}695696return !!(697testAllKeys && !(e.ctrlKey || e.metaKey) &&698(!goog.userAgent.GECKO || e.charCode));699};700701702/**703* Returns true if the keypress generates a change in the contents.704* due to a special key listed in goog.editor.Field.KEYS_CAUSING_CHANGES_705* @param {goog.events.BrowserEvent} e The event.706* @return {boolean} Whether the keypress generated a change in the contents.707* @private708*/709goog.editor.Field.isSpecialGeneratingKey_ = function(e) {710var testCtrlKeys = (e.ctrlKey || e.metaKey) &&711e.keyCode in goog.editor.Field.CTRL_KEYS_CAUSING_CHANGES_;712var testRegularKeys = !(e.ctrlKey || e.metaKey) &&713e.keyCode in goog.editor.Field.KEYS_CAUSING_CHANGES_;714715return testCtrlKeys || testRegularKeys;716};717718719/**720* Sets the application window.721* @param {!Window} appWindow The window where dialogs and bubbles should be722* rendered.723*/724goog.editor.Field.prototype.setAppWindow = function(appWindow) {725this.appWindow_ = appWindow;726};727728729/**730* Returns the "application" window, where dialogs and bubbles731* should be rendered.732* @return {!Window} The window.733*/734goog.editor.Field.prototype.getAppWindow = function() {735return this.appWindow_;736};737738739/**740* Sets the zIndex that the field should be based off of.741* TODO(user): Get rid of this completely. Here for Sites.742* Should this be set directly on UI plugins?743*744* @param {number} zindex The base zIndex of the editor.745*/746goog.editor.Field.prototype.setBaseZindex = function(zindex) {747this.baseZindex_ = zindex;748};749750751/**752* Returns the zindex of the base level of the field.753*754* @return {number} The base zindex of the editor.755*/756goog.editor.Field.prototype.getBaseZindex = function() {757return this.baseZindex_ || 0;758};759760761/**762* Sets up the field object and window util of this field, and enables this763* editable field with all registered plugins.764* This is essential to the initialization of the field.765* It must be called when the field becomes fully loaded and editable.766* @param {Element} field The field property.767* @protected768*/769goog.editor.Field.prototype.setupFieldObject = function(field) {770this.loadState_ = goog.editor.Field.LoadState_.EDITABLE;771this.field = field;772this.editableDomHelper = goog.dom.getDomHelper(field);773this.isModified_ = false;774this.isEverModified_ = false;775field.setAttribute('g_editable', 'true');776goog.a11y.aria.setRole(field, goog.a11y.aria.Role.TEXTBOX);777};778779780/**781* Help make the field not editable by setting internal data structures to null,782* and disabling this field with all registered plugins.783* @private784*/785goog.editor.Field.prototype.tearDownFieldObject_ = function() {786this.loadState_ = goog.editor.Field.LoadState_.UNEDITABLE;787788for (var classId in this.plugins_) {789var plugin = this.plugins_[classId];790if (!plugin.activeOnUneditableFields()) {791plugin.disable(this);792}793}794795this.field = null;796this.editableDomHelper = null;797};798799800/**801* Initialize listeners on the field.802* @private803*/804goog.editor.Field.prototype.setupChangeListeners_ = function() {805if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &&806this.usesIframe() && this.shouldRefocusOnInputMobileSafari()) {807// This is a workaround for the iOS bug that text input doesn't work808// when the main window listens touch events.809var editWindow = this.getEditableDomHelper().getWindow();810this.boundRefocusListenerMobileSafari_ =811goog.bind(editWindow.focus, editWindow);812editWindow.addEventListener(813goog.events.EventType.KEYDOWN, this.boundRefocusListenerMobileSafari_,814false);815editWindow.addEventListener(816goog.events.EventType.TOUCHEND, this.boundRefocusListenerMobileSafari_,817false);818}819if (goog.userAgent.OPERA && this.usesIframe()) {820// We can't use addListener here because we need to listen on the window,821// and removing listeners on window objects from the event register throws822// an exception if the window is closed.823this.boundFocusListenerOpera_ =824goog.bind(this.dispatchFocusAndBeforeFocus_, this);825this.boundBlurListenerOpera_ = goog.bind(this.dispatchBlur, this);826var editWindow = this.getEditableDomHelper().getWindow();827editWindow.addEventListener(828goog.events.EventType.FOCUS, this.boundFocusListenerOpera_, false);829editWindow.addEventListener(830goog.events.EventType.BLUR, this.boundBlurListenerOpera_, false);831} else {832if (goog.editor.BrowserFeature.SUPPORTS_FOCUSIN) {833this.addListener(goog.events.EventType.FOCUS, this.dispatchFocus_);834this.addListener(835goog.events.EventType.FOCUSIN, this.dispatchBeforeFocus_);836} else {837this.addListener(838goog.events.EventType.FOCUS, this.dispatchFocusAndBeforeFocus_);839}840this.addListener(841goog.events.EventType.BLUR, this.dispatchBlur,842goog.editor.BrowserFeature.USE_MUTATION_EVENTS);843}844845if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {846// Ways to detect changes in Mozilla:847//848// keypress - check event.charCode (only typable characters has a849// charCode), but also keyboard commands lile Ctrl+C will850// return a charCode.851// dragdrop - fires when the user drops something. This does not necessary852// lead to a change but we cannot detect if it will or not853//854// Known Issues: We cannot detect cut and paste using menus855// We cannot detect when someone moves something out of the856// field using drag and drop.857//858this.setupMutationEventHandlersGecko();859} else {860// Ways to detect that a change is about to happen in other browsers.861// (IE and Safari have these events. Opera appears to work, but we haven't862// researched it.)863//864// onbeforepaste865// onbeforecut866// ondrop - happens when the user drops something on the editable text867// field the value at this time does not contain the dropped text868// ondragleave - when the user drags something from the current document.869// This might not cause a change if the action was copy870// instead of move871// onkeypress - IE only fires keypress events if the key will generate872// output. It will not trigger for delete and backspace873// onkeydown - For delete and backspace874//875// known issues: IE triggers beforepaste just by opening the edit menu876// delete at the end should not cause beforechange877// backspace at the beginning should not cause beforechange878// see above in ondragleave879// TODO(user): Why don't we dispatchBeforeChange from the880// handleDrop event for all browsers?881this.addListener(882['beforecut', 'beforepaste', 'drop', 'dragend'],883this.dispatchBeforeChange);884this.addListener(885['cut', 'paste'], goog.functions.lock(this.dispatchChange));886this.addListener('drop', this.handleDrop_);887}888889// TODO(user): Figure out why we use dragend vs dragdrop and890// document this better.891var dropEventName = goog.userAgent.WEBKIT ? 'dragend' : 'dragdrop';892this.addListener(dropEventName, this.handleDrop_);893894this.addListener(goog.events.EventType.KEYDOWN, this.handleKeyDown_);895this.addListener(goog.events.EventType.KEYPRESS, this.handleKeyPress_);896this.addListener(goog.events.EventType.KEYUP, this.handleKeyUp_);897898this.selectionChangeTimer_ = new goog.async.Delay(899this.handleSelectionChangeTimer_,900goog.editor.Field.SELECTION_CHANGE_FREQUENCY_, this);901902if (this.followLinkInNewWindow_) {903this.addListener(904goog.events.EventType.CLICK, goog.editor.Field.cancelLinkClick_);905}906907this.addListener(goog.events.EventType.MOUSEDOWN, this.handleMouseDown_);908if (this.useWindowMouseUp_) {909this.eventRegister.listen(910this.editableDomHelper.getDocument(), goog.events.EventType.MOUSEUP,911this.handleMouseUp_);912this.addListener(goog.events.EventType.DRAGSTART, this.handleDragStart_);913} else {914this.addListener(goog.events.EventType.MOUSEUP, this.handleMouseUp_);915}916};917918919/**920* Frequency to check for selection changes.921* @type {number}922* @private923*/924goog.editor.Field.SELECTION_CHANGE_FREQUENCY_ = 250;925926927/**928* Stops all listeners and timers.929* @protected930*/931goog.editor.Field.prototype.clearListeners = function() {932if (this.eventRegister) {933this.eventRegister.removeAll();934}935936if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &&937this.usesIframe() && this.shouldRefocusOnInputMobileSafari()) {938try {939var editWindow = this.getEditableDomHelper().getWindow();940editWindow.removeEventListener(941goog.events.EventType.KEYDOWN, this.boundRefocusListenerMobileSafari_,942false);943editWindow.removeEventListener(944goog.events.EventType.TOUCHEND,945this.boundRefocusListenerMobileSafari_, false);946} catch (e) {947// The editWindow no longer exists, or has been navigated to a different-948// origin URL. Either way, the event listeners have already been removed949// for us.950}951delete this.boundRefocusListenerMobileSafari_;952}953if (goog.userAgent.OPERA && this.usesIframe()) {954try {955var editWindow = this.getEditableDomHelper().getWindow();956editWindow.removeEventListener(957goog.events.EventType.FOCUS, this.boundFocusListenerOpera_, false);958editWindow.removeEventListener(959goog.events.EventType.BLUR, this.boundBlurListenerOpera_, false);960} catch (e) {961// The editWindow no longer exists, or has been navigated to a different-962// origin URL. Either way, the event listeners have already been removed963// for us.964}965delete this.boundFocusListenerOpera_;966delete this.boundBlurListenerOpera_;967}968969if (this.changeTimerGecko_) {970this.changeTimerGecko_.stop();971}972this.delayedChangeTimer_.stop();973};974975976/** @override */977goog.editor.Field.prototype.disposeInternal = function() {978if (this.isLoading() || this.isLoaded()) {979goog.log.warning(this.logger, 'Disposing a field that is in use.');980}981982if (this.getOriginalElement()) {983this.execCommand(goog.editor.Command.CLEAR_LOREM);984}985986this.tearDownFieldObject_();987this.clearListeners();988this.clearFieldLoadListener_();989this.originalDomHelper = null;990991if (this.eventRegister) {992this.eventRegister.dispose();993this.eventRegister = null;994}995996this.removeAllWrappers();997998if (goog.editor.Field.getActiveFieldId() == this.id) {999goog.editor.Field.setActiveFieldId(null);1000}10011002for (var classId in this.plugins_) {1003var plugin = this.plugins_[classId];1004if (plugin.isAutoDispose()) {1005plugin.dispose();1006}1007}1008delete (this.plugins_);10091010goog.editor.Field.superClass_.disposeInternal.call(this);1011};101210131014/**1015* Attach an wrapper to this field, to be thrown out when the field1016* is disposed.1017* @param {goog.Disposable} wrapper The wrapper to attach.1018*/1019goog.editor.Field.prototype.attachWrapper = function(wrapper) {1020this.wrappers_.push(wrapper);1021};102210231024/**1025* Removes all wrappers and destroys them.1026*/1027goog.editor.Field.prototype.removeAllWrappers = function() {1028var wrapper;1029while (wrapper = this.wrappers_.pop()) {1030wrapper.dispose();1031}1032};103310341035/**1036* Sets whether activating a hyperlink in this editable field will open a new1037* window or not.1038* @param {boolean} followLinkInNewWindow1039*/1040goog.editor.Field.prototype.setFollowLinkInNewWindow = function(1041followLinkInNewWindow) {1042this.followLinkInNewWindow_ = followLinkInNewWindow;1043};104410451046/**1047* List of mutation events in Gecko browsers.1048* @type {Array<string>}1049* @protected1050*/1051goog.editor.Field.MUTATION_EVENTS_GECKO = [1052'DOMNodeInserted', 'DOMNodeRemoved', 'DOMNodeRemovedFromDocument',1053'DOMNodeInsertedIntoDocument', 'DOMCharacterDataModified'1054];105510561057/**1058* Mutation events tell us when something has changed for mozilla.1059* @protected1060*/1061goog.editor.Field.prototype.setupMutationEventHandlersGecko = function() {1062// Always use DOMSubtreeModified on Gecko when not using an iframe so that1063// DOM mutations outside the Field do not trigger handleMutationEventGecko_.1064if (goog.editor.BrowserFeature.HAS_DOM_SUBTREE_MODIFIED_EVENT ||1065!this.usesIframe()) {1066this.eventRegister.listen(1067this.getElement(), 'DOMSubtreeModified',1068this.handleMutationEventGecko_);1069} else {1070var doc = this.getEditableDomHelper().getDocument();1071this.eventRegister.listen(1072doc, goog.editor.Field.MUTATION_EVENTS_GECKO,1073this.handleMutationEventGecko_, true);10741075// DOMAttrModified fires for a lot of events we want to ignore. This goes1076// through a different handler so that we can ignore many of these.1077this.eventRegister.listen(1078doc, 'DOMAttrModified',1079goog.bind(1080this.handleDomAttrChange, this, this.handleMutationEventGecko_),1081true);1082}1083};108410851086/**1087* Handle before change key events and fire the beforetab event if appropriate.1088* This needs to happen on keydown in IE and keypress in FF.1089* @param {goog.events.BrowserEvent} e The browser event.1090* @return {boolean} Whether to still perform the default key action. Only set1091* to true if the actual event has already been canceled.1092* @private1093*/1094goog.editor.Field.prototype.handleBeforeChangeKeyEvent_ = function(e) {1095// There are two reasons to block a key:1096var block =1097// #1: to intercept a tab1098// TODO: possibly don't allow clients to intercept tabs outside of LIs and1099// maybe tables as well?1100(e.keyCode == goog.events.KeyCodes.TAB && !this.dispatchBeforeTab_(e)) ||1101// #2: to block a Firefox-specific bug where Macs try to navigate1102// back a page when you hit command+left arrow or comamnd-right arrow.1103// See https://bugzilla.mozilla.org/show_bug.cgi?id=3418861104// This was fixed in Firefox 29, but still exists in older versions.1105(goog.userAgent.GECKO && e.metaKey &&1106!goog.userAgent.isVersionOrHigher(29) &&1107(e.keyCode == goog.events.KeyCodes.LEFT ||1108e.keyCode == goog.events.KeyCodes.RIGHT));11091110if (block) {1111e.preventDefault();1112return false;1113} else {1114// In Gecko we have both keyCode and charCode. charCode is for human1115// readable characters like a, b and c. However pressing ctrl+c and so on1116// also causes charCode to be set.11171118// TODO(arv): Del at end of field or backspace at beginning should be1119// ignored.1120this.gotGeneratingKey_ = e.charCode ||1121goog.editor.Field.isGeneratingKey_(e, goog.userAgent.GECKO);1122if (this.gotGeneratingKey_) {1123this.dispatchBeforeChange();1124// TODO(robbyw): Should we return the value of the above?1125}1126}11271128return true;1129};113011311132/**1133* Keycodes that result in a selectionchange event (e.g. the cursor moving).1134* @type {!Object<number, number>}1135*/1136goog.editor.Field.SELECTION_CHANGE_KEYCODES = {11378: 1, // backspace11389: 1, // tab113913: 1, // enter114033: 1, // page up114134: 1, // page down114235: 1, // end114336: 1, // home114437: 1, // left114538: 1, // up114639: 1, // right114740: 1, // down114846: 1 // delete1149};115011511152/**1153* Map of keyCodes (not charCodes) that when used in conjunction with the1154* Ctrl key cause selection changes in the field contents. These are the keys1155* that are not handled by the basic formatting trogedit plugins. Note that1156* combinations like Ctrl-left etc are already handled in1157* SELECTION_CHANGE_KEYCODES1158* @type {Object}1159* @private1160*/1161goog.editor.Field.CTRL_KEYS_CAUSING_SELECTION_CHANGES_ = {116265: true, // A116386: true, // V116488: true // X1165};116611671168/**1169* Map of keyCodes (not charCodes) that might need to be handled as a keyboard1170* shortcut (even when ctrl/meta key is not pressed) by some plugin. Currently1171* it is a small list. If it grows too big we can optimize it by using ranges1172* or extending it from SELECTION_CHANGE_KEYCODES1173* @type {Object}1174* @private1175*/1176goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_ = {11778: 1, // backspace11789: 1, // tab117913: 1, // enter118027: 1, // esc118133: 1, // page up118234: 1, // page down118337: 1, // left118438: 1, // up118539: 1, // right118640: 1 // down1187};118811891190/**1191* Calls all the plugins of the given operation, in sequence, with the1192* given arguments. This is short-circuiting: once one plugin cancels1193* the event, no more plugins will be invoked.1194* @param {goog.editor.Plugin.Op} op A plugin op.1195* @param {...*} var_args The arguments to the plugin.1196* @return {boolean} True if one of the plugins cancel the event, false1197* otherwise.1198* @private1199*/1200goog.editor.Field.prototype.invokeShortCircuitingOp_ = function(op, var_args) {1201var plugins = this.indexedPlugins_[op];1202var argList = goog.array.slice(arguments, 1);1203for (var i = 0; i < plugins.length; ++i) {1204// If the plugin returns true, that means it handled the event and1205// we shouldn't propagate to the other plugins.1206var plugin = plugins[i];1207if ((plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) &&1208plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList)) {1209// Only one plugin is allowed to handle the event. If for some reason1210// a plugin wants to handle it and still allow other plugins to handle1211// it, it shouldn't return true.1212return true;1213}1214}12151216return false;1217};121812191220/**1221* Invoke this operation on all plugins with the given arguments.1222* @param {goog.editor.Plugin.Op} op A plugin op.1223* @param {...*} var_args The arguments to the plugin.1224* @private1225*/1226goog.editor.Field.prototype.invokeOp_ = function(op, var_args) {1227var plugins = this.indexedPlugins_[op];1228var argList = goog.array.slice(arguments, 1);1229for (var i = 0; i < plugins.length; ++i) {1230var plugin = plugins[i];1231if (plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) {1232plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList);1233}1234}1235};123612371238/**1239* Reduce this argument over all plugins. The result of each plugin invocation1240* will be passed to the next plugin invocation. See goog.array.reduce.1241* @param {goog.editor.Plugin.Op} op A plugin op.1242* @param {string} arg The argument to reduce. For now, we assume it's a1243* string, but we should widen this later if there are reducing1244* plugins that don't operate on strings.1245* @param {...*} var_args Any extra arguments to pass to the plugin. These args1246* will not be reduced.1247* @return {string} The reduced argument.1248* @private1249*/1250goog.editor.Field.prototype.reduceOp_ = function(op, arg, var_args) {1251var plugins = this.indexedPlugins_[op];1252var argList = goog.array.slice(arguments, 1);1253for (var i = 0; i < plugins.length; ++i) {1254var plugin = plugins[i];1255if (plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) {1256argList[0] = plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList);1257}1258}1259return argList[0];1260};126112621263/**1264* Prepare the given contents, then inject them into the editable field.1265* @param {?string} contents The contents to prepare.1266* @param {Element} field The field element.1267* @protected1268*/1269goog.editor.Field.prototype.injectContents = function(contents, field) {1270var styles = {};1271var newHtml = this.getInjectableContents(contents, styles);1272goog.style.setStyle(field, styles);1273goog.editor.node.replaceInnerHtml(field, newHtml);1274};127512761277/**1278* Returns prepared contents that can be injected into the editable field.1279* @param {?string} contents The contents to prepare.1280* @param {Object} styles A map that will be populated with styles that should1281* be applied to the field element together with the contents.1282* @return {string} The prepared contents.1283*/1284goog.editor.Field.prototype.getInjectableContents = function(contents, styles) {1285return this.reduceOp_(1286goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, contents || '', styles);1287};128812891290/**1291* Handles keydown on the field.1292* @param {goog.events.BrowserEvent} e The browser event.1293* @private1294*/1295goog.editor.Field.prototype.handleKeyDown_ = function(e) {1296// Mac only fires Cmd+A for keydown, not keyup: b/22407515.1297if (goog.userAgent.MAC && e.keyCode == goog.events.KeyCodes.A) {1298this.maybeStartSelectionChangeTimer_(e);1299}13001301if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {1302if (!this.handleBeforeChangeKeyEvent_(e)) {1303return;1304}1305}13061307if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYDOWN, e) &&1308goog.editor.BrowserFeature.USES_KEYDOWN) {1309this.handleKeyboardShortcut_(e);1310}1311};131213131314/**1315* Handles keypress on the field.1316* @param {goog.events.BrowserEvent} e The browser event.1317* @private1318*/1319goog.editor.Field.prototype.handleKeyPress_ = function(e) {1320if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {1321if (!this.handleBeforeChangeKeyEvent_(e)) {1322return;1323}1324} else {1325// In IE only keys that generate output trigger keypress1326// In Mozilla charCode is set for keys generating content.1327this.gotGeneratingKey_ = true;1328this.dispatchBeforeChange();1329}13301331if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYPRESS, e) &&1332!goog.editor.BrowserFeature.USES_KEYDOWN) {1333this.handleKeyboardShortcut_(e);1334}1335};133613371338/**1339* Handles keyup on the field.1340* @param {!goog.events.BrowserEvent} e The browser event.1341* @private1342*/1343goog.editor.Field.prototype.handleKeyUp_ = function(e) {1344if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS &&1345(this.gotGeneratingKey_ ||1346goog.editor.Field.isSpecialGeneratingKey_(e))) {1347// The special keys won't have set the gotGeneratingKey flag, so we check1348// for them explicitly1349this.handleChange();1350}13511352this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYUP, e);1353this.maybeStartSelectionChangeTimer_(e);1354};135513561357/**1358* Fires {@code BEFORESELECTIONCHANGE} and starts the selection change timer1359* (which will fire {@code SELECTIONCHANGE}) if the given event is a key event1360* that causes a selection change.1361* @param {!goog.events.BrowserEvent} e The browser event.1362* @private1363*/1364goog.editor.Field.prototype.maybeStartSelectionChangeTimer_ = function(e) {1365if (this.isEventStopped(goog.editor.Field.EventType.SELECTIONCHANGE)) {1366return;1367}13681369if (goog.editor.Field.SELECTION_CHANGE_KEYCODES[e.keyCode] ||1370((e.ctrlKey || e.metaKey) &&1371goog.editor.Field.CTRL_KEYS_CAUSING_SELECTION_CHANGES_[e.keyCode])) {1372this.dispatchEvent(goog.editor.Field.EventType.BEFORESELECTIONCHANGE);1373this.selectionChangeTimer_.start();1374}1375};137613771378/**1379* Handles keyboard shortcuts on the field. Note that we bake this into our1380* handleKeyPress/handleKeyDown rather than using goog.events.KeyHandler or1381* goog.ui.KeyboardShortcutHandler for performance reasons. Since these1382* are handled on every key stroke, we do not want to be going out to the1383* event system every time.1384* @param {goog.events.BrowserEvent} e The browser event.1385* @private1386*/1387goog.editor.Field.prototype.handleKeyboardShortcut_ = function(e) {1388// Alt key is used for i18n languages to enter certain characters. like1389// control + alt + z (used for IMEs) and control + alt + s for Polish.1390// So we don't invoke handleKeyboardShortcut at all for alt keys.1391if (e.altKey) {1392return;1393}13941395var isModifierPressed = goog.userAgent.MAC ? e.metaKey : e.ctrlKey;1396if (isModifierPressed ||1397goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_[e.keyCode]) {1398// TODO(user): goog.events.KeyHandler uses much more complicated logic1399// to determine key. Consider changing to what they do.1400var key = e.charCode || e.keyCode;14011402if (key == 17) { // Ctrl key1403// In IE and Webkit pressing Ctrl key itself results in this event.1404return;1405}14061407var stringKey = String.fromCharCode(key).toLowerCase();1408// Ctrl+Cmd+Space generates a charCode for a backtick on Mac Firefox, but1409// has the correct string key in the browser event.1410if (goog.userAgent.MAC && goog.userAgent.GECKO && stringKey == '`' &&1411e.getBrowserEvent().key == ' ') {1412stringKey = ' ';1413}1414// Converting the keyCode for "\" using fromCharCode creates "u", so we need1415// to look out for it specifically.1416if (e.keyCode == goog.events.KeyCodes.BACKSLASH) {1417stringKey = '\\';1418}14191420if (this.invokeShortCircuitingOp_(1421goog.editor.Plugin.Op.SHORTCUT, e, stringKey, isModifierPressed)) {1422e.preventDefault();1423// We don't call stopPropagation as some other handler outside of1424// trogedit might need it.1425}1426}1427};142814291430/**1431* Executes an editing command as per the registered plugins.1432* @param {string} command The command to execute.1433* @param {...*} var_args Any additional parameters needed to execute the1434* command.1435* @return {*} False if the command wasn't handled, otherwise, the result of1436* the command.1437*/1438goog.editor.Field.prototype.execCommand = function(command, var_args) {1439var args = arguments;1440var result;14411442var plugins = this.indexedPlugins_[goog.editor.Plugin.Op.EXEC_COMMAND];1443for (var i = 0; i < plugins.length; ++i) {1444// If the plugin supports the command, that means it handled the1445// event and we shouldn't propagate to the other plugins.1446var plugin = plugins[i];1447if (plugin.isEnabled(this) && plugin.isSupportedCommand(command)) {1448result = plugin.execCommand.apply(plugin, args);1449break;1450}1451}14521453return result;1454};145514561457/**1458* Gets the value of command(s).1459* @param {string|Array<string>} commands String name(s) of the command.1460* @return {*} Value of each command. Returns false (or array of falses)1461* if designMode is off or the field is otherwise uneditable, and1462* there are no activeOnUneditable plugins for the command.1463*/1464goog.editor.Field.prototype.queryCommandValue = function(commands) {1465var isEditable = this.isLoaded() && this.isSelectionEditable();1466if (goog.isString(commands)) {1467return this.queryCommandValueInternal_(commands, isEditable);1468} else {1469var state = {};1470for (var i = 0; i < commands.length; i++) {1471state[commands[i]] =1472this.queryCommandValueInternal_(commands[i], isEditable);1473}1474return state;1475}1476};147714781479/**1480* Gets the value of this command.1481* @param {string} command The command to check.1482* @param {boolean} isEditable Whether the field is currently editable.1483* @return {*} The state of this command. Null if not handled.1484* False if the field is uneditable and there are no handlers for1485* uneditable commands.1486* @private1487*/1488goog.editor.Field.prototype.queryCommandValueInternal_ = function(1489command, isEditable) {1490var plugins = this.indexedPlugins_[goog.editor.Plugin.Op.QUERY_COMMAND];1491for (var i = 0; i < plugins.length; ++i) {1492var plugin = plugins[i];1493if (plugin.isEnabled(this) && plugin.isSupportedCommand(command) &&1494(isEditable || plugin.activeOnUneditableFields())) {1495return plugin.queryCommandValue(command);1496}1497}1498return isEditable ? null : false;1499};150015011502/**1503* Fires a change event only if the attribute change effects the editiable1504* field. We ignore events that are internal browser events (ie scrollbar1505* state change)1506* @param {Function} handler The function to call if this is not an internal1507* browser event.1508* @param {goog.events.BrowserEvent} browserEvent The browser event.1509* @protected1510*/1511goog.editor.Field.prototype.handleDomAttrChange = function(1512handler, browserEvent) {1513if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) {1514return;1515}15161517var e = browserEvent.getBrowserEvent();15181519// For XUL elements, since we don't care what they are doing1520try {1521if (e.originalTarget.prefix ||1522/** @type {!Element} */ (e.originalTarget).nodeName == 'scrollbar') {1523return;1524}1525} catch (ex1) {1526// Some XUL nodes don't like you reading their properties. If we got1527// the exception, this implies a XUL node so we can return.1528return;1529}15301531// Check if prev and new values are different, sometimes this fires when1532// nothing has really changed.1533if (e.prevValue == e.newValue) {1534return;1535}1536handler.call(this, e);1537};153815391540/**1541* Handle a mutation event.1542* @param {goog.events.BrowserEvent|Event} e The browser event.1543* @private1544*/1545goog.editor.Field.prototype.handleMutationEventGecko_ = function(e) {1546if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) {1547return;1548}15491550e = e.getBrowserEvent ? e.getBrowserEvent() : e;1551// For people with firebug, firebug sets this property on elements it is1552// inserting into the dom.1553if (e.target.firebugIgnore) {1554return;1555}15561557this.isModified_ = true;1558this.isEverModified_ = true;1559this.changeTimerGecko_.start();1560};156115621563/**1564* Handle drop events. Deal with focus/selection issues and set the document1565* as changed.1566* @param {goog.events.BrowserEvent} e The browser event.1567* @private1568*/1569goog.editor.Field.prototype.handleDrop_ = function(e) {1570if (goog.userAgent.IE) {1571// TODO(user): This should really be done in the loremipsum plugin.1572this.execCommand(goog.editor.Command.CLEAR_LOREM, true);1573}15741575// TODO(user): I just moved this code to this location, but I wonder why1576// it is only done for this case. Investigate.1577if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {1578this.dispatchFocusAndBeforeFocus_();1579}15801581this.dispatchChange();1582};158315841585/**1586* @return {HTMLIFrameElement} The iframe that's body is editable.1587* @protected1588*/1589goog.editor.Field.prototype.getEditableIframe = function() {1590var dh;1591if (this.usesIframe() && (dh = this.getEditableDomHelper())) {1592// If the iframe has been destroyed, the dh could still exist since the1593// node may not be gc'ed, but fetching the window can fail.1594var win = dh.getWindow();1595return /** @type {HTMLIFrameElement} */ (win && win.frameElement);1596}1597return null;1598};159916001601/**1602* @return {goog.dom.DomHelper?} The dom helper for the editable node.1603*/1604goog.editor.Field.prototype.getEditableDomHelper = function() {1605return this.editableDomHelper;1606};160716081609/**1610* @return {goog.dom.AbstractRange?} Closure range object wrapping the selection1611* in this field or null if this field is not currently editable.1612*/1613goog.editor.Field.prototype.getRange = function() {1614var win = this.editableDomHelper && this.editableDomHelper.getWindow();1615return win && goog.dom.Range.createFromWindow(win);1616};161716181619/**1620* Dispatch a selection change event, optionally caused by the given browser1621* event or selecting the given target.1622* @param {goog.events.BrowserEvent=} opt_e Optional browser event causing this1623* event.1624* @param {Node=} opt_target The node the selection changed to.1625*/1626goog.editor.Field.prototype.dispatchSelectionChangeEvent = function(1627opt_e, opt_target) {1628if (this.isEventStopped(goog.editor.Field.EventType.SELECTIONCHANGE)) {1629return;1630}16311632// The selection is editable only if the selection is inside the1633// editable field.1634var range = this.getRange();1635var rangeContainer = range && range.getContainerElement();1636this.isSelectionEditable_ =1637!!rangeContainer && goog.dom.contains(this.getElement(), rangeContainer);16381639this.dispatchCommandValueChange();1640this.dispatchEvent({1641type: goog.editor.Field.EventType.SELECTIONCHANGE,1642originalType: opt_e && opt_e.type1643});16441645this.invokeShortCircuitingOp_(1646goog.editor.Plugin.Op.SELECTION, opt_e, opt_target);1647};164816491650/**1651* Dispatch a selection change event using a browser event that was1652* asynchronously saved earlier.1653* @private1654*/1655goog.editor.Field.prototype.handleSelectionChangeTimer_ = function() {1656var t = this.selectionChangeTarget_;1657this.selectionChangeTarget_ = null;1658this.dispatchSelectionChangeEvent(undefined, t);1659};166016611662/**1663* This dispatches the beforechange event on the editable field1664*/1665goog.editor.Field.prototype.dispatchBeforeChange = function() {1666if (this.isEventStopped(goog.editor.Field.EventType.BEFORECHANGE)) {1667return;1668}16691670this.dispatchEvent(goog.editor.Field.EventType.BEFORECHANGE);1671};167216731674/**1675* This dispatches the beforetab event on the editable field. If this event is1676* cancelled, then the default tab behavior is prevented.1677* @param {goog.events.BrowserEvent} e The tab event.1678* @private1679* @return {boolean} The result of dispatchEvent.1680*/1681goog.editor.Field.prototype.dispatchBeforeTab_ = function(e) {1682return this.dispatchEvent({1683type: goog.editor.Field.EventType.BEFORETAB,1684shiftKey: e.shiftKey,1685altKey: e.altKey,1686ctrlKey: e.ctrlKey1687});1688};168916901691/**1692* Temporarily ignore change events. If the time has already been set, it will1693* fire immediately now. Further setting of the timer is stopped and1694* dispatching of events is stopped until startChangeEvents is called.1695* @param {boolean=} opt_stopChange Whether to ignore base change events.1696* @param {boolean=} opt_stopDelayedChange Whether to ignore delayed change1697* events.1698*/1699goog.editor.Field.prototype.stopChangeEvents = function(1700opt_stopChange, opt_stopDelayedChange) {1701if (opt_stopChange) {1702if (this.changeTimerGecko_) {1703this.changeTimerGecko_.fireIfActive();1704}17051706this.stopEvent(goog.editor.Field.EventType.CHANGE);1707}1708if (opt_stopDelayedChange) {1709this.clearDelayedChange();1710this.stopEvent(goog.editor.Field.EventType.DELAYEDCHANGE);1711}1712};171317141715/**1716* Start change events again and fire once if desired.1717* @param {boolean=} opt_fireChange Whether to fire the change event1718* immediately.1719* @param {boolean=} opt_fireDelayedChange Whether to fire the delayed change1720* event immediately.1721*/1722goog.editor.Field.prototype.startChangeEvents = function(1723opt_fireChange, opt_fireDelayedChange) {17241725if (!opt_fireChange && this.changeTimerGecko_) {1726// In the case where change events were stopped and we're not firing1727// them on start, the user was trying to suppress all change or delayed1728// change events. Clear the change timer now while the events are still1729// stopped so that its firing doesn't fire a stopped change event, or1730// queue up a delayed change event that we were trying to stop.1731this.changeTimerGecko_.fireIfActive();1732}17331734this.startEvent(goog.editor.Field.EventType.CHANGE);1735this.startEvent(goog.editor.Field.EventType.DELAYEDCHANGE);1736if (opt_fireChange) {1737this.handleChange();1738}17391740if (opt_fireDelayedChange) {1741this.dispatchDelayedChange_();1742}1743};174417451746/**1747* Stops the event of the given type from being dispatched.1748* @param {goog.editor.Field.EventType} eventType type of event to stop.1749*/1750goog.editor.Field.prototype.stopEvent = function(eventType) {1751this.stoppedEvents_[eventType] = 1;1752};175317541755/**1756* Re-starts the event of the given type being dispatched, if it had1757* previously been stopped with stopEvent().1758* @param {goog.editor.Field.EventType} eventType type of event to start.1759*/1760goog.editor.Field.prototype.startEvent = function(eventType) {1761// Toggling this bit on/off instead of deleting it/re-adding it1762// saves array allocations.1763this.stoppedEvents_[eventType] = 0;1764};176517661767/**1768* Block an event for a short amount of time. Intended1769* for the situation where an event pair fires in quick succession1770* (e.g., mousedown/mouseup, keydown/keyup, focus/blur),1771* and we want the second event in the pair to get "debounced."1772*1773* WARNING: This should never be used to solve race conditions or for1774* mission-critical actions. It should only be used for UI improvements,1775* where it's okay if the behavior is non-deterministic.1776*1777* @param {goog.editor.Field.EventType} eventType type of event to debounce.1778*/1779goog.editor.Field.prototype.debounceEvent = function(eventType) {1780this.debouncedEvents_[eventType] = goog.now();1781};178217831784/**1785* Checks if the event of the given type has stopped being dispatched1786* @param {goog.editor.Field.EventType} eventType type of event to check.1787* @return {boolean} true if the event has been stopped with stopEvent().1788* @protected1789*/1790goog.editor.Field.prototype.isEventStopped = function(eventType) {1791return !!this.stoppedEvents_[eventType] ||1792(this.debouncedEvents_[eventType] &&1793(goog.now() - this.debouncedEvents_[eventType] <=1794goog.editor.Field.DEBOUNCE_TIME_MS_));1795};179617971798/**1799* Calls a function to manipulate the dom of this field. This method should be1800* used whenever Trogedit clients need to modify the dom of the field, so that1801* delayed change events are handled appropriately. Extra delayed change events1802* will cause undesired states to be added to the undo-redo stack. This method1803* will always fire at most one delayed change event, depending on the value of1804* {@code opt_preventDelayedChange}.1805*1806* @param {function()} func The function to call that will manipulate the dom.1807* @param {boolean=} opt_preventDelayedChange Whether delayed change should be1808* prevented after calling {@code func}. Defaults to always firing1809* delayed change.1810* @param {Object=} opt_handler Object in whose scope to call the listener.1811*/1812goog.editor.Field.prototype.manipulateDom = function(1813func, opt_preventDelayedChange, opt_handler) {18141815this.stopChangeEvents(true, true);1816// We don't want any problems with the passed in function permanently1817// stopping change events. That would break Trogedit.1818try {1819func.call(opt_handler);1820} finally {1821// If the field isn't loaded then change and delayed change events will be1822// started as part of the onload behavior.1823if (this.isLoaded()) {1824// We assume that func always modified the dom and so fire a single change1825// event. Delayed change is only fired if not prevented by the user.1826if (opt_preventDelayedChange) {1827this.startEvent(goog.editor.Field.EventType.CHANGE);1828this.handleChange();1829this.startEvent(goog.editor.Field.EventType.DELAYEDCHANGE);1830} else {1831this.dispatchChange();1832}1833}1834}1835};183618371838/**1839* Dispatches a command value change event.1840* @param {Array<string>=} opt_commands Commands whose state has1841* changed.1842*/1843goog.editor.Field.prototype.dispatchCommandValueChange = function(1844opt_commands) {1845if (opt_commands) {1846this.dispatchEvent({1847type: goog.editor.Field.EventType.COMMAND_VALUE_CHANGE,1848commands: opt_commands1849});1850} else {1851this.dispatchEvent(goog.editor.Field.EventType.COMMAND_VALUE_CHANGE);1852}1853};185418551856/**1857* Dispatches the appropriate set of change events. This only fires1858* synchronous change events in blended-mode, iframe-using mozilla. It just1859* starts the appropriate timer for goog.editor.Field.EventType.DELAYEDCHANGE.1860* This also starts up change events again if they were stopped.1861*1862* @param {boolean=} opt_noDelay True if1863* goog.editor.Field.EventType.DELAYEDCHANGE should be fired syncronously.1864*/1865goog.editor.Field.prototype.dispatchChange = function(opt_noDelay) {1866this.startChangeEvents(true, opt_noDelay);1867};186818691870/**1871* Handle a change in the Editable Field. Marks the field has modified,1872* dispatches the change event on the editable field (moz only), starts the1873* timer for the delayed change event. Note that these actions only occur if1874* the proper events are not stopped.1875*/1876goog.editor.Field.prototype.handleChange = function() {1877if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) {1878return;1879}18801881// Clear the changeTimerGecko_ if it's active, since any manual call to1882// handle change is equiavlent to changeTimerGecko_.fire().1883if (this.changeTimerGecko_) {1884this.changeTimerGecko_.stop();1885}18861887this.isModified_ = true;1888this.isEverModified_ = true;18891890if (this.isEventStopped(goog.editor.Field.EventType.DELAYEDCHANGE)) {1891return;1892}18931894this.delayedChangeTimer_.start();1895};189618971898/**1899* Dispatch a delayed change event.1900* @private1901*/1902goog.editor.Field.prototype.dispatchDelayedChange_ = function() {1903if (this.isEventStopped(goog.editor.Field.EventType.DELAYEDCHANGE)) {1904return;1905}1906// Clear the delayedChangeTimer_ if it's active, since any manual call to1907// dispatchDelayedChange_ is equivalent to delayedChangeTimer_.fire().1908this.delayedChangeTimer_.stop();1909this.isModified_ = false;1910this.dispatchEvent(goog.editor.Field.EventType.DELAYEDCHANGE);1911};191219131914/**1915* Don't wait for the timer and just fire the delayed change event if it's1916* pending.1917*/1918goog.editor.Field.prototype.clearDelayedChange = function() {1919// The changeTimerGecko_ will queue up a delayed change so to fully clear1920// delayed change we must also clear this timer.1921if (this.changeTimerGecko_) {1922this.changeTimerGecko_.fireIfActive();1923}1924this.delayedChangeTimer_.fireIfActive();1925};192619271928/**1929* Dispatch beforefocus and focus for FF. Note that both of these actually1930* happen in the document's "focus" event. Unfortunately, we don't actually1931* have a way of getting in before the focus event in FF (boo! hiss!).1932* In IE, we use onfocusin for before focus and onfocus for focus.1933* @private1934*/1935goog.editor.Field.prototype.dispatchFocusAndBeforeFocus_ = function() {1936this.dispatchBeforeFocus_();1937this.dispatchFocus_();1938};193919401941/**1942* Dispatches a before focus event.1943* @private1944*/1945goog.editor.Field.prototype.dispatchBeforeFocus_ = function() {1946if (this.isEventStopped(goog.editor.Field.EventType.BEFOREFOCUS)) {1947return;1948}19491950this.execCommand(goog.editor.Command.CLEAR_LOREM, true);1951this.dispatchEvent(goog.editor.Field.EventType.BEFOREFOCUS);1952};195319541955/**1956* Dispatches a focus event.1957* @private1958*/1959goog.editor.Field.prototype.dispatchFocus_ = function() {1960if (this.isEventStopped(goog.editor.Field.EventType.FOCUS)) {1961return;1962}1963goog.editor.Field.setActiveFieldId(this.id);19641965this.isSelectionEditable_ = true;19661967this.dispatchEvent(goog.editor.Field.EventType.FOCUS);19681969if (goog.editor.BrowserFeature1970.PUTS_CURSOR_BEFORE_FIRST_BLOCK_ELEMENT_ON_FOCUS) {1971// If the cursor is at the beginning of the field, make sure that it is1972// in the first user-visible line break, e.g.,1973// no selection: <div><p>...</p></div> --> <div><p>|cursor|...</p></div>1974// <div>|cursor|<p>...</p></div> --> <div><p>|cursor|...</p></div>1975// <body>|cursor|<p>...</p></body> --> <body><p>|cursor|...</p></body>1976var field = this.getElement();1977var range = this.getRange();19781979if (range) {1980var focusNode = /** @type {!Element} */ (range.getFocusNode());1981if (range.getFocusOffset() == 0 &&1982(!focusNode || focusNode == field ||1983focusNode.tagName == goog.dom.TagName.BODY)) {1984goog.editor.range.selectNodeStart(field);1985}1986}1987}19881989if (!goog.editor.BrowserFeature.CLEARS_SELECTION_WHEN_FOCUS_LEAVES &&1990this.usesIframe()) {1991var parent = this.getEditableDomHelper().getWindow().parent;1992parent.getSelection().removeAllRanges();1993}1994};199519961997/**1998* Dispatches a blur event.1999* @protected2000*/2001goog.editor.Field.prototype.dispatchBlur = function() {2002if (this.isEventStopped(goog.editor.Field.EventType.BLUR)) {2003return;2004}20052006// Another field may have already been registered as active, so only2007// clear out the active field id if we still think this field is active.2008if (goog.editor.Field.getActiveFieldId() == this.id) {2009goog.editor.Field.setActiveFieldId(null);2010}20112012this.isSelectionEditable_ = false;2013this.dispatchEvent(goog.editor.Field.EventType.BLUR);2014};201520162017/**2018* @return {boolean} Whether the selection is editable.2019*/2020goog.editor.Field.prototype.isSelectionEditable = function() {2021return this.isSelectionEditable_;2022};202320242025/**2026* Event handler for clicks in browsers that will follow a link when the user2027* clicks, even if it's editable. We stop the click manually2028* @param {goog.events.BrowserEvent} e The event.2029* @private2030*/2031goog.editor.Field.cancelLinkClick_ = function(e) {2032if (goog.dom.getAncestorByTagNameAndClass(2033/** @type {Node} */ (e.target), goog.dom.TagName.A)) {2034e.preventDefault();2035}2036};203720382039/**2040* Handle mouse down inside the editable field.2041* @param {goog.events.BrowserEvent} e The event.2042* @private2043*/2044goog.editor.Field.prototype.handleMouseDown_ = function(e) {2045goog.editor.Field.setActiveFieldId(this.id);20462047// Open links in a new window if the user control + clicks.2048if (goog.userAgent.IE) {2049var targetElement = e.target;2050if (targetElement &&2051/** @type {!Element} */ (targetElement).tagName == goog.dom.TagName.A &&2052e.ctrlKey) {2053this.originalDomHelper.getWindow().open(targetElement.href);2054}2055}2056this.waitingForMouseUp_ = true;2057};205820592060/**2061* Handle drag start. Needs to cancel listening for the mouse up event on the2062* window.2063* @param {goog.events.BrowserEvent} e The event.2064* @private2065*/2066goog.editor.Field.prototype.handleDragStart_ = function(e) {2067this.waitingForMouseUp_ = false;2068};206920702071/**2072* Handle mouse up inside the editable field.2073* @param {goog.events.BrowserEvent} e The event.2074* @private2075*/2076goog.editor.Field.prototype.handleMouseUp_ = function(e) {2077if (this.useWindowMouseUp_ && !this.waitingForMouseUp_) {2078return;2079}2080this.waitingForMouseUp_ = false;20812082/*2083* We fire a selection change event immediately for listeners that depend on2084* the native browser event object (e). On IE, a listener that tries to2085* retrieve the selection with goog.dom.Range may see an out-of-date2086* selection range.2087*/2088this.dispatchEvent(goog.editor.Field.EventType.BEFORESELECTIONCHANGE);2089this.dispatchSelectionChangeEvent(e);2090if (goog.userAgent.IE) {2091/*2092* Fire a second selection change event for listeners that need an2093* up-to-date selection range. Save the event's target to be sent with it2094* (it's safer than saving a copy of the event itself).2095*/2096this.selectionChangeTarget_ = /** @type {Node} */ (e.target);2097this.selectionChangeTimer_.start();2098}2099};210021012102/**2103* Retrieve the HTML contents of a field.2104*2105* Do NOT just get the innerHTML of a field directly--there's a lot of2106* processing that needs to happen.2107* @return {string} The scrubbed contents of the field.2108*/2109goog.editor.Field.prototype.getCleanContents = function() {2110if (this.queryCommandValue(goog.editor.Command.USING_LOREM)) {2111return goog.string.Unicode.NBSP;2112}21132114if (!this.isLoaded()) {2115// The field is uneditable, so it's ok to read contents directly.2116var elem = this.getOriginalElement();2117if (!elem) {2118goog.log.log(2119this.logger, goog.log.Level.SHOUT,2120"Couldn't get the field element to read the contents");2121}2122return elem.innerHTML;2123}21242125var fieldCopy = this.getFieldCopy();21262127// Allow the plugins to handle their cleanup.2128this.invokeOp_(goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM, fieldCopy);2129return this.reduceOp_(2130goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML, fieldCopy.innerHTML);2131};213221332134/**2135* Get the copy of the editable field element, which has the innerHTML set2136* correctly.2137* @return {!Element} The copy of the editable field.2138* @protected2139*/2140goog.editor.Field.prototype.getFieldCopy = function() {2141var field = this.getElement();2142// Deep cloneNode strips some script tag contents in IE, so we do this.2143var fieldCopy = /** @type {Element} */ (field.cloneNode(false));21442145// For some reason, when IE sets innerHtml of the cloned node, it strips2146// script tags that fall at the beginning of an element. Appending a2147// non-breaking space prevents this.2148var html = field.innerHTML;2149if (goog.userAgent.IE && html.match(/^\s*<script/i)) {2150html = goog.string.Unicode.NBSP + html;2151}2152fieldCopy.innerHTML = html;2153return fieldCopy;2154};215521562157/**2158* Sets the contents of the field.2159* @param {boolean} addParas Boolean to specify whether to add paragraphs2160* to long fields.2161* @param {?string} html html to insert. If html=null, then this defaults2162* to a nsbp for mozilla and an empty string for IE.2163* @param {boolean=} opt_dontFireDelayedChange True to make this content change2164* not fire a delayed change event.2165* @param {boolean=} opt_applyLorem Whether to apply lorem ipsum styles.2166* @deprecated Use setSafeHtml instead.2167*/2168goog.editor.Field.prototype.setHtml = function(2169addParas, html, opt_dontFireDelayedChange, opt_applyLorem) {2170var safeHtml =2171html ? goog.html.legacyconversions.safeHtmlFromString(html) : null;2172this.setSafeHtml(2173addParas, safeHtml, opt_dontFireDelayedChange, opt_applyLorem);2174};217521762177/**2178* Sets the contents of the field.2179* @param {boolean} addParas Boolean to specify whether to add paragraphs2180* to long fields.2181* @param {?goog.html.SafeHtml} html html to insert. If html=null, then this2182* defaults to a nsbp for mozilla and an empty string for IE.2183* @param {boolean=} opt_dontFireDelayedChange True to make this content change2184* not fire a delayed change event.2185* @param {boolean=} opt_applyLorem Whether to apply lorem ipsum styles.2186*/2187goog.editor.Field.prototype.setSafeHtml = function(2188addParas, html, opt_dontFireDelayedChange, opt_applyLorem) {2189if (this.isLoading()) {2190goog.log.error(this.logger, "Can't set html while loading Trogedit");2191return;2192}21932194// Clear the lorem ipsum style, always.2195if (opt_applyLorem) {2196this.execCommand(goog.editor.Command.CLEAR_LOREM);2197}21982199if (html && addParas) {2200html = goog.html.SafeHtml.create('p', {}, html);2201}22022203// If we don't want change events to fire, we have to turn off change events2204// before setting the field contents, since that causes mutation events.2205if (opt_dontFireDelayedChange) {2206this.stopChangeEvents(false, true);2207}22082209this.setInnerHtml_(html);22102211// Set the lorem ipsum style, if the element is empty.2212if (opt_applyLorem) {2213this.execCommand(goog.editor.Command.UPDATE_LOREM);2214}22152216// TODO(user): This check should probably be moved to isEventStopped and2217// startEvent.2218if (this.isLoaded()) {2219if (opt_dontFireDelayedChange) { // Turn back on change events2220// We must fire change timer if necessary before restarting change events!2221// Otherwise, the change timer firing after we restart events will cause2222// the delayed change we were trying to stop. Flow:2223// Stop delayed change2224// setInnerHtml_, this starts the change timer2225// start delayed change2226// change timer fires2227// starts delayed change timer since event was not stopped2228// delayed change fires for the delayed change we tried to stop.2229if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {2230this.changeTimerGecko_.fireIfActive();2231}2232this.startChangeEvents();2233} else { // Mark the document as changed and fire change events.2234this.dispatchChange();2235}2236}2237};223822392240/**2241* Sets the inner HTML of the field. Works on both editable and2242* uneditable fields.2243* @param {?goog.html.SafeHtml} html The new inner HTML of the field.2244* @private2245*/2246goog.editor.Field.prototype.setInnerHtml_ = function(html) {2247var field = this.getElement();2248if (field) {2249// Safari will put <style> tags into *new* <head> elements. When setting2250// HTML, we need to remove these spare <head>s to make sure there's a2251// clean slate, but keep the first <head>.2252// Note: We punt on this issue for the non iframe case since2253// we don't want to screw with the main document.2254if (this.usesIframe() && goog.editor.BrowserFeature.MOVES_STYLE_TO_HEAD) {2255var heads = goog.dom.getElementsByTagName(2256goog.dom.TagName.HEAD, goog.asserts.assert(field.ownerDocument));2257for (var i = heads.length - 1; i >= 1; --i) {2258heads[i].parentNode.removeChild(heads[i]);2259}2260}2261} else {2262field = this.getOriginalElement();2263}22642265if (field) {2266this.injectContents(html && goog.html.SafeHtml.unwrap(html), field);2267}2268};226922702271/**2272* Attemps to turn on designMode for a document. This function can fail under2273* certain circumstances related to the load event, and will throw an exception.2274* @protected2275*/2276goog.editor.Field.prototype.turnOnDesignModeGecko = function() {2277var doc = this.getEditableDomHelper().getDocument();22782279// NOTE(nicksantos): This will fail under certain conditions, like2280// when the node has display: none. It's up to clients to ensure that2281// their fields are valid when they try to make them editable.2282doc.designMode = 'on';22832284if (goog.editor.BrowserFeature.HAS_STYLE_WITH_CSS) {2285doc.execCommand('styleWithCSS', false, false);2286}2287};228822892290/**2291* Installs styles if needed. Only writes styles when they can't be written2292* inline directly into the field.2293* @protected2294*/2295goog.editor.Field.prototype.installStyles = function() {2296if (this.cssStyles && this.shouldLoadAsynchronously()) {2297goog.style.installSafeStyleSheet(2298goog.html.legacyconversions.safeStyleSheetFromString(this.cssStyles),2299this.getElement());2300}2301};230223032304/**2305* Signal that the field is loaded and ready to use. Change events now are2306* in effect.2307* @private2308*/2309goog.editor.Field.prototype.dispatchLoadEvent_ = function() {2310this.getElement();2311this.installStyles();2312this.startChangeEvents();2313goog.log.info(this.logger, 'Dispatching load ' + this.id);2314this.dispatchEvent(goog.editor.Field.EventType.LOAD);2315};231623172318/**2319* @return {boolean} Whether the field is uneditable.2320*/2321goog.editor.Field.prototype.isUneditable = function() {2322return this.loadState_ == goog.editor.Field.LoadState_.UNEDITABLE;2323};232423252326/**2327* @return {boolean} Whether the field has finished loading.2328*/2329goog.editor.Field.prototype.isLoaded = function() {2330return this.loadState_ == goog.editor.Field.LoadState_.EDITABLE;2331};233223332334/**2335* @return {boolean} Whether the field is in the process of loading.2336*/2337goog.editor.Field.prototype.isLoading = function() {2338return this.loadState_ == goog.editor.Field.LoadState_.LOADING;2339};234023412342/**2343* Gives the field focus.2344*/2345goog.editor.Field.prototype.focus = function() {2346if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && this.usesIframe()) {2347// In designMode, only the window itself can be focused; not the element.2348this.getEditableDomHelper().getWindow().focus();2349} else {2350if (goog.userAgent.OPERA) {2351// Opera will scroll to the bottom of the focused document, even2352// if it is contained in an iframe that is scrolled to the top and2353// the bottom flows past the end of it. To prevent this,2354// save the scroll position of the document containing the editor2355// iframe, then restore it after the focus.2356var scrollX = this.appWindow_.pageXOffset;2357var scrollY = this.appWindow_.pageYOffset;2358}2359this.getElement().focus();2360if (goog.userAgent.OPERA) {2361this.appWindow_.scrollTo(2362/** @type {number} */ (scrollX), /** @type {number} */ (scrollY));2363}2364}2365};236623672368/**2369* Gives the field focus and places the cursor at the start of the field.2370*/2371goog.editor.Field.prototype.focusAndPlaceCursorAtStart = function() {2372// NOTE(user): Excluding Gecko to maintain existing behavior post refactoring2373// placeCursorAtStart into its own method. In Gecko browsers that currently2374// have a selection the existing selection will be restored, otherwise it2375// will go to the start.2376// TODO(user): Refactor the code using this and related methods. We should2377// only mess with the selection in the case where there is not an existing2378// selection in the field.2379if (goog.editor.BrowserFeature.HAS_IE_RANGES || !goog.userAgent.GECKO) {2380this.placeCursorAtStart();2381}2382this.focus();2383};238423852386/**2387* Place the cursor at the start of this field. It's recommended that you only2388* use this method (and manipulate the selection in general) when there is not2389* an existing selection in the field.2390*/2391goog.editor.Field.prototype.placeCursorAtStart = function() {2392this.placeCursorAtStartOrEnd_(true);2393};239423952396/**2397* Place the cursor at the start of this field. It's recommended that you only2398* use this method (and manipulate the selection in general) when there is not2399* an existing selection in the field.2400*/2401goog.editor.Field.prototype.placeCursorAtEnd = function() {2402this.placeCursorAtStartOrEnd_(false);2403};240424052406/**2407* Helper method to place the cursor at the start or end of this field.2408* @param {boolean} isStart True for start, false for end.2409* @private2410*/2411goog.editor.Field.prototype.placeCursorAtStartOrEnd_ = function(isStart) {2412var field = this.getElement();2413if (field) {2414var cursorPosition = isStart ? goog.editor.node.getLeftMostLeaf(field) :2415goog.editor.node.getRightMostLeaf(field);2416if (field == cursorPosition) {2417// The rightmost leaf we found was the field element itself (which likely2418// means the field element is empty). We can't place the cursor next to2419// the field element, so just place it at the beginning.2420goog.dom.Range.createCaret(field, 0).select();2421} else {2422goog.editor.range.placeCursorNextTo(cursorPosition, isStart);2423}2424this.dispatchSelectionChangeEvent();2425}2426};242724282429/**2430* Restore a saved range, and set the focus on the field.2431* If no range is specified, we simply set the focus.2432* @param {goog.dom.SavedRange=} opt_range A previously saved selected range.2433*/2434goog.editor.Field.prototype.restoreSavedRange = function(opt_range) {2435if (opt_range) {2436opt_range.restore();2437}2438this.focus();2439};244024412442/**2443* Makes a field editable.2444*2445* @param {string=} opt_iframeSrc URL to set the iframe src to if necessary.2446*/2447goog.editor.Field.prototype.makeEditable = function(opt_iframeSrc) {2448this.loadState_ = goog.editor.Field.LoadState_.LOADING;24492450var field = this.getOriginalElement();24512452// TODO: In the fieldObj, save the field's id, className, cssText2453// in order to reset it on closeField. That way, we can muck with the field's2454// css, id, class and restore to how it was at the end.2455this.nodeName = field.nodeName;2456this.savedClassName_ = field.className;2457this.setInitialStyle(field.style.cssText);24582459goog.dom.classlist.add(field, 'editable');24602461this.makeEditableInternal(opt_iframeSrc);2462};246324642465/**2466* Handles actually making something editable - creating necessary nodes,2467* injecting content, etc.2468* @param {string=} opt_iframeSrc URL to set the iframe src to if necessary.2469* @protected2470*/2471goog.editor.Field.prototype.makeEditableInternal = function(opt_iframeSrc) {2472this.makeIframeField_(opt_iframeSrc);2473};247424752476/**2477* Handle the loading of the field (e.g. once the field is ready to setup).2478* TODO(user): this should probably just be moved into dispatchLoadEvent_.2479* @protected2480*/2481goog.editor.Field.prototype.handleFieldLoad = function() {2482if (goog.userAgent.IE) {2483// This sometimes fails if the selection is invalid. This can happen, for2484// example, if you attach a CLICK handler to the field that causes the2485// field to be removed from the DOM and replaced with an editor2486// -- however, listening to another event like MOUSEDOWN does not have this2487// issue since no mouse selection has happened at that time.2488goog.dom.Range.clearSelection(this.editableDomHelper.getWindow());2489}24902491if (goog.editor.Field.getActiveFieldId() != this.id) {2492this.execCommand(goog.editor.Command.UPDATE_LOREM);2493}24942495this.setupChangeListeners_();2496this.dispatchLoadEvent_();24972498// Enabling plugins after we fire the load event so that clients have a2499// chance to set initial field contents before we start mucking with2500// everything.2501for (var classId in this.plugins_) {2502this.plugins_[classId].enable(this);2503}2504};250525062507/**2508* Closes the field and cancels all pending change timers. Note that this2509* means that if a change event has not fired yet, it will not fire. Clients2510* should check fieldOj.isModified() if they depend on the final change event.2511* Throws an error if the field is already uneditable.2512*2513* @param {boolean=} opt_skipRestore True to prevent copying of editable field2514* contents back into the original node.2515*/2516goog.editor.Field.prototype.makeUneditable = function(opt_skipRestore) {2517if (this.isUneditable()) {2518throw Error('makeUneditable: Field is already uneditable');2519}25202521// Fire any events waiting on a timeout.2522// Clearing delayed change also clears changeTimerGecko_.2523this.clearDelayedChange();2524this.selectionChangeTimer_.fireIfActive();2525this.execCommand(goog.editor.Command.CLEAR_LOREM);25262527var html = null;2528if (!opt_skipRestore && this.getElement()) {2529// Rest of cleanup is simpler if field was never initialized.2530html = this.getCleanContents();2531}25322533// First clean up anything that happens in makeFieldEditable2534// (i.e. anything that needs cleanup even if field has not loaded).2535this.clearFieldLoadListener_();25362537var field = this.getOriginalElement();2538if (goog.editor.Field.getActiveFieldId() == field.id) {2539goog.editor.Field.setActiveFieldId(null);2540}25412542// Clear all listeners before removing the nodes from the dom - if2543// there are listeners on the iframe window, Firefox throws errors trying2544// to unlisten once the iframe is no longer in the dom.2545this.clearListeners();25462547// For fields that have loaded, clean up anything that happened in2548// handleFieldOpen or later.2549// If html is provided, copy it back and reset the properties on the field2550// so that the original node will have the same properties as it did before2551// it was made editable.2552if (goog.isString(html)) {2553goog.editor.node.replaceInnerHtml(field, html);2554this.resetOriginalElemProperties();2555}25562557this.restoreDom();2558this.tearDownFieldObject_();25592560// On Safari, make sure to un-focus the field so that the2561// native "current field" highlight style gets removed.2562if (goog.userAgent.WEBKIT) {2563field.blur();2564}25652566this.execCommand(goog.editor.Command.UPDATE_LOREM);2567this.dispatchEvent(goog.editor.Field.EventType.UNLOAD);2568};256925702571/**2572* Restores the dom to how it was before being made editable.2573* @protected2574*/2575goog.editor.Field.prototype.restoreDom = function() {2576// TODO(user): Consider only removing the iframe if we are2577// restoring the original node, aka, if opt_html.2578var field = this.getOriginalElement();2579// TODO(robbyw): Consider throwing an error if !field.2580if (field) {2581// If the field is in the process of loading when it starts getting torn2582// up, the iframe will not exist.2583var iframe = this.getEditableIframe();2584if (iframe) {2585goog.dom.replaceNode(field, iframe);2586}2587}2588};258925902591/**2592* Returns true if the field needs to be loaded asynchrnously.2593* @return {boolean} True if loads are async.2594* @protected2595*/2596goog.editor.Field.prototype.shouldLoadAsynchronously = function() {2597if (!goog.isDef(this.isHttps_)) {2598this.isHttps_ = false;25992600if (goog.userAgent.IE && this.usesIframe()) {2601// IE iframes need to load asynchronously if they are in https as we need2602// to set an actual src on the iframe and wait for it to load.26032604// Find the top-most window we have access to and see if it's https.2605// Technically this could fail if we have an http frame in an https frame2606// on the same domain (or vice versa), but walking up the window hierarchy2607// to find the first window that has an http* protocol seems like2608// overkill.2609var win = this.originalDomHelper.getWindow();2610while (win != win.parent) {2611try {2612win = win.parent;2613} catch (e) {2614break;2615}2616}2617var loc = win.location;2618this.isHttps_ =2619loc.protocol == 'https:' && loc.search.indexOf('nocheckhttps') == -1;2620}2621}2622return this.isHttps_;2623};262426252626/**2627* Start the editable iframe creation process for Mozilla or IE whitebox.2628* The iframes load asynchronously.2629*2630* @param {string=} opt_iframeSrc URL to set the iframe src to if necessary.2631* @private2632*/2633goog.editor.Field.prototype.makeIframeField_ = function(opt_iframeSrc) {2634var field = this.getOriginalElement();2635// TODO(robbyw): Consider throwing an error if !field.2636if (field) {2637var html = field.innerHTML;26382639// Invoke prepareContentsHtml on all plugins to prepare html for editing.2640// Make sure this is done before calling this.attachFrame which removes the2641// original element from DOM tree. Plugins may assume that the original2642// element is still in its original position in DOM.2643var styles = {};2644html = this.reduceOp_(2645goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, html, styles);26462647var iframe = this.originalDomHelper.createDom(2648goog.dom.TagName.IFRAME, this.getIframeAttributes());26492650// TODO(nicksantos): Figure out if this is ever needed in SAFARI?2651// In IE over HTTPS we need to wait for a load event before we set up the2652// iframe, this is to prevent a security prompt or access is denied2653// errors.2654// NOTE(user): This hasn't been confirmed. isHttps_ allows a query2655// param, nocheckhttps, which we can use to ascertain if this is actually2656// needed. It was originally thought to be needed for IE6 SP1, but2657// errors have been seen in IE7 as well.2658if (this.shouldLoadAsynchronously()) {2659// onLoad is the function to call once the iframe is ready to continue2660// loading.2661var onLoad =2662goog.bind(this.iframeFieldLoadHandler, this, iframe, html, styles);26632664this.fieldLoadListenerKey_ =2665goog.events.listen(iframe, goog.events.EventType.LOAD, onLoad, true);26662667if (opt_iframeSrc) {2668iframe.src = opt_iframeSrc;2669}2670}26712672this.attachIframe(iframe);26732674// Only continue if its not IE HTTPS in which case we're waiting for load.2675if (!this.shouldLoadAsynchronously()) {2676this.iframeFieldLoadHandler(iframe, html, styles);2677}2678}2679};268026812682/**2683* Given the original field element, and the iframe that is destined to2684* become the editable field, styles them appropriately and add the iframe2685* to the dom.2686*2687* @param {HTMLIFrameElement} iframe The iframe element.2688* @protected2689*/2690goog.editor.Field.prototype.attachIframe = function(iframe) {2691var field = this.getOriginalElement();2692// TODO(user): Why do we do these two lines .. and why whitebox only?2693iframe.className = field.className;2694iframe.id = field.id;2695goog.dom.replaceNode(iframe, field);2696};269726982699/**2700* @param {Object} extraStyles A map of extra styles.2701* @return {!goog.editor.icontent.FieldFormatInfo} The FieldFormatInfo2702* object for this field's configuration.2703* @protected2704*/2705goog.editor.Field.prototype.getFieldFormatInfo = function(extraStyles) {2706var originalElement = this.getOriginalElement();2707var isStandardsMode = goog.editor.node.isStandardsMode(originalElement);27082709return new goog.editor.icontent.FieldFormatInfo(2710this.id, isStandardsMode, false, false, extraStyles);2711};271227132714/**2715* Writes the html content into the iframe. Handles writing any aditional2716* styling as well.2717* @param {HTMLIFrameElement} iframe Iframe to write contents into.2718* @param {string} innerHtml The html content to write into the iframe.2719* @param {Object} extraStyles A map of extra style attributes.2720* @protected2721*/2722goog.editor.Field.prototype.writeIframeContent = function(2723iframe, innerHtml, extraStyles) {2724var formatInfo = this.getFieldFormatInfo(extraStyles);27252726if (this.shouldLoadAsynchronously()) {2727var doc = goog.dom.getFrameContentDocument(iframe);2728goog.editor.icontent.writeHttpsInitialIframe(formatInfo, doc, innerHtml);2729} else {2730var styleInfo = new goog.editor.icontent.FieldStyleInfo(2731this.getElement(), this.cssStyles);2732goog.editor.icontent.writeNormalInitialIframe(2733formatInfo, innerHtml, styleInfo, iframe);2734}2735};273627372738/**2739* The function to call when the editable iframe loads.2740*2741* @param {HTMLIFrameElement} iframe Iframe that just loaded.2742* @param {string} innerHtml Html to put inside the body of the iframe.2743* @param {Object} styles Property-value map of CSS styles to install on2744* editable field.2745* @protected2746*/2747goog.editor.Field.prototype.iframeFieldLoadHandler = function(2748iframe, innerHtml, styles) {2749this.clearFieldLoadListener_();27502751iframe.allowTransparency = 'true';2752this.writeIframeContent(iframe, innerHtml, styles);2753var doc = goog.dom.getFrameContentDocument(iframe);27542755// Make sure to get this pointer after the doc.write as the doc.write2756// clobbers all the document contents.2757var body = doc.body;2758this.setupFieldObject(body);27592760if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && this.usesIframe()) {2761this.turnOnDesignModeGecko();2762}27632764this.handleFieldLoad();2765};276627672768/**2769* Clears fieldLoadListener for a field. Must be called even (especially?) if2770* the field is not yet loaded and therefore not in this.fieldMap_2771* @private2772*/2773goog.editor.Field.prototype.clearFieldLoadListener_ = function() {2774if (this.fieldLoadListenerKey_) {2775goog.events.unlistenByKey(this.fieldLoadListenerKey_);2776this.fieldLoadListenerKey_ = null;2777}2778};277927802781/**2782* @return {!Object} Get the HTML attributes for this field's iframe.2783* @protected2784*/2785goog.editor.Field.prototype.getIframeAttributes = function() {2786var iframeStyle = 'padding:0;' + this.getOriginalElement().style.cssText;27872788if (!goog.string.endsWith(iframeStyle, ';')) {2789iframeStyle += ';';2790}27912792iframeStyle += 'background-color:white;';27932794// Ensure that the iframe has default overflow styling. If overflow is2795// set to auto, an IE rendering bug can occur when it tries to render a2796// table at the very bottom of the field, such that the table would cause2797// a scrollbar, that makes the entire field go blank.2798if (goog.userAgent.IE) {2799iframeStyle += 'overflow:visible;';2800}28012802return {'frameBorder': 0, 'style': iframeStyle};2803};280428052806