Sfoglia il codice sorgente

Merge remote-tracking branch 'upstream/master'

pull/731/head
Andrew Leedham 6 anni fa
parent
commit
3891970a3c
18 ha cambiato i file con 974 aggiunte e 73 eliminazioni
  1. +10
    -1
      .eslintrc
  2. +46
    -1
      .github/workflows/verify.yml
  3. +67
    -1
      CHANGELOG.md
  4. +0
    -5
      Rakefile
  5. +2
    -1
      bin/mustache
  6. +5
    -5
      hooks/pre-commit
  7. +27
    -22
      mustache.js
  8. +1
    -1
      mustache.js.nuspec
  9. +1
    -1
      mustache.min.js
  10. +717
    -0
      mustache.mjs
  11. +37
    -31
      package-lock.json
  12. +5
    -2
      package.json
  13. +3
    -0
      test/_files/cli.cjs
  14. +3
    -0
      test/_files/cli.js
  15. +18
    -0
      test/cli-test.js
  16. +17
    -0
      test/module-systems/deno-test.ts
  17. +12
    -0
      test/module-systems/esm-test.mjs
  18. +3
    -2
      test/render-helper.js

+ 10
- 1
.eslintrc Vedi File

@@ -16,5 +16,14 @@
"no-use-before-define": 0,
"no-process-exit": 0,
"strict": 0
}
},
"overrides": [
{
"files": ["mustache.mjs"],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2015
}
}
]
}

+ 46
- 1
.github/workflows/verify.yml Vedi File

@@ -2,7 +2,7 @@ name: Verify changes

on: [push, pull_request]

jobs:
jobs:
tests:
runs-on: ubuntu-latest

@@ -58,6 +58,25 @@ jobs:
npm install $ARCHIVE_FILENAME
node commonjs-test.js

esm-usage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '>=13.2.0'
- name: Package, install and test
run: |
export ARCHIVE_FILENAME=$(npm pack | tail -n 1)
export UNPACK_DESTINATION=$(mktemp -d)
mv $ARCHIVE_FILENAME $UNPACK_DESTINATION
cp test/module-systems/esm-test.mjs $UNPACK_DESTINATION
cd $UNPACK_DESTINATION
npm install $ARCHIVE_FILENAME
node esm-test.mjs

browser-usage:
runs-on: ubuntu-latest

@@ -71,3 +90,29 @@ jobs:
run: |
npm ci
npx mocha test/module-systems/browser-test.js

deno-usage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: denolib/setup-deno@v1
with:
deno-version: 'v0.23.0'
- run: deno --version
- run: deno test --allow-net=deno.land test/module-systems/deno-test.ts

build-output-sync:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
- name: Install, build and check git diff
run: |
npm ci
npm run build
git diff --quiet HEAD --

+ 67
- 1
CHANGELOG.md Vedi File

@@ -3,6 +3,66 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [3.2.1] / 30 December 2019

### Fixed

