UNPKG

ec-sri-invoice-signer

Version:
146 lines (145 loc) 5.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processAttributeValue = processAttributeValue; exports.processTagValue = processTagValue; const fast_xml_parser_1 = require("fast-xml-parser"); const utils_1 = require("../utils/utils"); function normalizeWhitespaceInAttributeValue(value) { const trimmed = value.replace(/&#x20/g, ' '); return trimmed; } function encodeEntitiesInTagValue(value) { const encodings = { "&": "&amp;", "<": "&lt;", ">": "&gt;", "\r": "&#xD;" }; return value.replace(/([<>\r])/gm, function (match, offset) { return encodings[match]; }) // Replace the ampersand only if it's not part of an entity .replace(/&(?!(#x[aA-fF\d]+;)|amp;|lt;|gt;)/gm, function (match, offset) { return encodings[match]; }); } function decodeUtf8HexEntitiesInTagValue(value) { var _a; const notDecodedEntities = new Set(['&#xD;']); const entitiesToReplace = (_a = value.match(/&#x[aA-fF\d]+;/gm)) !== null && _a !== void 0 ? _a : []; let newValue = value; for (const entity of entitiesToReplace) { if (!notDecodedEntities.has(entity)) { const hexToDecode = entity.match(/[aA-fF\d]+/)[0]; const decimalToDecode = parseInt(hexToDecode, 16); const decoded = String.fromCodePoint(decimalToDecode); newValue = newValue.replace(entity, decoded); } } return newValue; } function decodeUtf8EntitiesInTagValue(value) { var _a; const notDecodedEntities = new Set(['&amp;', '&lt;', '&gt;']); const entitiesToReplace = (_a = value.match(/&([aA-zZ]+);/gm)) !== null && _a !== void 0 ? _a : []; let newValue = value; for (const entity of entitiesToReplace) { if (!notDecodedEntities.has(entity)) { const decoded = new TextDecoder('utf-8').decode(new Uint8Array([parseInt(entity.slice(3), 16)])); newValue = newValue.replace(entity, decoded); } } return newValue; } /** * Replace characters with their respective XML entities. * & is a special case, as it's not replaced if it's part of an entity. */ function encodeEntitiesInAttributeValue(value) { const encodings = { "&": "&amp;", "<": "&lt;", '"': "&quot;", "\r": "&#xD;", "\n": "&#xA;", "\t": "&#x9;" }; // Replace the ampersand only if it's not part of an entity return value.replace(/&(?!(#x[aA-fF\d]+;)|amp;|quot;|lt;|apos;)/gm, function (match) { return encodings[match]; }).replace(/[<"\r\n\t]/gm, function (match) { return encodings[match]; }); } function decodeUtf8HexEntitiesInAttributeValue(value) { var _a; const notDecodedEntities = new Set(['&#xD;', '&#xA;', '&#x9;']); const entitiesToReplace = (_a = value.match(/&#x[aA-fF\d]+;/gm)) !== null && _a !== void 0 ? _a : []; let newValue = value; for (const entity of entitiesToReplace) { if (!notDecodedEntities.has(entity)) { const hexToDecode = entity.match(/[aA-fF\d]+/)[0]; const decimalToDecode = parseInt(hexToDecode, 16); const decoded = String.fromCodePoint(decimalToDecode); newValue = newValue.replace(entity, decoded); } } return newValue; } function decodeUtf8EntitiesInAttributeValue(value) { var _a, _b; const notDecodedEntities = new Set(['&amp;', '&lt;', '&quot;']); const entitiesToReplace = (_a = value.match(/&[aA-zZ]+;/gm)) !== null && _a !== void 0 ? _a : []; let newValue = value; for (const entity of entitiesToReplace) { if (!notDecodedEntities.has(entity)) { const decoded = (_b = new fast_xml_parser_1.XMLParser().parse(`<body>${entity}</body>`).body) !== null && _b !== void 0 ? _b : ''; newValue = newValue.replace(entity, decoded); } } return newValue; } function removeLeadingZerosInHexEntity(value) { return value.replace(/0+(?=[\daA-fF]+;)/g, ''); } function setCapitalsInHexEntities(value) { var _a; const entitiesToReplace = (_a = value.match(/&#x[aA-fF\d]+;/gm)) !== null && _a !== void 0 ? _a : []; let newValue = value; for (const entity of entitiesToReplace) { const entityWithoutLeadingZeros = removeLeadingZerosInHexEntity(entity); newValue = newValue.replace(entity, `&#x${entityWithoutLeadingZeros.slice(3).toUpperCase()}`); } return newValue; } function convertDecimalEntitiesIntoHexEntities(value) { var _a; const entitiesToReplace = (_a = value.match(/&#\d+;/gm)) !== null && _a !== void 0 ? _a : []; let newValue = value; for (const entity of entitiesToReplace) { const hexValue = Number(entity.slice(2, -1)).toString(16).toUpperCase(); newValue = newValue.replace(entity, `&#x${hexValue};`); } return newValue; } function processAttributeValue(value) { const processingSteps = [ setCapitalsInHexEntities, convertDecimalEntitiesIntoHexEntities, encodeEntitiesInAttributeValue, decodeUtf8HexEntitiesInAttributeValue, decodeUtf8EntitiesInAttributeValue, normalizeWhitespaceInAttributeValue ]; return (0, utils_1.pipe)(processingSteps)(value); } function processTagValue(value) { const processingSteps = [ setCapitalsInHexEntities, convertDecimalEntitiesIntoHexEntities, encodeEntitiesInTagValue, decodeUtf8HexEntitiesInTagValue, decodeUtf8EntitiesInTagValue ]; return (0, utils_1.pipe)(processingSteps)(value); }