Skip to content

Rewrite relative URLs in srcset attributes - #1986

Open
abduladnan-mt7 wants to merge 1 commit into
apache:masterfrom
abduladnan-mt7:fix/issue-1952
Open

abduladnan-mt7 wants to merge 1 commit into
apache:masterfrom
abduladnan-mt7:fix/issue-1952

Conversation

@abduladnan-mt7

Copy link
Copy Markdown

Fixes #1952.

I claimed this on the issue on Nov 20 and have since seen #1977, which covers the same ground. The difference is that this PR parses srcset following the HTML spec rather than splitting on every comma, so a data: URL with a comma in it stays intact (covered by the test). Happy for you to pick whichever fits better.

RelativePathPrefixHandler prefixed relative URLs in href, src, background and action, but not in srcset, so responsive images in static markup broke as soon as the page was not served from the context root:

<img src="small.jpg" srcset="small.jpg 480w, large.jpg 1080w">
<!-- before -->  <img src="../../small.jpg" srcset="small.jpg 480w, large.jpg 1080w">
<!-- after  -->  <img src="../../small.jpg" srcset="../../small.jpg 480w, ../../large.jpg 1080w">

Change

  • srcset is added to the attributes the handler considers, on any tag (in practice <img> and <source>).
  • The value is split into image candidates following the HTML parsing rules: a URL runs up to the next whitespace, and its descriptors run up to the next comma. A comma inside a data: URL therefore doesn't split it.
  • Each relative URL is rewritten with UrlUtils.rewriteToContextRelative, the same as src. Absolute URLs (/…, scheme:…), descriptors and whitespace are kept byte for byte.
  • A tag whose srcset holds only absolute URLs doesn't get the behavior, so it doesn't turn into an auto component.

Not in this PR: srcset inside <wicket:link>

Tags inside <wicket:link> are auto-linked and skipped by this handler. AutoLinkResolver turns their src into a package-resource URL, and it doesn't register <source> at all. Doing the same for srcset would mean one package-resource reference per candidate. That's a separate change, and I'm happy to follow up on it if you want it.

Test plan

  • New RelativePathPrefixHandlerTest#srcset (Page_4) covers: all-relative candidates with w descriptors, mixed context-absolute and relative, a data: URL containing a comma, an all-absolute value left untouched, and <source> inside <picture>.
  • The new test fails without the change to RelativePathPrefixHandler and passes with it.
  • Full mvn clean verify (JDK 21) is green: 3,393 tests, 0 failures. I ran it on Windows, where I had to exclude ExampleTitlesTest. It fails there on master too, because it turns class file paths into class names by replacing only / (ExampleTitlesTest.java:97), and that's unrelated to this change.

RelativePathPrefixHandler prefixed relative URLs in href, src, background
and action, but left srcset alone. A responsive image in static markup
such as <img srcset="small.jpg 480w, large.jpg 1080w"> therefore kept
URLs that only resolve when the page is served from the context root,
while the src next to it was rewritten.

srcset is now handled as well. The value is split into its image
candidates following the HTML parsing rules (a URL runs up to the next
whitespace, its descriptors up to the next comma), so a comma inside a
data: URL does not split it. Each relative URL is prefixed the same way
as src; absolute URLs, descriptors and whitespace are kept unchanged.

Applications that worked around this by writing context-absolute URLs in
srcset are unaffected, since those are not rewritten.

GitHub issue apache#1952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite srcset attributes inside wicket:link

1 participant