@e-mc/document 0.9.7 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/document",
3
- "version": "0.9.7",
3
+ "version": "0.10.0",
4
4
  "description": "Document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,9 +20,9 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/core": "0.9.7",
24
- "@e-mc/db": "0.9.7",
25
- "@e-mc/types": "0.9.7",
23
+ "@e-mc/core": "0.10.0",
24
+ "@e-mc/db": "0.10.0",
25
+ "@e-mc/types": "0.10.0",
26
26
  "chalk": "4.1.2",
27
27
  "htmlparser2": "^9.1.0",
28
28
  "js-yaml": "^4.1.0",
package/parse/dom.js CHANGED
@@ -3,10 +3,10 @@ exports.IGNORE_FLAG = exports.HtmlElement = exports.DomWriter = void 0;
3
3
  const htmlparser2 = require("htmlparser2");
4
4
  const domhandler = require("domhandler");
5
5
  const domutils = require("domutils");
6
- const types_1 = require("@e-mc/types");
7
- const util_1 = require("@e-mc/document/util");
8
6
  const index_1 = require("@e-mc/document/parse");
9
7
  Object.defineProperty(exports, "IGNORE_FLAG", { enumerable: true, get: function () { return index_1.IGNORE_FLAG; } });
8
+ const types_1 = require("@e-mc/types");
9
+ const util_1 = require("@e-mc/document/util");
10
10
  const Parser = htmlparser2.Parser;
11
11
  const DomHandler = domhandler.DomHandler;
12
12
  const TAG_VOID = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
@@ -38,7 +38,7 @@ class DomWriter extends index_1.XmlWriter {
38
38
  }
39
39
  }
40
40
  }
41
- const pattern = new RegExp(`<(?:!--[\\S\\s]*?--|([^\\s<>/${ignoreChar}]+)(${index_1.XmlWriter.PATTERN_TAGOPEN}*?)(\\s*\\/?\\s*)|\\/([^\\s>]+)(\\s*))>`, 'g');
41
+ const pattern = new RegExp(`<(?:!--.*?--|([^\\s<>/${ignoreChar}]+)(${index_1.XmlWriter.PATTERN_TAGOPEN}*?)(\\s*\\/?\\s*)|\\/([^\\s>]+)(\\s*))>`, 'gs');
42
42
  let match;
43
43
  while (match = pattern.exec(source)) {
44
44
  let tag;
@@ -126,9 +126,8 @@ class DomWriter extends index_1.XmlWriter {
126
126
  }
127
127
  }
128
128
  }
129
- const setNewline = (value) => this.newline = (0, util_1.getNewline)(value);
130
129
  if (!documentElement) {
131
- setNewline(source);
130
+ this.setNewline(source);
132
131
  if (normalize) {
133
132
  source = DomWriter.normalize(source, { newline: this.newline, ignoreChar: typeof normalize === 'string' ? normalize : '', ignoreTagGroup, escapeEntities });
134
133
  }
@@ -137,13 +136,14 @@ class DomWriter extends index_1.XmlWriter {
137
136
  }
138
137
  }
139
138
  else {
140
- setNewline(documentElement.innerXml);
139
+ this.setNewline(documentElement.innerXml);
141
140
  }
142
141
  const html = /^(\s*(?:<\?xml[^>]+>\s*)?(?:<!DOCTYPE[^>]+>\s*)?)<html[\s>]/i.exec(source) || /^(.*)<html[\s>]/is.exec(source);
143
142
  if (html) {
144
- const endIndex = index_1.XmlWriter.findCloseTag(source, html.index);
143
+ const index = html[1] ? html[1].length : 0;
144
+ const endIndex = index_1.XmlWriter.findCloseTag(source, index);
145
145
  if (endIndex !== -1) {
146
- startIndex = html.index;
146
+ startIndex = index;
147
147
  outerXml = source.substring(startIndex, endIndex + 1);
148
148
  }
149
149
  }
@@ -167,13 +167,15 @@ class DomWriter extends index_1.XmlWriter {
167
167
  }