* [#733]: Allow the CLI to use JavaScript views when the project has ES6 modules enabled, by [@eobrain].

## [3.2.0] / 18 December 2019

### Added

* [#728]: Expose ECMAScript Module in addition to UMD (CommonJS, AMD & global scope), by [@phillipj] and [@zekth].

### Using mustache.js as an ES module

To stay backwards compatible with already using projects, the default exposed module format is still UMD.
That means projects using mustache.js as an CommonJS, AMD or global scope module, from npm or directly from github.com
can keep on doing that for now.

For those projects who would rather want to use mustache.js as an ES module, the `mustache/mustache.mjs` file has to
be `import`ed directly.

Below are some usage scenarios for different runtimes.

#### Modern browser with ES module support

```html
<!-- index.html -->
<script type="module">
import mustache from "https://unpkg.com/mustache@3.2.0/mustache.mjs"

console.log(mustache.render('Hello {{name}}!', { name: 'Santa' }))
// Hello Santa!
</script>
```

#### [Node.js](https://nodejs.org) (>= v13.2.0 or using --experimental-modules flag)

```js
// index.mjs
import mustache from 'mustache/mustache.mjs'

console.log(mustache.render('Hello {{name}}!', { name: 'Santa' }))
// Hello Santa!
```

ES Module support for Node.js will be improved in the future when [Conditional Exports](https://nodejs.org/api/esm.html#esm_conditional_exports)
is enabled by default rather than being behind an experimental flag.

More info in [Node.js ECMAScript Modules docs](https://nodejs.org/api/esm.html).

#### [Deno](https://deno.land/)

```js
// index.ts
import mustache from 'https://unpkg.com/mustache@3.2.0/mustache.mjs'

console.log(mustache.render('Hello {{name}}!', { name: 'Santa' }))
// Hello Santa!
```

## [3.1.0] / 13 September 2019

### Added
@@ -357,6 +417,8 @@ This release is made to revert changes introduced in [2.3.1] that caused unexpec
* Fixed a bug that clashed with QUnit (thanks [@kannix]).
* Added volo support (thanks [@guybedford]).

[3.2.1]: https://github.com/janl/mustache.js/compare/v3.2.0...v3.2.1
[3.2.0]: https://github.com/janl/mustache.js/compare/v3.1.0...v3.2.0
[3.1.0]: https://github.com/janl/mustache.js/compare/v3.0.3...v3.1.0
[3.0.3]: https://github.com/janl/mustache.js/compare/v3.0.2...v3.0.3
[3.0.2]: https://github.com/janl/mustache.js/compare/v3.0.1...v3.0.2
@@ -424,6 +486,8 @@ This release is made to revert changes introduced in [2.3.1] that caused unexpec
[#714]: https://github.com/janl/mustache.js/issues/714
[#716]: https://github.com/janl/mustache.js/issues/716
[#717]: https://github.com/janl/mustache.js/issues/717
[#728]: https://github.com/janl/mustache.js/issues/728
[#733]: https://github.com/janl/mustache.js/issues/733

[@afc163]: https://github.com/afc163
[@andersk]: https://github.com/andersk
@@ -434,6 +498,7 @@ This release is made to revert changes introduced in [2.3.1] that caused unexpec
[@cweider]: https://github.com/cweider
[@dasilvacontin]: https://github.com/dasilvacontin
[@djchie]: https://github.com/djchie
[@eobrain]: https://github.com/eobrain
[@EvanLovely]: https://github.com/EvanLovely
[@fallenice]: https://github.com/fallenice
[@Flaque]: https://github.com/Flaque
@@ -468,7 +533,8 @@ This release is made to revert changes introduced in [2.3.1] that caused unexpec
[@TiddoLangerak]: https://github.com/TiddoLangerak
[@tomekwi]: https://github.com/tomekwi
[@wizawu]: https://github.com/wizawu
[@wol-soft]: https://github.com/wol-soft
[@Xcrucifier]: https://github.com/Xcrucifier
[@yotammadem]: https://github.com/yotammadem
[@yousefcisco]: https://github.com/yousefcisco
[@wol-soft]: https://github.com/wol-soft
[@zekth]: https://github.com/zekth

+ 0
- 5
Rakefile Vedi File

@@ -12,11 +12,6 @@ task :test do
sh "./node_modules/.bin/mocha test"
end

desc "Make a compressed build in #{minified_file}"
task :minify do
sh "./node_modules/.bin/uglifyjs mustache.js > #{minified_file}"
end

desc "Run JSHint"
task :hint do
sh "./node_modules/.bin/jshint mustache.js"


+ 2
- 1
bin/mustache Vedi File

@@ -131,7 +131,8 @@ function isStdin (view) {
}

function isJsFile (view) {
return path.extname(view) === '.js';
var extension = path.extname(view);
return extension === '.js' || extension === '.cjs';
}

function wasNotFound (err) {


+ 5
- 5
hooks/pre-commit Vedi File

@@ -34,9 +34,9 @@ class Bumper
# if bumped, do extra stuff and notify the user
if @bumped

# minify `mustache.js` using the Rakefile task
puts "> minifying `mustache.js`..."
`rake minify`
# keep .mjs & .js|.min.js in sync
puts "> building and minifying `mustache.mjs`..."
`npm run build`

# stage files for commit
`git add package.json`
@@ -78,7 +78,7 @@ class Bumper
end

bumper = Bumper.new([
Source.new('mustache.js', /mustache.version = '([\d\.]*)'/),
Source.new('mustache.js.nuspec', /<version>([\d\.]*)<\/version>/),
Source.new('mustache.mjs', /version: '([^']+)'/),
Source.new('mustache.js.nuspec', /<version>([^<]+)<\/version>/),
])
bumper.start

+ 27
- 22
mustache.js Vedi File

@@ -1,20 +1,14 @@
/*!
* mustache.js - Logic-less {{mustache}} templates with JavaScript
* http://github.com/janl/mustache.js
*/

/*global define: false Mustache: true*/

(function defineMustache (global, factory) {
if (typeof exports === 'object' && exports && typeof exports.nodeName !== 'string') {
factory(exports); // CommonJS
} else if (typeof define === 'function' && define.amd) {
define(['exports'], factory); // AMD
} else {
global.Mustache = {};
factory(global.Mustache); // script, wsh, asp
}
}(this, function mustacheFactory (mustache) {
// This file has been generated from mustache.mjs
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.Mustache = factory());
}(this, (function () { 'use strict';

/*!
* mustache.js - Logic-less {{mustache}} templates with JavaScript
* http://github.com/janl/mustache.js
*/

var objectToString = Object.prototype.toString;
var isArray = Array.isArray || function isArrayPolyfill (object) {
@@ -546,7 +540,7 @@
*/
Writer.prototype.render = function render (template, view, partials, tags) {
var tokens = this.parse(template, tags);
var context = (view instanceof Context) ? view : new Context(view);
var context = (view instanceof Context) ? view : new Context(view, undefined);
return this.renderTokens(tokens, context, partials, template, tags);
};

@@ -668,9 +662,19 @@
return token[1];
};

mustache.name = 'mustache.js';
mustache.version = '3.1.0';
mustache.tags = [ '{{', '}}' ];
var mustache = {
name: 'mustache.js',
version: '3.2.1',
tags: [ '{{', '}}' ],
clearCache: undefined,
escape: undefined,
parse: undefined,
render: undefined,
to_html: undefined,
Scanner: undefined,
Context: undefined,
Writer: undefined
};

// All high-level mustache.* functions use this writer.
var defaultWriter = new Writer();
@@ -740,4 +744,5 @@
mustache.Writer = Writer;

return mustache;
}));

})));

+ 1
- 1
mustache.js.nuspec Vedi File

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>mustache.js</id>
<version>3.1.0</version>
<version>3.2.1</version>
<authors>mustache.js Authors</authors>
<licenseUrl>https://github.com/janl/mustache.js/blob/master/LICENSE</licenseUrl>
<projectUrl>http://mustache.github.com/</projectUrl>


+ 1
- 1
mustache.min.js
File diff soppresso perché troppo grande
Vedi File


+ 717
- 0
mustache.mjs Vedi File

@@ -0,0 +1,717 @@
/*!
* mustache.js - Logic-less {{mustache}} templates with JavaScript
* http://github.com/janl/mustache.js
*/

var objectToString = Object.prototype.toString;
var isArray = Array.isArray || function isArrayPolyfill (object) {
return objectToString.call(object) === '[object Array]';
};

function isFunction (object) {
return typeof object === 'function';
}

/**
* More correct typeof string handling array
* which normally returns typeof 'object'
*/
function typeStr (obj) {
return isArray(obj) ? 'array' : typeof obj;
}

function escapeRegExp (string) {
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
}

/**
* Null safe way of checking whether or not an object,
* including its prototype, has a given property
*/
function hasProperty (obj, propName) {
return obj != null && typeof obj === 'object' && (propName in obj);
}

/**
* Safe way of detecting whether or not the given thing is a primitive and
* whether it has the given property
*/
function primitiveHasOwnProperty (primitive, propName) {
return (
primitive != null
&& typeof primitive !== 'object'
&& primitive.hasOwnProperty
&& primitive.hasOwnProperty(propName)
);
}

// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189
var regExpTest = RegExp.prototype.test;
function testRegExp (re, string) {
return regExpTest.call(re, string);
}

var nonSpaceRe = /\S/;
function isWhitespace (string) {
return !testRegExp(nonSpaceRe, string);
}

var entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;',
'`': '&#x60;',
'=': '&#x3D;'
};

function escapeHtml (string) {
return String(string).replace(/[&<>"'`=\/]/g, function fromEntityMap (s) {
return entityMap[s];
});
}

var whiteRe = /\s*/;
var spaceRe = /\s+/;
var equalsRe = /\s*=/;
var curlyRe = /\s*\}/;
var tagRe = /#|\^|\/|>|\{|&|=|!/;

/**
* Breaks up the given `template` string into a tree of tokens. If the `tags`
* argument is given here it must be an array with two string values: the
* opening and closing tags used in the template (e.g. [ "<%", "%>" ]). Of
* course, the default is to use mustaches (i.e. mustache.tags).
*
* A token is an array with at least 4 elements. The first element is the
* mustache symbol that was used inside the tag, e.g. "#" or "&". If the tag
* did not contain a symbol (i.e. {{myValue}}) this element is "name". For
* all text that appears outside a symbol this element is "text".
*
* The second element of a token is its "value". For mustache tags this is
* whatever else was inside the tag besides the opening symbol. For text tokens
* this is the text itself.
*
* The third and fourth elements of the token are the start and end indices,
* respectively, of the token in the original template.
*
* Tokens that are the root node of a subtree contain two more elements: 1) an
* array of tokens in the subtree and 2) the index in the original template at
* which the closing tag for that section begins.
*
* Tokens for partials also contain two more elements: 1) a string value of
* indendation prior to that tag and 2) the index of that tag on that line -
* eg a value of 2 indicates the partial is the third tag on this line.
*/
function parseTemplate (template, tags) {
if (!template)
return [];
var lineHasNonSpace = false;
var sections = []; // Stack to hold section tokens
var tokens = []; // Buffer to hold the tokens
var spaces = []; // Indices of whitespace tokens on the current line
var hasTag = false; // Is there a {{tag}} on the current line?
var nonSpace = false; // Is there a non-space char on the current line?
var indentation = ''; // Tracks indentation for tags that use it
var tagIndex = 0; // Stores a count of number of tags encountered on a line

// Strips all whitespace tokens array for the current line
// if there was a {{#tag}} on it and otherwise only space.
function stripSpace () {
if (hasTag && !nonSpace) {
while (spaces.length)
delete tokens[spaces.pop()];
} else {
spaces = [];
}

hasTag = false;
nonSpace = false;
}

var openingTagRe, closingTagRe, closingCurlyRe;
function compileTags (tagsToCompile) {
if (typeof tagsToCompile === 'string')
tagsToCompile = tagsToCompile.split(spaceRe, 2);

if (!isArray(tagsToCompile) || tagsToCompile.length !== 2)
throw new Error('Invalid tags: ' + tagsToCompile);

openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*');
closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1]));
closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1]));
}

