2011-12-07 12:13:02 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-07 12:13:02 -08:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["LayoutHelpers"];
|
|
|
|
|
|
|
|
LayoutHelpers = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the position and the dimensions for the visible portion
|
|
|
|
* of a node, relativalely to the root window.
|
|
|
|
*
|
|
|
|
* @param nsIDOMNode aNode
|
|
|
|
* a DOM element to be highlighted
|
|
|
|
*/
|
|
|
|
getDirtyRect: function LH_getDirectyRect(aNode) {
|
|
|
|
let frameWin = aNode.ownerDocument.defaultView;
|
|
|
|
let clientRect = aNode.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Go up in the tree of frames to determine the correct rectangle.
|
|
|
|
// clientRect is read-only, we need to be able to change properties.
|
|
|
|
rect = {top: clientRect.top,
|
|
|
|
left: clientRect.left,
|
|
|
|
width: clientRect.width,
|
|
|
|
height: clientRect.height};
|
|
|
|
|
|
|
|
// We iterate through all the parent windows.
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
// Does the selection overflow on the right of its window?
|
|
|
|
let diffx = frameWin.innerWidth - (rect.left + rect.width);
|
|
|
|
if (diffx < 0) {
|
|
|
|
rect.width += diffx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the selection overflow on the bottom of its window?
|
|
|
|
let diffy = frameWin.innerHeight - (rect.top + rect.height);
|
|
|
|
if (diffy < 0) {
|
|
|
|
rect.height += diffy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the selection overflow on the left of its window?
|
|
|
|
if (rect.left < 0) {
|
|
|
|
rect.width += rect.left;
|
|
|
|
rect.left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the selection overflow on the top of its window?
|
|
|
|
if (rect.top < 0) {
|
|
|
|
rect.height += rect.top;
|
|
|
|
rect.top = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selection has been clipped to fit in its own window.
|
|
|
|
|
|
|
|
// Are we in the top-level window?
|
|
|
|
if (frameWin.parent === frameWin || !frameWin.frameElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are in an iframe.
|
|
|
|
// We take into account the parent iframe position and its
|
|
|
|
// offset (borders and padding).
|
|
|
|
let frameRect = frameWin.frameElement.getBoundingClientRect();
|
|
|
|
|
|
|
|
let [offsetTop, offsetLeft] =
|
|
|
|
this.getIframeContentOffset(frameWin.frameElement);
|
|
|
|
|
|
|
|
rect.top += frameRect.top + offsetTop;
|
|
|
|
rect.left += frameRect.left + offsetLeft;
|
|
|
|
|
|
|
|
frameWin = frameWin.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
},
|
|
|
|
|
2012-01-19 07:13:08 -08:00
|
|
|
/**
|
|
|
|
* Compute the absolute position and the dimensions of a node, relativalely
|
|
|
|
* to the root window.
|
|
|
|
*
|
|
|
|
* @param nsIDOMNode aNode
|
|
|
|
* a DOM element to get the bounds for
|
|
|
|
* @param nsIWindow aContentWindow
|
|
|
|
* the content window holding the node
|
|
|
|
*/
|
|
|
|
getRect: function LH_getRect(aNode, aContentWindow) {
|
|
|
|
let frameWin = aNode.ownerDocument.defaultView;
|
|
|
|
let clientRect = aNode.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Go up in the tree of frames to determine the correct rectangle.
|
|
|
|
// clientRect is read-only, we need to be able to change properties.
|
|
|
|
rect = {top: clientRect.top + aContentWindow.pageYOffset,
|
|
|
|
left: clientRect.left + aContentWindow.pageXOffset,
|
|
|
|
width: clientRect.width,
|
|
|
|
height: clientRect.height};
|
|
|
|
|
|
|
|
// We iterate through all the parent windows.
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
// Are we in the top-level window?
|
|
|
|
if (frameWin.parent === frameWin || !frameWin.frameElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are in an iframe.
|
|
|
|
// We take into account the parent iframe position and its
|
|
|
|
// offset (borders and padding).
|
|
|
|
let frameRect = frameWin.frameElement.getBoundingClientRect();
|
|
|
|
|
|
|
|
let [offsetTop, offsetLeft] =
|
|
|
|
this.getIframeContentOffset(frameWin.frameElement);
|
|
|
|
|
|
|
|
rect.top += frameRect.top + offsetTop;
|
|
|
|
rect.left += frameRect.left + offsetLeft;
|
|
|
|
|
|
|
|
frameWin = frameWin.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
},
|
|
|
|
|
2011-12-07 12:13:02 -08:00
|
|
|
/**
|
|
|
|
* Returns iframe content offset (iframe border + padding).
|
|
|
|
* Note: this function shouldn't need to exist, had the platform provided a
|
|
|
|
* suitable API for determining the offset between the iframe's content and
|
|
|
|
* its bounding client rect. Bug 626359 should provide us with such an API.
|
|
|
|
*
|
|
|
|
* @param aIframe
|
|
|
|
* The iframe.
|
|
|
|
* @returns array [offsetTop, offsetLeft]
|
|
|
|
* offsetTop is the distance from the top of the iframe and the
|
|
|
|
* top of the content document.
|
|
|
|
* offsetLeft is the distance from the left of the iframe and the
|
|
|
|
* left of the content document.
|
|
|
|
*/
|
|
|
|
getIframeContentOffset: function LH_getIframeContentOffset(aIframe) {
|
|
|
|
let style = aIframe.contentWindow.getComputedStyle(aIframe, null);
|
|
|
|
|
2012-01-19 07:13:08 -08:00
|
|
|
// In some cases, the computed style is null
|
|
|
|
if (!style) {
|
|
|
|
return [0, 0];
|
|
|
|
}
|
|
|
|
|
2011-12-07 12:13:02 -08:00
|
|
|
let paddingTop = parseInt(style.getPropertyValue("padding-top"));
|
|
|
|
let paddingLeft = parseInt(style.getPropertyValue("padding-left"));
|
|
|
|
|
|
|
|
let borderTop = parseInt(style.getPropertyValue("border-top-width"));
|
|
|
|
let borderLeft = parseInt(style.getPropertyValue("border-left-width"));
|
|
|
|
|
|
|
|
return [borderTop + paddingTop, borderLeft + paddingLeft];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the page zoom factor.
|
|
|
|
*/
|
|
|
|
getZoomedRect: function LH_getZoomedRect(aWin, aRect) {
|
|
|
|
// get page zoom factor, if any
|
|
|
|
let zoom =
|
|
|
|
aWin.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIDOMWindowUtils)
|
|
|
|
.screenPixelsPerCSSPixel;
|
|
|
|
|
|
|
|
// adjust rect for zoom scaling
|
|
|
|
let aRectScaled = {};
|
|
|
|
for (let prop in aRect) {
|
|
|
|
aRectScaled[prop] = aRect[prop] * zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aRectScaled;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an element from the given coordinates. This method descends through
|
|
|
|
* frames to find the element the user clicked inside frames.
|
|
|
|
*
|
|
|
|
* @param DOMDocument aDocument the document to look into.
|
|
|
|
* @param integer aX
|
|
|
|
* @param integer aY
|
|
|
|
* @returns Node|null the element node found at the given coordinates.
|
|
|
|
*/
|
|
|
|
getElementFromPoint: function LH_elementFromPoint(aDocument, aX, aY)
|
|
|
|
{
|
|
|
|
let node = aDocument.elementFromPoint(aX, aY);
|
|
|
|
if (node && node.contentDocument) {
|
|
|
|
if (node instanceof Ci.nsIDOMHTMLIFrameElement) {
|
|
|
|
let rect = node.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Gap between the iframe and its content window.
|
|
|
|
let [offsetTop, offsetLeft] = LayoutHelpers.getIframeContentOffset(node);
|
|
|
|
|
|
|
|
aX -= rect.left + offsetLeft;
|
|
|
|
aY -= rect.top + offsetTop;
|
|
|
|
|
|
|
|
if (aX < 0 || aY < 0) {
|
|
|
|
// Didn't reach the content document, still over the iframe.
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node instanceof Ci.nsIDOMHTMLIFrameElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLFrameElement) {
|
|
|
|
let subnode = this.getElementFromPoint(node.contentDocument, aX, aY);
|
|
|
|
if (subnode) {
|
|
|
|
node = subnode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
};
|