X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fselection%2Fplugin.js;h=3d707fc99ed284ad564f0e5475ec6480595fb59a;hb=055b6b0792ce7dc53d47af606b367c04b927c2ab;hp=04be63171d0a106aeacbb2888e7f5b8277a498e7;hpb=8665a7c6c60586526e32e8941fe2896739b6ebfb;p=ckeditor.git diff --git a/_source/plugins/selection/plugin.js b/_source/plugins/selection/plugin.js index 04be631..3d707fc 100644 --- a/_source/plugins/selection/plugin.js +++ b/_source/plugins/selection/plugin.js @@ -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(); @@ -105,7 +105,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 +116,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 +133,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // 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) + {} } - catch (e) - {} savedRange = null; } @@ -160,21 +165,55 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Disable selections from being saved. saveEnabled = false; + 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 ) { - editor.document.$.selection.empty(); + editor.document && editor.document.$.selection.empty(); + }); + } + + // 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(); }); } // 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 () + { + disableSave(); + }); + body.on( 'mouseup', function() { @@ -235,9 +274,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; } @@ -475,6 +516,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return ( cache.type = type ); }, + /** + * 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 : CKEDITOR.env.ie ? ( function() @@ -836,6 +886,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this._.cache = {}; }, + /** + * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element. + * @param element + */ selectElement : function( element ) { if ( this.isLocked ) @@ -892,6 +946,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }, + /** + * Adding the specified ranges to document selection preceding + * by clearing up the original selection. + * @param {CKEDITOR.dom.range} ranges + */ selectRanges : function( ranges ) { if ( this.isLocked ) @@ -946,6 +1005,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }, + /** + * Create bookmark for every single of this selection range (from #getRanges) + * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method, + * with extra cares to avoid interferon among those ranges. Same arguments are + * received as with the underlay range method. + */ createBookmarks : function( serializable ) { var retval = [], @@ -978,6 +1043,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return retval; }, + /** + * Create bookmark for every single of this selection range (from #getRanges) + * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method, + * with extra cares to avoid interferon among those ranges. Same arguments are + * received as with the underlay range method. + */ createBookmarks2 : function( normalized ) { var bookmarks = [], @@ -989,6 +1060,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return bookmarks; }, + /** + * Select the virtual ranges denote by the bookmarks by calling #selectRanges. + * @param bookmarks + */ selectBookmarks : function( bookmarks ) { var ranges = []; @@ -1002,6 +1077,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return this; }, + /** + * Retrieve the common ancestor node of the first range and the last range. + */ getCommonAncestor : function() { var ranges = this.getRanges(), @@ -1010,7 +1088,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return startNode.getCommonAncestor( endNode ); }, - // Moving scroll bar to the current selection's start position. + /** + * Moving scroll bar to the current selection's start position. + */ scrollIntoView : function() { // If we have split the block, adds a temporary span at the @@ -1020,150 +1100,151 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }; })(); + ( function() { -var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ); -var fillerTextRegex = /\ufeff|\u00a0/; -var nonCells = { table:1,tbody:1,tr:1 }; - -CKEDITOR.dom.range.prototype.select = - CKEDITOR.env.ie ? - // V2 - function( forceExpand ) - { - var collapsed = this.collapsed; - var isStartMarkerAlone; - var dummySpan; - - // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g. - // [... =>
cell
... - if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells - || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells ) + var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ), + fillerTextRegex = /\ufeff|\u00a0/, + nonCells = { table:1,tbody:1,tr:1 }; + + CKEDITOR.dom.range.prototype.select = + CKEDITOR.env.ie ? + // V2 + function( forceExpand ) { - this.shrink( CKEDITOR.NODE_ELEMENT, true ); - } + var collapsed = this.collapsed; + var isStartMarkerAlone; + var dummySpan; + + // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g. + //
[cell
[... =>
cell
... + if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells + || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells ) + { + this.shrink( CKEDITOR.NODE_ELEMENT, true ); + } - var bookmark = this.createBookmark(); + var bookmark = this.createBookmark(); - // Create marker tags for the start and end boundaries. - var startNode = bookmark.startNode; + // Create marker tags for the start and end boundaries. + var startNode = bookmark.startNode; - var endNode; - if ( !collapsed ) - endNode = bookmark.endNode; + var endNode; + if ( !collapsed ) + endNode = bookmark.endNode; - // Create the main range which will be used for the selection. - var ieRange = this.document.$.body.createTextRange(); + // Create the main range which will be used for the selection. + var ieRange = this.document.$.body.createTextRange(); - // Position the range at the start boundary. - ieRange.moveToElementText( startNode.$ ); - ieRange.moveStart( 'character', 1 ); + // Position the range at the start boundary. + ieRange.moveToElementText( startNode.$ ); + ieRange.moveStart( 'character', 1 ); - if ( endNode ) - { - // Create a tool range for the end. - var ieRangeEnd = this.document.$.body.createTextRange(); + if ( endNode ) + { + // Create a tool range for the end. + var ieRangeEnd = this.document.$.body.createTextRange(); - // Position the tool range at the end. - ieRangeEnd.moveToElementText( endNode.$ ); + // Position the tool range at the end. + ieRangeEnd.moveToElementText( endNode.$ ); - // Move the end boundary of the main range to match the tool range. - ieRange.setEndPoint( 'EndToEnd', ieRangeEnd ); - ieRange.moveEnd( 'character', -1 ); - } - else - { - // The isStartMarkerAlone logic comes from V2. It guarantees that the lines - // will expand and that the cursor will be blinking on the right place. - // Actually, we are using this flag just to avoid using this hack in all - // situations, but just on those needed. - var next = startNode.getNext( notWhitespaces ); - isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there? - && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) ); - - // Append a temporary  before the selection. - // This is needed to avoid IE destroying selections inside empty - // inline elements, like (#253). - // It is also needed when placing the selection right after an inline - // element to avoid the selection moving inside of it. - dummySpan = this.document.createElement( 'span' ); - dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359. - dummySpan.insertBefore( startNode ); - - if ( isStartMarkerAlone ) + // Move the end boundary of the main range to match the tool range. + ieRange.setEndPoint( 'EndToEnd', ieRangeEnd ); + ieRange.moveEnd( 'character', -1 ); + } + else { - // To expand empty blocks or line spaces after
, we need - // instead to have any char, which will be later deleted using the - // selection. - // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359) - this.document.createText( '\ufeff' ).insertBefore( startNode ); + // The isStartMarkerAlone logic comes from V2. It guarantees that the lines + // will expand and that the cursor will be blinking on the right place. + // Actually, we are using this flag just to avoid using this hack in all + // situations, but just on those needed. + var next = startNode.getNext( notWhitespaces ); + isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there? + && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) ); + + // Append a temporary  before the selection. + // This is needed to avoid IE destroying selections inside empty + // inline elements, like (#253). + // It is also needed when placing the selection right after an inline + // element to avoid the selection moving inside of it. + dummySpan = this.document.createElement( 'span' ); + dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359. + dummySpan.insertBefore( startNode ); + + if ( isStartMarkerAlone ) + { + // To expand empty blocks or line spaces after
, we need + // instead to have any char, which will be later deleted using the + // selection. + // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359) + this.document.createText( '\ufeff' ).insertBefore( startNode ); + } } - } - // Remove the markers (reset the position, because of the changes in the DOM tree). - this.setStartBefore( startNode ); - startNode.remove(); + // Remove the markers (reset the position, because of the changes in the DOM tree). + this.setStartBefore( startNode ); + startNode.remove(); - if ( collapsed ) - { - if ( isStartMarkerAlone ) + if ( collapsed ) { - // Move the selection start to include the temporary \ufeff. - ieRange.moveStart( 'character', -1 ); + if ( isStartMarkerAlone ) + { + // Move the selection start to include the temporary \ufeff. + ieRange.moveStart( 'character', -1 ); - ieRange.select(); + ieRange.select(); + + // Remove our temporary stuff. + this.document.$.selection.clear(); + } + else + ieRange.select(); - // Remove our temporary stuff. - this.document.$.selection.clear(); + this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START ); + dummySpan.remove(); } else + { + this.setEndBefore( endNode ); + endNode.remove(); ieRange.select(); + } - this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START ); - dummySpan.remove(); + this.document.fire( 'selectionchange' ); } - else + : + function() { - this.setEndBefore( endNode ); - endNode.remove(); - ieRange.select(); - } - - this.document.fire( 'selectionchange' ); - } - : - function() - { - var startContainer = this.startContainer; + var startContainer = this.startContainer; - // If we have a collapsed range, inside an empty element, we must add - // something to it, otherwise the caret will not be visible. - if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() ) - startContainer.append( new CKEDITOR.dom.text( '' ) ); + // If we have a collapsed range, inside an empty element, we must add + // something to it, otherwise the caret will not be visible. + if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() ) + startContainer.append( new CKEDITOR.dom.text( '' ) ); - var nativeRange = this.document.$.createRange(); - nativeRange.setStart( startContainer.$, this.startOffset ); + var nativeRange = this.document.$.createRange(); + nativeRange.setStart( startContainer.$, this.startOffset ); - try - { - nativeRange.setEnd( this.endContainer.$, this.endOffset ); - } - catch ( e ) - { - // There is a bug in Firefox implementation (it would be too easy - // otherwise). The new start can't be after the end (W3C says it can). - // So, let's create a new range and collapse it to the desired point. - if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 ) + try { - this.collapse( true ); nativeRange.setEnd( this.endContainer.$, this.endOffset ); } - else - throw( e ); - } + catch ( e ) + { + // There is a bug in Firefox implementation (it would be too easy + // otherwise). The new start can't be after the end (W3C says it can). + // So, let's create a new range and collapse it to the desired point. + if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 ) + { + this.collapse( true ); + nativeRange.setEnd( this.endContainer.$, this.endOffset ); + } + else + throw( e ); + } - var selection = this.document.getSelection().getNative(); - selection.removeAllRanges(); - selection.addRange( nativeRange ); - }; + var selection = this.document.getSelection().getNative(); + selection.removeAllRanges(); + selection.addRange( nativeRange ); + }; } )();
[cell