Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the height of a text selection without changing the DOM

I am using the Range to manipulate selected text. I would like to calculate the height from where someone started selecting text to where they finished.

I have tried a span to the beginning and end of the selected range and I can accurately calculate the height form that, but it changes the DOM and prevents me form doing some other range manipulations like highlighting previously selected text.

I have also tried collecting the position of the mosedown and mosueup positions but I need an accurate height from the top of the text selected to the bottom of the text where the selection was released and thats not always the case.

So I was wondering if there was a way to calculate the height of a text selection without changing the DOM?

like image 304
Adim Avatar asked Jul 26 '11 22:07

Adim


People also ask

How do you get height in HTML?

Get Height of HTML Element using JavaScript Now with a padding of 5px, meaning padding on all sides is 5px, the new height of the HTML element is 85px + padding-top + padding-bottom. Since padding on top and bottom are 5px each, the resulting height is 85px + 5px + 5px = 95px.

How do I count text lines inside a DOM element?

To count number of text lines inside DOM element, we will use the following approach. Obtain the total height of content inside the DOM element. Obtain the height of one line. By dividing the total height of the content by the height of one line, you get the total number of lines inside the element.

How to calculate text width with JavaScript?

The dimensions of the text are calculated using the measureText() method. The text to be measured is passed to this method. It returns a TextMetrics object that contains information about the measured text. The width property of this TextMetrics object is used to get the width of the text.

How to find width of text in HTML?

The measureText() method returns an object that contains the width of the specified text, in pixels.


1 Answers

It depends which browsers you need to deal with. Here's a function that will work in IE >= 4 and browsers that support getClientRects() in Range (Firefox >= 4, WebKit since 2009, Opera). If you need support for earlier browsers, you'll need to modify the DOM, which is actually fine so long as you restore the DOM to its previous state.

jsFiddle: http://jsfiddle.net/W84yW/

Code:

function getSelectionHeight() {
    var selHeight = 0;
    if (document.selection && document.selection.type == "Text") {
        var textRange = document.selection.createRange();
        selHeight = textRange.boundingHeight;
    } else if (window.getSelection) {
        var sel = window.getSelection();
        if (sel.rangeCount > 0) {
            var range = sel.getRangeAt(0);
            if (!range.collapsed && range.getClientRects) {
                var startRange = range.cloneRange();
                startRange.collapse(true);
                var selTop = startRange.getClientRects()[0].top;
                startRange.detach();
                var endRange = range.cloneRange();
                endRange.collapse(false);
                selHeight = endRange.getClientRects()[0].bottom - selTop;
                endRange.detach();
            }
        }
    }
    return selHeight;
}
like image 112
Tim Down Avatar answered Oct 23 '22 04:10

Tim Down