PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/yui/3.9.0pr2/template-micro/template-micro-coverage.js

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