xml-model 1.3.3 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +39 -35
  2. package/dist/_virtual/_rolldown/runtime.js +8 -0
  3. package/dist/index.d.ts +3 -7
  4. package/dist/index.js +7 -20
  5. package/dist/model.d.ts +53 -0
  6. package/dist/model.js +75 -0
  7. package/dist/node_modules/sax/lib/sax.js +1249 -0
  8. package/dist/node_modules/xml-js/lib/array-helper.js +13 -0
  9. package/dist/node_modules/xml-js/lib/index.js +19 -0
  10. package/dist/node_modules/xml-js/lib/js2xml.js +258 -0
  11. package/dist/node_modules/xml-js/lib/json2xml.js +22 -0
  12. package/dist/node_modules/xml-js/lib/options-helper.js +33 -0
  13. package/dist/node_modules/xml-js/lib/xml2js.js +246 -0
  14. package/dist/node_modules/xml-js/lib/xml2json.js +26 -0
  15. package/dist/util/kebab-case.js +24 -13
  16. package/dist/util/zod.d.ts +4 -0
  17. package/dist/util/zod.js +21 -0
  18. package/dist/xml/codec.d.ts +87 -0
  19. package/dist/xml/codec.js +339 -0
  20. package/dist/xml/examples.d.ts +188 -0
  21. package/dist/xml/index.d.ts +5 -79
  22. package/dist/xml/index.js +4 -57
  23. package/dist/xml/model.d.ts +18 -0
  24. package/dist/xml/model.js +33 -0
  25. package/dist/xml/schema-meta.d.ts +57 -0
  26. package/dist/xml/schema-meta.js +88 -0
  27. package/dist/xml/xml-js.d.ts +280 -3
  28. package/dist/xml/xml-js.js +121 -8
  29. package/package.json +27 -42
  30. package/dist/_virtual/Reflect.js +0 -8
  31. package/dist/_virtual/Reflect2.js +0 -5
  32. package/dist/_virtual/_commonjsHelpers.js +0 -47
  33. package/dist/defaults.d.ts +0 -28
  34. package/dist/defaults.js +0 -165
  35. package/dist/errors.d.ts +0 -40
  36. package/dist/errors.js +0 -45
  37. package/dist/middleware.d.ts +0 -10
  38. package/dist/middleware.js +0 -25
  39. package/dist/model/built-ins.d.ts +0 -3
  40. package/dist/model/built-ins.js +0 -43
  41. package/dist/model/index.d.ts +0 -69
  42. package/dist/model/index.js +0 -264
  43. package/dist/model/property.d.ts +0 -18
  44. package/dist/model/property.js +0 -67
  45. package/dist/model/registry.d.ts +0 -9
  46. package/dist/model/registry.js +0 -19
  47. package/dist/model/types.d.ts +0 -109
  48. package/dist/node_modules/reflect-metadata/Reflect.js +0 -806
  49. package/dist/types.d.ts +0 -25
  50. package/dist/util/is-regexp.d.ts +0 -12
  51. package/dist/util/is-regexp.js +0 -8
  52. package/dist/util/merge-maps.d.ts +0 -2
  53. package/dist/util/merge-maps.js +0 -23
  54. package/dist/vite/index.d.ts +0 -80
  55. package/dist/vite/index.js +0 -71
  56. package/dist/vite/node_modules/typescript-rtti/dist.esm/common/format.js +0 -105
  57. package/dist/vite/node_modules/typescript-rtti/dist.esm/common/index.js +0 -55
  58. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/api-call-transformer.js +0 -152
  59. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/class-analyzer.js +0 -83
  60. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/compile-error.js +0 -8
  61. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/import-analyzer.js +0 -89
  62. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/interface-analyzer.js +0 -58
  63. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/common/visitor-base.js +0 -93
  64. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/declarations-emitter.js +0 -31
  65. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/encode-parameter.js +0 -64
  66. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/find-relative-path.js +0 -41
  67. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/flags.js +0 -43
  68. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/forward-ref.js +0 -20
  69. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/get-exports-for-symbol.js +0 -64
  70. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/index.js +0 -130
  71. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/legacy-decorator.js +0 -10
  72. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/legacy-type-encoder.js +0 -82
  73. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/literal-node.js +0 -9
  74. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-collector.js +0 -56
  75. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-decorator.js +0 -80
  76. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-emitter.js +0 -425
  77. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/metadata-encoder.js +0 -212
  78. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/rt-helper.js +0 -96
  79. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/rtti-visitor-base.js +0 -28
  80. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/serialize.js +0 -31
  81. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/type-encoder.js +0 -76
  82. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/type-literal.js +0 -499
  83. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/utils.js +0 -906
  84. package/dist/vite/node_modules/typescript-rtti/dist.esm/transformer/workarounds.js +0 -7
