@pi-r/chrome 0.8.0 → 0.8.2

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/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { IFileManager } from '@e-mc/types/lib';
2
-
3
- import type { ChromeDocumentConstructor, DocumentAsset } from './types';
4
-
5
- declare const Chrome: ChromeDocumentConstructor<IFileManager<DocumentAsset>>;
6
-
1
+ import type { IFileManager } from '@e-mc/types/lib';
2
+
3
+ import type { ChromeDocumentConstructor, DocumentAsset } from './types';
4
+
5
+ declare const Chrome: ChromeDocumentConstructor<IFileManager<DocumentAsset>>;
6
+
7
7
  export = Chrome;
package/index.js CHANGED
@@ -19,7 +19,6 @@ const CACHE_CONTENT = new Map();
19
19
  const CACHE_FORMAT = new Map();
20
20
  const CACHE_DATASET = {};
21
21
  const CACHE_ATRULES = {};
22
- let CACHE_TOTAL = 0;
23
22
  const [PATTERN_STRING_SOME, PATTERN_STRING_MANY] = (function () {
24
23
  const grouping = '(?:\\s|' + dom_1.DomWriter.PATTERN_COMMENT + '|`[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`)';
25
24
  return [grouping + '*', grouping + '+'];
@@ -36,7 +35,7 @@ const REGEXP_SRCSETSIZE = /~\s*([\d.]+)\s*([wx])/i;
36
35
  const REGEXP_TEMPLATECONDITIONAL = /(\n\s+)?\{\{if\s+((?:[^}]|}(?!}))+?)\}\}(\s*)(.*?)(?:\s*\{\{else(?:\s+if\s+((?:[^}]|}(?!}))+?))?\}\}(\s*)(.*?)\s*)?\s*\{\{end\}\}/gs;
