linkify-element
Version:
Browser DOM element interface for linkifyjs
2 lines (1 loc) • 1.46 kB
JavaScript
var linkifyElement=function(e){"use strict";function n(e,n,t){let r=t[t.length-1];e.replaceChild(r,n);for(let n=t.length-2;n>=0;n--)e.insertBefore(t[n],r),r=t[n]}function t(e,n,t){const r=[];for(let i=0;i<e.length;i++){const o=e[i];"nl"===o.t&&n.get("nl2br")?r.push(t.createElement("br")):o.isLink&&n.check(o)?r.push(n.render(o)):r.push(t.createTextNode(o.toString()))}return r}function r(i,o,l){if(!i||1!==i.nodeType)throw new Error(`Cannot linkify ${i} - Invalid DOM Node type`);if("A"===i.tagName||o.ignoreTags.indexOf(i.tagName)>=0)return i;let a=i.firstChild;for(;a;){let c,s,u;switch(a.nodeType){case 1:r(a,o,l);break;case 3:if(c=a.nodeValue,s=e.tokenize(c),0===s.length||1===s.length&&"text"===s[0].t)break;u=t(s,o,l),n(i,a,u),a=u[u.length-1]}a=a.nextSibling}return i}function i(e){return({tagName:n,attributes:t,content:r,eventListeners:i})=>{const o=e.createElement(n);for(const e in t)o.setAttribute(e,t[e]);if(i&&o.addEventListener)for(const e in i)o.addEventListener(e,i[e]);return o.appendChild(e.createTextNode(r)),o}}function o(n,t=null,o=null){try{o=o||document||window&&window.document||global&&global.document}catch(e){}if(!o)throw new Error("Cannot find document implementation. If you are in a non-browser environment like Node.js, pass the document implementation as the third argument to linkifyElement.");return r(n,new e.Options(t,i(o)),o)}return o.helper=r,o.getDefaultRender=i,o.normalize=(n,t)=>new e.Options(n,i(t)),o}(linkify);