compileTags(tags || mustache.tags);

var scanner = new Scanner(template);

var start, type, value, chr, token, openSection;
while (!scanner.eos()) {
start = scanner.pos;

// Match any text between tags.
value = scanner.scanUntil(openingTagRe);

if (value) {
for (var i = 0, valueLength = value.length; i < valueLength; ++i) {
chr = value.charAt(i);

if (isWhitespace(chr)) {
spaces.push(tokens.length);
indentation += chr;
} else {
nonSpace = true;
lineHasNonSpace = true;
indentation += ' ';
}

tokens.push([ 'text', chr, start, start + 1 ]);
start += 1;

// Check for whitespace on the current line.
if (chr === '\n') {
stripSpace();
indentation = '';
tagIndex = 0;
lineHasNonSpace = false;
}
}
}

// Match the opening tag.
if (!scanner.scan(openingTagRe))
break;

hasTag = true;

// Get the tag type.
type = scanner.scan(tagRe) || 'name';
scanner.scan(whiteRe);

// Get the tag value.
if (type === '=') {
value = scanner.scanUntil(equalsRe);
scanner.scan(equalsRe);
scanner.scanUntil(closingTagRe);
} else if (type === '{') {
value = scanner.scanUntil(closingCurlyRe);
scanner.scan(curlyRe);
scanner.scanUntil(closingTagRe);
type = '&';
} else {
value = scanner.scanUntil(closingTagRe);
}

// Match the closing tag.
if (!scanner.scan(closingTagRe))
throw new Error('Unclosed tag at ' + scanner.pos);

if (type == '>') {
token = [ type, value, start, scanner.pos, indentation, tagIndex, lineHasNonSpace ];
} else {
token = [ type, value, start, scanner.pos ];
}
tagIndex++;
tokens.push(token);

if (type === '#' || type === '^') {
sections.push(token);
} else if (type === '/') {
// Check section nesting.
openSection = sections.pop();

if (!openSection)
throw new Error('Unopened section "' + value + '" at ' + start);

if (openSection[1] !== value)
throw new Error('Unclosed section "' + openSection[1] + '" at ' + start);
} else if (type === 'name' || type === '{' || type === '&') {
nonSpace = true;
} else if (type === '=') {
// Set the tags for the next time around.
compileTags(value);
}
}

