Skip to content

Commit 3464aa5

Browse files
author
Jeff Escalante
committed
more tests
1 parent ac18ed9 commit 3464aa5

File tree

9 files changed

+85
-11
lines changed

9 files changed

+85
-11
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ indent_style = space
88
trim_trailing_whitespace = true
99
insert_final_newline = true
1010

11-
[*.md]
11+
[*.{md,html}]
1212
trim_trailing_whitespace = false

lib/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ const merge = require('lodash.merge')
33
const cloneDeep = require('lodash.clonedeep')
44
const parseAndReplace = require('./expression_parser')
55

6-
let delimiters, unescapeDelimiters, delimiterRegex, unescapeDelimiterRegex, conditionals, loops
6+
let delimiters, unescapeDelimiters, delimiterRegex, unescapeDelimiterRegex, conditionals, loops, options
77

8-
module.exports = function PostHTMLExpressions (options = {}) {
8+
module.exports = function PostHTMLExpressions (_options = {}) {
9+
options = _options
910
// set up delimiter options and detection
1011
delimiters = options.delimiters || ['{{', '}}']
1112
unescapeDelimiters = options.unescapeDelimiters || ['{{{', '}}}']
@@ -146,11 +147,11 @@ function walk (opts, nodes) {
146147
// run the loop, different types of loops for arrays and objects
147148
if (Array.isArray(target)) {
148149
for (let index = 0; index < target.length; index++) {
149-
m.push(executeLoop(loopParams.keys, target[index], index, node, opts))
150+
m.push(executeLoop(loopParams.keys, target[index], index, node))
150151
}
151152
} else {
152153
for (let key in target) {
153-
m.push(executeLoop(loopParams.keys, target[key], key, node, opts))
154+
m.push(executeLoop(loopParams.keys, target[key], key, node))
154155
}
155156
}
156157

@@ -240,15 +241,15 @@ function parseLoopStatement (input) {
240241
* Creates a set of local variables within the loop, and evaluates all nodes
241242
* within the loop, returning their contents
242243
*/
243-
function executeLoop (loopParams, p1, p2, node, opts) {
244+
function executeLoop (loopParams, p1, p2, node) {
244245
// two loop locals are allowed
245246
// - for arrays it's the current value and the index
246247
// - for objects, it's the value and the key
247248
const scopedLocals = {}
248249
scopedLocals[loopParams[0]] = p1
249250
if (loopParams[1]) scopedLocals[loopParams[1]] = p2
250251
// merge nondestructively into existing locals
251-
const scopedOptions = merge(opts, { locals: scopedLocals })
252+
const scopedOptions = merge(cloneDeep(options), { locals: scopedLocals })
252253
// walk through the contents and run replacements with modified options
253254
// we need to clone the node because the normal operation modifies
254255
// the node directly
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>bar</p>
2+
3+
<p>0: 1</p>
4+
5+
<p>1: 2</p>
6+
7+
<p>2: 3</p>
8+
9+
<p>bar</p>

test/fixtures/loop_conflict.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>{{item}}</p>
2+
<each loop='item, index in items'>
3+
<p>{{index}}: {{item}}</p>
4+
</each>
5+
<p>{{item}}</p>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>x</p>
2+
3+
<p>0, 1, bar</p>
4+
5+
<p>1, 2, bar</p>
6+
7+
<p>2, 3, bar</p>
8+
9+
<p>x</p>

test/fixtures/loop_locals.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>x</p>
2+
<each loop='item, index in items'>
3+
<p>{{index}}, {{item}}, {{ foo }}</p>
4+
</each>
5+
<p>x</p>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<p>x</p>
2+
3+
<p>c1: [1,2,3]</p>
4+
5+
<p>1</p>
6+
7+
<p>2</p>
8+
9+
<p>3</p>
10+
11+
12+
<p>c2: [4,5,6]</p>
13+
14+
<p>4</p>
15+
16+
<p>5</p>
17+
18+
<p>6</p>
19+
20+
21+
<p>x</p>

test/fixtures/loop_nested.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<p>x</p>
2+
<each loop='item_category, key in items'>
3+
<p>{{ key }}: {{ JSON.stringify(item_category) }}</p>
4+
<each loop='item in item_category'>
5+
<p>{{ item }}</p>
6+
</each>
7+
</each>
8+
<p>x</p>

test/index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,28 @@ test('loop', (t) => {
7272
})
7373

7474
test('loop object', (t) => {
75-
return matchExpected(t, 'loop_object', { locals: { items: { a: 'b', c: 'd' } } })
75+
return matchExpected(t, 'loop_object', {
76+
locals: { items: { a: 'b', c: 'd' } }
77+
})
7678
})
7779

78-
test.todo('loop with other locals included')
79-
test.todo('loop with conflicting locals')
80-
test.todo('nested loops')
80+
test('loop with other locals included', (t) => {
81+
return matchExpected(t, 'loop_locals', {
82+
locals: { items: [1, 2, 3], foo: 'bar' }
83+
})
84+
})
85+
86+
test('loop with conflicting locals', (t) => {
87+
return matchExpected(t, 'loop_conflict', {
88+
locals: { items: [1, 2, 3], item: 'bar' }
89+
})
90+
})
91+
92+
test('nested loops', (t) => {
93+
return matchExpected(t, 'loop_nested', {
94+
locals: { items: { c1: [1, 2, 3], c2: [4, 5, 6] } }
95+
})
96+
})
8197

8298
//
8399
// Utility

0 commit comments

Comments
 (0)