All files / src/custom-element module-url.js

92.85% Statements 13/14
50% Branches 4/8
100% Functions 3/3
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 352x           1x         2x   2x 2x         2x 2x 2x   2x       4x 2x       1x    
const attr = ( el, attr )=> el.getAttribute( attr );
 
 
export class ModuleUrl extends HTMLElement
{
    static observedAttributes=
            [   'slice'
            ,   'src' // module path, relative or absolute URL
            ];
 
    sliceInit()
    {   let path = attr(this,'src');
 
        try
        {   const url =  '.' === path.charAt(0)
                ? new URL(path, this.closest('[base]')?.getAttribute('base') || location.href).href
                : import.meta.resolve(path);
            this.setAttribute('value',this.value = url );
        }catch( er )
        {   this.setAttribute('error', er.message);
            this.setAttribute('value', path);
            console.error(er.message ?? er, path);
        }
        this.dispatchEvent( new Event('change') );
    }
    attributeChangedCallback( name, oldValue, newValue )
    {
        if( 'src'=== name )
            this.sliceInit();
    }
}
 
window.customElements.define( 'module-url', ModuleUrl );
export default ModuleUrl;