@pi-r/chrome 0.10.3 → 0.11.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/index.js CHANGED
@@ -21,10 +21,8 @@ const CACHE_CONTENT = new Map();
21
21
  const CACHE_FORMAT = new Map();
22
22
  const CACHE_DATASET = {};
23
23
  const CACHE_ATRULES = {};
24
- const [PATTERN_STRING_SOME, PATTERN_STRING_MANY] = (function () {
25
- const grouping = '(?:\\s|' + dom_1.DomWriter.PATTERN_COMMENT + '|`[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}`)';
26
- return [grouping + '*', grouping + '+'];
27
- })();
24
+ const PATTERN_STRING_SOME = '(?:\\s|' + dom_1.DomWriter.PATTERN_COMMENT + '|`[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}`)*';
25
+ const PATTERN_STRING_MANY = PATTERN_STRING_SOME.slice(0, -1) + '+';
28
26
  const [REGEXP_NTHCHILD, REGEXP_NTHCHILD_UNSAFE, REGEXP_REPLACECSS] = (function () {
29
27
  const parenthesis = '(?:,[^()]+(?=\\))|[^()]|\\([^()]*\\(+|\\([^()]*\\)|(?:\\)[^()]+)?\\)*?)+';
30
28
  return [
@@ -53,14 +51,6 @@ const REGEXP_FONTFACE_UNSAFE = new RegExp(`(\\s*)@font-face\\s*{([^}]+)}` + dom_
53
51
  const REGEXP_FONTFAMILY_UNSAFE = /\bfont-family\s*:([^;}]+)/i;
54
52
  const REGEXP_KEYFRAMES_UNSAFE = /(\s*)@keyframes\s+([^{]+){/gi;
55
53
  const REGEXP_PROPERTY_UNSAFE = /(\s*)@property\s+(--[^\s]+)\s*{/gi;
56
- const REGEXP_OBJECT_NAME = /([^[.\s]+)((?:\s*\[[^\]]+\]\s*)+)?\s*\.?\s*/g;
57
- const REGEXP_OBJECT_SUBSCRIPT = /\[\s*(["'])?(.+?)\1\s*\]/g;
58
- const REGEXP_TEMPLATE_LEADING = /\s*\{\{- \s*((?:[^}]|}(?!}))+?)\}\}/g;
59
- const REGEXP_TEMPLATE_TRAILING = /\{\{((?:[^}]|}(?!}))+?)\s*? -\}\}\s*/g;
60
- const REGEXP_URL = /(?:\b[Uu][Rr][Ll]\(|@import\s+(["'])|\b([Hh][Rr][Ee][Ff])\s*=)/g;
61
- const REGEXP_URL_JS = /\bimport\(/g;
62
- const REGEXP_URL_SVG = /(?:\b[Uu][Rr][Ll]\(|@import\s+(["'])|\b([Hh][Rr][Ee][Ff])\s*=)/g;
63
- const REGEXP_IMAGESET = /\b(?:-webkit-)?image-set\(/gi;
64
54
  const REGEXP_IMAGESET_URL = /(?:^|[^(])\s*("(\s*[^)][^"]*)"|'(\s*[^)][^"]*)')\s*(?:,|$|[^)][^,]*,?)/gd;
65
55
  const REGEXP_SANITIZEALL = new RegExp(`(?:\\s+|\\b)data-(?:pathname|filename|intl-locales|(?:use|exclude)-[^=\\s]+)\\s*${dom_1.DomWriter.PATTERN_ATTRVALUE}(?=(?:[^"<]+"[^"]*"|[^'<]+'[^']*')*[^<>]*>)`, 'g');
66
56
  function removeNamespace(name, source, newline, comments) {
@@ -80,16 +70,17 @@ function removeNamespace(name, source, newline, comments) {
80
70
  return (0, types_1.isArray)(comments) ? source.replace(REGEXP_BLOCKEXCLUDE, (...capture) => getNewlineString(capture[1], capture[2], newline)) : source;
81
71
  }
82
72
  function getObjectValue(value, key) {
83
- REGEXP_OBJECT_NAME.lastIndex = 0;
73
+ const objectName = /([^[.\s]+)((?:\s*\[[^\]]+\]\s*)+)?\s*\.?\s*/g;
74
+ const subscript = /\[\s*(["'])?(.+?)\1\s*\]/g;
84
75
  let found = false, match;
85
- while (match = REGEXP_OBJECT_NAME.exec(key)) {
76
+ while (match = objectName.exec(key)) {
86
77
  if ((0, types_1.isObject)(value)) {
87
78
  value = value[match[1]];
88
79
  const name = match[2];
89
80
  if (name) {
90
- REGEXP_OBJECT_SUBSCRIPT.lastIndex = 0;
81
+ subscript.lastIndex = 0;
91
82
  let index;
92
- while (index = REGEXP_OBJECT_SUBSCRIPT.exec(name)) {
83
+ while (index = subscript.exec(name)) {
93
84
  const quote = index[1];
94
85
  const attr = quote ? index[2] : index[2].trim();
95
86
  if (quote && (0, types_1.isObject)(value) || /^\d+$/.test(attr) && (typeof value === 'string' || (0, types_1.isObject)(value))) {
@@ -110,14 +101,14 @@ function getObjectValue(value, key) {
110
101
  return found ? value : null;
111
102
  }
112
103
  function trimTemplate(value) {
113
- REGEXP_TEMPLATE_LEADING.lastIndex = 0;
104
+ const opening = /\s*\{\{- \s*((?:[^}]|}(?!}))+?)\}\}/g;
105
+ const closing = /\{\{((?:[^}]|}(?!}))+?)\s*? -\}\}\s*/g;
114
106
  let match;
115
- while (match = REGEXP_TEMPLATE_LEADING.exec(value)) {
116
- value = replaceMatch(match, value, 1, REGEXP_TEMPLATE_LEADING);
107
+ while (match = opening.exec(value)) {
108
+ value = replaceMatch(match, value, 1, opening);
117
109
  }
118
- REGEXP_TEMPLATE_TRAILING.lastIndex = 0;
119
- while (match = REGEXP_TEMPLATE_TRAILING.exec(value)) {
120
- value = replaceMatch(match, value, 1, REGEXP_TEMPLATE_TRAILING);
110
+ while (match = closing.exec(value)) {
111
+ value = replaceMatch(match, value, 1, closing);
121
112
  }
122
113
  return value;
123
114
  }
@@ -148,7 +139,7 @@ function findClosingIndex(source, lastIndex, char) {
148
139
  return [endIndex, trailing];
149
140
  }
150
141
  function getPackageName(err) {
151
- if (err instanceof Error && err.code === types_1.ERR_CODE.MODULE_NOT_FOUND) {
142
+ if ((0, types_1.isErrorCode)(err, types_1.ERR_CODE.MODULE_NOT_FOUND)) {
152
143
  return (0, util_1.getModuleName)(err);
153
144
  }
154
145
  }
@@ -393,40 +384,6 @@ function createHash(host, file, hash, bufferMap) {
393
384
  host.writeFail(["Unable to read file", filename], err, 32);
394
385
  }
395
386
  }
396
- function getSrcUrl(instance, parent, file) {
397
- const relativeUrl = file.relativeUrl || instance.host.removeCwd(file.localUri) && getRelativeUrl(file);
398
- if (!relativeUrl) {
399
- return ['', false];
400
- }
401
- if (parent.inlineContent) {
402
- return [relativeUrl, false];
403
- }
404
- let parentDir = parent.pathname, assetDir = file.pathname;
405
- if (!instance.productionRelease) {
406
- if (parent.moveTo) {
407
- parentDir = Document.joinPath(parent.moveTo, parentDir);
408
- }
409
- if (file.moveTo) {
410
- assetDir = Document.joinPath(file.moveTo, assetDir);
411
- }
412
- }
413
- if (parentDir === assetDir) {
414
- return [file.filename, true];
415
- }
416
- if (!parentDir) {
417
- return [relativeUrl, false];
418
- }
419
- const splitPath = (value) => value.split(/\//).filter(seg => seg.trim());
420
- const prefix = splitPath(parentDir);
421
- const suffix = splitPath(assetDir);
422
- let found = false;
423
- while (prefix.length > 0 && suffix.length > 0 && prefix[0] === suffix[0]) {
424
- prefix.shift();
425
- suffix.shift();
426
- found = true;
427
- }
428
- return [found ? Document.joinPath('../'.repeat(prefix.length), suffix.join('/'), file.filename) : '../'.repeat(prefix.length) + relativeUrl, false];
429
- }
430
387
  function replaceCss(instance, file) {
431
388
  if (file.inlineUrlMap) {
432
389
  sanitizeCss(instance, file.inlineUrlMap);
@@ -747,7 +704,7 @@ class ChromeDocument extends Document {
747
704
  if (i !== start && !Document.createDir(destDir = path.join(destDir, ...subDir.slice(i, start)))) {
748
705
  throw (0, types_1.errorMessage)('production', destDir, 'create');
749
706
  }
750
- const dest = path.join(destDir, subDir[subDir.length - 1]);
707
+ const dest = path.join(destDir, subDir.at(-1));
751
708
  Document.renameFile(src, dest);
752
709
  instance.addLog(4, `production: ${dest} (mapped)`, { source: subDir.join(path.sep) });
753
710
  emptyDir.add(path.dirname(src));
@@ -1283,7 +1240,7 @@ class ChromeDocument extends Document {
1283
1240
  try {
1284
1241
  const asset = host.findAsset(relative ? new URL(from, uri) : from);
1285
1242
  if (asset) {
1286
- const [relativeUrl, sameDir] = getSrcUrl(this, bundleMain || file, asset);
1243
+ const [relativeUrl, sameDir] = this.getSrcUrl(bundleMain || file, asset);
1287
1244
  if (relativeUrl) {
1288
1245
  output = (0, util_1.spliceString)(output, a, b, quote + (sameDir ? './' : '') + relativeUrl + quote, REGEXP_IMPORTMODULE);
1289
1246
  }
@@ -1381,7 +1338,7 @@ class ChromeDocument extends Document {
1381
1338
  if (hosted) {
1382
1339
  const asset = this.host?.findAsset(uri);
1383
1340
  if (asset) {
1384
- const [relativeUrl, sameDir] = getSrcUrl(this, bundleMain || file, asset);
1341
+ const [relativeUrl, sameDir] = this.getSrcUrl(bundleMain || file, asset);
1385
1342
  if (relativeUrl) {
1386
1343
  value = (sameDir ? './' : '') + relativeUrl;
1387
1344
  }
@@ -2067,9 +2024,7 @@ class ChromeDocument extends Document {
2067
2024
  const detectHeight = domElement.getAttribute('height') === 'detect';
2068
2025
  if (detectWidth || detectHeight) {
2069
2026
  let width = 0, height = 0, buffer = null;
2070
- const addLog = (suffix, err) => {
2071
- this.addLog(3, err instanceof Error ? err : 'Could not detect image ' + suffix, { source: item.localUri ? path.basename(item.localUri) : item.filename });
2072
- };
2027
+ const addLog = (suffix, err) => this.addLog(3, err || ('Not detectable image ' + suffix), { source: item.localUri ? path.basename(item.localUri) : item.filename });
2073
2028
  if (isImage(item.mimeType) && (buffer = host.getBuffer(item))) {
2074
2029
  try {
2075
2030
  ({ width, height } = (0, image_size_1.imageSize)(buffer));
@@ -2974,7 +2929,7 @@ class ChromeDocument extends Document {
2974
2929
  case 'cloud': {
2975
2930
  const { service, table, id, query } = item;
2976
2931
  const queryString = (table ? 'table=' + table : '') + (id || query ? (table ? ';' : '') + (id ? 'id=' + id : 'query=' + Document.asString(query)) : '');
2977
- (cloud || this).formatFail(64, service, ["Unable to execute query", queryString], emptyResponse(queryString, service), { ...Cloud.LOG_CLOUD_FAIL, startTime });
2932
+ (cloud || this).formatFail(64, service, ["Unable to execute query", queryString], emptyResponse(queryString, service), Cloud.optionsLogMessage('FAIL', { startTime }));
2978
2933
  break;
2979
2934
  }
2980
2935
  case 'uri': {
@@ -3446,6 +3401,40 @@ class ChromeDocument extends Document {
3446
3401
  return current;
3447
3402
  }
3448
3403
  }
3404
+ getSrcUrl(parent, file) {
3405
+ const relativeUrl = file.relativeUrl || this.host.removeCwd(file.localUri) && getRelativeUrl(file);
3406
+ if (!relativeUrl) {
3407
+ return ['', false];
3408
+ }
3409
+ if (parent.inlineContent) {
3410
+ return [relativeUrl, false];
3411
+ }
3412
+ let parentDir = parent.pathname, assetDir = file.pathname;
3413
+ if (!this.productionRelease) {
3414
+ if (parent.moveTo) {
3415
+ parentDir = Document.joinPath(parent.moveTo, parentDir);
3416
+ }
3417
+ if (file.moveTo) {
3418
+ assetDir = Document.joinPath(file.moveTo, assetDir);
3419
+ }
3420
+ }
3421
+ if (parentDir === assetDir) {
3422
+ return [file.filename, true];
3423
+ }
3424
+ if (!parentDir) {
3425
+ return [relativeUrl, false];
3426
+ }
3427
+ const splitPath = (value) => value.split('/').filter(seg => seg.trim());
3428
+ const prefix = splitPath(parentDir);
3429
+ const suffix = splitPath(assetDir);
3430
+ let found = false;
3431
+ while (prefix.length > 0 && suffix.length > 0 && prefix[0] === suffix[0]) {
3432
+ prefix.shift();
3433
+ suffix.shift();
3434
+ found = true;
3435
+ }
3436
+ return [found ? Document.joinPath('../'.repeat(prefix.length), suffix.join('/'), file.filename) : '../'.repeat(prefix.length) + relativeUrl, false];
3437
+ }
3449
3438
  isWatched(file) {
3450
3439
  const { initialValue, format } = file;
3451
3440
  return (file.imported ||
@@ -3563,27 +3552,25 @@ class ChromeDocument extends Document {
3563
3552
  const baseHref = collectBaseUrl(importFile || file, this.baseUrl);
3564
3553
  const isHtml = type === 'html';
3565
3554
  const isJs = type === 'js';
3566
- const pattern = isJs ? REGEXP_URL_JS : type === 'svg' ? REGEXP_URL_SVG : REGEXP_URL;
3567
- pattern.lastIndex = 0;
3568
- REGEXP_IMAGESET.lastIndex = 0;
3555
+ const imageSet = /\b(?:-webkit-)?image-set\(/gi;
3556
+ const pattern = isJs ? /\bimport\(/g : type === 'svg' ? /(?:\b[Uu][Rr][Ll]\(|@import\s+(["'])|\b([Hh][Rr][Ee][Ff])\s*=)/g : /(?:\b[Uu][Rr][Ll]\(|@import\s+(["'])|\b([Hh][Rr][Ee][Ff])\s*=)/g;
3569
3557
  let length = source.length, modified = false, match;
3570
- while (match = REGEXP_IMAGESET.exec(source)) {
3558
+ while (match = imageSet.exec(source)) {
3571
3559
  const startIndex = match.index + match[0].length;
3572
3560
  const endIndex = findClosingIndex(source, startIndex, '(');
3573
- if (endIndex === -1) {
3574
- continue;
3575
- }
3576
- REGEXP_IMAGESET_URL.lastIndex = 0;
3577
- let content = source.substring(startIndex, endIndex), url;
3578
- while (url = REGEXP_IMAGESET_URL.exec(content)) {
3579
- const quote = url[2] ? '"' : "'";
3580
- const [a, b] = url.indices[1];
3581
- content = (0, util_1.spliceString)(content, a, b, `url(${quote + (url[2] || url[3]).trim() + quote})`, REGEXP_IMAGESET_URL);
3582
- modified = true;
3583
- }
3584
- if (modified) {
3585
- source = (0, util_1.spliceString)(source, startIndex, endIndex, content, REGEXP_IMAGESET);
3586
- modified = false;
3561
+ if (endIndex !== -1) {
3562
+ REGEXP_IMAGESET_URL.lastIndex = 0;
3563
+ let content = source.substring(startIndex, endIndex), url;
3564
+ while (url = REGEXP_IMAGESET_URL.exec(content)) {
3565
+ const quote = url[2] ? '"' : "'";
3566
+ const [a, b] = url.indices[1];
3567
+ content = (0, util_1.spliceString)(content, a, b, `url(${quote + (url[2] || url[3]).trim() + quote})`, REGEXP_IMAGESET_URL);
3568
+ modified = true;
3569
+ }
3570
+ if (modified) {
3571
+ source = (0, util_1.spliceString)(source, startIndex, endIndex, content, imageSet);
3572
+ modified = false;
3573
+ }
3587
3574
  }
3588
3575
  }
3589
3576
  while (match = pattern.exec(source)) {
@@ -3707,7 +3694,7 @@ class ChromeDocument extends Document {
3707
3694
  if (quote) {
3708
3695
  value = quote + value + quote;
3709
3696
  }
3710
- const revised = isJs ? 'import(' + value + trailing + ')' : isHref ? 'href=' + value : (isText ? '@import ' + value : `url(${value})`) + trailing;
3697
+ const revised = isJs ? `import(${value + trailing})` : isHref ? 'href=' + value : (isText ? '@import ' + value : `url(${value})`) + trailing;
3711
3698
  source = (0, util_1.spliceString)(source, startIndex, i + 1, revised, pattern);
3712
3699
  length = source.length;
3713
3700
  modified = true;
@@ -3717,7 +3704,7 @@ class ChromeDocument extends Document {
3717
3704
  if (!quote) {
3718
3705
  continue;
3719
3706
  }
3720
- const target = new RegExp('^\\s*(' + quote + '((?:[^' + quote + ']|(?<=\\\\)' + quote + ')+)' + quote + ')\\s*(,.+)$').exec(content);
3707
+ const target = new RegExp(`^\\s*(${quote}((?:[^${quote}]|(?<=\\\\)${quote})+)${quote})\\s*(,.+)$`).exec(content);
3721
3708
  if (target) {
3722
3709
  url = target[1].trim();
3723
3710
  trailing = target[2].trim();
@@ -3734,7 +3721,7 @@ class ChromeDocument extends Document {
3734
3721
  if (base64) {
3735
3722
  for (const item of this.assets) {
3736
3723
  if (item.base64 === base64) {
3737
- setOutputURL(getSrcUrl(this, file, item)[0], item);
3724
+ setOutputURL(this.getSrcUrl(file, item)[0], item);
3738
3725
  break;
3739
3726
  }
3740
3727
  }
@@ -3743,7 +3730,11 @@ class ChromeDocument extends Document {
3743
3730
  else if (baseHref.length > 0) {
3744
3731
  let fullUrl, asset;
3745
3732
  for (const parentUrl of baseHref) {
3746
- asset = host.findAsset(fullUrl = Document.resolvePath(url, parentUrl), { replaced: true }) || (this.related.has(file) ? host.findAsset(fullUrl, { assets: Array.from(this.related.get(file)), replaced: true }) : undefined);
3733
+ asset = host.findAsset(fullUrl = Document.resolvePath(url, parentUrl), { replaced: true });
3734
+ if (!asset) {
3735
+ const assets = this.related.get(file);
3736
+ asset = assets && host.findAsset(fullUrl, { assets: Array.from(assets), replaced: true });
3737
+ }
3747
3738
  if (asset) {
3748
3739
  break;
3749
3740
  }
@@ -3774,23 +3765,23 @@ class ChromeDocument extends Document {
3774
3765
  revised = this.processUrl(host, file, this.removeServerRoot(revised), type, asset) || revised;
3775
3766
  source = (0, util_1.spliceString)(source, k, l + 1, revised, pattern);
3776
3767
  length = source.length;
3777
- if (!file.watch) {
3778
- host.deleteFile(asset.localUri, { ignorePermission: true, emptyDir: true, all: true, id: asset.id });
3779
- asset.invalid = true;
3780
- }
3781
- else {
3768
+ if (file.watch) {
3782
3769
  related.push(asset);
3783
3770
  if (!(0, types_1.existsFlag)(asset.flags)) {
3784
3771
  asset.flags |= 8;
3785
3772
  }
3786
3773
  }
3774
+ else if (asset.localUri) {
3775
+ host.deleteFile(asset.localUri, { ignorePermission: true, emptyDir: true, all: true, id: asset.id });
3776
+ asset.invalid = true;
3777
+ }
3787
3778
  modified = true;
3788
3779
  continue;
3789
3780
  }
3790
3781
  }
3791
3782
  if (hasSameOrigin(asset.url || fullUrl, ...baseHref)) {
3792
3783
  let sameDir = false;
3793
- [fullUrl, sameDir] = getSrcUrl(this, file, asset);
3784
+ [fullUrl, sameDir] = this.getSrcUrl(file, asset);
3794
3785
  if (sameDir && isJs) {
3795
3786
  fullUrl = './' + fullUrl;
3796
3787
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/chrome",
3
- "version": "0.10.3",
3
+ "version": "0.11.1",
4
4
  "description": "Chrome document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,10 +20,10 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.12.7",
24
- "@e-mc/core": "^0.12.7",
25
- "@e-mc/document": "^0.12.7",
26
- "@e-mc/types": "^0.12.7",
23
+ "@e-mc/cloud": "^0.13.5",
24
+ "@e-mc/core": "^0.13.5",
25
+ "@e-mc/document": "^0.13.5",
26
+ "@e-mc/types": "^0.13.5",
27
27
  "image-size": "^2.0.2"
28
28
  }
29
29
  }
package/types/index.d.ts CHANGED
@@ -117,6 +117,7 @@ export interface IChromeDocument<T extends IFileManager<U>, U extends DocumentAs
117
117
  cloudObject(state: CloudScopeOrigin<T, U, V>, file: U): boolean;
118
118
  cloudUpload(state: CloudScopeOrigin<T, U, V>, file: U, url: string, active: boolean): Promise<boolean>;
119
119
  cloudFinalize(state: CloudScopeOrigin<T, U, V>): Promise<unknown[]>;
120
+ getSrcUrl(parent: U, file: U): [string, boolean];
120
121
  isWatched(file: U): boolean;
121
122
  get elements(): XmlTagNode[];
122
123
  get editing(): U[];
@@ -91,7 +91,7 @@ export interface UnusedAtRule {
91
91
  selector?: string;
92
92
  media?: ConditionProperty;
93
93
  supports?: ConditionProperty;
94
- container?: CssConditionData & Partial<ConditionProperty>;
94
+ container?: Omit<CssConditionData, "conditionText"> & Partial<ConditionProperty>;
95
95
  layer?: string[];
96
96
  }
97
97