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