stripSpace();

// Make sure there are no open sections when we're done.
openSection = sections.pop();

if (openSection)
throw new Error('Unclosed section "' + openSection[1] + '" at ' + scanner.pos);

return nestTokens(squashTokens(tokens));
}

/**
* Combines the values of consecutive text tokens in the given `tokens` array
* to a single token.
*/
function squashTokens (tokens) {
var squashedTokens = [];

var token, lastToken;
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
token = tokens[i];

if (token) {
if (token[0] === 'text' && lastToken && lastToken[0] === 'text') {
lastToken[1] += token[1];
lastToken[3] = token[3];
} else {
squashedTokens.push(token);
lastToken = token;
}
}
}

return squashedTokens;
}

/**
* Forms the given array of `tokens` into a nested tree structure where
* tokens that represent a section have two additional items: 1) an array of
* all tokens that appear in that section and 2) the index in the original
* template that represents the end of that section.
*/
function nestTokens (tokens) {
var nestedTokens = [];
var collector = nestedTokens;
var sections = [];

var token, section;
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
token = tokens[i];

switch (token[0]) {
case '#':
case '^':
collector.push(token);
sections.push(token);
collector = token[4] = [];
break;
case '/':
section = sections.pop();
section[5] = token[2];
collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
break;
default:
collector.push(token);
}
}

