Skip to content

Load a stylesheet url() with spaces, parentheses, inner quotes or an SVG data URI - #573

Merged
jakejackson1 merged 2 commits into
gravitypdffrom
fix/552-stylesheet-url-quoting
Sep 30, 2026
Merged

jakejackson1 merged 2 commits into
gravitypdffrom
fix/552-stylesheet-url-quoting

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Problem

A background image named in a stylesheet's url() was not drawn in these cases:

url() What went wrong
url("sub dir/im (1).png"), a local file The parentheses were percent-encoded to keep the CSS parser from splitting on them, and spaces became %20 in the image loader. Nothing decoded them again, so the file was not found.
url( a.png ) The whitespace was kept as part of the path.
Bootstrap 5's form-select arrow, url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' …%3e") Path::relativeToAbsolutePath() put the base path in front of the data URI. The :// inside it made Path::isLocal() treat it as remote, so the HTTP client was asked for it and cURL rejected the URL.
url("data:image/svg+xml;utf8,…") PHP's data: wrapper refuses a parameter without a value, such as ;utf8.

Part of #552 (Units, bug 10 in the audit). Part of #520.

Change

  • InlineStyleParser::processUrlsInCss() reads each url() in one pass, as CSS does:

    • in double quotes, which may hold single quotes, or in single quotes, which may hold double quotes, or unquoted;
    • without the whitespace inside the parentheses;
    • with a backslash dropped before a quote, a parenthesis or whitespace. Other backslashes are kept, because mPDF accepts Windows paths in url(), and resolving \a or \d as CSS hex escapes broke them.

    It writes url('…') back with (, ), {, } percent-encoded and ; as the %ZZ placeholder, as before. The groups are possessive, so a long data URI does not exhaust PCRE's stack. The change stays inside processUrlsInCss(), so CssParser itself is not touched.

  • AssetFetcher::fetchLocalContent() tries a local path percent-decoded when it is not found as written.

  • Path::relativeToAbsolutePath() returns a data: URI unchanged.

  • ImageProcessor::getImage() decodes a data URI that is not base64 itself, with rawurldecode(). It reads parameters before ;base64 as well.

Two related problems are left for their own PRs:

  • In a style attribute written in HTML, the SVG data URI is mangled before it reaches the CSS parser.
  • An unquoted url() with CSS escapes in a <style> block is split by CssLoader::resolveBackgroundUrls(), which runs first.

Tests

  • StylesheetUrlTest renders a stylesheet background for each case above, once with background-image and once with the background shorthand. It checks that the image or SVG form is registered. The local image is copied to sub dir/bg (1).jpg in a temporary directory.

  • InlineStyleParserTest::testProcessUrls checks what processUrlsInCss() writes for:

    • quotes inside the other quote, spaces and parentheses, and whitespace inside the parentheses;
    • ; and braces, escaped quotes, parentheses and spaces, and a Windows path;
    • two url()s, upper case URL(, and quoted and unquoted data URIs of 220 KB.
  • StylesheetUrlSnapshotTest (stylesheet-url) shows each case under a caption saying what should be seen:

    • url("img/bay eux.jpg") and url( img/bay%20eux.jpg ) both fill the box with the tapestry;
    • Bootstrap's arrow is a chevron at the right of a select-like box;
    • the same SVG with its quotes swapped is a chevron in the middle.

    Before this change, all four boxes are empty.

With the source change reverted, the new tests fail. The two long data URI cases also fail against the first, non-possessive version of the regex. No existing snapshot fixture changed.

Checks

  • composer test: OK. 4479 tests, 31 skipped for missing optional packages or ungenerated data. The snapshot suite ran with Imagick and Ghostscript.
  • composer cs: clean.
  • PHPStan: no errors.

🤖 Generated with Claude Code

@jakejackson1 jakejackson1 added bug Something isn't working create-upstream-pr labels Sep 29, 2026
@jakejackson1
jakejackson1 force-pushed the fix/552-stylesheet-url-quoting branch 3 times, most recently from 08d0f47 to 79d208d Compare September 29, 2026 22:37
…SVG data URI

A background image named in a stylesheet was not loaded when its local path had spaces or parentheses, when the url() had whitespace inside its parentheses, or when it was a percent-encoded SVG data URI such as Bootstrap's form-select arrow.

- url() is read as CSS reads it, in either quote or unquoted. A backslash before a quote, a parenthesis or whitespace is dropped; other backslashes are kept for Windows paths.
- A local path that is not found as written is tried percent-decoded.
- A data: URI is no longer resolved against the base path, and one that is not base64 is decoded directly instead of through PHP's data: wrapper or the HTTP client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1
jakejackson1 force-pushed the fix/552-stylesheet-url-quoting branch from 79d208d to c74ee6c Compare September 29, 2026 23:45
@jakejackson1
jakejackson1 merged commit 3ee35eb into gravitypdf Sep 30, 2026
13 checks passed
@jakejackson1
jakejackson1 deleted the fix/552-stylesheet-url-quoting branch September 30, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant