author Stone Shih <>
Fri, 11 Nov 2016 18:02:37 +0800
changeset 370264 038de5add834684bcb9cfe709df3a5868f7e54b7
parent 357046 4fb8ce27c37fd72b2ea08fe299f590071dc60f8c
child 406290 662df8ef329fd7a38fb2ceb24e305cd31e7bcaa7
permissions -rw-r--r--
Bug 1315862 Part1: Stop dispatching pointer events in the system group. r=masayuki,smaug MozReview-Commit-ID: 8Xqp3bxlRFt

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 */
#include "nsSelectsAreaFrame.h"
#include "nsIContent.h"
#include "nsListControlFrame.h"
#include "nsDisplayList.h"
#include "WritingModes.h"

using namespace mozilla;

NS_NewSelectsAreaFrame(nsIPresShell* aShell, nsStyleContext* aContext, nsFrameState aFlags)
  nsSelectsAreaFrame* it = new (aShell) nsSelectsAreaFrame(aContext);

  // We need NS_BLOCK_FLOAT_MGR to ensure that the options inside the select
  // aren't expanded by right floats outside the select.
  it->AddStateBits(aFlags | NS_BLOCK_FLOAT_MGR);

  return it;


 * This wrapper class lets us redirect mouse hits from the child frame of
 * an option element to the element's own frame.
 * REVIEW: This is what nsSelectsAreaFrame::GetFrameForPoint used to do
class nsDisplayOptionEventGrabber : public nsDisplayWrapList {
  nsDisplayOptionEventGrabber(nsDisplayListBuilder* aBuilder,
                              nsIFrame* aFrame, nsDisplayItem* aItem)
    : nsDisplayWrapList(aBuilder, aFrame, aItem) {}
  nsDisplayOptionEventGrabber(nsDisplayListBuilder* aBuilder,
                              nsIFrame* aFrame, nsDisplayList* aList)
    : nsDisplayWrapList(aBuilder, aFrame, aList) {}
  virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
                       HitTestState* aState, nsTArray<nsIFrame*> *aOutFrames) override;
  virtual bool ShouldFlattenAway(nsDisplayListBuilder* aBuilder) override {
    return false;

void nsDisplayOptionEventGrabber::HitTest(nsDisplayListBuilder* aBuilder,
    const nsRect& aRect, HitTestState* aState, nsTArray<nsIFrame*> *aOutFrames)
  nsTArray<nsIFrame*> outFrames;
  mList.HitTest(aBuilder, aRect, aState, &outFrames);

  for (uint32_t i = 0; i < outFrames.Length(); i++) {
    nsIFrame* selectedFrame = outFrames.ElementAt(i);
    while (selectedFrame &&
           !(selectedFrame->GetContent() &&
             selectedFrame->GetContent()->IsHTMLElement(nsGkAtoms::option))) {
      selectedFrame = selectedFrame->GetParent();
    if (selectedFrame) {
    } else {
      // keep the original result, which could be this frame

class nsOptionEventGrabberWrapper : public nsDisplayWrapper
  nsOptionEventGrabberWrapper() {}
  virtual nsDisplayItem* WrapList(nsDisplayListBuilder* aBuilder,
                                  nsIFrame* aFrame, nsDisplayList* aList) {
    return new (aBuilder) nsDisplayOptionEventGrabber(aBuilder, aFrame, aList);
  virtual nsDisplayItem* WrapItem(nsDisplayListBuilder* aBuilder,
                                  nsDisplayItem* aItem) {
    return new (aBuilder) nsDisplayOptionEventGrabber(aBuilder, aItem->Frame(), aItem);

static nsListControlFrame* GetEnclosingListFrame(nsIFrame* aSelectsAreaFrame)
  nsIFrame* frame = aSelectsAreaFrame->GetParent();
  while (frame) {
    if (frame->GetType() == nsGkAtoms::listControlFrame)
      return static_cast<nsListControlFrame*>(frame);
    frame = frame->GetParent();
  return nullptr;

class nsDisplayListFocus : public nsDisplayItem {
  nsDisplayListFocus(nsDisplayListBuilder* aBuilder,
                     nsSelectsAreaFrame* aFrame) :
    nsDisplayItem(aBuilder, aFrame) {
  virtual ~nsDisplayListFocus() {

  virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) override {
    *aSnap = false;
    // override bounds because the list item focus ring may extend outside
    // the nsSelectsAreaFrame
    nsListControlFrame* listFrame = GetEnclosingListFrame(Frame());
    return listFrame->GetVisualOverflowRectRelativeToSelf() +
  virtual void Paint(nsDisplayListBuilder* aBuilder,
                     nsRenderingContext* aCtx) override {
    nsListControlFrame* listFrame = GetEnclosingListFrame(Frame());
    // listFrame must be non-null or we wouldn't get called.

nsSelectsAreaFrame::BuildDisplayList(nsDisplayListBuilder*   aBuilder,
                                     const nsRect&           aDirtyRect,
                                     const nsDisplayListSet& aLists)
  if (!aBuilder->IsForEventDelivery()) {
    BuildDisplayListInternal(aBuilder, aDirtyRect, aLists);

  nsDisplayListCollection set;
  BuildDisplayListInternal(aBuilder, aDirtyRect, set);
  nsOptionEventGrabberWrapper wrapper;
  wrapper.WrapLists(aBuilder, this, set, aLists);

nsSelectsAreaFrame::BuildDisplayListInternal(nsDisplayListBuilder*   aBuilder,
                                             const nsRect&           aDirtyRect,
                                             const nsDisplayListSet& aLists)
  nsBlockFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);

  nsListControlFrame* listFrame = GetEnclosingListFrame(this);
  if (listFrame && listFrame->IsFocused()) {
    // we can't just associate the display item with the list frame,
    // because then the list's scrollframe won't clip it (the scrollframe
    // only clips contained descendants).
    aLists.Outlines()->AppendNewToTop(new (aBuilder)
      nsDisplayListFocus(aBuilder, this));

nsSelectsAreaFrame::Reflow(nsPresContext*           aPresContext, 
                           ReflowOutput&     aDesiredSize,
                           const ReflowInput& aReflowInput, 
                           nsReflowStatus&          aStatus)
  nsListControlFrame* list = GetEnclosingListFrame(this);
               "Must have an nsListControlFrame!  Frame constructor is "

  bool isInDropdownMode = list->IsInDropDownMode();

  // See similar logic in nsListControlFrame::Reflow and
  // nsListControlFrame::ReflowAsDropdown.  We need to match it here.
  WritingMode wm = aReflowInput.GetWritingMode();
  nscoord oldBSize;
  if (isInDropdownMode) {
    // Store the block size now in case it changes during
    // nsBlockFrame::Reflow for some odd reason.
    if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
      oldBSize = BSize(wm);
    } else {

  nsBlockFrame::Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);

  // Check whether we need to suppress scrollbar updates.  We want to do
  // that if we're in a possible first pass and our block size of a row
  // has changed.
  if (list->MightNeedSecondPass()) {
    nscoord newBSizeOfARow = list->CalcBSizeOfARow();
    // We'll need a second pass if our block size of a row changed.  For
    // comboboxes, we'll also need it if our block size changed.  If
    // we're going to do a second pass, suppress scrollbar updates for
    // this pass.
    if (newBSizeOfARow != mBSizeOfARow ||
        (isInDropdownMode && (oldBSize != aDesiredSize.BSize(wm) ||
                              oldBSize != BSize(wm)))) {
      mBSizeOfARow = newBSizeOfARow;