@percy/dom 1.32.13-beta.1 → 1.32.13-beta.2

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/dist/bundle.js +39 -21
  2. package/package.json +2 -2
package/dist/bundle.js CHANGED
@@ -434,7 +434,6 @@
434
434
  dom,
435
435
  clone,
436
436
  resources,
437
- cache,
438
437
  warnings,
439
438
  ignoreStyleSheetSerializationErrors,
440
439
  styleSheetClones
@@ -496,12 +495,34 @@
496
495
  }
497
496
  }
498
497
  }
498
+ serializeAdoptedStyleSheets(ctx);
499
+ }
499
500
 
500
- // clone Adopted Stylesheets
501
+ // Serializes constructed stylesheets adopted by a document or shadow root into <link> tags
502
+ // backed by CSS resources. Unlike the rest of the CSSOM, this also runs when JavaScript is
503
+ // enabled: adopted sheets have no DOM representation, so without this they are lost whenever
504
+ // the page's own JS doesn't re-adopt them at render time.
505
+ function serializeAdoptedStyleSheets(ctx) {
506
+ let {
507
+ dom,
508
+ clone,
509
+ resources,
510
+ cache,
511
+ warnings
512
+ } = ctx;
501
513
  // Regarding ordering of the adopted stylesheets - https://github.com/WICG/construct-stylesheets/issues/93
502
514
  /* istanbul ignore next: tested, but coverage is stripped */
503
515
  if (dom.adoptedStyleSheets) {
516
+ // Document and iframe clones are fragments carrying body/head/documentElement references; links
517
+ // go at the start of the body, else the head, else inside documentElement (the only part that is
518
+ // serialized). A shadow root has none of these and takes the links directly.
519
+ let container = clone && ['HTMLDocument', 'DocumentFragment', 'ShadowRoot'].includes(clone.constructor.name) ? clone.body || clone.head || clone.documentElement || clone : null;
520
+ // each link goes after the one before it, so the links keep the order the sheets were adopted in
521
+ let prevLink = null;
504
522
  for (let sheet of dom.adoptedStyleSheets) {
523
+ var _sheet$media;
524
+ // a disabled sheet doesn't apply on the live page, so it must not apply in the render
525
+ if (sheet.disabled) continue;
505
526
  const styleLink = document.createElement('link');
506
527
  styleLink.setAttribute('rel', 'stylesheet');
507
528
  if (!cache.has(sheet)) {
@@ -511,26 +532,15 @@
511
532
  }
512
533
  styleLink.setAttribute('data-percy-adopted-stylesheets-serialized', 'true');
513
534
  styleLink.setAttribute('data-percy-serialized-attribute-href', cache.get(sheet));
514
-
515
- /* istanbul ignore next: tested, but coverage is stripped */
516
- if (clone.constructor.name === 'HTMLDocument' || clone.constructor.name === 'DocumentFragment') {
517
- // handle document and iframe
518
- // We are checking if we have multiple stylesheets present for the same clone or clone.body then we add
519
- // them in the same order in which we receive them.
520
- const lastLink = clone.body.querySelector('link[data-percy-adopted-stylesheets-serialized]:last-of-type');
521
- if (lastLink) {
522
- lastLink.after(styleLink);
523
- } else {
524
- clone.body.prepend(styleLink);
525
- }
526
- } else if (clone.constructor.name === 'ShadowRoot') {
527
- const lastLink = clone.querySelector('link[data-percy-adopted-stylesheets-serialized]:last-of-type');
528
- if (lastLink) {
529
- lastLink.after(styleLink);
530
- } else {
531
- clone.prepend(styleLink);
532
- }
535
+ // a constructed sheet can carry a media condition (new CSSStyleSheet({ media })); keep it
536
+ if ((_sheet$media = sheet.media) !== null && _sheet$media !== void 0 && _sheet$media.mediaText) styleLink.setAttribute('media', sheet.media.mediaText);
537
+ if (!container) continue;
538
+ if (prevLink) {
539
+ prevLink.after(styleLink);
540
+ } else {
541
+ container.prepend(styleLink);
533
542
  }
543
+ prevLink = styleLink;
534
544
  }
535
545
  } else {
536
546
  warnings.add('Skipping `adoptedStyleSheets` as it is not supported.');
@@ -1641,6 +1651,14 @@
1641
1651
  if (!ctx.enableJavaScript) {
1642
1652
  serializeCSSOM(ctx);
1643
1653
  serializeCanvas(ctx);
1654
+ } else {
1655
+ // adopted stylesheets exist only in memory, so they must be captured even with JS enabled.
1656
+ // This path is new for JS-enabled pages, so a failure here must not fail the snapshot.
1657
+ try {
1658
+ serializeAdoptedStyleSheets(ctx);
1659
+ } catch (err) {
1660
+ ctx.warnings.add(`Error serializing \`adoptedStyleSheets\`, some may be missing: ${err.message}`);
1661
+ }
1644
1662
  }
1645
1663
  // Only process shadow hosts if forceShadowAsLightDOM is not enabled
1646
1664
  if (!ctx.forceShadowAsLightDOM) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/dom",
3
- "version": "1.32.13-beta.1",
3
+ "version": "1.32.13-beta.2",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,5 +35,5 @@
35
35
  "devDependencies": {
36
36
  "interactor.js": "^2.0.0-beta.10"
37
37
  },
38
- "gitHead": "ce1817a2a31d00c29821e26af150e985d5ba8bf6"
38
+ "gitHead": "dcca431a742a923312777825e05af9ac01706213"
39
39
  }