Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down Expand Up @@ -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('#')
Expand Down
10 changes: 5 additions & 5 deletions epubcfi.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,20 @@ 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 })]))
}
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 = {
Expand Down
Loading