From 9c626dea3d83862967af53e814023b9b71c6d16f Mon Sep 17 00:00:00 2001 From: David Mason Date: Sun, 16 Mar 2014 23:38:44 +1000 Subject: [PATCH] add javascript templates library --- .../JavaScript-Templates/2_5_3/.gitignore | 2 + .../JavaScript-Templates/2_5_3/Gruntfile.js | 71 ++++ .../JavaScript-Templates/2_5_3/README.md | 350 ++++++++++++++++++ .../JavaScript-Templates/2_5_3/bower.json | 40 ++ .../JavaScript-Templates/2_5_3/css/demo.css | 72 ++++ .../JavaScript-Templates/2_5_3/index.html | 82 ++++ .../JavaScript-Templates/2_5_3/js/compile.js | 84 +++++ .../JavaScript-Templates/2_5_3/js/demo.js | 48 +++ .../JavaScript-Templates/2_5_3/js/runtime.js | 47 +++ .../JavaScript-Templates/2_5_3/js/tmpl.js | 87 +++++ .../JavaScript-Templates/2_5_3/js/tmpl.min.js | 1 + .../JavaScript-Templates/2_5_3/package.json | 49 +++ .../2_5_3/test/index.html | 41 ++ .../JavaScript-Templates/2_5_3/test/test.js | 298 +++++++++++++++ 14 files changed, 1272 insertions(+) create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/.gitignore create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/Gruntfile.js create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/README.md create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/bower.json create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/css/demo.css create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/index.html create mode 100755 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/compile.js create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/demo.js create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/runtime.js create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.js create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.min.js create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/package.json create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/index.html create mode 100644 zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/test.js diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/.gitignore b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/.gitignore new file mode 100644 index 0000000000..9daa8247da --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +node_modules diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/Gruntfile.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/Gruntfile.js new file mode 100644 index 0000000000..c44f725355 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/Gruntfile.js @@ -0,0 +1,71 @@ +/* + * JavaScript Templates Gruntfile + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2013, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*global module */ + +module.exports = function (grunt) { + 'use strict'; + + grunt.initConfig({ + jshint: { + all: [ + 'Gruntfile.js', + 'js/compile.js', + 'js/demo.js', + 'js/runtime.js', + 'js/tmpl.js', + 'test/test.js' + ] + }, + simplemocha: { + options: { + ignoreLeaks: false, + ui: 'bdd', + reporter: 'spec' + }, + all: { + src: ['test/test.js'] + } + }, + mocha: { + all: { + src: ['test/index.html'], + options: { + run: true, + bail: true, + log: true, + reporter: 'Spec' + }, + mocha: { + ignoreLeaks: false + } + } + }, + uglify: { + production: { + src: [ + 'js/tmpl.js' + ], + dest: 'js/tmpl.min.js' + } + } + }); + + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-simple-mocha'); + grunt.loadNpmTasks('grunt-mocha'); + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-bump-build-git'); + + grunt.registerTask('test', ['jshint', 'simplemocha', 'mocha']); + grunt.registerTask('default', ['test', 'uglify']); + +}; diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/README.md b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/README.md new file mode 100644 index 0000000000..36ef7cd54f --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/README.md @@ -0,0 +1,350 @@ +# JavaScript Templates + +## Demo +[JavaScript Templates Demo](http://blueimp.github.io/JavaScript-Templates/) + +## Description +< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers. + +## Usage + +### Client-side +Include the (minified) JavaScript Templates script in your HTML markup: + +```html + +``` + +Add a script section with type **"text/x-tmpl"**, a unique **id** property and your template definition as content: + +```html + +``` + +**"o"** (the lowercase letter) is a reference to the data parameter of the template function (see the API section on how to modify this identifier). + +In your application code, create a JavaScript object to use as data for the template: + +```js +var data = { + "title": "JavaScript Templates", + "license": { + "name": "MIT license", + "url": "http://www.opensource.org/licenses/MIT" + }, + "features": [ + "lightweight & fast", + "powerful", + "zero dependencies" + ] +}; +``` + +In a real application, this data could be the result of retrieving a [JSON](http://json.org/) resource. + +Render the result by calling the **tmpl()** method with the id of the template and the data object as arguments: + +```js +document.getElementById("result").innerHTML = tmpl("tmpl-demo", data); +``` + +### Server-side + +The following is an example how to use the JavaScript Templates engine on the server-side with [node.js](http://nodejs.org/). + +Create a new directory and add the **tmpl.js** file. Or alternatively, install the **blueimp-tmpl** package with [npm](http://npmjs.org/): + +```sh +npm install blueimp-tmpl +``` + +Add a file **template.html** with the following content: + +```html + +{%=o.title%} +

