Skip to content

fix(web): load EPUB stylesheet assets whose URLs use CSS escapes - #46866

Merged
juliusmarminge merged 2 commits into
pingdotgg:mainfrom
saphid:hunter/c-573dd4b0c9
Dec 1, 2026
Merged

juliusmarminge merged 2 commits into
pingdotgg:mainfrom
saphid:hunter/c-573dd4b0c9

Conversation

@saphid

@saphid saphid commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Problem

An EPUB stylesheet image disappears when the url() that points to it uses a CSS escape. For example, a space written as \ or \20 in an archive filename. cssUrls in apps/web/src/components/files/epubPreview.ts passed quoted escapes to the ZIP lookup unchanged, so the lookup failed and the URL became about:blank. Its matcher for unquoted URLs also stopped at the escaped space, so the original archive-relative URL was left in the CSS and never resolved. This reaches users in Web and Electron EPUB previews of local files and attachments, through openEpub(zip).page(i).styles → EpubViewer. Mobile hits it too, because its WebView loads the same web OfficeViewer page (epubPreviewMain.tsx).

Deterministic repro on 2a8b392b956d373f88de3eed70cc8f1646eaad49 (tested main ancestor): build an in-memory EPUB with JSZip. Its manifest lists cover%20art.png, and its ZIP member cover art.png is a decodable 1x1 PNG. The chapter body has <p class="control|quoted|bare|hex|crlf"> elements. Its <head> has this <style>:

.control{background:url("cover art.png")} .quoted{background:url("cover\ art.png")} .bare{background:url(cover\ art.png)} .hex{background:url(cover\20 art.png)}

It also links crlf.css, which contains .crlf{background:url(cover\20<CR><LF>art.png)}. Then call openEpub(zip).page(0).

  • Observed: only .control is rewritten to the blob URL. .quoted becomes url(about:blank), and .bare, .hex, and .crlf keep their unresolved archive names.
  • Expected: all five rules are rewritten to the same blob URL for the packaged PNG.

Change

