@@ -69,7 +69,7 @@ function isEndOfHTMLComment(word: string): boolean {
6969}
7070
7171// Added head and style (for style tags inside the body)
72- const atomicTagsRegExp = / ^ < ( i f r a m e | o b j e c t | m a t h | s v g | s c r i p t | v i d e o | h e a d | s t y l e | a ) / ;
72+ const atomicTagsRegExp = / ^ < ( i f r a m e | o b j e c t | m a t h | s v g | s c r i p t | v i d e o | h e a d | s t y l e | a ) $ / ;
7373
7474/**
7575 * Checks if the current word is the beginning of an atomic tag. An atomic tag is one whose
@@ -264,7 +264,7 @@ export function htmlToTokens(html: string): Token[] {
264264 const char = html [ charIdx ] as string ;
265265 switch ( mode ) {
266266 case 'tag' : {
267- const atomicTag = isStartOfAtomicTag ( currentWord ) ;
267+ const atomicTag = ( ' ' === char || '/' === char || '>' === char ) ? isStartOfAtomicTag ( currentWord ) : false ;
268268 const styleTag = isStartOfStyleTag ( currentWord + char ) ;
269269 const latestStyleTag = currentStyleTags . length && currentStyleTags [ currentStyleTags . length - 1 ] ;
270270 const endOfStyleTag = isEndOfTag ( char ) && latestStyleTag && isEndOfStyleTag ( currentWord , latestStyleTag ) ;
@@ -313,7 +313,7 @@ export function htmlToTokens(html: string): Token[] {
313313 break ;
314314 }
315315 case 'atomic_tag' :
316- if ( isEndOfTag ( char ) && isEndOfAtomicTag ( currentWord , currentAtomicTag ) ) {
316+ if ( isEndOfTag ( char ) && ( isImage ( currentWord + '>' ) || isEndOfAtomicTag ( currentWord , currentAtomicTag ) ) ) {
317317 currentWord += '>' ;
318318 words . push ( createToken ( currentWord , currentStyleTags , currentTableTags ) ) ;
319319 currentWord = '' ;
@@ -445,6 +445,16 @@ function getKeyForToken(token: string){
445445 return token ;
446446}
447447
448+ /**
449+ * Checks if a given token is image
450+ *
451+ * @param { } token
452+ * @returns
453+ */
454+ function isImage ( token : string ) {
455+ return / ^ < i m g .* s r c = [ ' " ] ( [ ^ " ' ] * ) [ ' " ] .* > $ / . exec ( token ) ;
456+ }
457+
448458const tokenMapKey = ( token : Token ) => token . key + JSON . stringify ( token . styles ) + JSON . stringify ( token . tableTags ) ;
449459
450460/**
@@ -639,16 +649,16 @@ function getFullMatch(segment: Segment, beforeStart: number, afterStart: number,
639649 }
640650 }
641651
642- // Extend the current match as far foward as it can go, without overflowing beforeTokens or
652+ // Extend the current match as far forward as it can go, without overflowing beforeTokens or
643653 // afterTokens.
644654 let searching = true ;
645655 let currentLength = 1 ;
646656 let beforeIndex = beforeStart + currentLength ;
647657 let afterIndex = afterStart + currentLength ;
648658
649659 while ( searching && beforeIndex < beforeTokens . length && afterIndex < afterTokens . length ) {
650- const beforeWord = beforeTokens [ beforeIndex ] ?. key ;
651- const afterWord = afterTokens [ afterIndex ] ?. key ;
660+ const beforeWord = getTextToCompare ( beforeIndex , beforeTokens ) ;
661+ const afterWord = getTextToCompare ( afterIndex , afterTokens ) ;
652662 const beforeStyle = JSON . stringify ( beforeTokens [ beforeIndex ] ?. styles ) ;
653663 const afterStyle = JSON . stringify ( afterTokens [ afterIndex ] ?. styles ) ;
654664 if ( beforeWord === afterWord && beforeStyle === afterStyle ) {
@@ -675,6 +685,16 @@ function getFullMatch(segment: Segment, beforeStart: number, afterStart: number,
675685
676686 return makeMatch ( beforeStart , afterStart , currentLength , segment ) ;
677687}
688+
689+ function getTextToCompare ( index : number , tokens : any [ ] ) : string {
690+ const token = tokens [ index ] ;
691+ if ( ! token ) {
692+ throw Error ( `Expected ${ tokens } to have an element at position ${ index } ` ) ;
693+ }
694+ const key = ! ! isStartOfAtomicTag ( token . key ) ? 'string' : 'key' ;
695+ return token [ key ] ;
696+ }
697+
678698type Segment = {
679699 beforeTokens : Token [ ] ;
680700 afterTokens : Token [ ] ;
0 commit comments