return nestedTokens;
}

/**
* A simple string scanner that is used by the template parser to find
* tokens in template strings.
*/
function Scanner (string) {
this.string = string;
this.tail = string;
this.pos = 0;
}

/**
* Returns `true` if the tail is empty (end of string).
*/
Scanner.prototype.eos = function eos () {
return this.tail === '';
};

/**
* Tries to match the given regular expression at the current position.
* Returns the matched text if it can match, the empty string otherwise.
*/
Scanner.prototype.scan = function scan (re) {
var match = this.tail.match(re);

if (!match || match.index !== 0)
return '';

var string = match[0];

this.tail = this.tail.substring(string.length);
this.pos += string.length;

return string;
};

/**
* Skips all text until the given regular expression can be matched. Returns
* the skipped string, which is the entire tail if no match can be made.
*/
Scanner.prototype.scanUntil = function scanUntil (re) {
var index = this.tail.search(re), match;

switch (index) {
case -1:
match = this.tail;
this.tail = '';
break;
case 0:
match = '';
break;
default:
match = this.tail.substring(0, index);
this.tail = this.tail.substring(index);
}

this.pos += match.length;

return match;
};

/**
* Represents a rendering context by wrapping a view object and
* maintaining a reference to the parent context.
*/
function Context (view, parentContext) {
this.view = view;
this.cache = { '.': this.view };
this.parent = parentContext;
}

/**
* Creates a new context using the given view with this context
* as the parent.
*/
Context.prototype.push = function push (view) {
return new Context(view, this);
};

/**
* Returns the value of the given name in this context, traversing
* up the context hierarchy if the value is absent in this context's view.
*/
Context.prototype.lookup = function lookup (name) {
var cache = this.cache;

var value;
if (cache.hasOwnProperty(name)) {
value = cache[name];
} else {
var context = this, intermediateValue, names, index, lookupHit = false;

while (context) {
if (name.indexOf('.') > 0) {
intermediateValue = context.view;
names = name.split('.');
index = 0;

/**
* Using the dot notion path in `name`, we descend through the
* nested objects.
*
* To be certain that the lookup has been successful, we have to
* check if the last object in the path actually has the property
* we are looking for. We store the result in `lookupHit`.
*
* This is specially necessary for when the value has been set to
* `undefined` and we want to avoid looking up parent contexts.
*
* In the case where dot notation is used, we consider the lookup
* to be successful even if the last "object" in the path is
* not actually an object but a primitive (e.g., a string, or an
* integer), because it is sometimes useful to access a property
* of an autoboxed primitive, such as the length of a string.
**/
while (intermediateValue != null && index < names.length) {
if (index === names.length - 1)
lookupHit = (
hasProperty(intermediateValue, names[index])
|| primitiveHasOwnProperty(intermediateValue, names[index])
);

intermediateValue = intermediateValue[names[index++]];
}
} else {
intermediateValue = context.view[name];

/**
* Only checking against `hasProperty`, which always returns `false` if
* `context.view` is not an object. Deliberately omitting the check
* against `primitiveHasOwnProperty` if dot notation is not used.
*
* Consider this example:
* ```
* Mustache.render("The length of a football field is {{#length}}{{length}}{{/length}}.", {length: "100 yards"})
* ```
*
* If we were to check also against `primitiveHasOwnProperty`, as we do
* in the dot notation case, then render call would return:
*
* "The length of a football field is 9."
*
* rather than the expected:
*
* "The length of a football field is 100 yards."
**/
lookupHit = hasProperty(context.view, name);
}

if (lookupHit) {
value = intermediateValue;
break;
}

context = context.parent;
}

cache[name] = value;
}

if (isFunction(value))
value = value.call(this.view);

return value;
};

