@pi-r/chrome 0.2.13 → 0.2.15

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.
Files changed (2) hide show
  1. package/index.js +197 -200
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -32,7 +32,7 @@ const [REGEXP_NTHCHILD, REGEXP_NTHCHILD_UNSAFE, REGEXP_REPLACECSS] = (function (
32
32
  return [
33
33
  new RegExp(`\\(${PATTERN_STRING_SOME}([+-])?(?:(\\d*)[nN]|(0))?${PATTERN_STRING_SOME}([+-])?${PATTERN_STRING_SOME}(\\d*)(?:${PATTERN_STRING_MANY}[oO][fF]${PATTERN_STRING_MANY}(${parenthesis})${PATTERN_STRING_SOME}\\)|${PATTERN_STRING_SOME}\\))`, 'g'),
34
34
  new RegExp(`\\(\\s*([+-])?(?:(\\d*)[nN]|(0))?\\s*([+-])?\\s*(\\d*)(?:\\s+[oO][fF]\\s+(${parenthesis})\\s*\\)|\\s*\\))`, 'g'),
35
- new RegExp(`^@import\\s+(?:url\\((?:[^)]|(?<=\\\\)\\))+\\)|"(?:[^"]|(?<=\\\\)")+"|'(?:[^']|(?<=\\\\)')+')(\\s*layer(?:\\(([^)]*)\\)|\\b))?(?:\\s*supports\\((${parenthesis})\\))?(.*?);?$`)
35
+ new RegExp(`^@import\\s+(?:url\\((?:[^)]|(?<=\\\\)\\))+\\)|"(?:[^"]|(?<=\\\\)")+"|'(?:[^']|(?<=\\\\)')+')(\\s*layer(\\([^)]*\\))?)?(?:\\s*supports\\((${parenthesis})\\))?(.*)$`, 'si')
36
36
  ];
37
37
  })();
38
38
  const REGEXP_COMMENT = new RegExp(dom_1.DomWriter.PATTERN_COMMENT, 'g');
@@ -184,7 +184,7 @@ function hasCondition(data, condition) {
184
184
  if (items.length && !isSpace(match[0][0])) {
185
185
  return false;
186
186
  }
187
- items.push([match[1], trimQuote(match[2]), trimQuote(match[3])]);
187
+ items.push([match[1], trimQuote(match[2]), match[3] ? trimQuote(match[3]) : '']);
188
188
  }
189
189
  REGEXP_TEMPLATECOMPARISON.lastIndex = 0;
190
190
  const length = items.length;
