Skip to content

Commit e00c496

Browse files
committed
adding regression tests for "getting parents context" and also for double loop
case. Also refactored a bit pull request #91 . Also closes #90 and #87
1 parent 9430cf2 commit e00c496

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

coffee/docxgenTest.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,17 @@ describe 'SubContent', () ->
827827
xmlt=new DocXTemplater(content,{Tags:{name:"Henry"}}).applyTags()
828828
expect(xmlt.content).toContain('Henry</w:t><w:t')
829829

830+
describe 'getting parents context',()->
831+
it 'should work with simple loops',()->
832+
content= """{#loop}{name}{/loop}"""
833+
xmlt=new DocXTemplater(content,{Tags:{loop:[1],name:"Henry"}}).applyTags()
834+
expect(xmlt.content).toBe("Henry")
835+
836+
it 'should work with double loops',()->
837+
content= """{#loop_first}{#loop_second}{name_inner} {name_outer}{/loop_second}{/loop_first}"""
838+
xmlt=new DocXTemplater(content,{Tags:{loop_first:[1],loop_second:[{name_inner:"John"}],name_outer:"Henry"}}).applyTags()
839+
expect(xmlt.content).toBe("John Henry")
840+
830841
describe 'error messages', ()->
831842
it 'should work with unclosed', ()->
832843
content= """<w:t>{tag {age}</w:t>"""

coffee/scopeManager.coffee

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@ module.exports=class ScopeManager
77
value = @getValue(tag)
88
type = typeof value
99
if inverted
10-
return callback(@scopeList[@scopeList.length-1]) unless value
10+
return callback(@scopeList[@num]) unless value
1111
return if type == 'string'
1212
if type == 'object' && value.length < 1
13-
callback(@scopeList[@scopeList.length-1])
13+
callback(@scopeList[@num])
1414
return
1515
return unless value?
1616
if type == 'object'
1717
for scope,i in value
1818
callback(scope)
1919
if value == true
20-
callback(@scopeList[@scopeList.length-1])
21-
getValue:(tag, scope=@scopeList[@scopeList.length-1])->
20+
callback(@scopeList[@num])
21+
getValue:(tag,@num=@scopeList.length-1)->
22+
scope=@scopeList[@num]
2223
parser=@parser(DocUtils.wordToUtf8(tag))
2324
result=parser.get(scope)
25+
if result==undefined and @num>0 then return @getValue(tag,@num-1)
26+
result
2427
getValueFromScope: (tag) ->
2528
# search in the scopes (in reverse order) and keep the first defined value
26-
result = undefined
27-
for index in [@scopeList.length-1..0]
28-
result or= @getValue(tag, @scopeList[index])
29+
result = @getValue(tag)
2930
if result?
3031
if typeof result=='string'
3132
@useTag(tag)

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var server=null;
1111

1212
try {
1313
var Blink1 = require('node-blink1');
14-
var blink1 = new Blink1('20002C4A');
14+
var blink1 = new Blink1();
1515
} catch (e) {
1616
blink1={
1717
fadeToRGB:function(){

0 commit comments

Comments
 (0)