Skip to content

Commit 650c9f7

Browse files
authored
Merge pull request #1238 from cdrini/fix/urlencode-pagenum
Encode page number in url
2 parents e643489 + 0dedf86 commit 650c9f7

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

src/BookReader.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ BookReader.prototype.paramsFromFragment = function(fragment) {
17881788
// Index and page
17891789
if ('undefined' != typeof(urlHash['page'])) {
17901790
// page was set -- may not be int
1791-
params.page = urlHash['page'];
1791+
params.page = decodeURIComponent(urlHash['page']);
17921792
}
17931793

17941794
// $$$ process /region
@@ -1820,11 +1820,10 @@ BookReader.prototype.paramsFromFragment = function(fragment) {
18201820
* @return {string}
18211821
*/
18221822
BookReader.prototype.fragmentFromParams = function(params, urlMode = 'hash') {
1823-
const separator = '/';
18241823
const fragments = [];
18251824

18261825
if ('undefined' != typeof(params.page)) {
1827-
fragments.push('page', params.page);
1826+
fragments.push('page', encodeURIComponent(params.page));
18281827
} else {
18291828
if ('undefined' != typeof(params.index)) {
18301829
// Don't have page numbering but we do have the index
@@ -1850,10 +1849,10 @@ BookReader.prototype.fragmentFromParams = function(params, urlMode = 'hash') {
18501849

18511850
// search
18521851
if (params.search && urlMode === 'hash') {
1853-
fragments.push('search', params.search);
1852+
fragments.push('search', utils.encodeURIComponentPlus(params.search));
18541853
}
18551854

1856-
return utils.encodeURIComponentPlus(fragments.join(separator)).replace(/%2F/g, '/');
1855+
return fragments.join('/');
18571856
};
18581857

18591858
/**

src/plugins/url/UrlPlugin.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class UrlPlugin {
4545

4646
const strPathParams = this.urlSchema
4747
.filter(s => s.position == 'path')
48-
.map(schema => pathParams[schema.name] ? `${schema.name}/${pathParams[schema.name]}` : '')
48+
.map(schema => pathParams[schema.name] ? `${schema.name}/${encodeURIComponent(pathParams[schema.name])}` : '')
4949
.join('/');
5050

5151
// replace consecutive slashes with a single slash + remove trailing slashes
@@ -83,15 +83,13 @@ export class UrlPlugin {
8383
const hasPropertyKey = doesKeyExists(urlStrSplitSlashObj, schema.name);
8484
const hasDeprecatedKey = doesKeyExists(schema, 'deprecated_for') && hasPropertyKey;
8585

86-
if (hasDeprecatedKey) {
87-
urlState[schema.deprecated_for] = urlStrSplitSlashObj[schema.name];
86+
// Not in the URL
87+
if (!hasPropertyKey && !hasDeprecatedKey) {
8888
return;
8989
}
9090

91-
if (hasPropertyKey) {
92-
urlState[schema.name] = urlStrSplitSlashObj[schema.name];
93-
return;
94-
}
91+
const urlStateParam = hasDeprecatedKey ? schema.deprecated_for : schema.name;
92+
urlState[urlStateParam] = decodeURIComponent(urlStrSplitSlashObj[schema.name]);
9593
});
9694

9795
// Add searchParams to urlState

tests/jest/plugins/url/UrlPlugin.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ describe('UrlPlugin tests', () => {
1919
expect(urlPlugin.urlStateToUrlString(urlStateWithQueries)).toBe(expectedUrlFromStateWithQueries);
2020
});
2121

22+
test('encodes page number', () => {
23+
expect(urlPlugin.urlStateToUrlString({ page: '12/46' })).toBe(`page/12%2F46`);
24+
});
25+
2226
test('urlStateToUrlString with unknown states in schema', () => {
2327
const urlState = { page: 'n7', mode: '1up' };
2428
const urlStateWithQueries = { page: 'n7', mode: '1up', q: 'hello', viewer: 'theater', sortBy: 'title_asc' };
@@ -47,6 +51,10 @@ describe('UrlPlugin tests', () => {
4751
expect(urlPlugin.urlStringToUrlState(url1)).toEqual({page: 'n7', mode: '1up'});
4852
});
4953

54+
test('decodes page number', () => {
55+
expect(urlPlugin.urlStringToUrlState('/page/12%2F46')).toStrictEqual({ page: '12/46' });
56+
});
57+
5058
test('urlStringToUrlState with deprecated_for', () => {
5159
const url = '/page/n7/mode/2up/search/hello';
5260

0 commit comments

Comments
 (0)