Skip to content

Commit cb28e9c

Browse files
author
Piotr Jasiun
committed
Merge branch 't/12168' into major
2 parents 291fb3d + 026a13e commit cb28e9c

File tree

7 files changed

+529
-167
lines changed

7 files changed

+529
-167
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ CKEditor 4 Changelog
66
New Features:
77

88
* [#10931](http://dev.ckeditor.com/ticket/10931): Introduce ability to insert widgets into another widget's nested editables. Note that unless nested editable's [allowed content](http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.widget.nestedEditable.definition-property-allowedContent) is defined precisely since CKEditor 4.5 some widget buttons may become enabled.
9+
* [#11437](http://dev.ckeditor.com/ticket/11437): Drag and Drop support.
10+
* [#11460](http://dev.ckeditor.com/ticket/11460): Custom handling for dropped content in the editor.
11+
* [#12168](http://dev.ckeditor.com/ticket/12168): `dataTransfer` facade.
912

1013
Fixed Issues:
1114

core/editable.js

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -245,50 +245,26 @@
245245
insertText: function( text ) {
246246
beforeInsert( this );
247247

248-
var editor = this.editor,
249-
mode = editor.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : editor.activeEnterMode,
250-
isEnterBrMode = mode == CKEDITOR.ENTER_BR,
251-
tools = CKEDITOR.tools;
252-
253-
// CRLF -> LF
254-
var html = tools.htmlEncode( text.replace( /\r\n/g, '\n' ) );
255-
256-
// Tab -> &nbsp x 4;
257-
html = html.replace( /\t/g, '    ' );
258-
259-
var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
260-
261-
// Two line-breaks create one paragraphing block.
262-
if ( !isEnterBrMode ) {
263-
var duoLF = /\n{2}/g;
264-
if ( duoLF.test( html ) )
265-
{
266-
var openTag = '<' + paragraphTag + '>', endTag = '</' + paragraphTag + '>';
267-
html = openTag + html.replace( duoLF, function() { return endTag + openTag; } ) + endTag;
268-
}
269-
}
270-
271-
// One <br> per line-break.
272-
html = html.replace( /\n/g, '<br>' );
273-
274-
// Compensate padding <br> at the end of block, avoid loosing them during insertion.
275-
if ( !isEnterBrMode ) {
276-
html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match ) {
277-
return tools.repeat( match, 2 );
278-
} );
279-
}
280-
281-
// Preserve spaces at the ends, so they won't be lost after insertion (merged with adjacent ones).
282-
html = html.replace( /^ | $/g, '&nbsp;' );
248+
insert( this, 'text', this.transformPlainTextToHtml( text ) );
249+
},
283250

284-
// Finally, preserve whitespaces that are to be lost.
285-
html = html.replace( /(>|\s) /g, function( match, before ) {
286-
return before + '&nbsp;';
287-
} ).replace( / (?=<)/g, '&nbsp;' );
251+
/**
252+
* Set enterMode based on current selection and {@link CKEDITOR.editor#activeEnterMode}
253+
* and call {@link CKEDITOR.tools#transformPlainTextToHtml}.
254+
*
255+
* @since 4.5
256+
* @param {String} text Text to transform.
257+
* @returns {String} HTML generated from the text.
258+
*/
259+
transformPlainTextToHtml: function( text ) {
260+
var enterMode = this.editor.getSelection().getStartElement().hasAscendant( 'pre', true ) ?
261+
CKEDITOR.ENTER_BR :
262+
this.editor.activeEnterMode;
288263

289-
insert( this, 'text', html );
264+
return CKEDITOR.tools.transformPlainTextToHtml( text, enterMode );
290265
},
291266

267+
292268
/**
293269
* @see CKEDITOR.editor#insertElement
294270
*/

core/tools.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,55 @@
375375
return text.replace( quoteEscRegex, '"' ).replace( ltEscRegex, '<' ).replace( gtEscRegex, '>' );
376376
},
377377

378+
/**
379+
* Transforms text to the valid HTML: creates paragraphs, replaces tabs with no breaking spaces etc..
380+
*
381+
* @since 4.5
382+
* @param {String} text Text to transform.
383+
* @param {Number} enterMode Editors {@link CKEDITOR.config#enterMode enter mode}.
384+
* @returns {String} HTML generated from the text.
385+
*/
386+
transformPlainTextToHtml: function( text, enterMode ) {
387+
var isEnterBrMode = enterMode == CKEDITOR.ENTER_BR,
388+
// CRLF -> LF
389+
html = this.htmlEncode( text.replace( /\r\n/g, '\n' ) );
390+
391+
// Tab -> &nbsp x 4;
392+
html = html.replace( /\t/g, '&nbsp;&nbsp; &nbsp;' );
393+
394+
var paragraphTag = enterMode == CKEDITOR.ENTER_P ? 'p' : 'div';
395+
396+
// Two line-breaks create one paragraphing block.
397+
if ( !isEnterBrMode ) {
398+
var duoLF = /\n{2}/g;
399+
if ( duoLF.test( html ) )
400+
{
401+
var openTag = '<' + paragraphTag + '>', endTag = '</' + paragraphTag + '>';
402+
html = openTag + html.replace( duoLF, function() { return endTag + openTag; } ) + endTag;
403+
}
404+
}
405+
406+
// One <br> per line-break.
407+
html = html.replace( /\n/g, '<br>' );
408+
409+
// Compensate padding <br> at the end of block, avoid loosing them during insertion.
410+
if ( !isEnterBrMode ) {
411+
html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match ) {
412+
return CKEDITOR.tools.repeat( match, 2 );
413+
} );
414+
}
415+
416+
// Preserve spaces at the ends, so they won't be lost after insertion (merged with adjacent ones).
417+
html = html.replace( /^ | $/g, '&nbsp;' );
418+
419+
// Finally, preserve whitespaces that are to be lost.
420+
html = html.replace( /(>|\s) /g, function( match, before ) {
421+
return before + '&nbsp;';
422+
} ).replace( / (?=<)/g, '&nbsp;' );
423+
424+
return html;
425+
},
426+
378427
/**
379428
* Gets a unique number for this CKEDITOR execution session. It returns
380429
* consecutive numbers starting from 1.
@@ -405,6 +454,21 @@
405454
return 'cke_' + this.getNextNumber();
406455
},
407456

457+
/**
458+
* Gets a universally unique ID. It returns a random string
459+
* up to ISO/IEC 11578:1996, without dashes, with the "e" prefix to
460+
* make sure that ID does not starts with number.
461+
*
462+
* @returns {String} A global unique ID.
463+
*/
464+
getUniqueId: function() {
465+
var uuid = 'e'; // Make sure that id does not start with number.
466+
for ( var i = 0; i < 8; i++ ) {
467+
uuid += Math.floor( ( 1 + Math.random() ) * 0x10000 ).toString( 16 ).substring( 1 );
468+
}
469+
return uuid;
470+
},
471+
408472
/**
409473
* Creates a function override.
410474
*

0 commit comments

Comments
 (0)