Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pathname/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function(template, params) {
if (params == null) return template
var queryIndex = template.indexOf("?")
var hashIndex = template.indexOf("#")
if (hashIndex >= 0 && queryIndex > hashIndex) queryIndex = -1
var queryEnd = hashIndex < 0 ? template.length : hashIndex
var pathEnd = queryIndex < 0 ? queryEnd : queryIndex
var path = template.slice(0, pathEnd)
Expand All @@ -28,6 +29,7 @@ module.exports = function(template, params) {
// In case the template substitution adds new query/hash parameters.
var newQueryIndex = resolved.indexOf("?")
var newHashIndex = resolved.indexOf("#")
if (newHashIndex >= 0 && newQueryIndex > newHashIndex) newQueryIndex = -1
var newQueryEnd = newHashIndex < 0 ? resolved.length : newHashIndex
var newPathEnd = newQueryIndex < 0 ? newQueryEnd : newQueryIndex
var result = resolved.slice(0, newPathEnd)
Expand Down
12 changes: 12 additions & 0 deletions pathname/tests/test-buildPathname.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ o.spec("buildPathname", function() {

o(string).equals(prefix + "/image.png/view")
})
o("preserves question marks inside template fragments", function() {
o(buildPathname(prefix + "/item#section?tab=details", {page: 2})).equals(prefix + "/item?page=2#section?tab=details")
o(buildPathname(prefix + "/item#section?tab=details", {})).equals(prefix + "/item#section?tab=details")
})
o("preserves question marks inside variadic fragments", function() {
o(buildPathname(prefix + "/:path...", {path: "item#section?tab=details", page: 2})).equals(prefix + "/item?page=2#section?tab=details")
o(buildPathname(prefix + "/:path...?sort=asc", {path: "item#section?tab=details", page: 2})).equals(prefix + "/item?sort=asc&page=2#section?tab=details")
})
o("keeps queries before fragments and encoded question marks unchanged", function() {
o(buildPathname(prefix + "/item?sort=asc#section?tab=details", {page: 2})).equals(prefix + "/item?sort=asc&page=2#section?tab=details")
o(buildPathname(prefix + "/item#section%3Ftab=details", {page: 2})).equals(prefix + "/item?page=2#section%3Ftab=details")
})
o("merges query strings", function() {
var string = buildPathname(prefix + "/item?a=1&b=2", {c: 3})

Expand Down
Loading