@ntf/xml 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Night The Fox
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @ntf/xml
2
+
3
+ Another XML library
4
+
5
+ ## Why
6
+
7
+ Because I like reinventing the wheel :)
8
+
9
+ ## Installation
10
+
11
+ Use your favourite package manager, idk
12
+
13
+ ```sh
14
+ npm install @ntf/xml
15
+ ```
16
+
17
+ ```sh
18
+ yarn add @ntf/xml
19
+ ```
20
+
21
+ ```sh
22
+ pnpm install @ntf/xml
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Importing
28
+
29
+ This library can be used in `CommonJS` and `ESModule` environments
30
+
31
+ ```typescript
32
+ const { ... } = require("@ntf/xml");
33
+ ```
34
+
35
+ ```typescript
36
+ import { ... } from "@ntf/xml";
37
+ ```
38
+
39
+ ## License stuff that nobody reads
40
+
41
+ Just like any [Open Source Project](https://github.com/N1ghtTheF0x/ntf-xml) this has a [License](./LICENSE), the MIT License
@@ -0,0 +1,186 @@
1
+ //#region source/declaration.d.ts
2
+ declare class XMLDeclaration {
3
+ version: string;
4
+ encoding: string;
5
+ standalone?: boolean | undefined;
6
+ static readonly START = "<?xml";
7
+ static readonly END = "?>";
8
+ static readonly DEFAULT_ENCODING = "UTF-8";
9
+ static readonly DEFAULT_VERSION = "1.0";
10
+ constructor(version?: string, encoding?: string, standalone?: boolean | undefined);
11
+ toString(): string;
12
+ get [Symbol.toStringTag](): string;
13
+ }
14
+ //#endregion
15
+ //#region source/utilities.d.ts
16
+ /**
17
+ * A table of characters mapped to XML escape codes
18
+ */
19
+ declare const XML_ESCAPE: {
20
+ "<": string;
21
+ ">": string;
22
+ "&": string;
23
+ "'": string;
24
+ '"': string;
25
+ };
26
+ /**
27
+ * The media type of XML
28
+ */
29
+ declare const XML_MIMETYPE = "application/xml";
30
+ /**
31
+ * Escape `string` using XML escape codes
32
+ * @param string A string
33
+ */
34
+ declare function escapeXMLString(string: string): string;
35
+ /**
36
+ * Convert escaped XML string to a normal string
37
+ * @param string A string
38
+ */
39
+ declare function unescapeXMLString(string: string): string;
40
+ /**
41
+ * A symbol to tell XML elements have no content
42
+ */
43
+ declare const XML_EXPLICIT_END_SYMBOL: unique symbol;
44
+ /**
45
+ * The symbol used by NodeJS's `inspect` function from `node:util`
46
+ */
47
+ declare const NODEJS_CUSTOM_INSPECT_SYMBOL: unique symbol;
48
+ //#endregion
49
+ //#region source/element.d.ts
50
+ declare class XMLElement<P extends XMLElement.Attributes = XMLElement.Attributes> {
51
+ readonly type: string;
52
+ attributes: Partial<P>;
53
+ private _children;
54
+ private _parent?;
55
+ get parent(): XMLElement | undefined;
56
+ set parent(parent: XMLElement | undefined);
57
+ get children(): ReadonlyArray<XMLElement | string>;
58
+ set children(nodes: ReadonlyArray<XMLElement | string>);
59
+ get textContent(): string;
60
+ set textContent(value: string);
61
+ constructor(type: string, attributes?: Partial<P>, parent?: XMLElement, children?: Array<XMLElement | string>);
62
+ appendNode<Node extends XMLElement | string>(node: Node): Node;
63
+ append(...nodes: ReadonlyArray<XMLElement | string>): this;
64
+ remove(node: XMLElement | string): boolean;
65
+ clear(): void;
66
+ setAttribute<K extends keyof P>(key: K, value: P[K]): this;
67
+ setAttribute(key: string, value: any): this;
68
+ getAttribute<K extends keyof P>(key: K): P[K] | undefined;
69
+ getAttribute(key: string): any | undefined;
70
+ hasAttributes(): boolean;
71
+ hasAttribute<Key extends string>(key: Key): this is XMLElement<P & { [k in Key]: any }>;
72
+ toString(): string;
73
+ [Symbol.iterator](): Iterator<XMLElement | string>;
74
+ [NODEJS_CUSTOM_INSPECT_SYMBOL](..._args: Array<any>): string;
75
+ get [Symbol.toStringTag](): string;
76
+ }
77
+ declare namespace XMLElement {
78
+ type Attributes = Record<string, any>;
79
+ }
80
+ //#endregion
81
+ //#region source/document.d.ts
82
+ declare class XMLDocument {
83
+ root: XMLElement;
84
+ declaration: XMLDeclaration;
85
+ constructor(root: XMLElement, declaration?: XMLDeclaration);
86
+ toString(): string;
87
+ get [Symbol.toStringTag](): string;
88
+ [NODEJS_CUSTOM_INSPECT_SYMBOL](..._args: Array<any>): string;
89
+ }
90
+ //#endregion
91
+ //#region source/builder.d.ts
92
+ declare class XMLElementBuilder {
93
+ private _type?;
94
+ private _children;
95
+ private _attr;
96
+ constructor();
97
+ setType(type: string): this;
98
+ setAttribute(key: string, value: any): this;
99
+ addElement(element: XMLElement | XMLElementBuilder): this;
100
+ addText(text: string): this;
101
+ build(): XMLElement;
102
+ }
103
+ declare class XMLDocumentBuilder {
104
+ private _declaration?;
105
+ private _root?;
106
+ setDeclaration(declaration: XMLDeclaration | undefined): this;
107
+ setRoot(root: XMLElement | XMLElementBuilder): this;
108
+ build(): XMLDocument;
109
+ }
110
+ //#endregion
111
+ //#region source/parser.d.ts
112
+ /**
113
+ * Parse `string` as XML attributes
114
+ * @param string A string
115
+ */
116
+ declare function parseXMLAttributes(string: string): XMLElement.Attributes;
117
+ /**
118
+ * Parse `string` as a XML declaration
119
+ * @param string A string
120
+ */
121
+ declare function parseXMLDeclaration(string: string): XMLDeclaration;
122
+ /**
123
+ * Metadata of a parsed XML start tag
124
+ */
125
+ interface IXMLStartTag {
126
+ /**
127
+ * The XML type
128
+ */
129
+ type: string;
130
+ /**
131
+ * The XML attributes
132
+ */
133
+ attributes: XMLElement.Attributes;
134
+ /**
135
+ * Is an end tag required?
136
+ */
137
+ requiresEnd: boolean;
138
+ }
139
+ /**
140
+ * Parse `string` to get metadata about the XML start tag
141
+ * @param string A string
142
+ */
143
+ declare function parseXMLStartTag(string: string): IXMLStartTag;
144
+ /**
145
+ * Parse `string` to a XML element
146
+ * @param string A string
147
+ * @deprecated Currently not implemented
148
+ * @throws {Error} Not implemented
149
+ */
150
+ declare function parseXMLElement(string: string): XMLElement;
151
+ /**
152
+ * Parse `string` to a XML document
153
+ * @param string A string
154
+ * @deprecated {@link parseXMLElement} is not implemented yet
155
+ * @throws {Error} {@link parseXMLElement} is not implemented yet
156
+ */
157
+ declare function parseXMLDocument(string: string): XMLDocument;
158
+ //#endregion
159
+ //#region source/render.d.ts
160
+ /**
161
+ * Rendering XML attributes to a string
162
+ * @param attributes XML attributes as a object
163
+ */
164
+ declare function renderXMLAttributes(attributes: XMLElement.Attributes): string;
165
+ /**
166
+ * Render the start of an XML element
167
+ * @param elm A XML element
168
+ */
169
+ declare function renderXMLStartTag(elm: XMLElement): string;
170
+ /**
171
+ * Render the end of an XML element
172
+ * @param elm A XML element
173
+ */
174
+ declare function renderXMLEndTag(elm: XMLElement): string;
175
+ /**
176
+ * Render a XML element to a string
177
+ * @param root A XML element
178
+ */
179
+ declare function renderXMLElement(root: XMLElement): string;
180
+ /**
181
+ * Render a XML document to a string
182
+ * @param document A XML document
183
+ */
184
+ declare function renderXMLDocument(document: XMLDocument): string;
185
+ //#endregion
186
+ export { IXMLStartTag, NODEJS_CUSTOM_INSPECT_SYMBOL, XMLDeclaration, XMLDocument, XMLDocumentBuilder, XMLElement, XMLElementBuilder, XML_ESCAPE, XML_EXPLICIT_END_SYMBOL, XML_MIMETYPE, escapeXMLString, parseXMLAttributes, parseXMLDeclaration, parseXMLDocument, parseXMLElement, parseXMLStartTag, renderXMLAttributes, renderXMLDocument, renderXMLElement, renderXMLEndTag, renderXMLStartTag, unescapeXMLString };
@@ -0,0 +1,186 @@
1
+ //#region source/declaration.d.ts
2
+ declare class XMLDeclaration {
3
+ version: string;
4
+ encoding: string;
5
+ standalone?: boolean | undefined;
6
+ static readonly START = "<?xml";
7
+ static readonly END = "?>";
8
+ static readonly DEFAULT_ENCODING = "UTF-8";
9
+ static readonly DEFAULT_VERSION = "1.0";
10
+ constructor(version?: string, encoding?: string, standalone?: boolean | undefined);
11
+ toString(): string;
12
+ get [Symbol.toStringTag](): string;
13
+ }
14
+ //#endregion
15
+ //#region source/utilities.d.ts
16
+ /**
17
+ * A table of characters mapped to XML escape codes
18
+ */
19
+ declare const XML_ESCAPE: {
20
+ "<": string;
21
+ ">": string;
22
+ "&": string;
23
+ "'": string;
24
+ '"': string;
25
+ };
26
+ /**
27
+ * The media type of XML
28
+ */
29
+ declare const XML_MIMETYPE = "application/xml";
30
+ /**
31
+ * Escape `string` using XML escape codes
32
+ * @param string A string
33
+ */
34
+ declare function escapeXMLString(string: string): string;
35
+ /**
36
+ * Convert escaped XML string to a normal string
37
+ * @param string A string
38
+ */
39
+ declare function unescapeXMLString(string: string): string;
40
+ /**
41
+ * A symbol to tell XML elements have no content
42
+ */
43
+ declare const XML_EXPLICIT_END_SYMBOL: unique symbol;
44
+ /**
45
+ * The symbol used by NodeJS's `inspect` function from `node:util`
46
+ */
47
+ declare const NODEJS_CUSTOM_INSPECT_SYMBOL: unique symbol;
48
+ //#endregion
49
+ //#region source/element.d.ts
50
+ declare class XMLElement<P extends XMLElement.Attributes = XMLElement.Attributes> {
51
+ readonly type: string;
52
+ attributes: Partial<P>;
53
+ private _children;
54
+ private _parent?;
55
+ get parent(): XMLElement | undefined;
56
+ set parent(parent: XMLElement | undefined);
57
+ get children(): ReadonlyArray<XMLElement | string>;
58
+ set children(nodes: ReadonlyArray<XMLElement | string>);
59
+ get textContent(): string;
60
+ set textContent(value: string);
61
+ constructor(type: string, attributes?: Partial<P>, parent?: XMLElement, children?: Array<XMLElement | string>);
62
+ appendNode<Node extends XMLElement | string>(node: Node): Node;
63
+ append(...nodes: ReadonlyArray<XMLElement | string>): this;
64
+ remove(node: XMLElement | string): boolean;
65
+ clear(): void;
66
+ setAttribute<K extends keyof P>(key: K, value: P[K]): this;
67
+ setAttribute(key: string, value: any): this;
68
+ getAttribute<K extends keyof P>(key: K): P[K] | undefined;
69
+ getAttribute(key: string): any | undefined;
70
+ hasAttributes(): boolean;
71
+ hasAttribute<Key extends string>(key: Key): this is XMLElement<P & { [k in Key]: any }>;
72
+ toString(): string;
73
+ [Symbol.iterator](): Iterator<XMLElement | string>;
74
+ [NODEJS_CUSTOM_INSPECT_SYMBOL](..._args: Array<any>): string;
75
+ get [Symbol.toStringTag](): string;
76
+ }
77
+ declare namespace XMLElement {
78
+ type Attributes = Record<string, any>;
79
+ }
80
+ //#endregion
81
+ //#region source/document.d.ts
82
+ declare class XMLDocument {
83
+ root: XMLElement;
84
+ declaration: XMLDeclaration;
85
+ constructor(root: XMLElement, declaration?: XMLDeclaration);
86
+ toString(): string;
87
+ get [Symbol.toStringTag](): string;
88
+ [NODEJS_CUSTOM_INSPECT_SYMBOL](..._args: Array<any>): string;
89
+ }
90
+ //#endregion
91
+ //#region source/builder.d.ts
92
+ declare class XMLElementBuilder {
93
+ private _type?;
94
+ private _children;
95
+ private _attr;
96
+ constructor();
97
+ setType(type: string): this;
98
+ setAttribute(key: string, value: any): this;
99
+ addElement(element: XMLElement | XMLElementBuilder): this;
100
+ addText(text: string): this;
101
+ build(): XMLElement;
102
+ }
103
+ declare class XMLDocumentBuilder {
104
+ private _declaration?;
105
+ private _root?;
106
+ setDeclaration(declaration: XMLDeclaration | undefined): this;
107
+ setRoot(root: XMLElement | XMLElementBuilder): this;
108
+ build(): XMLDocument;
109
+ }
110
+ //#endregion
111
+ //#region source/parser.d.ts
112
+ /**
113
+ * Parse `string` as XML attributes
114
+ * @param string A string
115
+ */
116
+ declare function parseXMLAttributes(string: string): XMLElement.Attributes;
117
+ /**
118
+ * Parse `string` as a XML declaration
119
+ * @param string A string
120
+ */
121
+ declare function parseXMLDeclaration(string: string): XMLDeclaration;
122
+ /**
123
+ * Metadata of a parsed XML start tag
124
+ */
125
+ interface IXMLStartTag {
126
+ /**
127
+ * The XML type
128
+ */
129
+ type: string;
130
+ /**
131
+ * The XML attributes
132
+ */
133
+ attributes: XMLElement.Attributes;
134
+ /**
135
+ * Is an end tag required?
136
+ */
137
+ requiresEnd: boolean;
138
+ }
139
+ /**
140
+ * Parse `string` to get metadata about the XML start tag
141
+ * @param string A string
142
+ */
143
+ declare function parseXMLStartTag(string: string): IXMLStartTag;
144
+ /**
145
+ * Parse `string` to a XML element
146
+ * @param string A string
147
+ * @deprecated Currently not implemented
148
+ * @throws {Error} Not implemented
149
+ */
150
+ declare function parseXMLElement(string: string): XMLElement;
151
+ /**
152
+ * Parse `string` to a XML document
153
+ * @param string A string
154
+ * @deprecated {@link parseXMLElement} is not implemented yet
155
+ * @throws {Error} {@link parseXMLElement} is not implemented yet
156
+ */
157
+ declare function parseXMLDocument(string: string): XMLDocument;
158
+ //#endregion
159
+ //#region source/render.d.ts
160
+ /**
161
+ * Rendering XML attributes to a string
162
+ * @param attributes XML attributes as a object
163
+ */
164
+ declare function renderXMLAttributes(attributes: XMLElement.Attributes): string;
165
+ /**
166
+ * Render the start of an XML element
167
+ * @param elm A XML element
168
+ */
169
+ declare function renderXMLStartTag(elm: XMLElement): string;
170
+ /**
171
+ * Render the end of an XML element
172
+ * @param elm A XML element
173
+ */
174
+ declare function renderXMLEndTag(elm: XMLElement): string;
175
+ /**
176
+ * Render a XML element to a string
177
+ * @param root A XML element
178
+ */
179
+ declare function renderXMLElement(root: XMLElement): string;
180
+ /**
181
+ * Render a XML document to a string
182
+ * @param document A XML document
183
+ */
184
+ declare function renderXMLDocument(document: XMLDocument): string;
185
+ //#endregion
186
+ export { IXMLStartTag, NODEJS_CUSTOM_INSPECT_SYMBOL, XMLDeclaration, XMLDocument, XMLDocumentBuilder, XMLElement, XMLElementBuilder, XML_ESCAPE, XML_EXPLICIT_END_SYMBOL, XML_MIMETYPE, escapeXMLString, parseXMLAttributes, parseXMLDeclaration, parseXMLDocument, parseXMLElement, parseXMLStartTag, renderXMLAttributes, renderXMLDocument, renderXMLElement, renderXMLEndTag, renderXMLStartTag, unescapeXMLString };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ const e={"<":`&lt;`,">":`&gt;`,"&":`&amp;`,"'":`&apos;`,'"':`&quot;`},t=`application/xml`;function n(t){for(let[n,r]of Object.entries(e))t=t.replaceAll(n,r);return t}function r(t){for(let[n,r]of Object.entries(e))t=t.replaceAll(r,n);return t}const i=Symbol.for(`ntf-xml-explicit-end`),a=Symbol.for(`nodejs.util.inspect.custom`);var o=class e{static START=`<?xml`;static END=`?>`;static DEFAULT_ENCODING=`UTF-8`;static DEFAULT_VERSION=`1.0`;constructor(t=e.DEFAULT_VERSION,n=e.DEFAULT_ENCODING,r){this.version=t,this.encoding=n,this.standalone=r}toString(){let t=`${e.START} version="${n(this.version)}" encoding="${n(this.encoding)}"`;return typeof this.standalone==`boolean`&&(t+=` standalone="${this.standalone?`yes`:`no`}"`),t+e.END}get[Symbol.toStringTag](){return`XMLDeclaration`}},s=class{constructor(e,t=new o){this.root=e,this.declaration=t}toString(){return`${this.declaration.toString()}${this.root.toString()}`}get[Symbol.toStringTag](){return`XMLDocument`}[a](...e){return this.toString()}};function c(e){return Object.entries(e).map(function([e,t]){let r=n(e);if(t==null)return r;let i=n(String(t));return i.length===0?r:`${r}="${i}"`}).join(` `).trim()}function l(e){let t=`<${n(e.type)}`;return e.hasAttributes()&&(t+=` `+c(e.attributes)),i in e||e.children.length>0?t+`>`:t+` />`}function u(e){return i in e||e.children.length>0?`</${n(e.type)}>`:``}function d(e){let t=``,r=[{elm:e,children:[...e],index:0,started:!1}];for(;r.length>0;){let e=r[r.length-1];if(e===void 0)break;if(!e.started&&(t+=l(e.elm),e.started=!0,e.elm.children.length===0)){r.pop();continue}if(e.index>=e.children.length){t+=u(e.elm),r.pop();continue}let i=e.children[e.index++];i instanceof p&&r.push({elm:i,children:[...i],index:0,started:!1}),typeof i==`string`&&(t+=n(i))}return t}function f(e){return`${e.declaration.toString()}${d(e.root)}`}var p=class e{_children=[];_parent;get parent(){return this._parent}set parent(e){if(typeof e==`string`)throw TypeError(`cannot assign string as parent`);this._parent!==e&&(this._parent!==void 0&&this._parent.remove(this),e!==void 0&&(this._parent=e,e._children.push(this)))}get children(){return this._children}set children(e){this.clear(),this.append(...e)}get textContent(){return this._children.filter(e=>typeof e==`string`).join(``)}set textContent(e){this.children=[e]}constructor(e,t={},n,r=[]){this.type=e,this.attributes=t,this._parent=n,this.children=r}appendNode(t){return t instanceof e&&!this._children.includes(t)&&(t.parent=this),t}append(...e){for(let t of e)this.appendNode(t);return this}remove(e){let t=this._children.indexOf(e);return t<0?!1:(this._children.splice(t,1),!0)}clear(){for(let t of this._children)t instanceof e&&(t._parent=void 0);this._children=[]}setAttribute(e,t){return this.attributes[e]=t,this}getAttribute(e){return this.attributes[e]}hasAttributes(){return Object.keys(this.attributes).length>0}hasAttribute(e){return this.attributes[e]!==void 0}toString(){return`${l(this)}${this.children.length>0?`...`:``}${u(this)}`}*[Symbol.iterator](){for(let e of this._children)yield e}[a](...e){return this.toString()}get[Symbol.toStringTag](){return`XMLElement`}},m=class{_type;_children=[];_attr={};constructor(){}setType(e){return this._type=e,this}setAttribute(e,t){return this._attr[e]=t,this}addElement(e){let t=`build`in e?e.build():e;return this._children.push(t),this}addText(e){return this._children.push(e),this}build(){if(this._type===void 0)throw TypeError(`no XML type specified`);return new p(this._type,this._attr,void 0,this._children)}},h=class{_declaration;_root;setDeclaration(e){return this._declaration=e,this}setRoot(e){return this._root=`build`in e?e.build():e,this}build(){if(this._root===void 0)throw TypeError(`no XML document root specified`);return new s(this._root,this._declaration)}};function g(e){e=r(e.trim());let t={},n=``,i=``,a=0;for(let t=0;t<e.length;t++){let r=e[t];if(r===void 0)break;if(a===0){if(r===` `){o();continue}if(r===`=`){e[t+1]===`"`&&(a++,t++),a++;continue}n+=r;continue}if(a===1||a===2){if(r===` `&&a===1||r===`"`&&a===2){a=0,o();continue}i+=r;continue}}return o(),t;function o(){n.length>0&&(t[n]=i.length>0?i:void 0),n=i=``}}function _(e){if(e!==void 0){if(e=e.trim(),[`yes`,`true`,`on`].includes(e))return!0;if([`no`,`false`,`off`].includes(e))return!1;throw TypeError(`invalid XML declaration standalone value '${e}'`)}}function v(e){if(e=r(e.trim()),!e.startsWith(o.START)||!e.endsWith(o.END))throw TypeError(`XML declaration does not start with '${o.START}' and/or end with '${o.END}'`);let t=g(e.substring(o.START.length,e.length-o.END.length));if(`version`in t&&typeof t.version==`string`)return new o(t.version,`encoding`in t&&typeof t.encoding==`string`?t.encoding:void 0,_(`standalone`in t&&typeof t.standalone==`string`?t.standalone:void 0));throw TypeError(`XML declaration has no version`)}function y(e){if(e=r(e.trim()),!e.startsWith(`<`)||!e.endsWith(`>`))throw TypeError(`XML start tag does not start with < and/or ends with >`);let t=!e.endsWith(`/>`);e=e.substring(1,e.length-(e.endsWith(`/>`)?2:1));let n=e.substring(0,e.indexOf(` `));return{type:n,attributes:g(e.substring(n.length)),requiresEnd:t}}function b(e){throw Error(`not implemented`)}function x(e){e=r(e.trim());let t=e.substring(0,e.indexOf(`?>`)+2);return new s(b(e.substring(t.length)),v(t))}exports.NODEJS_CUSTOM_INSPECT_SYMBOL=a,exports.XMLDeclaration=o,exports.XMLDocument=s,exports.XMLDocumentBuilder=h,exports.XMLElement=p,exports.XMLElementBuilder=m,exports.XML_ESCAPE=e,exports.XML_EXPLICIT_END_SYMBOL=i,exports.XML_MIMETYPE=`application/xml`,exports.escapeXMLString=n,exports.parseXMLAttributes=g,exports.parseXMLDeclaration=v,exports.parseXMLDocument=x,exports.parseXMLElement=b,exports.parseXMLStartTag=y,exports.renderXMLAttributes=c,exports.renderXMLDocument=f,exports.renderXMLElement=d,exports.renderXMLEndTag=u,exports.renderXMLStartTag=l,exports.unescapeXMLString=r;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ const e={"<":`&lt;`,">":`&gt;`,"&":`&amp;`,"'":`&apos;`,'"':`&quot;`},t=`application/xml`;function n(t){for(let[n,r]of Object.entries(e))t=t.replaceAll(n,r);return t}function r(t){for(let[n,r]of Object.entries(e))t=t.replaceAll(r,n);return t}const i=Symbol.for(`ntf-xml-explicit-end`),a=Symbol.for(`nodejs.util.inspect.custom`);var o=class e{static START=`<?xml`;static END=`?>`;static DEFAULT_ENCODING=`UTF-8`;static DEFAULT_VERSION=`1.0`;constructor(t=e.DEFAULT_VERSION,n=e.DEFAULT_ENCODING,r){this.version=t,this.encoding=n,this.standalone=r}toString(){let t=`${e.START} version="${n(this.version)}" encoding="${n(this.encoding)}"`;return typeof this.standalone==`boolean`&&(t+=` standalone="${this.standalone?`yes`:`no`}"`),t+e.END}get[Symbol.toStringTag](){return`XMLDeclaration`}},s=class{constructor(e,t=new o){this.root=e,this.declaration=t}toString(){return`${this.declaration.toString()}${this.root.toString()}`}get[Symbol.toStringTag](){return`XMLDocument`}[a](...e){return this.toString()}};function c(e){return Object.entries(e).map(function([e,t]){let r=n(e);if(t==null)return r;let i=n(String(t));return i.length===0?r:`${r}="${i}"`}).join(` `).trim()}function l(e){let t=`<${n(e.type)}`;return e.hasAttributes()&&(t+=` `+c(e.attributes)),i in e||e.children.length>0?t+`>`:t+` />`}function u(e){return i in e||e.children.length>0?`</${n(e.type)}>`:``}function d(e){let t=``,r=[{elm:e,children:[...e],index:0,started:!1}];for(;r.length>0;){let e=r[r.length-1];if(e===void 0)break;if(!e.started&&(t+=l(e.elm),e.started=!0,e.elm.children.length===0)){r.pop();continue}if(e.index>=e.children.length){t+=u(e.elm),r.pop();continue}let i=e.children[e.index++];i instanceof p&&r.push({elm:i,children:[...i],index:0,started:!1}),typeof i==`string`&&(t+=n(i))}return t}function f(e){return`${e.declaration.toString()}${d(e.root)}`}var p=class e{_children=[];_parent;get parent(){return this._parent}set parent(e){if(typeof e==`string`)throw TypeError(`cannot assign string as parent`);this._parent!==e&&(this._parent!==void 0&&this._parent.remove(this),e!==void 0&&(this._parent=e,e._children.push(this)))}get children(){return this._children}set children(e){this.clear(),this.append(...e)}get textContent(){return this._children.filter(e=>typeof e==`string`).join(``)}set textContent(e){this.children=[e]}constructor(e,t={},n,r=[]){this.type=e,this.attributes=t,this._parent=n,this.children=r}appendNode(t){return t instanceof e&&!this._children.includes(t)&&(t.parent=this),t}append(...e){for(let t of e)this.appendNode(t);return this}remove(e){let t=this._children.indexOf(e);return t<0?!1:(this._children.splice(t,1),!0)}clear(){for(let t of this._children)t instanceof e&&(t._parent=void 0);this._children=[]}setAttribute(e,t){return this.attributes[e]=t,this}getAttribute(e){return this.attributes[e]}hasAttributes(){return Object.keys(this.attributes).length>0}hasAttribute(e){return this.attributes[e]!==void 0}toString(){return`${l(this)}${this.children.length>0?`...`:``}${u(this)}`}*[Symbol.iterator](){for(let e of this._children)yield e}[a](...e){return this.toString()}get[Symbol.toStringTag](){return`XMLElement`}},m=class{_type;_children=[];_attr={};constructor(){}setType(e){return this._type=e,this}setAttribute(e,t){return this._attr[e]=t,this}addElement(e){let t=`build`in e?e.build():e;return this._children.push(t),this}addText(e){return this._children.push(e),this}build(){if(this._type===void 0)throw TypeError(`no XML type specified`);return new p(this._type,this._attr,void 0,this._children)}},h=class{_declaration;_root;setDeclaration(e){return this._declaration=e,this}setRoot(e){return this._root=`build`in e?e.build():e,this}build(){if(this._root===void 0)throw TypeError(`no XML document root specified`);return new s(this._root,this._declaration)}};function g(e){e=r(e.trim());let t={},n=``,i=``,a=0;for(let t=0;t<e.length;t++){let r=e[t];if(r===void 0)break;if(a===0){if(r===` `){o();continue}if(r===`=`){e[t+1]===`"`&&(a++,t++),a++;continue}n+=r;continue}if(a===1||a===2){if(r===` `&&a===1||r===`"`&&a===2){a=0,o();continue}i+=r;continue}}return o(),t;function o(){n.length>0&&(t[n]=i.length>0?i:void 0),n=i=``}}function _(e){if(e!==void 0){if(e=e.trim(),[`yes`,`true`,`on`].includes(e))return!0;if([`no`,`false`,`off`].includes(e))return!1;throw TypeError(`invalid XML declaration standalone value '${e}'`)}}function v(e){if(e=r(e.trim()),!e.startsWith(o.START)||!e.endsWith(o.END))throw TypeError(`XML declaration does not start with '${o.START}' and/or end with '${o.END}'`);let t=g(e.substring(o.START.length,e.length-o.END.length));if(`version`in t&&typeof t.version==`string`)return new o(t.version,`encoding`in t&&typeof t.encoding==`string`?t.encoding:void 0,_(`standalone`in t&&typeof t.standalone==`string`?t.standalone:void 0));throw TypeError(`XML declaration has no version`)}function y(e){if(e=r(e.trim()),!e.startsWith(`<`)||!e.endsWith(`>`))throw TypeError(`XML start tag does not start with < and/or ends with >`);let t=!e.endsWith(`/>`);e=e.substring(1,e.length-(e.endsWith(`/>`)?2:1));let n=e.substring(0,e.indexOf(` `));return{type:n,attributes:g(e.substring(n.length)),requiresEnd:t}}function b(e){throw Error(`not implemented`)}function x(e){e=r(e.trim());let t=e.substring(0,e.indexOf(`?>`)+2);return new s(b(e.substring(t.length)),v(t))}export{a as NODEJS_CUSTOM_INSPECT_SYMBOL,o as XMLDeclaration,s as XMLDocument,h as XMLDocumentBuilder,p as XMLElement,m as XMLElementBuilder,e as XML_ESCAPE,i as XML_EXPLICIT_END_SYMBOL,t as XML_MIMETYPE,n as escapeXMLString,g as parseXMLAttributes,v as parseXMLDeclaration,x as parseXMLDocument,b as parseXMLElement,y as parseXMLStartTag,c as renderXMLAttributes,f as renderXMLDocument,d as renderXMLElement,u as renderXMLEndTag,l as renderXMLStartTag,r as unescapeXMLString};
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@ntf/xml",
3
+ "version": "0.1.0",
4
+ "description": "Another XML library",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ "require": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "import": {
17
+ "types": "./dist/index.d.mts",
18
+ "default": "./dist/index.mjs"
19
+ }
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/N1ghtTheF0x/ntf-xml.git"
24
+ },
25
+ "keywords": [
26
+ "esm",
27
+ "cjs",
28
+ "nodejs",
29
+ "browser"
30
+ ],
31
+ "author": "N1ghtTheF0x",
32
+ "license": "MIT",
33
+ "bugs": {
34
+ "url": "https://github.com/N1ghtTheF0x/ntf-xml/issues"
35
+ },
36
+ "homepage": "https://github.com/N1ghtTheF0x/ntf-xml#readme",
37
+ "devDependencies": {
38
+ "tsdown": "latest",
39
+ "typescript": "latest"
40
+ },
41
+ "scripts": {
42
+ "pretest": "pnpm run build",
43
+ "test": "node tests/test.mjs",
44
+ "check": "tsc --noEmit",
45
+ "prebuild": "pnpm check",
46
+ "build": "node build.mjs",
47
+ "build:prod": "pnpm build prod"
48
+ }
49
+ }