Bug 199692 - Add support for document.elementFromPoint(x, y). Patch by Ben Karel <web+moz@eschew.org>, r+sr=roc, a=jst

This commit is contained in:
jwalden@mit.edu 2007-08-29 13:38:44 -07:00
parent f21d8c2138
commit 4c25808b29
11 changed files with 337 additions and 3 deletions

View File

@ -77,6 +77,8 @@
#include "nsIDOMProcessingInstruction.h"
#include "nsDOMString.h"
#include "nsNodeUtils.h"
#include "nsLayoutUtils.h" // for GetFrameForPoint
#include "nsIFrame.h"
#include "nsRange.h"
#include "nsIDOMText.h"
@ -1650,6 +1652,60 @@ nsDocument::GetActiveElement(nsIDOMElement **aElement)
return GetDocumentElement(aElement);
}
NS_IMETHODIMP
nsDocument::ElementFromPoint(PRInt32 aX, PRInt32 aY, nsIDOMElement** aReturn)
{
NS_ENSURE_ARG_POINTER(aReturn);
*aReturn = nsnull;
// As per the the spec, we return null if either coord is negative
if (aX < 0 || aY < 0)
return NS_OK;
nscoord x = nsPresContext::CSSPixelsToAppUnits(aX);
nscoord y = nsPresContext::CSSPixelsToAppUnits(aY);
nsPoint pt(x, y);
// Make sure the layout information we get is up-to-date, and
// ensure we get a root frame (for everything but XUL)
FlushPendingNotifications(Flush_Layout);
nsIPresShell *ps = GetPrimaryShell();
NS_ENSURE_STATE(ps);
nsIFrame *rootFrame = ps->GetRootFrame();
// XUL docs, unlike HTML, have no frame tree until everything's done loading
if (!rootFrame)
return NS_OK; // return null to premature XUL callers as a reminder to wait
nsIFrame *ptFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, pt, PR_TRUE);
if (!ptFrame)
return NS_OK;
nsIContent* ptContent = ptFrame->GetContent();
NS_ENSURE_STATE(ptContent);
// If the content is in a subdocument, try to get the element from |this| doc
nsIDocument *currentDoc = ptContent->GetCurrentDoc();
if (currentDoc && (currentDoc != this)) {
*aReturn = CheckAncestryAndGetFrame(currentDoc).get();
return NS_OK;
}
// If we have an anonymous element (such as an internal div from a textbox),
// or a node that isn't an element (such as a text frame node),
// replace it with the first non-anonymous parent node of type element.
while (ptContent &&
!ptContent->IsNodeOfType(nsINode::eELEMENT) ||
ptContent->GetBindingParent() ||
ptContent->IsNativeAnonymous()) {
ptContent = ptContent->GetParent();
}
if (ptContent)
CallQueryInterface(ptContent, aReturn);
return NS_OK;
}
NS_IMETHODIMP
nsDocument::GetElementsByClassName(const nsAString& aClasses,
nsIDOMNodeList** aReturn)

View File

@ -46,6 +46,9 @@ include $(topsrcdir)/config/rules.mk
_TEST_FILES = test_bug1682.html \
test_bug1823.html \
test_bug199692.html \
bug199692-nested.html \
bug199692-nested-d2.html \
test_bug172261.html \
test_bug255820.html \
test_bug311681.html \

View File

@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=199692
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="nest2div" style="border: 2px dotted blue;">nested, depth 2</div>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=199692
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="nest1div" style="border: 2px dotted green;">nested, depth 1</div>
<iframe src="bug199692-nested-d2.html"></iframe>
</body>
</html>

View File