@@ -1,80 +1,6 @@
1
- import { XMLElement, XMLRoot } from '../types';
2
- /**
3
- * Parses an XML string into an `XMLRoot` document tree.
4
- *
5
- * @param string - A well-formed XML string.
6
- * @returns The root of the parsed element tree.
7
- */
8
- export declare function parse(string: string): XMLRoot;
9
- /**
10
- * Serialises an `XMLRoot` or `XMLElement` back into an XML string.
11
- * Delegates to xml-js's `js2xml` function.
12
- */
13
- export declare const stringify: typeof import('xml-js').js2xml;
14
- /**
15
- * Extracts the text content from an element that has a single text child node.
16
- *
17
- * @param xml - An `XMLElement` expected to contain a single text node.
18
- * @returns The text value, or an empty string when there are no child elements.
19
- * @throws {TypeError} When the element has multiple or non-text children.
20
- */
21
- export declare function getContent(xml: XMLElement): string | number | boolean;
22
- /**
23
- * Creates a minimal element structure wrapping the given text content.
24
- * When no tag name is provided, returns a fragment with a text child.
25
- * When a tag name is provided, returns a full element with the given name and optional attributes.
26
- *
27
- * @param content - The text content to wrap (defaults to empty string).
28
- * @param tag - Optional element tag name.
29
- * @param attributes - Optional attributes; only valid when `tag` is provided.
30
- * @throws {TypeError} When `attributes` are provided without a `tag`.
31
- */
32
- export declare function fromContent(content: string): {
33
- elements: [{
34
- type: "text";
35
- text: string;
36
- }] | [];
37
- };
38
- export declare function fromContent(content: string, tag: string, attributes?: XMLElement["attributes"]): {
39
- type: "element";
40
- name: string;
41
- attributes?: XMLElement["attributes"];
42
- elements: [{
43
- type: "text";
44
- text: string;
45
- }] | [];
46
- };
47
- /**
48
- * Appends a child element to `xml`, initialising the `elements` array if needed.
49
- *
50
- * @param xml - The parent element to modify.
51
- * @param element - The child element to append.
52
- */
53
- export declare function addElement(xml: XMLElement, element: XMLElement): void;
54
- /**
55
- * Sets an attribute on an element, initialising the `attributes` map if needed.
56
- *
57
- * @param xml - The element to modify.
58
- * @param attribute - The attribute name.
59
- * @param value - The attribute value.
60
- */
61
- export declare function setAttribute(xml: XMLElement, attribute: string, value: string): void;
62
- /**
63
- * Removes an attribute from an element. Does nothing if the element has no attributes.
64
- *
65
- * @param xml - The element to modify.
66
- * @param attribute - The attribute name to remove.
67
- */
68
- export declare function deleteAttribute(xml: XMLElement, attribute: string): void;
69
- /** Namespace object bundling all XML utility functions. */
70
- declare const XML: {
71
- parse: typeof parse;
72
- stringify: typeof import('xml-js').js2xml;
73
- fromContent: typeof fromContent;
74
- getContent: typeof getContent;
75
- addElement: typeof addElement;
76
- setAttribute: typeof setAttribute;
77
- deleteAttribute: typeof deleteAttribute;
78
- };
79
- export default XML;
1
+ export * from './xml-js';
2
+ export { xml } from './schema-meta';
3
+ export type { UserCodecOptions } from './codec';
4
+ export { registerDefault, normalizeCodecOptions } from './codec';
5
+ export { xmlModel, type XmlModelConstructor } from './model';
80
6
  //# sourceMappingURL=index.d.ts.map
