Commit eb9b6ddf authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

Cleanup of the code around the upload button of file upload control

* Add HTMLInputElement::UploadButton() to return a shadow element

* Update its active state for CanReceiveDroppedFiles() immediately.
  Remove LayoutFileUploadControl::can_receive_dropped_files_.

* Remove LayoutFileUploadControl::ButtonValue(), which was not used.

* Remove LayoutFileUploadControl::UploadButtonWidth(), which was not defined.

This CL has no behavior changes.

Bug: 1040828
Change-Id: Id2a598dacd9f60451614d38fba3785cea1ec4d92
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2000280Reviewed-by: default avatarKoji Ishii <kojii@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#731846}
parent 5e1535a8
...@@ -326,25 +326,27 @@ void FileInputType::CreateShadowSubtree() { ...@@ -326,25 +326,27 @@ void FileInputType::CreateShadowSubtree() {
GetElement().Multiple() ? IDS_FORM_MULTIPLE_FILES_BUTTON_LABEL GetElement().Multiple() ? IDS_FORM_MULTIPLE_FILES_BUTTON_LABEL
: IDS_FORM_FILE_BUTTON_LABEL))); : IDS_FORM_FILE_BUTTON_LABEL)));
button->SetShadowPseudoId(AtomicString("-webkit-file-upload-button")); button->SetShadowPseudoId(AtomicString("-webkit-file-upload-button"));
button->SetActive(GetElement().CanReceiveDroppedFiles());
GetElement().UserAgentShadowRoot()->AppendChild(button); GetElement().UserAgentShadowRoot()->AppendChild(button);
} }
HTMLInputElement* FileInputType::UploadButton() const {
Node* node = GetElement().UserAgentShadowRoot()->firstChild();
CHECK(!node || IsA<HTMLInputElement>(node));
return To<HTMLInputElement>(node);
}
void FileInputType::DisabledAttributeChanged() { void FileInputType::DisabledAttributeChanged() {
DCHECK(IsShadowHost(GetElement())); DCHECK(IsShadowHost(GetElement()));
CHECK(!GetElement().UserAgentShadowRoot()->firstChild() || if (Element* button = UploadButton()) {
IsA<Element>(GetElement().UserAgentShadowRoot()->firstChild()));
if (Element* button =
To<Element>(GetElement().UserAgentShadowRoot()->firstChild()))
button->SetBooleanAttribute(html_names::kDisabledAttr, button->SetBooleanAttribute(html_names::kDisabledAttr,
GetElement().IsDisabledFormControl()); GetElement().IsDisabledFormControl());
}
} }
void FileInputType::MultipleAttributeChanged() { void FileInputType::MultipleAttributeChanged() {
DCHECK(IsShadowHost(GetElement())); DCHECK(IsShadowHost(GetElement()));
CHECK(!GetElement().UserAgentShadowRoot()->firstChild() || if (Element* button = UploadButton()) {
IsA<Element>(GetElement().UserAgentShadowRoot()->firstChild()));
if (Element* button =
To<Element>(GetElement().UserAgentShadowRoot()->firstChild())) {
button->setAttribute( button->setAttribute(
html_names::kValueAttr, html_names::kValueAttr,
AtomicString(GetLocale().QueryString( AtomicString(GetLocale().QueryString(
......
...@@ -88,6 +88,7 @@ class CORE_EXPORT FileInputType final : public InputType, ...@@ -88,6 +88,7 @@ class CORE_EXPORT FileInputType final : public InputType,
bool ReceiveDroppedFiles(const DragData*) override; bool ReceiveDroppedFiles(const DragData*) override;
String DroppedFileSystemId() override; String DroppedFileSystemId() override;
void CreateShadowSubtree() override; void CreateShadowSubtree() override;
HTMLInputElement* UploadButton() const override;
void DisabledAttributeChanged() override; void DisabledAttributeChanged() override;
void MultipleAttributeChanged() override; void MultipleAttributeChanged() override;
String DefaultToolTip(const InputTypeView&) const override; String DefaultToolTip(const InputTypeView&) const override;
......
...@@ -1538,8 +1538,12 @@ void HTMLInputElement::SetCanReceiveDroppedFiles( ...@@ -1538,8 +1538,12 @@ void HTMLInputElement::SetCanReceiveDroppedFiles(
if (!!can_receive_dropped_files_ == can_receive_dropped_files) if (!!can_receive_dropped_files_ == can_receive_dropped_files)
return; return;
can_receive_dropped_files_ = can_receive_dropped_files; can_receive_dropped_files_ = can_receive_dropped_files;
if (GetLayoutObject()) if (HTMLInputElement* button = UploadButton())
GetLayoutObject()->UpdateFromElement(); button->SetActive(can_receive_dropped_files);
}
HTMLInputElement* HTMLInputElement::UploadButton() const {
return input_type_view_->UploadButton();
} }
String HTMLInputElement::SanitizeValue(const String& proposed_value) const { String HTMLInputElement::SanitizeValue(const String& proposed_value) const {
......
...@@ -235,6 +235,10 @@ class CORE_EXPORT HTMLInputElement ...@@ -235,6 +235,10 @@ class CORE_EXPORT HTMLInputElement
bool CanReceiveDroppedFiles() const; bool CanReceiveDroppedFiles() const;
void SetCanReceiveDroppedFiles(bool); void SetCanReceiveDroppedFiles(bool);
// Returns 'Choose File(s)' button in a file control. This returns
// nullptr for other input types.
HTMLInputElement* UploadButton() const;
void OnSearch(); void OnSearch();
void UpdateClearButtonVisibility(); void UpdateClearButtonVisibility();
......
...@@ -128,6 +128,10 @@ void InputTypeView::DestroyShadowSubtree() { ...@@ -128,6 +128,10 @@ void InputTypeView::DestroyShadowSubtree() {
root->RemoveChildren(); root->RemoveChildren();
} }
HTMLInputElement* InputTypeView::UploadButton() const {
return nullptr;
}
void InputTypeView::AltAttributeChanged() {} void InputTypeView::AltAttributeChanged() {}
void InputTypeView::SrcAttributeChanged() {} void InputTypeView::SrcAttributeChanged() {}
......
...@@ -106,9 +106,14 @@ class CORE_EXPORT InputTypeView : public GarbageCollectedMixin { ...@@ -106,9 +106,14 @@ class CORE_EXPORT InputTypeView : public GarbageCollectedMixin {
virtual TextDirection ComputedTextDirection(); virtual TextDirection ComputedTextDirection();
virtual void StartResourceLoading(); virtual void StartResourceLoading();
virtual void ClosePopupView(); virtual void ClosePopupView();
// Functions for shadow trees
virtual bool NeedsShadowSubtree() const; virtual bool NeedsShadowSubtree() const;
virtual void CreateShadowSubtree(); virtual void CreateShadowSubtree();
virtual void DestroyShadowSubtree(); virtual void DestroyShadowSubtree();
virtual HTMLInputElement* UploadButton() const;
virtual void MinOrMaxAttributeChanged(); virtual void MinOrMaxAttributeChanged();
virtual void StepAttributeChanged(); virtual void StepAttributeChanged();
virtual void AltAttributeChanged(); virtual void AltAttributeChanged();
......
...@@ -39,8 +39,7 @@ const int kDefaultWidthNumChars = 34; ...@@ -39,8 +39,7 @@ const int kDefaultWidthNumChars = 34;
const int kButtonShadowHeight = 2; const int kButtonShadowHeight = 2;
LayoutFileUploadControl::LayoutFileUploadControl(HTMLInputElement* input) LayoutFileUploadControl::LayoutFileUploadControl(HTMLInputElement* input)
: LayoutBlockFlow(input), : LayoutBlockFlow(input) {}
can_receive_dropped_files_(input->CanReceiveDroppedFiles()) {}
LayoutFileUploadControl::~LayoutFileUploadControl() = default; LayoutFileUploadControl::~LayoutFileUploadControl() = default;
...@@ -48,14 +47,6 @@ void LayoutFileUploadControl::UpdateFromElement() { ...@@ -48,14 +47,6 @@ void LayoutFileUploadControl::UpdateFromElement() {
auto* input = To<HTMLInputElement>(GetNode()); auto* input = To<HTMLInputElement>(GetNode());
DCHECK_EQ(input->type(), input_type_names::kFile); DCHECK_EQ(input->type(), input_type_names::kFile);
if (HTMLInputElement* button = UploadButton()) {
bool new_can_receive_dropped_files_state = input->CanReceiveDroppedFiles();
if (can_receive_dropped_files_ != new_can_receive_dropped_files_state) {
can_receive_dropped_files_ = new_can_receive_dropped_files_state;
button->SetActive(new_can_receive_dropped_files_state);
}
}
// This only supports clearing out the files, but that's OK because for // This only supports clearing out the files, but that's OK because for
// security reasons that's the only change the DOM is allowed to make. // security reasons that's the only change the DOM is allowed to make.
FileList* files = input->files(); FileList* files = input->files();
...@@ -159,18 +150,7 @@ PositionWithAffinity LayoutFileUploadControl::PositionForPoint( ...@@ -159,18 +150,7 @@ PositionWithAffinity LayoutFileUploadControl::PositionForPoint(
} }
HTMLInputElement* LayoutFileUploadControl::UploadButton() const { HTMLInputElement* LayoutFileUploadControl::UploadButton() const {
// FIXME: This should be on HTMLInputElement as an API like return To<HTMLInputElement>(GetNode())->UploadButton();
// innerButtonElement().
auto* input = To<HTMLInputElement>(GetNode());
return DynamicTo<HTMLInputElement>(
input->UserAgentShadowRoot()->firstChild());
}
String LayoutFileUploadControl::ButtonValue() {
if (HTMLInputElement* button = UploadButton())
return button->value();
return String();
} }
String LayoutFileUploadControl::FileTextValue() const { String LayoutFileUploadControl::FileTextValue() const {
......
...@@ -44,11 +44,9 @@ class CORE_EXPORT LayoutFileUploadControl final : public LayoutBlockFlow { ...@@ -44,11 +44,9 @@ class CORE_EXPORT LayoutFileUploadControl final : public LayoutBlockFlow {
LayoutBlockFlow::IsOfType(type); LayoutBlockFlow::IsOfType(type);
} }
String ButtonValue();
String FileTextValue() const; String FileTextValue() const;
HTMLInputElement* UploadButton() const; HTMLInputElement* UploadButton() const;
int UploadButtonWidth();
bool HasControlClip() const override { return true; } bool HasControlClip() const override { return true; }
PhysicalRect ControlClipRect(const PhysicalOffset&) const override; PhysicalRect ControlClipRect(const PhysicalOffset&) const override;
...@@ -75,8 +73,6 @@ class CORE_EXPORT LayoutFileUploadControl final : public LayoutBlockFlow { ...@@ -75,8 +73,6 @@ class CORE_EXPORT LayoutFileUploadControl final : public LayoutBlockFlow {
int MaxFilenameWidth() const; int MaxFilenameWidth() const;
PositionWithAffinity PositionForPoint(const PhysicalOffset&) const override; PositionWithAffinity PositionForPoint(const PhysicalOffset&) const override;
bool can_receive_dropped_files_;
}; };
DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutFileUploadControl, IsFileUploadControl()); DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutFileUploadControl, IsFileUploadControl());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment