Skip to content

Commit b7a30dd

Browse files
committed
Merge branch 't/12878' into major
2 parents 56e551d + 88d9937 commit b7a30dd

File tree

6 files changed

+124
-133
lines changed

6 files changed

+124
-133
lines changed

core/editable.js

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -621,59 +621,7 @@
621621
},
622622

623623
/**
624-
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
625-
* or a string). This method is designed to work as a user would expect the copy functionality to work.
626-
* For instance, if the following selection has been made:
627-
*
628-
* <p>a<b>b{c}d</b>e</p>
629-
*
630-
* The following HTML will be returned:
631-
*
632-
* <b>c</b>
633-
*
634-
* As you can see, the information about the bold has been preserved, even though the selection was
635-
* anchored inside the `<b>` element.
636-
*
637-
* See also:
638-
*
639-
* * the {@link #extractSelectedHtml} method,
640-
* * the {@link #getHtmlFromRange} method.
641-
*
642-
* @since 4.5
643-
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
644-
* @returns {CKEDITOR.dom.documentFragment/String}
645-
*/
646-
getSelectedHtml: function( toString ) {
647-
var docFragment = this.getHtmlFromRange( this.editor.getSelection().getRanges()[ 0 ] );
648-
649-
return toString ? docFragment.getHtml() : docFragment;
650-
},
651-
652-
/**
653-
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
654-
* or a string) and removes the selected part of the DOM. This method is designed to work as user would
655-
* expect the cut and delete functionalities to work.
656-
*
657-
* See also:
658-
*
659-
* * the {@link #getSelectedHtml} method,
660-
* * the {@link #extractHtmlFromRange} method.
661-
*
662-
* @since 4.5
663-
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
664-
* @returns {CKEDITOR.dom.documentFragment/String}
665-
*/
666-
extractSelectedHtml: function( toString ) {
667-
var range = this.editor.getSelection().getRanges()[ 0 ],
668-
docFragment = this.extractHtmlFromRange( range );
669-
670-
this.editor.getSelection().selectRanges( [ range ] );
671-
672-
return toString ? docFragment.getHtml() : docFragment;
673-
},
674-
675-
/**
676-
* A base of the {@link #getSelectedHtml} method.
624+
* A base of the {@link CKEDITOR.editor#getSelectedHtml} method.
677625
*
678626
* @since 4.5
679627
* @method getHtmlFromRange
@@ -705,7 +653,7 @@
705653
},
706654

707655
/**
708-
* A base of the {@link #extractSelectedHtml} method.
656+
* A base of the {@link CKEDITOR.editor#extractSelectedHtml} method.
709657
*
710658
* **Note:** The range is modified so it matches desired selection after extraction.
711659
* But the selection is not made.

core/editor.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,70 @@
10851085
this.fire( 'insertElement', element );
10861086
},
10871087

1088+
/**
1089+
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
1090+
* or a string). This method is designed to work as a user would expect the copy functionality to work.
1091+
* For instance, if the following selection has been made:
1092+
*
1093+
* <p>a<b>b{c}d</b>e</p>
1094+
*
1095+
* The following HTML will be returned:
1096+
*
1097+
* <b>c</b>
1098+
*
1099+
* As you can see, the information about the bold has been preserved, even though the selection was
1100+
* anchored inside the `<b>` element.
1101+
*
1102+
* See also:
1103+
*
1104+
* * the {@link #extractSelectedHtml} method,
1105+
* * the {@link CKEDITOR.editable#getHtmlFromRange} method.
1106+
*
1107+
* @since 4.5
1108+
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
1109+
* @returns {CKEDITOR.dom.documentFragment/String}
1110+
*/
1111+
getSelectedHtml: function( toString ) {
1112+
var editable = this.editable();
1113+
1114+
if ( !editable ) {
1115+
return null;
1116+
}
1117+
1118+
var docFragment = editable.getHtmlFromRange( this.getSelection().getRanges()[ 0 ] );
1119+
1120+
return toString ? docFragment.getHtml() : docFragment;
1121+
},
1122+
1123+
/**
1124+
* Gets the selected HTML (it is returned as a {@link CKEDITOR.dom.documentFragment document fragment}
1125+
* or a string) and removes the selected part of the DOM. This method is designed to work as user would
1126+
* expect the cut and delete functionalities to work.
1127+
*
1128+
* See also:
1129+
*
1130+
* * the {@link #getSelectedHtml} method,
1131+
* * the {@link CKEDITOR.editable#extractHtmlFromRange} method.
1132+
*
1133+
* @since 4.5
1134+
* @param {Boolean} [toString] If `true`, then a stringified HTML will be returned.
1135+
* @returns {CKEDITOR.dom.documentFragment/String}
1136+
*/
1137+
extractSelectedHtml: function( toString ) {
1138+
var editable = this.editable();
1139+
1140+
if ( !editable ) {
1141+
return null;
1142+
}
1143+
1144+
var range = this.getSelection().getRanges()[ 0 ],
1145+
docFragment = editable.extractHtmlFromRange( range );
1146+
1147+
this.getSelection().selectRanges( [ range ] );
1148+
1149+
return toString ? docFragment.getHtml() : docFragment;
1150+
},
1151+
10881152
/**
10891153
* Moves the selection focus to the editing area space in the editor.
10901154
*/

core/selection.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,37 +1597,6 @@
15971597
return ( cache.selectedText = text );
15981598
},
15991599