package/dist/xml/index.js CHANGED
@@ -1,57 +1,4 @@
1
- import XMLJS from "./xml-js.js";
2
- function parse(string) {
3
- return XMLJS.parse(string);
4
- }
5
- const stringify = XMLJS.stringify;
6
- function getContent(xml) {
7
- if (xml.elements?.length === 1) {
8
- const content = xml.elements[0];
9
- if (content.type === "text") return content.text;
10
- }
11
- if (!xml.elements) return "";
12
- throw new TypeError(`can't get text from XMLElement: ${JSON.stringify(xml)}`);
13
- }
14
- function fromContent(content = "", tag, attributes) {
15
- const el = {
16
- elements: content ? [{ type: "text", text: String(content) }] : []
17
- };
18
- if (tag) el.name = tag;
19
- if (attributes) {
20
- if (!el.name) throw new TypeError("please provide a name if you want to provide attributes");
21
- el.attributes = attributes;
22
- }
23
- if (el.name) el.type = "element";
24
- return el;
25
- }
26
- function addElement(xml, element) {
27
- if (!xml.elements) xml.elements = [];
28
- xml.elements.push(element);
29
- }
30
- function setAttribute(xml, attribute, value) {
31
- if (!xml.attributes) xml.attributes = {};
32
- xml.attributes[attribute] = value;
33
- }
34
- function deleteAttribute(xml, attribute) {
35
- if (!xml.attributes) return;
36
- delete xml.attributes[attribute];
37
- }
38
- const XML = {
39
- parse,
40
- stringify,
41
- fromContent,
42
- getContent,
43
- addElement,
44
- setAttribute,
45
- deleteAttribute
46
- };
47
- export {
48
- addElement,
49
- XML as default,
50
- deleteAttribute,
51
- fromContent,
52
- getContent,
53
- parse,
54
- setAttribute,
55
- stringify
56
- };
57
- //# sourceMappingURL=index.js.map
1
+ import "./xml-js.js";
2
+ import "./schema-meta.js";
3
+ import "./codec.js";
4
+ import "./model.js";
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod';
2
+ import { XMLElement, StringifyOptions, XMLRoot } from './xml-js';
3
+ import { ModelConstructor } from '../model';
4
+ import { UserCodecOptions } from './codec';
5
+ /**
6
+ * Constructor type for xmlModel classes.
7
+ * Extends ModelConstructor with XML-specific helpers and a typed extend().
8
+ */
9
+ export type XmlModelConstructor<S extends z.ZodObject<any> = z.ZodObject<any>, Inst extends z.infer<S> = z.infer<S>> = ModelConstructor<S, Inst> & {
10
+ /** Returns a new instance parsed from an XML string or XMLRoot. */
11
+ fromXML<T extends abstract new (...args: any[]) => any>(this: T, xmlInput: string | XMLRoot | XMLElement): InstanceType<T>;
12
+ /** Converts an instance to an XMLRoot document tree. */
13
+ toXML(instance: z.infer<S>): XMLRoot;
14
+ /** Converts an instance to an XML string. */
15
+ toXMLString(instance: z.infer<S>, options?: StringifyOptions): string;
16
+ };
17
+ export declare function xmlModel<S extends z.ZodObject<any>>(schema: S, options?: UserCodecOptions<S>): XmlModelConstructor<S>;
18
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1,33 @@
1
+ import { model } from "../model.js";
2
+ import XML from "./xml-js.js";
3
+ import { root } from "./schema-meta.js";
4
+ import { XML_STATE, decode, encode } from "./codec.js";
5
+ import "zod";
6
+ //#region src/xml/model.ts
7
+ function xmlModel(schema, options) {
8
+ const _schema = options ? root(schema, options) : schema;
9
+ return class extends model(_schema) {
10
+ static fromXML(input) {
11
+ if (typeof input === "string") input = XML.parse(input);
12
+ if (XML.isRoot(input)) input = XML.elementFromRoot(input);
13
+ const schema = this.dataSchema;
14
+ const inputData = decode(this.dataSchema, input);
15
+ const xmlState = inputData[XML_STATE];
16
+ const parsed = schema.parse(inputData);
17
+ parsed[XML_STATE] = xmlState;
18
+ return this.fromData(parsed);
19
+ }
20
+ static toXML(instance) {
21
+ const data = this.toData(instance);
22
+ return { elements: [encode(this.dataSchema, data)] };
23
+ }
24
+ static toXMLString(instance, options = {}) {
25
+ const xml = this.toXML(instance);
26
+ return XML.stringify(xml, options);
27
+ }
28
+ };
29
+ }
30
+ //#endregion
31
+ export { xmlModel };
32
+
33
+ //# sourceMappingURL=model.js.map
@@ -0,0 +1,57 @@
1
+ import { z } from 'zod';
2
+ import { UserCodecOptions, PropertyDecodingContext, PropertyEncodingContext } from './codec';
3
+ import { XMLElement } from './xml-js';
4
+ declare module "zod" {
5
+ interface GlobalMeta {
6
+ "@@xml-model"?: Record<string, unknown>;
7
+ }
8
+ }
9
+ type UserRootOptions<S extends z.ZodType = z.ZodType> = {
10
+ tagname?: string | UserCodecOptions<S>["tagname"];
11
+ decode?: UserCodecOptions<S>["decode"];
12
+ encode?: UserCodecOptions<S>["encode"];
13
+ };
14
+ type UserPropOptions = {
15
+ tagname?: string | UserCodecOptions["propertyTagname"];
16
+ inline?: boolean;
17
+ match?: RegExp | ((el: XMLElement) => boolean);
18
+ decode?: (ctx: PropertyDecodingContext) => Partial<Record<string, unknown>> | undefined;
19
+ encode?: (ctx: PropertyEncodingContext) => XMLElement | undefined;
20
+ };
21
+ export declare function root<S extends z.ZodType>(schema: S, options: UserRootOptions<S>): S;
22
+ export declare function root(options: UserRootOptions): z.GlobalMeta;
23
+ /**
24
+ * Annotate a field schema with XML child-element options.
25
+ *
26
+ * **`xml.prop()` with no options is a no-op.** The codec already iterates all
27
+ * ZodObject fields and defaults the tag name to `kebabCase(fieldKey)`. Wrap a
28
+ * schema in `xml.prop()` only when you need to customise at least one of:
29
+ * `tagname`, `inline`, `match`, `decode`, or `encode`.
30
+ *
31
+ * @example
32
+ * // ✅ Needed — custom tagname
33
+ * xml.prop(z.string(), { tagname: "pub-date" })
34
+ *
35
+ * // ✅ Needed — inline (children promoted to parent)
36
+ * xml.prop(z.array(ItemSchema), { inline: true })
37
+ *
38
+ * // ⚠️ Redundant — equivalent to plain z.string()
39
+ * xml.prop(z.string())
40
+ */
41
+ export declare function prop<PS extends z.ZodType>(schema: PS, options: UserPropOptions): PS;
42
+ export declare function prop(options: UserPropOptions): z.GlobalMeta;
43
+ type AttributePropOptions = {
44
+ name?: string;
45
+ };
46
+ export declare function attr<PS extends z.ZodType>(schema: PS, options?: AttributePropOptions): PS;
47
+ export declare function attr(options?: AttributePropOptions): z.GlobalMeta;
48
+ /** Namespace object for XML metadata helpers. */
49
+ export declare const xml: {
50
+ root: typeof root;
51
+ prop: typeof prop;
52
+ attr: typeof attr;
53
+ };
54
+ export declare function getOwnUserOptions<S extends z.ZodType>(schema: S): UserCodecOptions<S>;
55
+ export declare function getUserOptions<S extends z.ZodType>(schema: S): UserCodecOptions<S>;
56
+ export {};
57
+ //# sourceMappingURL=schema-meta.d.ts.map
@@ -0,0 +1,88 @@
1
+ import { getParentSchema, isZodType } from "../util/zod.js";
2
+ import "zod";
3
+ //#region src/xml/schema-meta.ts
4
+ var metaKey = "@@xml-model";
5
+ /** Merge `partial` into the schema's existing meta (shallow spread, no overwrite). */
6
+ function setMeta(schema, partial) {
7
+ const existing = schema.meta()?.[metaKey] ?? {};
8
+ return schema.meta({ [metaKey]: {
9
+ ...existing,
10
+ ...partial
11
+ } });
12
+ }
13
+ function normalizePropOptions(options) {
14
+ if (!options) return {};
15
+ const partial = {};
16
+ if (options.tagname !== void 0) partial.propertyTagname = options.tagname;
17
+ if (options.inline !== void 0) partial.inlineProperty = options.inline;
18
+ if (options.match !== void 0) partial.propertyMatch = options.match;
19
+ if (options.decode !== void 0) {
20
+ const userDecode = options.decode;
21
+ partial.decodeAsProperty = function(ctx) {
22
+ const res = userDecode(ctx);
23
+ if (typeof res !== "undefined") Object.assign(ctx.result, res);
24
+ };
25
+ }
26
+ if (options.encode !== void 0) {
27
+ const userEncode = options.encode;
28
+ partial.encodeAsProperty = function(ctx) {
29
+ const { property } = ctx;
30
+ const res = userEncode(ctx);
31
+ if (typeof res === "undefined") return;
32
+ if (property.options.inlineProperty) ctx.result.elements.push(...res.elements);
33
+ else {
34
+ res.name = property.tagname;
35
+ ctx.result.elements.push(res);
36
+ }
37
+ };
38
+ }
39
+ return partial;
40
+ }
41
+ function root(optionsOrSchema, options) {
42
+ if (isZodType(optionsOrSchema)) return setMeta(optionsOrSchema, options ?? {});
43
+ else return { [metaKey]: optionsOrSchema };
44
+ }
45
+ function prop(optionsOrSchema, options) {
46
+ if (isZodType(optionsOrSchema)) return setMeta(optionsOrSchema, normalizePropOptions(options));
47
+ else return { [metaKey]: normalizePropOptions(optionsOrSchema) };
48
+ }
49
+ function attr(optionsOrSchema, options) {
50
+ const opts = isZodType(optionsOrSchema) ? options ?? {} : optionsOrSchema ?? {};
51
+ const partial = {
52
+ decodeAsProperty(ctx) {
53
+ const { name, options: propOptions } = ctx.property;
54
+ const attrName = opts.name ?? name;
55
+ const attrValue = ctx.xml?.attributes[attrName];
56
+ ctx.result[name] = propOptions.schema.parse(attrValue);
57
+ },
58
+ encodeAsProperty(ctx) {
59
+ const { value, name } = ctx.property;
60
+ const attrName = opts.name ?? name;
61
+ ctx.result.attributes[attrName] = value.toString();
62
+ }
63
+ };
64
+ if (isZodType(optionsOrSchema)) return setMeta(optionsOrSchema, partial);
65
+ else return { [metaKey]: partial };
66
+ }
67
+ /** Namespace object for XML metadata helpers. */
68
+ var xml = {
69
+ root,
70
+ prop,
71
+ attr
72
+ };
73
+ function getOwnUserOptions(schema) {
74
+ return schema.meta()?.[metaKey] ?? {};
75
+ }
76
+ function getUserOptions(schema) {
77
+ const own = getOwnUserOptions(schema);
78
+ const parentSchema = getParentSchema(schema);
79
+ if (!parentSchema) return own;
80
+ return {
81
+ ...getUserOptions(parentSchema),
82
+ ...own
83
+ };
84
+ }
85
+ //#endregion
86
+ export { getUserOptions, root, xml };
87
+
88
+ //# sourceMappingURL=schema-meta.js.map
@@ -1,7 +1,284 @@
1
- import { xml2js as parse, js2xml as stringify } from 'xml-js';
2
- declare const _default: {
1
+ import { z } from 'zod';
2
+ export declare const ZXMLElementNode: z.ZodObject<{
3
+ type: z.ZodLiteral<"element">;
4
+ name: z.ZodString;
5
+ attributes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
6
+ elements: z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject</*elided*/ any, z.core.$strip>, z.ZodObject<{
7
+ type: z.ZodLiteral<"comment">;
8
+ comment: z.ZodString;
9
+ }, z.core.$strip>, z.ZodObject<{
10
+ type: z.ZodLiteral<"text">;
11
+ text: z.ZodString;
12
+ }, z.core.$strip>], "type">>>;
13
+ }, z.core.$strip>;
14
+ export type XMLElementNode = z.infer<typeof ZXMLElementNode>;
15
+ export declare const ZXMLCommentNode: z.ZodObject<{
16
+ type: z.ZodLiteral<"comment">;
17
+ comment: z.ZodString;
18
+ }, z.core.$strip>;
19
+ export type XMLCommentNode = z.infer<typeof ZXMLCommentNode>;
20
+ export declare const ZXMLTextNode: z.ZodObject<{
21
+ type: z.ZodLiteral<"text">;
22
+ text: z.ZodString;
23
+ }, z.core.$strip>;
24
+ export type XMLTextNode = z.infer<typeof ZXMLTextNode>;
25
+ export type XMLNode = XMLElementNode | XMLCommentNode | XMLTextNode;
26
+ export declare const ZXMLNode: z.ZodDiscriminatedUnion<[
27
+ typeof ZXMLElementNode,
28
+ typeof ZXMLCommentNode,
29
+ typeof ZXMLTextNode
30
+ ], "type">;
31
+ /**
32
+ * A single node in the xml-js element tree.
33
+ * Used as the internal representation for all XML parsing and serialization.
34
+ */
35
+ export type XMLElement = XMLElementNode;
36
+ export declare const ZXMLRoot: z.ZodObject<{
37
+ elements: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
38
+ type: z.ZodLiteral<"element">;
39
+ name: z.ZodString;
40
+ attributes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
41
+ elements: z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject</*elided*/ any, z.core.$strip>, z.ZodObject<{
42
+ type: z.ZodLiteral<"comment">;
43
+ comment: z.ZodString;
44
+ }, z.core.$strip>, z.ZodObject<{
45
+ type: z.ZodLiteral<"text">;
46
+ text: z.ZodString;
47
+ }, z.core.$strip>], "type">>>;
48
+ }, z.core.$strip>, z.ZodObject<{
49
+ type: z.ZodLiteral<"comment">;
50
+ comment: z.ZodString;
51
+ }, z.core.$strip>, z.ZodObject<{
52
+ type: z.ZodLiteral<"text">;
53
+ text: z.ZodString;
54
+ }, z.core.$strip>], "type">>;
55
+ }, z.core.$strip>;
56
+ /** The root of an xml-js document: a wrapper object whose `elements` array holds top-level nodes.
57
+ *
58
+ * **the `elements` array contains AT MOST one node with type `element`**
59
+ */
60
+ export type XMLRoot = {
61
+ elements: XMLNode[];
62
+ };
63
+ type EmptyObj = Record<PropertyKey, never>;
64
+ export type XMLVoid = EmptyObj;
65
+ type IgnoreOptions = {
66
+ /** Whether to ignore the XML declaration (`<?xml?>`). @default false */
67
+ ignoreDeclaration?: boolean;
68
+ /** Whether to ignore processing instructions (`<?go there?>`). @default false */
69
+ ignoreInstruction?: boolean;
70
+ /** Whether to ignore element attributes. @default false */
71
+ ignoreAttributes?: boolean;
72
+ /** Whether to ignore comments (`<!-- -->`). @default false */
73
+ ignoreComment?: boolean;
74
+ /** Whether to ignore CData sections (`<![CDATA[ ]]>`). @default false */
75
+ ignoreCdata?: boolean;
76
+ /** Whether to ignore DOCTYPE declarations. @default false */
77
+ ignoreDoctype?: boolean;
78
+ /** Whether to ignore text content inside elements. @default false */
79
+ ignoreText?: boolean;
80
+ };
81
+ type ChangingKeyNames = {
82
+ /** Override the key name used for the declaration property. @default "declaration" */
83
+ declarationKey?: string;
84
+ /** Override the key name used for processing instructions. @default "instruction" */
85
+ instructionKey?: string;
86
+ /** Override the key name used for element attributes. @default "attributes" */
87
+ attributesKey?: string;
88
+ /** Override the key name used for text content. @default "text" */
89
+ textKey?: string;
90
+ /** Override the key name used for CData sections. @default "cdata" */
91
+ cdataKey?: string;
92
+ /** Override the key name used for DOCTYPE. @default "doctype" */
93
+ doctypeKey?: string;
94
+ /** Override the key name used for comments. @default "comment" */
95
+ commentKey?: string;
96
+ /** Override the key name used for the parent back-reference (when `addParent` is enabled). @default "parent" */
97
+ parentKey?: string;
98
+ /** Override the key name used for node type. @default "type" */
99
+ typeKey?: string;
100
+ /** Override the key name used for element name. @default "name" */
101
+ nameKey?: string;
102
+ /** Override the key name used for child elements array. @default "elements" */
103
+ elementsKey?: string;
104
+ };
105
+ /** Options for parsing XML into a JS object tree. */
106
+ export type ParseOptions = IgnoreOptions & ChangingKeyNames & {
107
+ /** Whether to trim whitespace surrounding text content. @default false */
108
+ trim?: boolean;
109
+ /**
110
+ * Whether to replace `&`, `<`, `>` with their XML entities in text nodes.
111
+ * @deprecated See https://github.com/nashwaan/xml-js/issues/26
112
+ * @default false
113
+ */
114
+ sanitize?: boolean;
115
+ /** Whether to coerce numeric and boolean text values to their native JS types. @default false */
116
+ nativeType?: boolean;
117
+ /**
118
+ * Whether to add a `parent` property on each element pointing back to its parent.
119
+ * Useful for upward traversal but creates circular references.
120
+ * @default false
121
+ */
122
+ addParent?: boolean;
123
+ /**
124
+ * Whether to always wrap sub-elements in an array, even when there is only one.
125
+ * Pass an array of element names to restrict this behaviour to those names only.
126
+ * Only applicable in compact mode.
127
+ * @default false
128
+ */
129
+ alwaysArray?: boolean | Array<string>;
130
+ /**
131
+ * Whether to always emit an `elements` array even on empty elements.
132
+ * `<a></a>` becomes `{ elements: [{ type: "element", name: "a", elements: [] }] }`
133
+ * instead of `{ elements: [{ type: "element", name: "a" }] }`.
134
+ * Only applicable in non-compact mode.
135
+ * @default false
136
+ */
137
+ alwaysChildren?: boolean;
138
+ /**
139
+ * Whether to parse the contents of processing instructions as attributes.
140
+ * `<?go to="there"?>` becomes `{ go: { attributes: { to: "there" } } }`
141
+ * instead of `{ go: 'to="there"' }`.
142
+ * @default false
143
+ */
144
+ instructionHasAttributes?: boolean;
145
+ /** Whether to preserve whitespace-only text nodes that appear between elements. @default false */
146
+ captureSpacesBetweenElements?: boolean;
147
+ /** Custom processing hook called for each DOCTYPE value. */
148
+ doctypeFn?: (value: string, parentElement: object) => void;
149
+ /** Custom processing hook called for each processing instruction value. */
150
+ instructionFn?: (instructionValue: string, instructionName: string, parentElement: string) => void;
151
+ /** Custom processing hook called for each CData section. */
152
+ cdataFn?: (value: string, parentElement: object) => void;
153
+ /** Custom processing hook called for each comment. */
154
+ commentFn?: (value: string, parentElement: object) => void;
155
+ /** Custom processing hook called for each text node. */
156
+ textFn?: (value: string, parentElement: object) => void;
157
+ /** Custom processing hook called for each processing instruction name. */
158
+ instructionNameFn?: (instructionName: string, instructionValue: string, parentElement: string) => void;
159
+ /** Custom processing hook called for each element name. */
160
+ elementNameFn?: (value: string, parentElement: object) => void;
161
+ /** Custom processing hook called for each attribute name. */
162
+ attributeNameFn?: (attributeName: string, attributeValue: string, parentElement: string) => void;
163
+ /** Custom processing hook called for each attribute value. */
164
+ attributeValueFn?: (attributeValue: string, attributeName: string, parentElement: string) => void;
165
+ /** Custom processing hook called for the whole attributes object of an element. */
166
+ attributesFn?: (value: string, parentElement: string) => void;
167
+ };
168
+ declare function parse(xml: string, options?: ParseOptions): XMLRoot;
169
+ /** Options for serializing a JS object tree back to XML. */
170
+ export type StringifyOptions = IgnoreOptions & ChangingKeyNames & {
171
+ /** Number of spaces (or a string like `'\t'`) to use for indenting XML output. @default 0 */
172
+ spaces?: number | string;
173
+ /** Whether to indent text nodes onto their own line when `spaces` is set. @default false */
174
+ indentText?: boolean;
175
+ /** Whether to write CData sections on a new indented line. @default false */
176
+ indentCdata?: boolean;
177
+ /** Whether to print each attribute on its own indented line (when `spaces` is set). @default false */
178
+ indentAttributes?: boolean;
179
+ /** Whether to indent processing instructions onto their own line. @default false */
180
+ indentInstruction?: boolean;
181
+ /** Whether to emit empty elements as full tag pairs (`<a></a>`) instead of self-closing (`<a/>`). @default false */
182
+ fullTagEmptyElement?: boolean;
183
+ /** Whether to omit quotes around attribute values that are native JS types (numbers, booleans). @default false */
184
+ noQuotesForNativeAttributes?: boolean;
185
+ /** Custom processing hook called for each DOCTYPE value. */
186
+ doctypeFn?: (value: string, currentElementName: string, currentElementObj: object) => void;
187
+ /** Custom processing hook called for each processing instruction value. */
188
+ instructionFn?: (instructionValue: string, instructionName: string, currentElementName: string, currentElementObj: object) => void;
189
+ /** Custom processing hook called for each CData section. */
190
+ cdataFn?: (value: string, currentElementName: string, currentElementObj: object) => void;
191
+ /** Custom processing hook called for each comment. */
192
+ commentFn?: (value: string, currentElementName: string, currentElementObj: object) => void;
193
+ /** Custom processing hook called for each text node. */
194
+ textFn?: (value: string, currentElementName: string, currentElementObj: object) => void;
195
+ /** Custom processing hook called for each processing instruction name. */
196
+ instructionNameFn?: (instructionName: string, instructionValue: string, currentElementName: string, currentElementObj: object) => void;
197
+ /** Custom processing hook called for each element name. */
198
+ elementNameFn?: (value: string, currentElementName: string, currentElementObj: object) => void;
199
+ /** Custom processing hook called for each attribute name. */
200
+ attributeNameFn?: (attributeName: string, attributeValue: string, currentElementName: string, currentElementObj: object) => void;
201
+ /** Custom processing hook called for each attribute value. */
202
+ attributeValueFn?: (attributeValue: string, attributeName: string, currentElementName: string, currentElementObj: object) => void;
203
+ /** Custom processing hook called for the whole attributes object of an element. */
204
+ attributesFn?: (value: string, currentElementName: string, currentElementObj: object) => void;
205
+ /**
206
+ * Per-element override for `fullTagEmptyElement`.
207
+ * Return `true` to emit a full tag pair for the given element, `false` for self-closing.
208
+ */
209
+ fullTagEmptyElementFn?: (currentElementName: string, currentElementObj: object) => void;
210
+ };
211
+ declare function stringify(xml: XMLRoot, options?: StringifyOptions): string;
212
+ declare function isRoot(xml: object): xml is XMLRoot;
213
+ declare function elementFromRoot(root: XMLRoot): XMLElementNode | undefined;
214
+ declare function isEmpty(xml: object): xml is XMLVoid;
215
+ /**
216
+ * Extracts the text content from an element that has a single text child node.
217
+ *
218
+ * @param xml - An `XMLElement` expected to contain a single text node.
219
+ * @returns The text value, or an empty string when there are no child elements.
220
+ * @throws {TypeError} When the element has multiple or non-text children.
221
+ */
222
+ declare function getContent(xml: XMLElement): string;
223
+ /**
224
+ * Creates a minimal element structure wrapping the given text content.
225
+ * When no tag name is provided, returns a fragment with a text child.
226
+ * When a tag name is provided, returns a full element with the given name and optional attributes.
227
+ *
228
+ * @param content - The text content to wrap (defaults to empty string).
229
+ * @param tag - Optional element tag name.
230
+ * @param attributes - Optional attributes; only valid when `tag` is provided.
231
+ * @throws {TypeError} When `attributes` are provided without a `tag`.
232
+ */
233
+ declare function fromContent(content: string): {
234
+ elements: [{
235
+ type: "text";
236
+ text: string;
237
+ }] | [];
238
+ };
239
+ declare function fromContent(content: string, tag: string, attributes?: XMLElement["attributes"]): {
240
+ type: "element";
241
+ name: string;
242
+ attributes?: XMLElement["attributes"];
243
+ elements: [{
244
+ type: "text";
245
+ text: string;
246
+ }] | [];
247
+ };
248
+ /**
249
+ * Appends a child element to `xml`, initialising the `elements` array if needed.
250
+ *
251
+ * @param xml - The parent element to modify.
252
+ * @param element - The child element to append.
253
+ */
254
+ declare function addElement(xml: XMLElement, element: XMLElement): void;
255
+ /**
256
+ * Sets an attribute on an element, initialising the `attributes` map if needed.
257
+ *
258
+ * @param xml - The element to modify.
259
+ * @param attribute - The attribute name.
260
+ * @param value - The attribute value.
261
+ */
262
+ declare function setAttribute(xml: XMLElement, attribute: string, value: string): void;
263
+ /**
264
+ * Removes an attribute from an element. Does nothing if the element has no attributes.
265
+ *
266
+ * @param xml - The element to modify.
267
+ * @param attribute - The attribute name to remove.
268
+ */
269
+ declare function deleteAttribute(xml: XMLElement, attribute: string): void;
270
+ /** Namespace object bundling all XML utility functions. */
271
+ export declare const XML: {
3
272
  parse: typeof parse;
4
273
  stringify: typeof stringify;
274
+ isRoot: typeof isRoot;
275
+ elementFromRoot: typeof elementFromRoot;
276
+ isEmpty: typeof isEmpty;
277
+ fromContent: typeof fromContent;
278
+ getContent: typeof getContent;
279
+ addElement: typeof addElement;
280
+ setAttribute: typeof setAttribute;
281
+ deleteAttribute: typeof deleteAttribute;
5
282
  };
6
- export default _default;
283
+ export default XML;
7
284
  //# sourceMappingURL=xml-js.d.ts.map