168
168
  else {
169
169
  const trailing = items.filter(item => item.textContent);
170
- if (trailing.length) {
170
+ if (trailing.length > 0) {
171
171
  const match = /<\/body\s*>\s*<\/html\s*>.*$/is.exec(source) || /<\/body\s*>.*$/is.exec(source);
172
172
  if (match) {
173
173
  const textContent = trailing.reduce((a, b) => a + b.textContent, '');
174
174
  offsetMap = index_1.XmlWriter.getTagOffset(textContent, { ignoreCase: this.ignoreCaseTagName, ignoreTagName: this.ignoreTagName, parser: this.parser });
175
175
  source = source.substring(0, match.index) + textContent + source.substring(match.index);
176
- trailing.forEach(item => delete item.textContent);
176
+ trailing.forEach(item => {
177
+ delete item.textContent;
178
+ });
177
179
  }
178
180
  }
179
181
  }
@@ -215,6 +217,9 @@ class DomWriter extends index_1.XmlWriter {
215
217
  newElement(node) {
216
218
  return new HtmlElement(this.documentName, node);
217
219
  }
220
+ setNewline(value) {
221
+ this.newline = (0, util_1.getNewline)(value);
222
+ }
218
223
  }
219
224
  exports.DomWriter = DomWriter;
220
225
  class HtmlElement extends index_1.XmlElement {
package/parse/index.js CHANGED
@@ -3,8 +3,10 @@ exports.XmlElement = exports.XmlWriter = exports.IGNORE_FLAG = void 0;
3
3
  const htmlparser2 = require("htmlparser2");
4
4
  const domhandler = require("domhandler");
5
5
  const domutils = require("domutils");
6
- const types_1 = require("@e-mc/types");
6
+ const crypto = require("crypto");
7
7
  const module_1 = require("@e-mc/module");
8
+ const types_1 = require("@e-mc/types");
9
+ const util_1 = require("@e-mc/document/util");
8
10
  var IGNORE_FLAG;
9
11
  (function (IGNORE_FLAG) {
10
12
  IGNORE_FLAG[IGNORE_FLAG["NONE"] = 0] = "NONE";
@@ -96,15 +98,30 @@ function getParserDOM(source, parser) {
96
98
  }
97
99
  const isIndex = (value) => typeof value === 'number' && value >= 0 && value !== Infinity;
98
100
  const isCount = (value) => typeof value === 'number' && value > 0 && value !== Infinity;
101
+ const hasId = (id, source, startIndex, endIndex) => source.substring(startIndex, endIndex).indexOf(id) !== -1;
99
102
  const escapeTagName = (value) => value.replace(REGEXP_ESCAPECHAR, capture => capture === '-' ? '\\x2d' : '\\' + capture);
100
103
  class XmlWriter {
101
- static get PATTERN_ATTRNAME() { return PATTERN_ATTRNAME; }
102
- static get PATTERN_ATTRVALUE() { return PATTERN_ATTRVALUE; }
103
- static get PATTERN_TAGNAME() { return PATTERN_TAGNAME; }
104
- static get PATTERN_TAGOPEN() { return PATTERN_TAGOPEN; }
105
- static get PATTERN_QUOTEVALUE() { return PATTERN_QUOTEVALUE; }
106
- static get PATTERN_COMMENT() { return PATTERN_COMMENT; }
107
- static get PATTERN_TRAILINGSPACE() { return PATTERN_TRAILINGSPACE; }
104
+ static get PATTERN_ATTRNAME() {
105
+ return PATTERN_ATTRNAME;
106
+ }
107
+ static get PATTERN_ATTRVALUE() {
108
+ return PATTERN_ATTRVALUE;
109
+ }
110
+ static get PATTERN_TAGNAME() {
111
+ return PATTERN_TAGNAME;
112
+ }
113
+ static get PATTERN_TAGOPEN() {
114
+ return PATTERN_TAGOPEN;
115
+ }
116
+ static get PATTERN_QUOTEVALUE() {
117
+ return PATTERN_QUOTEVALUE;
118
+ }
119
+ static get PATTERN_COMMENT() {
120
+ return PATTERN_COMMENT;
121
+ }
122
+ static get PATTERN_TRAILINGSPACE() {
123
+ return PATTERN_TRAILINGSPACE;
124
+ }
108
125
  static replaceMatch(match, source, content = '', options) {
109
126
  if (typeof content === 'number') {
110
127
  content = match[content] || '';
@@ -150,17 +167,14 @@ class XmlWriter {
150
167
  });
151
168
  }
152
169
  static escapeAttributes(source) {
153
- let match;
170
+ let escaped, match, attribute;
154
171
  while (match = REGEXP_TAGATTR.exec(source)) {
155
- let output = match[0], modified, subMatch;
156
- while (subMatch = REGEXP_ATTRVALUE.exec(match[1])) {
157
- const value = subMatch[2] || subMatch[3] || subMatch[4];
158
- if (value) {
159
- const escaped = this.escapeXmlString(value);
160
- if (escaped !== value) {
161
- output = output.replace(subMatch[0], subMatch[1] + `="${escaped}"`);
162
- modified = true;
163
- }
172
+ let output = match[0], modified;
173
+ while (attribute = REGEXP_ATTRVALUE.exec(match[1])) {
174
+ const value = attribute[2] || attribute[3] || attribute[4];
175
+ if (value && (escaped = this.escapeXmlString(value)) !== value) {
176
+ output = (0, util_1.spliceMatch)(output, attribute, attribute[1] + `="${escaped}"`, REGEXP_ATTRVALUE);
177
+ modified = true;
164
178
  }
165
179
  }
166
180
  if (modified) {
@@ -211,7 +225,7 @@ class XmlWriter {
211
225
  return i;
212
226
  }
213
227
  }
214
- if (start.length) {
228
+ if (start.length > 0) {
215
229
  for (const index of start.reverse()) {
216
230
  for (let j = index + 1; j < length; ++j) {
217
231
  if (source[j] === '>') {
@@ -307,14 +321,14 @@ class XmlWriter {
307
321
  const { ignoreCase, ignoreTagName, ignoreTagGroup } = options;
308
322
  if (ignoreTagName) {
309
323
  const flags = ignoreCase ? 'gi' : 'g';
310
- source = source.replace(CACHE_TAGOFFSET[_a = ignoreTagName + flags] || (CACHE_TAGOFFSET[_a] = new RegExp(`<(?:${ignoreTagName.split('|').map(value => escapeTagName(value)).join('|')})${XmlWriter.PATTERN_TAGOPEN}*>[\\S\\s]*?<\\/\\1\\s*>`, flags)), '');
324
+ source = source.replace(CACHE_TAGOFFSET[_a = ignoreTagName + flags] || (CACHE_TAGOFFSET[_a] = new RegExp(`<(?:${ignoreTagName.split('|').map(value => escapeTagName(value)).join('|')})${this.PATTERN_TAGOPEN}*>[\\S\\s]*?<\\/\\1\\s*>`, flags)), '');
311
325
  }
312
326
  if (ignoreTagGroup) {
313
327
  for (let i = 0, length = ignoreTagGroup.length; i < length; i += 2) {
314
328
  const start = ignoreTagGroup[i];
315
329
  const end = ignoreTagGroup[i + 1];
316
330
  if (end) {
317
- source = source.replace(CACHE_TAGOFFSET[_b = start + end] || (CACHE_TAGOFFSET[_b] = new RegExp(escapeTagName(start) + '[\\S\\s]*?' + escapeTagName(end), 'g')), '');
331
+ source = source.replace(CACHE_TAGOFFSET[_b = start + end] || (CACHE_TAGOFFSET[_b] = new RegExp(escapeTagName(start) + '.*?' + escapeTagName(end), 'gs')), '');
318
332
  }
319
333
  }
320
334
  }
@@ -375,7 +389,7 @@ class XmlWriter {
375
389
  }
376
390
  const result = [];
377
391
  const flags = ignoreCase ? 'gi' : 'g';
378
- let pattern = CACHE_TAGNAME[_a = tagPattern + flags] || (CACHE_TAGNAME[_a] = new RegExp(`<(?:(!--[\\S\\s]*?--)|(!\\[CDATA\\[[\\S\\s]*?\\]\\])` + (tagPattern ? '|' + `(${tagPattern})${PATTERN_TAGOPEN}*` : '') + ')>', flags)), match;
392
+ let pattern = CACHE_TAGNAME[_a = tagPattern + flags] || (CACHE_TAGNAME[_a] = new RegExp(`<(?:(!--[\\S\\s]*?--)|(!\\[CDATA\\[[\\S\\s]*?\\]\\])` + (tagPattern ? '|' + `(${tagPattern})${this.PATTERN_TAGOPEN}*` : '') + ')>', flags)), match;
379
393
  pattern.lastIndex = 0;
380
394
  while (match = pattern.exec(source)) {
381
395
  if (stripXml) {
@@ -400,7 +414,7 @@ class XmlWriter {
400
414
  const start = tagGroup[i];
401
415
  const end = tagGroup[i + 1];
402
416
  if (end) {
403
- pattern = CACHE_TAGNAME[_b = start + end] || (CACHE_TAGNAME[_b] = new RegExp(escapeTagName(start) + '[\\S\\s]*?' + escapeTagName(end), 'g'));
417
+ pattern = CACHE_TAGNAME[_b = start + end] || (CACHE_TAGNAME[_b] = new RegExp(escapeTagName(start) + '.*?' + escapeTagName(end), 'gs'));
404
418
  for (const tag of source.matchAll(pattern)) {
405
419
  const startIndex = tag.index;
406
420
  result.push({ type: 'block', outerXml: tag[0], startIndex, endIndex: startIndex + tag[0].length - 1 });
@@ -464,12 +478,14 @@ class XmlWriter {
464
478
  }
465
479
  deletePosition(item, rootName);
466
480
  }
467
- if (appending.length) {
481
+ if (appending.length > 0) {
468
482
  this.insertNodes(appending);
469
483
  }
470
484
  }
471
485
  ignoreFlag(...values) {
472
- values.forEach(value => this._ignoreFlag |= value);
486
+ values.forEach(value => {
487
+ this._ignoreFlag |= value;
488
+ });
473
489
  }
474
490
  getInvalidArea() {
475
491
  if ((this._ignoreFlag & IGNORE_FLAG.INTERIOR) === 0 && this._hasInvalidContent) {
@@ -479,7 +495,7 @@ class XmlWriter {
479
495
  }
480
496
  const result = XmlWriter.getCommentsAndCDATA(this.source, this.ignoreTagGroup ? [this.ignoreTagName || '', this.ignoreTagGroup] : this.ignoreTagName, this.ignoreCaseTagName);
481
497
  this._invalidContent = result;
482
- if (result.length) {
498
+ if (result.length > 0) {
483
499
  return result;
484
500
  }
485
501
  this._hasInvalidContent = false;
@@ -512,7 +528,9 @@ class XmlWriter {
512
528
  }
513
529
  return b.index - a.index;
514
530
  })
515
- .forEach(item => this.append(item));
531
+ .forEach(item => {
532
+ this.append(item);
533
+ });
516
534
  }
517
535
  fromNode(node, append) {
518
536
  const element = this.newElement(node);
@@ -786,7 +804,9 @@ class XmlWriter {
786
804
  const revised = this.decrement(node);
787
805
  if (revised.includes(node)) {
788
806
  if (tagName in this._tagCount) {
789
- revised.forEach(item => updateTagName(item, tagName));
807
+ revised.forEach(item => {
808
+ updateTagName(item, tagName);
809
+ });
790
810
  this.indexTag(tagName);
791
811
  this.increment(revised);
792
812
  }
@@ -827,10 +847,10 @@ class XmlWriter {
827
847
  return;
828
848
  }
829
849
  }
830
- if (revised.length) {
850
+ if (revised.length > 0) {
831
851
  const tagCount = this._tagCount[tagName];
832
852
  const nextCount = tagCount + offset + 1;
833
- if (elements.length) {
853
+ if (elements.length > 0) {
834
854
  if (documentIndex < minIndex) {
835
855
  for (const item of revised) {
836
856
  item.tagIndex = 0;
@@ -920,7 +940,9 @@ class XmlWriter {
920
940
  }
921
941
  resetPosition(startIndex) {
922
942
  const rootName = this.rootName;
923
- this.elements.forEach(item => deletePosition(item, rootName, startIndex));
943
+ this.elements.forEach(item => {
944
+ deletePosition(item, rootName, startIndex);
945
+ });
924
946
  }
925
947
  close() {
926
948
  const source = this.source;
@@ -934,7 +956,7 @@ class XmlWriter {
934
956
  ({ tagName, tagVoid } = options);
935
957
  }
936
958
  const source = this.source;
937
- 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);
959
+ const match = new RegExp(`<(${tagName && escapeTagName(tagName) || '[^\\s>]+'})${XmlWriter.PATTERN_TAGOPEN}+?${(0, types_1.escapePattern)(this.nameOfId)}="${(0, types_1.escapePattern)(id)}"`, ignoreCase ? 'i' : '').exec(source);
938
960
  if (match) {
939
961
  tagName || (tagName = match[1]);
940
962
  const startIndex = match.index;
@@ -961,7 +983,7 @@ class XmlWriter {
961
983
  const result = [];
962
984
  const patternId = XmlWriter.getPatternId(this.nameOfId);
963
985
  const flags = ignoreCase ? 'gi' : 'g';
964
- const pattern = CACHE_TAGNAME[_a = tagName + flags + '3'] || (CACHE_TAGNAME[_a] = new RegExp(`<${escapeTagName(tagName) + PATTERN_TAGOPEN}*>`, flags));
986
+ const pattern = CACHE_TAGNAME[_a = tagName + flags + '3'] || (CACHE_TAGNAME[_a] = new RegExp(`<${escapeTagName(tagName) + XmlWriter.PATTERN_TAGOPEN}*>`, flags));
965
987
  for (const match of source.matchAll(pattern)) {
966
988
  const startIndex = match.index;
967
989
  let outerXml = match[0], endIndex = startIndex + outerXml.length - 1;
@@ -1010,7 +1032,7 @@ class XmlWriter {
1010
1032
  this.resetPosition();
1011
1033
  }
1012
1034
  get newId() {
1013
- return (0, types_1.generateUUID)();
1035
+ return crypto.randomUUID();
1014
1036
  }
1015
1037
  get nameOfId() {
1016
1038
  return XmlWriter.getNameOfId(this.documentName);
@@ -1043,7 +1065,7 @@ class XmlElement {
1043
1065
  if (value === null) {
1044
1066
  continue;
1045
1067
  }
1046
- result += '="' + (typeof value === 'string' ? escapeEntities ? XmlWriter.escapeXmlString(value) : value.replace(/"/g, '&quot;') : module_1.asString(value)) + '"';
1068
+ result += '="' + (typeof value === 'string' ? escapeEntities ? XmlWriter.escapeXmlString(value) : value.replaceAll('"', '&quot;') : module_1.asString(value)) + '"';
1047
1069
  }
1048
1070
  return result;
1049
1071
  }
@@ -1077,19 +1099,18 @@ class XmlElement {
1077
1099
  if (!node.append) {
1078
1100
  this._modified = attrs.size > 0;
1079
1101
  if (node.outerXml) {
1080
- const [tagStart, innerXml, isVoid] = this.parseOuterXml(node.outerXml, tagVoid);
1081
- let source = tagStart;
1082
- for (const match of tagStart.matchAll(REGEXP_ATTRVALUE)) {
1083
- const attr = ignoreCase ? match[1].toLowerCase() : match[1];
1084
- if (!attrs.has(attr)) {
1085
- attrs.set(attr, match[2] || match[3] || match[4] || '');
1102
+ let [source, innerXml, isVoid] = this.parseOuterXml(node.outerXml, tagVoid), match;
1103
+ while (match = REGEXP_ATTRVALUE.exec(source)) {
1104
+ const key = ignoreCase ? match[1].toLowerCase() : match[1];
1105
+ if (!attrs.has(key)) {
1106
+ attrs.set(key, match[2] || match[3] || match[4] || '');
1086
1107
  }
1087
- source = source.replace(match[0], '');
1108
+ source = (0, util_1.spliceMatch)(source, match, '', REGEXP_ATTRVALUE);
1088
1109
  }
1089
- for (const match of source.matchAll(REGEXP_ATTRNAME)) {
1090
- const attr = ignoreCase ? match[1].toLowerCase() : match[1];
1091
- if (!attrs.has(attr)) {
1092
- attrs.set(attr, null);
1110
+ for (const attr of source.matchAll(REGEXP_ATTRNAME)) {
1111
+ const key = ignoreCase ? attr[1].toLowerCase() : attr[1];
1112
+ if (!attrs.has(key)) {
1113
+ attrs.set(key, null);
1093
1114
  }
1094
1115
  }
1095
1116
  if (!dynamic) {
@@ -1256,8 +1277,6 @@ class XmlElement {
1256
1277
  const { tagName, tagIndex = -1, tagCount = Infinity, ignoreCase } = node;
1257
1278
  const id = this.id;
1258
1279
  const flags = ignoreCase ? 'gi' : 'g';
1259
- const hasId = (startIndex, endIndex) => source.substring(startIndex, endIndex).indexOf(id) !== -1;
1260
- const errorResult = () => ['', '', (0, types_1.errorValue)('Element was not found', tagName.toUpperCase() + (isIndex(tagIndex) ? ' @ ' + tagIndex : ''))];
1261
1280
  let position;
1262
1281
  if (XmlWriter.isTagVoid(this.documentType, tagName)) {
1263
1282
  const openTag = [];
@@ -1271,7 +1290,7 @@ class XmlElement {
1271
1290
  break;
1272
1291
  }
1273
1292
  if (!invalid || isValidIndex(invalid, startIndex, endIndex)) {
1274
- if (id && hasId(startIndex, endIndex)) {
1293
+ if (id && hasId(id, source, startIndex, endIndex)) {
1275
1294
  return getResult(startIndex, endIndex);
1276
1295
  }
1277
1296
  openCount = openTag.push([startIndex, endIndex]);
@@ -1318,7 +1337,7 @@ class XmlElement {
1318
1337
  ++tag[2];
1319
1338
  }
1320
1339
  }
1321
- openCount = openTag.push([startIndex, -1, 1, id && !found && hasId(startIndex, endIndex) ? (found = true) && 1 : 0]);
1340
+ openCount = openTag.push([startIndex, -1, 1, id && !found && hasId(id, source, startIndex, endIndex) ? (found = true) && 1 : 0]);
1322
1341
  }
1323
1342
  pattern.lastIndex = endIndex;
1324
1343
  }
@@ -1338,7 +1357,7 @@ class XmlElement {
1338
1357
  }
1339
1358
  return this.replace(source, position);
1340
1359
  }
1341
- return errorResult();
1360
+ return ['', '', (0, types_1.errorValue)('Element was not found', tagName.toUpperCase() + (isIndex(tagIndex) ? ' @ ' + tagIndex : ''))];
1342
1361
  }
1343
1362
  save(source, invalid) {
1344
1363
  const [output, outerXml, error] = this.write(source, invalid || XmlWriter.getCommentsAndCDATA(source));
@@ -1422,7 +1441,7 @@ class XmlElement {
1422
1441
  return this._innerXml;
1423
1442
  }
1424
1443
  set tagOffset(value) {
1425
- this._tagOffset = value && Object.keys(value).length ? value : undefined;
1444
+ this._tagOffset = value && Object.keys(value).length > 0 ? value : undefined;
1426
1445
  }
1427
1446
  get tagOffset() {
1428
1447
  return this._tagOffset;
@@ -150,8 +150,8 @@ export interface XmlWriterConstructor {
150
150
  readonly PATTERN_QUOTEVALUE: string;
151
151
  readonly PATTERN_COMMENT: string;
152
152
  readonly PATTERN_TRAILINGSPACE: string;
153
- replaceMatch(match: RegExpMatchArray, source: string, content: NumString, options: ReplaceMatchOptions): string;
154
- replaceMatch(match: RegExpMatchArray, source: string, content?: NumString, pattern?: RegExp): string;
153
+ replaceMatch(match: RegExpMatchArray, source: string, content: number | string, options: ReplaceMatchOptions): string;
154
+ replaceMatch(match: RegExpMatchArray, source: string, content?: number | string, pattern?: RegExp): string;
155
155
  escapeXmlString(value: string, ampersand?: boolean): string;
156
156
  escapeAttributes(source: string): string;
157
157
  getNewlineString(leading: string, trailing: string, newline?: string): string;
@@ -1,8 +1,8 @@
1
- import type { SourceMapConstructor, TransformSeriesConstructor } from '../../types/lib/document';
2
-
3
- declare namespace transform {
4
- const TransformSeries: TransformSeriesConstructor;
5
- const SourceMap: SourceMapConstructor;
6
- }
7
-
1
+ import type { SourceMapConstructor, TransformSeriesConstructor } from '../../types/lib/document';
2
+
3
+ declare namespace transform {
4
+ const TransformSeries: TransformSeriesConstructor;
5
+ const SourceMap: SourceMapConstructor;
6
+ }
7
+
8
8
  export = transform;
@@ -2,8 +2,8 @@
2
2
  var _a, _b, _c, _d;
3
3
  exports.SourceMap = exports.TransformSeries = void 0;
4
4
  const path = require("path");
5
- const types_1 = require("@e-mc/types");
6
5
  const core_1 = require("@e-mc/core");
6
+ const types_1 = require("@e-mc/types");
7
7
  const kCode = Symbol('code');
8
8
  const kMap = Symbol('map');
9
9
  const kOut = Symbol('out');
@@ -115,25 +115,49 @@ class TransformSeries extends core_1.Module {
115
115
  }
116
116
  return baseConfig;
117
117
  }
118
- upgrade(context, dirname, name) {
118
+ upgrade(context, dirname, pkgname) {
119
119
  if (this.version === 'latest') {
120
- if (dirname && path.isAbsolute(dirname)) {
121
- const pathname = path.join(dirname, 'node_modules', name || this.packageName);
122
- if (core_1.Module.isPath(pathname)) {
123
- name = pathname;
124
- }
125
- else {
126
- dirname = '';
120
+ try {
121
+ const pathname = dirname && this.findModule(dirname, pkgname) || pkgname;
122
+ if (pathname) {
123
+ return require(pathname);
127
124
  }
128
125
  }
126
+ catch {
127
+ }
128
+ }
129
+ return context;
130
+ }
131
+ async upgradeESM(context, dirname, pkgname) {
132
+ if (this.version === 'latest') {
129
133
  try {
130
- return require(name || dirname || this.packageName);
134
+ const pathname = dirname && this.findModule(dirname, pkgname);
135
+ let result;
136
+ if (pathname) {
137
+ result = await (0, types_1.importESM)(pathname, false, true);
138
+ }
139
+ else if (pkgname) {
140
+ result = await (0, types_1.importESM)(pkgname);
141
+ }
142
+ if (result) {
143
+ return result;
144
+ }
131
145
  }
132
146
  catch {
133
147
  }
148
+ return this.upgrade(context, dirname, pkgname);
134
149
  }
135
150
  return context;
136
151
  }
152
+ findModule(dirname, pkgname) {
153
+ if (path.isAbsolute(dirname)) {
154
+ const pathname = path.join(dirname, 'node_modules', pkgname || this.packageName);
155
+ if (core_1.Module.isDir(pathname)) {
156
+ return pathname;
157
+ }
158
+ }
159
+ return '';
160
+ }
137
161
  close(instance) {
138
162
  core_1.AbortComponent.detach(instance, this.signal);
139
163
  this.host?.release(this);
@@ -187,14 +211,16 @@ class TransformSeries extends core_1.Module {
187
211
  this.metadata.__version__ = value;
188
212
  }
189
213
  get version() {
190
- return this.metadata.__version__ || '';
214
+ const result = this.metadata.__version__;
215
+ return typeof result === 'string' ? result : '';
191
216
  }
192
217
  set packageName(value) {
193
218
  this.metadata.__packagename__ = value;
194
219
  this.version = 'latest';
195
220
  }
196
221
  get packageName() {
197
- return this.metadata.__packagename__ || '';
222
+ const result = this.metadata.__packagename__;
223
+ return typeof result === 'string' ? result : '';
198
224
  }
199
225
  get packageVersion() {
200
226
  const packageName = this.packageName;
@@ -313,4 +339,4 @@ class SourceMap {
313
339
  }
314
340
  exports.SourceMap = SourceMap;
315
341
  _d = kMap;
316
- SourceMap.RE_SOURCE_MAPPING_URL = /(?:\r\n|\n)*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\r?\n?/g;
342
+ SourceMap.RE_SOURCE_MAPPING_URL = /[\r\n]*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\r?\n?/g;
package/util.d.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  declare namespace util {
2
+ /** @deprecated Types.IMPORT_MAP */
2
3
  const IMPORTS: Record<string, string | undefined>;
4
+ const PATTERN_PARENTHESIS: string;
3
5
  function loadPlugins<T = unknown>(name: string | unknown[], plugins?: unknown[]): T[];
4
6
  function replaceAll(source: string, valueOf: (name: string) => string, opening?: string, closing?: string): string;
5
7
  function concatString(values: string[] | string | undefined, newline?: string): string;
8
+ function spliceString(source: string, startIndex: number, endIndex: number, content: string, pattern?: RegExp): string;
9
+ function spliceMatch(source: string, match: RegExpExecArray, content: string, pattern?: RegExp): string;
6
10
  function splitEnclosing(value: string, pattern?: string | RegExp, options?: { trim?: boolean; start?: number; startWith?: number; count?: number } | boolean | number, opening?: string, closing?: string): string[];
7
11
  function appendSuffix(filename: string, value: string, separator?: string): string;
8
12
  function getIndent(value: string, spaces?: number): string;
package/util.js CHANGED
@@ -1,26 +1,22 @@
1
1
  "use strict";
2
- exports.isObject = exports.removeInternalProperties = exports.getModuleName = exports.hasValue = exports.getHashData = exports.getNewline = exports.getIndent = exports.appendSuffix = exports.splitEnclosing = exports.concatString = exports.replaceAll = exports.loadPlugins = exports.IMPORTS = void 0;
2
+ exports.isObject = exports.IMPORTS = exports.PATTERN_PARENTHESIS = void 0;
3
+ exports.loadPlugins = loadPlugins;
4
+ exports.replaceAll = replaceAll;
5
+ exports.concatString = concatString;
6
+ exports.spliceString = spliceString;
7
+ exports.spliceMatch = spliceMatch;
8
+ exports.splitEnclosing = splitEnclosing;
9
+ exports.appendSuffix = appendSuffix;
10
+ exports.getIndent = getIndent;
11
+ exports.getNewline = getNewline;
12
+ exports.getHashData = getHashData;
13
+ exports.hasValue = hasValue;
14
+ exports.getModuleName = getModuleName;
15
+ exports.removeInternalProperties = removeInternalProperties;
3
16
  const path = require("path");
4
17
  const types_1 = require("@e-mc/types");
18
+ Object.defineProperty(exports, "IMPORTS", { enumerable: true, get: function () { return types_1.IMPORT_MAP; } });
5
19
  Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return types_1.isObject; } });
6
- exports.IMPORTS = {
7
- "@babel/core": "@pi-r/babel",
8
- "clean-css": "@pi-r/clean-css",
9
- "csso": "@pi-r/csso",
10
- "eslint": "@pi-r/eslint",
11
- "html-minifier": "@pi-r/html-minifier",
12
- "html-minifier-terser": "@pi-r/html-minifier-terser",
13
- "html-validate": "@pi-r/html-validate",
14
- "postcss": "@pi-r/postcss",
15
- "posthtml": "@pi-r/posthtml",
16
- "prettier": "@pi-r/prettier",
17
- "rollup": "@pi-r/rollup",
18
- "sass": "@pi-r/sass",
19
- "stylelint": "@pi-r/stylelint",
20
- "svgo": "@pi-r/svgo",
21
- "terser": "@pi-r/terser",
22
- "uglify-js": "@pi-r/uglify-js"
23
- };
24
20
  function loadPlugins(name, plugins = []) {
25
21
  if (Array.isArray(name)) {
26
22
  plugins = name;
@@ -34,7 +30,6 @@ function loadPlugins(name, plugins = []) {
34
30
  }
35
31
  return result;
36
32
  }
37
- exports.loadPlugins = loadPlugins;
38
33
  function replaceAll(source, valueOf, opening = '{{', closing = '}}') {
39
34
  return source.split(opening).reduce((a, b, i) => {
40
35
  if (i > 0) {
@@ -46,7 +41,6 @@ function replaceAll(source, valueOf, opening = '{{', closing = '}}') {
46
41
  return a + b;
47
42
  }, '');
48
43
  }
49
- exports.replaceAll = replaceAll;
50
44
  function concatString(values, newline) {
51
45
  if ((0, types_1.isArray)(values)) {
52
46
  newline || (newline = values.find(item => item.indexOf('\r') !== -1) ? '\r\n' : '\n');
@@ -54,7 +48,16 @@ function concatString(values, newline) {
54
48
  }
55
49
  return typeof values === 'string' ? values : '';
56
50
  }
57
- exports.concatString = concatString;
51
+ function spliceString(source, startIndex, endIndex, content, pattern) {
52
+ if (pattern) {
53
+ pattern.lastIndex = startIndex + content.length;
54
+ }
55
+ return source.substring(0, startIndex) + content + source.substring(endIndex);
56
+ }
57
+ function spliceMatch(source, match, content, pattern) {
58
+ const index = match.index;
59
+ return spliceString(source, index, index + match[0].length, content, pattern);
60
+ }
58
61
  function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
59
62
  pattern || (pattern = opening);
60
63
  let position = 0, index = -1, end = 0, char;
@@ -154,12 +157,10 @@ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
154
157
  }
155
158
  return result;
156
159
  }
157
- exports.splitEnclosing = splitEnclosing;
158
160
  function appendSuffix(filename, value, separator = '-') {
159
161
  const ext = path.extname(filename);
160
162
  return ext && value ? filename.substring(0, filename.length - ext.length) + separator + value + ext : filename;
161
163
  }
162
- exports.appendSuffix = appendSuffix;
163
164
  function getIndent(value, spaces = 4) {
164
165
  const match = /^(\t|[ ]{2,}[^\s])/m.exec(value);
165
166
  if (match) {
@@ -170,17 +171,14 @@ function getIndent(value, spaces = 4) {
170
171
  }
171
172
  return ' '.repeat(spaces);
172
173
  }
173
- exports.getIndent = getIndent;
174
174
  function getNewline(value) {
175
175
  const index = value.indexOf('\n');
176
176
  return index > 0 && value[index - 1] === '\r' ? '\r\n' : '\n';
177
177
  }
178
- exports.getNewline = getNewline;
179
178
  function getHashData(value) {
180
- const match = /^(md5|sha1|sha224|sha384|sha256|sha512)(?:\[(\d+)\])?$/.exec(value.trim().toLowerCase());
179
+ const match = /^(md5|sha1|sha224|sha384|sha256|sha512|ripemd(?:-160)?)(?:\[(\d+)\])?$/.exec(value.trim().toLowerCase());
181
180
  return match ? [match[1], match[2] ? Math.max(4, +match[2]) : 0] : ['', 0];
182
181
  }
183
- exports.getHashData = getHashData;
184
182
  function hasValue(target, ...values) {
185
183
  if (target) {
186
184
  if (typeof target === 'string') {
@@ -192,14 +190,12 @@ function hasValue(target, ...values) {
192
190
  }
193
191
  return false;
194
192
  }
195
- exports.hasValue = hasValue;
196
193
  function getModuleName(err) {
197
194
  const match = err instanceof Error && /Cannot find module '([^']+)'/.exec(err.message);
198
195
  if (match) {
199
196
  return match[1];
200
197
  }
201
198
  }
202
- exports.getModuleName = getModuleName;
203
199
  function removeInternalProperties(value, retaining) {
204
200
  if ((0, types_1.isObject)(value)) {
205
201
  for (const prop in value) {
@@ -217,4 +213,4 @@ function removeInternalProperties(value, retaining) {
217
213
  }
218
214
  return value;
219
215
  }
220
- exports.removeInternalProperties = removeInternalProperties;
216
+ exports.PATTERN_PARENTHESIS = '(?:[^()]+(?=\\))|[^()]|\\([^()]*\\(+|\\([^()]*\\)|(?:\\)[^()]+)?\\)*?)+';