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