-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
57 lines (44 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Module dependencies.
*/
var getDocument = require('get-document');
/**
* Module exports.
*/
module.exports = getBoundingClientRect;
/**
* Returns the "bounding client rectangle" of the given `TextNode`,
* `HTMLElement`, or `Range`.
*
* @param {Node} node
* @return {TextRectangle}
* @public
*/
function getBoundingClientRect (node) {
var rect = null;
var doc = getDocument(node);
if (node.nodeType === 3 /* TEXT_NODE */) {
// see: http://stackoverflow.com/a/6966613/376773
var range = doc.createRange();
range.selectNodeContents(node);
node = range;
}
if ('function' === typeof node.getBoundingClientRect) {
rect = node.getBoundingClientRect();
if (node.startContainer && rect.left === 0 && rect.top === 0) {
// Range instances sometimes report all `0`s
// see: http://stackoverflow.com/a/6847328/376773
var span = doc.createElement('span');
// Ensure span has dimensions and position by
// adding a zero-width space character
span.appendChild(doc.createTextNode('\u200b'));
node.insertNode(span);
rect = span.getBoundingClientRect();
// Remove temp SPAN and glue any broken text nodes back together
var spanParent = span.parentNode;
spanParent.removeChild(span);
spanParent.normalize();
}
}
return rect;
}