var CETEI=function(){"use strict";var e={};e.classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},e.createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}return function(t,r,a){return r&&e(t.prototype,r),a&&e(t,a),t}}();var t={handlers:{ptr:['<a href="$rw@target">$@target</a>'],ref:['<a href="$rw@target">',"</a>"],graphic:function(){var e=this;return function(){var t=this.createShadowRoot(),r=new Image;r.src=e.rw(this.getAttribute("url")),this.hasAttribute("width")&&(r.width=this.getAttribute("width").replace(/[^.0-9]/g,"")),this.hasAttribute("height")&&(r.height=this.getAttribute("height").replace(/[^.0-9]/g,"")),t.appendChild(r)}},egXML:function(){return function(){var e=this.createShadowRoot();e.innerHTML="<pre>"+this.innerHTML.replace(/</g,"&lt;")+"</pre>"}}},fallbacks:{ref:function(e){var t=this;e.addEventListener("click",function(r){window.location=t.rw(e.getAttribute("target"))})},graphic:function(e){var t=this,r=new Image;r.src=t.rw(this.getAttribute("url")),e.hasAttribute("width")&&(r.width=e.getAttribute("width").replace(/[^.0-9]/g,"")),e.hasAttribute("height")&&(r.height=e.getAttribute("height").replace(/[^.0-9]/g,"")),e.appendChild(r)},egXML:function(e){var t=e.innerHTML;e.innerHTML='<span style="display:none">'+e.innerHTML+"</span>",e.innerHTML+="<pre>"+t.replace(/</g,"&lt;")+"</pre>"}}},r=function(){function r(a){e.classCallCheck(this,r),this.els=[],this.behaviors=[],this.hasStyle=!1,this.prefixes=[],a?this.base=a:this.base=window.location.href.replace(/\/[^\/]*$/,"/"),this.behaviors.push(t)}return e.createClass(r,[{key:"getHTML5",value:function(e,t){var r=this,a=new Promise(function(t,r){var a=new XMLHttpRequest;a.open("GET",e),a.send(),a.onload=function(){this.status>=200&&this.status<300?t(this.response):r(this.statusText)},a.onerror=function(){r(this.statusText)}}).then(function(e){r.makeHTML5(e,t)})["catch"](function(e){console.log(e)});return a}},{key:"makeHTML5",value:function(e,t){var r=this,a=(new window.DOMParser).parseFromString(e,"text/xml");this._fromTEI(a);var i=function n(e){var t=void 0,a=!1;switch(e.namespaceURI){case"http://www.tei-c.org/ns/1.0":t=document.createElement("tei-"+e.tagName);break;case"http://www.tei-c.org/ns/Examples":if("egXML"==e.tagName){t=document.createElement("teieg-"+e.tagName);break}default:t=document.importNode(e,!1),a=!0}var i=!0,s=!1,l=void 0;try{for(var o,h=Array.from(e.attributes)[Symbol.iterator]();!(i=(o=h.next()).done);i=!0){var c=o.value;"xmlns"!=c.name||a?t.setAttribute(c.name,c.value):t.setAttribute("data-xmlns",c.value),"xml:id"!=c.name||a||t.setAttribute("id",c.value),"xml:lang"!=c.name||a||t.setAttribute("lang",c.value),"rendition"==c.name&&t.setAttribute("class",c.value.replace(/#/g,""))}}catch(u){s=!0,l=u}finally{try{!i&&h["return"]&&h["return"]()}finally{if(s)throw l}}var d=!0,f=!1,v=void 0;try{for(var g,m=Array.from(e.childNodes)[Symbol.iterator]();!(d=(g=m.next()).done);d=!0){var y=g.value;y.nodeType==Node.ELEMENT_NODE?t.appendChild(n(y)):t.appendChild(y.cloneNode())}}catch(u){f=!0,v=u}finally{try{!d&&m["return"]&&m["return"]()}finally{if(f)throw v}}if("tagsDecl"==e.localName){var b=document.createElement("style"),p=!0,w=!1,A=void 0;try{for(var k,T=Array.from(e.childNodes)[Symbol.iterator]();!(p=(k=T.next()).done);p=!0){var E=k.value;if(E.nodeType==Node.ELEMENT_NODE&&"rendition"==E.localName&&"css"==E.getAttribute("scheme")){var N="";E.hasAttribute("selector")?(N+=E.getAttribute("selector").replace(/([^#, >]+\w*)/g,"tei-$1").replace(/#tei-/g,"#")+"{\n",N+=E.textContent):(N+="."+E.getAttribute("xml:id")+"{\n",N+=E.textContent),N+="\n}\n",b.appendChild(document.createTextNode(N))}}}catch(u){w=!0,A=u}finally{try{!p&&T["return"]&&T["return"]()}finally{if(w)throw A}}b.childNodes.length>0&&(t.appendChild(b),r.hasStyle=!0)}return"prefixDef"==e.localName&&(r.prefixes.push(e.getAttribute("ident")),r.prefixes[e.getAttribute("ident")]={matchPattern:e.getAttribute("matchPattern"),replacementPattern:e.getAttribute("replacementPattern")}),t};return this.dom=i(a.documentElement),document.registerElement?this.registerAll(this.els):this.fallback(this.els),t?void t(this.dom,this):this.dom}},{key:"addStyle",value:function(e,t){this.hasStyle&&e.getElementsByTagName("head").item(0).appendChild(t.getElementsByTagName("style").item(0).cloneNode(!0))}},{key:"addBehaviors",value:function(e){e.handlers||e.fallbacks?this.behaviors.push(e):console.log("No handlers or fallback methods found.")}},{key:"setBaseUrl",value:function(e){this.base=e}},{key:"_fromTEI",value:function(e){var t=e.documentElement;this.els=new Set(Array.from(t.getElementsByTagName("*"),function(e){return e.tagName})),this.els.add(t.tagName)}},{key:"_insert",value:function(e,t){if(e.createShadowRoot){var r=e.createShadowRoot();r.innerHTML+=t[0]+e.innerHTML+(t[1]?t[1]:"")}else{t.length>1?t[0].includes("<")&&t[1].includes("</")?e.innerHTML=t[0]+e.innerHTML+t[1]:e.innerHTML="<span>"+t[0]+"</span>"+e.innerHTML+"<span>"+t[1]+"</span>":t[0].includes("<")?e.innerHTML=t[0]+e.innerHTML:e.innerHTML="<span>"+t[0]+"</span>"+e.innerHTML}}},{key:"_template",value:function(e,t){var r=e;if(e.search(/$(\w*)@(\w+)/))for(var a=/\$(\w*)@(\w+)/g,i=void 0;i=a.exec(e);)t.hasAttribute(i[2])&&(r=i[1]&&this[i[1]]?r.replace(i[0],this[i[1]].call(this,t.getAttribute(i[2]))):r.replace(i[0],t.getAttribute(i[2])));return r}},{key:"tagName",value:function(e){return"egXML"==e?"teieg-"+e:"tei-"+e}},{key:"decorator",value:function(e){return function(){var t=this;return function(r){var a=[];this!=t&&(r=this);for(var i=0;i<e.length;i++)a.push(t._template(e[i],r));t._insert(r,a)}}}},{key:"getHandler",value:function(e){for(var t=this.behaviors.length-1;t>=0;t--)if(this.behaviors[t].handlers[e])return Array.isArray(this.behaviors[t].handlers[e])?this.decorator(this.behaviors[t].handlers[e]):this.behaviors[t].handlers[e]}},{key:"getFallback",value:function(e){for(var t=this.behaviors.length-1;t>=0;t--){if(this.behaviors[t].fallbacks[e])return Array.isArray(this.behaviors[t].fallbacks[e])?this.decorator(this.behaviors[t].fallbacks[e]).call(this):this.behaviors[t].fallbacks[e];if(this.behaviors[t].handlers[e]&&Array.isArray(this.behaviors[t].handlers[e]))return this.decorator(this.behaviors[t].handlers[e]).call(this);if(this.behaviors[t].handlers[e]&&1==this.behaviors[t].handlers[e].call(this).length)return this.behaviors[t].handlers[e].call(this)}}},{key:"registerAll",value:function(e){var t=!0,r=!1,a=void 0;try{for(var i,n=e[Symbol.iterator]();!(t=(i=n.next()).done);t=!0){var s=i.value,l=Object.create(HTMLElement.prototype),o=this.getHandler(s);o&&(l.createdCallback=o.call(this));var h=this.tagName(s);try{document.registerElement(h,{prototype:l})}catch(c){console.log(h+" couldn't be registered or is already registered."),console.log(c)}}}catch(u){r=!0,a=u}finally{try{!t&&n["return"]&&n["return"]()}finally{if(r)throw a}}}},{key:"fallback",value:function(e){var t=!0,r=!1,a=void 0;try{for(var i,n=e[Symbol.iterator]();!(t=(i=n.next()).done);t=!0){var s=i.value,l=this.getFallback(s);if(l){var o=!0,h=!1,c=void 0;try{for(var u,d=Array.from(this.dom.getElementsByTagName(this.tagName(s)))[Symbol.iterator]();!(o=(u=d.next()).done);o=!0){var f=u.value;l.call(this,f)}}catch(v){h=!0,c=v}finally{try{!o&&d["return"]&&d["return"]()}finally{if(h)throw c}}}}}catch(v){r=!0,a=v}finally{try{!t&&n["return"]&&n["return"]()}finally{if(r)throw a}}}},{key:"rw",value:function(e){return e.match(/^(?:http|mailto|file|\/).*$/)?e:this.base+e}},{key:"first",value:function(e){return e.replace(/ .*$/,"")}},{key:"fromODD",value:function(){}}]),r}();return window&&(window.CETEI=r),r}();