@@ -195,18 +195,13 @@ function hasCondition(data, condition) {
195
195
  if (!isNaN(+value)) {
196
196
  return +value;
197
197
  }
198
- if (match = /^(["'])([\S\s]*)\1$/.exec(value)) {
199
- return match[2];
200
- }
201
198
  return getObjectValue(data, value);
202
199
  };
203
200
  for (let i = 0; i < length; ++i) {
204
- const item = items[i];
205
- const arg1 = item[1];
206
- const arg2 = item[2];
207
- let valid;
201
+ const [cmp, arg1, arg2] = items[i];
202
+ let valid = false;
208
203
  if (arg2) {
209
- switch (item[0]) {
204
+ switch (cmp) {
210
205
  case 'eq':
211
206
  valid = parseArg(arg1) == parseArg(arg2);
212
207
  break;
@@ -229,7 +224,7 @@ function hasCondition(data, condition) {
229
224
  return false;
230
225
  }
231
226
  }
232
- else if (item[0]) {
227
+ else if (cmp) {
233
228
  return false;
234
229
  }
235
230
  else {
@@ -380,9 +375,9 @@ function transformURL(host, parent, type, source, importFile) {
380
375
  var _a;
381
376
  const related = [];
382
377
  const baseHref = getBaseURLs(importFile || parent, this.baseUrl);
383
- const isHtml = type === 'html';
378
+ const isHtml = type === 'html' || type === 'svg';
384
379
  const isJs = type === 'js';
385
- 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+(["'])/g;
380
+ const pattern = isJs ? /\bimport\(/g : !isHtml ? /\burl\(|@import\s+(["'])/gi : /(?:\burl\(|@import\s+(["'])|\b(href)\s*=)/gi;
386
381
  let length = source.length, modified, match;
387
382
  while (match = pattern.exec(source)) {
388
383
  const isText = !!match[1];
@@ -390,227 +385,229 @@ function transformURL(host, parent, type, source, importFile) {
390
385
  const isUrl = !isText && !isHref;
391
386
  const startIndex = match.index;
392
387
  let quote = match[1], content = '', trailing = '', closed = -1, i = startIndex + match[0].length;
393
- for (let j = -1; i < length; ++i) {
394
- const ch = source[i];
395
- if (quote && ch === quote) {
396
- if (!isText) {
397
- content += ch;
398
- }
399
- if (isHref) {
400
- break;
401
- }
402
- if (source[i - 1] !== '\\') {
403
- if (isText) {
388
+ pass: {
389
+ for (let j = -1; i < length; ++i) {
390
+ const ch = source[i];
391
+ if (quote && ch === quote) {
392
+ if (!isText) {
393
+ content += ch;
394
+ }
395
+ if (isHref) {
404
396
  break;
405
397
  }
406
- if (closed === -1) {
407
- closed = i + 1;
398
+ if (source[i - 1] !== '\\') {
399
+ if (isText) {
400
+ break;
401
+ }
402
+ if (closed === -1) {
403
+ closed = i + 1;
404
+ }
408
405
  }
406
+ j = i + 1;
407
+ continue;
409
408
  }
410
- j = i + 1;
411
- continue;
412
- }
413
- if (!quote && (ch === '"' || ch === "'" || ch === '`' && isJs) && (isHref || !content.trim())) {
414
- quote = ch;
415
- }
416
- else if (isHref && !quote && isSpace(ch) && content.trim()) {
417
- break;
418
- }
419
- else if (ch === ')') {
420
- if (isUrl) {
421
- if (quote && closed !== -1 || !quote && (isJs || source[i - 1] !== '\\')) {
422
- break;
409
+ if (!quote && (ch === '"' || ch === "'" || ch === '`' && isJs) && (isHref || !content.trim())) {
410
+ quote = ch;
411
+ }
412
+ else if (isHref && (ch === '#' || !quote && isSpace(ch)) && content.trim()) {
413
+ break pass;
414
+ }
415
+ else if (ch === ')') {
416
+ if (isUrl) {
417
+ if (quote && closed !== -1 || !quote && (isJs || source[i - 1] !== '\\')) {
418
+ break;
419
+ }
420
+ j = i;
423
421
  }
424
- j = i;
425
422
  }
426
- }
427
- else if (j !== -1 && (ch === '\n' ||
428
- isText && (ch === ';' || ch === '{') ||
429
- isHref && (ch === '=' || ch === '>') ||
430
- isUrl && (ch === ';' ||
431
- !isJs && (!isHtml && (ch === ':' && !isJs || ch === '}') ||
432
- isHtml && (ch === '"' || ch === "'" || ch === ':' || ch === '>'))))) {
433
- if (closed !== -1) {
434
- i = closed;
423
+ else if (j !== -1 && (ch === '\n' ||
424
+ isText && (ch === ';' || ch === '{') ||
425
+ isHref && (ch === '=' || ch === '>') ||
426
+ isUrl && (ch === ';' ||
427
+ !isJs && (!isHtml && (ch === ':' && !isJs || ch === '}') ||
428
+ isHtml && (ch === '"' || ch === "'" || ch === ':' || ch === '>'))))) {
429
+ if (closed !== -1) {
430
+ i = closed;
431
+ }
432
+ else {
433
+ content = content.substring(0, i = j);
434
+ }
435
+ break;
435
436
  }
436
- else {
437
- content = content.substring(0, i = j);
437
+ if (closed === -1) {
438
+ content += ch;
438
439
  }
439
- break;
440
- }
441
- if (closed === -1) {
442
- content += ch;
443
- }
444
- }
445
- const setOutputURL = (value, asset) => {
446
- var _a;
447
- if (!value) {
448
- return;
449
440
  }
450
- if (asset) {
451
- if (!asset.inlineBase64) {
452
- if ((_a = host.Cloud) === null || _a === void 0 ? void 0 : _a.getStorage('upload', asset.cloudStorage)) {
453
- const inlineUrlCloud = asset.inlineUrlCloud || (asset.inlineUrlCloud = (0, types_1.generateUUID)());
454
- (parent.inlineUrlCloudMap || (parent.inlineUrlCloudMap = {}))[inlineUrlCloud] = value;
455
- value = inlineUrlCloud;
456
- }
457
- else if (asset.hash && this.productionRelease) {
458
- const inlineUrl = asset.inlineUrl || (asset.inlineUrl = (0, types_1.generateUUID)());
459
- (parent.inlineUrlMap || (parent.inlineUrlMap = {}))[inlineUrl] = value;
460
- value = inlineUrl;
461
- }
462
- }
463
- const font = findFont(asset.format);
464
- if (font) {
465
- found: {
466
- for (let k = i + 1; k < length; ++k) {
467
- switch (source[k]) {
468
- case ')': {
469
- const component = source.substring(i + 1, k + 1);
470
- const format = /\s+format\([^)]+\)$/i.exec(component);
471
- if (format) {
472
- trailing = component.replace(format[0], ` format("${font}")`);
473
- i = k;
474
- break found;
441
+ const setOutputURL = (value, asset) => {
442
+ var _a;
443
+ if (!value) {
444
+ return;
445
+ }
446
+ if (asset) {
447
+ if (!asset.inlineBase64) {
448
+ if ((_a = host.Cloud) === null || _a === void 0 ? void 0 : _a.getStorage('upload', asset.cloudStorage)) {
449
+ const inlineUrlCloud = asset.inlineUrlCloud || (asset.inlineUrlCloud = (0, types_1.generateUUID)());
450
+ (parent.inlineUrlCloudMap || (parent.inlineUrlCloudMap = {}))[inlineUrlCloud] = value;
451
+ value = inlineUrlCloud;
452
+ }
453
+ else if (asset.hash && this.productionRelease) {
454
+ const inlineUrl = asset.inlineUrl || (asset.inlineUrl = (0, types_1.generateUUID)());
455
+ (parent.inlineUrlMap || (parent.inlineUrlMap = {}))[inlineUrl] = value;
456
+ value = inlineUrl;
457
+ }
458
+ }
459
+ const font = findFont(asset.format);
460
+ if (font) {
461
+ found: {
462
+ for (let k = i + 1; k < length; ++k) {
463
+ switch (source[k]) {
464
+ case ')': {
465
+ const component = source.substring(i + 1, k + 1);
466
+ const format = /\s+format\([^)]+\)$/i.exec(component);
467
+ if (format) {
468
+ trailing = component.replace(format[0], ` format("${font}")`);
469
+ i = k;
470
+ break found;
471
+ }
472
+ break;
475
473
  }
476
- break;
474
+ case ',':
475
+ case ';':
476
+ case ':':
477
+ case '}':
478
+ break found;
477
479
  }
478
- case ',':
479
- case ';':
480
- case ':':
481
- case '}':
482
- break found;
483
480
  }
484
481
  }
485
482
  }
483
+ if (asset.initialValue && !asset.initialValue.cacheable) {
484
+ parent.flags |= 16;
485
+ }
486
+ related.push(asset);
486
487
  }
487
- if (asset.initialValue && !asset.initialValue.cacheable) {
488
- parent.flags |= 16;
488
+ if (!quote && !isHtml && !isJs) {
489
+ if (value.indexOf('"') === -1) {
490
+ quote = '"';
491
+ }
492
+ else if (value.indexOf("'") === -1) {
493
+ quote = "'";
494
+ }
489
495
  }
490
- related.push(asset);
491
- }
492
- if (!quote && !isHtml && !isJs) {
493
- if (value.indexOf('"') === -1) {
494
- quote = '"';
496
+ if (isUrl) {
497
+ const index = value.indexOf('#');
498
+ if (index !== -1) {
499
+ value = value.substring(0, index);
500
+ }
495
501
  }
496
- else if (value.indexOf("'") === -1) {
497
- quote = "'";
502
+ if (quote) {
503
+ value = quote + value + quote;
498
504
  }
499
- }
500
- if (isUrl) {
501
- const index = value.indexOf('#');
502
- if (index !== -1) {
503
- value = value.substring(0, index);
505
+ const revised = isJs ? 'import(' + value + trailing + ')' : isHref ? 'href=' + value : (isText ? '@import ' + value : `url(${value})`) + trailing;
506
+ source = spliceString(source, startIndex, i + 1, revised);
507
+ length = source.length;
508
+ pattern.lastIndex = startIndex + revised.length;
509
+ modified = true;
510
+ };
511
+ let url;
512
+ if (isJs) {
513
+ if (!quote) {
514
+ continue;
515
+ }
516
+ const file = new RegExp('^\\s*(' + quote + '((?:[^' + quote + ']|(?<=\\\\)' + quote + ')+)' + quote + ')\\s*(,.+)$').exec(content);
517
+ if (file) {
518
+ url = file[1].trim();
519
+ trailing = file[2].trim();
520
+ }
521
+ else {
522
+ url = trimQuote(content);
504
523
  }
505
- }
506
- if (quote) {
507
- value = quote + value + quote;
508
- }
509
- const revised = isJs ? 'import(' + value + trailing + ')' : isHref ? 'href=' + value : (isText ? '@import ' + value : `url(${value})`) + trailing;
510
- source = spliceString(source, startIndex, i + 1, revised);
511
- length = source.length;
512
- pattern.lastIndex = startIndex + revised.length;
513
- modified = true;
514
- };
515
- let url;
516
- if (isJs) {
517
- if (!quote) {
518
- continue;
519
- }
520
- const file = new RegExp('^\\s*(' + quote + '((?:[^' + quote + ']|(?<=\\\\)' + quote + ')+)' + quote + ')\\s*(,.+)$').exec(content);
521
- if (file) {
522
- url = file[1].trim();
523
- trailing = file[2].trim();
524
524
  }
525
525
  else {
526
526
  url = trimQuote(content);
527
527
  }
528
- }
529
- else {
530
- url = trimQuote(content);
531
- }
532
- if (url.startsWith('data:')) {
533
- const base64 = (_a = url.split(',')[1]) === null || _a === void 0 ? void 0 : _a.trim();
534
- if (base64) {
535
- for (const item of this.assets) {
536
- if (item.base64 === base64) {
537
- setOutputURL(getSrcURL.call(this, parent, item)[0], item);
538
- break;
528
+ if (url.startsWith('data:')) {
529
+ const base64 = (_a = url.split(',')[1]) === null || _a === void 0 ? void 0 : _a.trim();
530
+ if (base64) {
531
+ for (const item of this.assets) {
532
+ if (item.base64 === base64) {
533
+ setOutputURL(getSrcURL.call(this, parent, item)[0], item);
534
+ break;
535
+ }
539
536
  }
540
537
  }
541
538
  }
542
- }
543
- else if (baseHref.length) {
544
- let fullUrl, asset;
545
- for (const parentUrl of baseHref) {
546
- if (asset = host.findAsset(fullUrl = resolvePath(url, parentUrl), { replaced: true }) || (this.related.has(parent) ? host.findAsset(fullUrl, { assets: Array.from(this.related.get(parent)), replaced: true }) : undefined)) {
547
- break;
539
+ else if (baseHref.length) {
540
+ let fullUrl, asset;
541
+ for (const parentUrl of baseHref) {
542
+ if (asset = host.findAsset(fullUrl = resolvePath(url, parentUrl), { replaced: true }) || (this.related.has(parent) ? host.findAsset(fullUrl, { assets: Array.from(this.related.get(parent)), replaced: true }) : undefined)) {
543
+ break;
544
+ }
548
545
  }
549
- }
550
- if (asset && asset !== importFile) {
551
- if (!(0, util_1.hasValue)(asset.format, 'base64')) {
552
- if (asset.bundleReplace) {
553
- let k, valid;
554
- if (!isText) {
555
- let j = startIndex;
556
- while (isSpace(source[--j])) { }
557
- valid = source.substring(k = j - 6, j + 1) === '@import';
558
- }
559
- else {
560
- k = startIndex;
561
- valid = true;
562
- }
563
- if (valid) {
564
- let l = i, revised = transform_1.SourceMap.removeSourceMappingURL(host.getUTF8String(asset))[0];
565
- for (let ch; l < length; ++l) {
566
- if ((ch = source[l]) === ';' || ch === '\n') {
567
- break;
568
- }
569
- }
570
- const layer = this.replaceContent(revised, source.substring(k, l + 1), 'text/css');
571
- if (layer) {
572
- revised = layer;
573
- }
574
- source = spliceString(source, k, l + 1, revised = transformURL.call(this, host, parent, type, this.removeServerRoot(revised), asset) || revised);
575
- length = source.length;
576
- pattern.lastIndex = k + revised.length;
577
- if (!parent.watch) {
578
- host.deleteFile(asset.localUri, { ignorePermission: true, emptyDir: true, all: true, id: asset.id });
579
- asset.invalid = true;
546
+ if (asset && asset !== importFile) {
547
+ if (!(0, util_1.hasValue)(asset.format, 'base64')) {
548
+ if (asset.bundleReplace) {
549
+ let k, valid;
550
+ if (!isText) {
551
+ let j = startIndex;
552
+ while (isSpace(source[--j])) { }
553
+ valid = source.substring(k = j - 6, j + 1).toLowerCase() === '@import';
580
554
  }
581
555
  else {
582
- related.push(asset);
583
- if (!(0, types_1.existsFlag)(asset.flags)) {
584
- asset.flags |= 8;
556
+ k = startIndex;
557
+ valid = true;
558
+ }
559
+ if (valid) {
560
+ let l = i, revised = transform_1.SourceMap.removeSourceMappingURL(host.getUTF8String(asset))[0];
561
+ for (let ch; l < length; ++l) {
562
+ if ((ch = source[l]) === ';' || ch === '\n') {
563
+ break;
564
+ }
565
+ }
566
+ const layer = this.replaceContent(revised, source.substring(k, l + 1), 'text/css');
567
+ if (layer) {
568
+ revised = layer;
569
+ }
570
+ source = spliceString(source, k, l + 1, revised = transformURL.call(this, host, parent, type, this.removeServerRoot(revised), asset) || revised);
571
+ length = source.length;
572
+ pattern.lastIndex = k + revised.length;
573
+ if (!parent.watch) {
574
+ host.deleteFile(asset.localUri, { ignorePermission: true, emptyDir: true, all: true, id: asset.id });
575
+ asset.invalid = true;
576
+ }
577
+ else {
578
+ related.push(asset);
579
+ if (!(0, types_1.existsFlag)(asset.flags)) {
580
+ asset.flags |= 8;
581
+ }
585
582
  }
583
+ modified = true;
584
+ continue;
586
585
  }
587
- modified = true;
588
- continue;
589
586
  }
590
- }
591
- if (hasSameOrigin(asset.url || fullUrl, ...baseHref)) {
592
- let sameDir;
593
- [fullUrl, sameDir] = getSrcURL.call(this, parent, asset);
594
- if (sameDir && isJs) {
595
- fullUrl = './' + fullUrl;
587
+ if (hasSameOrigin(asset.url || fullUrl, ...baseHref)) {
588
+ let sameDir;
589
+ [fullUrl, sameDir] = getSrcURL.call(this, parent, asset);
590
+ if (sameDir && isJs) {
591
+ fullUrl = './' + fullUrl;
592
+ }
593
+ }
594
+ else {
595
+ const pathname = parent.pathname;
596
+ const count = pathname && pathname !== '/' && pathname !== '\\' ? pathname.split(/[\\/]/).length : 0;
597
+ fullUrl = (count ? '../'.repeat(count) : '') + asset.relativeUrl;
596
598
  }
597
599
  }
598
- else {
599
- const pathname = parent.pathname;
600
- const count = pathname && pathname !== '/' && pathname !== '\\' ? pathname.split(/[\\/]/).length : 0;
601
- fullUrl = (count ? '../'.repeat(count) : '') + asset.relativeUrl;
600
+ else if (!asset.invalid) {
601
+ fullUrl = asset.inlineBase64 || (asset.inlineBase64 = (0, types_1.generateUUID)());
602
+ }
603
+ if (url !== fullUrl) {
604
+ setOutputURL(fullUrl, asset);
602
605
  }
603
606
  }
604
- else if (!asset.invalid) {
605
- fullUrl = asset.inlineBase64 || (asset.inlineBase64 = (0, types_1.generateUUID)());
606
- }
607
- if (url !== fullUrl) {
608
- setOutputURL(fullUrl, asset);
607
+ else if (importFile && url !== fullUrl) {
608
+ setOutputURL(fullUrl);
609
609
  }
610
610
  }
611
- else if (importFile && url !== fullUrl) {
612
- setOutputURL(fullUrl);
613
- }
614
611
  }
615
612
  }
616
613
  if (modified) {
@@ -1630,14 +1627,14 @@ class ChromeDocument extends Document {
1630
1627
  }
1631
1628
  }
1632
1629
  replaceContent(source, statement, mimeType) {
1633
- var _a;
1630
+ var _a, _b;
1634
1631
  switch (mimeType) {
1635
1632
  case 'text/css': {
1636
1633
  const match = REGEXP_REPLACECSS.exec((typeof statement === 'string' ? statement : statement[0]).trim());
1637
1634
  if (match) {
1638
1635
  const layer = match[1];
1639
1636
  const supports = (_a = match[3]) === null || _a === void 0 ? void 0 : _a.trim();
1640
- const query = match[4].trim();
1637
+ const query = match[4].trim().replace(/;+$/, '');
1641
1638
  if (layer || supports || query) {
1642
1639
  const newline = (0, util_1.getNewline)(source);
1643
1640
  let result = supports ? `@supports (${supports}) {` + newline : '';
@@ -1645,8 +1642,8 @@ class ChromeDocument extends Document {
1645
1642
  result += `@media ${query} {` + newline;
1646
1643
  }
1647
1644
  if (layer) {
1648
- let name = match[2];
1649
- result += '@layer ' + (name && (name = name.trim()) ? name + ' ' : '') + `{${newline + source + newline}}` + (query ? newline + '}' : '');
1645
+ const name = (_b = match[2]) === null || _b === void 0 ? void 0 : _b.replace(/\s+/g, '');
1646
+ result += '@layer ' + (name ? name.slice(1, -1) + ' ' : '') + `{${newline + source + newline}}` + (query ? newline + '}' : '');
1650
1647
  }
1651
1648
  else if (query) {
1652
1649
  result += source + newline + '}';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/chrome",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "description": "Chrome document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -20,9 +20,9 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.5.18",
24
- "@e-mc/core": "^0.5.18",
25
- "@e-mc/document": "^0.5.18",
26
- "@e-mc/types": "^0.5.18"
23
+ "@e-mc/cloud": "^0.5.23",
24
+ "@e-mc/core": "^0.5.23",
25
+ "@e-mc/document": "^0.5.23",
26
+ "@e-mc/types": "^0.5.23"
27
27
  }
28
28
  }