Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(branding): see buttons behind entrance animations and page fades, read the label's text color (#5554)

Abimael Martell136cdc890cfa
diff --git a/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts b/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts
index 9554540d9..1da77c0ab 100644
--- a/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts
+++ b/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts
@@ -5,9 +5,9 @@ import { getBrandingScript } from "../../../scraper/scrapeURL/engines/fire-engin
// jsdom has no layout: elements get a box from data-top / data-w / data-h, and
// anything without data-top has an empty box (not visible).
-function scan(body: string, scrollY = 0, innerHeight = 1080) {
+function scan(body: string, scrollY = 0, innerHeight = 1080, bodyAttrs = "") {
const dom = new JSDOM(
- `<html><head><title>Acme</title></head><body>${body}</body></html>`,
+ `<html><head><title>Acme</title></head><body ${bodyAttrs}>${body}</body></html>`,
{ runScripts: "outside-only", url: "https://acme.test/" },
);
const window = dom.window as any;
@@ -47,7 +47,7 @@ function scan(body: string, scrollY = 0, innerHeight = 1080) {
isNavigation: boolean;
visible: boolean;
position: { top: number };
- colors: { background: string };
+ colors: { background: string; text: string };
}>;
}
@@ -93,6 +93,104 @@ describe("branding script button detection", () => {
expect(link?.colors.background).toBe("rgb(34, 34, 34)");
});
+ it("keeps a call to action that is only faded out until it scrolls into view", () => {
+ const cta = (text: string) =>
+ `<a href="/go" data-top="900" style="background-color: rgb(0, 97, 254); color: rgb(255, 255, 255); padding: 10px 20px; border-radius: 6px">${text}</a>`;
+ const snaps = scan(
+ // Wix: a box animated in by CSS, sitting at opacity 0 until then.
+ `<div style="opacity: 0; animation-name: motion-fadeIn; animation-duration: 1.2s">${cta("Contact us")}</div>
+ <!-- Framer: slid out of place by a motion script, inline opacity 0 with a transform. -->
+ <div style="will-change: transform; opacity: 0; transform: translateY(44px)">${cta("Book a call")}</div>
+ <!-- Shopify theme: a reveal class the page's script flips on scroll. -->
+ <section class="tc-row js-reveal" style="opacity: 0">${cta("Shop rugs")}</section>
+ <!-- Elementor: visibility hidden until its entrance animation. -->
+ <div class="elementor-element elementor-invisible" style="visibility: hidden">${cta("Get a quote")}</div>`,
+ );
+
+ for (const text of ["Contact us", "Book a call", "Shop rugs", "Get a quote"]) {
+ expect(byText(snaps, text), text).toMatchObject({
+ isButton: true,
+ visible: true,
+ });
+ }
+ });
+
+ it("still leaves out closed menus, inactive slides and plain transparent boxes", () => {
+ const cta = (text: string) =>
+ `<a href="/go" data-top="300" style="background-color: rgb(0, 97, 254); color: rgb(255, 255, 255); padding: 10px 20px; border-radius: 6px">${text}</a>`;
+ const snaps = scan(
+ `<div style="opacity: 0; position: absolute; transition-property: opacity; transition-duration: 0.3s">${cta("Menu item")}</div>
+ <!-- A closed accordion in normal flow fades too: a transition is not an entrance. -->
+ <div style="opacity: 0; transition-property: opacity; transition-duration: 0.3s">${cta("Accordion item")}</div>
+ <div style="opacity: 0; will-change: transform">${cta("Will change only")}</div>
+ <div style="opacity: 0; position: relative" aria-hidden="true">${cta("Slide two")}</div>
+ <div style="opacity: 0">${cta("Just transparent")}</div>
+ <div style="opacity: 0; position: fixed; transform: translateY(-100%); pointer-events: none">${cta("Filter bar")}</div>
+ <!-- A dialog closed for good, with an entrance-animated box inside it. -->
+ <div style="visibility: hidden"><div class="elementor-invisible" style="visibility: hidden">${cta("Inside closed dialog")}</div></div>`,
+ );
+
+ // Links are sampled for typography regardless, so they appear in the
+ // snapshots; what matters is that none of them counts as visible.
+ for (const text of [
+ "Menu item",
+ "Accordion item",
+ "Will change only",
+ "Slide two",
+ "Just transparent",
+ "Filter bar",
+ "Inside closed dialog",
+ ]) {
+ expect(byText(snaps, text)?.visible, text).toBe(false);
+ }
+ });
+
+ it("ignores a load-time fade on the page itself", () => {
+ const snaps = scan(
+ `<button data-top="400" style="background-color: rgb(83, 58, 253); color: rgb(255, 255, 255)">Start free trial</button>`,
+ 0,
+ 1080,
+ 'style="opacity: 0"',
+ );
+
+ expect(byText(snaps, "Start free trial")).toMatchObject({
+ isButton: true,
+ visible: true,
+ });
+ });
+
+ it("reports the label's color when the button's own color is only inherited", () => {
+ const snaps = scan(
+ `<button data-top="500" style="background-color: rgb(0, 0, 0); color: rgb(0, 0, 0)"><span style="color: rgb(255, 255, 255)">Subscribe Now</span></button>
+ <a href="/plain" data-top="600" style="background-color: rgb(0, 97, 254); color: rgb(255, 255, 255); padding: 8px 16px; border-radius: 6px">Plain label</a>
+ <button data-top="700" style="background-color: rgb(0, 0, 0); color: rgb(0, 0, 0)"><span style="display: none; color: rgb(255, 0, 0)">Mobile copy</span><span class="sr-only" style="position: absolute; width: 1px; height: 1px; color: rgb(0, 255, 0)">Opens in a new tab</span><span style="color: rgb(255, 255, 255)">Desktop label</span></button>`,
+ );
+
+ expect(byText(snaps, "Subscribe Now")?.colors).toMatchObject({
+ background: "rgb(0, 0, 0)",
+ text: "rgb(255, 255, 255)",
+ });
+ // Hidden copies for other breakpoints or screen readers are passed over.
+ expect(
+ snaps.find(s => s.text.startsWith("Mobile copy"))?.colors.text,
+ ).toBe("rgb(255, 255, 255)");
+ expect(byText(snaps, "Plain label")?.colors.text).toBe("rgb(255, 255, 255)");
+ });
+
+ it("still reads the label inside a button waiting on its entrance animation", () => {
+ const snaps = scan(
+ `<div class="elementor-element elementor-invisible" style="visibility: hidden">
+ <a href="/quote" data-top="800" class="elementor-button" style="background-color: rgb(200, 30, 30); color: rgb(0, 0, 0); padding: 10px 20px; border-radius: 4px"><span class="elementor-button-text" style="color: rgb(255, 255, 255)">Get a quote</span></a>
+ </div>`,
+ );
+
+ expect(byText(snaps, "Get a quote")).toMatchObject({
+ isButton: true,
+ visible: true,
+ colors: { background: "rgb(200, 30, 30)", text: "rgb(255, 255, 255)" },
+ });
+ });
+
it("leaves out hidden copies of buttons", () => {
const snaps = scan(
`<button data-top="300" style="display: none">Hidden menu CTA</button><div data-top="300" style="opacity: 0"><button data-top="300">Faded menu CTA</button></div><button data-top="300">Shown CTA</button>`,
diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts
index 33fa72a19..1a11cf333 100644
--- a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts
+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts
@@ -15,17 +15,82 @@ const isOpaque = (color: string | null | undefined): boolean => {
return alpha > CONSTANTS.MIN_ALPHA_THRESHOLD;
};
+// Classes site builders and reveal scripts put on content that is faded out
+// only until it scrolls into view (Elementor, WOW.js, AOS, animate.css, the
+// "js-reveal" convention), and attributes that mark the same thing.
+const ENTRANCE_CLASS =
+ /(?:^|\s)(?:elementor-invisible|wow|aos-init|animate__animated|[\w-]*reveal[\w-]*|[\w-]*fade-?in[\w-]*)(?:\s|$)/i;
+const ENTRANCE_ATTRS = ["data-aos", "data-framer-appear-id", "data-motion-enter"];
+
+/**
+ * An element at opacity 0 (or visibility hidden) that is only waiting for its
+ * entrance animation: in normal flow, not a dismissed overlay, and either
+ * animated by CSS, slid out of place by a motion script (inline opacity 0 with
+ * a translate), or tagged with a reveal class or attribute. A closed menu,
+ * accordion or dialog is absolutely or fixed positioned, display none, or
+ * aria-hidden, and none of these say it will appear. A transition alone is
+ * not proof either: a closed accordion fades too, so it needs a marker.
+ */
+const isAppearing = (node: Element, cs: CSSStyleDeclaration): boolean => {
+ if (cs.display === "none") return false;
+ if (
+ node.getAttribute("aria-hidden") === "true" ||
+ node.hasAttribute("hidden") ||
+ node.hasAttribute("inert")
+ ) {
+ return false;
+ }
+ const position = cs.position;
+ if (position === "fixed" || position === "absolute") return false;
+ if (cs.pointerEvents === "none") return false;
+
+ const animation = cs.getPropertyValue("animation-name");
+ if (animation && animation !== "none") return true;
+ const inline = (node.getAttribute("style") || "").toLowerCase();
+ if (
+ /opacity:\s*0(?:[;\s]|$)/.test(inline) &&
+ /transform:\s*[^;]*translate/.test(inline)
+ ) {
+ return true;
+ }
+ const className = node.getAttribute("class") || "";
+ if (ENTRANCE_CLASS.test(className)) return true;
+ return ENTRANCE_ATTRS.some(attr => node.hasAttribute(attr));
+};
+
/**
* Rendered, with a size, and not hidden or fully transparent. Opacity doesn't
- * inherit, so a faded-out ancestor (a closed menu) is checked too.
+ * inherit, so a faded-out ancestor (a closed menu) is checked too. Two kinds
+ * of transparency don't count: the page itself at opacity 0 (a load-time fade
+ * on html or body that headless rendering never finishes), and content faded
+ * out only until it scrolls into view, which the scan's tall window never
+ * triggers. Visitors see both.
*/
export const isVisibleElement = (el: Element, rect: DOMRect): boolean => {
if (!(rect.width > 0 && rect.height > 0)) return false;
const cs = getComputedStyleCached(el);
- if (cs.display === "none" || cs.visibility === "hidden") return false;
+ if (cs.display === "none") return false;
+ // visibility inherits: `el` reads hidden when any ancestor is. Each
+ // ancestor that sets it (hidden while its own parent is not) must be
+ // waiting on an entrance animation, as with Elementor's
+ // elementor-invisible; one that merely inherits is not judged, and one
+ // closed for good hides `el` whatever sits between them.
+ const checkVisibility = cs.visibility === "hidden";
let node: Element | null = el;
for (let depth = 0; node && depth < 15; depth++) {
- if (parseFloat(getComputedStyleCached(node).opacity) < 0.05) return false;
+ const nodeStyle = getComputedStyleCached(node);
+ if (nodeStyle.display === "none") return false;
+ if (checkVisibility && nodeStyle.visibility === "hidden") {
+ const parent = node.parentElement;
+ const setsIt =
+ !parent || getComputedStyleCached(parent).visibility !== "hidden";
+ if (setsIt && !isAppearing(node, nodeStyle)) return false;
+ }
+ if (parseFloat(nodeStyle.opacity) < 0.05) {
+ const isPage =
+ node === document.documentElement || node === document.body;
+ if (!isPage && !isAppearing(node, nodeStyle)) return false;
+ }
node = node.parentElement;
}
return true;
diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/elements.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/elements.ts
index 3f6a08f8d..851fe5e35 100644
--- a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/elements.ts
+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/elements.ts
@@ -112,6 +112,67 @@ interface InputMetadata {
label: string;
}
+/**
+ * The color of the element's own text as rendered: site builders (Wix,
+ * Elementor, Framer) put a button's label in an inner span with its own
+ * color, while the button or link itself keeps the page's inherited color.
+ * The first rendered descendant that directly holds text decides; copies
+ * for screen readers or other breakpoints (display none, visibility hidden,
+ * clipped to a pixel) are passed over. The element's own color when it
+ * holds the text itself or has none.
+ */
+export const getLabelColor = (el: Element, cs: CSSStyleDeclaration): string => {
+ try {
+ const hasOwnText = (node: Element): boolean => {
+ for (let i = 0; i < node.childNodes.length; i++) {
+ const child = node.childNodes[i];
+ if (child.nodeType === 3 && (child.textContent || "").trim()) {
+ return true;
+ }
+ }
+ return false;
+ };
+ const rendered = (node: Element, style: CSSStyleDeclaration): boolean => {
+ if (style.display === "none") return false;
+ // visibility inherits: a label inside a button that is itself hidden
+ // (waiting on an entrance animation) is still its label. Only a
+ // descendant that hides itself is passed over.
+ if (style.visibility === "hidden") {
+ const parent = node.parentElement;
+ if (
+ !parent ||
+ getComputedStyleCached(parent).visibility !== "hidden"
+ ) {
+ return false;
+ }
+ }
+ if (parseFloat(style.opacity) < 0.05) return false;
+ if (style.position === "absolute") {
+ // The visually-hidden pattern: positioned away and clipped to 1px.
+ const r = node.getBoundingClientRect();
+ if (r.width <= 1 || r.height <= 1) return false;
+ }
+ return true;
+ };
+ if (hasOwnText(el)) return cs.getPropertyValue("color");
+ const queue: Array<{ node: Element; depth: number }> = [{ node: el, depth: 0 }];
+ for (let i = 0; i < queue.length && i < 60; i++) {
+ const { node, depth } = queue[i];
+ if (depth >= 6) continue;
+ for (let c = 0; c < node.children.length; c++) {
+ const child = node.children[c];
+ const childStyle = getComputedStyleCached(child);
+ if (!rendered(child, childStyle)) continue;
+ if (hasOwnText(child)) return childStyle.getPropertyValue("color");
+ queue.push({ node: child, depth: depth + 1 });
+ }
+ }
+ } catch (e) {
+ recordError("getLabelColor", e);
+ }
+ return cs.getPropertyValue("color");
+};
+
export const getStyleSnapshot = (el: Element): StyleSnapshot => {
const cs = getComputedStyleCached(el);
const rect = el.getBoundingClientRect();
@@ -141,7 +202,7 @@ export const getStyleSnapshot = (el: Element): StyleSnapshot => {
}
let bgColor = cs.getPropertyValue("background-color");
- const textColor = cs.getPropertyValue("color");
+ const textColor = getLabelColor(el, cs);
const isTransparent =
bgColor === "transparent" || bgColor === "rgba(0, 0, 0, 0)";