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