@prose-reader/cfi 1.295.0 → 1.297.0

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/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
- function H(e) {
1
+ function G(e) {
2
2
  const n = [e];
3
3
  let t = e;
4
4
  for (; t.parentNode; )
5
5
  n.push(t.parentNode), t = t.parentNode;
6
6
  return n;
7
7
  }
8
- function U(e, n) {
8
+ function H(e, n) {
9
9
  if (e === n) return e;
10
- const t = H(e), i = new Set(t);
10
+ const t = G(e), i = new Set(t);
11
11
  let r = n;
12
12
  for (; r; ) {
13
13
  if (i.has(r))
@@ -16,34 +16,34 @@ function U(e, n) {
16
16
  }
17
17
  return null;
18
18
  }
19
- const G = /[[\]^,();]/g;
19
+ const K = /[[\]^,();]/g;
20
20
  function p(e) {
21
- return e.replace(G, "^$&");
21
+ return e.replace(K, "^$&");
22
22
  }
23
- const J = /^epubcfi\((.*)\)$/, v = (e) => e.nodeType === Node.ELEMENT_NODE, P = (e) => typeof e == "object" && e !== null && "nodeType" in e && (e.nodeType === Node.ELEMENT_NODE || e.nodeType === Node.TEXT_NODE), K = (e) => e.nodeType === Node.TEXT_NODE;
24
- function Q(e) {
25
- if (k(e))
23
+ const J = /^epubcfi\((.*)\)$/, T = (e) => e.nodeType === Node.ELEMENT_NODE, b = (e) => typeof e == "object" && e !== null && "nodeType" in e && (e.nodeType === Node.ELEMENT_NODE || e.nodeType === Node.TEXT_NODE), Q = (e) => e.nodeType === Node.TEXT_NODE;
24
+ function Z(e) {
25
+ if (S(e))
26
26
  return !1;
27
27
  const n = e[e.length - 1];
28
28
  return e.length > 1 && (n === void 0 || n.length === 0);
29
29
  }
30
- function k(e) {
30
+ function S(e) {
31
31
  return e !== null && typeof e == "object" && "parent" in e && "start" in e && "end" in e;
32
32
  }
33
- function Z(e) {
33
+ function ee(e) {
34
34
  const n = e.match(J);
35
35
  return n && n[1] || e;
36
36
  }
37
- function ee(e) {
37
+ function te(e) {
38
38
  const n = [];
39
39
  let t = null, i = !1, r = "";
40
- const s = (f) => {
41
- n.push(f), t = null, r = "";
42
- }, l = (f) => {
43
- r += f, i = !1;
44
- }, a = Z(e).trim(), c = Array.from(a).concat("");
45
- for (let f = 0; f < c.length; f++) {
46
- const o = c[f];
40
+ const s = (a) => {
41
+ n.push(a), t = null, r = "";
42
+ }, l = (a) => {
43
+ r += a, i = !1;
44
+ }, f = ee(e).trim(), c = Array.from(f).concat("");
45
+ for (let a = 0; a < c.length; a++) {
46
+ const o = c[a];
47
47
  if (!o) {
48
48
  t === "/" || t === ":" ? s([t, parseInt(r, 10)]) : t === "~" ? s(["~", parseFloat(r)]) : t === "@" ? s(["@", parseFloat(r)]) : t === "[" ? s(["[", r]) : t === ";" || t?.startsWith(";") ? s([t, r]) : t === "!" && s(["!", 0]);
49
49
  break;
@@ -122,37 +122,37 @@ function Y(e, n) {
122
122
  t.push(e.slice(i, r)), i = r;
123
123
  return t.push(e.slice(i)), t;
124
124
  }
125
- function te(e) {
125
+ function ne(e) {
126
126
  const n = [], t = {};
127
127
  let i = -1;
128
128
  for (let r = 0; r < e.length; r++) {
129
129
  const s = e[r];
130
130
  if (!s) continue;
131
- const [l, a] = s;
132
- l === "/" ? (i++, n[i] = { index: a }, t[i] = []) : i >= 0 && t[i]?.push(s);
131
+ const [l, f] = s;
132
+ l === "/" ? (i++, n[i] = { index: f }, t[i] = []) : i >= 0 && t[i]?.push(s);
133
133
  }
134
134
  for (let r = 0; r < n.length; r++) {
135
135
  const s = n[r];
136
136
  if (!s) continue;
137
137
  const l = t[r] || [];
138
- for (let a = 0; a < l.length; a++) {
139
- const c = l[a];
138
+ for (let f = 0; f < l.length; f++) {
139
+ const c = l[f];
140
140
  if (!c) continue;
141
- const [f, o] = c;
142
- if (f === ":")
141
+ const [a, o] = c;
142
+ if (a === ":")
143
143
  s.offset = o;
144
- else if (f === "~")
144
+ else if (a === "~")
145
145
  s.temporal = o;
146
- else if (f === "@")
146
+ else if (a === "@")
147
147
  s.spatial = (s.spatial || []).concat(o);
148
- else if (f === ";s")
148
+ else if (a === ";s")
149
149
  s.side = o;
150
- else if (f.startsWith(";") && f !== ";s") {
151
- const u = f.substring(1);
150
+ else if (a.startsWith(";") && a !== ";s") {
151
+ const u = a.substring(1);
152
152
  s.extensions || (s.extensions = {}), s.extensions[u] = o;
153
- } else if (f === "[") {
153
+ } else if (a === "[") {
154
154
  const u = typeof o == "string" && !o.includes(" ") && o.length < 50;
155
- if (a === 0 && u && !s.id)
155
+ if (f === 0 && u && !s.id)
156
156
  s.id = o;
157
157
  else if (o !== "") {
158
158
  const d = o.split(",").map((h) => h.trim());
@@ -163,126 +163,340 @@ function te(e) {
163
163
  }
164
164
  return n;
165
165
  }
166
- function A(e) {
166
+ function C(e) {
167
167
  const n = z(e, "!");
168
- return Y(e, n).map(te);
168
+ return Y(e, n).map(ne);
169
169
  }
170
- function w(e) {
170
+ function I(e) {
171
171
  if (!e)
172
172
  throw new Error("CFI string cannot be empty");
173
- const n = ee(e);
173
+ const n = te(e);
174
174
  if (!n || n.length === 0)
175
175
  throw new Error("Failed to tokenize CFI string");
176
176
  const t = z(n, ",");
177
177
  if (t.length === 0)
178
- return A(n);
178
+ return C(n);
179
179
  const [i, r, s] = Y(n, t);
180
180
  function l(o, u) {
181
181
  if (!u || u.length === 0) return [[]];
182
182
  const d = u.filter((h) => h[0] !== ",");
183
183
  if (d.length === 1 && d[0] && d[0][0] === ":") {
184
- const h = o.length > 0 ? o[o.length - 1] : void 0, g = d[0];
185
- if (h && h.length > 0 && g) {
186
- const x = h.map((y) => ({ ...y })), E = x[x.length - 1];
187
- return E && (E.offset = g[1]), [x];
184
+ const h = o.length > 0 ? o[o.length - 1] : void 0, x = d[0];
185
+ if (h && h.length > 0 && x) {
186
+ const g = h.map(($) => ({ ...$ })), E = g[g.length - 1];
187
+ return E && (E.offset = x[1]), [g];
188
188
  }
189
189
  return [[]];
190
190
  }
191
- return A(u);
191
+ return C(u);
192
192
  }
193
- const a = A(i || []), c = l(a, r || []), f = l(a, s || []);
193
+ const f = C(i || []), c = l(f, r || []), a = l(f, s || []);
194
194
  return {
195
- parent: a,
195
+ parent: f,
196
196
  start: c,
197
- end: f
197
+ end: a
198
198
  };
199
199
  }
200
- function ce(e, n, t = {}) {
200
+ function y(e, n = !1) {
201
+ return typeof e == "string" ? y(I(e), n) : "parent" in e ? n ? e.parent.concat(e.end) : e.parent.concat(e.start) : e;
202
+ }
203
+ function D(e) {
204
+ return e.offset !== void 0 ? 1 : e.index !== void 0 ? 2 : e.temporal !== void 0 || e.spatial !== void 0 ? 3 : 4;
205
+ }
206
+ function B(e, n) {
207
+ const t = typeof e == "string" ? I(e) : e, i = typeof n == "string" ? I(n) : n;
208
+ if ("parent" in t || "parent" in i)
209
+ return B(y(t), y(i)) || B(y(t, !0), y(i, !0));
210
+ for (let r = 0; r < Math.max(t.length, i.length); r++) {
211
+ const s = t[r] || [], l = i[r] || [], f = Math.max(s.length, l.length) - 1;
212
+ for (let c = 0; c <= f; c++) {
213
+ const a = s[c], o = l[c];
214
+ if (!a) return -1;
215
+ if (!o) return 1;
216
+ const u = D(a), d = D(o);
217
+ if (u !== d)
218
+ return u - d;
219
+ if (a.index > o.index) return 1;
220
+ if (a.index < o.index) return -1;
221
+ const h = a.temporal !== void 0, x = o.temporal !== void 0;
222
+ if (h && !x) return 1;
223
+ if (!h && x) return -1;
224
+ if (h && x) {
225
+ if ((a.temporal ?? 0) > (o.temporal ?? 0)) return 1;
226
+ if ((a.temporal ?? 0) < (o.temporal ?? 0)) return -1;
227
+ }
228
+ const g = a.spatial !== void 0, E = o.spatial !== void 0;
229
+ if (g && !E) return 1;
230
+ if (!g && E) return -1;
231
+ if (g && E) {
232
+ const $ = a.spatial?.[1] ?? 0, j = o.spatial?.[1] ?? 0;
233
+ if ($ > j) return 1;
234
+ if ($ < j) return -1;
235
+ const M = a.spatial?.[0] ?? 0, _ = o.spatial?.[0] ?? 0;
236
+ if (M > _) return 1;
237
+ if (M < _) return -1;
238
+ }
239
+ if (c === f) {
240
+ if ((a.offset ?? 0) > (o.offset ?? 0)) return 1;
241
+ if ((a.offset ?? 0) < (o.offset ?? 0)) return -1;
242
+ }
243
+ }
244
+ }
245
+ return 0;
246
+ }
247
+ const re = 6;
248
+ function U(e, n, t = {}) {
249
+ if (!e.textContent || e.textContent.trim() === "")
250
+ return null;
251
+ const i = e.textContent, r = t.textAssertionLength || 10;
252
+ if (n !== void 0 && n <= i.length) {
253
+ const s = Math.floor(r / 2), l = Math.max(0, n - s), f = Math.min(i.length, n + s);
254
+ return i.substring(l, f);
255
+ }
256
+ return i.substring(0, Math.min(i.length, r));
257
+ }
258
+ function ie(e) {
259
+ const [n, t] = e, i = Math.max(0, Math.min(100, n)), r = Math.max(0, Math.min(100, t));
260
+ return `@${i}:${r}`;
261
+ }
262
+ function A(e, n, t) {
263
+ let i = e;
264
+ return n !== void 0 && (i += `~${n}`), t !== void 0 && (i += ie(t)), i;
265
+ }
266
+ function V(e, n, t) {
267
+ let i = e;
268
+ if (n && (i += `[${p(n)}]`), t) {
269
+ const r = t === "before" ? "b" : "a";
270
+ n ? i = `${i.substring(0, i.length - 1)};s=${r}]` : i += `[;s=${r}]`;
271
+ }
272
+ return i;
273
+ }
274
+ function F(e, n, t = {}, i) {
275
+ let r = "", s = e, l = null;
276
+ if (e?.nodeType === Node.TEXT_NODE) {
277
+ l = e;
278
+ const c = e.parentNode;
279
+ if (!c)
280
+ throw new Error("Text node doesn't have a parent");
281
+ const o = Array.from(c.childNodes).indexOf(e);
282
+ if (o === -1)
283
+ throw new Error("Node not found in parent's children");
284
+ if (r = `/${o + 1}`, T(c) && c.id) {
285
+ const u = c.id, d = Array.from(c.parentNode?.childNodes || []);
286
+ r = `/${d.slice(0, d.indexOf(c) + 1).filter((g) => g.nodeType === Node.ELEMENT_NODE).length * 2}[${p(u)}]${r}`, s = c.parentNode;
287
+ } else
288
+ s = c;
289
+ }
290
+ let f = null;
291
+ for (l && t.includeTextAssertions && (f = U(l, n, t)); s?.parentNode; ) {
292
+ if (!(l && s === l.parentNode && r.includes(`[${T(s) ? s.id : ""}]`))) {
293
+ const c = s.parentNode, a = Array.from(c.childNodes), o = a.indexOf(s);
294
+ if (o === -1)
295
+ throw new Error("Node not found in parent's children");
296
+ const u = a.slice(0, o + 1).filter((x) => x.nodeType === Node.ELEMENT_NODE);
297
+ let d;
298
+ s.nodeType, Node.ELEMENT_NODE, d = u.length;
299
+ const h = d * 2;
300
+ T(s) && s.id ? r = `/${h}[${p(s.id)}]${r}` : r = `/${h}${r}`;
301
+ }
302
+ if (s.parentNode.nodeName.toLowerCase() === "html")
303
+ break;
304
+ s = s.parentNode;
305
+ }
306
+ return n !== void 0 && (r += `:${n}`), r = A(
307
+ r,
308
+ i?.temporal,
309
+ i?.spatial || t.spatialOffset
310
+ ), r = V(r, f, t.includeSideBias), r = N(r, t.extensions), r;
311
+ }
312
+ function L(e, n, t, i = {}, r) {
313
+ if (e === n) {
314
+ let c = t !== void 0 ? `:${t}` : "";
315
+ return c = A(c, r?.temporal, r?.spatial), c;
316
+ }
317
+ const s = [];
318
+ let l = n;
319
+ for (; l && l !== e; ) {
320
+ const c = l.parentNode;
321
+ if (!c) break;
322
+ const a = Array.from(c.childNodes), o = a.indexOf(l);
323
+ if (o === -1)
324
+ throw new Error("Node not found in parent's children");
325
+ let u;
326
+ l.nodeType === Node.ELEMENT_NODE ? u = (a.filter(
327
+ (x) => x.nodeType === Node.ELEMENT_NODE
328
+ ).indexOf(l) + 1) * 2 : u = o + 1, T(l) && l.id ? s.unshift(`/${u}[${p(l.id)}]`) : s.unshift(`/${u}`), l = c;
329
+ }
330
+ let f = s.join("");
331
+ if (t !== void 0 && (f += `:${t}`), f = A(f, r?.temporal, r?.spatial), i.includeTextAssertions && n.nodeType === Node.TEXT_NODE) {
332
+ const c = U(n, t, i);
333
+ f = V(
334
+ f,
335
+ c,
336
+ i.includeSideBias
337
+ );
338
+ }
339
+ return f = N(f, i.extensions), f;
340
+ }
341
+ const se = (e) => Object.entries(e).map(([n, t]) => {
342
+ const i = p(t);
343
+ return `${n}=${encodeURIComponent(i)}`;
344
+ }).join(";");
345
+ function N(e, n) {
346
+ if (!n || Object.keys(n).length === 0)
347
+ return e;
348
+ const t = se(n);
349
+ if (e.endsWith("]")) {
350
+ const i = e.lastIndexOf("["), r = e.substring(i + 1, e.length - 1);
351
+ return r.includes(t) ? e : `${e.substring(0, e.length - 1)}${r.includes(";"), ";"}${t}]`;
352
+ }
353
+ return e.endsWith("!") ? e : `${e}[;${t}]`;
354
+ }
355
+ function oe(e, n, t, i, r = {}, s, l) {
356
+ const f = H(e, t);
357
+ if (!f)
358
+ throw new Error("No common ancestor found");
359
+ const c = F(f, void 0, r), a = L(
360
+ f,
361
+ e,
362
+ n,
363
+ r,
364
+ s
365
+ ), o = L(
366
+ f,
367
+ t,
368
+ i,
369
+ r,
370
+ l
371
+ );
372
+ if (r.extensions && Object.keys(r.extensions).length > 0) {
373
+ const u = N(c, r.extensions), d = N(a, r.extensions), h = N(o, r.extensions);
374
+ return `${u},${d},${h}`;
375
+ }
376
+ return `${c},${a},${o}`;
377
+ }
378
+ const W = (e, n, t = {}, i) => {
379
+ let l = `/6/${(e + 1) * 2}`;
380
+ return n && (l += `[${p(n)}]`), l = i ? N(l, t.extensions) : l, `${l}!`;
381
+ };
382
+ function me(e, n = {}) {
383
+ if (b(e))
384
+ return `epubcfi(${F(e, void 0, n)})`;
385
+ let t = "";
386
+ if (!("start" in e))
387
+ return e.spineIndex !== void 0 && (t = W(
388
+ e.spineIndex,
389
+ e.spineId,
390
+ n,
391
+ !e.node
392
+ ), !e.node) ? `epubcfi(${t})` : (t += F(
393
+ e.node ?? null,
394
+ e.offset,
395
+ n,
396
+ e
397
+ ), `epubcfi(${t})`);
398
+ const { start: i, end: r } = e;
399
+ return i.spineIndex !== void 0 && (t = W(
400
+ i.spineIndex,
401
+ i.spineId,
402
+ n,
403
+ !i.node
404
+ )), i.node && r.node && (t += oe(
405
+ i.node,
406
+ i.offset ?? 0,
407
+ r.node,
408
+ r.offset ?? 0,
409
+ n,
410
+ i,
411
+ r
412
+ )), `epubcfi(${t})`;
413
+ }
414
+ function Ne(e, n, t = {}) {
201
415
  try {
202
- const i = typeof e != "string" ? e : w(e);
203
- return ne(i, n, t);
416
+ const i = typeof e != "string" ? e : I(e);
417
+ return le(i, n, t);
204
418
  } catch (i) {
205
419
  if (t.throwOnError)
206
420
  throw i;
207
421
  return { node: null, isRange: !1 };
208
422
  }
209
423
  }
210
- function ne(e, n, t = { asRange: !1 }) {
211
- if (k(e))
212
- return re(e, n);
213
- if (Q(e))
214
- return N(null);
424
+ function le(e, n, t = { asRange: !1 }) {
425
+ if (S(e))
426
+ return fe(e, n);
427
+ if (Z(e))
428
+ return m(null);
215
429
  const i = e.at(-1);
216
430
  if (i)
217
- return b(i, n, t);
431
+ return w(i, n, t);
218
432
  throw new Error("Invalid CFI structure");
219
433
  }
220
- function re(e, n) {
434
+ function fe(e, n) {
221
435
  const t = e.parent, i = e.start[0] || [], r = e.end[0] || [];
222
436
  let s = n.documentElement;
223
437
  if (t.length > 0)
224
438
  if (t.length > 1) {
225
439
  const d = t[0];
226
440
  if (d) {
227
- const h = b(d, n);
228
- P(h.node) && (s = h.node);
441
+ const h = w(d, n);
442
+ b(h.node) && (s = h.node);
229
443
  }
230
444
  if (t.length > 1 && s) {
231
445
  const h = t[1];
232
446
  if (h) {
233
- const g = b(h, n);
234
- P(g.node) && (s = g.node);
447
+ const x = w(h, n);
448
+ b(x.node) && (s = x.node);
235
449
  }
236
450
  }
237
451
  } else {
238
452
  const d = t[0];
239
453
  if (d) {
240
- const h = b(d, n);
241
- P(h.node) && (s = h.node);
454
+ const h = w(d, n);
455
+ b(h.node) && (s = h.node);
242
456
  }
243
457
  }
244
458
  if (!s)
245
459
  throw new Error("Failed to resolve parent node in CFI range");
246
460
  const l = s.nodeType === Node.TEXT_NODE;
247
- let a, c, f = 0, o = 0;
461
+ let f, c, a = 0, o = 0;
248
462
  if (l) {
249
- a = s, c = s;
463
+ f = s, c = s;
250
464
  const d = i[i.length - 1], h = r[r.length - 1];
251
- f = (Array.isArray(d?.offset) ? d.offset[0] : d?.offset) ?? 0, o = (Array.isArray(h?.offset) ? h.offset[0] : h?.offset) ?? 0;
465
+ a = (Array.isArray(d?.offset) ? d.offset[0] : d?.offset) ?? 0, o = (Array.isArray(h?.offset) ? h.offset[0] : h?.offset) ?? 0;
252
466
  } else {
253
467
  const d = i.length === 0 || i.length === 1 && typeof i[0]?.offset == "number", h = r.length === 0 || r.length === 1 && typeof r[0]?.offset == "number";
254
468
  if (d) {
255
469
  if (!s)
256
470
  throw new Error("Failed to resolve parent node in CFI range (start)");
257
- a = s, f = i[0]?.offset ?? 0;
471
+ f = s, a = i[0]?.offset ?? 0;
258
472
  } else {
259
- const g = O(s, i, 0, !0);
260
- if (!g)
473
+ const x = O(s, i, 0, !0);
474
+ if (!x)
261
475
  throw new Error("Failed to resolve start node in CFI range");
262
- a = g;
263
- const x = i[i.length - 1];
264
- f = (Array.isArray(x?.offset) ? x.offset[0] : x?.offset) ?? 0;
476
+ f = x;
477
+ const g = i[i.length - 1];
478
+ a = (Array.isArray(g?.offset) ? g.offset[0] : g?.offset) ?? 0;
265
479
  }
266
480
  if (h) {
267
481
  if (!s)
268
482
  throw new Error("Failed to resolve parent node in CFI range (end)");
269
483
  c = s, o = r[0]?.offset ?? 0;
270
484
  } else {
271
- const g = O(s, r, 0, !0);
272
- if (!g) throw new Error("Failed to resolve end node in CFI range");
273
- c = g;
274
- const x = r[r.length - 1];
275
- o = (Array.isArray(x?.offset) ? x.offset[0] : x?.offset) ?? 0;
485
+ const x = O(s, r, 0, !0);
486
+ if (!x) throw new Error("Failed to resolve end node in CFI range");
487
+ c = x;
488
+ const g = r[r.length - 1];
489
+ o = (Array.isArray(g?.offset) ? g.offset[0] : g?.offset) ?? 0;
276
490
  }
277
491
  }
278
492
  const u = n.createRange();
279
- return u.setStart(a, f), u.setEnd(c, o), {
280
- ...F(i[i.length - 1]),
493
+ return u.setStart(f, a), u.setEnd(c, o), {
494
+ ...R(i[i.length - 1]),
281
495
  node: u,
282
496
  isRange: !0
283
497
  };
284
498
  }
285
- function ie(e) {
499
+ function ae(e) {
286
500
  if (e?.side) return e.side;
287
501
  if (e?.text && e.text.length > 0) {
288
502
  const n = e.text[0];
@@ -293,14 +507,14 @@ function ie(e) {
293
507
  }
294
508
  }
295
509
  }
296
- function C(e) {
510
+ function k(e) {
297
511
  return e.index % 2 !== 0;
298
512
  }
299
- function de(e) {
300
- return (k(e) ? e.end : e).at(-1)?.at(-1)?.extensions;
513
+ function Ie(e) {
514
+ return (S(e) ? e.end : e).at(-1)?.at(-1)?.extensions;
301
515
  }
302
- function F(e) {
303
- const n = ie(e), t = e?.extensions;
516
+ function R(e) {
517
+ const n = ae(e), t = e?.extensions;
304
518
  return {
305
519
  offset: e?.offset,
306
520
  temporal: e?.temporal,
@@ -309,25 +523,25 @@ function F(e) {
309
523
  extensions: t
310
524
  };
311
525
  }
312
- function N(e, n) {
526
+ function m(e, n) {
313
527
  return {
314
528
  node: e,
315
529
  isRange: !1,
316
- ...F(n)
530
+ ...R(n)
317
531
  };
318
532
  }
319
- function I(e, n) {
533
+ function v(e, n) {
320
534
  return {
321
535
  node: e,
322
536
  isRange: !0,
323
- ...F(n)
537
+ ...R(n)
324
538
  };
325
539
  }
326
- function D(e, n, t) {
540
+ function X(e, n, t) {
327
541
  const i = e.createRange();
328
542
  if (i.selectNodeContents(n), t !== void 0) {
329
543
  const r = Array.isArray(t) ? t[0] : t;
330
- K(n) && i.setStart(n, r || 0);
544
+ Q(n) && i.setStart(n, r || 0);
331
545
  }
332
546
  return i;
333
547
  }
@@ -336,10 +550,10 @@ function O(e, n, t, i) {
336
550
  for (let s = t; s < n.length; s++) {
337
551
  const l = n[s];
338
552
  if (!r || !l) break;
339
- if (C(l)) {
340
- const a = l.index - 1;
341
- if (a >= 0 && a < r.childNodes.length)
342
- r = r.childNodes[a];
553
+ if (k(l)) {
554
+ const f = l.index - 1;
555
+ if (f >= 0 && f < r.childNodes.length)
556
+ r = r.childNodes[f];
343
557
  else {
344
558
  if (i)
345
559
  throw new Error(`Invalid text node index: ${l.index}`);
@@ -347,12 +561,12 @@ function O(e, n, t, i) {
347
561
  break;
348
562
  }
349
563
  } else {
350
- const a = Array.from(r.childNodes).filter(
351
- (f) => f.nodeType === Node.ELEMENT_NODE
564
+ const f = Array.from(r.childNodes).filter(
565
+ (a) => a.nodeType === Node.ELEMENT_NODE
352
566
  ), c = Math.floor(l.index / 2) - 1;
353
- if (c >= 0 && c < a.length) {
354
- const f = a[c];
355
- f && (r = f);
567
+ if (c >= 0 && c < f.length) {
568
+ const a = f[c];
569
+ a && (r = a);
356
570
  } else {
357
571
  if (i)
358
572
  throw new Error(`Invalid element step index: ${l.index}`);
@@ -363,68 +577,68 @@ function O(e, n, t, i) {
363
577
  }
364
578
  return r;
365
579
  }
366
- function b(e, n, t = {}) {
580
+ function w(e, n, t = {}) {
367
581
  const { throwOnError: i = !1, asRange: r = !1 } = t;
368
582
  if (!n) {
369
583
  if (i)
370
584
  throw new Error("Document is not available");
371
- return N(null);
585
+ return m(null);
372
586
  }
373
- const { node: s, remainingPathIndex: l } = se(n, e);
587
+ const { node: s, remainingPathIndex: l } = ce(n, e);
374
588
  if (s && l >= e.length) {
375
589
  const o = e.at(-1);
376
- if (o && C(o)) {
590
+ if (o && k(o)) {
377
591
  const u = o.index - 1;
378
592
  if (u >= 0 && u < s.childNodes.length) {
379
593
  const d = s.childNodes[u];
380
- return N(d, o);
594
+ return m(d, o);
381
595
  }
382
596
  }
383
597
  if (r) {
384
- const u = D(n, s, o?.offset);
385
- return I(u, o);
598
+ const u = X(n, s, o?.offset);
599
+ return v(u, o);
386
600
  }
387
- return N(s, o);
601
+ return m(s, o);
388
602
  }
389
- let a = s || n.documentElement;
603
+ let f = s || n.documentElement;
390
604
  const c = s ? l : 0;
391
605
  if (r && e.length > 0) {
392
606
  const o = e[e.length - 1];
393
- if (o && !C(o)) {
394
- if (o.index === 0 && a) {
607
+ if (o && !k(o)) {
608
+ if (o.index === 0 && f) {
395
609
  const d = n.createRange();
396
- return d.setStart(a, 0), d.setEnd(a, 0), I(d, o);
610
+ return d.setStart(f, 0), d.setEnd(f, 0), v(d, o);
397
611
  }
398
612
  const u = O(
399
- a,
613
+ f,
400
614
  e.slice(0, -1),
401
615
  c,
402
616
  i
403
617
  );
404
618
  if (u) {
405
619
  const d = Array.from(u.childNodes).filter(
406
- (g) => g.nodeType === Node.ELEMENT_NODE
620
+ (x) => x.nodeType === Node.ELEMENT_NODE
407
621
  );
408
622
  if (Math.floor(o.index / 2) - 1 === d.length) {
409
- const g = n.createRange();
410
- return g.selectNodeContents(u), g.collapse(!1), I(g, o);
623
+ const x = n.createRange();
624
+ return x.selectNodeContents(u), x.collapse(!1), v(x, o);
411
625
  }
412
626
  }
413
627
  }
414
628
  }
415
- if (a = O(a, e, c, i), !a) {
629
+ if (f = O(f, e, c, i), !f) {
416
630
  if (i)
417
631
  throw new Error("Failed to resolve CFI path");
418
- return N(null);
632
+ return m(null);
419
633
  }
420
- const f = e.at(-1);
634
+ const a = e.at(-1);
421
635
  if (r) {
422
- const o = D(n, a, f?.offset);
423
- return I(o, f);
636
+ const o = X(n, f, a?.offset);
637
+ return v(o, a);
424
638
  }
425
- return N(a, f);
639
+ return m(f, a);
426
640
  }
427
- function se(e, n) {
641
+ function ce(e, n) {
428
642
  for (let t = n.length - 1; t >= 0; t--) {
429
643
  const i = n[t];
430
644
  if (i?.id) {
@@ -434,220 +648,7 @@ function se(e, n) {
434
648
  }
435
649
  return { node: null, remainingPathIndex: 0 };
436
650
  }
437
- function V(e, n, t = {}) {
438
- if (!e.textContent || e.textContent.trim() === "")
439
- return null;
440
- const i = e.textContent, r = t.textAssertionLength || 10;
441
- if (n !== void 0 && n <= i.length) {
442
- const s = Math.floor(r / 2), l = Math.max(0, n - s), a = Math.min(i.length, n + s);
443
- return i.substring(l, a);
444
- }
445
- return i.substring(0, Math.min(i.length, r));
446
- }
447
- function oe(e) {
448
- const [n, t] = e, i = Math.max(0, Math.min(100, n)), r = Math.max(0, Math.min(100, t));
449
- return `@${i}:${r}`;
450
- }
451
- function S(e, n, t) {
452
- let i = e;
453
- return n !== void 0 && (i += `~${n}`), t !== void 0 && (i += oe(t)), i;
454
- }
455
- function q(e, n, t) {
456
- let i = e;
457
- if (n && (i += `[${p(n)}]`), t) {
458
- const r = t === "before" ? "b" : "a";
459
- n ? i = `${i.substring(0, i.length - 1)};s=${r}]` : i += `[;s=${r}]`;
460
- }
461
- return i;
462
- }
463
- function R(e, n, t = {}, i) {
464
- let r = "", s = e, l = null;
465
- if (e?.nodeType === Node.TEXT_NODE) {
466
- l = e;
467
- const c = e.parentNode;
468
- if (!c)
469
- throw new Error("Text node doesn't have a parent");
470
- const o = Array.from(c.childNodes).indexOf(e);
471
- if (o === -1)
472
- throw new Error("Node not found in parent's children");
473
- if (r = `/${o + 1}`, v(c) && c.id) {
474
- const u = c.id, d = Array.from(c.parentNode?.childNodes || []);
475
- r = `/${d.slice(0, d.indexOf(c) + 1).filter((x) => x.nodeType === Node.ELEMENT_NODE).length * 2}[${p(u)}]${r}`, s = c.parentNode;
476
- } else
477
- s = c;
478
- }
479
- let a = null;
480
- for (l && t.includeTextAssertions && (a = V(l, n, t)); s?.parentNode; ) {
481
- if (!(l && s === l.parentNode && r.includes(`[${v(s) ? s.id : ""}]`))) {
482
- const c = s.parentNode, f = Array.from(c.childNodes), o = f.indexOf(s);
483
- if (o === -1)
484
- throw new Error("Node not found in parent's children");
485
- const u = f.slice(0, o + 1).filter((g) => g.nodeType === Node.ELEMENT_NODE);
486
- let d;
487
- s.nodeType, Node.ELEMENT_NODE, d = u.length;
488
- const h = d * 2;
489
- v(s) && s.id ? r = `/${h}[${p(s.id)}]${r}` : r = `/${h}${r}`;
490
- }
491
- if (s.parentNode.nodeName.toLowerCase() === "html")
492
- break;
493
- s = s.parentNode;
494
- }
495
- return n !== void 0 && (r += `:${n}`), r = S(
496
- r,
497
- i?.temporal,
498
- i?.spatial || t.spatialOffset
499
- ), r = q(r, a, t.includeSideBias), r = m(r, t.extensions), r;
500
- }
501
- function L(e, n, t, i = {}, r) {
502
- if (e === n) {
503
- let c = t !== void 0 ? `:${t}` : "";
504
- return c = S(c, r?.temporal, r?.spatial), c;
505
- }
506
- const s = [];
507
- let l = n;
508
- for (; l && l !== e; ) {
509
- const c = l.parentNode;
510
- if (!c) break;
511
- const f = Array.from(c.childNodes), o = f.indexOf(l);
512
- if (o === -1)
513
- throw new Error("Node not found in parent's children");
514
- let u;
515
- l.nodeType === Node.ELEMENT_NODE ? u = (f.filter(
516
- (g) => g.nodeType === Node.ELEMENT_NODE
517
- ).indexOf(l) + 1) * 2 : u = o + 1, v(l) && l.id ? s.unshift(`/${u}[${p(l.id)}]`) : s.unshift(`/${u}`), l = c;
518
- }
519
- let a = s.join("");
520
- if (t !== void 0 && (a += `:${t}`), a = S(a, r?.temporal, r?.spatial), i.includeTextAssertions && n.nodeType === Node.TEXT_NODE) {
521
- const c = V(n, t, i);
522
- a = q(
523
- a,
524
- c,
525
- i.includeSideBias
526
- );
527
- }
528
- return a = m(a, i.extensions), a;
529
- }
530
- const le = (e) => Object.entries(e).map(([n, t]) => {
531
- const i = p(t);
532
- return `${n}=${encodeURIComponent(i)}`;
533
- }).join(";");
534
- function m(e, n) {
535
- if (!n || Object.keys(n).length === 0)
536
- return e;
537
- const t = le(n);
538
- if (e.endsWith("]")) {
539
- const i = e.lastIndexOf("["), r = e.substring(i + 1, e.length - 1);
540
- return r.includes(t) ? e : `${e.substring(0, e.length - 1)}${r.includes(";"), ";"}${t}]`;
541
- }
542
- return e.endsWith("!") ? e : `${e}[;${t}]`;
543
- }
544
- function ae(e, n, t, i, r = {}, s, l) {
545
- const a = U(e, t);
546
- if (!a)
547
- throw new Error("No common ancestor found");
548
- const c = R(a, void 0, r), f = L(
549
- a,
550
- e,
551
- n,
552
- r,
553
- s
554
- ), o = L(
555
- a,
556
- t,
557
- i,
558
- r,
559
- l
560
- );
561
- if (r.extensions && Object.keys(r.extensions).length > 0) {
562
- const u = m(c, r.extensions), d = m(f, r.extensions), h = m(o, r.extensions);
563
- return `${u},${d},${h}`;
564
- }
565
- return `${c},${f},${o}`;
566
- }
567
- const B = (e, n, t = {}, i) => {
568
- let l = `/6/${(e + 1) * 2}`;
569
- return n && (l += `[${p(n)}]`), l = i ? m(l, t.extensions) : l, `${l}!`;
570
- };
571
- function ue(e, n = {}) {
572
- if (P(e))
573
- return `epubcfi(${R(e, void 0, n)})`;
574
- let t = "";
575
- if (!("start" in e))
576
- return e.spineIndex !== void 0 && (t = B(
577
- e.spineIndex,
578
- e.spineId,
579
- n,
580
- !e.node
581
- ), !e.node) ? `epubcfi(${t})` : (t += R(
582
- e.node ?? null,
583
- e.offset,
584
- n,
585
- e
586
- ), `epubcfi(${t})`);
587
- const { start: i, end: r } = e;
588
- return i.spineIndex !== void 0 && (t = B(
589
- i.spineIndex,
590
- i.spineId,
591
- n,
592
- !i.node
593
- )), i.node && r.node && (t += ae(
594
- i.node,
595
- i.offset ?? 0,
596
- r.node,
597
- r.offset ?? 0,
598
- n,
599
- i,
600
- r
601
- )), `epubcfi(${t})`;
602
- }
603
- function $(e, n = !1) {
604
- return typeof e == "string" ? $(w(e), n) : "parent" in e ? n ? e.parent.concat(e.end) : e.parent.concat(e.start) : e;
605
- }
606
- function W(e) {
607
- return e.offset !== void 0 ? 1 : e.index !== void 0 ? 2 : e.temporal !== void 0 || e.spatial !== void 0 ? 3 : 4;
608
- }
609
- function X(e, n) {
610
- const t = typeof e == "string" ? w(e) : e, i = typeof n == "string" ? w(n) : n;
611
- if ("parent" in t || "parent" in i)
612
- return X($(t), $(i)) || X($(t, !0), $(i, !0));
613
- for (let r = 0; r < Math.max(t.length, i.length); r++) {
614
- const s = t[r] || [], l = i[r] || [], a = Math.max(s.length, l.length) - 1;
615
- for (let c = 0; c <= a; c++) {
616
- const f = s[c], o = l[c];
617
- if (!f) return -1;
618
- if (!o) return 1;
619
- const u = W(f), d = W(o);
620
- if (u !== d)
621
- return u - d;
622
- if (f.index > o.index) return 1;
623
- if (f.index < o.index) return -1;
624
- const h = f.temporal !== void 0, g = o.temporal !== void 0;
625
- if (h && !g) return 1;
626
- if (!h && g) return -1;
627
- if (h && g) {
628
- if ((f.temporal ?? 0) > (o.temporal ?? 0)) return 1;
629
- if ((f.temporal ?? 0) < (o.temporal ?? 0)) return -1;
630
- }
631
- const x = f.spatial !== void 0, E = o.spatial !== void 0;
632
- if (x && !E) return 1;
633
- if (!x && E) return -1;
634
- if (x && E) {
635
- const y = f.spatial?.[1] ?? 0, M = o.spatial?.[1] ?? 0;
636
- if (y > M) return 1;
637
- if (y < M) return -1;
638
- const j = f.spatial?.[0] ?? 0, _ = o.spatial?.[0] ?? 0;
639
- if (j > _) return 1;
640
- if (j < _) return -1;
641
- }
642
- if (c === a) {
643
- if ((f.offset ?? 0) > (o.offset ?? 0)) return 1;
644
- if ((f.offset ?? 0) < (o.offset ?? 0)) return -1;
645
- }
646
- }
647
- }
648
- return 0;
649
- }
650
- function fe(e) {
651
+ function de(e) {
651
652
  let n = `/${e.index}`;
652
653
  if (e.id) {
653
654
  if (n += `[${p(e.id)}`, e.extensions)
@@ -662,23 +663,65 @@ function fe(e) {
662
663
  t.push(`;${i}=${p(r)}`);
663
664
  return t.length > 0 && (n += `[${t.join("")}]`), n;
664
665
  }
665
- function T(e) {
666
- return e.map((n) => fe(n)).join("");
666
+ function P(e) {
667
+ return e.map((n) => de(n)).join("");
667
668
  }
668
- function he(e) {
669
+ function ue(e) {
669
670
  if (Array.isArray(e))
670
- return `epubcfi(${e.map(T).join("!")})`;
671
- const n = e.parent.map(T).join("!"), t = e.start.map(T).join("!"), i = e.end.map(T).join("!");
671
+ return `epubcfi(${e.map(P).join("!")})`;
672
+ const n = e.parent.map(P).join("!"), t = e.start.map(P).join("!"), i = e.end.map(P).join("!");
672
673
  return `epubcfi(${n},${t},${i})`;
673
674
  }
675
+ const he = (e) => (e + 1) * 2, xe = (e) => e / 2 - 1, ge = (e) => S(e) ? e.parent[0] : e[0], q = (e) => {
676
+ const n = ge(e);
677
+ if (n?.[0]?.index === re)
678
+ return n[1];
679
+ }, pe = (e, n) => {
680
+ const t = { ...e };
681
+ for (const [i, r] of Object.entries(n))
682
+ r === void 0 ? delete t[i] : t[i] = r;
683
+ return Object.keys(t).length > 0 ? t : void 0;
684
+ }, Ee = (e) => {
685
+ if (e.id !== void 0) return e.id;
686
+ if (!(!e.text || e.text.length === 0))
687
+ return e.text.join(",");
688
+ }, ye = (e) => {
689
+ try {
690
+ const n = q(I(e));
691
+ return n ? {
692
+ spineIndex: xe(n.index),
693
+ spineId: Ee(n),
694
+ extensions: n.extensions
695
+ } : void 0;
696
+ } catch {
697
+ return;
698
+ }
699
+ }, $e = (e, n) => {
700
+ try {
701
+ const t = I(e), i = q(t);
702
+ return i ? (n.spineIndex !== void 0 && (i.index = he(
703
+ n.spineIndex
704
+ )), n.spineId !== void 0 && (i.id = n.spineId, i.text = void 0, i.side = void 0), n.extensions !== void 0 && (i.extensions = pe(
705
+ i.extensions,
706
+ n.extensions
707
+ )), ue(t)) : void 0;
708
+ } catch {
709
+ return;
710
+ }
711
+ };
674
712
  export {
675
- X as compare,
676
- ue as generate,
677
- Q as isIndirectionOnly,
678
- k as isParsedCfiRange,
679
- w as parse,
680
- ce as resolve,
681
- de as resolveExtensions,
682
- he as serialize
713
+ re as EPUB_CFI_PACKAGE_SPINE_STEP_INDEX,
714
+ B as compare,
715
+ xe as epubCfiItemrefStepIndexToSpineIndex,
716
+ me as generate,
717
+ ye as getEpubCfiSpineItemref,
718
+ Z as isIndirectionOnly,
719
+ S as isParsedCfiRange,
720
+ I as parse,
721
+ Ne as resolve,
722
+ Ie as resolveExtensions,
723
+ ue as serialize,
724
+ he as spineIndexToEpubCfiItemrefStepIndex,
725
+ $e as updateEpubCfiSpineItemref
683
726
  };
684
727
  //# sourceMappingURL=index.js.map