Skip to content

Epub3 nav subitems not being parsed properly #427

@lenilsondc

Description

@lenilsondc

I found that table of contents of EPUB3 nav subitems wasn't being parsed properly, but NCX was ok.
The following code was taken from parser.js and I'm almost hundred percent sure is the cause of problem

parser.js

//...
EPUBJS.Parser.prototype.nav = function(navHtml, spineIndexByURL, bookSpine){
    var navElement = this.querySelectorByType(navHtml, "nav", "toc");
    var navItems = navElement ? navElement.querySelectorAll("ol li") : [];
    var length = navItems.length;
    var i;
    var toc = {};
    var list = [];
    var item, parent;

    if(!navItems || length === 0) return list;

    for (i = 0; i < length; ++i) {
        item = this.navItem(navItems[i], spineIndexByURL, bookSpine);
        toc[item.id] = item;
        if(!item.parent) {
            list.push(item);
        } else {
            parent = toc[item.parent];
            parent.subitems.push(item);
        }
    }

    return list;
};

EPUBJS.Parser.prototype.navItem = function(item, spineIndexByURL, bookSpine){
    var id = item.getAttribute('id') || false,
            content = item.querySelector("a, span"),
            src = content.getAttribute('href') || '',
            text = content.textContent || "",
            split = src.split("#"),
            baseUrl = split[0],
            spinePos = spineIndexByURL[baseUrl],
            spineItem = bookSpine[spinePos],
            subitems = [],
            parentNode = item.parentNode,
            parent,
            cfi = spineItem ? spineItem.cfi : '';

    if(parentNode && parentNode.nodeName === "navPoint") {
        parent = parentNode.getAttribute('id');
    }

    if(!id) {
        if(spinePos) {
            spineItem = bookSpine[spinePos];
            id = spineItem.id;
            cfi = spineItem.cfi;
        } else {
            id = 'epubjs-autogen-toc-id-' + EPUBJS.core.uuid();
            item.setAttribute('id', id);
        }
    }

    return {
        "id": id,
        "href": src,
        "label": text,
        "spinePos": spinePos,
        "subitems" : subitems,
        "parent" : parent,
        "cfi" : cfi
    };
};
//...

As you can see, the nav parser is using NCX's layout assumptions for Epup3 toc. For example, when defining the parent item of a subitem, it asks for a parent node named "navPoint" but on a nav toc, according to 2.2.4 EPUB Navigation Document Definition, it's suposed to be an <li>.

//...
if(parentNode && parentNode.nodeName === "navPoint") {
        parent = parentNode.getAttribute('id');
    }
//...

In the end, it never creates a subitem because is not possible determine when an item has a parent item.

Does it proceeds or am I mistaken?

Sorry by the english mistakes :{D

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions