UEA-Prodem
This commit is contained in:
+88
@@ -0,0 +1,88 @@
|
||||
import { escapeAttribute } from "./escape-attribute";
|
||||
import { XmlText } from "./XmlText";
|
||||
export class XmlNode {
|
||||
name;
|
||||
children;
|
||||
attributes = {};
|
||||
static of(name, childText, withName) {
|
||||
const node = new XmlNode(name);
|
||||
if (childText !== undefined) {
|
||||
node.addChildNode(new XmlText(childText));
|
||||
}
|
||||
if (withName !== undefined) {
|
||||
node.withName(withName);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
constructor(name, children = []) {
|
||||
this.name = name;
|
||||
this.children = children;
|
||||
}
|
||||
withName(name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
addAttribute(name, value) {
|
||||
this.attributes[name] = value;
|
||||
return this;
|
||||
}
|
||||
addChildNode(child) {
|
||||
this.children.push(child);
|
||||
return this;
|
||||
}
|
||||
removeAttribute(name) {
|
||||
delete this.attributes[name];
|
||||
return this;
|
||||
}
|
||||
n(name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
c(child) {
|
||||
this.children.push(child);
|
||||
return this;
|
||||
}
|
||||
a(name, value) {
|
||||
if (value != null) {
|
||||
this.attributes[name] = value;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
cc(input, field, withName = field) {
|
||||
if (input[field] != null) {
|
||||
const node = XmlNode.of(field, input[field]).withName(withName);
|
||||
this.c(node);
|
||||
}
|
||||
}
|
||||
l(input, listName, memberName, valueProvider) {
|
||||
if (input[listName] != null) {
|
||||
const nodes = valueProvider();
|
||||
nodes.map((node) => {
|
||||
node.withName(memberName);
|
||||
this.c(node);
|
||||
});
|
||||
}
|
||||
}
|
||||
lc(input, listName, memberName, valueProvider) {
|
||||
if (input[listName] != null) {
|
||||
const nodes = valueProvider();
|
||||
const containerNode = new XmlNode(memberName);
|
||||
nodes.map((node) => {
|
||||
containerNode.c(node);
|
||||
});
|
||||
this.c(containerNode);
|
||||
}
|
||||
}
|
||||
toString() {
|
||||
const hasChildren = Boolean(this.children.length);
|
||||
let xmlText = `<${this.name}`;
|
||||
const attributes = this.attributes;
|
||||
for (const attributeName of Object.keys(attributes)) {
|
||||
const attribute = attributes[attributeName];
|
||||
if (attribute != null) {
|
||||
xmlText += ` ${attributeName}="${escapeAttribute("" + attribute)}"`;
|
||||
}
|
||||
}
|
||||
return (xmlText += !hasChildren ? "/>" : `>${this.children.map((c) => c.toString()).join("")}</${this.name}>`);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { escapeElement } from "./escape-element";
|
||||
export class XmlText {
|
||||
value;
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
toString() {
|
||||
return escapeElement("" + this.value);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
const ATTR_ESCAPE_RE = /[&<>"]/g;
|
||||
const ATTR_ESCAPE_MAP = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
};
|
||||
export function escapeAttribute(value) {
|
||||
return value.replace(ATTR_ESCAPE_RE, (ch) => ATTR_ESCAPE_MAP[ch]);
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
const ELEMENT_ESCAPE_RE = /[&"'<>\r\n\u0085\u2028]/g;
|
||||
const ELEMENT_ESCAPE_MAP = {
|
||||
"&": "&",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
"\r": "
",
|
||||
"\n": "
",
|
||||
"\u0085": "…",
|
||||
"\u2028": "
",
|
||||
};
|
||||
export function escapeElement(value) {
|
||||
return value.replace(ELEMENT_ESCAPE_RE, (ch) => ELEMENT_ESCAPE_MAP[ch]);
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export * from "./XmlNode";
|
||||
export * from "./XmlText";
|
||||
export { parseXML } from "./xml-parser";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
export const XML = {
|
||||
amp: "&",
|
||||
apos: "'",
|
||||
gt: ">",
|
||||
lt: "<",
|
||||
quot: '"',
|
||||
};
|
||||
export const COMMON_HTML = {
|
||||
nbsp: "\u00a0",
|
||||
copy: "\u00a9",
|
||||
reg: "\u00ae",
|
||||
trade: "\u2122",
|
||||
mdash: "\u2014",
|
||||
ndash: "\u2013",
|
||||
hellip: "\u2026",
|
||||
laquo: "\u00ab",
|
||||
raquo: "\u00bb",
|
||||
lsquo: "\u2018",
|
||||
rsquo: "\u2019",
|
||||
ldquo: "\u201c",
|
||||
rdquo: "\u201d",
|
||||
bull: "\u2022",
|
||||
para: "\u00b6",
|
||||
sect: "\u00a7",
|
||||
deg: "\u00b0",
|
||||
frac12: "\u00bd",
|
||||
frac14: "\u00bc",
|
||||
frac34: "\u00be",
|
||||
};
|
||||
export const CURRENCY = {
|
||||
cent: "\u00a2",
|
||||
pound: "\u00a3",
|
||||
curren: "\u00a4",
|
||||
yen: "\u00a5",
|
||||
euro: "\u20ac",
|
||||
dollar: "$",
|
||||
fnof: "\u0192",
|
||||
inr: "\u20b9",
|
||||
af: "\u060b",
|
||||
birr: "\u1265\u122d",
|
||||
peso: "\u20b1",
|
||||
rub: "\u20bd",
|
||||
won: "\u20a9",
|
||||
yuan: "\u00a5",
|
||||
cedil: "\u00b8",
|
||||
};
|
||||
const SPECIAL_CHARS = new Set("!?\\/[]$%{}^&*()<>|+");
|
||||
function validateEntityName(name) {
|
||||
if (name[0] === "#") {
|
||||
throw new Error(`[EntityReplacer] Invalid character '#' in entity name: "${name}"`);
|
||||
}
|
||||
for (const ch of name) {
|
||||
if (SPECIAL_CHARS.has(ch)) {
|
||||
throw new Error(`[EntityReplacer] Invalid character '${ch}' in entity name: "${name}"`);
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
function mergeEntityMaps(...maps) {
|
||||
const out = Object.create(null);
|
||||
for (const map of maps) {
|
||||
if (!map) {
|
||||
continue;
|
||||
}
|
||||
for (const key of Object.keys(map)) {
|
||||
const raw = map[key];
|
||||
if (typeof raw === "string") {
|
||||
out[key] = raw;
|
||||
}
|
||||
else if (raw && typeof raw === "object" && raw.val !== undefined) {
|
||||
const val = raw.val;
|
||||
if (typeof val === "string") {
|
||||
out[key] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const LIMIT_TIER_EXTERNAL = "external";
|
||||
const LIMIT_TIER_BASE = "base";
|
||||
const LIMIT_TIER_ALL = "all";
|
||||
function parseLimitTiers(raw) {
|
||||
if (!raw || raw === LIMIT_TIER_EXTERNAL) {
|
||||
return new Set([LIMIT_TIER_EXTERNAL]);
|
||||
}
|
||||
if (raw === LIMIT_TIER_ALL) {
|
||||
return new Set([LIMIT_TIER_ALL]);
|
||||
}
|
||||
if (raw === LIMIT_TIER_BASE) {
|
||||
return new Set([LIMIT_TIER_BASE]);
|
||||
}
|
||||
if (Array.isArray(raw)) {
|
||||
return new Set(raw);
|
||||
}
|
||||
return new Set([LIMIT_TIER_EXTERNAL]);
|
||||
}
|
||||
const NCR_LEVEL = Object.freeze({ allow: 0, leave: 1, remove: 2, throw: 3 });
|
||||
const XML10_ALLOWED_C0 = new Set([0x09, 0x0a, 0x0d]);
|
||||
function parseNCRConfig(ncr) {
|
||||
if (!ncr) {
|
||||
return { xmlVersion: 1.0, onLevel: NCR_LEVEL.allow, nullLevel: NCR_LEVEL.remove };
|
||||
}
|
||||
const xmlVersion = ncr.xmlVersion === 1.1 ? 1.1 : 1.0;
|
||||
const onLevel = NCR_LEVEL[ncr.onNCR ?? "allow"] ?? NCR_LEVEL.allow;
|
||||
const nullLevel = NCR_LEVEL[ncr.nullNCR ?? "remove"] ?? NCR_LEVEL.remove;
|
||||
const clampedNull = Math.max(nullLevel, NCR_LEVEL.remove);
|
||||
return { xmlVersion, onLevel, nullLevel: clampedNull };
|
||||
}
|
||||
export const EntityDecoderImpl = class EntityDecoderImpl {
|
||||
_limit;
|
||||
_maxTotalExpansions;
|
||||
_maxExpandedLength;
|
||||
_postCheck;
|
||||
_limitTiers;
|
||||
_numericAllowed;
|
||||
_baseMap;
|
||||
_externalMap;
|
||||
_inputMap;
|
||||
_totalExpansions;
|
||||
_expandedLength;
|
||||
_removeSet;
|
||||
_leaveSet;
|
||||
_ncrXmlVersion;
|
||||
_ncrOnLevel;
|
||||
_ncrNullLevel;
|
||||
constructor(options = {}) {
|
||||
this._limit = options.limit || {};
|
||||
this._maxTotalExpansions = this._limit.maxTotalExpansions || 0;
|
||||
this._maxExpandedLength = this._limit.maxExpandedLength || 0;
|
||||
this._postCheck = typeof options.postCheck === "function" ? options.postCheck : (r) => r;
|
||||
this._limitTiers = parseLimitTiers(this._limit.applyLimitsTo ?? LIMIT_TIER_EXTERNAL);
|
||||
this._numericAllowed = options.numericAllowed ?? true;
|
||||
this._baseMap = mergeEntityMaps(XML, options.namedEntities || null);
|
||||
this._externalMap = Object.create(null);
|
||||
this._inputMap = Object.create(null);
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
this._removeSet = new Set(options.remove && Array.isArray(options.remove) ? options.remove : []);
|
||||
this._leaveSet = new Set(options.leave && Array.isArray(options.leave) ? options.leave : []);
|
||||
const ncrCfg = parseNCRConfig(options.ncr);
|
||||
this._ncrXmlVersion = ncrCfg.xmlVersion;
|
||||
this._ncrOnLevel = ncrCfg.onLevel;
|
||||
this._ncrNullLevel = ncrCfg.nullLevel;
|
||||
}
|
||||
setExternalEntities(map) {
|
||||
if (map) {
|
||||
for (const key of Object.keys(map)) {
|
||||
validateEntityName(key);
|
||||
}
|
||||
}
|
||||
this._externalMap = mergeEntityMaps(map);
|
||||
}
|
||||
addExternalEntity(key, value) {
|
||||
validateEntityName(key);
|
||||
if (typeof value === "string" && value.indexOf("&") === -1) {
|
||||
this._externalMap[key] = value;
|
||||
}
|
||||
}
|
||||
addInputEntities(map) {
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
this._inputMap = mergeEntityMaps(map);
|
||||
}
|
||||
reset() {
|
||||
this._inputMap = Object.create(null);
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
return this;
|
||||
}
|
||||
setXmlVersion(version) {
|
||||
this._ncrXmlVersion = version === "1.1" || version === 1.1 ? 1.1 : 1.0;
|
||||
}
|
||||
decode(str) {
|
||||
if (typeof str !== "string" || str.length === 0) {
|
||||
return str;
|
||||
}
|
||||
const original = str;
|
||||
const chunks = [];
|
||||
const len = str.length;
|
||||
let last = 0;
|
||||
let i = 0;
|
||||
const limitExpansions = this._maxTotalExpansions > 0;
|
||||
const limitLength = this._maxExpandedLength > 0;
|
||||
const checkLimits = limitExpansions || limitLength;
|
||||
while (i < len) {
|
||||
if (str.charCodeAt(i) !== 38) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let j = i + 1;
|
||||
while (j < len && str.charCodeAt(j) !== 59 && j - i <= 32) {
|
||||
j++;
|
||||
}
|
||||
if (j >= len || str.charCodeAt(j) !== 59) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
const token = str.slice(i + 1, j);
|
||||
if (token.length === 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let replacement;
|
||||
let tier;
|
||||
if (this._removeSet.has(token)) {
|
||||
replacement = "";
|
||||
if (tier === undefined) {
|
||||
tier = LIMIT_TIER_EXTERNAL;
|
||||
}
|
||||
}
|
||||
else if (this._leaveSet.has(token)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
else if (token.charCodeAt(0) === 35) {
|
||||
const ncrResult = this._resolveNCR(token);
|
||||
if (ncrResult === undefined) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
replacement = ncrResult;
|
||||
tier = LIMIT_TIER_BASE;
|
||||
}
|
||||
else {
|
||||
const resolved = this._resolveName(token);
|
||||
replacement = resolved?.value;
|
||||
tier = resolved?.tier;
|
||||
}
|
||||
if (replacement === undefined) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (i > last) {
|
||||
chunks.push(str.slice(last, i));
|
||||
}
|
||||
chunks.push(replacement);
|
||||
last = j + 1;
|
||||
i = last;
|
||||
if (checkLimits && this._tierCounts(tier)) {
|
||||
if (limitExpansions) {
|
||||
this._totalExpansions++;
|
||||
if (this._totalExpansions > this._maxTotalExpansions) {
|
||||
throw new Error(`[EntityReplacer] Entity expansion count limit exceeded: ` +
|
||||
`${this._totalExpansions} > ${this._maxTotalExpansions}`);
|
||||
}
|
||||
}
|
||||
if (limitLength) {
|
||||
const delta = replacement.length - (token.length + 2);
|
||||
if (delta > 0) {
|
||||
this._expandedLength += delta;
|
||||
if (this._expandedLength > this._maxExpandedLength) {
|
||||
throw new Error(`[EntityReplacer] Expanded content length limit exceeded: ` +
|
||||
`${this._expandedLength} > ${this._maxExpandedLength}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (last < len) {
|
||||
chunks.push(str.slice(last));
|
||||
}
|
||||
const result = chunks.length === 0 ? str : chunks.join("");
|
||||
return this._postCheck(result, original);
|
||||
}
|
||||
_tierCounts(tier) {
|
||||
if (this._limitTiers.has(LIMIT_TIER_ALL)) {
|
||||
return true;
|
||||
}
|
||||
return this._limitTiers.has(tier);
|
||||
}
|
||||
_resolveName(name) {
|
||||
if (name in this._inputMap) {
|
||||
return { value: this._inputMap[name], tier: LIMIT_TIER_EXTERNAL };
|
||||
}
|
||||
if (name in this._externalMap) {
|
||||
return { value: this._externalMap[name], tier: LIMIT_TIER_EXTERNAL };
|
||||
}
|
||||
if (name in this._baseMap) {
|
||||
return { value: this._baseMap[name], tier: LIMIT_TIER_BASE };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
_classifyNCR(cp) {
|
||||
if (cp === 0) {
|
||||
return this._ncrNullLevel;
|
||||
}
|
||||
if (cp >= 0xd800 && cp <= 0xdfff) {
|
||||
return NCR_LEVEL.remove;
|
||||
}
|
||||
if (this._ncrXmlVersion === 1.0) {
|
||||
if (cp >= 0x01 && cp <= 0x1f && !XML10_ALLOWED_C0.has(cp)) {
|
||||
return NCR_LEVEL.remove;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
_applyNCRAction(action, token, cp) {
|
||||
switch (action) {
|
||||
case NCR_LEVEL.allow:
|
||||
return String.fromCodePoint(cp);
|
||||
case NCR_LEVEL.remove:
|
||||
return "";
|
||||
case NCR_LEVEL.leave:
|
||||
return undefined;
|
||||
case NCR_LEVEL.throw:
|
||||
throw new Error(`[EntityDecoder] Prohibited numeric character reference ` +
|
||||
`&${token}; (U+${cp.toString(16).toUpperCase().padStart(4, "0")})`);
|
||||
default:
|
||||
return String.fromCodePoint(cp);
|
||||
}
|
||||
}
|
||||
_resolveNCR(token) {
|
||||
const second = token.charCodeAt(1);
|
||||
let cp;
|
||||
if (second === 120 || second === 88) {
|
||||
cp = parseInt(token.slice(2), 16);
|
||||
}
|
||||
else {
|
||||
cp = parseInt(token.slice(1), 10);
|
||||
}
|
||||
if (Number.isNaN(cp) || cp < 0 || cp > 0x10ffff) {
|
||||
return undefined;
|
||||
}
|
||||
const minimum = this._classifyNCR(cp);
|
||||
if (!this._numericAllowed && minimum < NCR_LEVEL.remove) {
|
||||
return undefined;
|
||||
}
|
||||
const effective = minimum === -1 ? this._ncrOnLevel : Math.max(this._ncrOnLevel, minimum);
|
||||
return this._applyNCRAction(effective, token, cp);
|
||||
}
|
||||
};
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
let parser;
|
||||
export function parseXML(xmlString) {
|
||||
if (!parser) {
|
||||
parser = new DOMParser();
|
||||
}
|
||||
const xmlDocument = parser.parseFromString(xmlString, "application/xml");
|
||||
if (xmlDocument.getElementsByTagName("parsererror").length > 0) {
|
||||
throw new Error("DOMParser XML parsing error.");
|
||||
}
|
||||
const xmlToObj = (node) => {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
if (node.textContent?.trim()) {
|
||||
return node.textContent;
|
||||
}
|
||||
}
|
||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
const element = node;
|
||||
if (element.attributes.length === 0 && element.childNodes.length === 0) {
|
||||
return "";
|
||||
}
|
||||
const obj = {};
|
||||
const attributes = Array.from(element.attributes);
|
||||
for (const attr of attributes) {
|
||||
obj[`${attr.name}`] = attr.value;
|
||||
}
|
||||
const childNodes = Array.from(element.childNodes);
|
||||
for (const child of childNodes) {
|
||||
const childResult = xmlToObj(child);
|
||||
if (childResult != null) {
|
||||
const childName = child.nodeName;
|
||||
if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") {
|
||||
return childResult;
|
||||
}
|
||||
if (obj[childName]) {
|
||||
if (Array.isArray(obj[childName])) {
|
||||
obj[childName].push(childResult);
|
||||
}
|
||||
else {
|
||||
obj[childName] = [obj[childName], childResult];
|
||||
}
|
||||
}
|
||||
else {
|
||||
obj[childName] = childResult;
|
||||
}
|
||||
}
|
||||
else if (childNodes.length === 1 && attributes.length === 0) {
|
||||
return element.textContent;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return {
|
||||
[xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement),
|
||||
};
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import { XMLParser } from "fast-xml-parser";
|
||||
import { COMMON_HTML, CURRENCY, EntityDecoderImpl, XML } from "./xml-external/nodable_entities";
|
||||
const entityDecoder = new EntityDecoderImpl({
|
||||
namedEntities: { ...XML, ...COMMON_HTML, ...CURRENCY },
|
||||
numericAllowed: true,
|
||||
limit: {
|
||||
maxTotalExpansions: Infinity,
|
||||
},
|
||||
ncr: {
|
||||
xmlVersion: 1.1,
|
||||
},
|
||||
});
|
||||
const parser = new XMLParser({
|
||||
attributeNamePrefix: "",
|
||||
processEntities: {
|
||||
enabled: true,
|
||||
maxTotalExpansions: Infinity,
|
||||
},
|
||||
htmlEntities: true,
|
||||
entityDecoder: {
|
||||
setExternalEntities: (entities) => {
|
||||
entityDecoder.setExternalEntities(entities);
|
||||
},
|
||||
addInputEntities: (entities) => {
|
||||
entityDecoder.addInputEntities(entities);
|
||||
},
|
||||
reset: () => {
|
||||
entityDecoder.reset();
|
||||
},
|
||||
decode: (text) => {
|
||||
return entityDecoder.decode(text);
|
||||
},
|
||||
setXmlVersion: (version) => void {},
|
||||
},
|
||||
ignoreAttributes: false,
|
||||
ignoreDeclaration: true,
|
||||
parseTagValue: false,
|
||||
trimValues: false,
|
||||
tagValueProcessor: (_, val) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
|
||||
maxNestedTags: Infinity,
|
||||
});
|
||||
export function parseXML(xmlString) {
|
||||
return parser.parse(xmlString, true);
|
||||
}
|
||||
Reference in New Issue
Block a user