429 lines
13 KiB
JavaScript
429 lines
13 KiB
JavaScript
"use strict";
|
|
|
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
var _require = require("@xmldom/xmldom"),
|
|
DOMParser = _require.DOMParser,
|
|
XMLSerializer = _require.XMLSerializer;
|
|
var _require2 = require("./errors.js"),
|
|
throwXmlTagNotFound = _require2.throwXmlTagNotFound;
|
|
var _require3 = require("./utils.js"),
|
|
last = _require3.last,
|
|
first = _require3.first;
|
|
function isWhiteSpace(value) {
|
|
return /^[ \n\r\t]+$/.test(value);
|
|
}
|
|
function parser(tag) {
|
|
return {
|
|
get: function get(scope) {
|
|
if (tag === ".") {
|
|
return scope;
|
|
}
|
|
if (scope) {
|
|
return scope[tag];
|
|
}
|
|
return scope;
|
|
}
|
|
};
|
|
}
|
|
var attrToRegex = {};
|
|
function setSingleAttribute(partValue, attr, attrValue) {
|
|
var regex;
|
|
// Stryker disable next-line all : because this is an optimisation
|
|
if (attrToRegex[attr]) {
|
|
regex = attrToRegex[attr];
|
|
} else {
|
|
regex = new RegExp("(<.* ".concat(attr, "=\")([^\"]*)(\".*)$"));
|
|
attrToRegex[attr] = regex;
|
|
}
|
|
if (regex.test(partValue)) {
|
|
return partValue.replace(regex, "$1".concat(attrValue, "$3"));
|
|
}
|
|
var end = partValue.lastIndexOf("/>");
|
|
if (end === -1) {
|
|
end = partValue.lastIndexOf(">");
|
|
}
|
|
return partValue.substr(0, end) + " ".concat(attr, "=\"").concat(attrValue, "\"") + partValue.substr(end);
|
|
}
|
|
function getSingleAttribute(value, attributeName) {
|
|
var index = value.indexOf(" ".concat(attributeName, "=\""));
|
|
if (index === -1) {
|
|
return null;
|
|
}
|
|
var startIndex = value.substr(index).search(/["']/) + index;
|
|
var endIndex = value.substr(startIndex + 1).search(/["']/) + startIndex;
|
|
return value.substr(startIndex + 1, endIndex - startIndex);
|
|
}
|
|
function endsWith(str, suffix) {
|
|
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
|
}
|
|
function startsWith(str, prefix) {
|
|
return str.substring(0, prefix.length) === prefix;
|
|
}
|
|
function getDuplicates(arr) {
|
|
var duplicates = [];
|
|
var hash = {},
|
|
result = [];
|
|
for (var i = 0, l = arr.length; i < l; ++i) {
|
|
if (!hash[arr[i]]) {
|
|
hash[arr[i]] = true;
|
|
result.push(arr[i]);
|
|
} else {
|
|
duplicates.push(arr[i]);
|
|
}
|
|
}
|
|
return duplicates;
|
|
}
|
|
function uniq(arr) {
|
|
var hash = {},
|
|
result = [];
|
|
for (var i = 0, l = arr.length; i < l; ++i) {
|
|
if (!hash[arr[i]]) {
|
|
hash[arr[i]] = true;
|
|
result.push(arr[i]);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
function chunkBy(parsed, f) {
|
|
return parsed.reduce(function (chunks, p) {
|
|
var currentChunk = last(chunks);
|
|
var res = f(p);
|
|
if (res === "start") {
|
|
chunks.push([p]);
|
|
} else if (res === "end") {
|
|
currentChunk.push(p);
|
|
chunks.push([]);
|
|
} else {
|
|
currentChunk.push(p);
|
|
}
|
|
return chunks;
|
|
}, [[]]).filter(function (p) {
|
|
return p.length > 0;
|
|
});
|
|
}
|
|
var defaults = {
|
|
errorLogging: "json",
|
|
paragraphLoop: false,
|
|
nullGetter: function nullGetter(part) {
|
|
return part.module ? "" : "undefined";
|
|
},
|
|
xmlFileNames: ["[Content_Types].xml"],
|
|
parser: parser,
|
|
linebreaks: false,
|
|
fileTypeConfig: null,
|
|
delimiters: {
|
|
start: "{",
|
|
end: "}"
|
|
},
|
|
syntax: {}
|
|
};
|
|
function xml2str(xmlNode) {
|
|
var a = new XMLSerializer();
|
|
return a.serializeToString(xmlNode).replace(/xmlns(:[a-z0-9]+)?="" ?/g, "");
|
|
}
|
|
function str2xml(str) {
|
|
if (str.charCodeAt(0) === 65279) {
|
|
// BOM sequence
|
|
str = str.substr(1);
|
|
}
|
|
return new DOMParser().parseFromString(str, "text/xml");
|
|
}
|
|
var charMap = [["&", "&"], ["<", "<"], [">", ">"], ['"', """], ["'", "'"]];
|
|
var charMapRegexes = charMap.map(function (_ref) {
|
|
var _ref2 = _slicedToArray(_ref, 2),
|
|
endChar = _ref2[0],
|
|
startChar = _ref2[1];
|
|
return {
|
|
rstart: new RegExp(startChar, "g"),
|
|
rend: new RegExp(endChar, "g"),
|
|
start: startChar,
|
|
end: endChar
|
|
};
|
|
});
|
|
function wordToUtf8(string) {
|
|
var r;
|
|
for (var i = charMapRegexes.length - 1; i >= 0; i--) {
|
|
r = charMapRegexes[i];
|
|
string = string.replace(r.rstart, r.end);
|
|
}
|
|
return string;
|
|
}
|
|
function utf8ToWord(string) {
|
|
// To make sure that the object given is a string (this is a noop for strings).
|
|
string = string.toString();
|
|
var r;
|
|
for (var i = 0, l = charMapRegexes.length; i < l; i++) {
|
|
r = charMapRegexes[i];
|
|
string = string.replace(r.rend, r.start);
|
|
}
|
|
return string;
|
|
}
|
|
|
|
// This function is written with for loops for performance
|
|
function concatArrays(arrays) {
|
|
var result = [];
|
|
for (var i = 0; i < arrays.length; i++) {
|
|
var array = arrays[i];
|
|
for (var j = 0, len = array.length; j < len; j++) {
|
|
result.push(array[j]);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
var spaceRegexp = new RegExp(String.fromCharCode(160), "g");
|
|
function convertSpaces(s) {
|
|
return s.replace(spaceRegexp, " ");
|
|
}
|
|
function pregMatchAll(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}
|
|
]
|
|
*/
|
|
var matchArray = [];
|
|
var match;
|
|
while ((match = regex.exec(content)) != null) {
|
|
matchArray.push({
|
|
array: match,
|
|
offset: match.index
|
|
});
|
|
}
|
|
return matchArray;
|
|
}
|
|
function isEnding(value, element) {
|
|
return value === "</" + element + ">";
|
|
}
|
|
function isStarting(value, element) {
|
|
return value.indexOf("<" + element) === 0 && [">", " ", "/"].indexOf(value[element.length + 1]) !== -1;
|
|
}
|
|
function getRight(parsed, element, index) {
|
|
var val = getRightOrNull(parsed, element, index);
|
|
if (val !== null) {
|
|
return val;
|
|
}
|
|
throwXmlTagNotFound({
|
|
position: "right",
|
|
element: element,
|
|
parsed: parsed,
|
|
index: index
|
|
});
|
|
}
|
|
function getRightOrNull(parsed, elements, index) {
|
|
if (typeof elements === "string") {
|
|
elements = [elements];
|
|
}
|
|
var level = 1;
|
|
for (var i = index, l = parsed.length; i < l; i++) {
|
|
var part = parsed[i];
|
|
for (var j = 0, len = elements.length; j < len; j++) {
|
|
var element = elements[j];
|
|
if (isEnding(part.value, element)) {
|
|
level--;
|
|
}
|
|
if (isStarting(part.value, element)) {
|
|
level++;
|
|
}
|
|
if (level === 0) {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
function getLeft(parsed, element, index) {
|
|
var val = getLeftOrNull(parsed, element, index);
|
|
if (val !== null) {
|
|
return val;
|
|
}
|
|
throwXmlTagNotFound({
|
|
position: "left",
|
|
element: element,
|
|
parsed: parsed,
|
|
index: index
|
|
});
|
|
}
|
|
function getLeftOrNull(parsed, elements, index) {
|
|
if (typeof elements === "string") {
|
|
elements = [elements];
|
|
}
|
|
var level = 1;
|
|
for (var i = index; i >= 0; i--) {
|
|
var part = parsed[i];
|
|
for (var j = 0, len = elements.length; j < len; j++) {
|
|
var element = elements[j];
|
|
if (isStarting(part.value, element)) {
|
|
level--;
|
|
}
|
|
if (isEnding(part.value, element)) {
|
|
level++;
|
|
}
|
|
if (level === 0) {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Stryker disable all : because those are functions that depend on the parsed
|
|
// structure based and we don't want minimal code here, but rather code that
|
|
// makes things clear.
|
|
function isTagStart(tagType, _ref3) {
|
|
var type = _ref3.type,
|
|
tag = _ref3.tag,
|
|
position = _ref3.position;
|
|
return type === "tag" && tag === tagType && (position === "start" || position === "selfclosing");
|
|
}
|
|
function isTagStartStrict(tagType, _ref4) {
|
|
var type = _ref4.type,
|
|
tag = _ref4.tag,
|
|
position = _ref4.position;
|
|
return type === "tag" && tag === tagType && position === "start";
|
|
}
|
|
function isTagEnd(tagType, _ref5) {
|
|
var type = _ref5.type,
|
|
tag = _ref5.tag,
|
|
position = _ref5.position;
|
|
return type === "tag" && tag === tagType && position === "end";
|
|
}
|
|
function isParagraphStart(part) {
|
|
return isTagStartStrict("w:p", part) || isTagStartStrict("a:p", part);
|
|
}
|
|
function isParagraphEnd(part) {
|
|
return isTagEnd("w:p", part) || isTagEnd("a:p", part);
|
|
}
|
|
function isTextStart(_ref6) {
|
|
var type = _ref6.type,
|
|
position = _ref6.position,
|
|
text = _ref6.text;
|
|
return type === "tag" && position === "start" && text;
|
|
}
|
|
function isTextEnd(_ref7) {
|
|
var type = _ref7.type,
|
|
position = _ref7.position,
|
|
text = _ref7.text;
|
|
return type === "tag" && position === "end" && text;
|
|
}
|
|
function isContent(_ref8) {
|
|
var type = _ref8.type,
|
|
position = _ref8.position;
|
|
return type === "placeholder" || type === "content" && position === "insidetag";
|
|
}
|
|
function isModule(_ref9, modules) {
|
|
var module = _ref9.module,
|
|
type = _ref9.type;
|
|
if (!(modules instanceof Array)) {
|
|
modules = [modules];
|
|
}
|
|
return type === "placeholder" && modules.indexOf(module) !== -1;
|
|
}
|
|
// Stryker restore all
|
|
|
|
var corruptCharacters = /[\x00-\x08\x0B\x0C\x0E-\x1F]/;
|
|
// 00 NUL '\0' (null character)
|
|
// 01 SOH (start of heading)
|
|
// 02 STX (start of text)
|
|
// 03 ETX (end of text)
|
|
// 04 EOT (end of transmission)
|
|
// 05 ENQ (enquiry)
|
|
// 06 ACK (acknowledge)
|
|
// 07 BEL '\a' (bell)
|
|
// 08 BS '\b' (backspace)
|
|
// 0B VT '\v' (vertical tab)
|
|
// 0C FF '\f' (form feed)
|
|
// 0E SO (shift out)
|
|
// 0F SI (shift in)
|
|
// 10 DLE (data link escape)
|
|
// 11 DC1 (device control 1)
|
|
// 12 DC2 (device control 2)
|
|
// 13 DC3 (device control 3)
|
|
// 14 DC4 (device control 4)
|
|
// 15 NAK (negative ack.)
|
|
// 16 SYN (synchronous idle)
|
|
// 17 ETB (end of trans. blk)
|
|
// 18 CAN (cancel)
|
|
// 19 EM (end of medium)
|
|
// 1A SUB (substitute)
|
|
// 1B ESC (escape)
|
|
// 1C FS (file separator)
|
|
// 1D GS (group separator)
|
|
// 1E RS (record separator)
|
|
// 1F US (unit separator)
|
|
function hasCorruptCharacters(string) {
|
|
return corruptCharacters.test(string);
|
|
}
|
|
function invertMap(map) {
|
|
return Object.keys(map).reduce(function (invertedMap, key) {
|
|
var value = map[key];
|
|
invertedMap[value] = invertedMap[value] || [];
|
|
invertedMap[value].push(key);
|
|
return invertedMap;
|
|
}, {});
|
|
}
|
|
// This ensures that the sort is stable. The default Array.sort of the browser
|
|
// is not stable in firefox, as the JS spec does not enforce the sort to be
|
|
// stable.
|
|
function stableSort(arr, compare) {
|
|
// Stryker disable all : in previous versions of Chrome, sort was not stable by itself, so we had to add this. This is to support older versions of JS runners.
|
|
return arr.map(function (item, index) {
|
|
return {
|
|
item: item,
|
|
index: index
|
|
};
|
|
}).sort(function (a, b) {
|
|
return compare(a.item, b.item) || a.index - b.index;
|
|
}).map(function (_ref10) {
|
|
var item = _ref10.item;
|
|
return item;
|
|
});
|
|
// Stryker restore all
|
|
}
|
|
module.exports = {
|
|
endsWith: endsWith,
|
|
startsWith: startsWith,
|
|
isContent: isContent,
|
|
isParagraphStart: isParagraphStart,
|
|
isParagraphEnd: isParagraphEnd,
|
|
isTagStart: isTagStart,
|
|
isTagEnd: isTagEnd,
|
|
isTextStart: isTextStart,
|
|
isTextEnd: isTextEnd,
|
|
isStarting: isStarting,
|
|
isEnding: isEnding,
|
|
isModule: isModule,
|
|
uniq: uniq,
|
|
getDuplicates: getDuplicates,
|
|
chunkBy: chunkBy,
|
|
last: last,
|
|
first: first,
|
|
xml2str: xml2str,
|
|
str2xml: str2xml,
|
|
getRightOrNull: getRightOrNull,
|
|
getRight: getRight,
|
|
getLeftOrNull: getLeftOrNull,
|
|
getLeft: getLeft,
|
|
pregMatchAll: pregMatchAll,
|
|
convertSpaces: convertSpaces,
|
|
charMapRegexes: charMapRegexes,
|
|
hasCorruptCharacters: hasCorruptCharacters,
|
|
defaults: defaults,
|
|
wordToUtf8: wordToUtf8,
|
|
utf8ToWord: utf8ToWord,
|
|
concatArrays: concatArrays,
|
|
invertMap: invertMap,
|
|
charMap: charMap,
|
|
getSingleAttribute: getSingleAttribute,
|
|
setSingleAttribute: setSingleAttribute,
|
|
isWhiteSpace: isWhiteSpace,
|
|
stableSort: stableSort
|
|
}; |