Files
palemoon27/dom/html/HTMLLabelElement.cpp
T
roytam1 343d0fbef3 import changes from `dev' branch of rmottola/Arctic-Fox:
- non-android part of Bug 1159371 - Make --disable-compile-environment work with --enable-application=mobile/android. r=glandium (489eef9b71)
- Bug 1159371 - Allow compiling sub-directories with --disable-compile-environment. r=glandium (5994752e02)
- Bug 1191819 - part 1 - substitute MOZ_BZ2_CFLAGS as a list; r=mshal (71cc3c5600)
- Bug 973933 - New updater-xpcshell binary for updater tests. r=rstrong (231074b8dd)
- Bug 973933 - Fix mochitest chrome updater tests. r=rstrong (30fbbcb6fc)
- Bug 973933 - Fix Nightly builds failing on updater-xpcshell. r=rstrong (8321af1395)
- Bug 1151827 - Fix re-building toolkit/mozapps/update on Windows. r=rstrong (ebc315c842)
- missing bit of Bug 1043692 - Add a DIST_INSTALL variable to moz.build, and replace NO_DIST_INSTALL with it. r=gps (cd79d1609d)
- Bug 1165061 - Fix Intermittent test_0102_background_restartNotification_staging.xul by not copying app files when staging for tests and enable the tests for Linux and Mac. r=spohl (918995f426)
- Bug 1191819 - part 2 - move updater's CXXFLAGS munging to moz.build; r=mshal (1e671a9838)
- Bug 1168042, return a bool from PerformAccessKey indicating if focus was changed, r=masayuki (2077cb0763)
- Bug 895274 part.36 Rename NS_MOUSEENTER to eMouseEnter r=smaug (fa49e67e92)
- Bug 895274 part.37 Rename NS_MOUSELEAVE to eMouseLeave r=smaug (1cc67b7bf5)
- Bug 895274 part.38 Rename NS_MOUSE_MOZLONGTAP to eMouseLongTap r=smaug (57b23d9678)
- Bug 1188234, part 1 - Initialize |number|. r=smaug (65734ffddd)
- Bug 1188234, part 2 - Add bounds checking in nsXULPrototypeElement::Deserialize(). r=smaug (c60e5e2eb0)
- Bug 1188234, part 3 - Make allocation of nsXULPrototypeAttribute fallible in nsXULPrototypeElement::Deserialize(). r=smaug (8ce4fe9925)
- Bug 1188234, part 4 - Don't append null to mChildren. r=smaug (2ec23c1c28)
- Bug 1190608, part 4 - Brace a few ifs in nsXULElement.cpp. r=poiru (1186ad8a86)
- Bug 1190608, part 3 - Remove some infallible new checks in nsXULElement.cpp. r=poiru (b2cf9b38b9)
- part of Bug 1190608, part 2 - Tuck elses in nsXULElement.cpp. r=poiru (98e6b93cdd)
- Bug 1190608, part 1 - Delete trailing whitespace in nsXULElement.cpp and nsXULPrototypeCache.cpp. r=poiru (3e80527246)
- Bug 1190608, part 5 - Sink the declaration of i in nsXULElement.cpp. r=poiru (d6917f5d96)
- Bug 1193572, part 1 - Don't use return values of failing calls in XUL deserialize methods. r=baku (8410bb3954)
- Bug 1193572, part 2 - nsXULPrototypeElement::Deserialize should use fallible SetCapacity. r=baku (15bf7ba655)
- Bug 1193572, part 3 - Don't re-initialize mType for XUL proto elements. r=baku (bdddcc6b0e)
- Bug 1193572, part 4 - Make script element ownership more direct. r=baku (7729aba5b3)
-  Bug 95274 part.39 Rename NS_POINTER_EVENT_START to ePointerEventFirst and add ePointerEventLast r=smaug (12289fc024)
- Bug 895274 part.40 Rename NS_POINTER_LOST_CAPTURE to ePointerLostCapture r=smaug (8cf49e2dce)
- Bug 895274 part.41 Rename NS_POINTER_GOT_CAPTURE to ePointerGotCapture r=smaug (11e1d37261)
- Bug 895274 part.42 Rename NS_POINTER_CANCEL to ePointerCancel r=smaug (ffe18b2169)
- Bug 1162990 - Add POINTER_LEAVE on captured element on e10s. r=smaug (98430e3bdc)
- Bug 895274 part.43 Rename NS_POINTER_LEAVE to ePointerLeave r=smaug (635c331338)
2022-04-22 14:33:40 +08:00

290 lines
9.0 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/**
* Implementation of HTML <label> elements.
*/
#include "HTMLLabelElement.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/dom/HTMLLabelElementBinding.h"
#include "nsFocusManager.h"
#include "nsIDOMMouseEvent.h"
#include "nsQueryObject.h"
// construction, destruction
NS_IMPL_NS_NEW_HTML_ELEMENT(Label)
namespace mozilla {
namespace dom {
HTMLLabelElement::~HTMLLabelElement()
{
}
JSObject*
HTMLLabelElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
{
return HTMLLabelElementBinding::Wrap(aCx, this, aGivenProto);
}
// nsISupports
NS_IMPL_ISUPPORTS_INHERITED(HTMLLabelElement, nsGenericHTMLFormElement,
nsIDOMHTMLLabelElement)
// nsIDOMHTMLLabelElement
NS_IMPL_ELEMENT_CLONE(HTMLLabelElement)
NS_IMETHODIMP
HTMLLabelElement::GetForm(nsIDOMHTMLFormElement** aForm)
{
return nsGenericHTMLFormElement::GetForm(aForm);
}
NS_IMETHODIMP
HTMLLabelElement::GetControl(nsIDOMHTMLElement** aElement)
{
nsCOMPtr<nsIDOMHTMLElement> element = do_QueryObject(GetLabeledElement());
element.forget(aElement);
return NS_OK;
}
NS_IMETHODIMP
HTMLLabelElement::SetHtmlFor(const nsAString& aHtmlFor)
{
ErrorResult rv;
SetHtmlFor(aHtmlFor, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
HTMLLabelElement::GetHtmlFor(nsAString& aHtmlFor)
{
nsString htmlFor;
GetHtmlFor(htmlFor);
aHtmlFor = htmlFor;
return NS_OK;
}
void
HTMLLabelElement::Focus(ErrorResult& aError)
{
// retarget the focus method at the for content
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
nsCOMPtr<nsIDOMElement> elem = do_QueryObject(GetLabeledElement());
if (elem)
fm->SetFocus(elem, 0);
}
}
static bool
InInteractiveHTMLContent(nsIContent* aContent, nsIContent* aStop)
{
nsIContent* content = aContent;
while (content && content != aStop) {
if (content->IsElement() &&
content->AsElement()->IsInteractiveHTMLContent(true)) {
return true;
}
content = content->GetParent();
}
return false;
}
nsresult
HTMLLabelElement::PostHandleEvent(EventChainPostVisitor& aVisitor)
{
WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
if (mHandlingEvent ||
(!(mouseEvent && mouseEvent->IsLeftClickEvent()) &&
aVisitor.mEvent->mMessage != eMouseDown) ||
aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault ||
!aVisitor.mPresContext ||
// Don't handle the event if it's already been handled by another label
aVisitor.mEvent->mFlags.mMultipleActionsPrevented) {
return NS_OK;
}
nsCOMPtr<nsIContent> target = do_QueryInterface(aVisitor.mEvent->target);
if (InInteractiveHTMLContent(target, this)) {
return NS_OK;
}
// Strong ref because event dispatch is going to happen.
nsRefPtr<Element> content = GetLabeledElement();
if (content) {
mHandlingEvent = true;
switch (aVisitor.mEvent->mMessage) {
case eMouseDown:
if (mouseEvent->button == WidgetMouseEvent::eLeftButton) {
// We reset the mouse-down point on every event because there is
// no guarantee we will reach the eMouseClick code below.
LayoutDeviceIntPoint* curPoint =
new LayoutDeviceIntPoint(mouseEvent->refPoint);
SetProperty(nsGkAtoms::labelMouseDownPtProperty,
static_cast<void*>(curPoint),
nsINode::DeleteProperty<LayoutDeviceIntPoint>);
}
break;
case eMouseClick:
if (mouseEvent->IsLeftClickEvent()) {
LayoutDeviceIntPoint* mouseDownPoint =
static_cast<LayoutDeviceIntPoint*>(
GetProperty(nsGkAtoms::labelMouseDownPtProperty));
bool dragSelect = false;
if (mouseDownPoint) {
LayoutDeviceIntPoint dragDistance = *mouseDownPoint;
DeleteProperty(nsGkAtoms::labelMouseDownPtProperty);
dragDistance -= mouseEvent->refPoint;
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
// have a kbd modifier (which adjusts a selection).
if (dragSelect || mouseEvent->IsShift() || mouseEvent->IsControl() ||
mouseEvent->IsAlt() || mouseEvent->IsMeta()) {
break;
}
// Only set focus on the first click of multiple clicks to prevent
// to prevent immediate de-focus.
if (mouseEvent->clickCount <= 1) {
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);
}
}
// 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.
EventFlags eventFlags;
eventFlags.mMultipleActionsPrevented = true;
DispatchClickEvent(aVisitor.mPresContext, mouseEvent,
content, false, &eventFlags, &status);
// Do we care about the status this returned? I don't think we do...
// Don't run another <label> off of this click
mouseEvent->mFlags.mMultipleActionsPrevented = true;
}
break;
default:
break;
}
mHandlingEvent = false;
}
return NS_OK;
}
nsresult
HTMLLabelElement::Reset()
{
return NS_OK;
}
NS_IMETHODIMP
HTMLLabelElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
{
return NS_OK;
}
bool
HTMLLabelElement::PerformAccesskey(bool aKeyCausesActivation,
bool aIsTrustedEvent)
{
if (!aKeyCausesActivation) {
nsRefPtr<Element> element = GetLabeledElement();
if (element) {
return element->PerformAccesskey(aKeyCausesActivation, aIsTrustedEvent);
}
} else {
nsPresContext *presContext = GetPresContext(eForUncomposedDoc);
if (!presContext) {
return false;
}
// Click on it if the users prefs indicate to do so.
WidgetMouseEvent event(aIsTrustedEvent, eMouseClick,
nullptr, WidgetMouseEvent::eReal);
event.inputSource = nsIDOMMouseEvent::MOZ_SOURCE_KEYBOARD;
nsAutoPopupStatePusher popupStatePusher(aIsTrustedEvent ?
openAllowed : openAbused);
EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
&event);
}
return aKeyCausesActivation;
}
nsGenericHTMLElement*
HTMLLabelElement::GetLabeledElement() const
{
nsAutoString elementId;
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.
return GetFirstLabelableDescendant();
}
// 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.
//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();
if (!doc) {
return nullptr;
}
Element* element = doc->GetElementById(elementId);
if (element && element->IsLabelable()) {
return static_cast<nsGenericHTMLElement*>(element);
}
return nullptr;
}
nsGenericHTMLElement*
HTMLLabelElement::GetFirstLabelableDescendant() const
{
for (nsIContent* cur = nsINode::GetFirstChild(); cur;
cur = cur->GetNextNode(this)) {
Element* element = cur->IsElement() ? cur->AsElement() : nullptr;
if (element && element->IsLabelable()) {
return static_cast<nsGenericHTMLElement*>(element);
}
}
return nullptr;
}
} // namespace dom
} // namespace mozilla