DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/* -*- 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/. */
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Implementation of HTML <label> elements.
 */
#include "HTMLLabelElement.h"
#include "HTMLLabelElement.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/dom/HTMLLabelElementBinding.h"
#include "mozilla/dom/MouseEventBinding.h"
#include "nsFocusManager.h"
#include "nsFocusManager.h"
#include "nsIFrame.h"
#include "nsContentUtils.h"
#include "nsQueryObject.h"
#include "mozilla/dom/ShadowRoot.h"


// construction, destruction

NS_IMPL_NS_NEW_HTML_ELEMENT(Label)

namespace mozilla {
namespace dom {

HTMLLabelElement::~HTMLLabelElement() = default;
HTMLLabelElement::~HTMLLabelElement() = default;

JSObject* HTMLLabelElement::WrapNode(JSContext* aCx,
                                     JS::Handle<JSObject*> aGivenProto) {
  return HTMLLabelElement_Binding::Wrap(aCx, this, aGivenProto);
}
}

// nsIDOMHTMLLabelElement

NS_IMPL_ELEMENT_CLONE(HTMLLabelElement)


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 nullptr;
  }

  return static_cast<HTMLFormElement*>(formControl->GetFormElement());
}
}

void HTMLLabelElement::Focus(const FocusOptions& aOptions,
                             const CallerType aCallerType,
                             ErrorResult& aError) {
  {
    nsIFrame* frame = GetPrimaryFrame(FlushType::Frames);
    nsIFrame* frame = GetPrimaryFrame(FlushType::Frames);
    if (frame && frame->IsFocusable()) {
      return nsGenericHTMLElement::Focus(aOptions, aCallerType, aError);
    }
  }


  if (RefPtr<Element> elem = GetLabeledElement()) {
    return elem->Focus(aOptions, aCallerType, aError);
  }
}


nsresult HTMLLabelElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
  WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
  if (mHandlingEvent ||
      (!(mouseEvent && mouseEvent->IsLeftClickEvent()) &&
       aVisitor.mEvent->mMessage != eMouseDown) ||
       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;
    return NS_OK;
  }

  nsCOMPtr<Element> target =
      do_QueryInterface(aVisitor.mEvent->GetOriginalDOMEventTarget());
      do_QueryInterface(aVisitor.mEvent->GetOriginalDOMEventTarget());
  if (nsContentUtils::IsInInteractiveHTMLContent(target, this)) {
    return NS_OK;
  }

  // Strong ref because event dispatch is going to happen.
  // Strong ref because event dispatch is going to happen.
  RefPtr<Element> content = GetLabeledElement();

  if (content) {
    mHandlingEvent = true;
    switch (aVisitor.mEvent->mMessage) {
    switch (aVisitor.mEvent->mMessage) {
      case eMouseDown:
        if (mouseEvent->mButton == MouseButton::eLeft) {
          // We reset the mouse-down point on every event because there is
          // no guarantee we will reach the eMouseClick code below.
          LayoutDeviceIntPoint* curPoint =
          LayoutDeviceIntPoint* curPoint =
              new LayoutDeviceIntPoint(mouseEvent->mRefPoint);
          SetProperty(nsGkAtoms::labelMouseDownPtProperty,
                      static_cast<void*>(curPoint),
                      nsINode::DeleteProperty<LayoutDeviceIntPoint>);
                      nsINode::DeleteProperty<LayoutDeviceIntPoint>);
        }
        break;

      case eMouseClick:
        if (mouseEvent->IsLeftClickEvent()) {
        if (mouseEvent->IsLeftClickEvent()) {
          LayoutDeviceIntPoint* mouseDownPoint =
              static_cast<LayoutDeviceIntPoint*>(
                  GetProperty(nsGkAtoms::labelMouseDownPtProperty));

          bool dragSelect = false;
          bool dragSelect = false;
          if (mouseDownPoint) {
            LayoutDeviceIntPoint dragDistance = *mouseDownPoint;
            RemoveProperty(nsGkAtoms::labelMouseDownPtProperty);

            dragDistance -= mouseEvent->mRefPoint;
            dragDistance -= mouseEvent->mRefPoint;
            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() ||
          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.
          // to prevent immediate de-focus.
          if (mouseEvent->mClickCount <= 1) {
            if (nsFocusManager* fm = nsFocusManager::GetFocusManager()) {
              // Use FLAG_BYMOVEFOCUS here so that the label is scrolled to.
              // Also, within HTMLInputElement::PostHandleEvent, inputs will
              // 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.
              // If the label has been clicked by the user, we also want to
              // pass FLAG_BYMOUSE so that we get correct focus ring behavior,
              // 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.
              bool byMouse = (mouseEvent->mInputSource !=
                              MouseEvent_Binding::MOZ_SOURCE_KEYBOARD);
              bool byTouch = (mouseEvent->mInputSource ==
              bool byTouch = (mouseEvent->mInputSource ==
                              MouseEvent_Binding::MOZ_SOURCE_TOUCH);
              fm->SetFocus(content,
                           nsIFocusManager::FLAG_BYMOVEFOCUS |
                               (byMouse ? nsIFocusManager::FLAG_BYMOUSE : 0) |
                               (byTouch ? nsIFocusManager::FLAG_BYTOUCH : 0));
                               (byTouch ? nsIFocusManager::FLAG_BYTOUCH : 0));
            }
          }
          // 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
          //    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;
          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(MOZ_KnownLive(aVisitor.mPresContext), mouseEvent,
          DispatchClickEvent(MOZ_KnownLive(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;
}


bool HTMLLabelElement::PerformAccesskey(bool aKeyCausesActivation,
                                        bool aIsTrustedEvent) {
                                        bool aIsTrustedEvent) {
  if (!aKeyCausesActivation) {
    RefPtr<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.mInputSource = MouseEvent_Binding::MOZ_SOURCE_KEYBOARD;
    event.mInputSource = MouseEvent_Binding::MOZ_SOURCE_KEYBOARD;

    AutoPopupStatePusher popupStatePusher(
        aIsTrustedEvent ? PopupBlocker::openAllowed : PopupBlocker::openAbused);


    EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
                              &event);
  }

  return aKeyCausesActivation;
  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.
    // 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 tree
  // and this element should be a labelable form control.
  // and this element should be a labelable form control.
  Element* element = nullptr;

  if (ShadowRoot* shadowRoot = GetContainingShadow()) {
    element = shadowRoot->GetElementById(elementId);
  } else if (Document* doc = GetUncomposedDoc()) {
    element = doc->GetElementById(elementId);
  } else {
  } else {
    element =
        nsContentUtils::MatchElementId(SubtreeRoot()->AsContent(), elementId);
  }

  if (element && element->IsLabelable()) {
  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 = Element::FromNode(cur);
    Element* element = Element::FromNode(cur);
    if (element && element->IsLabelable()) {
      return static_cast<nsGenericHTMLElement*>(element);
    }
    }
  }

  return nullptr;
}
}

}  // namespace dom
}  // namespace mozilla