|
1 | 1 | 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 |
33 | 34 |
|
34 | 35 | module.exports=XmlUtil |
0 commit comments