'use strict';dotclear.passwordStrength=(opts)=>{const computeEntropy=(e)=>{const stdCharsets=[{re:/[a-z]/,length:26,},{re:/[A-Z]/,length:26,},{re:/[0-9]/,length:10,},{re:/[^a-zA-Z0-9]/,length:33,},];const calcCharsetLengthWith=(charsets)=>(string)=>charsets.reduce((length,charset)=>length+(charset.re.test(string)?charset.length:0),0);const calcCharsetLength=calcCharsetLengthWith(stdCharsets);const calcEntropy=(charset,length)=>Math.round((length*Math.log(charset))/ Math.LN2);const passwordEntropy=(string)=>(string?calcEntropy(calcCharsetLength(string),string.length):0);return Math.max(Math.min(passwordEntropy(e.value),99),0);};const updateMeter=(e)=>{e.preventDefault();const password=e.currentTarget;let meter=password.nextElementSibling;while(meter){if(meter.matches('.pw-strength-meter'))break;meter=meter.nextElementSibling;}
if(!meter){return;}
const meterValue=computeEntropy(password);let meterContent='';if(meterValue>=meter.getAttribute('high')){meterContent=options.max;}else if(meterValue<=meter.getAttribute('low')){meterContent=options.min;}else{meterContent=options.avg;}
meter.setAttribute('value',meterValue);meter.setAttribute('title',meterContent);meter.innerHTML=meterContent;};const meterTemplate=new DOMParser().parseFromString(`<meter aria-live="polite" class="pw-strength-meter" value="" title="" min="0" max="99" optimum="99" low="30" high="60"></meter>`,'text/html',).body.firstChild;const options=opts||{min:'-',avg:'~',max:'+',};const passwordFields=document.querySelectorAll('input[type=password].pw-strength');for(const passwordField of passwordFields){const meter=meterTemplate.cloneNode(true);const sibling=passwordField.nextElementSibling;if(sibling?.classList.contains('pw-show')||sibling?.classList.contains('pw-hide')){sibling.after(meter);}else{passwordField.after(meter);}
meter.style.width=window.getComputedStyle(passwordField).getPropertyValue('width');passwordField.addEventListener('keyup',updateMeter);}};