2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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-05-24 17:18:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of HTML <label> elements.
|
|
|
|
*/
|
2012-12-21 06:06:50 -08:00
|
|
|
#include "HTMLLabelElement.h"
|
2014-03-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#include "mozilla/MouseEvents.h"
|
2012-12-21 06:07:28 -08:00
|
|
|
#include "mozilla/dom/HTMLLabelElementBinding.h"
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
#include "nsFocusManager.h"
|
2013-08-23 10:56:20 -07:00
|
|
|
#include "nsIDOMMouseEvent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// construction, destruction
|
|
|
|
|
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Label)
|
|
|
|
|
2012-12-21 06:06:50 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-21 06:06:50 -08:00
|
|
|
HTMLLabelElement::~HTMLLabelElement()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-21 06:07:28 -08:00
|
|
|
JSObject*
|
2014-04-08 15:27:17 -07:00
|
|
|
HTMLLabelElement::WrapNode(JSContext *aCx)
|
2012-12-21 06:07:28 -08:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return HTMLLabelElementBinding::Wrap(aCx, this);
|
2012-12-21 06:07:28 -08:00
|
|
|
}
|
|
|
|
|
2013-06-14 19:47:01 -07:00
|
|
|
// nsISupports
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(HTMLLabelElement, nsGenericHTMLFormElement,
|
|
|
|
nsIDOMHTMLLabelElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-30 20:32:35 -07:00
|
|
|
// nsIDOMHTMLLabelElement
|
|
|
|
|
2012-12-21 06:06:50 -08:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLLabelElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-30 20:32:35 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLLabelElement::GetForm(nsIDOMHTMLFormElement** aForm)
|
|
|
|
{
|
|
|
|
return nsGenericHTMLFormElement::GetForm(aForm);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLLabelElement::GetControl(nsIDOMHTMLElement** aElement)
|
|
|
|
{
|
2013-08-07 13:23:08 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLElement> element = do_QueryObject(GetLabeledElement());
|
2013-07-30 20:32:35 -07:00
|
|
|
element.forget(aElement);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLLabelElement::SetHtmlFor(const nsAString& aHtmlFor)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
SetHtmlFor(aHtmlFor, rv);
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLLabelElement::GetHtmlFor(nsAString& aHtmlFor)
|
|
|
|
{
|
|
|
|
nsString htmlFor;
|
|
|
|
GetHtmlFor(htmlFor);
|
|
|
|
aHtmlFor = htmlFor;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-06 00:19:52 -07:00
|
|
|
void
|
2012-12-21 06:06:50 -08:00
|
|
|
HTMLLabelElement::Focus(ErrorResult& aError)
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
{
|
|
|
|
// retarget the focus method at the for content
|
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm) {
|
2013-08-07 13:23:08 -07:00
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryObject(GetLabeledElement());
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
if (elem)
|
|
|
|
fm->SetFocus(elem, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2015-01-20 12:39:28 -08:00
|
|
|
InInteractiveHTMLContent(nsIContent* aContent, nsIContent* aStop)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2015-01-20 12:39:28 -08:00
|
|
|
nsIContent* content = aContent;
|
|
|
|
while (content && content != aStop) {
|
|
|
|
if (content->IsElement() &&
|
|
|
|
content->AsElement()->IsInteractiveHTMLContent()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
content = content->GetParent();
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-17 21:48:20 -07:00
|
|
|
HTMLLabelElement::PostHandleEvent(EventChainPostVisitor& aVisitor)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-28 02:03:19 -07:00
|
|
|
WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mHandlingEvent ||
|
2013-10-28 02:03:19 -07:00
|
|
|
(!(mouseEvent && mouseEvent->IsLeftClickEvent()) &&
|
2009-03-23 17:55:29 -07:00
|
|
|
aVisitor.mEvent->message != NS_MOUSE_BUTTON_DOWN) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault ||
|
2011-07-19 09:19:32 -07:00
|
|
|
!aVisitor.mPresContext ||
|
|
|
|
// Don't handle the event if it's already been handled by another label
|
2012-12-15 17:26:04 -08:00
|
|
|
aVisitor.mEvent->mFlags.mMultipleActionsPrevented) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-20 12:39:28 -08:00
|
|
|
nsCOMPtr<nsIContent> target = do_QueryInterface(aVisitor.mEvent->target);
|
|
|
|
if (InInteractiveHTMLContent(target, this)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-24 17:18:31 -07:00
|
|
|
// Strong ref because event dispatch is going to happen.
|
|
|
|
nsRefPtr<Element> content = GetLabeledElement();
|
2010-05-19 19:28:10 -07:00
|
|
|
|
2015-01-20 12:39:28 -08:00
|
|
|
if (content) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mHandlingEvent = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (aVisitor.mEvent->message) {
|
2009-03-23 17:55:29 -07:00
|
|
|
case NS_MOUSE_BUTTON_DOWN:
|
2013-10-28 02:03:19 -07:00
|
|
|
if (mouseEvent->button == WidgetMouseEvent::eLeftButton) {
|
2009-03-23 17:55:29 -07:00
|
|
|
// We reset the mouse-down point on every event because there is
|
|
|
|
// no guarantee we will reach the NS_MOUSE_CLICK code below.
|
2013-08-02 00:05:16 -07:00
|
|
|
LayoutDeviceIntPoint* curPoint =
|
2013-10-28 02:03:19 -07:00
|
|
|
new LayoutDeviceIntPoint(mouseEvent->refPoint);
|
2009-03-23 17:55:29 -07:00
|
|
|
SetProperty(nsGkAtoms::labelMouseDownPtProperty,
|
2013-08-02 00:05:16 -07:00
|
|
|
static_cast<void*>(curPoint),
|
2014-03-25 06:25:47 -07:00
|
|
|
nsINode::DeleteProperty<LayoutDeviceIntPoint>);
|
2009-03-23 17:55:29 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_MOUSE_CLICK:
|
2013-10-28 02:03:19 -07:00
|
|
|
if (mouseEvent->IsLeftClickEvent()) {
|
2013-08-02 00:05:16 -07:00
|
|
|
LayoutDeviceIntPoint* mouseDownPoint =
|
|
|
|
static_cast<LayoutDeviceIntPoint*>(
|
|
|
|
GetProperty(nsGkAtoms::labelMouseDownPtProperty));
|
2009-03-23 17:55:29 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool dragSelect = false;
|
2009-03-23 17:55:29 -07:00
|
|
|
if (mouseDownPoint) {
|
2013-08-02 00:05:16 -07:00
|
|
|
LayoutDeviceIntPoint dragDistance = *mouseDownPoint;
|
2009-03-23 17:55:29 -07:00
|
|
|
DeleteProperty(nsGkAtoms::labelMouseDownPtProperty);
|
|
|
|
|
2013-10-22 01:55:20 -07:00
|
|
|
dragDistance -= mouseEvent->refPoint;
|
2009-03-23 17:55:29 -07:00
|
|
|
const int CLICK_DISTANCE = 2;
|
|
|
|
dragSelect = dragDistance.x > CLICK_DISTANCE ||
|
|
|
|
dragDistance.x < -CLICK_DISTANCE ||
|
|
|
|
dragDistance.y > CLICK_DISTANCE ||
|
|
|
|
dragDistance.y < -CLICK_DISTANCE;
|
|
|
|
}
|
|
|
|
// Don't click the for-content if we did drag-select text or if we
|
2013-06-14 19:47:01 -07:00
|
|
|
// have a kbd modifier (which adjusts a selection).
|
2013-10-22 01:55:20 -07:00
|
|
|
if (dragSelect || mouseEvent->IsShift() || mouseEvent->IsControl() ||
|
|
|
|
mouseEvent->IsAlt() || mouseEvent->IsMeta()) {
|
2009-03-23 17:55:29 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-06-14 19:47:01 -07:00
|
|
|
// Only set focus on the first click of multiple clicks to prevent
|
|
|
|
// to prevent immediate de-focus.
|
2013-10-22 01:55:20 -07:00
|
|
|
if (mouseEvent->clickCount <= 1) {
|
2013-06-14 19:47:01 -07:00
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm) {
|
|
|
|
// Use FLAG_BYMOVEFOCUS here so that the label is scrolled to.
|
|
|
|
// Also, within HTMLInputElement::PostHandleEvent, inputs will
|
|
|
|
// be selected only when focused via a key or when the navigation
|
|
|
|
// flag is used and we want to select the text on label clicks as
|
|
|
|
// well.
|
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(content);
|
|
|
|
fm->SetFocus(elem, nsIFocusManager::FLAG_BYMOVEFOCUS);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// Dispatch a new click event to |content|
|
|
|
|
// (For compatibility with IE, we do only left click. If
|
|
|
|
// we wanted to interpret the HTML spec very narrowly, we
|
|
|
|
// would do nothing. If we wanted to do something
|
|
|
|
// sensible, we might send more events through like
|
|
|
|
// this.) See bug 7554, bug 49897, and bug 96813.
|
|
|
|
nsEventStatus status = aVisitor.mEventStatus;
|
|
|
|
// Ok to use aVisitor.mEvent as parameter because DispatchClickEvent
|
|
|
|
// will actually create a new event.
|
2013-09-13 19:39:41 -07:00
|
|
|
EventFlags eventFlags;
|
2012-12-15 17:26:04 -08:00
|
|
|
eventFlags.mMultipleActionsPrevented = true;
|
2013-10-22 01:55:20 -07:00
|
|
|
DispatchClickEvent(aVisitor.mPresContext, mouseEvent,
|
2012-12-15 17:26:04 -08:00
|
|
|
content, false, &eventFlags, &status);
|
2007-03-22 10:30:00 -07:00
|
|
|
// Do we care about the status this returned? I don't think we do...
|
2011-07-19 09:19:32 -07:00
|
|
|
// Don't run another <label> off of this click
|
2013-10-22 01:55:20 -07:00
|
|
|
mouseEvent->mFlags.mMultipleActionsPrevented = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mHandlingEvent = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-21 06:06:50 -08:00
|
|
|
HTMLLabelElement::Reset()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-21 06:06:50 -08:00
|
|
|
HTMLLabelElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-21 06:06:50 -08:00
|
|
|
HTMLLabelElement::PerformAccesskey(bool aKeyCausesActivation,
|
|
|
|
bool aIsTrustedEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!aKeyCausesActivation) {
|
2011-05-24 17:18:31 -07:00
|
|
|
nsRefPtr<Element> element = GetLabeledElement();
|
|
|
|
if (element)
|
|
|
|
element->PerformAccesskey(aKeyCausesActivation, aIsTrustedEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2014-08-22 13:11:27 -07:00
|
|
|
nsPresContext *presContext = GetPresContext(eForUncomposedDoc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!presContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Click on it if the users prefs indicate to do so.
|
2013-10-01 23:38:27 -07:00
|
|
|
WidgetMouseEvent event(aIsTrustedEvent, NS_MOUSE_CLICK,
|
|
|
|
nullptr, WidgetMouseEvent::eReal);
|
2011-08-26 00:43:56 -07:00
|
|
|
event.inputSource = nsIDOMMouseEvent::MOZ_SOURCE_KEYBOARD;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoPopupStatePusher popupStatePusher(aIsTrustedEvent ?
|
|
|
|
openAllowed : openAbused);
|
|
|
|
|
2014-03-17 21:48:21 -07:00
|
|
|
EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
|
|
|
|
&event);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 06:07:28 -08:00
|
|
|
nsGenericHTMLElement*
|
|
|
|
HTMLLabelElement::GetLabeledElement() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString elementId;
|
2010-05-19 19:28:10 -07:00
|
|
|
|
|
|
|
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::_for, elementId)) {
|
|
|
|
// No @for, so we are a label for our first form control element.
|
|
|
|
// Do a depth-first traversal to look for the first form control element.
|
2012-06-10 16:44:50 -07:00
|
|
|
return GetFirstLabelableDescendant();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-19 19:28:10 -07:00
|
|
|
|
|
|
|
// We have a @for. The id has to be linked to an element in the same document
|
|
|
|
// and this element should be a labelable form control.
|
2014-10-02 12:07:24 -07:00
|
|
|
//XXXsmaug It is unclear how this should work in case the element is in
|
|
|
|
// Shadow DOM.
|
|
|
|
// See https://www.w3.org/Bugs/Public/show_bug.cgi?id=26365.
|
|
|
|
nsIDocument* doc = GetUncomposedDoc();
|
2010-05-19 19:28:10 -07:00
|
|
|
if (!doc) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-19 19:28:10 -07:00
|
|
|
}
|
|
|
|
|
2011-05-24 17:18:31 -07:00
|
|
|
Element* element = doc->GetElementById(elementId);
|
2012-06-10 16:44:50 -07:00
|
|
|
if (element && element->IsLabelable()) {
|
2012-12-21 06:07:28 -08:00
|
|
|
return static_cast<nsGenericHTMLElement*>(element);
|
2010-05-19 19:28:10 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-12-21 06:07:28 -08:00
|
|
|
nsGenericHTMLElement*
|
|
|
|
HTMLLabelElement::GetFirstLabelableDescendant() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-10 16:44:50 -07:00
|
|
|
for (nsIContent* cur = nsINode::GetFirstChild(); cur;
|
2011-05-24 17:18:31 -07:00
|
|
|
cur = cur->GetNextNode(this)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
Element* element = cur->IsElement() ? cur->AsElement() : nullptr;
|
2012-06-10 16:44:50 -07:00
|
|
|
if (element && element->IsLabelable()) {
|
2012-12-21 06:07:28 -08:00
|
|
|
return static_cast<nsGenericHTMLElement*>(element);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-19 19:28:10 -07:00
|
|
|
|
2012-12-21 06:06:50 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|