Skip to content

Commit 86e67ae

Browse files
committed
refactoring of xmlUtil (no more a class)
1 parent f60cf7a commit 86e67ae

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

coffee/docxTemplater.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
XmlTemplater=require('./xmlTemplater')
2-
XmlUtil=require('./xmlUtil')
2+
xmlUtil=require('./xmlUtil')
33

44
DocXTemplater = class DocXTemplater extends XmlTemplater
5-
xmlUtil=new XmlUtil()
65
constructor:(content="",options={}) ->
76
super("",options)
87
@currentClass=DocXTemplater

coffee/docxgenTest.coffee

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ docX=DocUtils.docX
2626
docXData=DocUtils.docXData
2727
SubContent=require(p('subContent'))
2828
DocXTemplater=require(p('docxTemplater'))
29-
XmlUtil=require(p('xmlUtil'))
29+
xmlUtil=require(p('xmlUtil'))
3030
JSZip=require('jszip')
3131
PNG=require('png-js')
3232
DocxQrCode=require(p('docxQrCode'))
@@ -169,7 +169,6 @@ describe "DocxGenTemplatingForLoop", () ->
169169
expect(docX['tagInvertedLoopExample.docx'].getFullText()).toEqual('')
170170

171171
describe "Xml Util" , () ->
172-
xmlUtil= new XmlUtil()
173172
it "should compute the scope between 2 <w:t>" , () ->
174173
scope= xmlUtil.getListXmlElements """undefined</w:t></w:r></w:p><w:p w:rsidP="008A4B3C" w:rsidR="007929C1" w:rsidRDefault="007929C1" w:rsidRPr="008A4B3C"><w:pPr><w:pStyle w:val="Sous-titre"/></w:pPr><w:r w:rsidRPr="008A4B3C"><w:t xml:space="preserve">Audit réalisé le """
175174
expect(scope).toEqual([ { tag : '</w:t>', offset : 9 }, { tag : '</w:r>', offset : 15 }, { tag : '</w:p>', offset : 21 }, { tag : '<w:p>', offset : 27 }, { tag : '<w:r>', offset : 162 }, { tag : '<w:t>', offset : 188 } ])
@@ -181,7 +180,6 @@ describe "Xml Util" , () ->
181180
expect(scope).toEqual([ { tag : '</w:t>', offset : 8 }, { tag : '</w:r>', offset : 14 }, { tag : '</w:p>', offset : 20 }, { tag : '</w:tc>', offset : 26 }, { tag : '</w:tr>', offset : 33 }, { tag : '</w:tbl>', offset : 40 }, { tag : '<w:p>', offset : 188 }, { tag : '<w:r>', offset : 257 }, { tag : '<w:t>', offset : 306 } ])
182181

183182
describe "scope diff calculation", () ->
184-
xmlUtil= new XmlUtil()
185183
it "should compute the scopeDiff between 2 <w:t>" , () ->
186184
scope= xmlUtil.getListDifferenceXmlElements """undefined</w:t></w:r></w:p><w:p w:rsidP="008A4B3C" w:rsidR="007929C1" w:rsidRDefault="007929C1" w:rsidRPr="008A4B3C"><w:pPr><w:pStyle w:val="Sous-titre"/></w:pPr><w:r w:rsidRPr="008A4B3C"><w:t xml:space="preserve">Audit réalisé le """
187185
expect(scope).toEqual([])

