-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix desktop PDF/image export injectTitle replacement-pattern corruption #6931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+78
−0
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
apps/desktop/tests/main/inject-title-replacement-pattern.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import { dirname, join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| const here = dirname(fileURLToPath(import.meta.url)); | ||
| const desktopRoot = join(here, "../.."); | ||
|
|
||
| function readSource(relativePath: string): string { | ||
| return readFileSync(join(desktopRoot, relativePath), "utf8"); | ||
| } | ||
|
|
||
| // Extracts a top-level `function <name>(...) { ... }` declaration. The export | ||
| // pipeline helpers all end with a column-0 closing brace, so the first | ||
| // newline-followed-by-`}` after the declaration is that function's own end | ||
| // (the same convention the sibling artifact-export-image-height test relies on). | ||
| function extractFunction(source: string, name: string): string { | ||
| const start = source.indexOf(`function ${name}(`); | ||
| if (start < 0) throw new Error(`${name} not found in source`); | ||
| const end = source.indexOf("\n}", start); | ||
| if (end < 0) throw new Error(`closing brace for ${name} not found`); | ||
| return source.slice(start, end + 2); | ||
| } | ||
|
|
||
| // Strips the `name: string` / `): string` annotations from the extracted | ||
| // helper signatures so the pure helpers can compile as plain JavaScript. These | ||
| // helpers are dependency-free and carry no other type syntax in their bodies. | ||
| function stripTypeAnnotations(source: string): string { | ||
| return source | ||
| .replace(/\b(\w+)\s*:\s*string\b/g, "$1") | ||
| .replace(/\)\s*:\s*string\b/g, ")"); | ||
| } | ||
|
|
||
| function compileInjectTitle( | ||
| source: string, | ||
| escapeName: string, | ||
| ): (doc: string, title: string) => string { | ||
| const escape = stripTypeAnnotations(extractFunction(source, escapeName)); | ||
| const injectTitle = stripTypeAnnotations(extractFunction(source, "injectTitle")); | ||
| const factory = new Function(`${escape}\n${injectTitle}\nreturn injectTitle;`); | ||
| return factory() as (doc: string, title: string) => string; | ||
| } | ||
|
|
||
| const DOC = "<html><head><title>Old</title></head><body><p>BODY MARKER</p></body></html>"; | ||
|
|
||
| // Titles carrying JavaScript `String.prototype.replace` replacement patterns. | ||
| // With a string replacement argument, ECMA-262 `GetSubstitution` expands | ||
| // `$$`, `$&`, `$\``, and `$'` inside the inserted title, corrupting the | ||
| // rendered document. Each escaped value is what the surrounding escape helper | ||
| // (`escapeHtmlText` / `escapeText`) should produce — `& < >` HTML-escaped, | ||
| // with every `$` sequence left intact. | ||
| const TITLES: ReadonlyArray<{ title: string; escaped: string }> = [ | ||
| { title: "Save $$$ This Quarter", escaped: "Save $$$ This Quarter" }, | ||
| { title: "Before $& After", escaped: "Before $& After" }, | ||
| { title: "Rock $'n Roll Tour", escaped: "Rock $'n Roll Tour" }, | ||
| { title: "Price $`drop", escaped: "Price $`drop" }, | ||
| { title: "A <B> & $& C", escaped: "A <B> & $& C" }, | ||
| ]; | ||
|
|
||
| const FILES = [ | ||
| ["pdf-export.ts", "escapeHtmlText"], | ||
| ["artifact-export.ts", "escapeText"], | ||
| ] as const; | ||
|
|
||
| function expectedDocument(escapedTitle: string): string { | ||
| return `<html><head><title>${escapedTitle}</title></head><body><p>BODY MARKER</p></body></html>`; | ||
| } | ||
|
|
||
| describe.each(FILES)("%s injectTitle", (file, escapeName) => { | ||
| const injectTitle = compileInjectTitle(readSource(join("src/main", file)), escapeName); | ||
|
|
||
| for (const { title, escaped } of TITLES) { | ||
| it(`inserts the title verbatim for ${JSON.stringify(title)}`, () => { | ||
| expect(injectTitle(DOC, title)).toBe(expectedDocument(escaped)); | ||
| }); | ||
| } | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[non-blocking] Keep this regression at the real export seam
🔁 Powered by Looper · runner=reviewer · agent=codex · An autonomous AI dev team for your GitHub repos.apps/desktop/tests/main/export-title-replacement-patterns.test.tsalready drivesexportPdfFromHtmlandexportArtifactwith the same replacement-pattern cases. Thisnew Functionpath reads TypeScript text, strips annotations, and executes a reconstructed helper, so it duplicates coverage while bypassing the actual import/call path; later helper formatting or type-syntax changes can break this test without a product regression. Please move the exact-document assertion and the extra combined HTML/$& case into the existing export integration test, then remove this source-eval harness (or expose a shared pure helper if direct unit coverage is needed).