Skip to content

Commit

Permalink
Merge branch 't/13118' into major
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Apr 22, 2015
2 parents 2ad12d9 + b8660de commit fbfd310
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -3,6 +3,10 @@ CKEditor 4 Changelog

## CKEditor 4.5

Fixed Issues:

* [#13118](http://dev.ckeditor.com/ticket/13118): Fixed: The [`editor.getSelectedHtml()`](http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-getSelectedHtml) method throws error when called in the source mode.

## CKEditor 4.5 Beta

New Features:
Expand Down
5 changes: 3 additions & 2 deletions core/editor.js
Expand Up @@ -1112,9 +1112,10 @@
*/
getSelectedHtml: function( toString ) {
var editable = this.editable(),
ranges = this.getSelection().getRanges();
selection = this.getSelection(),
ranges = selection && selection.getRanges();

if ( !editable || ranges.length === 0 ) {
if ( !editable || !ranges || ranges.length === 0 ) {
return null;
}

Expand Down
51 changes: 39 additions & 12 deletions tests/core/editor/getextractselectedhtml.js
Expand Up @@ -2,10 +2,20 @@

'use strict';

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

editor2: {
name: 'editor2',
config: {
extraPlugins: 'sourcearea'
}
}
};

Expand All @@ -20,8 +30,25 @@ bender.test( {
}
},

'test getSelectedHtml in source mode (#13118)': function() {
var editor = this.editors.editor2;

editor.setMode( 'source', function() {
resume( function() {
editor.getSelectedHtml();
assert.isTrue( true, 'So far so good. Method "getSelectedHtml" shouldn\'t throw an error in a "source" mode.' );

// Clean up after the test.
editor.setMode( 'wysiwyg', resume );
wait();
} );
} );

wait();
},

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

var frag = editor.getSelectedHtml();
Expand All @@ -31,7 +58,7 @@ bender.test( {
},

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

assert.areSame( 'ob', editor.getSelectedHtml( true ) );
Expand All @@ -46,14 +73,14 @@ bender.test( {
sinon.stub( CKEDITOR.dom.selection.prototype, 'getRanges' ).returns( [] );
stubs.push( CKEDITOR.dom.selection.prototype.getRanges );

var editor = this.editor,
var editor = this.editors.editor,
selectedHtml = editor.getSelectedHtml();

assert.isNull( selectedHtml, 'There should be no error but null should be returns if selection contains no ranges' );
},

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

// We need to precisely check if selection was set, because
Expand All @@ -73,7 +100,7 @@ bender.test( {
},

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

assert.areSame( 'ob', editor.extractSelectedHtml( true ) );
Expand All @@ -85,14 +112,14 @@ bender.test( {
sinon.stub( CKEDITOR.dom.selection.prototype, 'getRanges' ).returns( [] );
stubs.push( CKEDITOR.dom.selection.prototype.getRanges );

var editor = this.editor,
var editor = this.editors.editor,
selectedHtml = editor.getSelectedHtml();

assert.isNull( selectedHtml, 'There should be no error but null should be returns if selection contains no ranges' );
},

'test extractSelectedHtml with removeEmptyBlock': function() {
var editor = this.editor;
var editor = this.editors.editor;
bender.tools.selection.setWithHtml( editor, '<p>{foo}</p><p>bar</p>' );

assert.areSame( 'foo', editor.extractSelectedHtml( true, true ) );
Expand All @@ -108,4 +135,4 @@ bender.test( {
assert.isTrue( html == null || html === '', 'returned value should be null if selection does not exist or should be an empty string' );
} );
}
} );
} );

0 comments on commit fbfd310

Please sign in to comment.