asteroid/templates/reader/extensions/epubcfi.js

8 lines
No EOL
5.3 KiB
JavaScript

/*!
*
* # Bibi Extension: EPUBCFI (℠)
*
* * © Satoru Matsushima - https://bibi.epub.link or https://github.com/satorumurmur/bibi
* * Open source under the MIT License - https://github.com/satorumurmur/bibi/blob/master/LICENSE
*
*/!function(e){var t={};function r(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)r.d(n,a,function(t){return e[t]}.bind(null,a));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=17)}({17:function(e,t){Bibi.x({id:"EPUBCFI",description:"Utilities for EPUBCFI",author:"Satoru Matsushima (@satorumurmur)",version:"1.2.0-demo"})((function(){"use strict";var e=this;this.CFIString="",this.Current=0,this.Log=!1,this.LogCorrection=!1,this.LogCancelation=!1,this.parse=function(t,r){if(!t||"string"!=typeof t)return null;try{t=decodeURIComponent(t)}catch(t){return e.log(0,"Unregulated URIEncoding."),null}return r&&"string"==typeof r&&"function"==typeof e["parse"+r]||(r="Fragment"),"Fragment"==r&&(t=t.replace(/^(epubcfi\()?/,"epubcfi(").replace(/(\))?$/,")")),e.CFIString=t,e.Current=0,e.Log&&(e.log(1,"Bibi EPUB-CFI"),e.log(2,"parse"),e.log(3,"CFIString: ".concat(e.CFIString))),e["parse"+r]()},this.parseFragment=function(){var t=e.Current;if(!e.parseString("epubcfi("))return e.cancel(t,"Fragment");var r=e.parseCFI();return null===r?e.cancel(t):e.parseString(")")?r:e.cancel(t,"Fragment")},this.parseCFI=function(){var t=e.Current,r={Type:"CFI",Path:e.parsePath()};if(!r.Path)return e.cancel(t,"CFI");if(e.parseString(",")){if(r.Start=e.parseLocalPath(),!r.Start.Steps.length&&!r.Start.TermStep)return e.cancel(t,"CFI > Range");if(!e.parseString(","))return e.cancel(t,"CFI > Range");if(r.End=e.parseLocalPath(),!r.End.Steps.length&&!r.End.TermStep)return e.cancel(t,"CFI > Range")}return r},this.parsePath=function(){var t=e.Current,r={Type:"Path",Steps:[e.parseStep()]},n=e.parseLocalPath();return r.Steps[0]&&n?(r.Steps=r.Steps.concat(n.Steps),r):e.cancel(t,"Path")},this.parseLocalPath=function(){e.Current;for(var t={Type:"LocalPath",Steps:[]},r=t,n=e.parseStep("Local"),a=null;null!==n&&(r.Steps.push(n),n=e.parseStep("Local"));)if("IndirectStep"==n.Type){var i={Type:"IndirectPath",Steps:[]};r.Steps.push(i),r=i}else if("TermStep"==n.Type){a=n;break}return a&&r.Steps.push(a),t.Steps.length?t:null},this.parseStep=function(t){var r=e.Current,n={};if(e.parseString("/"))n.Type="Step";else if(t&&e.parseString("!/"))n.Type="IndirectStep";else{if(!t||!e.parseString(":"))return e.cancel(r,"Step");n.Type="TermStep"}if(n.Index=e.parseString(/^(0|[1-9][0-9]*)/),null===n.Index)return e.cancel(r,"Step");if(n.Index=parseInt(n.Index),e.parseString("[")){if("TermStep"!=n.Type){if(n.ID=e.parseString(/^[a-zA-Z_:][a-zA-Z0-9_:\-\.]+/),!n.ID)return e.cancel(r,"Step > Assertion > ID")}else{var a=[],i=null,o=/^((\^[\^\[\]\(\)\,\;\=])|[_a-zA-Z0-9%\- ])*/;if(a.push(e.parseString(o)),e.parseString(",")&&a.push(e.parseString(o)),a[0]&&(n.Preceding=a[0]),a[1]&&(n.Following=a[1]),e.parseString(/^;s=/)&&(function(e){throw new Error('"'+e+'" is read-only')}("Side"),i=e.parseString(/^[ab]/)),i&&(n.Side=i),!n.Preceding&&!n.Following&&!n.Side)return e.cancel(r,"Step > Assertion > TextLocation")}if(!e.parseString("]"))return e.cancel(r,"Step > Assertion")}return n},this.parseString=function(t){var r=null,n=!1;if(t instanceof RegExp){var a=e.CFIString.substr(e.Current,e.CFIString.length-e.Current);t.test(a)&&(n=!0,t=a.match(t)[0])}else e.CFIString.substr(e.Current,t.length)===t&&(n=!0);return n&&(e.Current+=t.length,r=t),e.correct(r)},this.correct=function(t){return e.Log&&e.LogCorrection&&t&&e.log(3,t),t},this.cancel=function(t,r){return e.Log&&e.LogCancelation&&e.log(4,"cancel: parse ".concat(r," (").concat(t,"-").concat(e.Current,"/").concat(e.CFIString.length,")")),"number"==typeof t&&(e.Current=t),null},this.log=function(t,r){e.Log&&(0==t?r="[ERROR] ".concat(r):1==t?r="---------------- ".concat(r," ----------------"):2==t?r=r:3==t?r=" - ".concat(r):4==t&&(r=" . ".concat(r)),O.log("EPUBCFI: ".concat(r)))},this.getDestination=function(e){var t=X.EPUBCFI.parse(e);if(!t||t.Path.Steps.length<2||!t.Path.Steps[1].Index||t.Path.Steps[1].Index%2==1)return null;var r=t.Path.Steps[1].Index/2-1,n=null,a=null,i=null,o=null;return t.Path.Steps[2]&&t.Path.Steps[2].Steps&&(n="",t.Path.Steps[2].Steps.forEach((function(e,r){return"IndirectPath"==e.Type?(o=e,!1):"TermStep"==e.Type?(i=e,!1):(e.Index%2!=1||(a=e.Index-1,r==t.Path.Steps[2].Steps.length-2))&&void(null===a&&(n=e.ID?"#"+e.ID:n+">*:nth-child("+e.Index/2+")"))})),n&&/^>/.test(n)&&(n="html"+n),n||(n=null)),{CFI:t,CFIString:e,ItemIndexInAll:r,ElementSelector:n,TextNodeIndex:a,TermStep:i,IndirectPath:o}}}))}});