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