title-switcher 2.2.2 → 2.2.3

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.
@@ -285,6 +285,7 @@ var titleSwitcher = (() => {
285
285
  });
286
286
  return length;
287
287
  };
288
+ var escapeHtml = (text) => text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
288
289
  var typedPartialHtml = (domObject, remaining) => {
289
290
  let html = "";
290
291
  const children = Array.prototype.slice.call(domObject.childNodes);
@@ -307,7 +308,7 @@ var titleSwitcher = (() => {
307
308
  }
308
309
  --remaining;
309
310
  }
310
- html += taken;
311
+ html += escapeHtml(taken);
311
312
  } else if (child.nodeType === 1) {
312
313
  const result = typedPartialHtml(child, remaining);
313
314
  if (result.html) {
@@ -1 +1 @@
1
- "use strict";var titleSwitcher=(()=>{var t,e,s,i,r,n=Object.getOwnPropertyNames,o=(t={"js-build-tools-inject:process"(){}},function(){if(s)throw s[0];try{return t&&(e=(0,t[n(t)[0]])(t=0)),e}catch(t){throw s=[t],t}});return(i={"dist/main.js"(t){o();var e,s,i,r,n,a,l,c,f,h,u=function(t,e,s,i,r){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?r.call(t,s):r?r.value=s:e.set(t,s),s},p=function(t,e,s,i){if("a"===s&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?i:"a"===s?i.call(t):i?i.value:e.get(t)};Object.defineProperty(t,"__esModule",{value:!0});var d=class{constructor(t="",o="typingEffect"){e.set(this,!1),s.set(this,"displayTitle"),i.set(this,0),r.set(this,200),n.set(this,400),a.set(this,!1),l.set(this,[]),c.set(this,""),f.set(this,void 0),h.set(this,void 0),u(this,s,"displayTitle","f"),u(this,f,"string"==typeof o?"function"==typeof this[o]?this[o]:this.typingEffect:o,"f"),u(this,c,t,"f"),u(this,l,[],"f");const p=t?document.querySelectorAll(t):[];p&&p[0]&&(u(this,c,p[0],"f"),u(this,l,p[0].children,"f"))}get active(){return p(this,e,"f")}get currentClass(){return p(this,s,"f")}get currentIndex(){return p(this,i,"f")}get delayEffect(){return p(this,r,"f")}get delaySwitch(){return p(this,n,"f")}get switchStyle(){return p(this,f,"f")}get titles(){return p(this,l,"f")}get typeSurface(){return p(this,h,"f")}startTitles({delaySwitch:t=400,delayEffect:o=200,isRandom:d=!1,immediatePause:y=!1}={}){const w="typeSurface";if(u(this,n,t,"f"),u(this,r,o,"f"),u(this,a,d,"f"),u(this,e,!y,"f"),p(this,i,"f")>=p(this,l,"f").length||"string"==typeof p(this,c,"f"))return u(this,e,!1,"f"),console.warn(`No titles found for '${p(this,c,"f")}'`),this;p(this,a,"f")&&u(this,i,Math.round(Math.random()*(p(this,l,"f").length-2))+1,"f");const m=p(this,l,"f")[p(this,i,"f")];m.classList?m.classList.add(p(this,s,"f")):m.className+=" "+p(this,s,"f");const g=p(this,l,"f")[0].cloneNode(!0);return g.classList?g.classList.add(w):g.className+=" "+w,g.classList?g.classList.remove(p(this,s,"f")):g.className=g.className.replace(new RegExp("(^|\\b)"+g.className.split(" ").join("|")+"(\\b|$)","gi")," "),p(this,c,"f").insertBefore(g,p(this,c,"f").firstChild),u(this,h,p(this,c,"f").querySelectorAll("."+w)[0],"f"),p(this,h,"f").innerHTML="",p(this,h,"f").style.display="block",Array.prototype.forEach.call(p(this,l,"f"),(function(t){t.style.display="none"})),this.switchTitle(m,p(this,f,"f"),this)}pause(){u(this,e,!1,"f")}resume(){p(this,e,"f")||(u(this,e,!0,"f"),this.switchTitle(p(this,l,"f")[p(this,i,"f")],p(this,f,"f"),this))}switchTitle(t,s,r,n=!1){let o=1;const c=p(r=r||this,l,"f").length;for(let e=1;e<c;++e)if(p(r,l,"f")[e]===t){o=e;break}if(!p(r,e,"f"))return u(r,i,o,"f"),r;const f=p(r,l,"f").length-1;let h=1;if(1===f)return s(t,n?()=>r:r.switchTitle,r,n);if(p(r,a,"f")){r.typeSurface.textContent.trim()||(o=-1);do{h=Math.round(Math.random()*(f-1))+1}while(h===o)}else r.typeSurface.textContent.trim()||(o=f),h=o<f?o+1:1;const d=p(r,l,"f")[h];return t.classList?t.classList.remove(r.currentClass):t.className=t.className.replace(new RegExp("(^|\\b)"+t.className.split(" ").join("|")+"(\\b|$)","gi")," "),d.classList?d.classList.add(r.currentClass):d.className+=" "+r.currentClass,s(d,n?()=>r:r.switchTitle,r,n)}};e=new WeakMap,s=new WeakMap,i=new WeakMap,r=new WeakMap,n=new WeakMap,a=new WeakMap,l=new WeakMap,c=new WeakMap,f=new WeakMap,h=new WeakMap;var y=t=>`<span data-cursor style="display: inline-block; font-weight: normal; color: inherit; text-decoration: none">${t?"&#124;":"&nbsp;"}</span>`;d.prototype.cursorBlink=(t,e)=>{const s=(e=e||void 0).typeSurface.querySelectorAll("[data-cursor]");return Array.prototype.forEach.call(s,(t=>t.parentNode&&t.parentNode.removeChild(t))),(t||s.length)&&e.typeSurface.insertAdjacentHTML("beforeend",y(t)),e};var w=t=>{let e=0;return Array.prototype.forEach.call(t.childNodes,(t=>{if(3===t.nodeType){const s=t.textContent||"";let i=0;for(;i<s.length;){if(/\s/.test(s[i]))for(;i<s.length&&/\s/.test(s[i]);)++i;else++i;++e}}else 1===t.nodeType&&(e+=w(t))})),e},m=(t,e)=>{let s="";const i=Array.prototype.slice.call(t.childNodes);for(let t=0;t<i.length&&e>0;++t){const r=i[t];if(3===r.nodeType){const t=r.textContent||"";let i=0,n="";for(;i<t.length&&e>0;){if(/\s/.test(t[i])){const e=i;for(;i<t.length&&/\s/.test(t[i]);)++i;n+=t.slice(e,i)}else n+=t[i],++i;--e}s+=n}else if(1===r.nodeType){const t=m(r,e);if(t.html){const e=r.cloneNode(!1);e.innerHTML=t.html,s+=e.outerHTML}e=t.remaining}}return{html:s,remaining:e}};d.prototype.typingEffect=(t,e,s,i=!1)=>{const r=(s=s||void 0).titles.length;let n=0;for(let e=1;e<r;++e)if(s.titles[e]===t){n=e;break}t=t||s.titles[n+1];let o=!0;s.typeSurface.hasAttribute("style")&&s.typeSurface.removeAttribute("style"),t.hasAttribute("style")&&s.typeSurface.setAttribute("style",t.getAttribute("style")),s.typeSurface.innerHTML="",s.typeSurface.style.display="block";for(let t=0;t<4;++t)setTimeout((()=>{s.cursorBlink(o,s),o=!o}),t*s.delaySwitch);return setTimeout((()=>{s.typeSurface.innerHTML="",s.cursorBlink(!0,s);const r=w(t);for(let n=0;n<r;++n)setTimeout((()=>{if(s.typeSurface.innerHTML=m(t,n+1).html+y(!0),t.textContent+"|"===s.typeSurface.textContent){s.typeSurface.innerHTML=t.innerHTML+y(!0);for(let r=0;r<8;++r)setTimeout((()=>{--r,s.cursorBlink(o,s),0===r&&e(t,i?()=>s:s.switchStyle,s,i)}),r*s.delaySwitch)}}),n*s.delayEffect)}),4*s.delaySwitch),s},t.default=d,"undefined"!=typeof window&&(window.TitleSwitcher=d)}},function(){try{return r||(0,i[n(i)[0]])((r={exports:{}}).exports,r),r.exports}catch(t){throw r=0,t}})()})();"object"==typeof module&&module&&module.exports&&(module.exports=titleSwitcher);
1
+ "use strict";var titleSwitcher=(()=>{var t,e,s,r,i,n=Object.getOwnPropertyNames,a=(t={"js-build-tools-inject:process"(){}},function(){if(s)throw s[0];try{return t&&(e=(0,t[n(t)[0]])(t=0)),e}catch(t){throw s=[t],t}});return(r={"dist/main.js"(t){a();var e,s,r,i,n,o,l,c,f,h,u=function(t,e,s,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?i.call(t,s):i?i.value=s:e.set(t,s),s},p=function(t,e,s,r){if("a"===s&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?r:"a"===s?r.call(t):r?r.value:e.get(t)};Object.defineProperty(t,"__esModule",{value:!0});var d=class{constructor(t="",a="typingEffect"){e.set(this,!1),s.set(this,"displayTitle"),r.set(this,0),i.set(this,200),n.set(this,400),o.set(this,!1),l.set(this,[]),c.set(this,""),f.set(this,void 0),h.set(this,void 0),u(this,s,"displayTitle","f"),u(this,f,"string"==typeof a?"function"==typeof this[a]?this[a]:this.typingEffect:a,"f"),u(this,c,t,"f"),u(this,l,[],"f");const p=t?document.querySelectorAll(t):[];p&&p[0]&&(u(this,c,p[0],"f"),u(this,l,p[0].children,"f"))}get active(){return p(this,e,"f")}get currentClass(){return p(this,s,"f")}get currentIndex(){return p(this,r,"f")}get delayEffect(){return p(this,i,"f")}get delaySwitch(){return p(this,n,"f")}get switchStyle(){return p(this,f,"f")}get titles(){return p(this,l,"f")}get typeSurface(){return p(this,h,"f")}startTitles({delaySwitch:t=400,delayEffect:a=200,isRandom:d=!1,immediatePause:y=!1}={}){const w="typeSurface";if(u(this,n,t,"f"),u(this,i,a,"f"),u(this,o,d,"f"),u(this,e,!y,"f"),p(this,r,"f")>=p(this,l,"f").length||"string"==typeof p(this,c,"f"))return u(this,e,!1,"f"),console.warn(`No titles found for '${p(this,c,"f")}'`),this;p(this,o,"f")&&u(this,r,Math.round(Math.random()*(p(this,l,"f").length-2))+1,"f");const m=p(this,l,"f")[p(this,r,"f")];m.classList?m.classList.add(p(this,s,"f")):m.className+=" "+p(this,s,"f");const g=p(this,l,"f")[0].cloneNode(!0);return g.classList?g.classList.add(w):g.className+=" "+w,g.classList?g.classList.remove(p(this,s,"f")):g.className=g.className.replace(new RegExp("(^|\\b)"+g.className.split(" ").join("|")+"(\\b|$)","gi")," "),p(this,c,"f").insertBefore(g,p(this,c,"f").firstChild),u(this,h,p(this,c,"f").querySelectorAll("."+w)[0],"f"),p(this,h,"f").innerHTML="",p(this,h,"f").style.display="block",Array.prototype.forEach.call(p(this,l,"f"),(function(t){t.style.display="none"})),this.switchTitle(m,p(this,f,"f"),this)}pause(){u(this,e,!1,"f")}resume(){p(this,e,"f")||(u(this,e,!0,"f"),this.switchTitle(p(this,l,"f")[p(this,r,"f")],p(this,f,"f"),this))}switchTitle(t,s,i,n=!1){let a=1;const c=p(i=i||this,l,"f").length;for(let e=1;e<c;++e)if(p(i,l,"f")[e]===t){a=e;break}if(!p(i,e,"f"))return u(i,r,a,"f"),i;const f=p(i,l,"f").length-1;let h=1;if(1===f)return s(t,n?()=>i:i.switchTitle,i,n);if(p(i,o,"f")){i.typeSurface.textContent.trim()||(a=-1);do{h=Math.round(Math.random()*(f-1))+1}while(h===a)}else i.typeSurface.textContent.trim()||(a=f),h=a<f?a+1:1;const d=p(i,l,"f")[h];return t.classList?t.classList.remove(i.currentClass):t.className=t.className.replace(new RegExp("(^|\\b)"+t.className.split(" ").join("|")+"(\\b|$)","gi")," "),d.classList?d.classList.add(i.currentClass):d.className+=" "+i.currentClass,s(d,n?()=>i:i.switchTitle,i,n)}};e=new WeakMap,s=new WeakMap,r=new WeakMap,i=new WeakMap,n=new WeakMap,o=new WeakMap,l=new WeakMap,c=new WeakMap,f=new WeakMap,h=new WeakMap;var y=t=>`<span data-cursor style="display: inline-block; font-weight: normal; color: inherit; text-decoration: none">${t?"&#124;":"&nbsp;"}</span>`;d.prototype.cursorBlink=(t,e)=>{const s=(e=e||void 0).typeSurface.querySelectorAll("[data-cursor]");return Array.prototype.forEach.call(s,(t=>t.parentNode&&t.parentNode.removeChild(t))),(t||s.length)&&e.typeSurface.insertAdjacentHTML("beforeend",y(t)),e};var w=t=>{let e=0;return Array.prototype.forEach.call(t.childNodes,(t=>{if(3===t.nodeType){const s=t.textContent||"";let r=0;for(;r<s.length;){if(/\s/.test(s[r]))for(;r<s.length&&/\s/.test(s[r]);)++r;else++r;++e}}else 1===t.nodeType&&(e+=w(t))})),e},m=t=>t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"),g=(t,e)=>{let s="";const r=Array.prototype.slice.call(t.childNodes);for(let t=0;t<r.length&&e>0;++t){const i=r[t];if(3===i.nodeType){const t=i.textContent||"";let r=0,n="";for(;r<t.length&&e>0;){if(/\s/.test(t[r])){const e=r;for(;r<t.length&&/\s/.test(t[r]);)++r;n+=t.slice(e,r)}else n+=t[r],++r;--e}s+=m(n)}else if(1===i.nodeType){const t=g(i,e);if(t.html){const e=i.cloneNode(!1);e.innerHTML=t.html,s+=e.outerHTML}e=t.remaining}}return{html:s,remaining:e}};d.prototype.typingEffect=(t,e,s,r=!1)=>{const i=(s=s||void 0).titles.length;let n=0;for(let e=1;e<i;++e)if(s.titles[e]===t){n=e;break}t=t||s.titles[n+1];let a=!0;s.typeSurface.hasAttribute("style")&&s.typeSurface.removeAttribute("style"),t.hasAttribute("style")&&s.typeSurface.setAttribute("style",t.getAttribute("style")),s.typeSurface.innerHTML="",s.typeSurface.style.display="block";for(let t=0;t<4;++t)setTimeout((()=>{s.cursorBlink(a,s),a=!a}),t*s.delaySwitch);return setTimeout((()=>{s.typeSurface.innerHTML="",s.cursorBlink(!0,s);const i=w(t);for(let n=0;n<i;++n)setTimeout((()=>{if(s.typeSurface.innerHTML=g(t,n+1).html+y(!0),t.textContent+"|"===s.typeSurface.textContent){s.typeSurface.innerHTML=t.innerHTML+y(!0);for(let i=0;i<8;++i)setTimeout((()=>{--i,s.cursorBlink(a,s),0===i&&e(t,r?()=>s:s.switchStyle,s,r)}),i*s.delaySwitch)}}),n*s.delayEffect)}),4*s.delaySwitch),s},t.default=d,"undefined"!=typeof window&&(window.TitleSwitcher=d)}},function(){try{return i||(0,r[n(r)[0]])((i={exports:{}}).exports,i),i.exports}catch(t){throw i=0,t}})()})();"object"==typeof module&&module&&module.exports&&(module.exports=titleSwitcher);
package/dist/main.js CHANGED
@@ -286,6 +286,13 @@ const typedLength = domObject => {
286
286
  * @param domObject
287
287
  * @param remaining
288
288
  */
289
+ /**
290
+ * Escape text for use inside HTML, so a title's text is shown as text. A text node's characters come back from
291
+ * textContent unescaped (`<`, `&`), so they must be escaped again before they're joined into markup for innerHTML;
292
+ * otherwise text like `<img src=x onerror=...>` became a real element (and ran), and `a < b` displayed wrongly.
293
+ * @param text - Plain text from a text node.
294
+ */
295
+ const escapeHtml = text => text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
289
296
  const typedPartialHtml = (domObject, remaining) => {
290
297
  let html = '';
291
298
  const children = Array.prototype.slice.call(domObject.childNodes);
@@ -308,7 +315,7 @@ const typedPartialHtml = (domObject, remaining) => {
308
315
  }
309
316
  --remaining;
310
317
  }
311
- html += taken;
318
+ html += escapeHtml(taken);
312
319
  } else if (child.nodeType === 1) {
313
320
  const result = typedPartialHtml(child, remaining);
314
321
  if (result.html) {
package/dist/main.min.js CHANGED
@@ -1 +1 @@
1
- "use strict";var _TitleSwitcher_active,_TitleSwitcher_currentClass,_TitleSwitcher_currentIndex,_TitleSwitcher_delayEffect,_TitleSwitcher_delaySwitch,_TitleSwitcher_isRandom,_TitleSwitcher_titles,_TitleSwitcher_titlesContainer,_TitleSwitcher_switchStyle,_TitleSwitcher_typeSurface,__classPrivateFieldSet=function(e,t,i,s,l){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!l)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!l:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?l.call(e,i):l?l.value=i:t.set(e,i),i},__classPrivateFieldGet=function(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)};Object.defineProperty(exports,"__esModule",{value:!0});class TitleSwitcher{constructor(e="",t="typingEffect"){_TitleSwitcher_active.set(this,!1),_TitleSwitcher_currentClass.set(this,"displayTitle"),_TitleSwitcher_currentIndex.set(this,0),_TitleSwitcher_delayEffect.set(this,200),_TitleSwitcher_delaySwitch.set(this,400),_TitleSwitcher_isRandom.set(this,!1),_TitleSwitcher_titles.set(this,[]),_TitleSwitcher_titlesContainer.set(this,""),_TitleSwitcher_switchStyle.set(this,void 0),_TitleSwitcher_typeSurface.set(this,void 0),__classPrivateFieldSet(this,_TitleSwitcher_currentClass,"displayTitle","f"),__classPrivateFieldSet(this,_TitleSwitcher_switchStyle,"string"==typeof t?"function"==typeof this[t]?this[t]:this.typingEffect:t,"f"),__classPrivateFieldSet(this,_TitleSwitcher_titlesContainer,e,"f"),__classPrivateFieldSet(this,_TitleSwitcher_titles,[],"f");const i=e?document.querySelectorAll(e):[];i&&i[0]&&(__classPrivateFieldSet(this,_TitleSwitcher_titlesContainer,i[0],"f"),__classPrivateFieldSet(this,_TitleSwitcher_titles,i[0].children,"f"))}get active(){return __classPrivateFieldGet(this,_TitleSwitcher_active,"f")}get currentClass(){return __classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f")}get currentIndex(){return __classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")}get delayEffect(){return __classPrivateFieldGet(this,_TitleSwitcher_delayEffect,"f")}get delaySwitch(){return __classPrivateFieldGet(this,_TitleSwitcher_delaySwitch,"f")}get switchStyle(){return __classPrivateFieldGet(this,_TitleSwitcher_switchStyle,"f")}get titles(){return __classPrivateFieldGet(this,_TitleSwitcher_titles,"f")}get typeSurface(){return __classPrivateFieldGet(this,_TitleSwitcher_typeSurface,"f")}startTitles({delaySwitch:e=400,delayEffect:t=200,isRandom:i=!1,immediatePause:s=!1}={}){const l="typeSurface";if(__classPrivateFieldSet(this,_TitleSwitcher_delaySwitch,e,"f"),__classPrivateFieldSet(this,_TitleSwitcher_delayEffect,t,"f"),__classPrivateFieldSet(this,_TitleSwitcher_isRandom,i,"f"),__classPrivateFieldSet(this,_TitleSwitcher_active,!s,"f"),__classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")>=__classPrivateFieldGet(this,_TitleSwitcher_titles,"f").length||"string"==typeof __classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f"))return __classPrivateFieldSet(this,_TitleSwitcher_active,!1,"f"),console.warn(`No titles found for '${__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f")}'`),this;__classPrivateFieldGet(this,_TitleSwitcher_isRandom,"f")&&__classPrivateFieldSet(this,_TitleSwitcher_currentIndex,Math.round(Math.random()*(__classPrivateFieldGet(this,_TitleSwitcher_titles,"f").length-2))+1,"f");const r=__classPrivateFieldGet(this,_TitleSwitcher_titles,"f")[__classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")];r.classList?r.classList.add(__classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f")):r.className+=" "+__classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f");const c=__classPrivateFieldGet(this,_TitleSwitcher_titles,"f")[0].cloneNode(!0);return c.classList?c.classList.add(l):c.className+=" "+l,c.classList?c.classList.remove(__classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f")):c.className=c.className.replace(new RegExp("(^|\\b)"+c.className.split(" ").join("|")+"(\\b|$)","gi")," "),__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f").insertBefore(c,__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f").firstChild),__classPrivateFieldSet(this,_TitleSwitcher_typeSurface,__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f").querySelectorAll("."+l)[0],"f"),__classPrivateFieldGet(this,_TitleSwitcher_typeSurface,"f").innerHTML="",__classPrivateFieldGet(this,_TitleSwitcher_typeSurface,"f").style.display="block",Array.prototype.forEach.call(__classPrivateFieldGet(this,_TitleSwitcher_titles,"f"),(function(e){e.style.display="none"})),this.switchTitle(r,__classPrivateFieldGet(this,_TitleSwitcher_switchStyle,"f"),this)}pause(){__classPrivateFieldSet(this,_TitleSwitcher_active,!1,"f")}resume(){__classPrivateFieldGet(this,_TitleSwitcher_active,"f")||(__classPrivateFieldSet(this,_TitleSwitcher_active,!0,"f"),this.switchTitle(__classPrivateFieldGet(this,_TitleSwitcher_titles,"f")[__classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")],__classPrivateFieldGet(this,_TitleSwitcher_switchStyle,"f"),this))}switchTitle(e,t,i,s=!1){let l=1;const r=__classPrivateFieldGet(i=i||this,_TitleSwitcher_titles,"f").length;for(let t=1;t<r;++t)if(__classPrivateFieldGet(i,_TitleSwitcher_titles,"f")[t]===e){l=t;break}if(!__classPrivateFieldGet(i,_TitleSwitcher_active,"f"))return __classPrivateFieldSet(i,_TitleSwitcher_currentIndex,l,"f"),i;const c=__classPrivateFieldGet(i,_TitleSwitcher_titles,"f").length-1;let a=1;if(1===c)return t(e,s?()=>i:i.switchTitle,i,s);if(__classPrivateFieldGet(i,_TitleSwitcher_isRandom,"f")){i.typeSurface.textContent.trim()||(l=-1);do{a=Math.round(Math.random()*(c-1))+1}while(a===l)}else i.typeSurface.textContent.trim()||(l=c),a=l<c?l+1:1;const _=__classPrivateFieldGet(i,_TitleSwitcher_titles,"f")[a];return e.classList?e.classList.remove(i.currentClass):e.className=e.className.replace(new RegExp("(^|\\b)"+e.className.split(" ").join("|")+"(\\b|$)","gi")," "),_.classList?_.classList.add(i.currentClass):_.className+=" "+i.currentClass,t(_,s?()=>i:i.switchTitle,i,s)}}_TitleSwitcher_active=new WeakMap,_TitleSwitcher_currentClass=new WeakMap,_TitleSwitcher_currentIndex=new WeakMap,_TitleSwitcher_delayEffect=new WeakMap,_TitleSwitcher_delaySwitch=new WeakMap,_TitleSwitcher_isRandom=new WeakMap,_TitleSwitcher_titles=new WeakMap,_TitleSwitcher_titlesContainer=new WeakMap,_TitleSwitcher_switchStyle=new WeakMap,_TitleSwitcher_typeSurface=new WeakMap;const cursorHtml=e=>`<span data-cursor style="display: inline-block; font-weight: normal; color: inherit; text-decoration: none">${e?"&#124;":"&nbsp;"}</span>`;TitleSwitcher.prototype.cursorBlink=(e,t)=>{const i=(t=t||void 0).typeSurface.querySelectorAll("[data-cursor]");return Array.prototype.forEach.call(i,(e=>e.parentNode&&e.parentNode.removeChild(e))),(e||i.length)&&t.typeSurface.insertAdjacentHTML("beforeend",cursorHtml(e)),t};const typedLength=e=>{let t=0;return Array.prototype.forEach.call(e.childNodes,(e=>{if(3===e.nodeType){const i=e.textContent||"";let s=0;for(;s<i.length;){if(/\s/.test(i[s]))for(;s<i.length&&/\s/.test(i[s]);)++s;else++s;++t}}else 1===e.nodeType&&(t+=typedLength(e))})),t},typedPartialHtml=(e,t)=>{let i="";const s=Array.prototype.slice.call(e.childNodes);for(let e=0;e<s.length&&t>0;++e){const l=s[e];if(3===l.nodeType){const e=l.textContent||"";let s=0,r="";for(;s<e.length&&t>0;){if(/\s/.test(e[s])){const t=s;for(;s<e.length&&/\s/.test(e[s]);)++s;r+=e.slice(t,s)}else r+=e[s],++s;--t}i+=r}else if(1===l.nodeType){const e=typedPartialHtml(l,t);if(e.html){const t=l.cloneNode(!1);t.innerHTML=e.html,i+=t.outerHTML}t=e.remaining}}return{html:i,remaining:t}};TitleSwitcher.prototype.typingEffect=(e,t,i,s=!1)=>{const l=(i=i||void 0).titles.length;let r=0;for(let t=1;t<l;++t)if(i.titles[t]===e){r=t;break}e=e||i.titles[r+1];let c=!0;i.typeSurface.hasAttribute("style")&&i.typeSurface.removeAttribute("style"),e.hasAttribute("style")&&i.typeSurface.setAttribute("style",e.getAttribute("style")),i.typeSurface.innerHTML="",i.typeSurface.style.display="block";for(let e=0;e<4;++e)setTimeout((()=>{i.cursorBlink(c,i),c=!c}),e*i.delaySwitch);return setTimeout((()=>{i.typeSurface.innerHTML="",i.cursorBlink(!0,i);const l=typedLength(e);for(let r=0;r<l;++r)setTimeout((()=>{if(i.typeSurface.innerHTML=typedPartialHtml(e,r+1).html+cursorHtml(!0),e.textContent+"|"===i.typeSurface.textContent){i.typeSurface.innerHTML=e.innerHTML+cursorHtml(!0);for(let l=0;l<8;++l)setTimeout((()=>{--l,i.cursorBlink(c,i),0===l&&t(e,s?()=>i:i.switchStyle,i,s)}),l*i.delaySwitch)}}),r*i.delayEffect)}),4*i.delaySwitch),i},exports.default=TitleSwitcher,"undefined"!=typeof window&&(window.TitleSwitcher=TitleSwitcher);
1
+ "use strict";var _TitleSwitcher_active,_TitleSwitcher_currentClass,_TitleSwitcher_currentIndex,_TitleSwitcher_delayEffect,_TitleSwitcher_delaySwitch,_TitleSwitcher_isRandom,_TitleSwitcher_titles,_TitleSwitcher_titlesContainer,_TitleSwitcher_switchStyle,_TitleSwitcher_typeSurface,__classPrivateFieldSet=function(e,t,i,s,l){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!l)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!l:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?l.call(e,i):l?l.value=i:t.set(e,i),i},__classPrivateFieldGet=function(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)};Object.defineProperty(exports,"__esModule",{value:!0});class TitleSwitcher{constructor(e="",t="typingEffect"){_TitleSwitcher_active.set(this,!1),_TitleSwitcher_currentClass.set(this,"displayTitle"),_TitleSwitcher_currentIndex.set(this,0),_TitleSwitcher_delayEffect.set(this,200),_TitleSwitcher_delaySwitch.set(this,400),_TitleSwitcher_isRandom.set(this,!1),_TitleSwitcher_titles.set(this,[]),_TitleSwitcher_titlesContainer.set(this,""),_TitleSwitcher_switchStyle.set(this,void 0),_TitleSwitcher_typeSurface.set(this,void 0),__classPrivateFieldSet(this,_TitleSwitcher_currentClass,"displayTitle","f"),__classPrivateFieldSet(this,_TitleSwitcher_switchStyle,"string"==typeof t?"function"==typeof this[t]?this[t]:this.typingEffect:t,"f"),__classPrivateFieldSet(this,_TitleSwitcher_titlesContainer,e,"f"),__classPrivateFieldSet(this,_TitleSwitcher_titles,[],"f");const i=e?document.querySelectorAll(e):[];i&&i[0]&&(__classPrivateFieldSet(this,_TitleSwitcher_titlesContainer,i[0],"f"),__classPrivateFieldSet(this,_TitleSwitcher_titles,i[0].children,"f"))}get active(){return __classPrivateFieldGet(this,_TitleSwitcher_active,"f")}get currentClass(){return __classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f")}get currentIndex(){return __classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")}get delayEffect(){return __classPrivateFieldGet(this,_TitleSwitcher_delayEffect,"f")}get delaySwitch(){return __classPrivateFieldGet(this,_TitleSwitcher_delaySwitch,"f")}get switchStyle(){return __classPrivateFieldGet(this,_TitleSwitcher_switchStyle,"f")}get titles(){return __classPrivateFieldGet(this,_TitleSwitcher_titles,"f")}get typeSurface(){return __classPrivateFieldGet(this,_TitleSwitcher_typeSurface,"f")}startTitles({delaySwitch:e=400,delayEffect:t=200,isRandom:i=!1,immediatePause:s=!1}={}){const l="typeSurface";if(__classPrivateFieldSet(this,_TitleSwitcher_delaySwitch,e,"f"),__classPrivateFieldSet(this,_TitleSwitcher_delayEffect,t,"f"),__classPrivateFieldSet(this,_TitleSwitcher_isRandom,i,"f"),__classPrivateFieldSet(this,_TitleSwitcher_active,!s,"f"),__classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")>=__classPrivateFieldGet(this,_TitleSwitcher_titles,"f").length||"string"==typeof __classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f"))return __classPrivateFieldSet(this,_TitleSwitcher_active,!1,"f"),console.warn(`No titles found for '${__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f")}'`),this;__classPrivateFieldGet(this,_TitleSwitcher_isRandom,"f")&&__classPrivateFieldSet(this,_TitleSwitcher_currentIndex,Math.round(Math.random()*(__classPrivateFieldGet(this,_TitleSwitcher_titles,"f").length-2))+1,"f");const r=__classPrivateFieldGet(this,_TitleSwitcher_titles,"f")[__classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")];r.classList?r.classList.add(__classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f")):r.className+=" "+__classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f");const c=__classPrivateFieldGet(this,_TitleSwitcher_titles,"f")[0].cloneNode(!0);return c.classList?c.classList.add(l):c.className+=" "+l,c.classList?c.classList.remove(__classPrivateFieldGet(this,_TitleSwitcher_currentClass,"f")):c.className=c.className.replace(new RegExp("(^|\\b)"+c.className.split(" ").join("|")+"(\\b|$)","gi")," "),__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f").insertBefore(c,__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f").firstChild),__classPrivateFieldSet(this,_TitleSwitcher_typeSurface,__classPrivateFieldGet(this,_TitleSwitcher_titlesContainer,"f").querySelectorAll("."+l)[0],"f"),__classPrivateFieldGet(this,_TitleSwitcher_typeSurface,"f").innerHTML="",__classPrivateFieldGet(this,_TitleSwitcher_typeSurface,"f").style.display="block",Array.prototype.forEach.call(__classPrivateFieldGet(this,_TitleSwitcher_titles,"f"),(function(e){e.style.display="none"})),this.switchTitle(r,__classPrivateFieldGet(this,_TitleSwitcher_switchStyle,"f"),this)}pause(){__classPrivateFieldSet(this,_TitleSwitcher_active,!1,"f")}resume(){__classPrivateFieldGet(this,_TitleSwitcher_active,"f")||(__classPrivateFieldSet(this,_TitleSwitcher_active,!0,"f"),this.switchTitle(__classPrivateFieldGet(this,_TitleSwitcher_titles,"f")[__classPrivateFieldGet(this,_TitleSwitcher_currentIndex,"f")],__classPrivateFieldGet(this,_TitleSwitcher_switchStyle,"f"),this))}switchTitle(e,t,i,s=!1){let l=1;const r=__classPrivateFieldGet(i=i||this,_TitleSwitcher_titles,"f").length;for(let t=1;t<r;++t)if(__classPrivateFieldGet(i,_TitleSwitcher_titles,"f")[t]===e){l=t;break}if(!__classPrivateFieldGet(i,_TitleSwitcher_active,"f"))return __classPrivateFieldSet(i,_TitleSwitcher_currentIndex,l,"f"),i;const c=__classPrivateFieldGet(i,_TitleSwitcher_titles,"f").length-1;let a=1;if(1===c)return t(e,s?()=>i:i.switchTitle,i,s);if(__classPrivateFieldGet(i,_TitleSwitcher_isRandom,"f")){i.typeSurface.textContent.trim()||(l=-1);do{a=Math.round(Math.random()*(c-1))+1}while(a===l)}else i.typeSurface.textContent.trim()||(l=c),a=l<c?l+1:1;const _=__classPrivateFieldGet(i,_TitleSwitcher_titles,"f")[a];return e.classList?e.classList.remove(i.currentClass):e.className=e.className.replace(new RegExp("(^|\\b)"+e.className.split(" ").join("|")+"(\\b|$)","gi")," "),_.classList?_.classList.add(i.currentClass):_.className+=" "+i.currentClass,t(_,s?()=>i:i.switchTitle,i,s)}}_TitleSwitcher_active=new WeakMap,_TitleSwitcher_currentClass=new WeakMap,_TitleSwitcher_currentIndex=new WeakMap,_TitleSwitcher_delayEffect=new WeakMap,_TitleSwitcher_delaySwitch=new WeakMap,_TitleSwitcher_isRandom=new WeakMap,_TitleSwitcher_titles=new WeakMap,_TitleSwitcher_titlesContainer=new WeakMap,_TitleSwitcher_switchStyle=new WeakMap,_TitleSwitcher_typeSurface=new WeakMap;const cursorHtml=e=>`<span data-cursor style="display: inline-block; font-weight: normal; color: inherit; text-decoration: none">${e?"&#124;":"&nbsp;"}</span>`;TitleSwitcher.prototype.cursorBlink=(e,t)=>{const i=(t=t||void 0).typeSurface.querySelectorAll("[data-cursor]");return Array.prototype.forEach.call(i,(e=>e.parentNode&&e.parentNode.removeChild(e))),(e||i.length)&&t.typeSurface.insertAdjacentHTML("beforeend",cursorHtml(e)),t};const typedLength=e=>{let t=0;return Array.prototype.forEach.call(e.childNodes,(e=>{if(3===e.nodeType){const i=e.textContent||"";let s=0;for(;s<i.length;){if(/\s/.test(i[s]))for(;s<i.length&&/\s/.test(i[s]);)++s;else++s;++t}}else 1===e.nodeType&&(t+=typedLength(e))})),t},escapeHtml=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"),typedPartialHtml=(e,t)=>{let i="";const s=Array.prototype.slice.call(e.childNodes);for(let e=0;e<s.length&&t>0;++e){const l=s[e];if(3===l.nodeType){const e=l.textContent||"";let s=0,r="";for(;s<e.length&&t>0;){if(/\s/.test(e[s])){const t=s;for(;s<e.length&&/\s/.test(e[s]);)++s;r+=e.slice(t,s)}else r+=e[s],++s;--t}i+=escapeHtml(r)}else if(1===l.nodeType){const e=typedPartialHtml(l,t);if(e.html){const t=l.cloneNode(!1);t.innerHTML=e.html,i+=t.outerHTML}t=e.remaining}}return{html:i,remaining:t}};TitleSwitcher.prototype.typingEffect=(e,t,i,s=!1)=>{const l=(i=i||void 0).titles.length;let r=0;for(let t=1;t<l;++t)if(i.titles[t]===e){r=t;break}e=e||i.titles[r+1];let c=!0;i.typeSurface.hasAttribute("style")&&i.typeSurface.removeAttribute("style"),e.hasAttribute("style")&&i.typeSurface.setAttribute("style",e.getAttribute("style")),i.typeSurface.innerHTML="",i.typeSurface.style.display="block";for(let e=0;e<4;++e)setTimeout((()=>{i.cursorBlink(c,i),c=!c}),e*i.delaySwitch);return setTimeout((()=>{i.typeSurface.innerHTML="",i.cursorBlink(!0,i);const l=typedLength(e);for(let r=0;r<l;++r)setTimeout((()=>{if(i.typeSurface.innerHTML=typedPartialHtml(e,r+1).html+cursorHtml(!0),e.textContent+"|"===i.typeSurface.textContent){i.typeSurface.innerHTML=e.innerHTML+cursorHtml(!0);for(let l=0;l<8;++l)setTimeout((()=>{--l,i.cursorBlink(c,i),0===l&&t(e,s?()=>i:i.switchStyle,i,s)}),l*i.delaySwitch)}}),r*i.delayEffect)}),4*i.delaySwitch),i},exports.default=TitleSwitcher,"undefined"!=typeof window&&(window.TitleSwitcher=TitleSwitcher);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "title-switcher",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "Add typing effect to titles and also switch through several titles.",
5
5
  "main": "dist/main.min.js",
6
6
  "types": "dist/main.d.ts",