Use getTitleOrUrl function to return url if title is empty #485 (#488)

Closes #485 and #475.
This commit is contained in:
Jaifroid 2019-05-04 18:38:16 +01:00 committed by GitHub
parent d69b84d2dc
commit 9ef679e9ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 24 deletions

View File

@ -176,7 +176,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
var callbackFunction = function(dirEntryList) {
assert.ok(dirEntryList && dirEntryList.length === 5, "Article list with 5 results");
var firstDirEntry = dirEntryList[0];
assert.equal(firstDirEntry.title , 'A Fool for You', 'First result should be "A Fool for You"');
assert.equal(firstDirEntry.getTitleOrUrl() , 'A Fool for You', 'First result should be "A Fool for You"');
done();
};
localZimArchive.findDirEntriesWithPrefix('A', 5, callbackFunction);
@ -187,7 +187,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
var callbackFunction = function(dirEntryList) {
assert.ok(dirEntryList && dirEntryList.length === 5, "Article list with 5 results");
var firstDirEntry = dirEntryList[0];
assert.equal(firstDirEntry.title , 'A Fool for You', 'First result should be "A Fool for You"');
assert.equal(firstDirEntry.getTitleOrUrl() , 'A Fool for You', 'First result should be "A Fool for You"');
done();
};
localZimArchive.findDirEntriesWithPrefix('a', 5, callbackFunction);
@ -198,7 +198,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
var callbackFunction = function(dirEntryList) {
assert.ok(dirEntryList && dirEntryList.length === 3, "Article list with 3 result");
var firstDirEntry = dirEntryList[0];
assert.equal(firstDirEntry.title , 'Blues Brothers (film)', 'First result should be "Blues Brothers (film)"');
assert.equal(firstDirEntry.getTitleOrUrl() , 'Blues Brothers (film)', 'First result should be "Blues Brothers (film)"');
done();
};
localZimArchive.findDirEntriesWithPrefix('blues brothers', 5, callbackFunction);
@ -210,11 +210,11 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.title, "(The Night Time Is) The Right Time", "Correct redirect title name.");
assert.equal(dirEntry.getTitleOrUrl(), "(The Night Time Is) The Right Time", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.title, "Night Time Is the Right Time", "Correct redirected title name.");
assert.equal(dirEntry.getTitleOrUrl(), "Night Time Is the Right Time", "Correct redirected title name.");
done();
});
} else {
@ -229,11 +229,11 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.title, "Raelettes", "Correct redirect title name.");
assert.equal(dirEntry.getTitleOrUrl(), "Raelettes", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.title, "The Raelettes", "Correct redirected title name.");
assert.equal(dirEntry.getTitleOrUrl(), "The Raelettes", "Correct redirected title name.");
done();
});
} else {
@ -248,11 +248,11 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.title, "Bein Green", "Correct redirect title name.");
assert.equal(dirEntry.getTitleOrUrl(), "Bein Green", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.title, "Bein' Green", "Correct redirected title name.");
assert.equal(dirEntry.getTitleOrUrl(), "Bein' Green", "Correct redirected title name.");
done();
});
} else {
@ -267,11 +267,11 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.title, "America, the Beautiful", "Correct redirect title name.");
assert.equal(dirEntry.getTitleOrUrl(), "America, the Beautiful", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.title, "America the Beautiful", "Correct redirected title name.");
assert.equal(dirEntry.getTitleOrUrl(), "America the Beautiful", "Correct redirected title name.");
done();
});
} else {
@ -346,7 +346,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
if (dirEntry !== null) {
assert.equal(dirEntry.namespace +"/"+ dirEntry.url, "A/Ray_Charles.html", "URL is correct.");
localZimArchive.readUtf8File(dirEntry, function(dirEntry2, data) {
assert.equal(dirEntry2.title, "Ray Charles", "Title is correct.");
assert.equal(dirEntry2.getTitleOrUrl(), "Ray Charles", "Title is correct.");
assert.equal(data.length, 157186, "Data length is correct.");
assert.equal(data.indexOf("the only true genius in show business"), 5535, "Specific substring at beginning found.");
assert.equal(data.indexOf("Random Access Memories"), 154107, "Specific substring at end found.");
@ -364,7 +364,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.expect(2);
var callbackRandomArticleFound = function(dirEntry) {
assert.ok(dirEntry !== null, "One DirEntry should be found");
assert.ok(dirEntry.title !== null, "The random DirEntry should have a title" );
assert.ok(dirEntry.getTitleOrUrl() !== null, "The random DirEntry should have a title" );
done();
};
@ -375,7 +375,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'uiUtil', 'utf8'],
assert.expect(2);
var callbackMainPageArticleFound = function(dirEntry) {
assert.ok(dirEntry !== null, "Main DirEntry should be found");
assert.equal(dirEntry.title, "Summary", "The main DirEntry should be called Summary" );
assert.equal(dirEntry.getTitleOrUrl(), "Summary", "The main DirEntry should be called Summary" );
done();
};

View File

@ -708,9 +708,8 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
var listLength = dirEntryArray.length < MAX_SEARCH_RESULT_SIZE ? dirEntryArray.length : MAX_SEARCH_RESULT_SIZE;
for (var i = 0; i < listLength; i++) {
var dirEntry = dirEntryArray[i];
var title = dirEntry.title ? dirEntry.title : '[' + dirEntry.url + ']';
articleListDivHtml += '<a href="#" dirEntryId="' + dirEntry.toStringId().replace(/'/g, '&apos;') +
'" class="list-group-item">' + title + '</a>';
'" class="list-group-item">' + dirEntry.getTitleOrUrl() + '</a>';
}
articleListDiv.html(articleListDivHtml);
$('#articleList a').on('click', handleTitleClick);

View File

@ -185,13 +185,7 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
if (dirEntry.namespace < "A") return 1;
if (dirEntry.namespace > "A") return -1;
// We should now be in namespace A
if (dirEntry.title) {
return prefix <= dirEntry.title ? -1 : 1;
} else {
// Some dirEntries (e.g. subtitles) have no title, but are still sorted in the A namespace,
// so, like libzim, we have to use the url as a comparator [kiwix-js #440 #443]
return prefix <= dirEntry.url ? -1 : 1;
}
return prefix <= dirEntry.getTitleOrUrl() ? -1 : 1;
});
}, true).then(function(firstIndex) {
var dirEntries = [];
@ -199,7 +193,7 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
if (index >= firstIndex + resultSize || index >= that._file.articleCount)
return dirEntries;
return that._file.dirEntryByTitleIndex(index).then(function(dirEntry) {
var title = dirEntry.title ? dirEntry.title : dirEntry.url;
var title = dirEntry.getTitleOrUrl();
if (!title.indexOf(prefix) && dirEntry.namespace === "A")
dirEntries.push(dirEntry);
return addDirEntries(index + 1);

View File

@ -107,6 +107,16 @@ define([], function() {
return new DirEntry(zimfile, data);
};
/**
* Defines a function that returns the URL if the title is empty, as per the specification
* See https://wiki.openzim.org/wiki/ZIM_file_format#Directory_Entries
*
* @returns {String} The dirEntry's title or, if empty, the dirEntry's (unescaped) URL
*/
DirEntry.prototype.getTitleOrUrl = function() {
return this.title ? this.title : this.url;
};
/**
* Functions and classes exposed by this module
*/