forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.ts
More file actions
873 lines (706 loc) · 24.4 KB
/
node.ts
File metadata and controls
873 lines (706 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
import { xmlDocPtr, xmlNodePtr, xmlDtdPtr, xmlNsPtr, XMLReferenceType, xmlXPathObjectPtr } from "./bindings/types";
import { XMLReference, createXMLReference, createXMLReferenceOrThrow } from "./bindings";
import {
xmlXPathNewContext,
xmlXPathNodeEval,
xmlUnlinkNode,
xmlHasProp,
xmlSetProp,
xmlAddChild,
xmlDocCopyNode,
xmlNewCDataBlock,
xmlNodeGetContent,
xmlNodeSetContent,
xmlNodeSetName,
xmlGetNodePath,
xmlCopyNode,
xmlAddNextSibling,
xmlAddPrevSibling,
xmlReplaceNode,
xmlNewNs,
xmlSetNs,
xmlEncodeEntitiesReentrant,
xmlStringGetNodeList,
xmlGetNsList,
xmlXPathRegisterNs,
xmlGetLineNo,
xmlDocSetRootElement,
xmlSaveTree,
xmlReconciliateNs,
xmlSetTreeDoc,
xmlXPathFreeObject,
xmlXPathFreeContext,
} from "./bindings/functions";
import { xmlSaveCtxtPtr }from "./bindings/types"
import bindings from "./bindings";
export enum XMLNodeError {
NO_REF = "Node has no native reference",
NO_DOC = "Node has no document",
}
export type XPathNamespace = string | XMLNamespace | { [key: string]: string };
export type XMLAttributeMap = {
[key: string]: string | number;
};
import { XMLDocument } from "./document";
import { XMLElementType, XMLSaveOptions } from "./types";
export type XMLXPathNode = XMLNode | XMLAttribute | XMLElement;
enum xmlXPathObjectType {
XPATH_UNDEFINED = bindings.XPATH_UNDEFINED, // 0
XPATH_NODESET = bindings.XPATH_NODESET, // 1
XPATH_BOOLEAN = bindings.XPATH_BOOLEAN, // 2
XPATH_NUMBER = bindings.XPATH_NUMBER, // 3
XPATH_STRING = bindings.XPATH_STRING, // 4
XPATH_POINT = bindings.XPATH_POINT, // 5
XPATH_RANGE = bindings.XPATH_RANGE, // 6
XPATH_LOCATIONSET = bindings.XPATH_LOCATIONSET, // 7
XPATH_USERS = bindings.XPATH_USERS, // 8
XPATH_XSLT_TREE = bindings.XPATH_XSLT_TREE, // 9 : An XSLT value tree, non modifiable
}
function refToNodeType(node: xmlNodePtr | xmlDocPtr | null): XMLNode | XMLAttribute | XMLElement | XMLDocument | null {
if (node === null) {
return null;
}
if (
node.type === XMLElementType.XML_DOCUMENT_NODE ||
node.type === XMLElementType.XML_DOCB_DOCUMENT_NODE ||
node.type === XMLElementType.XML_HTML_DOCUMENT_NODE
) {
return createXMLReference(XMLDocument, node);
}
if (node.type === XMLElementType.XML_ATTRIBUTE_NODE) {
return createXMLReference(XMLAttribute, node);
}
if (node.type === XMLElementType.XML_ELEMENT_NODE) {
return createXMLReference(XMLElement, node);
}
if (node.type === XMLElementType.XML_TEXT_NODE) {
return createXMLReference(XMLText, node);
}
return createXMLReference(XMLNode, node);
}
export class XMLNode extends XMLReference<xmlNodePtr> {
/**
* @private
* @param _ref
*/
constructor(_ref: any) {
super(_ref);
}
/**
* Get the parent node for the current
*
* @returns {XMLElement} parent node
*/
public parent() {
const _ref = this.getNativeReference();
if (_ref.parent === null) {
return refToNodeType(_ref.doc);
}
return refToNodeType(_ref.parent);
}
/**
* Get the tag name of current node
*
* @returns {string} name
*/
public name(): string {
const _ref = this.getNativeReference();
if (
_ref.type === XMLElementType.XML_DOCUMENT_NODE ||
_ref.type === XMLElementType.XML_DOCB_DOCUMENT_NODE ||
_ref.type === XMLElementType.XML_HTML_DOCUMENT_NODE
) {
return createXMLReferenceOrThrow(XMLDocument, _ref, XMLNodeError.NO_REF).name();
}
if (
_ref.type === XMLElementType.XML_ATTRIBUTE_NODE ||
_ref.type === XMLElementType.XML_ELEMENT_NODE ||
_ref.type === XMLElementType.XML_PI_NODE
) {
return _ref.name;
}
if (_ref.type === XMLElementType.XML_NAMESPACE_DECL) {
return createXMLReferenceOrThrow(XMLNamespace, _ref, XMLNodeError.NO_REF).name();
}
return this.type();
}
/**
* Get the line number that this node starts on in the original parsed document
*
* @returns {number} line number
*/
public line() {
const _ref = this.getNativeReference();
return xmlGetLineNo(_ref);
}
/**
* Get the type of element (XMLElementType)
*
* "text" | "cdata" | "comment" | "element" | "node"
*
* @returns {string} the node type
*/
public type() {
const _ref = this.getNativeReference();
switch (_ref.type) {
case XMLElementType.XML_ELEMENT_NODE:
return "element";
case XMLElementType.XML_ATTRIBUTE_NODE:
return "attribute";
case XMLElementType.XML_TEXT_NODE:
return "text";
case XMLElementType.XML_CDATA_SECTION_NODE:
return "cdata";
case XMLElementType.XML_ENTITY_REF_NODE:
return "entity_ref";
case XMLElementType.XML_ENTITY_NODE:
return "entity";
case XMLElementType.XML_PI_NODE:
return "pi";
case XMLElementType.XML_COMMENT_NODE:
return "comment";
case XMLElementType.XML_DOCUMENT_NODE:
return "document";
case XMLElementType.XML_DOCUMENT_TYPE_NODE:
return "document_type";
case XMLElementType.XML_DOCUMENT_FRAG_NODE:
return "document_frag";
case XMLElementType.XML_NOTATION_NODE:
return "notation";
case XMLElementType.XML_HTML_DOCUMENT_NODE:
return "html_document";
case XMLElementType.XML_DTD_NODE:
return "dtd";
case XMLElementType.XML_ELEMENT_DECL:
return "element_decl";
case XMLElementType.XML_ATTRIBUTE_DECL:
return "attribute_decl";
case XMLElementType.XML_ENTITY_DECL:
return "entity_decl";
case XMLElementType.XML_NAMESPACE_DECL:
return "namespace_decl";
case XMLElementType.XML_XINCLUDE_START:
return "xinclude_start";
case XMLElementType.XML_XINCLUDE_END:
return "xinclude_end";
case XMLElementType.XML_DOCB_DOCUMENT_NODE:
return "docb_document";
}
return "node";
}
/**
* Create a new element with the given name and content,
* then append it as a child of the current node
*
* @see XMLNode.createElement
* @param name
* @param content
* @returns the appended element
*/
public node(name: string, content?: string): XMLElement {
return this.addChild(this.createElement(name, content));
}
/**
* Create a new element with the given name and content
*
* @param name
* @param content
* @returns the created element
*/
public createElement(name: string, content?: string): XMLElement {
const _ref = this.getNativeReference();
if (_ref.doc === null) {
throw new Error(XMLNodeError.NO_DOC);
}
return createXMLReferenceOrThrow(XMLDocument, this.getNativeReference().doc, XMLNodeError.NO_REF).createElement(
name,
content
);
}
public defineNamespace(prefix: string | null, href?: string | null): XMLNamespace {
if (!href) {
href = prefix;
prefix = null;
}
return createXMLReferenceOrThrow(
XMLNamespace,
xmlNewNs(this.getNativeReference(), href, prefix),
XMLNodeError.NO_REF
);
}
/**
* @private
* @param _docRef
*/
public setDocumentRoot(_docRef: xmlDocPtr) {
xmlDocSetRootElement(_docRef, this.getNativeReference());
}
/**
* Get the previous sibling relative to the current node
*
* @returns {XMLElement | null} previous element
*/
public prevElement() {
let node = this.prevSibling();
while (node !== null && node.type() !== "element") {
node = node.prevSibling();
}
return node;
}
/**
* Get the next sibling relative to the current node
*
* @returns {XMLElement | null} next element
*/
public nextElement() {
let node = this.nextSibling();
while (node !== null && node.type() !== "element") {
node = node.nextSibling();
}
return node;
}
protected importNode(node: xmlNodePtr): XMLNode {
const _ref = this.getNativeReference();
if (_ref.doc === node.doc) {
if (node.parent !== null) {
xmlUnlinkNode(node);
}
return createXMLReferenceOrThrow(XMLNode, node, XMLNodeError.NO_REF);
}
return createXMLReferenceOrThrow(XMLNode, xmlDocCopyNode(node, _ref.doc, 1), XMLNodeError.NO_REF);
}
private childWillMerge(_nodeRef: xmlNodePtr) {
const _selfRef = this.getNativeReference();
return (
_nodeRef.type === XMLElementType.XML_TEXT_NODE &&
_selfRef.last !== null &&
_selfRef.last.type === XMLElementType.XML_TEXT_NODE &&
_selfRef.last.name === _nodeRef.name &&
_selfRef.last !== _nodeRef
);
}
/**
* Append node after the last node child
*
* @param node the XMLElement to append
* @returns the appended child
*/
public addChild(node: XMLElement): XMLElement {
const _parentRef = this.getNativeReference();
const _childRef = node.getNativeReference();
let childNode: xmlNodePtr | null = this.importNode(_childRef).getNativeReference();
if (childNode === _childRef && this.childWillMerge(childNode)) {
childNode = xmlAddChild(_parentRef, xmlCopyNode(childNode, 0));
} else {
childNode = xmlAddChild(_parentRef, childNode);
}
return createXMLReferenceOrThrow(XMLElement, childNode, XMLNodeError.NO_REF);
}
private nextSiblingWillMerge(node: XMLElement) {
const _selfRef = this.getNativeReference();
const _nodeRef = node.getNativeReference();
return (
_nodeRef.type === XMLElementType.XML_TEXT_NODE &&
(_selfRef.type === XMLElementType.XML_TEXT_NODE ||
(_selfRef.next !== null &&
_selfRef.next.type === XMLElementType.XML_TEXT_NODE &&
_selfRef.name === _selfRef.next.name))
); // libxml2 bug?
}
/**
* Insert a node directly after the current node
*
* @param node the node to be inserted
* @returns the inserted sibling
*/
public addNextSibling(node: XMLElement) {
const _parentRef = this.getNativeReference();
const _childRef = node.getNativeReference();
let childNode: xmlNodePtr | null = this.importNode(_childRef).getNativeReference();
if (childNode === _childRef && this.nextSiblingWillMerge(node)) {
childNode = xmlCopyNode(childNode, 0);
}
return createXMLReference(XMLElement, xmlAddNextSibling(_parentRef, childNode));
}
private prevSiblingWillMerge(node: XMLElement) {
const _selfRef = this.getNativeReference();
const _nodeRef = node.getNativeReference();
return (
_nodeRef.type === XMLElementType.XML_TEXT_NODE &&
(_selfRef.type === XMLElementType.XML_TEXT_NODE ||
(_selfRef.prev !== null &&
_selfRef.prev.type === XMLElementType.XML_TEXT_NODE &&
_selfRef.name === _selfRef.prev.name))
);
}
/**
* Insert a node directly before the current node
*
* @param node the node to be inserted
* @returns the inserted sibling
*/
public addPrevSibling(node: XMLElement) {
const _parentRef = this.getNativeReference();
const _childRef = node.getNativeReference();
let childNode: xmlNodePtr | null = this.importNode(_childRef).getNativeReference();
if (childNode === _childRef && this.prevSiblingWillMerge(node)) {
childNode = xmlCopyNode(childNode, 0);
}
return createXMLReferenceOrThrow(XMLElement, xmlAddPrevSibling(_parentRef, childNode), XMLNodeError.NO_REF);
}
/**
* Get an array of child nodes
*
* @returns {XMLElement[]} array of child nodes
*/
public childNodes() {
const _ref = this.getNativeReference()
const children: XMLElement[] = [];
let child = _ref.children;
while (child !== null) {
children.push(createXMLReferenceOrThrow(XMLElement, child, XMLNodeError.NO_REF));
child = child.next;
}
return children;
}
public prevSibling() {
return createXMLReference(XMLNode, this.getNativeReference().prev);
}
public nextSibling() {
return createXMLReference(XMLNode, this.getNativeReference().next);
}
public evaluateXPath(xpath: string, namespace?: XPathNamespace): xmlXPathObjectPtr {
const _ref = this.getNativeReference();
const xpathContext = xmlXPathNewContext(_ref.doc);
if (namespace) {
if (namespace instanceof XMLNamespace) {
xmlXPathRegisterNs(xpathContext, namespace.prefix(), namespace.href());
} else if (typeof namespace === "string") {
xmlXPathRegisterNs(xpathContext, "xmlns", namespace);
} else {
Object.keys(namespace).forEach((prefix) => {
const href = namespace[prefix];
xmlXPathRegisterNs(xpathContext, prefix, href || null);
});
}
}
const ret = xmlXPathNodeEval(_ref, xpath, xpathContext);
xmlXPathFreeContext(xpathContext);
return ret;
}
/**
* Find decendant nodes matching the given xpath selector
*
* @param xpath XPath selector
* @param namespace optional namespace
* @returns {XMLXPathNodeSet} array of matching decendant nodes
*/
public find(xpath: string, namespace?: XPathNamespace): XMLXPathNode[] {
const result = this.evaluateXPath(xpath, namespace);
const nodeSet: XMLXPathNode[] = [];
result.nodesetval.forEach((node) => {
const instance = refToNodeType(node);
if (instance !== null && !(instance instanceof XMLDocument)) {
nodeSet.push(instance);
}
});
xmlXPathFreeObject(result);
return nodeSet;
}
/**
* Find the first decendant node matching the given xpath selector
*
* @param xpath XPath selector
* @param namespace optional namespace
* @returns {XMLElement} first matching decendant node
*/
public get(xpath: string, namespace?: XPathNamespace): XMLXPathNode | boolean | number | string | null {
const result = this.evaluateXPath(xpath, namespace);
let ret: ReturnType<typeof XMLNode.prototype.get> = null;
if (result.type === xmlXPathObjectType.XPATH_BOOLEAN) {
ret = !!result.boolval;
} else if (result.type === xmlXPathObjectType.XPATH_NUMBER) {
ret = result.floatval;
} else if (result.type === xmlXPathObjectType.XPATH_STRING) {
ret = result.stringval;
} else if (result.type === xmlXPathObjectType.XPATH_NODESET) {
const _ref = result.nodesetval[0];
if (_ref) {
const node = refToNodeType(_ref);
if (node !== null && !(node instanceof XMLDocument)) {
ret = node;
}
}
}
xmlXPathFreeObject(result);
return ret;
}
/**
* Return the child at the given index
* @param index the index of the child
* @returns {XMLElement | null} the child to return
*/
public child(index: number) {
let currIndex = 0;
let _childRef = this.getNativeReference().children;
while (_childRef != null) {
if (currIndex >= index) {
break;
}
currIndex++;
_childRef = _childRef.next;
}
return createXMLReference(XMLElement, _childRef);
}
public remove() {
xmlUnlinkNode(this.getNativeReference());
}
/**
* Get the associated XMLDocument for the current node
*
* @returns {XMLDocument | null}
*/
public doc() {
const _ref = this.getNativeReference();
return createXMLReference(XMLDocument, _ref.doc);
}
public toString(options: XMLSaveOptions | boolean = {}): string {
return XMLDocument.toString(this, options) || "";
}
/**
* Get or set the namespace for the current node
*
* @param prefix namespace prefix
* @param href namespace URL
* @returns {XMLNamespace} the current namespace
*/
public namespace(prefix?: string | XMLNamespace | null, href?: string | null): XMLNamespace | null {
const _ref = this.getNativeReference();
if (prefix === null) {
_ref.ns = null;
} else if (prefix instanceof XMLNamespace) {
prefix._applyToNode(_ref);
} else if (typeof prefix === "string") {
if (!href) {
href = prefix;
prefix = null;
}
const namespace = this.doc()?._findNamespace(_ref, prefix, href) || this.defineNamespace(prefix, href);
if (namespace) {
namespace._applyToNode(_ref);
}
}
return createXMLReference(XMLNamespace, _ref.ns);
}
/**
* Get an array of namespaces that appy to the current node
*
* @param onlyLocal whether to include inherited namespaces
* @returns {XMLNamespace[]} an array of namespaces for the current node
*/
public namespaces(onlyLocal: boolean = false) {
const namespaces: XMLNamespace[] = [];
const _ref = this.getNativeReference();
if (onlyLocal === true) {
let namespace = _ref.nsDef;
while (namespace !== null) {
namespaces.push(createXMLReferenceOrThrow(XMLNamespace, namespace, XMLNodeError.NO_REF));
namespace = namespace.next;
}
} else {
xmlGetNsList(_ref.doc, _ref).forEach((namespace) => {
namespaces.push(createXMLReferenceOrThrow(XMLNamespace, namespace, XMLNodeError.NO_REF));
});
}
return namespaces;
}
public clone() {
const _ref = this.getNativeReference();
return refToNodeType(xmlDocCopyNode(_ref, _ref.doc, 1));
}
/**
* @private
* @param context
*/
public _xmlSaveTree(context: xmlSaveCtxtPtr) {
xmlSaveTree(context, this.getNativeReference());
}
}
export class XMLElement extends XMLNode {
public name(value?: string): string {
if (value !== undefined) {
xmlNodeSetName(this.getNativeReference(), value);
}
return super.name();
}
public getAttribute(key: string): XMLAttribute | null {
const _ref = this.getNativeReference();
return createXMLReference(XMLAttribute, xmlHasProp(_ref, key));
}
public setAttribute(key: string, value: string | number): XMLAttribute | null {
const _ref = this.getNativeReference();
return createXMLReference(XMLAttribute, xmlSetProp(_ref, key, value.toString()));
}
/**
* set multiple attributes
* BREAKING CHANGE: no longer overloaded for setting single attr
* @param attributes
* @returns
*/
public attr(attributes: XMLAttributeMap): XMLElement {
if (typeof attributes === "string") {
console.error("Use XMLElement.setAttribute instead");
return this;
}
Object.keys(attributes).forEach((k) => {
this.setAttribute(k, attributes[k] || "");
});
return this;
}
public attrs(): XMLAttribute[] {
const attrs: XMLAttribute[] = [];
const _ref = this.getNativeReference();
let attr = _ref.properties;
while (attr !== null) {
attrs.push(createXMLReferenceOrThrow(XMLAttribute, attr, XMLNodeError.NO_REF));
attr = attr.next;
}
return attrs;
}
public cdata(content: string) {
const _ref = this.getNativeReference();
const cdata = xmlNewCDataBlock(_ref.doc, content, content.length);
this.addChild(createXMLReferenceOrThrow(XMLElement, cdata, XMLNodeError.NO_REF));
return this;
}
public text(content?: string) {
const _ref = this.getNativeReference();
if (content === undefined) {
return xmlNodeGetContent(_ref);
}
const doc = this.doc();
if (doc && this.type() !== "comment") {
content = doc.encode(content);
}
this.childNodes().forEach((child) => {
xmlUnlinkNode(child.getNativeReference());
});
xmlNodeSetContent(_ref, content);
return content;
}
public path() {
return xmlGetNodePath(this.getNativeReference());
}
public replace(value: XMLElement | string) {
const _ref = this.getNativeReference();
let element: XMLNode | null = null;
const doc = this.doc();
if (doc && typeof value === "string") {
element = doc.createText(value, false);
} else if (value instanceof XMLNode) {
element = this.importNode(value.getNativeReference());
}
const _newRef = xmlReplaceNode(_ref, (element as XMLElement).getNativeReference());
if (_newRef !== null) {
this.setNativeReference(_newRef);
}
}
}
export class XMLNamespace extends XMLReference<xmlNsPtr> {
/**
* @private
* @param _ref
*/
constructor(_ref: any) {
super(_ref);
}
public name() {
return this.prefix() || "";
}
public prefix() {
return this.getNativeReference().prefix || null;
}
public href(): string {
return this.getNativeReference().href;
}
/**
* @private
* @param _nodeRef
*/
public _applyToNode(_nodeRef: xmlNodePtr) {
xmlSetNs(_nodeRef, this.getNativeReference());
}
}
export class XMLAttribute extends XMLNode {
/**
* @private
* @param _ref
*/
constructor(_ref: any) {
super(_ref);
}
public name(): string {
return this.getNativeReference().name;
}
public defineNamespace(prefix: string | null, href?: string | null): XMLNamespace {
// should probably do xmlSetNsProp
return this.node().defineNamespace(prefix, href);
}
public value(value?: string): string {
const _ref = this.getNativeReference();
if (typeof value === "string") {
const content = xmlEncodeEntitiesReentrant(_ref.doc, value);
_ref.children = xmlStringGetNodeList(_ref.doc, content);
_ref.last = null;
let child = _ref.children;
while (child !== null) {
child.parent = _ref;
child.doc = _ref.doc;
if (child.next === null) {
_ref.last = child;
}
child = child.next;
}
}
return _ref.children?.content || "";
}
public node(): XMLElement {
return createXMLReferenceOrThrow(XMLElement, this.getNativeReference().parent, XMLNodeError.NO_REF);
}
}
export class XMLDTD extends XMLReference<xmlDtdPtr> {
public name: string;
public externalId: string | null;
public systemId: string | null;
/**
* @private
* @param _ref
*/
constructor(_ref: any) {
super(_ref);
this.name = this.getName();
this.externalId = this.getExternalID();
this.systemId = this.getSystemID();
}
public getName(): string {
return this.getNativeReference()?.name || "";
}
public getExternalID(): string | null {
return this.getNativeReference()?.ExternalID || null;
}
public getSystemID(): string | null {
return this.getNativeReference()?.SystemID || null;
}
public unlink() {
xmlUnlinkNode(this.getNativeReference() as any);
}
}
export class XMLText extends XMLElement {
/**
* @private
* @param _ref
*/
constructor(_ref: any) {
super(_ref);
}
}