PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/yui/3.8.0/template-micro/template-micro-coverage.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 288 lines | 118 code | 24 blank | 146 comment | 7 complexity | 968202c220169a33d2fe0c25753b6454 MD5 | raw file
  1. if (typeof _yuitest_coverage == "undefined"){
  2. _yuitest_coverage = {};
  3. _yuitest_coverline = function(src, line){
  4. var coverage = _yuitest_coverage[src];
  5. if (!coverage.lines[line]){
  6. coverage.calledLines++;
  7. }
  8. coverage.lines[line]++;
  9. };
  10. _yuitest_coverfunc = function(src, name, line){
  11. var coverage = _yuitest_coverage[src],
  12. funcId = name + ":" + line;
  13. if (!coverage.functions[funcId]){
  14. coverage.calledFunctions++;
  15. }
  16. coverage.functions[funcId]++;
  17. };
  18. }
  19. _yuitest_coverage["build/template-micro/template-micro.js"] = {
  20. lines: {},
  21. functions: {},
  22. coveredLines: 0,
  23. calledLines: 0,
  24. coveredFunctions: 0,
  25. calledFunctions: 0,
  26. path: "build/template-micro/template-micro.js",
  27. code: []
  28. };
  29. _yuitest_coverage["build/template-micro/template-micro.js"].code=["YUI.add('template-micro', function (Y, NAME) {","","/**","Adds the `Y.Template.Micro` template engine, which provides fast, simple","string-based micro-templating similar to ERB or Underscore templates.","","@module template","@submodule template-micro","@since 3.8.0","**/","","/**","Fast, simple string-based micro-templating engine similar to ERB or Underscore","templates.","","@class Template.Micro","@static","@since 3.8.0","**/","","// This code was heavily inspired by Underscore.js's _.template() method","// (written by Jeremy Ashkenas), which was in turn inspired by John Resig's","// micro-templating implementation.","","var Micro = Y.namespace('Template.Micro');","","/**","Default options for `Y.Template.Micro`.","","@property {Object} options",""," @param {RegExp} [options.code] Regex that matches code blocks like"," `<% ... %>`."," @param {RegExp} [options.escapedOutput] Regex that matches escaped output"," tags like `<%= ... %>`."," @param {RegExp} [options.rawOutput] Regex that matches raw output tags like"," `<%== ... %>`."," @param {RegExp} [options.stringEscape] Regex that matches characters that"," need to be escaped inside single-quoted JavaScript string literals.","","@static","@since 3.8.0","**/","Micro.options = {"," code : /<%([\\s\\S]+?)%>/g,"," escapedOutput: /<%=([\\s\\S]+?)%>/g,"," rawOutput : /<%==([\\s\\S]+?)%>/g,"," stringEscape : /\\\\|'|\\r|\\n|\\t|\\u2028|\\u2029/g","};","","/**","Compiles a template string into a JavaScript function. Pass a data object to the","function to render the template using the given data and get back a rendered","string.","","Within a template, use `<%= ... %>` to output the value of an expression (where","`...` is the JavaScript expression or data variable to evaluate). The output","will be HTML-escaped by default. To output a raw value without escaping, use","`<%== ... %>`, but be careful not to do this with untrusted user input.","","To execute arbitrary JavaScript code within the template without rendering its","output, use `<% ... %>`, where `...` is the code to be executed. This allows the","use of if/else blocks, loops, function calls, etc., although it's recommended","that you avoid embedding anything beyond basic flow control logic in your","templates.","","Properties of the data object passed to a template function are made available","on a `data` variable within the scope of the template. So, if you pass in","the object `{message: 'hello!'}`, you can print the value of the `message`","property using `<%= data.message %>`.","","@example",""," YUI().use('template-micro', function (Y) {"," var template = '<ul class=\"<%= data.classNames.list %>\">' +"," '<% Y.Array.each(data.items, function (item) { %>' +"," '<li><%= item %></li>' +"," '<% }); %>' +"," '</ul>';",""," // Compile the template into a function."," var compiled = Y.Template.Micro.compile(template);",""," // Render the template to HTML, passing in the data to use."," var html = compiled({"," classNames: {list: 'demo'},"," items : ['one', 'two', 'three', 'four']"," });"," });","","@method compile","@param {String} text Template text to compile.","@param {Object} [options] Options. If specified, these options will override the"," default options defined in `Y.Template.Micro.options`. See the documentation"," for that property for details on which options are available.","@return {Function} Compiled template function. Execute this function and pass in"," a data object to render the template with the given data.","@static","@since 3.8.0","**/","Micro.compile = function (text, options) {"," var blocks = [],"," tokenClose = \"\\uffff\","," tokenOpen = \"\\ufffe\","," source;",""," options = Y.merge(Micro.options, options);",""," // Parse the input text into a string of JavaScript code, with placeholders"," // for code blocks. Text outside of code blocks will be escaped for safe"," // usage within a double-quoted string literal."," source = \"var $b='',$t='\" +",""," // U+FFFE and U+FFFF are guaranteed to represent non-characters, so no"," // valid UTF-8 string should ever contain them. That means we can freely"," // strip them out of the input text (just to be safe) and then use them"," // for our own nefarious purposes as token placeholders!"," //"," // See http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Noncharacters"," text.replace(/\\ufffe|\\uffff/g, '')",""," .replace(options.rawOutput, function (match, code) {"," return tokenOpen + (blocks.push(\"'+\\n((\" + code + \")||$b)+\\n'\") - 1) + tokenClose;"," })",""," .replace(options.escapedOutput, function (match, code) {"," return tokenOpen + (blocks.push(\"'+\\n$e((\" + code + \")||$b)+\\n'\") - 1) + tokenClose;"," })",""," .replace(options.code, function (match, code) {"," return tokenOpen + (blocks.push(\"';\\n\" + code + \"\\n$t+='\") - 1) + tokenClose;"," })",""," .replace(options.stringEscape, \"\\\\$&\")",""," // Replace the token placeholders with code."," .replace(/\\ufffe(\\d+)\\uffff/g, function (match, index) {"," return blocks[parseInt(index, 10)];"," })",""," // Remove noop string concatenations that have been left behind."," .replace(/\\n\\$t\\+='';\\n/g, '\\n') +",""," \"';\\nreturn $t;\";",""," // If compile() was called from precompile(), return precompiled source."," if (options.precompile) {"," return \"function (Y, $e, data) {\\n\" + source + \"\\n}\";"," }",""," // Otherwise, return an executable function."," return this.revive(new Function('Y', '$e', 'data', source));","};","","/**","Precompiles the given template text into a string of JavaScript source code that","can be evaluated later in another context (or on another machine) to render the","template.","","A common use case is to precompile templates at build time or on the server,","then evaluate the code on the client to render a template. The client only needs","to revive and render the template, avoiding the work of the compilation step.","","@method precompile","@param {String} text Template text to precompile.","@param {Object} [options] Options. If specified, these options will override the"," default options defined in `Y.Template.Micro.options`. See the documentation"," for that property for details on which options are available.","@return {String} Source code for the precompiled template.","@static","@since 3.8.0","**/","Micro.precompile = function (text, options) {"," options || (options = {});"," options.precompile = true;",""," return this.compile(text, options);","};","","/**","Compiles and renders the given template text in a single step.","","This can be useful for single-use templates, but if you plan to render the same","template multiple times, it's much better to use `compile()` to compile it once,","then simply call the compiled function multiple times to avoid recompiling.","","@method render","@param {String} text Template text to render.","@param {Object} data Data to pass to the template.","@param {Object} [options] Options. If specified, these options will override the"," default options defined in `Y.Template.Micro.options`. See the documentation"," for that property for details on which options are available.","@return {String} Rendered result.","@static","@since 3.8.0","**/","Micro.render = function (text, data, options) {"," return this.compile(text, options)(data);","};","","/**","Revives a precompiled template function into a normal compiled template function","that can be called to render the template. The precompiled function must already","have been evaluated to a function -- you can't pass raw JavaScript code to","`revive()`.","","@method revive","@param {Function} precompiled Precompiled template function.","@return {Function} Revived template function, ready to be rendered.","@static","@since 3.8.0","**/","Micro.revive = function (precompiled) {"," return function (data) {"," data || (data = {});"," return precompiled.call(data, Y, Y.Escape.html, data);"," };","};","","","}, '@VERSION@', {\"requires\": [\"escape\"]});"];
  30. _yuitest_coverage["build/template-micro/template-micro.js"].lines = {"1":0,"25":0,"44":0,"101":0,"102":0,"107":0,"112":0,"123":0,"127":0,"131":0,"138":0,"147":0,"148":0,"152":0,"173":0,"174":0,"175":0,"177":0,"197":0,"198":0,"213":0,"214":0,"215":0,"216":0};
  31. _yuitest_coverage["build/template-micro/template-micro.js"].functions = {"(anonymous 2):122":0,"(anonymous 3):126":0,"(anonymous 4):130":0,"(anonymous 5):137":0,"compile:101":0,"precompile:173":0,"render:197":0,"(anonymous 6):214":0,"revive:213":0,"(anonymous 1):1":0};
  32. _yuitest_coverage["build/template-micro/template-micro.js"].coveredLines = 24;
  33. _yuitest_coverage["build/template-micro/template-micro.js"].coveredFunctions = 10;
  34. _yuitest_coverline("build/template-micro/template-micro.js", 1);
  35. YUI.add('template-micro', function (Y, NAME) {
  36. /**
  37. Adds the `Y.Template.Micro` template engine, which provides fast, simple
  38. string-based micro-templating similar to ERB or Underscore templates.
  39. @module template
  40. @submodule template-micro
  41. @since 3.8.0
  42. **/
  43. /**
  44. Fast, simple string-based micro-templating engine similar to ERB or Underscore
  45. templates.
  46. @class Template.Micro
  47. @static
  48. @since 3.8.0
  49. **/
  50. // This code was heavily inspired by Underscore.js's _.template() method
  51. // (written by Jeremy Ashkenas), which was in turn inspired by John Resig's
  52. // micro-templating implementation.
  53. _yuitest_coverfunc("build/template-micro/template-micro.js", "(anonymous 1)", 1);
  54. _yuitest_coverline("build/template-micro/template-micro.js", 25);
  55. var Micro = Y.namespace('Template.Micro');
  56. /**
  57. Default options for `Y.Template.Micro`.
  58. @property {Object} options
  59. @param {RegExp} [options.code] Regex that matches code blocks like
  60. `<% ... %>`.
  61. @param {RegExp} [options.escapedOutput] Regex that matches escaped output
  62. tags like `<%= ... %>`.
  63. @param {RegExp} [options.rawOutput] Regex that matches raw output tags like
  64. `<%== ... %>`.
  65. @param {RegExp} [options.stringEscape] Regex that matches characters that
  66. need to be escaped inside single-quoted JavaScript string literals.
  67. @static
  68. @since 3.8.0
  69. **/
  70. _yuitest_coverline("build/template-micro/template-micro.js", 44);
  71. Micro.options = {
  72. code : /<%([\s\S]+?)%>/g,
  73. escapedOutput: /<%=([\s\S]+?)%>/g,
  74. rawOutput : /<%==([\s\S]+?)%>/g,
  75. stringEscape : /\\|'|\r|\n|\t|\u2028|\u2029/g
  76. };
  77. /**
  78. Compiles a template string into a JavaScript function. Pass a data object to the
  79. function to render the template using the given data and get back a rendered
  80. string.
  81. Within a template, use `<%= ... %>` to output the value of an expression (where
  82. `...` is the JavaScript expression or data variable to evaluate). The output
  83. will be HTML-escaped by default. To output a raw value without escaping, use
  84. `<%== ... %>`, but be careful not to do this with untrusted user input.
  85. To execute arbitrary JavaScript code within the template without rendering its
  86. output, use `<% ... %>`, where `...` is the code to be executed. This allows the
  87. use of if/else blocks, loops, function calls, etc., although it's recommended
  88. that you avoid embedding anything beyond basic flow control logic in your
  89. templates.
  90. Properties of the data object passed to a template function are made available
  91. on a `data` variable within the scope of the template. So, if you pass in
  92. the object `{message: 'hello!'}`, you can print the value of the `message`
  93. property using `<%= data.message %>`.
  94. @example
  95. YUI().use('template-micro', function (Y) {
  96. var template = '<ul class="<%= data.classNames.list %>">' +
  97. '<% Y.Array.each(data.items, function (item) { %>' +
  98. '<li><%= item %></li>' +
  99. '<% }); %>' +
  100. '</ul>';
  101. // Compile the template into a function.
  102. var compiled = Y.Template.Micro.compile(template);
  103. // Render the template to HTML, passing in the data to use.
  104. var html = compiled({
  105. classNames: {list: 'demo'},
  106. items : ['one', 'two', 'three', 'four']
  107. });
  108. });
  109. @method compile
  110. @param {String} text Template text to compile.
  111. @param {Object} [options] Options. If specified, these options will override the
  112. default options defined in `Y.Template.Micro.options`. See the documentation
  113. for that property for details on which options are available.
  114. @return {Function} Compiled template function. Execute this function and pass in
  115. a data object to render the template with the given data.
  116. @static
  117. @since 3.8.0
  118. **/
  119. _yuitest_coverline("build/template-micro/template-micro.js", 101);
  120. Micro.compile = function (text, options) {
  121. _yuitest_coverfunc("build/template-micro/template-micro.js", "compile", 101);
  122. _yuitest_coverline("build/template-micro/template-micro.js", 102);
  123. var blocks = [],
  124. tokenClose = "\uffff",
  125. tokenOpen = "\ufffe",
  126. source;
  127. _yuitest_coverline("build/template-micro/template-micro.js", 107);
  128. options = Y.merge(Micro.options, options);
  129. // Parse the input text into a string of JavaScript code, with placeholders
  130. // for code blocks. Text outside of code blocks will be escaped for safe
  131. // usage within a double-quoted string literal.
  132. _yuitest_coverline("build/template-micro/template-micro.js", 112);
  133. source = "var $b='',$t='" +
  134. // U+FFFE and U+FFFF are guaranteed to represent non-characters, so no
  135. // valid UTF-8 string should ever contain them. That means we can freely
  136. // strip them out of the input text (just to be safe) and then use them
  137. // for our own nefarious purposes as token placeholders!
  138. //
  139. // See http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Noncharacters
  140. text.replace(/\ufffe|\uffff/g, '')
  141. .replace(options.rawOutput, function (match, code) {
  142. _yuitest_coverfunc("build/template-micro/template-micro.js", "(anonymous 2)", 122);
  143. _yuitest_coverline("build/template-micro/template-micro.js", 123);
  144. return tokenOpen + (blocks.push("'+\n((" + code + ")||$b)+\n'") - 1) + tokenClose;
  145. })
  146. .replace(options.escapedOutput, function (match, code) {
  147. _yuitest_coverfunc("build/template-micro/template-micro.js", "(anonymous 3)", 126);
  148. _yuitest_coverline("build/template-micro/template-micro.js", 127);
  149. return tokenOpen + (blocks.push("'+\n$e((" + code + ")||$b)+\n'") - 1) + tokenClose;
  150. })
  151. .replace(options.code, function (match, code) {
  152. _yuitest_coverfunc("build/template-micro/template-micro.js", "(anonymous 4)", 130);
  153. _yuitest_coverline("build/template-micro/template-micro.js", 131);
  154. return tokenOpen + (blocks.push("';\n" + code + "\n$t+='") - 1) + tokenClose;
  155. })
  156. .replace(options.stringEscape, "\\$&")
  157. // Replace the token placeholders with code.
  158. .replace(/\ufffe(\d+)\uffff/g, function (match, index) {
  159. _yuitest_coverfunc("build/template-micro/template-micro.js", "(anonymous 5)", 137);
  160. _yuitest_coverline("build/template-micro/template-micro.js", 138);
  161. return blocks[parseInt(index, 10)];
  162. })
  163. // Remove noop string concatenations that have been left behind.
  164. .replace(/\n\$t\+='';\n/g, '\n') +
  165. "';\nreturn $t;";
  166. // If compile() was called from precompile(), return precompiled source.
  167. _yuitest_coverline("build/template-micro/template-micro.js", 147);
  168. if (options.precompile) {
  169. _yuitest_coverline("build/template-micro/template-micro.js", 148);
  170. return "function (Y, $e, data) {\n" + source + "\n}";
  171. }
  172. // Otherwise, return an executable function.
  173. _yuitest_coverline("build/template-micro/template-micro.js", 152);
  174. return this.revive(new Function('Y', '$e', 'data', source));
  175. };
  176. /**
  177. Precompiles the given template text into a string of JavaScript source code that
  178. can be evaluated later in another context (or on another machine) to render the
  179. template.
  180. A common use case is to precompile templates at build time or on the server,
  181. then evaluate the code on the client to render a template. The client only needs
  182. to revive and render the template, avoiding the work of the compilation step.
  183. @method precompile
  184. @param {String} text Template text to precompile.
  185. @param {Object} [options] Options. If specified, these options will override the
  186. default options defined in `Y.Template.Micro.options`. See the documentation
  187. for that property for details on which options are available.
  188. @return {String} Source code for the precompiled template.
  189. @static
  190. @since 3.8.0
  191. **/
  192. _yuitest_coverline("build/template-micro/template-micro.js", 173);
  193. Micro.precompile = function (text, options) {
  194. _yuitest_coverfunc("build/template-micro/template-micro.js", "precompile", 173);
  195. _yuitest_coverline("build/template-micro/template-micro.js", 174);
  196. options || (options = {});
  197. _yuitest_coverline("build/template-micro/template-micro.js", 175);
  198. options.precompile = true;
  199. _yuitest_coverline("build/template-micro/template-micro.js", 177);
  200. return this.compile(text, options);
  201. };
  202. /**
  203. Compiles and renders the given template text in a single step.
  204. This can be useful for single-use templates, but if you plan to render the same
  205. template multiple times, it's much better to use `compile()` to compile it once,
  206. then simply call the compiled function multiple times to avoid recompiling.
  207. @method render
  208. @param {String} text Template text to render.
  209. @param {Object} data Data to pass to the template.
  210. @param {Object} [options] Options. If specified, these options will override the
  211. default options defined in `Y.Template.Micro.options`. See the documentation
  212. for that property for details on which options are available.
  213. @return {String} Rendered result.
  214. @static
  215. @since 3.8.0
  216. **/
  217. _yuitest_coverline("build/template-micro/template-micro.js", 197);
  218. Micro.render = function (text, data, options) {
  219. _yuitest_coverfunc("build/template-micro/template-micro.js", "render", 197);
  220. _yuitest_coverline("build/template-micro/template-micro.js", 198);
  221. return this.compile(text, options)(data);
  222. };
  223. /**
  224. Revives a precompiled template function into a normal compiled template function
  225. that can be called to render the template. The precompiled function must already
  226. have been evaluated to a function -- you can't pass raw JavaScript code to
  227. `revive()`.
  228. @method revive
  229. @param {Function} precompiled Precompiled template function.
  230. @return {Function} Revived template function, ready to be rendered.
  231. @static
  232. @since 3.8.0
  233. **/
  234. _yuitest_coverline("build/template-micro/template-micro.js", 213);
  235. Micro.revive = function (precompiled) {
  236. _yuitest_coverfunc("build/template-micro/template-micro.js", "revive", 213);
  237. _yuitest_coverline("build/template-micro/template-micro.js", 214);
  238. return function (data) {
  239. _yuitest_coverfunc("build/template-micro/template-micro.js", "(anonymous 6)", 214);
  240. _yuitest_coverline("build/template-micro/template-micro.js", 215);
  241. data || (data = {});
  242. _yuitest_coverline("build/template-micro/template-micro.js", 216);
  243. return precompiled.call(data, Y, Y.Escape.html, data);
  244. };
  245. };
  246. }, '@VERSION@', {"requires": ["escape"]});