{%=o.title%}

+

Features

+ +``` + +Add a file **server.js** with the following content: + +```js +require("http").createServer(function (req, res) { + var fs = require("fs"), + // The tmpl module exports the tmpl() function: + tmpl = require("./tmpl").tmpl, + // Use the following version if you installed the package with npm: + // tmpl = require("blueimp-tmpl").tmpl, + // Sample data: + data = { + "title": "JavaScript Templates", + "url": "https://github.com/blueimp/JavaScript-Templates", + "features": [ + "lightweight & fast", + "powerful", + "zero dependencies" + ] + }; + // Override the template loading method: + tmpl.load = function (id) { + var filename = id + ".html"; + console.log("Loading " + filename); + return fs.readFileSync(filename, "utf8"); + }; + res.writeHead(200, {"Content-Type": "text/x-tmpl"}); + // Render the content: + res.end(tmpl("template", data)); +}).listen(8080, "localhost"); +console.log("Server running at http://localhost:8080/"); +``` + +Run the application with the following command: + +```sh +node server.js +``` + +## Requirements +The JavaScript Templates script has zero dependencies. + +## API + +### tmpl() function +The **tmpl()** function is added to the global **window** object and can be called as global function: + +```js +var result = tmpl("tmpl-demo", data); +``` + +The **tmpl()** function can be called with the id of a template, or with a template string: + +```js +var result = tmpl("

{%=o.title%}

", data); +``` + +If called without second argument, **tmpl()** returns a reusable template function: + +```js +var func = tmpl("

{%=o.title%}

"); +document.getElementById("result").innerHTML = func(data); +``` + +### Templates cache +Templates loaded by id are cached in the map **tmpl.cache**: + +```js +var func = tmpl("tmpl-demo"), // Loads and parses the template + cached = typeof tmpl.cache["tmpl-demo"] === "function", // true + result = tmpl("tmpl-demo", data); // Uses cached template function + +tmpl.cache["tmpl-demo"] = null; +result = tmpl("tmpl-demo", data); // Loads and parses the template again +``` + +### Output encoding +The method **tmpl.encode** is used to escape HTML special characters in the template output: + +```js +var output = tmpl.encode("<>&\"'\x00"); // Renders "<>&"'" +``` + +**tmpl.encode** makes use of the regular expression **tmpl.encReg** and the encoding map **tmpl.encMap** to match and replace special characters, which can be modified to change the behavior of the output encoding. +Strings matched by the regular expression, but not found in the encoding map are removed from the output. This allows for example to automatically trim input values (removing whitespace from the start and end of the string): + +```js +tmpl.encReg = /(^\s+)|(\s+$)|[<>&"'\x00]/g; +var output = tmpl.encode(" Banana! "); // Renders "Banana" (without whitespace) +``` + +### Local helper variables +The local variables available inside the templates are the following: + +* **o**: The data object given as parameter to the template function (see the next section on how to modify the parameter name). +* **tmpl**: A reference to the **tmpl** function object. +* **_s**: The string for the rendered result content. +* **_e**: A reference to the **tmpl.encode** method. +* **print**: Helper function to add content to the rendered result string. +* **include**: Helper function to include the return value of a different template in the result. + +To introduce additional local helper variables, the string **tmpl.helper** can be extended. The following adds a convenience function for *console.log* and a streaming function, that streams the template rendering result back to the callback argument (note the comma at the beginning of each variable declaration): + +```js +tmpl.helper += ",log=function(){console.log.apply(console, arguments)}" + + ",st='',stream=function(cb){var l=st.length;st=_s;cb( _s.slice(l));}"; +``` + +Those new helper functions could be used to stream the template contents to the console output: + +```html + +``` + +### Template function argument +The generated template functions accept one argument, which is the data object given to the **tmpl(id, data)** function. This argument is available inside the template definitions as parameter **o** (the lowercase letter). + +The argument name can be modified by overriding **tmpl.arg**: + +```js +tmpl.arg = "p"; + +// Renders "

JavaScript Templates

": +var result = tmpl("

{%=p.title%}

", {title: "JavaScript Templates"}); +``` + +### Template parsing +The template contents are matched and replaced using the regular expression **tmpl.regexp** and the replacement function **tmpl.func**. The replacement function operates based on the [parenthesized submatch strings](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter). + +To use different tags for the template syntax, override **tmpl.regexp** with a modified regular expression, by exchanging all occurrences of "{%" and "%}", e.g. with "[%" and "%]": + +```js +tmpl.regexp = /([\s'\\])(?!(?:[^[]|\[(?!%))*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g; +``` + +By default, the plugin preserves whitespace (newlines, carriage returns, tabs and spaces). To strip unnecessary whitespace, you can override the **tmpl.func** function, e.g. with the following code: + +```js +var originalFunc = tmpl.func; +tmpl.func = function (s, p1, p2, p3, p4, p5, offset, str) { + if (p1 && /\s/.test(p1)) { + if (!offset || /\s/.test(str.charAt(offset - 1)) || + /^\s+$/g.test(str.slice(offset))) { + return ''; + } + return ' '; + } + return originalFunc.apply(tmpl, arguments); +}; +``` + +## Templates syntax + +### Interpolation +Print variable with HTML special characters escaped: + +```html +

{%=o.title%}

+``` + +Print variable without escaping: + +```html +

{%#o.user_id%}

+``` + +Print output of function calls: + +```html +Website +``` + +Use dot notation to print nested properties: + +```html +{%=o.author.name%} +``` + +### Evaluation +Use **print(str)** to add escaped content to the output: + +```html +Year: {% var d=new Date(); print(d.getFullYear()); %} +``` + +Use **print(str, true)** to add unescaped content to the output: + +```html +{% print("Fast & powerful", true); %} +``` + +Use **include(str, obj)** to include content from a different template: + +```html +
+{% include('tmpl-link', {name: "Website", url: "http://example.org"}); %} +
+``` + +**If else condition**: + +```html +{% if (o.author.url) { %} + {%=o.author.name%} +{% } else { %} + No author url. +{% } %} +``` + +**For loop**: + +```html + +``` + +## Compiled templates +The JavaScript Templates project comes with a compilation script, that allows you to compile your templates into JavaScript code and combine them with a minimal Templates runtime into one minified JavaScript file. + +The compilation script is built for [node.js](http://nodejs.org/) and also requires [UglifyJS](https://github.com/mishoo/UglifyJS). +To use it, first install both the JavaScript Templates project and UglifyJS via [npm](http://npmjs.org/): + +```sh +npm install uglify-js +npm install blueimp-tmpl +``` + +This will put the executables **uglifyjs** and **tmpl.js** into the folder **node_modules/.bin**. It will also make them available on your PATH if you install the packages globally (by adding the **-g** flag to the install command). + +The **tmpl.js** executable accepts the paths to one or multiple template files as command line arguments and prints the generated JavaScript code to the console output. The following command line shows you how to store the generated code in a new JavaScript file that can be included in your project: + +```sh +tmpl.js templates/upload.html templates/download.html > tmpl.min.js +``` + +The files given as command line arguments to **tmpl.js** can either be pure template files or HTML documents with embedded template script sections. For the pure template files, the file names (without extension) serve as template ids. +The generated file can be included in your project as a replacement for the original **tmpl.js** runtime. It provides you with the same API and provides a **tmpl(id, data)** function that accepts the id of one of your templates as first and a data object as optional second parameter. + +## Tests +The JavaScript Templates project comes with [Unit Tests](http://en.wikipedia.org/wiki/Unit_testing). +There are two different ways to run the tests: + +* Open test/index.html in your browser or +* run `npm test` in the Terminal in the root path of the repository package. + +The first one tests the browser integration, the second one the [node.js](http://nodejs.org/) integration. + +## License +The JavaScript Templates script is released under the [MIT license](http://www.opensource.org/licenses/MIT). diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/bower.json b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/bower.json new file mode 100644 index 0000000000..b55c220883 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/bower.json @@ -0,0 +1,40 @@ +{ + "name": "blueimp-tmpl", + "version": "2.5.3", + "title": "JavaScript Templates", + "description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.", + "keywords": [ + "javascript", + "templates", + "templating" + ], + "homepage": "https://github.com/blueimp/JavaScript-Templates", + "author": { + "name": "Sebastian Tschan", + "url": "https://blueimp.net" + }, + "maintainers": [ + { + "name": "Sebastian Tschan", + "url": "https://blueimp.net" + } + ], + "repository": { + "type": "git", + "url": "git://github.com/blueimp/JavaScript-Templates.git" + }, + "bugs": "https://github.com/blueimp/JavaScript-Templates/issues", + "licenses": [ + { + "type": "MIT", + "url": "http://www.opensource.org/licenses/MIT" + } + ], + "main": "js/tmpl.js", + "ignore": [ + "/*.*", + "css", + "js/demo.js", + "test" + ] +} diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/css/demo.css b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/css/demo.css new file mode 100644 index 0000000000..186844acce --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/css/demo.css @@ -0,0 +1,72 @@ +/* + * JavaScript Templates Demo CSS 2.4.0 + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2013, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +body { + max-width: 750px; + margin: 0 auto; + padding: 1em; + font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, sans-serif; + font-size: 1em; + line-height: 1.4em; + background: #222; + color: #fff; + -webkit-text-size-adjust: 100%; + -ms-text-size-adjust: 100%; +} +a { + color: orange; + text-decoration: none; +} +img { + border: 0; + vertical-align: middle; +} +h1 { + line-height: 1em; +} +textarea, +input { + display: inline-block; + width: 100%; + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; + padding: 10px; + margin: 0 0 10px; + font-family: "Lucida Console", Monaco, monospace; +} +.result { + padding: 20px 40px; + background: #fff; + color: #222; +} + +@media (min-width: 481px) { + .navigation { + list-style: none; + padding: 0; + } + .navigation li { + display: inline-block; + } + .navigation li:not(:first-child):before { + content: '| '; + } +} + +/* IE7 fixes */ +*+html textarea, +*+html input { + width: 460px; +} +*+html .result { + width: 400px; +} diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/index.html b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/index.html new file mode 100644 index 0000000000..b5e94269b4 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/index.html @@ -0,0 +1,82 @@ + + + + + + +JavaScript Templates Demo + + + + + +

JavaScript Templates Demo

+

< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies.
+Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.

+ +
+

Template

+ +
+

Data (JSON)

+ +
+ + +

Result

+
+
+
+ + + + + + + + + diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/compile.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/compile.js new file mode 100755 index 0000000000..84ff2ceb98 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/compile.js @@ -0,0 +1,84 @@ +#!/usr/bin/env node +/* + * JavaScript Templates Compiler 2.4.0 + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2011, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*jslint nomen: true, stupid: true */ +/*global require, __dirname, process, console */ + +(function () { + "use strict"; + var tmpl = require("./tmpl.js").tmpl, + fs = require("fs"), + path = require("path"), + uglifyJS = require("uglify-js"), + // Retrieve the content of the minimal runtime: + runtime = fs.readFileSync(__dirname + "/runtime.js", "utf8"), + // A regular expression to parse templates from script tags in a HTML page: + regexp = /([\s\S]+?)<\/script>/gi, + // A regular expression to match the helper function names: + helperRegexp = new RegExp( + tmpl.helper.match(/\w+(?=\s*=\s*function\s*\()/g).join("\\s*\\(|") + "\\s*\\(" + ), + // A list to store the function bodies: + list = [], + code; + // Extend the Templating engine with a print method for the generated functions: + tmpl.print = function (str) { + // Only add helper functions if they are used inside of the template: + var helper = helperRegexp.test(str) ? tmpl.helper : "", + body = str.replace(tmpl.regexp, tmpl.func); + if (helper || (/_e\s*\(/.test(body))) { + helper = "_e=tmpl.encode" + helper + ","; + } + return "function(" + tmpl.arg + ",tmpl){" + + ("var " + helper + "_s='" + body + "';return _s;") + .split("_s+='';").join("") + "}"; + }; + // Loop through the command line arguments: + process.argv.forEach(function (file, index) { + var listLength = list.length, + stats, + content, + result, + id; + // Skipt the first two arguments, which are "node" and the script: + if (index > 1) { + stats = fs.statSync(file); + if (!stats.isFile()) { + console.error(file + ' is not a file.'); + return; + } + content = fs.readFileSync(file, "utf8"); + while (true) { + // Find templates in script tags: + result = regexp.exec(content); + if (!result) { + break; + } + id = result[2] || result[4]; + list.push("'" + id + "':" + tmpl.print(result[5])); + } + if (listLength === list.length) { + // No template script tags found, use the complete content: + id = path.basename(file, path.extname(file)); + list.push("'" + id + "':" + tmpl.print(content)); + } + } + }); + if (!list.length) { + console.error('Missing input file.'); + return; + } + // Combine the generated functions as cache of the minimal runtime: + code = runtime.replace("{}", "{" + list.join(",") + "}"); + // Generate the minified code and print it to the console output: + console.log(uglifyJS.minify(code, {fromString: true}).code); +}()); diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/demo.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/demo.js new file mode 100644 index 0000000000..afcedc29fc --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/demo.js @@ -0,0 +1,48 @@ +/* + * JavaScript Templates Demo JS 2.4.0 + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2013, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*global window, $ */ + +$(function () { + 'use strict'; + + var render = function (event) { + event.preventDefault(); + var result; + try { + result = window.tmpl( + $('#template').val(), + $.parseJSON($('#data').val()) + ); + } catch (e) { + result = window.tmpl('tmpl-error', e); + } + $('#result').html(result); + }, + init = function (event) { + if (event) { + event.preventDefault(); + } + $('#template').val($.trim($('#tmpl-demo').html())); + $('#data').val($.trim($('#tmpl-data').html())); + $('#result').empty(); + }, + error = function (e) { + $('#result').html( + window.tmpl('tmpl-error', e.originalEvent.message) + ); + }; + $(window).on('error', error); + $('#render').on('click', render); + $('#reset').on('click', init); + init(); + +}); diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/runtime.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/runtime.js new file mode 100644 index 0000000000..c4121fdc5c --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/runtime.js @@ -0,0 +1,47 @@ +/* + * JavaScript Templates Runtime 2.4.1 + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2011, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*jslint sloppy: true */ +/*global define */ + +(function ($) { + var tmpl = function (id, data) { + var f = tmpl.cache[id]; + return data ? f(data, tmpl) : function (data) { + return f(data, tmpl); + }; + }; + tmpl.cache = {}; + tmpl.encReg = /[<>&"'\x00]/g; + tmpl.encMap = { + "<" : "<", + ">" : ">", + "&" : "&", + "\"" : """, + "'" : "'" + }; + tmpl.encode = function (s) { + /*jshint eqnull:true */ + return (s == null ? "" : "" + s).replace( + tmpl.encReg, + function (c) { + return tmpl.encMap[c] || ""; + } + ); + }; + if (typeof define === "function" && define.amd) { + define(function () { + return tmpl; + }); + } else { + $.tmpl = tmpl; + } +}(this)); diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.js new file mode 100644 index 0000000000..9380053164 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.js @@ -0,0 +1,87 @@ +/* + * JavaScript Templates 2.4.1 + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2011, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + * + * Inspired by John Resig's JavaScript Micro-Templating: + * http://ejohn.org/blog/javascript-micro-templating/ + */ + +/*jslint evil: true, regexp: true, unparam: true */ +/*global document, define */ + +(function ($) { + "use strict"; + var tmpl = function (str, data) { + var f = !/[^\w\-\.:]/.test(str) ? tmpl.cache[str] = tmpl.cache[str] || + tmpl(tmpl.load(str)) : + new Function( + tmpl.arg + ',tmpl', + "var _e=tmpl.encode" + tmpl.helper + ",_s='" + + str.replace(tmpl.regexp, tmpl.func) + + "';return _s;" + ); + return data ? f(data, tmpl) : function (data) { + return f(data, tmpl); + }; + }; + tmpl.cache = {}; + tmpl.load = function (id) { + return document.getElementById(id).innerHTML; + }; + tmpl.regexp = /([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g; + tmpl.func = function (s, p1, p2, p3, p4, p5) { + if (p1) { // whitespace, quote and backspace in HTML context + return { + "\n": "\\n", + "\r": "\\r", + "\t": "\\t", + " " : " " + }[p1] || "\\" + p1; + } + if (p2) { // interpolation: {%=prop%}, or unescaped: {%#prop%} + if (p2 === "=") { + return "'+_e(" + p3 + ")+'"; + } + return "'+(" + p3 + "==null?'':" + p3 + ")+'"; + } + if (p4) { // evaluation start tag: {% + return "';"; + } + if (p5) { // evaluation end tag: %} + return "_s+='"; + } + }; + tmpl.encReg = /[<>&"'\x00]/g; + tmpl.encMap = { + "<" : "<", + ">" : ">", + "&" : "&", + "\"" : """, + "'" : "'" + }; + tmpl.encode = function (s) { + /*jshint eqnull:true */ + return (s == null ? "" : "" + s).replace( + tmpl.encReg, + function (c) { + return tmpl.encMap[c] || ""; + } + ); + }; + tmpl.arg = "o"; + tmpl.helper = ",print=function(s,e){_s+=e?(s==null?'':s):_e(s);}" + + ",include=function(s,d){_s+=tmpl(s,d);}"; + if (typeof define === "function" && define.amd) { + define(function () { + return tmpl; + }); + } else { + $.tmpl = tmpl; + } +}(this)); diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.min.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.min.js new file mode 100644 index 0000000000..64bb2a3487 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/js/tmpl.min.js @@ -0,0 +1 @@ +!function(a){"use strict";var b=function(a,c){var d=/[^\w\-\.:]/.test(a)?new Function(b.arg+",tmpl","var _e=tmpl.encode"+b.helper+",_s='"+a.replace(b.regexp,b.func)+"';return _s;"):b.cache[a]=b.cache[a]||b(b.load(a));return c?d(c,b):function(a){return d(a,b)}};b.cache={},b.load=function(a){return document.getElementById(a).innerHTML},b.regexp=/([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g,b.func=function(a,b,c,d,e,f){return b?{"\n":"\\n","\r":"\\r"," ":"\\t"," ":" "}[b]||"\\"+b:c?"="===c?"'+_e("+d+")+'":"'+("+d+"==null?'':"+d+")+'":e?"';":f?"_s+='":void 0},b.encReg=/[<>&"'\x00]/g,b.encMap={"<":"<",">":">","&":"&",'"':""","'":"'"},b.encode=function(a){return(null==a?"":""+a).replace(b.encReg,function(a){return b.encMap[a]||""})},b.arg="o",b.helper=",print=function(s,e){_s+=e?(s==null?'':s):_e(s);},include=function(s,d){_s+=tmpl(s,d);}","function"==typeof define&&define.amd?define(function(){return b}):a.tmpl=b}(this); \ No newline at end of file diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/package.json b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/package.json new file mode 100644 index 0000000000..156d586043 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/package.json @@ -0,0 +1,49 @@ +{ + "name": "blueimp-tmpl", + "version": "2.5.3", + "title": "JavaScript Templates", + "description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.", + "keywords": [ + "javascript", + "templates", + "templating" + ], + "homepage": "https://github.com/blueimp/JavaScript-Templates", + "author": { + "name": "Sebastian Tschan", + "url": "https://blueimp.net" + }, + "maintainers": [ + { + "name": "Sebastian Tschan", + "url": "https://blueimp.net" + } + ], + "repository": { + "type": "git", + "url": "git://github.com/blueimp/JavaScript-Templates.git" + }, + "bugs": "https://github.com/blueimp/JavaScript-Templates/issues", + "licenses": [ + { + "type": "MIT", + "url": "http://www.opensource.org/licenses/MIT" + } + ], + "devDependencies": { + "grunt": "~0.4.1", + "grunt-contrib-uglify": "~0.2.7", + "grunt-contrib-jshint": "~0.7.1", + "grunt-bump-build-git": "~1.0.0", + "grunt-simple-mocha": "~0.4.0", + "grunt-mocha": "~0.4.1", + "expect.js": "0.2.0" + }, + "scripts": { + "test": "grunt test" + }, + "bin": { + "tmpl.js": "js/compile.js" + }, + "main": "js/tmpl.js" +} diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/index.html b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/index.html new file mode 100644 index 0000000000..e5787d7af6 --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/index.html @@ -0,0 +1,41 @@ + + + + + + +JavaScript Templates Test + + + + +
+ + + + + + + + + diff --git a/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/test.js b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/test.js new file mode 100644 index 0000000000..00e3044f1b --- /dev/null +++ b/zanata-war/src/main/webapp/resources/JavaScript-Templates/2_5_3/test/test.js @@ -0,0 +1,298 @@ +/* + * JavaScript Templates Test 2.4.1 + * https://github.com/blueimp/JavaScript-Templates + * + * Copyright 2011, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*global beforeEach, afterEach, describe, it, expect, require */ + +(function (context, expect, tmpl) { + 'use strict'; + + if (context.require === undefined) { + // Override the template loading method: + tmpl.load = function (id) { + switch (id) { + case 'template': + return '{%=o.value%}'; + } + }; + } + + var data; + + beforeEach(function () { + // Initialize the sample data: + data = { + value: 'value', + nullValue: null, + falseValue: false, + zeroValue: 0, + special: '<>&"\'\x00', + list: [1, 2, 3, 4, 5], + func: function () { + return this.value; + }, + deep: { + value: 'value' + } + }; + }); + + afterEach(function () { + // Purge the template cache: + tmpl.cache = {}; + }); + + describe('Template loading', function () { + + it('String template', function () { + expect( + tmpl('{%=o.value%}', data), + 'value' + ); + }); + + it('Load template by id', function () { + expect( + tmpl('template', data), + 'value' + ); + }); + + it('Retun function when called without data parameter', function () { + expect( + tmpl('{%=o.value%}')(data), + 'value' + ); + }); + + it('Cache templates loaded by id', function () { + tmpl('template'); + expect( + tmpl.cache.template + ).to.be.a('function'); + }); + + }); + + describe('Interpolation', function () { + + it('Escape HTML special characters with {%=o.prop%}', function () { + expect( + tmpl('{%=o.special%}', data), + '<>&"'' + ); + }); + + it('Allow HTML special characters with {%#o.prop%}', function () { + expect( + tmpl('{%#o.special%}', data), + '<>&"\'\x00' + ); + }); + + it('Function call', function () { + expect( + tmpl('{%=o.func()%}', data), + 'value' + ); + }); + + it('Dot notation', function () { + expect( + tmpl('{%=o.deep.value%}', data), + 'value' + ); + }); + + it('Handle single quotes', function () { + expect( + tmpl('\'single quotes\'{%=": \'"%}', data), + '\'single quotes\': \'' + ); + }); + + it('Handle double quotes', function () { + expect( + tmpl('"double quotes"{%=": \\""%}', data), + '"double quotes": "' + ); + }); + + it('Handle backslashes', function () { + expect( + tmpl('\\backslashes\\{%=": \\\\"%}', data), + '\\backslashes\\: \\' + ); + }); + + it('Interpolate escaped falsy values except undefined or null', function () { + expect( + tmpl( + '{%=o.undefinedValue%}' + + '{%=o.nullValue%}' + + '{%=o.falseValue%}' + + '{%=o.zeroValue%}', + data + ) + ).to.be( + 'false0' + ); + }); + + it('Interpolate unescaped falsy values except undefined or null', function () { + expect( + tmpl( + '{%#o.undefinedValue%}' + + '{%#o.nullValue%}' + + '{%#o.falseValue%}' + + '{%#o.zeroValue%}', + data + ) + ).to.be( + 'false0' + ); + }); + + it('Preserve whitespace', function () { + expect( + tmpl( + '\n\r\t{%=o.value%} \n\r\t{%=o.value%} ', + data + ) + ).to.be( + '\n\r\tvalue \n\r\tvalue ' + ); + }); + + }); + + describe('Evaluation', function () { + + it('Escape HTML special characters with print(data)', function () { + expect( + tmpl('{% print(o.special); %}', data), + '<>&"'' + ); + }); + + it('Allow HTML special characters with print(data, true)', function () { + expect( + tmpl('{% print(o.special, true); %}', data), + '<>&"\'\x00' + ); + }); + + it('Print out escaped falsy values except undefined or null', function () { + expect( + tmpl( + '{% print(o.undefinedValue); %}' + + '{% print(o.nullValue); %}' + + '{% print(o.falseValue); %}' + + '{% print(o.zeroValue); %}', + data + ) + ).to.be( + 'false0' + ); + }); + + it('Print out unescaped falsy values except undefined or null', function () { + expect( + tmpl( + '{% print(o.undefinedValue, true); %}' + + '{% print(o.nullValue, true); %}' + + '{% print(o.falseValue, true); %}' + + '{% print(o.zeroValue, true); %}', + data + ) + ).to.be( + 'false0' + ); + }); + + it('Include template', function () { + expect( + tmpl('{% include("template", {value: "value"}); %}', data), + 'value' + ); + }); + + it('If condition', function () { + expect( + tmpl('{% if (o.value) { %}true{% } else { %}false{% } %}', data), + 'true' + ); + }); + + it('Else condition', function () { + expect( + tmpl( + '{% if (o.undefinedValue) { %}false{% } else { %}true{% } %}', + data + ) + ).to.be( + 'true' + ); + }); + + it('For loop', function () { + expect( + tmpl( + '{% for (var i=0; i