37
36
  const REGEXP_TEMPLATECOMPARISON = /\s*\(\s*(?:\b(eq|ne|lt|le|gt|ge)\s+)?("[^"]+"|'[^']+'|[^\s)]+)(?:\s+("[^"]+"|'[^']+'|[^\s)]+))?\s*\)/g;
38
37
  const REGEXP_TEMPLATEMATCH = new RegExp(`^(?:\\b(and|or|not)\\s+)?((?:${REGEXP_TEMPLATECOMPARISON.source})+)|(!|not\\s+)?([^\\s][\\S\\s]*)$`);
39
- const REGEXP_IMPORTMODULE = /(?:(?:^|[\n;]+)\s*import\s+(?:("(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+')|.+?from\s+("(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+')(?:[ \t]+assert[ \t]+\{((?:"(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+'|`(?:[^`]|(?<=\\)`)+`|{[^{]*{|{[^}]*}|}[^}]*}|[^"'`]+)+)\})?)|\bimport\(\s*("(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+')\s*(?:,\s*\{((?:"(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+'|`(?:[^`]|(?<=\\)`)+`|{[^{]*{|{[^}]*}|}[^}]*}|[^"'`]+)+)\}\s*)?\))/gd;
38
+ const REGEXP_IMPORTMODULE = /(?:(?:^|[\n;]+)\s*import\s+(?:("(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+')|.+?from\s+("(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+')(?:[ \t]+(?:assert|with)[ \t]+\{((?:"(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+'|`(?:[^`]|(?<=\\)`)+`|{[^{]*{|{[^}]*}|}[^}]*}|[^"'`]+)+)\})?)|\bimport\(\s*("(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+')\s*(?:,\s*\{((?:"(?:[^"]|(?<=\\)")+"|'(?:[^']|(?<=\\)')+'|`(?:[^`]|(?<=\\)`)+`|{[^{]*{|{[^}]*}|}[^}]*}|[^"'`]+)+)\}\s*)?\))/gd;
40
39
  const REGEXP_BLOCKEXCLUDE = new RegExp(`(\\s*)<!--\\s*exclude\\s*:\\s*(?:start|end)\\s*-->` + dom_1.DomWriter.PATTERN_TRAILINGSPACE, 'gi');
41
40
  const REGEXP_COMMENT = new RegExp(dom_1.DomWriter.PATTERN_COMMENT, 'g');
42
41
  const REGEXP_QUOTEDVALUE = new RegExp(`\\b(?:@import${PATTERN_STRING_MANY}|content${PATTERN_STRING_SOME}:${PATTERN_STRING_SOME})(?:"[^"]*"|'[^']*')`, 'gi');
@@ -98,7 +97,7 @@ function getObjectValue(value, key) {
98
97
  }
99
98
  }
100
99
  }
101
- if (value !== undefined && value !== null) {
100
+ if (value != null) {
102
101
  found = true;
103
102
  continue;
104
103
  }
@@ -164,9 +163,6 @@ function getCacheItem(map, key) {
164
163
  }
165
164
  }
166
165
  function addCacheItem(map, key, data, timeout) {
167
- if (!map.has(key)) {
168
- ++CACHE_TOTAL;
169
- }
170
166
  map.set(key, [
171
167
  Date.now(),
172
168
  data,
@@ -180,7 +176,6 @@ function deleteCacheItem(map, key) {
180
176
  if (item) {
181
177
  clearTimeout(item[2]);
182
178
  map.delete(key);
183
- --CACHE_TOTAL;
184
179
  }
185
180
  }
186
181
  function spliceSource(source, startIndex, endIndex, pattern, leading = '', trailing = '', content = '') {
@@ -282,12 +277,17 @@ function isTruthy(data, attr, falsey) {
282
277
  return falsey ? !value : !!value;
283
278
  }
284
279
  function hasSameOrigin(value, ...other) {
280
+ if (typeof value === 'string') {
281
+ try {
282
+ value = new URL(value);
283
+ }
284
+ catch {
285
+ return null;
286
+ }
287
+ }
285
288
  for (let url of other) {
286
289
  if (url) {
287
290
  try {
288
- if (typeof value === 'string') {
289
- value = new URL(value);
290
- }
291
291
  if (typeof url === 'string') {
292
292
  url = new URL(url);
293
293
  }
@@ -355,7 +355,7 @@ function createHash(host, file, hash, bufferMap) {
355
355
  if (localUri) {
356
356
  bufferMap[localUri] = buffer;
357
357
  }
358
- host.rename(file, (0, util_1.appendSuffix)(filename, length ? value.substring(0, length) : value));
358
+ host.rename(file, (0, util_1.appendSuffix)(filename, length > 0 ? value.substring(0, length) : value));
359
359
  return file.filename;
360
360
  }
361
361
  }
@@ -385,7 +385,6 @@ function getSrcURL(instance, parent, file) {
385
385
  if (parentDir === assetDir) {
386
386
  return [file.filename, true];
387
387
  }
388
- const splitPath = (value) => value.split(/[\\/]/).filter(segment => segment.trim());
389
388
  const prefix = splitPath(parentDir);
390
389
  const suffix = splitPath(assetDir);
391
390
  let found;
@@ -588,7 +587,7 @@ function transformURL(instance, host, parent, type, source, importFile) {
588
587
  if (asset && asset !== importFile) {
589
588
  if (!(0, util_1.hasValue)(asset.format, 'base64')) {
590
589
  if (asset.bundleReplace) {
591
- let k, valid;
590
+ let k = 0, valid;
592
591
  if (!isText) {
593
592
  let j = startIndex;
594
593
  while (isSpace(source[--j])) { }
@@ -674,7 +673,7 @@ function getFormatUUID(instance, attr, ext) {
674
673
  function setLocationUUID(instance, data, file, ext) {
675
674
  const { pathname, filename } = data;
676
675
  const assignUUID = ChromeDocument.INTERNAL_ASSIGNUUID;
677
- let index, modified;
676
+ let index = 0, modified;
678
677
  if (filename) {
679
678
  ext ||= path.extname(filename).substring(1);
680
679
  if ((index = filename.indexOf(assignUUID)) !== -1) {
@@ -842,7 +841,7 @@ function loadLocalFile(instance, item, pathname, cacheData, encoding, out) {
842
841
  }
843
842
  content ||= fs.readFileSync(pathname, encoding);
844
843
  const result = instance.tryParse(content, ext);
845
- if (result !== undefined && result !== null) {
844
+ if (result != null) {
846
845
  setCacheData(instance, item, result, pathname, cacheData, true);
847
846
  return result;
848
847
  }
@@ -850,7 +849,7 @@ function loadLocalFile(instance, item, pathname, cacheData, encoding, out) {
850
849
  function setCacheData(instance, item, data, dataKey, cacheData, template) {
851
850
  if ((0, types_1.isObject)(data) || template) {
852
851
  const cache = instance.settingsOf(item.source, template && item.source !== 'local' ? 'local_file' : 'cache');
853
- let timeout;
852
+ let timeout = 0;
854
853
  if (cache && (timeout = core_1.ClientDb.getTimeout(cache)) && timeout > 0) {
855
854
  addCacheItem(template ? CACHE_LOCALFILE : CACHE_DBRESULT, dataKey, data, timeout);
856
855
  }
@@ -895,6 +894,11 @@ function removeElement(instance, domBase, item, element) {
895
894
  }
896
895
  }
897
896
  }
897
+ function removeWatchReload(item) {
898
+ if ((0, types_1.isObject)(item.watch) && 'reload' in item.watch) {
899
+ delete item.watch.reload;
900
+ }
901
+ }
898
902
  function errorCredential(instance, domBase, item, err) {
899
903
  removeElement(instance, domBase, item);
900
904
  const pkg = getPackageName(err);
@@ -907,6 +911,7 @@ const replaceMatch = dom_1.DomWriter.replaceMatch.bind(dom_1.DomWriter);
907
911
  const getNewlineString = dom_1.DomWriter.getNewlineString.bind(dom_1.DomWriter);
908
912
  const getBuffer = (item, localUri) => item.sourceUTF8 || item.buffer || ((localUri ||= item.localUri) && Document.isPath(localUri) ? fs.readFileSync(localUri, item.encoding) : null);
909
913
  const hasSourceMap = (metadata, value) => (0, types_1.isObject)(metadata) && metadata.__sourcemap__ === value;
914
+ const splitPath = (value) => value.split(/[\\/]/).filter(segment => segment.trim());
910
915
  const isBundled = (value, main) => typeof value === 'number' && (value > 0 || main && value === 0);
911
916
  const isFont = (value) => value === 'truetype' || value === 'opentype' || value === 'woff' || value === 'woff2';
912
917
  const isImage = (value) => typeof value === 'string' && value.startsWith('image/') && value !== "image/svg+xml";
@@ -929,13 +934,13 @@ class ChromeDocument extends Document {
929
934
  parent = limit;
930
935
  limit = 0;
931
936
  }
937
+ const total = CACHE_LOCALFILE.size + CACHE_DBRESULT.size;
932
938
  let result = 0;
933
- if ((limit === 0 || CACHE_TOTAL >= limit) && percent > 0) {
939
+ if ((limit === 0 || total >= limit) && percent > 0) {
934
940
  if (percent >= 1) {
935
- result += CACHE_DBRESULT.size + CACHE_LOCALFILE.size;
941
+ result += total;
936
942
  CACHE_DBRESULT.clear();
937
943
  CACHE_LOCALFILE.clear();
938
- CACHE_TOTAL = 0;
939
944
  }
940
945
  else {
941
946
  const stored = [];
@@ -950,7 +955,6 @@ class ChromeDocument extends Document {
950
955
  const [map, key] = stored[i];
951
956
  deleteCacheItem(map, key);
952
957
  }
953
- CACHE_TOTAL = stored.length - result;
954
958
  }
955
959
  CACHE_QUERY.clear();
956
960
  CACHE_SELECTOR.clear();
@@ -1684,7 +1688,7 @@ class ChromeDocument extends Document {
1684
1688
  const from = trimQuote(match[idx]);
1685
1689
  const quote = output[a];
1686
1690
  const relative = /^\.{0,2}\//.test(from);
1687
- if (assert && /\btype\s*:\s*(["'`])css\1/.test(assert)) {
1691
+ if (assert && /\btype\s*:\s*(["'`])(?:css|json)\1/.test(assert)) {
1688
1692
  try {
1689
1693
  const asset = host.findAsset(relative ? new URL(from, uri) : from);
1690
1694
  if (asset) {
@@ -1731,7 +1735,6 @@ class ChromeDocument extends Document {
1731
1735
  break;
1732
1736
  case "text/css":
1733
1737
  if (file.editing) {
1734
- const htmlFile = this.htmlFile;
1735
1738
  const bundleId = file.bundleId;
1736
1739
  if (!(0, types_1.isEmpty)(bundleId)) {
1737
1740
  for (const asset of this.assets) {
@@ -1745,8 +1748,8 @@ class ChromeDocument extends Document {
1745
1748
  }
1746
1749
  }
1747
1750
  }
1748
- if (trailing && htmlFile) {
1749
- const output = transformURL(this, host, file, 'css', trailing, htmlFile);
1751
+ if (trailing && this.htmlFile) {
1752
+ const output = transformURL(this, host, file, 'css', trailing, this.htmlFile);
1750
1753
  if (output) {
1751
1754
  trailing = output;
1752
1755
  }
@@ -1871,9 +1874,8 @@ class ChromeDocument extends Document {
1871
1874
  this._cloudUrlMap = Object.create(null);
1872
1875
  this._cloudUploaded = new Set();
1873
1876
  this._cloudEndpoint = null;
1874
- const htmlFile = this.htmlFile;
1875
- if (htmlFile) {
1876
- const endpoint = instance.getStorage('upload', htmlFile.cloudStorage)?.upload?.endpoint;
1877
+ if (this.htmlFile) {
1878
+ const endpoint = instance.getStorage('upload', this.htmlFile.cloudStorage)?.upload?.endpoint;
1877
1879
  if (endpoint) {
1878
1880
  this._cloudEndpoint = new RegExp((0, types_1.escapePattern)(Document.toPosix(endpoint)) + '/', 'g');
1879
1881
  }
@@ -1912,7 +1914,7 @@ class ChromeDocument extends Document {
1912
1914
  if (this.aborted) {
1913
1915
  return (0, types_1.createAbortError)(true);
1914
1916
  }
1915
- const { host, instance, localStorage } = state;
1917
+ const { host, instance } = state;
1916
1918
  const htmlFile = this.htmlFile;
1917
1919
  const preferBuffer = process.env.EMC_CLOUD_UPLOAD_BUFFER === 'true';
1918
1920
  const checkDocument = (item) => {
@@ -1923,7 +1925,7 @@ class ChromeDocument extends Document {
1923
1925
  const inlineUrlCloud = item.inlineUrlCloudMap[id];
1924
1926
  if (inlineUrlCloud && !this._cloudUploaded.has(id)) {
1925
1927
  output = (output || source).replace(new RegExp((0, types_1.escapePattern)(id), 'g'), inlineUrlCloud);
1926
- localStorage.delete(this._cloudUrlMap[id]);
1928
+ state.localStorage.delete(this._cloudUrlMap[id]);
1927
1929
  }
1928
1930
  }
1929
1931
  if (output && output !== source) {
@@ -2086,10 +2088,9 @@ class ChromeDocument extends Document {
2086
2088
  if ((0, util_1.hasValue)(file.format, 'srcset')) {
2087
2089
  const srcset = element.getAttribute('srcset');
2088
2090
  if (srcset) {
2089
- const htmlFile = this.htmlFile;
2090
2091
  const uri = Document.toPosix(file.uri);
2091
2092
  const items = new Set([uri]);
2092
- const baseHref = getBaseURLs(htmlFile, this.baseUrl);
2093
+ const baseHref = getBaseURLs(this.htmlFile, this.baseUrl);
2093
2094
  const sameOrigin = hasSameOrigin(file.url || uri, ...baseHref);
2094
2095
  if (sameOrigin) {
2095
2096
  const src = element.getAttribute('src');
@@ -2152,7 +2153,7 @@ class ChromeDocument extends Document {
2152
2153
  }
2153
2154
  }
2154
2155
  if (productionRelease) {
2155
- const { inlineUrl, inlineUrlCloud, imported, hash } = item;
2156
+ const { inlineUrl, inlineUrlCloud, hash } = item;
2156
2157
  if (inlineUrl || inlineUrlCloud) {
2157
2158
  const filename = hash && createHash(host, item, hash, bufferMap);
2158
2159
  if (filename) {
@@ -2180,16 +2181,16 @@ class ChromeDocument extends Document {
2180
2181
  }
2181
2182
  hashed.add(item);
2182
2183
  }
2183
- else if (hash && (!item.editing || imported) && createHash(host, item, hash, bufferMap)) {
2184
+ else if (hash && (!item.editing || item.imported) && createHash(host, item, hash, bufferMap)) {
2184
2185
  hashed.add(item);
2185
2186
  }
2186
2187
  }
2187
2188
  }
2188
2189
  }
2189
2190
  async applyTransforms(processing) {
2190
- const { host, hashed, bufferMap, productionRelease } = processing;
2191
+ const host = processing.host;
2191
2192
  const sanitizeSource = (item, source) => {
2192
- if (productionRelease) {
2193
+ if (processing.productionRelease) {
2193
2194
  replaceCss(this, item);
2194
2195
  }
2195
2196
  const output = replaceHtml(this, source, processing, item.inlineUrlMap);
@@ -2236,8 +2237,8 @@ class ChromeDocument extends Document {
2236
2237
  }
2237
2238
  }
2238
2239
  item.sourceUTF8 = source;
2239
- if (productionRelease && item.hash && !hashed.has(item) && createHash(host, item, item.hash, bufferMap)) {
2240
- hashed.add(item);
2240
+ if (processing.productionRelease && item.hash && !processing.hashed.has(item) && createHash(host, item, item.hash, processing.bufferMap)) {
2241
+ processing.hashed.add(item);
2241
2242
  }
2242
2243
  }
2243
2244
  for (const item of this.cssFiles) {
@@ -2292,6 +2293,7 @@ class ChromeDocument extends Document {
2292
2293
  }
2293
2294
  }
2294
2295
  return Promise.allSettled(tasks).then(items => {
2296
+ const productionRelease = processing.productionRelease;
2295
2297
  for (let i = 0, length = items.length, value; i < length; ++i) {
2296
2298
  const item = items[i];
2297
2299
  if (item.status === 'fulfilled' && (value = item.value)) {
@@ -2314,7 +2316,7 @@ class ChromeDocument extends Document {
2314
2316
  });
2315
2317
  }
2316
2318
  async rewriteHtml(processing) {
2317
- const { host, startTime, cacheMiss, productionRelease } = processing;
2319
+ const { host, startTime, cacheMiss } = processing;
2318
2320
  const { moduleName, config } = this;
2319
2321
  const htmlFile = this.htmlFile;
2320
2322
  const localUri = htmlFile.localUri;
@@ -2333,7 +2335,7 @@ class ChromeDocument extends Document {
2333
2335
  if ((0, types_1.isArray)(config.ignoreServerCodeBlocks)) {
2334
2336
  domBase.ignoreTag(config.ignoreServerCodeBlocks);
2335
2337
  }
2336
- if (productionRelease) {
2338
+ if (processing.productionRelease) {
2337
2339
  replaceCss(this, htmlFile);
2338
2340
  }
2339
2341
  await this.setElementAttributes(processing, domBase);
@@ -2370,15 +2372,14 @@ class ChromeDocument extends Document {
2370
2372
  if ((0, types_1.isString)(config.stripCommentsAndCDATA)) {
2371
2373
  source = dom_1.DomWriter.getCommentsAndCDATA(source, config.stripCommentsAndCDATA, true, true);
2372
2374
  }
2373
- const format = htmlFile.format;
2374
- if (format) {
2375
+ if (htmlFile.format) {
2375
2376
  const { filename, uri, encoding, metadata } = htmlFile;
2376
- const result = await this.transform('html', source, format, {
2377
+ const result = await this.transform('html', source, htmlFile.format, {
2377
2378
  pathname: path.dirname(localUri),
2378
2379
  filename,
2379
2380
  mimeType: "text/html",
2380
2381
  metadata: Object.assign({ '__fromhtml__': true, ...this.config }, metadata),
2381
- cacheData: !productionRelease || this.productionIncremental ? { uri, encoding } : undefined
2382
+ cacheData: !processing.productionRelease || this.productionIncremental ? { uri, encoding } : undefined
2382
2383
  });
2383
2384
  if (result) {
2384
2385
  source = result.code;
@@ -2411,15 +2412,12 @@ class ChromeDocument extends Document {
2411
2412
  htmlFile.sourceUTF8 = source;
2412
2413
  }
2413
2414
  async setElementAttributes({ host, startTime, productionRelease }, domBase) {
2414
- const { moduleName, htmlFile } = this;
2415
- const cloud = host.Cloud;
2416
- const removeWatchReload = (item) => (0, types_1.isObject)(item.watch) && delete item.watch.reload;
2417
2415
  for (const item of this.assets) {
2418
2416
  const { element, inlineContent } = item;
2419
2417
  if (!element || element.removed || isRemoved(item) || isUnedited(item) || (0, types_1.ignoreFlag)(item.flags) && !(0, types_1.processFlag)(item.flags)) {
2420
2418
  continue;
2421
2419
  }
2422
- const domElement = new dom_1.HtmlElement(moduleName, element, item.attributes);
2420
+ const domElement = new dom_1.HtmlElement(this.moduleName, element, item.attributes);
2423
2421
  if (inlineContent) {
2424
2422
  let [innerXml, sourceMappingURL, inlineMap] = transform_1.SourceMap.removeSourceMappingURL(host.getUTF8String(item).trim());
2425
2423
  if (sourceMappingURL && !inlineMap && item.localUri) {
@@ -2445,10 +2443,10 @@ class ChromeDocument extends Document {
2445
2443
  uri = '';
2446
2444
  }
2447
2445
  }
2448
- if (uri && !(0, util_1.hasValue)(item.format, 'crossorigin') && item !== htmlFile) {
2449
- if (cloud?.getStorage('upload', item.cloudStorage)) {
2446
+ if (uri && !(0, util_1.hasValue)(item.format, 'crossorigin') && item !== this.htmlFile) {
2447
+ if (host.Cloud?.getStorage('upload', item.cloudStorage)) {
2450
2448
  const inlineUrlCloud = item.inlineUrlCloud ||= (0, crypto_1.randomUUID)();
2451
- (htmlFile.inlineUrlCloudMap ||= {})[inlineUrlCloud] = productionRelease ? this.removeServerRoot(uri) : uri;
2449
+ (this.htmlFile.inlineUrlCloudMap ||= {})[inlineUrlCloud] = productionRelease ? this.removeServerRoot(uri) : uri;
2452
2450
  uri = inlineUrlCloud;
2453
2451
  }
2454
2452
  if (!(0, util_1.hasValue)(item.format, 'webbundle')) {
@@ -2537,8 +2535,6 @@ class ChromeDocument extends Document {
2537
2535
  if (this.dataSource.length === 0) {
2538
2536
  return;
2539
2537
  }
2540
- const db = this.Db;
2541
- const cloud = host.Cloud;
2542
2538
  const sessionKey = (0, crypto_1.randomUUID)();
2543
2539
  const cacheData = Object.create(null);
2544
2540
  const dataItems = [];
@@ -2588,6 +2584,8 @@ class ChromeDocument extends Document {
2588
2584
  return Array.isArray(data) ? data : (0, types_1.isObject)(data) ? [data] : null;
2589
2585
  };
2590
2586
  const setElements = async (sourceSet) => {
2587
+ const db = this.Db;
2588
+ const cloud = host.Cloud;
2591
2589
  const batchMap = Object.create(null);
2592
2590
  const batchGroup = [];
2593
2591
  for (const item of sourceSet) {
@@ -3058,7 +3056,7 @@ class ChromeDocument extends Document {
3058
3056
  const data = items[index];
3059
3057
  items = data ? [data] : [];
3060
3058
  }
3061
- else if (limit !== undefined && limit > 0 && items.length > limit) {
3059
+ else if (typeof limit === 'number' && limit > 0 && items.length > limit) {
3062
3060
  items = items.slice(0, limit);
3063
3061
  }
3064
3062
  if (postQuery) {
@@ -3068,7 +3066,7 @@ class ChromeDocument extends Document {
3068
3066
  }
3069
3067
  }
3070
3068
  if (items.length > 0 && !item.transactionFail) {
3071
- const { element, preRender } = item;
3069
+ const element = item.element;
3072
3070
  const domElement = new dom_1.HtmlElement(this.moduleName, element);
3073
3071
  let value = '';
3074
3072
  const checkValue = async (name) => {
@@ -3163,8 +3161,8 @@ class ChromeDocument extends Document {
3163
3161
  value += trimTemplate(segment);
3164
3162
  }
3165
3163
  }
3166
- if (preRender) {
3167
- await checkValue(preRender);
3164
+ if (item.preRender) {
3165
+ await checkValue(item.preRender);
3168
3166
  }
3169
3167
  const { leadingText, trailingText } = item;
3170
3168
  if (leadingText) {
@@ -3239,8 +3237,8 @@ class ChromeDocument extends Document {
3239
3237
  }
3240
3238
  }
3241
3239
  if (valid) {
3242
- if (preRender) {
3243
- await checkValue(preRender);
3240
+ if (item.preRender) {
3241
+ await checkValue(item.preRender);
3244
3242
  }
3245
3243
  domElement.setAttribute(attr, value);
3246
3244
  }
@@ -3470,14 +3468,13 @@ class ChromeDocument extends Document {
3470
3468
  }
3471
3469
  const queryMap = useSessionCache ? CACHE_QUERY : this._queryMap;
3472
3470
  const selectorMap = useSessionCache ? CACHE_SELECTOR : this._selectorMap;
3471
+ const [stringSome, stringMany] = useUnsafeCssReplace ? ['\\s*', '\\s+'] : [PATTERN_STRING_SOME, PATTERN_STRING_MANY];
3473
3472
  const replaceMap = [];
3474
- const getStringSome = () => useUnsafeCssReplace ? '\\s*' : PATTERN_STRING_SOME;
3475
- const getStringMany = () => useUnsafeCssReplace ? '\\s+' : PATTERN_STRING_MANY;
3476
3473
  let current = source, modified, checkEmpty, match;
3477
3474
  const parseSelector = (value) => {
3478
3475
  const items = [];
3479
3476
  const revised = value.replace(useUnsafeCssReplace ? REGEXP_NTHCHILD_UNSAFE : REGEXP_NTHCHILD, (...capture) => {
3480
- return '`%' + (items.push('\\(' + getStringSome() + (capture[1] === '-' ? '\\x2d' : '\\x2b?') + (capture[2] || '[01]?') + (capture[3] === '0' ? !capture[1] && !capture[2] && !capture[4] && !capture[5] ? '[+-]?0[nN]?' : '0' : '[nN]') + getStringSome() + (capture[5] ? (capture[4] === '-' ? '\\x2d' : '\\x2b') + getStringSome() + capture[5] : `(?:[+-]${getStringSome()}0)?`) + (capture[6] ? getStringMany() + '[oO][fF]' + getStringMany() + capture[6].trim() : '') + getStringSome() + '\\)') - 1) + '`';
3477
+ return '`%' + (items.push('\\(' + stringSome + (capture[1] === '-' ? '\\x2d' : '\\x2b?') + (capture[2] || '[01]?') + (capture[3] === '0' ? !capture[1] && !capture[2] && !capture[4] && !capture[5] ? '[+-]?0[nN]?' : '0' : '[nN]') + stringSome + (capture[5] ? (capture[4] === '-' ? '\\x2d' : '\\x2b') + stringSome + capture[5] : `(?:[+-]${stringSome}0)?`) + (capture[6] ? stringMany + '[oO][fF]' + stringMany + capture[6].trim() : '') + stringSome + '\\)') - 1) + '`';
3481
3478
  });
3482
3479
  let selector = '';
3483
3480
  for (let i = 0, length = revised.length, casing = false, attr = false, quote = false; i < length; i++) {
@@ -3553,13 +3550,13 @@ class ChromeDocument extends Document {
3553
3550
  selector += (space & 1 ? '`%%`' : '') + (!casing && ch >= 'a' && ch <= 'z' ? '`' + ch + ch.toUpperCase() + '`' : ch) + (space & 2 ? '`%%`' : '');
3554
3551
  }
3555
3552
  selector = (0, types_1.escapePattern)(selector)
3556
- .replace(/\s*(>|~(?!=)|\\\+|\\\*(?!=)])\s*/g, (...capture) => getStringSome() + capture[1] + getStringSome())
3553
+ .replace(/\s*(>|~(?!=)|\\\+|\\\*(?!=)])\s*/g, (...capture) => stringSome + capture[1] + stringSome)
3557
3554
  .replace(/`([a-z][A-Z]|%{2,4})`/g, (...capture) => {
3558
3555
  switch (capture[1]) {
3559
3556
  case '%%':
3560
- return getStringSome();
3557
+ return stringSome;
3561
3558
  case '%%%':
3562
- return `(?:["']|${getStringSome()})`;
3559
+ return `(?:["']|${stringSome})`;
3563
3560
  case '%%%%':
3564
3561
  return '\\\\{0,}';
3565
3562
  default:
@@ -3569,7 +3566,7 @@ class ChromeDocument extends Document {
3569
3566
  for (let i = 0, length = revised.length; i < length; ++i) {
3570
3567
  selector = selector.replace('`%' + i + '`', items[i]);
3571
3568
  }
3572
- return selector.replace(/ +/g, getStringMany()) + getStringSome();
3569
+ return selector.replace(/ +/g, stringMany) + stringSome;
3573
3570
  };
3574
3571
  const replaceUnunsed = (name, items, nesting, increment = 1) => {
3575
3572
  for (let i = 0; i < items.length; i += increment) {
@@ -3601,9 +3598,9 @@ class ChromeDocument extends Document {
3601
3598
  flags = 'g';
3602
3599
  }
3603
3600
  else if (name === 'scope') {
3604
- selector = `\\(${getStringSome() + parseSelector(value)}\\)`;
3601
+ selector = `\\(${stringSome + parseSelector(value)}\\)`;
3605
3602
  if (valueTo) {
3606
- selector += `${getStringMany()}[tT][oO]${getStringMany()}\\(${getStringSome() + parseSelector(valueTo)}\\)`;
3603
+ selector += `${stringMany}[tT][oO]${stringMany}\\(${stringSome + parseSelector(valueTo)}\\)`;
3607
3604
  }
3608
3605
  flags = 'g';
3609
3606
  revised = true;
@@ -3612,22 +3609,22 @@ class ChromeDocument extends Document {
3612
3609
  const startIndex = value.indexOf('(');
3613
3610
  const endIndex = value.lastIndexOf(')');
3614
3611
  if (startIndex !== -1 && endIndex !== -1) {
3615
- selector = '[sS][eE][lL][eE][cC][tT][oO][rR]\\(' + getStringSome() + parseSelector(value.substring(startIndex + 1, endIndex)) + '\\)';
3612
+ selector = '[sS][eE][lL][eE][cC][tT][oO][rR]\\(' + stringSome + parseSelector(value.substring(startIndex + 1, endIndex)) + '\\)';
3616
3613
  flags = 'g';
3617
3614
  revised = true;
3618
3615
  }
3619
3616
  }
3620
3617
  if (!revised) {
3621
3618
  selector = (0, types_1.escapePattern)(selector || value)
3622
- .replace(/\s*(\\[()])\s*/g, (...capture) => getStringSome() + capture[1] + getStringSome())
3623
- .replace(/:\s+/g, getStringSome() + ':' + getStringSome())
3624
- .replace(/\s+([<>/]|<=|>=|\\\*)\s+/g, (...capture) => getStringSome() + capture[1] + getStringSome())
3625
- .replace(/ +/g, getStringMany());
3619
+ .replace(/\s*(\\[()])\s*/g, (...capture) => stringSome + capture[1] + stringSome)
3620
+ .replace(/:\s+/g, stringSome + ':' + stringSome)
3621
+ .replace(/\s+([<>/]|<=|>=|\\\*)\s+/g, (...capture) => stringSome + capture[1] + stringSome)
3622
+ .replace(/ +/g, stringMany);
3626
3623
  if (flags === 'g') {
3627
3624
  selector = selector.replace(/`([a-z][A-Z])`/g, (...capture) => `[${capture[1]}]`);
3628
3625
  }
3629
3626
  }
3630
- pattern = new RegExp('(\\s*)@' + (flags === 'g' ? name.split('').map(ch => `[${ch + ch.toUpperCase()}]`).join('') : name) + getStringMany() + selector + getStringSome() + '{', flags);
3627
+ pattern = new RegExp('(\\s*)@' + (flags === 'g' ? name.split('').map(ch => `[${ch + ch.toUpperCase()}]`).join('') : name) + stringMany + selector + stringSome + '{', flags);
3631
3628
  queryMap.set(key, pattern);
3632
3629
  }
3633
3630
  else {
@@ -3702,9 +3699,9 @@ class ChromeDocument extends Document {
3702
3699
  const pattern = selectorMap.get(key);
3703
3700
  let single, group;
3704
3701
  if (!pattern) {
3705
- const selector = parseSelector(value) + (child ? `(?:${getStringSome()}&${getStringSome()})*` : '');
3706
- single = new RegExp(`(^|;)(${getStringSome()})(?<!${child ? '&|' : ''}@[^};]*)${selector}\\{$`);
3707
- group = new RegExp(`((?:^|;)[^{]*?${getStringSome()}(,${getStringSome()}|(?<!@[^};]*)))(${(child ? '(?:&\\s+|(?<!&))' : '') + selector})(,[^{]*)?\\{$`, 'g');
3702
+ const selector = parseSelector(value) + (child ? `(?:${stringSome}&${stringSome})*` : '');
3703
+ single = new RegExp(`(^|;)(${stringSome})(?<!${child ? '&|' : ''}@[^};]*)${selector}\\{$`);
3704
+ group = new RegExp(`((?:^|;)[^{]*?${stringSome}(,${stringSome}|(?<!@[^};]*)))(${(child ? '(?:&\\s+|(?<!&))' : '') + selector})(,[^{]*)?\\{$`, 'g');
3708
3705
  selectorMap.set(key, [single, group]);
3709
3706
  }
3710
3707
  else {
@@ -3838,7 +3835,7 @@ class ChromeDocument extends Document {
3838
3835
  }
3839
3836
  if (modified) {
3840
3837
  for (const name in checkEmpty) {
3841
- const pattern = CACHE_ATRULES[name + (useUnsafeCssReplace ? '_1' : '')] ||= new RegExp(`(\\s*)@${name}[^{]*{${getStringSome()}}` + dom_1.DomWriter.PATTERN_TRAILINGSPACE, 'gi');
3838
+ const pattern = CACHE_ATRULES[name + (useUnsafeCssReplace ? '_1' : '')] ||= new RegExp(`(\\s*)@${name}[^{]*{${stringSome}}` + dom_1.DomWriter.PATTERN_TRAILINGSPACE, 'gi');
3842
3839
  while (match = pattern.exec(current)) {
3843
3840
  current = spliceSource(current, match.index, getEndIndex(match), pattern, match[1], match[2]);
3844
3841
  }
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
- {
2
- "name": "@pi-r/chrome",
3
- "version": "0.8.0",
4
- "description": "Chrome 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/pi-r.git",
13
- "directory": "src/module/chrome"
14
- },
15
- "keywords": [
16
- "squared",
17
- "e-mc",
18
- "squared-functions"
19
- ],
20
- "author": "An Pham <anpham6@gmail.com>",
21
- "license": "MIT",
22
- "homepage": "https://github.com/anpham6/pi-r#readme",
23
- "dependencies": {
24
- "@e-mc/cloud": "^0.10.0",
25
- "@e-mc/core": "^0.10.0",
26
- "@e-mc/document": "^0.10.0",
27
- "@e-mc/types": "^0.10.0"
28
- }
29
- }
1
+ {
2
+ "name": "@pi-r/chrome",
3
+ "version": "0.8.2",
4
+ "description": "Chrome 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/pi-r.git",
13
+ "directory": "src/module/chrome"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "e-mc",
18
+ "squared-functions"
19
+ ],
20
+ "author": "An Pham <anpham6@gmail.com>",
21
+ "license": "MIT",
22
+ "homepage": "https://github.com/anpham6/pi-r#readme",
23
+ "dependencies": {
24
+ "@e-mc/cloud": "^0.10.2",
25
+ "@e-mc/core": "^0.10.2",
26
+ "@e-mc/document": "^0.10.2",
27
+ "@e-mc/types": "^0.10.2"
28
+ }
29
+ }
@@ -29,7 +29,6 @@ interface ConditionProperty {
29
29
 
30
30
  interface CssConditionData extends ConditionProperty {
31
31
  name?: string;
32
- conditionText: string;
33
32
  }
34
33
 
35
34
  export interface UserAgentData {