/**
* A Writer knows how to take a stream of tokens and render them to a
* string, given a context. It also maintains a cache of templates to
* avoid the need to parse the same template twice.
*/
function Writer () {
this.cache = {};
}

/**
* Clears all cached templates in this writer.
*/
Writer.prototype.clearCache = function clearCache () {
this.cache = {};
};

/**
* Parses and caches the given `template` according to the given `tags` or
* `mustache.tags` if `tags` is omitted, and returns the array of tokens
* that is generated from the parse.
*/
Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache;
var cacheKey = template + ':' + (tags || mustache.tags).join(':');
var tokens = cache[cacheKey];

if (tokens == null)
tokens = cache[cacheKey] = parseTemplate(template, tags);

return tokens;
};

/**
* High-level method that is used to render the given `template` with
* the given `view`.
*
* The optional `partials` argument may be an object that contains the
* names and templates of partials that are used in the template. It may
* also be a function that is used to load partial templates on the fly
* that takes a single argument: the name of the partial.
*
* If the optional `tags` argument is given here it must be an array with two
* string values: the opening and closing tags used in the template (e.g.
* [ "<%", "%>" ]). The default is to mustache.tags.
*/
Writer.prototype.render = function render (template, view, partials, tags) {
var tokens = this.parse(template, tags);
var context = (view instanceof Context) ? view : new Context(view, undefined);
return this.renderTokens(tokens, context, partials, template, tags);
};

/**
* Low-level method that renders the given array of `tokens` using
* the given `context` and `partials`.
*
* Note: The `originalTemplate` is only ever used to extract the portion
* of the original template that was contained in a higher-order section.
* If the template doesn't use higher-order sections, this argument may
* be omitted.
*/
Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, tags) {
var buffer = '';

var token, symbol, value;
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
value = undefined;
token = tokens[i];
symbol = token[0];

if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
else if (symbol === '>') value = this.renderPartial(token, context, partials, tags);
else if (symbol === '&') value = this.unescapedValue(token, context);
else if (symbol === 'name') value = this.escapedValue(token, context);
else if (symbol === 'text') value = this.rawValue(token);

if (value !== undefined)
buffer += value;
}

return buffer;
};

Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
var self = this;
var buffer = '';
var value = context.lookup(token[1]);

// This function is used to render an arbitrary template
// in the current context by higher-order sections.
function subRender (template) {
return self.render(template, context, partials);
}

if (!value) return;

if (isArray(value)) {
for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
}
} else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
} else if (isFunction(value)) {
if (typeof originalTemplate !== 'string')
throw new Error('Cannot use higher-order sections without the original template');

// Extract the portion of the original template that the section contains.
value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender);

if (value != null)
buffer += value;
} else {
buffer += this.renderTokens(token[4], context, partials, originalTemplate);
}
return buffer;
};

Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
var value = context.lookup(token[1]);

// Use JavaScript's definition of falsy. Include empty arrays.
// See https://github.com/janl/mustache.js/issues/186
if (!value || (isArray(value) && value.length === 0))
return this.renderTokens(token[4], context, partials, originalTemplate);
};

Writer.prototype.indentPartial = function indentPartial (partial, indentation, lineHasNonSpace) {
var filteredIndentation = indentation.replace(/[^ \t]/g, '');
var partialByNl = partial.split('\n');
for (var i = 0; i < partialByNl.length; i++) {
if (partialByNl[i].length && (i > 0 || !lineHasNonSpace)) {
partialByNl[i] = filteredIndentation + partialByNl[i];
}
}
return partialByNl.join('\n');
};

Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags) {
if (!partials) return;

var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
if (value != null) {
var lineHasNonSpace = token[6];
var tagIndex = token[5];
var indentation = token[4];
var indentedValue = value;
if (tagIndex == 0 && indentation) {
indentedValue = this.indentPartial(value, indentation, lineHasNonSpace);
}
return this.renderTokens(this.parse(indentedValue, tags), context, partials, indentedValue);
}
};

