@file-type/xml 0.2.0 → 0.2.1
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/lib/index.js +17 -9
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -83,18 +83,22 @@ const rootNameMapping = {
|
|
|
83
83
|
|
|
84
84
|
export class XmlTextDetector {
|
|
85
85
|
|
|
86
|
-
constructor() {
|
|
86
|
+
constructor(options) {
|
|
87
|
+
this.options = options ?? {};
|
|
87
88
|
this.firstTag = true;
|
|
88
89
|
this.onEnd = false;
|
|
89
90
|
this.parser = sax.parser(true);
|
|
90
|
-
this.
|
|
91
|
-
this.validClose = false;
|
|
91
|
+
this.nesting = 0;
|
|
92
92
|
|
|
93
93
|
this.parser.onerror = e => {
|
|
94
|
+
if (e.message.startsWith('Invalid character entity')) { // Allow entity reference
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.fileType = undefined;
|
|
94
98
|
this.onEnd = true;
|
|
95
99
|
};
|
|
96
100
|
this.parser.onopentag = node => {
|
|
97
|
-
++this.
|
|
101
|
+
++this.nesting;
|
|
98
102
|
if (!this.firstTag || this.onEnd) {
|
|
99
103
|
return;
|
|
100
104
|
}
|
|
@@ -108,13 +112,14 @@ export class XmlTextDetector {
|
|
|
108
112
|
this.fileType = rootNameMapping[nsNode.name?.toLowerCase()];
|
|
109
113
|
}
|
|
110
114
|
|
|
111
|
-
if (this.fileType) {
|
|
115
|
+
if (this.fileType && !this.options.fullScan) {
|
|
112
116
|
this.onEnd = true;
|
|
113
117
|
}
|
|
114
118
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
119
|
+
|
|
120
|
+
this.parser.onclosetag = () => {
|
|
121
|
+
--this.nesting;
|
|
122
|
+
}
|
|
118
123
|
}
|
|
119
124
|
|
|
120
125
|
write(text) {
|
|
@@ -125,6 +130,10 @@ export class XmlTextDetector {
|
|
|
125
130
|
this.parser.close();
|
|
126
131
|
this.onEnd = true;
|
|
127
132
|
}
|
|
133
|
+
|
|
134
|
+
isValid() {
|
|
135
|
+
return this.nesting === 0;
|
|
136
|
+
}
|
|
128
137
|
}
|
|
129
138
|
|
|
130
139
|
export const detectXml = async tokenizer => {
|
|
@@ -138,7 +147,6 @@ export const detectXml = async tokenizer => {
|
|
|
138
147
|
|
|
139
148
|
if (xml) {
|
|
140
149
|
await tokenizer.ignore(offset);
|
|
141
|
-
let fileType;
|
|
142
150
|
|
|
143
151
|
const xmlTextDetector = new XmlTextDetector();
|
|
144
152
|
|