Bug 1514040 - Dispatch events to hidden datetimebox UA Widget r=smaug

The XBL binding implementation relied on nsDateTimeControlFrame to call into
its nsIDateTimeInputArea implementation. This is correct because the XBL binding
is only constructed when the element has a frame. If the value is set while the
element is hidden, the XBL binding will pick up the correct value during construction.

That is not the case for UA Widget. As it is constructed when the DOM is attached,
relying on nsDateTimeControlFrame to send an event when attributes change means
the event won't be sent to the already constructed UA Widget.

This patch fixes that by moving the event dispatching calls originating from
HTMLInputElement out of nsDateTimeControlFrame, so they will behave correctly in
the absence of the frame.

I've also moved the gut of nsDateTimeControlFrame::HasBadInput() to
DateTimeInputTypeBase::HasBadInput(). Content script should be allowed to validate
the input without the frame.

Sadly this means the XBL implementation and the UA Widget implementation
have further diverged. The complexity should go away when we could finally
remove the XBL implementation.

nsDateTimeControlFrame still dispatches a few events to UA Widget, in
AttributeChanged() and SyncDisabledState(), as they are originated from the layout.

The name of the events in AttributeChanged() are incorrect though -- I am correcting
that in this patch too.

Differential Revision: https://phabricator.services.mozilla.com/D15601

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Timothy Guan-tin Chien 2019-01-04 21:53:51 +00:00
Родитель 542ea80ecd
Коммит 28b5c3416c
9 изменённых файлов: 210 добавлений и 159 удалений

Просмотреть файл

@ -2057,12 +2057,26 @@ void HTMLInputElement::GetDateTimeInputBoxValue(DateTimeValue& aValue) {
aValue = *mDateTimeInputBoxValue;
}
Element* HTMLInputElement::GetDateTimeBoxElementInUAWidget() {
if (GetShadowRoot()) {
// The datetimebox <div> is the only child of the UA Widget Shadow Root
// if it is present.
MOZ_ASSERT(GetShadowRoot()->IsUAWidget());
MOZ_ASSERT(1 >= GetShadowRoot()->GetChildCount());
if (nsIContent* inputAreaContent = GetShadowRoot()->GetFirstChild()) {
return inputAreaContent->AsElement();
}
}
return nullptr;
}
Element* HTMLInputElement::GetDateTimeBoxElement() {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame && frame->GetInputAreaContent()) {
return frame->GetInputAreaContent()->AsElement();
}
return nullptr;
return GetDateTimeBoxElementInUAWidget();
}
void HTMLInputElement::OpenDateTimePicker(const DateTimeValue& aInitialValue) {
@ -2620,9 +2634,17 @@ nsresult HTMLInputElement::SetValueInternal(const nsAString& aValue,
mType == NS_FORM_INPUT_DATE) &&
!IsExperimentalMobileType(mType) &&
!(aFlags & nsTextEditorState::eSetValue_BySetUserInput)) {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->OnValueChanged();
if (Element* dateTimeBoxElement = GetDateTimeBoxElementInUAWidget()) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
dateTimeBoxElement,
NS_LITERAL_STRING("MozDateTimeValueChanged"), CanBubble::eNo,
ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
} else {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->OnValueChanged();
}
}
}
if (mDoneCreating) {
@ -2909,10 +2931,18 @@ void HTMLInputElement::Blur(ErrorResult& aError) {
if ((mType == NS_FORM_INPUT_TIME || mType == NS_FORM_INPUT_DATE) &&
!IsExperimentalMobileType(mType)) {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->HandleBlurEvent();
if (Element* dateTimeBoxElement = GetDateTimeBoxElementInUAWidget()) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
dateTimeBoxElement, NS_LITERAL_STRING("MozBlurInnerTextBox"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
return;
} else {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->HandleBlurEvent();
return;
}
}
}
@ -2935,10 +2965,18 @@ void HTMLInputElement::Focus(ErrorResult& aError) {
if ((mType == NS_FORM_INPUT_TIME || mType == NS_FORM_INPUT_DATE) &&
!IsExperimentalMobileType(mType)) {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->HandleFocusEvent();
if (Element* dateTimeBoxElement = GetDateTimeBoxElementInUAWidget()) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
dateTimeBoxElement, NS_LITERAL_STRING("MozFocusInnerTextBox"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
return;
} else {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->HandleFocusEvent();
return;
}
}
}
@ -3245,11 +3283,18 @@ void HTMLInputElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
if ((mType == NS_FORM_INPUT_TIME || mType == NS_FORM_INPUT_DATE) &&
!IsExperimentalMobileType(mType) && aVisitor.mEvent->mMessage == eFocus &&
aVisitor.mEvent->mOriginalTarget == this) {
// If original target is this and not the anonymous text control, we should
// pass the focus to the anonymous text control.
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->HandleFocusEvent();
// If original target is this and not the inner text control, we should
// pass the focus to the inner text control.
if (Element* dateTimeBoxElement = GetDateTimeBoxElementInUAWidget()) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
dateTimeBoxElement, NS_LITERAL_STRING("MozFocusInnerTextBox"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
} else {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->HandleFocusEvent();
}
}
}