coffee/xmlUtil.coffee

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
DocUtils=require('./docUtils')
2-
XmlUtil = class XmlUtil
3-
getListXmlElements: (text,start=0,end=text.length-1) ->
4-
###
5-
get the different closing and opening tags between two texts (doesn't take into account tags that are opened then closed (those that are closed then opened are returned)):
6-
returns:[{"tag":"</w:r>","offset":13},{"tag":"</w:p>","offset":265},{"tag":"</w:tc>","offset":271},{"tag":"<w:tc>","offset":828},{"tag":"<w:p>","offset":883},{"tag":"<w:r>","offset":1483}]
7-
###
8-
tags= DocUtils.preg_match_all("<(\/?[^/> ]+)([^>]*)>",text.substr(start,end)) #getThemAll (the opening and closing tags)!
9-
result=[]
10-
for tag,i in tags
11-
if tag[1][0]=='/' #closing tag
12-
justOpened= false
13-
if result.length>0
14-
lastTag= result[result.length-1]
15-
innerLastTag= lastTag.tag.substr(1,lastTag.tag.length-2)
16-
innerCurrentTag= tag[1].substr(1)
17-
if innerLastTag==innerCurrentTag then justOpened= true #tag was just opened
18-
if justOpened then result.pop() else result.push {tag:'<'+tag[1]+'>',offset:tag.offset}
19-
else if tag[2][tag[2].length-1]=='/' #open/closing tag aren't taken into account(for example <w:style/>)
20-
else #opening tag
21-
result.push {tag:'<'+tag[1]+'>',offset:tag.offset}
22-
result
23-
getListDifferenceXmlElements: (text,start=0,end=text.length-1) -> #it returns the difference between two scopes, ie simplifyes closes and opens. If it is not null, it means that the beginning is for example in a table, and the second one is not. If you hard copy this text, the XML will break
24-
scope= @getListXmlElements text,start,end
25-
while(1)
26-
if (scope.length<=1) #if scope.length==1, then they can't be an opeining and closing tag
27-
break;
28-
if ((scope[0]).tag.substr(2)==(scope[scope.length-1]).tag.substr(1)) #if the first closing is the same than the last opening, ie: [</tag>,...,<tag>]
29-
scope.pop() #remove both the first and the last one
30-
scope.shift()
31-
else break;
32-
scope
2+
XmlUtil={}
3+
4+
XmlUtil.getListXmlElements= (text,start=0,end=text.length-1) ->
5+
###
6+
get the different closing and opening tags between two texts (doesn't take into account tags that are opened then closed (those that are closed then opened are returned)):
7+
returns:[{"tag":"</w:r>","offset":13},{"tag":"</w:p>","offset":265},{"tag":"</w:tc>","offset":271},{"tag":"<w:tc>","offset":828},{"tag":"<w:p>","offset":883},{"tag":"<w:r>","offset":1483}]
8+
###
9+
tags= DocUtils.preg_match_all("<(\/?[^/> ]+)([^>]*)>",text.substr(start,end)) #getThemAll (the opening and closing tags)!
10+
result=[]
11+
for tag,i in tags
12+
if tag[1][0]=='/' #closing tag
13+
justOpened= false
14+
if result.length>0
15+
lastTag= result[result.length-1]
16+
innerLastTag= lastTag.tag.substr(1,lastTag.tag.length-2)
17+
innerCurrentTag= tag[1].substr(1)
18+
if innerLastTag==innerCurrentTag then justOpened= true #tag was just opened
19+
if justOpened then result.pop() else result.push {tag:'<'+tag[1]+'>',offset:tag.offset}
20+
else if tag[2][tag[2].length-1]=='/' #open/closing tag aren't taken into account(for example <w:style/>)
21+
else #opening tag
22+
result.push {tag:'<'+tag[1]+'>',offset:tag.offset}
23+
result
24+
XmlUtil.getListDifferenceXmlElements= (text,start=0,end=text.length-1) -> #it returns the difference between two scopes, ie simplifyes closes and opens. If it is not null, it means that the beginning is for example in a table, and the second one is not. If you hard copy this text, the XML will break
25+
scope= @getListXmlElements text,start,end
26+
while(1)
27+
if (scope.length<=1) #if scope.length==1, then they can't be an opeining and closing tag
28+
break;
29+
if ((scope[0]).tag.substr(2)==(scope[scope.length-1]).tag.substr(1)) #if the first closing is the same than the last opening, ie: [</tag>,...,<tag>]
30+
scope.pop() #remove both the first and the last one
31+
scope.shift()
32+
else break;
33+
scope
3334

3435
module.exports=XmlUtil

0 commit comments

Comments
 (0)