export const BLOCKQUOTE_SELECTORS = [ '.protonmail_quote' , '.gmail_quote' , '.yahoo_quoted' , '.gmail_extra' , '.zmail_extra' , '.moz-cite-prefix' , '#isForwardContent' , '#isReplyContent' , '#mailcontent:not(table)' , '#origbody' , '#reply139content' , '#oriMsgHtmlSeperator' , 'blockquote[type="cite"]' , '[name="quote"]' , ] ; const BLOCKQUOTE_TEXT_SELECTORS = [ '-----Original Message-----' ] ; const BLOCKQUOTE_SELECTOR = BLOCKQUOTE_SELECTORS . map ( ( selector ) => `${ selector }:not(:empty)` ) . join ( ',' ) ; /**
 * Returns content before and after match in the source
 * Beware, String.prototype.split does almost the same but will not if there is several match
 */ const split = ( source , match ) : [ string , string ] => { const index = source . indexOf ( match ) ; if ( index === - 1 ) { return [ source , '' ] ; } return [ source . slice ( 0 , index ) , source . slice ( index + match . length ) ] ; } ; const searchForContent = ( element , text ) => { const xpathResult = element . ownerDocument ?. evaluate ( `//*[text()='${ text }']` , element , null , XPathResult . ORDERED_NODE_ITERATOR_TYPE , null ) ; const result = [ ] ; let match = null ; while ( ( match = xpathResult ?. iterateNext ( ) ) ) { result . push ( match as Element ) ; } return result ; } ; /**
 * Try to locate the eventual blockquote present in the document no matter the expeditor of the mail
 * Return the HTML content splitted at the blockquote start
 */ export const locateBlockquote = ( inputDocument ) : [ content : string , blockquote : string ] => { if ( ! inputDocument ) { return [ '' , '' ] ; } const body = inputDocument . querySelector ( 'body' ) ; const document = body || inputDocument ; const parentHTML = document . innerHTML || '' ; const parentText = document . textContent || '' ; let result = null ; const testBlockquote = ( blockquote ) => { const blockquoteText = blockquote . textContent || '' ; const [ beforeText = '' , afterText = '' ] = split ( parentText , blockquoteText ) ; if ( beforeText . trim ( ) . length && ! afterText . trim ( ) . length ) { const blockquoteHTML = blockquote . outerHTML || '' ; const [ beforeHTML = '' ] = split ( parentHTML , blockquoteHTML ) ; return [ beforeHTML , blockquoteHTML ] as [ string , string ] ; } return null ; } ; const blockquotes = [ ... document . querySelectorAll ( BLOCKQUOTE_SELECTOR ) ] ; blockquotes . forEach ( ( blockquote ) => { if ( result === null ) { result = testBlockquote ( blockquote ) ; } } ) ; if ( result === null ) { BLOCKQUOTE_TEXT_SELECTORS . forEach ( ( text ) => { if ( result === null ) { searchForContent ( document , text ) . forEach ( ( blockquote ) => { if ( result === null ) { result = testBlockquote ( blockquote ) ; } } ) ; } } ) ; } return result || [ parentHTML , '' ] ; } ;