diff --git a/INDEX.md b/INDEX.md index c309c6f..dda0fae 100644 --- a/INDEX.md +++ b/INDEX.md @@ -43,7 +43,7 @@
escapeHTML(unescaped)String

Escape HTML characters with their respective entities

-
placeholders(input, ctx, settings)String
+
placeholders(input, ctx, settings, opts)String

Replace Expressions

getNextTag(nodes, i)Array
@@ -200,7 +200,7 @@ Escape HTML characters with their respective entities -## placeholders(input, ctx, settings) ⇒ String +## placeholders(input, ctx, settings, opts) ⇒ String Replace Expressions **Kind**: global function @@ -211,6 +211,7 @@ Replace Expressions | input | String | Input | | ctx | Object | Context | | settings | Array | Settings | +| opts | Array | Options | diff --git a/changelog.md b/changelog.md index dc7916a..5596c62 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,12 @@ +## [1.6.1](https://github.com/posthtml/posthtml-expressions/compare/v1.6.0...v1.6.1) (2020-11-09) + + +### Bug Fixes + +* check if key exists in locals as text, close [#102](https://github.com/posthtml/posthtml-expressions/issues/102) ([1d7596a](https://github.com/posthtml/posthtml-expressions/commit/1d7596a8a4d12fd29a74f03637057772a9b81d09)) + + + # [1.6.0](https://github.com/posthtml/posthtml-expressions/compare/v1.5.0...v1.6.0) (2020-10-30) diff --git a/lib/index.js b/lib/index.js index e515aed..55df9d7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -182,7 +182,7 @@ function walk (opts, nodes) { // if we have a string, match and replace it if (typeof node === 'string') { - node = placeholders(node, ctx, delimitersSettings) + node = placeholders(node, ctx, delimitersSettings, opts) node = node .replace(unescapeDelimitersReplace, delimitersSettings[0].text[0]) .replace(delimitersReplace, delimitersSettings[1].text[0]) @@ -196,14 +196,14 @@ function walk (opts, nodes) { if (node.attrs) { for (const key in node.attrs) { if (typeof node.attrs[key] === 'string') { - node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings) + node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings, opts) node.attrs[key] = node.attrs[key] .replace(unescapeDelimitersReplace, delimitersSettings[0].text[0]) .replace(delimitersReplace, delimitersSettings[1].text[0]) } // if key is parametr - const _key = placeholders(key, ctx, delimitersSettings) + const _key = placeholders(key, ctx, delimitersSettings, opts) if (key !== _key) { node.attrs[_key] = node.attrs[key] delete node.attrs[key] diff --git a/lib/placeholders.js b/lib/placeholders.js index fdcf7b6..52368dd 100644 --- a/lib/placeholders.js +++ b/lib/placeholders.js @@ -32,10 +32,11 @@ function escapeHTML (unescaped) { * @param {String} input Input * @param {Object} ctx Context * @param {Array} settings Settings + * @param {Array} opts Options * * @return {String} input Replaced Input */ -function placeholders (input, ctx, settings) { +function placeholders (input, ctx, settings, opts) { // Since we are matching multiple sets of delimiters, we need to run a loop // here to match each one. for (let i = 0; i < settings.length; i++) { @@ -54,7 +55,13 @@ function placeholders (input, ctx, settings) { let value if (/\W+/.test(expression)) { - value = vm.runInContext(expression, ctx) + try { + value = vm.runInContext(expression, ctx) + } catch (error) { + if (opts.strictMode) { + throw new SyntaxError(error) + } + } } else if (Object.prototype.hasOwnProperty.call(ctx, expression)) { value = ctx[expression] } diff --git a/package-lock.json b/package-lock.json index 3bfb091..5a8cf5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "posthtml-expressions", - "version": "1.6.0", + "version": "1.6.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.6.0", + "version": "1.6.1", "license": "MIT", "dependencies": { "fclone": "^1.0.11", diff --git a/package.json b/package.json index d934944..0b42342 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "posthtml-expressions", - "version": "1.6.0", + "version": "1.6.1", "description": "Expressions Plugin for PostHTML", "engines": { "node": ">=10" diff --git a/test/fixtures/conditional_if_key_not_exists.html b/test/fixtures/conditional_if_key_not_exists.html index 4f1ab3f..20a2382 100644 --- a/test/fixtures/conditional_if_key_not_exists.html +++ b/test/fixtures/conditional_if_key_not_exists.html @@ -1,3 +1,8 @@ it works! + + + {{ key_not_exists.foo }} + it works! + diff --git a/test/test-core.js b/test/test-core.js index 83be71e..598b3be 100644 --- a/test/test-core.js +++ b/test/test-core.js @@ -66,7 +66,7 @@ test('Expressions - spacing', (t) => { test('Expressions - error', (t) => { return error('expression_error', (err) => { - t.is(err.message, 'Invalid or unexpected token') + t.is(err.message, 'SyntaxError: Invalid or unexpected token') }) })