Writer.prototype.unescapedValue = function unescapedValue (token, context) {
var value = context.lookup(token[1]);
if (value != null)
return value;
};

Writer.prototype.escapedValue = function escapedValue (token, context) {
var value = context.lookup(token[1]);
if (value != null)
return mustache.escape(value);
};

Writer.prototype.rawValue = function rawValue (token) {
return token[1];
};

var mustache = {
name: 'mustache.js',
version: '3.2.1',
tags: [ '{{', '}}' ],
clearCache: undefined,
escape: undefined,
parse: undefined,
render: undefined,
to_html: undefined,
Scanner: undefined,
Context: undefined,
Writer: undefined
};

// All high-level mustache.* functions use this writer.
var defaultWriter = new Writer();

/**
* Clears all cached templates in the default writer.
*/
mustache.clearCache = function clearCache () {
return defaultWriter.clearCache();
};

/**
* Parses and caches the given template in the default writer and returns the
* array of tokens it contains. Doing this ahead of time avoids the need to
* parse templates on the fly as they are rendered.
*/
mustache.parse = function parse (template, tags) {
return defaultWriter.parse(template, tags);
};

/**
* Renders the `template` with the given `view` and `partials` using the
* default writer. If the optional `tags` argument is given here it must be an
* array with two string values: the opening and closing tags used in the
* template (e.g. [ "<%", "%>" ]). The default is to mustache.tags.
*/
mustache.render = function render (template, view, partials, tags) {
if (typeof template !== 'string') {
throw new TypeError('Invalid template! Template should be a "string" ' +
'but "' + typeStr(template) + '" was given as the first ' +
'argument for mustache#render(template, view, partials)');
}

return defaultWriter.render(template, view, partials, tags);
};

// This is here for backwards compatibility with 0.4.x.,
/*eslint-disable */ // eslint wants camel cased function name
mustache.to_html = function to_html (template, view, partials, send) {
/*eslint-enable*/

var result = mustache.render(template, view, partials);

if (isFunction(send)) {
send(result);
} else {
return result;
}
};

// Export the escaping function so that the user may override it.
// See https://github.com/janl/mustache.js/issues/244
mustache.escape = escapeHtml;

// Export these mainly for testing, but also for advanced usage.
mustache.Scanner = Scanner;
mustache.Context = Context;
mustache.Writer = Writer;

export default mustache;

+ 37
- 31
package-lock.json Vedi File

@@ -1,6 +1,6 @@
{
"name": "mustache",
"version": "3.1.0",
"version": "3.2.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -32,6 +32,12 @@
}
}
},
"@types/estree": {
"version": "0.0.39",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz",
"integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==",
"dev": true
},
"@types/node": {
"version": "8.10.52",
"resolved": "https://registry.npmjs.org/@types/node/-/node-8.10.52.tgz",
@@ -2899,8 +2905,7 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"aproba": {
"version": "1.2.0",
@@ -2921,14 +2926,12 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@@ -2943,20 +2946,17 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@@ -3073,8 +3073,7 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"ini": {
"version": "1.3.5",
@@ -3086,7 +3085,6 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@@ -3101,7 +3099,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@@ -3109,14 +3106,12 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
@@ -3135,7 +3130,6 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@@ -3216,8 +3210,7 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"object-assign": {
"version": "4.1.1",
@@ -3229,7 +3222,6 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@@ -3315,8 +3307,7 @@
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
@@ -3352,7 +3343,6 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@@ -3372,7 +3362,6 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@@ -3416,14 +3405,12 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
}
}
},
@@ -6771,6 +6758,25 @@
"inherits": "^2.0.1"
}
},
"rollup": {
"version": "1.26.3",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-1.26.3.tgz",
"integrity": "sha512-8MhY/M8gnv3Q/pQQSWYWzbeJ5J1C5anCNY5BK1kV8Yzw9RFS0FF4lbLt+uyPO3wLKWXSXrhAL5pWL85TZAh+Sw==",
"dev": true,
"requires": {
"@types/estree": "*",
"@types/node": "*",
"acorn": "^7.1.0"
},
"dependencies": {
"acorn": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz",
"integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==",
"dev": true
}
}
},
"run-async": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/run-async/-/run-async-2.3.0.tgz",


