Bug 656379 part 3. Set :hover and :active state for labeled elements when their label has that state. r=dbaron

--HG--
rename : content/events/test/test_bug426082.html => content/events/test/test_bug656379-1.html
This commit is contained in:
Boris Zbarsky 2011-05-24 20:18:40 -04:00
parent 20ff2430d9
commit 81f0e8f50c
6 changed files with 279 additions and 29 deletions

View File

@ -153,6 +153,7 @@
#include "nsICommandParams.h"
#include "mozilla/Services.h"
#include "mozAutoDocUpdate.h"
#include "nsHTMLLabelElement.h"
#ifdef XP_MACOSX
#import <ApplicationServices/ApplicationServices.h>
@ -4227,35 +4228,26 @@ nsEventStateManager::GetEventTargetContent(nsEvent* aEvent)
return content;
}
static already_AddRefed<nsIContent>
GetLabelTarget(nsIContent* aLabel)
static Element*
GetLabelTarget(nsIContent* aPossibleLabel)
{
nsCOMPtr<nsIDOMHTMLLabelElement> label = do_QueryInterface(aLabel);
nsHTMLLabelElement* label = nsHTMLLabelElement::FromContent(aPossibleLabel);
if (!label)
return nsnull;
nsCOMPtr<nsIDOMHTMLElement> target;
label->GetControl(getter_AddRefs(target));
nsIContent* targetContent = nsnull;
if (target) {
CallQueryInterface(target, &targetContent);
}
return targetContent;
return label->GetLabeledElement();
}
static bool
IsAncestorOf(nsIContent* aPossibleAncestor, nsIContent* aPossibleDescendant,
PRBool aFollowLabels)
IsAncestorOf(nsIContent* aPossibleAncestor, nsIContent* aPossibleDescendant)
{
for (; aPossibleDescendant; aPossibleDescendant = aPossibleDescendant->GetParent()) {
if (aPossibleAncestor == aPossibleDescendant)
return true;
if (aFollowLabels) {
nsCOMPtr<nsIContent> labelTarget = GetLabelTarget(aPossibleDescendant);
if (labelTarget == aPossibleAncestor)
return true;
}
Element* labelTarget = GetLabelTarget(aPossibleDescendant);
if (labelTarget == aPossibleAncestor)
return true;
}
return false;
}
@ -4273,14 +4265,14 @@ ShouldShowFocusRing(nsIContent* aContent)
}
nsEventStates
nsEventStateManager::GetContentState(nsIContent *aContent, PRBool aFollowLabels)
nsEventStateManager::GetContentState(nsIContent *aContent)
{
nsEventStates state = aContent->IntrinsicState();
if (IsAncestorOf(aContent, mActiveContent, aFollowLabels)) {
if (IsAncestorOf(aContent, mActiveContent)) {
state |= NS_EVENT_STATE_ACTIVE;
}
if (IsAncestorOf(aContent, mHoverContent, aFollowLabels)) {
if (IsAncestorOf(aContent, mHoverContent)) {
state |= NS_EVENT_STATE_HOVER;
}
@ -4352,7 +4344,7 @@ NotifyAncestors(nsIDocument* aDocument, nsIContent* aStartNode,
{
while (aStartNode && aStartNode != aStopBefore) {
aDocument->ContentStateChanged(aStartNode, aState);
nsCOMPtr<nsIContent> labelTarget = GetLabelTarget(aStartNode);
Element* labelTarget = GetLabelTarget(aStartNode);
if (labelTarget) {
aDocument->ContentStateChanged(labelTarget, aState);
}

View File

@ -122,15 +122,9 @@ public:
/**
* Returns the content state of aContent.
* @param aContent The control whose state is requested.
* @param aFollowLabels Whether to reflect a label's content state on its
* associated control. If aFollowLabels is true and
* aContent is a control which has a label that has the
* hover or active content state set, GetContentState
* will pretend that those states are also set on aContent.
* @return The content state.
*/
virtual nsEventStates GetContentState(nsIContent *aContent,
PRBool aFollowLabels = PR_FALSE);
virtual nsEventStates GetContentState(nsIContent *aContent);
/**
* Notify that the given NS_EVENT_STATE_* bit has changed for this content.

View File

@ -104,6 +104,8 @@ _TEST_FILES = \
test_bug641477.html \
test_bug648573.html \
test_bug615597.html \
test_bug656379-1.html \
test_bug656379-2.html \
test_bug656954.html \
$(NULL)

View File

@ -0,0 +1,199 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=656379
-->
<head>
<title>Test for Bug 656379</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
canvas {
display: none;
}
input[type=button] {
-moz-appearance: none;
padding: 0;
border: none;
color: black;
background: white;
}
input[type=button]::-moz-focus-inner { border: none; }
/* Make sure that normal, focused, hover+active, focused+hover+active
buttons all have different styles so that the test keeps moving along. */
input[type=button]:hover:active {
background: red;
}
input[type=button]:focus {
background: green;
}
input[type=button]:focus:hover:active {
background: purple;
}
</style>
</head>
<body onload="runTests()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=426082">Mozilla Bug 426082</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript;version=1.8">
/** Test for Bug 426082 **/
SimpleTest.waitForExplicitFinish();
var normalButtonCanvas, pressedButtonCanvas, normalFocusedButtonCanvas,
pressedFocusedButtonCanvas, currentSnapshot, button, label, outside;
function runTests() {
normalButtonCanvas = $("normalButtonCanvas");
pressedButtonCanvas = $("pressedButtonCanvas");
normalFocusedButtonCanvas = $("normalFocusedButtonCanvas");
pressedFocusedButtonCanvas = $("pressedFocusedButtonCanvas");
currentSnapshot = $("currentSnapshot");
button = $("button");
label = $("label");
outside = $("outside");
SimpleTest.executeSoon(executeTests);
}
function isRectContainedInRectFromRegion(rect, region) {
return Array.some(region, function (r) {
return rect.left >= r.left &&
rect.top >= r.top &&
rect.right <= r.right &&
rect.bottom <= r.bottom;
});
}
function paintListener(e) {
if (isRectContainedInRectFromRegion(buttonRect(), e.clientRects)) {
gNeedsPaint = false;
takeSnapshot(currentSnapshot);
}
}
var gNeedsPaint = false;
function executeTests() {
var testYielder = tests();
function execNext() {
try {
if (!gNeedsPaint) {
testYielder.next();
button.getBoundingClientRect(); // Flush.
gNeedsPaint = true;
}
SimpleTest.executeSoon(execNext);
} catch (e) {}
}
execNext();
}
function tests() {
window.addEventListener("MozAfterPaint", paintListener, false);
takeSnapshot(normalButtonCanvas);
// Press the button.
sendMouseEvent("mousemove", button);
sendMouseEvent("mousedown", button);
yield;
takeSnapshot(pressedFocusedButtonCanvas);
compareSnapshots_(normalButtonCanvas, pressedFocusedButtonCanvas, false, "Pressed focused buttons should look different from normal buttons.");
// Release.
sendMouseEvent("mouseup", button);
yield;
// make sure the button is focused as this doesn't happen on click on Mac
button.focus();
takeSnapshot(normalFocusedButtonCanvas);
compareSnapshots_(normalFocusedButtonCanvas, pressedFocusedButtonCanvas, false, "Pressed focused buttons should look different from normal focused buttons.");
// Unfocus the button.
sendMouseEvent("mousedown", outside);
sendMouseEvent("mouseup", outside);
yield;
// Press the label.
sendMouseEvent("mousemove", label);
sendMouseEvent("mousedown", label);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, false, "Pressing the label should have pressed the button.");
takeSnapshot(pressedButtonCanvas);
// Move the mouse down from the label.
sendMouseEvent("mousemove", outside);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Moving the mouse down from the label should have unpressed the button.");
// ... and up again.
sendMouseEvent("mousemove", label);
yield;
compareSnapshots_(pressedButtonCanvas, currentSnapshot, true, "Moving the mouse back on top of the label should have pressed the button.");
// Release.
sendMouseEvent("mouseup", label);
yield;
compareSnapshots_(normalFocusedButtonCanvas, currentSnapshot, true, "Releasing the mouse over the label should have unpressed (and focused) the button.");
// Press the label and remove it.
sendMouseEvent("mousemove", label);
sendMouseEvent("mousedown", label);
yield;
label.parentNode.removeChild(label);
yield;
compareSnapshots_(normalButtonCanvas, currentSnapshot, true, "Removing the label should have unpressed the button.");
sendMouseEvent("mouseup", label);
window.removeEventListener("MozAfterPaint", paintListener, false);
SimpleTest.finish();
}
function sendMouseEvent(t, elem) {
var r = elem.getBoundingClientRect();
synthesizeMouse(elem, r.width / 2, r.height / 2, {type: t});
}
function compareSnapshots_(c1, c2, shouldBeIdentical, msg) {
var [correct, c1url, c2url] = compareSnapshots(c1, c2, shouldBeIdentical);
if (correct) {
if (shouldBeIdentical) {
ok(true, msg + " - expected " + c1url);
} else {
ok(true, msg + " - got " + c1url + " and " + c2url);
}
} else {
if (shouldBeIdentical) {
ok(false, msg + " - expected " + c1url + " but got " + c2url);
} else {
ok(false, msg + " - expected something other than " + c1url);
}
}
}
function takeSnapshot(canvas) {
var r = buttonRect();
var ctx = canvas.getContext("2d");
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
canvas.width = r.width + 4;
canvas.height = r.height + 4;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawWindow(window, r.left - 2, r.top - 2, r.width + 4, r.height + 4, "#FFF");
}
function buttonRect() {
return button.getBoundingClientRect();
}
</script>
</pre>
<p><input type="button" value="Button" id="button"></p>
<p><label for="button" id="label">Label</label></p>
<p id="outside">Something under the label</p>
<canvas id="normalButtonCanvas"></canvas>
<canvas id="pressedButtonCanvas"></canvas>
<canvas id="normalFocusedButtonCanvas"></canvas>
<canvas id="pressedFocusedButtonCanvas"></canvas>
<canvas id="currentSnapshot"></canvas>
</body>
</html>

View File

@ -0,0 +1,63 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=656379
-->
<head>
<title>Test for Bug 656379</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
button:hover { color: green; }
</style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=656379">Mozilla Bug 656379</a>
<p id="display">
<label for="button1" id="label1">Label 1</label>
<button id="button1">Button 1</button>
<label><span id="label2">Label 2</span><button id="button2">Button 2</button></label>
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript;version=1.8">
/** Test for Bug 656379 **/
SimpleTest.waitForExplicitFinish();
function tests() {
synthesizeMouseAtCenter($("label1"), { type: "mousemove" });
yield;
is($("button1").mozMatchesSelector(":hover"), true,
"Button 1 should be hovered");
is($("button2").mozMatchesSelector(":hover"), false,
"Button 2 should not be hovered");
synthesizeMouseAtCenter($("label2"), { type: "mousemove" });
yield;
is($("button1").mozMatchesSelector(":hover"), false,
"Button 1 should not be hovered");
is($("button2").mozMatchesSelector(":hover"), true,
"Button 2 should be hovered");
SimpleTest.finish();
}
function executeTests() {
var testYielder = tests();
function execNext() {
try {
testYielder.next();
SimpleTest.executeSoon(execNext);
} catch(e) {}
}
execNext();
}
SimpleTest.waitForFocus(executeTests);
</script>
</pre>
</body>
</html>

View File

@ -93,7 +93,7 @@ nsNativeTheme::GetContentState(nsIFrame* aFrame, PRUint8 aWidgetType)
return nsEventStates();
nsEventStateManager* esm = shell->GetPresContext()->EventStateManager();
nsEventStates flags = esm->GetContentState(aFrame->GetContent(), PR_TRUE);
nsEventStates flags = esm->GetContentState(aFrame->GetContent());
if (isXULCheckboxRadio && aWidgetType == NS_THEME_RADIO) {
if (IsFocused(aFrame))