1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function getElementRegion(element) {
6 // Check that node type is element.
7 if (element.nodeType != 1)
8 throw new Error(element + ' is not an element');
10 // We try 2 methods to determine element region. Try the first client rect,
11 // and then the bounding client rect.
12 // SVG is one case that doesn't have a first client rect.
13 var clientRects = element.getClientRects();
14 if (clientRects.length == 0) {
15 var box = element.getBoundingClientRect();
23 var clientRect = clientRects[0];
24 var box = element.getBoundingClientRect();
26 'left': clientRect.left - box.left,
27 'top': clientRect.top - box.top,
28 'width': clientRect.right - clientRect.left,
29 'height': clientRect.bottom - clientRect.top