|
375 | 375 | return text.replace( quoteEscRegex, '"' ).replace( ltEscRegex, '<' ).replace( gtEscRegex, '>' );
|
376 | 376 | },
|
377 | 377 |
|
| 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 ->   x 4; |
| 392 | + html = html.replace( /\t/g, ' ' ); |
| 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, ' ' ); |
| 418 | + |
| 419 | + // Finally, preserve whitespaces that are to be lost. |
| 420 | + html = html.replace( /(>|\s) /g, function( match, before ) { |
| 421 | + return before + ' '; |
| 422 | + } ).replace( / (?=<)/g, ' ' ); |
| 423 | + |
| 424 | + return html; |
| 425 | + }, |
| 426 | + |
378 | 427 | /**
|
379 | 428 | * Gets a unique number for this CKEDITOR execution session. It returns
|
380 | 429 | * consecutive numbers starting from 1.
|
|
405 | 454 | return 'cke_' + this.getNextNumber();
|
406 | 455 | },
|
407 | 456 |
|
| 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 | + |
408 | 472 | /**
|
409 | 473 | * Creates a function override.
|
410 | 474 | *
|
|
0 commit comments