1600-
/**
1601-
* Retrieves the HTML contained within the range. If selection
1602-
* contains multiple ranges method will return concatenation of HTMLs from ranges.
1603-
*
1604-
* var text = editor.getSelection().getSelectedText();
1605-
* alert( text );
1606-
*
1607-
* @since 4.4
1608-
* @returns {String} A string of HTML within the current selection.
1609-
*/
1610-
getSelectedHtml: function() {
1611-
var nativeSel = this.getNative();
1612-
1613-
if ( this.isFake ) {
1614-
return this.getSelectedElement().getHtml();
1615-
}
1616-
1617-
if ( nativeSel && nativeSel.createRange )
1618-
return nativeSel.createRange().htmlText;
1619-
1620-
if ( nativeSel.rangeCount > 0 ) {
1621-
var div = document.createElement( 'div' );
1622-
1623-
for ( var i = 0; i < nativeSel.rangeCount; i++ ) {
1624-
div.appendChild( nativeSel.getRangeAt( i ).cloneContents() );
1625-
}
1626-
return div.innerHTML;
1627-
}
1628-
return '';
1629-
},
1630-
16311600
/**
16321601
* Locks the selection made in the editor in order to make it possible to
16331602
* manipulate it without browser interference. A locked selection is

tests/core/editable/getextractselectedhtml.js renamed to tests/core/editable/getextracthtmlfromrange.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -319,54 +319,6 @@
319319
]
320320
}, 'header' );
321321

322-
CKEDITOR.tools.extend( tests, {
323-
'test getSelectedHtml': function() {
324-
var editor = this.editors.inline;
325-
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
326-
327-
var frag = editor.editable().getSelectedHtml();
328-
329-
assert.isInstanceOf( CKEDITOR.dom.documentFragment, frag );
330-
assert.areSame( 'ob', frag.getHtml() );
331-
},
332-
333-
'test getSelectedHtml with toString option': function() {
334-
var editor = this.editors.inline;
335-
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
336-
337-
assert.areSame( 'ob', editor.editable().getSelectedHtml( true ) );
338-
},
339-
340-
'test extractSelectedHtml': function() {
341-
var editor = this.editors.inline;
342-
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
343-
344-
// We need to precisely check if selection was set, because
345-
// after the selected part of the DOM is extracted browser would
346-
// make a similar selection in similar place. This way we're distinguishing who made it.
347-
sinon.spy( CKEDITOR.dom.selection.prototype, 'selectRanges' );
348-
349-
var frag = editor.editable().extractSelectedHtml(),
350-
selectionWasSetOnce = CKEDITOR.dom.selection.prototype.selectRanges.calledOnce;
351-
352-
CKEDITOR.dom.selection.prototype.selectRanges.restore();
353-
354-
assert.areSame( 'ob', frag.getHtml(), 'extracted HTML' );
355-
assert.isTrue( selectionWasSetOnce, 'new selection has been set' );
356-
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
357-
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
358-
},
359-
360-
'test extractSelectedHtml with toString option': function() {
361-
var editor = this.editors.inline;
362-
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
363-
364-
assert.areSame( 'ob', editor.editable().extractSelectedHtml( true ) );
365-
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
366-
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
367-
}
368-
} );
369-
370322
bender.test( tests );
371323

372324
// <DEV>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* bender-tags: editor,unit */
2+
3+
'use strict';
4+
5+
bender.editor = {
6+
creator: 'inline',
7+
config: {
8+
allowedContent: true
9+
}
10+
};
11+
12+
bender.test( {
13+
'test getSelectedHtml': function() {
14+
var editor = this.editor;
15+
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
16+
17+
var frag = editor.getSelectedHtml();
18+
19+
assert.isInstanceOf( CKEDITOR.dom.documentFragment, frag );
20+
assert.areSame( 'ob', frag.getHtml() );
21+
},
22+
23+
'test getSelectedHtml with toString option': function() {
24+
var editor = this.editor;
25+
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
26+
27+
assert.areSame( 'ob', editor.getSelectedHtml( true ) );
28+
},
29+
30+
'test extractSelectedHtml': function() {
31+
var editor = this.editor;
32+
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
33+
34+
// We need to precisely check if selection was set, because
35+
// after the selected part of the DOM is extracted browser would
36+
// make a similar selection in similar place. This way we're distinguishing who made it.
37+
sinon.spy( CKEDITOR.dom.selection.prototype, 'selectRanges' );
38+
39+
var frag = editor.extractSelectedHtml(),
40+
selectionWasSetOnce = CKEDITOR.dom.selection.prototype.selectRanges.calledOnce;
41+
42+
CKEDITOR.dom.selection.prototype.selectRanges.restore();
43+
44+
assert.areSame( 'ob', frag.getHtml(), 'extracted HTML' );
45+
assert.isTrue( selectionWasSetOnce, 'new selection has been set' );
46+
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
47+
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
48+
},
49+
50+
'test extractSelectedHtml with toString option': function() {
51+
var editor = this.editor;
52+
bender.tools.selection.setWithHtml( editor, '<p>fo{ob}ar</p>' );
53+
54+
assert.areSame( 'ob', editor.extractSelectedHtml( true ) );
55+
assert.isInnerHtmlMatching( '<p>fo^ar@</p>', bender.tools.selection.getWithHtml( editor ),
56+
{ compareSelection: true, normalizeSelection: true }, 'contents of the editor' );
57+
}
58+
} );

0 commit comments

Comments
 (0)