Skip to content

Commit

Permalink
Merge branch 't/12168' into major
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Jasiun committed Jul 28, 2014
2 parents 291fb3d + 026a13e commit cb28e9c
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 167 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -6,6 +6,9 @@ CKEditor 4 Changelog
New Features:

* [#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.
* [#11437](http://dev.ckeditor.com/ticket/11437): Drag and Drop support.
* [#11460](http://dev.ckeditor.com/ticket/11460): Custom handling for dropped content in the editor.
* [#12168](http://dev.ckeditor.com/ticket/12168): `dataTransfer` facade.

Fixed Issues:

Expand Down
56 changes: 16 additions & 40 deletions core/editable.js
Expand Up @@ -245,50 +245,26 @@
insertText: function( text ) {
beforeInsert( this );

var editor = this.editor,
mode = editor.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : editor.activeEnterMode,
isEnterBrMode = mode == CKEDITOR.ENTER_BR,
tools = CKEDITOR.tools;

// CRLF -> LF
var html = tools.htmlEncode( text.replace( /\r\n/g, '\n' ) );

// Tab -> &nbsp x 4;
html = html.replace( /\t/g, '    ' );

var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';

// Two line-breaks create one paragraphing block.
if ( !isEnterBrMode ) {
var duoLF = /\n{2}/g;
if ( duoLF.test( html ) )
{
var openTag = '<' + paragraphTag + '>', endTag = '</' + paragraphTag + '>';
html = openTag + html.replace( duoLF, function() { return endTag + openTag; } ) + endTag;
}
}

// One <br> per line-break.
html = html.replace( /\n/g, '<br>' );

// Compensate padding <br> at the end of block, avoid loosing them during insertion.
if ( !isEnterBrMode ) {
html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match ) {
return tools.repeat( match, 2 );
} );
}

// Preserve spaces at the ends, so they won't be lost after insertion (merged with adjacent ones).
html = html.replace( /^ | $/g, '&nbsp;' );
insert( this, 'text', this.transformPlainTextToHtml( text ) );
},

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

insert( this, 'text', html );
return CKEDITOR.tools.transformPlainTextToHtml( text, enterMode );
},


/**
* @see CKEDITOR.editor#insertElement
*/
Expand Down
64 changes: 64 additions & 0 deletions core/tools.js
Expand Up @@ -375,6 +375,55 @@
return text.replace( quoteEscRegex, '"' ).replace( ltEscRegex, '<' ).replace( gtEscRegex, '>' );
},

/**
* Transforms text to the valid HTML: creates paragraphs, replaces tabs with no breaking spaces etc..
*
* @since 4.5
* @param {String} text Text to transform.
* @param {Number} enterMode Editors {@link CKEDITOR.config#enterMode enter mode}.
* @returns {String} HTML generated from the text.
*/
transformPlainTextToHtml: function( text, enterMode ) {
var isEnterBrMode = enterMode == CKEDITOR.ENTER_BR,
// CRLF -> LF
html = this.htmlEncode( text.replace( /\r\n/g, '\n' ) );

// Tab -> &nbsp x 4;
html = html.replace( /\t/g, '&nbsp;&nbsp; &nbsp;' );

var paragraphTag = enterMode == CKEDITOR.ENTER_P ? 'p' : 'div';

// Two line-breaks create one paragraphing block.
if ( !isEnterBrMode ) {
var duoLF = /\n{2}/g;
if ( duoLF.test( html ) )
{
var openTag = '<' + paragraphTag + '>', endTag = '</' + paragraphTag + '>';
html = openTag + html.replace( duoLF, function() { return endTag + openTag; } ) + endTag;
}
}

// One <br> per line-break.
html = html.replace( /\n/g, '<br>' );

// Compensate padding <br> at the end of block, avoid loosing them during insertion.
if ( !isEnterBrMode ) {
html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match ) {
return CKEDITOR.tools.repeat( match, 2 );
} );
}

// Preserve spaces at the ends, so they won't be lost after insertion (merged with adjacent ones).
html = html.replace( /^ | $/g, '&nbsp;' );

// Finally, preserve whitespaces that are to be lost.
html = html.replace( /(>|\s) /g, function( match, before ) {
return before + '&nbsp;';
} ).replace( / (?=<)/g, '&nbsp;' );

return html;
},

/**
* Gets a unique number for this CKEDITOR execution session. It returns
* consecutive numbers starting from 1.
Expand Down Expand Up @@ -405,6 +454,21 @@
return 'cke_' + this.getNextNumber();
},

/**
* Gets a universally unique ID. It returns a random string
* up to ISO/IEC 11578:1996, without dashes, with the "e" prefix to
* make sure that ID does not starts with number.
*
* @returns {String} A global unique ID.
*/
getUniqueId: function() {
var uuid = 'e'; // Make sure that id does not start with number.
for ( var i = 0; i < 8; i++ ) {
uuid += Math.floor( ( 1 + Math.random() ) * 0x10000 ).toString( 16 ).substring( 1 );
}
return uuid;
},

/**
* Creates a function override.
*
Expand Down

0 comments on commit cb28e9c

Please sign in to comment.