import { IMG_SRC_BASE_64_PREFIX } from './transformEscape' ; const ELEMENTS = [ { selector : 'a:not([href^="http"]):not([href^="mailto:"])' , attribute : 'href' , } , { selector : `img:not([src^="http"]):not([proton-src^="cid"]):not([${ IMG_SRC_BASE_64_PREFIX }])` , attribute : 'src' , } , ] ; const getBaseURL = ( base ) => { const baseUrl = base . getAttribute ( 'href' ) || '' ; if ( baseUrl . substr ( - 1 , 1 ) !== '/' ) { return `${ baseUrl }/` ; } return baseUrl ; } ; param html return export const transformBase = ( document ) => { const base = document . querySelector ( 'base' ) ; if ( ! base || ! base . getAttribute ( 'href' ) ) { return { document } ; } const BASE_URL = getBaseURL ( base ) ; const bindAttribute = ( node , key , value = '' ) => { if ( ! value . startsWith ( 'http' ) ) { node . setAttribute ( key , BASE_URL + value ) ; } } ; ELEMENTS . forEach ( ( { selector , attribute } ) => { [ ... document . querySelectorAll ( selector ) ] . forEach ( ( el ) => { const keyproton = `proton-${ attribute }` ; const value = el . getAttribute ( attribute ) || '' ; const ptValue = el . getAttribute ( keyproton ) || '' ; const url = value . charAt ( 0 ) === '/' ? value . slice ( 1 ) : value ; if ( ptValue || ( attribute === 'src' && ! value ) ) { bindAttribute ( el , keyproton , ptValue ) ; } else { bindAttribute ( el , attribute , url ) ; } } ) ; } ) ; document . querySelectorAll ( 'base' ) . forEach ( ( base ) => { base . parentElement ?. removeChild ( base ) ; } ) ; } ;