Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | // functions for dealing with layout and geometry of page elements. |
| 6 | // Requires shapes.js |
| 7 | |
| 8 | /** returns the bounding box of the given DOM node in document space. |
| 9 | * |
| 10 | * @param {Element?} obj a DOM node. |
| 11 | * @return {Rect?} |
| 12 | */ |
| 13 | function nodeBounds(obj) { |
| 14 | if (!obj) return null; |
| 15 | |
| 16 | function fixRectForScrolling(r) { |
| 17 | // Need to take into account scrolling offset of ancestors (IE already does |
| 18 | // this) |
| 19 | for (let o = obj.offsetParent; |
| 20 | o && o.offsetParent; |
| 21 | o = o.offsetParent) { |
| 22 | if (o.scrollLeft) { |
| 23 | r.x -= o.scrollLeft; |
| 24 | } |
| 25 | if (o.scrollTop) { |
| 26 | r.y -= o.scrollTop; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | let refWindow; |
| 32 | if (obj.ownerDocument && obj.ownerDocument.parentWindow) { |
| 33 | refWindow = obj.ownerDocument.parentWindow; |
| 34 | } else if (obj.ownerDocument && obj.ownerDocument.defaultView) { |
| 35 | refWindow = obj.ownerDocument.defaultView; |
| 36 | } else { |
| 37 | refWindow = window; |
| 38 | } |
| 39 | |
| 40 | // IE, Mozilla 3+ |
| 41 | if (obj.getBoundingClientRect) { |
| 42 | let rect = obj.getBoundingClientRect(); |
| 43 | |
| 44 | return new Rect(rect.left + GetScrollLeft(refWindow), |
| 45 | rect.top + GetScrollTop(refWindow), |
| 46 | rect.right - rect.left, |
| 47 | rect.bottom - rect.top, |
| 48 | refWindow); |
| 49 | } |
| 50 | |
| 51 | // Mozilla < 3 |
| 52 | if (obj.ownerDocument && obj.ownerDocument.getBoxObjectFor) { |
| 53 | let box = obj.ownerDocument.getBoxObjectFor(obj); |
| 54 | var r = new Rect(box.x, box.y, box.width, box.height, refWindow); |
| 55 | fixRectForScrolling(r); |
| 56 | return r; |
| 57 | } |
| 58 | |
| 59 | // Fallback to recursively computing this |
| 60 | let left = 0; |
| 61 | let top = 0; |
| 62 | for (let o = obj; o.offsetParent; o = o.offsetParent) { |
| 63 | left += o.offsetLeft; |
| 64 | top += o.offsetTop; |
| 65 | } |
| 66 | |
| 67 | var r = new Rect(left, top, obj.offsetWidth, obj.offsetHeight, refWindow); |
| 68 | fixRectForScrolling(r); |
| 69 | return r; |
| 70 | } |
| 71 | |
| 72 | function GetMousePosition(e) { |
| 73 | // copied from http://www.quirksmode.org/js/events_compinfo.html |
| 74 | let posx = 0; |
| 75 | let posy = 0; |
| 76 | if (e.pageX || e.pageY) { |
| 77 | posx = e.pageX; |
| 78 | posy = e.pageY; |
| 79 | } else if (e.clientX || e.clientY) { |
| 80 | let obj = (e.target ? e.target : e.srcElement); |
| 81 | let refWindow; |
| 82 | if (obj.ownerDocument && obj.ownerDocument.parentWindow) { |
| 83 | refWindow = obj.ownerDocument.parentWindow; |
| 84 | } else { |
| 85 | refWindow = window; |
| 86 | } |
| 87 | posx = e.clientX + GetScrollLeft(refWindow); |
| 88 | posy = e.clientY + GetScrollTop(refWindow); |
| 89 | } |
| 90 | return new Point(posx, posy, window); |
| 91 | } |