]>
git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blob - sources/core/template.js
2b9e93281e8ea0c14057f50dd1f6351b0b0b52b3
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.template} class, which represents
8 * an UI template for an editor instance.
13 rePlaceholder
= /{([^}]+)}/g,
14 reEscapableChars
= /([\\'])/g,
16 reCarriageReturn
= /\r/g;
19 * Lightweight template used to build the output string from variables.
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>'
26 * @constructor Creates a template class instance.
27 * @param {String} source The template source.
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('');
36 // Try to read from the cache.
37 if ( cache
[ source
] )
38 this.output
= cache
[ 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
+ "'],'";
50 fn
= "return buffer?buffer.push('" + fn
+ "'):['" + fn
+ "'].join('');";
51 this.output
= cache
[ source
] = Function( 'data', 'buffer', fn
);
57 * Processes the template, filling its variables with the provided data.
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
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`.