Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/child/utils.js

Issue 29363476: Issue 2879 - Move element selection into the content process (Closed) Base URL: https://hg.adblockplus.org/elemhidehelper
Patch Set: Addressed comments and marked extension as E10S-compatible Created Nov. 24, 2016, 2 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/child/selection.js ('k') | lib/main.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This Source Code is subject to the terms of the Mozilla Public License
3 * version 2.0 (the "License"). You can obtain a copy of the License at
4 * http://mozilla.org/MPL/2.0/.
5 */
6
7 "use strict";
8
9 /**
10 * Element creation helper, allows defining attributes and child elements in one
11 * go.
12 * @param {Document} doc
13 * Document to create the element in
14 * @param {string} tagName
15 * Tag name of the new element
16 * @param {Object.<string, string>} [attrs]
17 * Attributes to set on the element
18 * @param {Array.<Node>} [children]
19 * Child nodes to add to the element
20 * @return {Element}
21 * Element that was created
22 */
23 function createElement(doc, tagName, attrs, children)
24 {
25 let el = doc.createElement(tagName);
26 if (attrs)
27 for (let key in attrs)
28 el.setAttribute(key, attrs[key]);
29 if (children)
30 for (let child of children)
31 el.appendChild(child)
32 return el;
33 }
34 exports.createElement = createElement;
35
36 /**
37 * Calculates the document size for a window.
38 * @return {Array.<number>}
39 * Width and height of the document loaded into the window
40 */
41 function getWindowSize(/**Window*/ wnd)
42 {
43 return [wnd.innerWidth, wnd.document.documentElement.clientHeight];
44 }
45 exports.getWindowSize = getWindowSize;
46
47 /**
48 * Determines the parent element for a document node, if any. Will ascend into
49 * parent frames if necessary.
50 */
51 function getParentElement(/**Node*/ elem) /**Element*/
52 {
53 let result = elem.parentNode;
54 if (result && result.nodeType == result.DOCUMENT_NODE && result.defaultView && result.defaultView.frameElement)
55 result = result.defaultView.frameElement;
56
57 if (result && result.nodeType != result.ELEMENT_NODE)
58 return null;
59
60 return result;
61 }
62 exports.getParentElement = getParentElement;
63
64 /**
65 * Modifies a rectangle with coordinates relative to a window's client area
66 * to make sure it doesn't exceed that client area.
67 * @param {Object} rect
68 * Rectangle with properties left, top, right, bottom.
69 * @param {Window} wnd
70 * Window to restrict the rectangle to.
71 */
72 function intersectRect(rect, wnd)
73 {
74 let [wndWidth, wndHeight] = getWindowSize(wnd);
75 rect.left = Math.max(rect.left, 0);
76 rect.top = Math.max(rect.top, 0);
77 rect.right = Math.min(rect.right, wndWidth);
78 rect.bottom = Math.min(rect.bottom, wndHeight);
79 }
80
81 /**
82 * Calculates the element's position within the top frame. This will consider
83 * the element being clipped by frame boundaries.
84 * @return {Object}
85 * Object with properties left, top, width, height denoting the element's
86 * position and size within the top frame.
87 */
88 function getElementPosition(/**Element*/ element)
89 {
90 let rect = element.getBoundingClientRect();
91 let wnd = element.ownerDocument.defaultView;
92
93 rect = {left: rect.left, top: rect.top,
94 right: rect.right, bottom: rect.bottom};
95 while (true)
96 {
97 intersectRect(rect, wnd);
98
99 if (!wnd.frameElement)
100 break;
101
102 // Recalculate coordinates to be relative to frame's parent window
103 let frameElement = wnd.frameElement;
104 wnd = frameElement.ownerDocument.defaultView;
105
106 let frameRect = frameElement.getBoundingClientRect();
107 let frameStyle = wnd.getComputedStyle(frameElement, null);
108 let relLeft = frameRect.left + parseFloat(frameStyle.borderLeftWidth) + pars eFloat(frameStyle.paddingLeft);
109 let relTop = frameRect.top + parseFloat(frameStyle.borderTopWidth) + parseFl oat(frameStyle.paddingTop);
110
111 rect.left += relLeft;
112 rect.right += relLeft;
113 rect.top += relTop;
114 rect.bottom += relTop;
115 }
116
117 return rect;
118 }
119 exports.getElementPosition = getElementPosition;
OLDNEW
« no previous file with comments | « lib/child/selection.js ('k') | lib/main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld