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
28 changes: 12 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const revertBackupedLocals = require('./backup').revert
const placeholders = require('./placeholders')

const delimitersSettings = []
let conditionals, switches, loops, scopes, ignored
let conditionals, switches, loops, scopes, ignored, delimitersReplace, unescapeDelimitersReplace

/**
* @description Creates a set of local variables within the loop, and evaluates all nodes within the loop, returning their contents
Expand Down Expand Up @@ -124,12 +124,12 @@ module.exports = function postHTMLExpressions (options) {
let before = escapeRegexpString(options.delimiters[0])
let after = escapeRegexpString(options.delimiters[1])

const delimitersRegexp = new RegExp(`${before}(.+?)${after}`, 'g')
const delimitersRegexp = new RegExp(`(?<!@)${before}(.+?)${after}`, 'g')

before = escapeRegexpString(options.unescapeDelimiters[0])
after = escapeRegexpString(options.unescapeDelimiters[1])

const unescapeDelimitersRegexp = new RegExp(`${before}(.+?)${after}`, 'g')
const unescapeDelimitersRegexp = new RegExp(`(?<!@)${before}(.+?)${after}`, 'g')

// make array of delimiters
const delimiters = [
Expand All @@ -148,6 +148,9 @@ module.exports = function postHTMLExpressions (options) {
delimitersSettings[1] = delimiters[0]
}

delimitersReplace = new RegExp(`@${delimitersSettings[1].text[0]}`, 'g')
unescapeDelimitersReplace = new RegExp(`@${delimitersSettings[0].text[0]}`, 'g')

// kick off the parsing
return walk.bind(null, { locals: options.locals })
}
Expand Down Expand Up @@ -176,20 +179,10 @@ function walk (opts, nodes) {

// if we have a string, match and replace it
if (typeof node === 'string') {
// if node contains ignored expression delimiter, output as-is
const before = delimitersSettings[1].text[0]
const after = delimitersSettings[1].text[1]
const ignoredDelimiter = new RegExp(`@${before}(.+?)${after}`, 'g')

if (ignoredDelimiter.test(node)) {
node = node.replace(`@${before}`, before)

m.push(node)

return m
}

node = placeholders(node, ctx, delimitersSettings)
node = node
.replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
.replace(delimitersReplace, delimitersSettings[1].text[0])

m.push(node)

Expand All @@ -200,6 +193,9 @@ function walk (opts, nodes) {
if (node.attrs) {
for (const key in node.attrs) {
node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings)
node.attrs[key] = node.attrs[key]
.replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
.replace(delimitersReplace, delimitersSettings[1].text[0])
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/expect/expression_ignored.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ foo }}
<p>
And here's a {{ variable }} that would be undefined.
<p data-username="{{ user.name }}" data-user-id="user-{{ user.id }}-bar-{{ foo }}">
Here's one {{ variable }} and here's {{ another }}. And some bar.
</p>
4 changes: 2 additions & 2 deletions test/fixtures/expression_ignored.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@{{ foo }}
<p>
And here's a @{{ variable }} that would be undefined.
<p data-username="@{{ user.name }}" data-user-id="user-@{{ user.id }}-{{ foo }}-@{{ foo }}">
Here's one @{{ variable }} and here's @{{ another }}. And some {{ foo }}.
</p>
6 changes: 3 additions & 3 deletions test/test-conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
return clean(result.html)
})
.then((html) => {
t.truthy(html === expect(name).trim())
t.is(html, expect(name).trim())
})
}

Expand Down Expand Up @@ -51,13 +51,13 @@ test('Conditionals - no render', (t) => {

test('Conditionals - "if" tag missing condition', (t) => {
return error('conditional_if_error', (err) => {
t.truthy(err.toString() === 'Error: the "if" tag must have a "condition" attribute')
t.is(err.toString(), 'Error: the "if" tag must have a "condition" attribute')
})
})

test('Conditionals - "elseif" tag missing condition', (t) => {
return error('conditional_elseif_error', (err) => {
t.truthy(err.toString() === 'Error: the "elseif" tag must have a "condition" attribute')
t.is(err.toString(), 'Error: the "elseif" tag must have a "condition" attribute')
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
return clean(result.html)
})
.then((html) => {
t.truthy(html === expect(name).trim())
t.is(html, expect(name).trim())
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/test-scopes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
return clean(result.html)
})
.then((html) => {
t.truthy(html === expect(name).trim())
t.is(html, expect(name).trim())
})
}

Expand Down
4 changes: 2 additions & 2 deletions test/test-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
return clean(result.html)
})
.then((html) => {
t.truthy(html === expect(name).trim())
t.is(html, expect(name).trim())
})
}

Expand Down Expand Up @@ -75,7 +75,7 @@ test('Switch - dynamic expression', (t) => {

test('Switch - no switch attribute', (t) => {
return error('switch_no_attr', (err) => {
t.truthy(err.toString() === 'Error: the "switch" tag must have a "expression" attribute')
t.is(err.toString(), 'Error: the "switch" tag must have a "expression" attribute')
})
})

Expand Down