@ -0,0 +1,120 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=199692
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test for Bug 199692</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<style type="text/css">
#content * {
border: 2px solid black;
margin: 2px;
clear: both;
height: 20px;
overflow: hidden;
}
#txt, #static, #fixed, #absolute, #relative, #hidden, #float, #empty, #static, #relative {
width: 200px !important;
}
</style>
</head>
<!-- Elements are styled in such a way that they don't overlap visually unless they also overlap structurally. -->
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=199692">Mozilla Bug 199692</a>
<div id="content" style="width: 500px; background-color: #ccc;">
<!-- element containing text -->
<div id="txt" style="height: 30px;">txt</div>
<!-- element not containing text -->
<div id="empty" style="border: 2px solid blue;"></div>
<!-- element with only whitespace -->
<p id="whitespace" style="border: 2px solid magenta;"> </p>
<!-- position: static -->
<span id="static" style="position: static; border-color: green;">static</span>
<!-- floated element -->
<div id="float" style="border-color: red; float: right;">float</div>
<!-- position: fixed -->
<span id="fixed" style="position: fixed; top: 500px; left: 100px; border: 3px solid yellow;">fixed</span>
<!-- position: absolute -->
<span id="absolute" style="position: absolute; top: 550px; left: 150px; border-color: orange;">abs</span>
<!-- position: relative -->
<div id="relative" style="position: relative; top: 200px; border-color: teal;">rel</div>
<!-- visibility: hidden -->
<div id="hidden-wrapper" style="border: 1px dashed teal;">
<div id="hidden" style="opacity: 0.5; background-color: blue; visibility:hidden;">hidden</div>
</div>
<!-- iframe (within iframe) -->
<iframe id="our-iframe" src="bug199692-nested.html" style="height: 100px;"></iframe>
<input type="textbox" id="textbox" value="textbox"></input>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 199692 **/
SimpleTest.waitForExplicitFinish();
ok('elementFromPoint' in document, "document.elementFromPoint() must exist");
var elts = ["txt", "empty", "whitespace", "static", "fixed", "absolute",
"relative", "float", "textbox"];
var doc = document;
doc.pt = doc.elementFromPoint; // for shorter lines
is(doc.pt(-1, 0), null, "Negative coordinates should return null");
function testPoints() {
for(var i in elts) {
var id = elts[i];
var elt = $(id);
// The upper left corner of an element (with a moderate offset) will usually contain text,
// and the upper right corner usually won't.
var x = elt.offsetLeft, y = elt.offsetTop, w = elt.scrollWidth, h = elt.scrollHeight;
var d = 5;
is(doc.pt(x+d,y+d).id, id, "("+(x+d)+","+(y+d)+") IDs should match (upper left corner of "+id+")");
is(doc.pt(x+w-d, y+h-d).id, id, "("+(x+w-d)+","+(y+h-d)+") IDs should match (lower right corner of "+id+")");
}
// content
var c = $('content');
x = c.offsetLeft + c.clientWidth/2, y = c.offsetTop;
is(doc.pt(x,y).id, c.id, "Point to right of #txt should be #content");
// hidden
c = $('hidden');
x = c.offsetLeft; y = c.offsetTop;
is(doc.pt(x,y).id, 'hidden-wrapper', "Hit testing should bypass hidden elements.");
// iframe nested
var iframe = $("our-iframe");
x = iframe.offsetLeft, y = iframe.offsetTop;
is(doc.pt(x+20, y+20).id, "our-iframe", "Element from nested iframe returned is from calling document");
// iframe, doubly nested
is(doc.pt(x+60, y+60).id, "our-iframe", "Element from doubly nested iframe returned is from calling document");
SimpleTest.finish();
}
addLoadEvent(testPoints);
</script>
</pre>
</body>
</html>

View File

@ -46,6 +46,7 @@ include $(topsrcdir)/config/rules.mk
_TEST_FILES = \
test_bug311681.xul \
test_bug199692.xul \
$(NULL)
libs:: $(_TEST_FILES)

View File

@ -0,0 +1,95 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="/tests/SimpleTest/test.css" type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=199692
-->
<window title="Test for Bug 199692"
id="test_bug199692_xul"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<bindings xmlns="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<binding id="anon">
<content>
<xul:label id="anon-label" value="ANON"/>
</content>
</binding>
</bindings>
<body id="body" xmlns="http://www.w3.org/1999/xhtml">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=199692">Mozilla Bug 199692</a>
<vbox id="content" style="position: relative;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<xul:label id="non-anon-label" value="a textbox!:" control="textbox"/>
<xul:textbox id="textbox" multiline="true" rows="4" />
<xul:radiogroup style="outline: 2px solid orange;">
<xul:radio id="unselected-radio" label="Orange" style="outline: 2px solid red;"/>
<xul:radio id="selected-radio" label="Violet" selected="true"/>
<xul:radio id="disabled-radio" label="Yellow" disabled="true"/>
</xul:radiogroup>
<hbox id="bound" style="-moz-binding:url('#anon'); border: 2px solid green;"></hbox>
</vbox>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[
SimpleTest.waitForExplicitFinish();
// Before onload, XUL docs have no root frame.
is(document.elementFromPoint(10,10), null,
"Calls to elementFromPoint before onload should return null");
var d = 10;
function middle(e) {
return { "x": e.boxObject.x + e.boxObject.width/2,
"y": e.boxObject.y + e.boxObject.height/2 };
}
function lower_right(e) {
return { "x": e.boxObject.x + e.boxObject.width - d,
"y": e.boxObject.y + e.boxObject.height - d };
}
function upper_left(e) {
return { "x": e.boxObject.x + d,
"y": e.boxObject.y + d };
}
function scrollbar_button(e) { // a bit down from upper right
return { "x": e.boxObject.x + e.boxObject.width - d,
"y": e.boxObject.y + d + 15 };
}
function test(ptFunc, id, message) {
var pt = ptFunc($(id));
is(document.elementFromPoint(pt.x, pt.y).id, id, message);
}
function do_test() {
// Avoid hardcoding x,y pixel values, to better deal with differing default
// font sizes or other layout defaults.
test(middle, 'textbox', "Point within textbox should return textbox element");
test(lower_right, 'textbox', "Point on textbox's scrollbar should return textbox element");
test(scrollbar_button, 'textbox', "Point on textbox's scrollbar button should return textbox element");
test(middle, 'non-anon-label', "Point on label should return label");
test(upper_left, 'bound', "Point on XBL content should return element with -moz-binding style");
SimpleTest.finish();
}
$("textbox").setAttribute("value",
"lorem ipsum dolor sit amet " +
"lorem ipsum dolor sit amet " +
"lorem ipsum dolor sit amet " +
"lorem ipsum dolor sit amet " +
"lorem ipsum dolor sit amet " +
"lorem ipsum dolor sit amet " +
"lorem ipsum dolor sit amet "); // force scrollbars to appear
addLoadEvent(do_test);
]]>
</script>
</pre>
</body>
</window>

