Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dbe04946ce | |||
| d069c1caf1 | |||
| 6714e07a6a | |||
| dce542c9a6 | |||
| 8a41389ba5 | |||
| 68d8df5a88 | |||
| b2a083136b | |||
| 9f98c16298 | |||
| 45443b4290 | |||
| 8841a5f6d3 | |||
| e0137c26f2 | |||
| e914d6037f | |||
| 7de4b41c34 | |||
| eab1d141cb | |||
| de4414d7fc | |||
| 08fddee033 | |||
| 4512766919 | |||
| e497a35d7f | |||
| 8c9f866655 | |||
| 520e1d5f08 | |||
| 02423780a9 | |||
| be92d2f254 | |||
| 443a613b3a | |||
| 83ee5908f2 | |||
| 8dc3d2517b | |||
| 668c4fb878 | |||
| c65c6cce3f | |||
| 3d3796c1e9 | |||
| 075b354a3b | |||
| 30dbf04781 | |||
| e3a54485db | |||
| 8e23642ea2 | |||
| 88ac06875f | |||
| c68bc08a0d | |||
| 6cfbc2653a | |||
| b65135acef |
@@ -0,0 +1,24 @@
|
|||||||
|
.rvmrc
|
||||||
|
.DS_Store
|
||||||
|
/tmp/
|
||||||
|
*.sublime-project
|
||||||
|
*.sublime-workspace
|
||||||
|
npm-debug.log
|
||||||
|
.idea
|
||||||
|
yarn-error.log
|
||||||
|
node_modules
|
||||||
|
/handlebars-release.tgz
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
lib/handlebars/compiler/parser.js
|
||||||
|
/coverage/
|
||||||
|
/dist/
|
||||||
|
/tests/integration/*/dist/
|
||||||
|
|
||||||
|
# Third-party or files that must remain unchanged
|
||||||
|
/spec/expected/
|
||||||
|
/spec/vendor
|
||||||
|
|
||||||
|
# JS-Snippets
|
||||||
|
src/*.js
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
module.exports = {
|
||||||
|
extends: ['eslint:recommended', 'plugin:compat/recommended', 'prettier'],
|
||||||
|
globals: {
|
||||||
|
self: false
|
||||||
|
},
|
||||||
|
env: {
|
||||||
|
node: true,
|
||||||
|
es6: true
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
'no-console': 'warn',
|
||||||
|
|
||||||
|
// temporarily disabled until the violating places are fixed.
|
||||||
|
'no-func-assign': 'off',
|
||||||
|
'no-sparse-arrays': 'off',
|
||||||
|
|
||||||
|
// Best Practices //
|
||||||
|
//----------------//
|
||||||
|
'default-case': 'warn',
|
||||||
|
'guard-for-in': 'warn',
|
||||||
|
'no-alert': 'error',
|
||||||
|
'no-caller': 'error',
|
||||||
|
'no-div-regex': 'warn',
|
||||||
|
'no-eval': 'error',
|
||||||
|
'no-extend-native': 'error',
|
||||||
|
'no-extra-bind': 'error',
|
||||||
|
'no-floating-decimal': 'error',
|
||||||
|
'no-implied-eval': 'error',
|
||||||
|
'no-iterator': 'error',
|
||||||
|
'no-labels': 'error',
|
||||||
|
'no-lone-blocks': 'error',
|
||||||
|
'no-loop-func': 'error',
|
||||||
|
'no-multi-str': 'warn',
|
||||||
|
'no-global-assign': 'error',
|
||||||
|
'no-new': 'error',
|
||||||
|
'no-new-func': 'error',
|
||||||
|
'no-new-wrappers': 'error',
|
||||||
|
'no-octal-escape': 'error',
|
||||||
|
'no-process-env': 'error',
|
||||||
|
'no-proto': 'error',
|
||||||
|
'no-return-assign': 'error',
|
||||||
|
'no-script-url': 'error',
|
||||||
|
'no-self-compare': 'error',
|
||||||
|
'no-sequences': 'error',
|
||||||
|
'no-throw-literal': 'error',
|
||||||
|
'no-unused-expressions': 'error',
|
||||||
|
'no-warning-comments': 'warn',
|
||||||
|
'no-with': 'error',
|
||||||
|
radix: 'error',
|
||||||
|
|
||||||
|
// Variables //
|
||||||
|
//-----------//
|
||||||
|
'no-label-var': 'error',
|
||||||
|
'no-undef-init': 'error',
|
||||||
|
'no-use-before-define': ['error', 'nofunc'],
|
||||||
|
|
||||||
|
// ECMAScript 6 //
|
||||||
|
//--------------//
|
||||||
|
'no-var': 'error'
|
||||||
|
},
|
||||||
|
parserOptions: {
|
||||||
|
sourceType: 'module',
|
||||||
|
ecmaVersion: 6,
|
||||||
|
ecmaFeatures: {}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
# Upgrade to Prettier 2.7
|
|
||||||
3d228334530860a6e3f99dc10777c84bf22292c1
|
|
||||||
# Format markdown files with Prettier
|
|
||||||
dfe2eaaf20f0b679d94e5a799757c4394d80f1cc
|
|
||||||
# migrate to oxlint and oxfmt
|
|
||||||
0c1d00282ca619c3416ad819bdf53c0852b32415
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
Before filing issues, please check the following points first:
|
|
||||||
|
|
||||||
- [ ] Please don't open issues for security issues. Instead, file a report at https://www.npmjs.com/advisories/report?package=handlebars
|
|
||||||
- [ ] Have a look at https://github.com/handlebars-lang/handlebars.js/blob/master/CONTRIBUTING.md
|
|
||||||
- [ ] Read the FAQ at https://github.com/handlebars-lang/handlebars.js/blob/master/FAQ.md
|
|
||||||
- [ ] Use the jsfiddle-template at https://jsfiddle.net/4nbwjaqz/4/ to reproduce problems or bugs
|
|
||||||
|
|
||||||
This will probably help you to get a solution faster.
|
|
||||||
For bugs, it would be great to have a PR with a failing test-case.
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
Before creating a pull-request, please check https://github.com/handlebars-lang/handlebars.js/blob/master/CONTRIBUTING.md first.
|
|
||||||
|
|
||||||
Generally we like to see pull requests that
|
|
||||||
|
|
||||||
- [ ] Please don't start pull requests for security issues. Instead, file a report at https://www.npmjs.com/advisories/report?package=handlebars
|
|
||||||
- [ ] Maintain the existing code style
|
|
||||||
- [ ] Are focused on a single change (i.e. avoid large refactoring or style adjustments in untouched code if not the primary goal of the pull request)
|
|
||||||
- [ ] Have good commit messages
|
|
||||||
- [ ] Have tests
|
|
||||||
- [ ] Have the [typings](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html) (types/index.d.ts) updated on every API change. If you need help, updating those, please mention that in the PR description.
|
|
||||||
- [ ] Don't significantly decrease the current code coverage (see coverage/lcov-report/index.html)
|
|
||||||
- [ ] Please target the `master` branch in the PR.
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
version: 2
|
version: 2
|
||||||
updates:
|
updates:
|
||||||
- package-ecosystem: npm
|
- package-ecosystem: npm
|
||||||
directory: '/'
|
directory: "/"
|
||||||
open-pull-requests-limit: 0
|
open-pull-requests-limit: 0
|
||||||
schedule:
|
schedule:
|
||||||
interval: weekly
|
interval: weekly
|
||||||
|
|||||||
+18
-20
@@ -12,12 +12,12 @@ jobs:
|
|||||||
runs-on: 'ubuntu-latest'
|
runs-on: 'ubuntu-latest'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: '24'
|
node-version: '16'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
@@ -33,16 +33,16 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
operating-system: ['ubuntu-latest', 'windows-latest']
|
operating-system: ['ubuntu-latest', 'windows-latest']
|
||||||
# https://nodejs.org/en/about/releases/
|
# https://nodejs.org/en/about/releases/
|
||||||
node-version: ['20', '22', '24']
|
node-version: ['16', '18', '20', '22']
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: ${{ matrix.node-version }}
|
node-version: ${{ matrix.node-version }}
|
||||||
|
|
||||||
@@ -52,27 +52,27 @@ jobs:
|
|||||||
- name: Test
|
- name: Test
|
||||||
run: npm run test
|
run: npm run test
|
||||||
|
|
||||||
- name: Test (Publish)
|
|
||||||
if: matrix.node-version != '20'
|
|
||||||
run: npx vitest run --project publish
|
|
||||||
|
|
||||||
- name: Test (Integration)
|
- name: Test (Integration)
|
||||||
if: matrix.operating-system == 'ubuntu-latest'
|
# https://github.com/webpack/webpack/issues/14532
|
||||||
run: npm run test:integration
|
if: ${{ matrix.node-version == '16' }}
|
||||||
|
run: |
|
||||||
|
cd ./tests/integration/rollup-test && ./test.sh && cd -
|
||||||
|
cd ./tests/integration/webpack-babel-test && ./test.sh && cd -
|
||||||
|
cd ./tests/integration/webpack-test && ./test.sh && cd -
|
||||||
|
|
||||||
browser:
|
browser:
|
||||||
name: Test (Browser)
|
name: Test (Browser)
|
||||||
runs-on: ubuntu-latest
|
runs-on: 'ubuntu-22.04'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: '24'
|
node-version: '16'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
@@ -83,9 +83,7 @@ jobs:
|
|||||||
npx playwright install
|
npx playwright install
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: npm run build
|
run: npx grunt prepare
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: npm run test:browser
|
||||||
npm run test:browser-smoke
|
|
||||||
npm run test:browser
|
|
||||||
|
|||||||
@@ -15,14 +15,14 @@ jobs:
|
|||||||
environment: 'builds.handlebarsjs.com.s3.amazonaws.com'
|
environment: 'builds.handlebarsjs.com.s3.amazonaws.com'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v2
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: '24'
|
node-version: '16'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
@@ -33,7 +33,6 @@ jobs:
|
|||||||
git config --global user.name "handlebars-lang"
|
git config --global user.name "handlebars-lang"
|
||||||
npm run publish:aws
|
npm run publish:aws
|
||||||
env:
|
env:
|
||||||
S3_BUCKET_NAME: 'builds.handlebarsjs.com'
|
S3_BUCKET_NAME: "builds.handlebarsjs.com"
|
||||||
S3_REGION: 'us-east-1'
|
|
||||||
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
|
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
|
||||||
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
|
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
|
||||||
|
|||||||
+2
-1
@@ -12,8 +12,9 @@ node_modules
|
|||||||
.nyc_output
|
.nyc_output
|
||||||
|
|
||||||
# Generated files
|
# Generated files
|
||||||
|
lib/handlebars/compiler/parser.js
|
||||||
/coverage/
|
/coverage/
|
||||||
/dist/
|
/dist/
|
||||||
/tests/bench/results/
|
/test-results/
|
||||||
/tests/integration/*/dist/
|
/tests/integration/*/dist/
|
||||||
/spec/tmp/*
|
/spec/tmp/*
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://raw.githubusercontent.com/nicolo-ribaudo/oxfmt-config-schema/refs/heads/main/schema.json",
|
|
||||||
"singleQuote": true,
|
|
||||||
"tabWidth": 2,
|
|
||||||
"semi": true,
|
|
||||||
"trailingComma": "es5",
|
|
||||||
"printWidth": 80,
|
|
||||||
"ignorePatterns": [
|
|
||||||
".rvmrc",
|
|
||||||
".DS_Store",
|
|
||||||
"/tmp/",
|
|
||||||
"*.sublime-project",
|
|
||||||
"*.sublime-workspace",
|
|
||||||
"npm-debug.log",
|
|
||||||
"sauce_connect.log*",
|
|
||||||
".idea",
|
|
||||||
"yarn-error.log",
|
|
||||||
"/coverage/",
|
|
||||||
".nyc_output/",
|
|
||||||
"/dist/",
|
|
||||||
"/tests/integration/*/dist/",
|
|
||||||
"/spec/expected/",
|
|
||||||
"/spec/mustache",
|
|
||||||
"/spec/vendor",
|
|
||||||
"*.handlebars",
|
|
||||||
"*.hbs"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
-150
@@ -1,150 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://raw.githubusercontent.com/oxc-project/oxc/main/npm/oxlint/configuration_schema.json",
|
|
||||||
"plugins": ["eslint", "typescript", "unicorn", "oxc", "node", "vitest"],
|
|
||||||
"categories": {
|
|
||||||
"correctness": "error"
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"no-console": "warn",
|
|
||||||
"no-func-assign": "off",
|
|
||||||
"no-sparse-arrays": "off",
|
|
||||||
|
|
||||||
"default-case": "warn",
|
|
||||||
"guard-for-in": "warn",
|
|
||||||
"no-alert": "error",
|
|
||||||
"no-caller": "error",
|
|
||||||
"no-div-regex": "warn",
|
|
||||||
"no-eval": "error",
|
|
||||||
"no-extend-native": "error",
|
|
||||||
"no-extra-bind": "error",
|
|
||||||
"no-implied-eval": "error",
|
|
||||||
"no-iterator": "error",
|
|
||||||
"no-labels": "error",
|
|
||||||
"no-lone-blocks": "error",
|
|
||||||
"no-loop-func": "error",
|
|
||||||
"no-multi-str": "warn",
|
|
||||||
"no-global-assign": "error",
|
|
||||||
"no-new": "error",
|
|
||||||
"no-new-func": "error",
|
|
||||||
"no-new-wrappers": "error",
|
|
||||||
"no-proto": "error",
|
|
||||||
"no-return-assign": "error",
|
|
||||||
"no-script-url": "error",
|
|
||||||
"no-self-compare": "error",
|
|
||||||
"no-sequences": "error",
|
|
||||||
"no-throw-literal": "error",
|
|
||||||
"no-unused-expressions": "error",
|
|
||||||
"no-warning-comments": "warn",
|
|
||||||
"no-with": "error",
|
|
||||||
"radix": "error",
|
|
||||||
|
|
||||||
"no-label-var": "error",
|
|
||||||
"no-use-before-define": ["error", { "functions": false }],
|
|
||||||
|
|
||||||
"no-var": "error",
|
|
||||||
|
|
||||||
"node/no-process-env": "error"
|
|
||||||
},
|
|
||||||
"ignorePatterns": [
|
|
||||||
"tmp/",
|
|
||||||
"dist/",
|
|
||||||
"coverage/",
|
|
||||||
".nyc_output/",
|
|
||||||
"handlebars-release.tgz",
|
|
||||||
"tests/integration/*/dist/",
|
|
||||||
"spec/expected/",
|
|
||||||
"spec/mustache",
|
|
||||||
"spec/vendor",
|
|
||||||
"node_modules",
|
|
||||||
"types/"
|
|
||||||
],
|
|
||||||
"overrides": [
|
|
||||||
{
|
|
||||||
"files": ["lib/**/*.js"],
|
|
||||||
"env": {
|
|
||||||
"node": false,
|
|
||||||
"browser": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": ["spec/**/*.js"],
|
|
||||||
"globals": {
|
|
||||||
"CompilerContext": "readonly",
|
|
||||||
"Handlebars": "writable",
|
|
||||||
"handlebarsEnv": "readonly",
|
|
||||||
"expectTemplate": "readonly",
|
|
||||||
"suite": "readonly",
|
|
||||||
"test": "readonly",
|
|
||||||
"testBoth": "readonly",
|
|
||||||
"raises": "readonly",
|
|
||||||
"deepEqual": "readonly",
|
|
||||||
"start": "readonly",
|
|
||||||
"stop": "readonly",
|
|
||||||
"ok": "readonly",
|
|
||||||
"vi": "readonly",
|
|
||||||
"strictEqual": "readonly",
|
|
||||||
"define": "readonly",
|
|
||||||
"expect": "readonly",
|
|
||||||
"beforeEach": "readonly",
|
|
||||||
"afterEach": "readonly",
|
|
||||||
"describe": "readonly",
|
|
||||||
"it": "readonly"
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"no-var": "off",
|
|
||||||
"dot-notation": "off",
|
|
||||||
"vitest/no-conditional-tests": "off"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": ["tasks/**/*.js"],
|
|
||||||
"rules": {
|
|
||||||
"node/no-process-env": "off",
|
|
||||||
"prefer-const": "warn",
|
|
||||||
"dot-notation": "error"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": ["tasks/tests/**/*.js"],
|
|
||||||
"globals": {
|
|
||||||
"describe": "readonly",
|
|
||||||
"it": "readonly",
|
|
||||||
"expect": "readonly",
|
|
||||||
"beforeEach": "readonly",
|
|
||||||
"afterEach": "readonly",
|
|
||||||
"vi": "readonly"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": ["tests/bench/**/*.js"],
|
|
||||||
"rules": {
|
|
||||||
"no-console": "off"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": ["tests/integration/multi-nodejs-test/**/*.js"],
|
|
||||||
"rules": {
|
|
||||||
"no-console": "off",
|
|
||||||
"no-var": "off"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": ["tests/browser/**/*.js"],
|
|
||||||
"env": {
|
|
||||||
"browser": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"files": [
|
|
||||||
"tests/integration/webpack-babel-test/src/**/*.js",
|
|
||||||
"tests/integration/webpack-test/src/**/*.js"
|
|
||||||
],
|
|
||||||
"env": {
|
|
||||||
"browser": true
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"no-var": "off"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
.rvmrc
|
||||||
|
.DS_Store
|
||||||
|
/tmp/
|
||||||
|
*.sublime-project
|
||||||
|
*.sublime-workspace
|
||||||
|
npm-debug.log
|
||||||
|
sauce_connect.log*
|
||||||
|
.idea
|
||||||
|
yarn-error.log
|
||||||
|
node_modules
|
||||||
|
/handlebars-release.tgz
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
lib/handlebars/compiler/parser.js
|
||||||
|
/coverage/
|
||||||
|
/dist/
|
||||||
|
/tests/integration/*/dist/
|
||||||
|
|
||||||
|
# Third-party or files that must remain unchanged
|
||||||
|
/spec/expected/
|
||||||
|
/spec/vendor
|
||||||
+111
-28
@@ -1,16 +1,10 @@
|
|||||||
# How to Contribute
|
# How to Contribute
|
||||||
|
|
||||||
## Reporting Security Issues
|
|
||||||
|
|
||||||
Please refer to our [Security Policy](https://github.com/handlebars-lang/handlebars.js/blob/master/SECURITY.md).
|
|
||||||
|
|
||||||
## Reporting Issues
|
## Reporting Issues
|
||||||
|
|
||||||
Please refer to our [FAQ](https://github.com/handlebars-lang/handlebars.js/blob/master/FAQ.md) for common issues that people run into.
|
Please see our [FAQ](https://github.com/handlebars-lang/handlebars.js/blob/master/FAQ.md) for common issues that people run into.
|
||||||
|
|
||||||
Should you run into other issues with the project, please don't hesitate to let us know by filing an [issue][issue]!
|
Should you run into other issues with the project, please don't hesitate to let us know by filing an [issue][issue]! In general we are going to ask for an example of the problem failing, which can be as simple as a jsfiddle/jsbin/etc. We've put together a jsfiddle [template][jsfiddle] to ease this. (We will keep this link up to date as new releases occur, so feel free to check back here)
|
||||||
|
|
||||||
In general, we are going to ask for an **example** of the problem failing, which can be as simple as a jsfiddle/jsbin/etc. We've put together a jsfiddle **[template][jsfiddle]** to ease this. (We will keep this link up to date as new releases occur, so feel free to check back here).
|
|
||||||
|
|
||||||
Pull requests containing only failing tests demonstrating the issue are welcomed and this also helps ensure that your issue won't regress in the future once it's fixed.
|
Pull requests containing only failing tests demonstrating the issue are welcomed and this also helps ensure that your issue won't regress in the future once it's fixed.
|
||||||
|
|
||||||
@@ -18,8 +12,9 @@ Documentation issues on the [handlebarsjs.com](https://handlebarsjs.com) site sh
|
|||||||
|
|
||||||
## Branches
|
## Branches
|
||||||
|
|
||||||
- The branch `master` contains the current development version (v5).
|
- The branch `4.x` contains the currently released version. Bugfixes should be made in this branch.
|
||||||
- The branch `4.x` contains the previous stable version. Only critical bugfixes are backported there.
|
- The branch `master` contains the next version. A release date is not yet specified. Maintainers
|
||||||
|
should merge the branch `4.x` into the master branch regularly.
|
||||||
|
|
||||||
## Pull Requests
|
## Pull Requests
|
||||||
|
|
||||||
@@ -35,13 +30,21 @@ Generally we like to see pull requests that
|
|||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
To build Handlebars.js you'll need Node.js installed.
|
To build Handlebars.js you'll need a few things installed.
|
||||||
|
|
||||||
|
- Node.js
|
||||||
|
- [Grunt](http://gruntjs.com/getting-started)
|
||||||
|
|
||||||
Before building, you need to make sure that the Git submodule `spec/mustache` is included (i.e. the directory `spec/mustache` should not be empty). To include it, if using Git version 1.6.5 or newer, use `git clone --recursive` rather than `git clone`. Or, if you already cloned without `--recursive`, use `git submodule update --init`.
|
Before building, you need to make sure that the Git submodule `spec/mustache` is included (i.e. the directory `spec/mustache` should not be empty). To include it, if using Git version 1.6.5 or newer, use `git clone --recursive` rather than `git clone`. Or, if you already cloned without `--recursive`, use `git submodule update --init`.
|
||||||
|
|
||||||
Project dependencies may be installed via `npm install`.
|
Project dependencies may be installed via `npm install`.
|
||||||
|
|
||||||
To build Handlebars.js from scratch, run `npm run build` in the root of the project. That will compile CJS modules via SWC and bundle UMD distributions via rspack, outputting results to the dist/ folder. To run tests, use `npm test`.
|
To build Handlebars.js from scratch, you'll want to run `grunt`
|
||||||
|
in the root of the project. That will build Handlebars and output the
|
||||||
|
results to the dist/ folder. To re-run tests, run `grunt test` or `npm test`.
|
||||||
|
You can also run our set of benchmarks with `grunt bench`.
|
||||||
|
|
||||||
|
The `grunt dev` implements watching for tests and allows for in browser testing at `http://localhost:9999/spec/`.
|
||||||
|
|
||||||
If you notice any problems, please report them to the GitHub issue tracker at
|
If you notice any problems, please report them to the GitHub issue tracker at
|
||||||
[http://github.com/handlebars-lang/handlebars.js/issues](http://github.com/handlebars-lang/handlebars.js/issues).
|
[http://github.com/handlebars-lang/handlebars.js/issues](http://github.com/handlebars-lang/handlebars.js/issues).
|
||||||
@@ -70,36 +73,116 @@ npm test
|
|||||||
|
|
||||||
## Linting and Formatting
|
## Linting and Formatting
|
||||||
|
|
||||||
Handlebars uses `oxlint` for linting, `oxfmt` for formatting, and `eslint` (with `eslint-plugin-compat`) for browser API compatibility checks.
|
Handlebars uses `eslint` to enforce best-practices and `prettier` to auto-format files.
|
||||||
Committed files are linted and formatted in a pre-commit hook.
|
We do linting and formatting in two phases:
|
||||||
|
|
||||||
|
- Committed files are linted and formatted in a pre-commit hook. In this stage eslint-errors are forbidden,
|
||||||
|
while warnings are allowed.
|
||||||
|
- The GitHub CI job also lints all files and checks if they are formatted correctly. In this stage, warnings
|
||||||
|
are forbidden.
|
||||||
|
|
||||||
You can use the following scripts to make sure that the CI job does not fail:
|
You can use the following scripts to make sure that the CI job does not fail:
|
||||||
|
|
||||||
- **npm run lint** will run all linters and fail on warnings
|
- **npm run lint** will run `eslint` and fail on warnings
|
||||||
- **npm run format** will format all files
|
- **npm run format** will run `prettier` on all files
|
||||||
- **npm run check-before-pull-request** will perform all checks that our CI job does, excluding integration tests.
|
- **npm run check-before-pull-request** will perform all most checks that our CI job does in its build-job, excluding the "integration-test".
|
||||||
- **npm run test:integration** will run integration tests (bundler compatibility with webpack, rollup, etc.)
|
- **npm run test:integration** will run integration tests (using old NodeJS versions and integrations with webpack, babel and so on)
|
||||||
These tests only work on Linux.
|
These tests only work on a Linux-machine with `nvm` installed (for running tests in multiple versions of NodeJS).
|
||||||
|
|
||||||
## Releasing the latest version
|
## Releasing the latest version
|
||||||
|
|
||||||
Before attempting the release Handlebars, please make sure that you have the following authorizations:
|
Before attempting the release Handlebars, please make sure that you have the following authorizations:
|
||||||
|
|
||||||
- Push-access to `handlebars-lang/handlebars.js`
|
- Push-access to [handlebars-lang/handlebars.js](https://github.com/handlebars-lang/handlebars.js/)
|
||||||
- Publishing rights on npmjs.com for the `handlebars` package
|
- Publishing rights on npmjs.com for the [handlebars](https://www.npmjs.com/package/handlebars) package
|
||||||
- Publishing rights on gemfury for the `handlebars-source` package
|
- Publishing rights on rubygems for the [handlebars-source](https://rubygems.org/gems/handlebars-source) package
|
||||||
- Push-access to the repo for legacy package managers: `components/handlebars`
|
- Push-access to the repo for legacy package managers: [components/handlebars.js](https://github.com/components/handlebars.js)
|
||||||
- Push-access to the production-repo of the handlebars site: `handlebars-lang/handlebarsjs.com-github-pages`
|
- Push-access to the production-repo of the handlebars site: [handlebars-lang/docs](https://github.com/handlebars-lang/docs)
|
||||||
|
|
||||||
_When releasing a previous version of Handlebars, please look into the CONTRIBUNG.md in the corresponding branch._
|
_When releasing a previous version of Handlebars, please look into the CONTRIBUNG.md in the corresponding branch._
|
||||||
|
|
||||||
A full release may be completed with the following:
|
A full release via Docker may be completed with the following:
|
||||||
|
|
||||||
|
1. Create a `Dockerfile` in this folder for releasing
|
||||||
|
```Dockerfile
|
||||||
|
FROM node:10-slim
|
||||||
|
|
||||||
|
ENV EDITOR=vim
|
||||||
|
|
||||||
|
# Update stretch repositories
|
||||||
|
RUN sed -i -e 's/deb.debian.org/archive.debian.org/g' \
|
||||||
|
-e 's|security.debian.org|archive.debian.org/|g' \
|
||||||
|
-e '/stretch-updates/d' /etc/apt/sources.list
|
||||||
|
|
||||||
|
# Install release dependencies
|
||||||
|
RUN apt-get update
|
||||||
|
RUN apt-get install -y git vim
|
||||||
|
|
||||||
|
# Work around deprecated npm dependency install via unauthenticated git-protocol:
|
||||||
|
# https://github.com/kpdecker/generator-release/blob/87aab9b84c9f083635c3fcc822f18acce1f48736/package.json#L31
|
||||||
|
RUN git config --system url."https://github.com/".insteadOf git://github.com/
|
||||||
|
|
||||||
|
# Configure git
|
||||||
|
RUN git config --system user.email "release@handlebarsjs.com"
|
||||||
|
RUN git config --system user.name "handlebars-lang"
|
||||||
|
|
||||||
|
RUN mkdir /home/node/.config
|
||||||
|
RUN mkdir /home/node/.ssh
|
||||||
|
RUN mkdir /home/node/tmp
|
||||||
|
|
||||||
|
# Generate config for yo generator-release:
|
||||||
|
# https://github.com/kpdecker/generator-release#example
|
||||||
|
# You have to add a valid GitHub access token! (Used for reading issues and pull requests.)
|
||||||
|
RUN echo "module.exports = {\n auth: 'oauth',\n token: 'GitHub personal access token'\n};" > /home/node/.config/generator-release
|
||||||
|
RUN chown -R node:node /home/node/.config
|
||||||
|
RUN chown -R node:node /home/node/.ssh
|
||||||
|
RUN chown -R node:node /home/node/tmp
|
||||||
|
|
||||||
|
# Add the generated key to GitHub: https://github.com/settings/keys
|
||||||
|
RUN ssh-keygen -q -t ed25519 -N '' -f /home/node/.ssh/id_ed25519 -C "release@handlebarsjs.com"
|
||||||
|
RUN chmod 0600 /home/node/.ssh/id_ed25519*
|
||||||
|
RUN chown node:node /home/node/.ssh/id_ed25519*
|
||||||
```
|
```
|
||||||
npm ci
|
2. Build and run the Docker image
|
||||||
npm run build
|
```bash
|
||||||
|
docker build --tag handlebars:release .
|
||||||
|
docker run --rm --interactive --tty \
|
||||||
|
--volume $PWD:/app \
|
||||||
|
--workdir /app \
|
||||||
|
--user $(id -u):$(id -g) \
|
||||||
|
--env NPM_CONFIG_PREFIX=/home/node/.npm-global \
|
||||||
|
handlebars:release bash -c 'export PATH=$PATH:/home/node/.npm-global/bin; bash'
|
||||||
|
```
|
||||||
|
* Add SSH key to GitHub: `cat /home/node/.ssh/id_ed25519.pub` (https://github.com/settings/keys)
|
||||||
|
* Add GitHub API token: `vi /home/node/.config/generator-release`
|
||||||
|
* Execute the following steps:
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
npm install -g yo@1 grunt@1 generator-release
|
||||||
|
npm run release
|
||||||
|
# Warning! This step will collect data from GitHub, bump the version,
|
||||||
|
# create a new commit, create a new tag and push it to GitHub.
|
||||||
|
# https://github.com/kpdecker/generator-release?tab=readme-ov-file#usage
|
||||||
|
yo release
|
||||||
|
npm login
|
||||||
npm publish
|
npm publish
|
||||||
|
yo release:publish components handlebars.js dist/components/
|
||||||
```
|
```
|
||||||
|
6. Publish Ruby `handlebars-source` gem:
|
||||||
|
```bash
|
||||||
|
docker run --rm --interactive --tty \
|
||||||
|
--volume $PWD:/app \
|
||||||
|
--workdir /app \
|
||||||
|
ruby:3.2-slim bash
|
||||||
|
```
|
||||||
|
* Execute the following steps:
|
||||||
|
```bash
|
||||||
|
cd dist/components/
|
||||||
|
gem build handlebars-source.gemspec
|
||||||
|
gem push handlebars-source-*.gem
|
||||||
|
```
|
||||||
|
|
||||||
|
### After the release
|
||||||
|
|
||||||
After the release, you should check that all places have really been updated. Especially verify that the `latest`-tags
|
After the release, you should check that all places have really been updated. Especially verify that the `latest`-tags
|
||||||
in those places still point to the latest version
|
in those places still point to the latest version
|
||||||
@@ -120,4 +203,4 @@ in the `handlebars-lang/docs`-repo).
|
|||||||
[generator-release]: https://github.com/walmartlabs/generator-release
|
[generator-release]: https://github.com/walmartlabs/generator-release
|
||||||
[pull-request]: https://github.com/handlebars-lang/handlebars.js/pull/new/master
|
[pull-request]: https://github.com/handlebars-lang/handlebars.js/pull/new/master
|
||||||
[issue]: https://github.com/handlebars-lang/handlebars.js/issues/new
|
[issue]: https://github.com/handlebars-lang/handlebars.js/issues/new
|
||||||
[jsfiddle]: https://jsfiddle.net/4nbwjaqz/4/
|
[jsfiddle]: https://jsfiddle.net/9D88g/180/
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
# Frequently Asked Questions
|
# Frequently Asked Questions
|
||||||
|
|
||||||
## How can I file a bug report:
|
1. How can I file a bug report:
|
||||||
|
|
||||||
See our guidelines on [reporting issues](https://github.com/handlebars-lang/handlebars.js/blob/master/CONTRIBUTING.md#reporting-issues).
|
See our guidelines on [reporting issues](https://github.com/handlebars-lang/handlebars.js/blob/master/CONTRIBUTING.md#reporting-issues).
|
||||||
|
|
||||||
## Why isn't my Mustache template working?
|
1. Why isn't my Mustache template working?
|
||||||
|
|
||||||
Handlebars deviates from Mustache slightly on a few behaviors. These variations are documented in our [readme](https://github.com/handlebars-lang/handlebars.js#differences-between-handlebarsjs-and-mustache).
|
Handlebars deviates from Mustache slightly on a few behaviors. These variations are documented in our [readme](https://github.com/handlebars-lang/handlebars.js#differences-between-handlebarsjs-and-mustache).
|
||||||
|
|
||||||
## Why is it slower when compiling?
|
1. Why is it slower when compiling?
|
||||||
|
|
||||||
The Handlebars compiler must parse the template and construct a JavaScript program which can then be run. Under some environments such as older mobile devices this can have a performance impact which can be avoided by precompiling. Generally it's recommended that precompilation and the runtime library be used on all clients.
|
The Handlebars compiler must parse the template and construct a JavaScript program which can then be run. Under some environments such as older mobile devices this can have a performance impact which can be avoided by precompiling. Generally it's recommended that precompilation and the runtime library be used on all clients.
|
||||||
|
|
||||||
## Why doesn't this work with Content Security Policy restrictions?
|
1. Why doesn't this work with Content Security Policy restrictions?
|
||||||
|
|
||||||
When not using the precompiler, Handlebars generates a dynamic function for each template which can cause issues with pages that have enabled Content Policy. It's recommended that templates are precompiled or the `unsafe-eval` policy is enabled for sites that must generate dynamic templates at runtime.
|
When not using the precompiler, Handlebars generates a dynamic function for each template which can cause issues with pages that have enabled Content Policy. It's recommended that templates are precompiled or the `unsafe-eval` policy is enabled for sites that must generate dynamic templates at runtime.
|
||||||
|
|
||||||
## How can I include script tags in my template?
|
1. How can I include script tags in my template?
|
||||||
|
|
||||||
If loading the template via an inlined `<script type="text/x-handlebars">` tag then you may need to break up the script tag with an empty comment to avoid browser parser errors:
|
If loading the template via an inlined `<script type="text/x-handlebars">` tag then you may need to break up the script tag with an empty comment to avoid browser parser errors:
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ If loading the template via an inlined `<script type="text/x-handlebars">` tag t
|
|||||||
|
|
||||||
It's generally recommended that templates are served through external, precompiled, files, which do not suffer from this issue.
|
It's generally recommended that templates are served through external, precompiled, files, which do not suffer from this issue.
|
||||||
|
|
||||||
## Why are my precompiled scripts throwing exceptions?
|
1. Why are my precompiled scripts throwing exceptions?
|
||||||
|
|
||||||
When using the precompiler, it's important that a supporting version of the Handlebars runtime be loaded on the target page. In version 1.x there were rudimentary checks to compare the version but these did not always work. This is fixed under 2.x but the version checking does not work between these two versions. If you see unexpected errors such as `undefined is not a function` or similar, please verify that the same version is being used for both the precompiler and the client. This can be checked via:
|
When using the precompiler, it's important that a supporting version of the Handlebars runtime be loaded on the target page. In version 1.x there were rudimentary checks to compare the version but these did not always work. This is fixed under 2.x but the version checking does not work between these two versions. If you see unexpected errors such as `undefined is not a function` or similar, please verify that the same version is being used for both the precompiler and the client. This can be checked via:
|
||||||
|
|
||||||
@@ -49,6 +49,14 @@ We include the built client libraries in the npm package for those who want to b
|
|||||||
|
|
||||||
Should these match, please file an issue with us, per our [issue filing guidelines](https://github.com/handlebars-lang/handlebars.js/blob/master/CONTRIBUTING.md#reporting-issues).
|
Should these match, please file an issue with us, per our [issue filing guidelines](https://github.com/handlebars-lang/handlebars.js/blob/master/CONTRIBUTING.md#reporting-issues).
|
||||||
|
|
||||||
## How do I load the runtime library when using AMD?
|
1. Why doesn't IE like the `default` name in the AMD module?
|
||||||
|
|
||||||
The `handlebars.runtime.js` file includes a UMD build, which exposes the library as both the module root and the `default` field for compatibility.
|
Some browsers such as particular versions of IE treat `default` as a reserved word in JavaScript source files. To safely use this you need to reference this via the `Handlebars['default']` lookup method. This is an unfortunate side effect of the shims necessary to backport the Handlebars ES6 code to all current browsers.
|
||||||
|
|
||||||
|
1. How do I load the runtime library when using AMD?
|
||||||
|
|
||||||
|
There are two options for loading under AMD environments. The first is to use the `handlebars.runtime.amd.js` file. This may require a [path mapping](https://github.com/handlebars-lang/handlebars.js/blob/master/spec/amd-runtime.html#L31) as well as access via the `default` field.
|
||||||
|
|
||||||
|
The other option is to load the `handlebars.runtime.js` UMD build, which might not require path configuration and exposes the library as both the module root and the `default` field for compatibility.
|
||||||
|
|
||||||
|
If not using ES6 transpilers or accessing submodules in the build the former option should be sufficient for most use cases.
|
||||||
|
|||||||
+237
@@ -0,0 +1,237 @@
|
|||||||
|
/* eslint-disable no-process-env */
|
||||||
|
module.exports = function(grunt) {
|
||||||
|
grunt.initConfig({
|
||||||
|
pkg: grunt.file.readJSON('package.json'),
|
||||||
|
|
||||||
|
clean: [
|
||||||
|
'tmp',
|
||||||
|
'dist',
|
||||||
|
'lib/handlebars/compiler/parser.js',
|
||||||
|
'/tests/integration/**/node_modules'
|
||||||
|
],
|
||||||
|
|
||||||
|
copy: {
|
||||||
|
dist: {
|
||||||
|
options: {
|
||||||
|
processContent: function(content) {
|
||||||
|
return (
|
||||||
|
grunt.template.process(
|
||||||
|
'/**!\n\n @license\n <%= pkg.name %> v<%= pkg.version %>\n\n<%= grunt.file.read("LICENSE") %>\n*/\n'
|
||||||
|
) + content
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
files: [{ expand: true, cwd: 'dist/', src: ['*.js'], dest: 'dist/' }]
|
||||||
|
},
|
||||||
|
cdnjs: {
|
||||||
|
files: [
|
||||||
|
{ expand: true, cwd: 'dist/', src: ['*.js'], dest: 'dist/cdnjs' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
components: {
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
expand: true,
|
||||||
|
cwd: 'components/',
|
||||||
|
src: ['**'],
|
||||||
|
dest: 'dist/components'
|
||||||
|
},
|
||||||
|
{ expand: true, cwd: 'dist/', src: ['*.js'], dest: 'dist/components' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
babel: {
|
||||||
|
options: {
|
||||||
|
sourceMaps: 'inline',
|
||||||
|
loose: ['es6.modules'],
|
||||||
|
auxiliaryCommentBefore: 'istanbul ignore next'
|
||||||
|
},
|
||||||
|
amd: {
|
||||||
|
options: {
|
||||||
|
modules: 'amd'
|
||||||
|
},
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
expand: true,
|
||||||
|
cwd: 'lib/',
|
||||||
|
src: '**/!(index).js',
|
||||||
|
dest: 'dist/amd/'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
cjs: {
|
||||||
|
options: {
|
||||||
|
modules: 'common'
|
||||||
|
},
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
cwd: 'lib/',
|
||||||
|
expand: true,
|
||||||
|
src: '**/!(index).js',
|
||||||
|
dest: 'dist/cjs/'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
webpack: {
|
||||||
|
options: {
|
||||||
|
context: __dirname,
|
||||||
|
module: {
|
||||||
|
loaders: [
|
||||||
|
// the optional 'runtime' transformer tells babel to require the runtime instead of inlining it.
|
||||||
|
{
|
||||||
|
test: /\.jsx?$/,
|
||||||
|
exclude: /node_modules/,
|
||||||
|
loader:
|
||||||
|
'babel-loader?optional=runtime&loose=es6.modules&auxiliaryCommentBefore=istanbul%20ignore%20next'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
path: 'dist/',
|
||||||
|
library: 'Handlebars',
|
||||||
|
libraryTarget: 'umd'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
handlebars: {
|
||||||
|
entry: './lib/handlebars.js',
|
||||||
|
output: {
|
||||||
|
filename: 'handlebars.js'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
runtime: {
|
||||||
|
entry: './lib/handlebars.runtime.js',
|
||||||
|
output: {
|
||||||
|
filename: 'handlebars.runtime.js'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
requirejs: {
|
||||||
|
options: {
|
||||||
|
optimize: 'none',
|
||||||
|
baseUrl: 'dist/amd/'
|
||||||
|
},
|
||||||
|
dist: {
|
||||||
|
options: {
|
||||||
|
name: 'handlebars',
|
||||||
|
out: 'dist/handlebars.amd.js'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
runtime: {
|
||||||
|
options: {
|
||||||
|
name: 'handlebars.runtime',
|
||||||
|
out: 'dist/handlebars.runtime.amd.js'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
uglify: {
|
||||||
|
options: {
|
||||||
|
mangle: true,
|
||||||
|
compress: true,
|
||||||
|
preserveComments: /(?:^!|@(?:license|preserve|cc_on))/
|
||||||
|
},
|
||||||
|
dist: {
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
cwd: 'dist/',
|
||||||
|
expand: true,
|
||||||
|
src: ['handlebars*.js', '!*.min.js'],
|
||||||
|
dest: 'dist/',
|
||||||
|
rename: function(dest, src) {
|
||||||
|
return dest + src.replace(/\.js$/, '.min.js');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
concat: {
|
||||||
|
tests: {
|
||||||
|
src: ['spec/!(require).js'],
|
||||||
|
dest: 'tmp/tests.js'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
connect: {
|
||||||
|
server: {
|
||||||
|
options: {
|
||||||
|
base: '.',
|
||||||
|
hostname: '*',
|
||||||
|
port: 9999
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
shell: {
|
||||||
|
integrationTests: {
|
||||||
|
command: './tests/integration/run-integration-tests.sh'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
scripts: {
|
||||||
|
options: {
|
||||||
|
atBegin: true
|
||||||
|
},
|
||||||
|
|
||||||
|
files: ['src/*', 'lib/**/*.js', 'spec/**/*.js'],
|
||||||
|
tasks: ['on-file-change']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Load tasks from npm
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-clean');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-concat');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-connect');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-copy');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-requirejs');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-uglify');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-watch');
|
||||||
|
grunt.loadNpmTasks('grunt-babel');
|
||||||
|
grunt.loadNpmTasks('grunt-shell');
|
||||||
|
grunt.loadNpmTasks('grunt-webpack');
|
||||||
|
|
||||||
|
grunt.task.loadTasks('tasks');
|
||||||
|
|
||||||
|
grunt.registerTask('node', ['babel:cjs']);
|
||||||
|
grunt.registerTask('amd', ['babel:amd', 'requirejs']);
|
||||||
|
grunt.registerTask('globals', ['webpack']);
|
||||||
|
grunt.registerTask('release', 'Build final packages', [
|
||||||
|
'uglify',
|
||||||
|
'test:min',
|
||||||
|
'copy:dist',
|
||||||
|
'copy:components',
|
||||||
|
'copy:cdnjs'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Requires secret properties from .travis.yaml
|
||||||
|
grunt.registerTask('extensive-tests-and-publish-to-aws', [
|
||||||
|
'default',
|
||||||
|
'shell:integrationTests',
|
||||||
|
'metrics',
|
||||||
|
'publish-to-aws'
|
||||||
|
]);
|
||||||
|
|
||||||
|
grunt.registerTask('on-file-change', ['build', 'concat:tests', 'test']);
|
||||||
|
|
||||||
|
// === Primary tasks ===
|
||||||
|
grunt.registerTask('dev', ['clean', 'connect', 'watch']);
|
||||||
|
grunt.registerTask('default', ['clean', 'build', 'test', 'release']);
|
||||||
|
grunt.registerTask('test', ['test:bin', 'test:cov']);
|
||||||
|
grunt.registerTask('bench', ['metrics']);
|
||||||
|
grunt.registerTask('prepare', ['build', 'concat:tests']);
|
||||||
|
grunt.registerTask(
|
||||||
|
'build',
|
||||||
|
'Builds a distributable version of the current project',
|
||||||
|
['parser', 'node', 'amd', 'globals']
|
||||||
|
);
|
||||||
|
grunt.registerTask('integration-tests', [
|
||||||
|
'default',
|
||||||
|
'shell:integrationTests'
|
||||||
|
]);
|
||||||
|
};
|
||||||
+167
@@ -0,0 +1,167 @@
|
|||||||
|
[](https://github.com/handlebars-lang/handlebars.js/actions/workflows/ci.yml)
|
||||||
|
[](https://www.jsdelivr.com/package/npm/handlebars)
|
||||||
|
[](https://www.npmjs.com/package/handlebars)
|
||||||
|
[](https://www.npmjs.com/package/handlebars)
|
||||||
|
[](https://bundlephobia.com/package/handlebars)
|
||||||
|
[](https://packagephobia.com/result?p=handlebars)
|
||||||
|
|
||||||
|
Handlebars.js
|
||||||
|
=============
|
||||||
|
|
||||||
|
Handlebars provides the power necessary to let you build **semantic templates** effectively with no frustration.
|
||||||
|
Handlebars is largely compatible with Mustache templates. In most cases it is possible to swap out Mustache with Handlebars and continue using your current templates.
|
||||||
|
|
||||||
|
Checkout the official Handlebars docs site at
|
||||||
|
[handlebarsjs.com](https://handlebarsjs.com) and try our [live demo](https://handlebarsjs.com/playground.html).
|
||||||
|
|
||||||
|
Installing
|
||||||
|
----------
|
||||||
|
|
||||||
|
See our [installation documentation](https://handlebarsjs.com/installation/).
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
In general, the syntax of Handlebars.js templates is a superset
|
||||||
|
of Mustache templates. For basic syntax, check out the [Mustache
|
||||||
|
manpage](https://mustache.github.io/mustache.5.html).
|
||||||
|
|
||||||
|
Once you have a template, use the `Handlebars.compile` method to compile
|
||||||
|
the template into a function. The generated function takes a context
|
||||||
|
argument, which will be used to render the template.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
|
||||||
|
"{{kids.length}} kids:</p>" +
|
||||||
|
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
|
||||||
|
var template = Handlebars.compile(source);
|
||||||
|
|
||||||
|
var data = { "name": "Alan", "hometown": "Somewhere, TX",
|
||||||
|
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
|
||||||
|
var result = template(data);
|
||||||
|
|
||||||
|
// Would render:
|
||||||
|
// <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>
|
||||||
|
// <ul>
|
||||||
|
// <li>Jimmy is 12</li>
|
||||||
|
// <li>Sally is 4</li>
|
||||||
|
// </ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
Full documentation and more examples are at [handlebarsjs.com](https://handlebarsjs.com/).
|
||||||
|
|
||||||
|
Precompiling Templates
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
Handlebars allows templates to be precompiled and included as javascript code rather than the handlebars template allowing for faster startup time. Full details are located [here](https://handlebarsjs.com/installation/precompilation.html).
|
||||||
|
|
||||||
|
Differences Between Handlebars.js and Mustache
|
||||||
|
----------------------------------------------
|
||||||
|
Handlebars.js adds a couple of additional features to make writing
|
||||||
|
templates easier and also changes a tiny detail of how partials work.
|
||||||
|
|
||||||
|
- [Nested Paths](https://handlebarsjs.com/guide/expressions.html#path-expressions)
|
||||||
|
- [Helpers](https://handlebarsjs.com/guide/expressions.html#helpers)
|
||||||
|
- [Block Expressions](https://handlebarsjs.com/guide/block-helpers.html#basic-blocks)
|
||||||
|
- [Literal Values](https://handlebarsjs.com/guide/expressions.html#literal-segments)
|
||||||
|
- [Delimited Comments](https://handlebarsjs.com/guide/#template-comments)
|
||||||
|
|
||||||
|
Block expressions have the same syntax as mustache sections but should not be confused with one another. Sections are akin to an implicit `each` or `with` statement depending on the input data and helpers are explicit pieces of code that are free to implement whatever behavior they like. The [mustache spec](https://mustache.github.io/mustache.5.html) defines the exact behavior of sections. In the case of name conflicts, helpers are given priority.
|
||||||
|
|
||||||
|
### Compatibility
|
||||||
|
|
||||||
|
There are a few Mustache behaviors that Handlebars does not implement.
|
||||||
|
- Handlebars deviates from Mustache slightly in that it does not perform recursive lookup by default. The compile time `compat` flag must be set to enable this functionality. Users should note that there is a performance cost for enabling this flag. The exact cost varies by template, but it's recommended that performance sensitive operations should avoid this mode and instead opt for explicit path references.
|
||||||
|
- The optional Mustache-style lambdas are not supported. Instead Handlebars provides its own lambda resolution that follows the behaviors of helpers.
|
||||||
|
- Alternative delimiters are not supported.
|
||||||
|
|
||||||
|
|
||||||
|
Supported Environments
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
Handlebars has been designed to work in any ECMAScript 3 environment. This includes
|
||||||
|
|
||||||
|
- Node.js
|
||||||
|
- Chrome
|
||||||
|
- Firefox
|
||||||
|
- Safari 5+
|
||||||
|
- Opera 11+
|
||||||
|
- IE 6+
|
||||||
|
|
||||||
|
Older versions and other runtimes are likely to work but have not been formally
|
||||||
|
tested. The compiler requires `JSON.stringify` to be implemented natively or via a polyfill. If using the precompiler this is not necessary.
|
||||||
|
|
||||||
|
Performance
|
||||||
|
-----------
|
||||||
|
|
||||||
|
In a rough performance test, precompiled Handlebars.js templates (in
|
||||||
|
the original version of Handlebars.js) rendered in about half the
|
||||||
|
time of Mustache templates. It would be a shame if it were any other
|
||||||
|
way, since they were precompiled, but the difference in architecture
|
||||||
|
does have some big performance advantages. Justin Marney, a.k.a.
|
||||||
|
[gotascii](http://github.com/gotascii), confirmed that with an
|
||||||
|
[independent test](http://sorescode.com/2010/09/12/benchmarks.html). The
|
||||||
|
rewritten Handlebars (current version) is faster than the old version,
|
||||||
|
with many performance tests being 5 to 7 times faster than the Mustache equivalent.
|
||||||
|
|
||||||
|
|
||||||
|
Upgrading
|
||||||
|
---------
|
||||||
|
|
||||||
|
See [release-notes.md](https://github.com/handlebars-lang/handlebars.js/blob/master/release-notes.md) for upgrade notes.
|
||||||
|
|
||||||
|
Known Issues
|
||||||
|
------------
|
||||||
|
|
||||||
|
See [FAQ.md](https://github.com/handlebars-lang/handlebars.js/blob/master/FAQ.md) for known issues and common pitfalls.
|
||||||
|
|
||||||
|
|
||||||
|
Handlebars in the Wild
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
* [Assemble](http://assemble.io), by [@jonschlinkert](https://github.com/jonschlinkert)
|
||||||
|
and [@doowb](https://github.com/doowb), is a static site generator that uses Handlebars.js
|
||||||
|
as its template engine.
|
||||||
|
* [Cory](https://github.com/leo/cory), by [@leo](https://github.com/leo), is another tiny static site generator
|
||||||
|
* [CoSchedule](http://coschedule.com) An editorial calendar for WordPress that uses Handlebars.js
|
||||||
|
* [dashbars](https://github.com/pismute/dashbars) A modern helper library for Handlebars.js.
|
||||||
|
* [Ember.js](http://www.emberjs.com) makes Handlebars.js the primary way to
|
||||||
|
structure your views, also with automatic data binding support.
|
||||||
|
* [Ghost](https://ghost.org/) Just a blogging platform.
|
||||||
|
* [handlebars_assets](http://github.com/leshill/handlebars_assets): A Rails Asset Pipeline gem
|
||||||
|
from Les Hill (@leshill).
|
||||||
|
* [handlebars-helpers](https://github.com/assemble/handlebars-helpers) is an extensive library
|
||||||
|
with 100+ handlebars helpers.
|
||||||
|
* [handlebars-layouts](https://github.com/shannonmoeller/handlebars-layouts) is a set of helpers which implement extendible and embeddable layout blocks as seen in other popular templating languages.
|
||||||
|
* [hbs](http://github.com/donpark/hbs): An Express.js view engine adapter for Handlebars.js,
|
||||||
|
from Don Park.
|
||||||
|
* [koa-hbs](https://github.com/jwilm/koa-hbs): [koa](https://github.com/koajs/koa) generator based
|
||||||
|
renderer for Handlebars.js.
|
||||||
|
* [jblotus](http://github.com/jblotus) created [http://tryhandlebarsjs.com](http://tryhandlebarsjs.com)
|
||||||
|
for anyone who would like to try out Handlebars.js in their browser.
|
||||||
|
* [jQuery plugin](http://71104.github.io/jquery-handlebars/): allows you to use
|
||||||
|
Handlebars.js with [jQuery](http://jquery.com/).
|
||||||
|
* [Lumbar](http://walmartlabs.github.io/lumbar) provides easy module-based template management for
|
||||||
|
handlebars projects.
|
||||||
|
* [Marionette.Handlebars](https://github.com/hashchange/marionette.handlebars) adds support for Handlebars and Mustache templates to Marionette.
|
||||||
|
* [sammy.js](http://github.com/quirkey/sammy) by Aaron Quint, a.k.a. quirkey,
|
||||||
|
supports Handlebars.js as one of its template plugins.
|
||||||
|
* [SproutCore](http://www.sproutcore.com) uses Handlebars.js as its main
|
||||||
|
templating engine, extending it with automatic data binding support.
|
||||||
|
* [YUI](http://yuilibrary.com/yui/docs/handlebars/) implements a port of handlebars
|
||||||
|
* [Swag](https://github.com/elving/swag) by [@elving](https://github.com/elving) is a growing collection of helpers for handlebars.js. Give your handlebars.js templates some swag son!
|
||||||
|
* [DOMBars](https://github.com/blakeembrey/dombars) is a DOM-based templating engine built on the Handlebars parser and runtime **DEPRECATED**
|
||||||
|
* [promised-handlebars](https://github.com/nknapp/promised-handlebars) is a wrapper for Handlebars that allows helpers to return Promises.
|
||||||
|
* [just-handlebars-helpers](https://github.com/leapfrogtechnology/just-handlebars-helpers) A fully tested lightweight package with common Handlebars helpers.
|
||||||
|
|
||||||
|
External Resources
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* [Gist about Synchronous and asynchronous loading of external handlebars templates](https://gist.github.com/2287070)
|
||||||
|
|
||||||
|
Have a project using Handlebars? Send us a [pull request][pull-request]!
|
||||||
|
|
||||||
|
License
|
||||||
|
-------
|
||||||
|
Handlebars.js is released under the MIT license.
|
||||||
|
|
||||||
|
[pull-request]: https://github.com/handlebars-lang/handlebars.js/pull/new/master
|
||||||
@@ -1,198 +0,0 @@
|
|||||||
[](https://github.com/handlebars-lang/handlebars.js/actions/workflows/ci.yml)
|
|
||||||
[](https://www.jsdelivr.com/package/npm/handlebars)
|
|
||||||
[](https://www.npmjs.com/package/handlebars)
|
|
||||||
[](https://www.npmjs.com/package/handlebars)
|
|
||||||
[](https://bundlephobia.com/package/handlebars)
|
|
||||||
[](https://packagephobia.com/result?p=handlebars)
|
|
||||||
|
|
||||||
# Handlebars.js
|
|
||||||
|
|
||||||
Handlebars provides the power necessary to let you build **semantic templates** effectively with no frustration.
|
|
||||||
Handlebars is largely compatible with Mustache templates. In most cases it is possible to swap out Mustache with Handlebars and continue using your current templates.
|
|
||||||
|
|
||||||
Checkout the official Handlebars docs site at
|
|
||||||
[handlebarsjs.com](https://handlebarsjs.com) and try our [live demo](https://handlebarsjs.com/playground.html).
|
|
||||||
|
|
||||||
## Installing
|
|
||||||
|
|
||||||
See our [installation documentation](https://handlebarsjs.com/guide/installation/).
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
In general, the syntax of Handlebars.js templates is a superset
|
|
||||||
of Mustache templates. For basic syntax, check out the [Mustache
|
|
||||||
manpage](https://mustache.github.io/mustache.5.html).
|
|
||||||
|
|
||||||
Once you have a template, use the `Handlebars.compile` method to compile
|
|
||||||
the template into a function. The generated function takes a context
|
|
||||||
argument, which will be used to render the template.
|
|
||||||
|
|
||||||
```js
|
|
||||||
var source =
|
|
||||||
'<p>Hello, my name is {{name}}. I am from {{hometown}}. I have ' +
|
|
||||||
'{{kids.length}} kids:</p>' +
|
|
||||||
'<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>';
|
|
||||||
var template = Handlebars.compile(source);
|
|
||||||
|
|
||||||
var data = {
|
|
||||||
name: 'Alan',
|
|
||||||
hometown: 'Somewhere, TX',
|
|
||||||
kids: [
|
|
||||||
{ name: 'Jimmy', age: '12' },
|
|
||||||
{ name: 'Sally', age: '4' },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
var result = template(data);
|
|
||||||
|
|
||||||
// Would render:
|
|
||||||
// <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>
|
|
||||||
// <ul>
|
|
||||||
// <li>Jimmy is 12</li>
|
|
||||||
// <li>Sally is 4</li>
|
|
||||||
// </ul>
|
|
||||||
```
|
|
||||||
|
|
||||||
Full documentation and more examples are at [handlebarsjs.com](https://handlebarsjs.com/).
|
|
||||||
|
|
||||||
## Precompiling Templates
|
|
||||||
|
|
||||||
Handlebars allows templates to be precompiled and included as javascript code rather than the handlebars template allowing for faster startup time. Full details are located [here](https://handlebarsjs.com/guide/installation/precompilation.html).
|
|
||||||
|
|
||||||
## Differences Between Handlebars.js and Mustache
|
|
||||||
|
|
||||||
Handlebars.js adds a couple of additional features to make writing
|
|
||||||
templates easier and also changes a tiny detail of how partials work.
|
|
||||||
|
|
||||||
- [Nested Paths](https://handlebarsjs.com/guide/expressions.html#path-expressions)
|
|
||||||
- [Helpers](https://handlebarsjs.com/guide/expressions.html#helpers)
|
|
||||||
- [Block Expressions](https://handlebarsjs.com/guide/block-helpers.html#basic-blocks)
|
|
||||||
- [Literal Values](https://handlebarsjs.com/guide/expressions.html#literal-segments)
|
|
||||||
- [Delimited Comments](https://handlebarsjs.com/guide/#template-comments)
|
|
||||||
|
|
||||||
Block expressions have the same syntax as mustache sections but should not be confused with one another. Sections are akin to an implicit `each` or `with` statement depending on the input data and helpers are explicit pieces of code that are free to implement whatever behavior they like. The [mustache spec](https://mustache.github.io/mustache.5.html) defines the exact behavior of sections. In the case of name conflicts, helpers are given priority.
|
|
||||||
|
|
||||||
### Compatibility
|
|
||||||
|
|
||||||
There are a few Mustache behaviors that Handlebars does not implement.
|
|
||||||
|
|
||||||
- Handlebars deviates from Mustache slightly in that it does not perform recursive lookup by default. The compile time `compat` flag must be set to enable this functionality. Users should note that there is a performance cost for enabling this flag. The exact cost varies by template, but it's recommended that performance sensitive operations should avoid this mode and instead opt for explicit path references.
|
|
||||||
- The optional Mustache-style lambdas are not supported. Instead Handlebars provides its own lambda resolution that follows the behaviors of helpers.
|
|
||||||
- Handlebars does not allow space between the opening `{{` and a command character such as `#`, `/` or `>`. The command character must immediately follow the braces, so for example `{{> partial }}` is allowed but `{{ > partial }}` is not.
|
|
||||||
- Alternative delimiters are not supported.
|
|
||||||
|
|
||||||
## Supported Environments
|
|
||||||
|
|
||||||
Handlebars has been designed to work in any ECMAScript 2020 environment. This includes
|
|
||||||
|
|
||||||
- Node.js
|
|
||||||
- Chrome
|
|
||||||
- Firefox
|
|
||||||
- Safari
|
|
||||||
- Edge
|
|
||||||
|
|
||||||
If you need to support older environments, use Handlebars version 4.
|
|
||||||
|
|
||||||
## Performance
|
|
||||||
|
|
||||||
In a rough performance test, precompiled Handlebars.js templates (in
|
|
||||||
the original version of Handlebars.js) rendered in about half the
|
|
||||||
time of Mustache templates. It would be a shame if it were any other
|
|
||||||
way, since they were precompiled, but the difference in architecture
|
|
||||||
does have some big performance advantages. Justin Marney, a.k.a.
|
|
||||||
[gotascii](http://github.com/gotascii), confirmed that with an
|
|
||||||
[independent test](http://sorescode.com/2010/09/12/benchmarks.html). The
|
|
||||||
rewritten Handlebars (current version) is faster than the old version,
|
|
||||||
with many performance tests being 5 to 7 times faster than the Mustache equivalent.
|
|
||||||
|
|
||||||
### Benchmarks
|
|
||||||
|
|
||||||
The project includes a comprehensive benchmark suite (powered by [tinybench](https://github.com/tinylibs/tinybench)) that measures compilation, execution, precompilation, and end-to-end performance across templates of varying size and complexity.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Run benchmarks (auto-labels with current git branch)
|
|
||||||
npm run bench
|
|
||||||
|
|
||||||
# Run with a custom label
|
|
||||||
npm run bench -- --label my-optimization
|
|
||||||
|
|
||||||
# Filter templates by name (regex, case-insensitive)
|
|
||||||
npm run bench -- --grep "complex|recursive"
|
|
||||||
|
|
||||||
# Run only specific sections (regex, case-insensitive)
|
|
||||||
npm run bench -- --section precompil
|
|
||||||
npm run bench -- --section "compilation|precompil"
|
|
||||||
|
|
||||||
# Compare results
|
|
||||||
npm run bench:compare
|
|
||||||
|
|
||||||
# Or specify files explicitly
|
|
||||||
npm run bench:compare -- bench/results/bench-*-main.md bench/results/bench-*-feat.md
|
|
||||||
```
|
|
||||||
|
|
||||||
Results are saved as timestamped Markdown files in `bench/results/`. Each report includes ops/sec, avg latency, p50/p75/p99 percentiles, and sample counts.
|
|
||||||
|
|
||||||
Typical workflow for comparing branches:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
git checkout main && npm run bench
|
|
||||||
git checkout my-feature && npm run bench
|
|
||||||
npm run bench:compare
|
|
||||||
```
|
|
||||||
|
|
||||||
When run without arguments, `bench:compare` auto-selects two result files: if a file labelled "main" exists it is always used as the baseline, otherwise the older file is the baseline. The comparison uses p75 latency for the diff to filter outliers, and marks changes with `!` (>2%) and `!!` (>5%).
|
|
||||||
|
|
||||||
## Upgrading
|
|
||||||
|
|
||||||
See [release-notes.md](https://github.com/handlebars-lang/handlebars.js/blob/master/release-notes.md) for upgrade notes.
|
|
||||||
|
|
||||||
If you are using Handlebars in production, please regularly look for issues labeled
|
|
||||||
[possibly breaking](https://github.com/handlebars-lang/handlebars.js/issues?q=is%3Aopen+is%3Aissue+label%3A%22possibly+breaking%22).
|
|
||||||
If this label is applied to an issue, it means that the requested change is probably not a breaking change,
|
|
||||||
but since Handlebars is widely in use by a lot of people, there's always a chance that it breaks somebody's build.
|
|
||||||
|
|
||||||
## Known Issues
|
|
||||||
|
|
||||||
See [FAQ.md](https://github.com/handlebars-lang/handlebars.js/blob/master/FAQ.md) for known issues and common pitfalls.
|
|
||||||
|
|
||||||
## Handlebars in the Wild
|
|
||||||
|
|
||||||
- [apiDoc](https://github.com/apidoc/apidoc) apiDoc uses handlebars as parsing engine for api documentation view generation.
|
|
||||||
- [Assemble](https://assemble.io), by [@jonschlinkert](https://github.com/jonschlinkert) and [@doowb](https://github.com/doowb), is a static site generator that uses Handlebars.js as its template engine.
|
|
||||||
- [CoSchedule](https://coschedule.com) An editorial calendar for WordPress that uses Handlebars.js.
|
|
||||||
- [Ember.js](https://www.emberjs.com) makes Handlebars.js the primary way to structure your views, also with automatic data binding support.
|
|
||||||
- [express-handlebars](https://github.com/express-handlebars/express-handlebars) A Handlebars view engine for Express which doesn't suck.
|
|
||||||
- [express-hbs](https://github.com/TryGhost/express-hbs) Express Handlebars template engine with inheritance, partials, i18n and async helpers.
|
|
||||||
- [Ghost](https://ghost.org/) Just a blogging platform.
|
|
||||||
- [handlebars-action](https://github.com/marketplace/actions/handlebars-action) A GitHub action to transform files in your repository with Handlebars templating.
|
|
||||||
- [handlebars_assets](https://github.com/leshill/handlebars_assets) A Rails Asset Pipeline gem from Les Hill (@leshill).
|
|
||||||
- [handlebars-helpers](https://github.com/assemble/handlebars-helpers) is an extensive library with 100+ handlebars helpers.
|
|
||||||
- [handlebars-layouts](https://github.com/shannonmoeller/handlebars-layouts) is a set of helpers which implement extensible and embeddable layout blocks as seen in other popular templating languages.
|
|
||||||
- [handlebars-loader](https://github.com/pcardune/handlebars-loader) A handlebars template loader for webpack.
|
|
||||||
- [handlebars-wax](https://github.com/shannonmoeller/handlebars-wax) The missing Handlebars API. Effortless registration of data, partials, helpers, and decorators using file-system globs, modules, and plain-old JavaScript objects.
|
|
||||||
- [hbs](https://github.com/pillarjs/hbs) An Express.js view engine adapter for Handlebars.js, from Don Park.
|
|
||||||
- [html-bundler-webpack-plugin](https://github.com/webdiscus/html-bundler-webpack-plugin) The webpack plugin to compile templates, [supports Handlebars](https://github.com/webdiscus/html-bundler-webpack-plugin#using-template-handlebars).
|
|
||||||
- [incremental-bars](https://github.com/atomictag/incremental-bars) adds support for [incremental-dom](https://github.com/google/incremental-dom) as template target to Handlebars.
|
|
||||||
- [jQuery plugin](https://71104.github.io/jquery-handlebars/) allows you to use Handlebars.js with [jQuery](http://jquery.com/).
|
|
||||||
- [just-handlebars-helpers](https://github.com/leapfrogtechnology/just-handlebars-helpers) A fully tested lightweight package with common Handlebars helpers.
|
|
||||||
- [koa-hbs](https://github.com/jwilm/koa-hbs) [koa](https://github.com/koajs/koa) generator based renderer for Handlebars.js.
|
|
||||||
- [Marionette.Handlebars](https://github.com/hashchange/marionette.handlebars) adds support for Handlebars and Mustache templates to Marionette.
|
|
||||||
- [openVALIDATION](https://github.com/openvalidation/openvalidation) a natural language compiler for validation rules. Generates program code in Java, JavaScript, C#, Python and Rust with handlebars.
|
|
||||||
- [Plop](https://plopjs.com/) is a micro-generator framework that makes it easy to create files with a level of uniformity.
|
|
||||||
- [promised-handlebars](https://github.com/nknapp/promised-handlebars) is a wrapper for Handlebars that allows helpers to return Promises.
|
|
||||||
- [sammy.js](https://github.com/quirkey/sammy) by Aaron Quint, a.k.a. quirkey, supports Handlebars.js as one of its template plugins.
|
|
||||||
- [Swag](https://github.com/elving/swag) by [@elving](https://github.com/elving) is a growing collection of helpers for handlebars.js. Give your handlebars.js templates some swag son!
|
|
||||||
- [SproutCore](https://www.sproutcore.com) uses Handlebars.js as its main templating engine, extending it with automatic data binding support.
|
|
||||||
- [vite-plugin-handlebars](https://github.com/alexlafroscia/vite-plugin-handlebars) A package for Vite 2. Allows for running your HTML files through the Handlebars compiler.
|
|
||||||
- [YUI](https://yuilibrary.com/yui/docs/handlebars/) implements a port of handlebars.
|
|
||||||
|
|
||||||
## External Resources
|
|
||||||
|
|
||||||
- [Gist about Synchronous and asynchronous loading of external handlebars templates](https://gist.github.com/2287070)
|
|
||||||
|
|
||||||
Have a project using Handlebars? Send us a [pull request][pull-request]!
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Handlebars.js is released under the MIT license.
|
|
||||||
|
|
||||||
[pull-request]: https://github.com/handlebars-lang/handlebars.js/pull/new/master
|
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
# Security Policy
|
|
||||||
|
|
||||||
We recommend always using the latest versions of Handlebars and its official companion libraries to ensure your application remains as secure as possible.
|
|
||||||
|
|
||||||
## Supported Versions
|
|
||||||
|
|
||||||
| Version | Supported |
|
|
||||||
| ------- | ------------------ |
|
|
||||||
| 5.0.x | :white_check_mark: |
|
|
||||||
| 4.7.x | :white_check_mark: |
|
|
||||||
| < 4.7 | :x: |
|
|
||||||
|
|
||||||
## Reporting a Vulnerability
|
|
||||||
|
|
||||||
To report a vulnerability, please visit https://github.com/handlebars-lang/handlebars.js/security.
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module.exports = {
|
||||||
|
rules: {
|
||||||
|
'no-console': 0,
|
||||||
|
'no-var': 0
|
||||||
|
}
|
||||||
|
};
|
||||||
Executable
+176
@@ -0,0 +1,176 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
var argv = parseArgs({
|
||||||
|
'f': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Output File',
|
||||||
|
'alias': 'output'
|
||||||
|
},
|
||||||
|
'map': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Source Map File'
|
||||||
|
},
|
||||||
|
'a': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Exports amd style (require.js)',
|
||||||
|
'alias': 'amd'
|
||||||
|
},
|
||||||
|
'c': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Exports CommonJS style, path to Handlebars module',
|
||||||
|
'alias': 'commonjs',
|
||||||
|
'default': null
|
||||||
|
},
|
||||||
|
'h': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Path to handlebar.js (only valid for amd-style)',
|
||||||
|
'alias': 'handlebarPath',
|
||||||
|
'default': ''
|
||||||
|
},
|
||||||
|
'k': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Known helpers',
|
||||||
|
'alias': 'known'
|
||||||
|
},
|
||||||
|
'o': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Known helpers only',
|
||||||
|
'alias': 'knownOnly'
|
||||||
|
},
|
||||||
|
'm': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Minimize output',
|
||||||
|
'alias': 'min'
|
||||||
|
},
|
||||||
|
'n': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Template namespace',
|
||||||
|
'alias': 'namespace',
|
||||||
|
'default': 'Handlebars.templates'
|
||||||
|
},
|
||||||
|
's': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Output template function only.',
|
||||||
|
'alias': 'simple'
|
||||||
|
},
|
||||||
|
'N': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Name of passed string templates. Optional if running in a simple mode. Required when operating on multiple templates.',
|
||||||
|
'alias': 'name'
|
||||||
|
},
|
||||||
|
'i': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Generates a template from the passed CLI argument.\n"-" is treated as a special value and causes stdin to be read for the template value.',
|
||||||
|
'alias': 'string'
|
||||||
|
},
|
||||||
|
'r': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Template root. Base value that will be stripped from template names.',
|
||||||
|
'alias': 'root'
|
||||||
|
},
|
||||||
|
'p': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Compiling a partial template',
|
||||||
|
'alias': 'partial'
|
||||||
|
},
|
||||||
|
'd': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Include data when compiling',
|
||||||
|
'alias': 'data'
|
||||||
|
},
|
||||||
|
'e': {
|
||||||
|
'type': 'string',
|
||||||
|
'description': 'Template extension.',
|
||||||
|
'alias': 'extension',
|
||||||
|
'default': 'handlebars'
|
||||||
|
},
|
||||||
|
'b': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Removes the BOM (Byte Order Mark) from the beginning of the templates.',
|
||||||
|
'alias': 'bom'
|
||||||
|
},
|
||||||
|
'v': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Prints the current compiler version',
|
||||||
|
'alias': 'version'
|
||||||
|
},
|
||||||
|
'help': {
|
||||||
|
'type': 'boolean',
|
||||||
|
'description': 'Outputs this message'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
argv.files = argv._;
|
||||||
|
delete argv._;
|
||||||
|
|
||||||
|
var Precompiler = require('../dist/cjs/precompiler');
|
||||||
|
Precompiler.loadTemplates(argv, function(err, opts) {
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.help || (!opts.templates.length && !opts.version)) {
|
||||||
|
printUsage(argv._spec, 120);
|
||||||
|
} else {
|
||||||
|
Precompiler.cli(opts);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function pad(n) {
|
||||||
|
var str = '';
|
||||||
|
while (str.length < n) {
|
||||||
|
str += ' ';
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseArgs(spec) {
|
||||||
|
var opts = { alias: {}, boolean: [], default: {}, string: [] };
|
||||||
|
|
||||||
|
Object.keys(spec).forEach(function (arg) {
|
||||||
|
var opt = spec[arg];
|
||||||
|
opts[opt.type].push(arg);
|
||||||
|
if ('alias' in opt) opts.alias[arg] = opt.alias;
|
||||||
|
if ('default' in opt) opts.default[arg] = opt.default;
|
||||||
|
});
|
||||||
|
|
||||||
|
var argv = require('minimist')(process.argv.slice(2), opts);
|
||||||
|
argv._spec = spec;
|
||||||
|
return argv;
|
||||||
|
}
|
||||||
|
|
||||||
|
function printUsage(spec, wrap) {
|
||||||
|
var wordwrap = require('wordwrap');
|
||||||
|
|
||||||
|
console.log('Precompile handlebar templates.');
|
||||||
|
console.log('Usage: handlebars [template|directory]...');
|
||||||
|
|
||||||
|
var opts = [];
|
||||||
|
var width = 0;
|
||||||
|
Object.keys(spec).forEach(function (arg) {
|
||||||
|
var opt = spec[arg];
|
||||||
|
|
||||||
|
var name = (arg.length === 1 ? '-' : '--') + arg;
|
||||||
|
if ('alias' in opt) name += ', --' + opt.alias;
|
||||||
|
|
||||||
|
var meta = '[' + opt.type + ']';
|
||||||
|
if ('default' in opt) meta += ' [default: ' + JSON.stringify(opt.default) + ']';
|
||||||
|
|
||||||
|
opts.push({ name: name, desc: opt.description, meta: meta });
|
||||||
|
if (name.length > width) width = name.length;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Options:');
|
||||||
|
opts.forEach(function (opt) {
|
||||||
|
var desc = wordwrap(width + 4, wrap + 1)(opt.desc);
|
||||||
|
|
||||||
|
console.log(' %s%s%s%s%s',
|
||||||
|
opt.name,
|
||||||
|
pad(width - opt.name.length + 2),
|
||||||
|
desc.slice(width + 4),
|
||||||
|
pad(wrap - opt.meta.length - desc.split(/\n/).pop().length),
|
||||||
|
opt.meta
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
|
|
||||||
import { loadTemplates, cli } from '../lib/precompiler.js';
|
|
||||||
import yargs from 'yargs';
|
|
||||||
|
|
||||||
const parser = yargs(process.argv.slice(2))
|
|
||||||
.usage('Precompile handlebar templates.\nUsage: $0 [template|directory]...')
|
|
||||||
.help(false)
|
|
||||||
.version(false)
|
|
||||||
.option('f', {
|
|
||||||
type: 'string',
|
|
||||||
description: 'Output File',
|
|
||||||
alias: 'output',
|
|
||||||
})
|
|
||||||
.option('map', {
|
|
||||||
type: 'string',
|
|
||||||
description: 'Source Map File',
|
|
||||||
})
|
|
||||||
.option('k', {
|
|
||||||
type: 'string',
|
|
||||||
description: 'Known helpers',
|
|
||||||
alias: 'known',
|
|
||||||
})
|
|
||||||
.option('o', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Known helpers only',
|
|
||||||
alias: 'knownOnly',
|
|
||||||
})
|
|
||||||
.option('m', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Minimize output',
|
|
||||||
alias: 'min',
|
|
||||||
})
|
|
||||||
.option('n', {
|
|
||||||
type: 'string',
|
|
||||||
description: 'Template namespace',
|
|
||||||
alias: 'namespace',
|
|
||||||
default: 'Handlebars.templates',
|
|
||||||
})
|
|
||||||
.option('s', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Output template function only.',
|
|
||||||
alias: 'simple',
|
|
||||||
})
|
|
||||||
.option('N', {
|
|
||||||
type: 'string',
|
|
||||||
description:
|
|
||||||
'Name of passed string templates. Optional if running in a simple mode. Required when operating on multiple templates.',
|
|
||||||
alias: 'name',
|
|
||||||
})
|
|
||||||
.option('i', {
|
|
||||||
type: 'string',
|
|
||||||
description:
|
|
||||||
'Generates a template from the passed CLI argument.\n"-" is treated as a special value and causes stdin to be read for the template value.',
|
|
||||||
alias: 'string',
|
|
||||||
})
|
|
||||||
.option('r', {
|
|
||||||
type: 'string',
|
|
||||||
description:
|
|
||||||
'Template root. Base value that will be stripped from template names.',
|
|
||||||
alias: 'root',
|
|
||||||
})
|
|
||||||
.option('p', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Compiling a partial template',
|
|
||||||
alias: 'partial',
|
|
||||||
})
|
|
||||||
.option('d', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Include data when compiling',
|
|
||||||
alias: 'data',
|
|
||||||
})
|
|
||||||
.option('e', {
|
|
||||||
type: 'string',
|
|
||||||
description: 'Template extension.',
|
|
||||||
alias: 'extension',
|
|
||||||
default: 'handlebars',
|
|
||||||
})
|
|
||||||
.option('b', {
|
|
||||||
type: 'boolean',
|
|
||||||
description:
|
|
||||||
'Removes the BOM (Byte Order Mark) from the beginning of the templates.',
|
|
||||||
alias: 'bom',
|
|
||||||
})
|
|
||||||
.option('v', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Prints the current compiler version',
|
|
||||||
alias: 'version',
|
|
||||||
})
|
|
||||||
.option('help', {
|
|
||||||
type: 'boolean',
|
|
||||||
description: 'Outputs this message',
|
|
||||||
})
|
|
||||||
.wrap(120);
|
|
||||||
|
|
||||||
const argv = parser.parseSync();
|
|
||||||
argv.files = argv._;
|
|
||||||
delete argv._;
|
|
||||||
|
|
||||||
loadTemplates(argv, function (err, opts) {
|
|
||||||
if (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.help || (!opts.templates.length && !opts.version)) {
|
|
||||||
parser.showHelp('log');
|
|
||||||
} else {
|
|
||||||
// cli() is async (returns a Promise), so errors would become unhandled
|
|
||||||
// rejections. Re-throw via nextTick to surface them as uncaught exceptions.
|
|
||||||
Promise.resolve(cli(opts)).catch((error) => {
|
|
||||||
process.nextTick(() => {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "handlebars",
|
"name": "handlebars",
|
||||||
"version": "5.0.0-alpha.1",
|
"version": "4.7.9",
|
||||||
"main": "handlebars.js",
|
"main": "handlebars.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {}
|
"dependencies": {}
|
||||||
|
|||||||
@@ -3,5 +3,7 @@
|
|||||||
"repo": "components/handlebars.js",
|
"repo": "components/handlebars.js",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "handlebars.js",
|
"main": "handlebars.js",
|
||||||
"scripts": ["handlebars.js"]
|
"scripts": [
|
||||||
|
"handlebars.js"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "components/handlebars.js",
|
"name": "components/handlebars.js",
|
||||||
"description": "Handlebars.js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be.",
|
"description": "Handlebars.js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be.",
|
||||||
"homepage": "http://handlebarsjs.com",
|
"homepage": "https://handlebarsjs.com",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "component",
|
"type": "component",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -11,13 +11,9 @@
|
|||||||
],
|
],
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Chris Wanstrath",
|
"name": "Chris Wanstrath"
|
||||||
"homepage": "http://chriswanstrath.com"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
|
||||||
"robloach/component-installer": "*"
|
|
||||||
},
|
|
||||||
"extra": {
|
"extra": {
|
||||||
"component": {
|
"component": {
|
||||||
"name": "handlebars",
|
"name": "handlebars",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<package>
|
<package>
|
||||||
<metadata>
|
<metadata>
|
||||||
<id>handlebars.js</id>
|
<id>handlebars.js</id>
|
||||||
<version>5.0.0-alpha.1</version>
|
<version>4.7.9</version>
|
||||||
<authors>handlebars.js Authors</authors>
|
<authors>handlebars.js Authors</authors>
|
||||||
<licenseUrl>https://github.com/handlebars-lang/handlebars.js/blob/master/LICENSE</licenseUrl>
|
<licenseUrl>https://github.com/handlebars-lang/handlebars.js/blob/master/LICENSE</licenseUrl>
|
||||||
<projectUrl>https://github.com/handlebars-lang/handlebars.js/</projectUrl>
|
<projectUrl>https://github.com/handlebars-lang/handlebars.js/</projectUrl>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "handlebars",
|
"name": "handlebars",
|
||||||
"version": "5.0.0-alpha.1",
|
"version": "4.7.9",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"jspm": {
|
"jspm": {
|
||||||
"main": "handlebars",
|
"main": "handlebars",
|
||||||
|
|||||||
+24
-23
@@ -34,8 +34,8 @@ let ast = Handlebars.parseWithoutProcessing(myTemplate);
|
|||||||
|
|
||||||
`Handlebars.parse` will parse the template with `parseWithoutProcessing` (see above) then it will update the AST to strip extraneous whitespace. The whitespace stripping functionality handles two distinct situations:
|
`Handlebars.parse` will parse the template with `parseWithoutProcessing` (see above) then it will update the AST to strip extraneous whitespace. The whitespace stripping functionality handles two distinct situations:
|
||||||
|
|
||||||
- Removes whitespace around dynamic statements that are on a line by themselves (aka "stand alone")
|
* Removes whitespace around dynamic statements that are on a line by themselves (aka "stand alone")
|
||||||
- Applies "whitespace control" characters (i.e. `~`) by truncating the `ContentStatement` `value` property appropriately (e.g. `\n\n{{~foo}}` would have a `ContentStatement` with a `value` of `''`)
|
* Applies "whitespace control" characters (i.e. `~`) by truncating the `ContentStatement` `value` property appropriately (e.g. `\n\n{{~foo}}` would have a `ContentStatement` with a `value` of `''`)
|
||||||
|
|
||||||
`Handlebars.parse` is used internally by `Handlebars.precompile` and `Handlebars.compile`.
|
`Handlebars.parse` is used internally by `Handlebars.precompile` and `Handlebars.compile`.
|
||||||
|
|
||||||
@@ -132,6 +132,7 @@ interface PartialBlockStatement <: Statement {
|
|||||||
|
|
||||||
`name` will be a `SubExpression` when tied to a dynamic partial, i.e. `{{> (foo) }}`, otherwise this is a path or literal whose `original` value is used to lookup the desired partial.
|
`name` will be a `SubExpression` when tied to a dynamic partial, i.e. `{{> (foo) }}`, otherwise this is a path or literal whose `original` value is used to lookup the desired partial.
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
interface ContentStatement <: Statement {
|
interface ContentStatement <: Statement {
|
||||||
type: "ContentStatement";
|
type: "ContentStatement";
|
||||||
@@ -147,6 +148,7 @@ interface CommentStatement <: Statement {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
interface Decorator <: Statement {
|
interface Decorator <: Statement {
|
||||||
type: "Decorator";
|
type: "Decorator";
|
||||||
@@ -207,6 +209,7 @@ interface PathExpression <: Expression {
|
|||||||
- `parts` is an array of the names in the path. `foo.bar` would be `['foo', 'bar']`. Scope references, `.`, `..`, and `this` should be omitted from this array.
|
- `parts` is an array of the names in the path. `foo.bar` would be `['foo', 'bar']`. Scope references, `.`, `..`, and `this` should be omitted from this array.
|
||||||
- `original` is the path as entered by the user. Separator and scope references are left untouched.
|
- `original` is the path as entered by the user. Separator and scope references are left untouched.
|
||||||
|
|
||||||
|
|
||||||
##### Literals
|
##### Literals
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@@ -239,6 +242,7 @@ interface NullLiteral <: Literal {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Miscellaneous
|
### Miscellaneous
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@@ -287,7 +291,7 @@ scanner.accept(ast);
|
|||||||
|
|
||||||
The current node's ancestors will be maintained in the `parents` array, with the most recent parent listed first.
|
The current node's ancestors will be maintained in the `parents` array, with the most recent parent listed first.
|
||||||
|
|
||||||
The visitor may also be configured to operate in mutation mode by setting the `mutating` field to true. When in this mode, handler methods may return any valid AST node and it will replace the one they are currently operating on. Returning `false` will remove the given value (if valid) and returning `undefined` will leave the node intact. This return structure only apply to mutation mode and non-mutation mode visitors are free to return whatever values they wish.
|
The visitor may also be configured to operate in mutation mode by setting the `mutation` field to true. When in this mode, handler methods may return any valid AST node and it will replace the one they are currently operating on. Returning `false` will remove the given value (if valid) and returning `undefined` will leave the node in tact. This return structure only apply to mutation mode and non-mutation mode visitors are free to return whatever values they wish.
|
||||||
|
|
||||||
Implementors that may need to support mutation mode are encouraged to utilize the `acceptKey`, `acceptRequired` and `acceptArray` helpers which provide the conditional overwrite behavior as well as implement sanity checks where pertinent.
|
Implementors that may need to support mutation mode are encouraged to utilize the `acceptKey`, `acceptRequired` and `acceptArray` helpers which provide the conditional overwrite behavior as well as implement sanity checks where pertinent.
|
||||||
|
|
||||||
@@ -296,7 +300,8 @@ Implementors that may need to support mutation mode are encouraged to utilize th
|
|||||||
The `Handlebars.JavaScriptCompiler` object has a number of methods that may be customized to alter the output of the compiler:
|
The `Handlebars.JavaScriptCompiler` object has a number of methods that may be customized to alter the output of the compiler:
|
||||||
|
|
||||||
- `nameLookup(parent, name, type)`
|
- `nameLookup(parent, name, type)`
|
||||||
Used to generate the code to resolve a given path component.
|
Used to generate the code to resolve a give path component.
|
||||||
|
|
||||||
- `parent` is the existing code in the path resolution
|
- `parent` is the existing code in the path resolution
|
||||||
- `name` is the current path component
|
- `name` is the current path component
|
||||||
- `type` is the type of name being evaluated. May be one of `context`, `data`, `helper`, `decorator`, or `partial`.
|
- `type` is the type of name being evaluated. May be one of `context`, `data`, `helper`, `decorator`, or `partial`.
|
||||||
@@ -311,6 +316,7 @@ The `Handlebars.JavaScriptCompiler` object has a number of methods that may be c
|
|||||||
|
|
||||||
- `appendToBuffer(source, location, explicit)`
|
- `appendToBuffer(source, location, explicit)`
|
||||||
Allows for code buffer emitting code. Defaults behavior is string concatenation.
|
Allows for code buffer emitting code. Defaults behavior is string concatenation.
|
||||||
|
|
||||||
- `source` is the source code whose result is to be appending
|
- `source` is the source code whose result is to be appending
|
||||||
- `location` is the location of the source in the source map.
|
- `location` is the location of the source in the source map.
|
||||||
- `explicit` is a flag signaling that the emit operation must occur, vs. the lazy evaled options otherwise.
|
- `explicit` is a flag signaling that the emit operation must occur, vs. the lazy evaled options otherwise.
|
||||||
@@ -324,6 +330,7 @@ This example changes all lookups of properties are performed by a helper (`looku
|
|||||||
|
|
||||||
There is also [a jsfiddle with this code](https://jsfiddle.net/9D88g/162/) if you want to play around with it.
|
There is also [a jsfiddle with this code](https://jsfiddle.net/9D88g/162/) if you want to play around with it.
|
||||||
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function MyCompiler() {
|
function MyCompiler() {
|
||||||
Handlebars.JavaScriptCompiler.apply(this, arguments);
|
Handlebars.JavaScriptCompiler.apply(this, arguments);
|
||||||
@@ -331,35 +338,29 @@ function MyCompiler() {
|
|||||||
MyCompiler.prototype = new Handlebars.JavaScriptCompiler();
|
MyCompiler.prototype = new Handlebars.JavaScriptCompiler();
|
||||||
|
|
||||||
// Use this compile to compile BlockStatment-Blocks
|
// Use this compile to compile BlockStatment-Blocks
|
||||||
MyCompiler.prototype.compiler = MyCompiler;
|
MyCompiler.prototype.compiler = MyCompiler
|
||||||
|
|
||||||
MyCompiler.prototype.nameLookup = function(parent, name, type) {
|
MyCompiler.prototype.nameLookup = function(parent, name, type) {
|
||||||
if (type === 'context') {
|
if (type === 'context') {
|
||||||
return this.source.functionCall('helpers.lookupLowerCase', '', [
|
return this.source.functionCall('helpers.lookupLowerCase', '', [parent, JSON.stringify(name)])
|
||||||
parent,
|
|
||||||
JSON.stringify(name),
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
return Handlebars.JavaScriptCompiler.prototype.nameLookup.call(
|
return Handlebars.JavaScriptCompiler.prototype.nameLookup.call(this, parent, name, type);
|
||||||
this,
|
}
|
||||||
parent,
|
|
||||||
name,
|
|
||||||
type
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
var env = Handlebars.create();
|
var env = Handlebars.create();
|
||||||
env.registerHelper('lookupLowerCase', function(parent, name) {
|
env.registerHelper('lookupLowerCase', function(parent, name) {
|
||||||
return parent[name.toLowerCase()];
|
return parent[name.toLowerCase()]
|
||||||
});
|
})
|
||||||
|
|
||||||
env.JavaScriptCompiler = MyCompiler;
|
env.JavaScriptCompiler = MyCompiler;
|
||||||
|
|
||||||
var template = env.compile('{{#each Test}} ({{Value}}) {{/each}}');
|
var template = env.compile('{{#each Test}} ({{Value}}) {{/each}}');
|
||||||
console.log(
|
console.log(template({
|
||||||
template({
|
test: [
|
||||||
test: [{ value: 'a' }, { value: 'b' }, { value: 'c' }],
|
{value: 'a'},
|
||||||
})
|
{value: 'b'},
|
||||||
);
|
{value: 'c'}
|
||||||
|
]
|
||||||
|
}));
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
# Decorators
|
# Decorators
|
||||||
|
|
||||||
**Decorators are deprecated, please join the discussion at [#1574](https://github.com/handlebars-lang/handlebars.js/issues/1574) to see what we can do about it.**
|
|
||||||
|
|
||||||
Decorators allow for blocks to be annotated with metadata or wrapped in functionality prior to execution of the block. This may be used to communicate with the containing helper or to setup a particular state in the system prior to running the block.
|
Decorators allow for blocks to be annotated with metadata or wrapped in functionality prior to execution of the block. This may be used to communicate with the containing helper or to setup a particular state in the system prior to running the block.
|
||||||
|
|
||||||
Decorators are registered through similar methods as helpers, `registerDecorators` and `unregisterDecorators`. These can then be referenced via the friendly name in the template using the `{{* decorator}}` and `{{#* decorator}}{/decorator}}` syntaxes. These syntaxes are derivatives of the normal mustache syntax and as such have all of the same argument and whitespace behaviors.
|
Decorators are registered through similar methods as helpers, `registerDecorators` and `unregisterDecorators`. These can then be referenced via the friendly name in the template using the `{{* decorator}}` and `{{#* decorator}}{/decorator}}` syntaxes. These syntaxes are derivatives of the normal mustache syntax and as such have all of the same argument and whitespace behaviors.
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
import compat from 'eslint-plugin-compat';
|
|
||||||
|
|
||||||
export default [
|
|
||||||
{
|
|
||||||
// Ignore everything except lib/
|
|
||||||
ignores: ['**', '!lib/**'],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// Only check browser API compat in the runtime library code.
|
|
||||||
// All other linting is handled by oxlint.
|
|
||||||
...compat.configs['flat/recommended'],
|
|
||||||
files: ['lib/**/*.js'],
|
|
||||||
linterOptions: {
|
|
||||||
reportUnusedDisableDirectives: 'off',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
import Handlebars from './lib/index.js';
|
|
||||||
import fs from 'fs';
|
|
||||||
|
|
||||||
// Read the basic.js test file and extract test cases
|
|
||||||
// For now, manually define a set of representative test cases
|
|
||||||
const tests = [];
|
|
||||||
|
|
||||||
function T(template, input, expected, opts) {
|
|
||||||
tests.push({
|
|
||||||
template,
|
|
||||||
input: input ?? {},
|
|
||||||
expected,
|
|
||||||
compileOpts: opts?.compile ?? {},
|
|
||||||
runtimeOpts: opts?.runtime ?? {},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============ basic.js equivalents ============
|
|
||||||
const g = (t, i, e, co, ro) => T(t, i, e, { compile: co, runtime: ro });
|
|
||||||
|
|
||||||
g('{{foo}}', { foo: 'foo' }, 'foo');
|
|
||||||
g('\\{{foo}}', { foo: 'food' }, '{{foo}}');
|
|
||||||
g('content \\{{foo}}', { foo: 'food' }, 'content {{foo}}');
|
|
||||||
g('\\\\{{foo}}', { foo: 'food' }, '\\food');
|
|
||||||
g('\\\\ {{foo}}', { foo: 'food' }, '\\\\ food');
|
|
||||||
g(
|
|
||||||
'Goodbye\n{{cruel}}\n{{world}}!',
|
|
||||||
{ cruel: 'cruel', world: 'world' },
|
|
||||||
'Goodbye\ncruel\nworld!'
|
|
||||||
);
|
|
||||||
g('{{.}}{{length}}', 'bye', 'bye3');
|
|
||||||
g('Goodbye\n{{cruel}}\n{{world.bar}}!', undefined, 'Goodbye\n\n!');
|
|
||||||
g(
|
|
||||||
'{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!',
|
|
||||||
{ cruel: 'cruel', world: 'world' },
|
|
||||||
'Goodbye\ncruel\nworld!'
|
|
||||||
);
|
|
||||||
g(' {{~! comment ~}} blah', {}, 'blah');
|
|
||||||
g(' {{~!-- long-comment --~}} blah', {}, 'blah');
|
|
||||||
g(' {{! comment ~}} blah', {}, ' blah');
|
|
||||||
g(' {{~! comment}} blah', {}, ' blah');
|
|
||||||
g('{{#if foo}}foo{{/if}}', { foo: true }, 'foo');
|
|
||||||
g('{{#if foo}}foo{{else}}bar{{/if}}', { foo: false }, 'bar');
|
|
||||||
g('{{#unless foo}}bar{{/unless}}', { foo: false }, 'bar');
|
|
||||||
g('{{#with foo}}{{bar}}{{/with}}', { foo: { bar: 'baz' } }, 'baz');
|
|
||||||
g('{{#each items}}{{this}}{{/each}}', { items: ['a', 'b', 'c'] }, 'abc');
|
|
||||||
g('{{#each items}}{{@index}}{{/each}}', { items: ['a', 'b'] }, '01');
|
|
||||||
g('<b>{{foo}}</b>', { foo: '&' }, '<b>&</b>');
|
|
||||||
g('{{{foo}}}', { foo: '<b>' }, '<b>');
|
|
||||||
g('{{foo.bar}}', { foo: { bar: 'baz' } }, 'baz');
|
|
||||||
g('{{foo/bar}}', { foo: { bar: 'baz' } }, 'baz');
|
|
||||||
g('{{../foo}}', {}, ''); // depth at root = empty
|
|
||||||
g('{{{foo}}}', { foo: '&' }, '&');
|
|
||||||
|
|
||||||
// ============ blocks.js equivalents ============
|
|
||||||
g(
|
|
||||||
'{{#list people}}{{firstName}} {{lastName}}\n{{/list}}',
|
|
||||||
{
|
|
||||||
people: [
|
|
||||||
{ firstName: 'Yehuda', lastName: 'Katz' },
|
|
||||||
{ firstName: 'Carl', lastName: 'Lerche' },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
'Yehuda Katz\nCarl Lerche\n'
|
|
||||||
);
|
|
||||||
g(
|
|
||||||
'{{#list people}}{{../prefix}} {{firstName}}\n{{/list}}',
|
|
||||||
{ people: [{ firstName: 'Yehuda' }, { firstName: 'Carl' }], prefix: 'Mr' },
|
|
||||||
'Mr Yehuda\nMr Carl\n'
|
|
||||||
);
|
|
||||||
g(
|
|
||||||
'{{#each people as |person|}}{{person}}\n{{/each}}',
|
|
||||||
{ people: ['Yehuda', 'Carl'] },
|
|
||||||
'Yehuda\nCarl\n'
|
|
||||||
);
|
|
||||||
|
|
||||||
// ============ builtins.js equivalents ============
|
|
||||||
g(
|
|
||||||
'{{#if goodbye}}GOODBYE {{/if}}cruel {{world}}!',
|
|
||||||
{ goodbye: true, world: 'world' },
|
|
||||||
'GOODBYE cruel world!'
|
|
||||||
);
|
|
||||||
g(
|
|
||||||
'{{#if goodbye}}GOODBYE {{/if}}cruel {{world}}!',
|
|
||||||
{ goodbye: false, world: 'world' },
|
|
||||||
'cruel world!'
|
|
||||||
);
|
|
||||||
g('{{lookup foo "bar"}}', { foo: { bar: 'val' } }, 'val');
|
|
||||||
g(
|
|
||||||
'{{#each items as |value key|}}{{key}}:{{value}},{{/each}}',
|
|
||||||
{ items: { a: 1, b: 2 } },
|
|
||||||
'a:1,b:2,'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Write output
|
|
||||||
const results = tests.map((t, i) => {
|
|
||||||
let result,
|
|
||||||
error = null;
|
|
||||||
try {
|
|
||||||
const env = Handlebars.create();
|
|
||||||
const compiled = env.compile(t.template, t.compileOpts);
|
|
||||||
result = compiled(t.input, t.runtimeOpts);
|
|
||||||
} catch (e) {
|
|
||||||
error = e.message;
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
i,
|
|
||||||
template: t.template,
|
|
||||||
input: JSON.stringify(t.input),
|
|
||||||
expected: t.expected,
|
|
||||||
result,
|
|
||||||
error,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
fs.writeFileSync('/tmp/golden_results.json', JSON.stringify(results, null, 2));
|
|
||||||
console.log(`Generated ${results.length} golden results`);
|
|
||||||
const failures = results.filter((r) => r.result !== r.expected);
|
|
||||||
if (failures.length) {
|
|
||||||
console.log(`\nFAILURES (${failures.length}):`);
|
|
||||||
failures.forEach((f) =>
|
|
||||||
console.log(
|
|
||||||
` [${f.i}] "${f.template}" expected="${f.expected}" got="${f.result}" error="${f.error}"`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
// Handlebars should not use node or browser-specific APIs
|
||||||
|
'shared-node-browser': true,
|
||||||
|
node: false,
|
||||||
|
browser: false
|
||||||
|
}
|
||||||
|
};
|
||||||
+10
-15
@@ -1,22 +1,17 @@
|
|||||||
|
import runtime from './handlebars.runtime';
|
||||||
|
|
||||||
|
// Compiler imports
|
||||||
|
import AST from './handlebars/compiler/ast';
|
||||||
import {
|
import {
|
||||||
parser as Parser,
|
parser as Parser,
|
||||||
parse,
|
parse,
|
||||||
parseWithoutProcessing,
|
parseWithoutProcessing
|
||||||
Visitor,
|
} from './handlebars/compiler/base';
|
||||||
} from '@handlebars/parser';
|
import { Compiler, compile, precompile } from './handlebars/compiler/compiler';
|
||||||
|
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler';
|
||||||
|
import Visitor from './handlebars/compiler/visitor';
|
||||||
|
|
||||||
import runtime from './handlebars.runtime.js';
|
import noConflict from './handlebars/no-conflict';
|
||||||
|
|
||||||
// Compiler imports
|
|
||||||
import AST from './handlebars/compiler/ast.js';
|
|
||||||
import {
|
|
||||||
Compiler,
|
|
||||||
compile,
|
|
||||||
precompile,
|
|
||||||
} from './handlebars/compiler/compiler.js';
|
|
||||||
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler.js';
|
|
||||||
|
|
||||||
import noConflict from './handlebars/no-conflict.js';
|
|
||||||
|
|
||||||
let _create = runtime.create;
|
let _create = runtime.create;
|
||||||
function create() {
|
function create() {
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import * as base from './handlebars/base';
|
||||||
import * as base from './handlebars/base.js';
|
|
||||||
|
|
||||||
// Each of these augment the Handlebars object. No need to setup here.
|
// Each of these augment the Handlebars object. No need to setup here.
|
||||||
// (This is done to easily share code between module systems and browser envs)
|
// (This is done to easily share code between commonjs and browse envs)
|
||||||
import SafeString from './handlebars/safe-string.js';
|
import SafeString from './handlebars/safe-string';
|
||||||
import * as Utils from './handlebars/utils.js';
|
import Exception from './handlebars/exception';
|
||||||
import * as runtime from './handlebars/runtime.js';
|
import * as Utils from './handlebars/utils';
|
||||||
|
import * as runtime from './handlebars/runtime';
|
||||||
|
|
||||||
import noConflict from './handlebars/no-conflict.js';
|
import noConflict from './handlebars/no-conflict';
|
||||||
|
|
||||||
// For compatibility and usage outside of module systems, make the Handlebars object a namespace
|
// For compatibility and usage outside of module systems, make the Handlebars object a namespace
|
||||||
function create() {
|
function create() {
|
||||||
@@ -19,10 +19,7 @@ function create() {
|
|||||||
hb.Utils = Utils;
|
hb.Utils = Utils;
|
||||||
hb.escapeExpression = Utils.escapeExpression;
|
hb.escapeExpression = Utils.escapeExpression;
|
||||||
|
|
||||||
// Spread into a plain object so that runtime functions (e.g. checkRevision)
|
hb.VM = runtime;
|
||||||
// can be overridden by consumers. ES module namespace objects are sealed with
|
|
||||||
// getter-only properties per spec, which would prevent monkey-patching.
|
|
||||||
hb.VM = { ...runtime };
|
|
||||||
hb.template = function(spec) {
|
hb.template = function(spec) {
|
||||||
return runtime.template(spec, hb);
|
return runtime.template(spec, hb);
|
||||||
};
|
};
|
||||||
@@ -37,15 +34,4 @@ noConflict(inst);
|
|||||||
|
|
||||||
inst['default'] = inst;
|
inst['default'] = inst;
|
||||||
|
|
||||||
// Named re-exports for CJS interop.
|
|
||||||
// See the comment in lib/index.js for the full explanation. In short:
|
|
||||||
// require('handlebars/runtime').COMPILER_REVISION must be directly accessible
|
|
||||||
// for tools like handlebars-loader that compare compiler and runtime revisions.
|
|
||||||
export {
|
|
||||||
VERSION,
|
|
||||||
COMPILER_REVISION,
|
|
||||||
LAST_COMPATIBLE_COMPILER_REVISION,
|
|
||||||
REVISION_CHANGES,
|
|
||||||
} from './handlebars/base.js';
|
|
||||||
|
|
||||||
export default inst;
|
export default inst;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import { createFrame, extend, toString } from './utils';
|
||||||
import { createFrame, extend, toString } from './utils.js';
|
import Exception from './exception';
|
||||||
import { registerDefaultHelpers } from './helpers.js';
|
import { registerDefaultHelpers } from './helpers';
|
||||||
import { registerDefaultDecorators } from './decorators.js';
|
import { registerDefaultDecorators } from './decorators';
|
||||||
import logger from './logger.js';
|
import logger from './logger';
|
||||||
import { resetLoggedProperties } from './internal/proto-access.js';
|
import { resetLoggedProperties } from './internal/proto-access';
|
||||||
|
|
||||||
export const VERSION = '4.7.7';
|
export const VERSION = '4.7.9';
|
||||||
export const COMPILER_REVISION = 8;
|
export const COMPILER_REVISION = 8;
|
||||||
export const LAST_COMPATIBLE_COMPILER_REVISION = 7;
|
export const LAST_COMPATIBLE_COMPILER_REVISION = 7;
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ export const REVISION_CHANGES = {
|
|||||||
5: '== 2.0.0-alpha.x',
|
5: '== 2.0.0-alpha.x',
|
||||||
6: '>= 2.0.0-beta.1',
|
6: '>= 2.0.0-beta.1',
|
||||||
7: '>= 4.0.0 <4.3.0',
|
7: '>= 4.0.0 <4.3.0',
|
||||||
8: '>= 4.3.0',
|
8: '>= 4.3.0'
|
||||||
};
|
};
|
||||||
|
|
||||||
const objectType = '[object Object]';
|
const objectType = '[object Object]';
|
||||||
@@ -86,7 +86,7 @@ HandlebarsEnvironment.prototype = {
|
|||||||
*/
|
*/
|
||||||
resetLoggedPropertyAccesses() {
|
resetLoggedPropertyAccesses() {
|
||||||
resetLoggedProperties();
|
resetLoggedProperties();
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export let log = logger.log;
|
export let log = logger.log;
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ let AST = {
|
|||||||
return (
|
return (
|
||||||
path.parts.length === 1 && !AST.helpers.scopedId(path) && !path.depth
|
path.parts.length === 1 && !AST.helpers.scopedId(path) && !path.depth
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Must be exported as an object rather than the root of the module as the jison lexer
|
// Must be exported as an object rather than the root of the module as the jison lexer
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import parser from './parser';
|
||||||
|
import WhitespaceControl from './whitespace-control';
|
||||||
|
import * as Helpers from './helpers';
|
||||||
|
import { extend } from '../utils';
|
||||||
|
|
||||||
|
export { parser };
|
||||||
|
|
||||||
|
let yy = {};
|
||||||
|
extend(yy, Helpers);
|
||||||
|
|
||||||
|
export function parseWithoutProcessing(input, options) {
|
||||||
|
// Just return if an already-compiled AST was passed in.
|
||||||
|
if (input.type === 'Program') {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.yy = yy;
|
||||||
|
|
||||||
|
// Altering the shared object here, but this is ok as parser is a sync operation
|
||||||
|
yy.locInfo = function(locInfo) {
|
||||||
|
return new yy.SourceLocation(options && options.srcName, locInfo);
|
||||||
|
};
|
||||||
|
|
||||||
|
let ast = parser.parse(input);
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parse(input, options) {
|
||||||
|
let ast = parseWithoutProcessing(input, options);
|
||||||
|
let strip = new WhitespaceControl(options);
|
||||||
|
|
||||||
|
return strip.accept(ast);
|
||||||
|
}
|
||||||
@@ -1,5 +1,50 @@
|
|||||||
import { isArray } from '../utils.js';
|
/* global define, require */
|
||||||
import { SourceNode } from '#source-node';
|
import { isArray } from '../utils';
|
||||||
|
|
||||||
|
let SourceNode;
|
||||||
|
|
||||||
|
try {
|
||||||
|
/* istanbul ignore next */
|
||||||
|
if (typeof define !== 'function' || !define.amd) {
|
||||||
|
// We don't support this in AMD environments. For these environments, we assume that
|
||||||
|
// they are running on the browser and thus have no need for the source-map library.
|
||||||
|
let SourceMap = require('source-map');
|
||||||
|
SourceNode = SourceMap.SourceNode;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
/* NOP */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore if: tested but not covered in istanbul due to dist build */
|
||||||
|
if (!SourceNode) {
|
||||||
|
SourceNode = function(line, column, srcFile, chunks) {
|
||||||
|
this.src = '';
|
||||||
|
if (chunks) {
|
||||||
|
this.add(chunks);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/* istanbul ignore next */
|
||||||
|
SourceNode.prototype = {
|
||||||
|
add: function(chunks) {
|
||||||
|
if (isArray(chunks)) {
|
||||||
|
chunks = chunks.join('');
|
||||||
|
}
|
||||||
|
this.src += chunks;
|
||||||
|
},
|
||||||
|
prepend: function(chunks) {
|
||||||
|
if (isArray(chunks)) {
|
||||||
|
chunks = chunks.join('');
|
||||||
|
}
|
||||||
|
this.src = chunks + this.src;
|
||||||
|
},
|
||||||
|
toStringWithSourceMap: function() {
|
||||||
|
return { code: this.toString() };
|
||||||
|
},
|
||||||
|
toString: function() {
|
||||||
|
return this.src;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function castChunk(chunk, codeGen, loc) {
|
function castChunk(chunk, codeGen, loc) {
|
||||||
if (isArray(chunk)) {
|
if (isArray(chunk)) {
|
||||||
@@ -87,7 +132,7 @@ CodeGen.prototype = {
|
|||||||
objectLiteral: function(obj) {
|
objectLiteral: function(obj) {
|
||||||
let pairs = [];
|
let pairs = [];
|
||||||
|
|
||||||
Object.keys(obj).forEach((key) => {
|
Object.keys(obj).forEach(key => {
|
||||||
let value = castChunk(obj[key], this);
|
let value = castChunk(obj[key], this);
|
||||||
if (value !== 'undefined') {
|
if (value !== 'undefined') {
|
||||||
pairs.push([this.quotedString(key), ':', value]);
|
pairs.push([this.quotedString(key), ':', value]);
|
||||||
@@ -120,7 +165,7 @@ CodeGen.prototype = {
|
|||||||
ret.add(']');
|
ret.add(']');
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CodeGen;
|
export default CodeGen;
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
/* eslint-disable new-cap */
|
||||||
import { isArray, indexOf, extend } from '../utils.js';
|
|
||||||
import AST from './ast.js';
|
import Exception from '../exception';
|
||||||
|
import {
|
||||||
|
isArray,
|
||||||
|
indexOf,
|
||||||
|
extend,
|
||||||
|
sanitizeDepth,
|
||||||
|
sanitizeParts
|
||||||
|
} from '../utils';
|
||||||
|
import AST from './ast';
|
||||||
|
|
||||||
const slice = [].slice;
|
const slice = [].slice;
|
||||||
|
|
||||||
@@ -50,6 +58,8 @@ Compiler.prototype = {
|
|||||||
this.opcodes = [];
|
this.opcodes = [];
|
||||||
this.children = [];
|
this.children = [];
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
this.stringParams = options.stringParams;
|
||||||
|
this.trackIds = options.trackIds;
|
||||||
|
|
||||||
options.blockParams = options.blockParams || [];
|
options.blockParams = options.blockParams || [];
|
||||||
|
|
||||||
@@ -63,7 +73,7 @@ Compiler.prototype = {
|
|||||||
unless: true,
|
unless: true,
|
||||||
with: true,
|
with: true,
|
||||||
log: true,
|
log: true,
|
||||||
lookup: true,
|
lookup: true
|
||||||
},
|
},
|
||||||
options.knownHelpers
|
options.knownHelpers
|
||||||
);
|
);
|
||||||
@@ -72,7 +82,7 @@ Compiler.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
compileProgram: function(program) {
|
compileProgram: function(program) {
|
||||||
let childCompiler = new this.compiler(),
|
let childCompiler = new this.compiler(), // eslint-disable-line new-cap
|
||||||
result = childCompiler.compile(program, this.options),
|
result = childCompiler.compile(program, this.options),
|
||||||
guid = this.guid++;
|
guid = this.guid++;
|
||||||
|
|
||||||
@@ -85,7 +95,7 @@ Compiler.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
accept: function(node) {
|
accept: function(node) {
|
||||||
/* v8 ignore next -- Sanity code */
|
/* istanbul ignore next: Sanity code */
|
||||||
if (!this[node.type]) {
|
if (!this[node.type]) {
|
||||||
throw new Exception('Unknown type: ' + node.type, node);
|
throw new Exception('Unknown type: ' + node.type, node);
|
||||||
}
|
}
|
||||||
@@ -239,7 +249,7 @@ Compiler.prototype = {
|
|||||||
name = path.parts[0],
|
name = path.parts[0],
|
||||||
isBlock = program != null || inverse != null;
|
isBlock = program != null || inverse != null;
|
||||||
|
|
||||||
this.opcode('getContext', path.depth);
|
this.opcode('getContext', sanitizeDepth(path.depth));
|
||||||
|
|
||||||
this.opcode('pushProgram', program);
|
this.opcode('pushProgram', program);
|
||||||
this.opcode('pushProgram', inverse);
|
this.opcode('pushProgram', inverse);
|
||||||
@@ -284,29 +294,32 @@ Compiler.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
PathExpression: function(path) {
|
PathExpression: function(path) {
|
||||||
this.addDepth(path.depth);
|
// Sanitize untrusted AST values at the compiler boundary.
|
||||||
this.opcode('getContext', path.depth);
|
// javascript-compiler.js trusts all opcode arguments to be safe.
|
||||||
|
const depth = sanitizeDepth(path.depth);
|
||||||
|
const parts = sanitizeParts(path.parts);
|
||||||
|
|
||||||
let name = path.parts[0],
|
this.addDepth(depth);
|
||||||
|
this.opcode('getContext', depth);
|
||||||
|
|
||||||
|
let name = parts[0],
|
||||||
scoped = AST.helpers.scopedId(path),
|
scoped = AST.helpers.scopedId(path),
|
||||||
blockParamId = !path.depth && !scoped && this.blockParamIndex(name);
|
blockParamId = !depth && !scoped && this.blockParamIndex(name);
|
||||||
|
|
||||||
if (blockParamId) {
|
if (blockParamId) {
|
||||||
this.opcode('lookupBlockParam', blockParamId, path.parts);
|
this.opcode(
|
||||||
|
'lookupBlockParam',
|
||||||
|
[Number(blockParamId[0]), Number(blockParamId[1])],
|
||||||
|
parts
|
||||||
|
);
|
||||||
} else if (!name) {
|
} else if (!name) {
|
||||||
// Context reference, i.e. `{{foo .}}` or `{{foo ..}}`
|
// Context reference, i.e. `{{foo .}}` or `{{foo ..}}`
|
||||||
this.opcode('pushContext');
|
this.opcode('pushContext');
|
||||||
} else if (path.data) {
|
} else if (path.data) {
|
||||||
this.options.data = true;
|
this.options.data = true;
|
||||||
this.opcode('lookupData', path.depth, path.parts, path.strict);
|
this.opcode('lookupData', depth, parts, path.strict);
|
||||||
} else {
|
} else {
|
||||||
this.opcode(
|
this.opcode('lookupOnContext', parts, path.falsy, path.strict, scoped);
|
||||||
'lookupOnContext',
|
|
||||||
path.parts,
|
|
||||||
path.falsy,
|
|
||||||
path.strict,
|
|
||||||
scoped
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -315,11 +328,11 @@ Compiler.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
NumberLiteral: function(number) {
|
NumberLiteral: function(number) {
|
||||||
this.opcode('pushLiteral', number.value);
|
this.opcode('pushLiteral', Number(number.value));
|
||||||
},
|
},
|
||||||
|
|
||||||
BooleanLiteral: function(bool) {
|
BooleanLiteral: function(bool) {
|
||||||
this.opcode('pushLiteral', bool.value);
|
this.opcode('pushLiteral', bool.value === true ? 'true' : 'false');
|
||||||
},
|
},
|
||||||
|
|
||||||
UndefinedLiteral: function() {
|
UndefinedLiteral: function() {
|
||||||
@@ -351,7 +364,7 @@ Compiler.prototype = {
|
|||||||
this.opcodes.push({
|
this.opcodes.push({
|
||||||
opcode: name,
|
opcode: name,
|
||||||
args: slice.call(arguments, 1),
|
args: slice.call(arguments, 1),
|
||||||
loc: this.sourceNode[0].loc,
|
loc: this.sourceNode[0].loc
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -405,7 +418,47 @@ Compiler.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
pushParam: function(val) {
|
pushParam: function(val) {
|
||||||
|
let value = val.value != null ? val.value : val.original || '';
|
||||||
|
let depth = sanitizeDepth(val.depth);
|
||||||
|
|
||||||
|
if (this.stringParams) {
|
||||||
|
if (value.replace) {
|
||||||
|
value = value.replace(/^(\.?\.\/)*/g, '').replace(/\//g, '.');
|
||||||
|
}
|
||||||
|
if (depth) {
|
||||||
|
this.addDepth(depth);
|
||||||
|
}
|
||||||
|
this.opcode('getContext', depth);
|
||||||
|
this.opcode('pushStringParam', value, val.type);
|
||||||
|
|
||||||
|
if (val.type === 'SubExpression') {
|
||||||
|
// SubExpressions get evaluated and passed in
|
||||||
|
// in string params mode.
|
||||||
this.accept(val);
|
this.accept(val);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.trackIds) {
|
||||||
|
let blockParamIndex;
|
||||||
|
if (val.parts && !AST.helpers.scopedId(val) && !val.depth) {
|
||||||
|
blockParamIndex = this.blockParamIndex(val.parts[0]);
|
||||||
|
}
|
||||||
|
if (blockParamIndex) {
|
||||||
|
let blockParamChild = val.parts.slice(1).join('.');
|
||||||
|
this.opcode('pushId', 'BlockParam', blockParamIndex, blockParamChild);
|
||||||
|
} else {
|
||||||
|
value = val.original || value;
|
||||||
|
if (value.replace) {
|
||||||
|
value = value
|
||||||
|
.replace(/^this(?:\.|$)/, '')
|
||||||
|
.replace(/^\.\//, '')
|
||||||
|
.replace(/^\.$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.opcode('pushId', val.type, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.accept(val);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setupFullMustacheParams: function(sexpr, program, inverse, omitEmpty) {
|
setupFullMustacheParams: function(sexpr, program, inverse, omitEmpty) {
|
||||||
@@ -436,44 +489,34 @@ Compiler.prototype = {
|
|||||||
return [depth, param];
|
return [depth, param];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function precompile(input, options = {}, env) {
|
export function precompile(input, options, env) {
|
||||||
validateInput(input, options);
|
if (
|
||||||
|
input == null ||
|
||||||
|
(typeof input !== 'string' && input.type !== 'Program')
|
||||||
|
) {
|
||||||
|
throw new Exception(
|
||||||
|
'You must pass a string or Handlebars AST to Handlebars.precompile. You passed ' +
|
||||||
|
input
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let environment = compileEnvironment(input, options, env);
|
options = options || {};
|
||||||
|
if (!('data' in options)) {
|
||||||
|
options.data = true;
|
||||||
|
}
|
||||||
|
if (options.compat) {
|
||||||
|
options.useDepths = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ast = env.parse(input, options),
|
||||||
|
environment = new env.Compiler().compile(ast, options);
|
||||||
return new env.JavaScriptCompiler().compile(environment, options);
|
return new env.JavaScriptCompiler().compile(environment, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function compile(input, options = {}, env) {
|
export function compile(input, options = {}, env) {
|
||||||
options = extend({}, options);
|
|
||||||
|
|
||||||
validateInput(input, options);
|
|
||||||
|
|
||||||
let compiled;
|
|
||||||
|
|
||||||
function compileInput() {
|
|
||||||
let environment = compileEnvironment(input, options, env),
|
|
||||||
templateSpec = new env.JavaScriptCompiler().compile(
|
|
||||||
environment,
|
|
||||||
options,
|
|
||||||
undefined,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
return env.template(templateSpec);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Template is only compiled on first use and cached after that point.
|
|
||||||
return function (context, execOptions) {
|
|
||||||
if (!compiled) {
|
|
||||||
compiled = compileInput();
|
|
||||||
}
|
|
||||||
return compiled.call(this, context, execOptions);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateInput(input, options) {
|
|
||||||
if (
|
if (
|
||||||
input == null ||
|
input == null ||
|
||||||
(typeof input !== 'string' && input.type !== 'Program')
|
(typeof input !== 'string' && input.type !== 'Program')
|
||||||
@@ -484,22 +527,48 @@ function validateInput(input, options) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.trackIds || options.stringParams) {
|
options = extend({}, options);
|
||||||
throw new Exception(
|
|
||||||
'TrackIds and stringParams are no longer supported. See Github #1145'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!('data' in options)) {
|
if (!('data' in options)) {
|
||||||
options.data = true;
|
options.data = true;
|
||||||
}
|
}
|
||||||
if (options.compat) {
|
if (options.compat) {
|
||||||
options.useDepths = true;
|
options.useDepths = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let compiled;
|
||||||
|
|
||||||
|
function compileInput() {
|
||||||
|
let ast = env.parse(input, options),
|
||||||
|
environment = new env.Compiler().compile(ast, options),
|
||||||
|
templateSpec = new env.JavaScriptCompiler().compile(
|
||||||
|
environment,
|
||||||
|
options,
|
||||||
|
undefined,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
return env.template(templateSpec);
|
||||||
}
|
}
|
||||||
function compileEnvironment(input, options, env) {
|
|
||||||
let ast = env.parse(input, options);
|
// Template is only compiled on first use and cached after that point.
|
||||||
return new env.Compiler().compile(ast, options);
|
function ret(context, execOptions) {
|
||||||
|
if (!compiled) {
|
||||||
|
compiled = compileInput();
|
||||||
|
}
|
||||||
|
return compiled.call(this, context, execOptions);
|
||||||
|
}
|
||||||
|
ret._setup = function(setupOptions) {
|
||||||
|
if (!compiled) {
|
||||||
|
compiled = compileInput();
|
||||||
|
}
|
||||||
|
return compiled._setup(setupOptions);
|
||||||
|
};
|
||||||
|
ret._child = function(i, data, blockParams, depths) {
|
||||||
|
if (!compiled) {
|
||||||
|
compiled = compileInput();
|
||||||
|
}
|
||||||
|
return compiled._child(i, data, blockParams, depths);
|
||||||
|
};
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function argEquals(a, b) {
|
function argEquals(a, b) {
|
||||||
@@ -528,7 +597,7 @@ function transformLiteralToPath(sexpr) {
|
|||||||
depth: 0,
|
depth: 0,
|
||||||
parts: [literal.original + ''],
|
parts: [literal.original + ''],
|
||||||
original: literal.original + '',
|
original: literal.original + '',
|
||||||
loc: literal.loc,
|
loc: literal.loc
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,219 @@
|
|||||||
|
import Exception from '../exception';
|
||||||
|
|
||||||
|
function validateClose(open, close) {
|
||||||
|
close = close.path ? close.path.original : close;
|
||||||
|
|
||||||
|
if (open.path.original !== close) {
|
||||||
|
let errorNode = { loc: open.path.loc };
|
||||||
|
|
||||||
|
throw new Exception(
|
||||||
|
open.path.original + " doesn't match " + close,
|
||||||
|
errorNode
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SourceLocation(source, locInfo) {
|
||||||
|
this.source = source;
|
||||||
|
this.start = {
|
||||||
|
line: locInfo.first_line,
|
||||||
|
column: locInfo.first_column
|
||||||
|
};
|
||||||
|
this.end = {
|
||||||
|
line: locInfo.last_line,
|
||||||
|
column: locInfo.last_column
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function id(token) {
|
||||||
|
if (/^\[.*\]$/.test(token)) {
|
||||||
|
return token.substring(1, token.length - 1);
|
||||||
|
} else {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stripFlags(open, close) {
|
||||||
|
return {
|
||||||
|
open: open.charAt(2) === '~',
|
||||||
|
close: close.charAt(close.length - 3) === '~'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stripComment(comment) {
|
||||||
|
return comment.replace(/^\{\{~?!-?-?/, '').replace(/-?-?~?\}\}$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function preparePath(data, parts, loc) {
|
||||||
|
loc = this.locInfo(loc);
|
||||||
|
|
||||||
|
let original = data ? '@' : '',
|
||||||
|
dig = [],
|
||||||
|
depth = 0;
|
||||||
|
|
||||||
|
for (let i = 0, l = parts.length; i < l; i++) {
|
||||||
|
let part = parts[i].part,
|
||||||
|
// If we have [] syntax then we do not treat path references as operators,
|
||||||
|
// i.e. foo.[this] resolves to approximately context.foo['this']
|
||||||
|
isLiteral = parts[i].original !== part;
|
||||||
|
original += (parts[i].separator || '') + part;
|
||||||
|
|
||||||
|
if (!isLiteral && (part === '..' || part === '.' || part === 'this')) {
|
||||||
|
if (dig.length > 0) {
|
||||||
|
throw new Exception('Invalid path: ' + original, { loc });
|
||||||
|
} else if (part === '..') {
|
||||||
|
depth++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dig.push(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'PathExpression',
|
||||||
|
data,
|
||||||
|
depth,
|
||||||
|
parts: dig,
|
||||||
|
original,
|
||||||
|
loc
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prepareMustache(path, params, hash, open, strip, locInfo) {
|
||||||
|
// Must use charAt to support IE pre-10
|
||||||
|
let escapeFlag = open.charAt(3) || open.charAt(2),
|
||||||
|
escaped = escapeFlag !== '{' && escapeFlag !== '&';
|
||||||
|
|
||||||
|
let decorator = /\*/.test(open);
|
||||||
|
return {
|
||||||
|
type: decorator ? 'Decorator' : 'MustacheStatement',
|
||||||
|
path,
|
||||||
|
params,
|
||||||
|
hash,
|
||||||
|
escaped,
|
||||||
|
strip,
|
||||||
|
loc: this.locInfo(locInfo)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prepareRawBlock(openRawBlock, contents, close, locInfo) {
|
||||||
|
validateClose(openRawBlock, close);
|
||||||
|
|
||||||
|
locInfo = this.locInfo(locInfo);
|
||||||
|
let program = {
|
||||||
|
type: 'Program',
|
||||||
|
body: contents,
|
||||||
|
strip: {},
|
||||||
|
loc: locInfo
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'BlockStatement',
|
||||||
|
path: openRawBlock.path,
|
||||||
|
params: openRawBlock.params,
|
||||||
|
hash: openRawBlock.hash,
|
||||||
|
program,
|
||||||
|
openStrip: {},
|
||||||
|
inverseStrip: {},
|
||||||
|
closeStrip: {},
|
||||||
|
loc: locInfo
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prepareBlock(
|
||||||
|
openBlock,
|
||||||
|
program,
|
||||||
|
inverseAndProgram,
|
||||||
|
close,
|
||||||
|
inverted,
|
||||||
|
locInfo
|
||||||
|
) {
|
||||||
|
if (close && close.path) {
|
||||||
|
validateClose(openBlock, close);
|
||||||
|
}
|
||||||
|
|
||||||
|
let decorator = /\*/.test(openBlock.open);
|
||||||
|
|
||||||
|
program.blockParams = openBlock.blockParams;
|
||||||
|
|
||||||
|
let inverse, inverseStrip;
|
||||||
|
|
||||||
|
if (inverseAndProgram) {
|
||||||
|
if (decorator) {
|
||||||
|
throw new Exception(
|
||||||
|
'Unexpected inverse block on decorator',
|
||||||
|
inverseAndProgram
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inverseAndProgram.chain) {
|
||||||
|
inverseAndProgram.program.body[0].closeStrip = close.strip;
|
||||||
|
}
|
||||||
|
|
||||||
|
inverseStrip = inverseAndProgram.strip;
|
||||||
|
inverse = inverseAndProgram.program;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inverted) {
|
||||||
|
inverted = inverse;
|
||||||
|
inverse = program;
|
||||||
|
program = inverted;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: decorator ? 'DecoratorBlock' : 'BlockStatement',
|
||||||
|
path: openBlock.path,
|
||||||
|
params: openBlock.params,
|
||||||
|
hash: openBlock.hash,
|
||||||
|
program,
|
||||||
|
inverse,
|
||||||
|
openStrip: openBlock.strip,
|
||||||
|
inverseStrip,
|
||||||
|
closeStrip: close && close.strip,
|
||||||
|
loc: this.locInfo(locInfo)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prepareProgram(statements, loc) {
|
||||||
|
if (!loc && statements.length) {
|
||||||
|
const firstLoc = statements[0].loc,
|
||||||
|
lastLoc = statements[statements.length - 1].loc;
|
||||||
|
|
||||||
|
/* istanbul ignore else */
|
||||||
|
if (firstLoc && lastLoc) {
|
||||||
|
loc = {
|
||||||
|
source: firstLoc.source,
|
||||||
|
start: {
|
||||||
|
line: firstLoc.start.line,
|
||||||
|
column: firstLoc.start.column
|
||||||
|
},
|
||||||
|
end: {
|
||||||
|
line: lastLoc.end.line,
|
||||||
|
column: lastLoc.end.column
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'Program',
|
||||||
|
body: statements,
|
||||||
|
strip: {},
|
||||||
|
loc: loc
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function preparePartialBlock(open, program, close, locInfo) {
|
||||||
|
validateClose(open, close);
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'PartialBlockStatement',
|
||||||
|
name: open.path,
|
||||||
|
params: open.params,
|
||||||
|
hash: open.hash,
|
||||||
|
program,
|
||||||
|
openStrip: open.strip,
|
||||||
|
closeStrip: close && close.strip,
|
||||||
|
loc: this.locInfo(locInfo)
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import { COMPILER_REVISION, REVISION_CHANGES } from '../base';
|
||||||
import { COMPILER_REVISION, REVISION_CHANGES } from '../base.js';
|
import Exception from '../exception';
|
||||||
import { isArray } from '../utils.js';
|
import { isArray } from '../utils';
|
||||||
import CodeGen from './code-gen.js';
|
import CodeGen from './code-gen';
|
||||||
|
|
||||||
function Literal(value) {
|
function Literal(value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
@@ -20,7 +20,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.aliasable('container.lookup'),
|
this.aliasable('container.lookup'),
|
||||||
'(depths, ',
|
'(depths, ',
|
||||||
JSON.stringify(name),
|
JSON.stringify(name),
|
||||||
')',
|
')'
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -64,6 +64,8 @@ JavaScriptCompiler.prototype = {
|
|||||||
compile: function(environment, options, context, asObject) {
|
compile: function(environment, options, context, asObject) {
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
this.stringParams = this.options.stringParams;
|
||||||
|
this.trackIds = this.options.trackIds;
|
||||||
this.precompile = !asObject;
|
this.precompile = !asObject;
|
||||||
|
|
||||||
this.name = this.environment.name;
|
this.name = this.environment.name;
|
||||||
@@ -71,7 +73,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.context = context || {
|
this.context = context || {
|
||||||
decorators: [],
|
decorators: [],
|
||||||
programs: [],
|
programs: [],
|
||||||
environments: [],
|
environments: []
|
||||||
};
|
};
|
||||||
|
|
||||||
this.preamble();
|
this.preamble();
|
||||||
@@ -112,7 +114,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.source.currentLocation = firstLoc;
|
this.source.currentLocation = firstLoc;
|
||||||
this.pushSource('');
|
this.pushSource('');
|
||||||
|
|
||||||
/* v8 ignore next */
|
/* istanbul ignore next */
|
||||||
if (this.stackSlot || this.inlineStack.length || this.compileStack.length) {
|
if (this.stackSlot || this.inlineStack.length || this.compileStack.length) {
|
||||||
throw new Exception('Compile completed with content left on stack');
|
throw new Exception('Compile completed with content left on stack');
|
||||||
}
|
}
|
||||||
@@ -123,12 +125,11 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.decorators.prepend([
|
this.decorators.prepend([
|
||||||
'var decorators = container.decorators, ',
|
'var decorators = container.decorators, ',
|
||||||
this.lookupPropertyFunctionVarDeclaration(),
|
this.lookupPropertyFunctionVarDeclaration(),
|
||||||
';\n',
|
';\n'
|
||||||
]);
|
]);
|
||||||
this.decorators.push('return fn;');
|
this.decorators.push('return fn;');
|
||||||
|
|
||||||
if (asObject) {
|
if (asObject) {
|
||||||
// eslint-disable-next-line no-new-func
|
|
||||||
this.decorators = Function.apply(this, [
|
this.decorators = Function.apply(this, [
|
||||||
'fn',
|
'fn',
|
||||||
'props',
|
'props',
|
||||||
@@ -137,7 +138,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
'data',
|
'data',
|
||||||
'blockParams',
|
'blockParams',
|
||||||
'depths',
|
'depths',
|
||||||
this.decorators.merge(),
|
this.decorators.merge()
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
this.decorators.prepend(
|
this.decorators.prepend(
|
||||||
@@ -154,27 +155,22 @@ JavaScriptCompiler.prototype = {
|
|||||||
if (!this.isChild) {
|
if (!this.isChild) {
|
||||||
let ret = {
|
let ret = {
|
||||||
compiler: this.compilerInfo(),
|
compiler: this.compilerInfo(),
|
||||||
main: fn,
|
main: fn
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.decorators) {
|
if (this.decorators) {
|
||||||
ret.main_d = this.decorators;
|
ret.main_d = this.decorators; // eslint-disable-line camelcase
|
||||||
ret.useDecorators = true;
|
ret.useDecorators = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { programs, decorators } = this.context;
|
let { programs, decorators } = this.context;
|
||||||
for (i = 0, l = programs.length; i < l; i++) {
|
for (i = 0, l = programs.length; i < l; i++) {
|
||||||
if (programs[i]) {
|
|
||||||
ret[i] = programs[i];
|
ret[i] = programs[i];
|
||||||
if (decorators[i]) {
|
if (decorators[i]) {
|
||||||
ret[i + '_d'] = decorators[i];
|
ret[i + '_d'] = decorators[i];
|
||||||
ret.useDecorators = true;
|
ret.useDecorators = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Release AST/compiler references only needed during compilation for dedup
|
|
||||||
this.context.environments.length = 0;
|
|
||||||
|
|
||||||
if (this.environment.usePartial) {
|
if (this.environment.usePartial) {
|
||||||
ret.usePartial = true;
|
ret.usePartial = true;
|
||||||
@@ -237,7 +233,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
// aliases will not be used, but this case is already being run on the client and
|
// aliases will not be used, but this case is already being run on the client and
|
||||||
// we aren't concern about minimizing the template size.
|
// we aren't concern about minimizing the template size.
|
||||||
let aliasCount = 0;
|
let aliasCount = 0;
|
||||||
Object.keys(this.aliases).forEach((alias) => {
|
Object.keys(this.aliases).forEach(alias => {
|
||||||
let node = this.aliases[alias];
|
let node = this.aliases[alias];
|
||||||
if (node.children && node.referenceCount > 1) {
|
if (node.children && node.referenceCount > 1) {
|
||||||
varDeclarations += ', alias' + ++aliasCount + '=' + alias;
|
varDeclarations += ', alias' + ++aliasCount + '=' + alias;
|
||||||
@@ -264,14 +260,14 @@ JavaScriptCompiler.prototype = {
|
|||||||
if (asObject) {
|
if (asObject) {
|
||||||
params.push(source);
|
params.push(source);
|
||||||
|
|
||||||
return Function.apply(this, params); // eslint-disable-line no-new-func
|
return Function.apply(this, params);
|
||||||
} else {
|
} else {
|
||||||
return this.source.wrap([
|
return this.source.wrap([
|
||||||
'function(',
|
'function(',
|
||||||
params.join(','),
|
params.join(','),
|
||||||
') {\n ',
|
') {\n ',
|
||||||
source,
|
source,
|
||||||
'}',
|
'}'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -282,7 +278,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
sourceSeen,
|
sourceSeen,
|
||||||
bufferStart,
|
bufferStart,
|
||||||
bufferEnd;
|
bufferEnd;
|
||||||
this.source.each((line) => {
|
this.source.each(line => {
|
||||||
if (line.appendToBuffer) {
|
if (line.appendToBuffer) {
|
||||||
if (bufferStart) {
|
if (bufferStart) {
|
||||||
line.prepend(' + ');
|
line.prepend(' + ');
|
||||||
@@ -395,7 +391,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
current,
|
current,
|
||||||
' = ',
|
' = ',
|
||||||
this.source.functionCall(blockHelperMissing, 'call', params),
|
this.source.functionCall(blockHelperMissing, 'call', params),
|
||||||
'}',
|
'}'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -426,7 +422,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
// Otherwise, the empty string is appended
|
// Otherwise, the empty string is appended
|
||||||
append: function() {
|
append: function() {
|
||||||
if (this.isInline()) {
|
if (this.isInline()) {
|
||||||
this.replaceStack((current) => [' != null ? ', current, ' : ""']);
|
this.replaceStack(current => [' != null ? ', current, ' : ""']);
|
||||||
|
|
||||||
this.pushSource(this.appendToBuffer(this.popStack()));
|
this.pushSource(this.appendToBuffer(this.popStack()));
|
||||||
} else {
|
} else {
|
||||||
@@ -436,13 +432,13 @@ JavaScriptCompiler.prototype = {
|
|||||||
local,
|
local,
|
||||||
' != null) { ',
|
' != null) { ',
|
||||||
this.appendToBuffer(local, undefined, true),
|
this.appendToBuffer(local, undefined, true),
|
||||||
' }',
|
' }'
|
||||||
]);
|
]);
|
||||||
if (this.environment.isSimple) {
|
if (this.environment.isSimple) {
|
||||||
this.pushSource([
|
this.pushSource([
|
||||||
'else { ',
|
'else { ',
|
||||||
this.appendToBuffer("''", undefined, true),
|
this.appendToBuffer("''", undefined, true),
|
||||||
' }',
|
' }'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -460,7 +456,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.aliasable('container.escapeExpression'),
|
this.aliasable('container.escapeExpression'),
|
||||||
'(',
|
'(',
|
||||||
this.popStack(),
|
this.popStack(),
|
||||||
')',
|
')'
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -554,7 +550,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
let len = parts.length;
|
let len = parts.length;
|
||||||
for (let i = startPartIndex; i < len; i++) {
|
for (let i = startPartIndex; i < len; i++) {
|
||||||
/* eslint-disable no-loop-func */
|
/* eslint-disable no-loop-func */
|
||||||
this.replaceStack((current) => {
|
this.replaceStack(current => {
|
||||||
let lookup = this.nameLookup(current, parts[i], type);
|
let lookup = this.nameLookup(current, parts[i], type);
|
||||||
// We want to ensure that zero and false are handled properly if the context (falsy flag)
|
// We want to ensure that zero and false are handled properly if the context (falsy flag)
|
||||||
// needs to have the special handling for these values.
|
// needs to have the special handling for these values.
|
||||||
@@ -583,23 +579,61 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.popStack(),
|
this.popStack(),
|
||||||
', ',
|
', ',
|
||||||
this.contextName(0),
|
this.contextName(0),
|
||||||
')',
|
')'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// [pushStringParam]
|
||||||
|
//
|
||||||
|
// On stack, before: ...
|
||||||
|
// On stack, after: string, currentContext, ...
|
||||||
|
//
|
||||||
|
// This opcode is designed for use in string mode, which
|
||||||
|
// provides the string value of a parameter along with its
|
||||||
|
// depth rather than resolving it immediately.
|
||||||
|
pushStringParam: function(string, type) {
|
||||||
|
this.pushContext();
|
||||||
|
this.pushString(type);
|
||||||
|
|
||||||
|
// If it's a subexpression, the string result
|
||||||
|
// will be pushed after this opcode.
|
||||||
|
if (type !== 'SubExpression') {
|
||||||
|
if (typeof string === 'string') {
|
||||||
|
this.pushString(string);
|
||||||
|
} else {
|
||||||
|
this.pushStackLiteral(string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
emptyHash: function(omitEmpty) {
|
emptyHash: function(omitEmpty) {
|
||||||
|
if (this.trackIds) {
|
||||||
|
this.push('{}'); // hashIds
|
||||||
|
}
|
||||||
|
if (this.stringParams) {
|
||||||
|
this.push('{}'); // hashContexts
|
||||||
|
this.push('{}'); // hashTypes
|
||||||
|
}
|
||||||
this.pushStackLiteral(omitEmpty ? 'undefined' : '{}');
|
this.pushStackLiteral(omitEmpty ? 'undefined' : '{}');
|
||||||
},
|
},
|
||||||
pushHash: function() {
|
pushHash: function() {
|
||||||
if (this.hash) {
|
if (this.hash) {
|
||||||
this.hashes.push(this.hash);
|
this.hashes.push(this.hash);
|
||||||
}
|
}
|
||||||
this.hash = { values: {} };
|
this.hash = { values: {}, types: [], contexts: [], ids: [] };
|
||||||
},
|
},
|
||||||
popHash: function() {
|
popHash: function() {
|
||||||
let hash = this.hash;
|
let hash = this.hash;
|
||||||
this.hash = this.hashes.pop();
|
this.hash = this.hashes.pop();
|
||||||
|
|
||||||
|
if (this.trackIds) {
|
||||||
|
this.push(this.objectLiteral(hash.ids));
|
||||||
|
}
|
||||||
|
if (this.stringParams) {
|
||||||
|
this.push(this.objectLiteral(hash.contexts));
|
||||||
|
this.push(this.objectLiteral(hash.types));
|
||||||
|
}
|
||||||
|
|
||||||
this.push(this.objectLiteral(hash.values));
|
this.push(this.objectLiteral(hash.values));
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -652,15 +686,24 @@ JavaScriptCompiler.prototype = {
|
|||||||
let foundDecorator = this.nameLookup('decorators', name, 'decorator'),
|
let foundDecorator = this.nameLookup('decorators', name, 'decorator'),
|
||||||
options = this.setupHelperArgs(name, paramSize);
|
options = this.setupHelperArgs(name, paramSize);
|
||||||
|
|
||||||
|
// Store the resolved decorator in a variable and verify it is a function before
|
||||||
|
// calling it. Without this, unregistered decorators can cause an unhandled TypeError
|
||||||
|
// (calling undefined), which crashes the process — enabling Denial of Service.
|
||||||
|
this.decorators.push(['var decorator = ', foundDecorator, ';']);
|
||||||
|
this.decorators.push([
|
||||||
|
'if (typeof decorator !== "function") { throw new Error(',
|
||||||
|
this.quotedString('Missing decorator: "' + name + '"'),
|
||||||
|
'); }'
|
||||||
|
]);
|
||||||
this.decorators.push([
|
this.decorators.push([
|
||||||
'fn = ',
|
'fn = ',
|
||||||
this.decorators.functionCall(foundDecorator, '', [
|
this.decorators.functionCall('decorator', '', [
|
||||||
'fn',
|
'fn',
|
||||||
'props',
|
'props',
|
||||||
'container',
|
'container',
|
||||||
options,
|
options
|
||||||
]),
|
]),
|
||||||
' || fn;',
|
' || fn;'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -694,7 +737,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
let functionLookupCode = [
|
let functionLookupCode = [
|
||||||
'(',
|
'(',
|
||||||
this.itemsSeparatedBy(possibleFunctionCalls, '||'),
|
this.itemsSeparatedBy(possibleFunctionCalls, '||'),
|
||||||
')',
|
')'
|
||||||
];
|
];
|
||||||
let functionCall = this.source.functionCall(
|
let functionCall = this.source.functionCall(
|
||||||
functionLookupCode,
|
functionLookupCode,
|
||||||
@@ -768,7 +811,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.aliasable('"function"'),
|
this.aliasable('"function"'),
|
||||||
' ? ',
|
' ? ',
|
||||||
this.source.functionCall('helper', 'call', helper.callParams),
|
this.source.functionCall('helper', 'call', helper.callParams),
|
||||||
' : helper))',
|
' : helper))'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -817,7 +860,49 @@ JavaScriptCompiler.prototype = {
|
|||||||
//
|
//
|
||||||
// Pops a value off the stack and assigns it to the current hash
|
// Pops a value off the stack and assigns it to the current hash
|
||||||
assignToHash: function(key) {
|
assignToHash: function(key) {
|
||||||
this.hash.values[key] = this.popStack();
|
let value = this.popStack(),
|
||||||
|
context,
|
||||||
|
type,
|
||||||
|
id;
|
||||||
|
|
||||||
|
if (this.trackIds) {
|
||||||
|
id = this.popStack();
|
||||||
|
}
|
||||||
|
if (this.stringParams) {
|
||||||
|
type = this.popStack();
|
||||||
|
context = this.popStack();
|
||||||
|
}
|
||||||
|
|
||||||
|
let hash = this.hash;
|
||||||
|
if (context) {
|
||||||
|
hash.contexts[key] = context;
|
||||||
|
}
|
||||||
|
if (type) {
|
||||||
|
hash.types[key] = type;
|
||||||
|
}
|
||||||
|
if (id) {
|
||||||
|
hash.ids[key] = id;
|
||||||
|
}
|
||||||
|
hash.values[key] = value;
|
||||||
|
},
|
||||||
|
|
||||||
|
pushId: function(type, name, child) {
|
||||||
|
if (type === 'BlockParam') {
|
||||||
|
this.pushStackLiteral(
|
||||||
|
'blockParams[' +
|
||||||
|
name[0] +
|
||||||
|
'].path[' +
|
||||||
|
name[1] +
|
||||||
|
']' +
|
||||||
|
(child ? ' + ' + JSON.stringify('.' + child) : '')
|
||||||
|
);
|
||||||
|
} else if (type === 'PathExpression') {
|
||||||
|
this.pushString(name);
|
||||||
|
} else if (type === 'SubExpression') {
|
||||||
|
this.pushStackLiteral('true');
|
||||||
|
} else {
|
||||||
|
this.pushStackLiteral('null');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// HELPERS
|
// HELPERS
|
||||||
@@ -831,13 +916,13 @@ JavaScriptCompiler.prototype = {
|
|||||||
|
|
||||||
for (let i = 0, l = children.length; i < l; i++) {
|
for (let i = 0, l = children.length; i < l; i++) {
|
||||||
child = children[i];
|
child = children[i];
|
||||||
compiler = new this.compiler();
|
compiler = new this.compiler(); // eslint-disable-line new-cap
|
||||||
|
|
||||||
let existing = this.matchExistingProgram(child);
|
let existing = this.matchExistingProgram(child);
|
||||||
|
|
||||||
if (existing == null) {
|
if (existing == null) {
|
||||||
this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
|
// Placeholder to prevent name conflicts for nested children
|
||||||
let index = this.context.programs.length;
|
let index = this.context.programs.push('') - 1;
|
||||||
child.index = index;
|
child.index = index;
|
||||||
child.name = 'program' + index;
|
child.name = 'program' + index;
|
||||||
this.context.programs[index] = compiler.compile(
|
this.context.programs[index] = compiler.compile(
|
||||||
@@ -927,7 +1012,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
createdStack,
|
createdStack,
|
||||||
usedLiteral;
|
usedLiteral;
|
||||||
|
|
||||||
/* v8 ignore next */
|
/* istanbul ignore next */
|
||||||
if (!this.isInline()) {
|
if (!this.isInline()) {
|
||||||
throw new Exception('replaceStack on non-inline');
|
throw new Exception('replaceStack on non-inline');
|
||||||
}
|
}
|
||||||
@@ -975,7 +1060,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
this.inlineStack = [];
|
this.inlineStack = [];
|
||||||
for (let i = 0, len = inlineStack.length; i < len; i++) {
|
for (let i = 0, len = inlineStack.length; i < len; i++) {
|
||||||
let entry = inlineStack[i];
|
let entry = inlineStack[i];
|
||||||
/* v8 ignore next */
|
/* istanbul ignore if */
|
||||||
if (entry instanceof Literal) {
|
if (entry instanceof Literal) {
|
||||||
this.compileStack.push(entry);
|
this.compileStack.push(entry);
|
||||||
} else {
|
} else {
|
||||||
@@ -997,7 +1082,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
return item.value;
|
return item.value;
|
||||||
} else {
|
} else {
|
||||||
if (!inline) {
|
if (!inline) {
|
||||||
/* v8 ignore next */
|
/* istanbul ignore next */
|
||||||
if (!this.stackSlot) {
|
if (!this.stackSlot) {
|
||||||
throw new Exception('Invalid stack pop');
|
throw new Exception('Invalid stack pop');
|
||||||
}
|
}
|
||||||
@@ -1011,7 +1096,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
let stack = this.isInline() ? this.inlineStack : this.compileStack,
|
let stack = this.isInline() ? this.inlineStack : this.compileStack,
|
||||||
item = stack[stack.length - 1];
|
item = stack[stack.length - 1];
|
||||||
|
|
||||||
/* v8 ignore next */
|
/* istanbul ignore if */
|
||||||
if (item instanceof Literal) {
|
if (item instanceof Literal) {
|
||||||
return item.value;
|
return item.value;
|
||||||
} else {
|
} else {
|
||||||
@@ -1063,12 +1148,15 @@ JavaScriptCompiler.prototype = {
|
|||||||
params: params,
|
params: params,
|
||||||
paramsInit: paramsInit,
|
paramsInit: paramsInit,
|
||||||
name: foundHelper,
|
name: foundHelper,
|
||||||
callParams: [callContext].concat(params),
|
callParams: [callContext].concat(params)
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
setupParams: function(helper, paramSize, params) {
|
setupParams: function(helper, paramSize, params) {
|
||||||
let options = {},
|
let options = {},
|
||||||
|
contexts = [],
|
||||||
|
types = [],
|
||||||
|
ids = [],
|
||||||
objectArgs = !params,
|
objectArgs = !params,
|
||||||
param;
|
param;
|
||||||
|
|
||||||
@@ -1079,6 +1167,14 @@ JavaScriptCompiler.prototype = {
|
|||||||
options.name = this.quotedString(helper);
|
options.name = this.quotedString(helper);
|
||||||
options.hash = this.popStack();
|
options.hash = this.popStack();
|
||||||
|
|
||||||
|
if (this.trackIds) {
|
||||||
|
options.hashIds = this.popStack();
|
||||||
|
}
|
||||||
|
if (this.stringParams) {
|
||||||
|
options.hashTypes = this.popStack();
|
||||||
|
options.hashContexts = this.popStack();
|
||||||
|
}
|
||||||
|
|
||||||
let inverse = this.popStack(),
|
let inverse = this.popStack(),
|
||||||
program = this.popStack();
|
program = this.popStack();
|
||||||
|
|
||||||
@@ -1095,12 +1191,28 @@ JavaScriptCompiler.prototype = {
|
|||||||
while (i--) {
|
while (i--) {
|
||||||
param = this.popStack();
|
param = this.popStack();
|
||||||
params[i] = param;
|
params[i] = param;
|
||||||
|
|
||||||
|
if (this.trackIds) {
|
||||||
|
ids[i] = this.popStack();
|
||||||
|
}
|
||||||
|
if (this.stringParams) {
|
||||||
|
types[i] = this.popStack();
|
||||||
|
contexts[i] = this.popStack();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (objectArgs) {
|
if (objectArgs) {
|
||||||
options.args = this.source.generateArray(params);
|
options.args = this.source.generateArray(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.trackIds) {
|
||||||
|
options.ids = this.source.generateArray(ids);
|
||||||
|
}
|
||||||
|
if (this.stringParams) {
|
||||||
|
options.types = this.source.generateArray(types);
|
||||||
|
options.contexts = this.source.generateArray(contexts);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.options.data) {
|
if (this.options.data) {
|
||||||
options.data = 'data';
|
options.data = 'data';
|
||||||
}
|
}
|
||||||
@@ -1124,7 +1236,7 @@ JavaScriptCompiler.prototype = {
|
|||||||
} else {
|
} else {
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -1175,24 +1287,7 @@ function strictLookup(requireTerminal, compiler, parts, startPartIndex, type) {
|
|||||||
stack = compiler.nameLookup(stack, parts[i], type);
|
stack = compiler.nameLookup(stack, parts[i], type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!requireTerminal) {
|
if (requireTerminal) {
|
||||||
return stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (startPartIndex > len) {
|
|
||||||
// Compat mode already consumed all parts via depthedLookup; the stack
|
|
||||||
// holds the resolved value, not an object to look the property up on.
|
|
||||||
// Use container.strictLookup to traverse depths with a strict error on miss.
|
|
||||||
return [
|
|
||||||
compiler.aliasable('container.strictLookup'),
|
|
||||||
'(depths, ',
|
|
||||||
compiler.quotedString(parts[len]),
|
|
||||||
', ',
|
|
||||||
JSON.stringify(compiler.source.currentLocation),
|
|
||||||
' )',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
compiler.aliasable('container.strict'),
|
compiler.aliasable('container.strict'),
|
||||||
'(',
|
'(',
|
||||||
@@ -1201,8 +1296,11 @@ function strictLookup(requireTerminal, compiler, parts, startPartIndex, type) {
|
|||||||
compiler.quotedString(parts[len]),
|
compiler.quotedString(parts[len]),
|
||||||
', ',
|
', ',
|
||||||
JSON.stringify(compiler.source.currentLocation),
|
JSON.stringify(compiler.source.currentLocation),
|
||||||
' )',
|
' )'
|
||||||
];
|
];
|
||||||
|
} else {
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default JavaScriptCompiler;
|
export default JavaScriptCompiler;
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
/* eslint-disable new-cap */
|
||||||
|
import Visitor from './visitor';
|
||||||
|
|
||||||
|
export function print(ast) {
|
||||||
|
return new PrintVisitor().accept(ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PrintVisitor() {
|
||||||
|
this.padding = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintVisitor.prototype = new Visitor();
|
||||||
|
|
||||||
|
PrintVisitor.prototype.pad = function(string) {
|
||||||
|
let out = '';
|
||||||
|
|
||||||
|
for (let i = 0, l = this.padding; i < l; i++) {
|
||||||
|
out += ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
out += string + '\n';
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.Program = function(program) {
|
||||||
|
let out = '',
|
||||||
|
body = program.body,
|
||||||
|
i,
|
||||||
|
l;
|
||||||
|
|
||||||
|
if (program.blockParams) {
|
||||||
|
let blockParams = 'BLOCK PARAMS: [';
|
||||||
|
for (i = 0, l = program.blockParams.length; i < l; i++) {
|
||||||
|
blockParams += ' ' + program.blockParams[i];
|
||||||
|
}
|
||||||
|
blockParams += ' ]';
|
||||||
|
out += this.pad(blockParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0, l = body.length; i < l; i++) {
|
||||||
|
out += this.accept(body[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.padding--;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.MustacheStatement = function(mustache) {
|
||||||
|
return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');
|
||||||
|
};
|
||||||
|
PrintVisitor.prototype.Decorator = function(mustache) {
|
||||||
|
return this.pad('{{ DIRECTIVE ' + this.SubExpression(mustache) + ' }}');
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.BlockStatement = PrintVisitor.prototype.DecoratorBlock = function(
|
||||||
|
block
|
||||||
|
) {
|
||||||
|
let out = '';
|
||||||
|
|
||||||
|
out += this.pad(
|
||||||
|
(block.type === 'DecoratorBlock' ? 'DIRECTIVE ' : '') + 'BLOCK:'
|
||||||
|
);
|
||||||
|
this.padding++;
|
||||||
|
out += this.pad(this.SubExpression(block));
|
||||||
|
if (block.program) {
|
||||||
|
out += this.pad('PROGRAM:');
|
||||||
|
this.padding++;
|
||||||
|
out += this.accept(block.program);
|
||||||
|
this.padding--;
|
||||||
|
}
|
||||||
|
if (block.inverse) {
|
||||||
|
if (block.program) {
|
||||||
|
this.padding++;
|
||||||
|
}
|
||||||
|
out += this.pad('{{^}}');
|
||||||
|
this.padding++;
|
||||||
|
out += this.accept(block.inverse);
|
||||||
|
this.padding--;
|
||||||
|
if (block.program) {
|
||||||
|
this.padding--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.padding--;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.PartialStatement = function(partial) {
|
||||||
|
let content = 'PARTIAL:' + partial.name.original;
|
||||||
|
if (partial.params[0]) {
|
||||||
|
content += ' ' + this.accept(partial.params[0]);
|
||||||
|
}
|
||||||
|
if (partial.hash) {
|
||||||
|
content += ' ' + this.accept(partial.hash);
|
||||||
|
}
|
||||||
|
return this.pad('{{> ' + content + ' }}');
|
||||||
|
};
|
||||||
|
PrintVisitor.prototype.PartialBlockStatement = function(partial) {
|
||||||
|
let content = 'PARTIAL BLOCK:' + partial.name.original;
|
||||||
|
if (partial.params[0]) {
|
||||||
|
content += ' ' + this.accept(partial.params[0]);
|
||||||
|
}
|
||||||
|
if (partial.hash) {
|
||||||
|
content += ' ' + this.accept(partial.hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
content += ' ' + this.pad('PROGRAM:');
|
||||||
|
this.padding++;
|
||||||
|
content += this.accept(partial.program);
|
||||||
|
this.padding--;
|
||||||
|
|
||||||
|
return this.pad('{{> ' + content + ' }}');
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.ContentStatement = function(content) {
|
||||||
|
return this.pad("CONTENT[ '" + content.value + "' ]");
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.CommentStatement = function(comment) {
|
||||||
|
return this.pad("{{! '" + comment.value + "' }}");
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.SubExpression = function(sexpr) {
|
||||||
|
let params = sexpr.params,
|
||||||
|
paramStrings = [],
|
||||||
|
hash;
|
||||||
|
|
||||||
|
for (let i = 0, l = params.length; i < l; i++) {
|
||||||
|
paramStrings.push(this.accept(params[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
params = '[' + paramStrings.join(', ') + ']';
|
||||||
|
|
||||||
|
hash = sexpr.hash ? ' ' + this.accept(sexpr.hash) : '';
|
||||||
|
|
||||||
|
return this.accept(sexpr.path) + ' ' + params + hash;
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.PathExpression = function(id) {
|
||||||
|
let path = id.parts.join('/');
|
||||||
|
return (id.data ? '@' : '') + 'PATH:' + path;
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.StringLiteral = function(string) {
|
||||||
|
return '"' + string.value + '"';
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.NumberLiteral = function(number) {
|
||||||
|
return 'NUMBER{' + number.value + '}';
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.BooleanLiteral = function(bool) {
|
||||||
|
return 'BOOLEAN{' + bool.value + '}';
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.UndefinedLiteral = function() {
|
||||||
|
return 'UNDEFINED';
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.NullLiteral = function() {
|
||||||
|
return 'NULL';
|
||||||
|
};
|
||||||
|
|
||||||
|
PrintVisitor.prototype.Hash = function(hash) {
|
||||||
|
let pairs = hash.pairs,
|
||||||
|
joinedPairs = [];
|
||||||
|
|
||||||
|
for (let i = 0, l = pairs.length; i < l; i++) {
|
||||||
|
joinedPairs.push(this.accept(pairs[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'HASH{' + joinedPairs.join(', ') + '}';
|
||||||
|
};
|
||||||
|
PrintVisitor.prototype.HashPair = function(pair) {
|
||||||
|
return pair.key + '=' + this.accept(pair.value);
|
||||||
|
};
|
||||||
|
/* eslint-enable new-cap */
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { isArray } from '../utils.js';
|
|
||||||
|
|
||||||
// Lightweight stub for browser environments where the source-map package
|
|
||||||
// (which depends on Node.js built-ins) is not available.
|
|
||||||
export function SourceNode(line, column, srcFile, chunks) {
|
|
||||||
this.src = '';
|
|
||||||
if (chunks) {
|
|
||||||
this.add(chunks);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SourceNode.prototype = {
|
|
||||||
add(chunks) {
|
|
||||||
if (isArray(chunks)) {
|
|
||||||
chunks = chunks.join('');
|
|
||||||
}
|
|
||||||
this.src += chunks;
|
|
||||||
},
|
|
||||||
prepend(chunks) {
|
|
||||||
if (isArray(chunks)) {
|
|
||||||
chunks = chunks.join('');
|
|
||||||
}
|
|
||||||
this.src = chunks + this.src;
|
|
||||||
},
|
|
||||||
toStringWithSourceMap() {
|
|
||||||
return { code: this.toString() };
|
|
||||||
},
|
|
||||||
toString() {
|
|
||||||
return this.src;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { SourceNode } from 'source-map';
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
import Exception from '../exception';
|
||||||
|
|
||||||
|
function Visitor() {
|
||||||
|
this.parents = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
Visitor.prototype = {
|
||||||
|
constructor: Visitor,
|
||||||
|
mutating: false,
|
||||||
|
|
||||||
|
// Visits a given value. If mutating, will replace the value if necessary.
|
||||||
|
acceptKey: function(node, name) {
|
||||||
|
let value = this.accept(node[name]);
|
||||||
|
if (this.mutating) {
|
||||||
|
// Hacky sanity check: This may have a few false positives for type for the helper
|
||||||
|
// methods but will generally do the right thing without a lot of overhead.
|
||||||
|
if (value && !Visitor.prototype[value.type]) {
|
||||||
|
throw new Exception(
|
||||||
|
'Unexpected node type "' +
|
||||||
|
value.type +
|
||||||
|
'" found when accepting ' +
|
||||||
|
name +
|
||||||
|
' on ' +
|
||||||
|
node.type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
node[name] = value;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Performs an accept operation with added sanity check to ensure
|
||||||
|
// required keys are not removed.
|
||||||
|
acceptRequired: function(node, name) {
|
||||||
|
this.acceptKey(node, name);
|
||||||
|
|
||||||
|
if (!node[name]) {
|
||||||
|
throw new Exception(node.type + ' requires ' + name);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Traverses a given array. If mutating, empty respnses will be removed
|
||||||
|
// for child elements.
|
||||||
|
acceptArray: function(array) {
|
||||||
|
for (let i = 0, l = array.length; i < l; i++) {
|
||||||
|
this.acceptKey(array, i);
|
||||||
|
|
||||||
|
if (!array[i]) {
|
||||||
|
array.splice(i, 1);
|
||||||
|
i--;
|
||||||
|
l--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
accept: function(object) {
|
||||||
|
if (!object) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore next: Sanity code */
|
||||||
|
if (!this[object.type]) {
|
||||||
|
throw new Exception('Unknown type: ' + object.type, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.current) {
|
||||||
|
this.parents.unshift(this.current);
|
||||||
|
}
|
||||||
|
this.current = object;
|
||||||
|
|
||||||
|
let ret = this[object.type](object);
|
||||||
|
|
||||||
|
this.current = this.parents.shift();
|
||||||
|
|
||||||
|
if (!this.mutating || ret) {
|
||||||
|
return ret;
|
||||||
|
} else if (ret !== false) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
Program: function(program) {
|
||||||
|
this.acceptArray(program.body);
|
||||||
|
},
|
||||||
|
|
||||||
|
MustacheStatement: visitSubExpression,
|
||||||
|
Decorator: visitSubExpression,
|
||||||
|
|
||||||
|
BlockStatement: visitBlock,
|
||||||
|
DecoratorBlock: visitBlock,
|
||||||
|
|
||||||
|
PartialStatement: visitPartial,
|
||||||
|
PartialBlockStatement: function(partial) {
|
||||||
|
visitPartial.call(this, partial);
|
||||||
|
|
||||||
|
this.acceptKey(partial, 'program');
|
||||||
|
},
|
||||||
|
|
||||||
|
ContentStatement: function(/* content */) {},
|
||||||
|
CommentStatement: function(/* comment */) {},
|
||||||
|
|
||||||
|
SubExpression: visitSubExpression,
|
||||||
|
|
||||||
|
PathExpression: function(/* path */) {},
|
||||||
|
|
||||||
|
StringLiteral: function(/* string */) {},
|
||||||
|
NumberLiteral: function(/* number */) {},
|
||||||
|
BooleanLiteral: function(/* bool */) {},
|
||||||
|
UndefinedLiteral: function(/* literal */) {},
|
||||||
|
NullLiteral: function(/* literal */) {},
|
||||||
|
|
||||||
|
Hash: function(hash) {
|
||||||
|
this.acceptArray(hash.pairs);
|
||||||
|
},
|
||||||
|
HashPair: function(pair) {
|
||||||
|
this.acceptRequired(pair, 'value');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function visitSubExpression(mustache) {
|
||||||
|
this.acceptRequired(mustache, 'path');
|
||||||
|
this.acceptArray(mustache.params);
|
||||||
|
this.acceptKey(mustache, 'hash');
|
||||||
|
}
|
||||||
|
function visitBlock(block) {
|
||||||
|
visitSubExpression.call(this, block);
|
||||||
|
|
||||||
|
this.acceptKey(block, 'program');
|
||||||
|
this.acceptKey(block, 'inverse');
|
||||||
|
}
|
||||||
|
function visitPartial(partial) {
|
||||||
|
this.acceptRequired(partial, 'name');
|
||||||
|
this.acceptArray(partial.params);
|
||||||
|
this.acceptKey(partial, 'hash');
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Visitor;
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
import Visitor from './visitor';
|
||||||
|
|
||||||
|
function WhitespaceControl(options = {}) {
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
WhitespaceControl.prototype = new Visitor();
|
||||||
|
|
||||||
|
WhitespaceControl.prototype.Program = function(program) {
|
||||||
|
const doStandalone = !this.options.ignoreStandalone;
|
||||||
|
|
||||||
|
let isRoot = !this.isRootSeen;
|
||||||
|
this.isRootSeen = true;
|
||||||
|
|
||||||
|
let body = program.body;
|
||||||
|
for (let i = 0, l = body.length; i < l; i++) {
|
||||||
|
let current = body[i],
|
||||||
|
strip = this.accept(current);
|
||||||
|
|
||||||
|
if (!strip) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let _isPrevWhitespace = isPrevWhitespace(body, i, isRoot),
|
||||||
|
_isNextWhitespace = isNextWhitespace(body, i, isRoot),
|
||||||
|
openStandalone = strip.openStandalone && _isPrevWhitespace,
|
||||||
|
closeStandalone = strip.closeStandalone && _isNextWhitespace,
|
||||||
|
inlineStandalone =
|
||||||
|
strip.inlineStandalone && _isPrevWhitespace && _isNextWhitespace;
|
||||||
|
|
||||||
|
if (strip.close) {
|
||||||
|
omitRight(body, i, true);
|
||||||
|
}
|
||||||
|
if (strip.open) {
|
||||||
|
omitLeft(body, i, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (doStandalone && inlineStandalone) {
|
||||||
|
omitRight(body, i);
|
||||||
|
|
||||||
|
if (omitLeft(body, i)) {
|
||||||
|
// If we are on a standalone node, save the indent info for partials
|
||||||
|
if (current.type === 'PartialStatement') {
|
||||||
|
// Pull out the whitespace from the final line
|
||||||
|
current.indent = /([ \t]+$)/.exec(body[i - 1].original)[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (doStandalone && openStandalone) {
|
||||||
|
omitRight((current.program || current.inverse).body);
|
||||||
|
|
||||||
|
// Strip out the previous content node if it's whitespace only
|
||||||
|
omitLeft(body, i);
|
||||||
|
}
|
||||||
|
if (doStandalone && closeStandalone) {
|
||||||
|
// Always strip the next node
|
||||||
|
omitRight(body, i);
|
||||||
|
|
||||||
|
omitLeft((current.inverse || current.program).body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return program;
|
||||||
|
};
|
||||||
|
|
||||||
|
WhitespaceControl.prototype.BlockStatement = WhitespaceControl.prototype.DecoratorBlock = WhitespaceControl.prototype.PartialBlockStatement = function(
|
||||||
|
block
|
||||||
|
) {
|
||||||
|
this.accept(block.program);
|
||||||
|
this.accept(block.inverse);
|
||||||
|
|
||||||
|
// Find the inverse program that is involed with whitespace stripping.
|
||||||
|
let program = block.program || block.inverse,
|
||||||
|
inverse = block.program && block.inverse,
|
||||||
|
firstInverse = inverse,
|
||||||
|
lastInverse = inverse;
|
||||||
|
|
||||||
|
if (inverse && inverse.chained) {
|
||||||
|
firstInverse = inverse.body[0].program;
|
||||||
|
|
||||||
|
// Walk the inverse chain to find the last inverse that is actually in the chain.
|
||||||
|
while (lastInverse.chained) {
|
||||||
|
lastInverse = lastInverse.body[lastInverse.body.length - 1].program;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let strip = {
|
||||||
|
open: block.openStrip.open,
|
||||||
|
close: block.closeStrip.close,
|
||||||
|
|
||||||
|
// Determine the standalone candiacy. Basically flag our content as being possibly standalone
|
||||||
|
// so our parent can determine if we actually are standalone
|
||||||
|
openStandalone: isNextWhitespace(program.body),
|
||||||
|
closeStandalone: isPrevWhitespace((firstInverse || program).body)
|
||||||
|
};
|
||||||
|
|
||||||
|
if (block.openStrip.close) {
|
||||||
|
omitRight(program.body, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inverse) {
|
||||||
|
let inverseStrip = block.inverseStrip;
|
||||||
|
|
||||||
|
if (inverseStrip.open) {
|
||||||
|
omitLeft(program.body, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inverseStrip.close) {
|
||||||
|
omitRight(firstInverse.body, null, true);
|
||||||
|
}
|
||||||
|
if (block.closeStrip.open) {
|
||||||
|
omitLeft(lastInverse.body, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find standalone else statments
|
||||||
|
if (
|
||||||
|
!this.options.ignoreStandalone &&
|
||||||
|
isPrevWhitespace(program.body) &&
|
||||||
|
isNextWhitespace(firstInverse.body)
|
||||||
|
) {
|
||||||
|
omitLeft(program.body);
|
||||||
|
omitRight(firstInverse.body);
|
||||||
|
}
|
||||||
|
} else if (block.closeStrip.open) {
|
||||||
|
omitLeft(program.body, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return strip;
|
||||||
|
};
|
||||||
|
|
||||||
|
WhitespaceControl.prototype.Decorator = WhitespaceControl.prototype.MustacheStatement = function(
|
||||||
|
mustache
|
||||||
|
) {
|
||||||
|
return mustache.strip;
|
||||||
|
};
|
||||||
|
|
||||||
|
WhitespaceControl.prototype.PartialStatement = WhitespaceControl.prototype.CommentStatement = function(
|
||||||
|
node
|
||||||
|
) {
|
||||||
|
/* istanbul ignore next */
|
||||||
|
let strip = node.strip || {};
|
||||||
|
return {
|
||||||
|
inlineStandalone: true,
|
||||||
|
open: strip.open,
|
||||||
|
close: strip.close
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function isPrevWhitespace(body, i, isRoot) {
|
||||||
|
if (i === undefined) {
|
||||||
|
i = body.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nodes that end with newlines are considered whitespace (but are special
|
||||||
|
// cased for strip operations)
|
||||||
|
let prev = body[i - 1],
|
||||||
|
sibling = body[i - 2];
|
||||||
|
if (!prev) {
|
||||||
|
return isRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prev.type === 'ContentStatement') {
|
||||||
|
return (sibling || !isRoot ? /\r?\n\s*?$/ : /(^|\r?\n)\s*?$/).test(
|
||||||
|
prev.original
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function isNextWhitespace(body, i, isRoot) {
|
||||||
|
if (i === undefined) {
|
||||||
|
i = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let next = body[i + 1],
|
||||||
|
sibling = body[i + 2];
|
||||||
|
if (!next) {
|
||||||
|
return isRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next.type === 'ContentStatement') {
|
||||||
|
return (sibling || !isRoot ? /^\s*?\r?\n/ : /^\s*?(\r?\n|$)/).test(
|
||||||
|
next.original
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marks the node to the right of the position as omitted.
|
||||||
|
// I.e. {{foo}}' ' will mark the ' ' node as omitted.
|
||||||
|
//
|
||||||
|
// If i is undefined, then the first child will be marked as such.
|
||||||
|
//
|
||||||
|
// If mulitple is truthy then all whitespace will be stripped out until non-whitespace
|
||||||
|
// content is met.
|
||||||
|
function omitRight(body, i, multiple) {
|
||||||
|
let current = body[i == null ? 0 : i + 1];
|
||||||
|
if (
|
||||||
|
!current ||
|
||||||
|
current.type !== 'ContentStatement' ||
|
||||||
|
(!multiple && current.rightStripped)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let original = current.value;
|
||||||
|
current.value = current.value.replace(
|
||||||
|
multiple ? /^\s+/ : /^[ \t]*\r?\n?/,
|
||||||
|
''
|
||||||
|
);
|
||||||
|
current.rightStripped = current.value !== original;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marks the node to the left of the position as omitted.
|
||||||
|
// I.e. ' '{{foo}} will mark the ' ' node as omitted.
|
||||||
|
//
|
||||||
|
// If i is undefined then the last child will be marked as such.
|
||||||
|
//
|
||||||
|
// If mulitple is truthy then all whitespace will be stripped out until non-whitespace
|
||||||
|
// content is met.
|
||||||
|
function omitLeft(body, i, multiple) {
|
||||||
|
let current = body[i == null ? body.length - 1 : i - 1];
|
||||||
|
if (
|
||||||
|
!current ||
|
||||||
|
current.type !== 'ContentStatement' ||
|
||||||
|
(!multiple && current.leftStripped)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We omit the last node if it's whitespace only and not preceded by a non-content node.
|
||||||
|
let original = current.value;
|
||||||
|
current.value = current.value.replace(multiple ? /\s+$/ : /[ \t]+$/, '');
|
||||||
|
current.leftStripped = current.value !== original;
|
||||||
|
return current.leftStripped;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default WhitespaceControl;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import registerInline from './decorators/inline.js';
|
import registerInline from './decorators/inline';
|
||||||
|
|
||||||
export function registerDefaultDecorators(instance) {
|
export function registerDefaultDecorators(instance) {
|
||||||
registerInline(instance);
|
registerInline(instance);
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import { extend } from '../utils.js';
|
import { extend } from '../utils';
|
||||||
|
|
||||||
export default function(instance) {
|
export default function(instance) {
|
||||||
instance.registerDecorator(
|
instance.registerDecorator('inline', function(fn, props, container, options) {
|
||||||
'inline',
|
|
||||||
function (fn, props, container, options) {
|
|
||||||
let ret = fn;
|
let ret = fn;
|
||||||
if (!props.partials) {
|
if (!props.partials) {
|
||||||
props.partials = {};
|
props.partials = {};
|
||||||
@@ -20,6 +18,5 @@ export default function (instance) {
|
|||||||
props.partials[options.args[0]] = options.fn;
|
props.partials[options.args[0]] = options.fn;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
const errorProps = [
|
||||||
|
'description',
|
||||||
|
'fileName',
|
||||||
|
'lineNumber',
|
||||||
|
'endLineNumber',
|
||||||
|
'message',
|
||||||
|
'name',
|
||||||
|
'number',
|
||||||
|
'stack'
|
||||||
|
];
|
||||||
|
|
||||||
|
function Exception(message, node) {
|
||||||
|
let loc = node && node.loc,
|
||||||
|
line,
|
||||||
|
endLineNumber,
|
||||||
|
column,
|
||||||
|
endColumn;
|
||||||
|
|
||||||
|
if (loc) {
|
||||||
|
line = loc.start.line;
|
||||||
|
endLineNumber = loc.end.line;
|
||||||
|
column = loc.start.column;
|
||||||
|
endColumn = loc.end.column;
|
||||||
|
|
||||||
|
message += ' - ' + line + ':' + column;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tmp = Error.prototype.constructor.call(this, message);
|
||||||
|
|
||||||
|
// Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
|
||||||
|
for (let idx = 0; idx < errorProps.length; idx++) {
|
||||||
|
this[errorProps[idx]] = tmp[errorProps[idx]];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore else */
|
||||||
|
if (Error.captureStackTrace) {
|
||||||
|
Error.captureStackTrace(this, Exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (loc) {
|
||||||
|
this.lineNumber = line;
|
||||||
|
this.endLineNumber = endLineNumber;
|
||||||
|
|
||||||
|
// Work around issue under safari where we can't directly set the column value
|
||||||
|
/* istanbul ignore next */
|
||||||
|
if (Object.defineProperty) {
|
||||||
|
Object.defineProperty(this, 'column', {
|
||||||
|
value: column,
|
||||||
|
enumerable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(this, 'endColumn', {
|
||||||
|
value: endColumn,
|
||||||
|
enumerable: true
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.column = column;
|
||||||
|
this.endColumn = endColumn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (nop) {
|
||||||
|
/* Ignore if the browser is very particular */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception.prototype = new Error();
|
||||||
|
|
||||||
|
export default Exception;
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import registerBlockHelperMissing from './helpers/block-helper-missing.js';
|
import registerBlockHelperMissing from './helpers/block-helper-missing';
|
||||||
import registerEach from './helpers/each.js';
|
import registerEach from './helpers/each';
|
||||||
import registerHelperMissing from './helpers/helper-missing.js';
|
import registerHelperMissing from './helpers/helper-missing';
|
||||||
import registerIf from './helpers/if.js';
|
import registerIf from './helpers/if';
|
||||||
import registerLog from './helpers/log.js';
|
import registerLog from './helpers/log';
|
||||||
import registerLookup from './helpers/lookup.js';
|
import registerLookup from './helpers/lookup';
|
||||||
import registerWith from './helpers/with.js';
|
import registerWith from './helpers/with';
|
||||||
|
|
||||||
export function registerDefaultHelpers(instance) {
|
export function registerDefaultHelpers(instance) {
|
||||||
registerBlockHelperMissing(instance);
|
registerBlockHelperMissing(instance);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { isArray } from '../utils.js';
|
import { appendContextPath, createFrame, isArray } from '../utils';
|
||||||
|
|
||||||
export default function(instance) {
|
export default function(instance) {
|
||||||
instance.registerHelper('blockHelperMissing', function(context, options) {
|
instance.registerHelper('blockHelperMissing', function(context, options) {
|
||||||
@@ -11,11 +11,24 @@ export default function (instance) {
|
|||||||
return inverse(this);
|
return inverse(this);
|
||||||
} else if (isArray(context)) {
|
} else if (isArray(context)) {
|
||||||
if (context.length > 0) {
|
if (context.length > 0) {
|
||||||
|
if (options.ids) {
|
||||||
|
options.ids = [options.name];
|
||||||
|
}
|
||||||
|
|
||||||
return instance.helpers.each(context, options);
|
return instance.helpers.each(context, options);
|
||||||
} else {
|
} else {
|
||||||
return inverse(this);
|
return inverse(this);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (options.data && options.ids) {
|
||||||
|
let data = createFrame(options.data);
|
||||||
|
data.contextPath = appendContextPath(
|
||||||
|
options.data.contextPath,
|
||||||
|
options.name
|
||||||
|
);
|
||||||
|
options = { data: data };
|
||||||
|
}
|
||||||
|
|
||||||
return fn(context, options);
|
return fn(context, options);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import {
|
||||||
import { createFrame, isArray, isFunction, isMap, isSet } from '../utils.js';
|
appendContextPath,
|
||||||
|
blockParams,
|
||||||
|
createFrame,
|
||||||
|
isArray,
|
||||||
|
isFunction
|
||||||
|
} from '../utils';
|
||||||
|
import Exception from '../exception';
|
||||||
|
|
||||||
export default function(instance) {
|
export default function(instance) {
|
||||||
instance.registerHelper('each', function(context, options) {
|
instance.registerHelper('each', function(context, options) {
|
||||||
@@ -11,7 +17,13 @@ export default function (instance) {
|
|||||||
inverse = options.inverse,
|
inverse = options.inverse,
|
||||||
i = 0,
|
i = 0,
|
||||||
ret = '',
|
ret = '',
|
||||||
data;
|
data,
|
||||||
|
contextPath;
|
||||||
|
|
||||||
|
if (options.data && options.ids) {
|
||||||
|
contextPath =
|
||||||
|
appendContextPath(options.data.contextPath, options.ids[0]) + '.';
|
||||||
|
}
|
||||||
|
|
||||||
if (isFunction(context)) {
|
if (isFunction(context)) {
|
||||||
context = context.call(this);
|
context = context.call(this);
|
||||||
@@ -21,19 +33,26 @@ export default function (instance) {
|
|||||||
data = createFrame(options.data);
|
data = createFrame(options.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function execIteration(field, value, index, last) {
|
function execIteration(field, index, last) {
|
||||||
if (data) {
|
if (data) {
|
||||||
data.key = field;
|
data.key = field;
|
||||||
data.index = index;
|
data.index = index;
|
||||||
data.first = index === 0;
|
data.first = index === 0;
|
||||||
data.last = !!last;
|
data.last = !!last;
|
||||||
|
|
||||||
|
if (contextPath) {
|
||||||
|
data.contextPath = contextPath + field;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret =
|
ret =
|
||||||
ret +
|
ret +
|
||||||
fn(value, {
|
fn(context[field], {
|
||||||
data: data,
|
data: data,
|
||||||
blockParams: [context[field], field],
|
blockParams: blockParams(
|
||||||
|
[context[field], field],
|
||||||
|
[contextPath + field, null]
|
||||||
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,19 +60,9 @@ export default function (instance) {
|
|||||||
if (isArray(context)) {
|
if (isArray(context)) {
|
||||||
for (let j = context.length; i < j; i++) {
|
for (let j = context.length; i < j; i++) {
|
||||||
if (i in context) {
|
if (i in context) {
|
||||||
execIteration(i, context[i], i, i === context.length - 1);
|
execIteration(i, i, i === context.length - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isMap(context)) {
|
|
||||||
const j = context.size;
|
|
||||||
for (const [key, value] of context) {
|
|
||||||
execIteration(key, value, i++, i === j);
|
|
||||||
}
|
|
||||||
} else if (isSet(context)) {
|
|
||||||
const j = context.size;
|
|
||||||
for (const value of context) {
|
|
||||||
execIteration(i, value, i++, i === j);
|
|
||||||
}
|
|
||||||
} else if (typeof Symbol === 'function' && context[Symbol.iterator]) {
|
} else if (typeof Symbol === 'function' && context[Symbol.iterator]) {
|
||||||
const newContext = [];
|
const newContext = [];
|
||||||
const iterator = context[Symbol.iterator]();
|
const iterator = context[Symbol.iterator]();
|
||||||
@@ -62,23 +71,23 @@ export default function (instance) {
|
|||||||
}
|
}
|
||||||
context = newContext;
|
context = newContext;
|
||||||
for (let j = context.length; i < j; i++) {
|
for (let j = context.length; i < j; i++) {
|
||||||
execIteration(i, context[i], i, i === context.length - 1);
|
execIteration(i, i, i === context.length - 1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let priorKey;
|
let priorKey;
|
||||||
|
|
||||||
Object.keys(context).forEach((key) => {
|
Object.keys(context).forEach(key => {
|
||||||
// We're running the iterations one step out of sync so we can detect
|
// We're running the iterations one step out of sync so we can detect
|
||||||
// the last iteration without have to scan the object twice and create
|
// the last iteration without have to scan the object twice and create
|
||||||
// an intermediate keys array.
|
// an itermediate keys array.
|
||||||
if (priorKey !== undefined) {
|
if (priorKey !== undefined) {
|
||||||
execIteration(priorKey, context[priorKey], i - 1);
|
execIteration(priorKey, i - 1);
|
||||||
}
|
}
|
||||||
priorKey = key;
|
priorKey = key;
|
||||||
i++;
|
i++;
|
||||||
});
|
});
|
||||||
if (priorKey !== undefined) {
|
if (priorKey !== undefined) {
|
||||||
execIteration(priorKey, context[priorKey], i - 1, true);
|
execIteration(priorKey, i - 1, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import Exception from '../exception';
|
||||||
|
|
||||||
export default function(instance) {
|
export default function(instance) {
|
||||||
instance.registerHelper('helperMissing', function(/* [args, ]options */) {
|
instance.registerHelper('helperMissing', function(/* [args, ]options */) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import { isEmpty, isFunction } from '../utils';
|
||||||
import { isEmpty, isFunction } from '../utils.js';
|
import Exception from '../exception';
|
||||||
|
|
||||||
export default function(instance) {
|
export default function(instance) {
|
||||||
instance.registerHelper('if', function(conditional, options) {
|
instance.registerHelper('if', function(conditional, options) {
|
||||||
@@ -11,7 +11,7 @@ export default function (instance) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Default behavior is to render the positive path if the value is truthy and not empty.
|
// Default behavior is to render the positive path if the value is truthy and not empty.
|
||||||
// The `includeZero` option may be set to treat the conditional as purely not empty based on the
|
// The `includeZero` option may be set to treat the condtional as purely not empty based on the
|
||||||
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
|
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
|
||||||
if ((!options.hash.includeZero && !conditional) || isEmpty(conditional)) {
|
if ((!options.hash.includeZero && !conditional) || isEmpty(conditional)) {
|
||||||
return options.inverse(this);
|
return options.inverse(this);
|
||||||
@@ -27,7 +27,7 @@ export default function (instance) {
|
|||||||
return instance.helpers['if'].call(this, conditional, {
|
return instance.helpers['if'].call(this, conditional, {
|
||||||
fn: options.inverse,
|
fn: options.inverse,
|
||||||
inverse: options.fn,
|
inverse: options.fn,
|
||||||
hash: options.hash,
|
hash: options.hash
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import {
|
||||||
import { isEmpty, isFunction } from '../utils.js';
|
appendContextPath,
|
||||||
|
blockParams,
|
||||||
|
createFrame,
|
||||||
|
isEmpty,
|
||||||
|
isFunction
|
||||||
|
} from '../utils';
|
||||||
|
import Exception from '../exception';
|
||||||
|
|
||||||
export default function(instance) {
|
export default function(instance) {
|
||||||
instance.registerHelper('with', function(context, options) {
|
instance.registerHelper('with', function(context, options) {
|
||||||
@@ -14,10 +20,17 @@ export default function (instance) {
|
|||||||
|
|
||||||
if (!isEmpty(context)) {
|
if (!isEmpty(context)) {
|
||||||
let data = options.data;
|
let data = options.data;
|
||||||
|
if (options.data && options.ids) {
|
||||||
|
data = createFrame(options.data);
|
||||||
|
data.contextPath = appendContextPath(
|
||||||
|
options.data.contextPath,
|
||||||
|
options.ids[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return fn(context, {
|
return fn(context, {
|
||||||
data: data,
|
data: data,
|
||||||
blockParams: [context],
|
blockParams: blockParams([context], [data && data.contextPath])
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return options.inverse(this);
|
return options.inverse(this);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { extend } from '../utils.js';
|
import { extend } from '../utils';
|
||||||
import logger from '../logger.js';
|
import logger from '../logger';
|
||||||
|
|
||||||
const loggedProperties = Object.create(null);
|
const loggedProperties = Object.create(null);
|
||||||
|
|
||||||
@@ -16,17 +16,18 @@ export function createProtoAccessControl(runtimeOptions) {
|
|||||||
methodWhiteList['__defineGetter__'] = false;
|
methodWhiteList['__defineGetter__'] = false;
|
||||||
methodWhiteList['__defineSetter__'] = false;
|
methodWhiteList['__defineSetter__'] = false;
|
||||||
methodWhiteList['__lookupGetter__'] = false;
|
methodWhiteList['__lookupGetter__'] = false;
|
||||||
|
methodWhiteList['__lookupSetter__'] = false;
|
||||||
extend(methodWhiteList, runtimeOptions.allowedProtoMethods);
|
extend(methodWhiteList, runtimeOptions.allowedProtoMethods);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
properties: {
|
properties: {
|
||||||
whitelist: propertyWhiteList,
|
whitelist: propertyWhiteList,
|
||||||
defaultValue: runtimeOptions.allowProtoPropertiesByDefault,
|
defaultValue: runtimeOptions.allowProtoPropertiesByDefault
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
whitelist: methodWhiteList,
|
whitelist: methodWhiteList,
|
||||||
defaultValue: runtimeOptions.allowProtoMethodsByDefault,
|
defaultValue: runtimeOptions.allowProtoMethodsByDefault
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,11 +46,11 @@ function checkWhiteList(protoAccessControlForType, propertyName) {
|
|||||||
if (protoAccessControlForType.defaultValue !== undefined) {
|
if (protoAccessControlForType.defaultValue !== undefined) {
|
||||||
return protoAccessControlForType.defaultValue;
|
return protoAccessControlForType.defaultValue;
|
||||||
}
|
}
|
||||||
logUnexpectedPropertyAccessOnce(propertyName);
|
logUnexpecedPropertyAccessOnce(propertyName);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function logUnexpectedPropertyAccessOnce(propertyName) {
|
function logUnexpecedPropertyAccessOnce(propertyName) {
|
||||||
if (loggedProperties[propertyName] !== true) {
|
if (loggedProperties[propertyName] !== true) {
|
||||||
loggedProperties[propertyName] = true;
|
loggedProperties[propertyName] = true;
|
||||||
logger.log(
|
logger.log(
|
||||||
@@ -62,7 +63,7 @@ function logUnexpectedPropertyAccessOnce(propertyName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function resetLoggedProperties() {
|
export function resetLoggedProperties() {
|
||||||
Object.keys(loggedProperties).forEach((propertyName) => {
|
Object.keys(loggedProperties).forEach(propertyName => {
|
||||||
delete loggedProperties[propertyName];
|
delete loggedProperties[propertyName];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
export function wrapHelper(helper, transformOptionsFn) {
|
export function wrapHelper(helper, transformOptionsFn) {
|
||||||
if (typeof helper !== 'function') {
|
if (typeof helper !== 'function') {
|
||||||
// This should not happen, but apparently it does in https://github.com/handlebars-lang/handlebars.js/issues/1639
|
// This should not happen, but apparently it does in https://github.com/wycats/handlebars.js/issues/1639
|
||||||
// We try to make the wrapper least-invasive by not wrapping it, if the helper is not a function.
|
// We try to make the wrapper least-invasive by not wrapping it, if the helper is not a function.
|
||||||
return helper;
|
return helper;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { indexOf } from './utils.js';
|
import { indexOf } from './utils';
|
||||||
|
|
||||||
let logger = {
|
let logger = {
|
||||||
methodMap: ['debug', 'info', 'warn', 'error'],
|
methodMap: ['debug', 'info', 'warn', 'error'],
|
||||||
@@ -33,7 +33,7 @@ let logger = {
|
|||||||
}
|
}
|
||||||
console[method](...message); // eslint-disable-line no-console
|
console[method](...message); // eslint-disable-line no-console
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default logger;
|
export default logger;
|
||||||
|
|||||||
@@ -1,7 +1,19 @@
|
|||||||
|
/* global globalThis */
|
||||||
export default function(Handlebars) {
|
export default function(Handlebars) {
|
||||||
let $Handlebars = globalThis.Handlebars;
|
/* istanbul ignore next */
|
||||||
|
// https://mathiasbynens.be/notes/globalthis
|
||||||
|
(function() {
|
||||||
|
if (typeof globalThis === 'object') return;
|
||||||
|
Object.prototype.__defineGetter__('__magic__', function() {
|
||||||
|
return this;
|
||||||
|
});
|
||||||
|
__magic__.globalThis = __magic__; // eslint-disable-line no-undef
|
||||||
|
delete Object.prototype.__magic__;
|
||||||
|
})();
|
||||||
|
|
||||||
/* v8 ignore next */
|
const $Handlebars = globalThis.Handlebars;
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
Handlebars.noConflict = function() {
|
Handlebars.noConflict = function() {
|
||||||
if (globalThis.Handlebars === Handlebars) {
|
if (globalThis.Handlebars === Handlebars) {
|
||||||
globalThis.Handlebars = $Handlebars;
|
globalThis.Handlebars = $Handlebars;
|
||||||
|
|||||||
+51
-43
@@ -1,17 +1,17 @@
|
|||||||
import { Exception } from '@handlebars/parser';
|
import * as Utils from './utils';
|
||||||
import * as Utils from './utils.js';
|
import Exception from './exception';
|
||||||
import {
|
import {
|
||||||
COMPILER_REVISION,
|
COMPILER_REVISION,
|
||||||
createFrame,
|
createFrame,
|
||||||
LAST_COMPATIBLE_COMPILER_REVISION,
|
LAST_COMPATIBLE_COMPILER_REVISION,
|
||||||
REVISION_CHANGES,
|
REVISION_CHANGES
|
||||||
} from './base.js';
|
} from './base';
|
||||||
import { moveHelperToHooks } from './helpers.js';
|
import { moveHelperToHooks } from './helpers';
|
||||||
import { wrapHelper } from './internal/wrapHelper.js';
|
import { wrapHelper } from './internal/wrapHelper';
|
||||||
import {
|
import {
|
||||||
createProtoAccessControl,
|
createProtoAccessControl,
|
||||||
resultIsAllowed,
|
resultIsAllowed
|
||||||
} from './internal/proto-access.js';
|
} from './internal/proto-access';
|
||||||
|
|
||||||
export function checkRevision(compilerInfo) {
|
export function checkRevision(compilerInfo) {
|
||||||
const compilerRevision = (compilerInfo && compilerInfo[0]) || 1,
|
const compilerRevision = (compilerInfo && compilerInfo[0]) || 1,
|
||||||
@@ -47,7 +47,7 @@ export function checkRevision(compilerInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function template(templateSpec, env) {
|
export function template(templateSpec, env) {
|
||||||
/* v8 ignore next */
|
/* istanbul ignore next */
|
||||||
if (!env) {
|
if (!env) {
|
||||||
throw new Exception('No environment passed to template');
|
throw new Exception('No environment passed to template');
|
||||||
}
|
}
|
||||||
@@ -68,6 +68,9 @@ export function template(templateSpec, env) {
|
|||||||
function invokePartialWrapper(partial, context, options) {
|
function invokePartialWrapper(partial, context, options) {
|
||||||
if (options.hash) {
|
if (options.hash) {
|
||||||
context = Utils.extend({}, context, options.hash);
|
context = Utils.extend({}, context, options.hash);
|
||||||
|
if (options.ids) {
|
||||||
|
options.ids[0] = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
partial = env.VM.resolvePartial.call(this, partial, context, options);
|
partial = env.VM.resolvePartial.call(this, partial, context, options);
|
||||||
|
|
||||||
@@ -111,32 +114,12 @@ export function template(templateSpec, env) {
|
|||||||
strict: function(obj, name, loc) {
|
strict: function(obj, name, loc) {
|
||||||
if (!obj || !(name in obj)) {
|
if (!obj || !(name in obj)) {
|
||||||
throw new Exception('"' + name + '" not defined in ' + obj, {
|
throw new Exception('"' + name + '" not defined in ' + obj, {
|
||||||
loc: loc,
|
loc: loc
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return container.lookupProperty(obj, name);
|
return container.lookupProperty(obj, name);
|
||||||
},
|
},
|
||||||
strictLookup: function (depths, name, loc) {
|
|
||||||
const len = depths.length;
|
|
||||||
let depth;
|
|
||||||
for (let i = 0; i < len; i++) {
|
|
||||||
const d = depths[i];
|
|
||||||
if (
|
|
||||||
d &&
|
|
||||||
(typeof d === 'object' || typeof d === 'function') &&
|
|
||||||
name in d
|
|
||||||
) {
|
|
||||||
depth = d;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return container.strict(depth, name, loc);
|
|
||||||
},
|
|
||||||
lookupProperty: function(parent, propertyName) {
|
lookupProperty: function(parent, propertyName) {
|
||||||
if (Utils.isMap(parent)) {
|
|
||||||
return parent.get(propertyName);
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = parent[propertyName];
|
let result = parent[propertyName];
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
return result;
|
return result;
|
||||||
@@ -155,7 +138,7 @@ export function template(templateSpec, env) {
|
|||||||
for (let i = 0; i < len; i++) {
|
for (let i = 0; i < len; i++) {
|
||||||
let result = depths[i] && container.lookupProperty(depths[i], name);
|
let result = depths[i] && container.lookupProperty(depths[i], name);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return depths[i][name];
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -211,13 +194,13 @@ export function template(templateSpec, env) {
|
|||||||
nullContext: Object.seal({}),
|
nullContext: Object.seal({}),
|
||||||
|
|
||||||
noop: env.VM.noop,
|
noop: env.VM.noop,
|
||||||
compilerInfo: templateSpec.compiler,
|
compilerInfo: templateSpec.compiler
|
||||||
};
|
};
|
||||||
|
|
||||||
function ret(context, options = {}) {
|
function ret(context, options = {}) {
|
||||||
let data = options.data;
|
let data = options.data;
|
||||||
|
|
||||||
_setup(options);
|
ret._setup(options);
|
||||||
if (!options.partial && templateSpec.useData) {
|
if (!options.partial && templateSpec.useData) {
|
||||||
data = initData(context, data);
|
data = initData(context, data);
|
||||||
}
|
}
|
||||||
@@ -262,7 +245,7 @@ export function template(templateSpec, env) {
|
|||||||
|
|
||||||
ret.isTop = true;
|
ret.isTop = true;
|
||||||
|
|
||||||
function _setup(options) {
|
ret._setup = function(options) {
|
||||||
if (!options.partial) {
|
if (!options.partial) {
|
||||||
let mergedHelpers = {};
|
let mergedHelpers = {};
|
||||||
addHelpers(mergedHelpers, env.helpers, container);
|
addHelpers(mergedHelpers, env.helpers, container);
|
||||||
@@ -299,8 +282,26 @@ export function template(templateSpec, env) {
|
|||||||
container.decorators = options.decorators;
|
container.decorators = options.decorators;
|
||||||
container.hooks = options.hooks;
|
container.hooks = options.hooks;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ret._child = function(i, data, blockParams, depths) {
|
||||||
|
if (templateSpec.useBlockParams && !blockParams) {
|
||||||
|
throw new Exception('must pass block params');
|
||||||
|
}
|
||||||
|
if (templateSpec.useDepths && !depths) {
|
||||||
|
throw new Exception('must pass parent depths');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return wrapProgram(
|
||||||
|
container,
|
||||||
|
i,
|
||||||
|
templateSpec[i],
|
||||||
|
data,
|
||||||
|
0,
|
||||||
|
blockParams,
|
||||||
|
depths
|
||||||
|
);
|
||||||
|
};
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,22 +349,25 @@ export function wrapProgram(
|
|||||||
export function resolvePartial(partial, context, options) {
|
export function resolvePartial(partial, context, options) {
|
||||||
if (!partial) {
|
if (!partial) {
|
||||||
if (options.name === '@partial-block') {
|
if (options.name === '@partial-block') {
|
||||||
partial = options.data['partial-block'];
|
partial = lookupOwnProperty(options.data, 'partial-block');
|
||||||
} else {
|
} else {
|
||||||
partial = options.partials[options.name];
|
partial = lookupOwnProperty(options.partials, options.name);
|
||||||
}
|
}
|
||||||
} else if (!partial.call && !options.name) {
|
} else if (!partial.call && !options.name) {
|
||||||
// This is a dynamic partial that returned a string
|
// This is a dynamic partial that returned a string
|
||||||
options.name = partial;
|
options.name = partial;
|
||||||
partial = options.partials[partial];
|
partial = lookupOwnProperty(options.partials, partial);
|
||||||
}
|
}
|
||||||
return partial;
|
return partial;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function invokePartial(partial, context, options) {
|
export function invokePartial(partial, context, options) {
|
||||||
// Use the current closure context to save the partial-block if this partial
|
// Use the current closure context to save the partial-block if this partial
|
||||||
const currentPartialBlock = options.data && options.data['partial-block'];
|
const currentPartialBlock = lookupOwnProperty(options.data, 'partial-block');
|
||||||
options.partial = true;
|
options.partial = true;
|
||||||
|
if (options.ids) {
|
||||||
|
options.data.contextPath = options.ids[0] || options.data.contextPath;
|
||||||
|
}
|
||||||
|
|
||||||
let partialBlock;
|
let partialBlock;
|
||||||
if (options.fn && options.fn !== noop) {
|
if (options.fn && options.fn !== noop) {
|
||||||
@@ -390,9 +394,7 @@ export function invokePartial(partial, context, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (partial === undefined) {
|
if (partial === undefined) {
|
||||||
throw new Exception(
|
throw new Exception('The partial ' + options.name + ' could not be found');
|
||||||
'The partial "' + options.name + '" could not be found'
|
|
||||||
);
|
|
||||||
} else if (partial instanceof Function) {
|
} else if (partial instanceof Function) {
|
||||||
return partial(context, options);
|
return partial(context, options);
|
||||||
}
|
}
|
||||||
@@ -402,6 +404,12 @@ export function noop() {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function lookupOwnProperty(obj, name) {
|
||||||
|
if (obj && Object.prototype.hasOwnProperty.call(obj, name)) {
|
||||||
|
return obj[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function initData(context, data) {
|
function initData(context, data) {
|
||||||
if (!data || !('root' in data)) {
|
if (!data || !('root' in data)) {
|
||||||
data = data ? createFrame(data) : {};
|
data = data ? createFrame(data) : {};
|
||||||
@@ -429,7 +437,7 @@ function executeDecorators(fn, prog, container, depths, data, blockParams) {
|
|||||||
|
|
||||||
function addHelpers(mergedHelpers, helpers, container) {
|
function addHelpers(mergedHelpers, helpers, container) {
|
||||||
if (!helpers) return;
|
if (!helpers) return;
|
||||||
Object.keys(helpers).forEach((helperName) => {
|
Object.keys(helpers).forEach(helperName => {
|
||||||
let helper = helpers[helperName];
|
let helper = helpers[helperName];
|
||||||
mergedHelpers[helperName] = passLookupPropertyOption(helper, container);
|
mergedHelpers[helperName] = passLookupPropertyOption(helper, container);
|
||||||
});
|
});
|
||||||
@@ -437,7 +445,7 @@ function addHelpers(mergedHelpers, helpers, container) {
|
|||||||
|
|
||||||
function passLookupPropertyOption(helper, container) {
|
function passLookupPropertyOption(helper, container) {
|
||||||
const lookupProperty = container.lookupProperty;
|
const lookupProperty = container.lookupProperty;
|
||||||
return wrapHelper(helper, (options) => {
|
return wrapHelper(helper, options => {
|
||||||
options.lookupProperty = lookupProperty;
|
options.lookupProperty = lookupProperty;
|
||||||
return options;
|
return options;
|
||||||
});
|
});
|
||||||
|
|||||||
+59
-14
@@ -5,7 +5,7 @@ const escape = {
|
|||||||
'"': '"',
|
'"': '"',
|
||||||
"'": ''',
|
"'": ''',
|
||||||
'`': '`',
|
'`': '`',
|
||||||
'=': '=',
|
'=': '='
|
||||||
};
|
};
|
||||||
|
|
||||||
const badChars = /[&<>"'`=]/g,
|
const badChars = /[&<>"'`=]/g,
|
||||||
@@ -30,23 +30,32 @@ export function extend(obj /* , ...source */) {
|
|||||||
export let toString = Object.prototype.toString;
|
export let toString = Object.prototype.toString;
|
||||||
|
|
||||||
// Sourced from lodash
|
// Sourced from lodash
|
||||||
// https://github.com/lodash/lodash/blob/4.17.21/LICENSE
|
// https://github.com/bestiejs/lodash/blob/master/LICENSE.txt
|
||||||
export function isFunction(value) {
|
/* eslint-disable func-style */
|
||||||
|
let isFunction = function(value) {
|
||||||
return typeof value === 'function';
|
return typeof value === 'function';
|
||||||
}
|
};
|
||||||
|
// fallback for older versions of Chrome and Safari
|
||||||
function testTag(name) {
|
/* istanbul ignore next */
|
||||||
const tag = '[object ' + name + ']';
|
if (isFunction(/x/)) {
|
||||||
return function (value) {
|
isFunction = function(value) {
|
||||||
return value && typeof value === 'object'
|
return (
|
||||||
? toString.call(value) === tag
|
typeof value === 'function' &&
|
||||||
: false;
|
toString.call(value) === '[object Function]'
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
export { isFunction };
|
||||||
|
/* eslint-enable func-style */
|
||||||
|
|
||||||
export const isArray = Array.isArray;
|
/* istanbul ignore next */
|
||||||
export const isMap = testTag('Map');
|
export const isArray =
|
||||||
export const isSet = testTag('Set');
|
Array.isArray ||
|
||||||
|
function(value) {
|
||||||
|
return value && typeof value === 'object'
|
||||||
|
? toString.call(value) === '[object Array]'
|
||||||
|
: false;
|
||||||
|
};
|
||||||
|
|
||||||
// Older IE versions do not directly support indexOf so we must implement our own, sadly.
|
// Older IE versions do not directly support indexOf so we must implement our own, sadly.
|
||||||
export function indexOf(array, value) {
|
export function indexOf(array, value) {
|
||||||
@@ -96,3 +105,39 @@ export function createFrame(object) {
|
|||||||
frame._parent = object;
|
frame._parent = object;
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function blockParams(params, ids) {
|
||||||
|
params.path = ids;
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function appendContextPath(contextPath, id) {
|
||||||
|
return (contextPath ? contextPath + '.' : '') + id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Coerce an untrusted depth value to a safe non-negative integer.
|
||||||
|
* Returns `0` for any value that is not a finite, non-negative number.
|
||||||
|
*
|
||||||
|
* @param {unknown} depth - The depth value to sanitize.
|
||||||
|
* @returns {number} A non-negative integer.
|
||||||
|
*/
|
||||||
|
export function sanitizeDepth(depth) {
|
||||||
|
let number = Number(depth);
|
||||||
|
if (!Number.isFinite(number) || number < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Math.floor(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a sanitized copy of a PathExpression AST node's parts array.
|
||||||
|
* Coerces each element to a string, or returns an empty array if parts
|
||||||
|
* is not an array.
|
||||||
|
*
|
||||||
|
* @param {unknown} parts - The parts value to sanitize.
|
||||||
|
* @returns {string[]} A safe string array.
|
||||||
|
*/
|
||||||
|
export function sanitizeParts(parts) {
|
||||||
|
return Array.isArray(parts) ? parts.map(String) : [];
|
||||||
|
}
|
||||||
|
|||||||
+24
-44
@@ -1,46 +1,26 @@
|
|||||||
import handlebars from './handlebars.js';
|
// USAGE:
|
||||||
import { PrintVisitor, print } from '@handlebars/parser';
|
// var handlebars = require('handlebars');
|
||||||
|
/* eslint-env node */
|
||||||
|
/* eslint-disable no-var */
|
||||||
|
|
||||||
handlebars.PrintVisitor = PrintVisitor;
|
// var local = handlebars.create();
|
||||||
handlebars.print = print;
|
|
||||||
|
|
||||||
// Named exports for CJS interop.
|
var handlebars = require('../dist/cjs/handlebars')['default'];
|
||||||
//
|
|
||||||
// When Node.js (v22+) runs require() on an ESM module, it returns the module
|
var printer = require('../dist/cjs/handlebars/compiler/printer');
|
||||||
// namespace object — only named exports become direct properties. Without these,
|
handlebars.PrintVisitor = printer.PrintVisitor;
|
||||||
// require('handlebars') would return { default: inst } and calls like
|
handlebars.print = printer.print;
|
||||||
// Handlebars.precompile() or Handlebars.COMPILER_REVISION would be undefined.
|
|
||||||
//
|
module.exports = handlebars;
|
||||||
// Tools like handlebars-loader rely on these being directly accessible via
|
|
||||||
// require('handlebars').create(), require('handlebars').COMPILER_REVISION, etc.
|
// Publish a Node.js require() handler for .handlebars and .hbs files
|
||||||
export const {
|
function extension(module, filename) {
|
||||||
create,
|
var fs = require('fs');
|
||||||
compile,
|
var templateString = fs.readFileSync(filename, 'utf8');
|
||||||
precompile,
|
module.exports = handlebars.compile(templateString);
|
||||||
parse,
|
}
|
||||||
parseWithoutProcessing,
|
/* istanbul ignore else */
|
||||||
COMPILER_REVISION,
|
if (typeof require !== 'undefined' && require.extensions) {
|
||||||
LAST_COMPATIBLE_COMPILER_REVISION,
|
require.extensions['.handlebars'] = extension;
|
||||||
REVISION_CHANGES,
|
require.extensions['.hbs'] = extension;
|
||||||
VERSION,
|
}
|
||||||
AST,
|
|
||||||
Compiler,
|
|
||||||
JavaScriptCompiler,
|
|
||||||
Parser,
|
|
||||||
Visitor,
|
|
||||||
SafeString,
|
|
||||||
Exception,
|
|
||||||
Utils,
|
|
||||||
escapeExpression,
|
|
||||||
VM,
|
|
||||||
template,
|
|
||||||
log,
|
|
||||||
registerHelper,
|
|
||||||
unregisterHelper,
|
|
||||||
registerPartial,
|
|
||||||
unregisterPartial,
|
|
||||||
registerDecorator,
|
|
||||||
unregisterDecorator,
|
|
||||||
} = handlebars;
|
|
||||||
export { PrintVisitor, print };
|
|
||||||
export default handlebars;
|
|
||||||
|
|||||||
+97
-34
@@ -1,11 +1,12 @@
|
|||||||
|
/* eslint-env node */
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
import Async from 'neo-async';
|
import Async from 'neo-async';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import Handlebars from './handlebars.js';
|
import * as Handlebars from './handlebars';
|
||||||
import { basename } from 'path';
|
import { basename } from 'path';
|
||||||
import { SourceMapConsumer, SourceNode } from 'source-map';
|
import { SourceMapConsumer, SourceNode } from 'source-map';
|
||||||
|
|
||||||
export function loadTemplates(opts, callback) {
|
module.exports.loadTemplates = function(opts, callback) {
|
||||||
loadStrings(opts, function(err, strings) {
|
loadStrings(opts, function(err, strings) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
@@ -20,7 +21,7 @@ export function loadTemplates(opts, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function loadStrings(opts, callback) {
|
function loadStrings(opts, callback) {
|
||||||
let strings = arrayCast(opts.string),
|
let strings = arrayCast(opts.string),
|
||||||
@@ -56,7 +57,7 @@ function loadStrings(opts, callback) {
|
|||||||
strings = strings.map((string, index) => ({
|
strings = strings.map((string, index) => ({
|
||||||
name: names[index],
|
name: names[index],
|
||||||
path: names[index],
|
path: names[index],
|
||||||
source: string,
|
source: string
|
||||||
}));
|
}));
|
||||||
callback(err, strings);
|
callback(err, strings);
|
||||||
}
|
}
|
||||||
@@ -74,10 +75,7 @@ function loadFiles(opts, callback) {
|
|||||||
extension = new RegExp('\\.' + extension + '$');
|
extension = new RegExp('\\.' + extension + '$');
|
||||||
|
|
||||||
let ret = [],
|
let ret = [],
|
||||||
queue = (opts.files || []).map((template) => ({
|
queue = (opts.files || []).map(template => ({ template, root: opts.root }));
|
||||||
template,
|
|
||||||
root: opts.root,
|
|
||||||
}));
|
|
||||||
Async.whilst(
|
Async.whilst(
|
||||||
() => queue.length,
|
() => queue.length,
|
||||||
function(callback) {
|
function(callback) {
|
||||||
@@ -94,7 +92,7 @@ function loadFiles(opts, callback) {
|
|||||||
opts.hasDirectory = true;
|
opts.hasDirectory = true;
|
||||||
|
|
||||||
fs.readdir(path, function(err, children) {
|
fs.readdir(path, function(err, children) {
|
||||||
/* v8 ignore next -- Race condition that being too lazy to test */
|
/* istanbul ignore next : Race condition that being too lazy to test */
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@@ -113,7 +111,7 @@ function loadFiles(opts, callback) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
fs.readFile(path, 'utf8', function(err, data) {
|
||||||
/* v8 ignore next -- Race condition that being too lazy to test */
|
/* istanbul ignore next : Race condition that being too lazy to test */
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@@ -134,7 +132,7 @@ function loadFiles(opts, callback) {
|
|||||||
ret.push({
|
ret.push({
|
||||||
path: path,
|
path: path,
|
||||||
name: name,
|
name: name,
|
||||||
source: data,
|
source: data
|
||||||
});
|
});
|
||||||
|
|
||||||
callback();
|
callback();
|
||||||
@@ -152,7 +150,7 @@ function loadFiles(opts, callback) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cli(opts) {
|
module.exports.cli = function(opts) {
|
||||||
if (opts.version) {
|
if (opts.version) {
|
||||||
console.log(Handlebars.VERSION);
|
console.log(Handlebars.VERSION);
|
||||||
return;
|
return;
|
||||||
@@ -176,7 +174,12 @@ export async function cli(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Force simple mode if we have only one template and it's unnamed.
|
// Force simple mode if we have only one template and it's unnamed.
|
||||||
if (opts.templates.length === 1 && !opts.templates[0].name) {
|
if (
|
||||||
|
!opts.amd &&
|
||||||
|
!opts.commonjs &&
|
||||||
|
opts.templates.length === 1 &&
|
||||||
|
!opts.templates[0].name
|
||||||
|
) {
|
||||||
opts.simple = true;
|
opts.simple = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,9 +196,27 @@ export async function cli(opts) {
|
|||||||
|
|
||||||
const objectName = opts.partial ? 'Handlebars.partials' : 'templates';
|
const objectName = opts.partial ? 'Handlebars.partials' : 'templates';
|
||||||
|
|
||||||
|
if (opts.namespace && !isValidNamespace(opts.namespace)) {
|
||||||
|
throw new Handlebars.Exception('Invalid namespace format');
|
||||||
|
}
|
||||||
|
|
||||||
let output = new SourceNode();
|
let output = new SourceNode();
|
||||||
if (!opts.simple) {
|
if (!opts.simple) {
|
||||||
|
if (opts.amd) {
|
||||||
|
const runtimeModulePath =
|
||||||
|
(opts.handlebarPath || '') + 'handlebars.runtime';
|
||||||
|
output.add(
|
||||||
|
'define([' +
|
||||||
|
quoteForJavaScript(runtimeModulePath) +
|
||||||
|
'], function(Handlebars) {\n Handlebars = Handlebars["default"];'
|
||||||
|
);
|
||||||
|
} else if (opts.commonjs) {
|
||||||
|
output.add(
|
||||||
|
'var Handlebars = require(' + quoteForJavaScript(opts.commonjs) + ');'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
output.add('(function() {\n');
|
output.add('(function() {\n');
|
||||||
|
}
|
||||||
output.add(' var template = Handlebars.template, templates = ');
|
output.add(' var template = Handlebars.template, templates = ');
|
||||||
if (opts.namespace) {
|
if (opts.namespace) {
|
||||||
output.add(opts.namespace);
|
output.add(opts.namespace);
|
||||||
@@ -206,10 +227,10 @@ export async function cli(opts) {
|
|||||||
output.add('{};\n');
|
output.add('{};\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const template of opts.templates) {
|
opts.templates.forEach(function(template) {
|
||||||
let options = {
|
let options = {
|
||||||
knownHelpers: known,
|
knownHelpers: known,
|
||||||
knownHelpersOnly: opts.o,
|
knownHelpersOnly: opts.o
|
||||||
};
|
};
|
||||||
|
|
||||||
if (opts.map) {
|
if (opts.map) {
|
||||||
@@ -223,12 +244,11 @@ export async function cli(opts) {
|
|||||||
|
|
||||||
// If we are generating a source map, we have to reconstruct the SourceNode object
|
// If we are generating a source map, we have to reconstruct the SourceNode object
|
||||||
if (opts.map) {
|
if (opts.map) {
|
||||||
let consumer = await new SourceMapConsumer(precompiled.map);
|
let consumer = new SourceMapConsumer(precompiled.map);
|
||||||
precompiled = SourceNode.fromStringWithSourceMap(
|
precompiled = SourceNode.fromStringWithSourceMap(
|
||||||
precompiled.code,
|
precompiled.code,
|
||||||
consumer
|
consumer
|
||||||
);
|
);
|
||||||
consumer.destroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.simple) {
|
if (opts.simple) {
|
||||||
@@ -238,31 +258,43 @@ export async function cli(opts) {
|
|||||||
throw new Handlebars.Exception('Name missing for template');
|
throw new Handlebars.Exception('Name missing for template');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opts.amd && !multiple) {
|
||||||
|
output.add('return ');
|
||||||
|
}
|
||||||
output.add([
|
output.add([
|
||||||
objectName,
|
objectName,
|
||||||
"['",
|
'[',
|
||||||
template.name,
|
quoteForJavaScript(template.name),
|
||||||
"'] = template(",
|
'] = template(',
|
||||||
precompiled,
|
precompiled,
|
||||||
');\n',
|
');\n'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// Output the content
|
// Output the content
|
||||||
if (!opts.simple) {
|
if (!opts.simple) {
|
||||||
|
if (opts.amd) {
|
||||||
|
if (multiple) {
|
||||||
|
output.add(['return ', objectName, ';\n']);
|
||||||
|
}
|
||||||
|
output.add('});');
|
||||||
|
} else if (!opts.commonjs) {
|
||||||
output.add('})();');
|
output.add('})();');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (opts.map) {
|
if (opts.map) {
|
||||||
output.add('\n//# sourceMappingURL=' + opts.map + '\n');
|
output.add(
|
||||||
|
'\n//# sourceMappingURL=' + sanitizeSourceMapComment(opts.map) + '\n'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
output = output.toStringWithSourceMap();
|
output = output.toStringWithSourceMap();
|
||||||
output.map = output.map + '';
|
output.map = output.map + '';
|
||||||
|
|
||||||
if (opts.min) {
|
if (opts.min) {
|
||||||
output = await minify(output, opts.map);
|
output = minify(output, opts.map);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.map) {
|
if (opts.map) {
|
||||||
@@ -275,7 +307,7 @@ export async function cli(opts) {
|
|||||||
} else {
|
} else {
|
||||||
console.log(output);
|
console.log(output);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function arrayCast(value) {
|
function arrayCast(value) {
|
||||||
value = value != null ? value : [];
|
value = value != null ? value : [];
|
||||||
@@ -285,31 +317,62 @@ function arrayCast(value) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Safely quotes a value for embedding in generated JavaScript strings
|
||||||
|
*
|
||||||
|
* Uses JSON.stringify which handles all special characters.
|
||||||
|
*/
|
||||||
|
function quoteForJavaScript(value) {
|
||||||
|
return JSON.stringify(String(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that a namespace is a legitimate dotted JavaScript identifier
|
||||||
|
* (e.g. "App.templates") to prevent arbitrary code injection
|
||||||
|
*/
|
||||||
|
function isValidNamespace(namespace) {
|
||||||
|
return /^[A-Za-z_$][A-Za-z0-9_$]*(\.[A-Za-z_$][A-Za-z0-9_$]*)*$/.test(
|
||||||
|
namespace
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strips line terminators from source map URLs to prevent injection of new
|
||||||
|
* JavaScript lines via the sourceMappingURL comment
|
||||||
|
*/
|
||||||
|
function sanitizeSourceMapComment(value) {
|
||||||
|
return String(value).replace(/[\r\n\u2028\u2029]/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run uglify to minify the compiled template, if uglify exists in the dependencies.
|
* Run uglify to minify the compiled template, if uglify exists in the dependencies.
|
||||||
*
|
*
|
||||||
|
* We are using `require` instead of `import` here, because es6-modules do not allow
|
||||||
|
* dynamic imports and uglify-js is an optional dependency. Since we are inside NodeJS here, this
|
||||||
|
* should not be a problem.
|
||||||
|
*
|
||||||
* @param {string} output the compiled template
|
* @param {string} output the compiled template
|
||||||
* @param {string} sourceMapFile the file to write the source map to.
|
* @param {string} sourceMapFile the file to write the source map to.
|
||||||
*/
|
*/
|
||||||
async function minify(output, sourceMapFile) {
|
function minify(output, sourceMapFile) {
|
||||||
let uglify;
|
|
||||||
try {
|
try {
|
||||||
uglify = await import('uglify-js');
|
// Try to resolve uglify-js in order to see if it does exist
|
||||||
// Handle both default and named exports
|
require.resolve('uglify-js');
|
||||||
uglify = uglify.default || uglify;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code !== 'ERR_MODULE_NOT_FOUND' && e.code !== 'MODULE_NOT_FOUND') {
|
if (e.code !== 'MODULE_NOT_FOUND') {
|
||||||
|
// Something else seems to be wrong
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
// it does not exist!
|
||||||
console.error(
|
console.error(
|
||||||
'Code minimization is disabled due to missing uglify-js dependency'
|
'Code minimization is disabled due to missing uglify-js dependency'
|
||||||
);
|
);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
return uglify.minify(output.code, {
|
return require('uglify-js').minify(output.code, {
|
||||||
sourceMap: {
|
sourceMap: {
|
||||||
content: output.map,
|
content: output.map,
|
||||||
url: sourceMapFile,
|
url: sourceMapFile
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
module.exports = {
|
||||||
|
'check-coverage': true,
|
||||||
|
branches: 100,
|
||||||
|
lines: 100,
|
||||||
|
functions: 100,
|
||||||
|
statements: 100,
|
||||||
|
exclude: ['**/spec/**', '**/handlebars/compiler/parser.js'],
|
||||||
|
reporter: 'html'
|
||||||
|
};
|
||||||
Generated
+14743
-15043
File diff suppressed because it is too large
Load Diff
+101
-99
@@ -1,132 +1,134 @@
|
|||||||
{
|
{
|
||||||
"name": "handlebars",
|
"name": "handlebars",
|
||||||
"version": "5.0.0-alpha.1",
|
"barename": "handlebars",
|
||||||
|
"version": "4.7.9",
|
||||||
"description": "Handlebars provides the power necessary to let you build semantic templates effectively with no frustration",
|
"description": "Handlebars provides the power necessary to let you build semantic templates effectively with no frustration",
|
||||||
|
"homepage": "https://handlebarsjs.com/",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"handlebars",
|
"handlebars",
|
||||||
"html",
|
|
||||||
"mustache",
|
"mustache",
|
||||||
"template"
|
"template",
|
||||||
|
"html"
|
||||||
],
|
],
|
||||||
"homepage": "https://handlebarsjs.com/",
|
|
||||||
"license": "MIT",
|
|
||||||
"author": "Yehuda Katz",
|
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/handlebars-lang/handlebars.js.git"
|
"url": "https://github.com/handlebars-lang/handlebars.js.git"
|
||||||
},
|
},
|
||||||
|
"author": "Yehuda Katz",
|
||||||
|
"license": "MIT",
|
||||||
|
"readmeFilename": "README.md",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.4.7"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"minimist": "^1.2.5",
|
||||||
|
"neo-async": "^2.6.2",
|
||||||
|
"source-map": "^0.6.1",
|
||||||
|
"wordwrap": "^1.0.0"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"uglify-js": "^3.1.4"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@playwright/test": "1.44.1",
|
||||||
|
"aws-sdk": "^2.1.49",
|
||||||
|
"babel-loader": "^5.0.0",
|
||||||
|
"babel-runtime": "^5.1.10",
|
||||||
|
"benchmark": "~1.0",
|
||||||
|
"chai": "^4.2.0",
|
||||||
|
"chai-diff": "^1.0.1",
|
||||||
|
"concurrently": "^5.0.0",
|
||||||
|
"dirty-chai": "^2.0.1",
|
||||||
|
"dustjs-linkedin": "^2.0.2",
|
||||||
|
"eco": "~1.1.0-rc-3",
|
||||||
|
"eslint": "^6.7.2",
|
||||||
|
"eslint-config-prettier": "^6.7.0",
|
||||||
|
"eslint-plugin-compat": "^3.13.0",
|
||||||
|
"eslint-plugin-es5": "^1.4.1",
|
||||||
|
"fs-extra": "^8.1.0",
|
||||||
|
"grunt": "1.5.3",
|
||||||
|
"grunt-babel": "^5.0.0",
|
||||||
|
"grunt-cli": "^1",
|
||||||
|
"grunt-contrib-clean": "^1",
|
||||||
|
"grunt-contrib-concat": "^1",
|
||||||
|
"grunt-contrib-connect": "^1",
|
||||||
|
"grunt-contrib-copy": "^1",
|
||||||
|
"grunt-contrib-requirejs": "^1",
|
||||||
|
"grunt-contrib-uglify": "^1",
|
||||||
|
"grunt-contrib-watch": "^1.1.0",
|
||||||
|
"grunt-shell": "^4.0.0",
|
||||||
|
"grunt-webpack": "^1.0.8",
|
||||||
|
"husky": "^3.1.0",
|
||||||
|
"jison": "~0.3.0",
|
||||||
|
"lint-staged": "^9.5.0",
|
||||||
|
"mocha": "^5",
|
||||||
|
"mock-stdin": "^0.3.0",
|
||||||
|
"mustache": "^2.1.3",
|
||||||
|
"nyc": "^14.1.1",
|
||||||
|
"prettier": "^1.19.1",
|
||||||
|
"semver": "^5.0.1",
|
||||||
|
"sinon": "^7.5.0",
|
||||||
|
"typescript": "^3.4.3",
|
||||||
|
"underscore": "^1.5.1",
|
||||||
|
"webpack": "^1.12.6",
|
||||||
|
"webpack-dev-server": "^1.12.1"
|
||||||
|
},
|
||||||
|
"main": "lib/index.js",
|
||||||
|
"types": "types/index.d.ts",
|
||||||
|
"browser": "./dist/cjs/handlebars.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
"handlebars": "bin/handlebars.js"
|
"handlebars": "bin/handlebars"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "grunt build",
|
||||||
|
"release": "npm run build && grunt release",
|
||||||
|
"format": "prettier --write '**/*.js' && eslint --fix .",
|
||||||
|
"lint": "npm run lint:eslint && npm run lint:prettier && npm run lint:types",
|
||||||
|
"lint:eslint": "eslint --max-warnings 0 .",
|
||||||
|
"lint:prettier": "prettier --check '**/*.js'",
|
||||||
|
"lint:types": "tsc --noEmit --project types",
|
||||||
|
"test": "npm run test:mocha",
|
||||||
|
"test:mocha": "grunt build && grunt test",
|
||||||
|
"test:browser": "playwright test --config tests/browser/playwright.config.js tests/browser/spec.js",
|
||||||
|
"test:integration": "grunt integration-tests",
|
||||||
|
"test:serve": "grunt connect:server:keepalive",
|
||||||
|
"extensive-tests-and-publish-to-aws": "npx mocha tasks/tests/ && grunt --stack extensive-tests-and-publish-to-aws",
|
||||||
|
"--- combined tasks ---": "",
|
||||||
|
"check-before-pull-request": "concurrently --kill-others-on-fail npm:lint npm:test"
|
||||||
|
},
|
||||||
|
"jspm": {
|
||||||
|
"main": "handlebars",
|
||||||
|
"directories": {
|
||||||
|
"lib": "dist/amd"
|
||||||
|
},
|
||||||
|
"buildConfig": {
|
||||||
|
"minify": true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"bin",
|
"bin",
|
||||||
"dist/*.js",
|
"dist/*.js",
|
||||||
|
"dist/amd/**/*.js",
|
||||||
|
"dist/cjs/**/*.js",
|
||||||
"lib",
|
"lib",
|
||||||
"release-notes.md",
|
"release-notes.md",
|
||||||
"runtime.js",
|
"runtime.js",
|
||||||
"types/*.d.ts",
|
"types/*.d.ts",
|
||||||
"runtime.d.ts"
|
"runtime.d.ts"
|
||||||
],
|
],
|
||||||
"type": "module",
|
|
||||||
"main": "lib/index.js",
|
|
||||||
"types": "types/index.d.ts",
|
|
||||||
"imports": {
|
|
||||||
"#source-node": {
|
|
||||||
"node": "./lib/handlebars/compiler/source-node.node.js",
|
|
||||||
"default": "./lib/handlebars/compiler/source-node.browser.js"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"exports": {
|
|
||||||
".": {
|
|
||||||
"types": "./types/index.d.ts",
|
|
||||||
"default": "./lib/index.js"
|
|
||||||
},
|
|
||||||
"./runtime": {
|
|
||||||
"types": "./runtime.d.ts",
|
|
||||||
"default": "./lib/handlebars.runtime.js"
|
|
||||||
},
|
|
||||||
"./package.json": "./package.json"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"clean": "node --input-type=module -e \"import fs from 'fs';fs.rmSync('dist',{recursive:true,force:true});fs.rmSync('tmp',{recursive:true,force:true})\"",
|
|
||||||
"build": "npm run clean && rspack build",
|
|
||||||
"release": "npm run build",
|
|
||||||
"publish:aws": "npm run build && npm run test:tasks && node tasks/publish-to-aws.js",
|
|
||||||
"format": "oxfmt --write . && oxlint --fix .",
|
|
||||||
"lint": "npm run lint:oxlint && npm run lint:format && npm run lint:types && npm run lint:compat",
|
|
||||||
"lint:oxlint": "oxlint --max-warnings 0 .",
|
|
||||||
"lint:format": "oxfmt --check .",
|
|
||||||
"lint:types": "tstyche",
|
|
||||||
"lint:compat": "eslint",
|
|
||||||
"test": "npm run build && vitest run --project node --project tasks --project rspack --coverage",
|
|
||||||
"test:browser": "vitest run --project browser",
|
|
||||||
"test:unit": "vitest run --project node",
|
|
||||||
"test:tasks": "vitest run --project tasks",
|
|
||||||
"test:publish": "npm run build && vitest run --project publish",
|
|
||||||
"test:browser-smoke": "playwright test --config tests/browser/playwright.config.js",
|
|
||||||
"test:serve": "npx serve -l 9999 .",
|
|
||||||
"test:integration": "npm run build && ./tests/integration/run-integration-tests.sh",
|
|
||||||
"bench": "node tests/bench/perf.js",
|
|
||||||
"bench:compare": "node tests/bench/compare.js",
|
|
||||||
"bench:size": "node tests/bench/size.js",
|
|
||||||
"--- combined tasks ---": "",
|
|
||||||
"check-before-pull-request": "concurrently --kill-others-on-fail npm:lint npm:test"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@handlebars/parser": "^2.2.2",
|
|
||||||
"neo-async": "^2.6.2",
|
|
||||||
"source-map": "^0.7.6",
|
|
||||||
"yargs": "^18.0.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@aws-sdk/client-s3": "^3.1011.0",
|
|
||||||
"@playwright/test": "^1.58.2",
|
|
||||||
"@rspack/cli": "^1.7.8",
|
|
||||||
"@rspack/core": "^1.7.8",
|
|
||||||
"@vitest/browser": "^4.0.18",
|
|
||||||
"@vitest/browser-playwright": "^4.0.18",
|
|
||||||
"@vitest/coverage-v8": "^4.0.18",
|
|
||||||
"cli-testlab": "^6.0.0",
|
|
||||||
"concurrently": "^5.0.0",
|
|
||||||
"eslint": "^10.0.3",
|
|
||||||
"eslint-plugin-compat": "^7.0.1",
|
|
||||||
"fs-extra": "^8.1.0",
|
|
||||||
"husky": "^3.1.0",
|
|
||||||
"lint-staged": "^16.3.2",
|
|
||||||
"mock-stdin": "^0.3.0",
|
|
||||||
"oxfmt": "^0.36.0",
|
|
||||||
"oxlint": "^1.51.0",
|
|
||||||
"semver": "^5.0.1",
|
|
||||||
"tinybench": "^6.0.0",
|
|
||||||
"tstyche": "^6.2.0",
|
|
||||||
"typescript": "^5.9.3",
|
|
||||||
"uglify-js": "^3.19.3",
|
|
||||||
"vitest": "^4.0.18"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"uglify-js": "^3.19.3"
|
|
||||||
},
|
|
||||||
"husky": {
|
"husky": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
"pre-commit": "lint-staged"
|
"pre-commit": "lint-staged"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"*.{js,css,json,md}": [
|
"*.{js,css,json}": [
|
||||||
"oxfmt --write"
|
"prettier --write",
|
||||||
|
"git add"
|
||||||
],
|
],
|
||||||
"*.js": [
|
"*.js": [
|
||||||
"oxlint --fix"
|
"eslint --fix",
|
||||||
|
"git add"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
"browserslist": [
|
|
||||||
"last 2 versions",
|
|
||||||
"Firefox ESR",
|
|
||||||
"not dead",
|
|
||||||
"not IE 11",
|
|
||||||
"maintained node versions"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": ">=20"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
tabWidth: 2,
|
||||||
|
semi: true,
|
||||||
|
singleQuote: true
|
||||||
|
};
|
||||||
+297
-287
File diff suppressed because it is too large
Load Diff
@@ -1,103 +0,0 @@
|
|||||||
import { rspack } from '@rspack/core';
|
|
||||||
import path from 'path';
|
|
||||||
import fs from 'fs';
|
|
||||||
import { fileURLToPath } from 'url';
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
|
||||||
const __dirname = path.dirname(__filename);
|
|
||||||
|
|
||||||
const pkg = JSON.parse(
|
|
||||||
fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8')
|
|
||||||
);
|
|
||||||
const license = fs.readFileSync(path.resolve(__dirname, 'LICENSE'), 'utf8');
|
|
||||||
const banner = `/*!
|
|
||||||
|
|
||||||
@license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
|
|
||||||
${pkg.name} v${pkg.version}
|
|
||||||
|
|
||||||
${license}
|
|
||||||
*/`;
|
|
||||||
|
|
||||||
function createConfig(entry, filename, minimize) {
|
|
||||||
const plugins = [];
|
|
||||||
|
|
||||||
if (!minimize) {
|
|
||||||
// For non-minified builds, use BannerPlugin to add the license header
|
|
||||||
plugins.push(new rspack.BannerPlugin({ banner, raw: true }));
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
mode: minimize ? 'production' : 'none',
|
|
||||||
context: __dirname,
|
|
||||||
entry,
|
|
||||||
output: {
|
|
||||||
path: path.resolve(__dirname, 'dist'),
|
|
||||||
filename,
|
|
||||||
library: {
|
|
||||||
name: 'Handlebars',
|
|
||||||
type: 'self',
|
|
||||||
export: 'default',
|
|
||||||
},
|
|
||||||
clean: false,
|
|
||||||
},
|
|
||||||
optimization: {
|
|
||||||
minimize,
|
|
||||||
minimizer: minimize
|
|
||||||
? [
|
|
||||||
new rspack.SwcJsMinimizerRspackPlugin({
|
|
||||||
extractComments: false,
|
|
||||||
minimizerOptions: {
|
|
||||||
compress: { passes: 2 },
|
|
||||||
mangle: true,
|
|
||||||
format: {
|
|
||||||
comments: false,
|
|
||||||
// Prepend the license banner in the minified output
|
|
||||||
preamble: banner,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
: [],
|
|
||||||
},
|
|
||||||
plugins,
|
|
||||||
resolve: {
|
|
||||||
extensions: ['.js', '.json'],
|
|
||||||
mainFields: ['main'],
|
|
||||||
},
|
|
||||||
module: {
|
|
||||||
rules: [
|
|
||||||
{
|
|
||||||
test: /\.js$/,
|
|
||||||
resolve: {
|
|
||||||
fullySpecified: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
test: /\.js$/,
|
|
||||||
exclude: /node_modules/,
|
|
||||||
use: {
|
|
||||||
loader: 'builtin:swc-loader',
|
|
||||||
options: {
|
|
||||||
jsc: {
|
|
||||||
parser: { syntax: 'ecmascript' },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
target: ['web', 'browserslist'],
|
|
||||||
devtool: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default [
|
|
||||||
createConfig('./lib/handlebars.js', 'handlebars.js', false),
|
|
||||||
createConfig('./lib/handlebars.runtime.js', 'handlebars.runtime.js', false),
|
|
||||||
createConfig('./lib/handlebars.js', 'handlebars.min.js', true),
|
|
||||||
createConfig(
|
|
||||||
'./lib/handlebars.runtime.js',
|
|
||||||
'handlebars.runtime.min.js',
|
|
||||||
true
|
|
||||||
),
|
|
||||||
];
|
|
||||||
Vendored
+4
-3
@@ -1,4 +1,5 @@
|
|||||||
import Handlebars from 'handlebars';
|
import Handlebars = require('handlebars')
|
||||||
|
|
||||||
declare const runtime: typeof Handlebars;
|
declare module "handlebars/runtime" {
|
||||||
export default runtime;
|
|
||||||
|
}
|
||||||
+3
-3
@@ -1,3 +1,3 @@
|
|||||||
import runtime from './lib/handlebars.runtime.js';
|
// Create a simple path alias to allow browserify to resolve
|
||||||
|
// the runtime on a supported path.
|
||||||
export default runtime;
|
module.exports = require('./dist/cjs/handlebars.runtime')['default'];
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"extends": [
|
||||||
|
"../.eslintrc.js",
|
||||||
|
"plugin:es5/no-es2015",
|
||||||
|
"prettier"
|
||||||
|
],
|
||||||
|
"plugins": [
|
||||||
|
"es5"
|
||||||
|
],
|
||||||
|
"globals": {
|
||||||
|
"CompilerContext": true,
|
||||||
|
"Handlebars": true,
|
||||||
|
"handlebarsEnv": true,
|
||||||
|
"shouldCompileTo": true,
|
||||||
|
"shouldCompileToWithPartials": true,
|
||||||
|
"shouldThrow": true,
|
||||||
|
"expectTemplate": true,
|
||||||
|
"compileWithPartials": true,
|
||||||
|
"console": true,
|
||||||
|
"require": true,
|
||||||
|
"suite": true,
|
||||||
|
"equal": true,
|
||||||
|
"equals": true,
|
||||||
|
"test": true,
|
||||||
|
"testBoth": true,
|
||||||
|
"raises": true,
|
||||||
|
"deepEqual": true,
|
||||||
|
"start": true,
|
||||||
|
"stop": true,
|
||||||
|
"ok": true,
|
||||||
|
"sinon": true,
|
||||||
|
"strictEqual": true,
|
||||||
|
"define": true,
|
||||||
|
"expect": true,
|
||||||
|
"chai": true
|
||||||
|
},
|
||||||
|
"env": {
|
||||||
|
"mocha": true
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
// Disabling for tests, for now.
|
||||||
|
"no-path-concat": "off",
|
||||||
|
|
||||||
|
"no-var": "off",
|
||||||
|
"dot-notation": "off"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Mocha</title>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
|
||||||
|
<style>
|
||||||
|
.headless .suite > h1,
|
||||||
|
.headless .test.pass {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Show only errors in "headless", non-interactive mode.
|
||||||
|
if (/headless=true/.test(location.href)) {
|
||||||
|
document.documentElement.className = 'headless';
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<script src="/node_modules/sinon/pkg/sinon.js"></script>
|
||||||
|
<script src="/node_modules/chai/chai.js"></script>
|
||||||
|
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
|
||||||
|
<script src="/node_modules/mocha/mocha.js"></script>
|
||||||
|
<script>
|
||||||
|
window.expect = chai.expect;
|
||||||
|
mocha.setup('bdd');
|
||||||
|
</script>
|
||||||
|
<script src="/spec/vendor/json2.js"></script>
|
||||||
|
<script src="/spec/vendor/require.js"></script>
|
||||||
|
|
||||||
|
<script src="/spec/env/common.js"></script>
|
||||||
|
<script>
|
||||||
|
var requireFailure;
|
||||||
|
|
||||||
|
requirejs.config({
|
||||||
|
paths: {
|
||||||
|
'handlebars.runtime': '/dist/handlebars.runtime.amd'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
requirejs.onError = function (err) {
|
||||||
|
requireFailure = err;
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
onload = function(){
|
||||||
|
require(['handlebars.runtime'], function(Handlebars) {
|
||||||
|
describe('runtime', function() {
|
||||||
|
it('should load', function() {
|
||||||
|
equal(!!Handlebars['default'].template, true);
|
||||||
|
equal(!!Handlebars['default'].VERSION, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
mocha.globals(['mochaResults'])
|
||||||
|
// The test harness leaks under FF. We should have decent global leak coverage from other tests
|
||||||
|
if (!navigator.userAgent.match(/Firefox\/([\d.]+)/)) {
|
||||||
|
mocha.checkLeaks();
|
||||||
|
}
|
||||||
|
var runner = mocha.run();
|
||||||
|
|
||||||
|
// Reporting to test-runner
|
||||||
|
var failedTests = [];
|
||||||
|
runner.on('end', function(){
|
||||||
|
window.mochaResults = runner.stats;
|
||||||
|
window.mochaResults.reports = failedTests;
|
||||||
|
});
|
||||||
|
|
||||||
|
runner.on('fail', logFailure);
|
||||||
|
|
||||||
|
// Inject any require initilizer failures into the first test so they are properly
|
||||||
|
// reported.
|
||||||
|
if (requireFailure) {
|
||||||
|
runner.on('hook end', function(hook){
|
||||||
|
if (requireFailure) {
|
||||||
|
runner.uncaught(requireFailure);
|
||||||
|
requireFailure = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function logFailure(test, err){
|
||||||
|
|
||||||
|
var flattenTitles = function(test){
|
||||||
|
var titles = [];
|
||||||
|
while (test.parent.title){
|
||||||
|
titles.push(test.parent.title);
|
||||||
|
test = test.parent;
|
||||||
|
}
|
||||||
|
return titles.reverse();
|
||||||
|
};
|
||||||
|
|
||||||
|
failedTests.push({
|
||||||
|
name: test.title,
|
||||||
|
result: false,
|
||||||
|
message: err.message,
|
||||||
|
stack: err.stack,
|
||||||
|
titles: flattenTitles(test)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="mocha"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+128
@@ -0,0 +1,128 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Mocha</title>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
|
||||||
|
<style>
|
||||||
|
.headless .suite > h1,
|
||||||
|
.headless .test.pass {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Show only errors in "headless", non-interactive mode.
|
||||||
|
if (/headless=true/.test(location.href)) {
|
||||||
|
document.documentElement.className = 'headless';
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<script src="/node_modules/sinon/pkg/sinon.js"></script>
|
||||||
|
<script src="/node_modules/chai/chai.js"></script>
|
||||||
|
<script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
|
||||||
|
<script src="/node_modules/mocha/mocha.js"></script>
|
||||||
|
<script>
|
||||||
|
window.expect = chai.expect;
|
||||||
|
mocha.setup('bdd');
|
||||||
|
</script>
|
||||||
|
<script src="/spec/vendor/json2.js"></script>
|
||||||
|
<script src="/spec/vendor/require.js"></script>
|
||||||
|
|
||||||
|
<script src="/spec/env/common.js"></script>
|
||||||
|
<script>
|
||||||
|
var requireFailure;
|
||||||
|
|
||||||
|
requirejs.config({
|
||||||
|
paths: {
|
||||||
|
handlebars: '/dist/handlebars.amd',
|
||||||
|
tests: '/tmp/tests'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
requirejs.onError = function (err) {
|
||||||
|
requireFailure = err;
|
||||||
|
};
|
||||||
|
|
||||||
|
var CompilerContext = {
|
||||||
|
compile: function(template, options) {
|
||||||
|
var templateSpec = handlebarsEnv.precompile(template, options);
|
||||||
|
return handlebarsEnv.template(safeEval(templateSpec));
|
||||||
|
},
|
||||||
|
compileWithPartial: function(template, options) {
|
||||||
|
return handlebarsEnv.compile(template, options);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function safeEval(templateSpec) {
|
||||||
|
try {
|
||||||
|
var ret;
|
||||||
|
eval('ret = ' + templateSpec);
|
||||||
|
return ret;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(templateSpec);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
onload = function(){
|
||||||
|
|
||||||
|
require(['handlebars'], function(Handlebars) {
|
||||||
|
window.Handlebars = Handlebars['default'];
|
||||||
|
|
||||||
|
require(['tests'], function(Handlebars) {
|
||||||
|
mocha.globals(['mochaResults'])
|
||||||
|
// The test harness leaks under FF. We should have decent global leak coverage from other tests
|
||||||
|
if (!navigator.userAgent.match(/Firefox\/([\d.]+)/)) {
|
||||||
|
mocha.checkLeaks();
|
||||||
|
}
|
||||||
|
var runner = mocha.run();
|
||||||
|
|
||||||
|
// Reporting to test-runner
|
||||||
|
var failedTests = [];
|
||||||
|
runner.on('end', function(){
|
||||||
|
window.mochaResults = runner.stats;
|
||||||
|
window.mochaResults.reports = failedTests;
|
||||||
|
});
|
||||||
|
|
||||||
|
runner.on('fail', logFailure);
|
||||||
|
|
||||||
|
// Inject any require initilizer failures into the first test so they are properly
|
||||||
|
// reported.
|
||||||
|
if (requireFailure) {
|
||||||
|
runner.on('hook end', function(hook){
|
||||||
|
if (requireFailure) {
|
||||||
|
runner.uncaught(requireFailure);
|
||||||
|
requireFailure = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function logFailure(test, err){
|
||||||
|
function flattenTitles(test){
|
||||||
|
var titles = [];
|
||||||
|
while (test.parent.title){
|
||||||
|
titles.push(test.parent.title);
|
||||||
|
test = test.parent;
|
||||||
|
}
|
||||||
|
return titles.reverse();
|
||||||
|
}
|
||||||
|
|
||||||
|
failedTests.push({
|
||||||
|
name: test.title,
|
||||||
|
result: false,
|
||||||
|
message: err.message,
|
||||||
|
stack: err.stack,
|
||||||
|
titles: flattenTitles(test)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="mocha"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+317
-66
@@ -7,100 +7,102 @@ describe('ast', function () {
|
|||||||
|
|
||||||
describe('BlockStatement', function() {
|
describe('BlockStatement', function() {
|
||||||
it('should throw on mustache mismatch', function() {
|
it('should throw on mustache mismatch', function() {
|
||||||
expect(function () {
|
shouldThrow(
|
||||||
|
function() {
|
||||||
handlebarsEnv.parse('\n {{#foo}}{{/bar}}');
|
handlebarsEnv.parse('\n {{#foo}}{{/bar}}');
|
||||||
}).toThrow("foo doesn't match bar - 2:5");
|
},
|
||||||
|
Handlebars.Exception,
|
||||||
|
"foo doesn't match bar - 2:5"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('helpers', function() {
|
describe('helpers', function() {
|
||||||
describe('#helperExpression', function() {
|
describe('#helperExpression', function() {
|
||||||
it('should handle mustache statements', function() {
|
it('should handle mustache statements', function() {
|
||||||
expect(
|
equals(
|
||||||
AST.helpers.helperExpression({
|
AST.helpers.helperExpression({
|
||||||
type: 'MustacheStatement',
|
type: 'MustacheStatement',
|
||||||
params: [],
|
params: [],
|
||||||
hash: undefined,
|
hash: undefined
|
||||||
})
|
}),
|
||||||
).toBe(false);
|
false
|
||||||
expect(
|
);
|
||||||
|
equals(
|
||||||
AST.helpers.helperExpression({
|
AST.helpers.helperExpression({
|
||||||
type: 'MustacheStatement',
|
type: 'MustacheStatement',
|
||||||
params: [1],
|
params: [1],
|
||||||
hash: undefined,
|
hash: undefined
|
||||||
})
|
}),
|
||||||
).toBe(true);
|
true
|
||||||
expect(
|
);
|
||||||
|
equals(
|
||||||
AST.helpers.helperExpression({
|
AST.helpers.helperExpression({
|
||||||
type: 'MustacheStatement',
|
type: 'MustacheStatement',
|
||||||
params: [],
|
params: [],
|
||||||
hash: {},
|
hash: {}
|
||||||
})
|
}),
|
||||||
).toBe(true);
|
|
||||||
});
|
|
||||||
it('should handle block statements', function () {
|
|
||||||
expect(
|
|
||||||
AST.helpers.helperExpression({
|
|
||||||
type: 'BlockStatement',
|
|
||||||
params: [],
|
|
||||||
hash: undefined,
|
|
||||||
})
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
AST.helpers.helperExpression({
|
|
||||||
type: 'BlockStatement',
|
|
||||||
params: [1],
|
|
||||||
hash: undefined,
|
|
||||||
})
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
AST.helpers.helperExpression({
|
|
||||||
type: 'BlockStatement',
|
|
||||||
params: [],
|
|
||||||
hash: {},
|
|
||||||
})
|
|
||||||
).toBe(true);
|
|
||||||
});
|
|
||||||
it('should handle subexpressions', function () {
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'SubExpression' })).toBe(
|
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
it('should handle block statements', function() {
|
||||||
|
equals(
|
||||||
|
AST.helpers.helperExpression({
|
||||||
|
type: 'BlockStatement',
|
||||||
|
params: [],
|
||||||
|
hash: undefined
|
||||||
|
}),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
equals(
|
||||||
|
AST.helpers.helperExpression({
|
||||||
|
type: 'BlockStatement',
|
||||||
|
params: [1],
|
||||||
|
hash: undefined
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
equals(
|
||||||
|
AST.helpers.helperExpression({
|
||||||
|
type: 'BlockStatement',
|
||||||
|
params: [],
|
||||||
|
hash: {}
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it('should handle subexpressions', function() {
|
||||||
|
equals(AST.helpers.helperExpression({ type: 'SubExpression' }), true);
|
||||||
|
});
|
||||||
it('should work with non-helper nodes', function() {
|
it('should work with non-helper nodes', function() {
|
||||||
expect(AST.helpers.helperExpression({ type: 'Program' })).toBe(false);
|
equals(AST.helpers.helperExpression({ type: 'Program' }), false);
|
||||||
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'PartialStatement' })).toBe(
|
equals(
|
||||||
|
AST.helpers.helperExpression({ type: 'PartialStatement' }),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
expect(AST.helpers.helperExpression({ type: 'ContentStatement' })).toBe(
|
equals(
|
||||||
|
AST.helpers.helperExpression({ type: 'ContentStatement' }),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
expect(AST.helpers.helperExpression({ type: 'CommentStatement' })).toBe(
|
equals(
|
||||||
|
AST.helpers.helperExpression({ type: 'CommentStatement' }),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'PathExpression' })).toBe(
|
equals(AST.helpers.helperExpression({ type: 'PathExpression' }), false);
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'StringLiteral' })).toBe(
|
equals(AST.helpers.helperExpression({ type: 'StringLiteral' }), false);
|
||||||
false
|
equals(AST.helpers.helperExpression({ type: 'NumberLiteral' }), false);
|
||||||
);
|
equals(AST.helpers.helperExpression({ type: 'BooleanLiteral' }), false);
|
||||||
expect(AST.helpers.helperExpression({ type: 'NumberLiteral' })).toBe(
|
equals(
|
||||||
false
|
AST.helpers.helperExpression({ type: 'UndefinedLiteral' }),
|
||||||
);
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'BooleanLiteral' })).toBe(
|
|
||||||
false
|
|
||||||
);
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'UndefinedLiteral' })).toBe(
|
|
||||||
false
|
|
||||||
);
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'NullLiteral' })).toBe(
|
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
equals(AST.helpers.helperExpression({ type: 'NullLiteral' }), false);
|
||||||
|
|
||||||
expect(AST.helpers.helperExpression({ type: 'Hash' })).toBe(false);
|
equals(AST.helpers.helperExpression({ type: 'Hash' }), false);
|
||||||
expect(AST.helpers.helperExpression({ type: 'HashPair' })).toBe(false);
|
equals(AST.helpers.helperExpression({ type: 'HashPair' }), false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -109,12 +111,13 @@ describe('ast', function () {
|
|||||||
var ast, body;
|
var ast, body;
|
||||||
|
|
||||||
function testColumns(node, firstLine, lastLine, firstColumn, lastColumn) {
|
function testColumns(node, firstLine, lastLine, firstColumn, lastColumn) {
|
||||||
expect(node.loc.start.line).toBe(firstLine);
|
equals(node.loc.start.line, firstLine);
|
||||||
expect(node.loc.start.column).toBe(firstColumn);
|
equals(node.loc.start.column, firstColumn);
|
||||||
expect(node.loc.end.line).toBe(lastLine);
|
equals(node.loc.end.line, lastLine);
|
||||||
expect(node.loc.end.column).toBe(lastColumn);
|
equals(node.loc.end.column, lastColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* eslint-disable no-multi-spaces */
|
||||||
ast = Handlebars.parse(
|
ast = Handlebars.parse(
|
||||||
'line 1 {{line1Token}}\n' + // 1
|
'line 1 {{line1Token}}\n' + // 1
|
||||||
' line 2 {{line2token}}\n' + // 2
|
' line 2 {{line2token}}\n' + // 2
|
||||||
@@ -128,6 +131,7 @@ describe('ast', function () {
|
|||||||
'{{else}}\n' + // 10
|
'{{else}}\n' + // 10
|
||||||
'{{/open}}'
|
'{{/open}}'
|
||||||
); // 11
|
); // 11
|
||||||
|
/* eslint-enable no-multi-spaces */
|
||||||
body = ast.body;
|
body = ast.body;
|
||||||
|
|
||||||
it('gets ContentNode line numbers', function() {
|
it('gets ContentNode line numbers', function() {
|
||||||
@@ -177,4 +181,251 @@ describe('ast', function () {
|
|||||||
testColumns(chainInverseNode.inverse.body[0].inverse, 10, 11, 8, 0);
|
testColumns(chainInverseNode.inverse.body[0].inverse, 10, 11, 8, 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('whitespace control', function() {
|
||||||
|
describe('parse', function() {
|
||||||
|
it('mustache', function() {
|
||||||
|
var ast = Handlebars.parse(' {{~comment~}} ');
|
||||||
|
|
||||||
|
equals(ast.body[0].value, '');
|
||||||
|
equals(ast.body[2].value, '');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('block statements', function() {
|
||||||
|
var ast = Handlebars.parse(' {{# comment~}} \nfoo\n {{~/comment}}');
|
||||||
|
|
||||||
|
equals(ast.body[0].value, '');
|
||||||
|
equals(ast.body[1].program.body[0].value, 'foo');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('parseWithoutProcessing', function() {
|
||||||
|
it('mustache', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(' {{~comment~}} ');
|
||||||
|
|
||||||
|
equals(ast.body[0].value, ' ');
|
||||||
|
equals(ast.body[2].value, ' ');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('block statements', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(
|
||||||
|
' {{# comment~}} \nfoo\n {{~/comment}}'
|
||||||
|
);
|
||||||
|
|
||||||
|
equals(ast.body[0].value, ' ');
|
||||||
|
equals(ast.body[1].program.body[0].value, ' \nfoo\n ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('standalone flags', function() {
|
||||||
|
describe('mustache', function() {
|
||||||
|
it('does not mark mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(' {{comment}} ');
|
||||||
|
equals(!!ast.body[0].value, true);
|
||||||
|
equals(!!ast.body[2].value, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('blocks - parseWithoutProcessing', function() {
|
||||||
|
it('block mustaches', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(
|
||||||
|
' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
|
||||||
|
),
|
||||||
|
block = ast.body[1];
|
||||||
|
|
||||||
|
equals(ast.body[0].value, ' ');
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \nfoo\n ');
|
||||||
|
equals(block.inverse.body[0].value, ' \n bar \n ');
|
||||||
|
|
||||||
|
equals(ast.body[2].value, ' ');
|
||||||
|
});
|
||||||
|
it('initial block mustaches', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(
|
||||||
|
'{{# comment}} \nfoo\n {{/comment}}'
|
||||||
|
),
|
||||||
|
block = ast.body[0];
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \nfoo\n ');
|
||||||
|
});
|
||||||
|
it('mustaches with children', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(
|
||||||
|
'{{# comment}} \n{{foo}}\n {{/comment}}'
|
||||||
|
),
|
||||||
|
block = ast.body[0];
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \n');
|
||||||
|
equals(block.program.body[1].path.original, 'foo');
|
||||||
|
equals(block.program.body[2].value, '\n ');
|
||||||
|
});
|
||||||
|
it('nested block mustaches', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(
|
||||||
|
'{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'
|
||||||
|
),
|
||||||
|
body = ast.body[0].program.body,
|
||||||
|
block = body[1];
|
||||||
|
|
||||||
|
equals(body[0].value, ' \n');
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \nfoo\n ');
|
||||||
|
equals(block.inverse.body[0].value, ' \n bar \n ');
|
||||||
|
});
|
||||||
|
it('column 0 block mustaches', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(
|
||||||
|
'test\n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
|
||||||
|
),
|
||||||
|
block = ast.body[1];
|
||||||
|
|
||||||
|
equals(ast.body[0].omit, undefined);
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \nfoo\n ');
|
||||||
|
equals(block.inverse.body[0].value, ' \n bar \n ');
|
||||||
|
|
||||||
|
equals(ast.body[2].value, ' ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('blocks', function() {
|
||||||
|
it('marks block mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(
|
||||||
|
' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
|
||||||
|
),
|
||||||
|
block = ast.body[1];
|
||||||
|
|
||||||
|
equals(ast.body[0].value, '');
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, 'foo\n');
|
||||||
|
equals(block.inverse.body[0].value, ' bar \n');
|
||||||
|
|
||||||
|
equals(ast.body[2].value, '');
|
||||||
|
});
|
||||||
|
it('marks initial block mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse('{{# comment}} \nfoo\n {{/comment}}'),
|
||||||
|
block = ast.body[0];
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, 'foo\n');
|
||||||
|
});
|
||||||
|
it('marks mustaches with children as standalone', function() {
|
||||||
|
var ast = Handlebars.parse('{{# comment}} \n{{foo}}\n {{/comment}}'),
|
||||||
|
block = ast.body[0];
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, '');
|
||||||
|
equals(block.program.body[1].path.original, 'foo');
|
||||||
|
equals(block.program.body[2].value, '\n');
|
||||||
|
});
|
||||||
|
it('marks nested block mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(
|
||||||
|
'{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'
|
||||||
|
),
|
||||||
|
body = ast.body[0].program.body,
|
||||||
|
block = body[1];
|
||||||
|
|
||||||
|
equals(body[0].value, '');
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, 'foo\n');
|
||||||
|
equals(block.inverse.body[0].value, ' bar \n');
|
||||||
|
|
||||||
|
equals(body[0].value, '');
|
||||||
|
});
|
||||||
|
it('does not mark nested block mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(
|
||||||
|
'{{#foo}} {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} {{/foo}}'
|
||||||
|
),
|
||||||
|
body = ast.body[0].program.body,
|
||||||
|
block = body[1];
|
||||||
|
|
||||||
|
equals(body[0].omit, undefined);
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \nfoo\n');
|
||||||
|
equals(block.inverse.body[0].value, ' bar \n ');
|
||||||
|
|
||||||
|
equals(body[0].omit, undefined);
|
||||||
|
});
|
||||||
|
it('does not mark nested initial block mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(
|
||||||
|
'{{#foo}}{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}}{{/foo}}'
|
||||||
|
),
|
||||||
|
body = ast.body[0].program.body,
|
||||||
|
block = body[0];
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, ' \nfoo\n');
|
||||||
|
equals(block.inverse.body[0].value, ' bar \n ');
|
||||||
|
|
||||||
|
equals(body[0].omit, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('marks column 0 block mustaches as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(
|
||||||
|
'test\n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '
|
||||||
|
),
|
||||||
|
block = ast.body[1];
|
||||||
|
|
||||||
|
equals(ast.body[0].omit, undefined);
|
||||||
|
|
||||||
|
equals(block.program.body[0].value, 'foo\n');
|
||||||
|
equals(block.inverse.body[0].value, ' bar \n');
|
||||||
|
|
||||||
|
equals(ast.body[2].value, '');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('partials - parseWithoutProcessing', function() {
|
||||||
|
it('simple partial', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing('{{> partial }} ');
|
||||||
|
equals(ast.body[1].value, ' ');
|
||||||
|
});
|
||||||
|
it('indented partial', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(' {{> partial }} ');
|
||||||
|
equals(ast.body[0].value, ' ');
|
||||||
|
equals(ast.body[1].indent, '');
|
||||||
|
equals(ast.body[2].value, ' ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('partials', function() {
|
||||||
|
it('marks partial as standalone', function() {
|
||||||
|
var ast = Handlebars.parse('{{> partial }} ');
|
||||||
|
equals(ast.body[1].value, '');
|
||||||
|
});
|
||||||
|
it('marks indented partial as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(' {{> partial }} ');
|
||||||
|
equals(ast.body[0].value, '');
|
||||||
|
equals(ast.body[1].indent, ' ');
|
||||||
|
equals(ast.body[2].value, '');
|
||||||
|
});
|
||||||
|
it('marks those around content as not standalone', function() {
|
||||||
|
var ast = Handlebars.parse('a{{> partial }}');
|
||||||
|
equals(ast.body[0].omit, undefined);
|
||||||
|
|
||||||
|
ast = Handlebars.parse('{{> partial }}a');
|
||||||
|
equals(ast.body[1].omit, undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('comments - parseWithoutProcessing', function() {
|
||||||
|
it('simple comment', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing('{{! comment }} ');
|
||||||
|
equals(ast.body[1].value, ' ');
|
||||||
|
});
|
||||||
|
it('indented comment', function() {
|
||||||
|
var ast = Handlebars.parseWithoutProcessing(' {{! comment }} ');
|
||||||
|
equals(ast.body[0].value, ' ');
|
||||||
|
equals(ast.body[2].value, ' ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('comments', function() {
|
||||||
|
it('marks comment as standalone', function() {
|
||||||
|
var ast = Handlebars.parse('{{! comment }} ');
|
||||||
|
equals(ast.body[1].value, '');
|
||||||
|
});
|
||||||
|
it('marks indented comment as standalone', function() {
|
||||||
|
var ast = Handlebars.parse(' {{! comment }} ');
|
||||||
|
equals(ast.body[0].value, '');
|
||||||
|
equals(ast.body[2].value, '');
|
||||||
|
});
|
||||||
|
it('marks those around content as not standalone', function() {
|
||||||
|
var ast = Handlebars.parse('a{{! comment }}');
|
||||||
|
equals(ast.body[0].omit, undefined);
|
||||||
|
|
||||||
|
ast = Handlebars.parse('{{! comment }}a');
|
||||||
|
equals(ast.body[1].omit, undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+71
-50
@@ -1,6 +1,14 @@
|
|||||||
|
global.handlebarsEnv = null;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
global.handlebarsEnv = Handlebars.create();
|
||||||
|
});
|
||||||
|
|
||||||
describe('basic context', function() {
|
describe('basic context', function() {
|
||||||
it('most basic', function() {
|
it('most basic', function() {
|
||||||
expectTemplate('{{foo}}').withInput({ foo: 'foo' }).toCompileTo('foo');
|
expectTemplate('{{foo}}')
|
||||||
|
.withInput({ foo: 'foo' })
|
||||||
|
.toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('escaping', function() {
|
it('escaping', function() {
|
||||||
@@ -29,14 +37,16 @@ describe('basic context', function () {
|
|||||||
expectTemplate('Goodbye\n{{cruel}}\n{{world}}!')
|
expectTemplate('Goodbye\n{{cruel}}\n{{world}}!')
|
||||||
.withInput({
|
.withInput({
|
||||||
cruel: 'cruel',
|
cruel: 'cruel',
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('It works if all the required keys are provided')
|
.withMessage('It works if all the required keys are provided')
|
||||||
.toCompileTo('Goodbye\ncruel\nworld!');
|
.toCompileTo('Goodbye\ncruel\nworld!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('compiling with a string context', function() {
|
it('compiling with a string context', function() {
|
||||||
expectTemplate('{{.}}{{length}}').withInput('bye').toCompileTo('bye3');
|
expectTemplate('{{.}}{{length}}')
|
||||||
|
.withInput('bye')
|
||||||
|
.toCompileTo('bye3');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('compiling with an undefined context', function() {
|
it('compiling with an undefined context', function() {
|
||||||
@@ -53,7 +63,7 @@ describe('basic context', function () {
|
|||||||
expectTemplate('{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!')
|
expectTemplate('{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!')
|
||||||
.withInput({
|
.withInput({
|
||||||
cruel: 'cruel',
|
cruel: 'cruel',
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('comments are ignored')
|
.withMessage('comments are ignored')
|
||||||
.toCompileTo('Goodbye\ncruel\nworld!');
|
.toCompileTo('Goodbye\ncruel\nworld!');
|
||||||
@@ -82,7 +92,7 @@ describe('basic context', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: true,
|
goodbye: true,
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('booleans show the contents when true')
|
.withMessage('booleans show the contents when true')
|
||||||
.toCompileTo('GOODBYE cruel world!');
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
@@ -90,7 +100,7 @@ describe('basic context', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: false,
|
goodbye: false,
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('booleans do not show the contents when false')
|
.withMessage('booleans do not show the contents when false')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -100,11 +110,13 @@ describe('basic context', function () {
|
|||||||
expectTemplate('num1: {{num1}}, num2: {{num2}}')
|
expectTemplate('num1: {{num1}}, num2: {{num2}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
num1: 42,
|
num1: 42,
|
||||||
num2: 0,
|
num2: 0
|
||||||
})
|
})
|
||||||
.toCompileTo('num1: 42, num2: 0');
|
.toCompileTo('num1: 42, num2: 0');
|
||||||
|
|
||||||
expectTemplate('num: {{.}}').withInput(0).toCompileTo('num: 0');
|
expectTemplate('num: {{.}}')
|
||||||
|
.withInput(0)
|
||||||
|
.toCompileTo('num: 0');
|
||||||
|
|
||||||
expectTemplate('num: {{num1/num2}}')
|
expectTemplate('num: {{num1/num2}}')
|
||||||
.withInput({ num1: { num2: 0 } })
|
.withInput({ num1: { num2: 0 } })
|
||||||
@@ -116,11 +128,13 @@ describe('basic context', function () {
|
|||||||
expectTemplate('val1: {{val1}}, val2: {{val2}}')
|
expectTemplate('val1: {{val1}}, val2: {{val2}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
val1: false,
|
val1: false,
|
||||||
val2: new Boolean(false),
|
val2: new Boolean(false)
|
||||||
})
|
})
|
||||||
.toCompileTo('val1: false, val2: false');
|
.toCompileTo('val1: false, val2: false');
|
||||||
|
|
||||||
expectTemplate('val: {{.}}').withInput(false).toCompileTo('val: false');
|
expectTemplate('val: {{.}}')
|
||||||
|
.withInput(false)
|
||||||
|
.toCompileTo('val: false');
|
||||||
|
|
||||||
expectTemplate('val: {{val1/val2}}')
|
expectTemplate('val: {{val1/val2}}')
|
||||||
.withInput({ val1: { val2: false } })
|
.withInput({ val1: { val2: false } })
|
||||||
@@ -129,7 +143,7 @@ describe('basic context', function () {
|
|||||||
expectTemplate('val1: {{{val1}}}, val2: {{{val2}}}')
|
expectTemplate('val1: {{{val1}}}, val2: {{{val2}}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
val1: false,
|
val1: false,
|
||||||
val2: new Boolean(false),
|
val2: new Boolean(false)
|
||||||
})
|
})
|
||||||
.toCompileTo('val1: false, val2: false');
|
.toCompileTo('val1: false, val2: false');
|
||||||
|
|
||||||
@@ -150,7 +164,7 @@ describe('basic context', function () {
|
|||||||
' ' +
|
' ' +
|
||||||
typeof options
|
typeof options
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('true true object');
|
.toCompileTo('true true object');
|
||||||
|
|
||||||
@@ -158,7 +172,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
undefined: function() {
|
undefined: function() {
|
||||||
return 'undefined!';
|
return 'undefined!';
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('undefined!');
|
.toCompileTo('undefined!');
|
||||||
|
|
||||||
@@ -166,7 +180,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
null: function() {
|
null: function() {
|
||||||
return 'null!';
|
return 'null!';
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('null!');
|
.toCompileTo('null!');
|
||||||
});
|
});
|
||||||
@@ -229,7 +243,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return new Handlebars.SafeString("&'\\<>");
|
return new Handlebars.SafeString("&'\\<>");
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage("functions returning safestrings aren't escaped")
|
.withMessage("functions returning safestrings aren't escaped")
|
||||||
.toCompileTo("&'\\<>");
|
.toCompileTo("&'\\<>");
|
||||||
@@ -240,7 +254,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return 'Awesome';
|
return 'Awesome';
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage('functions are called and render their output')
|
.withMessage('functions are called and render their output')
|
||||||
.toCompileTo('Awesome');
|
.toCompileTo('Awesome');
|
||||||
@@ -250,7 +264,7 @@ describe('basic context', function () {
|
|||||||
awesome: function() {
|
awesome: function() {
|
||||||
return this.more;
|
return this.more;
|
||||||
},
|
},
|
||||||
more: 'More awesome',
|
more: 'More awesome'
|
||||||
})
|
})
|
||||||
.withMessage('functions are bound to the context')
|
.withMessage('functions are bound to the context')
|
||||||
.toCompileTo('More awesome');
|
.toCompileTo('More awesome');
|
||||||
@@ -262,7 +276,7 @@ describe('basic context', function () {
|
|||||||
awesome: function(context) {
|
awesome: function(context) {
|
||||||
return context;
|
return context;
|
||||||
},
|
},
|
||||||
frank: 'Frank',
|
frank: 'Frank'
|
||||||
})
|
})
|
||||||
.withMessage('functions are called with context arguments')
|
.withMessage('functions are called with context arguments')
|
||||||
.toCompileTo('Frank');
|
.toCompileTo('Frank');
|
||||||
@@ -274,9 +288,9 @@ describe('basic context', function () {
|
|||||||
bar: {
|
bar: {
|
||||||
awesome: function(context) {
|
awesome: function(context) {
|
||||||
return context;
|
return context;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
frank: 'Frank'
|
||||||
frank: 'Frank',
|
|
||||||
})
|
})
|
||||||
.withMessage('functions are called with context arguments')
|
.withMessage('functions are called with context arguments')
|
||||||
.toCompileTo('Frank');
|
.toCompileTo('Frank');
|
||||||
@@ -288,7 +302,7 @@ describe('basic context', function () {
|
|||||||
awesome: function(context) {
|
awesome: function(context) {
|
||||||
return context;
|
return context;
|
||||||
},
|
},
|
||||||
frank: 'Frank',
|
frank: 'Frank'
|
||||||
})
|
})
|
||||||
.withMessage('functions are called with context arguments')
|
.withMessage('functions are called with context arguments')
|
||||||
.toCompileTo('Frank');
|
.toCompileTo('Frank');
|
||||||
@@ -299,7 +313,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
awesome: function(context, options) {
|
awesome: function(context, options) {
|
||||||
return options.fn(context);
|
return options.fn(context);
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage('block functions are called with context and options')
|
.withMessage('block functions are called with context and options')
|
||||||
.toCompileTo('inner 1');
|
.toCompileTo('inner 1');
|
||||||
@@ -313,7 +327,7 @@ describe('basic context', function () {
|
|||||||
value: true,
|
value: true,
|
||||||
awesome: function(context, options) {
|
awesome: function(context, options) {
|
||||||
return options.fn(context);
|
return options.fn(context);
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage('block functions are called with context and options')
|
.withMessage('block functions are called with context and options')
|
||||||
.toCompileTo('inner 1');
|
.toCompileTo('inner 1');
|
||||||
@@ -324,7 +338,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
awesome: function(options) {
|
awesome: function(options) {
|
||||||
return options.fn(this);
|
return options.fn(this);
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage('block functions are called with options')
|
.withMessage('block functions are called with options')
|
||||||
.toCompileTo('inner');
|
.toCompileTo('inner');
|
||||||
@@ -336,8 +350,8 @@ describe('basic context', function () {
|
|||||||
foo: {
|
foo: {
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return this;
|
return this;
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage('block functions are called with options')
|
.withMessage('block functions are called with options')
|
||||||
.toCompileTo('inner');
|
.toCompileTo('inner');
|
||||||
@@ -351,7 +365,7 @@ describe('basic context', function () {
|
|||||||
value: true,
|
value: true,
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return this;
|
return this;
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withMessage('block functions are called with options')
|
.withMessage('block functions are called with options')
|
||||||
.toCompileTo('inner');
|
.toCompileTo('inner');
|
||||||
@@ -381,13 +395,6 @@ describe('basic context', function () {
|
|||||||
.toCompileTo('Goodbye beautiful world!');
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nested paths with Map', function () {
|
|
||||||
expectTemplate('Goodbye {{alan/expression}} world!')
|
|
||||||
.withInput({ alan: new Map([['expression', 'beautiful']]) })
|
|
||||||
.withMessage('Nested paths access nested objects')
|
|
||||||
.toCompileTo('Goodbye beautiful world!');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('nested paths with empty string value', function() {
|
it('nested paths with empty string value', function() {
|
||||||
expectTemplate('Goodbye {{alan/expression}} world!')
|
expectTemplate('Goodbye {{alan/expression}} world!')
|
||||||
.withInput({ alan: { expression: '' } })
|
.withInput({ alan: { expression: '' } })
|
||||||
@@ -445,7 +452,9 @@ describe('basic context', function () {
|
|||||||
.withInput({ person: { name: null } })
|
.withInput({ person: { name: null } })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
|
|
||||||
expectTemplate('{{person/name}}').withInput({ person: {} }).toCompileTo('');
|
expectTemplate('{{person/name}}')
|
||||||
|
.withInput({ person: {} })
|
||||||
|
.toCompileTo('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('this keyword in paths', function() {
|
it('this keyword in paths', function() {
|
||||||
@@ -456,7 +465,7 @@ describe('basic context', function () {
|
|||||||
|
|
||||||
expectTemplate('{{#hellos}}{{this/text}}{{/hellos}}')
|
expectTemplate('{{#hellos}}{{this/text}}{{/hellos}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }],
|
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }]
|
||||||
})
|
})
|
||||||
.withMessage('This keyword evaluates in more complex paths')
|
.withMessage('This keyword evaluates in more complex paths')
|
||||||
.toCompileTo('helloHelloHELLO');
|
.toCompileTo('helloHelloHELLO');
|
||||||
@@ -468,7 +477,9 @@ describe('basic context', function () {
|
|||||||
'Invalid path: text/this - 1:13'
|
'Invalid path: text/this - 1:13'
|
||||||
);
|
);
|
||||||
|
|
||||||
expectTemplate('{{[this]}}').withInput({ this: 'bar' }).toCompileTo('bar');
|
expectTemplate('{{[this]}}')
|
||||||
|
.withInput({ this: 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
expectTemplate('{{text/[this]}}')
|
expectTemplate('{{text/[this]}}')
|
||||||
.withInput({ text: { this: 'bar' } })
|
.withInput({ text: { this: 'bar' } })
|
||||||
@@ -479,7 +490,7 @@ describe('basic context', function () {
|
|||||||
var helpers = {
|
var helpers = {
|
||||||
foo: function(value) {
|
foo: function(value) {
|
||||||
return 'bar ' + value;
|
return 'bar ' + value;
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
expectTemplate('{{#goodbyes}}{{foo this}}{{/goodbyes}}')
|
expectTemplate('{{#goodbyes}}{{foo this}}{{/goodbyes}}')
|
||||||
@@ -490,7 +501,7 @@ describe('basic context', function () {
|
|||||||
|
|
||||||
expectTemplate('{{#hellos}}{{foo this/text}}{{/hellos}}')
|
expectTemplate('{{#hellos}}{{foo this/text}}{{/hellos}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }],
|
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }]
|
||||||
})
|
})
|
||||||
.withHelpers(helpers)
|
.withHelpers(helpers)
|
||||||
.withMessage('This keyword evaluates in more complex paths')
|
.withMessage('This keyword evaluates in more complex paths')
|
||||||
@@ -508,7 +519,7 @@ describe('basic context', function () {
|
|||||||
foo: function(value) {
|
foo: function(value) {
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
this: 'bar',
|
this: 'bar'
|
||||||
})
|
})
|
||||||
.toCompileTo('bar');
|
.toCompileTo('bar');
|
||||||
|
|
||||||
@@ -517,7 +528,7 @@ describe('basic context', function () {
|
|||||||
foo: function(value) {
|
foo: function(value) {
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
text: { this: 'bar' },
|
text: { this: 'bar' }
|
||||||
})
|
})
|
||||||
.toCompileTo('bar');
|
.toCompileTo('bar');
|
||||||
});
|
});
|
||||||
@@ -525,11 +536,13 @@ describe('basic context', function () {
|
|||||||
it('pass string literals', function() {
|
it('pass string literals', function() {
|
||||||
expectTemplate('{{"foo"}}').toCompileTo('');
|
expectTemplate('{{"foo"}}').toCompileTo('');
|
||||||
|
|
||||||
expectTemplate('{{"foo"}}').withInput({ foo: 'bar' }).toCompileTo('bar');
|
expectTemplate('{{"foo"}}')
|
||||||
|
.withInput({ foo: 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
expectTemplate('{{#"foo"}}{{.}}{{/"foo"}}')
|
expectTemplate('{{#"foo"}}{{.}}{{/"foo"}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
foo: ['bar', 'baz'],
|
foo: ['bar', 'baz']
|
||||||
})
|
})
|
||||||
.toCompileTo('barbaz');
|
.toCompileTo('barbaz');
|
||||||
});
|
});
|
||||||
@@ -537,17 +550,21 @@ describe('basic context', function () {
|
|||||||
it('pass number literals', function() {
|
it('pass number literals', function() {
|
||||||
expectTemplate('{{12}}').toCompileTo('');
|
expectTemplate('{{12}}').toCompileTo('');
|
||||||
|
|
||||||
expectTemplate('{{12}}').withInput({ 12: 'bar' }).toCompileTo('bar');
|
expectTemplate('{{12}}')
|
||||||
|
.withInput({ '12': 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
expectTemplate('{{12.34}}').toCompileTo('');
|
expectTemplate('{{12.34}}').toCompileTo('');
|
||||||
|
|
||||||
expectTemplate('{{12.34}}').withInput({ 12.34: 'bar' }).toCompileTo('bar');
|
expectTemplate('{{12.34}}')
|
||||||
|
.withInput({ '12.34': 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
expectTemplate('{{12.34 1}}')
|
expectTemplate('{{12.34 1}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
12.34: function (arg) {
|
'12.34': function(arg) {
|
||||||
return 'bar' + arg;
|
return 'bar' + arg;
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('bar1');
|
.toCompileTo('bar1');
|
||||||
});
|
});
|
||||||
@@ -555,9 +572,13 @@ describe('basic context', function () {
|
|||||||
it('pass boolean literals', function() {
|
it('pass boolean literals', function() {
|
||||||
expectTemplate('{{true}}').toCompileTo('');
|
expectTemplate('{{true}}').toCompileTo('');
|
||||||
|
|
||||||
expectTemplate('{{true}}').withInput({ '': 'foo' }).toCompileTo('');
|
expectTemplate('{{true}}')
|
||||||
|
.withInput({ '': 'foo' })
|
||||||
|
.toCompileTo('');
|
||||||
|
|
||||||
expectTemplate('{{false}}').withInput({ false: 'foo' }).toCompileTo('foo');
|
expectTemplate('{{false}}')
|
||||||
|
.withInput({ false: 'foo' })
|
||||||
|
.toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle literals in subexpression', function() {
|
it('should handle literals in subexpression', function() {
|
||||||
@@ -565,7 +586,7 @@ describe('basic context', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
false: function() {
|
false: function() {
|
||||||
return 'bar';
|
return 'bar';
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.withHelper('foo', function(arg) {
|
.withHelper('foo', function(arg) {
|
||||||
return arg;
|
return arg;
|
||||||
|
|||||||
+40
-36
@@ -7,9 +7,9 @@ describe('blocks', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('Arrays iterate over the contents when not empty')
|
.withMessage('Arrays iterate over the contents when not empty')
|
||||||
.toCompileTo('goodbye! Goodbye! GOODBYE! cruel world!');
|
.toCompileTo('goodbye! Goodbye! GOODBYE! cruel world!');
|
||||||
@@ -17,7 +17,7 @@ describe('blocks', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: [],
|
goodbyes: [],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('Arrays ignore the contents when empty')
|
.withMessage('Arrays ignore the contents when empty')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -31,9 +31,9 @@ describe('blocks', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withCompileOptions({ compat: false })
|
.withCompileOptions({ compat: false })
|
||||||
.toCompileTo('goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE');
|
.toCompileTo('goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE');
|
||||||
@@ -47,9 +47,9 @@ describe('blocks', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @index variable is used')
|
.withMessage('The @index variable is used')
|
||||||
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
||||||
@@ -63,9 +63,9 @@ describe('blocks', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('Arrays iterate over the contents when not empty')
|
.withMessage('Arrays iterate over the contents when not empty')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -73,7 +73,7 @@ describe('blocks', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: [],
|
goodbyes: [],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('Arrays ignore the contents when empty')
|
.withMessage('Arrays ignore the contents when empty')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -86,8 +86,8 @@ describe('blocks', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
]
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'Templates can access variables in contexts up the stack with relative path syntax'
|
'Templates can access variables in contexts up the stack with relative path syntax'
|
||||||
@@ -104,8 +104,8 @@ describe('blocks', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
]
|
||||||
})
|
})
|
||||||
.toCompileTo('AlanAlanAlanAlanAlanAlan');
|
.toCompileTo('AlanAlanAlanAlanAlanAlan');
|
||||||
});
|
});
|
||||||
@@ -122,7 +122,7 @@ describe('blocks', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
omg: 'OMG!',
|
omg: 'OMG!',
|
||||||
outer: [{ sibling: 'sad', inner: [{ text: 'goodbye' }] }],
|
outer: [{ sibling: 'sad', inner: [{ text: 'goodbye' }] }]
|
||||||
})
|
})
|
||||||
.toCompileTo('Goodbye cruel sad OMG!');
|
.toCompileTo('Goodbye cruel sad OMG!');
|
||||||
});
|
});
|
||||||
@@ -135,8 +135,8 @@ describe('blocks', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
person: [
|
person: [
|
||||||
{ first: 'Alan', last: 'Johnson' },
|
{ first: 'Alan', last: 'Johnson' },
|
||||||
{ first: 'Alan', last: 'Johnson' },
|
{ first: 'Alan', last: 'Johnson' }
|
||||||
],
|
]
|
||||||
})
|
})
|
||||||
.toCompileTo('Alan JohnsonAlan Johnson');
|
.toCompileTo('Alan JohnsonAlan Johnson');
|
||||||
});
|
});
|
||||||
@@ -202,7 +202,7 @@ describe('blocks', function () {
|
|||||||
expectTemplate('{{#people}}{{name}}{{^}}{{none}}{{/people}}')
|
expectTemplate('{{#people}}{{name}}{{^}}{{none}}{{/people}}')
|
||||||
.withInput({
|
.withInput({
|
||||||
none: 'No people',
|
none: 'No people',
|
||||||
people: [],
|
people: []
|
||||||
})
|
})
|
||||||
.toCompileTo('No people');
|
.toCompileTo('No people');
|
||||||
});
|
});
|
||||||
@@ -252,7 +252,7 @@ describe('blocks', function () {
|
|||||||
it('should handle nesting', function() {
|
it('should handle nesting', function() {
|
||||||
expectTemplate('{{#data}}\n{{#if true}}\n{{.}}\n{{/if}}\n{{/data}}\nOK.')
|
expectTemplate('{{#data}}\n{{#if true}}\n{{.}}\n{{/if}}\n{{/data}}\nOK.')
|
||||||
.withInput({
|
.withInput({
|
||||||
data: [1, 3, 5],
|
data: [1, 3, 5]
|
||||||
})
|
})
|
||||||
.toCompileTo('1\n3\n5\nOK.');
|
.toCompileTo('1\n3\n5\nOK.');
|
||||||
});
|
});
|
||||||
@@ -274,7 +274,7 @@ describe('blocks', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
omg: { yes: 'OMG!' },
|
omg: { yes: 'OMG!' },
|
||||||
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }],
|
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }]
|
||||||
})
|
})
|
||||||
.withCompileOptions({ compat: true })
|
.withCompileOptions({ compat: true })
|
||||||
.toCompileTo('Goodbye cruel OMG!');
|
.toCompileTo('Goodbye cruel OMG!');
|
||||||
@@ -286,7 +286,7 @@ describe('blocks', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
omg: { no: 'OMG!' },
|
omg: { no: 'OMG!' },
|
||||||
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }],
|
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }]
|
||||||
})
|
})
|
||||||
.withCompileOptions({ compat: true })
|
.withCompileOptions({ compat: true })
|
||||||
.toCompileTo('Goodbye cruel ');
|
.toCompileTo('Goodbye cruel ');
|
||||||
@@ -345,7 +345,7 @@ describe('blocks', function () {
|
|||||||
},
|
},
|
||||||
nested: function(fn, props, container, options) {
|
nested: function(fn, props, container, options) {
|
||||||
props.nested = options.fn();
|
props.nested = options.fn();
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('success');
|
.toCompileTo('success');
|
||||||
});
|
});
|
||||||
@@ -381,26 +381,26 @@ describe('blocks', function () {
|
|||||||
var run;
|
var run;
|
||||||
expectTemplate('{{*decorator "success"}}')
|
expectTemplate('{{*decorator "success"}}')
|
||||||
.withDecorator('decorator', function(fn, props, container, options) {
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
expect(options.args[0]).toBe('success');
|
equals(options.args[0], 'success');
|
||||||
run = true;
|
run = true;
|
||||||
return fn;
|
return fn;
|
||||||
})
|
})
|
||||||
.withInput({ foo: 'success' })
|
.withInput({ foo: 'success' })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(run).toBe(true);
|
equals(run, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when accessing variables from root', function() {
|
it('should fail when accessing variables from root', function() {
|
||||||
var run;
|
var run;
|
||||||
expectTemplate('{{*decorator foo}}')
|
expectTemplate('{{*decorator foo}}')
|
||||||
.withDecorator('decorator', function(fn, props, container, options) {
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
expect(options.args[0]).toBeUndefined();
|
equals(options.args[0], undefined);
|
||||||
run = true;
|
run = true;
|
||||||
return fn;
|
return fn;
|
||||||
})
|
})
|
||||||
.withInput({ foo: 'fail' })
|
.withInput({ foo: 'fail' })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(run).toBe(true);
|
equals(run, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('registration', function() {
|
describe('registration', function() {
|
||||||
@@ -411,9 +411,9 @@ describe('blocks', function () {
|
|||||||
return 'fail';
|
return 'fail';
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(handlebarsEnv.decorators.foo).toBeTruthy();
|
equals(!!handlebarsEnv.decorators.foo, true);
|
||||||
handlebarsEnv.unregisterDecorator('foo');
|
handlebarsEnv.unregisterDecorator('foo');
|
||||||
expect(handlebarsEnv.decorators.foo).toBeUndefined();
|
equals(handlebarsEnv.decorators.foo, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows multiple globals', function() {
|
it('allows multiple globals', function() {
|
||||||
@@ -421,19 +421,20 @@ describe('blocks', function () {
|
|||||||
|
|
||||||
handlebarsEnv.registerDecorator({
|
handlebarsEnv.registerDecorator({
|
||||||
foo: function() {},
|
foo: function() {},
|
||||||
bar: function () {},
|
bar: function() {}
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(handlebarsEnv.decorators.foo).toBeTruthy();
|
equals(!!handlebarsEnv.decorators.foo, true);
|
||||||
expect(handlebarsEnv.decorators.bar).toBeTruthy();
|
equals(!!handlebarsEnv.decorators.bar, true);
|
||||||
handlebarsEnv.unregisterDecorator('foo');
|
handlebarsEnv.unregisterDecorator('foo');
|
||||||
handlebarsEnv.unregisterDecorator('bar');
|
handlebarsEnv.unregisterDecorator('bar');
|
||||||
expect(handlebarsEnv.decorators.foo).toBeUndefined();
|
equals(handlebarsEnv.decorators.foo, undefined);
|
||||||
expect(handlebarsEnv.decorators.bar).toBeUndefined();
|
equals(handlebarsEnv.decorators.bar, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fails with multiple and args', function() {
|
it('fails with multiple and args', function() {
|
||||||
expect(function () {
|
shouldThrow(
|
||||||
|
function() {
|
||||||
handlebarsEnv.registerDecorator(
|
handlebarsEnv.registerDecorator(
|
||||||
{
|
{
|
||||||
world: function() {
|
world: function() {
|
||||||
@@ -441,11 +442,14 @@ describe('blocks', function () {
|
|||||||
},
|
},
|
||||||
testHelper: function() {
|
testHelper: function() {
|
||||||
return 'found it!';
|
return 'found it!';
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
}).toThrow('Arg not supported with multiple decorators');
|
},
|
||||||
|
Error,
|
||||||
|
'Arg not supported with multiple decorators'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+84
-126
@@ -6,7 +6,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: true,
|
goodbye: true,
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('if with boolean argument shows the contents when true')
|
.withMessage('if with boolean argument shows the contents when true')
|
||||||
.toCompileTo('GOODBYE cruel world!');
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
@@ -14,7 +14,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: 'dummy',
|
goodbye: 'dummy',
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('if with string argument shows the contents')
|
.withMessage('if with string argument shows the contents')
|
||||||
.toCompileTo('GOODBYE cruel world!');
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
@@ -22,7 +22,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: false,
|
goodbye: false,
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'if with boolean argument does not show the contents when false'
|
'if with boolean argument does not show the contents when false'
|
||||||
@@ -37,7 +37,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: ['foo'],
|
goodbye: ['foo'],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('if with non-empty array shows the contents')
|
.withMessage('if with non-empty array shows the contents')
|
||||||
.toCompileTo('GOODBYE cruel world!');
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
@@ -45,7 +45,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: [],
|
goodbye: [],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('if with empty array does not show the contents')
|
.withMessage('if with empty array does not show the contents')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -53,7 +53,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: 0,
|
goodbye: 0,
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('if with zero does not show the contents')
|
.withMessage('if with zero does not show the contents')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -63,7 +63,7 @@ describe('builtin helpers', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbye: 0,
|
goodbye: 0,
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('if with zero does not show the contents')
|
.withMessage('if with zero does not show the contents')
|
||||||
.toCompileTo('GOODBYE cruel world!');
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
@@ -77,7 +77,7 @@ describe('builtin helpers', function () {
|
|||||||
goodbye: function() {
|
goodbye: function() {
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'if with function shows the contents when function returns true'
|
'if with function shows the contents when function returns true'
|
||||||
@@ -89,7 +89,7 @@ describe('builtin helpers', function () {
|
|||||||
goodbye: function() {
|
goodbye: function() {
|
||||||
return this.world;
|
return this.world;
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'if with function shows the contents when function returns string'
|
'if with function shows the contents when function returns string'
|
||||||
@@ -101,7 +101,7 @@ describe('builtin helpers', function () {
|
|||||||
goodbye: function() {
|
goodbye: function() {
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'if with function does not show the contents when returns false'
|
'if with function does not show the contents when returns false'
|
||||||
@@ -113,7 +113,7 @@ describe('builtin helpers', function () {
|
|||||||
goodbye: function() {
|
goodbye: function() {
|
||||||
return this.foo;
|
return this.foo;
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'if with function does not show the contents when returns undefined'
|
'if with function does not show the contents when returns undefined'
|
||||||
@@ -127,7 +127,7 @@ describe('builtin helpers', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
foo: { goodbye: true },
|
foo: { goodbye: true },
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.toCompileTo('GOODBYE cruel world!');
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
});
|
});
|
||||||
@@ -139,8 +139,8 @@ describe('builtin helpers', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
person: {
|
person: {
|
||||||
first: 'Alan',
|
first: 'Alan',
|
||||||
last: 'Johnson',
|
last: 'Johnson'
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('Alan Johnson');
|
.toCompileTo('Alan Johnson');
|
||||||
});
|
});
|
||||||
@@ -151,9 +151,9 @@ describe('builtin helpers', function () {
|
|||||||
person: function() {
|
person: function() {
|
||||||
return {
|
return {
|
||||||
first: 'Alan',
|
first: 'Alan',
|
||||||
last: 'Johnson',
|
last: 'Johnson'
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('Alan Johnson');
|
.toCompileTo('Alan Johnson');
|
||||||
});
|
});
|
||||||
@@ -169,8 +169,8 @@ describe('builtin helpers', function () {
|
|||||||
.withInput({
|
.withInput({
|
||||||
person: {
|
person: {
|
||||||
first: 'Alan',
|
first: 'Alan',
|
||||||
last: 'Johnson',
|
last: 'Johnson'
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('Alan Johnson');
|
.toCompileTo('Alan Johnson');
|
||||||
});
|
});
|
||||||
@@ -198,9 +198,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'each with array argument iterates over the contents when not empty'
|
'each with array argument iterates over the contents when not empty'
|
||||||
@@ -210,7 +210,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: [],
|
goodbyes: [],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('each with array argument ignores the contents when empty')
|
.withMessage('each with array argument ignores the contents when empty')
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
@@ -222,9 +222,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withRuntimeOptions({ data: false })
|
.withRuntimeOptions({ data: false })
|
||||||
.withCompileOptions({ data: false })
|
.withCompileOptions({ data: false })
|
||||||
@@ -257,18 +257,22 @@ describe('builtin helpers', function () {
|
|||||||
// Object property iteration order is undefined according to ECMA spec,
|
// Object property iteration order is undefined according to ECMA spec,
|
||||||
// so we need to check both possible orders
|
// so we need to check both possible orders
|
||||||
// @see http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop
|
// @see http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop
|
||||||
var actual = CompilerContext.compile(string)(hash);
|
var actual = compileWithPartials(string, hash);
|
||||||
var expected1 =
|
var expected1 =
|
||||||
'<b>#1</b>. goodbye! 2. GOODBYE! cruel world!';
|
'<b>#1</b>. goodbye! 2. GOODBYE! cruel world!';
|
||||||
var expected2 =
|
var expected2 =
|
||||||
'2. GOODBYE! <b>#1</b>. goodbye! cruel world!';
|
'2. GOODBYE! <b>#1</b>. goodbye! cruel world!';
|
||||||
|
|
||||||
expect([expected1, expected2]).toContain(actual);
|
equals(
|
||||||
|
actual === expected1 || actual === expected2,
|
||||||
|
true,
|
||||||
|
'each with object argument iterates over the contents when not empty'
|
||||||
|
);
|
||||||
|
|
||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: {},
|
goodbyes: {},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.toCompileTo('cruel world!');
|
.toCompileTo('cruel world!');
|
||||||
});
|
});
|
||||||
@@ -281,9 +285,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @index variable is used')
|
.withMessage('The @index variable is used')
|
||||||
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
||||||
@@ -297,9 +301,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @index variable is used')
|
.withMessage('The @index variable is used')
|
||||||
.toCompileTo(
|
.toCompileTo(
|
||||||
@@ -313,7 +317,7 @@ describe('builtin helpers', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }],
|
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.toCompileTo(
|
.toCompileTo(
|
||||||
'0. goodbye! 0 0 0 1 After 0 1. Goodbye! 1 0 1 1 After 1 cruel world!'
|
'0. goodbye! 0 0 0 1 After 0 1. Goodbye! 1 0 1 1 After 1 cruel world!'
|
||||||
@@ -337,9 +341,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: {
|
goodbyes: {
|
||||||
a: { text: 'goodbye' },
|
a: { text: 'goodbye' },
|
||||||
b: { text: 'Goodbye' },
|
b: { text: 'Goodbye' },
|
||||||
c: { text: 'GOODBYE' },
|
c: { text: 'GOODBYE' }
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @index variable is used')
|
.withMessage('The @index variable is used')
|
||||||
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
||||||
@@ -353,9 +357,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @first variable is used')
|
.withMessage('The @first variable is used')
|
||||||
.toCompileTo('goodbye! cruel world!');
|
.toCompileTo('goodbye! cruel world!');
|
||||||
@@ -369,9 +373,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @first variable is used')
|
.withMessage('The @first variable is used')
|
||||||
.toCompileTo(
|
.toCompileTo(
|
||||||
@@ -385,7 +389,7 @@ describe('builtin helpers', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: { foo: { text: 'goodbye' }, bar: { text: 'Goodbye' } },
|
goodbyes: { foo: { text: 'goodbye' }, bar: { text: 'Goodbye' } },
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @first variable is used')
|
.withMessage('The @first variable is used')
|
||||||
.toCompileTo('goodbye! cruel world!');
|
.toCompileTo('goodbye! cruel world!');
|
||||||
@@ -399,9 +403,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @last variable is used')
|
.withMessage('The @last variable is used')
|
||||||
.toCompileTo('GOODBYE! cruel world!');
|
.toCompileTo('GOODBYE! cruel world!');
|
||||||
@@ -413,7 +417,7 @@ describe('builtin helpers', function () {
|
|||||||
)
|
)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: { foo: { text: 'goodbye' }, bar: { text: 'Goodbye' } },
|
goodbyes: { foo: { text: 'goodbye' }, bar: { text: 'Goodbye' } },
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @last variable is used')
|
.withMessage('The @last variable is used')
|
||||||
.toCompileTo('Goodbye! cruel world!');
|
.toCompileTo('Goodbye! cruel world!');
|
||||||
@@ -427,9 +431,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: [
|
goodbyes: [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
],
|
],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('The @last variable is used')
|
.withMessage('The @last variable is used')
|
||||||
.toCompileTo(
|
.toCompileTo(
|
||||||
@@ -446,10 +450,10 @@ describe('builtin helpers', function () {
|
|||||||
return [
|
return [
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'each with array function argument iterates over the contents when not empty'
|
'each with array function argument iterates over the contents when not empty'
|
||||||
@@ -459,7 +463,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: [],
|
goodbyes: [],
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'each with array function argument ignores the contents when empty'
|
'each with array function argument ignores the contents when empty'
|
||||||
@@ -475,9 +479,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: {
|
goodbyes: {
|
||||||
a: { text: 'goodbye' },
|
a: { text: 'goodbye' },
|
||||||
b: { text: 'Goodbye' },
|
b: { text: 'Goodbye' },
|
||||||
'': { text: 'GOODBYE' },
|
'': { text: 'GOODBYE' }
|
||||||
},
|
},
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage('Empty string key is not skipped')
|
.withMessage('Empty string key is not skipped')
|
||||||
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
||||||
@@ -491,8 +495,8 @@ describe('builtin helpers', function () {
|
|||||||
.withMessage('should output data')
|
.withMessage('should output data')
|
||||||
.withRuntimeOptions({
|
.withRuntimeOptions({
|
||||||
data: {
|
data: {
|
||||||
exclaim: '!',
|
exclaim: '!'
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('a!b!c!');
|
.toCompileTo('a!b!c!');
|
||||||
});
|
});
|
||||||
@@ -504,50 +508,6 @@ describe('builtin helpers', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('each on Map', function () {
|
|
||||||
var map = new Map([
|
|
||||||
[1, 'one'],
|
|
||||||
[2, 'two'],
|
|
||||||
[3, 'three'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expectTemplate('{{#each map}}{{@key}}(i{{@index}}) {{.}} {{/each}}')
|
|
||||||
.withInput({ map: map })
|
|
||||||
.toCompileTo('1(i0) one 2(i1) two 3(i2) three ');
|
|
||||||
|
|
||||||
expectTemplate('{{#each map}}{{#if @first}}{{.}}{{/if}}{{/each}}')
|
|
||||||
.withInput({ map: map })
|
|
||||||
.toCompileTo('one');
|
|
||||||
|
|
||||||
expectTemplate('{{#each map}}{{#if @last}}{{.}}{{/if}}{{/each}}')
|
|
||||||
.withInput({ map: map })
|
|
||||||
.toCompileTo('three');
|
|
||||||
|
|
||||||
expectTemplate('{{#each map}}{{.}}{{/each}}not-in-each')
|
|
||||||
.withInput({ map: new Map() })
|
|
||||||
.toCompileTo('not-in-each');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('each on Set', function () {
|
|
||||||
var set = new Set([1, 2, 3]);
|
|
||||||
|
|
||||||
expectTemplate('{{#each set}}{{@key}}(i{{@index}}) {{.}} {{/each}}')
|
|
||||||
.withInput({ set: set })
|
|
||||||
.toCompileTo('0(i0) 1 1(i1) 2 2(i2) 3 ');
|
|
||||||
|
|
||||||
expectTemplate('{{#each set}}{{#if @first}}{{.}}{{/if}}{{/each}}')
|
|
||||||
.withInput({ set: set })
|
|
||||||
.toCompileTo('1');
|
|
||||||
|
|
||||||
expectTemplate('{{#each set}}{{#if @last}}{{.}}{{/if}}{{/each}}')
|
|
||||||
.withInput({ set: set })
|
|
||||||
.toCompileTo('3');
|
|
||||||
|
|
||||||
expectTemplate('{{#each set}}{{.}}{{/each}}not-in-each')
|
|
||||||
.withInput({ set: new Set() })
|
|
||||||
.toCompileTo('not-in-each');
|
|
||||||
});
|
|
||||||
|
|
||||||
if (global.Symbol && global.Symbol.iterator) {
|
if (global.Symbol && global.Symbol.iterator) {
|
||||||
it('each on iterable', function() {
|
it('each on iterable', function() {
|
||||||
function Iterator(arr) {
|
function Iterator(arr) {
|
||||||
@@ -575,9 +535,9 @@ describe('builtin helpers', function () {
|
|||||||
goodbyes: new Iterable([
|
goodbyes: new Iterable([
|
||||||
{ text: 'goodbye' },
|
{ text: 'goodbye' },
|
||||||
{ text: 'Goodbye' },
|
{ text: 'Goodbye' },
|
||||||
{ text: 'GOODBYE' },
|
{ text: 'GOODBYE' }
|
||||||
]),
|
]),
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'each with array argument iterates over the contents when not empty'
|
'each with array argument iterates over the contents when not empty'
|
||||||
@@ -587,7 +547,7 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate(string)
|
expectTemplate(string)
|
||||||
.withInput({
|
.withInput({
|
||||||
goodbyes: new Iterable([]),
|
goodbyes: new Iterable([]),
|
||||||
world: 'world',
|
world: 'world'
|
||||||
})
|
})
|
||||||
.withMessage(
|
.withMessage(
|
||||||
'each with array argument ignores the contents when empty'
|
'each with array argument ignores the contents when empty'
|
||||||
@@ -626,8 +586,8 @@ describe('builtin helpers', function () {
|
|||||||
.withInput({ blah: 'whee' })
|
.withInput({ blah: 'whee' })
|
||||||
.withMessage('log should not display')
|
.withMessage('log should not display')
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(levelArg).toBe(1);
|
equals(1, levelArg, 'should call log with 1');
|
||||||
expect(logArg).toBe('whee');
|
equals('whee', logArg, "should call log with 'whee'");
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call logger at data level', function() {
|
it('should call logger at data level', function() {
|
||||||
@@ -642,21 +602,21 @@ describe('builtin helpers', function () {
|
|||||||
.withRuntimeOptions({ data: { level: '03' } })
|
.withRuntimeOptions({ data: { level: '03' } })
|
||||||
.withCompileOptions({ data: true })
|
.withCompileOptions({ data: true })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(levelArg).toBe('03');
|
equals('03', levelArg);
|
||||||
expect(logArg).toBe('whee');
|
equals('whee', logArg);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should output to info', function() {
|
it('should output to info', function() {
|
||||||
var called;
|
var called;
|
||||||
|
|
||||||
console.info = function(info) {
|
console.info = function(info) {
|
||||||
expect(info).toBe('whee');
|
equals('whee', info);
|
||||||
called = true;
|
called = true;
|
||||||
console.info = $info;
|
console.info = $info;
|
||||||
console.log = $log;
|
console.log = $log;
|
||||||
};
|
};
|
||||||
console.log = function(log) {
|
console.log = function(log) {
|
||||||
expect(log).toBe('whee');
|
equals('whee', log);
|
||||||
called = true;
|
called = true;
|
||||||
console.info = $info;
|
console.info = $info;
|
||||||
console.log = $log;
|
console.log = $log;
|
||||||
@@ -665,14 +625,14 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate('{{log blah}}')
|
expectTemplate('{{log blah}}')
|
||||||
.withInput({ blah: 'whee' })
|
.withInput({ blah: 'whee' })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should log at data level', function() {
|
it('should log at data level', function() {
|
||||||
var called;
|
var called;
|
||||||
|
|
||||||
console.error = function(log) {
|
console.error = function(log) {
|
||||||
expect(log).toBe('whee');
|
equals('whee', log);
|
||||||
called = true;
|
called = true;
|
||||||
console.error = $error;
|
console.error = $error;
|
||||||
};
|
};
|
||||||
@@ -682,7 +642,7 @@ describe('builtin helpers', function () {
|
|||||||
.withRuntimeOptions({ data: { level: '03' } })
|
.withRuntimeOptions({ data: { level: '03' } })
|
||||||
.withCompileOptions({ data: true })
|
.withCompileOptions({ data: true })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle missing logger', function() {
|
it('should handle missing logger', function() {
|
||||||
@@ -690,7 +650,7 @@ describe('builtin helpers', function () {
|
|||||||
|
|
||||||
console.error = undefined;
|
console.error = undefined;
|
||||||
console.log = function(log) {
|
console.log = function(log) {
|
||||||
expect(log).toBe('whee');
|
equals('whee', log);
|
||||||
called = true;
|
called = true;
|
||||||
console.log = $log;
|
console.log = $log;
|
||||||
};
|
};
|
||||||
@@ -700,14 +660,14 @@ describe('builtin helpers', function () {
|
|||||||
.withRuntimeOptions({ data: { level: '03' } })
|
.withRuntimeOptions({ data: { level: '03' } })
|
||||||
.withCompileOptions({ data: true })
|
.withCompileOptions({ data: true })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle string log levels', function() {
|
it('should handle string log levels', function() {
|
||||||
var called;
|
var called;
|
||||||
|
|
||||||
console.error = function(log) {
|
console.error = function(log) {
|
||||||
expect(log).toBe('whee');
|
equals('whee', log);
|
||||||
called = true;
|
called = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -716,7 +676,7 @@ describe('builtin helpers', function () {
|
|||||||
.withRuntimeOptions({ data: { level: 'error' } })
|
.withRuntimeOptions({ data: { level: 'error' } })
|
||||||
.withCompileOptions({ data: true })
|
.withCompileOptions({ data: true })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
|
|
||||||
called = false;
|
called = false;
|
||||||
|
|
||||||
@@ -725,31 +685,27 @@ describe('builtin helpers', function () {
|
|||||||
.withRuntimeOptions({ data: { level: 'ERROR' } })
|
.withRuntimeOptions({ data: { level: 'ERROR' } })
|
||||||
.withCompileOptions({ data: true })
|
.withCompileOptions({ data: true })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle hash log levels', function() {
|
it('should handle hash log levels', function() {
|
||||||
var called;
|
var called;
|
||||||
|
|
||||||
console.error = function(log) {
|
console.error = function(log) {
|
||||||
expect(log).toBe('whee');
|
equals('whee', log);
|
||||||
called = true;
|
called = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
expectTemplate('{{log blah level="error"}}')
|
expectTemplate('{{log blah level="error"}}')
|
||||||
.withInput({ blah: 'whee' })
|
.withInput({ blah: 'whee' })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle hash log levels', function() {
|
it('should handle hash log levels', function() {
|
||||||
var called = false;
|
var called = false;
|
||||||
|
|
||||||
console.info =
|
console.info = console.log = console.error = console.debug = function() {
|
||||||
console.log =
|
|
||||||
console.error =
|
|
||||||
console.debug =
|
|
||||||
function () {
|
|
||||||
called = true;
|
called = true;
|
||||||
console.info = console.log = console.error = console.debug = $log;
|
console.info = console.log = console.error = console.debug = $log;
|
||||||
};
|
};
|
||||||
@@ -757,16 +713,16 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate('{{log blah level="debug"}}')
|
expectTemplate('{{log blah level="debug"}}')
|
||||||
.withInput({ blah: 'whee' })
|
.withInput({ blah: 'whee' })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(false);
|
equals(false, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass multiple log arguments', function() {
|
it('should pass multiple log arguments', function() {
|
||||||
var called;
|
var called;
|
||||||
|
|
||||||
console.info = console.log = function(log1, log2, log3) {
|
console.info = console.log = function(log1, log2, log3) {
|
||||||
expect(log1).toBe('whee');
|
equals('whee', log1);
|
||||||
expect(log2).toBe('foo');
|
equals('foo', log2);
|
||||||
expect(log3).toBe(1);
|
equals(1, log3);
|
||||||
called = true;
|
called = true;
|
||||||
console.log = $log;
|
console.log = $log;
|
||||||
};
|
};
|
||||||
@@ -774,20 +730,22 @@ describe('builtin helpers', function () {
|
|||||||
expectTemplate('{{log blah "foo" 1}}')
|
expectTemplate('{{log blah "foo" 1}}')
|
||||||
.withInput({ blah: 'whee' })
|
.withInput({ blah: 'whee' })
|
||||||
.toCompileTo('');
|
.toCompileTo('');
|
||||||
expect(called).toBe(true);
|
equals(true, called);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass zero log arguments', function() {
|
it('should pass zero log arguments', function() {
|
||||||
var called;
|
var called;
|
||||||
|
|
||||||
console.info = console.log = function() {
|
console.info = console.log = function() {
|
||||||
expect(arguments.length).toBe(0);
|
expect(arguments.length).to.equal(0);
|
||||||
called = true;
|
called = true;
|
||||||
console.log = $log;
|
console.log = $log;
|
||||||
};
|
};
|
||||||
|
|
||||||
expectTemplate('{{log}}').withInput({ blah: 'whee' }).toCompileTo('');
|
expectTemplate('{{log}}')
|
||||||
expect(called).toBe(true);
|
.withInput({ blah: 'whee' })
|
||||||
|
.toCompileTo('');
|
||||||
|
expect(called).to.be.true();
|
||||||
});
|
});
|
||||||
/* eslint-enable no-console */
|
/* eslint-enable no-console */
|
||||||
});
|
});
|
||||||
|
|||||||
+205
-68
@@ -10,56 +10,69 @@ describe('compiler', function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
it('should treat as equal', function() {
|
it('should treat as equal', function() {
|
||||||
expect(compile('foo').equals(compile('foo'))).toBe(true);
|
equal(compile('foo').equals(compile('foo')), true);
|
||||||
expect(compile('{{foo}}').equals(compile('{{foo}}'))).toBe(true);
|
equal(compile('{{foo}}').equals(compile('{{foo}}')), true);
|
||||||
expect(compile('{{foo.bar}}').equals(compile('{{foo.bar}}'))).toBe(true);
|
equal(compile('{{foo.bar}}').equals(compile('{{foo.bar}}')), true);
|
||||||
expect(
|
equal(
|
||||||
compile('{{foo.bar baz "foo" true false bat=1}}').equals(
|
compile('{{foo.bar baz "foo" true false bat=1}}').equals(
|
||||||
compile('{{foo.bar baz "foo" true false bat=1}}')
|
compile('{{foo.bar baz "foo" true false bat=1}}')
|
||||||
)
|
),
|
||||||
).toBe(true);
|
true
|
||||||
expect(
|
);
|
||||||
|
equal(
|
||||||
compile('{{foo.bar (baz bat=1)}}').equals(
|
compile('{{foo.bar (baz bat=1)}}').equals(
|
||||||
compile('{{foo.bar (baz bat=1)}}')
|
compile('{{foo.bar (baz bat=1)}}')
|
||||||
)
|
),
|
||||||
).toBe(true);
|
true
|
||||||
expect(
|
);
|
||||||
compile('{{#foo}} {{/foo}}').equals(compile('{{#foo}} {{/foo}}'))
|
equal(
|
||||||
).toBe(true);
|
compile('{{#foo}} {{/foo}}').equals(compile('{{#foo}} {{/foo}}')),
|
||||||
|
true
|
||||||
|
);
|
||||||
});
|
});
|
||||||
it('should treat as not equal', function() {
|
it('should treat as not equal', function() {
|
||||||
expect(compile('foo').equals(compile('bar'))).toBe(false);
|
equal(compile('foo').equals(compile('bar')), false);
|
||||||
expect(compile('{{foo}}').equals(compile('{{bar}}'))).toBe(false);
|
equal(compile('{{foo}}').equals(compile('{{bar}}')), false);
|
||||||
expect(compile('{{foo.bar}}').equals(compile('{{bar.bar}}'))).toBe(false);
|
equal(compile('{{foo.bar}}').equals(compile('{{bar.bar}}')), false);
|
||||||
expect(
|
equal(
|
||||||
compile('{{foo.bar baz bat=1}}').equals(
|
compile('{{foo.bar baz bat=1}}').equals(
|
||||||
compile('{{foo.bar bar bat=1}}')
|
compile('{{foo.bar bar bat=1}}')
|
||||||
)
|
),
|
||||||
).toBe(false);
|
false
|
||||||
expect(
|
);
|
||||||
|
equal(
|
||||||
compile('{{foo.bar (baz bat=1)}}').equals(
|
compile('{{foo.bar (baz bat=1)}}').equals(
|
||||||
compile('{{foo.bar (bar bat=1)}}')
|
compile('{{foo.bar (bar bat=1)}}')
|
||||||
)
|
),
|
||||||
).toBe(false);
|
false
|
||||||
expect(
|
);
|
||||||
compile('{{#foo}} {{/foo}}').equals(compile('{{#bar}} {{/bar}}'))
|
equal(
|
||||||
).toBe(false);
|
compile('{{#foo}} {{/foo}}').equals(compile('{{#bar}} {{/bar}}')),
|
||||||
expect(
|
false
|
||||||
compile('{{#foo}} {{/foo}}').equals(compile('{{#foo}} {{foo}}{{/foo}}'))
|
);
|
||||||
).toBe(false);
|
equal(
|
||||||
|
compile('{{#foo}} {{/foo}}').equals(
|
||||||
|
compile('{{#foo}} {{foo}}{{/foo}}')
|
||||||
|
),
|
||||||
|
false
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#compile', function() {
|
describe('#compile', function() {
|
||||||
it('should fail with invalid input', function() {
|
it('should fail with invalid input', function() {
|
||||||
expect(function () {
|
shouldThrow(
|
||||||
|
function() {
|
||||||
Handlebars.compile(null);
|
Handlebars.compile(null);
|
||||||
}).toThrow(
|
},
|
||||||
|
Error,
|
||||||
'You must pass a string or Handlebars AST to Handlebars.compile. You passed null'
|
'You must pass a string or Handlebars AST to Handlebars.compile. You passed null'
|
||||||
);
|
);
|
||||||
expect(function () {
|
shouldThrow(
|
||||||
|
function() {
|
||||||
Handlebars.compile({});
|
Handlebars.compile({});
|
||||||
}).toThrow(
|
},
|
||||||
|
Error,
|
||||||
'You must pass a string or Handlebars AST to Handlebars.compile. You passed [object Object]'
|
'You must pass a string or Handlebars AST to Handlebars.compile. You passed [object Object]'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -67,94 +80,218 @@ describe('compiler', function () {
|
|||||||
it('should include the location in the error (row and column)', function() {
|
it('should include the location in the error (row and column)', function() {
|
||||||
try {
|
try {
|
||||||
Handlebars.compile(' \n {{#if}}\n{{/def}}')();
|
Handlebars.compile(' \n {{#if}}\n{{/def}}')();
|
||||||
expect.unreachable('Statement must throw exception');
|
equal(
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
'Statement must throw exception. This line should not be executed.'
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err.message).toBe("if doesn't match def - 2:5");
|
equal(
|
||||||
|
err.message,
|
||||||
|
"if doesn't match def - 2:5",
|
||||||
|
'Checking error message'
|
||||||
|
);
|
||||||
if (Object.getOwnPropertyDescriptor(err, 'column').writable) {
|
if (Object.getOwnPropertyDescriptor(err, 'column').writable) {
|
||||||
// In Safari 8, the column-property is read-only. This means that even if it is set with defineProperty,
|
// In Safari 8, the column-property is read-only. This means that even if it is set with defineProperty,
|
||||||
// its value won't change (https://github.com/jquery/esprima/issues/1290#issuecomment-132455482)
|
// its value won't change (https://github.com/jquery/esprima/issues/1290#issuecomment-132455482)
|
||||||
// Since this was neither working in Handlebars 3 nor in 4.0.5, we only check the column for other browsers.
|
// Since this was neither working in Handlebars 3 nor in 4.0.5, we only check the column for other browsers.
|
||||||
expect(err.column).toBe(5);
|
equal(err.column, 5, 'Checking error column');
|
||||||
}
|
}
|
||||||
expect(err.lineNumber).toBe(2);
|
equal(err.lineNumber, 2, 'Checking error row');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include the location as enumerable property', function() {
|
it('should include the location as enumerable property', function() {
|
||||||
try {
|
try {
|
||||||
Handlebars.compile(' \n {{#if}}\n{{/def}}')();
|
Handlebars.compile(' \n {{#if}}\n{{/def}}')();
|
||||||
expect.unreachable('Statement must throw exception');
|
equal(
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
'Statement must throw exception. This line should not be executed.'
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(Object.prototype.propertyIsEnumerable.call(err, 'column')).toBe(
|
equal(
|
||||||
true
|
Object.prototype.propertyIsEnumerable.call(err, 'column'),
|
||||||
|
true,
|
||||||
|
'Checking error column'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can utilize AST instance', function() {
|
it('can utilize AST instance', function() {
|
||||||
expect(
|
equal(
|
||||||
Handlebars.compile({
|
Handlebars.compile({
|
||||||
type: 'Program',
|
type: 'Program',
|
||||||
body: [{ type: 'ContentStatement', value: 'Hello' }],
|
body: [{ type: 'ContentStatement', value: 'Hello' }]
|
||||||
})()
|
})(),
|
||||||
).toBe('Hello');
|
'Hello'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
function createPathExpressionAST(depth, parts) {
|
||||||
|
return {
|
||||||
|
type: 'Program',
|
||||||
|
body: [
|
||||||
|
{
|
||||||
|
type: 'MustacheStatement',
|
||||||
|
escaped: true,
|
||||||
|
strip: { open: false, close: false },
|
||||||
|
path: {
|
||||||
|
type: 'PathExpression',
|
||||||
|
data: false,
|
||||||
|
depth: depth,
|
||||||
|
parts: parts,
|
||||||
|
original: 'this'
|
||||||
|
},
|
||||||
|
params: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should safely handle AST with non-integer PathExpression depth', function() {
|
||||||
|
// depth '0' is coerced to 0 via Number(), compiles safely
|
||||||
|
var result = Handlebars.compile(createPathExpressionAST('0', ['this']))();
|
||||||
|
expect(result).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should safely handle AST with negative PathExpression depth', function() {
|
||||||
|
// Negative depth is clamped to 0
|
||||||
|
var result = Handlebars.compile(createPathExpressionAST(-1, ['this']))();
|
||||||
|
expect(result).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should safely handle AST with fractional PathExpression depth', function() {
|
||||||
|
// Fractional depth is floored to an integer
|
||||||
|
var result = Handlebars.compile(createPathExpressionAST(0.5, ['this']))();
|
||||||
|
expect(result).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should safely handle AST with non-array PathExpression parts', function() {
|
||||||
|
// Non-array parts are coerced to empty array, compiles safely
|
||||||
|
var result = Handlebars.compile(createPathExpressionAST(0, 'this'))();
|
||||||
|
expect(result).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should safely handle AST with non-string PathExpression part', function() {
|
||||||
|
// Non-string parts are coerced to strings via String()
|
||||||
|
var result = Handlebars.compile(createPathExpressionAST(0, [1]))();
|
||||||
|
expect(result).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should safely handle AST with non-boolean BooleanLiteral value type', function() {
|
||||||
|
// The compiler coerces BooleanLiteral.value via === true before
|
||||||
|
// emitting a pushLiteral opcode, so a non-boolean value like the
|
||||||
|
// string 'true' becomes the literal 'false'.
|
||||||
|
var loc = {
|
||||||
|
source: null,
|
||||||
|
start: { line: 1, column: 0 },
|
||||||
|
end: { line: 1, column: 10 }
|
||||||
|
};
|
||||||
|
var result = Handlebars.compile({
|
||||||
|
type: 'Program',
|
||||||
|
body: [
|
||||||
|
{
|
||||||
|
type: 'MustacheStatement',
|
||||||
|
escaped: true,
|
||||||
|
strip: { open: false, close: false },
|
||||||
|
loc: loc,
|
||||||
|
path: {
|
||||||
|
type: 'BooleanLiteral',
|
||||||
|
value: 'true',
|
||||||
|
original: true,
|
||||||
|
loc: loc
|
||||||
|
},
|
||||||
|
params: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})();
|
||||||
|
// 'true' !== true, so the compiler emits pushLiteral('false').
|
||||||
|
// Handlebars does not render falsy values, so the output is empty.
|
||||||
|
expect(result).to.equal('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ignore loc metadata in AST nodes', function() {
|
||||||
|
equal(
|
||||||
|
Handlebars.compile({
|
||||||
|
type: 'Program',
|
||||||
|
meta: null,
|
||||||
|
loc: { source: 'fake', start: { line: 1, column: 0 } },
|
||||||
|
body: [{ type: 'ContentStatement', value: 'Hello' }]
|
||||||
|
})(),
|
||||||
|
'Hello'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should accept AST with valid NumberLiteral values', function() {
|
||||||
|
equal(
|
||||||
|
Handlebars.compile(Handlebars.parse('{{lookup this 1}}'))(['a', 'b']),
|
||||||
|
'b'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should accept AST with valid BooleanLiteral values', function() {
|
||||||
|
equal(
|
||||||
|
Handlebars.compile(Handlebars.parse('{{#if true}}ok{{/if}}'))({}),
|
||||||
|
'ok'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can pass through an empty string', function() {
|
it('can pass through an empty string', function() {
|
||||||
expect(Handlebars.compile('')()).toBe('');
|
equal(Handlebars.compile('')(), '');
|
||||||
});
|
|
||||||
|
|
||||||
it('throws on desupported options', function () {
|
|
||||||
expect(function () {
|
|
||||||
Handlebars.compile('Dudes', { trackIds: true });
|
|
||||||
}).toThrow(
|
|
||||||
'TrackIds and stringParams are no longer supported. See Github #1145'
|
|
||||||
);
|
|
||||||
expect(function () {
|
|
||||||
Handlebars.compile('Dudes', { stringParams: true });
|
|
||||||
}).toThrow(
|
|
||||||
'TrackIds and stringParams are no longer supported. See Github #1145'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not modify the options.data property(GH-1327)', function() {
|
it('should not modify the options.data property(GH-1327)', function() {
|
||||||
var options = { data: [{ a: 'foo' }, { a: 'bar' }] };
|
var options = { data: [{ a: 'foo' }, { a: 'bar' }] };
|
||||||
Handlebars.compile('{{#each data}}{{@index}}:{{a}} {{/each}}', options)();
|
Handlebars.compile('{{#each data}}{{@index}}:{{a}} {{/each}}', options)();
|
||||||
expect(options).toStrictEqual({ data: [{ a: 'foo' }, { a: 'bar' }] });
|
equal(
|
||||||
|
JSON.stringify(options, 0, 2),
|
||||||
|
JSON.stringify({ data: [{ a: 'foo' }, { a: 'bar' }] }, 0, 2)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not modify the options.knownHelpers property(GH-1327)', function() {
|
it('should not modify the options.knownHelpers property(GH-1327)', function() {
|
||||||
var options = { knownHelpers: {} };
|
var options = { knownHelpers: {} };
|
||||||
Handlebars.compile('{{#each data}}{{@index}}:{{a}} {{/each}}', options)();
|
Handlebars.compile('{{#each data}}{{@index}}:{{a}} {{/each}}', options)();
|
||||||
expect(options).toStrictEqual({ knownHelpers: {} });
|
equal(
|
||||||
|
JSON.stringify(options, 0, 2),
|
||||||
|
JSON.stringify({ knownHelpers: {} }, 0, 2)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#precompile', function() {
|
describe('#precompile', function() {
|
||||||
it('should fail with invalid input', function() {
|
it('should fail with invalid input', function() {
|
||||||
expect(function () {
|
shouldThrow(
|
||||||
|
function() {
|
||||||
Handlebars.precompile(null);
|
Handlebars.precompile(null);
|
||||||
}).toThrow(
|
},
|
||||||
'You must pass a string or Handlebars AST to Handlebars.compile. You passed null'
|
Error,
|
||||||
|
'You must pass a string or Handlebars AST to Handlebars.precompile. You passed null'
|
||||||
);
|
);
|
||||||
expect(function () {
|
shouldThrow(
|
||||||
|
function() {
|
||||||
Handlebars.precompile({});
|
Handlebars.precompile({});
|
||||||
}).toThrow(
|
},
|
||||||
'You must pass a string or Handlebars AST to Handlebars.compile. You passed [object Object]'
|
Error,
|
||||||
|
'You must pass a string or Handlebars AST to Handlebars.precompile. You passed [object Object]'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can utilize AST instance', function() {
|
it('can utilize AST instance', function() {
|
||||||
expect(
|
equal(
|
||||||
|
/return "Hello"/.test(
|
||||||
Handlebars.precompile({
|
Handlebars.precompile({
|
||||||
type: 'Program',
|
type: 'Program',
|
||||||
body: [{ type: 'ContentStatement', value: 'Hello' }],
|
body: [{ type: 'ContentStatement', value: 'Hello' }]
|
||||||
})
|
})
|
||||||
).toMatch(/return "Hello"/);
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can pass through an empty string', function() {
|
it('can pass through an empty string', function() {
|
||||||
expect(Handlebars.precompile('')).toMatch(/return ""/);
|
equal(/return ""/.test(Handlebars.precompile('')), true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+9
-9
@@ -93,8 +93,8 @@ describe('data', function () {
|
|||||||
data: {
|
data: {
|
||||||
hello: function() {
|
hello: function() {
|
||||||
return 'hello';
|
return 'hello';
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('hello');
|
.toCompileTo('hello');
|
||||||
});
|
});
|
||||||
@@ -105,8 +105,8 @@ describe('data', function () {
|
|||||||
data: {
|
data: {
|
||||||
hello: function(arg) {
|
hello: function(arg) {
|
||||||
return arg;
|
return arg;
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('hello');
|
.toCompileTo('hello');
|
||||||
});
|
});
|
||||||
@@ -159,7 +159,7 @@ describe('data', function () {
|
|||||||
it('passing in data to a compiled function that expects data - works with block helpers', function() {
|
it('passing in data to a compiled function that expects data - works with block helpers', function() {
|
||||||
expectTemplate('{{#hello}}{{world}}{{/hello}}')
|
expectTemplate('{{#hello}}{{world}}{{/hello}}')
|
||||||
.withCompileOptions({
|
.withCompileOptions({
|
||||||
data: true,
|
data: true
|
||||||
})
|
})
|
||||||
.withHelper('hello', function(options) {
|
.withHelper('hello', function(options) {
|
||||||
return options.fn(this);
|
return options.fn(this);
|
||||||
@@ -217,7 +217,7 @@ describe('data', function () {
|
|||||||
})
|
})
|
||||||
.withInput({ exclaim: true, zomg: 'planet' })
|
.withInput({ exclaim: true, zomg: 'planet' })
|
||||||
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
.withMessage('Overridden data output by helper')
|
.withMessage('Overriden data output by helper')
|
||||||
.toCompileTo('sad world?');
|
.toCompileTo('sad world?');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -232,7 +232,7 @@ describe('data', function () {
|
|||||||
})
|
})
|
||||||
.withInput({ exclaim: true, zomg: 'world' })
|
.withInput({ exclaim: true, zomg: 'world' })
|
||||||
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
.withMessage('Overridden data output by helper')
|
.withMessage('Overriden data output by helper')
|
||||||
.toCompileTo('sad world?');
|
.toCompileTo('sad world?');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -269,8 +269,8 @@ describe('data', function () {
|
|||||||
})
|
})
|
||||||
.withRuntimeOptions({
|
.withRuntimeOptions({
|
||||||
data: {
|
data: {
|
||||||
depth: 0,
|
depth: 0
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
.toCompileTo('2 1 0');
|
.toCompileTo('2 1 0');
|
||||||
});
|
});
|
||||||
|
|||||||
Vendored
-6
@@ -1,6 +0,0 @@
|
|||||||
// Pre-setup for browser tests. Must run before the main setup file
|
|
||||||
// imports the Handlebars library, so that noConflict() captures this value.
|
|
||||||
globalThis.Handlebars = 'no-conflict';
|
|
||||||
|
|
||||||
// Polyfill Node.js 'global' for specs that reference it at module level
|
|
||||||
globalThis.global = globalThis;
|
|
||||||
Vendored
-27
@@ -1,27 +0,0 @@
|
|||||||
import './common.js';
|
|
||||||
import Handlebars from '../../lib/handlebars.js';
|
|
||||||
|
|
||||||
globalThis.Handlebars = Handlebars;
|
|
||||||
|
|
||||||
globalThis.CompilerContext = {
|
|
||||||
browser: true,
|
|
||||||
|
|
||||||
compile: function (template, options) {
|
|
||||||
var templateSpec = handlebarsEnv.precompile(template, options);
|
|
||||||
return handlebarsEnv.template(safeEval(templateSpec));
|
|
||||||
},
|
|
||||||
compileWithPartial: function (template, options) {
|
|
||||||
return handlebarsEnv.compile(template, options);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
function safeEval(templateSpec) {
|
|
||||||
/* eslint-disable no-eval, no-console */
|
|
||||||
try {
|
|
||||||
return eval('(' + templateSpec + ')');
|
|
||||||
} catch (err) {
|
|
||||||
console.error(templateSpec);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
/* eslint-enable no-eval, no-console */
|
|
||||||
}
|
|
||||||
Vendored
+14
-6
@@ -1,14 +1,22 @@
|
|||||||
import './common.js';
|
require('./common');
|
||||||
|
|
||||||
import fs from 'fs';
|
var fs = require('fs'),
|
||||||
import vm from 'vm';
|
vm = require('vm');
|
||||||
import { createRequire } from 'module';
|
|
||||||
|
|
||||||
const require = createRequire(import.meta.url);
|
var chai = require('chai');
|
||||||
|
var dirtyChai = require('dirty-chai');
|
||||||
|
|
||||||
|
chai.use(dirtyChai);
|
||||||
|
global.expect = chai.expect;
|
||||||
|
|
||||||
|
global.sinon = require('sinon');
|
||||||
|
|
||||||
global.Handlebars = 'no-conflict';
|
global.Handlebars = 'no-conflict';
|
||||||
|
|
||||||
var filename = 'dist/handlebars.js';
|
var filename = 'dist/handlebars.js';
|
||||||
|
if (global.minimizedTest) {
|
||||||
|
filename = 'dist/handlebars.min.js';
|
||||||
|
}
|
||||||
var distHandlebars = fs.readFileSync(
|
var distHandlebars = fs.readFileSync(
|
||||||
require.resolve('../../' + filename),
|
require.resolve('../../' + filename),
|
||||||
'utf-8'
|
'utf-8'
|
||||||
@@ -24,7 +32,7 @@ global.CompilerContext = {
|
|||||||
},
|
},
|
||||||
compileWithPartial: function(template, options) {
|
compileWithPartial: function(template, options) {
|
||||||
return handlebarsEnv.compile(template, options);
|
return handlebarsEnv.compile(template, options);
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function safeEval(templateSpec) {
|
function safeEval(templateSpec) {
|
||||||
|
|||||||
Vendored
+129
-23
@@ -1,4 +1,125 @@
|
|||||||
var global = globalThis;
|
var global = (function() {
|
||||||
|
return this;
|
||||||
|
})();
|
||||||
|
|
||||||
|
var AssertError;
|
||||||
|
if (Error.captureStackTrace) {
|
||||||
|
AssertError = function AssertError(message, caller) {
|
||||||
|
Error.prototype.constructor.call(this, message);
|
||||||
|
this.message = message;
|
||||||
|
|
||||||
|
if (Error.captureStackTrace) {
|
||||||
|
Error.captureStackTrace(this, caller || AssertError);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
AssertError.prototype = new Error();
|
||||||
|
} else {
|
||||||
|
AssertError = Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||||
|
*/
|
||||||
|
global.shouldCompileTo = function(string, hashOrArray, expected, message) {
|
||||||
|
shouldCompileToWithPartials(string, hashOrArray, false, expected, message);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||||
|
*/
|
||||||
|
global.shouldCompileToWithPartials = function shouldCompileToWithPartials(
|
||||||
|
string,
|
||||||
|
hashOrArray,
|
||||||
|
partials,
|
||||||
|
expected,
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
var result = compileWithPartials(string, hashOrArray, partials);
|
||||||
|
if (result !== expected) {
|
||||||
|
throw new AssertError(
|
||||||
|
"'" + result + "' should === '" + expected + "': " + message,
|
||||||
|
shouldCompileToWithPartials
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||||
|
*/
|
||||||
|
global.compileWithPartials = function(string, hashOrArray, partials) {
|
||||||
|
var template, ary, options;
|
||||||
|
if (hashOrArray && hashOrArray.hash) {
|
||||||
|
ary = [hashOrArray.hash, hashOrArray];
|
||||||
|
delete hashOrArray.hash;
|
||||||
|
} else if (Object.prototype.toString.call(hashOrArray) === '[object Array]') {
|
||||||
|
ary = [];
|
||||||
|
ary.push(hashOrArray[0]); // input
|
||||||
|
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
|
||||||
|
options =
|
||||||
|
typeof hashOrArray[3] === 'object'
|
||||||
|
? hashOrArray[3]
|
||||||
|
: { compat: hashOrArray[3] };
|
||||||
|
if (hashOrArray[4] != null) {
|
||||||
|
options.data = !!hashOrArray[4];
|
||||||
|
ary[1].data = hashOrArray[4];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ary = [hashOrArray];
|
||||||
|
}
|
||||||
|
|
||||||
|
template = CompilerContext[partials ? 'compileWithPartial' : 'compile'](
|
||||||
|
string,
|
||||||
|
options
|
||||||
|
);
|
||||||
|
return template.apply(this, ary);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
|
||||||
|
* @see https://www.chaijs.com/api/bdd/
|
||||||
|
*/
|
||||||
|
global.equals = global.equal = function equals(a, b, msg) {
|
||||||
|
if (a !== b) {
|
||||||
|
throw new AssertError(
|
||||||
|
"'" + a + "' should === '" + b + "'" + (msg ? ': ' + msg : ''),
|
||||||
|
equals
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use chai's expect-style API instead (`expect(actualValue).to.equal(expectedValue)`)
|
||||||
|
* @see https://www.chaijs.com/api/bdd/#method_throw
|
||||||
|
*/
|
||||||
|
global.shouldThrow = function(callback, type, msg) {
|
||||||
|
var failed;
|
||||||
|
try {
|
||||||
|
callback();
|
||||||
|
failed = true;
|
||||||
|
} catch (caught) {
|
||||||
|
if (type && !(caught instanceof type)) {
|
||||||
|
throw new AssertError('Type failure: ' + caught);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
msg &&
|
||||||
|
!(msg.test ? msg.test(caught.message) : msg === caught.message)
|
||||||
|
) {
|
||||||
|
throw new AssertError(
|
||||||
|
'Throw mismatch: Expected ' +
|
||||||
|
caught.message +
|
||||||
|
' to match ' +
|
||||||
|
msg +
|
||||||
|
'\n\n' +
|
||||||
|
caught.stack,
|
||||||
|
shouldThrow
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failed) {
|
||||||
|
throw new AssertError('It failed to throw', shouldThrow);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
global.expectTemplate = function(templateAsString) {
|
global.expectTemplate = function(templateAsString) {
|
||||||
return new HandlebarsTestBench(templateAsString);
|
return new HandlebarsTestBench(templateAsString);
|
||||||
@@ -79,29 +200,18 @@ HandlebarsTestBench.prototype.withMessage = function (message) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
HandlebarsTestBench.prototype.toCompileTo = function(expectedOutputAsString) {
|
HandlebarsTestBench.prototype.toCompileTo = function(expectedOutputAsString) {
|
||||||
expect(this._compileAndExecute()).toBe(expectedOutputAsString);
|
expect(this._compileAndExecute()).to.equal(
|
||||||
|
expectedOutputAsString,
|
||||||
|
this.message
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// see chai "to.throw" (https://www.chaijs.com/api/bdd/#method_throw)
|
||||||
HandlebarsTestBench.prototype.toThrow = function(errorLike, errMsgMatcher) {
|
HandlebarsTestBench.prototype.toThrow = function(errorLike, errMsgMatcher) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var caught;
|
expect(function() {
|
||||||
try {
|
|
||||||
self._compileAndExecute();
|
self._compileAndExecute();
|
||||||
} catch (e) {
|
}).to.throw(errorLike, errMsgMatcher, this.message);
|
||||||
caught = e;
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(caught).toBeDefined();
|
|
||||||
|
|
||||||
if (typeof errorLike === 'function') {
|
|
||||||
expect(caught).toBeInstanceOf(errorLike);
|
|
||||||
if (errMsgMatcher) {
|
|
||||||
expect(caught.message).toMatch(errMsgMatcher);
|
|
||||||
}
|
|
||||||
} else if (errorLike) {
|
|
||||||
// errorLike is a string or regex message matcher (single-argument form)
|
|
||||||
expect(caught.message).toMatch(errorLike);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
HandlebarsTestBench.prototype._compileAndExecute = function() {
|
HandlebarsTestBench.prototype._compileAndExecute = function() {
|
||||||
@@ -127,7 +237,3 @@ HandlebarsTestBench.prototype._combineRuntimeOptions = function () {
|
|||||||
combinedRuntimeOptions.decorators = this.decorators;
|
combinedRuntimeOptions.decorators = this.decorators;
|
||||||
return combinedRuntimeOptions;
|
return combinedRuntimeOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(function () {
|
|
||||||
global.handlebarsEnv = Handlebars.create();
|
|
||||||
});
|
|
||||||
|
|||||||
Vendored
+11
-4
@@ -1,7 +1,14 @@
|
|||||||
import './common.js';
|
require('./common');
|
||||||
import Handlebars from '../../lib/index.js';
|
|
||||||
|
|
||||||
global.Handlebars = Handlebars;
|
var chai = require('chai');
|
||||||
|
var dirtyChai = require('dirty-chai');
|
||||||
|
|
||||||
|
chai.use(dirtyChai);
|
||||||
|
global.expect = chai.expect;
|
||||||
|
|
||||||
|
global.sinon = require('sinon');
|
||||||
|
|
||||||
|
global.Handlebars = require('../../lib');
|
||||||
|
|
||||||
global.CompilerContext = {
|
global.CompilerContext = {
|
||||||
compile: function(template, options) {
|
compile: function(template, options) {
|
||||||
@@ -10,7 +17,7 @@ global.CompilerContext = {
|
|||||||
},
|
},
|
||||||
compileWithPartial: function(template, options) {
|
compileWithPartial: function(template, options) {
|
||||||
return handlebarsEnv.compile(template, options);
|
return handlebarsEnv.compile(template, options);
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function safeEval(templateSpec) {
|
function safeEval(templateSpec) {
|
||||||
|
|||||||
Vendored
+63
@@ -0,0 +1,63 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
|
var fs = require('fs'),
|
||||||
|
Mocha = require('mocha'),
|
||||||
|
path = require('path');
|
||||||
|
|
||||||
|
var errors = 0,
|
||||||
|
testDir = path.dirname(__dirname),
|
||||||
|
grep = process.argv[2];
|
||||||
|
|
||||||
|
// Lazy hack, but whatever
|
||||||
|
if (grep === '--min') {
|
||||||
|
global.minimizedTest = true;
|
||||||
|
grep = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
var files = fs
|
||||||
|
.readdirSync(testDir)
|
||||||
|
.filter(function(name) {
|
||||||
|
return /.*\.js$/.test(name);
|
||||||
|
})
|
||||||
|
.map(function(name) {
|
||||||
|
return testDir + path.sep + name;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (global.minimizedTest) {
|
||||||
|
run('./runtime', function() {
|
||||||
|
run('./browser', function() {
|
||||||
|
/* eslint-disable no-process-exit */
|
||||||
|
process.exit(errors);
|
||||||
|
/* eslint-enable no-process-exit */
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
run('./runtime', function() {
|
||||||
|
run('./browser', function() {
|
||||||
|
run('./node', function() {
|
||||||
|
/* eslint-disable no-process-exit */
|
||||||
|
process.exit(errors);
|
||||||
|
/* eslint-enable no-process-exit */
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function run(env, callback) {
|
||||||
|
var mocha = new Mocha();
|
||||||
|
mocha.ui('bdd');
|
||||||
|
mocha.files = files.slice();
|
||||||
|
if (grep) {
|
||||||
|
mocha.grep(grep);
|
||||||
|
}
|
||||||
|
|
||||||
|
files.forEach(function(name) {
|
||||||
|
delete require.cache[name];
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Running env: ' + env);
|
||||||
|
require(env);
|
||||||
|
mocha.run(function(errorCount) {
|
||||||
|
errors += errorCount;
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
}
|
||||||
Vendored
+70
@@ -0,0 +1,70 @@
|
|||||||
|
require('./common');
|
||||||
|
|
||||||
|
var fs = require('fs'),
|
||||||
|
vm = require('vm');
|
||||||
|
|
||||||
|
var chai = require('chai');
|
||||||
|
var dirtyChai = require('dirty-chai');
|
||||||
|
|
||||||
|
chai.use(dirtyChai);
|
||||||
|
global.expect = chai.expect;
|
||||||
|
|
||||||
|
global.sinon = require('sinon');
|
||||||
|
|
||||||
|
global.Handlebars = 'no-conflict';
|
||||||
|
|
||||||
|
var filename = 'dist/handlebars.runtime.js';
|
||||||
|
if (global.minimizedTest) {
|
||||||
|
filename = 'dist/handlebars.runtime.min.js';
|
||||||
|
}
|
||||||
|
vm.runInThisContext(
|
||||||
|
fs.readFileSync(__dirname + '/../../' + filename),
|
||||||
|
filename
|
||||||
|
);
|
||||||
|
|
||||||
|
var parse = require('../../dist/cjs/handlebars/compiler/base').parse;
|
||||||
|
var compiler = require('../../dist/cjs/handlebars/compiler/compiler');
|
||||||
|
var JavaScriptCompiler = require('../../dist/cjs/handlebars/compiler/javascript-compiler');
|
||||||
|
|
||||||
|
global.CompilerContext = {
|
||||||
|
browser: true,
|
||||||
|
|
||||||
|
compile: function(template, options) {
|
||||||
|
// Hack the compiler on to the environment for these specific tests
|
||||||
|
handlebarsEnv.precompile = function(precompileTemplate, precompileOptions) {
|
||||||
|
return compiler.precompile(
|
||||||
|
precompileTemplate,
|
||||||
|
precompileOptions,
|
||||||
|
handlebarsEnv
|
||||||
|
);
|
||||||
|
};
|
||||||
|
handlebarsEnv.parse = parse;
|
||||||
|
handlebarsEnv.Compiler = compiler.Compiler;
|
||||||
|
handlebarsEnv.JavaScriptCompiler = JavaScriptCompiler;
|
||||||
|
|
||||||
|
var templateSpec = handlebarsEnv.precompile(template, options);
|
||||||
|
return handlebarsEnv.template(safeEval(templateSpec));
|
||||||
|
},
|
||||||
|
compileWithPartial: function(template, options) {
|
||||||
|
// Hack the compiler on to the environment for these specific tests
|
||||||
|
handlebarsEnv.compile = function(compileTemplate, compileOptions) {
|
||||||
|
return compiler.compile(compileTemplate, compileOptions, handlebarsEnv);
|
||||||
|
};
|
||||||
|
handlebarsEnv.parse = parse;
|
||||||
|
handlebarsEnv.Compiler = compiler.Compiler;
|
||||||
|
handlebarsEnv.JavaScriptCompiler = JavaScriptCompiler;
|
||||||
|
|
||||||
|
return handlebarsEnv.compile(template, options);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function safeEval(templateSpec) {
|
||||||
|
/* eslint-disable no-eval, no-console */
|
||||||
|
try {
|
||||||
|
return eval('(' + templateSpec + ')');
|
||||||
|
} catch (err) {
|
||||||
|
console.error(templateSpec);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
/* eslint-enable no-eval, no-console */
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
return templates["bom"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "a";
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
return templates["empty"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
define(["handlebars.runtime"],function(e){var t=(e=e.default).template;return(e.templates=e.templates||{}).empty=t({compiler:[8,">= 4.3.0"],main:function(e,t,a,n,r){return""},useData:!0})});
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = CustomNamespace.templates = CustomNamespace.templates || {};
|
||||||
|
return templates["empty"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
(function() {
|
||||||
|
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
templates["empty"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true});
|
||||||
|
})();
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
templates["firstTemplate"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "<div>1</div>";
|
||||||
|
},"useData":true});
|
||||||
|
templates["secondTemplate"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "<div>2</div>";
|
||||||
|
},"useData":true});
|
||||||
|
return templates;
|
||||||
|
});
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
(function() {
|
|
||||||
var template = Handlebars.template, templates = CustomNamespace.templates = CustomNamespace.templates || {};
|
|
||||||
templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
|
||||||
return "";
|
|
||||||
},"useData":true});
|
|
||||||
})();
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
return templates["artifacts/partial.template"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "<div>Test Partial</div>";
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
define(["some-path/handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
return templates["empty"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
Precompile handlebar templates.
|
Precompile handlebar templates.
|
||||||
Usage: handlebars.js [template|directory]...
|
Usage: handlebars [template|directory]...
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-f, --output Output File [string]
|
-f, --output Output File [string]
|
||||||
--map Source Map File [string]
|
--map Source Map File [string]
|
||||||
|
-a, --amd Exports amd style (require.js) [boolean]
|
||||||
|
-c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null]
|
||||||
|
-h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""]
|
||||||
-k, --known Known helpers [string]
|
-k, --known Known helpers [string]
|
||||||
-o, --knownOnly Known helpers only [boolean]
|
-o, --knownOnly Known helpers only [boolean]
|
||||||
-m, --min Minimize output [boolean]
|
-m, --min Minimize output [boolean]
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = someNameSpace = someNameSpace || {};
|
||||||
|
templates["empty"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true});
|
||||||
|
templates["empty"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "";
|
||||||
|
},"useData":true});
|
||||||
|
return templates;
|
||||||
|
});
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
return templates["non.default.extension"] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
return "<div>This is a test</div>";
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
define(["handlebars.runtime"], function(Handlebars) {
|
||||||
|
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
|
||||||
|
return templates["known.helpers"] = template({"0":function(container,depth0,helpers,partials,data) {
|
||||||
|
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
|
||||||
|
return parent[propertyName];
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
};
|
||||||
|
return " <div>Some known helper</div>\n"
|
||||||
|
+ ((stack1 = lookupProperty(helpers,"anotherHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"anotherHelper","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":22}}})) != null ? stack1 : "");
|
||||||
|
},"1":function(container,depth0,helpers,partials,data) {
|
||||||
|
return " <div>Another known helper</div>\n";
|
||||||
|
},"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
|
||||||
|
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
|
||||||
|
return parent[propertyName];
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
};
|
||||||
|
return ((stack1 = lookupProperty(helpers,"someHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"someHelper","hash":{},"fn":container.program(0, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":15}}})) != null ? stack1 : "");
|
||||||
|
},"useData":true});
|
||||||
|
});
|
||||||
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user