0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 23:40:35 -05:00

Ensure there is always a selection.

Many functions rely on there being a selection; if the browser doesn't return
one, make one up (collapsed, at beginning of document).
This commit is contained in:
Neil Jenkins 2014-06-30 15:10:30 +01:00
parent c09c5e98f7
commit bbf765bee7
3 changed files with 30 additions and 18 deletions

View file

@ -1401,21 +1401,21 @@ proto.setSelection = function ( range ) {
}; };
proto.getSelection = function () { proto.getSelection = function () {
var sel = this._sel; var sel = this._sel,
selection, startContainer, endContainer;
if ( sel.rangeCount ) { if ( sel.rangeCount ) {
var lastSelection = this._lastSelection = selection = sel.getRangeAt( 0 ).cloneRange();
sel.getRangeAt( 0 ).cloneRange(); startContainer = selection.startContainer;
var startContainer = lastSelection.startContainer, endContainer = selection.endContainer;
endContainer = lastSelection.endContainer;
// FF sometimes throws an error reading the isLeaf property. Let's // 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. // catch and log it to see if we can find what's going on.
try { try {
// FF can return the selection as being inside an <img>. WTF? // FF can return the selection as being inside an <img>. WTF?
if ( startContainer && isLeaf( startContainer ) ) { if ( startContainer && isLeaf( startContainer ) ) {
lastSelection.setStartBefore( startContainer ); selection.setStartBefore( startContainer );
} }
if ( endContainer && isLeaf( endContainer ) ) { if ( endContainer && isLeaf( endContainer ) ) {
lastSelection.setEndBefore( endContainer ); selection.setEndBefore( endContainer );
} }
} catch ( error ) { } catch ( error ) {
this.didError({ this.didError({
@ -1424,8 +1424,14 @@ proto.getSelection = function () {
'\nEnds: ' + endContainer.nodeName '\nEnds: ' + endContainer.nodeName
}); });
} }
this._lastSelection = selection;
} else {
selection = this._lastSelection;
} }
return this._lastSelection; if ( !selection ) {
selection = this._createRange( this._body.firstChild, 0 );
}
return selection;
}; };
proto.getSelectedText = function () { proto.getSelectedText = function () {

File diff suppressed because one or more lines are too long

View file

@ -288,21 +288,21 @@ proto.setSelection = function ( range ) {
}; };
proto.getSelection = function () { proto.getSelection = function () {
var sel = this._sel; var sel = this._sel,
selection, startContainer, endContainer;
if ( sel.rangeCount ) { if ( sel.rangeCount ) {
var lastSelection = this._lastSelection = selection = sel.getRangeAt( 0 ).cloneRange();
sel.getRangeAt( 0 ).cloneRange(); startContainer = selection.startContainer;
var startContainer = lastSelection.startContainer, endContainer = selection.endContainer;
endContainer = lastSelection.endContainer;
// FF sometimes throws an error reading the isLeaf property. Let's // 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. // catch and log it to see if we can find what's going on.
try { try {
// FF can return the selection as being inside an <img>. WTF? // FF can return the selection as being inside an <img>. WTF?
if ( startContainer && isLeaf( startContainer ) ) { if ( startContainer && isLeaf( startContainer ) ) {
lastSelection.setStartBefore( startContainer ); selection.setStartBefore( startContainer );
} }
if ( endContainer && isLeaf( endContainer ) ) { if ( endContainer && isLeaf( endContainer ) ) {
lastSelection.setEndBefore( endContainer ); selection.setEndBefore( endContainer );
} }
} catch ( error ) { } catch ( error ) {
this.didError({ this.didError({
@ -311,8 +311,14 @@ proto.getSelection = function () {
'\nEnds: ' + endContainer.nodeName '\nEnds: ' + endContainer.nodeName
}); });
} }
this._lastSelection = selection;
} else {
selection = this._lastSelection;
} }
return this._lastSelection; if ( !selection ) {
selection = this._createRange( this._body.firstChild, 0 );
}
return selection;
}; };
proto.getSelectedText = function () { proto.getSelectedText = function () {