template.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview Defines the {@link CKEDITOR.template} class, which represents
  7. * an UI template for an editor instance.
  8. */
  9. ( function() {
  10. var cache = {},
  11. rePlaceholder = /{([^}]+)}/g,
  12. reEscapableChars = /([\\'])/g,
  13. reNewLine = /\n/g,
  14. reCarriageReturn = /\r/g;
  15. /**
  16. * Lightweight template used to build the output string from variables.
  17. *
  18. * // HTML template for presenting a label UI.
  19. * var tpl = new CKEDITOR.template( '<div class="{cls}">{label}</div>' );
  20. * alert( tpl.output( { cls: 'cke-label', label: 'foo'} ) ); // '<div class="cke-label">foo</div>'
  21. *
  22. * @class
  23. * @constructor Creates a template class instance.
  24. * @param {String} source The template source.
  25. */
  26. CKEDITOR.template = function( source ) {
  27. // Builds an optimized function body for the output() method, focused on performance.
  28. // For example, if we have this "source":
  29. // '<div style="{style}">{editorName}</div>'
  30. // ... the resulting function body will be (apart from the "buffer" handling):
  31. // return [ '<div style="', data['style'] == undefined ? '{style}' : data['style'], '">', data['editorName'] == undefined ? '{editorName}' : data['editorName'], '</div>' ].join('');
  32. // Try to read from the cache.
  33. if ( cache[ source ] )
  34. this.output = cache[ source ];
  35. else {
  36. var fn = source
  37. // Escape chars like slash "\" or single quote "'".
  38. .replace( reEscapableChars, '\\$1' )
  39. .replace( reNewLine, '\\n' )
  40. .replace( reCarriageReturn, '\\r' )
  41. // Inject the template keys replacement.
  42. .replace( rePlaceholder, function( m, key ) {
  43. return "',data['" + key + "']==undefined?'{" + key + "}':data['" + key + "'],'";
  44. } );
  45. fn = "return buffer?buffer.push('" + fn + "'):['" + fn + "'].join('');";
  46. this.output = cache[ source ] = Function( 'data', 'buffer', fn );
  47. }
  48. };
  49. } )();
  50. /**
  51. * Processes the template, filling its variables with the provided data.
  52. *
  53. * @method output
  54. * @param {Object} data An object containing properties which values will be
  55. * used to fill the template variables. The property names must match the
  56. * template variables names. Variables without matching properties will be
  57. * kept untouched.
  58. * @param {Array} [buffer] An array into which the output data will be pushed into.
  59. * The number of entries appended to the array is unknown.
  60. * @returns {String/Number} If `buffer` has not been provided, the processed
  61. * template output data, otherwise the new length of `buffer`.
  62. */