editor.js revision 008f8a72828325c3326b73e5d40abef1cdac4896
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * Creates a wrapper around an iframe. It loads the content either from a local
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * file or from script and creates a local YUI instance bound to that new window and document.
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * @module editor
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * @submodule frame
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * Creates a wrapper around an iframe. It loads the content either from a local
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * file or from script and creates a local YUI instance bound to that new window and document.
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * @class Frame
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * @for Frame
bdade071951e7b602a31cd83bd3f3a167f2e45a1gouldtj * @extends Base
var Frame = function() {
_ready: null,
_rendered: null,
_iframe: null,
_instance: null,
extra_css = ((this.get('extracss')) ? '<style id="extra_css">' + this.get('extracss') + '</style>' : '');
* @return {Object} Object hash of window and document references, if a YUI config was passed, it is returned.
_resolveWinDoc: function(c) {
var config = (c) ? c : {};
return config;
* takes the current EventFacade and augments it to fire on the Frame host. It adds two new properties
_onDomEvent: function(e) {
e.frameEvent = e;
initializer: function() {
emitFacade: true,
destructor: function() {
inst = null;
_DOMPaste: function(e) {
data = null;
e.frameEvent = e;
if (data) {
e.clipboardData = {
getData: function() {
return data;
e.clipboardData = null;
_defReadyFn: function() {
this._fixIECursors();
* It appears that having a BR tag anywhere in the source "below" a table with a percentage width (in IE 7 & 8)
* if there is any TEXTINPUT's outside the iframe, the cursor will rapidly flickr and the CPU would occasionally
* spike. This method finds all <BR>'s below the sourceIndex of the first table. Does some checks to see if they
* can be modified and replaces then with a <WBR> so the layout will remain in tact, but the flickering will
_fixIECursors: function() {
if (b.size()) {
* @description Called once the content is available in the frame/window and calls the final use call
_onContentReady: function(e) {
if (!this._ready) {
this._ready = true;
this._ieSetBodyHeight();
_ieHeightCounter: null,
_ieSetBodyHeight: function(e) {
if (!this._ieHeightCounter) {
this._ieHeightCounter++;
var run = false;
run = true;
switch (e.keyCode) {
run = true;
if (run) {
* @description Resolves the basehref of the page the frame is created on. Only applies to dynamic content.
return href;
if (this._ready) {
return html;
if (this._ready) {
}, this, html));
return html;
if (!this._ready) {
return str;
if (this._ready) {
return css;
if (this._ready) {
return css;
this._onContentReady();
} catch (err) {}
* @description This is a scoped version of the normal YUI.use method & is bound to this frame/window.
use: function() {
cb = false;
if (cb) {
* @param {String} cont The container to act as a delegate, if no "sel" passed, the body is assumed as the container.
if (!inst) {
if (!sel) {
getInstance: function() {
return this._instance;
* @param {String/HTMLElement/Node} node The node to render to
if (this._rendered) {
this._rendered = true;
if (node) {
this._instanceLoaded(i);
config = {
debug: false,
if (timer) {
fn();
_handleFocus: function() {
if (c.size()) {
if (par) {
} catch (ierr) {
if (fn === true) {
this._handleFocus();
fn();
if (fn === true) {
this._handleFocus();
fn();
} catch (ferr) {
show: function() {
this.focus();
hide: function() {
DOM_EVENTS: {
//DEFAULT_CSS: 'html { height: 95%; } body { padding: 7px; background-color: #fff; font: 13px/1.22 arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small; } a, a:visited, a:hover { color: blue !important; text-decoration: underline !important; cursor: text !important; } img { cursor: pointer !important; border: none; }',
DEFAULT_CSS: 'body { background-color: #fff; font: 13px/1.22 arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small; } a, a:visited, a:hover { color: blue !important; text-decoration: underline !important; cursor: text !important; } img { cursor: pointer !important; border: none; }',
//HTML: '<iframe border="0" frameBorder="0" marginWidth="0" marginHeight="0" leftMargin="0" topMargin="0" allowTransparency="true" width="100%" height="99%"></iframe>',
HTML: '<iframe border="0" frameBorder="0" marginWidth="0" marginHeight="0" leftMargin="0" topMargin="0" allowTransparency="true" width="100%" height="99%"></iframe>',
PAGE_HTML: '<html dir="{DIR}" lang="{LANG}"><head><title>{TITLE}</title>{META}<base href="{BASE_HREF}"/>{LINKED_CSS}<style id="editor_css">{DEFAULT_CSS}</style>{EXTRA_CSS}</head><body>{CONTENT}</body></html>',
* @description Parses document.doctype and generates a DocType to match the parent page, if supported.
* For IE8, it grabs document.all[0].nodeValue and uses that. For IE < 8, it falls back to Frame.DOC_TYPE.
getDocType: function() {
if (dt) {
str = '<!DOCTYPE ' + dt.name + ((dt.publicId) ? ' ' + dt.publicId : '') + ((dt.systemId) ? ' ' + dt.systemId : '') + '>';
return str;
* @description The DOCTYPE to prepend to the new document when created. Should match the one on the page being served.
DOC_TYPE: '<!DOCTYPE HTML PUBLIC "-/'+'/W3C/'+'/DTD HTML 4.01/'+'/EN" "http:/'+'/www.w3.org/TR/html4/strict.dtd">',
META: '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=7">',
ATTRS: {
title: {
dir: {
lang: {
src: {
designMode: {
writeOnce: true,
value: false
content: {
basehref: {
value: false,
* @description Array of modules to include in the scoped YUI instance at render time. Default: ['none', 'selector-css2']
use: {
writeOnce: true,
* @type String/HTMLElement/Node
container: {
setter: function(n) {
return Y.one(n);
node: {
readOnly: true,
value: null,
getter: function() {
return this._iframe;
id: {
writeOnce: true,
if (!id) {
return id;
linkedcss: {
extracss: {
host: {
value: false
defaultblock: {
if (this.isCollapsed) {
if (domEvent) {
if (!ieNode) {
if (!ieNode) {
if (ieNode) {
this.anchorOffset = this.focusOffset = (this.anchorNode.nodeValue) ? this.anchorNode.nodeValue.length : 0 ;
* Performs a prefilter on all nodes in the editor. Looks for nodes with a style: fontFamily or font face
* It then creates a dynamic class assigns it and removed the property. This is so that we don't lose
ls;
* Method attempts to replace all "orphined" text nodes in the main body by wrapping them with a <p>. Called from filter.
var childs = Y.config.doc.body.childNodes, i, node, wrapped = false, doit = true,
if (wrapped) {
var newChild = Y.Node.create('<' + Y.Selection.DEFAULT_BLOCK_TAG + '></' + Y.Selection.DEFAULT_BLOCK_TAG + '>'),
n.remove();
if (body) {
return html;
n = n.parentNode;
} catch (re) {
return Y.one(n);
return txt;
if (c.size()) {
b.remove();
b.remove();
text: null,
isCollapsed: null,
anchorNode: null,
anchorOffset: null,
anchorTextNode: null,
focusNode: null,
focusOffset: null,
focusTextNode: null,
_selection: null,
getSelected: function() {
items = [];
* Insert HTML at the current cursor position and return a Node instance of the newly inserted element.
* Insert HTML at the current cursor position, this method gives you control over the text node to insert into and the offset where to put it.
var cur = Y.Node.create('<' + Y.Selection.DEFAULT_TAG + ' class="yui-non"></' + Y.Selection.DEFAULT_TAG + '>'),
node = b;
if (inHTML) {
return newNode;
newNode = b;
if (txt2) {
return newNode;
* Get all elements inside a selection and wrap them with a new element and return a NodeList of all elements touched.
if (!this.isCollapsed) {
return changed;
return Y.all([]);
return newNode;
remove: function() {
createRange: function() {
if (!node) {
if (collapse) {
} catch (err) {
if (collapse) {
setCursor: function() {
this.removeCursor(false);
getCursor: function() {
* @param {Boolean} keep Setting this to true will keep the node, but remove the unique parts that make it the cursor.
if (cur) {
if (keep) {
return cur;
if (collapse !== false) {
collapse = true;
if (end !== false) {
end = true;
if (cur) {
toString: function() {
var ExecCommand = function() {
_lastKey: null,
_inst: null,
if (fn) {
} catch (e1) {
} catch (e2) {
getInstance: function() {
if (!this._inst) {
return this._inst;
initializer: function() {
ATTRS: {
host: {
value: false
COMMANDS: {
return out;
var insert = function(n) {
last = s;
n.remove();
n = this.command('inserthtml', '<span style="color: ' + val + '">' + inst.Selection.CURSOR + '</span>');
n = this.command('inserthtml', '<span style="background-color: ' + val + '">' + inst.Selection.CURSOR + '</span>');
hilitecolor: function() {
* Noramlizes lists creation/destruction for IE. All others pass through to native calls
n.replace(s);
if (par) {
html = [];
if (dir) {
if (dir) {
if (dir) {
* This method is meant to normalize IE's in ability to exec the proper command on elements with CSS styling.
p = p.parentNode;
c = d.childNodes;
Y.each(c, function(f) {
s.appendChild(f);
var EditorTab = function() {
_onNodeChange: function(e) {
e.preventDefault();
initializer: function() {
ATTRS: {
host: {
value: false
* Base class for Editor. Handles the business logic of Editor, no GUI involved only utility methods and events.
* Adds prompt style link creation. Adds an override for the <a href="Plugin.ExecCommand.html#method_COMMANDS.createlink">createlink execCommand</a>.
var CreateLinkBase = {};
* Override for the createlink method from the <a href="Plugin.CreateLinkBase.html">CreateLinkBase</a> plugin.
if (url) {
url = inst.config.doc.createTextNode(url);
* Base class for Editor. Handles the business logic of Editor, no GUI involved only utility methods and events.
* Base class for Editor. Handles the business logic of Editor, no GUI involved only utility methods and events.
sel = inst.config.doc.selection.createRange();
if (!EditorBase.NC_KEYS[e.changedEvent.keyCode] && !e.changedEvent.shiftKey && !e.changedEvent.ctrlKey && (e.changedEvent.keyCode !== 13)) {
if (!e.fontFamily) {
if (!e.fontSize) {
if (!e.fontColor) {
if (!e.backgroundColor) {
while (domNode !== null) {
if ((domNode === inst.config.doc.documentElement) || (domNode === inst.config.doc) || !domNode.tagName) {
domNode = null;
domNode = null;
domNode = null;
if (nodeList) {
_afterFrameReady: function() {
_beforeFrameDeactivate: function(e) {
_onFrameActivate: function(e) {
n.remove();
_onPaste: function(e) {
this.fire('nodeChange', { changedNode: e.frameTarget, changedType: 'paste', changedEvent: e.frameEvent });
_onFrameMouseUp: function(e) {
this.fire('nodeChange', { changedNode: e.frameTarget, changedType: 'mouseup', changedEvent: e.frameEvent });
_onFrameMouseDown: function(e) {
this.fire('nodeChange', { changedNode: e.frameTarget, changedType: 'mousedown', changedEvent: e.frameEvent });
_currentSelection: null,
_currentSelectionTimer: null,
_currentSelectionClear: null,
_onFrameKeyDown: function(e) {
if (!this._currentSelection) {
if (this._currentSelectionTimer) {
this._currentSelectionClear = true;
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: 'keydown', changedEvent: e.frameEvent });
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: EditorBase.NC_KEYS[e.keyCode], changedEvent: e.frameEvent });
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: EditorBase.NC_KEYS[e.keyCode] + '-down', changedEvent: e.frameEvent });
_onFrameKeyPress: function(e) {
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: 'keypress', changedEvent: e.frameEvent });
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: EditorBase.NC_KEYS[e.keyCode] + '-press', changedEvent: e.frameEvent });
_onFrameKeyUp: function(e) {
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: 'keyup', selection: sel, changedEvent: e.frameEvent });
this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: EditorBase.NC_KEYS[e.keyCode] + '-up', selection: sel, changedEvent: e.frameEvent });
if (this._currentSelectionClear) {
* @return {Node/NodeList} The Node or Nodelist affected by the command. Only returns on override commands, not browser defined commands.
switch (cmd) {
return ret;
getInstance: function() {
* @param {Selector/HTMLElement/Node} node The node to append the Editor to
show: function() {
hide: function() {
getContent: function() {
return html;
NORMALIZE_FONTSIZE: function(n) {
switch (size) {
return size;
* @description Converts an RGB color string to a hex color, example: rgb(0, 255, 0) converts to #00ff00
var exp = new RegExp("(.*?)rgb\\s*?\\(\\s*?([0-9]+).*?,\\s*?([0-9]+).*?,\\s*?([0-9]+).*?\\)(.*?)", "gi");
return css;
TAG2CMD: {
NC_KEYS: {
STRINGS: {
ATTRS: {
content: {
getter: function() {
dir: {
writeOnce: true,
linkedcss: {
if (this.frame) {
return css;
extracss: {
value: false,
if (this.frame) {
return css;
defaultblock: {
* <dt>changedType</dt><dd>The type of change: mousedown, mouseup, right, left, backspace, tab, enter, etc..</dd>
* <dt>commands</dt><dd>The list of execCommands that belong to this change and the dompath that's associated with the changedNode</dd>
* <dt>classNames</dt><dd>An array of classNames that are applied to the changedNode and all of it's parents</dd>
* <dt>dompath</dt><dd>A sorted array of node instances that make up the DOM path from the changedNode to body.</dd>
}, '@VERSION@' ,{requires:['base', 'frame', 'node', 'exec-command', 'selection', 'editor-para'], skinnable:false});
* Handles list manipulation inside the Editor. Adds keyboard manipulation and execCommand support. Adds overrides for the <a href="Plugin.ExecCommand.html#method_COMMANDS.insertorderedlist">insertorderedlist</a> and <a href="Plugin.ExecCommand.html#method_COMMANDS.insertunorderedlist">insertunorderedlist</a> execCommands.
* Handles list manipulation inside the Editor. Adds keyboard manipulation and execCommand support. Adds overrides for the <a href="Plugin.ExecCommand.html#method_COMMANDS.insertorderedlist">insertorderedlist</a> and <a href="Plugin.ExecCommand.html#method_COMMANDS.insertunorderedlist">insertunorderedlist</a> execCommands.
var EditorLists = function() {
* Listener for host's nodeChange event and captures the tabkey interaction only when inside a list node.
_onNodeChange: function(e) {
e.preventDefault();
if (sTab) {
moved = true;
focusEnd = true;
moved = true;
if (moved) {
initializer: function() {
ATTRS: {
host: {
value: false
var EditorBidi = function() {
lastDirection: null,
firstEvent: null,
_checkForChange: function() {
if (node) {
this.lastDirection = null;
_afterNodeChange: function(e) {
this._checkForChange();
this.firstEvent = false;
_afterMouseUp: function(e) {
this._checkForChange();
this.firstEvent = false;
initializer: function() {
this.firstEvent = true;
EVENTS: {
if (!parent) {
return parent;
addParent = true;
addParent = false;
if (addParent) {
return nodeArray;
ATTRS: {
host: {
value: false
removeTextAlign: function(n) {
if (!ns) {
if (!direction) {
b.remove();
selectedBlocks = [];
var d = direction;
return returnValue;
var EditorPara = function() {
_fixFirstPara: function() {
_onNodeChange: function(e) {
switch (e.changedType) {
if (this._lastPara) {
delete this._lastPara;
b.remove();
if (para2) {
para2 = null;
if (prev) {
while (!found) {
if (lc) {
if (lc2) {
found = true;
found = true;
if (lc) {
b.remove();
top = false;
while (!top) {
var start = false;
if (start) {
n.append(c);
if (c === aNode) {
start = true;
node2 = n;
top = true;
if (txt2) {
this._fixFirstPara();
if (br) {
this._fixFirstPara();
p = e.changedNode;
if (p && !p.test(P)) {
p = p.ancestor(P);
if (e.changedNode) {
e.preventDefault();
d = e.changedNode;
d.appendChild(t);
d.removeChild(t);
this._lastPara = p;
_afterEditorReady: function() {
if (inst) {
P = btag;
_afterContentChange: function() {
_afterPaste: function() {
initializer: function() {
ATTRS: {
host: {
value: false
var EditorBR = function() {
_onKeyDown: function(e) {
if (e.stopped) {
e.halt();
if (sel) {
e.halt();
e.halt();
_afterEditorReady: function() {
} catch (bre) {}
* Adds a nodeChange listener only for FF, in the event of a backspace or delete, it creates an empy textNode
* inserts it into the DOM after the e.changedNode, then removes it. Causing FF to redraw the content.
_onNodeChange: function(e) {
switch (e.changedType) {
var d = e.changedNode;
d.appendChild(t);
d.removeChild(t);
initializer: function() {
ATTRS: {
host: {
value: false