Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use browser's path for br-resume coookie #359

Merged
merged 6 commits into from
Jul 9, 2020
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
23 changes: 19 additions & 4 deletions src/js/plugins/plugin.resume.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BookReader.prototype.init = (function(super_) {
})(BookReader.prototype.init);

/**
* Get's the page resume value, for remembering reader's page
* Gets page resume value, for remembering reader's page
* Can be overriden for different implementation
*
* @return {number|null}
Expand All @@ -40,14 +40,29 @@ BookReader.prototype.getResumeValue = function() {
}

/**
* Set's the page resume value, for remembering reader's page
* Return cookie path using pathname up to /page/... or /mode/...
* using window.location.pathname for urlPathPart:
* - matches encoding
* - ignores querystring part
* - ignores fragment part (after #)
* @param {string} urlPathPart - window.location.pathname
*/
BookReader.prototype.getCookiePath = function(urlPathPart) {
return urlPathPart.match('.+?(?=/page/|/mode/|$)')[0];
mc2 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets page resume value, for remembering reader's page
* Can be overriden for different implementation
*
* @param {Number} index leaf index
* @param {string} [cookieName]
*/
BookReader.prototype.updateResumeValue = function(index, cookieName) {
const ttl = new Date(+new Date + 12096e5); // 2 weeks
const path = this.options.resumeCookiePath || window.location.pathname;
// For multiple files in item, leave resumeCookiePath blank
// It's likely we can remove resumeCookiePath using getCookiePath()
const path = this.options.resumeCookiePath
|| this.getCookiePath(window.location.pathname);
BookReader.docCookies.setItem(cookieName || 'br-resume', index, ttl, path, null, false);
}
}
37 changes: 33 additions & 4 deletions tests/plugins/plugin.resume.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,40 @@ describe('updateResumeValue', () => {
});

test('handles resumeCookiePath not set', () => {
const { updateResumeValue } = BookReader.prototype;
const setItemSpy = sinon.spy(docCookies, 'setItem');
const fakeBr = { options: { } };
// Save function
const saveFn = br.getCookiePath;
br.getCookiePath = jest.fn(() => '/details/foo');
br.updateResumeValue(16);
expect(setItemSpy.args[0][3]).toEqual('/details/foo');
// Restore function
br.getCookiePath = saveFn;
});

updateResumeValue.call(fakeBr, 16);
expect(setItemSpy.args[0][3]).toEqual('/');
test('handles cookie path from URL with decoration', () => {
const complexPathWithPage = '/details/2008ELMValidityStudyFinalReportRevised/Executive%20Summary%20for%20the%20EPT%26ELM%20Validity%20Studie_20100603%20-%20Copy/page/n1/mode/2up';
const complexPath = '/details/2008ELMValidityStudyFinalReportRevised/Executive%20Summary%20for%20the%20EPT%26ELM%20Validity%20Studie_20100603%20-%20Copy';
mc2 marked this conversation as resolved.
Show resolved Hide resolved
expect(br.getCookiePath(complexPathWithPage))
.toEqual(complexPath);

expect(br.getCookiePath('/details/item/mode/1up'))
.toEqual('/details/item');

expect(br.getCookiePath('/details/item/inside/a/long/path/model/is/used'))
.toEqual('/details/item/inside/a/long/path/model/is/used');

expect(br.getCookiePath('/details/item/inside/a/long/path/mode/is/used'))
.toEqual('/details/item/inside/a/long/path');
});

test('handles cookie path from URL with no decoration', () => {
expect(br.getCookiePath('/details/item'))
.toEqual('/details/item');

expect(br.getCookiePath('/details/item/'))
.toEqual('/details/item/');

expect(br.getCookiePath('/details/item/almost/any/kind/of/long/path'))
.toEqual('/details/item/almost/any/kind/of/long/path');
});
});