Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from "./xml.common.js";
//# sourceMappingURL=index.d.ts.map
+5
View File
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,iBAAiB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { stringifyXML, parseXML } from \"./xml.js\";\nexport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common.js\";\n"]}
+3
View File
@@ -0,0 +1,3 @@
{
"type": "module"
}
+34
View File
@@ -0,0 +1,34 @@
/**
* Default key used to access the XML attributes.
*/
export declare const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export declare const XML_CHARKEY = "_";
/**
* Options to govern behavior of xml parser and builder.
*/
export interface XmlOptions {
/**
* indicates the name of the root element in the resulting XML when building XML.
*/
rootName?: string;
/**
* indicates whether the root element is to be included or not in the output when parsing XML.
*/
includeRoot?: boolean;
/**
* key used to access the XML value content when parsing XML.
*/
xmlCharKey?: string;
/**
* property name for a CDATA section.
*/
cdataPropName?: string;
/**
* XML nodes to exclude from parsing.
*/
stopNodes?: string[];
}
//# sourceMappingURL=xml.common.d.ts.map
+11
View File
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Default key used to access the XML attributes.
*/
export const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export const XML_CHARKEY = "_";
//# sourceMappingURL=xml.common.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"xml.common.js","sourceRoot":"","sources":["../../src/xml.common.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAC/B;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * Default key used to access the XML attributes.\n */\nexport const XML_ATTRKEY = \"$\";\n/**\n * Default key used to access the XML value content.\n */\nexport const XML_CHARKEY = \"_\";\n\n/**\n * Options to govern behavior of xml parser and builder.\n */\nexport interface XmlOptions {\n /**\n * indicates the name of the root element in the resulting XML when building XML.\n */\n rootName?: string;\n /**\n * indicates whether the root element is to be included or not in the output when parsing XML.\n */\n includeRoot?: boolean;\n /**\n * key used to access the XML value content when parsing XML.\n */\n xmlCharKey?: string;\n\n /**\n * property name for a CDATA section.\n */\n cdataPropName?: string;\n\n /**\n * XML nodes to exclude from parsing.\n */\n stopNodes?: string[];\n}\n"]}
+16
View File
@@ -0,0 +1,16 @@
import { type XmlOptions } from "./xml.common.js";
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export declare function stringifyXML(obj: unknown, opts?: XmlOptions): string;
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
//# sourceMappingURL=xml.d.ts.map
+63
View File
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
function getCommonOptions(options) {
var _a;
return {
attributesGroupName: XML_ATTRKEY,
textNodeName: (_a = options.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY,
ignoreAttributes: false,
suppressBooleanAttributes: false,
};
}
function getSerializerOptions(options = {}) {
var _a, _b;
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true, suppressEmptyNode: true, indentBy: "", rootNodeName: (_a = options.rootName) !== null && _a !== void 0 ? _a : "root", cdataPropName: (_b = options.cdataPropName) !== null && _b !== void 0 ? _b : "__cdata" });
}
function getParserOptions(options = {}) {
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false, parseTagValue: false, attributeNamePrefix: "", stopNodes: options.stopNodes, processEntities: true, trimValues: false });
}
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export function stringifyXML(obj, opts = {}) {
const parserOptions = getSerializerOptions(opts);
const j2x = new XMLBuilder(parserOptions);
const node = { [parserOptions.rootNodeName]: obj };
const xmlData = j2x.build(node);
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
}
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export async function parseXML(str, opts = {}) {
if (!str) {
throw new Error("Document is empty");
}
const validation = XMLValidator.validate(str);
if (validation !== true) {
throw validation;
}
const parser = new XMLParser(getParserOptions(opts));
const parsedXml = parser.parse(str);
// Remove the <?xml version="..." ?> node.
// This is a change in behavior on fxp v4. Issue #424
if (parsedXml["?xml"]) {
delete parsedXml["?xml"];
}
if (!opts.includeRoot) {
for (const key of Object.keys(parsedXml)) {
const value = parsedXml[key];
return typeof value === "object" ? Object.assign({}, value) : value;
}
}
return parsedXml;
}
//# sourceMappingURL=xml.js.map
File diff suppressed because one or more lines are too long