-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH Allow base URL to not have trailing slash #1438
Merged
emteknetnz
merged 1 commit into
silverstripe:2
from
creative-commoners:pulls/2/base-url-no-trailing-slash
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* global jest, describe, beforeEach, it, expect, console */ | ||
import { joinUrlPaths } from 'lib/urls'; | ||
|
||
describe('urls', () => { | ||
describe('joinUrlPaths()', () => { | ||
it('should add a slash between paths', () => { | ||
const value = joinUrlPaths('some', 'path'); | ||
expect(value).toBe('some/path'); | ||
}); | ||
|
||
it('should only include a single slash between paths', () => { | ||
const value1 = joinUrlPaths('some/', 'path'); | ||
const value2 = joinUrlPaths('some', '/path'); | ||
const value3 = joinUrlPaths('some/', '/path'); | ||
expect(value1).toBe('some/path'); | ||
expect(value2).toBe('some/path'); | ||
expect(value3).toBe('some/path'); | ||
}); | ||
|
||
it('should retain leading and trailing slashes', () => { | ||
const value = joinUrlPaths('/some', 'path/'); | ||
expect(value).toBe('/some/path/'); | ||
}); | ||
|
||
it('should retain slashes in the middle of a given path', () => { | ||
const value = joinUrlPaths('some/path', 'another-path'); | ||
expect(value).toBe('some/path/another-path'); | ||
}); | ||
|
||
it('should join an arbitrarily large number of paths', () => { | ||
const value = joinUrlPaths('some', 'path', 'another', 'path'); | ||
expect(value).toBe('some/path/another/path'); | ||
}); | ||
|
||
it('should not alter the result if only one path is passed in', () => { | ||
const value1 = joinUrlPaths('some-path'); | ||
const value2 = joinUrlPaths('/some-path/'); | ||
expect(value1).toBe('some-path'); | ||
expect(value2).toBe('/some-path/'); | ||
}); | ||
|
||
it('should allow easily ensuring a preceding or leading slash', () => { | ||
const value = joinUrlPaths('/', 'some-path', '/'); | ||
expect(value).toBe('/some-path/'); | ||
}); | ||
|
||
it('should retain URL scheme, host, query string, and fragment identifier', () => { | ||
const value = joinUrlPaths('https://www.example.com/', 'some-path?arg=val#some-id'); | ||
expect(value).toBe('https://www.example.com/some-path?arg=val#some-id'); | ||
}); | ||
|
||
it('should return an empty string if no path is passed in', () => { | ||
const value = joinUrlPaths(); | ||
expect(value).toBe(''); | ||
}); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const joinUrlPaths = (...urlPaths) => { | ||
// Just return a blank string if there's no paths passed in | ||
if (!urlPaths.length) { | ||
return ''; | ||
} | ||
|
||
// Combine paths with a single '/' between them. | ||
let result = urlPaths.shift(); | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const path of urlPaths) { | ||
result = `${result.replace(/\/$/, '')}/${path.replace(/^\//, '')}`; | ||
} | ||
return result; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
templates/SilverStripe/Admin/Includes/LeftAndMain_MenuStatus.ss
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't directly related to the purpose of this PR, but I'm adding escapeRegExp in this PR so it makes sense to apply it in the one other place I found that part of a URL is used as a variable in
new RegExp()
.