Skip to content

Commit a24528d

Browse files
kyle-mccarthydesandro
authored andcommitted
provide fix for bug related to parsing GET parameters (#832)
* provide fix for bug related to parsing GET parameters * expand to test for a path that has already escaped the query string
1 parent c1ca3e7 commit a24528d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

js/core.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ proto.updateGetPathTemplate = function( optPath ) {
233233
}.bind( this );
234234
// get pageIndex from location
235235
// convert path option into regex to look for pattern in location
236-
var regexString = optPath.replace( '{{#}}', '(\\d\\d?\\d?)' );
236+
// escape query (?) in url, allows for parsing GET parameters
237+
var regexString = optPath
238+
.replace(/(?<!\\)\?/, '\\?')
239+
.replace( '{{#}}', '(\\d\\d?\\d?)' );
237240
var templateRe = new RegExp( regexString );
238241
var match = location.href.match( templateRe );
242+
239243
if ( match ) {
240244
this.pageIndex = parseInt( match[1], 10 );
241245
this.log( 'pageIndex', [ this.pageIndex, 'template string' ] );

test/unit/page-index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,29 @@ QUnit.test( 'pageIndex', function( assert ) {
2626

2727
history.replaceState( null, document.title, prevURL );
2828

29+
infScroll.destroy();
30+
31+
history.replaceState(null, document.title, 'page?currPage=8');
32+
infScroll = new InfiniteScroll( demoElem, {
33+
path: 'page?currPage={{#}}',
34+
history: false,
35+
scrollThreshold: false,
36+
});
37+
38+
assert.equal( infScroll.pageIndex, 8, 'pageIndex from GET param with {{#}}' );
39+
40+
history.replaceState( null, document.title, prevURL );
41+
42+
infScroll.destroy();
43+
44+
history.replaceState(null, document.title, 'page?currPage=8');
45+
infScroll = new InfiniteScroll( demoElem, {
46+
path: 'page\\?currPage={{#}}',
47+
history: false,
48+
scrollThreshold: false,
49+
});
50+
51+
assert.equal( infScroll.pageIndex, 8, 'pageIndex from GET param with {{#}} and pre-escaped regexp' );
52+
53+
history.replaceState( null, document.title, prevURL );
2954
});

0 commit comments

Comments
 (0)