@pi-r/chrome 0.9.6 → 0.9.7

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 +186 -184
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -56,7 +56,7 @@ const REGEXP_TEMPLATE_LEADING = /\s*\{\{- \s*((?:[^}]|}(?!}))+?)\}\}/g;
56
56
  const REGEXP_TEMPLATE_TRAILING = /\{\{((?:[^}]|}(?!}))+?)\s*? -\}\}\s*/g;
57
57
  const REGEXP_URL = /(?:\burl\(|@import\s+(["'])|\b(href)\s*=)/gi;
58
58
  const REGEXP_URL_JS = /\bimport\(/g;
59
- const REGEXP_URL_SVG = /(?:\burl\(|@import\s+(["'])|\b(href)\s*=)/gi;
59
+ const REGEXP_URL_CSS = /(?:\burl\(|@import\s+(["']))/gi;
60
60
  const REGEXP_IMAGESET = /\b(?:-webkit-)?image-set\(/gi;
61
61
  const REGEXP_IMAGESET_URL = /(?:^|[^(])\s*("(\s*[^)][^"]*)"|'(\s*[^)][^"]*)')\s*(?:,|$|[^)][^,]*,?)/gd;
62
62
  const REGEXP_SANITIZEALL = new RegExp(`(?:\\s+|\\b)data-(?:pathname|filename|intl-locales|(?:use|exclude)-[^=\\s]+)\\s*${dom_1.DomWriter.PATTERN_ATTRVALUE}(?=(?:[^"<]+"[^"]*"|[^'<]+'[^']*')*[^<>]*>)`, 'g');
@@ -397,9 +397,9 @@ function getSrcURL(instance, parent, file) {
397
397
  function transformURL(instance, host, parent, type, source, importFile) {
398
398
  const related = [];
399
399
  const baseHref = getBaseURLs(importFile || parent, instance.baseUrl);
400
- const isHtml = type === 'html';
400
+ const isHtml = type === 'html' || type === 'svg';
401
401
  const isJs = type === 'js';
402
- const pattern = isJs ? REGEXP_URL_JS : type === 'svg' ? REGEXP_URL_SVG : REGEXP_URL;
402
+ const pattern = isJs ? REGEXP_URL_JS : !isHtml ? REGEXP_URL_CSS : REGEXP_URL;
403
403
  pattern.lastIndex = 0;
404
404
  REGEXP_IMAGESET.lastIndex = 0;
405
405
  let length = source.length, modified = false, match;
@@ -428,225 +428,227 @@ function transformURL(instance, host, parent, type, source, importFile) {
428
428
  const isUrl = !isText && !isHref;
429
429
  const startIndex = match.index;
430
430
  let quote = match[1], content = '', trailing = '', closed = -1, i = startIndex + match[0].length;
431
- for (let j = -1; i < length; ++i) {
432
- const ch = source[i];
433
- if (quote && ch === quote) {
434
- if (!isText) {
435
- content += ch;
436
- }
437
- if (isHref) {
438
- break;
439
- }
440
- if (source[i - 1] !== '\\') {
441
- if (isText) {
431
+ pass: {
432
+ for (let j = -1; i < length; ++i) {
433
+ const ch = source[i];
434
+ if (quote && ch === quote) {
435
+ if (!isText) {
436
+ content += ch;
437
+ }
438
+ if (isHref) {
442
439
  break;
443
440
  }
444
- if (closed === -1) {
445
- closed = i + 1;
441
+ if (source[i - 1] !== '\\') {
442
+ if (isText) {
443
+ break;
444
+ }
445
+ if (closed === -1) {
446
+ closed = i + 1;
447
+ }
446
448
  }
449
+ j = i + 1;
450
+ continue;
447
451
  }
448
- j = i + 1;
449
- continue;
450
- }
451
- if (!quote && (ch === '"' || ch === "'" || ch === '`' && isJs) && (isHref || !content.trim())) {
452
- quote = ch;
453
- }
454
- else if (isHref && !quote && isSpace(ch) && content.trim()) {
455
- break;
456
- }
457
- else if (ch === ')') {
458
- if (isUrl) {
459
- if (quote && closed !== -1 || !quote && (isJs || source[i - 1] !== '\\')) {
460
- break;
452
+ if (!quote && (ch === '"' || ch === "'" || ch === '`' && isJs) && (isHref || !content.trim())) {
453
+ quote = ch;
454
+ }
455
+ else if (isHref && (ch === '#' || !quote && isSpace(ch)) && content.trim()) {
456
+ break pass;
457
+ }
458
+ else if (ch === ')') {
459
+ if (isUrl) {
460
+ if (quote && closed !== -1 || !quote && (isJs || source[i - 1] !== '\\')) {
461
+ break;
462
+ }
463
+ j = i;
461
464
  }
462
- j = i;
463
465
  }
464
- }
465
- else if (j !== -1 && (ch === '\n' ||
466
- isText && (ch === ';' || ch === '{') ||
467
- isHref && (ch === '=' || ch === '>') ||
468
- isUrl && (ch === ';' ||
469
- !isJs && (!isHtml && (ch === ':' && !isJs || ch === '}') ||
470
- isHtml && (ch === '"' || ch === "'" || ch === ':' || ch === '>'))))) {
471
- if (closed !== -1) {
472
- i = closed;
466
+ else if (j !== -1 && (ch === '\n' ||
467
+ isText && (ch === ';' || ch === '{') ||
468
+ isHref && (ch === '=' || ch === '>') ||
469
+ isUrl && (ch === ';' ||
470
+ !isJs && (!isHtml && (ch === ':' && !isJs || ch === '}') ||
471
+ isHtml && (ch === '"' || ch === "'" || ch === ':' || ch === '>'))))) {
472
+ if (closed !== -1) {
473
+ i = closed;
474
+ }
475
+ else {
476
+ content = content.substring(0, i = j);
477
+ }
478
+ break;
473
479
  }
474
- else {
475
- content = content.substring(0, i = j);
480
+ if (closed === -1) {
481
+ content += ch;
476
482
  }
477
- break;
478
- }
479
- if (closed === -1) {
480
- content += ch;
481
- }
482
- }
483
- const setOutputURL = (value, asset) => {
484
- if (!value) {
485
- return;
486
483
  }
487
- if (asset) {
488
- if (!asset.inlineBase64) {
489
- if (host.Cloud?.getStorage('upload', asset.cloudStorage)) {
490
- const inlineUrlCloud = asset.inlineUrlCloud ||= (0, node_crypto_1.randomUUID)();
491
- (parent.inlineUrlCloudMap ||= {})[inlineUrlCloud] = value;
492
- value = inlineUrlCloud;
493
- }
494
- else if (asset.hash && instance.productionRelease) {
495
- const inlineUrl = asset.inlineUrl ||= (0, node_crypto_1.randomUUID)();
496
- (parent.inlineUrlMap ||= {})[inlineUrl] = value;
497
- value = inlineUrl;
498
- }
499
- }
500
- const font = findFont(asset.format);
501
- if (font) {
502
- found: {
503
- for (let k = i + 1; k < length; ++k) {
504
- switch (source[k]) {
505
- case ')': {
506
- const component = source.substring(i + 1, k + 1);
507
- const format = /\s+format\([^)]+\)$/i.exec(component);
508
- if (format) {
509
- trailing = component.replace(format[0], ` format("${font}")`);
510
- i = k;
511
- break found;
484
+ const setOutputURL = (value, asset) => {
485
+ if (!value) {
486
+ return;
487
+ }
488
+ if (asset) {
489
+ if (!asset.inlineBase64) {
490
+ if (host.Cloud?.getStorage('upload', asset.cloudStorage)) {
491
+ const inlineUrlCloud = asset.inlineUrlCloud ||= (0, node_crypto_1.randomUUID)();
492
+ (parent.inlineUrlCloudMap ||= {})[inlineUrlCloud] = value;
493
+ value = inlineUrlCloud;
494
+ }
495
+ else if (asset.hash && instance.productionRelease) {
496
+ const inlineUrl = asset.inlineUrl ||= (0, node_crypto_1.randomUUID)();
497
+ (parent.inlineUrlMap ||= {})[inlineUrl] = value;
498
+ value = inlineUrl;
499
+ }
500
+ }
501
+ const font = findFont(asset.format);
502
+ if (font) {
503
+ found: {
504
+ for (let k = i + 1; k < length; ++k) {
505
+ switch (source[k]) {
506
+ case ')': {
507
+ const component = source.substring(i + 1, k + 1);
508
+ const format = /\s+format\([^)]+\)$/i.exec(component);
509
+ if (format) {
510
+ trailing = component.replace(format[0], ` format("${font}")`);
511
+ i = k;
512
+ break found;
513
+ }
514
+ break;
512
515
  }
513
- break;
516
+ case ',':
517
+ case ';':
518
+ case ':':
519
+ case '}':
520
+ break found;
514
521
  }
515
- case ',':
516
- case ';':
517
- case ':':
518
- case '}':
519
- break found;
520
522
  }
521
523
  }
522
524
  }
525
+ if (asset.initialValue && !asset.initialValue.cacheable) {
526
+ parent.flags |= 16;
527
+ }
528
+ related.push(asset);
523
529
  }
524
- if (asset.initialValue && !asset.initialValue.cacheable) {
525
- parent.flags |= 16;
530
+ if (!quote && !isHtml && !isJs) {
531
+ if (!value.includes('"')) {
532
+ quote = '"';
533
+ }
534
+ else if (!value.includes("'")) {
535
+ quote = "'";
536
+ }
526
537
  }
527
- related.push(asset);
528
- }
529
- if (!quote && !isHtml && !isJs) {
530
- if (!value.includes('"')) {
531
- quote = '"';
538
+ if (isUrl) {
539
+ const index = value.indexOf('#');
540
+ if (index !== -1) {
541
+ value = value.substring(0, index);
542
+ }
532
543
  }
533
- else if (!value.includes("'")) {
534
- quote = "'";
544
+ if (quote) {
545
+ value = quote + value + quote;
535
546
  }
536
- }
537
- if (isUrl) {
538
- const index = value.indexOf('#');
539
- if (index !== -1) {
540
- value = value.substring(0, index);
547
+ const revised = isJs ? 'import(' + value + trailing + ')' : isHref ? 'href=' + value : (isText ? '@import ' + value : `url(${value})`) + trailing;
548
+ source = (0, util_1.spliceString)(source, startIndex, i + 1, revised, pattern);
549
+ length = source.length;
550
+ modified = true;
551
+ };
552
+ let url;
553
+ if (isJs) {
554
+ if (!quote) {
555
+ continue;
556
+ }
557
+ const file = new RegExp('^\\s*(' + quote + '((?:[^' + quote + ']|(?<=\\\\)' + quote + ')+)' + quote + ')\\s*(,.+)$').exec(content);
558
+ if (file) {
559
+ url = file[1].trim();
560
+ trailing = file[2].trim();
561
+ }
562
+ else {
563
+ url = trimQuote(content);
541
564
  }
542
- }
543
- if (quote) {
544
- value = quote + value + quote;
545
- }
546
- const revised = isJs ? 'import(' + value + trailing + ')' : isHref ? 'href=' + value : (isText ? '@import ' + value : `url(${value})`) + trailing;
547
- source = (0, util_1.spliceString)(source, startIndex, i + 1, revised, pattern);
548
- length = source.length;
549
- modified = true;
550
- };
551
- let url;
552
- if (isJs) {
553
- if (!quote) {
554
- continue;
555
- }
556
- const file = new RegExp('^\\s*(' + quote + '((?:[^' + quote + ']|(?<=\\\\)' + quote + ')+)' + quote + ')\\s*(,.+)$').exec(content);
557
- if (file) {
558
- url = file[1].trim();
559
- trailing = file[2].trim();
560
565
  }
561
566
  else {
562
567
  url = trimQuote(content);
563
568
  }
564
- }
565
- else {
566
- url = trimQuote(content);
567
- }
568
- if (url.startsWith('data:')) {
569
- const base64 = url.split(',')[1]?.trim();
570
- if (base64) {
571
- for (const item of instance.assets) {
572
- if (item.base64 === base64) {
573
- setOutputURL(getSrcURL(instance, parent, item)[0], item);
574
- break;
569
+ if (url.startsWith('data:')) {
570
+ const base64 = url.split(',')[1]?.trim();
571
+ if (base64) {
572
+ for (const item of instance.assets) {
573
+ if (item.base64 === base64) {
574
+ setOutputURL(getSrcURL(instance, parent, item)[0], item);
575
+ break;
576
+ }
575
577
  }
576
578
  }
577
579
  }
578
- }
579
- else if (baseHref.length > 0) {
580
- let fullUrl, asset;
581
- for (const parentUrl of baseHref) {
582
- if (asset = host.findAsset(fullUrl = Document.resolvePath(url, parentUrl), { replaced: true }) || (instance.related.has(parent) ? host.findAsset(fullUrl, { assets: Array.from(instance.related.get(parent)), replaced: true }) : undefined)) {
583
- break;
580
+ else if (baseHref.length > 0) {
581
+ let fullUrl, asset;
582
+ for (const parentUrl of baseHref) {
583
+ if (asset = host.findAsset(fullUrl = Document.resolvePath(url, parentUrl), { replaced: true }) || (instance.related.has(parent) ? host.findAsset(fullUrl, { assets: Array.from(instance.related.get(parent)), replaced: true }) : undefined)) {
584
+ break;
585
+ }
584
586
  }
585
- }
586
- if (asset && asset !== importFile) {
587
- if (!(0, util_1.hasValue)(asset.format, 'base64')) {
588
- if (asset.bundleReplace) {
589
- let k = 0, valid;
590
- if (!isText) {
591
- let j = startIndex;
592
- while (isSpace(source[--j])) { }
593
- valid = source.substring(k = j - 6, j + 1).toLowerCase() === '@import';
594
- }
595
- else {
596
- k = startIndex;
597
- valid = true;
598
- }
599
- if (valid) {
600
- let l = i, revised = transform_1.SourceMap.removeSourceMappingURL(host.getUTF8String(asset))[0];
601
- for (let ch; l < length; ++l) {
602
- if ((ch = source[l]) === ';' || ch === '\n') {
603
- break;
604
- }
605
- }
606
- const layer = instance.replaceContent(revised, source.substring(k, l + 1), "text/css");
607
- if (layer) {
608
- revised = layer;
609
- }
610
- revised = transformURL(instance, host, parent, type, instance.removeServerRoot(revised), asset) || revised;
611
- source = (0, util_1.spliceString)(source, k, l + 1, revised, pattern);
612
- length = source.length;
613
- if (!parent.watch) {
614
- host.deleteFile(asset.localUri, { ignorePermission: true, emptyDir: true, all: true, id: asset.id });
615
- asset.invalid = true;
587
+ if (asset && asset !== importFile) {
588
+ if (!(0, util_1.hasValue)(asset.format, 'base64')) {
589
+ if (asset.bundleReplace) {
590
+ let k = 0, valid;
591
+ if (!isText) {
592
+ let j = startIndex;
593
+ while (isSpace(source[--j])) { }
594
+ valid = source.substring(k = j - 6, j + 1).toLowerCase() === '@import';
616
595
  }
617
596
  else {
618
- related.push(asset);
619
- if (!(0, types_1.existsFlag)(asset.flags)) {
620
- asset.flags |= 8;
597
+ k = startIndex;
598
+ valid = true;
599
+ }
600
+ if (valid) {
601
+ let l = i, revised = transform_1.SourceMap.removeSourceMappingURL(host.getUTF8String(asset))[0];
602
+ for (let ch; l < length; ++l) {
603
+ if ((ch = source[l]) === ';' || ch === '\n') {
604
+ break;
605
+ }
606
+ }
607
+ const layer = instance.replaceContent(revised, source.substring(k, l + 1), "text/css");
608
+ if (layer) {
609
+ revised = layer;
621
610
  }
611
+ revised = transformURL(instance, host, parent, type, instance.removeServerRoot(revised), asset) || revised;
612
+ source = (0, util_1.spliceString)(source, k, l + 1, revised, pattern);
613
+ length = source.length;
614
+ if (!parent.watch) {
615
+ host.deleteFile(asset.localUri, { ignorePermission: true, emptyDir: true, all: true, id: asset.id });
616
+ asset.invalid = true;
617
+ }
618
+ else {
619
+ related.push(asset);
620
+ if (!(0, types_1.existsFlag)(asset.flags)) {
621
+ asset.flags |= 8;
622
+ }
623
+ }
624
+ modified = true;
625
+ continue;
622
626
  }
623
- modified = true;
624
- continue;
625
627
  }
626
- }
627
- if (hasSameOrigin(asset.url || fullUrl, ...baseHref)) {
628
- let sameDir = false;
629
- [fullUrl, sameDir] = getSrcURL(instance, parent, asset);
630
- if (sameDir && isJs) {
631
- fullUrl = './' + fullUrl;
628
+ if (hasSameOrigin(asset.url || fullUrl, ...baseHref)) {
629
+ let sameDir = false;
630
+ [fullUrl, sameDir] = getSrcURL(instance, parent, asset);
631
+ if (sameDir && isJs) {
632
+ fullUrl = './' + fullUrl;
633
+ }
634
+ }
635
+ else {
636
+ const pathname = parent.pathname;
637
+ const count = pathname && pathname !== '/' ? pathname.split('/').length : 0;
638
+ fullUrl = (count ? '../'.repeat(count) : '') + asset.relativeUrl;
632
639
  }
633
640
  }
634
- else {
635
- const pathname = parent.pathname;
636
- const count = pathname && pathname !== '/' ? pathname.split('/').length : 0;
637
- fullUrl = (count ? '../'.repeat(count) : '') + asset.relativeUrl;
641
+ else if (!asset.invalid) {
642
+ fullUrl = asset.inlineBase64 ||= (0, node_crypto_1.randomUUID)();
643
+ }
644
+ if (url !== fullUrl) {
645
+ setOutputURL(fullUrl, asset);
638
646
  }
639
647
  }
640
- else if (!asset.invalid) {
641
- fullUrl = asset.inlineBase64 ||= (0, node_crypto_1.randomUUID)();
642
- }
643
- if (url !== fullUrl) {
644
- setOutputURL(fullUrl, asset);
648
+ else if (importFile && url !== fullUrl) {
649
+ setOutputURL(fullUrl);
645
650
  }
646
651
  }
647
- else if (importFile && url !== fullUrl) {
648
- setOutputURL(fullUrl);
649
- }
650
652
  }
651
653
  }
652
654
  if (modified) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/chrome",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "Chrome document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -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.11.18",
24
- "@e-mc/core": "^0.11.18",
25
- "@e-mc/document": "^0.11.18",
26
- "@e-mc/types": "^0.11.18"
23
+ "@e-mc/cloud": "^0.11.19",
24
+ "@e-mc/core": "^0.11.19",
25
+ "@e-mc/document": "^0.11.19",
26
+ "@e-mc/types": "^0.11.19"
27
27
  }
28
28
  }