11import {
2- XmlError , xmlDocGetRootElement , xmlFreeDoc , xmlNewDoc ,
2+ XmlError ,
3+ xmlDocGetRootElement ,
4+ xmlFreeDoc ,
5+ xmlNewDoc ,
6+ xmlResetLastError ,
7+ xmlGetLastError ,
8+ XmlErrorStruct , xmlReadString , XmlParseError , xmlReadMemory ,
39} from './libxml2.mjs' ;
410import { XmlElement , XmlNode } from './nodes.mjs' ;
511import { XmlXPath , NamespaceMap } from './xpath.mjs' ;
12+ import type { XmlDocPtr } from './libxml2raw' ;
613
7- export default class XmlDocument {
14+ export enum ParseOption {
15+ XML_PARSE_DEFAULT = 0 ,
16+ /** recover on errors */
17+ XML_PARSE_RECOVER = 1 << 0 ,
18+ /** substitute entities */
19+ XML_PARSE_NOENT = 1 << 1 ,
20+ /** load the external subset */
21+ XML_PARSE_DTDLOAD = 1 << 2 ,
22+ /** default DTD attributes */
23+ XML_PARSE_DTDATTR = 1 << 3 ,
24+ /** validate with the DTD */
25+ XML_PARSE_DTDVALID = 1 << 4 ,
26+ /** suppress error reports */
27+ XML_PARSE_NOERROR = 1 << 5 ,
28+ /** suppress warning reports */
29+ XML_PARSE_NOWARNING = 1 << 6 ,
30+ /** pedantic error reporting */
31+ XML_PARSE_PEDANTIC = 1 << 7 ,
32+ /** remove blank nodes */
33+ XML_PARSE_NOBLANKS = 1 << 8 ,
34+ /** use the SAX1 interface internally */
35+ XML_PARSE_SAX1 = 1 << 9 ,
36+ /** Implement XInclude substitution */
37+ XML_PARSE_XINCLUDE = 1 << 10 ,
38+ /** Forbid network access */
39+ XML_PARSE_NONET = 1 << 11 ,
40+ /** Do not reuse the context dictionary */
41+ XML_PARSE_NODICT = 1 << 12 ,
42+ /** remove redundant namespaces declarations */
43+ XML_PARSE_NSCLEAN = 1 << 13 ,
44+ /** merge CDATA as text nodes */
45+ XML_PARSE_NOCDATA = 1 << 14 ,
46+ /** do not generate XINCLUDE START/END nodes */
47+ XML_PARSE_NOXINCNODE = 1 << 15 ,
48+ /** compact small text nodes;
49+ * no modification of the tree allowed afterward
50+ * (will possibly crash if you try to modify the tree)
51+ */
52+ XML_PARSE_COMPACT = 1 << 16 ,
53+ /** parse using XML-1.0 before update 5 */
54+ XML_PARSE_OLD10 = 1 << 17 ,
55+ /** do not fixup XINCLUDE xml:base uris */
56+ XML_PARSE_NOBASEFIX = 1 << 18 ,
57+ /** relax any hardcoded limit from the parser */
58+ XML_PARSE_HUGE = 1 << 19 ,
59+ /* parse using SAX2 interface before 2.7.0 */
60+ XML_PARSE_OLDSAX = 1 << 20 ,
61+ /** ignore internal document encoding hint */
62+ XML_PARSE_IGNORE_ENC = 1 << 21 ,
63+ /** Store big lines numbers in text PSVI field */
64+ XML_PARSE_BIG_LINES = 1 << 22 ,
65+ }
66+
67+ export interface ParseOptions {
68+ url ?: string ; // reserved
69+ encoding ?: string ; // reserved
70+ option ?: ParseOption ;
71+ }
72+
73+ export class XmlDocument {
874 /** @internal */
975 _docPtr : number ;
1076
11- /** Create a new document.
12- * To parse an existing xml, use {@link parseXmlString}.
13- */
14- constructor ( ) ;
1577 /** Create a document object wrapping document parsed by libxml2.
1678 * @see {@link parseXmlString }
1779 * @internal
1880 */
19- constructor ( xmlDocPtr : number ) ;
20- constructor ( xmlDocPtr ?: number ) {
21- this . _docPtr = xmlDocPtr ?? xmlNewDoc ( ) ;
81+ private constructor ( xmlDocPtr : XmlDocPtr ) {
82+ if ( ! xmlDocPtr ) {
83+ const err = xmlGetLastError ( ) ;
84+ throw new XmlParseError ( XmlErrorStruct . message ( err ) ) ;
85+ }
86+ this . _docPtr = xmlDocPtr ;
2287 }
2388
2489 /**
@@ -30,6 +95,43 @@ export default class XmlDocument {
3095 xmlFreeDoc ( this . _docPtr ) ;
3196 }
3297
98+ /** Create a new document from scratch.
99+ * To parse an existing xml, use {@link fromBuffer} or {@link fromString}.
100+ */
101+ static create ( ) : XmlDocument {
102+ return new XmlDocument ( xmlNewDoc ( ) ) ;
103+ }
104+
105+ /**
106+ * Parse and create an {@link XmlDocument} from an XML string.
107+ * @param source The XML string
108+ * @param options Parsing options
109+ */
110+ static fromString (
111+ source : string ,
112+ options : ParseOptions = { } ,
113+ ) : XmlDocument {
114+ xmlResetLastError ( ) ;
115+ return new XmlDocument (
116+ xmlReadString ( source , '' , '' , options . option ?? ParseOption . XML_PARSE_DEFAULT ) ,
117+ ) ;
118+ }
119+
120+ /**
121+ * Parse and create an {@link XmlDocument} from an XML buffer.
122+ * @param source The XML buffer
123+ * @param options Parsing options
124+ */
125+ static fromBuffer (
126+ source : Uint8Array ,
127+ options : ParseOptions = { } ,
128+ ) : XmlDocument {
129+ xmlResetLastError ( ) ;
130+ return new XmlDocument (
131+ xmlReadMemory ( source , '' , '' , options . option ?? ParseOption . XML_PARSE_DEFAULT ) ,
132+ ) ;
133+ }
134+
33135 get ( xpath : XmlXPath ) : XmlNode | null ;
34136 get ( xpath : string , namespaces ?: NamespaceMap ) : XmlNode | null ;
35137 /**
0 commit comments