@e-mc/document 0.4.2 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/document",
3
- "version": "0.4.2",
3
+ "version": "0.5.1",
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.4.2",
24
- "@e-mc/db": "0.4.2",
25
- "@e-mc/types": "0.4.2",
23
+ "@e-mc/core": "0.5.1",
24
+ "@e-mc/db": "0.5.1",
25
+ "@e-mc/types": "0.5.1",
26
26
  "chalk": "4.1.2",
27
27
  "htmlparser2": "^8.0.2",
28
28
  "js-yaml": "^4.1.0",
package/parse/dom.js CHANGED
@@ -210,23 +210,20 @@ class DomWriter extends index_1.XmlWriter {
210
210
  }
211
211
  close() {
212
212
  if (this.documentElement || this._appendCount > 0) {
213
- this.source = this.source.replace(new RegExp(this.patternId, 'g'), '');
213
+ this.source = this.source.replace(new RegExp(index_1.XmlWriter.getPatternId(this.nameOfId), 'g'), '');
214
214
  }
215
215
  return super.close();
216
216
  }
217
217
  newElement(node) {
218
218
  return new HtmlElement(this.documentName, node);
219
219
  }
220
- get nameOfId() {
221
- return index_1.XmlWriter.getNameOfId(this.documentName);
222
- }
223
220
  }
224
221
  exports.DomWriter = DomWriter;
