@cronn/element-snapshot 0.31.0 → 0.32.1

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.
@@ -22,7 +22,8 @@
22
22
  function resolveElementReference(element, attributeName) {
23
23
  const referenceId = element.getAttribute(attributeName);
24
24
  if (referenceId === null) return null;
25
- return element.ownerDocument.getElementById(referenceId);
25
+ const normalizedReferenceId = referenceId.trim();
26
+ return element.ownerDocument.getElementById(normalizedReferenceId);
26
27
  }
27
28
  //#endregion
28
29
  //#region src/utils/factories.ts
@@ -451,7 +452,7 @@
451
452
  function resolveLabelName(element) {
452
453
  if (element.id !== null) {
453
454
  const referencedElement = element.ownerDocument.querySelector(attributeSelector("for", element.id));
454
- if (referencedElement !== null) return resolveAccessibleTextContent(referencedElement);
455
+ if (referencedElement !== null) return resolveAccessibleTextContent(referencedElement, [element]);
455
456
  }
456
457
  const closestLabel = element.closest("label");
457
458
  if (closestLabel !== null) return resolveAccessibleTextContent(closestLabel, [element]);
@@ -515,6 +516,11 @@
515
516
  };
516
517
  }
517
518
  //#endregion
519
+ //#region src/utils/array.ts
520
+ function unwrapSingleItem(items) {
521
+ return items.length === 1 ? items.at(0) : items;
522
+ }
523
+ //#endregion
518
524
  //#region src/browser/description.ts
519
525
  function discribableAttributes(element) {
520
526
  return { description: resolveDescription(element) };
@@ -600,8 +606,7 @@
600
606
  if (!(element instanceof HTMLSelectElement)) return resolveAccessibleTextContent(element);
601
607
  const selectedLabels = Array.from(element.selectedOptions).map((option) => option.label);
602
608
  if (selectedLabels.length === 0) return;
603
- if (selectedLabels.length === 1) return selectedLabels.at(0);
604
- return selectedLabels;
609
+ return unwrapSingleItem(selectedLabels);
605
610
  }
606
611
  function snapshotOptions(element) {
607
612
  const optionsContainer = resolveOptionsContainer(element);
package/dist/index.mjs CHANGED
@@ -96,6 +96,11 @@ var ElementSnapshotProxy = class ElementSnapshotProxy {
96
96
  }
97
97
  };
98
98
  //#endregion
99
+ //#region src/utils/array.ts
100
+ function unwrapSingleItem(items) {
101
+ return items.length === 1 ? items.at(0) : items;
102
+ }
103
+ //#endregion
99
104
  //#region src/transformers/semantic-snapshot-transformer.ts
100
105
  function semanticSnapshotTransformer(snapshot, context) {
101
106
  if (snapshot.role === "text") return snapshot.name;
@@ -113,12 +118,12 @@ function transformElementSnapshot(snapshot, transformRecursive) {
113
118
  }
114
119
  if (hasOnlyChildren(normalizedSnapshot)) {
115
120
  const { role, children } = normalizedSnapshot;
116
- return transformedSnapshot(role, children.length === 1 ? children.at(0) : children);
121
+ return transformedSnapshot(role, unwrapSingleItem(children));
117
122
  }
118
123
  const { role, name, attributes, children } = normalizedSnapshot;
119
124
  return transformedSnapshot(role, name, {
120
125
  ...attributes,
121
- children: children.length === 0 ? void 0 : children
126
+ children: isEmpty(children) ? void 0 : unwrapSingleItem(children)
122
127
  });
123
128
  }
124
129
  function normalizeElementSnapshot(snapshot, transformRecursive) {
@@ -195,9 +200,7 @@ var SnapshotSerializer = class {
195
200
  this.defaultTransformer = options.defaultTransformer ?? semanticSnapshotTransformer;
196
201
  }
197
202
  transform(snapshots) {
198
- const transformedSnapshots = this.filterSnapshots(snapshots).map(this.transformSnapshotRecursive);
199
- if (transformedSnapshots.length === 1) return transformedSnapshots.at(0);
200
- return transformedSnapshots;
203
+ return unwrapSingleItem(this.filterSnapshots(snapshots).map(this.transformSnapshotRecursive));
201
204
  }
202
205
  filterSnapshots(snapshots) {
203
206
  if (this.filter === void 0) return snapshots;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cronn/element-snapshot",
3
- "version": "0.31.0",
3
+ "version": "0.32.1",
4
4
  "description": "Element snapshots for Playwright",
5
5
  "homepage": "https://cronn.github.io/file-snapshots/playwright/element-snapshots/",
6
6
  "bugs": {