GeezerVison is showing github.com — fix(server): skip EPUB passages hidden by important inline styles (#4… · pingdotgg/t3code@4bc38b1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/documentTextWorker.ts +33-2Lines changed: 33 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -163,6 +163,36 @@ function isXhtmlElement( | 163 | 163 | ); | 164 | 164 | } | 165 | 165 |
| | 166 | +function hasImportantDisplayNone(style: string): boolean { | | 167 | + // Stylesheet rules can override non-important inline display. | | 168 | + if (!style || style.includes("\\") || !/display/i.test(style) || !/important/i.test(style)) | | 169 | + return false; | | 170 | + let badString = false; | | 171 | + const declarations = style.replaceAll( | | 172 | + /\/\*[\s\S]*?(?:\*\/|$)|"[^"]*(?:"|$)|'[^']*(?:'|$)/g, | | 173 | + (token) => { | | 174 | + if (token.startsWith("/")) return " "; | | 175 | + // CSS ends a string at a newline, so later declarations can still apply. | | 176 | + if (/[\n\r\f]/.test(token)) badString = true; | | 177 | + return '""'; | | 178 | + }, | | 179 | + ); | | 180 | + // Semicolons in blocks can be part of a declaration's value. | | 181 | + if (badString || /[()[\]{}]/.test(declarations)) return false; | | 182 | + let hidden = false; | | 183 | + for (const [declaration] of declarations.matchAll(/[^;]+/g)) { | | 184 | + const match = | | 185 | + /^[\t\n\f\r ]*(display|all)[\t\n\f\r ]*:([^!]+)![\t\n\f\r ]*important[\t\n\f\r ]*$/i.exec( | | 186 | + declaration, | | 187 | + ); | | 188 | + if (!match) continue; | | 189 | + const [, property, value] = match; | | 190 | + if (!value?.trim()) continue; | | 191 | + hidden = property?.toLowerCase() === "display" && /^[\t\n\f\r ]*none[\t\n\f\r ]*$/i.test(value); | | 192 | + } | | 193 | + return hidden; | | 194 | +} | | 195 | + | 166 | 196 | function xml(source: string, extension: string): XmlNode { | 167 | 197 | const odf = [".odt", ".ods", ".odp"].includes(extension); | 168 | 198 | const root: XmlNode = { name: "root", attrs: {}, children: [] }; | @@ -255,8 +285,9 @@ function xml(source: string, extension: string): XmlNode { | 255 | 285 | isXhtmlElement(tag.name, [...stack, node], rootTagName)) || | 256 | 286 | (extension === ".epub" && | 257 | 287 | (stack[1] ?? node).name === "html" && | 258 | | - Object.hasOwn(tag.attributes, "hidden") && | 259 | | - !/^until-found$/i.test(String(tag.attributes.hidden))) || | | 288 | + ((Object.hasOwn(tag.attributes, "hidden") && | | 289 | + !/^until-found$/i.test(String(tag.attributes.hidden))) || | | 290 | + hasImportantDisplayNone(tag.attributes.style ?? ""))) || | 260 | 291 | (extension === ".docx" && node.name === "del") || | 261 | 292 | (extension === ".docx" && | 262 | 293 | node.name === "r" && | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/userInputAttachments.test.ts +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1984,7 +1984,18 @@ describe("question answer paths", () => { | 1984 | 1984 | '<!DOCTYPE html><html><head><title>My Novel</title><meta name="author" content="Hidden Author"/></head><body><h1>First chapter</h1><p>Larkspur</p><p>Map: <img src="map.png" alt="North & south piers"/> open<img src="blank.png" alt=""/><img src="missing.png"/>.</p><div><img src="cover.png" alt="Cover art"/></div><img hidden="" src="draft.png" alt="Hidden image text"/><img src="none.png" alt="Display none image" style="width: 1em; DISPLAY: none"/><img src="invisible.png" alt="Invisible image" style="visibility:hidden !important"/><div>Direct div<section>Direct section<li>Direct item <em>emphasis</em> trailing</li></section></div><div>Account: A<br/>Balance: 731<line-break/>Currency: USD</div><table><thead><tr><th>Heading</th><th>Total</th></tr></thead><tbody><tr><td>Important value</td><td>731</td></tr><tr><td>Before<table><tr><td>Inner</td></tr></table>After</td><td>Neighbor</td></tr></tbody></table><dl><dt>Port</dt><dd>443</dd><dt>Timeout</dt><dd>30 seconds</dd></dl><p hidden="">Draft note</p><div hidden="hidden">Hidden block<p>Hidden nested</p></div><p hidden="UNTIL-FOUND">Findable note</p><p aria-hidden="true">Decorative note</p><p x:hidden="true">Prefixed note</p><ol><li>Before boarding<ul><li>Check passports<ol><li>Compare names</li></ol></li><li>Confirm gate</li></ul></li><li>Board promptly</li><li><p>Before landing</p><ul><li>Collect bags</li></ul></li></ol>' + | 1985 | 1985 | "<ol><li>Indented\n <ul><li>Alpha</li></ul></li></ol>" + | 1986 | 1986 | '<div>Board<template><h2>Sample berth</h2><img alt="Sample map"/></template> now.</div>' + | 1987 | | - "<p>Keep <template>Example sailing at dawn.</template>reading.</p></body></html>", | | 1987 | + "<p>Keep <template>Example sailing at dawn.</template>reading.</p>" + | | 1988 | + "<style>.published{display:block!important}</style>" + | | 1989 | + '<p style="display:none!important; display:block!important">Service charge: $700.</p>' + | | 1990 | + '<div style="DISPLAY: /* draft */ none ! important; display:block"><p>Cancelled surcharge: $500.</p><img alt="Hidden invoice illustration"/></div>' + | | 1991 | + '<p>Review <span style="display:none!important">the retired version of </span>this invoice.</p>' + | | 1992 | + '<p style="display:block !important; display:none">Total due: $700.</p>' + | | 1993 | + '<p class="published" style="display:none">Bank transfer accepted.</p>' + | | 1994 | + '<p style="--label: "example;display:none!important;"">Payment due tomorrow.</p>' + | | 1995 | + '<p style="--options: {label: draft; display:none!important;};">Payment instructions.</p>' + | | 1996 | + '<p style="--options:[);display:none!important">Accounts contact.</p>' + | | 1997 | + '<p style="display:none!important;--label:"draft ;display:block!important">Visible total: $700.</p>' + | | 1998 | + '<img alt="Hidden old payment instructions" style="--label:old\\ payment;display:none"/></body></html>', | 1988 | 1999 | "OEBPS/c.xhtml": '<html hidden=""><body><p>Root hidden</p></body></html>', | 1989 | 2000 | "OEBPS/d.xhtml": | 1990 | 2001 | '<xh:html xmlns:xh="http://www.w3.org/1999/xhtml"><xh:body><xh:p>Start <template>Visible excerpt</template>' + | @@ -2009,6 +2020,8 @@ describe("question answer paths", () => { | 2009 | 2020 | "Before landing\nCollect bags", | 2010 | 2021 | "Indented\n Alpha", | 2011 | 2022 | "Board now.\nKeep reading.", | | 2023 | + "Service charge: $700.\nReview this invoice.\nTotal due: $700.", | | 2024 | + "Bank transfer accepted.\nPayment due tomorrow.\nPayment instructions.\nAccounts contact.\nVisible total: $700.", | 2012 | 2025 | "## Chapter 2", | 2013 | 2026 | "Last chapter", | 2014 | 2027 | "Totals\nTable 1: Revenue\nQ1\tQ2\n100\t200", | @@ -2031,6 +2044,9 @@ describe("question answer paths", () => { | 2031 | 2044 | "Sample berth", | 2032 | 2045 | "Sample map", | 2033 | 2046 | "Example sailing at dawn.", | | 2047 | + "Cancelled surcharge", | | 2048 | + "Hidden invoice illustration", | | 2049 | + "Hidden old payment instructions", | 2034 | 2050 | "Hidden excerpt", | 2035 | 2051 | ], | 2036 | 2052 | }, | |
|