aksos-react-native-epub-reader 0.1.1 → 0.2.1

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +158 -13
  3. package/lib/EpubReader.d.ts +47 -9
  4. package/lib/EpubReader.js +69 -16
  5. package/lib/EpubReaderView.d.ts +27 -2
  6. package/lib/EpubReaderView.js +38 -32
  7. package/lib/cfiOrder.d.ts +2 -0
  8. package/lib/cfiOrder.js +34 -0
  9. package/lib/engine/html.js +1 -1
  10. package/lib/index.d.ts +1 -1
  11. package/lib/themes.d.ts +24 -6
  12. package/lib/themes.js +80 -20
  13. package/lib/types.d.ts +66 -1
  14. package/lib/ui/AppearanceSheet.d.ts +4 -2
  15. package/lib/ui/AppearanceSheet.js +20 -13
  16. package/lib/ui/ContentsSheet.d.ts +2 -0
  17. package/lib/ui/ContentsSheet.js +21 -13
  18. package/lib/ui/CustomizeSheet.d.ts +4 -2
  19. package/lib/ui/CustomizeSheet.js +20 -18
  20. package/lib/ui/HighlightMenu.d.ts +4 -2
  21. package/lib/ui/HighlightMenu.js +3 -3
  22. package/lib/ui/NoteEditor.js +6 -5
  23. package/lib/ui/SearchSheet.js +6 -5
  24. package/lib/ui/Sheet.js +5 -4
  25. package/lib/ui/Text.d.ts +9 -0
  26. package/lib/ui/Text.js +62 -0
  27. package/package.json +81 -76
  28. package/src/EpubReader.tsx +121 -25
  29. package/src/EpubReaderView.tsx +61 -17
  30. package/src/cfiOrder.ts +28 -0
  31. package/src/engine/epubcfi.js +527 -0
  32. package/src/engine/html.ts +1 -1
  33. package/src/engine/reader.js +830 -65
  34. package/src/index.ts +1 -1
  35. package/src/themes.ts +97 -21
  36. package/src/types.ts +71 -1
  37. package/src/ui/AppearanceSheet.tsx +19 -10
  38. package/src/ui/ContentsSheet.tsx +14 -4
  39. package/src/ui/CustomizeSheet.tsx +14 -10
  40. package/src/ui/HighlightMenu.tsx +6 -4
  41. package/src/ui/NoteEditor.tsx +2 -1
  42. package/src/ui/SearchSheet.tsx +2 -1
  43. package/src/ui/Sheet.tsx +4 -3
  44. package/src/ui/Text.tsx +27 -0
