@softure-ai/marketing-kit 0.1.11 → 0.1.12

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.
@@ -126,10 +126,39 @@ async function measureElement(page: Page, descriptor: LocatorDescriptor, key: st
126
126
  return { ok: true, rect };
127
127
  }
128
128
 
129
+ /** Rounding of the stretched element's edge against the frame's, in CSS pixels. */
130
+ const FILL_TOLERANCE_PX = 1;
131
+
132
+ /**
133
+ * `crop.fill`: stretches a target shorter than its frame (minus the top padding) to the frame's height and centres its
134
+ * content vertically, with `!important` inline styles in this browser only.
135
+ */
136
+ async function stretchToFrame(page: Page, crop: Crop, element: Rect): Promise<void> {
137
+ const frameHeight = Math.round(((element.width + 2 * crop.padding) * crop.aspect.height) / crop.aspect.width);
138
+ const height = frameHeight - crop.padding;
139
+ if (element.height >= height) return;
140
+ await getLocator(page, crop.target).evaluate((node, minHeight) => {
141
+ if (!(node instanceof HTMLElement)) return;
142
+ const styles: [string, string][] = [
143
+ ["box-sizing", "border-box"],
144
+ ["min-height", `${minHeight}px`],
145
+ ["display", "flex"],
146
+ ["flex-direction", "column"],
147
+ ["justify-content", "center"],
148
+ ];
149
+ for (const [property, value] of styles) node.style.setProperty(property, value, "important");
150
+ }, height);
151
+ }
152
+
129
153
  /** The crop's frame from its element (and `crop.top`'s top edge) and the page's scrollable size. */
130
154
  async function measureCrop(page: Page, crop: Crop): Promise<CropMeasure> {
131
- const element = await measureElement(page, crop.target, "crop.target");
155
+ let element = await measureElement(page, crop.target, "crop.target");
132
156
  if (!element.ok) return element;
157
+ if (crop.fill) {
158
+ await stretchToFrame(page, crop, element.rect);
159
+ element = await measureElement(page, crop.target, "crop.target");
160
+ if (!element.ok) return element;
161
+ }
133
162
  let top: number | undefined;
134
163
  if (crop.top !== undefined) {
135
164
  const topElement = await measureElement(page, crop.top, "crop.top");
@@ -137,7 +166,11 @@ async function measureCrop(page: Page, crop: Crop): Promise<CropMeasure> {
137
166
  top = topElement.rect.y;
138
167
  }
139
168
  const size = await page.evaluate(() => ({ width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight }));
140
- return findCropFrame({ element: element.rect, ...(top === undefined ? {} : { top }), page: size, aspect: crop.aspect, padding: crop.padding });
169
+ const frame = findCropFrame({ element: element.rect, ...(top === undefined ? {} : { top }), page: size, aspect: crop.aspect, padding: crop.padding });
170
+ if (!frame.ok || !crop.fill) return frame;
171
+ const gap = Math.round(frame.frame.y + frame.frame.height - (element.rect.y + element.rect.height));
172
+ if (gap > FILL_TOLERANCE_PX) return { ok: false, message: `crop.fill left crop.target ${gap} px short of the frame's bottom edge (it is not an HTML element, or a script reset its style)` };
173
+ return frame;
141
174
  }
142
175
 
143
176
  /**
@@ -7,7 +7,7 @@ import { getLocator } from "../record/locator.js";
7
7
  * Steps of the sign-in and of a screenshot entry, done in order on the page. A step that cannot be done (its
8
8
  * element never appears, matches several, is not an input, is not a `<details>`) is the page's or the config's
9
9
  * fault, so it comes back as a message naming the step; the message never carries a `fill` value, which may be a
10
- * password. `open` and `hide` act on every match of their target; the others need exactly one.
10
+ * password. `open`, `hide` and `flatten` act on every match of their target; the others need exactly one.
11
11
  */
12
12
 
13
13
  /** Long enough for a client-side page to render the element, short enough that a typo does not hang the run. */
@@ -32,6 +32,8 @@ async function runStep(page: Page, step: ShotStep): Promise<void> {
32
32
  return openEvery(getLocator(page, step.target));
33
33
  case "hide":
34
34
  return hideAllButLast(getLocator(page, step.target), step.keepLast);
35
+ case "flatten":
36
+ return flattenEvery(getLocator(page, step.target));
35
37
  }
36
38
  }
37
39
 
@@ -71,6 +73,18 @@ async function hideAllButLast(locator: Locator, keepLast: number): Promise<void>
71
73
  if (total <= keepLast) throw new Error(`matches ${total} elements, not more than keepLast ${keepLast}, so nothing would be hidden`);
72
74
  }
73
75
 
76
+ /** Drops the top border and top margin of every match, with `!important` like `hide`. */
77
+ async function flattenEvery(locator: Locator): Promise<void> {
78
+ await waitForMatches(locator);
79
+ await locator.evaluateAll((nodes) => {
80
+ for (const node of nodes) {
81
+ if (!(node instanceof HTMLElement || node instanceof SVGElement)) continue;
82
+ node.style.setProperty("border-top", "none", "important");
83
+ node.style.setProperty("margin-top", "0", "important");
84
+ }
85
+ });
86
+ }
87
+
74
88
  /** Null when every step was done; otherwise which one failed (`<where>[<index>] (<do>)`) and why. */
75
89
  export async function runShotSteps(page: Page, steps: readonly ShotStep[], where: string): Promise<string | null> {
76
90
  for (const [index, step] of steps.entries()) {