225
222
  class HtmlElement extends index_1.XmlElement {
226
223
  constructor(documentName, node, attributes, options = {}) {
227
224
  options.parser || (options.parser = { ...PARSER_OPTIONS });
228
225
  super(documentName, node, attributes, { ...options, tagVoid: TAG_VOID.includes(node.tagName) });
229
- this.TAG_VOID = TAG_VOID;
226
+ this._documentType = "HTML" /* VALUES.DOCUMENT_TYPE */;
230
227
  }
231
228
  getTagOffset(source) {
232
229
  switch (this.node.append?.tagName || this.tagName) {
@@ -249,8 +246,6 @@ class HtmlElement extends index_1.XmlElement {
249
246
  const [tagName, items, innerXml] = this.getOuterContent();
250
247
  return '<' + tagName + HtmlElement.writeAttributes(items) + '>' + (DomWriter.hasInnerXml(tagName) && tagName !== "html" /* VALUES.ROOT */ ? (tagName === 'title' ? index_1.XmlWriter.escapeXmlString(innerXml) : innerXml) + `</${tagName}>` : '');
251
248
  }
252
- get nameOfId() {
253
- return index_1.XmlWriter.getNameOfId(this.documentName);
254
- }
255
249
  }
256
250
  exports.HtmlElement = HtmlElement;
251
+ index_1.XmlWriter.namesOfTagVoid("HTML" /* VALUES.DOCUMENT_TYPE */, TAG_VOID);
package/parse/index.js CHANGED
@@ -15,8 +15,9 @@ var IGNORE_FLAG;
15
15
  const Parser = htmlparser2.Parser;
16
16
  const DomHandler = domhandler.DomHandler;
17
17
  const CACHE_TAGNAME = {};
18
- const CACHE_NAMEOFID = {};
19
18
  const CACHE_TAGOFFSET = {};
19
+ const CACHE_TAGVOID = {};
20
+ const CACHE_PATTERNID = {};
20
21
  const PATTERN_ATTRNAME = '([^\\s=>]+)';
21
22
  const PATTERN_ATTRVALUE = `=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]*))`;
22
23
  const PATTERN_TAGNAME = '[A-Za-z][\\w-]*';
@@ -96,7 +97,6 @@ function getParserDOM(source, parser) {
96
97
  }
97
98
  const isIndex = (value) => typeof value === 'number' && value >= 0 && value !== Infinity;
98
99
  const isCount = (value) => typeof value === 'number' && value > 0 && value !== Infinity;
99
- const getPatternId = (name) => CACHE_NAMEOFID[name] || (CACHE_NAMEOFID[name] = new RegExp(`\\s${(0, types_1.escapePattern)(name)}="([^"]+)"`));
100
100
  const escapeTagName = (value) => value.replace(REGEXP_ESCAPECHAR, capture => capture === '-' ? '\\x2d' : '\\' + capture);
101
101
  class XmlWriter {
102
102
  static get PATTERN_ATTRNAME() { return PATTERN_ATTRNAME; }
@@ -342,12 +342,21 @@ class XmlWriter {
342
342
  }
343
343
  return result;
344
344
  }
345
+ static namesOfTagVoid(type, values) {
346
+ CACHE_TAGVOID[type] = values;
347
+ }
348
+ static isTagVoid(type, tagName) {
349
+ return !!CACHE_TAGVOID[type]?.includes(tagName);
350
+ }
345
351
  static getNodeId(node, document) {
346
352
  return node.id?.[document] || '';
347
353
  }
348
354
  static getNameOfId(document) {
349
355
  return `data-${document}-id`;
350
356
  }
357
+ static getPatternId(name) {
358
+ return CACHE_PATTERNID[name] || (CACHE_PATTERNID[name] = new RegExp(`\\s${(0, types_1.escapePattern)(name)}="([^"]+)"`));
359
+ }
351
360
  static getCommentsAndCDATA(source, tagPattern = '', ignoreCase, stripXml) {
352
361
  var _a, _b;
353
362
  let tagGroup;
@@ -438,7 +447,6 @@ class XmlWriter {
438
447
  this._ignoreFlag = 0;
439
448
  this._appendCount = 0;
440
449
  this._invalidContent = null;
441
- this._patternId = null;
442
450
  this._patternIgnore = null;
443
451
  this._writeStartIndex = -1;
444
452
  this._source = source;
@@ -953,7 +961,7 @@ class XmlWriter {
953
961
  const source = this.source;
954
962
  const invalid = this.getInvalidArea();
955
963
  const result = [];
956
- const patternId = this.patternId;
964
+ const patternId = XmlWriter.getPatternId(this.nameOfId);
957
965
  const flags = ignoreCase ? 'gi' : 'g';
958
966
  const pattern = CACHE_TAGNAME[_a = tagName + flags + '3'] || (CACHE_TAGNAME[_a] = new RegExp(`<${escapeTagName(tagName) + PATTERN_TAGOPEN}*>`, flags));
959
967
  pattern.lastIndex = 0;
@@ -1008,8 +1016,8 @@ class XmlWriter {
1008
1016
  get newId() {
1009
1017
  return (0, types_1.generateUUID)();
1010
1018
  }
1011
- get patternId() {
1012
- return this._patternId || (this._patternId = getPatternId(this.nameOfId));
1019
+ get nameOfId() {
1020
+ return XmlWriter.getNameOfId(this.documentName);
1013
1021
  }
1014
1022
  get patternIgnore() {
1015
1023
  return this.ignoreTagName ? this._patternIgnore || (this._patternIgnore = new RegExp(`^(?:${this.ignoreTagName})$`, this.ignoreCaseTagName ? 'i' : '')) : null;
@@ -1043,23 +1051,23 @@ class XmlElement {
1043
1051
  }
1044
1052
  return result;
1045
1053
  }
1046
- constructor(documentName, node, attributes, tagVoid = false, parser) {
1054
+ constructor(documentName, node, attributes, tagVoid, parser) {
1047
1055
  this.documentName = documentName;
1048
1056
  this.node = node;
1049
1057
  this.newline = '\n';
1050
- this.TAG_VOID = [];
1051
1058
  this._modified = true;
1052
1059
  this._tagName = '';
1053
1060
  this._innerXml = '';
1054
1061
  this._remove = false;
1055
- this._tagVoid = false;
1062
+ this._tagVoid = null;
1056
1063
  this._prevTagName = null;
1057
1064
  this._prevInnerXml = null;
1058
1065
  this._append = undefined;
1059
1066
  this._tagOffset = undefined;
1067
+ this._documentType = '';
1060
1068
  this._attributes = new Map();
1061
1069
  if ((0, types_1.isObject)(tagVoid)) {
1062
- ({ tagVoid = false, parser } = tagVoid);
1070
+ ({ tagVoid, parser } = tagVoid);
1063
1071
  }
1064
1072
  if (parser) {
1065
1073
  this.parser = parser;
@@ -1069,6 +1077,7 @@ class XmlElement {
1069
1077
  applyAttributes(attrs, node.attributes, ignoreCase);
1070
1078
  applyAttributes(attrs, attributes, ignoreCase);
1071
1079
  this._ignoreCase = ignoreCase;
1080
+ this._nameOfId = XmlWriter.getNameOfId(documentName);
1072
1081
  if (!node.append) {
1073
1082
  this._modified = attrs.size > 0;
1074
1083
  if (node.outerXml) {
@@ -1126,7 +1135,7 @@ class XmlElement {
1126
1135
  return [tagStart || `<${this.tagName}>`, innerXml || '', false];
1127
1136
  }
1128
1137
  getTagOffset(replacement, options = {}) {
1129
- if (!this.tagVoid || this._prevTagName && !this.TAG_VOID.includes(this._prevTagName)) {
1138
+ if (!this.tagVoid || this._prevTagName && !XmlWriter.isTagVoid(this.documentType, this._prevTagName)) {
1130
1139
  options.parser || (options.parser = this.parser);
1131
1140
  if (!this.node.append) {
1132
1141
  return XmlWriter.getTagOffset(this.innerXml, replacement, options);
@@ -1155,7 +1164,7 @@ class XmlElement {
1155
1164
  if (outerXml) {
1156
1165
  const index = XmlWriter.findCloseTag(outerXml);
1157
1166
  if (index !== -1) {
1158
- return this.patternId.exec(outerXml.substring(0, index))?.[1] || '';
1167
+ return XmlWriter.getPatternId(this.nameOfId).exec(outerXml.substring(0, index))?.[1] || '';
1159
1168
  }
1160
1169
  }
1161
1170
  }
@@ -1254,7 +1263,7 @@ class XmlElement {
1254
1263
  const hasId = (startIndex, endIndex) => source.substring(startIndex, endIndex).indexOf(id) !== -1;
1255
1264
  const errorResult = () => ['', '', (0, types_1.errorValue)('Element was not found', tagName.toUpperCase() + (isIndex(tagIndex) ? ' @ ' + tagIndex : ''))];
1256
1265
  let position;
1257
- if (this.TAG_VOID.includes(tagName)) {
1266
+ if (XmlWriter.isTagVoid(this.documentType, tagName)) {
1258
1267
  const openTag = [];
1259
1268
  const pattern = CACHE_TAGNAME[_a = tagName + flags + '1'] || (CACHE_TAGNAME[_a] = new RegExp(`<${escapeTagName(tagName)}[\\s|>]`, flags));
1260
1269
  let openCount = 0, match;
@@ -1386,7 +1395,10 @@ class XmlElement {
1386
1395
  if (value !== tagName) {
1387
1396
  this._prevTagName || (this._prevTagName = tagName);
1388
1397
  this._tagName = value;
1389
- if (this._tagVoid = this.TAG_VOID.includes(value)) {
1398
+ if (!CACHE_TAGVOID[this.documentType]) {
1399
+ this._tagVoid = null;
1400
+ }
1401
+ else if (this._tagVoid = XmlWriter.isTagVoid(this.documentType, value)) {
1390
1402
  this.innerXml = '';
1391
1403
  }
1392
1404
  this._modified = true;
@@ -1396,8 +1408,11 @@ class XmlElement {
1396
1408
  const tagName = this._tagName || this.node.tagName;
1397
1409
  return this.node.ignoreCase ? tagName.toLowerCase() : tagName;
1398
1410
  }
1411
+ get documentType() {
1412
+ return this._documentType || this.documentName;
1413
+ }
1399
1414
  get tagVoid() {
1400
- return this._tagVoid || this.TAG_VOID.includes(this.tagName);
1415
+ return this._tagVoid ?? XmlWriter.isTagVoid(this.documentType, this.tagName);
1401
1416
  }
1402
1417
  set innerXml(value) {
1403
1418
  if (!this.tagVoid && value !== this._innerXml) {
@@ -1442,8 +1457,8 @@ class XmlElement {
1442
1457
  get append() {
1443
1458
  return this._append;
1444
1459
  }
1445
- get patternId() {
1446
- return this._patternId || (this._patternId = getPatternId(this.nameOfId));
1460
+ get nameOfId() {
1461
+ return this._nameOfId;
1447
1462
  }
1448
1463
  get modified() {
1449
1464
  return this._modified;
@@ -100,7 +100,6 @@ export class IXmlBase {
100
100
  save(...args: unknown[]): unknown;
101
101
  reset(): void;
102
102
  get nameOfId(): string;
103
- get patternId(): RegExp;
104
103
  get modified(): boolean;
105
104
  }
106
105
 
@@ -162,8 +161,11 @@ export interface XmlWriterConstructor {
162
161
  findElement(source: string, node: XmlTagNode, options?: FindElementOptions): ParserResult;
163
162
  getTagOffset(source: string, options?: TagOffsetOptions): TagOffsetMap;
164
163
  getTagOffset(source: string, replacement?: string, options?: TagOffsetOptions): TagOffsetMap;
164
+ namesOfTagVoid(type: string, values: string[]): void;
165
+ isTagVoid(type: string, tagName: string): boolean;
165
166
  getNodeId(node: XmlTagNode, document: string): string;
166
167
  getNameOfId(document: string): string;
168
+ getPatternId(name: string): RegExp;
167
169
  getCommentsAndCDATA(source: string, tagPattern: unknown, ignoreCase: boolean, stripXml: true): string;
168
170
  getCommentsAndCDATA(source: string, tagPattern?: StringOfArray | [Undef<string>, Undef<string[]>], ignoreCase?: boolean, stripXml?: boolean): SourceContent[];
169
171
  isEqual(node: XmlTagNode, other: XmlTagNode, ignoreCase: boolean): boolean;
@@ -176,7 +178,6 @@ export interface XmlWriterConstructor {
176
178
  }
177
179
 
178
180
  export class IXmlElement extends IXmlBase {
179
- TAG_VOID: string[];
180
181
  parser?: ParserOptions;
181
182
  readonly node: XmlTagNode;
182
183
  setAppend(value?: TagAppend): void;
@@ -198,6 +199,7 @@ export class IXmlElement extends IXmlBase {
198
199
  get id(): string;
199
200
  set tagName(value: string);
200
201
  get tagName(): string;
202
+ get documentType(): string;
201
203
  get tagVoid(): boolean;
202
204
  set innerXml(value: string);
203
205
  get innerXml(): string;