WPURL: Preserve trailing-slash style for origin-only URLs#220
Merged
Conversation
The WHATWG URL parser normalises an empty path to "/", so "https://example.com" gets a pathname of "/". The existing trailing-slash logic skipped trimming when the pathname was "/", which meant origin-only URLs gained an unwanted trailing slash after replace_base_url(). When the raw URL string is available in options, use it to detect whether the original URL ended with "/" and strip the WHATWG-added slash accordingly.
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.
Summary
WPURL::replace_base_url()relies on the WHATWG URL parser, which normalises an empty path to/. When an origin-only URL likehttps://example.com(no trailing slash) went throughreplace_base_url(), its parsed pathname was/, and the existing trailing-slash logic explicitly skipped trimming when$from_pathname === '/'. The result gained an unwanted trailing slash (https://newsite.com/).This fixes it by also checking the
raw_urloption string (when available) to detect the original trailing-slash style. If the raw URL didn't end with/but the WHATWG parser added one, we strip it from the output. This matters for values likesiteurlandhomeduring content migrations.Test plan
WPURLTestcovering origin-only URLs with and without trailing slashRewriteUrlsTestfor origin-only URLs in block attributes and HTML attributes