| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 |
1×
1×
1×
1×
1×
1×
77×
315×
16×
299×
1×
17×
16×
1×
1×
1×
112×
112×
112×
336×
336×
336×
1232×
112×
1×
1×
1×
1×
1×
1×
1×
8×
1×
4×
4×
1×
365×
365×
1460×
1460×
365×
1×
252×
7×
252×
252×
1008×
1008×
252×
1×
69×
1×
283×
572×
572×
1×
1×
63×
1×
55×
55×
55×
595×
55×
1×
1×
1×
1×
1×
1×
1×
23×
163×
163×
23×
1×
23×
133×
133×
22×
1×
1×
| "use strict";
const memoize = require("./memoize");
const DOMParser = require("xmldom").DOMParser;
const XMLSerializer = require("xmldom").XMLSerializer;
const Errors = require("./errors");
const DocUtils = {};
function parser(tag) {
return {
["get"](scope) {
if (tag === ".") {
return scope;
}
return scope[tag];
},
};
}
DocUtils.defaults = {
nullGetter(part) {
if (!part.module) {
return "undefined";
}
Eif (part.module === "rawxml") {
return "";
}
return "";
},
parser: memoize(parser),
delimiters: {
start: "{",
end: "}",
},
};
DocUtils.mergeObjects = function () {
const resObj = {};
let obj, keys;
for(let i = 0; i < arguments.length; i += 1) {
obj = arguments[i];
keys = Object.keys(obj);
for(let j = 0; j < keys.length; j += 1) {
resObj[keys[j]] = obj[keys[j]];
}
}
return resObj;
};
DocUtils.xml2str = function (xmlNode) {
const a = new XMLSerializer();
return a.serializeToString(xmlNode);
};
DocUtils.decodeUtf8 = function (s) {
try {
if (s === undefined) { return undefined; }
// replace Ascii 160 space by the normal space, Ascii 32
return decodeURIComponent(escape(DocUtils.convertSpaces(s)));
}
catch (e) {
const err = new Error("End");
err.properties.data = s;
err.properties.explanation = "Could not decode string to UTF8";
throw err;
}
};
DocUtils.encodeUtf8 = function (s) {
return unescape(encodeURIComponent(s));
};
DocUtils.str2xml = function (str, errorHandler) {
const parser = new DOMParser({errorHandler});
return parser.parseFromString(str, "text/xml");
};
DocUtils.charMap = {
"&": "&",
"'": "'",
"<": "<",
">": ">",
};
const regexStripRegexp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
DocUtils.escapeRegExp = function (str) {
return str.replace(regexStripRegexp, "\\$&");
};
DocUtils.charMapRegexes = Object.keys(DocUtils.charMap).map(function (endChar) {
const startChar = DocUtils.charMap[endChar];
return {
rstart: new RegExp(DocUtils.escapeRegExp(startChar), "g"),
rend: new RegExp(DocUtils.escapeRegExp(endChar), "g"),
start: startChar,
end: endChar,
};
});
DocUtils.wordToUtf8 = function (string) {
let r;
for (let i = 0, l = DocUtils.charMapRegexes.length; i < l; i++) {
r = DocUtils.charMapRegexes[i];
string = string.replace(r.rstart, r.end);
}
return string;
};
DocUtils.utf8ToWord = function (string) {
if (typeof string !== "string") {
string = string.toString();
}
let r;
for (let i = 0, l = DocUtils.charMapRegexes.length; i < l; i++) {
r = DocUtils.charMapRegexes[i];
string = string.replace(r.rend, r.start);
}
return string;
};
DocUtils.cloneDeep = function (obj) {
return JSON.parse(JSON.stringify(obj));
};
DocUtils.concatArrays = function (arrays) {
return arrays.reduce(function (result, array) {
Array.prototype.push.apply(result, array);
return result;
}, []);
};
const spaceRegexp = new RegExp(String.fromCharCode(160), "g");
DocUtils.convertSpaces = function (s) {
return s.replace(spaceRegexp, " ");
};
DocUtils.pregMatchAll = function (regex, content) {
/* regex is a string, content is the content. It returns an array of all matches with their offset, for example:
regex=la
content=lolalolilala
returns: [{array: {0: 'la'},offset: 2},{array: {0: 'la'},offset: 8},{array: {0: 'la'} ,offset: 10}]
*/
const matchArray = [];
let match;
while ((match = regex.exec(content)) != null) {
matchArray.push({array: match, offset: match.index});
}
return matchArray;
};
DocUtils.sizeOfObject = function (obj) {
return Object.keys(obj).length;
};
function throwXmlTagNotFound(options) {
const err = new Errors.XTTemplateError(`No tag '${options.element}' was found at the ${options.position}`);
err.properties = {
id: `no_xml_tag_found_at_${options.position}`,
explanation: `No tag '${options.element}' was found at the ${options.position}`,
parsed: options.parsed,
index: options.index,
element: options.element,
};
throw err;
}
DocUtils.getRight = function (parsed, element, index) {
for (let i = index, l = parsed.length; i < l; i++) {
const part = parsed[i];
if (part.value === "</" + element + ">") {
return i;
}
}
throwXmlTagNotFound({position: "right", element, parsed, index});
};
DocUtils.getLeft = function (parsed, element, index) {
for (let i = index; i >= 0; i--) {
const part = parsed[i];
if (part.value.indexOf("<" + element) === 0 && [">", " "].indexOf(part.value[element.length + 1]) !== -1) {
return i;
}
}
throwXmlTagNotFound({position: "left", element, parsed, index});
};
module.exports = DocUtils;
|