@prose-reader/cfi 1.214.0 → 1.215.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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { parse } from './parse';
1
+ export { parse, type ParsedCfi, type CfiPart } from './parse';
2
2
  export { resolve } from './resolve';
3
3
  export { generate } from './generate';
4
4
  export { compare } from './compare';
package/dist/index.js CHANGED
@@ -28,78 +28,78 @@ function ie(e) {
28
28
  function se(e) {
29
29
  const t = [];
30
30
  let n = null, i = !1, r = "";
31
- const o = (c) => {
31
+ const s = (c) => {
32
32
  t.push(c), n = null, r = "";
33
33
  }, l = (c) => {
34
34
  r += c, i = !1;
35
35
  }, f = ie(e).trim(), a = Array.from(f).concat("");
36
36
  for (let c = 0; c < a.length; c++) {
37
- const s = a[c];
38
- if (!s) {
39
- n === "/" || n === ":" ? o([n, parseInt(r, 10)]) : n === "~" ? o(["~", parseFloat(r)]) : n === "@" ? o(["@", parseFloat(r)]) : n === "[" ? o(["[", r]) : (n === ";" || n != null && n.startsWith(";")) && o([n, r]);
37
+ const o = a[c];
38
+ if (!o) {
39
+ n === "/" || n === ":" ? s([n, parseInt(r, 10)]) : n === "~" ? s(["~", parseFloat(r)]) : n === "@" ? s(["@", parseFloat(r)]) : n === "[" ? s(["[", r]) : (n === ";" || n != null && n.startsWith(";")) && s([n, r]);
40
40
  break;
41
41
  }
42
- if (s === "^" && !i) {
42
+ if (o === "^" && !i) {
43
43
  i = !0;
44
44
  continue;
45
45
  }
46
46
  if (n === "!")
47
- o(["!", 0]);
47
+ s(["!", 0]);
48
48
  else if (n === ",")
49
- o([",", 0]);
49
+ s([",", 0]);
50
50
  else if (n === "/" || n === ":") {
51
- if (/^\d$/.test(s)) {
52
- l(s);
51
+ if (/^\d$/.test(o)) {
52
+ l(o);
53
53
  continue;
54
54
  }
55
- o([n, parseInt(r, 10)]);
55
+ s([n, parseInt(r, 10)]);
56
56
  } else if (n === "~") {
57
- if (/^\d$/.test(s) || s === ".") {
58
- l(s);
57
+ if (/^\d$/.test(o) || o === ".") {
58
+ l(o);
59
59
  continue;
60
60
  }
61
- o(["~", parseFloat(r)]);
61
+ s(["~", parseFloat(r)]);
62
62
  } else if (n === "@") {
63
- if (s === ":") {
64
- o(["@", parseFloat(r)]), n = "@";
63
+ if (o === ":") {
64
+ s(["@", parseFloat(r)]), n = "@";
65
65
  continue;
66
66
  }
67
- if (/^\d$/.test(s) || s === ".") {
68
- l(s);
67
+ if (/^\d$/.test(o) || o === ".") {
68
+ l(o);
69
69
  continue;
70
70
  }
71
- o(["@", parseFloat(r)]);
71
+ s(["@", parseFloat(r)]);
72
72
  } else if (n === "[")
73
- if (s === ";" && !i)
74
- o(["[", r]), n = ";";
75
- else if (s === "]" && !i)
76
- o(["[", r]);
73
+ if (o === ";" && !i)
74
+ s(["[", r]), n = ";";
75
+ else if (o === "]" && !i)
76
+ s(["[", r]);
77
77
  else {
78
- l(s);
78
+ l(o);
79
79
  continue;
80
80
  }
81
81
  else if (n === ";")
82
- if (s === "=" && !i)
82
+ if (o === "=" && !i)
83
83
  n = `;${r}`, r = "";
84
- else if (s === ";" && !i)
85
- o([n, r]), n = ";";
86
- else if (s === "]" && !i)
87
- o([n, r]);
84
+ else if (o === ";" && !i)
85
+ s([n, r]), n = ";";
86
+ else if (o === "]" && !i)
87
+ s([n, r]);
88
88
  else {
89
- l(s);
89
+ l(o);
90
90
  continue;
91
91
  }
92
92
  else if (n != null && n.startsWith(";"))
93
- if (s === ";" && !i)
94
- o([n, r]), n = ";";
95
- else if (s === "]" && !i)
96
- o([n, r]);
93
+ if (o === ";" && !i)
94
+ s([n, r]), n = ";";
95
+ else if (o === "]" && !i)
96
+ s([n, r]);
97
97
  else {
98
- l(s);
98
+ l(o);
99
99
  continue;
100
100
  }
101
- else n === null && s === ";" && (n = ";");
102
- (s === "/" || s === ":" || s === "~" || s === "@" || s === "[" || s === "!" || s === ",") && (n = s);
101
+ else n === null && o === ";" && (n = ";");
102
+ (o === "/" || o === ":" || o === "~" || o === "@" || o === "[" || o === "!" || o === ",") && (n = o);
103
103
  }
104
104
  return t;
105
105
  }
@@ -117,32 +117,32 @@ function oe(e) {
117
117
  var r;
118
118
  const t = [], n = {};
119
119
  let i = -1;
120
- for (let o = 0; o < e.length; o++) {
121
- const l = e[o];
120
+ for (let s = 0; s < e.length; s++) {
121
+ const l = e[s];
122
122
  if (!l) continue;
123
123
  const [f, a] = l;
124
124
  f === "/" ? (i++, t[i] = { index: a }, n[i] = []) : i >= 0 && ((r = n[i]) == null || r.push(l));
125
125
  }
126
- for (let o = 0; o < t.length; o++) {
127
- const l = t[o];
126
+ for (let s = 0; s < t.length; s++) {
127
+ const l = t[s];
128
128
  if (!l) continue;
129
- const f = n[o] || [];
129
+ const f = n[s] || [];
130
130
  for (let a = 0; a < f.length; a++) {
131
131
  const c = f[a];
132
132
  if (!c) continue;
133
- const [s, d] = c;
134
- if (s === ":")
133
+ const [o, d] = c;
134
+ if (o === ":")
135
135
  l.offset = d;
136
- else if (s === "~")
136
+ else if (o === "~")
137
137
  l.temporal = d;
138
- else if (s === "@")
138
+ else if (o === "@")
139
139
  l.spatial = (l.spatial || []).concat(d);
140
- else if (s === ";s")
140
+ else if (o === ";s")
141
141
  l.side = d;
142
- else if (s.startsWith(";") && s !== ";s") {
143
- const u = s.substring(1);
142
+ else if (o.startsWith(";") && o !== ";s") {
143
+ const u = o.substring(1);
144
144
  l.extensions || (l.extensions = {}), l.extensions[u] = d;
145
- } else if (s === "[") {
145
+ } else if (o === "[") {
146
146
  const u = typeof d == "string" && !d.includes(" ") && d.length < 50;
147
147
  if (a === 0 && u && !l.id)
148
148
  l.id = d;
@@ -168,14 +168,14 @@ function O(e) {
168
168
  const n = H(t, ",");
169
169
  if (n.length === 0)
170
170
  return T(t);
171
- const [i, r, o] = U(t, n);
171
+ const [i, r, s] = U(t, n);
172
172
  return {
173
173
  parent: T(i || []),
174
174
  start: T(r || []),
175
- end: T(o || [])
175
+ end: T(s || [])
176
176
  };
177
177
  }
178
- function P(e) {
178
+ function M(e) {
179
179
  return e !== null && typeof e == "object" && "parent" in e && "start" in e && "end" in e;
180
180
  }
181
181
  function he(e, t, n = {}) {
@@ -183,7 +183,7 @@ function he(e, t, n = {}) {
183
183
  if (typeof e != "string")
184
184
  return D(e, t, n);
185
185
  const i = O(e);
186
- return n.asRange && P(i) ? G(i, t) : D(i, t, n);
186
+ return n.asRange && M(i) ? G(i, t) : D(i, t, n);
187
187
  } catch (i) {
188
188
  if (n.throwOnError)
189
189
  throw i;
@@ -192,38 +192,39 @@ function he(e, t, n = {}) {
192
192
  }
193
193
  function D(e, t, n = { asRange: !1 }) {
194
194
  const { asRange: i = !1 } = n;
195
- if (P(e)) {
195
+ if (M(e)) {
196
196
  if (i)
197
197
  return G(e, t);
198
- const r = e.start[0] || [];
199
- return y(r, t, n);
198
+ const s = e.start.at(-1) || [];
199
+ return y(s, t, n);
200
200
  }
201
- if (e[0])
202
- return y(e[0], t, n);
201
+ const r = e.at(-1);
202
+ if (r)
203
+ return y(r, t, n);
203
204
  throw new Error("Invalid CFI structure");
204
205
  }
205
206
  function G(e, t) {
206
207
  const n = e.parent[0] || [], i = e.start[0] || [], r = e.end[0] || [];
207
- let o = t.documentElement;
208
+ let s = t.documentElement;
208
209
  if (n.length > 0) {
209
210
  const d = y(n, t);
210
- p(d.node) && (o = d.node);
211
+ p(d.node) && (s = d.node);
211
212
  }
212
- if (!o)
213
+ if (!s)
213
214
  throw new Error("Failed to resolve parent node in CFI range");
214
215
  const l = y(i, t), f = y(r, t);
215
216
  if (!p(l.node) || !p(f.node))
216
217
  throw new Error("Failed to resolve start or end node in CFI range");
217
- const a = l.node, c = f.node, s = t.createRange();
218
- return s.setStart(
218
+ const a = l.node, c = f.node, o = t.createRange();
219
+ return o.setStart(
219
220
  a,
220
221
  (Array.isArray(l.offset) ? l.offset[0] : l.offset) ?? 0
221
- ), s.setEnd(
222
+ ), o.setEnd(
222
223
  c,
223
224
  (Array.isArray(f.offset) ? f.offset[0] : f.offset) ?? 0
224
225
  ), {
225
226
  ...l,
226
- node: s,
227
+ node: o,
227
228
  isRange: !0
228
229
  };
229
230
  }
@@ -242,7 +243,7 @@ function S(e) {
242
243
  return e.index % 2 !== 0;
243
244
  }
244
245
  function xe(e) {
245
- const n = (P(e) ? e.end : e).at(-1), i = n == null ? void 0 : n.at(-1);
246
+ const n = (M(e) ? e.end : e).at(-1), i = n == null ? void 0 : n.at(-1);
246
247
  return i == null ? void 0 : i.extensions;
247
248
  }
248
249
  function J(e) {
@@ -279,8 +280,8 @@ function W(e, t, n) {
279
280
  }
280
281
  function X(e, t, n, i) {
281
282
  let r = e;
282
- for (let o = n; o < t.length; o++) {
283
- const l = t[o];
283
+ for (let s = n; s < t.length; s++) {
284
+ const l = t[s];
284
285
  if (!r || !l) break;
285
286
  if (S(l)) {
286
287
  const f = l.index - 1;
@@ -316,30 +317,30 @@ function y(e, t, n = {}) {
316
317
  throw new Error("Document is not available");
317
318
  return E(null);
318
319
  }
319
- const { node: o, remainingPathIndex: l } = fe(t, e);
320
- if (o && l >= e.length) {
321
- const s = e.at(-1);
322
- if (s && S(s)) {
323
- const d = s.index - 1;
324
- if (d >= 0 && d < o.childNodes.length) {
325
- const u = o.childNodes[d];
326
- return E(u, s);
320
+ const { node: s, remainingPathIndex: l } = fe(t, e);
321
+ if (s && l >= e.length) {
322
+ const o = e.at(-1);
323
+ if (o && S(o)) {
324
+ const d = o.index - 1;
325
+ if (d >= 0 && d < s.childNodes.length) {
326
+ const u = s.childNodes[d];
327
+ return E(u, o);
327
328
  }
328
329
  }
329
330
  if (r) {
330
- const d = W(t, o, s == null ? void 0 : s.offset);
331
- return v(d, s);
331
+ const d = W(t, s, o == null ? void 0 : o.offset);
332
+ return v(d, o);
332
333
  }
333
- return E(o, s);
334
+ return E(s, o);
334
335
  }
335
- let f = o || t.documentElement;
336
- const a = o ? l : 0;
336
+ let f = s || t.documentElement;
337
+ const a = s ? l : 0;
337
338
  if (r && e.length > 0) {
338
- const s = e[e.length - 1];
339
- if (s && !S(s)) {
340
- if (s.index === 0 && f) {
339
+ const o = e[e.length - 1];
340
+ if (o && !S(o)) {
341
+ if (o.index === 0 && f) {
341
342
  const u = t.createRange();
342
- return u.setStart(f, 0), u.setEnd(f, 0), v(u, s);
343
+ return u.setStart(f, 0), u.setEnd(f, 0), v(u, o);
343
344
  }
344
345
  const d = X(
345
346
  f,
@@ -351,9 +352,9 @@ function y(e, t, n = {}) {
351
352
  const u = Array.from(d.childNodes).filter(
352
353
  (x) => x.nodeType === Node.ELEMENT_NODE
353
354
  );
354
- if (Math.floor(s.index / 2) - 1 === u.length) {
355
+ if (Math.floor(o.index / 2) - 1 === u.length) {
355
356
  const x = t.createRange();
356
- return x.selectNodeContents(d), x.collapse(!1), v(x, s);
357
+ return x.selectNodeContents(d), x.collapse(!1), v(x, o);
357
358
  }
358
359
  }
359
360
  }
@@ -365,8 +366,8 @@ function y(e, t, n = {}) {
365
366
  }
366
367
  const c = e.at(-1);
367
368
  if (r) {
368
- const s = W(t, f, c == null ? void 0 : c.offset);
369
- return v(s, c);
369
+ const o = W(t, f, c == null ? void 0 : c.offset);
370
+ return v(o, c);
370
371
  }
371
372
  return E(f, c);
372
373
  }
@@ -385,7 +386,7 @@ function K(e, t, n = {}) {
385
386
  return null;
386
387
  const i = e.textContent, r = n.textAssertionLength || 10;
387
388
  if (t !== void 0 && t <= i.length) {
388
- const o = Math.floor(r / 2), l = Math.max(0, t - o), f = Math.min(i.length, t + o);
389
+ const s = Math.floor(r / 2), l = Math.max(0, t - s), f = Math.min(i.length, t + s);
389
390
  return i.substring(l, f);
390
391
  }
391
392
  return i.substring(0, Math.min(i.length, r));
@@ -394,7 +395,7 @@ function ce(e) {
394
395
  const [t, n] = e, i = Math.max(0, Math.min(100, t)), r = Math.max(0, Math.min(100, n));
395
396
  return `@${i}:${r}`;
396
397
  }
397
- function j(e, t, n) {
398
+ function P(e, t, n) {
398
399
  let i = e;
399
400
  return t !== void 0 && (i += `~${t}`), n !== void 0 && (i += ce(n)), i;
400
401
  }
@@ -406,9 +407,9 @@ function Q(e, t, n) {
406
407
  }
407
408
  return i;
408
409
  }
409
- function M(e, t, n = {}, i) {
410
+ function j(e, t, n = {}, i) {
410
411
  var a;
411
- let r = "", o = e, l = null;
412
+ let r = "", s = e, l = null;
412
413
  if ((e == null ? void 0 : e.nodeType) === Node.TEXT_NODE) {
413
414
  l = e;
414
415
  const c = e.parentNode;
@@ -419,27 +420,27 @@ function M(e, t, n = {}, i) {
419
420
  throw new Error("Node not found in parent's children");
420
421
  if (r = `/${d + 1}`, w(c) && c.id) {
421
422
  const u = c.id, h = Array.from(((a = c.parentNode) == null ? void 0 : a.childNodes) || []);
422
- r = `/${h.slice(0, h.indexOf(c) + 1).filter((I) => I.nodeType === Node.ELEMENT_NODE).length * 2}[${g(u)}]${r}`, o = c.parentNode;
423
+ r = `/${h.slice(0, h.indexOf(c) + 1).filter((I) => I.nodeType === Node.ELEMENT_NODE).length * 2}[${g(u)}]${r}`, s = c.parentNode;
423
424
  } else
424
- o = c;
425
+ s = c;
425
426
  }
426
427
  let f = null;
427
- for (l && n.includeTextAssertions && (f = K(l, t, n)); o != null && o.parentNode; ) {
428
- if (!(l && o === l.parentNode && r.includes(`[${w(o) ? o.id : ""}]`))) {
429
- const c = o.parentNode, s = Array.from(c.childNodes), d = s.indexOf(o);
428
+ for (l && n.includeTextAssertions && (f = K(l, t, n)); s != null && s.parentNode; ) {
429
+ if (!(l && s === l.parentNode && r.includes(`[${w(s) ? s.id : ""}]`))) {
430
+ const c = s.parentNode, o = Array.from(c.childNodes), d = o.indexOf(s);
430
431
  if (d === -1)
431
432
  throw new Error("Node not found in parent's children");
432
- const u = s.slice(0, d + 1).filter(($) => $.nodeType === Node.ELEMENT_NODE);
433
+ const u = o.slice(0, d + 1).filter(($) => $.nodeType === Node.ELEMENT_NODE);
433
434
  let h;
434
- o.nodeType, Node.ELEMENT_NODE, h = u.length;
435
+ s.nodeType, Node.ELEMENT_NODE, h = u.length;
435
436
  const x = h * 2;
436
- w(o) && o.id ? r = `/${x}[${g(o.id)}]${r}` : r = `/${x}${r}`;
437
+ w(s) && s.id ? r = `/${x}[${g(s.id)}]${r}` : r = `/${x}${r}`;
437
438
  }
438
- if (o.parentNode.nodeName.toLowerCase() === "html")
439
+ if (s.parentNode.nodeName.toLowerCase() === "html")
439
440
  break;
440
- o = o.parentNode;
441
+ s = s.parentNode;
441
442
  }
442
- return t !== void 0 && (r += `:${t}`), r = j(
443
+ return t !== void 0 && (r += `:${t}`), r = P(
443
444
  r,
444
445
  i == null ? void 0 : i.temporal,
445
446
  (i == null ? void 0 : i.spatial) || n.spatialOffset
@@ -448,27 +449,27 @@ function M(e, t, n = {}, i) {
448
449
  function z(e, t, n, i = {}, r) {
449
450
  if (e === t) {
450
451
  let a = n !== void 0 ? `:${n}` : "";
451
- return a = j(a, r == null ? void 0 : r.temporal, r == null ? void 0 : r.spatial), a;
452
+ return a = P(a, r == null ? void 0 : r.temporal, r == null ? void 0 : r.spatial), a;
452
453
  }
453
- const o = [];
454
+ const s = [];
454
455
  let l = t;
455
456
  for (; l && l !== e; ) {
456
457
  const a = l.parentNode;
457
458
  if (!a) break;
458
459
  const c = a.childNodes;
459
- let s = -1;
460
+ let o = -1;
460
461
  for (let u = 0; u < c.length; u++)
461
462
  if (c[u] === l) {
462
- s = u;
463
+ o = u;
463
464
  break;
464
465
  }
465
- if (s === -1)
466
+ if (o === -1)
466
467
  throw new Error("Node not found in parent's children");
467
- const d = s + 1;
468
- w(l) && l.id ? o.unshift(`/${d}[${g(l.id)}]`) : o.unshift(`/${d}`), l = a;
468
+ const d = o + 1;
469
+ w(l) && l.id ? s.unshift(`/${d}[${g(l.id)}]`) : s.unshift(`/${d}`), l = a;
469
470
  }
470
- let f = o.join("");
471
- if (n !== void 0 && (f += `:${n}`), f = j(f, r == null ? void 0 : r.temporal, r == null ? void 0 : r.spatial), i.includeTextAssertions && t.nodeType === Node.TEXT_NODE) {
471
+ let f = s.join("");
472
+ if (n !== void 0 && (f += `:${n}`), f = P(f, r == null ? void 0 : r.temporal, r == null ? void 0 : r.spatial), i.includeTextAssertions && t.nodeType === Node.TEXT_NODE) {
472
473
  const a = K(t, n, i);
473
474
  f = Q(
474
475
  f,
@@ -492,17 +493,17 @@ function N(e, t) {
492
493
  }
493
494
  return e.endsWith("!") ? e : `${e}[;${n}]`;
494
495
  }
495
- function de(e, t, n, i, r = {}, o, l) {
496
+ function de(e, t, n, i, r = {}, s, l) {
496
497
  const f = ee(e, n);
497
498
  if (!f)
498
499
  throw new Error("No common ancestor found");
499
- const a = M(f, void 0, r), c = z(
500
+ const a = j(f, void 0, r), c = z(
500
501
  f,
501
502
  e,
502
503
  t,
503
504
  r,
504
- o
505
- ), s = z(
505
+ s
506
+ ), o = z(
506
507
  f,
507
508
  n,
508
509
  i,
@@ -510,10 +511,10 @@ function de(e, t, n, i, r = {}, o, l) {
510
511
  l
511
512
  );
512
513
  if (r.extensions && Object.keys(r.extensions).length > 0) {
513
- const d = N(a, r.extensions), u = N(c, r.extensions), h = N(s, r.extensions);
514
+ const d = N(a, r.extensions), u = N(c, r.extensions), h = N(o, r.extensions);
514
515
  return `${d},${u},${h}`;
515
516
  }
516
- return `${a},${c},${s}`;
517
+ return `${a},${c},${o}`;
517
518
  }
518
519
  const Y = (e, t, n = {}, i) => {
519
520
  const r = "";
@@ -522,7 +523,7 @@ const Y = (e, t, n = {}, i) => {
522
523
  };
523
524
  function ge(e, t = {}) {
524
525
  if (p(e))
525
- return `epubcfi(${M(e, void 0, t)})`;
526
+ return `epubcfi(${j(e, void 0, t)})`;
526
527
  let n = "";
527
528
  if (!("start" in e))
528
529
  return e.spineIndex !== void 0 && (n = Y(
@@ -530,7 +531,7 @@ function ge(e, t = {}) {
530
531
  e.spineId,
531
532
  t,
532
533
  !e.node
533
- ), !e.node) ? `epubcfi(${n})` : (n += M(
534
+ ), !e.node) ? `epubcfi(${n})` : (n += j(
534
535
  e.node ?? null,
535
536
  e.offset,
536
537
  t,
@@ -559,14 +560,14 @@ function V(e) {
559
560
  return e.offset !== void 0 ? 1 : e.index !== void 0 ? 2 : e.temporal !== void 0 || e.spatial !== void 0 ? 3 : 4;
560
561
  }
561
562
  function q(e, t) {
562
- var r, o, l, f;
563
+ var r, s, l, f;
563
564
  const n = typeof e == "string" ? O(e) : e, i = typeof t == "string" ? O(t) : t;
564
565
  if ("parent" in n || "parent" in i)
565
566
  return q(m(n), m(i)) || q(m(n, !0), m(i, !0));
566
567
  for (let a = 0; a < Math.max(n.length, i.length); a++) {
567
- const c = n[a] || [], s = i[a] || [], d = Math.max(c.length, s.length) - 1;
568
+ const c = n[a] || [], o = i[a] || [], d = Math.max(c.length, o.length) - 1;
568
569
  for (let u = 0; u <= d; u++) {
569
- const h = c[u], x = s[u];
570
+ const h = c[u], x = o[u];
570
571
  if (!h) return -1;
571
572
  if (!x) return 1;
572
573
  const $ = V(h), I = V(x);
@@ -585,7 +586,7 @@ function q(e, t) {
585
586
  if (A && !C) return 1;
586
587
  if (!A && C) return -1;
587
588
  if (A && C) {
588
- const F = ((r = h.spatial) == null ? void 0 : r[1]) ?? 0, B = ((o = x.spatial) == null ? void 0 : o[1]) ?? 0;
589
+ const F = ((r = h.spatial) == null ? void 0 : r[1]) ?? 0, B = ((s = x.spatial) == null ? void 0 : s[1]) ?? 0;
589
590
  if (F > B) return 1;
590
591
  if (F < B) return -1;
591
592
  const _ = ((l = h.spatial) == null ? void 0 : l[0]) ?? 0, L = ((f = x.spatial) == null ? void 0 : f[0]) ?? 0;
@@ -627,7 +628,7 @@ function Ne(e) {
627
628
  export {
628
629
  q as compare,
629
630
  ge as generate,
630
- P as isParsedCfiRange,
631
+ M as isParsedCfiRange,
631
632
  O as parse,
632
633
  he as resolve,
633
634
  xe as resolveExtensions,