X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fselection%2Fplugin.js;h=fd8332e0a04a2925d00e9a4dba166a1168210942;hb=8f6c203fdaa543c3bca40baea6ae4ddcdf1a77f5;hp=04be63171d0a106aeacbb2888e7f5b8277a498e7;hpb=8665a7c6c60586526e32e8941fe2896739b6ebfb;p=ckeditor.git
diff --git a/_source/plugins/selection/plugin.js b/_source/plugins/selection/plugin.js
index 04be631..fd8332e 100644
--- a/_source/plugins/selection/plugin.js
+++ b/_source/plugins/selection/plugin.js
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
@@ -17,7 +17,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// In IE, the "selectionchange" event may still get thrown when
// releasing the WYSIWYG mode, so we need to check it first.
var sel = this.getSelection();
- if ( !sel )
+ if ( !sel || !sel.document.getWindow().$ )
return;
var firstElement = sel.getStartElement();
@@ -79,20 +79,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
{
case 'wysiwyg' :
editor.document.$.execCommand( 'SelectAll', false, null );
+ // Force triggering selectionChange (#7008)
+ editor.forceNextSelectionCheck();
+ editor.selectionChange();
break;
case 'source' :
// Select the contents of the textarea
- var textarea = editor.textarea.$ ;
+ var textarea = editor.textarea.$;
if ( CKEDITOR.env.ie )
- {
- textarea.createTextRange().execCommand( 'SelectAll' ) ;
- }
+ textarea.createTextRange().execCommand( 'SelectAll' );
else
{
- textarea.selectionStart = 0 ;
- textarea.selectionEnd = textarea.value.length ;
+ textarea.selectionStart = 0;
+ textarea.selectionEnd = textarea.value.length;
}
- textarea.focus() ;
+ textarea.focus();
}
},
canUndo : false
@@ -105,7 +106,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
editor.on( 'contentDom', function()
{
var doc = editor.document,
- body = doc.getBody();
+ body = doc.getBody(),
+ html = doc.getDocumentElement();
if ( CKEDITOR.env.ie )
{
@@ -115,7 +117,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// than firing the selection change event.
var savedRange,
- saveEnabled;
+ saveEnabled,
+ restoreEnabled = 1;
// "onfocusin" is fired before "onfocus". It makes it
// possible to restore the selection before click
@@ -131,13 +134,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// point.
if ( savedRange )
{
- // Well not break because of this.
- try
+ // Range restored here might invalidate the DOM structure thus break up
+ // the locked selection, give it up. (#6083)
+ var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
+ if ( restoreEnabled && !lockedSelection )
{
- savedRange.select();
+ // Well not break because of this.
+ try
+ {
+ savedRange.select();
+ }
+ catch (e)
+ {}
}
- catch (e)
- {}
savedRange = null;
}
@@ -146,7 +155,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
body.on( 'focus', function()
{
// Enable selections to be saved.
- saveEnabled = true;
+ saveEnabled = 1;
saveSelection();
});
@@ -159,26 +168,83 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
return;
// Disable selections from being saved.
- saveEnabled = false;
+ saveEnabled = 0;
+ restoreEnabled = 1;
});
// IE before version 8 will leave cursor blinking inside the document after
// editor blurred unless we clean up the selection. (#4716)
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
{
- doc.getWindow().on( 'blur', function( evt )
+ editor.on( 'blur', function( evt )
+ {
+ // Try/Catch to avoid errors if the editor is hidden. (#6375)
+ try
+ {
+ editor.document && editor.document.$.selection.empty();
+ }
+ catch (e) {}
+ });
+ }
+
+ // Listening on document element ensures that
+ // scrollbar is included. (#5280)
+ html.on( 'mousedown', function()
+ {
+ // Lock restore selection now, as we have
+ // a followed 'click' event which introduce
+ // new selection. (#5735)
+ restoreEnabled = 0;
+ });
+
+ html.on( 'mouseup', function()
+ {
+ restoreEnabled = 1;
+ });
+
+ // In IE6/7 the blinking cursor appears, but contents are
+ // not editable. (#5634)
+ if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )
+ {
+ // The 'click' event is not fired when clicking the
+ // scrollbars, so we can use it to check whether
+ // the empty space following
has been clicked.
+ html.on( 'click', function( evt )
{
- editor.document.$.selection.empty();
+ if ( evt.data.getTarget().getName() == 'html' )
+ editor.getSelection().getRanges()[ 0 ].select();
});
}
+ var scroll;
// IE fires the "selectionchange" event when clicking
// inside a selection. We don't want to capture that.
- body.on( 'mousedown', disableSave );
+ body.on( 'mousedown', function( evt )
+ {
+ // IE scrolls document to top on right mousedown
+ // when editor has no focus, remember this scroll
+ // position and revert it before context menu opens. (#5778)
+ if ( evt.data.$.button == 2 )
+ {
+ var sel = editor.document.$.selection;
+ if ( sel.type == 'None' )
+ scroll = editor.window.getScrollPosition();
+ }
+ disableSave();
+ });
+
body.on( 'mouseup',
- function()
+ function( evt )
{
- saveEnabled = true;
+ // Restore recorded scroll position when needed on right mouseup.
+ if ( evt.data.$.button == 2 && scroll )
+ {
+ editor.document.$.documentElement.scrollLeft = scroll.x;
+ editor.document.$.documentElement.scrollTop = scroll.y;
+ }
+ scroll = null;
+
+ saveEnabled = 1;
setTimeout( function()
{
saveSelection( true );
@@ -190,7 +256,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
body.on( 'keyup',
function()
{
- saveEnabled = true;
+ saveEnabled = 1;
saveSelection();
});
@@ -201,7 +267,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
function disableSave()
{
- saveEnabled = false;
+ saveEnabled = 0;
}
function saveSelection( testIt )
@@ -235,9 +301,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// Avoid saving selection from within text input. (#5747)
var parentTag;
- if ( nativeSel.type == 'Text'
- && ( parentTag = nativeSel.createRange().parentElement().nodeName.toLowerCase() )
- && parentTag in { input: 1, textarea : 1 } )
+ if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
+ && ( parentTag = nativeSel.createRange() )
+ && ( parentTag = parentTag.parentElement() )
+ && ( parentTag = parentTag.nodeName )
+ && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
{
return;
}
@@ -259,6 +327,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
}
});
+ // Clear the cached range path before unload. (#7174)
+ editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );
+
editor.addCommand( 'selectAll', selectAllCmd );
editor.ui.addButton( 'SelectAll',
{
@@ -341,7 +412,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
return lockedSelection;
this.document = document;
- this.isLocked = false;
+ this.isLocked = 0;
this._ =
{
cache : {}
@@ -475,10 +546,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
return ( cache.type = type );
},
- getRanges :
- CKEDITOR.env.ie ?
+ /**
+ * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
+ * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
+ * one range for each table cell when one or more table row is selected.
+ * @return {Array}
+ * @example
+ * var ranges = selection.getRanges();
+ * alert(ranges.length);
+ */
+ getRanges : (function()
+ {
+ var func = CKEDITOR.env.ie ?
( function()
{
+ function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }
+
// Finds the container and offset for a specific boundary
// of an IE range.
var getBoundaryInformation = function( range, start )
@@ -489,85 +572,108 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// Gets the element that encloses the range entirely.
var parent = range.parentElement();
- var siblings = parent.childNodes;
-
- var testRange;
- for ( var i = 0 ; i < siblings.length ; i++ )
+ // Empty parent element, e.g. ^
+ if ( !parent.hasChildNodes() )
+ return { container : parent, offset : 0 };
+
+ var siblings = parent.children,
+ child,
+ testRange = range.duplicate(),
+ startIndex = 0,
+ endIndex = siblings.length - 1,
+ index = -1,
+ position,
+ distance;
+
+ // Binary search over all element childs to test the range to see whether
+ // range is right on the boundary of one element.
+ while ( startIndex <= endIndex )
{
- var child = siblings[ i ];
- if ( child.nodeType == 1 )
- {
- testRange = range.duplicate();
+ index = Math.floor( ( startIndex + endIndex ) / 2 );
+ child = siblings[ index ];
+ testRange.moveToElementText( child );
+ position = testRange.compareEndPoints( 'StartToStart', range );
+
+ if ( position > 0 )
+ endIndex = index - 1;
+ else if ( position < 0 )
+ startIndex = index + 1;
+ else
+ return { container : parent, offset : getNodeIndex( child ) };
+ }
- testRange.moveToElementText( child );
+ // All childs are text nodes,
+ // or to the right hand of test range are all text nodes. (#6992)
+ if ( index == -1 || index == siblings.length - 1 && position < 0 )
+ {
+ // Adapt test range to embrace the entire parent contents.
+ testRange.moveToElementText( parent );
+ testRange.setEndPoint( 'StartToStart', range );
- var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),
- comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );
+ // IE report line break as CRLF with range.text but
+ // only LF with textnode.nodeValue, normalize them to avoid
+ // breaking character counting logic below. (#3949)
+ distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
- testRange.collapse();
+ siblings = parent.childNodes;
- if ( comparisonStart > 0 )
- break;
- // When selection stay at the side of certain self-closing elements, e.g. BR,
- // our comparison will never shows an equality. (#4824)
- else if ( !comparisonStart
- || comparisonEnd == 1 && comparisonStart == -1 )
- return { container : parent, offset : i };
- else if ( !comparisonEnd )
- return { container : parent, offset : i + 1 };
+ // Actual range anchor right beside test range at the boundary of text node.
+ if ( !distance )
+ {
+ child = siblings[ siblings.length - 1 ];
- testRange = null;
+ if ( child.nodeType == CKEDITOR.NODE_ELEMENT )
+ return { container : parent, offset : siblings.length };
+ else
+ return { container : child, offset : child.nodeValue.length };
}
- }
- if ( !testRange )
- {
- testRange = range.duplicate();
- testRange.moveToElementText( parent );
- testRange.collapse( false );
- }
-
- testRange.setEndPoint( 'StartToStart', range );
- // IE report line break as CRLF with range.text but
- // only LF with textnode.nodeValue, normalize them to avoid
- // breaking character counting logic below. (#3949)
- var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
-
- try
- {
+ // Start the measuring until distance overflows, meanwhile count the text nodes.
+ var i = siblings.length;
while ( distance > 0 )
distance -= siblings[ --i ].nodeValue.length;
- }
- // Measurement in IE could be somtimes wrong because of