Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions apps/server/src/provider/documentTextWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,40 @@ function hasImportantDisplayNone(style: string): boolean {
return false;
let badString = false;
const declarations = style.replaceAll(
/\/\*[\s\S]*?(?:\*\/|$)|"[^"]*(?:"|$)|'[^']*(?:'|$)/g,
/(?<![\w-])url\((?![\t\n\f\r ]*["'])[^)]*(?:\)|$)|\/\*[\s\S]*?(?:\*\/|$)|"[^"]*(?:"|$)|'[^']*(?:'|$)/gi,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High provider/documentTextWorker.ts:299

hasImportantDisplayNone returns true for style="--a: #url(/*); display: none !important; --b: url(*/)", so the EPUB caller removes a visible element and its text even though display: none is inside a CSS comment. The URL matcher treats url( after # or a non-ASCII identifier character as a URL token and strips the comment delimiters; recognize CSS name-code-point and hash-token boundaries instead.

Suggested change
/(?<![\w-])url\((?![\t\n\f\r ]*["'])[^)]*(?:\)|$)|\/\*[\s\S]*?(?:\*\/|$)|"[^"]*(?:"|$)|'[^']*(?:'|$)/gi,
/(?<![#\w\u0080-\u{10FFFF}-])url\((?![\t\n\f\r ]*["'])[^)]*(?:\)|$)|\/\*[\s\S]*?(?:\*\/|$)|"[^"]*(?:"|$)|'[^']*(?:'|$)/giu,
🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/provider/documentTextWorker.ts around line 299:

`hasImportantDisplayNone` returns `true` for `style="--a: #url(/*); display: none !important; --b: url(*/)"`, so the EPUB caller removes a visible element and its text even though `display: none` is inside a CSS comment. The URL matcher treats `url(` after `#` or a non-ASCII identifier character as a URL token and strips the comment delimiters; recognize CSS name-code-point and hash-token boundaries instead.

(token) => {
if (token.startsWith("/")) return " ";
// Unquoted URLs keep comment and bracket characters as content.
if (/^url/i.test(token)) {
if (!/^url\([\t\n\f\r ]*[^\s"'()]*[\t\n\f\r ]*\)$/i.test(token)) badString = true;
return "url()";
}
// CSS ends a string at a newline, so later declarations can still apply.
if (/[\n\r\f]/.test(token)) badString = true;
return '""';
},
);
// Semicolons in blocks can be part of a declaration's value.
if (badString || /[()[\]{}]/.test(declarations)) return false;
if (badString) return false;
// Semicolons in blocks are part of a declaration's value, so split only at depth zero.
const closers: string[] = [];
const parts: string[] = [];
let start = 0;
for (let index = 0; index < declarations.length; index++) {
const char = declarations[index];
if (char === "(") closers.push(")");
else if (char === "[") closers.push("]");
else if (char === "{") closers.push("}");
else if (char === ")" || char === "]" || char === "}") {
if (closers.pop() !== char) return false;
} else if (char === ";" && closers.length === 0) {
parts.push(declarations.slice(start, index));
start = index + 1;
}
}
if (closers.length > 0) return false;
parts.push(declarations.slice(start));
let hidden = false;
for (const [declaration] of declarations.matchAll(/[^;]+/g)) {
for (const declaration of parts) {
const match =
/^[\t\n\f\r ]*(display|all)[\t\n\f\r ]*:([^!]+)![\t\n\f\r ]*important[\t\n\f\r ]*$/i.exec(
declaration,
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/provider/userInputAttachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,11 @@ describe("question answer paths", () => {
"fig.svg":
'<svg xmlns="http://www.w3.org/2000/svg"><metadata>Standalone SVG metadata</metadata>' +
'<g style="display:none !important"><text>Superseded gate</text></g>' +
'<g style="transform: translate(10px); display: none !important"><text>Closed ferry berth</text></g>' +
'<g style="filter: drop-shadow(0 0 1px red); display: none !important"><text>Old pier</text></g>' +
'<g style="--map-options: {; display: none !important; }"><text>Harbour light</text></g>' +
'<g style="display: none !important; --a: url(/*a); display: inline !important; --b: url(b*/)"><text>Ferry timetable</text></g>' +
'<g style="filter: url(/maps/legend[old.svg); display: none !important"><text>Old legend</text></g>' +
'<text style="display:none!important;display:inline!important">Standalone chart</text></svg>',
},
expected: [
Expand All @@ -1282,6 +1287,8 @@ describe("question answer paths", () => {
"Q4 Revenue",
"Foreign metadata",
"Prefixed label",
"Harbour light",
"Ferry timetable",
"Standalone chart",
],
absent: [
Expand All @@ -1291,6 +1298,9 @@ describe("question answer paths", () => {
"Prefixed SVG metadata",
"Standalone SVG metadata",
"Superseded gate",
"Closed ferry berth",
"Old pier",
"Old legend",
],
},
{
Expand Down
Loading