X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2Fpackagist%2Fpiedsjaloux-ckeditor-component.git;a=blobdiff_plain;f=sources%2Fcore%2Ftemplate.js;fp=sources%2Fcore%2Ftemplate.js;h=a627c34d2281ebb494d105af710048240e24805f;hp=a3fe55b54215ff43108e12abc64d52745dbfd10c;hb=317f8f8f0651488f226b5280a8f036c7c135c639;hpb=1096cdefb1c9a3f3c4ca6807e272da6c92e5ed9c diff --git a/sources/core/template.js b/sources/core/template.js index a3fe55b..a627c34 100644 --- a/sources/core/template.js +++ b/sources/core/template.js @@ -1,5 +1,5 @@ /** - * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. + * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or http://ckeditor.com/license */ @@ -9,11 +9,7 @@ */ ( function() { - var cache = {}, - rePlaceholder = /{([^}]+)}/g, - reEscapableChars = /([\\'])/g, - reNewLine = /\n/g, - reCarriageReturn = /\r/g; + var rePlaceholder = /{([^}]+)}/g; /** * Lightweight template used to build the output string from variables. @@ -27,42 +23,35 @@ * @param {String} source The template source. */ CKEDITOR.template = function( source ) { - // Builds an optimized function body for the output() method, focused on performance. - // For example, if we have this "source": - // '
{editorName}
' - // ... the resulting function body will be (apart from the "buffer" handling): - // return [ '
', data['editorName'] == undefined ? '{editorName}' : data['editorName'], '
' ].join(''); + /** + * The current template source. + * + * @readonly + * @member CKEDITOR.template + * @property {String} + */ + this.source = String( source ); + }; - // Try to read from the cache. - if ( cache[ source ] ) - this.output = cache[ source ]; - else { - var fn = source - // Escape chars like slash "\" or single quote "'". - .replace( reEscapableChars, '\\$1' ) - .replace( reNewLine, '\\n' ) - .replace( reCarriageReturn, '\\r' ) - // Inject the template keys replacement. - .replace( rePlaceholder, function( m, key ) { - return "',data['" + key + "']==undefined?'{" + key + "}':data['" + key + "'],'"; - } ); + /** + * Processes the template, filling its variables with the provided data. + * + * @method + * @member CKEDITOR.template + * @param {Object} data An object containing properties whose values will be + * used to fill the template variables. The property names must match the + * template variables names. Variables without matching properties will be + * kept untouched. + * @param {Array} [buffer] An array that the output data will be pushed into. + * The number of entries appended to the array is unknown. + * @returns {String/Number} If `buffer` has not been provided, the processed + * template output data; otherwise the new length of `buffer`. + */ + CKEDITOR.template.prototype.output = function( data, buffer ) { + var output = this.source.replace( rePlaceholder, function( fullMatch, dataKey ) { + return data[ dataKey ] !== undefined ? data[ dataKey ] : fullMatch; + } ); - fn = "return buffer?buffer.push('" + fn + "'):['" + fn + "'].join('');"; - this.output = cache[ source ] = Function( 'data', 'buffer', fn ); - } + return buffer ? buffer.push( output ) : output; }; } )(); - -/** - * Processes the template, filling its variables with the provided data. - * - * @method output - * @param {Object} data An object containing properties which values will be - * used to fill the template variables. The property names must match the - * template variables names. Variables without matching properties will be - * kept untouched. - * @param {Array} [buffer] An array into which the output data will be pushed into. - * The number of entries appended to the array is unknown. - * @returns {String/Number} If `buffer` has not been provided, the processed - * template output data, otherwise the new length of `buffer`. - */