+ var selection = this.getSelection(),\r
+ mode = selection.getStartElement().hasAscendant( 'pre', true ) ?\r
+ CKEDITOR.ENTER_BR : this.config.enterMode,\r
+ isEnterBrMode = mode == CKEDITOR.ENTER_BR;\r
+\r
+ var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );\r
+\r
+ // Convert leading and trailing whitespaces into \r
+ html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )\r
+ {\r
+ if ( match.length == 1 ) // one space, preserve it\r
+ return ' ';\r
+ else if ( !offset ) // beginning of block\r
+ return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';\r
+ else // end of block\r
+ return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );\r
+ } );\r
+\r
+ // Convert subsequent whitespaces into \r
+ html = html.replace( /[ \t]{2,}/g, function ( match )\r
+ {\r
+ return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';\r
+ } );\r
+\r
+ var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';\r
+\r
+ // Two line-breaks create one paragraph.\r
+ if ( !isEnterBrMode )\r
+ {\r
+ html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,\r
+ function( match, group1, text )\r
+ {\r
+ return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';\r
+ });\r
+ }\r
+\r
+ // One <br> per line-break.\r
+ html = html.replace( /\n/g, '<br>' );\r
+\r
+ // Compensate padding <br> for non-IE.\r
+ if ( !( isEnterBrMode || CKEDITOR.env.ie ) )\r
+ {\r
+ html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )\r
+ {\r
+ return CKEDITOR.tools.repeat( match, 2 );\r
+ } );\r
+ }\r
+\r
+ // Inline styles have to be inherited in Firefox.\r
+ if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )\r