diff options
Diffstat (limited to 'sources/core/template.js')
-rw-r--r-- | sources/core/template.js | 69 |
1 files changed, 29 insertions, 40 deletions
diff --git a/sources/core/template.js b/sources/core/template.js index 2b9e932..a627c34 100644 --- a/sources/core/template.js +++ b/sources/core/template.js | |||
@@ -9,11 +9,7 @@ | |||
9 | */ | 9 | */ |
10 | 10 | ||
11 | ( function() { | 11 | ( function() { |
12 | var cache = {}, | 12 | var rePlaceholder = /{([^}]+)}/g; |
13 | rePlaceholder = /{([^}]+)}/g, | ||
14 | reEscapableChars = /([\\'])/g, | ||
15 | reNewLine = /\n/g, | ||
16 | reCarriageReturn = /\r/g; | ||
17 | 13 | ||
18 | /** | 14 | /** |
19 | * Lightweight template used to build the output string from variables. | 15 | * Lightweight template used to build the output string from variables. |
@@ -27,42 +23,35 @@ | |||
27 | * @param {String} source The template source. | 23 | * @param {String} source The template source. |
28 | */ | 24 | */ |
29 | CKEDITOR.template = function( source ) { | 25 | CKEDITOR.template = function( source ) { |
30 | // Builds an optimized function body for the output() method, focused on performance. | 26 | /** |
31 | // For example, if we have this "source": | 27 | * The current template source. |
32 | // '<div style="{style}">{editorName}</div>' | 28 | * |
33 | // ... the resulting function body will be (apart from the "buffer" handling): | 29 | * @readonly |
34 | // return [ '<div style="', data['style'] == undefined ? '{style}' : data['style'], '">', data['editorName'] == undefined ? '{editorName}' : data['editorName'], '</div>' ].join(''); | 30 | * @member CKEDITOR.template |
31 | * @property {String} | ||
32 | */ | ||
33 | this.source = String( source ); | ||
34 | }; | ||
35 | 35 | ||
36 | // Try to read from the cache. | 36 | /** |
37 | if ( cache[ source ] ) | 37 | * Processes the template, filling its variables with the provided data. |
38 | this.output = cache[ source ]; | 38 | * |
39 | else { | 39 | * @method |
40 | var fn = source | 40 | * @member CKEDITOR.template |
41 | // Escape chars like slash "\" or single quote "'". | 41 | * @param {Object} data An object containing properties whose values will be |
42 | .replace( reEscapableChars, '\\$1' ) | 42 | * used to fill the template variables. The property names must match the |
43 | .replace( reNewLine, '\\n' ) | 43 | * template variables names. Variables without matching properties will be |
44 | .replace( reCarriageReturn, '\\r' ) | 44 | * kept untouched. |
45 | // Inject the template keys replacement. | 45 | * @param {Array} [buffer] An array that the output data will be pushed into. |
46 | .replace( rePlaceholder, function( m, key ) { | 46 | * The number of entries appended to the array is unknown. |
47 | return "',data['" + key + "']==undefined?'{" + key + "}':data['" + key + "'],'"; | 47 | * @returns {String/Number} If `buffer` has not been provided, the processed |
48 | } ); | 48 | * template output data; otherwise the new length of `buffer`. |
49 | */ | ||
50 | CKEDITOR.template.prototype.output = function( data, buffer ) { | ||
51 | var output = this.source.replace( rePlaceholder, function( fullMatch, dataKey ) { | ||
52 | return data[ dataKey ] !== undefined ? data[ dataKey ] : fullMatch; | ||
53 | } ); | ||
49 | 54 | ||
50 | fn = "return buffer?buffer.push('" + fn + "'):['" + fn + "'].join('');"; | 55 | return buffer ? buffer.push( output ) : output; |
51 | this.output = cache[ source ] = Function( 'data', 'buffer', fn ); | ||
52 | } | ||
53 | }; | 56 | }; |
54 | } )(); | 57 | } )(); |
55 | |||
56 | /** | ||
57 | * Processes the template, filling its variables with the provided data. | ||
58 | * | ||
59 | * @method output | ||
60 | * @param {Object} data An object containing properties which values will be | ||
61 | * used to fill the template variables. The property names must match the | ||
62 | * template variables names. Variables without matching properties will be | ||
63 | * kept untouched. | ||
64 | * @param {Array} [buffer] An array into which the output data will be pushed into. | ||
65 | * The number of entries appended to the array is unknown. | ||
66 | * @returns {String/Number} If `buffer` has not been provided, the processed | ||
67 | * template output data, otherwise the new length of `buffer`. | ||
68 | */ | ||