Merge pull request #346 from kiwix/accept-links-with-anchors

Accept links to articles that have an anchor.
This commit is contained in:
Mossroy 2018-03-19 20:58:38 +01:00 committed by GitHub
commit b1118a9747
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 14 deletions

View File

@ -36,7 +36,7 @@ self.addEventListener('activate', function(event) {
console.log("ServiceWorker activated");
});
var regexpRemoveUrlParameters = new RegExp(/([^\?]+)\?.*$/);
var regexpRemoveUrlParameters = new RegExp(/([^?#]+)[?#].*$/);
// This function is duplicated from uiUtil.js
// because using requirejs would force to add the 'fetch' event listener
@ -44,12 +44,14 @@ var regexpRemoveUrlParameters = new RegExp(/([^\?]+)\?.*$/);
// in recent versions of the browsers.
// Cf https://bugzilla.mozilla.org/show_bug.cgi?id=1181127
// TODO : find a way to avoid this duplication
/**
* Removes parameters and anchors from a URL
* @param {type} url
* @returns {String} same URL without its parameters and anchors
*/
function removeUrlParameters(url) {
if (regexpRemoveUrlParameters.test(url)) {
return regexpRemoveUrlParameters.exec(url)[1];
} else {
return url;
}
return url.replace(regexpRemoveUrlParameters, "$1");
}
console.log("ServiceWorker startup");

View File

@ -19,8 +19,8 @@
* You should have received a copy of the GNU General Public License
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/
define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
function($, zimArchive, zimDirEntry, util, utf8) {
define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
function($, zimArchive, zimDirEntry, util, uiUtil, utf8) {
var localZimArchive;
@ -119,6 +119,16 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
var expectedArray = [{title:"a"}, {title:"b"}, {title:"c"}, {title:"d"}];
assert.deepEqual(util.removeDuplicateTitlesInDirEntryArray(array), expectedArray, "Duplicates should be removed from the array");
});
QUnit.test("check removal of parameters in URL", function(assert) {
var testUrl1 = "A/question.html";
var testUrl2 = "A/question.html?param1=toto&param2=titi";
var testUrl3 = "A/question.html?param1=toto&param2=titi#anchor";
var testUrl4 = "A/question.html#anchor";
assert.equal(uiUtil.removeUrlParameters(testUrl1), testUrl1);
assert.equal(uiUtil.removeUrlParameters(testUrl2), testUrl1);
assert.equal(uiUtil.removeUrlParameters(testUrl3), testUrl1);
assert.equal(uiUtil.removeUrlParameters(testUrl4), testUrl1);
});
QUnit.module("ZIM initialisation");
QUnit.test("ZIM archive is ready", function(assert) {

View File

@ -1000,6 +1000,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
* @param {String} title
*/
function goToArticle(title) {
title = uiUtil.removeUrlParameters(title);
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
if (dirEntry === null || dirEntry === undefined) {
$("#readingArticle").hide();

View File

@ -73,14 +73,15 @@ define([], function() {
link.replaceWith(cssElement);
}
var regexpRemoveUrlParameters = new RegExp(/([^\?]+)\?.*$/);
var regexpRemoveUrlParameters = new RegExp(/([^?#]+)[?#].*$/);
/**
* Removes parameters and anchors from a URL
* @param {type} url
* @returns {String} same URL without its parameters and anchors
*/
function removeUrlParameters(url) {
if (regexpRemoveUrlParameters.test(url)) {
return regexpRemoveUrlParameters.exec(url)[1];
} else {
return url;
}
return url.replace(regexpRemoveUrlParameters, "$1");
}
/**