Просмотреть файл

@ -747,9 +747,17 @@ class HTMLInputElement final : public nsGenericHTMLFormElementWithState,
*/
void GetDateTimeInputBoxValue(DateTimeValue& aValue);
/*
* This locates the inner datetimebox UA Widget element and only the
* UA Widget
* element. This should fold into GetDateTimeBoxElement() when the XBL binding is removed.
*/
Element* GetDateTimeBoxElementInUAWidget();
/*
* This allows chrome JavaScript to dispatch event to the inner datetimebox
* anonymous element or access nsIDateTimeInputArea implmentation.
* anonymous or UA Widget element and access nsIDateTimeInputArea
* implementation.
*/
Element* GetDateTimeBoxElement();

Просмотреть файл

@ -7,8 +7,10 @@
#include "DateTimeInputTypes.h"
#include "js/Date.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "nsDateTimeControlFrame.h"
#include "nsDOMTokenList.h"
const double DateTimeInputTypeBase::kMinimumYear = 1;
const double DateTimeInputTypeBase::kMaximumYear = 275760;
@ -16,6 +18,9 @@ const double DateTimeInputTypeBase::kMaximumMonthInMaximumYear = 9;
const double DateTimeInputTypeBase::kMaximumWeekInMaximumYear = 37;
const double DateTimeInputTypeBase::kMsPerDay = 24 * 60 * 60 * 1000;
using namespace mozilla;
using namespace mozilla::dom;
/* static */ bool DateTimeInputTypeBase::IsInputDateTimeEnabled() {
static bool sDateTimeEnabled = false;
static bool sDateTimePrefCached = false;
@ -95,13 +100,42 @@ bool DateTimeInputTypeBase::HasStepMismatch(bool aUseZeroIfValueNaN) const {
}
bool DateTimeInputTypeBase::HasBadInput() const {
Element* editWrapperElement = nullptr;
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (!frame) {
if (frame && frame->GetInputAreaContent()) {
// edit-wrapper is inside an XBL binding
editWrapperElement =
mInputElement->GetComposedDoc()->GetAnonymousElementByAttribute(
frame->GetInputAreaContent(), nsGkAtoms::anonid,
NS_LITERAL_STRING("edit-wrapper"));
} else if (mInputElement->GetShadowRoot()) {
// edit-wrapper is inside an UA Widget Shadow DOM
editWrapperElement = mInputElement->GetShadowRoot()->GetElementById(
NS_LITERAL_STRING("edit-wrapper"));
}
if (!editWrapperElement) {
return false;
}
return frame->HasBadInput();
;
// Incomplete field does not imply bad input.
for (Element* child = editWrapperElement->GetFirstElementChild(); child;
child = child->GetNextElementSibling()) {
if (child->ClassList()->Contains(
NS_LITERAL_STRING("datetime-edit-field"))) {
nsAutoString value;
child->GetAttr(kNameSpaceID_None, nsGkAtoms::value, value);
if (value.IsEmpty()) {
return false;
}
}
}
// All fields are available but input element's value is empty implies
// it has been sanitized.
nsAutoString value;
mInputElement->GetValue(value, CallerType::System);
return value.IsEmpty();
}
nsresult DateTimeInputTypeBase::GetRangeOverflowMessage(nsAString& aMessage) {
@ -125,9 +159,17 @@ nsresult DateTimeInputTypeBase::GetRangeUnderflowMessage(nsAString& aMessage) {
}
nsresult DateTimeInputTypeBase::MinMaxStepAttrChanged() {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->OnMinMaxStepAttrChanged();
if (Element* dateTimeBoxElement =
mInputElement->GetDateTimeBoxElementInUAWidget()) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
dateTimeBoxElement, NS_LITERAL_STRING("MozNotifyMinMaxStepAttrChanged"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
} else {
nsDateTimeControlFrame* frame = do_QueryFrame(GetPrimaryFrame());
if (frame) {
frame->OnMinMaxStepAttrChanged();
}
}
return NS_OK;

Просмотреть файл

@ -38,6 +38,7 @@ skip-if = android_version == '18' # Android, bug 1147974
[test_input_datetime_focus_blur.html]
[test_input_datetime_focus_blur_events.html]
[test_input_datetime_focus_state.html]
[test_input_datetime_hidden.html]
[test_input_datetime_tabindex.html]
[test_input_defaultValue.html]
[test_input_email.html]

Просмотреть файл

@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1514040
-->
<head>
<title>Test construction of hidden date input type</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1514040">Mozilla Bug 1514040</a>
<p id="display"></p>
<div id="content">
<input id="date" type="date" hidden value="1947-02-28">
</div>
<pre id="test">
<script type="application/javascript">
let el = document.getElementById("date");
ok(el.hidden, "element is hidden");
is(el.value, "1947-02-28", ".value is set correctly");
let fieldElements = Array.from(SpecialPowers.wrap(el).openOrClosedShadowRoot.getElementsByClassName("datetime-edit-field"));
is(fieldElements[0].textContent, "02", "month is set");
is(fieldElements[1].textContent, "28", "day is set");
is(fieldElements[2].textContent, "1947", "year is set");
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -34,6 +34,15 @@ SimpleTest.waitForFocus(function() {
SimpleTest.finish();
});
function checkInnerTextboxTabindex(input, tabindex) {
let fields = SpecialPowers.wrap(input).openOrClosedShadowRoot.getElementsByClassName("datetime-edit-field");
for (let field of fields) {
is(field.tabIndex, tabindex, "tabIndex in the inner textbox should be correct");
}
}
function testTabindex(type) {
let input1 = document.getElementById(type + "1");
let input2 = document.getElementById(type + "2");
@ -62,11 +71,17 @@ function testTabindex(type) {
is(document.activeElement, input2,
"input element with tabindex=-1 is still focusable");
checkInnerTextboxTabindex(input1, 0);
checkInnerTextboxTabindex(input2, -1);
checkInnerTextboxTabindex(input3, 0);
// Changing the tabindex attribute dynamically.
input3.setAttribute("tabindex", "-1");
synthesizeKey("KEY_Tab"); // need only one TAB since input2 is not tabbable
isnot(document.activeElement, input3,
"element with tabindex changed to -1 should not be tabbable");
checkInnerTextboxTabindex(input3, -1);
}
function test() {

Просмотреть файл

@ -18,7 +18,6 @@
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/dom/MutationEventBinding.h"
#include "nsDOMTokenList.h"
#include "nsNodeInfoManager.h"
#include "nsIDateTimeInputArea.h"
#include "nsIObserverService.h"
@ -51,121 +50,47 @@ void nsDateTimeControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
}
void nsDateTimeControlFrame::OnValueChanged() {
if (mInputAreaContent) {
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->NotifyInputElementValueChanged();
}
} else {
Element* inputAreaContent = GetInputAreaContentAsElement();
if (!inputAreaContent) {
return;
}
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent, NS_LITERAL_STRING("MozDateTimeValueChanged"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
if (!mInputAreaContent) {
return;
}
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->NotifyInputElementValueChanged();
}
}
void nsDateTimeControlFrame::OnMinMaxStepAttrChanged() {
if (mInputAreaContent) {
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->NotifyMinMaxStepAttrChanged();
}
} else {
Element* inputAreaContent = GetInputAreaContentAsElement();
if (!inputAreaContent) {
return;
}
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent, NS_LITERAL_STRING("MozNotifyMinMaxStepAttrChanged"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
if (!mInputAreaContent) {
return;
}
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->NotifyMinMaxStepAttrChanged();
}
}
void nsDateTimeControlFrame::HandleFocusEvent() {
if (mInputAreaContent) {
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->FocusInnerTextBox();
}
} else {
Element* inputAreaContent = GetInputAreaContentAsElement();
if (!inputAreaContent) {
return;
}
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent, NS_LITERAL_STRING("MozFocusInnerTextBox"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
if (!mInputAreaContent) {
return;
}
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->FocusInnerTextBox();
}
}
void nsDateTimeControlFrame::HandleBlurEvent() {
if (mInputAreaContent) {
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->BlurInnerTextBox();
}
} else {
Element* inputAreaContent = GetInputAreaContentAsElement();
if (!inputAreaContent) {
return;
}
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent, NS_LITERAL_STRING("MozBlurInnerTextBox"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
if (!mInputAreaContent) {
return;
}
}
bool nsDateTimeControlFrame::HasBadInput() {
Element* editWrapperElement = nullptr;
if (mInputAreaContent) {
// edit-wrapper is inside an XBL binding
editWrapperElement =
mInputAreaContent->GetComposedDoc()->GetAnonymousElementByAttribute(
mInputAreaContent, nsGkAtoms::anonid,
NS_LITERAL_STRING("edit-wrapper"));
} else if (mContent->GetShadowRoot()) {
// edit-wrapper is inside an UA Widget Shadow DOM
editWrapperElement = mContent->GetShadowRoot()->GetElementById(
NS_LITERAL_STRING("edit-wrapper"));
nsCOMPtr<nsIDateTimeInputArea> inputAreaContent =
do_QueryInterface(mInputAreaContent);
if (inputAreaContent) {
inputAreaContent->BlurInnerTextBox();
}
if (!editWrapperElement) {
return false;
}
// Incomplete field does not imply bad input.
for (Element* child = editWrapperElement->GetFirstElementChild(); child;
child = child->GetNextElementSibling()) {
if (child->ClassList()->Contains(
NS_LITERAL_STRING("datetime-edit-field"))) {
nsAutoString value;
child->GetAttr(kNameSpaceID_None, nsGkAtoms::value, value);
if (value.IsEmpty()) {
return false;
}
}
}
// All fields are available but input element's value is empty implies
// it has been sanitized.
nsAutoString value;
HTMLInputElement::FromNode(mContent)->GetValue(value, CallerType::System);
return value.IsEmpty();
}
nscoord nsDateTimeControlFrame::GetMinISize(gfxContext* aRenderingContext) {
@ -384,13 +309,15 @@ void nsDateTimeControlFrame::SyncDisabledState() {
}
inputAreaContent->UpdateEditAttributes();
} else {
Element* inputAreaContent = GetInputAreaContentAsElement();
if (!inputAreaContent) {
Element* dateTimeBoxElement =
static_cast<dom::HTMLInputElement*>(GetContent())
->GetDateTimeBoxElementInUAWidget();
if (!dateTimeBoxElement) {
return;
}
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent, NS_LITERAL_STRING("MozDateTimeAttributeChanged"),
dateTimeBoxElement, NS_LITERAL_STRING("MozDateTimeAttributeChanged"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
}
@ -426,21 +353,23 @@ nsresult nsDateTimeControlFrame::AttributeChanged(int32_t aNameSpaceID,
}
}
} else {
Element* inputAreaContent = GetInputAreaContentAsElement();
Element* dateTimeBoxElement =
static_cast<dom::HTMLInputElement*>(GetContent())
->GetDateTimeBoxElementInUAWidget();
if (aAttribute == nsGkAtoms::value) {
if (inputAreaContent) {
if (dateTimeBoxElement) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent,
NS_LITERAL_STRING("NotifyInputElementValueChanged"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dateTimeBoxElement,
NS_LITERAL_STRING("MozDateTimeValueChanged"), CanBubble::eNo,
ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
}
} else {
if (inputAreaContent) {
if (dateTimeBoxElement) {
AsyncEventDispatcher* dispatcher = new AsyncEventDispatcher(
inputAreaContent,
NS_LITERAL_STRING("MozDateTimeValueChanged"), CanBubble::eNo,
ChromeOnlyDispatch::eNo);
dateTimeBoxElement,
NS_LITERAL_STRING("MozDateTimeAttributeChanged"),
CanBubble::eNo, ChromeOnlyDispatch::eNo);
dispatcher->RunDOMEventWhenSafe();
}
}
@ -453,25 +382,7 @@ nsresult nsDateTimeControlFrame::AttributeChanged(int32_t aNameSpaceID,
}
nsIContent* nsDateTimeControlFrame::GetInputAreaContent() {
if (mInputAreaContent) {
return mInputAreaContent;
}
if (mContent->GetShadowRoot()) {
// The datetimebox <div> is the only child of the UA Widget Shadow Root
// if it is present.
MOZ_ASSERT(mContent->GetShadowRoot()->IsUAWidget());
MOZ_ASSERT(1 >= mContent->GetShadowRoot()->GetChildCount());
return mContent->GetShadowRoot()->GetFirstChild();
}
return nullptr;
}
Element* nsDateTimeControlFrame::GetInputAreaContentAsElement() {
nsIContent* inputAreaContent = GetInputAreaContent();
if (inputAreaContent) {
return inputAreaContent->AsElement();
}
return nullptr;
return mInputAreaContent;
}
void nsDateTimeControlFrame::ContentStatesChanged(EventStates aStates) {

Просмотреть файл

@ -80,7 +80,6 @@ class nsDateTimeControlFrame final : public nsContainerFrame,
void OnMinMaxStepAttrChanged();
void HandleFocusEvent();
void HandleBlurEvent();
bool HasBadInput();
private:
class SyncDisabledStateEvent;
@ -109,8 +108,6 @@ class nsDateTimeControlFrame final : public nsContainerFrame,
*/
void SyncDisabledState();
mozilla::dom::Element* GetInputAreaContentAsElement();
// Anonymous child which is bound via XBL to an element that wraps the input
// area and reset button.
RefPtr<mozilla::dom::Element> mInputAreaContent;

Просмотреть файл

@ -267,7 +267,7 @@ this.DateTimeInputBaseImplWidget = class {
// Used to store the non-formatted value, cleared when value is
// cleared.
// nsDateTimeControlFrame::HasBadInput() will read this to decide
// DateTimeInputTypeBase::HasBadInput() will read this to decide
// if the input has value.
field.setAttribute("value", "");