The url() matcher in cssUrls now reads escape sequences in quoted and unquoted URL tokens. An unquoted escape is consumed in one deterministic way: up to six hex digits, then an optional single whitespace or CRLF, or else one non-hex character. An unescaped ( ends the unquoted branch, which CSS forbids in an unquoted URL; \( stays valid. Each failed url( start therefore stops at the next start instead of rescanning the rest of the stylesheet. Malformed CSS cannot cause catastrophic or quadratic backtracking. A small cssUnescape helper decodes the captured argument before the existing bounded localPath/resource lookup. Out-of-range or surrogate code points decode to U+FFFD.

data: URLs and fragment-only URLs keep their existing handling. Stylesheets, <style> blocks, and style/presentation attributes all go through cssUrls, so this covers every declaration url(). The separate @import prelude parser still does not decode escapes, and this PR leaves it unchanged.

Scope

Small fix for an obvious bug; no issue needed under CONTRIBUTING.md.

Verification

  • Added loads EPUB stylesheet assets whose URLs use CSS escapes to apps/web/src/components/files/FilePreviewPanel.test.ts.
    • The fixture uses a PNG with valid chunk CRCs and an IDAT that inflates. Each rule matches a visible element, and an external stylesheet uses a CRLF after a hex escape.
    • On base 2a8b392b956 it fails. Received: .quoted{background:url(about:blank)} .bare{background:url(cover\ art.png)} .hex{background:url(cover\20 art.png)} and .crlf{background:url(cover\20⏎art.png)}.
    • On head it passes, and all 119 tests in the file pass.
  • Malformed-input scaling, using the actual regex extracted from each revision (full table in the gist):
    • 2,048 failed url(\20 a\ starts (22 KiB): 0.31 ms on base, 333 ms with the previous draft, 0.36 ms on head.
    • One start with 20,000 \123456 escapes: 3.5 ms on base, 6.2 ms on head.
    • 8,192 nested url( starts (32 KiB): 1.16 s on base, already slow before this PR. 0.48 ms on head, because an unescaped ( now ends the token.
  • commit-checked.sh → vp run check:changed exited 0 at HEAD 92a19469e2c: format, lint, knip, typecheck, affected tests, and React Compiler all passed.
  • Before/after transcript, timing, and the check:changed summary: https://gist.github.com/saphid/47d6bafcb2425dfa89e42afe7d3a5ba6

Not shown:

  • A rendered before/after capture of the EPUB preview in web or Electron, or a scenarios:prove run. The rewritten CSS is what EpubViewer injects into the iframe, whose CSP allows blob images, but I have not shown the visible result in a running app.
  • The fixture in a native mobile WebView. I traced mobile through the source: EpubFilePreview loads the same web viewer, so it gets the fix without a native change, but I did not run it.

Authored by Claude Opus 5.5 (Model Hub); reviewed by GPT-6.2 Sol (Codex).

Not shown

  • apps/web/src/components/files/epubPreview.ts:673 — Narrow the draft’s blanket no-quadratic-backtracking claim to the two measured input families. The exact matcher still has pre-existing superlinear behavior on malformed whitespace-heavy input on both base and head.
  • apps/web/src/components/files/EpubViewer.tsx:118 — No rendered web or Electron before/after or scenarios:prove result. The draft discloses this; for visual proof, save the deterministic fixture as an EPUB and open it through the real preview on base and head.
  • apps/mobile/src/features/files/EpubFilePreview.tsx:30 — Native WebView behavior is source-traced, not device-verified. The draft discloses this; open the same EPUB in the native viewer to close that gap.

@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Dec 1, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at a150fce

Macroscope's review found this PR approvable — EPUB previews now resolve stylesheet assets whose URLs contain CSS escapes. Existing archive confinement, asset validation, and resource limits remain unchanged, keeping the impact limited to preview rendering.

No code changes detected at 45108fb. Prior analysis still applies.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a150fce and 45108fb.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The EPUB stylesheet parser now recognizes and decodes CSS escapes in URL values before resolving resources. Tests cover quoted, unquoted, hexadecimal, and CRLF-continued escapes, including rewritten CSS URLs and object URL cleanup.

Changes: EPUB CSS URL handling

Layer / File(s) Summary
Decode and resolve CSS URLs
apps/web/src/components/files/epubPreview.ts, apps/web/src/components/files/FilePreviewPanel.test.ts
The parser decodes CSS escapes in quoted and unquoted URL values before resolving non-data resources. Tests check escaped image URLs in inline and linked stylesheets and verify generated object URL cleanup.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: yash-singh1, iamshadmantaqi

Merge Risk: Minimal · previous review a150f

This change makes EPUB stylesheet assets with CSS-escaped URLs load in the preview. No merge-blocking risk was identified in the supplied context.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: EPUB stylesheet assets whose URLs use CSS escapes are now loaded. It uses a conventional commit prefix and matches the raw summary, which describes CSS escape decoding…
Description check Passed The description follows the template sections: Problem, Change, Scope, and Verification. It explains the bug, the fix, and why no issue is needed. It gives a concrete repro, a named test, base and hea…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: EPUB stylesheet assets whose URLs use CSS escapes are now loaded. It uses a conventional commit prefix and matches the raw summary, which describes CSS escape decoding in cssUrls and a new test for escaped URLs. Length is 64 characters, which is acceptable.

Full details: Description check

Explanation

The description follows the template sections: Problem, Change, Scope, and Verification. It explains the bug, the fix, and why no issue is needed. It gives a concrete repro, a named test, base and head results, timing data, and a clear list of what was not verified. Minor gaps: the Verification section mentions a gist link for the full transcript, and the description includes an agent attribution line, which the template requests. The description also has some extra detail on the matcher's internals that goes beyond what the template asks for, but this does not reduce completeness.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between dbe124d and a150fce.

Files selected for processing: 2

  • apps/web/src/components/files/FilePreviewPanel.test.ts
  • apps/web/src/components/files/epubPreview.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@juliusmarminge juliusmarminge added the takeover:in-progress A takeover lead is working on this PR label Dec 1, 2026
@juliusmarminge

Copy link
Copy Markdown
Member

Evidence

Generate escapes.epub containing cover art.png, an OPF cover%20art.png manifest entry, four inline CSS URL forms (plain quoted space, quoted escaped space, bare escaped space, bare hexadecimal escape), and external CSS with a hexadecimal escape terminated by CRLF. Seed a fresh project and replay thread through t3.project/t3.thread, open the thread, use Ctrl+P to open escapes.epub, hide the file explorer, and maximize the panel. Capture all five rows and check that every background decodes as the packaged 480px image.

Scenario: EPUB CSS escaped URLs load the same packaged background image

Proven: the scenario fails on base 19da14db03 and passes on head 45108fbcbd.

Base 19da14db03 Head 45108fbcbd
Result ❌ failed
ASSERTION_FAILED: expect.poll(...).toEqual(...) timed out after 15000 ms
last: expected [true,false,false,false,false] to equal [true,true,true,true,true]
✅ passed
epub css escaped backgrounds Base 19da14db03: epub css escaped backgrounds Head 45108fbcbd: epub css escaped backgrounds
failure Base 19da14db03: failure

Scenario and EPUB generator

Not shown:

  • Desktop and mobile were not run; this proof covers the web EPUB preview.

@juliusmarminge juliusmarminge added takeover:ready Takeover done; auto-lands when gates pass and removed takeover:in-progress A takeover lead is working on this PR labels Dec 1, 2026
@juliusmarminge
juliusmarminge merged commit e173215 into pingdotgg:main Dec 1, 2026
61 checks passed
@juliusmarminge juliusmarminge removed the takeover:ready Takeover done; auto-lands when gates pass label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S 10-29 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants