Skip to content

Commit f7a458d

Browse files
committed
Merge branch 'pathname-ampersands'
Closes #501
2 parents 65c4f34 + f811710 commit f7a458d

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

jquery.pjax.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function pjax(options) {
227227

228228
var url = parseURL(settings.url)
229229
if (hash) url.hash = hash
230-
options.requestUrl = stripInternalParams(url.href)
230+
options.requestUrl = stripInternalParams(url)
231231
}
232232

233233
options.complete = function(xhr, textStatus) {
@@ -565,22 +565,12 @@ function cloneContents(container) {
565565
return [container.selector, cloned.contents()]
566566
}
567567

568-
// Internal: Strips named query param from url
568+
// Internal: Strip internal query params from parsed URL.
569569
//
570-
// url - String
571-
//
572-
// Returns String.
573-
function stripParam(url, name) {
574-
return url
575-
.replace(new RegExp('[?&]' + name + '=[^&#]*'), '')
576-
.replace(/[?&]($|#)/, '\1')
577-
.replace(/[?&]/, '?')
578-
}
579-
570+
// Returns sanitized url.href String.
580571
function stripInternalParams(url) {
581-
url = stripParam(url, '_pjax')
582-
url = stripParam(url, '_')
583-
return url
572+
url.search = url.search.replace(/([?&])(_pjax|_)=[^&]*/g, '')
573+
return url.href.replace(/\?($|#)/, '$1')
584574
}
585575

586576
// Internal: Parse URL components and returns a Locationish object.
@@ -697,7 +687,7 @@ function extractContainer(data, xhr, options) {
697687
// Prefer X-PJAX-URL header if it was set, otherwise fallback to
698688
// using the original requested url.
699689
var serverUrl = xhr.getResponseHeader('X-PJAX-URL')
700-
obj.url = serverUrl ? stripInternalParams(serverUrl) : options.requestUrl
690+
obj.url = serverUrl ? stripInternalParams(parseURL(serverUrl)) : options.requestUrl
701691

702692
// Attempt to parse response html into elements
703693
if (fullDocument) {

test/app.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@ def title(str)
9393
get '/:page.html' do
9494
erb :"#{params[:page]}", :layout => !pjax?
9595
end
96+
97+
get '/some-&-path/hello.html' do
98+
erb :hello, :layout => !pjax?
99+
end

test/unit/pjax.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,4 +1091,12 @@ if ($.support.pjax) {
10911091
container: "#main"
10921092
})
10931093
})
1094+
1095+
asyncTest("copes with ampersands when pushing urls", 2, function() {
1096+
navigate(this.frame)
1097+
.pjax({ url: "/some-&-path/hello.html", container: "#main" }, function(frame) {
1098+
equal(frame.location.pathname, "/some-&-path/hello.html")
1099+
equal(frame.location.search, "")
1100+
})
1101+
})
10941102
}

0 commit comments

Comments
 (0)