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