@pi-r/chrome 0.7.6 → 0.7.8

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