@readium/shared 2.3.0 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2022, Readium Foundation
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -1 +1 @@
1
- import{getTime as g,getHtmlId as k}from"../../html/Locations.js";import{Profile as u}from"../../Profiles.js";import{isNptStartOfResource as v,formatNptTime as I,parseNptTime as T}from"../../../util/npt.js";class l{constructor(e,t,i=[],s=[],n=void 0){this._trimmedLinkMap=new Map,this._allItems=e,this.linkMap=t,this._conformsTo=i,this.tocLinks=s,this._tocDepth=n}static build(e,t={}){const i=e.toc?.items??[],s=e.readingOrder.items,{depth:n}=t,o=e.metadata?.conformsTo??[],r=new Map,c=[];for(let h=0;h<s.length;h++){const f=s[h],a=l.bareHref(f.href),p=f.title??l.findTitleInToc(i,a,n),d=l.collectChildrenFromToc(i,a,n,1,r),m={title:p,references:[f.href],children:d.length>0?d:void 0};r.set(m,f),c.push(m)}return new l(c,r,o,i,n)}augment(e){for(const t of this.flatAll){const i=this.linkFor(t);if(!i)continue;const s=e(t,i);s.position!==void 0&&(t.position=s.position),s.scroll!==void 0&&(t.scroll=s.scroll),s.role!==void 0&&(t.role=s.role)}}get depth(){return this._depth}set depth(e){this._depth!==e&&(this._depth=e,this._tocDepth=e,this._items=void 0,this._flat=void 0,this._toc=void 0)}get items(){return this._items||(this._depth!==void 0?(this._trimmedLinkMap=new Map,this._items=l.trimToDepth(this._allItems,this._depth,this.linkMap,this._trimmedLinkMap)):this._items=this._allItems),this._items}locate(e){const t=e.href.split("#")[0],i=g(e.locations),s=k(e.locations),n=e.locations.progression;if(i!==void 0){let o=-1/0,r;for(const c of this.flat){if(!this.itemMatchesHref(c,t))continue;const h=this.itemStartTime(c,t);h!==void 0&&h<=i&&h>o&&(o=h,r=c)}if(r)return r}if(s){const o=this.flat.find(r=>this.itemMatchesHref(r,t)?r.references.some(c=>c.split("#")[1]===s):!1);if(o)return o}if(n!==void 0){let o=-1/0,r;for(const h of this.flat){if(!this.itemMatchesHref(h,t))continue;const f=this.itemScrollPosition(h);f!==void 0&&f<=n&&f>o&&(o=f,r=h)}if(r)return r;const c=this.items.find(h=>this.itemMatchesHref(h,t));if(c?.children?.length&&!c.children.some(h=>h.scroll!==void 0)){const h=Math.min(Math.floor(n*c.children.length),c.children.length-1);return c.children[h]}}return this.flat.find(o=>this.itemMatchesHref(o,t))}adjacentTo(e){const t=this.flat.indexOf(e);return{previous:t>0?this.flat[t-1]:void 0,next:t>=0&&t<this.flat.length-1?this.flat[t+1]:void 0}}segmentsForHref(e){const t=e.split("#")[0],i=this.items.find(s=>this.itemMatchesHref(s,t));return i?i.children?.length?i.children:[i]:[]}ancestors(e){return this.ancestorPath(this.items,e)??[]}linkFor(e){return this.linkMap.get(e)??this._trimmedLinkMap.get(e)}get contextualizedToc(){return this._toc||(this._toc=this.tocLinks.length>0?this.buildTocEntries(this.tocLinks,this._tocDepth,1):this._allItems.map(e=>this.entryFor(this.linkFor(e),e))),this._toc}tocEntryFor(e){const t=this.linkFor(e);if(!t)return;const i=this.findTocEntryByLink(this.contextualizedToc,t);return i||this.nearestTocEntryForResource(t.href,e)}get flat(){return this._flat||(this._flat=this.flattenItems(this.items)),this._flat}get flatAll(){return this.flattenItems(this._allItems)}static findTitleInToc(e,t,i){const{atStart:s,fragments:n}=l.collectTocCandidates(e,t,i,1);if(s.length>0)return s[0].title;if(n.length===1)return n[0].title}static collectChildrenFromToc(e,t,i,s,n){if(i!==void 0&&s>i)return[];const o=[];for(const r of e){const c=l.bareHref(r.href);if((c===t||c==="")&&r.title&&!l.isStartOfResource(r.href)){const f=c===""?t+r.href:r.href,a={title:r.title,references:[f]};n.set(a,r),o.push(a)}r.children?.items?.length&&o.push(...l.collectChildrenFromToc(r.children.items,t,i,s+1,n))}return o}static collectTocCandidates(e,t,i,s){if(i!==void 0&&s>i)return{atStart:[],fragments:[]};const n=[],o=[];for(const r of e){const c=l.bareHref(r.href);if((c===t||c==="")&&r.title&&(l.isStartOfResource(r.href)?n.push(r):o.push(r)),r.children?.items?.length){const h=l.collectTocCandidates(r.children.items,t,i,s+1);n.push(...h.atStart),o.push(...h.fragments)}}return{atStart:n,fragments:o}}static isStartOfResource(e){const t=e.split("#")[1];if(!t)return!0;const i=t.match(/(?:^|&)t=([^&]+)/);return i!==null&&v(i[1])}get linkToItem(){if(!this._linkToItem){this._linkToItem=new Map;for(const[e,t]of this.linkMap)this._linkToItem.set(t,e)}return this._linkToItem}buildTocEntries(e,t,i){return t!==void 0&&i>t?[]:e.map(s=>{const n=this.linkToItem.get(s)??this.resourceStartItem(s.href),o=s.children?.items?.length?this.buildTocEntries(s.children.items,t,i+1):[];return{...this.entryFor(s,n),children:o.length>0?o:void 0}})}tocLinksFlat(e,t,i){if(t!==void 0&&i>t)return[];const s=[];for(const n of e)s.push(n),n.children?.items?.length&&s.push(...this.tocLinksFlat(n.children.items,t,i+1));return s}entryFor(e,t){const i=this._conformsTo.includes(u.AUDIOBOOK);return{link:e,position:!i&&t?.position!==void 0?String(t.position):void 0,timestamp:i&&t?.position!==void 0?I(t.position):void 0}}resourceStartItem(e){const t=l.bareHref(e);return this._allItems.find(i=>this.itemMatchesHref(i,t))}findTocEntryByLink(e,t){for(const i of e){if(i.link===t)return i;if(i.children){const s=this.findTocEntryByLink(i.children,t);if(s)return s}}}nearestTocEntryForResource(e,t){const i=l.bareHref(e),s=this.tocLinksFlat(this.tocLinks,this._tocDepth,1).filter(f=>l.bareHref(f.href)===i).map(f=>({link:f,item:this.linkToItem.get(f)??this.resourceStartItem(f.href)}));if(s.length===0)return;const n=this._conformsTo.includes(u.AUDIOBOOK),o=n?t.position??0:t.scroll??0;let r,c=-1/0;for(const f of s){const a=n?f.item?.position??0:f.item?.scroll??0;a<=o&&a>c&&(c=a,r=f)}const h=r??s[0];return this.findTocEntryByLink(this.contextualizedToc,h.link)}static trimToDepth(e,t,i,s){return e.map(n=>{const o={...n,children:t>1&&n.children?.length?l.trimToDepth(n.children,t-1,i,s):void 0},r=i.get(n);return r&&s.set(o,r),o})}flattenItems(e){const t=[];for(const i of e)t.push(i),i.children&&t.push(...this.flattenItems(i.children));return t}itemStartTime(e,t){for(const i of e.references){const s=i.indexOf("#"),n=s>=0?i.slice(0,s):i,o=s>=0?i.slice(s+1):void 0;if((n||t)!==t)continue;if(!o)return;const c=o.match(/(?:^|&)t=([^&]+)/);return c?T(c[1]):void 0}}itemMatchesHref(e,t){return e.references.some(i=>{const s=i.split("#")[0];return s===t||s===""})}ancestorPath(e,t){for(const i of e){if(i===t)return[];if(i.children){const s=this.ancestorPath(i.children,t);if(s!==null)return[i,...s]}}return null}itemScrollPosition(e){return e.scroll}static bareHref(e){return e.split("#")[0]}}export{l as Timeline};
1
+ import{getTime as g,getHtmlId as v}from"../../html/Locations.js";import{Profile as p}from"../../Profiles.js";import{isNptStartOfResource as k,formatNptTime as I,parseNptTime as T}from"../../../util/npt.js";class l{constructor(i,t,e=[],s=[],n=void 0){this._trimmedLinkMap=new Map,this._allItems=i,this.linkMap=t,this._conformsTo=e,this.tocLinks=s,this._tocDepth=n}static build(i,t={}){const e=i.toc?.items??[],s=i.readingOrder.items,{depth:n}=t,o=i.metadata?.conformsTo??[],r=new Map,c=[];for(let h=0;h<s.length;h++){const f=s[h],a=l.bareHref(f.href),d=f.title??l.findTitleInToc(e,a,n),u=l.collectChildrenFromToc(e,a,n,1,r),m={title:d,references:[f.href],children:u.length>0?u:void 0};r.set(m,f),c.push(m)}return new l(c,r,o,e,n)}augment(i){for(const t of this.flatAll){const e=this.linkFor(t);if(!e)continue;const s=i(t,e);s.position!==void 0&&(t.position=s.position),s.scroll!==void 0&&(t.scroll=s.scroll),s.role!==void 0&&(t.role=s.role)}}get depth(){return this._depth}set depth(i){this._depth!==i&&(this._depth=i,this._tocDepth=i,this._items=void 0,this._flat=void 0,this._toc=void 0)}get items(){return this._items||(this._depth!==void 0?(this._trimmedLinkMap=new Map,this._items=l.trimToDepth(this._allItems,this._depth,this.linkMap,this._trimmedLinkMap)):this._items=this._allItems),this._items}locate(i){const t=i.href.split("#")[0],e=g(i.locations),s=v(i.locations),n=i.locations.progression;if(e!==void 0){let o=-1/0,r;for(const c of this.flat){if(!this.itemMatchesHref(c,t))continue;const h=this.itemStartTime(c,t);h!==void 0&&h<=e&&h>o&&(o=h,r=c)}if(r)return r}if(s){const o=this.flat.find(r=>this.itemMatchesHref(r,t)?r.references.some(c=>c.split("#")[1]===s):!1);if(o)return o}if(n!==void 0){let o=-1/0,r;for(const h of this.flat){if(!this.itemMatchesHref(h,t))continue;const f=this.itemScrollPosition(h);f!==void 0&&f<=n&&f>o&&(o=f,r=h)}if(r)return r;const c=this.items.find(h=>this.itemMatchesHref(h,t));if(c?.children?.length&&!c.children.some(h=>h.scroll!==void 0)){const h=Math.min(Math.floor(n*c.children.length),c.children.length-1);return c.children[h]}}return this.flat.find(o=>this.itemMatchesHref(o,t))}adjacentTo(i){const t=this.flat.indexOf(i);return{previous:t>0?this.flat[t-1]:void 0,next:t>=0&&t<this.flat.length-1?this.flat[t+1]:void 0}}segmentsForHref(i){const t=i.split("#")[0],e=this.items.find(s=>this.itemMatchesHref(s,t));return e?e.children?.length?e.children:[e]:[]}ancestors(i){return this.ancestorPath(this.items,i)??[]}linkFor(i){return this.linkMap.get(i)??this._trimmedLinkMap.get(i)}get contextualizedToc(){return this._toc||(this._toc=this.tocLinks.length>0?this.buildTocEntries(this.tocLinks,this._tocDepth,1):this._allItems.map(i=>this.entryFor(this.linkFor(i),i))),this._toc}tocEntryFor(i){const t=this.linkFor(i);if(!t)return;const e=this.findTocEntryByLink(this.contextualizedToc,t);if(e)return e;const s=this.nearestTocEntryForResource(t.href,i);return s||this.previousResolvedTocEntry(t)}get flat(){return this._flat||(this._flat=this.flattenItems(this.items)),this._flat}get flatAll(){return this.flattenItems(this._allItems)}static findTitleInToc(i,t,e){const{atStart:s,fragments:n}=l.collectTocCandidates(i,t,e,1);if(s.length>0)return s[0].title;if(n.length===1)return n[0].title}static collectChildrenFromToc(i,t,e,s,n){if(e!==void 0&&s>e)return[];const o=[];for(const r of i){const c=l.bareHref(r.href),h=r.href.startsWith("#");if((c===t||h&&c==="")&&r.title&&!l.isStartOfResource(r.href)){const a=c===""?t+r.href:r.href,d={title:r.title,references:[a]};n.set(d,r),o.push(d)}r.children?.items?.length&&o.push(...l.collectChildrenFromToc(r.children.items,t,e,s+1,n))}return o}static collectTocCandidates(i,t,e,s){if(e!==void 0&&s>e)return{atStart:[],fragments:[]};const n=[],o=[];for(const r of i){const c=l.bareHref(r.href),h=r.href.startsWith("#");if((c===t||h&&c==="")&&r.title&&(l.isStartOfResource(r.href)?n.push(r):o.push(r)),r.children?.items?.length){const f=l.collectTocCandidates(r.children.items,t,e,s+1);n.push(...f.atStart),o.push(...f.fragments)}}return{atStart:n,fragments:o}}static isStartOfResource(i){const t=i.split("#")[1];if(!t)return!0;const e=t.match(/(?:^|&)t=([^&]+)/);return e!==null&&k(e[1])}get linkToItem(){if(!this._linkToItem){this._linkToItem=new Map;for(const[i,t]of this.linkMap)this._linkToItem.set(t,i)}return this._linkToItem}buildTocEntries(i,t,e){return t!==void 0&&e>t?[]:i.map(s=>{const n=this.linkToItem.get(s)??this.resourceStartItem(s.href),o=s.children?.items?.length?this.buildTocEntries(s.children.items,t,e+1):[];return{...this.entryFor(s,n),children:o.length>0?o:void 0}})}tocLinksFlat(i,t,e){if(t!==void 0&&e>t)return[];const s=[];for(const n of i)s.push(n),n.children?.items?.length&&s.push(...this.tocLinksFlat(n.children.items,t,e+1));return s}entryFor(i,t){const e=this._conformsTo.includes(p.AUDIOBOOK);return{link:i,position:!e&&t?.position!==void 0?String(t.position):void 0,timestamp:e&&t?.position!==void 0?I(t.position):void 0}}resourceStartItem(i){const t=l.bareHref(i);return this._allItems.find(e=>this.itemMatchesHref(e,t))}findTocEntryByLink(i,t){for(const e of i){if(e.link===t)return e;if(e.children){const s=this.findTocEntryByLink(e.children,t);if(s)return s}}}nearestTocEntryForResource(i,t){const e=l.bareHref(i),s=this.tocLinksFlat(this.tocLinks,this._tocDepth,1).filter(f=>l.bareHref(f.href)===e).map(f=>({link:f,item:this.linkToItem.get(f)??this.resourceStartItem(f.href)}));if(s.length===0)return;const n=this._conformsTo.includes(p.AUDIOBOOK),o=n?t.position??0:t.scroll??0;let r,c=-1/0;for(const f of s){const a=n?f.item?.position??0:f.item?.scroll??0;a<=o&&a>c&&(c=a,r=f)}const h=r??s[0];return this.findTocEntryByLink(this.contextualizedToc,h.link)}previousResolvedTocEntry(i){const t=this._allItems.findIndex(e=>this.linkFor(e)===i);if(t!==-1)for(let e=t-1;e>=0;e--){const s=this.linkFor(this._allItems[e]);if(!s)continue;const n=l.bareHref(s.href),{atStart:o,fragments:r}=l.collectTocCandidates(this.tocLinks,n,this._tocDepth,1),c=o.filter(d=>l.bareHref(d.href)===n),h=r.filter(d=>l.bareHref(d.href)===n),f=c[0]??(h.length>0?h[h.length-1]:void 0);if(!f)continue;const a=this.findTocEntryByLink(this.contextualizedToc,f);if(a)return a}}static trimToDepth(i,t,e,s){return i.map(n=>{const o={...n,children:t>1&&n.children?.length?l.trimToDepth(n.children,t-1,e,s):void 0},r=e.get(n);return r&&s.set(o,r),o})}flattenItems(i){const t=[];for(const e of i)t.push(e),e.children&&t.push(...this.flattenItems(e.children));return t}itemStartTime(i,t){for(const e of i.references){const s=e.indexOf("#"),n=s>=0?e.slice(0,s):e,o=s>=0?e.slice(s+1):void 0;if((n||t)!==t)continue;if(!o)return;const c=o.match(/(?:^|&)t=([^&]+)/);return c?T(c[1]):void 0}}itemMatchesHref(i,t){return i.references.some(e=>{const s=e.split("#")[0];return s===t||s===""})}ancestorPath(i,t){for(const e of i){if(e===t)return[];if(e.children){const s=this.ancestorPath(e.children,t);if(s!==null)return[e,...s]}}return null}itemScrollPosition(i){return i.scroll}static bareHref(i){return i.split("#")[0]}}export{l as Timeline};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@readium/shared",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "type": "module",
5
5
  "description": "Shared models to be used across other Readium projects and implementations in Typescript",
