Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dt><a href="#escapeHTML">escapeHTML(unescaped)</a> ⇒ <code>String</code></dt>
<dd><p>Escape HTML characters with their respective entities</p>
</dd>
<dt><a href="#placeholders">placeholders(input, ctx, settings)</a> ⇒ <code>String</code></dt>
<dt><a href="#placeholders">placeholders(input, ctx, settings, opts)</a> ⇒ <code>String</code></dt>
<dd><p>Replace Expressions</p>
</dd>
<dt><a href="#getNextTag">getNextTag(nodes, i)</a> ⇒ <code>Array</code></dt>
Expand Down Expand Up @@ -200,7 +200,7 @@ Escape HTML characters with their respective entities

<a name="placeholders"></a>

## placeholders(input, ctx, settings) ⇒ <code>String</code>
## placeholders(input, ctx, settings, opts) ⇒ <code>String</code>
Replace Expressions

**Kind**: global function
Expand All @@ -211,6 +211,7 @@ Replace Expressions
| input | <code>String</code> | Input |
| ctx | <code>Object</code> | Context |
| settings | <code>Array</code> | Settings |
| opts | <code>Array</code> | Options |

<a name="getNextTag"></a>

Expand Down
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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]
Expand Down
11 changes: 9 additions & 2 deletions lib/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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]
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthtml-expressions",
"version": "1.6.0",
"version": "1.6.1",
"description": "Expressions Plugin for PostHTML",
"engines": {
"node": ">=10"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/conditional_if_key_not_exists.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<if condition="key_not_exists">
it works!
</if>

<if condition="key_not_exists">
{{ key_not_exists.foo }}
it works!
</if>
2 changes: 1 addition & 1 deletion test/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

Expand Down