+ 5
- 2
package.json Vedi File

@@ -1,6 +1,6 @@
{
"name": "mustache",
"version": "3.1.0",
"version": "3.2.1",
"description": "Logic-less {{mustache}} templates with JavaScript",
"author": "mustache.js Authors <http://github.com/janl/mustache.js>",
"homepage": "https://github.com/janl/mustache.js",
@@ -20,6 +20,7 @@
},
"files": [
"mustache.js",
"mustache.mjs",
"mustache.min.js",
"bin",
"wrappers",
@@ -32,8 +33,9 @@
"npm": ">=1.4.0"
},
"scripts": {
"build": "rollup mustache.mjs --file mustache.js --format umd --name Mustache --banner '// This file has been generated from mustache.mjs' && uglifyjs mustache.js > mustache.min.js",
"test": "npm run test-lint && npm run test-unit",
"test-lint": "eslint mustache.js bin/mustache test/**/*.js",
"test-lint": "eslint mustache.mjs bin/mustache test/**/*.js",
"test-unit": "mocha --reporter spec test/*-test.js",
"test-render": "mocha --reporter spec test/render-test",
"pre-test-browser": "node test/create-browser-suite.js",
@@ -46,6 +48,7 @@
"jshint": "^2.9.5",
"mocha": "^3.0.2",
"puppeteer": "^2.0.0",
"rollup": "^1.26.3",
"uglify-js": "^3.4.6",
"zuul": "^3.11.0",
"zuul-ngrok": "nolanlawson/zuul-ngrok#patch-1"


+ 3
- 0
test/_files/cli.cjs Vedi File

@@ -0,0 +1,3 @@
module.exports = {
name: 'LeBron'
};

+ 3
- 0
test/_files/cli.js Vedi File

@@ -0,0 +1,3 @@
module.exports = {
name: 'LeBron'
};

+ 18
- 0
test/cli-test.js Vedi File

@@ -76,6 +76,24 @@ describe('Mustache CLI', function () {
});
});

it('can handle view written in JavaScript with .js suffix', function (done) {
exec('bin/mustache test/_files/cli.js test/_files/cli.mustache', function (err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});

it('can handle view written in JavaScript with .cjs suffix', function (done) {
exec('bin/mustache test/_files/cli.cjs test/_files/cli.mustache', function (err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});

it('writes rendered template into the file specified by the third argument', function (done) {
var outputFile = 'test/_files/cli_output.txt';
exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function (err, stdout, stderr) {


+ 17
- 0
test/module-systems/deno-test.ts Vedi File

@@ -0,0 +1,17 @@
import { test } from "https://deno.land/std@v0.21.0/testing/mod.ts";
import { assertEquals } from "https://deno.land/std@v0.21.0/testing/asserts.ts";
import mustache from "../../mustache.mjs";

const view = {
title: "Joe",
calc: function() {
return 2 + 4;
}
};

test(function canUseMustache() {
assertEquals(
mustache.render("{{title}} spends {{calc}}", view),
"Joe spends 6"
);
});

+ 12
- 0
test/module-systems/esm-test.mjs Vedi File

@@ -0,0 +1,12 @@
import assert from 'assert';
import mustache from 'mustache/mustache.mjs';

const view = {
title: 'Joe',
calc: () => 2 + 4
};

assert.strictEqual(
mustache.render('{{title}} spends {{calc}}', view),
'Joe spends 6'
);

+ 3
- 2
test/render-helper.js Vedi File

@@ -13,6 +13,7 @@ function getContents (testName, ext) {

function getView (testName) {
var view = getContents(testName, 'js');
if (!view) view = getContents(testName, 'cjs');
if (!view) throw new Error('Cannot find view for test "' + testName + '"');
return view;
}
@@ -34,9 +35,9 @@ if (testToRun) {
testNames = testToRun.split(',');
} else {
testNames = fs.readdirSync(_files).filter(function (file) {
return (/\.js$/).test(file);
return (/\.c?js$/).test(file);
}).map(function (file) {
return path.basename(file).replace(/\.js$/, '');
return path.basename(file).replace(/\.c?js$/, '');
});
}



Loading…
Annulla
Salva