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() {
GetElement().Multiple() ? IDS_FORM_MULTIPLE_FILES_BUTTON_LABEL
: IDS_FORM_FILE_BUTTON_LABEL)));
button->SetShadowPseudoId(AtomicString("-webkit-file-upload-button"));
button->SetActive(GetElement().CanReceiveDroppedFiles());
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() {
DCHECK(IsShadowHost(GetElement()));
CHECK(!GetElement().UserAgentShadowRoot()->firstChild() ||
IsA<Element>(GetElement().UserAgentShadowRoot()->firstChild()));
if (Element* button =
To<Element>(GetElement().UserAgentShadowRoot()->firstChild()))
if (Element* button = UploadButton()) {
button->SetBooleanAttribute(html_names::kDisabledAttr,
GetElement().IsDisabledFormControl());
}
}
void FileInputType::MultipleAttributeChanged() {
DCHECK(IsShadowHost(GetElement()));
CHECK(!GetElement().UserAgentShadowRoot()->firstChild() ||
IsA<Element>(GetElement().UserAgentShadowRoot()->firstChild()));
if (Element* button =
To<Element>(GetElement().UserAgentShadowRoot()->firstChild())) {
if (Element* button = UploadButton()) {
button->setAttribute(
html_names::kValueAttr,
AtomicString(GetLocale().QueryString(
......
......@@ -88,6 +88,7 @@ class CORE_EXPORT FileInputType final : public InputType,
bool ReceiveDroppedFiles(const DragData*) override;
String DroppedFileSystemId() override;
void CreateShadowSubtree() override;
HTMLInputElement* UploadButton() const override;
void DisabledAttributeChanged() override;
void MultipleAttributeChanged() override;
String DefaultToolTip(const InputTypeView&) const override;
......
......@@ -1538,8 +1538,12 @@ void HTMLInputElement::SetCanReceiveDroppedFiles(
if (!!can_receive_dropped_files_ == can_receive_dropped_files)
return;
can_receive_dropped_files_ = can_receive_dropped_files;
if (GetLayoutObject())
GetLayoutObject()->UpdateFromElement();
if (HTMLInputElement* button = UploadButton())
button->SetActive(can_receive_dropped_files);
}
HTMLInputElement* HTMLInputElement::UploadButton() const {
return input_type_view_->UploadButton();
}
String HTMLInputElement::SanitizeValue(const String& proposed_value) const {
......
......@@ -235,6 +235,10 @@ class CORE_EXPORT HTMLInputElement
bool CanReceiveDroppedFiles() const;
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 UpdateClearButtonVisibility();
......
......@@ -128,6 +128,10 @@ void InputTypeView::DestroyShadowSubtree() {
root->RemoveChildren();
}
HTMLInputElement* InputTypeView::UploadButton() const {
return nullptr;
}
void InputTypeView::AltAttributeChanged() {}
void InputTypeView::SrcAttributeChanged() {}
......
......@@ -106,9 +106,14 @@ class CORE_EXPORT InputTypeView : public GarbageCollectedMixin {
virtual TextDirection ComputedTextDirection();
virtual void StartResourceLoading();
virtual void ClosePopupView();
// Functions for shadow trees
virtual bool NeedsShadowSubtree() const;
virtual void CreateShadowSubtree();
virtual void DestroyShadowSubtree();
virtual HTMLInputElement* UploadButton() const;
virtual void MinOrMaxAttributeChanged();
virtual void StepAttributeChanged();
virtual void AltAttributeChanged();
......
......@@ -39,8 +39,7 @@ const int kDefaultWidthNumChars = 34;
const int kButtonShadowHeight = 2;
LayoutFileUploadControl::LayoutFileUploadControl(HTMLInputElement* input)
: LayoutBlockFlow(input),
can_receive_dropped_files_(input->CanReceiveDroppedFiles()) {}
: LayoutBlockFlow(input) {}
LayoutFileUploadControl::~LayoutFileUploadControl() = default;
......@@ -48,14 +47,6 @@ void LayoutFileUploadControl::UpdateFromElement() {
auto* input = To<HTMLInputElement>(GetNode());
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
// security reasons that's the only change the DOM is allowed to make.
FileList* files = input->files();
......@@ -159,18 +150,7 @@ PositionWithAffinity LayoutFileUploadControl::PositionForPoint(
}
HTMLInputElement* LayoutFileUploadControl::UploadButton() const {
// FIXME: This should be on HTMLInputElement as an API like
// innerButtonElement().
auto* input = To<HTMLInputElement>(GetNode());
return DynamicTo<HTMLInputElement>(
input->UserAgentShadowRoot()->firstChild());
}
String LayoutFileUploadControl::ButtonValue() {
if (HTMLInputElement* button = UploadButton())
return button->value();
return String();
return To<HTMLInputElement>(GetNode())->UploadButton();
}
String LayoutFileUploadControl::FileTextValue() const {
......
......@@ -44,11 +44,9 @@ class CORE_EXPORT LayoutFileUploadControl final : public LayoutBlockFlow {
LayoutBlockFlow::IsOfType(type);
}
String ButtonValue();
String FileTextValue() const;
HTMLInputElement* UploadButton() const;
int UploadButtonWidth();
bool HasControlClip() const override { return true; }
PhysicalRect ControlClipRect(const PhysicalOffset&) const override;
......@@ -75,8 +73,6 @@ class CORE_EXPORT LayoutFileUploadControl final : public LayoutBlockFlow {
int MaxFilenameWidth() const;
PositionWithAffinity PositionForPoint(const PhysicalOffset&) const override;
bool can_receive_dropped_files_;
};
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