Commit 580e6550 authored by Emilio Cobos Álvarez's avatar Emilio Cobos Álvarez
Browse files

Bug 1729292 - Honor scroll margin of host when we're scrolling to a...

Bug 1729292 - Honor scroll margin of host when we're scrolling to a chrome-only-access element. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D124691
parent 91911402
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -2966,6 +2966,8 @@ void HTMLInputElement::SetCheckedInternal(bool aChecked, bool aNotify) {
void HTMLInputElement::Blur(ErrorResult& aError) {
  if (CreatesDateTimeWidget()) {
    if (Element* dateTimeBoxElement = GetDateTimeBoxElement()) {
      // TODO(emilio, bug 1729342): This should probably use delegatesFocus
      // instead.
      AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
          dateTimeBoxElement, u"MozBlurInnerTextBox"_ns, CanBubble::eNo,
          ChromeOnlyDispatch::eNo);
@@ -2981,6 +2983,8 @@ void HTMLInputElement::Focus(const FocusOptions& aOptions,
                             CallerType aCallerType, ErrorResult& aError) {
  if (CreatesDateTimeWidget()) {
    if (Element* dateTimeBoxElement = GetDateTimeBoxElement()) {
      // TODO(emilio, bug 1729342): This should probably use delegatesFocus
      // instead.
      AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
          dateTimeBoxElement, u"MozFocusInnerTextBox"_ns, CanBubble::eNo,
          ChromeOnlyDispatch::eNo);
+20 −1
Original line number Diff line number Diff line
@@ -3505,6 +3505,25 @@ nsresult PresShell::ScrollContentIntoView(nsIContent* aContent,
  return NS_OK;
}

static nsMargin GetScrollMargin(const nsIContent* aContentToScrollTo,
                                const nsIFrame* aFrame) {
  MOZ_ASSERT(aContentToScrollTo->GetPrimaryFrame() == aFrame);
  // If we're focusing something that can't be targeted by content, allow
  // content to customize the margin.
  //
  // TODO: This is also a bit of an issue for delegated focus, see
  // https://github.com/whatwg/html/issues/7033.
  if (aContentToScrollTo->ChromeOnlyAccess()) {
    if (const nsIContent* userContent =
            aContentToScrollTo->GetChromeOnlyAccessSubtreeRootParent()) {
      if (const nsIFrame* frame = userContent->GetPrimaryFrame()) {
        return frame->StyleMargin()->GetScrollMargin();
      }
    }
  }
  return aFrame->StyleMargin()->GetScrollMargin();
}

void PresShell::DoScrollContentIntoView() {
  NS_ASSERTION(mDidInitialize, "should have done initial reflow by now");

@@ -3540,7 +3559,7 @@ void PresShell::DoScrollContentIntoView() {

  // Get the scroll-margin here since |frame| is going to be changed to iterate
  // over all continuation frames below.
  const nsMargin scrollMargin = frame->StyleMargin()->GetScrollMargin();
  const nsMargin scrollMargin = GetScrollMargin(mContentToScrollTo, frame);

  // This is a two-step process.
  // Step 1: Find the bounds of the rect we want to scroll into view.  For
+31 −0
Original line number Diff line number Diff line
<!doctype html>
<title>scroll-margin on input widget</title>
<link rel="author" href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez">
<link rel="author" href="https://mozilla.org" title="Mozilla">
<link rel="help" href="https://www.w3.org/TR/css-scroll-snap-1/#scroll-margin">
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1729292">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
  #target {
    scroll-margin-top: 200px;
  }
  .padding {
    height: 5000px;
  }
</style>

<div id="container">
  <div class="padding"></div>
  <input type="date" id="target">
  <div class="padding"></div>
</div>

<script>
test(function() {
  document.scrollingElement.scrollTo(0, 20000);
  document.getElementById("target").focus();
  // Should be around 4900 (5000 - 200px of margin). Give some leeway to account for line height / borders / input padding / etc.
  assert_between_exclusive(document.scrollingElement.scrollTop, 4750, 4850, "Should honor date input scroll-margin");
});
</script>