edg2s / rangefix

Workaround for browser bugs in Range.prototype.getClientRects and Range.prototype.getBoundingClientRect.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RangeFix.getClientRects(range) Not returning correct rects.

myself3421 opened this issue · comments

When we select text 'Vestibulum neque massa, scelerisque sit amet ligula eu, congue molestie mi. Praesent ut
varius sem. Nullam at porttitor arcu, nec lacinia nisi. Ut ac dolor vitae odio interdum
condimentum.
' from given sample pdf file then
let range = document.getSelection().getRangeAt(0);
RangeFix.getClientRects(range); returns the 3 rects in firefox and 4 rects in chrome browser.
Please find the attached document.
file-sample_150kB.pdf

commented

What are you using to render the PDF in the browser?

We are using pdf.js library to render PDF.

commented

If I extract the HTML from the pdf.js render, I get all 3 rects in both FF and Chrome. If I run RangeFix inside the original pdf.js online viewer, I get 6 rects in both browser (maybe 3 more for the fake selection?)

Can you provide a standalone example of the bug you describe, e.g. in a CodePen?

Hi,
-Please go through the link "https://codesandbox.io/s/displaying-pdf-using-react-forked-6bqup7?file=/src/components/pdf/all-pages.js"
-Open the output window in a separate tab and also open devTool console tab.
-Select the 'Vestibulum neque massa, scelerisque sit amet ligula eu, congue molestie mi. Praesent ut
varius sem. Nullam at porttitor arcu, nec lacinia nisi. Ut ac dolor vitae odio interdum
condimentum.
' text from first paragraph.
-On mouse up event, we console log the "clientRects received from RangeFix" so please check the console log.

Follow the same scenario on the Google Chrome browser, Firefox, and Edge browser.
Observe the console log on the respective browser.

Note: I have verified on windows 10
Chrome browser Version 105.0.5195.102 (Official Build) (64-bit),
Firefox 104.0.2 (64-bit), and
Microsoft Edge Version 105.0.1343.33 (Official build) (64-bit)

Observation: Received clientRects from the Below code
let range = document.getSelection().getRangeAt(0);
let rects = RangeFix.getClientRects(range);

Google Chrome: 4 clientRects
Firefox: 3 clientRects
Microsoft Edge: 4 clientRects

One more eg. select the first 4 lines from the document and check the console log on Chrome and Firefox it gives a different result.
At my end, I am receiving 10 rects in Chrome and 6 rects in Firefox.

commented

A minimal test case is:

<p>Foo <b>bar</b> baz<p>

In Chrome you get a rect for the text node "bar" as well as a rect for the <b> element. In Firefox you only get one for the <b> tag.

I don't know which behaviour is closer to spec, or if this library could detect and fix it. Generally when using the rects lists you should assume that the rects could overlap and adapt your use case accordingly.