@@ -0,0 +1,527 @@
1
+ /*
2
+ * epub.js compatibility: CFI strings and "locations" (chunks).
3
+ *
4
+ * Ported from epub.js 0.3 (https://github.com/futurepress/epub.js): src/epubcfi.js, src/locations.js
5
+ * and the helpers they use. Copyright (c) 2013, FuturePress. BSD 2-Clause licence, see the end of this file.
6
+ *
7
+ * Goal: the exact same strings and numbers as epub.js. So its quirks are kept on purpose:
8
+ * - a text step counts only the text nodes of its parent, not its place between elements
9
+ * - compare() looks at step numbers only, not at the step type
10
+ * - parseLocations() skips one character after every chunk ("pos += 1")
11
+ * Do not "fix" them. Saved highlights and chunk numbers depend on them.
12
+ * Left out: ignoreClass (epub.js passes "" by default, so it never runs).
13
+ */
14
+ var EpubCompat = (function () {
15
+ 'use strict';
16
+
17
+ function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }
18
+ function typeOf(obj) { return Object.prototype.toString.call(obj).slice(8, -1); }
19
+ function extend(target, source) {
20
+ if (source) Object.keys(source).forEach(function (k) { target[k] = source[k]; });
21
+ return target;
22
+ }
23
+
24
+ function EpubCFI(cfiFrom, base) {
25
+ this.str = '';
26
+ this.base = {};
27
+ this.spinePos = 0;
28
+ this.range = false;
29
+ this.path = {};
30
+ this.start = null;
31
+ this.end = null;
32
+ if (typeof base === 'string') this.base = this.parseComponent(base);
33
+ else if (typeof base === 'object' && base && base.steps) this.base = base;
34
+ var type = this.checkType(cfiFrom);
35
+ if (type === 'string') { this.str = cfiFrom; return extend(this, this.parse(cfiFrom)); }
36
+ if (type === 'range') return extend(this, this.fromRange(cfiFrom, this.base));
37
+ if (type === 'node') return extend(this, this.fromNode(cfiFrom, this.base));
38
+ if (type === 'EpubCFI' && cfiFrom.path) return cfiFrom;
39
+ if (cfiFrom) throw new TypeError('not a valid argument for EpubCFI');
40
+ }
41
+
42
+ var P = EpubCFI.prototype;
43
+
44
+ P.checkType = function (cfi) {
45
+ if (this.isCfiString(cfi)) return 'string';
46
+ if (cfi && typeof cfi === 'object' && (typeOf(cfi) === 'Range' || typeof cfi.startContainer !== 'undefined')) return 'range';
47
+ if (cfi && typeof cfi === 'object' && typeof cfi.nodeType !== 'undefined') return 'node';
48
+ if (cfi && typeof cfi === 'object' && cfi instanceof EpubCFI) return 'EpubCFI';
49
+ return false;
50
+ };
51
+
52
+ P.parse = function (cfiStr) {
53
+ var cfi = { spinePos: -1, range: false, base: {}, path: {}, start: null, end: null };
54
+ if (typeof cfiStr !== 'string') return { spinePos: -1 };
55
+ if (cfiStr.indexOf('epubcfi(') === 0 && cfiStr[cfiStr.length - 1] === ')') cfiStr = cfiStr.slice(8, cfiStr.length - 1);
56
+ var baseComponent = this.getChapterComponent(cfiStr);
57
+ if (!baseComponent) return { spinePos: -1 };
58
+ cfi.base = this.parseComponent(baseComponent);
59
+ cfi.path = this.parseComponent(this.getPathComponent(cfiStr));
60
+ var range = this.getRange(cfiStr);
61
+ if (range) {
62
+ cfi.range = true;
63
+ cfi.start = this.parseComponent(range[0]);
64
+ cfi.end = this.parseComponent(range[1]);
65
+ }
66
+ cfi.spinePos = cfi.base.steps[1].index;
67
+ return cfi;
68
+ };
69
+
70
+ P.parseComponent = function (componentStr) {
71
+ var component = { steps: [], terminal: { offset: null, assertion: null } };
72
+ var parts = componentStr.split(':');
73
+ var steps = parts[0].split('/');
74
+ if (parts.length > 1) component.terminal = this.parseTerminal(parts[1]);
75
+ if (steps[0] === '') steps.shift();
76
+ component.steps = steps.map(function (step) { return this.parseStep(step); }.bind(this));
77
+ return component;
78
+ };
79
+
80
+ P.parseStep = function (stepStr) {
81
+ var type, index, id;
82
+ var brackets = stepStr.match(/\[(.*)\]/);
83
+ if (brackets && brackets[1]) id = brackets[1];
84
+ var num = parseInt(stepStr);
85
+ if (isNaN(num)) return;
86
+ if (num % 2 === 0) { type = 'element'; index = num / 2 - 1; }
87
+ else { type = 'text'; index = (num - 1) / 2; }
88
+ return { type: type, index: index, id: id || null };
89
+ };
90
+
91
+ P.parseTerminal = function (termialStr) {
92
+ var characterOffset, textLocationAssertion;
93
+ var assertion = termialStr.match(/\[(.*)\]/);
94
+ if (assertion && assertion[1]) {
95
+ characterOffset = parseInt(termialStr.split('[')[0]);
96
+ textLocationAssertion = assertion[1];
97
+ } else {
98
+ characterOffset = parseInt(termialStr);
99
+ }
100
+ if (!isNumber(characterOffset)) characterOffset = null;
101
+ return { offset: characterOffset, assertion: textLocationAssertion };
102
+ };
103
+
104
+ P.getChapterComponent = function (cfiStr) { return cfiStr.split('!')[0]; };
105
+ P.getPathComponent = function (cfiStr) {
106
+ var indirection = cfiStr.split('!');
107
+ if (indirection[1]) return indirection[1].split(',')[0];
108
+ };
109
+ P.getRange = function (cfiStr) {
110
+ var ranges = cfiStr.split(',');
111
+ return ranges.length === 3 ? [ranges[1], ranges[2]] : false;
112
+ };
113
+
114
+ P.joinSteps = function (steps) {
115
+ if (!steps) return '';
116
+ return steps.map(function (part) {
117
+ var segment = '';
118
+ if (part.type === 'element') segment += (part.index + 1) * 2;
119
+ if (part.type === 'text') segment += 1 + (2 * part.index);
120
+ if (part.id) segment += '[' + part.id + ']';
121
+ return segment;
122
+ }).join('/');
123
+ };
124
+
125
+ P.segmentString = function (segment) {
126
+ var segmentString = '/';
127
+ segmentString += this.joinSteps(segment.steps);
128
+ if (segment.terminal && segment.terminal.offset != null) segmentString += ':' + segment.terminal.offset;
129
+ if (segment.terminal && segment.terminal.assertion != null) segmentString += '[' + segment.terminal.assertion + ']';
130
+ return segmentString;
131
+ };
132
+
133
+ P.toString = function () {
134
+ var cfiString = 'epubcfi(';
135
+ cfiString += this.segmentString(this.base);
136
+ cfiString += '!';
137
+ cfiString += this.segmentString(this.path);
138
+ if (this.range && this.start) cfiString += ',' + this.segmentString(this.start);
139
+ if (this.range && this.end) cfiString += ',' + this.segmentString(this.end);
140
+ cfiString += ')';
141
+ return cfiString;
142
+ };
143
+
144
+ P.compare = function (cfiOne, cfiTwo) {
145
+ var stepsA, stepsB, terminalA, terminalB;
146
+ if (typeof cfiOne === 'string') cfiOne = new EpubCFI(cfiOne);
147
+ if (typeof cfiTwo === 'string') cfiTwo = new EpubCFI(cfiTwo);
148
+ if (cfiOne.spinePos > cfiTwo.spinePos) return 1;
149
+ if (cfiOne.spinePos < cfiTwo.spinePos) return -1;
150
+ if (cfiOne.range) { stepsA = cfiOne.path.steps.concat(cfiOne.start.steps); terminalA = cfiOne.start.terminal; }
151
+ else { stepsA = cfiOne.path.steps; terminalA = cfiOne.path.terminal; }
152
+ if (cfiTwo.range) { stepsB = cfiTwo.path.steps.concat(cfiTwo.start.steps); terminalB = cfiTwo.start.terminal; }
153
+ else { stepsB = cfiTwo.path.steps; terminalB = cfiTwo.path.terminal; }
154
+ for (var i = 0; i < stepsA.length; i++) {
155
+ if (!stepsA[i]) return -1;
156
+ if (!stepsB[i]) return 1;
157
+ if (stepsA[i].index > stepsB[i].index) return 1;
158
+ if (stepsA[i].index < stepsB[i].index) return -1;
159
+ }
160
+ if (stepsA.length < stepsB.length) return -1;
161
+ if (terminalA.offset > terminalB.offset) return 1;
162
+ if (terminalA.offset < terminalB.offset) return -1;
163
+ return 0;
164
+ };
165
+
166
+ P.step = function (node) {
167
+ return { id: node.id, tagName: node.tagName, type: node.nodeType === 3 ? 'text' : 'element', index: this.position(node) };
168
+ };
169
+
170
+ P.pathTo = function (node, offset) {
171
+ var segment = { steps: [], terminal: { offset: null, assertion: null } };
172
+ var currentNode = node, step;
173
+ while (currentNode && currentNode.parentNode && currentNode.parentNode.nodeType != 9) {
174
+ step = this.step(currentNode);
175
+ if (step) segment.steps.unshift(step);
176
+ currentNode = currentNode.parentNode;
177
+ }
178
+ if (offset != null && offset >= 0) {
179
+ segment.terminal.offset = offset;
180
+ // make sure we are getting to a text node
181
+ if (segment.steps[segment.steps.length - 1].type != 'text') segment.steps.push({ type: 'text', index: 0 });
182
+ }
183
+ return segment;
184
+ };
185
+
186
+ P.equalStep = function (stepA, stepB) {
187
+ if (!stepA || !stepB) return false;
188
+ return stepA.index === stepB.index && stepA.id === stepB.id && stepA.type === stepB.type;
189
+ };
190
+
191
+ P.fromRange = function (range, base) {
192
+ var cfi = { range: false, base: {}, path: {}, start: null, end: null };
193
+ var start = range.startContainer, end = range.endContainer;
194
+ var startOffset = range.startOffset, endOffset = range.endOffset;
195
+ if (typeof base === 'string') {
196
+ cfi.base = this.parseComponent(base);
197
+ cfi.spinePos = cfi.base.steps[1].index;
198
+ } else if (typeof base === 'object') {
199
+ cfi.base = base;
200
+ }
201
+ if (range.collapsed) {
202
+ cfi.path = this.pathTo(start, startOffset);
203
+ } else {
204
+ cfi.range = true;
205
+ cfi.start = this.pathTo(start, startOffset);
206
+ cfi.end = this.pathTo(end, endOffset);
207
+ cfi.path = { steps: [], terminal: null };
208
+ var len = cfi.start.steps.length, i;
209
+ for (i = 0; i < len && this.equalStep(cfi.start.steps[i], cfi.end.steps[i]); i++) {
210
+ if (i === len - 1) {
211
+ // terminals are different objects, so this never matches (same as epub.js)
212
+ if (cfi.start.terminal === cfi.end.terminal) {
213
+ cfi.path.steps.push(cfi.start.steps[i]);
214
+ cfi.range = false;
215
+ }
216
+ } else {
217
+ cfi.path.steps.push(cfi.start.steps[i]);
218
+ }
219
+ }
220
+ cfi.start.steps = cfi.start.steps.slice(cfi.path.steps.length);
221
+ cfi.end.steps = cfi.end.steps.slice(cfi.path.steps.length);
222
+ }
223
+ return cfi;
224
+ };
225
+
226
+ P.fromNode = function (anchor, base) {
227
+ var cfi = { range: false, base: {}, path: {}, start: null, end: null };
228
+ if (typeof base === 'string') {
229
+ cfi.base = this.parseComponent(base);
230
+ cfi.spinePos = cfi.base.steps[1].index;
231
+ } else if (typeof base === 'object') {
232
+ cfi.base = base;
233
+ }
234
+ cfi.path = this.pathTo(anchor, null);
235
+ return cfi;
236
+ };
237
+
238
+ P.normalizedMap = function (children, nodeType, ignoreClass) {
239
+ var output = {}, prevIndex = -1, len = children.length, currNodeType, prevNodeType;
240
+ for (var i = 0; i < len; i++) {
241
+ currNodeType = children[i].nodeType;
242
+ if (currNodeType === 1 && children[i].classList && children[i].classList.contains(ignoreClass)) currNodeType = 3;
243
+ if (i > 0 && currNodeType === 3 && prevNodeType === 3) {
244
+ output[i] = prevIndex;
245
+ } else if (nodeType === currNodeType) {
246
+ prevIndex = prevIndex + 1;
247
+ output[i] = prevIndex;
248
+ }
249
+ prevNodeType = currNodeType;
250
+ }
251
+ return output;
252
+ };
253
+
254
+ P.position = function (anchor) {
255
+ var children, index;
256
+ if (anchor.nodeType === 1) {
257
+ children = anchor.parentNode.children || findChildren(anchor.parentNode);
258
+ index = Array.prototype.indexOf.call(children, anchor);
259
+ } else {
260
+ children = this.textNodes(anchor.parentNode);
261
+ index = children.indexOf(anchor);
262
+ }
263
+ return index;
264
+ };
265
+
266
+ function findChildren(el) {
267
+ var result = [];
268
+ for (var i = 0; i < el.childNodes.length; i++) if (el.childNodes[i].nodeType === 1) result.push(el.childNodes[i]);
269
+ return result;
270
+ }
271
+
272
+ P.stepsToXpath = function (steps) {
273
+ var xpath = ['.', '*'];
274
+ steps.forEach(function (step) {
275
+ var position = step.index + 1;
276
+ if (step.id) xpath.push('*[position()=' + position + ' and @id=\'' + step.id + '\']');
277
+ else if (step.type === 'text') xpath.push('text()[' + position + ']');
278
+ else xpath.push('*[' + position + ']');
279
+ });
280
+ return xpath.join('/');
281
+ };
282
+
283
+ P.textNodes = function (container) {
284
+ return Array.prototype.slice.call(container.childNodes).filter(function (node) { return node.nodeType === 3; });
285
+ };
286
+
287
+ P.walkToNode = function (steps, _doc) {
288
+ var doc = _doc || document, container = doc.documentElement, step;
289
+ for (var i = 0; i < steps.length; i++) {
290
+ step = steps[i];
291
+ if (step.type === 'element') {
292
+ if (step.id) container = doc.getElementById(step.id);
293
+ else container = (container.children || findChildren(container))[step.index];
294
+ } else if (step.type === 'text') {
295
+ container = this.textNodes(container)[step.index];
296
+ }
297
+ if (!container) break;
298
+ }
299
+ return container;
300
+ };
301
+
302
+ P.findNode = function (steps, _doc) {
303
+ var doc = _doc || document;
304
+ if (typeof doc.evaluate === 'undefined') return this.walkToNode(steps, doc);
305
+ return doc.evaluate(this.stepsToXpath(steps), doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
306
+ };
307
+
308
+ P.fixMiss = function (steps, offset, _doc) {
309
+ var container = this.findNode(steps.slice(0, -1), _doc);
310
+ var children = container.childNodes;
311
+ var map = this.normalizedMap(children, 3, null);
312
+ var lastStepIndex = steps[steps.length - 1].index, child, len;
313
+ for (var childIndex in map) {
314
+ if (!map.hasOwnProperty(childIndex)) return;
315
+ if (map[childIndex] === lastStepIndex) {
316
+ child = children[childIndex];
317
+ len = child.textContent.length;
318
+ if (offset > len) {
319
+ offset = offset - len;
320
+ } else {
321
+ container = child.nodeType === 1 ? child.childNodes[0] : child;
322
+ break;
323
+ }
324
+ }
325
+ }
326
+ return { container: container, offset: offset };
327
+ };
328
+
329
+ P.toRange = function (_doc) {
330
+ var doc = _doc || document, range = doc.createRange();
331
+ var start, end, startContainer, endContainer, startSteps, endSteps, missed;
332
+ if (this.range) {
333
+ start = this.start;
334
+ startSteps = this.path.steps.concat(start.steps);
335
+ startContainer = this.findNode(startSteps, doc);
336
+ end = this.end;
337
+ endSteps = this.path.steps.concat(end.steps);
338
+ endContainer = this.findNode(endSteps, doc);
339
+ } else {
340
+ start = this.path;
341
+ startSteps = this.path.steps;
342
+ startContainer = this.findNode(this.path.steps, doc);
343
+ }
344
+ if (!startContainer) return null;
345
+ try {
346
+ if (start.terminal.offset != null) range.setStart(startContainer, start.terminal.offset);
347
+ else range.setStart(startContainer, 0);
348
+ } catch (e) {
349
+ missed = this.fixMiss(startSteps, start.terminal.offset, doc);
350
+ range.setStart(missed.container, missed.offset);
351
+ }
352
+ if (endContainer) {
353
+ try {
354
+ if (end.terminal.offset != null) range.setEnd(endContainer, end.terminal.offset);
355
+ else range.setEnd(endContainer, 0);
356
+ } catch (e) {
357
+ missed = this.fixMiss(endSteps, this.end.terminal.offset, doc);
358
+ range.setEnd(missed.container, missed.offset);
359
+ }
360
+ }
361
+ return range;
362
+ };
363
+
364
+ P.isCfiString = function (str) {
365
+ return typeof str === 'string' && str.indexOf('epubcfi(') === 0 && str[str.length - 1] === ')';
366
+ };
367
+
368
+ P.generateChapterComponent = function (_spineNodeIndex, _pos, id) {
369
+ var pos = parseInt(_pos), spineNodeIndex = (_spineNodeIndex + 1) * 2;
370
+ var cfi = '/' + spineNodeIndex + '/';
371
+ cfi += (pos + 1) * 2;
372
+ if (id) cfi += '[' + id + ']';
373
+ return cfi;
374
+ };
375
+
376
+ P.collapse = function (toStart) {
377
+ if (!this.range) return;
378
+ this.range = false;
379
+ if (toStart) {
380
+ this.path.steps = this.path.steps.concat(this.start.steps);
381
+ this.path.terminal = this.start.terminal;
382
+ } else {
383
+ this.path.steps = this.path.steps.concat(this.end.steps);
384
+ this.path.terminal = this.end.terminal;
385
+ }
386
+ };
387
+
388
+ /* ------------------------------------------------------------ helpers */
389
+
390
+ // core.indexOfElementNode: place of an element among its parent's element children
391
+ function indexOfElementNode(el) {
392
+ var children = el.parentNode.childNodes, index = -1;
393
+ for (var i = 0; i < children.length; i++) {
394
+ if (children[i].nodeType === 1) index++;
395
+ if (children[i] == el) break;
396
+ }
397
+ return index;
398
+ }
399
+
400
+ // core.locationOf: where `item` would be inserted in the sorted `array`
401
+ function locationOf(item, array, compareFunction, _start, _end) {
402
+ var start = _start || 0, end = _end || array.length;
403
+ var pivot = parseInt(start + (end - start) / 2), compared;
404
+ if (!compareFunction) compareFunction = function (a, b) { return a > b ? 1 : a < b ? -1 : a == b ? 0 : undefined; };
405
+ if (end - start <= 0) return pivot;
406
+ compared = compareFunction(array[pivot], item);
407
+ if (end - start === 1) return compared >= 0 ? pivot : pivot + 1;
408
+ if (compared === 0) return pivot;
409
+ if (compared === -1) return locationOf(item, array, compareFunction, pivot, end);
410
+ return locationOf(item, array, compareFunction, start, pivot);
411
+ }
412
+
413
+ // core.sprint: every text node under root, in document order
414
+ function sprint(root, func) {
415
+ var walker = (root.ownerDocument || root).createTreeWalker(root, 4, null, false), node;
416
+ while ((node = walker.nextNode())) func(node);
417
+ }
418
+
419
+ // Locations.parse: the chunk CFIs of one section. `contents` is the section's documentElement.
420
+ function parseLocations(contents, cfiBase, chars) {
421
+ var locations = [], range, prev, counter = 0, _break = chars;
422
+ var body = contents.ownerDocument.querySelector('body');
423
+ function createRange() { return { startContainer: undefined, startOffset: undefined, endContainer: undefined, endOffset: undefined }; }
424
+ if (!body) return locations;
425
+ sprint(body, function (node) {
426
+ var len = node.length, dist, pos = 0;
427
+ if (node.textContent.trim().length === 0) return false;
428
+ // start a new range
429
+ if (counter == 0) {
430
+ range = createRange();
431
+ range.startContainer = node;
432
+ range.startOffset = 0;
433
+ }
434
+ dist = _break - counter;
435
+ // node is smaller than a break, skip over it
436
+ if (dist > len) {
437
+ counter += len;
438
+ pos = len;
439
+ }
440
+ while (pos < len) {
441
+ dist = _break - counter;
442
+ if (counter === 0) {
443
+ // start new range
444
+ pos += 1;
445
+ range = createRange();
446
+ range.startContainer = node;
447
+ range.startOffset = pos;
448
+ }
449
+ if (pos + dist >= len) {
450
+ // node is smaller than a break, skip it
451
+ counter += len - pos;
452
+ pos = len;
453
+ } else {
454
+ // advance the position to the break
455
+ pos += dist;
456
+ // end the previous range
457
+ range.endContainer = node;
458
+ range.endOffset = pos;
459
+ locations.push(new EpubCFI(range, cfiBase).toString());
460
+ counter = 0;
461
+ }
462
+ }
463
+ prev = node;
464
+ });
465
+ // close remaining
466
+ if (range && range.startContainer && prev) {
467
+ range.endContainer = prev;
468
+ range.endOffset = prev.length;
469
+ locations.push(new EpubCFI(range, cfiBase).toString());
470
+ counter = 0;
471
+ }
472
+ return locations;
473
+ }
474
+
475
+ // Locations: lookups on a finished list, like book.locations in epub.js
476
+ function Locations(list) {
477
+ this._locations = list;
478
+ this.total = list.length - 1;
479
+ // parsed once; compare() gives the same answer for strings and parsed CFIs
480
+ this._parsed = list.map(function (s) { return new EpubCFI(s); });
481
+ }
482
+ Locations.prototype.locationFromCfi = function (cfi) {
483
+ if (P.isCfiString(cfi)) cfi = new EpubCFI(cfi);
484
+ if (this._locations.length === 0) return -1;
485
+ var loc = locationOf(cfi, this._parsed, P.compare);
486
+ return loc > this.total ? this.total : loc;
487
+ };
488
+ Locations.prototype.percentageFromLocation = function (loc) {
489
+ if (!loc || !this.total) return 0;
490
+ return loc / this.total;
491
+ };
492
+ Locations.prototype.percentageFromCfi = function (cfi) {
493
+ if (this._locations.length === 0) return null;
494
+ return this.percentageFromLocation(this.locationFromCfi(cfi));
495
+ };
496
+
497
+ return {
498
+ EpubCFI: EpubCFI,
499
+ Locations: Locations,
500
+ parseLocations: parseLocations,
501
+ locationOf: locationOf,
502
+ indexOfElementNode: indexOfElementNode
503
+ };
504
+ })();
505
+
506
+ /*
507
+ * epub.js licence (for the code ported above):
508
+ *
509
+ * Copyright (c) 2013, FuturePress
510
+ * All rights reserved.
511
+ *
512
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that
513
+ * the following conditions are met:
514
+ *
515
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
516
+ * following disclaimer.
517
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
518
+ * following disclaimer in the documentation and/or other materials provided with the distribution.
519
+ *
520
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
521
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
522
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
523
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
524
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
525
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
526
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
527
+ */