Repository navigation
Rewrite relative URLs in srcset attributes - #1986
Open
abduladnan-mt7 wants to merge 1 commit into
Open
abduladnan-mt7 wants to merge 1 commit into
abduladnan-mt7 wants to merge 1 commit into
Conversation
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
srcsetfollowing the HTML spec rather than splitting on every comma, so adata:URL with a comma in it stays intact (covered by the test). Happy for you to pick whichever fits better.RelativePathPrefixHandlerprefixed relative URLs inhref,src,backgroundandaction, but not insrcset, so responsive images in static markup broke as soon as the page was not served from the context root:Change
srcsetis added to the attributes the handler considers, on any tag (in practice<img>and<source>).data:URL therefore doesn't split it.UrlUtils.rewriteToContextRelative, the same assrc. Absolute URLs (/…,scheme:…), descriptors and whitespace are kept byte for byte.srcsetholds only absolute URLs doesn't get the behavior, so it doesn't turn into an auto component.Not in this PR:
srcsetinside<wicket:link>Tags inside
<wicket:link>are auto-linked and skipped by this handler.AutoLinkResolverturns theirsrcinto a package-resource URL, and it doesn't register<source>at all. Doing the same forsrcsetwould 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
RelativePathPrefixHandlerTest#srcset(Page_4) covers: all-relative candidates withwdescriptors, mixed context-absolute and relative, adata:URL containing a comma, an all-absolute value left untouched, and<source>inside<picture>.RelativePathPrefixHandlerand passes with it.mvn clean verify(JDK 21) is green: 3,393 tests, 0 failures. I ran it on Windows, where I had to excludeExampleTitlesTest. It fails there onmastertoo, because it turns class file paths into class names by replacing only/(ExampleTitlesTest.java:97), and that's unrelated to this change.