Skip to content

Commit 4488115

Browse files
authored
Merge pull request #78 from cossssmin/fix-ignored
Fix expression ignoring
2 parents 77aaf56 + 28ad6ac commit 4488115

File tree

7 files changed

+23
-27
lines changed

7 files changed

+23
-27
lines changed

lib/index.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const revertBackupedLocals = require('./backup').revert
1010
const placeholders = require('./placeholders')
1111

1212
const delimitersSettings = []
13-
let conditionals, switches, loops, scopes, ignored
13+
let conditionals, switches, loops, scopes, ignored, delimitersReplace, unescapeDelimitersReplace
1414

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

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

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

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

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

151+
delimitersReplace = new RegExp(`@${delimitersSettings[1].text[0]}`, 'g')
152+
unescapeDelimitersReplace = new RegExp(`@${delimitersSettings[0].text[0]}`, 'g')
153+
151154
// kick off the parsing
152155
return walk.bind(null, { locals: options.locals })
153156
}
@@ -176,20 +179,10 @@ function walk (opts, nodes) {
176179

177180
// if we have a string, match and replace it
178181
if (typeof node === 'string') {
179-
// if node contains ignored expression delimiter, output as-is
180-
const before = delimitersSettings[1].text[0]
181-
const after = delimitersSettings[1].text[1]
182-
const ignoredDelimiter = new RegExp(`@${before}(.+?)${after}`, 'g')
183-
184-
if (ignoredDelimiter.test(node)) {
185-
node = node.replace(`@${before}`, before)
186-
187-
m.push(node)
188-
189-
return m
190-
}
191-
192182
node = placeholders(node, ctx, delimitersSettings)
183+
node = node
184+
.replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
185+
.replace(delimitersReplace, delimitersSettings[1].text[0])
193186

194187
m.push(node)
195188

@@ -200,6 +193,9 @@ function walk (opts, nodes) {
200193
if (node.attrs) {
201194
for (const key in node.attrs) {
202195
node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings)
196+
node.attrs[key] = node.attrs[key]
197+
.replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
198+
.replace(delimitersReplace, delimitersSettings[1].text[0])
203199
}
204200
}
205201

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{{ foo }}
2-
<p>
3-
And here's a {{ variable }} that would be undefined.
2+
<p data-username="{{ user.name }}" data-user-id="user-{{ user.id }}-bar-{{ foo }}">
3+
Here's one {{ variable }} and here's {{ another }}. And some bar.
44
</p>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@{{ foo }}
2-
<p>
3-
And here's a @{{ variable }} that would be undefined.
2+
<p data-username="@{{ user.name }}" data-user-id="user-@{{ user.id }}-{{ foo }}-@{{ foo }}">
3+
Here's one @{{ variable }} and here's @{{ another }}. And some {{ foo }}.
44
</p>

test/test-conditionals.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
2323
return clean(result.html)
2424
})
2525
.then((html) => {
26-
t.truthy(html === expect(name).trim())
26+
t.is(html, expect(name).trim())
2727
})
2828
}
2929

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

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

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

test/test-core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
2323
return clean(result.html)
2424
})
2525
.then((html) => {
26-
t.truthy(html === expect(name).trim())
26+
t.is(html, expect(name).trim())
2727
})
2828
}
2929

test/test-scopes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
2323
return clean(result.html)
2424
})
2525
.then((html) => {
26-
t.truthy(html === expect(name).trim())
26+
t.is(html, expect(name).trim())
2727
})
2828
}
2929

test/test-switch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function process (t, name, options, log = false) {
2323
return clean(result.html)
2424
})
2525
.then((html) => {
26-
t.truthy(html === expect(name).trim())
26+
t.is(html, expect(name).trim())
2727
})
2828
}
2929

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

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

0 commit comments

Comments
 (0)