Skip to content
Merged
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
6 changes: 5 additions & 1 deletion js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ proto.updateGetPathTemplate = function( optPath ) {
}.bind( this );
// get pageIndex from location
// convert path option into regex to look for pattern in location
var regexString = optPath.replace( '{{#}}', '(\\d\\d?\\d?)' );
// escape query (?) in url, allows for parsing GET parameters
var regexString = optPath
.replace(/(?<!\\)\?/, '\\?')
.replace( '{{#}}', '(\\d\\d?\\d?)' );
var templateRe = new RegExp( regexString );
var match = location.href.match( templateRe );

if ( match ) {
this.pageIndex = parseInt( match[1], 10 );
this.log( 'pageIndex', [ this.pageIndex, 'template string' ] );
Expand Down
25 changes: 25 additions & 0 deletions test/unit/page-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,29 @@ QUnit.test( 'pageIndex', function( assert ) {

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

infScroll.destroy();

history.replaceState(null, document.title, 'page?currPage=8');
infScroll = new InfiniteScroll( demoElem, {
path: 'page?currPage={{#}}',
history: false,
scrollThreshold: false,
});

assert.equal( infScroll.pageIndex, 8, 'pageIndex from GET param with {{#}}' );

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

infScroll.destroy();

history.replaceState(null, document.title, 'page?currPage=8');
infScroll = new InfiniteScroll( demoElem, {
path: 'page\\?currPage={{#}}',
history: false,
scrollThreshold: false,
});

assert.equal( infScroll.pageIndex, 8, 'pageIndex from GET param with {{#}} and pre-escaped regexp' );

history.replaceState( null, document.title, prevURL );
});