import { protonizer as purifyHTML } from '@proton/shared/lib/sanitize' ; import { uniqID } from '../string' ; import { Base64Cache } from '../../hooks/useBase64Cache' ; export const IMG_SRC_BASE_64_PREFIX = 'data-proton-replace-base' ; param input const removeBase64 = ( input , cache ? ) : string => { return input . replace ( /src="data:image\/([a-zA-Z]*);base64,([^\"]*)\"/g , ( match ) => { const hash = uniqID ( ) ; if ( cache ) { cache . set ( hash , match ) ; } return `${ IMG_SRC_BASE_64_PREFIX }="${ hash }"` ; } ) ; } ; return export const attachBase64 = ( element , cache ) => { const nodes = [ ... element . querySelectorAll ( `[${ IMG_SRC_BASE_64_PREFIX }]` ) ] ; nodes . forEach ( ( node ) => { const hash = node . getAttribute ( IMG_SRC_BASE_64_PREFIX ) ; const src = ( cache . get ( hash || '' ) || '' ) . replace ( /^src="/ , '' ) . replace ( /"$/ , '' ) . replace ( /\n/ , '' ) ; if ( src ) { node . setAttribute ( 'src' , src ) ; } node . removeAttribute ( IMG_SRC_BASE_64_PREFIX ) ; } ) ; } ; param message param action export const transformEscape = ( content = '' , cache ? ) => { const value = removeBase64 ( content , cache ) ; const document = purifyHTML ( value , true ) ; return document ; } ;