From 0f014b7012df0e8f225a00cadb5574795ab443c9 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Fri, 28 Jun 2019 15:25:40 +0100 Subject: [PATCH] Preserve content order of fixed RectList Fixes #11 --- demo.css | 22 ++++++++++++++++++---- demo.js | 8 ++++++-- index.html | 4 ++-- rangefix.js | 6 ++++-- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/demo.css b/demo.css index 2405984..dc9d48b 100644 --- a/demo.css +++ b/demo.css @@ -17,24 +17,38 @@ .highlights { position: absolute; - opacity: 0.5; - pointer-events: none; } .highlight { position: absolute; } +.highlight > span { + display: none; +} + +.highlight:hover > span { + display: inline; + background: #fff; + font-weight: bold; + font-family: sans-serif; + font-size: 12px; + position: absolute; + top: 1px; + left: 1px; +} + .bounding { position: absolute; border: 1px solid; padding: 2px; margin: -3px 0 0 -3px; border-radius: 3px; + pointer-events: none; } .highlights-native .highlight { - background: #f00; + background: rgba( 255, 0, 0, 0.3 ); box-shadow: inset 0 0 0 1px #600; } @@ -43,7 +57,7 @@ } .highlights-fixed .highlight { - background: #0a0; + background: rgba( 0, 170, 0, 0.3 ); box-shadow: inset 0 0 0 1px #060; } diff --git a/demo.js b/demo.js index 9864d35..ce5e40b 100644 --- a/demo.js +++ b/demo.js @@ -52,7 +52,9 @@ $( function () { // Native rects = range.getClientRects(); for ( i = 0, l = rects.length; i < l; i++ ) { - $highlightsNative.append( $( '
' ).addClass( 'highlight' ).css( cssProps( rects[ i ] ) ) ); + $highlightsNative.append( + $( '
' ).addClass( 'highlight' ).css( cssProps( rects[ i ] ) ).append( $( '' ).text( i ) ) + ); } $( '.highlights-native' ).empty().append( $highlightsNative ); @@ -62,7 +64,9 @@ $( function () { // Fixed rects = RangeFix.getClientRects( range ); for ( i = 0, l = rects.length; i < l; i++ ) { - $highlightsFixed.append( $( '
' ).addClass( 'highlight' ).css( cssProps( rects[ i ] ) ) ); + $highlightsFixed.append( + $( '
' ).addClass( 'highlight' ).css( cssProps( rects[ i ] ) ).append( $( '' ).text( i ) ) + ); } $( '.highlights-fixed' ).empty().append( $highlightsFixed ); diff --git a/index.html b/index.html index 1d8bad1..26ea201 100644 --- a/index.html +++ b/index.html @@ -17,13 +17,13 @@

Select text here

Lorem ipsum

-

Native

+
-

Fixed

+
diff --git a/rangefix.js b/rangefix.js index d2cfbcd..7a3f582 100644 --- a/rangefix.js +++ b/rangefix.js @@ -166,7 +166,7 @@ * @return {ClientRectList|ClientRect[]} ClientRectList or list of ClientRect objects describing range */ rangeFix.getClientRects = function ( range ) { - var rects, endContainer, endOffset, partialRange, + var rects, endContainer, endContainerRects, endOffset, partialRange, broken = this.isBroken(); if ( broken.ieZoom ) { @@ -181,6 +181,7 @@ // we got up to // https://code.google.com/p/chromium/issues/detail?id=324437 rects = []; + endContainerRects = []; endContainer = range.endContainer; endOffset = range.endOffset; partialRange = document.createRange(); @@ -189,7 +190,7 @@ partialRange.setStart( endContainer, 0 ); partialRange.setEnd( endContainer, endOffset ); - batchPush( rects, partialRange.getClientRects() ); + batchPush( endContainerRects, partialRange.getClientRects() ); endOffset = Array.prototype.indexOf.call( endContainer.parentNode.childNodes, endContainer ); endContainer = endContainer.parentNode; @@ -200,6 +201,7 @@ partialRange = range.cloneRange(); partialRange.setEnd( endContainer, endOffset ); batchPush( rects, partialRange.getClientRects() ); + batchPush( rects, endContainerRects ); return rects; };