X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fselection%2Fplugin.js;h=a42ef66f3617031c1cc52514d8cd1132407dc52c;hb=4e70ea24db840898be8cc21c950363a52a2a6aba;hp=cf699ce83c89ef64137525a3c601d88301235702;hpb=7cd80714081a8ffdf4a1a8d2c72f120ed5ef3d6d;p=ckeditor.git
diff --git a/_source/plugins/selection/plugin.js b/_source/plugins/selection/plugin.js
index cf699ce..a42ef66 100644
--- a/_source/plugins/selection/plugin.js
+++ b/_source/plugins/selection/plugin.js
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2003-2009, 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();
@@ -70,30 +70,170 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// #### checkSelectionChange : END
+ function rangeRequiresFix( range )
+ {
+ function isInlineCt( node )
+ {
+ return node && node.type == CKEDITOR.NODE_ELEMENT
+ && node.getName() in CKEDITOR.dtd.$removeEmpty;
+ }
+
+ var start = range.startContainer,
+ offset = range.startOffset;
+
+ if ( start.type == CKEDITOR.NODE_TEXT )
+ return false;
+
+ // 1. Empty inline element. ^
+ // 2. Adjoin to inline element.
text^
+ return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );
+ }
+
var selectAllCmd =
{
+ modes : { wysiwyg : 1, source : 1 },
+ readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit,
exec : function( editor )
{
switch ( editor.mode )
{
case 'wysiwyg' :
editor.document.$.execCommand( 'SelectAll', false, null );
+ // Force triggering selectionChange (#7008)
+ editor.forceNextSelectionCheck();
+ editor.selectionChange();
break;
case 'source' :
- // TODO
+ // Select the contents of the textarea
+ var textarea = editor.textarea.$;
+ if ( CKEDITOR.env.ie )
+ textarea.createTextRange().execCommand( 'SelectAll' );
+ else
+ {
+ textarea.selectionStart = 0;
+ textarea.selectionEnd = textarea.value.length;
+ }
+ textarea.focus();
}
},
canUndo : false
};
+ function createFillingChar( doc )
+ {
+ removeFillingChar( doc );
+
+ var fillingChar = doc.createText( '\u200B' );
+ doc.setCustomData( 'cke-fillingChar', fillingChar );
+
+ return fillingChar;
+ }
+
+ function getFillingChar( doc )
+ {
+ return doc && doc.getCustomData( 'cke-fillingChar' );
+ }
+
+ // Checks if a filling char has been used, eventualy removing it (#1272).
+ function checkFillingChar( doc )
+ {
+ var fillingChar = doc && getFillingChar( doc );
+ if ( fillingChar )
+ {
+ // Use this flag to avoid removing the filling char right after
+ // creating it.
+ if ( fillingChar.getCustomData( 'ready' ) )
+ removeFillingChar( doc );
+ else
+ fillingChar.setCustomData( 'ready', 1 );
+ }
+ }
+
+ function removeFillingChar( doc )
+ {
+ var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );
+ if ( fillingChar )
+ {
+ // We can't simply remove the filling node because the user
+ // will actually enlarge it when typing, so we just remove the
+ // invisible char from it.
+ fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) );
+ fillingChar = 0;
+ }
+ }
+
CKEDITOR.plugins.add( 'selection',
{
init : function( editor )
{
+ // On WebKit only, we need a special "filling" char on some situations
+ // (#1272). Here we set the events that should invalidate that char.
+ if ( CKEDITOR.env.webkit )
+ {
+ editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );
+ editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );
+ editor.on( 'key', function( e )
+ {
+ // Remove the filling char before some keys get
+ // executed, so they'll not get blocked by it.
+ switch ( e.data.keyCode )
+ {
+ case 13 : // ENTER
+ case CKEDITOR.SHIFT + 13 : // SHIFT-ENTER
+ case 37 : // LEFT-ARROW
+ case 39 : // RIGHT-ARROW
+ case 8 : // BACKSPACE
+ removeFillingChar( editor.document );
+ }
+ }, null, null, 10 );
+
+ var fillingCharBefore,
+ resetSelection;
+
+ function beforeData()
+ {
+ var doc = editor.document,
+ fillingChar = getFillingChar( doc );
+
+ if ( fillingChar )
+ {
+ // If cursor is right blinking by side of the filler node, save it for restoring,
+ // as the following text substitution will blind it. (#7437)
+ var sel = doc.$.defaultView.getSelection();
+ if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ )
+ resetSelection = 1;
+
+ fillingCharBefore = fillingChar.getText();
+ fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );
+ }
+ }
+ function afterData()
+ {
+ var doc = editor.document,
+ fillingChar = getFillingChar( doc );
+
+ if ( fillingChar )
+ {
+ fillingChar.setText( fillingCharBefore );
+
+ if ( resetSelection )
+ {
+ doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );
+ resetSelection = 0;
+ }
+ }
+ }
+ editor.on( 'beforeUndoImage', beforeData );
+ editor.on( 'afterUndoImage', afterData );
+ editor.on( 'beforeGetData', beforeData, null, null, 0 );
+ editor.on( 'getData', afterData );
+ }
+
editor.on( 'contentDom', function()
{
var doc = editor.document,
- body = doc.getBody();
+ body = doc.getBody(),
+ html = doc.getDocumentElement();
if ( CKEDITOR.env.ie )
{
@@ -103,57 +243,139 @@ 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
// events get executed.
- body.on( 'focusin', function()
+ body.on( 'focusin', function( evt )
{
+ // If there are elements with layout they fire this event but
+ // it must be ignored to allow edit its contents #4682
+ if ( evt.data.$.srcElement.nodeName != 'BODY' )
+ return;
+
// If we have saved a range, restore it at this
// point.
if ( savedRange )
{
- // Well not break because of this.
- try
+ if ( restoreEnabled )
{
- savedRange.select();
+ // Well not break because of this.
+ try
+ {
+ savedRange.select();
+ }
+ catch (e)
+ {}
+
+ // Update locked selection because of the normalized text nodes. (#6083, #6987)
+ var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
+ if ( lockedSelection )
+ {
+ lockedSelection.unlock();
+ lockedSelection.lock();
+ }
}
- catch (e)
- {}
savedRange = null;
}
});
- editor.window.on( 'focus', function()
+ body.on( 'focus', function()
{
// Enable selections to be saved.
- saveEnabled = true;
+ saveEnabled = 1;
saveSelection();
});
- body.on( 'beforedeactivate', function()
+ body.on( 'beforedeactivate', function( evt )
{
+ // Ignore this event if it's caused by focus switch between
+ // internal editable control type elements, e.g. layouted paragraph. (#4682)
+ if ( evt.data.$.toElement )
+ 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 )
+ {
+ 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 )
+ {
+ 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( evt )
{
- // IE context-menu event in table cells collapse
- // whatever selection is, avoiding saving this
- // 'wrong' snapshot.(#3001)
- evt = evt.data;
- if ( evt.$.button == 2 && evt.getTarget().hasAscendant( 'table' ) )
- return;
+ // 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 = true;
+ saveEnabled = 1;
setTimeout( function()
{
saveSelection( true );
@@ -165,7 +387,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
body.on( 'keyup',
function()
{
- saveEnabled = true;
+ saveEnabled = 1;
saveSelection();
});
@@ -176,7 +398,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
function disableSave()
{
- saveEnabled = false;
+ saveEnabled = 0;
}
function saveSelection( testIt )
@@ -184,7 +406,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
if ( saveEnabled )
{
var doc = editor.document,
- sel = doc && doc.$.selection;
+ sel = editor.getSelection(),
+ nativeSel = sel && sel.getNative();
// There is a very specific case, when clicking
// inside a text selection. In that case, the
@@ -194,7 +417,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
// range at the very start of the document. In
// such situation we have to test the range, to
// be sure it's valid.
- if ( testIt && sel && sel.type == 'None' )
+ if ( testIt && nativeSel && nativeSel.type == 'None' )
{
// The "InsertImage" command can be used to
// test whether the selection is good or not.
@@ -207,7 +430,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
}
}
- savedRange = sel && sel.createRange();
+ // Avoid saving selection from within text input. (#5747)
+ var parentTag;
+ if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
+ && ( parentTag = nativeSel.createRange() )
+ && ( parentTag = parentTag.parentElement() )
+ && ( parentTag = parentTag.nodeName )
+ && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
+ {
+ return;
+ }
+
+ savedRange = nativeSel && sel.getRanges()[ 0 ];
checkSelectionChangeTimeout.call( editor );
}
@@ -224,6 +458,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',
{
@@ -306,7 +543,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
return lockedSelection;
this.document = document;
- this.isLocked = false;
+ this.isLocked = 0;
this._ =
{
cache : {}
@@ -331,10 +568,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
};
var styleObjectElements =
- {
- img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
- a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
- };
+ {
+ img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
+ a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1
+ };
CKEDITOR.dom.selection.prototype =
{
@@ -440,10 +677,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 )
@@ -453,73 +702,126 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
range.collapse( start );
// 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++ )
+ var parent = range.parentElement(),
+ doc = parent.ownerDocument;
+
+ // Empty parent element, e.g. ^
+ if ( !parent.hasChildNodes() )
+ return { container : parent, offset : 0 };
+
+ var siblings = parent.children,
+ child,
+ sibling,
+ 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 )
+ 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
{
- testRange = range.duplicate();
-
- testRange.moveToElementText( child );
- testRange.collapse();
-
- var comparison = testRange.compareEndPoints( 'StartToStart', range );
-
- if ( comparison > 0 )
- break;
- else if ( comparison === 0 )
- return {
- container : parent,
- offset : i
- };
-
- testRange = null;
+ // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.
+ // e.g.
text ^
(#7433)
+ if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )
+ {
+ var bmId = 'cke_range_marker';
+ range.execCommand( 'CreateBookmark', false, bmId );
+ child = doc.getElementsByName( bmId )[ 0 ];
+ var offset = getNodeIndex( child );
+ parent.removeChild( child );
+ return { container : parent, offset : offset };
+ }
+ else
+ return { container : parent, offset : getNodeIndex( child ) };
}
}
- if ( !testRange )
+ // 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 )
{
- testRange = range.duplicate();
+ // Adapt test range to embrace the entire parent contents.
testRange.moveToElementText( parent );
- testRange.collapse( false );
- }
+ testRange.setEndPoint( 'StartToStart', range );
- 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;
+ // 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;
- while ( distance > 0 )
- distance -= siblings[ --i ].nodeValue.length;
+ siblings = parent.childNodes;
- if ( distance === 0 )
- {
- return {
- container : parent,
- offset : i
- };
+ // Actual range anchor right beside test range at the boundary of text node.
+ if ( !distance )
+ {
+ child = siblings[ siblings.length - 1 ];
+
+ if ( child.nodeType == CKEDITOR.NODE_ELEMENT )
+ return { container : parent, offset : siblings.length };
+ else
+ return { container : child, offset : child.nodeValue.length };
+ }
+
+ // Start the measuring until distance overflows, meanwhile count the text nodes.
+ var i = siblings.length;
+ while ( distance > 0 )
+ distance -= siblings[ --i ].nodeValue.length;
+
+ return { container : siblings[ i ], offset : -distance };
}
+ // Test range was one offset beyond OR behind the anchored text node.
else
{
- return {
- container : siblings[ i ],
- offset : -distance
- };
+ // Adapt one side of test range to the actual range
+ // for measuring the offset between them.
+ testRange.collapse( position > 0 ? true : false );
+ testRange.setEndPoint( position > 0 ? 'StartToStart' : '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;
+
+ // Actual range anchor right beside test range at the inner boundary of text node.
+ if ( !distance )
+ return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };
+
+ // Start the measuring until distance overflows, meanwhile count the text nodes.
+ while ( distance > 0 )
+ {
+ try
+ {
+ sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];
+ distance -= sibling.nodeValue.length;
+ child = sibling;
+ }
+ // Measurement in IE could be somtimes wrong because of