Code Monkey home page Code Monkey logo

rangefix's Introduction

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

In particular:

  • Chrome <= 54: Selections spanning multiple nodes return rectangles for all the parents of the endContainer. See https://code.google.com/p/chromium/issues/detail?id=324437.
  • Chrome 55: Images get no rectangle when they are wrapped in a node and you select across them.
  • Safari: Similar to the Chrome <= 54 bug, but only triggered near the edge of a block node, or programmatically near an inline node.
  • Firefox: Similar to the Chrome <= 54 bug, but only triggered near the edge of a inline node
  • IE <= 10: Rectangles are incorrectly scaled when using the browser's zoom feature.
  • Chrome: Selection across a space which spans two lines results in a bounding rectangle which doesn't cover all the individual rectangles.
  • Firefox: Selections across a space which spans two lines, and text on the next line results in a bounding rectangle which doesn't cover all the individual rectangles.

There are no known issues in Edge and IE >= 11. In these browsers the library will fall through to native behaviour.

Install

$ npm install rangefix

Usage

CommonJS

var RangeFix = require( 'rangefix' );

AMD

define( [ 'rangefix' ], function ( Rangefix ) {
	// ...
} );

Browser global

<script src="path/to/rangefix.js"></script>

API

Replace instances of Range.prototype.getClientRects/getBoundingClientRect with RangeFix.getClientRects/getBoundingClientRect:

var range = document.getSelection().getRangeAt( 0 );

// Before
var rects = range.getClientRects();
var boundingRect = range.getBoundingClientRect();

// After
var rects = RangeFix.getClientRects( range );
var boundingRect = RangeFix.getBoundingClientRect( range );

Demo

http://edg2s.github.io/rangefix/

rangefix's People

Contributors

dependabot[bot] avatar edg2s avatar jdforrester avatar siadcock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rangefix's Issues

RangeFix.getClientRects(range) Not returning correct rects.

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

range.getBoundingClientRect() misbehaviour on multiline texts

Hi,

I am experiencing a werid range.getBoundingClientRect() bug on https://www.theguardian.com/international. For my layout analyzer layoutstats, I want to calculate the area and the positions of all visible text nodes on the page.

I am using range.getBoundingClientRect() for that purpose. If the analyzed textnode spans multiple lines, the reported left and right values of the ClientRect are way off the correct numbers (see code and screenshot), range.getClientRects() reports that there is just one client rect, however by spec there should be multiple rects (one for each line break)

s = document.querySelector('#spotlight > div >... (selector copied from chrome developer tools)...')
s.childNodes[0].textContent; //"America’s eviction epidemic"
r = document.createRange(); 
r.selectNode(s.childNodes[0]);
console.log(r.getBoundingClientRect());  
//ClientRect {top: 142.09375, right: 1295.75, bottom: 162.09375, left: 1081.5, width: 214.25…}
//expected {left: 438. right: 898, (other values ok)} - see screenshot
r.getClientRects() //1, should be 2 client rects according to spec

image

Can rangefix correct this behaviour? If not, is this an intentional behaviour or a browser bug?

Some client rects are redundant

I say redundant because the dimension of a few client rects are almost the same, with the only difference sometimes is lesser height by a few pixels or perhaps width.

Would be a nice feature if this gets cleaned up and only the rect with the larger area gets returned.

Bounding rect incorrect when selecting space spanning lines

  1. Place the cursor after the last word on a line
  2. Select the space to the right, up to the next character, or one character beyond
  3. Observe incorrect bounding boxes in various browsers:

Selection "from[ ]here" in Chrome (broken):

image

Selection "from[ h]ere" in Chrome (not broken):

image

Selection "from[ ]here" in Firefox (not broken):

image

Selection "from[ h]ere" in Firefox (broken):

image

Some client rects appear twice

The fixed version of getClientRects() returns two copies of client rects for RTL text. Here's a fork of your demo which shows the problem. If you select the first line line which includes the Arabic text, then view the highlights in the web inspector, you'll see that the client rect corresponding to the RTL span appears twice:

https://rawgit.com/jahewson/rangefix/master/demo.html

P.S. Good work with this fix, because Chrome doesn't seem to be progressing on this front.

Investigate IE/Edge bi-di issue

All versions of IE/Edge don't handle bi-di text correctly. It's possible there is no performant way to fix this, but worth investigating.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.