d2-ui
Version:
63 lines (56 loc) • 1.68 kB
JavaScript
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule getRangeBoundingClientRect
* @typechecks
*
*/
;
Object.defineProperty(exports, '__esModule', {
value: true
});
var getRangeClientRects = require('./getRangeClientRects');
/**
* Like range.getBoundingClientRect() but normalizes for browser bugs.
*/
function getRangeBoundingClientRect(range) {
// "Return a DOMRect object describing the smallest rectangle that includes
// the first rectangle in list and all of the remaining rectangles of which
// the height or width is not zero."
// http://www.w3.org/TR/cssom-view/#dom-range-getboundingclientrect
var rects = getRangeClientRects(range);
var top = 0;
var right = 0;
var bottom = 0;
var left = 0;
if (rects.length) {
var _rects$0 = rects[0];
top = _rects$0.top;
right = _rects$0.right;
bottom = _rects$0.bottom;
left = _rects$0.left;
for (var ii = 1; ii < rects.length; ii++) {
var rect = rects[ii];
if (rect.height !== 0 || rect.width !== 0) {
top = Math.min(top, rect.top);
right = Math.max(right, rect.right);
bottom = Math.max(bottom, rect.bottom);
left = Math.min(left, rect.left);
}
}
}
return {
top: top,
right: right,
bottom: bottom,
left: left,
width: right - left,
height: bottom - top
};
}
module.exports = getRangeBoundingClientRect;