]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | |
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | |
4 | */ | |
5 | ||
6 | /** | |
7 | * @fileOverview Defines the {@link CKEDITOR.template} class, which represents | |
8 | * an UI template for an editor instance. | |
9 | */ | |
10 | ||
11 | ( function() { | |
12 | var cache = {}, | |
13 | rePlaceholder = /{([^}]+)}/g, | |
14 | reEscapableChars = /([\\'])/g, | |
15 | reNewLine = /\n/g, | |
16 | reCarriageReturn = /\r/g; | |
17 | ||
18 | /** | |
19 | * Lightweight template used to build the output string from variables. | |
20 | * | |
21 | * // HTML template for presenting a label UI. | |
22 | * var tpl = new CKEDITOR.template( '<div class="{cls}">{label}</div>' ); | |
23 | * alert( tpl.output( { cls: 'cke-label', label: 'foo'} ) ); // '<div class="cke-label">foo</div>' | |
24 | * | |
25 | * @class | |
26 | * @constructor Creates a template class instance. | |
27 | * @param {String} source The template source. | |
28 | */ | |
29 | CKEDITOR.template = function( source ) { | |
30 | // Builds an optimized function body for the output() method, focused on performance. | |
31 | // For example, if we have this "source": | |
32 | // '<div style="{style}">{editorName}</div>' | |
33 | // ... the resulting function body will be (apart from the "buffer" handling): | |
34 | // return [ '<div style="', data['style'] == undefined ? '{style}' : data['style'], '">', data['editorName'] == undefined ? '{editorName}' : data['editorName'], '</div>' ].join(''); | |
35 | ||
36 | // Try to read from the cache. | |
37 | if ( cache[ source ] ) | |
38 | this.output = cache[ source ]; | |
39 | else { | |
40 | var fn = source | |
41 | // Escape chars like slash "\" or single quote "'". | |
42 | .replace( reEscapableChars, '\\$1' ) | |
43 | .replace( reNewLine, '\\n' ) | |
44 | .replace( reCarriageReturn, '\\r' ) | |
45 | // Inject the template keys replacement. | |
46 | .replace( rePlaceholder, function( m, key ) { | |
47 | return "',data['" + key + "']==undefined?'{" + key + "}':data['" + key + "'],'"; | |
48 | } ); | |
49 | ||
50 | fn = "return buffer?buffer.push('" + fn + "'):['" + fn + "'].join('');"; | |
51 | this.output = cache[ source ] = Function( 'data', 'buffer', fn ); | |
52 | } | |
53 | }; | |
54 | } )(); | |
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 | */ |