mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
Fix wrong TOC item retruns by Navigation.get() (#831)
This commit is contained in:
parent
a6bbf71363
commit
920846bda2
2 changed files with 30 additions and 1 deletions
|
@ -93,7 +93,34 @@ class Navigation {
|
|||
index = this.tocByHref[target];
|
||||
}
|
||||
|
||||
return this.toc[index];
|
||||
return this.getByIndex(target, index, this.toc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from navigation subitems recursively by index
|
||||
* @param {string} target
|
||||
* @param {number} index
|
||||
* @param {array} navItems
|
||||
* @return {object} navItem
|
||||
*/
|
||||
getByIndex(target, index, navItems) {
|
||||
if (navItems.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const item = navItems[index];
|
||||
if (item && (target === item.id || target === item.href)) {
|
||||
return item;
|
||||
} else {
|
||||
let result;
|
||||
for (let i = 0; i < navItems.length; ++i) {
|
||||
result = this.getByIndex(target, index, navItems[i].subitems);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
2
types/navigation.d.ts
vendored
2
types/navigation.d.ts
vendored
|
@ -41,4 +41,6 @@ export default class Navigation {
|
|||
private parseNcx(navHtml: XMLDocument): Array<NavItem>;
|
||||
|
||||
private ncxItem(item: Element): NavItem;
|
||||
|
||||
private getByIndex(target: string, index: number, navItems: NavItem[]): NavItem;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue