diff --git a/epub.js b/epub.js index e8bd703..24312c2 100644 --- a/epub.js +++ b/epub.js @@ -685,20 +685,20 @@ class Resources { getItemByProperty(prop) { return this.manifest.find(item => item.properties?.includes(prop)) } - resolveCFI(cfi) { + resolveCFI(cfi, filter) { const parts = CFI.parse(cfi) const top = (parts.parent ?? parts).shift() - let $itemref = CFI.toElement(this.opf, top) + let $itemref = CFI.toElement(this.opf, top, filter) // make sure it's an idref; if not, try again without the ID assertion // mainly because Epub.js used to generate wrong ID assertions // https://github.com/futurepress/epub.js/issues/1236 if ($itemref && $itemref.nodeName !== 'idref') { top.at(-1).id = null - $itemref = CFI.toElement(this.opf, top) + $itemref = CFI.toElement(this.opf, top, filter) } const idref = $itemref?.getAttribute('idref') const index = this.spine.findIndex(item => item.idref === idref) - const anchor = doc => CFI.toRange(doc, parts) + const anchor = doc => CFI.toRange(doc, parts, filter) return { index, anchor } } } @@ -1042,8 +1042,8 @@ ${doc.querySelector('parsererror').innerText}`) getMediaOverlay() { return new MediaOverlay(this, this.#loadXML.bind(this)) } - resolveCFI(cfi) { - return this.resources.resolveCFI(cfi) + resolveCFI(cfi, filter) { + return this.resources.resolveCFI(cfi, filter) } resolveHref(href) { const [path, hash] = href.split('#') diff --git a/epubcfi.js b/epubcfi.js index 712ead5..da3481d 100644 --- a/epubcfi.js +++ b/epubcfi.js @@ -314,11 +314,11 @@ export const toRange = (doc, parts, filter) => { } // faster way of getting CFIs for sorted elements in a single parent -export const fromElements = elements => { +export const fromElements = (elements, filter) => { const results = [] const { parentNode } = elements[0] - const parts = nodeToParts(parentNode) - for (const [index, node] of indexChildNodes(parentNode).entries()) { + const parts = nodeToParts(parentNode, filter) + for (const [index, node] of indexChildNodes(parentNode, filter).entries()) { const el = elements[results.length] if (node === el) results.push(toString([parts.concat({ id: el.id, index })])) @@ -326,8 +326,8 @@ export const fromElements = elements => { return results } -export const toElement = (doc, parts) => - partsToNode(doc.documentElement, collapse(parts)).node +export const toElement = (doc, parts, filter) => + partsToNode(doc.documentElement, collapse(parts), filter).node // turn indices into standard CFIs when you don't have an actual package document export const fake = {