View File

@ -43,7 +43,7 @@
interface nsIBoxObject;
interface nsIDOMLocation;
[scriptable, uuid(6b395644-9a2a-4200-8247-bf2d07b8e1ea)]
[scriptable, uuid(533a8131-8d0c-4ebf-990b-7fad7cd514ee)]
interface nsIDOMNSDocument : nsISupports
{
readonly attribute DOMString characterSet;
@ -70,4 +70,25 @@ interface nsIDOMNSDocument : nsISupports
* See <http://whatwg.org/specs/web-apps/current-work/>
*/
nsIDOMNodeList getElementsByClassName(in DOMString classes);
/**
* Returns the element visible at the given point, relative to the
* upper-left-most visible point in the document.
*
* If the element at the given point belongs to another document (such as
* an iframe's subdocument), the element in the calling document's DOM
* (e.g. the iframe) is returned. If the element at the given point is
* anonymous or XBL generated content, such as a textbox's scrollbars, then
* the first non-anonymous parent element (that is, the textbox) is returned.
*
* If the specified point is outside the visible portion of the document,
* or either coordinate is negative, this method returns null.
*
* Callers from XUL documents should wait until the onload event has fired
* before calling this method.
*
* <a href="http://dev.w3.org/cvsweb/~checkout~/csswg/cssom/Overview.html?content-type=text/html;%20charset=utf-8#documentlayout-elementfrompoint">preliminary spec</a>
*/
nsIDOMElement elementFromPoint(in long x, in long y);
};

View File

@ -204,6 +204,11 @@ public:
*/
void SetPaintAllFrames() { mPaintAllFrames = PR_TRUE; }
PRBool GetPaintAllFrames() { return mPaintAllFrames; }
/**
* Allows callers to selectively override the regular paint suppression checks,
* so that methods like GetFrameForPoint work when painting is suppressed.
*/
void IgnorePaintSuppression() { mIsBackgroundOnly = PR_FALSE; }
/**
* Display the caret if needed.
*/

View File

@ -745,12 +745,15 @@ static PRBool gDumpRepaintRegionForCopy = PR_FALSE;
#endif
nsIFrame*
nsLayoutUtils::GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt)
nsLayoutUtils::GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt,
PRBool aShouldIgnoreSuppression)
{
nsDisplayListBuilder builder(aFrame, PR_TRUE, PR_FALSE);
nsDisplayList list;
nsRect target(aPt, nsSize(1, 1));
if (aShouldIgnoreSuppression)
builder.IgnorePaintSuppression();
builder.EnterPresShell(aFrame, target);
nsresult rv =

View File

@ -378,8 +378,11 @@ public:
* frame under the point aPt that receives a mouse event at that location,
* or nsnull if there is no such frame.
* @param aPt the point, relative to the frame origin
* @param aShouldIgnoreSuppression a boolean to control if the display
* list builder should ignore paint suppression or not
*/
static nsIFrame* GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt);
static nsIFrame* GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt,
PRBool aShouldIgnoreSuppression = PR_FALSE);
/**
* Given aFrame, the root frame of a stacking context, paint it and its