@e-mc/document 0.9.9 → 0.9.11
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 +10 -10
- package/README.md +196 -196
- package/asset.d.ts +7 -7
- package/asset.js +1 -1
- package/index.d.ts +4 -4
- package/index.js +18 -20
- package/package.json +31 -31
- package/parse/dom.d.ts +8 -8
- package/parse/dom.js +2 -2
- package/parse/index.d.ts +8 -8
- package/parse/index.js +18 -23
- package/parse/types/index.d.ts +240 -240
- package/transform/index.d.ts +7 -7
- package/transform/index.js +2 -2
- package/util.d.ts +16 -16
- package/util.js +2 -2
package/package.json
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@e-mc/document",
|
|
3
|
-
"version": "0.9.
|
|
4
|
-
"description": "Document constructor for E-mc.",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
13
|
-
"directory": "src/document"
|
|
14
|
-
},
|
|
15
|
-
"keywords": [
|
|
16
|
-
"squared",
|
|
17
|
-
"squared-functions"
|
|
18
|
-
],
|
|
19
|
-
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
-
"license": "BSD 3-Clause",
|
|
21
|
-
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.9.
|
|
24
|
-
"@e-mc/db": "0.9.
|
|
25
|
-
"@e-mc/types": "0.9.
|
|
26
|
-
"chalk": "4.1.2",
|
|
27
|
-
"htmlparser2": "^9.1.0",
|
|
28
|
-
"js-yaml": "^4.1.0",
|
|
29
|
-
"picomatch": "^4.0.2"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@e-mc/document",
|
|
3
|
+
"version": "0.9.11",
|
|
4
|
+
"description": "Document constructor for E-mc.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
13
|
+
"directory": "src/document"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"squared",
|
|
17
|
+
"squared-functions"
|
|
18
|
+
],
|
|
19
|
+
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
+
"license": "BSD 3-Clause",
|
|
21
|
+
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@e-mc/core": "0.9.11",
|
|
24
|
+
"@e-mc/db": "0.9.11",
|
|
25
|
+
"@e-mc/types": "0.9.11",
|
|
26
|
+
"chalk": "4.1.2",
|
|
27
|
+
"htmlparser2": "^9.1.0",
|
|
28
|
+
"js-yaml": "^4.1.0",
|
|
29
|
+
"picomatch": "^4.0.2"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/parse/dom.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { DomWriterConstructor, HtmlElementConstructor, IGNORE_FLAG } from './types';
|
|
2
|
-
|
|
3
|
-
declare namespace dom {
|
|
4
|
-
const IGNORE_FLAG: IGNORE_FLAG;
|
|
5
|
-
const DomWriter: DomWriterConstructor;
|
|
6
|
-
const HtmlElement: HtmlElementConstructor;
|
|
7
|
-
}
|
|
8
|
-
|
|
1
|
+
import type { DomWriterConstructor, HtmlElementConstructor, IGNORE_FLAG } from './types';
|
|
2
|
+
|
|
3
|
+
declare namespace dom {
|
|
4
|
+
const IGNORE_FLAG: IGNORE_FLAG;
|
|
5
|
+
const DomWriter: DomWriterConstructor;
|
|
6
|
+
const HtmlElement: HtmlElementConstructor;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
9
|
export = dom;
|
package/parse/dom.js
CHANGED
|
@@ -106,7 +106,7 @@ class DomWriter extends index_1.XmlWriter {
|
|
|
106
106
|
else {
|
|
107
107
|
options = { normalize: options, escapeEntities, parser };
|
|
108
108
|
}
|
|
109
|
-
parser
|
|
109
|
+
parser ||= { ...PARSER_OPTIONS };
|
|
110
110
|
super(documentName, source, elements, { parser });
|
|
111
111
|
this.documentElement = null;
|
|
112
112
|
this.ignoreTagName = "title|style|script";
|
|
@@ -219,7 +219,7 @@ class DomWriter extends index_1.XmlWriter {
|
|
|
219
219
|
exports.DomWriter = DomWriter;
|
|
220
220
|
class HtmlElement extends index_1.XmlElement {
|
|
221
221
|
constructor(documentName, node, attributes, options = {}) {
|
|
222
|
-
options.parser
|
|
222
|
+
options.parser ||= { ...PARSER_OPTIONS };
|
|
223
223
|
super(documentName, node, attributes, { ...options, tagVoid: TAG_VOID.includes(node.tagName) });
|
|
224
224
|
this._documentType = "HTML";
|
|
225
225
|
}
|
package/parse/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { IGNORE_FLAG, XmlElementConstructor, XmlWriterConstructor } from './types';
|
|
2
|
-
|
|
3
|
-
declare namespace parse {
|
|
4
|
-
const IGNORE_FLAG: IGNORE_FLAG;
|
|
5
|
-
const XmlWriter: XmlWriterConstructor;
|
|
6
|
-
const XmlElement: XmlElementConstructor;
|
|
7
|
-
}
|
|
8
|
-
|
|
1
|
+
import type { IGNORE_FLAG, XmlElementConstructor, XmlWriterConstructor } from './types';
|
|
2
|
+
|
|
3
|
+
declare namespace parse {
|
|
4
|
+
const IGNORE_FLAG: IGNORE_FLAG;
|
|
5
|
+
const XmlWriter: XmlWriterConstructor;
|
|
6
|
+
const XmlElement: XmlElementConstructor;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
9
|
export = parse;
|
package/parse/index.js
CHANGED
|
@@ -49,9 +49,8 @@ function resetTagPosition(item) {
|
|
|
49
49
|
item.tagCount = 0;
|
|
50
50
|
}
|
|
51
51
|
function findCloseIndex(source, tagName, lastIndex, ignoreCase) {
|
|
52
|
-
var _a;
|
|
53
52
|
const flags = ignoreCase ? 'gi' : 'g';
|
|
54
|
-
const pattern = CACHE_TAGNAME[
|
|
53
|
+
const pattern = CACHE_TAGNAME[tagName + flags + '0'] ||= new RegExp(`<(?:${escapeTagName(tagName)}\\s*|/${escapeTagName(tagName)}\\s*>)`, flags);
|
|
55
54
|
pattern.lastIndex = lastIndex;
|
|
56
55
|
let openTag = 1, closeTag = 0, match;
|
|
57
56
|
while (match = pattern.exec(source)) {
|
|
@@ -134,7 +133,7 @@ class XmlWriter {
|
|
|
134
133
|
return leading + content + trailing;
|
|
135
134
|
}
|
|
136
135
|
static escapeXmlString(value, ampersand) {
|
|
137
|
-
return value.replace(ampersand ? /[<>"'&]/g : /[<>"']|&(?![a-z][a-z\d]+|#(?:\d+|x[a-f\d]+))/
|
|
136
|
+
return value.replace(ampersand ? /[<>"'&]/g : /[<>"']|&(?![a-z][a-z\d]+|#(?:\d+|x[a-f\d]+))/gi, capture => {
|
|
138
137
|
switch (capture) {
|
|
139
138
|
case '<':
|
|
140
139
|
return '<';
|
|
@@ -297,7 +296,6 @@ class XmlWriter {
|
|
|
297
296
|
return { element, parser, outDom, error: null };
|
|
298
297
|
}
|
|
299
298
|
static getTagOffset(source, replacement, options) {
|
|
300
|
-
var _a, _b;
|
|
301
299
|
if ((0, types_1.isObject)(replacement)) {
|
|
302
300
|
options = replacement;
|
|
303
301
|
replacement = undefined;
|
|
@@ -307,14 +305,14 @@ class XmlWriter {
|
|
|
307
305
|
const { ignoreCase, ignoreTagName, ignoreTagGroup } = options;
|
|
308
306
|
if (ignoreTagName) {
|
|
309
307
|
const flags = ignoreCase ? 'gi' : 'g';
|
|
310
|
-
source = source.replace(CACHE_TAGOFFSET[
|
|
308
|
+
source = source.replace(CACHE_TAGOFFSET[ignoreTagName + flags] ||= new RegExp(`<(?:${ignoreTagName.split('|').map(value => escapeTagName(value)).join('|')})${XmlWriter.PATTERN_TAGOPEN}*>[\\S\\s]*?<\\/\\1\\s*>`, flags), '');
|
|
311
309
|
}
|
|
312
310
|
if (ignoreTagGroup) {
|
|
313
311
|
for (let i = 0, length = ignoreTagGroup.length; i < length; i += 2) {
|
|
314
312
|
const start = ignoreTagGroup[i];
|
|
315
313
|
const end = ignoreTagGroup[i + 1];
|
|
316
314
|
if (end) {
|
|
317
|
-
source = source.replace(CACHE_TAGOFFSET[
|
|
315
|
+
source = source.replace(CACHE_TAGOFFSET[start + end] ||= new RegExp(escapeTagName(start) + '[\\S\\s]*?' + escapeTagName(end), 'g'), '');
|
|
318
316
|
}
|
|
319
317
|
}
|
|
320
318
|
}
|
|
@@ -354,10 +352,9 @@ class XmlWriter {
|
|
|
354
352
|
return `data-${document}-id`;
|
|
355
353
|
}
|
|
356
354
|
static getPatternId(name) {
|
|
357
|
-
return CACHE_PATTERNID[name]
|
|
355
|
+
return CACHE_PATTERNID[name] ||= new RegExp(`\\s${(0, types_1.escapePattern)(name)}="([^"]+)"`);
|
|
358
356
|
}
|
|
359
357
|
static getCommentsAndCDATA(source, tagPattern = '', ignoreCase, stripXml) {
|
|
360
|
-
var _a, _b;
|
|
361
358
|
let tagGroup;
|
|
362
359
|
if (stripXml) {
|
|
363
360
|
if (typeof tagPattern !== 'string') {
|
|
@@ -375,7 +372,7 @@ class XmlWriter {
|
|
|
375
372
|
}
|
|
376
373
|
const result = [];
|
|
377
374
|
const flags = ignoreCase ? 'gi' : 'g';
|
|
378
|
-
let pattern = CACHE_TAGNAME[
|
|
375
|
+
let pattern = CACHE_TAGNAME[tagPattern + flags] ||= new RegExp(`<(?:(!--[\\S\\s]*?--)|(!\\[CDATA\\[[\\S\\s]*?\\]\\])` + (tagPattern ? '|' + `(${tagPattern})${PATTERN_TAGOPEN}*` : '') + ')>', flags), match;
|
|
379
376
|
pattern.lastIndex = 0;
|
|
380
377
|
while (match = pattern.exec(source)) {
|
|
381
378
|
if (stripXml) {
|
|
@@ -400,7 +397,7 @@ class XmlWriter {
|
|
|
400
397
|
const start = tagGroup[i];
|
|
401
398
|
const end = tagGroup[i + 1];
|
|
402
399
|
if (end) {
|
|
403
|
-
pattern = CACHE_TAGNAME[
|
|
400
|
+
pattern = CACHE_TAGNAME[start + end] ||= new RegExp(escapeTagName(start) + '[\\S\\s]*?' + escapeTagName(end), 'g');
|
|
404
401
|
for (const tag of source.matchAll(pattern)) {
|
|
405
402
|
const startIndex = tag.index;
|
|
406
403
|
result.push({ type: 'block', outerXml: tag[0], startIndex, endIndex: startIndex + tag[0].length - 1 });
|
|
@@ -521,7 +518,7 @@ class XmlWriter {
|
|
|
521
518
|
if (!(tagName in this._tagCount) && isCount(append.tagCount)) {
|
|
522
519
|
this._tagCount[tagName] = append.tagCount;
|
|
523
520
|
}
|
|
524
|
-
append.id
|
|
521
|
+
append.id ||= this.newId;
|
|
525
522
|
element.setAppend(append);
|
|
526
523
|
}
|
|
527
524
|
return element;
|
|
@@ -552,7 +549,7 @@ class XmlWriter {
|
|
|
552
549
|
return true;
|
|
553
550
|
}
|
|
554
551
|
const { node, append } = element;
|
|
555
|
-
element.parser
|
|
552
|
+
element.parser ||= this.parser;
|
|
556
553
|
element.newline = this.newline;
|
|
557
554
|
let output, outerXml = '', error;
|
|
558
555
|
if (element.hasPosition()) {
|
|
@@ -580,7 +577,7 @@ class XmlWriter {
|
|
|
580
577
|
if (!prepend) {
|
|
581
578
|
node.index = nextSibling ?? -1;
|
|
582
579
|
}
|
|
583
|
-
(node.id
|
|
580
|
+
(node.id ||= {})[this.documentName] = id;
|
|
584
581
|
element.id = id;
|
|
585
582
|
element.innerXml = textContent;
|
|
586
583
|
const offset = element.getInnerOffset(tagName);
|
|
@@ -936,7 +933,7 @@ class XmlWriter {
|
|
|
936
933
|
const source = this.source;
|
|
937
934
|
const match = new RegExp(`<(${tagName && escapeTagName(tagName) || '[^\\s>]+'})${PATTERN_TAGOPEN}+?${(0, types_1.escapePattern)(this.nameOfId)}="${(0, types_1.escapePattern)(id)}"`, ignoreCase ? 'i' : '').exec(source);
|
|
938
935
|
if (match) {
|
|
939
|
-
tagName
|
|
936
|
+
tagName ||= match[1];
|
|
940
937
|
const startIndex = match.index;
|
|
941
938
|
let endIndex = -1, closeTag = 0;
|
|
942
939
|
if (!tagVoid) {
|
|
@@ -951,7 +948,6 @@ class XmlWriter {
|
|
|
951
948
|
}
|
|
952
949
|
}
|
|
953
950
|
getElementsByTagName(tagName, ignoreCase = false, options) {
|
|
954
|
-
var _a;
|
|
955
951
|
let tagVoid;
|
|
956
952
|
if (options) {
|
|
957
953
|
({ tagVoid } = options);
|
|
@@ -961,7 +957,7 @@ class XmlWriter {
|
|
|
961
957
|
const result = [];
|
|
962
958
|
const patternId = XmlWriter.getPatternId(this.nameOfId);
|
|
963
959
|
const flags = ignoreCase ? 'gi' : 'g';
|
|
964
|
-
const pattern = CACHE_TAGNAME[
|
|
960
|
+
const pattern = CACHE_TAGNAME[tagName + flags + '3'] ||= new RegExp(`<${escapeTagName(tagName) + PATTERN_TAGOPEN}*>`, flags);
|
|
965
961
|
for (const match of source.matchAll(pattern)) {
|
|
966
962
|
const startIndex = match.index;
|
|
967
963
|
let outerXml = match[0], endIndex = startIndex + outerXml.length - 1;
|
|
@@ -1016,7 +1012,7 @@ class XmlWriter {
|
|
|
1016
1012
|
return XmlWriter.getNameOfId(this.documentName);
|
|
1017
1013
|
}
|
|
1018
1014
|
get patternIgnore() {
|
|
1019
|
-
return this.ignoreTagName ? this._patternIgnore
|
|
1015
|
+
return this.ignoreTagName ? this._patternIgnore ||= new RegExp(`^(?:${this.ignoreTagName})$`, this.ignoreCaseTagName ? 'i' : '') : null;
|
|
1020
1016
|
}
|
|
1021
1017
|
set source(value) {
|
|
1022
1018
|
if (value !== this._source) {
|
|
@@ -1132,7 +1128,7 @@ class XmlElement {
|
|
|
1132
1128
|
}
|
|
1133
1129
|
getTagOffset(replacement, options = {}) {
|
|
1134
1130
|
if (!this.tagVoid || this._prevTagName && !XmlWriter.isTagVoid(this.documentType, this._prevTagName)) {
|
|
1135
|
-
options.parser
|
|
1131
|
+
options.parser ||= this.parser;
|
|
1136
1132
|
if (!this.node.append) {
|
|
1137
1133
|
return XmlWriter.getTagOffset(this.innerXml, replacement, options);
|
|
1138
1134
|
}
|
|
@@ -1244,7 +1240,6 @@ class XmlElement {
|
|
|
1244
1240
|
return [source.substring(0, startIndex) + leading + outerXml + trailing + source.substring(endIndex), outerXml];
|
|
1245
1241
|
}
|
|
1246
1242
|
write(source, invalid) {
|
|
1247
|
-
var _a, _b;
|
|
1248
1243
|
if (!this._modified) {
|
|
1249
1244
|
return ['', ''];
|
|
1250
1245
|
}
|
|
@@ -1261,7 +1256,7 @@ class XmlElement {
|
|
|
1261
1256
|
let position;
|
|
1262
1257
|
if (XmlWriter.isTagVoid(this.documentType, tagName)) {
|
|
1263
1258
|
const openTag = [];
|
|
1264
|
-
const pattern = CACHE_TAGNAME[
|
|
1259
|
+
const pattern = CACHE_TAGNAME[tagName + flags + '1'] ||= new RegExp(`<${escapeTagName(tagName)}[\\s|>]`, flags);
|
|
1265
1260
|
let openCount = 0, match;
|
|
1266
1261
|
pattern.lastIndex = 0;
|
|
1267
1262
|
while (match = pattern.exec(source)) {
|
|
@@ -1284,7 +1279,7 @@ class XmlElement {
|
|
|
1284
1279
|
}
|
|
1285
1280
|
else if (id || isIndex(tagIndex) && isCount(tagCount)) {
|
|
1286
1281
|
complete: {
|
|
1287
|
-
const pattern = CACHE_TAGNAME[
|
|
1282
|
+
const pattern = CACHE_TAGNAME[tagName + flags + '2'] ||= new RegExp(`<(?:${escapeTagName(tagName)}[\\s|>]|/${escapeTagName(tagName)}\\s*>)`, flags);
|
|
1288
1283
|
const openTag = [];
|
|
1289
1284
|
let openCount = 0, found, match;
|
|
1290
1285
|
pattern.lastIndex = 0;
|
|
@@ -1329,7 +1324,7 @@ class XmlElement {
|
|
|
1329
1324
|
}
|
|
1330
1325
|
}
|
|
1331
1326
|
}
|
|
1332
|
-
if (position
|
|
1327
|
+
if (position ||= this.findIndexOf(source)) {
|
|
1333
1328
|
if (remove) {
|
|
1334
1329
|
position.remove = true;
|
|
1335
1330
|
}
|
|
@@ -1389,7 +1384,7 @@ class XmlElement {
|
|
|
1389
1384
|
}
|
|
1390
1385
|
const tagName = this.tagName;
|
|
1391
1386
|
if (value !== tagName) {
|
|
1392
|
-
this._prevTagName
|
|
1387
|
+
this._prevTagName ||= tagName;
|
|
1393
1388
|
this._tagName = value;
|
|
1394
1389
|
if (!CACHE_TAGVOID[this.documentType]) {
|
|
1395
1390
|
this._tagVoid = null;
|