2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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-25 04:18:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of HTML <label> elements.
|
|
|
|
*/
|
2012-12-21 18:06:50 +04:00
|
|
|
#include "HTMLLabelElement.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#include "mozilla/MouseEvents.h"
|
2012-12-21 18:07:28 +04:00
|
|
|
#include "mozilla/dom/HTMLLabelElementBinding.h"
|
2018-03-20 07:16:06 +03:00
|
|
|
#include "mozilla/dom/MouseEventBinding.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 22:00:39 +04:00
|
|
|
#include "nsFocusManager.h"
|
2017-12-31 22:57:32 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2017-06-16 03:04:00 +03:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
1998-09-03 05:03:33 +04:00
|
|
|
|
1998-10-23 03:00:37 +04:00
|
|
|
// construction, destruction
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2004-05-19 00:58:12 +04:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Label)
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2012-12-21 18:06:50 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
1998-09-03 05:03:33 +04:00
|
|
|
|
2012-12-21 18:06:50 +04:00
|
|
|
HTMLLabelElement::~HTMLLabelElement() {}
|
1998-09-03 05:03:33 +04:00
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* HTMLLabelElement::WrapNode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return HTMLLabelElement_Binding::Wrap(aCx, this, aGivenProto);
|
2012-12-21 18:07:28 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 07:32:35 +04:00
|
|
|
// nsIDOMHTMLLabelElement
|
|
|
|
|
2012-12-21 18:06:50 +04:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLLabelElement)
|
1999-07-28 09:26:55 +04:00
|
|
|
|
2016-05-27 02:39:03 +03:00
|
|
|
HTMLFormElement* HTMLLabelElement::GetForm() const {
|
|
|
|
nsGenericHTMLElement* control = GetControl();
|
|
|
|
if (!control) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not all labeled things have a form association. Stick to the ones that do.
|
|
|
|
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(control);
|
|
|
|
if (!formControl) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<HTMLFormElement*>(formControl->GetFormElement());
|
|
|
|
}
|
|
|
|
|
2012-12-21 18:06:50 +04:00
|
|
|
void 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 22:00:39 +04:00
|
|
|
// retarget the focus method at the for content
|
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm) {
|
2018-04-26 17:37:46 +03:00
|
|
|
RefPtr<Element> elem = GetLabeledElement();
|
|
|
|
if (elem) {
|
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 22:00:39 +04:00
|
|
|
fm->SetFocus(elem, 0);
|
2018-04-26 17:37:46 +03:00
|
|
|
}
|
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 22:00:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:39:28 +03:00
|
|
|
static bool InInteractiveHTMLContent(nsIContent* aContent, nsIContent* aStop) {
|
|
|
|
nsIContent* content = aContent;
|
|
|
|
while (content && content != aStop) {
|
|
|
|
if (content->IsElement() &&
|
2015-03-17 23:42:14 +03:00
|
|
|
content->AsElement()->IsInteractiveHTMLContent(true)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-05-23 04:23:12 +04:00
|
|
|
}
|
2003-07-29 02:32:31 +04:00
|
|
|
content = content->GetParent();
|
2002-05-23 04:23:12 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-05-23 04:23:12 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 08:48:20 +04:00
|
|
|
nsresult HTMLLabelElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
|
2013-10-28 13:03:19 +04:00
|
|
|
WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
|
2002-05-22 04:14:51 +04:00
|
|
|
if (mHandlingEvent ||
|
2013-10-28 13:03:19 +04:00
|
|
|
(!(mouseEvent && mouseEvent->IsLeftClickEvent()) &&
|
2015-08-29 02:58:30 +03:00
|
|
|
aVisitor.mEvent->mMessage != eMouseDown) ||
|
2006-03-07 20:08:51 +03:00
|
|
|
aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault ||
|
2011-07-19 20:19:32 +04:00
|
|
|
!aVisitor.mPresContext ||
|
|
|
|
// Don't handle the event if it's already been handled by another label
|
2012-12-16 05:26:04 +04:00
|
|
|
aVisitor.mEvent->mFlags.mMultipleActionsPrevented) {
|
2002-05-22 04:14:51 +04:00
|
|
|
return NS_OK;
|
2006-03-07 20:08:51 +03:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2016-04-18 18:46:00 +03:00
|
|
|
nsCOMPtr<nsIContent> target = do_QueryInterface(aVisitor.mEvent->mTarget);
|
2015-01-20 23:39:28 +03:00
|
|
|
if (InInteractiveHTMLContent(target, this)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-25 04:18:31 +04:00
|
|
|
// Strong ref because event dispatch is going to happen.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Element> content = GetLabeledElement();
|
2010-05-20 06:28:10 +04:00
|
|
|
|
2015-01-20 23:39:28 +03:00
|
|
|
if (content) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mHandlingEvent = true;
|
2015-08-22 04:34:51 +03:00
|
|
|
switch (aVisitor.mEvent->mMessage) {
|
2015-08-29 02:58:30 +03:00
|
|
|
case eMouseDown:
|
2013-10-28 13:03:19 +04:00
|
|
|
if (mouseEvent->button == WidgetMouseEvent::eLeftButton) {
|
2009-03-24 03:55:29 +03:00
|
|
|
// We reset the mouse-down point on every event because there is
|
2015-08-29 02:58:32 +03:00
|
|
|
// no guarantee we will reach the eMouseClick code below.
|
2013-08-02 11:05:16 +04:00
|
|
|
LayoutDeviceIntPoint* curPoint =
|
2016-04-18 17:09:02 +03:00
|
|
|
new LayoutDeviceIntPoint(mouseEvent->mRefPoint);
|
2009-03-24 03:55:29 +03:00
|
|
|
SetProperty(nsGkAtoms::labelMouseDownPtProperty,
|
2013-08-02 11:05:16 +04:00
|
|
|
static_cast<void*>(curPoint),
|
2014-03-25 17:25:47 +04:00
|
|
|
nsINode::DeleteProperty<LayoutDeviceIntPoint>);
|
2009-03-24 03:55:29 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-08-29 02:58:32 +03:00
|
|
|
case eMouseClick:
|
2013-10-28 13:03:19 +04:00
|
|
|
if (mouseEvent->IsLeftClickEvent()) {
|
2013-08-02 11:05:16 +04:00
|
|
|
LayoutDeviceIntPoint* mouseDownPoint =
|
|
|
|
static_cast<LayoutDeviceIntPoint*>(
|
|
|
|
GetProperty(nsGkAtoms::labelMouseDownPtProperty));
|
2009-03-24 03:55:29 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool dragSelect = false;
|
2009-03-24 03:55:29 +03:00
|
|
|
if (mouseDownPoint) {
|
2013-08-02 11:05:16 +04:00
|
|
|
LayoutDeviceIntPoint dragDistance = *mouseDownPoint;
|
2009-03-24 03:55:29 +03:00
|
|
|
DeleteProperty(nsGkAtoms::labelMouseDownPtProperty);
|
|
|
|
|
2016-04-18 17:09:02 +03:00
|
|
|
dragDistance -= mouseEvent->mRefPoint;
|
2009-03-24 03:55:29 +03: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-15 06:47:01 +04:00
|
|
|
// have a kbd modifier (which adjusts a selection).
|
2013-10-22 12:55:20 +04:00
|
|
|
if (dragSelect || mouseEvent->IsShift() || mouseEvent->IsControl() ||
|
|
|
|
mouseEvent->IsAlt() || mouseEvent->IsMeta()) {
|
2009-03-24 03:55:29 +03:00
|
|
|
break;
|
|
|
|
}
|
2013-06-15 06:47:01 +04:00
|
|
|
// Only set focus on the first click of multiple clicks to prevent
|
|
|
|
// to prevent immediate de-focus.
|
2016-05-10 17:29:14 +03:00
|
|
|
if (mouseEvent->mClickCount <= 1) {
|
2013-06-15 06:47:01 +04: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.
|
2015-07-08 07:46:48 +03:00
|
|
|
// If the label has been clicked by the user, we also want to
|
|
|
|
// pass FLAG_BYMOUSE so that we get correct focus ring behavior,
|
|
|
|
// but we don't want to pass FLAG_BYMOUSE if this click event was
|
|
|
|
// caused by the user pressing an accesskey.
|
2018-06-26 00:20:54 +03:00
|
|
|
bool byMouse = (mouseEvent->inputSource !=
|
|
|
|
MouseEvent_Binding::MOZ_SOURCE_KEYBOARD);
|
|
|
|
bool byTouch = (mouseEvent->inputSource ==
|
|
|
|
MouseEvent_Binding::MOZ_SOURCE_TOUCH);
|
2018-04-26 17:37:46 +03:00
|
|
|
fm->SetFocus(content,
|
|
|
|
nsIFocusManager::FLAG_BYMOVEFOCUS |
|
|
|
|
(byMouse ? nsIFocusManager::FLAG_BYMOUSE : 0) |
|
|
|
|
(byTouch ? nsIFocusManager::FLAG_BYTOUCH : 0));
|
2013-06-15 06:47:01 +04:00
|
|
|
}
|
2005-01-20 19:59:42 +03: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.
|
2006-03-07 20:08:51 +03:00
|
|
|
nsEventStatus status = aVisitor.mEventStatus;
|
|
|
|
// Ok to use aVisitor.mEvent as parameter because DispatchClickEvent
|
|
|
|
// will actually create a new event.
|
2013-09-14 06:39:41 +04:00
|
|
|
EventFlags eventFlags;
|
2012-12-16 05:26:04 +04:00
|
|
|
eventFlags.mMultipleActionsPrevented = true;
|
2013-10-22 12:55:20 +04:00
|
|
|
DispatchClickEvent(aVisitor.mPresContext, mouseEvent, content, false,
|
2012-12-16 05:26:04 +04:00
|
|
|
&eventFlags, &status);
|
2005-01-20 19:59:42 +03:00
|
|
|
// Do we care about the status this returned? I don't think we do...
|
2011-07-19 20:19:32 +04:00
|
|
|
// Don't run another <label> off of this click
|
2013-10-22 12:55:20 +04:00
|
|
|
mouseEvent->mFlags.mMultipleActionsPrevented = true;
|
2004-11-04 04:36:18 +03:00
|
|
|
}
|
2002-05-22 04:14:51 +04:00
|
|
|
break;
|
2015-08-26 15:56:59 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2002-05-22 04:14:51 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mHandlingEvent = false;
|
2002-05-22 04:14:51 +04:00
|
|
|
}
|
2006-03-07 20:08:51 +03:00
|
|
|
return NS_OK;
|
1999-11-03 02:38:17 +03:00
|
|
|
}
|
1999-10-07 04:35:04 +04:00
|
|
|
|
2012-12-21 18:06:50 +04:00
|
|
|
bool HTMLLabelElement::PerformAccesskey(bool aKeyCausesActivation,
|
|
|
|
bool aIsTrustedEvent) {
|
2007-02-13 18:02:57 +03:00
|
|
|
if (!aKeyCausesActivation) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Element> element = GetLabeledElement();
|
2015-09-18 15:18:42 +03:00
|
|
|
if (element) {
|
|
|
|
return element->PerformAccesskey(aKeyCausesActivation, aIsTrustedEvent);
|
|
|
|
}
|
2007-02-13 18:02:57 +03:00
|
|
|
} else {
|
2014-08-23 00:11:27 +04:00
|
|
|
nsPresContext* presContext = GetPresContext(eForUncomposedDoc);
|
2015-09-18 15:18:42 +03:00
|
|
|
if (!presContext) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-13 18:02:57 +03:00
|
|
|
|
|
|
|
// Click on it if the users prefs indicate to do so.
|
2015-08-29 02:58:32 +03:00
|
|
|
WidgetMouseEvent event(aIsTrustedEvent, eMouseClick, nullptr,
|
2013-10-02 10:38:27 +04:00
|
|
|
WidgetMouseEvent::eReal);
|
2018-06-26 00:20:54 +03:00
|
|
|
event.inputSource = MouseEvent_Binding::MOZ_SOURCE_KEYBOARD;
|
2007-02-13 18:02:57 +03:00
|
|
|
|
2018-12-16 12:21:16 +03:00
|
|
|
nsAutoPopupStatePusher popupStatePusher(
|
|
|
|
aIsTrustedEvent ? PopupBlocker::openAllowed : PopupBlocker::openAbused);
|
2007-02-13 18:02:57 +03:00
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
|
|
|
|
&event);
|
2007-02-13 18:02:57 +03:00
|
|
|
}
|
2015-09-18 15:18:42 +03:00
|
|
|
|
|
|
|
return aKeyCausesActivation;
|
2007-02-13 18:02:57 +03:00
|
|
|
}
|
|
|
|
|
2012-12-21 18:07:28 +04:00
|
|
|
nsGenericHTMLElement* HTMLLabelElement::GetLabeledElement() const {
|
2002-05-22 04:14:51 +04:00
|
|
|
nsAutoString elementId;
|
2010-05-20 06:28:10 +04: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-11 03:44:50 +04:00
|
|
|
return GetFirstLabelableDescendant();
|
2003-02-22 16:01:03 +03:00
|
|
|
}
|
2010-05-20 06:28:10 +04:00
|
|
|
|
2017-06-16 03:04:00 +03:00
|
|
|
// We have a @for. The id has to be linked to an element in the same tree
|
2010-05-20 06:28:10 +04:00
|
|
|
// and this element should be a labelable form control.
|
2017-06-16 03:04:00 +03:00
|
|
|
Element* element = nullptr;
|
|
|
|
|
2017-12-31 22:57:32 +03:00
|
|
|
if (ShadowRoot* shadowRoot = GetContainingShadow()) {
|
|
|
|
element = shadowRoot->GetElementById(elementId);
|
|
|
|
} else if (nsIDocument* doc = GetUncomposedDoc()) {
|
|
|
|
element = doc->GetElementById(elementId);
|
2017-06-16 03:04:00 +03:00
|
|
|
} else {
|
2017-12-31 22:57:32 +03:00
|
|
|
element =
|
|
|
|
nsContentUtils::MatchElementId(SubtreeRoot()->AsContent(), elementId);
|
2010-05-20 06:28:10 +04:00
|
|
|
}
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
if (element && element->IsLabelable()) {
|
2012-12-21 18:07:28 +04:00
|
|
|
return static_cast<nsGenericHTMLElement*>(element);
|
2010-05-20 06:28:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-02-22 16:01:03 +03:00
|
|
|
}
|
|
|
|
|
2012-12-21 18:07:28 +04:00
|
|
|
nsGenericHTMLElement* HTMLLabelElement::GetFirstLabelableDescendant() const {
|
2012-06-11 03:44:50 +04:00
|
|
|
for (nsIContent* cur = nsINode::GetFirstChild(); cur;
|
2011-05-25 04:18:31 +04:00
|
|
|
cur = cur->GetNextNode(this)) {
|
2018-05-30 17:56:24 +03:00
|
|
|
Element* element = Element::FromNode(cur);
|
2012-06-11 03:44:50 +04:00
|
|
|
if (element && element->IsLabelable()) {
|
2012-12-21 18:07:28 +04:00
|
|
|
return static_cast<nsGenericHTMLElement*>(element);
|
2002-05-22 04:14:51 +04:00
|
|
|
}
|
|
|
|
}
|
2003-02-22 16:01:03 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2002-05-22 04:14:51 +04:00
|
|
|
}
|
2010-05-20 06:28:10 +04:00
|
|
|
|
2012-12-21 18:06:50 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|