6
6
  "author": "readium",
@@ -63,14 +63,6 @@
63
63
  "engines": {
64
64
  "node": ">=18"
65
65
  },
66
- "scripts": {
67
- "dev": "vite",
68
- "clean": "rimraf types dist",
69
- "build": "pnpm clean && tsc && vite build",
70
- "test": "jest",
71
- "check-locales": "node scripts/check-locales.mjs",
72
- "update-locales": "node scripts/update-locales.mjs && node scripts/check-locales.mjs"
73
- },
74
66
  "devDependencies": {
75
67
  "@babel/core": "^7.28.6",
76
68
  "@babel/preset-env": "^7.28.6",
@@ -84,5 +76,13 @@
84
76
  "tslib": "^2.8.1",
85
77
  "typescript": "^5.9.3",
86
78
  "vite": "^7.3.1"
79
+ },
80
+ "scripts": {
81
+ "dev": "vite",
82
+ "clean": "rimraf types dist",
83
+ "build": "pnpm clean && tsc && vite build",
84
+ "test": "jest",
85
+ "check-locales": "node scripts/check-locales.mjs",
86
+ "update-locales": "node scripts/update-locales.mjs && node scripts/check-locales.mjs"
87
87
  }
88
- }
88
+ }
@@ -267,10 +267,12 @@ export class Timeline {
267
267
  }
