-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
1062 lines (909 loc) · 27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// If using node.js
if(typeof Node === 'undefined') {
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var CDATA_SECTION_NODE = 4;
} else { // In the browser
var ELEMENT_NODE = Node.ELEMENT_NODE;
var TEXT_NODE = Node.TEXT_NODE;
var CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE;
}
function cfiEscape(str) {
return str.replace(/[\[\]\^,();]/g, "^$&");
}
// Get indices of all matches of regExp in str
// if `add` is non-null, add it to the matched indices
function matchAll(str, regExp, add) {
add = add || 0;
var matches = [];
var offset = 0;
var m;
do {
m = str.match(regExp);
if(!m) break
matches.push(m.index + add);
offset += m.index + m.length;
str = str.slice(m.index + m.length);
} while(offset < str.length);
return matches;
}
// Get the number in a that has the smallest diff to n
function closest(a, n) {
var minDiff;
var closest;
var i, diff;
for(i=0; i < a.length; i++) {
diff = Math.abs(a[i] - n);
if(!i || diff < minDiff) {
diff = minDiff;
closest = a[i];
}
}
return closest;
}
// Given a set of nodes that are all children
// and a reference to one of those nodes
// calculate the count/index of the node
// according to the CFI spec.
// Also re-calculate offset if supplied and relevant
function calcSiblingCount(nodes, n, offset) {
var count = 0;
var lastWasElement;
var prevOffset = 0;
var firstNode = true;
var i, node;
for(i=0; i < nodes.length; i++) {
node = nodes[i];
if(node.nodeType === ELEMENT_NODE) {
if(lastWasElement || firstNode) {
count += 2;
firstNode = false;
} else {
count++;
}
if(n === node) {
if(node.tagName.toLowerCase() === 'img') {
return {count, offset};
} else {
return {count};
}
}
prevOffset = 0;
lastWasElement = true;
} else if (node.nodeType === TEXT_NODE ||
node.nodeType === CDATA_SECTION_NODE) {
if(lastWasElement || firstNode) {
count++;
firstNode = false;
}
if(n === node) {
return {count, offset: offset + prevOffset};
}
prevOffset += node.textContent.length;
lastWasElement = false;
} else {
continue;
}
}
throw new Error("The specified node was not found in the array of siblings");
}
function compareTemporal(a, b) {
const isA = (typeof a === 'number');
const isB = (typeof b === 'number');
if(!isA && !isB) return 0;
if(!isA && isB) return -1;
if(isA && !isB) return 1;
return (a || 0.0) - (b || 0.0);
}
function compareSpatial(a, b) {
if(!a && !b) return 0;
if(!a && b) return -1;
if(a && !b) return 1;
var diff = (a.y || 0) - (b.y || 0);
if(diff) return diff;
return (a.x || 0) - (b.x || 0);
}
class CFI {
constructor(str, opts) {
this.opts = Object.assign({
// If CFI is a Simple Range, pretend it isn't
// by parsing only the start of the range
flattenRange: false,
// Strip temporal, spatial, offset and textLocationAssertion
// from places where they don't make sense
stricter: true
}, opts || {});
this.cfi = str;
const isCFI = new RegExp(/^epubcfi\((.*)\)$/);
str = str.trim();
var m = str.match(isCFI);
if(!m) throw new Error("Not a valid CFI");
if(m.length < 2) return; // Empty CFI
str = m[1];
this.parts = [];
var parsed, offset, newDoc;
var subParts = [];
var sawComma = 0;
while(str.length) {
({parsed, offset, newDoc} = this.parse(str));
if(!parsed || offset === null) throw new Error("Parsing failed");
if(sawComma && newDoc) throw new Error("CFI is a range that spans multiple documents. This is not allowed");
subParts.push(parsed);
// Handle end of string
if(newDoc || str.length - offset <= 0) {
// Handle end if this was a range
if(sawComma === 2) {
this.to = subParts;
} else { // not a range
this.parts.push(subParts);
}
subParts = [];
}
str = str.slice(offset);
// Handle Simple Ranges
if(str[0] === ',') {
if(sawComma === 0) {
if(subParts.length) {
this.parts.push(subParts);
}
subParts = [];
} else if(sawComma === 1) {
if(subParts.length) {
this.from = subParts;
}
subParts = [];
}
str = str.slice(1);
sawComma++;
}
}
if(this.from && this.from.length) {
if(this.opts.flattenRange || !this.to || !this.to.length) {
this.parts = this.parts.concat(this.from);
delete this.from;
delete this.to;
} else {
this.isRange = true;
}
}
if(this.opts.stricter) {
this.removeIllegalOpts();
}
}
removeIllegalOpts(parts) {
if(!parts) {
if(this.from) {
this.removeIllegalOpts(this.from);
if(!this.to) return;
parts = this.to;
} else {
parts = this.parts;
}
}
var i, j, part, subpart;
for(i=0; i < parts.length; i++) {
part = parts[i];
for(j=0; j < part.length - 1; j++) {
subpart = part[j];
delete subpart.temporal;
delete subpart.spatial;
delete subpart.offset;
delete subpart.textLocationAssertion;
}
}
}
static generatePart(node, offset, extra) {
var cfi = '';
var o;
// The leading path of CFI corresponding to the 'spine' element must be relative
// to the ancestor 'package' element. If this is a spine child element, we need
// to stop traversing when we reach the 'package' node.
var isSpineElement = node.parentNode.nodeName === 'spine' ? true : false;
while(node.parentNode) {
o = calcSiblingCount(node.parentNode.childNodes, node, offset);
if(!cfi && o.offset) cfi = ':'+o.offset;
cfi = '/'+o.count+((node.id) ? '['+cfiEscape(node.id)+']' : '') + cfi;
node = node.parentNode;
if(isSpineElement && node.nodeName === 'package'){
break;
}
}
return cfi;
}
static generate(node, offset, extra) {
var cfi;
if(node instanceof Array) {
var strs = [];
for(let o of node) {
strs.push(this.generatePart(o.node, o.offset));
}
cfi = strs.join('!');
} else {
cfi = this.generatePart(node, offset, extra);
}
if(extra) cfi += extra;
return 'epubcfi('+cfi+')';
}
static toParsed(cfi) {
if(typeof cfi === 'string') cif = new this(cfi);
if(cfi.isRange) {
return cfi.getFrom();
} else {
return cfi.get();
}
}
// Takes two CFI paths and compares them
static comparePath(a, b) {
const max = Math.max(a.length, b.length);
var i, cA, cB, diff;
for(i=0; i < max; i++) {
cA = a[i];
cB = b[i];
if(!cA) return -1;
if(!cB) return 1;
diff = this.compareParts(cA, cB);
if(diff) return diff;
}
return 0;
}
// Sort an array of CFI objects
static sort(a) {
a.sort((a, b) => {
return this.compare(a, b)
});
}
// Takes two CFI objects and compares them.
static compare(a, b) {
var oA = a.get();
var oB = b.get();
if(a.isRange || b.isRange) {
if(a.isRange && b.isRange) {
var diff = this.comparePath(oA.from, oB.from);
if(diff) return diff;
return this.comparePath(oA.to, oB.to);
}
if(a.isRange) oA = oA.from;
if(b.isRange) oB = oB.from;
return this.comparePath(oA, oB);
} else { // neither a nor b is a range
return this.comparePath(oA, oB);
}
}
// Takes two parsed path parts (assuming path is split on '!') and compares them.
static compareParts(a, b) {
const max = Math.max(a.length, b.length);
var i, cA, cB, diff;
for(i=0; i < max; i++) {
cA = a[i];
cB = b[i];
if(!cA) return -1;
if(!cB) return 1;
diff = cA.nodeIndex - cB.nodeIndex;
if(diff) return diff;
// The paths must be equal if the "before the first node" syntax is used
// and this must be the last subpart (assuming a valid CFI)
if(cA.nodeIndex === 0) {
return 0;
}
// Don't bother comparing offsets, temporals or spatials
// unless we're on the last element, since they're not
// supposed to be on elements other than the last
if(i < max - 1) continue;
// Only compare spatials or temporals for element nodes
if(cA.nodeIndex % 2 === 0) {
diff = compareTemporal(cA.temporal, cB.temporal);
if(diff) return diff;
diff = compareSpatial(cA.spatial, cB.spatial);
if(diff) return diff;
}
diff = (cA.offset || 0) - (cB.offset || 0);
if(diff) return diff;
}
return 0;
}
decodeEntities(dom, str) {
try {
const el = dom.createElement('textarea');
el.innerHTML = str;
return el.value
} catch(err) {
// TODO fall back to simpler decode?
// e.g. regex match for stuff like   and
return str;
}
}
// decode HTML/XML entities and compute length
trueLength(dom, str) {
return this.decodeEntities(dom, str).length;
}
getFrom() {
if(!this.isRange) throw new Error("Trying to get beginning of non-range CFI");
if(!this.from) {
return this.deepClone(this.parts);
}
const parts = this.deepClone(this.parts);
parts[parts.length-1] = parts[parts.length-1].concat(this.from);
return parts;
}
getTo() {
if(!this.isRange) throw new Error("Trying to get end of non-range CFI");
const parts = this.deepClone(this.parts);
parts[parts.length-1] = parts[parts.length-1].concat(this.to);
return parts
}
get() {
if(this.isRange) {
return {
from: this.getFrom(),
to: this.getTo(),
isRange: true
};
}
return this.deepClone(this.parts);
}
parseSideBias(o, loc) {
if(!loc) return;
const m = loc.trim().match(/^(.*);s=([ba])$/);
if(!m || m.length < 3) {
if(typeof o.textLocationAssertion === 'object') {
o.textLocationAssertion.post = loc;
} else {
o.textLocationAssertion = loc;
}
return;
}
if(m[1]) {
if(typeof o.textLocationAssertion === 'object') {
o.textLocationAssertion.post = m[1];
} else {
o.textLocationAssertion = m[1];
}
}
if(m[2] === 'a') {
o.sideBias = 'after';
} else {
o.sideBias = 'before';
}
}
parseSpatialRange(range) {
if(!range) return undefined;
const m = range.trim().match(/^([\d\.]+):([\d\.]+)$/);
if(!m || m.length < 3) return undefined;
const o = {
x: parseInt(m[1]),
y: parseInt(m[2]),
};
if(typeof o.x !== 'number' || typeof o.y !== 'number') {
return undefined;
}
return o;
}
parse(cfi) {
var o = {};
const isNumber = new RegExp(/[\d]/);
var f;
var state;
var prevState;
var cur, escape;
var seenColon = false;
var seenSlash = false;
var i;
for(i=0; i <= cfi.length; i++) {
if(i < cfi.length) {
cur = cfi[i];
} else {
cur = '';
}
if(cur === '^' && !escape) {
escape = true;
continue;
}
if(state === '/') {
if(cur.match(isNumber)) {
if(!f) {
f = cur;
} else {
f += cur;
}
escape = false;
continue;
} else {
if(f) {
o.nodeIndex = parseInt(f);
f = null;
}
prevState = state;
state = null;
}
}
if(state === ':') {
if(cur.match(isNumber)) {
if(!f) {
f = cur;
} else {
f += cur;
}
escape = false;
continue;
} else {
if(f) {
o.offset = parseInt(f);
f = null;
}
prevState = state;
state = null;
}
}
if(state === '@') {
let done = false;
if(cur.match(isNumber) || cur === '.' || cur === ':') {
if(cur === ':') {
if(!seenColon) {
seenColon = true;
} else {
done = true;
}
}
} else {
done = true;
}
if(!done) {
if(!f) {
f = cur;
} else {
f += cur;
}
escape = false;
continue;
} else {
prevState = state;
state = null;
if(f && seenColon) o.spatial = this.parseSpatialRange(f);
f = null;
}
}
if(state === '~' ) {
if(cur.match(isNumber) || cur === '.') {
if(!f) {
f = cur;
} else {
f += cur;
}
escape = false;
continue;
} else {
if(f) {
o.temporal = parseFloat(f);
}
prevState = state;
state = null;
f = null;
}
}
if(!state) {
if(cur === '!') {
i++;
state = cur;
break;
}
if(cur === ',') {
break;
}
if(cur === '/') {
if(seenSlash) {
break;
} else {
seenSlash = true;
prevState = state;
state = cur;
escape = false;
continue;
}
}
if(cur === ':' || cur === '~' || cur === '@') {
if(this.opts.stricter) {
// We've already had a temporal or spatial indicator
// and offset does not make sense and the same time
if(cur === ':' && (typeof o.temporal !== 'undefined' || typeof o.spatial !== 'undefined')) {
break;
}
// We've already had an offset
// and temporal or spatial do not make sense at the same time
if((cur === '~' || cur === '@') && (typeof o.offset !== 'undefined')) {
break;
}
}
prevState = state;
state = cur;
escape = false;
seenColon = false; // only relevant for '@'
continue;
}
if(cur === '[' && !escape && prevState === ':') {
prevState = state;
state = '[';
escape = false;
continue;
}
if(cur === '[' && !escape && prevState === '/') {
prevState = state;
state = 'nodeID';
escape = false;
continue;
}
}
if(state === '[') {
if(cur === ']' && !escape) {
prevState = state;
state = null;
this.parseSideBias(o, f);
f = null;
} else if(cur === ',' && !escape) {
o.textLocationAssertion = {};
if(f) {
o.textLocationAssertion.pre = f;
}
f = null;
} else {
if(!f) {
f = cur;
} else {
f += cur;
}
}
escape = false;
continue;
}
if(state === 'nodeID') {
if(cur === ']' && !escape) {
prevState = state;
state = null;
o.nodeID = f;
f = null;
} else {
if(!f) {
f = cur;
} else {
f += cur;
}
}
escape = false;
continue;
}
escape = false;
}
if(!o.nodeIndex && o.nodeIndex !== 0) throw new Error("Missing child node index in CFI");
return {parsed: o, offset: i, newDoc: (state === '!')};
}
// The CFI counts child nodes differently from the DOM
// Retrive the child of parentNode at the specified index
// according to the CFI standard way of counting
getChildNodeByCFIIndex(dom, parentNode, index, offset) {
const children = parentNode.childNodes;
if(!children.length) return {node: parentNode, offset: 0};
// index is pointing to the virtual node before the first node
// as defined in the CFI spec
if(index <= 0) {
return {node: children[0], relativeToNode: 'before', offset: 0}
}
var cfiCount = 0;
var lastChild;
var i, child;
for(i=0; i < children.length; i++) {
child = children[i];
switch(child.nodeType) {
case ELEMENT_NODE:
// If the previous node was also an element node
// then we have to pretend there was a text node in between
// the current and previous nodes (according to the CFI spec)
// so we increment cfiCount by two
if(cfiCount % 2 === 0) {
cfiCount += 2;
if(cfiCount >= index) {
if(child.tagName.toLowerCase() === 'img' && offset) {
return {node: child, offset}
}
return {node: child, offset: 0}
}
} else { // Previous node was a text node
cfiCount += 1;
if(cfiCount === index) {
if(child.tagName.toLowerCase() === 'img' && offset) {
return {node: child, offset}
}
return {node: child, offset: 0}
// This happens when offset into the previous text node was greater
// than the number of characters in that text node
// So we return a position at the end of the previous text node
} else if(cfiCount > index) {
if(!lastChild) {
return {node: parentNode, offset: 0};
}
return {node: lastChild, offset: this.trueLength(dom, lastChild.textContent)};
}
}
lastChild = child;
break;
case TEXT_NODE:
case CDATA_SECTION_NODE:
// If this is the first node or the previous node was an element node
if(cfiCount === 0 || cfiCount % 2 === 0) {
cfiCount += 1;
} else {
// If previous node was a text node then they should be combined
// so we count them as one, meaning we don't increment the count
}
if(cfiCount === index) {
// If offset is greater than the length of the current text node
// then we assume that the next node will also be a text node
// and that we'll be combining them with the current node
let trueLength = this.trueLength(dom, child.textContent);
if(offset >= trueLength) {
offset -= trueLength;
} else {
return {node: child, offset: offset}
}
}
lastChild = child;
break;
default:
continue
}
}
// index is pointing to the virtual node after the last child
// as defined in the CFI spec
if(index > cfiCount) {
var o = {relativeToNode: 'after', offset: 0};
if(!lastChild) {
o.node = parentNode;
} else {
o.node = lastChild;
}
if(this.isTextNode(o.node)) {
o.offset = this.trueLength(dom, o.node.textContent.length);
}
return o;
}
}
isTextNode(node) {
if(!node) return false;
if(node.nodeType === TEXT_NODE || node.nodeType === CDATA_SECTION_NODE) {
return true;
}
return false;
}
// Use a Text Location Assertion to correct and offset
correctOffset(dom, node, offset, assertion) {
var curNode = node;
if(typeof assertion === 'string') {
var matchStr = this.decodeEntities(dom, assertion);
} else {
assertion.pre = this.decodeEntities(dom, assertion.pre);
assertion.post = this.decodeEntities(dom, assertion.post);
var matchStr = assertion.pre + '.' + assertion.post;
}
if(!(this.isTextNode(node))) {
return {node, offset: 0};
}
while(this.isTextNode(curNode.previousSibling)) {
curNode = curNode.previousSibling;
}
const startNode = curNode;
var str;
const nodeLengths = [];
var txt = '';
var i = 0;
while(this.isTextNode(curNode)) {
str = this.decodeEntities(dom, curNode.textContent);
nodeLengths[i] = str.length;
txt += str;
if(!curNode.nextSibling) break;
curNode = curNode.nextSibling;
i++;
}
// Find all matches to the Text Location Assertion
const matchOffset = (assertion.pre) ? assertion.pre.length : 0;
const m = matchAll(txt, new RegExp(matchStr), matchOffset);
if(!m.length) return {node, offset};
// Get the match that has the closest offset to the existing offset
var newOffset = closest(m, offset);
if(curNode === node && newOffset === offset) {
return {node, offset};
}
i = 0;
curNode = startNode;
while(newOffset >= nodeLengths[i]) {
newOffset -= nodeLengths[i];
if(newOffset < 0) return {node, offset}
if(!curNode.nextSibling || i+1 >= nodeOffsets.length) return {node, offset}
i++;
curNode = curNode.nextSibling;
}
return {node: curNode, offset: newOffset};
}
resolveNode(index, subparts, dom, opts) {
opts = Object.assign({}, opts || {});
if(!dom) throw new Error("Missing DOM argument");
// Traverse backwards until a subpart with a valid ID is found
// or the first subpart is reached
var startNode;
if(index === 0) {
startNode = dom.querySelector('package');
}
if(!startNode) {
for(let n of dom.childNodes) {
if(n.nodeType === ELEMENT_NODE) {
startNode = n;
break;
}
}
}
if(!startNode) throw new Error("Document incompatible with CFIs");
var node = startNode;
var startFrom = 0;
var i, subpart;
for(i=subparts.length-1; i >=0; i--) {
subpart = subparts[i];
if(!opts.ignoreIDs && subpart.nodeID && (node = dom.getElementById(subpart.nodeID))) {
startFrom = i + 1;
break;
}
}
if(!node) {
node = startNode;
}
var o = {node, offset: 0};
var nodeIndex;
for(i=startFrom; i < subparts.length; i++) {
subpart = subparts[i];
o = this.getChildNodeByCFIIndex(dom, o.node, subpart.nodeIndex, subpart.offset);
if(subpart.textLocationAssertion) {
o = this.correctOffset(dom, o.node, subpart.offset, subpart.textLocationAssertion);
}
}
return o;
}
// Each part of a CFI (as separated by '!')
// references a separate HTML/XHTML/XML document.
// This function takes an index specifying the part
// of the CFI and the appropriate Document or XMLDocument
// that is referenced by the specified part of the CFI
// and returns the URI for the document referenced by
// the next part of the CFI
// If the opt `ignoreIDs` is true then IDs
// will not be used while resolving
resolveURI(index, dom, opts) {
opts = opts || {};
if(index < 0 || index > this.parts.length - 2) {
throw new Error("index is out of bounds");
}
const subparts = this.parts[index];
if(!subparts) throw new Error("Missing CFI part for index: " + index);
var o = this.resolveNode(index, subparts, dom, opts);
var node = o.node;
const tagName = node.tagName.toLowerCase();
if(tagName === 'itemref'
&& node.parentNode.tagName.toLowerCase() === 'spine') {
const idref = node.getAttribute('idref');
if(!idref) throw new Error("Referenced node had not 'idref' attribute");
node = dom.getElementById(idref);
if(!node) throw new Error("Specified node is missing from manifest");
const href = node.getAttribute('href');
if(!href) throw new Error("Manifest item is missing href attribute");
return href;
}
if(tagName === 'iframe' || tagName === 'embed') {
const src = node.getAttribute('src');
if(!src) throw new Error(tagName + " element is missing 'src' attribute");
return src;
}
if(tagName === 'object') {
const data = node.getAttribute('data');
if(!data) throw new Error(tagName + " element is missing 'data' attribute");
return data;
}
if(tagName === 'image'|| tagName === 'use') {
const href = node.getAttribute('xlink:href');
if(!href) throw new Error(tagName + " element is missing 'xlink:href' attribute");
return href;
}
throw new Error("No URI found");
}
deepClone(o) {
return JSON.parse(JSON.stringify(o));
}
resolveLocation(dom, parts) {
const index = parts.length - 1;
const subparts = parts[index];
if(!subparts) throw new Error("Missing CFI part for index: " + index);
var o = this.resolveNode(index, subparts, dom);
var lastpart = this.deepClone(subparts[subparts.length - 1]);
delete lastpart.nodeIndex;
if(!lastpart.offset) delete o.offset;
Object.assign(lastpart, o);
return lastpart;
}
// Takes the Document or XMLDocument for the final
// document referenced by the CFI
// and returns the node and offset into that node
resolveLast(dom, opts) {
opts = Object.assign({
range: false
}, opts || {});
if(!this.isRange) {
return this.resolveLocation(dom, this.parts);
}
if(opts.range) {
const range = dom.createRange();
const from = this.getFrom();
if(from.relativeToNode === 'before') {
range.setStartBefore(from.node, from.offset)
} else if(from.relativeToNode === 'after') {
range.setStartAfter(from.node, from.offset)
} else {
range.setStart(from.node, from.offset);
}
const to = this.getTo();
if(to.relativeToNode === 'before') {
range.setEndBefore(to.node, to.offset)
} else if(to.relativeToNode === 'after') {
range.setEndAfter(to.node, to.offset)
} else {
range.setEnd(to.node, to.offset);
}
return range;
}
return {
from: this.resolveLocation(dom, this.getFrom()),
to: this.resolveLocation(dom, this.getTo()),