Skip to content
Permalink
Browse files

Merge branch 't/12878' into major

  • Loading branch information...
Reinmar committed Feb 24, 2015
2 parents 56e551d + 88d9937 commit b7a30ddf8444285925587df70bab8ca5039cfcb4
@@ -621,59 +621,7 @@
},

/**
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
* or a string). This method is designed to work as a user would expect the copy functionality to work.
* For instance, if the following selection has been made:
*
* <p>a<b>b{c}d</b>e</p>
*
* The following HTML will be returned:
*
* <b>c</b>
*
* As you can see, the information about the bold has been preserved, even though the selection was
* anchored inside the `<b>` element.
*
* See also:
*
* * the {@link #extractSelectedHtml} method,
* * the {@link #getHtmlFromRange} method.
*
* @since 4.5
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
* @returns {CKEDITOR.dom.documentFragment/String}
*/
getSelectedHtml: function( toString ) {
var docFragment = this.getHtmlFromRange( this.editor.getSelection().getRanges()[ 0 ] );

return toString ? docFragment.getHtml() : docFragment;
},

/**
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
* or a string) and removes the selected part of the DOM. This method is designed to work as user would
* expect the cut and delete functionalities to work.
*
* See also:
*
* * the {@link #getSelectedHtml} method,
* * the {@link #extractHtmlFromRange} method.
*
* @since 4.5
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
* @returns {CKEDITOR.dom.documentFragment/String}
*/
extractSelectedHtml: function( toString ) {
var range = this.editor.getSelection().getRanges()[ 0 ],
docFragment = this.extractHtmlFromRange( range );

this.editor.getSelection().selectRanges( [ range ] );

return toString ? docFragment.getHtml() : docFragment;
},

/**
* A base of the {@link #getSelectedHtml} method.
* A base of the {@link CKEDITOR.editor#getSelectedHtml} method.
*
* @since 4.5
* @method getHtmlFromRange
@@ -705,7 +653,7 @@
},

/**
* A base of the {@link #extractSelectedHtml} method.
* A base of the {@link CKEDITOR.editor#extractSelectedHtml} method.
*
* **Note:** The range is modified so it matches desired selection after extraction.
* But the selection is not made.
@@ -1085,6 +1085,70 @@
this.fire( 'insertElement', element );
},

/**
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
* or a string). This method is designed to work as a user would expect the copy functionality to work.
* For instance, if the following selection has been made:
*
* <p>a<b>b{c}d</b>e</p>
*
* The following HTML will be returned:
*
* <b>c</b>
*
* As you can see, the information about the bold has been preserved, even though the selection was
* anchored inside the `<b>` element.
*
* See also:
*
* * the {@link #extractSelectedHtml} method,
* * the {@link CKEDITOR.editable#getHtmlFromRange} method.
*
* @since 4.5
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
* @returns {CKEDITOR.dom.documentFragment/String}
*/
getSelectedHtml: function( toString ) {
var editable = this.editable();

if ( !editable ) {
return null;
}

var docFragment = editable.getHtmlFromRange( this.getSelection().getRanges()[ 0 ] );

return toString ? docFragment.getHtml() : docFragment;
},

/**
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
* or a string) and removes the selected part of the DOM. This method is designed to work as user would
* expect the cut and delete functionalities to work.
*
* See also:
*
* * the {@link #getSelectedHtml} method,
* * the {@link CKEDITOR.editable#extractHtmlFromRange} method.
*
* @since 4.5
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
* @returns {CKEDITOR.dom.documentFragment/String}
*/
extractSelectedHtml: function( toString ) {
var editable = this.editable();

if ( !editable ) {
return null;
}

var range = this.getSelection().getRanges()[ 0 ],
docFragment = editable.extractHtmlFromRange( range );

this.getSelection().selectRanges( [ range ] );

return toString ? docFragment.getHtml() : docFragment;
},

/**
* Moves the selection focus to the editing area space in the editor.
*/
@@ -1597,37 +1597,6 @@
return ( cache.selectedText = text );
},

