@@ -1108,6 +1108,25 @@ function Highlighter(options = hhDefaultOptions) {
11081108 const unmergedRects = Array . from ( range . getClientRects ( ) ) ;
11091109 const mergedRects = [ ] ;
11101110
1111+ // Remove element rects (only text node rects are needed)
1112+ const ancestorElementsInRange = [ ] ;
1113+ for ( const paragraph of paragraphs ) {
1114+ let element = paragraph ;
1115+ while ( element !== range . commonAncestorContainer ) {
1116+ if ( ! ancestorElementsInRange . includes ( element ) ) ancestorElementsInRange . push ( element ) ;
1117+ element = element . parentElement ;
1118+ }
1119+ }
1120+ for ( let ur = unmergedRects . length - 1 ; ur >= 0 ; ur -- ) {
1121+ const rect = unmergedRects [ ur ] ;
1122+ for ( const element of ancestorElementsInRange ) {
1123+ const elementRect = element . getBoundingClientRect ( ) ;
1124+ if ( Math . round ( elementRect . width ) === Math . round ( rect . width ) && Math . round ( elementRect . height ) === Math . round ( rect . height ) && Math . round ( elementRect . top ) === Math . round ( rect . top ) && Math . round ( elementRect . left ) === Math . round ( rect . left ) ) {
1125+ unmergedRects . splice ( ur , 1 ) ;
1126+ }
1127+ }
1128+ }
1129+
11111130 // Loop through the highlight's paragraphs
11121131 for ( const paragraph of paragraphs ) {
11131132 const paragraphRect = paragraph . getBoundingClientRect ( ) ;
@@ -1142,11 +1161,7 @@ function Highlighter(options = hhDefaultOptions) {
11421161 for ( let r = 0 ; r < unmergedRects . length ; r ++ ) {
11431162 const rect = unmergedRects [ r ] ;
11441163 const rectVerticalPosition = rect . y + ( rect . height / 2 ) ;
1145- if ( Math . round ( paragraphRect . width ) === Math . round ( rect . width ) && Math . round ( paragraphRect . height ) === Math . round ( rect . height ) && Math . round ( paragraphRect . top ) === Math . round ( rect . top ) && Math . round ( paragraphRect . left ) === Math . round ( rect . left ) ) {
1146- // Remove rects that are the same size as the paragraph rect (the highlight range gives us these unneeded rects for "middle" paragraphs between the start and end paragraph)
1147- unmergedRects . splice ( r , 1 ) ;
1148- r -- ;
1149- } else if ( rectVerticalPosition > mergedRect . y && rectVerticalPosition < mergedRect . y + mergedRect . height ) {
1164+ if ( rectVerticalPosition > mergedRect . y && rectVerticalPosition < mergedRect . y + mergedRect . height ) {
11501165 if ( mergedRect . x === - 1 ) {
11511166 mergedRect . x = rect . x ;
11521167 mergedRect . width = rect . width ;
0 commit comments