Skip to content

Commit d6dbbac

Browse files
committed
Decode hash value before querying for named anchor
With a URL like `#%24.ajax`, we need to first decode the value before searching the DOM for the element named `$.ajax`. In Firefox, this is not necessary, as the browser already auto-decodes hash values. Therefore, in Firefox this will perform double-decoding, which could theoretically lead to inability to match anchors that contain a percent-encoded value literally. However, since Firefox also seems to automatically decode values for anchors named via `name="..."` HTML attribute, you're going to have a bad time anyway with named anchors that contain percent-encoded characters literally, with or without pjax.
1 parent fab2641 commit d6dbbac

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

jquery.pjax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function pjax(options) {
327327
pjax.state.url = url.href
328328
window.history.replaceState(pjax.state, container.title, url.href)
329329

330-
var name = hash.slice(1)
330+
var name = decodeURIComponent(hash.slice(1))
331331
var target = document.getElementById(name) || document.getElementsByName(name)[0]
332332
if (target) $(window).scrollTop($(target).offset().top)
333333
}

test/unit/pjax_fallback.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,23 @@ asyncTest("scrolls to anchor at bottom page"+s, function() {
170170
})
171171
})
172172

173+
asyncTest("scrolls to named encoded anchor"+s, function() {
174+
var frame = this.frame
175+
176+
equal(frame.window.scrollY, 0)
177+
178+
this.loaded = function(frame) {
179+
setTimeout(function() {
180+
equal(frame.window.scrollY, 10008)
181+
start()
182+
}, 10)
183+
}
173184

185+
frame.$.pjax({
186+
url: "/anchor.html#%62%6F%74%74%6F%6D",
187+
container: "#main"
188+
})
189+
})
174190

175191
asyncTest("sets GET method"+s, function() {
176192
var frame = this.frame

0 commit comments

Comments
 (0)