/**
* Retrieves the HTML contained within the range. If selection
* contains multiple ranges method will return concatenation of HTMLs from ranges.
*
* var text = editor.getSelection().getSelectedText();
* alert( text );
*
* @since 4.4
* @returns {String} A string of HTML within the current selection.
*/
getSelectedHtml: function() {
var nativeSel = this.getNative();

if ( this.isFake ) {
return this.getSelectedElement().getHtml();
}

if ( nativeSel && nativeSel.createRange )
return nativeSel.createRange().htmlText;

if ( nativeSel.rangeCount > 0 ) {
var div = document.createElement( 'div' );

for ( var i = 0; i < nativeSel.rangeCount; i++ ) {
div.appendChild( nativeSel.getRangeAt( i ).cloneContents() );
}
return div.innerHTML;
}
return '';
},

/**
* Locks the selection made in the editor in order to make it possible to
* manipulate it without browser interference. A locked selection is
@@ -319,54 +319,6 @@
]
}, 'header' );

CKEDITOR.tools.extend( tests, {
'test getSelectedHtml': function() {
var editor = this.editors.inline;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

var frag = editor.editable().getSelectedHtml();

assert.isInstanceOf( CKEDITOR.dom.documentFragment, frag );
assert.areSame( 'ob', frag.getHtml() );
},

'test getSelectedHtml with toString option': function() {
var editor = this.editors.inline;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

assert.areSame( 'ob', editor.editable().getSelectedHtml( true ) );
},

'test extractSelectedHtml': function() {
var editor = this.editors.inline;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

// We need to precisely check if selection was set, because
// after the selected part of the DOM is extracted browser would
// make a similar selection in similar place. This way we're distinguishing who made it.
sinon.spy( CKEDITOR.dom.selection.prototype, 'selectRanges' );

var frag = editor.editable().extractSelectedHtml(),
selectionWasSetOnce = CKEDITOR.dom.selection.prototype.selectRanges.calledOnce;

CKEDITOR.dom.selection.prototype.selectRanges.restore();

assert.areSame( 'ob', frag.getHtml(), 'extracted HTML' );
assert.isTrue( selectionWasSetOnce, 'new selection has been set' );
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
},

'test extractSelectedHtml with toString option': function() {
var editor = this.editors.inline;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

assert.areSame( 'ob', editor.editable().extractSelectedHtml( true ) );
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
}
} );

bender.test( tests );

// <DEV>
@@ -0,0 +1,58 @@
/* bender-tags: editor,unit */

'use strict';

bender.editor = {
creator: 'inline',
config: {
allowedContent: true
}
};

bender.test( {
'test getSelectedHtml': function() {
var editor = this.editor;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

var frag = editor.getSelectedHtml();

assert.isInstanceOf( CKEDITOR.dom.documentFragment, frag );
assert.areSame( 'ob', frag.getHtml() );
},

'test getSelectedHtml with toString option': function() {
var editor = this.editor;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

assert.areSame( 'ob', editor.getSelectedHtml( true ) );
},

'test extractSelectedHtml': function() {
var editor = this.editor;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

// We need to precisely check if selection was set, because
// after the selected part of the DOM is extracted browser would
// make a similar selection in similar place. This way we're distinguishing who made it.
sinon.spy( CKEDITOR.dom.selection.prototype, 'selectRanges' );

var frag = editor.extractSelectedHtml(),
selectionWasSetOnce = CKEDITOR.dom.selection.prototype.selectRanges.calledOnce;

CKEDITOR.dom.selection.prototype.selectRanges.restore();

assert.areSame( 'ob', frag.getHtml(), 'extracted HTML' );
assert.isTrue( selectionWasSetOnce, 'new selection has been set' );
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
},

'test extractSelectedHtml with toString option': function() {
var editor = this.editor;
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );

assert.areSame( 'ob', editor.extractSelectedHtml( true ) );
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
}
} );

0 comments on commit b7a30dd

Please sign in to comment.
You can’t perform that action at this time.