268
268
 
269
269
  /**
270
- * Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry`:
271
- * a direct match first, falling back to the nearest preceding toc entry
272
- * for that resource when there's no exact match (e.g. a mid-resource
273
- * audio position between chapter markers).
270
+ * Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry`,
271
+ * trying three fallbacks in order:
272
+ * 1. A direct match on `current`'s own link.
273
+ * 2. The nearest preceding toc entry within the same resource.
274
+ * 3. When no toc entry references the resource at all, the nearest
275
+ * preceding resource's toc entry.
274
276
  */
275
277
  tocEntryFor(current: TimelineItem): ContextualizedTocEntry | undefined {
276
278
  const link = this.linkFor(current);
@@ -279,7 +281,10 @@ export class Timeline {
279
281
  const direct = this.findTocEntryByLink(this.contextualizedToc, link);
280
282
  if (direct) return direct;
281
283
 
282
- return this.nearestTocEntryForResource(link.href, current);
284
+ const nearest = this.nearestTocEntryForResource(link.href, current);
285
+ if (nearest) return nearest;
286
+
287
+ return this.previousResolvedTocEntry(link);
283
288
  }
284
289
 
285
290
  private get flat(): TimelineItem[] {
@@ -334,8 +339,11 @@ export class Timeline {
334
339
  for (const link of tocLinks) {
335
340
  const linkBare = Timeline.bareHref(link.href);
336
341
  // Fragment-only hrefs (e.g. "#t=60" in single-track audio) have no bare href,
337
- // so they are associated with the current resource.
338
- const matchesBare = linkBare === bare || linkBare === '';
342
+ // so they are associated with the current resource. A hrefless link (e.g. a
343
+ // <span> grouping heading) also bareHrefs to '', but names no resource at all
344
+ // and must not match every resource.
345
+ const isFragmentOnly = link.href.startsWith('#');
346
+ const matchesBare = linkBare === bare || (isFragmentOnly && linkBare === '');
339
347
  if (matchesBare && link.title && !Timeline.isStartOfResource(link.href)) {
340
348
  // Prepend the resource href when the link uses a fragment-only reference.
341
349
  const ref = linkBare === '' ? bare + link.href : link.href;
@@ -377,7 +385,8 @@ export class Timeline {
377
385
 
378
386
  for (const link of tocLinks) {
379
387
  const linkBare = Timeline.bareHref(link.href);
380
- if ((linkBare === bare || linkBare === '') && link.title) {
388
+ const isFragmentOnly = link.href.startsWith('#');
389
+ if ((linkBare === bare || (isFragmentOnly && linkBare === '')) && link.title) {
381
390
  if (Timeline.isStartOfResource(link.href)) {
382
391
  atStart.push(link);
383
392
  } else {
@@ -470,6 +479,11 @@ export class Timeline {
470
479
  return undefined;
471
480
  }
472
481
 
482
+ /**
483
+ * Tier-2 fallback for `tocEntryFor`: among the toc entries that reference
484
+ * `href`'s own resource (e.g. chapter markers within one audio file),
485
+ * return the nearest one at or before `current`'s position/scroll.
486
+ */
473
487
  private nearestTocEntryForResource(href: string, current: TimelineItem): ContextualizedTocEntry | undefined {
474
488
  const bare = Timeline.bareHref(href);
475
489
  // Re-walk with raw items (not the display-formatted TocEntry tree) so we can compare
@@ -491,6 +505,33 @@ export class Timeline {
491
505
  return this.findTocEntryByLink(this.contextualizedToc, chosen.link);
492
506
  }
493
507
 
508
+ /**
509
+ * Tier-3 fallback for `tocEntryFor`: when no toc entry references
510
+ * `href`'s resource at all, walk backward through the reading order and
511
+ * return the nearest preceding resource's toc entry.
512
+ */
513
+ private previousResolvedTocEntry(link: Link): ContextualizedTocEntry | undefined {
514
+ const index = this._allItems.findIndex(item => this.linkFor(item) === link);
515
+ if (index === -1) return undefined;
516
+
517
+ for (let i = index - 1; i >= 0; i--) {
518
+ const precedingLink = this.linkFor(this._allItems[i]);
519
+ if (!precedingLink) continue;
520
+ const precedingBare = Timeline.bareHref(precedingLink.href);
521
+ const { atStart, fragments } = Timeline.collectTocCandidates(this.tocLinks, precedingBare, this._tocDepth, 1);
522
+ // Exclude fragment-only entries (bareHref "") — collectTocCandidates
523
+ // treats them as matching any bare href for single-track audio, but
524
+ // here they must belong to this specific preceding resource.
525
+ const ownAtStart = atStart.filter(l => Timeline.bareHref(l.href) === precedingBare);
526
+ const ownFragments = fragments.filter(l => Timeline.bareHref(l.href) === precedingBare);
527
+ const chosen = ownAtStart[0] ?? (ownFragments.length > 0 ? ownFragments[ownFragments.length - 1] : undefined);
528
+ if (!chosen) continue;
529
+ const entry = this.findTocEntryByLink(this.contextualizedToc, chosen);
530
+ if (entry) return entry;
531
+ }
532
+ return undefined;
533
+ }
534
+
494
535
  // -------------------------------------------------------------------------
495
536
  // Shared utilities
496
537
  // -------------------------------------------------------------------------
@@ -103,10 +103,12 @@ export declare class Timeline {
103
103
  */
104
104
  get contextualizedToc(): ContextualizedTocEntry[];
105
105
  /**
106
- * Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry`:
107
- * a direct match first, falling back to the nearest preceding toc entry
108
- * for that resource when there's no exact match (e.g. a mid-resource
109
- * audio position between chapter markers).
106
+ * Maps a `TimelineItem` (typically from `locate()`) to its `ContextualizedTocEntry`,
107
+ * trying three fallbacks in order:
108
+ * 1. A direct match on `current`'s own link.
109
+ * 2. The nearest preceding toc entry within the same resource.
110
+ * 3. When no toc entry references the resource at all, the nearest
111
+ * preceding resource's toc entry.
110
112
  */
111
113
  tocEntryFor(current: TimelineItem): ContextualizedTocEntry | undefined;
112
114
  private get flat();
@@ -146,7 +148,18 @@ export declare class Timeline {
146
148
  private entryFor;
147
149
  private resourceStartItem;
148
150
  private findTocEntryByLink;
151
+ /**
152
+ * Tier-2 fallback for `tocEntryFor`: among the toc entries that reference
153
+ * `href`'s own resource (e.g. chapter markers within one audio file),
154
+ * return the nearest one at or before `current`'s position/scroll.
155
+ */
149
156
  private nearestTocEntryForResource;
157
+ /**
158
+ * Tier-3 fallback for `tocEntryFor`: when no toc entry references
159
+ * `href`'s resource at all, walk backward through the reading order and
160
+ * return the nearest preceding resource's toc entry.
161
+ */
162
+ private previousResolvedTocEntry;
150
163
  private static trimToDepth;
151
164
  private flattenItems;
152
165
  private itemStartTime;