diff --git a/src/core/effects.ts b/src/core/effects.ts index 96c8409..fdf6fec 100644 --- a/src/core/effects.ts +++ b/src/core/effects.ts @@ -11,10 +11,10 @@ const CSS_IMAGE_VALUE_REGEX = /(?:url|-webkit-image-set|image-set|cross-fade)\s*\(/i; const HIDE_IMAGES_STYLE_ID = "arap-hide-images-style"; const HIDE_IMAGES_PSEUDO_CSS = [ - "[data-arap-hide-before-bg]::before { background-image: none !important; }", - "[data-arap-hide-after-bg]::after { background-image: none !important; }", - "[data-arap-hide-before-content]::before { visibility: hidden !important; }", - "[data-arap-hide-after-content]::after { visibility: hidden !important; }", + "[data-arap-hide-before-bg]:not(#arap-s1):not(#arap-s2):not(#arap-s3)::before { background-image: none !important; }", + "[data-arap-hide-after-bg]:not(#arap-s1):not(#arap-s2):not(#arap-s3)::after { background-image: none !important; }", + "[data-arap-hide-before-content]:not(#arap-s1):not(#arap-s2):not(#arap-s3)::before { visibility: hidden !important; }", + "[data-arap-hide-after-content]:not(#arap-s1):not(#arap-s2):not(#arap-s3)::after { visibility: hidden !important; }", ].join("\n"); type StyleableElement = HTMLElement | SVGElement; export const MAX_HOST_ELEMENTS = 8000; @@ -2575,8 +2575,10 @@ export function useBuiltinEffects( let refreshTimer: ReturnType | null = null; let incrementalTimer: ReturnType | null = null; let mediaTimer: ReturnType | null = null; + let largeClassTimer: ReturnType | null = null; let pendingAddedNodes: HTMLElement[] = []; const pendingMediaNodes = new Set(); + const pendingLargeClassRoots = new Set(); const flushIncremental = () => { if (pendingAddedNodes.length === 0) return; @@ -2594,6 +2596,30 @@ export function useBuiltinEffects( observer.takeRecords(); }; + const flushLargeClassSubtrees = () => { + if (pendingLargeClassRoots.size === 0) return; + const roots = Array.from(pendingLargeClassRoots); + pendingLargeClassRoots.clear(); + const collected: StyleableElement[] = []; + for (const root of roots) { + if (!document.contains(root)) continue; + if (isStyleableElement(root) && !root.matches(EXCLUDED)) { + collected.push(root); + } + for (const desc of Array.from(root.querySelectorAll("*"))) { + if (collected.length >= MAX_HOST_ELEMENTS) break; + if (isStyleableElement(desc) && !desc.matches(EXCLUDED)) { + collected.push(desc); + } + } + if (collected.length >= MAX_HOST_ELEMENTS) break; + } + if (collected.length > 0) { + activeSession.applyMediaIncremental?.(collected); + observer.takeRecords(); + } + }; + const scheduleFullRefresh = (delay = 25) => { if (refreshTimer) clearTimeout(refreshTimer); refreshTimer = setTimeout(() => { @@ -2608,6 +2634,7 @@ export function useBuiltinEffects( let hasRootAttributeChange = false; const added: HTMLElement[] = []; const mediaNodes: StyleableElement[] = []; + const largeClassRoots: Element[] = []; for (const record of records) { if (record.type === "attributes") { @@ -2640,14 +2667,16 @@ export function useBuiltinEffects( ) ) { if (attrName === "class" && values.hideImages) { - mediaNodes.push(record.target); const descendants = record.target.querySelectorAll("*"); if (descendants.length <= 200) { + mediaNodes.push(record.target); for (const desc of Array.from(descendants)) { if (isStyleableElement(desc) && !desc.matches(EXCLUDED)) { mediaNodes.push(desc); } } + } else { + largeClassRoots.push(record.target); } } else if ( record.target.matches( @@ -2699,6 +2728,14 @@ export function useBuiltinEffects( if (mediaTimer) clearTimeout(mediaTimer); mediaTimer = setTimeout(flushMediaIncremental, 16); } + + if (largeClassRoots.length > 0) { + for (const root of largeClassRoots) { + pendingLargeClassRoots.add(root); + } + if (largeClassTimer) clearTimeout(largeClassTimer); + largeClassTimer = setTimeout(flushLargeClassSubtrees, 50); + } }); const attributeFilter = ["class", "data-theme", "dir", "lang"]; @@ -2786,6 +2823,7 @@ export function useBuiltinEffects( if (refreshTimer) clearTimeout(refreshTimer); if (incrementalTimer) clearTimeout(incrementalTimer); if (mediaTimer) clearTimeout(mediaTimer); + if (largeClassTimer) clearTimeout(largeClassTimer); observer.disconnect(); for (const cleanup of fontCleanups) cleanup(); window.removeEventListener("resize", resize); diff --git a/src/core/profiles.ts b/src/core/profiles.ts index 69b4ed8..28da79a 100644 --- a/src/core/profiles.ts +++ b/src/core/profiles.ts @@ -84,7 +84,7 @@ export function mapProfileSettingsToFeatures( } if (settings.hideDecorativeImages !== undefined) { - features.hideImages = settings.hideDecorativeImages; + features.hideImages = settings.hideDecorativeImages ? "decorative" : false; } if (settings.fontFamily !== undefined) { diff --git a/tests/browser/widget.spec.ts b/tests/browser/widget.spec.ts index 68840e6..45c86a0 100644 --- a/tests/browser/widget.spec.ts +++ b/tests/browser/widget.spec.ts @@ -2347,6 +2347,7 @@ test("Hide Images hides HTML img/picture and CSS/pseudo/root images while Hide D .test-sheet-bg { width: 60px; height: 30px; background-image: url("${svgUrl}"); } .test-var-bg { width: 60px; height: 30px; background-image: var(--test-hero-bg); } .test-pseudo-after::after { content: url("${svgUrl}"); } + #test-high-spec-pseudo::before { content: ""; display: inline-block; width: 16px; height: 16px; background-image: url("${svgUrl}") !important; } .test-dyn-bg { width: 60px; height: 30px; background-image: url("${svgUrl}"); } `, }); @@ -2380,6 +2381,10 @@ test("Hide Images hides HTML img/picture and CSS/pseudo/root images while Hide D pseudoAfter.className = "test-pseudo-after"; pseudoAfter.textContent = "After"; + const highSpecPseudo = document.createElement("div"); + highSpecPseudo.id = "test-high-spec-pseudo"; + highSpecPseudo.textContent = "High specificity pseudo"; + const paragraphSection = document.createElement("section"); paragraphSection.id = "test-section-paragraph"; paragraphSection.setAttribute("aria-label", "Hero banner"); @@ -2403,6 +2408,7 @@ test("Hide Images hides HTML img/picture and CSS/pseudo/root images while Hide D sheetBg, varBg, pseudoAfter, + highSpecPseudo, paragraphSection, gradientBox, dynTarget, @@ -2477,6 +2483,11 @@ test("Hide Images hides HTML img/picture and CSS/pseudo/root images while Hide D .evaluate((el) => getComputedStyle(el, "::after").visibility); expect(afterVis).toBe("hidden"); + const highSpecBeforeBg = await page + .locator("#test-high-spec-pseudo") + .evaluate((el) => getComputedStyle(el, "::before").backgroundImage); + expect(highSpecBeforeBg).toBe("none"); + // Dynamic class + inline style mutation while active await page.evaluate((imgSrc) => { const dyn = document.getElementById("test-dyn-target"); @@ -2512,6 +2523,10 @@ test("Hide Images hides HTML img/picture and CSS/pseudo/root images while Hide D .locator("#sample-pseudo-bg") .evaluate((el) => getComputedStyle(el, "::before").backgroundImage); expect(restoredBeforeBg).not.toBe("none"); + const restoredHighSpecBg = await page + .locator("#test-high-spec-pseudo") + .evaluate((el) => getComputedStyle(el, "::before").backgroundImage); + expect(restoredHighSpecBg).not.toBe("none"); // Hide Decorative Images preserves informative & missing-alt images while hiding decorative & CSS images await page diff --git a/tests/decorativeImages.test.tsx b/tests/decorativeImages.test.tsx index 95067ea..57d561d 100644 --- a/tests/decorativeImages.test.tsx +++ b/tests/decorativeImages.test.tsx @@ -14,9 +14,11 @@ import { HideImagesController, HideDecorativeImagesController, HideVideosController, + PresetsController, ResetController, isEligibleDecorativeImage, isFunctionalOrMeaningfulImage, + mapProfileSettingsToFeatures, } from "../src"; const injectedStyleTags: HTMLStyleElement[] = []; @@ -693,4 +695,92 @@ describe("HideImagesController (default all mode) & HideDecorativeImagesControll expect(markedWithAltInFigure.style.visibility).toBe(""); expect(meaningfulInFigure.style.visibility).toBe(""); }); + + it("maps hideDecorativeImages profile preference to decorative mode and preserves decorative-only behavior across preset round-trips", () => { + expect( + mapProfileSettingsToFeatures({ hideDecorativeImages: true }), + ).toEqual({ hideImages: "decorative" }); + expect( + mapProfileSettingsToFeatures({ hideDecorativeImages: false }), + ).toEqual({ hideImages: false }); + + const { host } = renderFixture( + <> + + + , + ); + + const decoToggle = screen.getByRole("button", { + name: "Hide Decorative Images", + }); + fireEvent.click(decoToggle); + expect( + (host.querySelector("#deco-empty-alt") as HTMLElement).style.visibility, + ).toBe("hidden"); + expect( + (host.querySelector("#meaningful-alt") as HTMLElement).style.visibility, + ).toBe(""); + + // Save custom preset while Hide Decorative Images is active + fireEvent.click(screen.getByRole("button", { name: "Presets" })); + fireEvent.change(screen.getByRole("textbox", { name: "Preset name" }), { + target: { value: "Deco Only" }, + }); + fireEvent.click( + screen.getByRole("button", { name: "Save current preset" }), + ); + + // Reset All clears image hiding + fireEvent.click(screen.getByRole("button", { name: /Reset All/i })); + expect( + (host.querySelector("#deco-empty-alt") as HTMLElement).style.visibility, + ).toBe(""); + + // Reapplying the saved preset preserves decorative-only mode + fireEvent.click( + screen.getByRole("button", { name: "Apply preset Deco Only" }), + ); + expect( + (host.querySelector("#deco-empty-alt") as HTMLElement).style.visibility, + ).toBe("hidden"); + expect( + (host.querySelector("#meaningful-alt") as HTMLElement).style.visibility, + ).toBe(""); + }); + + it("reprocesses descendants beyond 200 nodes on dynamic class mutations", async () => { + injectTestStyle(` + .large-wrap.show-bg .late-descendant-bg { + background-image: url("oversized-subtree.png"); + } + `); + + const { host } = renderFixture(); + const container = document.createElement("div"); + container.className = "large-wrap"; + for (let i = 0; i < 220; i++) { + const child = document.createElement("div"); + if (i === 215) { + child.id = "target-215"; + child.className = "late-descendant-bg"; + } + container.appendChild(child); + } + host.appendChild(container); + + fireEvent.click(screen.getByRole("button", { name: "Hide Images" })); + const target215 = host.querySelector("#target-215") as HTMLElement; + expect(target215.style.backgroundImage).toBe(""); + + // Trigger class mutation on oversized >200-node container + container.classList.add("show-bg"); + + await waitFor(() => { + expect(target215.style.backgroundImage).toBe("none"); + }); + + fireEvent.click(screen.getByRole("button", { name: /Reset All/i })); + expect(target215.style.backgroundImage).toBe(""); + }); });