2014-10-02 04:36:39 -05:00
|
|
|
/*jshint strict:false, undef:false, unused:false */
|
|
|
|
|
|
|
|
var instances = [];
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
function Squire ( doc ) {
|
|
|
|
var win = doc.defaultView;
|
|
|
|
var body = doc.body;
|
|
|
|
this._win = win;
|
|
|
|
this._doc = doc;
|
|
|
|
this._body = body;
|
|
|
|
|
|
|
|
this._events = {};
|
|
|
|
|
|
|
|
this._sel = win.getSelection();
|
|
|
|
this._lastSelection = null;
|
|
|
|
|
|
|
|
// IE loses selection state of iframe on blur, so make sure we
|
|
|
|
// cache it just before it loses focus.
|
|
|
|
if ( losesSelectionOnBlur ) {
|
|
|
|
this.addEventListener( 'beforedeactivate', this.getSelection );
|
|
|
|
}
|
|
|
|
|
2014-04-16 03:06:10 -05:00
|
|
|
this._hasZWS = false;
|
2013-06-20 08:15:18 -05:00
|
|
|
|
|
|
|
this._lastAnchorNode = null;
|
|
|
|
this._lastFocusNode = null;
|
|
|
|
this._path = '';
|
|
|
|
|
|
|
|
this.addEventListener( 'keyup', this._updatePathOnEvent );
|
|
|
|
this.addEventListener( 'mouseup', this._updatePathOnEvent );
|
|
|
|
|
|
|
|
win.addEventListener( 'focus', this, false );
|
|
|
|
win.addEventListener( 'blur', this, false );
|
|
|
|
|
|
|
|
this._undoIndex = -1;
|
|
|
|
this._undoStack = [];
|
|
|
|
this._undoStackLength = 0;
|
|
|
|
this._isInUndoState = false;
|
|
|
|
|
2014-06-01 19:22:52 -05:00
|
|
|
this.defaultBlockProperties = undefined;
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
this.addEventListener( 'keyup', this._docWasChanged );
|
|
|
|
|
|
|
|
// IE sometimes fires the beforepaste event twice; make sure it is not run
|
|
|
|
// again before our after paste function is called.
|
|
|
|
this._awaitingPaste = false;
|
2013-10-07 23:00:38 -05:00
|
|
|
this.addEventListener( isIE8or9or10 ? 'beforecut' : 'cut', this._onCut );
|
|
|
|
this.addEventListener( isIE8or9or10 ? 'beforepaste' : 'paste', this._onPaste );
|
2013-06-20 08:15:18 -05:00
|
|
|
|
|
|
|
if ( isIE8 ) {
|
|
|
|
this.addEventListener( 'keyup', this._ieSelAllClean );
|
|
|
|
}
|
|
|
|
// Opera does not fire keydown repeatedly.
|
2014-10-02 04:22:51 -05:00
|
|
|
this.addEventListener( isPresto ? 'keypress' : 'keydown', this._onKey );
|
2013-06-20 08:15:18 -05:00
|
|
|
|
|
|
|
// Fix IE8/9's buggy implementation of Text#splitText.
|
|
|
|
// If the split is at the end of the node, it doesn't insert the newly split
|
|
|
|
// node into the document, and sets its value to undefined rather than ''.
|
|
|
|
// And even if the split is not at the end, the original node is removed
|
|
|
|
// from the document and replaced by another, rather than just having its
|
|
|
|
// data shortened.
|
2014-10-02 04:36:39 -05:00
|
|
|
if ( hasBuggySplit( doc ) ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
win.Text.prototype.splitText = function ( offset ) {
|
|
|
|
var afterSplit = this.ownerDocument.createTextNode(
|
|
|
|
this.data.slice( offset ) ),
|
|
|
|
next = this.nextSibling,
|
|
|
|
parent = this.parentNode,
|
|
|
|
toDelete = this.length - offset;
|
|
|
|
if ( next ) {
|
|
|
|
parent.insertBefore( afterSplit, next );
|
|
|
|
} else {
|
|
|
|
parent.appendChild( afterSplit );
|
|
|
|
}
|
|
|
|
if ( toDelete ) {
|
|
|
|
this.deleteData( offset, toDelete );
|
|
|
|
}
|
|
|
|
return afterSplit;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
body.setAttribute( 'contenteditable', 'true' );
|
|
|
|
this.setHTML( '' );
|
2014-05-19 22:55:11 -05:00
|
|
|
|
|
|
|
// Remove Firefox's built-in controls
|
|
|
|
try {
|
|
|
|
doc.execCommand( 'enableObjectResizing', false, 'false' );
|
|
|
|
doc.execCommand( 'enableInlineTableEditing', false, 'false' );
|
|
|
|
} catch ( error ) {}
|
2014-10-02 04:36:39 -05:00
|
|
|
|
|
|
|
instances.push( this );
|
2013-06-20 08:15:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var proto = Squire.prototype;
|
|
|
|
|
|
|
|
proto.createElement = function ( tag, props, children ) {
|
|
|
|
return createElement( this._doc, tag, props, children );
|
|
|
|
};
|
|
|
|
|
2014-06-01 19:22:52 -05:00
|
|
|
proto.createDefaultBlock = function ( children ) {
|
|
|
|
return fixCursor(
|
|
|
|
this.createElement( 'DIV', this.defaultBlockProperties, children )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.didError = function ( error ) {
|
|
|
|
console.log( error );
|
|
|
|
};
|
|
|
|
|
|
|
|
proto.getDocument = function () {
|
|
|
|
return this._doc;
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
// --- Events ---
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// Subscribing to these events won't automatically add a listener to the
|
|
|
|
// document node, since these events are fired in a custom manner by the
|
|
|
|
// editor code.
|
|
|
|
var customEvents = {
|
|
|
|
focus: 1, blur: 1,
|
|
|
|
pathChange: 1, select: 1, input: 1, undoStateChange: 1
|
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.fireEvent = function ( type, event ) {
|
|
|
|
var handlers = this._events[ type ],
|
2013-04-07 22:27:06 -05:00
|
|
|
i, l, obj;
|
|
|
|
if ( handlers ) {
|
|
|
|
if ( !event ) {
|
|
|
|
event = {};
|
|
|
|
}
|
|
|
|
if ( event.type !== type ) {
|
|
|
|
event.type = type;
|
|
|
|
}
|
2013-05-23 23:02:12 -05:00
|
|
|
// Clone handlers array, so any handlers added/removed do not affect it.
|
|
|
|
handlers = handlers.slice();
|
2013-04-07 22:27:06 -05:00
|
|
|
for ( i = 0, l = handlers.length; i < l; i += 1 ) {
|
|
|
|
obj = handlers[i];
|
|
|
|
try {
|
|
|
|
if ( obj.handleEvent ) {
|
|
|
|
obj.handleEvent( event );
|
|
|
|
} else {
|
2013-06-20 08:15:18 -05:00
|
|
|
obj.call( this, event );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
} catch ( error ) {
|
2013-05-19 20:43:08 -05:00
|
|
|
error.details = 'Squire: fireEvent error. Event type: ' + type;
|
2013-06-20 08:15:18 -05:00
|
|
|
this.didError( error );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2014-10-02 00:06:17 -05:00
|
|
|
proto.destroy = function () {
|
|
|
|
var win = this._win,
|
|
|
|
doc = this._doc,
|
|
|
|
events = this._events,
|
|
|
|
type;
|
|
|
|
win.removeEventListener( 'focus', this, false );
|
|
|
|
win.removeEventListener( 'blur', this, false );
|
|
|
|
for ( type in events ) {
|
|
|
|
if ( !customEvents[ type ] ) {
|
2014-11-18 00:02:36 -05:00
|
|
|
doc.removeEventListener( type, this, true );
|
2014-10-02 00:06:17 -05:00
|
|
|
}
|
|
|
|
}
|
2014-10-02 04:36:39 -05:00
|
|
|
var l = instances.length;
|
|
|
|
while ( l-- ) {
|
|
|
|
if ( instances[l] === this ) {
|
|
|
|
instances.splice( l, 1 );
|
|
|
|
}
|
|
|
|
}
|
2014-10-02 00:06:17 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.handleEvent = function ( event ) {
|
|
|
|
this.fireEvent( event.type, event );
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.addEventListener = function ( type, fn ) {
|
|
|
|
var handlers = this._events[ type ];
|
2013-05-19 20:43:08 -05:00
|
|
|
if ( !fn ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.didError({
|
2013-05-19 20:43:08 -05:00
|
|
|
name: 'Squire: addEventListener with null or undefined fn',
|
|
|
|
message: 'Event type: ' + type
|
|
|
|
});
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-05-19 20:43:08 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !handlers ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
handlers = this._events[ type ] = [];
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !customEvents[ type ] ) {
|
2014-11-18 00:02:36 -05:00
|
|
|
this._doc.addEventListener( type, this, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
handlers.push( fn );
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.removeEventListener = function ( type, fn ) {
|
|
|
|
var handlers = this._events[ type ],
|
2013-04-07 22:27:06 -05:00
|
|
|
l;
|
|
|
|
if ( handlers ) {
|
|
|
|
l = handlers.length;
|
|
|
|
while ( l-- ) {
|
|
|
|
if ( handlers[l] === fn ) {
|
|
|
|
handlers.splice( l, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !handlers.length ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
delete this._events[ type ];
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !customEvents[ type ] ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._doc.removeEventListener( type, this, false );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2011-10-28 22:15:21 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// --- Selection and Path ---
|
2011-10-28 22:15:21 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._createRange =
|
|
|
|
function ( range, startOffset, endContainer, endOffset ) {
|
|
|
|
if ( range instanceof this._win.Range ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
return range.cloneRange();
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
var domRange = this._doc.createRange();
|
2013-04-07 22:27:06 -05:00
|
|
|
domRange.setStart( range, startOffset );
|
|
|
|
if ( endContainer ) {
|
|
|
|
domRange.setEnd( endContainer, endOffset );
|
|
|
|
} else {
|
|
|
|
domRange.setEnd( range, startOffset );
|
|
|
|
}
|
|
|
|
return domRange;
|
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setSelection = function ( range ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( range ) {
|
|
|
|
// iOS bug: if you don't focus the iframe before setting the
|
|
|
|
// selection, you can end up in a state where you type but the input
|
|
|
|
// doesn't get directed into the contenteditable area but is instead
|
|
|
|
// lost in a black hole. Very strange.
|
|
|
|
if ( isIOS ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._win.focus();
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
var sel = this._sel;
|
2013-04-07 22:27:06 -05:00
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange( range );
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.getSelection = function () {
|
2014-06-30 09:10:30 -05:00
|
|
|
var sel = this._sel,
|
|
|
|
selection, startContainer, endContainer;
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( sel.rangeCount ) {
|
2014-06-30 09:10:30 -05:00
|
|
|
selection = sel.getRangeAt( 0 ).cloneRange();
|
|
|
|
startContainer = selection.startContainer;
|
|
|
|
endContainer = selection.endContainer;
|
2013-04-07 22:27:06 -05:00
|
|
|
// FF sometimes throws an error reading the isLeaf property. Let's
|
|
|
|
// catch and log it to see if we can find what's going on.
|
|
|
|
try {
|
|
|
|
// FF can return the selection as being inside an <img>. WTF?
|
|
|
|
if ( startContainer && isLeaf( startContainer ) ) {
|
2014-06-30 09:10:30 -05:00
|
|
|
selection.setStartBefore( startContainer );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
if ( endContainer && isLeaf( endContainer ) ) {
|
2014-06-30 09:10:30 -05:00
|
|
|
selection.setEndBefore( endContainer );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
} catch ( error ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.didError({
|
2013-04-07 22:27:06 -05:00
|
|
|
name: 'Squire#getSelection error',
|
|
|
|
message: 'Starts: ' + startContainer.nodeName +
|
|
|
|
'\nEnds: ' + endContainer.nodeName
|
|
|
|
});
|
|
|
|
}
|
2014-06-30 09:10:30 -05:00
|
|
|
this._lastSelection = selection;
|
|
|
|
} else {
|
|
|
|
selection = this._lastSelection;
|
|
|
|
}
|
|
|
|
if ( !selection ) {
|
|
|
|
selection = this._createRange( this._body.firstChild, 0 );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2014-06-30 09:10:30 -05:00
|
|
|
return selection;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.getSelectedText = function () {
|
|
|
|
return getTextContentInRange( this.getSelection() );
|
|
|
|
};
|
|
|
|
|
|
|
|
proto.getPath = function () {
|
|
|
|
return this._path;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// --- Workaround for browsers that can't focus empty text nodes ---
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=15256
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-04-16 03:06:10 -05:00
|
|
|
proto._didAddZWS = function () {
|
|
|
|
this._hasZWS = true;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2014-04-16 03:06:10 -05:00
|
|
|
proto._removeZWS = function () {
|
|
|
|
if ( !this._hasZWS ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var walker = new TreeWalker( this._body, SHOW_TEXT, function () {
|
2014-09-04 05:19:02 -05:00
|
|
|
return true;
|
2014-04-16 03:06:10 -05:00
|
|
|
}, false ),
|
|
|
|
node, index;
|
|
|
|
while ( node = walker.nextNode() ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
while ( ( index = node.data.indexOf( '\u200B' ) ) > -1 ) {
|
|
|
|
node.deleteData( index, 1 );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2014-04-16 03:06:10 -05:00
|
|
|
this._hasZWS = false;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Path change events ---
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._updatePath = function ( range, force ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
var anchor = range.startContainer,
|
|
|
|
focus = range.endContainer,
|
|
|
|
newPath;
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( force || anchor !== this._lastAnchorNode ||
|
|
|
|
focus !== this._lastFocusNode ) {
|
|
|
|
this._lastAnchorNode = anchor;
|
|
|
|
this._lastFocusNode = focus;
|
2013-04-07 22:27:06 -05:00
|
|
|
newPath = ( anchor && focus ) ? ( anchor === focus ) ?
|
|
|
|
getPath( focus ) : '(selection)' : '';
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( this._path !== newPath ) {
|
|
|
|
this._path = newPath;
|
|
|
|
this.fireEvent( 'pathChange', { path: newPath } );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
if ( anchor !== focus ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.fireEvent( 'select' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
2013-06-20 08:15:18 -05:00
|
|
|
|
|
|
|
proto._updatePathOnEvent = function () {
|
|
|
|
this._updatePath( this.getSelection() );
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Focus ---
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.focus = function () {
|
2014-12-07 02:33:43 -05:00
|
|
|
// FF seems to need the body to be focussed (at least on first load).
|
|
|
|
// Chrome also now needs body to be focussed in order to show the cursor
|
|
|
|
// (otherwise it is focussed, but the cursor doesn't appear).
|
|
|
|
this._body.focus();
|
2013-06-20 08:15:18 -05:00
|
|
|
this._win.focus();
|
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.blur = function () {
|
2013-04-07 22:27:06 -05:00
|
|
|
// IE will remove the whole browser window from focus if you call
|
|
|
|
// win.blur() or body.blur(), so instead we call top.focus() to focus
|
|
|
|
// the top frame, thus blurring this frame. This works in everything
|
|
|
|
// except FF, so we need to call body.blur() in that as well.
|
|
|
|
if ( isGecko ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._body.blur();
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
top.focus();
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Bookmarking ---
|
|
|
|
|
|
|
|
var startSelectionId = 'squire-selection-start';
|
|
|
|
var endSelectionId = 'squire-selection-end';
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._saveRangeToBookmark = function ( range ) {
|
|
|
|
var startNode = this.createElement( 'INPUT', {
|
2013-04-07 22:27:06 -05:00
|
|
|
id: startSelectionId,
|
|
|
|
type: 'hidden'
|
|
|
|
}),
|
2013-06-20 08:15:18 -05:00
|
|
|
endNode = this.createElement( 'INPUT', {
|
2013-04-07 22:27:06 -05:00
|
|
|
id: endSelectionId,
|
|
|
|
type: 'hidden'
|
|
|
|
}),
|
|
|
|
temp;
|
|
|
|
|
2013-06-20 06:03:01 -05:00
|
|
|
insertNodeInRange( range, startNode );
|
2013-04-07 22:27:06 -05:00
|
|
|
range.collapse( false );
|
2013-06-20 06:03:01 -05:00
|
|
|
insertNodeInRange( range, endNode );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// In a collapsed range, the start is sometimes inserted after the end!
|
|
|
|
if ( startNode.compareDocumentPosition( endNode ) &
|
|
|
|
DOCUMENT_POSITION_PRECEDING ) {
|
|
|
|
startNode.id = endSelectionId;
|
|
|
|
endNode.id = startSelectionId;
|
|
|
|
temp = startNode;
|
|
|
|
startNode = endNode;
|
|
|
|
endNode = temp;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
range.setStartAfter( startNode );
|
|
|
|
range.setEndBefore( endNode );
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._getRangeAndRemoveBookmark = function ( range ) {
|
|
|
|
var doc = this._doc,
|
|
|
|
start = doc.getElementById( startSelectionId ),
|
2013-04-07 22:27:06 -05:00
|
|
|
end = doc.getElementById( endSelectionId );
|
2012-04-02 00:55:09 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( start && end ) {
|
|
|
|
var startContainer = start.parentNode,
|
|
|
|
endContainer = end.parentNode,
|
|
|
|
collapsed;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var _range = {
|
|
|
|
startContainer: startContainer,
|
|
|
|
endContainer: endContainer,
|
|
|
|
startOffset: indexOf.call( startContainer.childNodes, start ),
|
|
|
|
endOffset: indexOf.call( endContainer.childNodes, end )
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( startContainer === endContainer ) {
|
|
|
|
_range.endOffset -= 1;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
detach( start );
|
|
|
|
detach( end );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Merge any text nodes we split
|
|
|
|
mergeInlines( startContainer, _range );
|
|
|
|
if ( startContainer !== endContainer ) {
|
|
|
|
mergeInlines( endContainer, _range );
|
2011-11-07 21:59:47 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !range ) {
|
|
|
|
range = doc.createRange();
|
2011-11-07 21:59:47 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
range.setStart( _range.startContainer, _range.startOffset );
|
|
|
|
range.setEnd( _range.endContainer, _range.endOffset );
|
|
|
|
collapsed = range.collapsed;
|
|
|
|
|
2013-06-20 06:03:01 -05:00
|
|
|
moveRangeBoundariesDownTree( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( collapsed ) {
|
|
|
|
range.collapse( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return range || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --- Undo ---
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._docWasChanged = function ( event ) {
|
|
|
|
var code = event && event.keyCode;
|
2013-04-07 22:27:06 -05:00
|
|
|
// Presume document was changed if:
|
|
|
|
// 1. A modifier key (other than shift) wasn't held down
|
|
|
|
// 2. The key pressed is not in range 16<=x<=20 (control keys)
|
|
|
|
// 3. The key pressed is not in range 33<=x<=45 (navigation keys)
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( !event || ( !event.ctrlKey && !event.metaKey && !event.altKey &&
|
2013-04-07 22:27:06 -05:00
|
|
|
( code < 16 || code > 20 ) &&
|
2013-06-20 08:15:18 -05:00
|
|
|
( code < 33 || code > 45 ) ) ) {
|
|
|
|
if ( this._isInUndoState ) {
|
|
|
|
this._isInUndoState = false;
|
|
|
|
this.fireEvent( 'undoStateChange', {
|
|
|
|
canUndo: true,
|
|
|
|
canRedo: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.fireEvent( 'input' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Leaves bookmark
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._recordUndoState = function ( range ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Don't record if we're already in an undo state
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( !this._isInUndoState ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Advance pointer to new position
|
2013-06-20 08:15:18 -05:00
|
|
|
var undoIndex = this._undoIndex += 1,
|
|
|
|
undoStack = this._undoStack;
|
2011-10-28 22:15:21 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Truncate stack if longer (i.e. if has been previously undone)
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( undoIndex < this._undoStackLength) {
|
|
|
|
undoStack.length = this._undoStackLength = undoIndex;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// Write out data
|
|
|
|
if ( range ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._saveRangeToBookmark( range );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
undoStack[ undoIndex ] = this._getHTML();
|
|
|
|
this._undoStackLength += 1;
|
|
|
|
this._isInUndoState = true;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.undo = function () {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Sanity check: must not be at beginning of the history stack
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( this._undoIndex !== 0 || !this._isInUndoState ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Make sure any changes since last checkpoint are saved.
|
2013-06-20 08:15:18 -05:00
|
|
|
this._recordUndoState( this.getSelection() );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
this._undoIndex -= 1;
|
|
|
|
this._setHTML( this._undoStack[ this._undoIndex ] );
|
|
|
|
var range = this._getRangeAndRemoveBookmark();
|
2012-04-02 00:55:09 -05:00
|
|
|
if ( range ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.setSelection( range );
|
2012-04-02 00:55:09 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
this._isInUndoState = true;
|
|
|
|
this.fireEvent( 'undoStateChange', {
|
|
|
|
canUndo: this._undoIndex !== 0,
|
2013-04-07 22:27:06 -05:00
|
|
|
canRedo: true
|
|
|
|
});
|
2013-06-20 08:15:18 -05:00
|
|
|
this.fireEvent( 'input' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.redo = function () {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Sanity check: must not be at end of stack and must be in an undo
|
|
|
|
// state.
|
2013-06-20 08:15:18 -05:00
|
|
|
var undoIndex = this._undoIndex,
|
|
|
|
undoStackLength = this._undoStackLength;
|
|
|
|
if ( undoIndex + 1 < undoStackLength && this._isInUndoState ) {
|
|
|
|
this._undoIndex += 1;
|
|
|
|
this._setHTML( this._undoStack[ this._undoIndex ] );
|
|
|
|
var range = this._getRangeAndRemoveBookmark();
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( range ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.setSelection( range );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
this.fireEvent( 'undoStateChange', {
|
2013-04-07 22:27:06 -05:00
|
|
|
canUndo: true,
|
2013-06-20 08:15:18 -05:00
|
|
|
canRedo: undoIndex + 2 < undoStackLength
|
2013-04-07 22:27:06 -05:00
|
|
|
});
|
2013-06-20 08:15:18 -05:00
|
|
|
this.fireEvent( 'input' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Inline formatting ---
|
|
|
|
|
|
|
|
// Looks for matching tag and attributes, so won't work
|
|
|
|
// if <strong> instead of <b> etc.
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.hasFormat = function ( tag, attributes, range ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// 1. Normalise the arguments and get selection
|
|
|
|
tag = tag.toUpperCase();
|
|
|
|
if ( !attributes ) { attributes = {}; }
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( !range && !( range = this.getSelection() ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
return false;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// If the common ancestor is inside the tag we require, we definitely
|
|
|
|
// have the format.
|
|
|
|
var root = range.commonAncestorContainer,
|
|
|
|
walker, node;
|
|
|
|
if ( getNearest( root, tag, attributes ) ) {
|
|
|
|
return true;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// If common ancestor is a text node and doesn't have the format, we
|
|
|
|
// definitely don't have it.
|
|
|
|
if ( root.nodeType === TEXT_NODE ) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-02 00:53:24 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Otherwise, check each text node at least partially contained within
|
|
|
|
// the selection and make sure all of them have the format we want.
|
|
|
|
walker = new TreeWalker( root, SHOW_TEXT, function ( node ) {
|
2014-09-04 05:19:02 -05:00
|
|
|
return isNodeContainedInRange( range, node, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
}, false );
|
2012-04-02 00:53:24 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var seenNode = false;
|
|
|
|
while ( node = walker.nextNode() ) {
|
|
|
|
if ( !getNearest( node, tag, attributes ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seenNode = true;
|
|
|
|
}
|
2012-04-10 00:26:17 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
return seenNode;
|
|
|
|
};
|
2012-04-10 00:26:17 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._addFormat = function ( tag, attributes, range ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// If the range is collapsed we simply insert the node by wrapping
|
|
|
|
// it round the range and focus it.
|
|
|
|
var el, walker, startContainer, endContainer, startOffset, endOffset,
|
2014-10-02 04:00:35 -05:00
|
|
|
textNode, needsFormat;
|
2012-04-10 00:26:17 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( range.collapsed ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
el = fixCursor( this.createElement( tag, attributes ) );
|
2013-06-20 06:03:01 -05:00
|
|
|
insertNodeInRange( range, el );
|
2013-04-07 22:27:06 -05:00
|
|
|
range.setStart( el.firstChild, el.firstChild.length );
|
|
|
|
range.collapse( true );
|
|
|
|
}
|
|
|
|
// Otherwise we find all the textnodes in the range (splitting
|
|
|
|
// partially selected nodes) and if they're not already formatted
|
|
|
|
// correctly we wrap them in the appropriate tag.
|
|
|
|
else {
|
|
|
|
// We don't want to apply formatting twice so we check each text
|
|
|
|
// node to see if it has an ancestor with the formatting already.
|
|
|
|
// Create an iterator to walk over all the text nodes under this
|
|
|
|
// ancestor which are in the range and not already formatted
|
|
|
|
// correctly.
|
|
|
|
walker = new TreeWalker(
|
|
|
|
range.commonAncestorContainer,
|
|
|
|
SHOW_TEXT,
|
|
|
|
function ( node ) {
|
2014-09-04 05:19:02 -05:00
|
|
|
return isNodeContainedInRange( range, node, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2012-04-10 00:26:17 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Start at the beginning node of the range and iterate through
|
|
|
|
// all the nodes in the range that need formatting.
|
2014-10-02 04:00:35 -05:00
|
|
|
startContainer = range.startContainer;
|
|
|
|
startOffset = range.startOffset;
|
|
|
|
endContainer = range.endContainer;
|
|
|
|
endOffset = range.endOffset;
|
|
|
|
|
|
|
|
// Make sure we start inside a text node.
|
|
|
|
walker.currentNode = startContainer;
|
|
|
|
if ( startContainer.nodeType !== TEXT_NODE ) {
|
|
|
|
startContainer = walker.nextNode();
|
|
|
|
startOffset = 0;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-04-10 00:26:17 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
do {
|
2014-10-02 04:00:35 -05:00
|
|
|
textNode = walker.currentNode;
|
|
|
|
needsFormat = !getNearest( textNode, tag, attributes );
|
|
|
|
if ( needsFormat ) {
|
|
|
|
if ( textNode === endContainer &&
|
|
|
|
textNode.length > endOffset ) {
|
|
|
|
textNode.splitText( endOffset );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2014-10-02 04:00:35 -05:00
|
|
|
if ( textNode === startContainer && startOffset ) {
|
|
|
|
textNode = textNode.splitText( startOffset );
|
2014-10-03 04:38:20 -05:00
|
|
|
if ( endContainer === startContainer ) {
|
|
|
|
endContainer = textNode;
|
|
|
|
endOffset -= startOffset;
|
|
|
|
}
|
2014-10-02 04:00:35 -05:00
|
|
|
startContainer = textNode;
|
|
|
|
startOffset = 0;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
el = this.createElement( tag, attributes );
|
2014-10-02 04:00:35 -05:00
|
|
|
replaceWith( textNode, el );
|
|
|
|
el.appendChild( textNode );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2014-10-02 04:00:35 -05:00
|
|
|
} while ( walker.nextNode() );
|
|
|
|
|
|
|
|
// Make sure we finish inside a text node. Otherwise offset may have
|
|
|
|
// changed.
|
|
|
|
if ( endContainer.nodeType !== TEXT_NODE ) {
|
|
|
|
endContainer = textNode;
|
|
|
|
endOffset = textNode.length;
|
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// Now set the selection to as it was before
|
2013-06-20 08:15:18 -05:00
|
|
|
range = this._createRange(
|
2013-04-07 22:27:06 -05:00
|
|
|
startContainer, startOffset, endContainer, endOffset );
|
|
|
|
}
|
|
|
|
return range;
|
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._removeFormat = function ( tag, attributes, range, partial ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Add bookmark
|
2013-06-20 08:15:18 -05:00
|
|
|
this._saveRangeToBookmark( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// We need a node in the selection to break the surrounding
|
|
|
|
// formatted text.
|
2013-06-20 08:15:18 -05:00
|
|
|
var doc = this._doc,
|
|
|
|
fixer;
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( range.collapsed ) {
|
|
|
|
if ( cantFocusEmptyTextNodes ) {
|
|
|
|
fixer = doc.createTextNode( '\u200B' );
|
2014-04-16 03:06:10 -05:00
|
|
|
this._didAddZWS();
|
2013-04-07 22:27:06 -05:00
|
|
|
} else {
|
|
|
|
fixer = doc.createTextNode( '' );
|
2011-11-07 21:59:47 -05:00
|
|
|
}
|
2013-06-20 06:03:01 -05:00
|
|
|
insertNodeInRange( range, fixer );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Find block-level ancestor of selection
|
|
|
|
var root = range.commonAncestorContainer;
|
|
|
|
while ( isInline( root ) ) {
|
|
|
|
root = root.parentNode;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Find text nodes inside formatTags that are not in selection and
|
|
|
|
// add an extra tag with the same formatting.
|
|
|
|
var startContainer = range.startContainer,
|
|
|
|
startOffset = range.startOffset,
|
|
|
|
endContainer = range.endContainer,
|
|
|
|
endOffset = range.endOffset,
|
|
|
|
toWrap = [],
|
|
|
|
examineNode = function ( node, exemplar ) {
|
|
|
|
// If the node is completely contained by the range then
|
|
|
|
// we're going to remove all formatting so ignore it.
|
2013-06-20 06:03:01 -05:00
|
|
|
if ( isNodeContainedInRange( range, node, false ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var isText = ( node.nodeType === TEXT_NODE ),
|
|
|
|
child, next;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// If not at least partially contained, wrap entire contents
|
|
|
|
// in a clone of the tag we're removing and we're done.
|
2013-06-20 06:03:01 -05:00
|
|
|
if ( !isNodeContainedInRange( range, node, true ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Ignore bookmarks and empty text nodes
|
|
|
|
if ( node.nodeName !== 'INPUT' &&
|
|
|
|
( !isText || node.data ) ) {
|
|
|
|
toWrap.push([ exemplar, node ]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Split any partially selected text nodes.
|
|
|
|
if ( isText ) {
|
|
|
|
if ( node === endContainer && endOffset !== node.length ) {
|
|
|
|
toWrap.push([ exemplar, node.splitText( endOffset ) ]);
|
|
|
|
}
|
|
|
|
if ( node === startContainer && startOffset ) {
|
|
|
|
node.splitText( startOffset );
|
|
|
|
toWrap.push([ exemplar, node ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If not a text node, recurse onto all children.
|
|
|
|
// Beware, the tree may be rewritten with each call
|
|
|
|
// to examineNode, hence find the next sibling first.
|
|
|
|
else {
|
|
|
|
for ( child = node.firstChild; child; child = next ) {
|
|
|
|
next = child.nextSibling;
|
|
|
|
examineNode( child, exemplar );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
formatTags = Array.prototype.filter.call(
|
|
|
|
root.getElementsByTagName( tag ), function ( el ) {
|
2013-06-20 06:03:01 -05:00
|
|
|
return isNodeContainedInRange( range, el, true ) &&
|
2013-04-07 22:27:06 -05:00
|
|
|
hasTagAttributes( el, tag, attributes );
|
|
|
|
}
|
|
|
|
);
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !partial ) {
|
|
|
|
formatTags.forEach( function ( node ) {
|
|
|
|
examineNode( node, node );
|
|
|
|
});
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Now wrap unselected nodes in the tag
|
|
|
|
toWrap.forEach( function ( item ) {
|
|
|
|
// [ exemplar, node ] tuple
|
|
|
|
var el = item[0].cloneNode( false ),
|
|
|
|
node = item[1];
|
|
|
|
replaceWith( node, el );
|
|
|
|
el.appendChild( node );
|
|
|
|
});
|
|
|
|
// and remove old formatting tags.
|
|
|
|
formatTags.forEach( function ( el ) {
|
|
|
|
replaceWith( el, empty( el ) );
|
|
|
|
});
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Merge adjacent inlines:
|
2013-06-20 08:15:18 -05:00
|
|
|
this._getRangeAndRemoveBookmark( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( fixer ) {
|
|
|
|
range.collapse( false );
|
|
|
|
}
|
|
|
|
var _range = {
|
|
|
|
startContainer: range.startContainer,
|
|
|
|
startOffset: range.startOffset,
|
|
|
|
endContainer: range.endContainer,
|
|
|
|
endOffset: range.endOffset
|
2011-10-28 22:15:21 -05:00
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
mergeInlines( root, _range );
|
|
|
|
range.setStart( _range.startContainer, _range.startOffset );
|
|
|
|
range.setEnd( _range.endContainer, _range.endOffset );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
return range;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.changeFormat = function ( add, remove, range, partial ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Normalise the arguments and get selection
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( !range && !( range = this.getSelection() ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-08-08 01:20:28 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Save undo checkpoint
|
2013-06-20 08:15:18 -05:00
|
|
|
this._recordUndoState( range );
|
|
|
|
this._getRangeAndRemoveBookmark( range );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( remove ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
range = this._removeFormat( remove.tag.toUpperCase(),
|
2013-04-07 22:27:06 -05:00
|
|
|
remove.attributes || {}, range, partial );
|
|
|
|
}
|
|
|
|
if ( add ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
range = this._addFormat( add.tag.toUpperCase(),
|
2013-04-07 22:27:06 -05:00
|
|
|
add.attributes || {}, range );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
this.setSelection( range );
|
|
|
|
this._updatePath( range, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// We're not still in an undo state
|
2013-06-20 08:15:18 -05:00
|
|
|
this._docWasChanged();
|
|
|
|
|
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Block formatting ---
|
|
|
|
|
|
|
|
var tagAfterSplit = {
|
|
|
|
DIV: 'DIV',
|
|
|
|
PRE: 'DIV',
|
|
|
|
H1: 'DIV',
|
|
|
|
H2: 'DIV',
|
|
|
|
H3: 'DIV',
|
|
|
|
H4: 'DIV',
|
|
|
|
H5: 'DIV',
|
|
|
|
H6: 'DIV',
|
|
|
|
P: 'DIV',
|
|
|
|
DT: 'DD',
|
|
|
|
DD: 'DT',
|
|
|
|
LI: 'LI'
|
|
|
|
};
|
|
|
|
|
|
|
|
var splitBlock = function ( block, node, offset ) {
|
|
|
|
var splitTag = tagAfterSplit[ block.nodeName ],
|
|
|
|
nodeAfterSplit = split( node, offset, block.parentNode );
|
|
|
|
|
|
|
|
// Make sure the new node is the correct type.
|
|
|
|
if ( nodeAfterSplit.nodeName !== splitTag ) {
|
2013-07-17 01:13:29 -05:00
|
|
|
block = createElement( nodeAfterSplit.ownerDocument, splitTag );
|
2013-04-07 22:27:06 -05:00
|
|
|
block.className = nodeAfterSplit.dir === 'rtl' ? 'dir-rtl' : '';
|
|
|
|
block.dir = nodeAfterSplit.dir;
|
|
|
|
replaceWith( nodeAfterSplit, block );
|
|
|
|
block.appendChild( empty( nodeAfterSplit ) );
|
|
|
|
nodeAfterSplit = block;
|
|
|
|
}
|
|
|
|
return nodeAfterSplit;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.forEachBlock = function ( fn, mutates, range ) {
|
|
|
|
if ( !range && !( range = this.getSelection() ) ) {
|
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Save undo checkpoint
|
|
|
|
if ( mutates ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._recordUndoState( range );
|
|
|
|
this._getRangeAndRemoveBookmark( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 06:03:01 -05:00
|
|
|
var start = getStartBlockOfRange( range ),
|
|
|
|
end = getEndBlockOfRange( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( start && end ) {
|
|
|
|
do {
|
|
|
|
if ( fn( start ) || start === end ) { break; }
|
|
|
|
} while ( start = getNextBlock( start ) );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( mutates ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.setSelection( range );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Path may have changed
|
2013-06-20 08:15:18 -05:00
|
|
|
this._updatePath( range, true );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// We're not still in an undo state
|
2013-06-20 08:15:18 -05:00
|
|
|
this._docWasChanged();
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.modifyBlocks = function ( modify, range ) {
|
|
|
|
if ( !range && !( range = this.getSelection() ) ) {
|
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 1. Save undo checkpoint and bookmark selection
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( this._isInUndoState ) {
|
|
|
|
this._saveRangeToBookmark( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
} else {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._recordUndoState( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 2. Expand range to block boundaries
|
2013-06-20 06:03:01 -05:00
|
|
|
expandRangeToBlockBoundaries( range );
|
2012-07-01 21:41:38 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 3. Remove range.
|
|
|
|
var body = this._body,
|
|
|
|
frag;
|
2013-06-20 06:03:01 -05:00
|
|
|
moveRangeBoundariesUpTree( range, body );
|
2014-06-26 10:13:54 -05:00
|
|
|
frag = extractContentsOfRange( range, body );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 4. Modify tree of fragment and reinsert.
|
2013-06-20 08:15:18 -05:00
|
|
|
insertNodeInRange( range, modify.call( this, frag ) );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 5. Merge containers at edges
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( range.endOffset < range.endContainer.childNodes.length ) {
|
|
|
|
mergeContainers( range.endContainer.childNodes[ range.endOffset ] );
|
|
|
|
}
|
|
|
|
mergeContainers( range.startContainer.childNodes[ range.startOffset ] );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 6. Restore selection
|
2013-06-20 08:15:18 -05:00
|
|
|
this._getRangeAndRemoveBookmark( range );
|
|
|
|
this.setSelection( range );
|
|
|
|
this._updatePath( range, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2014-06-26 10:13:54 -05:00
|
|
|
// 7. We're not still in an undo state
|
2013-06-20 08:15:18 -05:00
|
|
|
this._docWasChanged();
|
|
|
|
|
|
|
|
return this;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var increaseBlockQuoteLevel = function ( frag ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
return this.createElement( 'BLOCKQUOTE', [
|
2013-04-07 22:27:06 -05:00
|
|
|
frag
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
var decreaseBlockQuoteLevel = function ( frag ) {
|
|
|
|
var blockquotes = frag.querySelectorAll( 'blockquote' );
|
|
|
|
Array.prototype.filter.call( blockquotes, function ( el ) {
|
|
|
|
return !getNearest( el.parentNode, 'BLOCKQUOTE' );
|
|
|
|
}).forEach( function ( el ) {
|
|
|
|
replaceWith( el, empty( el ) );
|
|
|
|
});
|
|
|
|
return frag;
|
|
|
|
};
|
|
|
|
|
2014-05-19 22:28:22 -05:00
|
|
|
var removeBlockQuote = function (/* frag */) {
|
2014-06-01 19:22:52 -05:00
|
|
|
return this.createDefaultBlock([
|
2014-05-19 22:28:22 -05:00
|
|
|
this.createElement( 'INPUT', {
|
|
|
|
id: startSelectionId,
|
|
|
|
type: 'hidden'
|
|
|
|
}),
|
|
|
|
this.createElement( 'INPUT', {
|
|
|
|
id: endSelectionId,
|
|
|
|
type: 'hidden'
|
|
|
|
})
|
2014-06-01 19:22:52 -05:00
|
|
|
]);
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2014-04-06 22:05:44 -05:00
|
|
|
var makeList = function ( self, frag, type ) {
|
|
|
|
var walker = getBlockWalker( frag ),
|
|
|
|
node, tag, prev, newLi;
|
|
|
|
|
|
|
|
while ( node = walker.nextNode() ) {
|
|
|
|
tag = node.parentNode.nodeName;
|
|
|
|
if ( tag !== 'LI' ) {
|
|
|
|
newLi = self.createElement( 'LI', {
|
|
|
|
'class': node.dir === 'rtl' ? 'dir-rtl' : undefined,
|
|
|
|
dir: node.dir || undefined
|
|
|
|
});
|
|
|
|
// Have we replaced the previous block with a new <ul>/<ol>?
|
|
|
|
if ( ( prev = node.previousSibling ) &&
|
|
|
|
prev.nodeName === type ) {
|
|
|
|
prev.appendChild( newLi );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2014-04-06 22:05:44 -05:00
|
|
|
// Otherwise, replace this block with the <ul>/<ol>
|
|
|
|
else {
|
|
|
|
replaceWith(
|
|
|
|
node,
|
|
|
|
self.createElement( type, [
|
|
|
|
newLi
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
newLi.appendChild( node );
|
|
|
|
} else {
|
|
|
|
node = node.parentNode.parentNode;
|
|
|
|
tag = node.nodeName;
|
|
|
|
if ( tag !== type && ( /^[OU]L$/.test( tag ) ) ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
replaceWith( node,
|
|
|
|
self.createElement( type, [ empty( node ) ] )
|
|
|
|
);
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var makeUnorderedList = function ( frag ) {
|
2014-04-06 22:05:44 -05:00
|
|
|
makeList( this, frag, 'UL' );
|
2013-04-07 22:27:06 -05:00
|
|
|
return frag;
|
|
|
|
};
|
|
|
|
|
|
|
|
var makeOrderedList = function ( frag ) {
|
2014-04-06 22:05:44 -05:00
|
|
|
makeList( this, frag, 'OL' );
|
|
|
|
return frag;
|
|
|
|
};
|
|
|
|
|
|
|
|
var removeList = function ( frag ) {
|
|
|
|
var lists = frag.querySelectorAll( 'UL, OL' ),
|
|
|
|
i, l, ll, list, listFrag, children, child;
|
|
|
|
for ( i = 0, l = lists.length; i < l; i += 1 ) {
|
|
|
|
list = lists[i];
|
|
|
|
listFrag = empty( list );
|
|
|
|
children = listFrag.childNodes;
|
|
|
|
ll = children.length;
|
|
|
|
while ( ll-- ) {
|
|
|
|
child = children[ll];
|
|
|
|
replaceWith( child, empty( child ) );
|
|
|
|
}
|
2014-05-26 20:31:06 -05:00
|
|
|
fixContainer( listFrag );
|
2014-04-06 22:05:44 -05:00
|
|
|
replaceWith( list, listFrag );
|
|
|
|
}
|
|
|
|
return frag;
|
|
|
|
};
|
|
|
|
|
|
|
|
var increaseListLevel = function ( frag ) {
|
|
|
|
var items = frag.querySelectorAll( 'LI' ),
|
|
|
|
i, l, item,
|
|
|
|
type, newParent;
|
|
|
|
for ( i = 0, l = items.length; i < l; i += 1 ) {
|
|
|
|
item = items[i];
|
|
|
|
if ( !isContainer( item.firstChild ) ) {
|
|
|
|
// type => 'UL' or 'OL'
|
|
|
|
type = item.parentNode.nodeName;
|
|
|
|
newParent = item.previousSibling;
|
|
|
|
if ( !newParent || !( newParent = newParent.lastChild ) ||
|
|
|
|
newParent.nodeName !== type ) {
|
|
|
|
replaceWith(
|
|
|
|
item,
|
|
|
|
this.createElement( 'LI', [
|
|
|
|
newParent = this.createElement( type )
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
newParent.appendChild( item );
|
|
|
|
}
|
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
return frag;
|
|
|
|
};
|
|
|
|
|
|
|
|
var decreaseListLevel = function ( frag ) {
|
2014-04-06 22:05:44 -05:00
|
|
|
var items = frag.querySelectorAll( 'LI' );
|
|
|
|
Array.prototype.filter.call( items, function ( el ) {
|
|
|
|
return !isContainer( el.firstChild );
|
|
|
|
}).forEach( function ( item ) {
|
|
|
|
var parent = item.parentNode,
|
|
|
|
newParent = parent.parentNode,
|
|
|
|
first = item.firstChild,
|
|
|
|
node = first,
|
|
|
|
next;
|
|
|
|
if ( item.previousSibling ) {
|
|
|
|
parent = split( parent, item, newParent );
|
|
|
|
}
|
|
|
|
while ( node ) {
|
|
|
|
next = node.nextSibling;
|
|
|
|
if ( isContainer( node ) ) {
|
|
|
|
break;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2014-04-06 22:05:44 -05:00
|
|
|
newParent.insertBefore( node, parent );
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
if ( newParent.nodeName === 'LI' && first.previousSibling ) {
|
|
|
|
split( newParent, first, newParent.parentNode );
|
|
|
|
}
|
|
|
|
while ( item !== frag && !item.childNodes.length ) {
|
|
|
|
parent = item.parentNode;
|
|
|
|
parent.removeChild( item );
|
|
|
|
item = parent;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
}, this );
|
2014-05-26 20:31:06 -05:00
|
|
|
fixContainer( frag );
|
2013-04-07 22:27:06 -05:00
|
|
|
return frag;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// --- Clean ---
|
|
|
|
|
2014-07-03 08:16:20 -05:00
|
|
|
var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i;
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
var addLinks = function ( frag ) {
|
|
|
|
var doc = frag.ownerDocument,
|
|
|
|
walker = new TreeWalker( frag, SHOW_TEXT,
|
|
|
|
function ( node ) {
|
2014-09-04 05:19:02 -05:00
|
|
|
return !getNearest( node, 'A' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}, false ),
|
2014-07-03 08:16:20 -05:00
|
|
|
node, data, parent, match, index, endIndex, child;
|
2013-04-07 22:27:06 -05:00
|
|
|
while ( node = walker.nextNode() ) {
|
2014-07-03 08:16:20 -05:00
|
|
|
data = node.data;
|
|
|
|
parent = node.parentNode;
|
|
|
|
while ( match = linkRegExp.exec( data ) ) {
|
|
|
|
index = match.index;
|
|
|
|
endIndex = index + match[0].length;
|
|
|
|
if ( index ) {
|
|
|
|
child = doc.createTextNode( data.slice( 0, index ) );
|
|
|
|
parent.insertBefore( child, node );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2014-07-03 08:16:20 -05:00
|
|
|
child = doc.createElement( 'A' );
|
|
|
|
child.textContent = data.slice( index, endIndex );
|
|
|
|
child.href = match[1] ?
|
|
|
|
/^(?:ht|f)tps?:/.test( match[1] ) ?
|
|
|
|
match[1] :
|
|
|
|
'http://' + match[1] :
|
|
|
|
'mailto:' + match[2];
|
|
|
|
parent.insertBefore( child, node );
|
|
|
|
node.data = data = data.slice( endIndex );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var allowedBlock = /^(?:A(?:DDRESS|RTICLE|SIDE)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL)$/;
|
|
|
|
|
|
|
|
var fontSizes = {
|
|
|
|
1: 10,
|
|
|
|
2: 13,
|
|
|
|
3: 16,
|
|
|
|
4: 18,
|
|
|
|
5: 24,
|
|
|
|
6: 32,
|
|
|
|
7: 48
|
|
|
|
};
|
|
|
|
|
|
|
|
var spanToSemantic = {
|
|
|
|
backgroundColor: {
|
|
|
|
regexp: notWS,
|
2013-07-16 20:13:49 -05:00
|
|
|
replace: function ( doc, colour ) {
|
|
|
|
return createElement( doc, 'SPAN', {
|
2013-04-07 22:27:06 -05:00
|
|
|
'class': 'highlight',
|
|
|
|
style: 'background-color: ' + colour
|
2011-10-28 22:15:21 -05:00
|
|
|
});
|
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
},
|
|
|
|
color: {
|
|
|
|
regexp: notWS,
|
2013-07-16 20:13:49 -05:00
|
|
|
replace: function ( doc, colour ) {
|
|
|
|
return createElement( doc, 'SPAN', {
|
2013-04-07 22:27:06 -05:00
|
|
|
'class': 'colour',
|
|
|
|
style: 'color:' + colour
|
|
|
|
});
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
},
|
|
|
|
fontWeight: {
|
|
|
|
regexp: /^bold/i,
|
2013-07-16 20:13:49 -05:00
|
|
|
replace: function ( doc ) {
|
|
|
|
return createElement( doc, 'B' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
fontStyle: {
|
|
|
|
regexp: /^italic/i,
|
2013-07-16 20:13:49 -05:00
|
|
|
replace: function ( doc ) {
|
|
|
|
return createElement( doc, 'I' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
fontFamily: {
|
|
|
|
regexp: notWS,
|
2013-07-16 20:13:49 -05:00
|
|
|
replace: function ( doc, family ) {
|
|
|
|
return createElement( doc, 'SPAN', {
|
2013-04-07 22:27:06 -05:00
|
|
|
'class': 'font',
|
|
|
|
style: 'font-family:' + family
|
|
|
|
});
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
},
|
|
|
|
fontSize: {
|
|
|
|
regexp: notWS,
|
2013-07-16 20:13:49 -05:00
|
|
|
replace: function ( doc, size ) {
|
|
|
|
return createElement( doc, 'SPAN', {
|
2013-04-07 22:27:06 -05:00
|
|
|
'class': 'size',
|
|
|
|
style: 'font-size:' + size
|
|
|
|
});
|
2011-11-02 20:44:54 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-03 01:57:20 -05:00
|
|
|
var replaceWithTag = function ( tag ) {
|
|
|
|
return function ( node, parent ) {
|
|
|
|
var el = createElement( node.ownerDocument, tag );
|
|
|
|
parent.replaceChild( el, node );
|
|
|
|
el.appendChild( empty( node ) );
|
|
|
|
return el;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var stylesRewriters = {
|
|
|
|
SPAN: function ( span, parent ) {
|
|
|
|
var style = span.style,
|
2013-07-16 20:13:49 -05:00
|
|
|
doc = span.ownerDocument,
|
2013-04-07 22:27:06 -05:00
|
|
|
attr, converter, css, newTreeBottom, newTreeTop, el;
|
|
|
|
|
|
|
|
for ( attr in spanToSemantic ) {
|
|
|
|
converter = spanToSemantic[ attr ];
|
|
|
|
css = style[ attr ];
|
|
|
|
if ( css && converter.regexp.test( css ) ) {
|
2013-07-16 20:13:49 -05:00
|
|
|
el = converter.replace( doc, css );
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( newTreeBottom ) {
|
|
|
|
newTreeBottom.appendChild( el );
|
|
|
|
}
|
|
|
|
newTreeBottom = el;
|
|
|
|
if ( !newTreeTop ) {
|
|
|
|
newTreeTop = el;
|
|
|
|
}
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( newTreeTop ) {
|
|
|
|
newTreeBottom.appendChild( empty( span ) );
|
|
|
|
parent.replaceChild( newTreeTop, span );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
return newTreeBottom || span;
|
|
|
|
},
|
2014-02-03 01:57:20 -05:00
|
|
|
STRONG: replaceWithTag( 'B' ),
|
|
|
|
EM: replaceWithTag( 'I' ),
|
2014-02-03 02:19:13 -05:00
|
|
|
STRIKE: replaceWithTag( 'S' ),
|
2013-04-07 22:27:06 -05:00
|
|
|
FONT: function ( node, parent ) {
|
|
|
|
var face = node.face,
|
|
|
|
size = node.size,
|
2014-01-28 01:37:31 -05:00
|
|
|
colour = node.color,
|
2013-07-16 20:13:49 -05:00
|
|
|
doc = node.ownerDocument,
|
2014-01-28 01:37:31 -05:00
|
|
|
fontSpan, sizeSpan, colourSpan,
|
2013-04-07 22:27:06 -05:00
|
|
|
newTreeBottom, newTreeTop;
|
|
|
|
if ( face ) {
|
2013-07-16 20:13:49 -05:00
|
|
|
fontSpan = createElement( doc, 'SPAN', {
|
2011-11-15 05:46:47 -05:00
|
|
|
'class': 'font',
|
2013-04-07 22:27:06 -05:00
|
|
|
style: 'font-family:' + face
|
2011-11-15 05:46:47 -05:00
|
|
|
});
|
2014-01-28 01:37:31 -05:00
|
|
|
newTreeTop = fontSpan;
|
|
|
|
newTreeBottom = fontSpan;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( size ) {
|
2013-07-16 20:13:49 -05:00
|
|
|
sizeSpan = createElement( doc, 'SPAN', {
|
2013-04-07 22:27:06 -05:00
|
|
|
'class': 'size',
|
|
|
|
style: 'font-size:' + fontSizes[ size ] + 'px'
|
|
|
|
});
|
2014-01-28 01:37:31 -05:00
|
|
|
if ( !newTreeTop ) {
|
|
|
|
newTreeTop = sizeSpan;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2014-01-28 01:37:31 -05:00
|
|
|
if ( newTreeBottom ) {
|
|
|
|
newTreeBottom.appendChild( sizeSpan );
|
|
|
|
}
|
|
|
|
newTreeBottom = sizeSpan;
|
|
|
|
}
|
|
|
|
if ( colour && /^#?([\dA-F]{3}){1,2}$/i.test( colour ) ) {
|
|
|
|
if ( colour.charAt( 0 ) !== '#' ) {
|
|
|
|
colour = '#' + colour;
|
|
|
|
}
|
|
|
|
colourSpan = createElement( doc, 'SPAN', {
|
|
|
|
'class': 'colour',
|
|
|
|
style: 'color:' + colour
|
|
|
|
});
|
|
|
|
if ( !newTreeTop ) {
|
|
|
|
newTreeTop = colourSpan;
|
|
|
|
}
|
|
|
|
if ( newTreeBottom ) {
|
|
|
|
newTreeBottom.appendChild( colourSpan );
|
|
|
|
}
|
|
|
|
newTreeBottom = colourSpan;
|
|
|
|
}
|
|
|
|
if ( !newTreeTop ) {
|
|
|
|
newTreeTop = newTreeBottom = createElement( doc, 'SPAN' );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
parent.replaceChild( newTreeTop, node );
|
|
|
|
newTreeBottom.appendChild( empty( node ) );
|
|
|
|
return newTreeBottom;
|
|
|
|
},
|
|
|
|
TT: function ( node, parent ) {
|
2013-07-16 20:13:49 -05:00
|
|
|
var el = createElement( node.ownerDocument, 'SPAN', {
|
2013-04-07 22:27:06 -05:00
|
|
|
'class': 'font',
|
|
|
|
style: 'font-family:menlo,consolas,"courier new",monospace'
|
|
|
|
});
|
|
|
|
parent.replaceChild( el, node );
|
|
|
|
el.appendChild( empty( node ) );
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var removeEmptyInlines = function ( root ) {
|
|
|
|
var children = root.childNodes,
|
|
|
|
l = children.length,
|
|
|
|
child;
|
|
|
|
while ( l-- ) {
|
|
|
|
child = children[l];
|
2013-05-17 02:24:00 -05:00
|
|
|
if ( child.nodeType === ELEMENT_NODE && child.nodeName !== 'IMG' ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
removeEmptyInlines( child );
|
|
|
|
if ( isInline( child ) && !child.firstChild ) {
|
|
|
|
root.removeChild( child );
|
2013-02-21 23:06:56 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Two purposes:
|
|
|
|
|
|
|
|
1. Remove nodes we don't want, such as weird <o:p> tags, comment nodes
|
|
|
|
and whitespace nodes.
|
|
|
|
2. Convert inline tags into our preferred format.
|
|
|
|
*/
|
|
|
|
var cleanTree = function ( node, allowStyles ) {
|
|
|
|
var children = node.childNodes,
|
2014-04-24 19:59:30 -05:00
|
|
|
i, l, child, nodeName, nodeType, rewriter, childLength,
|
|
|
|
data, j, ll;
|
2013-04-07 22:27:06 -05:00
|
|
|
for ( i = 0, l = children.length; i < l; i += 1 ) {
|
|
|
|
child = children[i];
|
|
|
|
nodeName = child.nodeName;
|
|
|
|
nodeType = child.nodeType;
|
|
|
|
rewriter = stylesRewriters[ nodeName ];
|
|
|
|
if ( nodeType === ELEMENT_NODE ) {
|
|
|
|
childLength = child.childNodes.length;
|
|
|
|
if ( rewriter ) {
|
|
|
|
child = rewriter( child, node );
|
|
|
|
} else if ( !allowedBlock.test( nodeName ) &&
|
|
|
|
!isInline( child ) ) {
|
2013-02-20 19:37:20 -05:00
|
|
|
i -= 1;
|
2013-04-07 22:27:06 -05:00
|
|
|
l += childLength - 1;
|
|
|
|
node.replaceChild( empty( child ), child );
|
|
|
|
continue;
|
|
|
|
} else if ( !allowStyles && child.style.cssText ) {
|
|
|
|
child.removeAttribute( 'style' );
|
|
|
|
}
|
|
|
|
if ( childLength ) {
|
|
|
|
cleanTree( child, allowStyles );
|
|
|
|
}
|
2014-04-24 19:59:30 -05:00
|
|
|
} else {
|
|
|
|
if ( nodeType === TEXT_NODE ) {
|
|
|
|
data = child.data;
|
2014-05-20 02:47:02 -05:00
|
|
|
// Use \S instead of notWS, because we want to remove nodes
|
|
|
|
// which are just nbsp, in order to cleanup <div>nbsp<br></div>
|
|
|
|
// construct.
|
|
|
|
if ( /\S/.test( data ) ) {
|
2014-05-26 20:36:50 -05:00
|
|
|
// If the parent node is inline, don't trim this node as
|
|
|
|
// it probably isn't at the end of the block.
|
|
|
|
if ( isInline( node ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-24 19:59:30 -05:00
|
|
|
j = 0;
|
|
|
|
ll = data.length;
|
|
|
|
if ( !i || !isInline( children[ i - 1 ] ) ) {
|
|
|
|
while ( j < ll && !notWS.test( data.charAt( j ) ) ) {
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
if ( j ) {
|
|
|
|
child.data = data = data.slice( j );
|
|
|
|
ll -= j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( i + 1 === l || !isInline( children[ i + 1 ] ) ) {
|
2014-04-30 00:15:54 -05:00
|
|
|
j = ll;
|
|
|
|
while ( j > 0 && !notWS.test( data.charAt( j - 1 ) ) ) {
|
2014-04-24 19:59:30 -05:00
|
|
|
j -= 1;
|
|
|
|
}
|
2014-04-30 00:15:54 -05:00
|
|
|
if ( j < ll ) {
|
2014-04-24 19:59:30 -05:00
|
|
|
child.data = data.slice( 0, j );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-05 04:58:22 -05:00
|
|
|
// If we have just white space, it may still be important if it
|
|
|
|
// separates two inline nodes, e.g. "<a>link</a> <a>link</a>".
|
|
|
|
else if ( i && i + 1 < l &&
|
|
|
|
isInline( children[ i - 1 ] ) &&
|
|
|
|
isInline( children[ i + 1 ] ) ) {
|
|
|
|
child.data = ' ';
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-24 19:59:30 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
node.removeChild( child );
|
|
|
|
i -= 1;
|
|
|
|
l -= 1;
|
2011-10-30 23:16:08 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
|
|
|
|
var notWSTextNode = function ( node ) {
|
2014-09-04 05:19:02 -05:00
|
|
|
return node.nodeType === ELEMENT_NODE ?
|
2013-04-07 22:27:06 -05:00
|
|
|
node.nodeName === 'BR' :
|
2014-09-04 05:19:02 -05:00
|
|
|
notWS.test( node.data );
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
var isLineBreak = function ( br ) {
|
|
|
|
var block = br.parentNode,
|
|
|
|
walker;
|
|
|
|
while ( isInline( block ) ) {
|
|
|
|
block = block.parentNode;
|
|
|
|
}
|
|
|
|
walker = new TreeWalker(
|
|
|
|
block, SHOW_ELEMENT|SHOW_TEXT, notWSTextNode );
|
|
|
|
walker.currentNode = br;
|
|
|
|
return !!walker.nextNode();
|
|
|
|
};
|
|
|
|
|
|
|
|
// <br> elements are treated specially, and differently depending on the
|
|
|
|
// browser, when in rich text editor mode. When adding HTML from external
|
|
|
|
// sources, we must remove them, replacing the ones that actually affect
|
|
|
|
// line breaks with a split of the block element containing it (and wrapping
|
|
|
|
// any not inside a block). Browsers that want <br> elements at the end of
|
|
|
|
// each block will then have them added back in a later fixCursor method
|
|
|
|
// call.
|
|
|
|
var cleanupBRs = function ( root ) {
|
|
|
|
var brs = root.querySelectorAll( 'BR' ),
|
|
|
|
brBreaksLine = [],
|
|
|
|
l = brs.length,
|
|
|
|
i, br, block;
|
|
|
|
|
|
|
|
// Must calculate whether the <br> breaks a line first, because if we
|
|
|
|
// have two <br>s next to each other, after the first one is converted
|
|
|
|
// to a block split, the second will be at the end of a block and
|
|
|
|
// therefore seem to not be a line break. But in its original context it
|
|
|
|
// was, so we should also convert it to a block split.
|
|
|
|
for ( i = 0; i < l; i += 1 ) {
|
|
|
|
brBreaksLine[i] = isLineBreak( brs[i] );
|
|
|
|
}
|
|
|
|
while ( l-- ) {
|
|
|
|
br = brs[l];
|
|
|
|
// Cleanup may have removed it
|
|
|
|
block = br.parentNode;
|
|
|
|
if ( !block ) { continue; }
|
|
|
|
while ( isInline( block ) ) {
|
2013-02-25 02:05:32 -05:00
|
|
|
block = block.parentNode;
|
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
// If this is not inside a block, replace it by wrapping
|
2014-08-05 05:59:48 -05:00
|
|
|
// inlines in a <div>.
|
|
|
|
if ( !isBlock( block ) ) {
|
2014-05-26 20:31:06 -05:00
|
|
|
fixContainer( block );
|
2013-02-25 02:05:32 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
else {
|
2014-08-05 05:59:48 -05:00
|
|
|
// If it doesn't break a line, just remove it; it's not doing
|
|
|
|
// anything useful. We'll add it back later if required by the
|
|
|
|
// browser. If it breaks a line, split the block or leave it as
|
|
|
|
// appropriate.
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( brBreaksLine[l] ) {
|
2014-08-05 05:59:48 -05:00
|
|
|
// If in a <div>, split, but anywhere else we might change
|
|
|
|
// the formatting too much (e.g. <li> -> to two list items!)
|
|
|
|
// so just play it safe and leave it.
|
|
|
|
if ( block.nodeName !== 'DIV' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
split( br.parentNode, br, block.parentNode );
|
2011-11-04 00:53:12 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
detach( br );
|
2011-11-04 00:53:12 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-06-01 19:32:21 -05:00
|
|
|
proto._ensureBottomLine = function () {
|
|
|
|
var body = this._body,
|
|
|
|
div = body.lastChild;
|
|
|
|
if ( !div || div.nodeName !== 'DIV' || !isBlock( div ) ) {
|
|
|
|
body.appendChild( this.createDefaultBlock() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// --- Cut and Paste ---
|
2012-08-03 03:13:41 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._onCut = function () {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Save undo checkpoint
|
2013-06-20 08:15:18 -05:00
|
|
|
var range = this.getSelection();
|
2013-07-18 00:49:21 -05:00
|
|
|
var self = this;
|
2013-07-17 02:12:43 -05:00
|
|
|
this._recordUndoState( range );
|
|
|
|
this._getRangeAndRemoveBookmark( range );
|
|
|
|
this.setSelection( range );
|
2013-07-18 00:49:21 -05:00
|
|
|
setTimeout( function () {
|
|
|
|
try {
|
|
|
|
// If all content removed, ensure div at start of body.
|
2014-06-01 19:32:21 -05:00
|
|
|
self._ensureBottomLine();
|
2013-07-18 00:49:21 -05:00
|
|
|
} catch ( error ) {
|
|
|
|
self.didError( error );
|
|
|
|
}
|
|
|
|
}, 0 );
|
2013-06-20 08:15:18 -05:00
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._onPaste = function ( event ) {
|
|
|
|
if ( this._awaitingPaste ) { return; }
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// Treat image paste as a drop of an image file.
|
|
|
|
var clipboardData = event.clipboardData,
|
|
|
|
items = clipboardData && clipboardData.items,
|
|
|
|
fireDrop = false,
|
2013-06-03 00:34:51 -05:00
|
|
|
hasImage = false,
|
|
|
|
l, type;
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( items ) {
|
|
|
|
l = items.length;
|
|
|
|
while ( l-- ) {
|
2013-06-03 00:34:51 -05:00
|
|
|
type = items[l].type;
|
|
|
|
if ( type === 'text/html' ) {
|
|
|
|
hasImage = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( /^image\/.*/.test( type ) ) {
|
|
|
|
hasImage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( hasImage ) {
|
|
|
|
event.preventDefault();
|
2013-06-20 08:15:18 -05:00
|
|
|
this.fireEvent( 'dragover', {
|
2013-06-03 00:34:51 -05:00
|
|
|
dataTransfer: clipboardData,
|
|
|
|
/*jshint loopfunc: true */
|
|
|
|
preventDefault: function () {
|
|
|
|
fireDrop = true;
|
2012-08-03 03:13:41 -05:00
|
|
|
}
|
2013-06-03 00:34:51 -05:00
|
|
|
/*jshint loopfunc: false */
|
|
|
|
});
|
|
|
|
if ( fireDrop ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this.fireEvent( 'drop', {
|
2013-06-03 00:34:51 -05:00
|
|
|
dataTransfer: clipboardData
|
|
|
|
});
|
2012-08-03 03:13:41 -05:00
|
|
|
}
|
2013-06-03 00:34:51 -05:00
|
|
|
return;
|
2012-08-03 03:13:41 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-08-03 03:13:41 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
this._awaitingPaste = true;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
var self = this,
|
|
|
|
body = this._body,
|
|
|
|
range = this.getSelection(),
|
2013-04-07 22:27:06 -05:00
|
|
|
startContainer = range.startContainer,
|
|
|
|
startOffset = range.startOffset,
|
|
|
|
endContainer = range.endContainer,
|
2014-08-06 07:42:57 -05:00
|
|
|
endOffset = range.endOffset,
|
2014-09-24 04:36:14 -05:00
|
|
|
startBlock = getStartBlockOfRange( range );
|
2014-08-06 07:42:57 -05:00
|
|
|
|
2014-09-30 21:29:54 -05:00
|
|
|
// Record undo checkpoint
|
|
|
|
self._recordUndoState( range );
|
|
|
|
self._getRangeAndRemoveBookmark( range );
|
|
|
|
|
2014-08-06 07:42:57 -05:00
|
|
|
// We need to position the pasteArea in the visible portion of the screen
|
|
|
|
// to stop the browser auto-scrolling.
|
2013-06-20 08:15:18 -05:00
|
|
|
var pasteArea = this.createElement( 'DIV', {
|
2013-04-07 22:27:06 -05:00
|
|
|
style: 'position: absolute; overflow: hidden; top:' +
|
2014-09-24 04:36:14 -05:00
|
|
|
( body.scrollTop +
|
|
|
|
( startBlock ? startBlock.getBoundingClientRect().top : 0 ) ) +
|
2014-08-06 07:42:57 -05:00
|
|
|
'px; left: 0; width: 1px; height: 1px;'
|
2013-01-23 19:54:44 -05:00
|
|
|
});
|
2013-04-07 22:27:06 -05:00
|
|
|
body.appendChild( pasteArea );
|
|
|
|
range.selectNodeContents( pasteArea );
|
2013-06-20 08:15:18 -05:00
|
|
|
this.setSelection( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// A setTimeout of 0 means this is added to the back of the
|
|
|
|
// single javascript thread, so it will be executed after the
|
|
|
|
// paste event.
|
|
|
|
setTimeout( function () {
|
|
|
|
try {
|
|
|
|
// Get the pasted content and clean
|
|
|
|
var frag = empty( detach( pasteArea ) ),
|
|
|
|
first = frag.firstChild,
|
2013-06-20 08:15:18 -05:00
|
|
|
range = self._createRange(
|
2013-04-07 22:27:06 -05:00
|
|
|
startContainer, startOffset, endContainer, endOffset );
|
|
|
|
|
|
|
|
// Was anything actually pasted?
|
|
|
|
if ( first ) {
|
|
|
|
// Safari and IE like putting extra divs around things.
|
|
|
|
if ( first === frag.lastChild &&
|
|
|
|
first.nodeName === 'DIV' ) {
|
|
|
|
frag.replaceChild( empty( first ), first );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
frag.normalize();
|
|
|
|
addLinks( frag );
|
|
|
|
cleanTree( frag, false );
|
|
|
|
cleanupBRs( frag );
|
|
|
|
removeEmptyInlines( frag );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var node = frag,
|
|
|
|
doPaste = true;
|
|
|
|
while ( node = getNextBlock( node ) ) {
|
|
|
|
fixCursor( node );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
self.fireEvent( 'willPaste', {
|
2013-04-07 22:27:06 -05:00
|
|
|
fragment: frag,
|
|
|
|
preventDefault: function () {
|
|
|
|
doPaste = false;
|
|
|
|
}
|
|
|
|
});
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Insert pasted data
|
|
|
|
if ( doPaste ) {
|
2013-06-20 06:03:01 -05:00
|
|
|
insertTreeFragmentIntoRange( range, frag );
|
2013-06-20 08:15:18 -05:00
|
|
|
self._docWasChanged();
|
2013-04-07 22:27:06 -05:00
|
|
|
range.collapse( false );
|
2014-06-01 19:32:21 -05:00
|
|
|
self._ensureBottomLine();
|
2013-01-23 19:54:44 -05:00
|
|
|
}
|
2012-07-01 21:41:38 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
self._awaitingPaste = false;
|
2013-01-23 19:54:44 -05:00
|
|
|
} catch ( error ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
self.didError( error );
|
2012-07-01 21:41:38 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}, 0 );
|
2013-06-20 08:15:18 -05:00
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// --- Keyboard interaction ---
|
|
|
|
|
|
|
|
var keys = {
|
|
|
|
8: 'backspace',
|
|
|
|
9: 'tab',
|
|
|
|
13: 'enter',
|
|
|
|
32: 'space',
|
2013-05-20 01:14:28 -05:00
|
|
|
37: 'left',
|
|
|
|
39: 'right',
|
2014-04-06 23:44:44 -05:00
|
|
|
46: 'delete',
|
|
|
|
219: '[',
|
|
|
|
221: ']'
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
var mapKeyTo = function ( method ) {
|
|
|
|
return function ( self, event ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
event.preventDefault();
|
2013-06-20 08:15:18 -05:00
|
|
|
self[ method ]();
|
2012-07-01 21:41:38 -05:00
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2012-07-01 21:41:38 -05:00
|
|
|
|
2014-01-12 22:31:19 -05:00
|
|
|
var mapKeyToFormat = function ( tag, remove ) {
|
|
|
|
remove = remove || null;
|
2013-06-20 08:15:18 -05:00
|
|
|
return function ( self, event ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
event.preventDefault();
|
2013-06-20 08:15:18 -05:00
|
|
|
var range = self.getSelection();
|
|
|
|
if ( self.hasFormat( tag, null, range ) ) {
|
|
|
|
self.changeFormat( null, { tag: tag }, range );
|
2013-04-07 22:27:06 -05:00
|
|
|
} else {
|
2014-01-12 22:31:19 -05:00
|
|
|
self.changeFormat( { tag: tag }, remove, range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// If you delete the content inside a span with a font styling, Webkit will
|
|
|
|
// replace it with a <font> tag (!). If you delete all the text inside a
|
|
|
|
// link in Opera, it won't delete the link. Let's make things consistent. If
|
|
|
|
// you delete all text inside an inline tag, remove the inline tag.
|
2013-06-20 08:15:18 -05:00
|
|
|
var afterDelete = function ( self ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
try {
|
2013-07-17 01:13:29 -05:00
|
|
|
var range = self.getSelection(),
|
2013-04-07 22:27:06 -05:00
|
|
|
node = range.startContainer,
|
|
|
|
parent;
|
|
|
|
if ( node.nodeType === TEXT_NODE ) {
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
// If focussed in empty inline element
|
|
|
|
if ( isInline( node ) && !node.textContent ) {
|
|
|
|
do {
|
|
|
|
parent = node.parentNode;
|
|
|
|
} while ( isInline( parent ) &&
|
|
|
|
!parent.textContent && ( node = parent ) );
|
|
|
|
range.setStart( parent,
|
|
|
|
indexOf.call( parent.childNodes, node ) );
|
|
|
|
range.collapse( true );
|
|
|
|
parent.removeChild( node );
|
|
|
|
if ( !isBlock( parent ) ) {
|
|
|
|
parent = getPreviousBlock( parent );
|
2012-08-08 01:20:28 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
fixCursor( parent );
|
2013-06-20 06:03:01 -05:00
|
|
|
moveRangeBoundariesDownTree( range );
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2014-06-01 19:32:21 -05:00
|
|
|
self._ensureBottomLine();
|
2013-04-07 22:27:06 -05:00
|
|
|
} catch ( error ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
self.didError( error );
|
2012-08-08 01:20:28 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// If you select all in IE8 then type, it makes a P; replace it with
|
|
|
|
// a DIV.
|
|
|
|
if ( isIE8 ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._ieSelAllClean = function () {
|
|
|
|
var firstChild = this._body.firstChild;
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( firstChild.nodeName === 'P' ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
this._saveRangeToBookmark( this.getSelection() );
|
2014-06-01 19:22:52 -05:00
|
|
|
replaceWith( firstChild, this.createDefaultBlock([
|
2013-04-07 22:27:06 -05:00
|
|
|
empty( firstChild )
|
2014-06-01 19:22:52 -05:00
|
|
|
]));
|
2013-06-20 08:15:18 -05:00
|
|
|
this.setSelection( this._getRangeAndRemoveBookmark() );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-08-08 01:20:28 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
var keyHandlers = {
|
2013-06-20 08:15:18 -05:00
|
|
|
enter: function ( self, event ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// We handle this ourselves
|
|
|
|
event.preventDefault();
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Must have some form of selection
|
2014-04-06 22:05:44 -05:00
|
|
|
var range = self.getSelection(),
|
|
|
|
block, parent, tag, splitTag, nodeAfterSplit;
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !range ) { return; }
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Save undo checkpoint and add any links in the preceding section.
|
2014-09-02 21:21:16 -05:00
|
|
|
// Remove any zws so we don't think there's content in an empty
|
|
|
|
// block.
|
2013-06-20 08:15:18 -05:00
|
|
|
self._recordUndoState( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
addLinks( range.startContainer );
|
2014-09-02 21:21:16 -05:00
|
|
|
self._removeZWS();
|
2013-06-20 08:15:18 -05:00
|
|
|
self._getRangeAndRemoveBookmark( range );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Selected text is overwritten, therefore delete the contents
|
|
|
|
// to collapse selection.
|
|
|
|
if ( !range.collapsed ) {
|
2013-06-20 06:03:01 -05:00
|
|
|
deleteContentsOfRange( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-04-06 22:05:44 -05:00
|
|
|
block = getStartBlockOfRange( range );
|
|
|
|
if ( block && ( parent = getNearest( block, 'LI' ) ) ) {
|
|
|
|
block = parent;
|
|
|
|
}
|
|
|
|
tag = block ? block.nodeName : 'DIV';
|
|
|
|
splitTag = tagAfterSplit[ tag ];
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// If this is a malformed bit of document, just play it safe
|
|
|
|
// and insert a <br>.
|
|
|
|
if ( !block ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
insertNodeInRange( range, self.createElement( 'BR' ) );
|
2013-04-07 22:27:06 -05:00
|
|
|
range.collapse( false );
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
|
|
|
self._docWasChanged();
|
2013-04-07 22:27:06 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// We need to wrap the contents in divs.
|
|
|
|
var splitNode = range.startContainer,
|
|
|
|
splitOffset = range.startOffset,
|
|
|
|
replacement;
|
|
|
|
if ( !splitTag ) {
|
|
|
|
// If the selection point is inside the block, we're going to
|
2014-04-06 22:05:44 -05:00
|
|
|
// rewrite it so our saved reference points won't be valid.
|
2013-04-07 22:27:06 -05:00
|
|
|
// Pick a node at a deeper point in the tree to avoid this.
|
|
|
|
if ( splitNode === block ) {
|
|
|
|
splitNode = splitOffset ?
|
|
|
|
splitNode.childNodes[ splitOffset - 1 ] : null;
|
|
|
|
splitOffset = 0;
|
|
|
|
if ( splitNode ) {
|
|
|
|
if ( splitNode.nodeName === 'BR' ) {
|
|
|
|
splitNode = splitNode.nextSibling;
|
|
|
|
} else {
|
|
|
|
splitOffset = getLength( splitNode );
|
|
|
|
}
|
|
|
|
if ( !splitNode || splitNode.nodeName === 'BR' ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
replacement = fixCursor( self.createElement( 'DIV' ) );
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( splitNode ) {
|
|
|
|
block.replaceChild( replacement, splitNode );
|
2011-11-04 00:53:12 -05:00
|
|
|
} else {
|
2013-04-07 22:27:06 -05:00
|
|
|
block.appendChild( replacement );
|
2011-11-04 00:53:12 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
splitNode = replacement;
|
2011-11-04 00:53:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 20:31:06 -05:00
|
|
|
fixContainer( block );
|
2013-04-07 22:27:06 -05:00
|
|
|
splitTag = 'DIV';
|
|
|
|
if ( !splitNode ) {
|
|
|
|
splitNode = block.firstChild;
|
|
|
|
}
|
|
|
|
range.setStart( splitNode, splitOffset );
|
|
|
|
range.setEnd( splitNode, splitOffset );
|
2013-06-20 06:03:01 -05:00
|
|
|
block = getStartBlockOfRange( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( !block.textContent ) {
|
|
|
|
// Break list
|
|
|
|
if ( getNearest( block, 'UL' ) || getNearest( block, 'OL' ) ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
return self.modifyBlocks( decreaseListLevel, range );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
// Break blockquote
|
|
|
|
else if ( getNearest( block, 'BLOCKQUOTE' ) ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
return self.modifyBlocks( removeBlockQuote, range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Otherwise, split at cursor point.
|
|
|
|
nodeAfterSplit = splitBlock( block, splitNode, splitOffset );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Focus cursor
|
|
|
|
// If there's a <b>/<i> etc. at the beginning of the split
|
|
|
|
// make sure we focus inside it.
|
|
|
|
while ( nodeAfterSplit.nodeType === ELEMENT_NODE ) {
|
|
|
|
var child = nodeAfterSplit.firstChild,
|
|
|
|
next;
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Don't continue links over a block break; unlikely to be the
|
|
|
|
// desired outcome.
|
|
|
|
if ( nodeAfterSplit.nodeName === 'A' ) {
|
|
|
|
replaceWith( nodeAfterSplit, empty( nodeAfterSplit ) );
|
2011-10-28 22:15:21 -05:00
|
|
|
nodeAfterSplit = child;
|
2013-04-07 22:27:06 -05:00
|
|
|
continue;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
while ( child && child.nodeType === TEXT_NODE && !child.data ) {
|
|
|
|
next = child.nextSibling;
|
|
|
|
if ( !next || next.nodeName === 'BR' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
detach( child );
|
|
|
|
child = next;
|
2011-12-08 21:15:15 -05:00
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// 'BR's essentially don't count; they're a browser hack.
|
|
|
|
// If you try to select the contents of a 'BR', FF will not let
|
|
|
|
// you type anything!
|
|
|
|
if ( !child || child.nodeName === 'BR' ||
|
2014-10-02 04:22:51 -05:00
|
|
|
( child.nodeType === TEXT_NODE && !isPresto ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
break;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
nodeAfterSplit = child;
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
range = self._createRange( nodeAfterSplit, 0 );
|
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// Scroll into view
|
|
|
|
if ( nodeAfterSplit.nodeType === TEXT_NODE ) {
|
|
|
|
nodeAfterSplit = nodeAfterSplit.parentNode;
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
var doc = self._doc,
|
|
|
|
body = self._body;
|
2013-04-07 22:27:06 -05:00
|
|
|
if ( nodeAfterSplit.offsetTop + nodeAfterSplit.offsetHeight >
|
|
|
|
( doc.documentElement.scrollTop || body.scrollTop ) +
|
|
|
|
body.offsetHeight ) {
|
|
|
|
nodeAfterSplit.scrollIntoView( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're not still in an undo state
|
2013-06-20 08:15:18 -05:00
|
|
|
self._docWasChanged();
|
2013-04-07 22:27:06 -05:00
|
|
|
},
|
2013-06-20 08:15:18 -05:00
|
|
|
backspace: function ( self, event ) {
|
2014-09-02 21:21:16 -05:00
|
|
|
self._removeZWS();
|
2014-09-30 21:29:54 -05:00
|
|
|
// Record undo checkpoint.
|
2013-06-20 08:15:18 -05:00
|
|
|
var range = self.getSelection();
|
2014-09-30 21:29:54 -05:00
|
|
|
self._recordUndoState( range );
|
|
|
|
self._getRangeAndRemoveBookmark( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
// If not collapsed, delete contents
|
|
|
|
if ( !range.collapsed ) {
|
|
|
|
event.preventDefault();
|
2013-06-20 06:03:01 -05:00
|
|
|
deleteContentsOfRange( range );
|
2014-06-01 19:32:21 -05:00
|
|
|
self._ensureBottomLine();
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
// If at beginning of block, merge with previous
|
2013-06-20 06:03:01 -05:00
|
|
|
else if ( rangeDoesStartAtBlockBoundary( range ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
event.preventDefault();
|
2013-06-20 06:03:01 -05:00
|
|
|
var current = getStartBlockOfRange( range ),
|
2013-04-07 22:27:06 -05:00
|
|
|
previous = current && getPreviousBlock( current );
|
|
|
|
// Must not be at the very beginning of the text area.
|
|
|
|
if ( previous ) {
|
|
|
|
// If not editable, just delete whole block.
|
|
|
|
if ( !previous.isContentEditable ) {
|
|
|
|
detach( previous );
|
|
|
|
return;
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
// Otherwise merge.
|
|
|
|
mergeWithBlock( previous, current, range );
|
|
|
|
// If deleted line between containers, merge newly adjacent
|
|
|
|
// containers.
|
|
|
|
current = previous.parentNode;
|
|
|
|
while ( current && !current.nextSibling ) {
|
|
|
|
current = current.parentNode;
|
|
|
|
}
|
|
|
|
if ( current && ( current = current.nextSibling ) ) {
|
|
|
|
mergeContainers( current );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
// If at very beginning of text area, allow backspace
|
|
|
|
// to break lists/blockquote.
|
2013-04-09 20:08:40 -05:00
|
|
|
else if ( current ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
// Break list
|
|
|
|
if ( getNearest( current, 'UL' ) ||
|
|
|
|
getNearest( current, 'OL' ) ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
return self.modifyBlocks( decreaseListLevel, range );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
// Break blockquote
|
|
|
|
else if ( getNearest( current, 'BLOCKQUOTE' ) ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
return self.modifyBlocks( decreaseBlockQuoteLevel, range );
|
2013-02-24 21:52:22 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
// Otherwise, leave to browser but check afterwards whether it has
|
|
|
|
// left behind an empty inline tag.
|
|
|
|
else {
|
2014-09-30 21:29:54 -05:00
|
|
|
self.setSelection( range );
|
2013-06-20 08:15:18 -05:00
|
|
|
setTimeout( function () { afterDelete( self ); }, 0 );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
},
|
2013-06-20 08:15:18 -05:00
|
|
|
'delete': function ( self, event ) {
|
2014-09-02 21:21:16 -05:00
|
|
|
self._removeZWS();
|
2014-09-30 21:29:54 -05:00
|
|
|
// Record undo checkpoint.
|
2013-06-20 08:15:18 -05:00
|
|
|
var range = self.getSelection();
|
2014-09-30 21:29:54 -05:00
|
|
|
self._recordUndoState( range );
|
|
|
|
self._getRangeAndRemoveBookmark( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
// If not collapsed, delete contents
|
|
|
|
if ( !range.collapsed ) {
|
|
|
|
event.preventDefault();
|
2013-06-20 06:03:01 -05:00
|
|
|
deleteContentsOfRange( range );
|
2014-06-01 19:32:21 -05:00
|
|
|
self._ensureBottomLine();
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
2012-03-29 18:18:17 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
// If at end of block, merge next into this block
|
2013-06-20 06:03:01 -05:00
|
|
|
else if ( rangeDoesEndAtBlockBoundary( range ) ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
event.preventDefault();
|
2013-06-20 06:03:01 -05:00
|
|
|
var current = getStartBlockOfRange( range ),
|
2013-04-07 22:27:06 -05:00
|
|
|
next = current && getNextBlock( current );
|
|
|
|
// Must not be at the very end of the text area.
|
|
|
|
if ( next ) {
|
|
|
|
// If not editable, just delete whole block.
|
|
|
|
if ( !next.isContentEditable ) {
|
|
|
|
detach( next );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Otherwise merge.
|
|
|
|
mergeWithBlock( current, next, range );
|
|
|
|
// If deleted line between containers, merge newly adjacent
|
|
|
|
// containers.
|
|
|
|
next = current.parentNode;
|
|
|
|
while ( next && !next.nextSibling ) {
|
|
|
|
next = next.parentNode;
|
|
|
|
}
|
|
|
|
if ( next && ( next = next.nextSibling ) ) {
|
|
|
|
mergeContainers( next );
|
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
self.setSelection( range );
|
|
|
|
self._updatePath( range, true );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, leave to browser but check afterwards whether it has
|
|
|
|
// left behind an empty inline tag.
|
|
|
|
else {
|
2014-09-30 21:29:54 -05:00
|
|
|
self.setSelection( range );
|
2013-06-20 08:15:18 -05:00
|
|
|
setTimeout( function () { afterDelete( self ); }, 0 );
|
2011-10-28 22:15:21 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
},
|
2014-04-06 22:05:44 -05:00
|
|
|
tab: function ( self, event ) {
|
2014-09-02 21:21:16 -05:00
|
|
|
self._removeZWS();
|
2014-04-06 22:05:44 -05:00
|
|
|
var range = self.getSelection(),
|
|
|
|
node, parent;
|
|
|
|
// If no selection and in an empty block
|
|
|
|
if ( range.collapsed &&
|
|
|
|
rangeDoesStartAtBlockBoundary( range ) &&
|
|
|
|
rangeDoesEndAtBlockBoundary( range ) ) {
|
|
|
|
node = getStartBlockOfRange( range );
|
|
|
|
// Iterate through the block's parents
|
|
|
|
while ( parent = node.parentNode ) {
|
|
|
|
// If we find a UL or OL (so are in a list, node must be an LI)
|
|
|
|
if ( parent.nodeName === 'UL' || parent.nodeName === 'OL' ) {
|
|
|
|
// AND the LI is not the first in the list
|
|
|
|
if ( node.previousSibling ) {
|
|
|
|
// Then increase the list level
|
|
|
|
event.preventDefault();
|
|
|
|
self.modifyBlocks( increaseListLevel, range );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = parent;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
2013-06-20 08:15:18 -05:00
|
|
|
space: function ( self ) {
|
|
|
|
var range = self.getSelection();
|
|
|
|
self._recordUndoState( range );
|
2013-04-07 22:27:06 -05:00
|
|
|
addLinks( range.startContainer );
|
2013-06-20 08:15:18 -05:00
|
|
|
self._getRangeAndRemoveBookmark( range );
|
|
|
|
self.setSelection( range );
|
2014-04-16 03:06:10 -05:00
|
|
|
},
|
|
|
|
left: function ( self ) {
|
|
|
|
self._removeZWS();
|
|
|
|
},
|
|
|
|
right: function ( self ) {
|
|
|
|
self._removeZWS();
|
2013-05-20 01:14:28 -05:00
|
|
|
}
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2014-09-30 21:29:54 -05:00
|
|
|
|
2013-05-20 01:14:28 -05:00
|
|
|
// Firefox incorrectly handles Cmd-left/Cmd-right on Mac:
|
|
|
|
// it goes back/forward in history! Override to do the right
|
|
|
|
// thing.
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=289384
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( isMac && isGecko && win.getSelection().modify ) {
|
|
|
|
keyHandlers[ 'meta-left' ] = function ( self, event ) {
|
2013-05-20 01:14:28 -05:00
|
|
|
event.preventDefault();
|
2013-06-20 08:15:18 -05:00
|
|
|
self._sel.modify( 'move', 'backward', 'lineboundary' );
|
2013-05-20 01:14:28 -05:00
|
|
|
};
|
2013-06-20 08:15:18 -05:00
|
|
|
keyHandlers[ 'meta-right' ] = function ( self, event ) {
|
2013-05-20 01:14:28 -05:00
|
|
|
event.preventDefault();
|
2013-06-20 08:15:18 -05:00
|
|
|
self._sel.modify( 'move', 'forward', 'lineboundary' );
|
2013-05-20 01:14:28 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
keyHandlers[ ctrlKey + 'b' ] = mapKeyToFormat( 'B' );
|
|
|
|
keyHandlers[ ctrlKey + 'i' ] = mapKeyToFormat( 'I' );
|
|
|
|
keyHandlers[ ctrlKey + 'u' ] = mapKeyToFormat( 'U' );
|
2014-02-03 02:19:13 -05:00
|
|
|
keyHandlers[ ctrlKey + 'shift-7' ] = mapKeyToFormat( 'S' );
|
2014-01-12 22:31:19 -05:00
|
|
|
keyHandlers[ ctrlKey + 'shift-5' ] = mapKeyToFormat( 'SUB', { tag: 'SUP' } );
|
|
|
|
keyHandlers[ ctrlKey + 'shift-6' ] = mapKeyToFormat( 'SUP', { tag: 'SUB' } );
|
2014-04-06 23:44:44 -05:00
|
|
|
keyHandlers[ ctrlKey + 'shift-8' ] = mapKeyTo( 'makeUnorderedList' );
|
|
|
|
keyHandlers[ ctrlKey + 'shift-9' ] = mapKeyTo( 'makeOrderedList' );
|
|
|
|
keyHandlers[ ctrlKey + '[' ] = mapKeyTo( 'decreaseQuoteLevel' );
|
|
|
|
keyHandlers[ ctrlKey + ']' ] = mapKeyTo( 'increaseQuoteLevel' );
|
2013-06-20 08:15:18 -05:00
|
|
|
keyHandlers[ ctrlKey + 'y' ] = mapKeyTo( 'redo' );
|
|
|
|
keyHandlers[ ctrlKey + 'z' ] = mapKeyTo( 'undo' );
|
|
|
|
keyHandlers[ ctrlKey + 'shift-z' ] = mapKeyTo( 'redo' );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
// Ref: http://unixpapa.com/js/key.html
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._onKey = function ( event ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
var code = event.keyCode,
|
2014-04-06 23:44:44 -05:00
|
|
|
key = keys[ code ],
|
2013-04-07 22:27:06 -05:00
|
|
|
modifiers = '';
|
|
|
|
|
2014-04-06 23:44:44 -05:00
|
|
|
if ( !key ) {
|
|
|
|
key = String.fromCharCode( code ).toLowerCase();
|
|
|
|
// Only reliable for letters and numbers
|
|
|
|
if ( !/^[A-Za-z0-9]$/.test( key ) ) {
|
|
|
|
key = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// On keypress, delete and '.' both have event.keyCode 46
|
|
|
|
// Must check event.which to differentiate.
|
2014-10-02 04:22:51 -05:00
|
|
|
if ( isPresto && event.which === 46 ) {
|
2013-04-07 22:27:06 -05:00
|
|
|
key = '.';
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-04-07 22:27:06 -05:00
|
|
|
// Function keys
|
|
|
|
if ( 111 < code && code < 124 ) {
|
|
|
|
key = 'f' + ( code - 111 );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-11-20 00:45:36 -05:00
|
|
|
// We need to apply the backspace/delete handlers regardless of
|
|
|
|
// control key modifiers.
|
2014-09-30 21:29:54 -05:00
|
|
|
if ( key !== 'backspace' && key !== 'delete' ) {
|
2014-11-20 00:45:36 -05:00
|
|
|
if ( event.altKey ) { modifiers += 'alt-'; }
|
2014-09-30 21:29:54 -05:00
|
|
|
if ( event.ctrlKey ) { modifiers += 'ctrl-'; }
|
|
|
|
if ( event.metaKey ) { modifiers += 'meta-'; }
|
|
|
|
}
|
2014-11-20 00:45:36 -05:00
|
|
|
// However, on Windows, shift-delete is apparently "cut" (WTF right?), so
|
|
|
|
// we want to let the browser handle shift-delete.
|
|
|
|
if ( event.shiftKey ) { modifiers += 'shift-'; }
|
|
|
|
|
|
|
|
key = modifiers + key;
|
2013-04-07 22:27:06 -05:00
|
|
|
|
|
|
|
if ( keyHandlers[ key ] ) {
|
2013-06-20 08:15:18 -05:00
|
|
|
keyHandlers[ key ]( this, event );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
// --- Get/Set data ---
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._getHTML = function () {
|
|
|
|
return this._body.innerHTML;
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto._setHTML = function ( html ) {
|
|
|
|
var node = this._body;
|
|
|
|
node.innerHTML = html;
|
|
|
|
do {
|
|
|
|
fixCursor( node );
|
|
|
|
} while ( node = getNextBlock( node ) );
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.getHTML = function ( withBookMark ) {
|
|
|
|
var brs = [],
|
|
|
|
node, fixer, html, l, range;
|
|
|
|
if ( withBookMark && ( range = this.getSelection() ) ) {
|
|
|
|
this._saveRangeToBookmark( range );
|
|
|
|
}
|
|
|
|
if ( useTextFixer ) {
|
|
|
|
node = this._body;
|
|
|
|
while ( node = getNextBlock( node ) ) {
|
|
|
|
if ( !node.textContent && !node.querySelector( 'BR' ) ) {
|
|
|
|
fixer = this.createElement( 'BR' );
|
|
|
|
node.appendChild( fixer );
|
|
|
|
brs.push( fixer );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-16 03:06:10 -05:00
|
|
|
html = this._getHTML().replace( /\u200B/g, '' );
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( useTextFixer ) {
|
|
|
|
l = brs.length;
|
|
|
|
while ( l-- ) {
|
|
|
|
detach( brs[l] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( range ) {
|
|
|
|
this._getRangeAndRemoveBookmark( range );
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setHTML = function ( html ) {
|
|
|
|
var frag = this._doc.createDocumentFragment(),
|
|
|
|
div = this.createElement( 'DIV' ),
|
|
|
|
child;
|
2012-04-02 00:53:24 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
// Parse HTML into DOM tree
|
|
|
|
div.innerHTML = html;
|
|
|
|
frag.appendChild( empty( div ) );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
cleanTree( frag, true );
|
|
|
|
cleanupBRs( frag );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2014-05-26 20:31:06 -05:00
|
|
|
fixContainer( frag );
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
// Fix cursor
|
|
|
|
var node = frag;
|
|
|
|
while ( node = getNextBlock( node ) ) {
|
|
|
|
fixCursor( node );
|
|
|
|
}
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
// Remove existing body children
|
|
|
|
var body = this._body;
|
|
|
|
while ( child = body.lastChild ) {
|
|
|
|
body.removeChild( child );
|
|
|
|
}
|
|
|
|
|
|
|
|
// And insert new content
|
|
|
|
body.appendChild( frag );
|
|
|
|
fixCursor( body );
|
|
|
|
|
|
|
|
// Reset the undo stack
|
|
|
|
this._undoIndex = -1;
|
|
|
|
this._undoStack.length = 0;
|
|
|
|
this._undoStackLength = 0;
|
|
|
|
this._isInUndoState = false;
|
|
|
|
|
|
|
|
// Record undo state
|
|
|
|
var range = this._getRangeAndRemoveBookmark() ||
|
|
|
|
this._createRange( body.firstChild, 0 );
|
|
|
|
this._recordUndoState( range );
|
|
|
|
this._getRangeAndRemoveBookmark( range );
|
|
|
|
// IE will also set focus when selecting text so don't use
|
|
|
|
// setSelection. Instead, just store it in lastSelection, so if
|
|
|
|
// anything calls getSelection before first focus, we have a range
|
|
|
|
// to return.
|
|
|
|
if ( losesSelectionOnBlur ) {
|
|
|
|
this._lastSelection = range;
|
|
|
|
} else {
|
|
|
|
this.setSelection( range );
|
|
|
|
}
|
|
|
|
this._updatePath( range, true );
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
proto.insertElement = function ( el, range ) {
|
|
|
|
if ( !range ) { range = this.getSelection(); }
|
|
|
|
range.collapse( true );
|
|
|
|
if ( isInline( el ) ) {
|
|
|
|
insertNodeInRange( range, el );
|
|
|
|
range.setStartAfter( el );
|
|
|
|
} else {
|
|
|
|
// Get containing block node.
|
|
|
|
var body = this._body,
|
|
|
|
splitNode = getStartBlockOfRange( range ) || body,
|
|
|
|
parent, nodeAfterSplit;
|
|
|
|
// While at end of container node, move up DOM tree.
|
|
|
|
while ( splitNode !== body && !splitNode.nextSibling ) {
|
|
|
|
splitNode = splitNode.parentNode;
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
// If in the middle of a container node, split up to body.
|
|
|
|
if ( splitNode !== body ) {
|
|
|
|
parent = splitNode.parentNode;
|
|
|
|
nodeAfterSplit = split( parent, splitNode.nextSibling, body );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
if ( nodeAfterSplit ) {
|
|
|
|
body.insertBefore( el, nodeAfterSplit );
|
|
|
|
range.setStart( nodeAfterSplit, 0 );
|
|
|
|
range.setStart( nodeAfterSplit, 0 );
|
|
|
|
moveRangeBoundariesDownTree( range );
|
|
|
|
} else {
|
|
|
|
body.appendChild( el );
|
|
|
|
// Insert blank line below block.
|
2014-06-01 19:22:52 -05:00
|
|
|
body.appendChild( this.createDefaultBlock() );
|
2013-06-20 08:15:18 -05:00
|
|
|
range.setStart( el, 0 );
|
|
|
|
range.setEnd( el, 0 );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
this.focus();
|
|
|
|
this.setSelection( range );
|
|
|
|
this._updatePath( range );
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.insertImage = function ( src ) {
|
|
|
|
var img = this.createElement( 'IMG', {
|
|
|
|
src: src
|
|
|
|
});
|
|
|
|
this.insertElement( img );
|
|
|
|
return img;
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
// --- Formatting ---
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
var command = function ( method, arg, arg2 ) {
|
|
|
|
return function () {
|
|
|
|
this[ method ]( arg, arg2 );
|
|
|
|
return this.focus();
|
|
|
|
};
|
|
|
|
};
|
2012-01-24 19:47:26 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.addStyles = function ( styles ) {
|
|
|
|
if ( styles ) {
|
|
|
|
var head = this._doc.documentElement.firstChild,
|
|
|
|
style = this.createElement( 'STYLE', {
|
|
|
|
type: 'text/css'
|
|
|
|
});
|
|
|
|
if ( style.styleSheet ) {
|
|
|
|
// IE8: must append to document BEFORE adding styles
|
|
|
|
// or you get the IE7 CSS parser!
|
|
|
|
head.appendChild( style );
|
|
|
|
style.styleSheet.cssText = styles;
|
2013-04-07 22:27:06 -05:00
|
|
|
} else {
|
2013-06-20 08:15:18 -05:00
|
|
|
// Everyone else
|
|
|
|
style.appendChild( this._doc.createTextNode( styles ) );
|
|
|
|
head.appendChild( style );
|
2013-04-07 22:27:06 -05:00
|
|
|
}
|
2013-06-20 08:15:18 -05:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.bold = command( 'changeFormat', { tag: 'B' } );
|
|
|
|
proto.italic = command( 'changeFormat', { tag: 'I' } );
|
|
|
|
proto.underline = command( 'changeFormat', { tag: 'U' } );
|
2014-02-03 02:19:13 -05:00
|
|
|
proto.strikethrough = command( 'changeFormat', { tag: 'S' } );
|
2014-01-12 22:31:19 -05:00
|
|
|
proto.subscript = command( 'changeFormat', { tag: 'SUB' }, { tag: 'SUP' } );
|
|
|
|
proto.superscript = command( 'changeFormat', { tag: 'SUP' }, { tag: 'SUB' } );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.removeBold = command( 'changeFormat', null, { tag: 'B' } );
|
|
|
|
proto.removeItalic = command( 'changeFormat', null, { tag: 'I' } );
|
|
|
|
proto.removeUnderline = command( 'changeFormat', null, { tag: 'U' } );
|
2014-02-03 02:19:13 -05:00
|
|
|
proto.removeStrikethrough = command( 'changeFormat', null, { tag: 'S' } );
|
2014-01-12 22:31:19 -05:00
|
|
|
proto.removeSubscript = command( 'changeFormat', null, { tag: 'SUB' } );
|
|
|
|
proto.removeSuperscript = command( 'changeFormat', null, { tag: 'SUP' } );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.makeLink = function ( url ) {
|
|
|
|
var range = this.getSelection();
|
|
|
|
if ( range.collapsed ) {
|
|
|
|
var protocolEnd = url.indexOf( ':' ) + 1;
|
|
|
|
if ( protocolEnd ) {
|
|
|
|
while ( url[ protocolEnd ] === '/' ) { protocolEnd += 1; }
|
|
|
|
}
|
2013-07-16 20:35:34 -05:00
|
|
|
insertNodeInRange(
|
|
|
|
range,
|
2013-06-20 08:15:18 -05:00
|
|
|
this._doc.createTextNode( url.slice( protocolEnd ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.changeFormat({
|
|
|
|
tag: 'A',
|
|
|
|
attributes: {
|
|
|
|
href: url
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
tag: 'A'
|
|
|
|
}, range );
|
|
|
|
return this.focus();
|
|
|
|
};
|
|
|
|
proto.removeLink = function () {
|
|
|
|
this.changeFormat( null, {
|
|
|
|
tag: 'A'
|
|
|
|
}, this.getSelection(), true );
|
|
|
|
return this.focus();
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setFontFace = function ( name ) {
|
|
|
|
this.changeFormat({
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: {
|
|
|
|
'class': 'font',
|
|
|
|
style: 'font-family: ' + name + ', sans-serif;'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: { 'class': 'font' }
|
|
|
|
});
|
|
|
|
return this.focus();
|
|
|
|
};
|
|
|
|
proto.setFontSize = function ( size ) {
|
|
|
|
this.changeFormat({
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: {
|
|
|
|
'class': 'size',
|
|
|
|
style: 'font-size: ' +
|
|
|
|
( typeof size === 'number' ? size + 'px' : size )
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: { 'class': 'size' }
|
|
|
|
});
|
|
|
|
return this.focus();
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setTextColour = function ( colour ) {
|
|
|
|
this.changeFormat({
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: {
|
|
|
|
'class': 'colour',
|
|
|
|
style: 'color: ' + colour
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: { 'class': 'colour' }
|
|
|
|
});
|
|
|
|
return this.focus();
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setHighlightColour = function ( colour ) {
|
|
|
|
this.changeFormat({
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: {
|
|
|
|
'class': 'highlight',
|
|
|
|
style: 'background-color: ' + colour
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
tag: 'SPAN',
|
|
|
|
attributes: { 'class': 'highlight' }
|
|
|
|
});
|
|
|
|
return this.focus();
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setTextAlignment = function ( alignment ) {
|
|
|
|
this.forEachBlock( function ( block ) {
|
|
|
|
block.className = ( block.className
|
|
|
|
.split( /\s+/ )
|
|
|
|
.filter( function ( klass ) {
|
|
|
|
return !( /align/.test( klass ) );
|
|
|
|
})
|
|
|
|
.join( ' ' ) +
|
|
|
|
' align-' + alignment ).trim();
|
|
|
|
block.style.textAlign = alignment;
|
|
|
|
}, true );
|
|
|
|
return this.focus();
|
2013-04-07 22:27:06 -05:00
|
|
|
};
|
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.setTextDirection = function ( direction ) {
|
|
|
|
this.forEachBlock( function ( block ) {
|
|
|
|
block.className = ( block.className
|
|
|
|
.split( /\s+/ )
|
|
|
|
.filter( function ( klass ) {
|
|
|
|
return !( /dir/.test( klass ) );
|
|
|
|
})
|
|
|
|
.join( ' ' ) +
|
|
|
|
' dir-' + direction ).trim();
|
|
|
|
block.dir = direction;
|
|
|
|
}, true );
|
|
|
|
return this.focus();
|
|
|
|
};
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.increaseQuoteLevel = command( 'modifyBlocks', increaseBlockQuoteLevel );
|
|
|
|
proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel );
|
2013-04-07 22:27:06 -05:00
|
|
|
|
2013-06-20 08:15:18 -05:00
|
|
|
proto.makeUnorderedList = command( 'modifyBlocks', makeUnorderedList );
|
|
|
|
proto.makeOrderedList = command( 'modifyBlocks', makeOrderedList );
|
2014-04-06 22:05:44 -05:00
|
|
|
proto.removeList = command( 'modifyBlocks', removeList );
|
|
|
|
|
|
|
|
proto.increaseListLevel = command( 'modifyBlocks', increaseListLevel );
|
|
|
|
proto.decreaseListLevel = command( 'modifyBlocks', decreaseListLevel );
|