1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "nsFileControlFrame.h"
8
9 #include "nsGkAtoms.h"
10 #include "nsCOMPtr.h"
11 #include "mozilla/dom/BlobImpl.h"
12 #include "mozilla/dom/Document.h"
13 #include "mozilla/dom/NodeInfo.h"
14 #include "mozilla/dom/Element.h"
15 #include "mozilla/dom/DOMStringList.h"
16 #include "mozilla/dom/DataTransfer.h"
17 #include "mozilla/dom/Directory.h"
18 #include "mozilla/dom/DragEvent.h"
19 #include "mozilla/dom/Event.h"
20 #include "mozilla/dom/FileList.h"
21 #include "mozilla/dom/HTMLButtonElement.h"
22 #include "mozilla/dom/HTMLInputElement.h"
23 #include "mozilla/dom/MutationEventBinding.h"
24 #include "mozilla/EventStates.h"
25 #include "mozilla/intl/Segmenter.h"
26 #include "mozilla/Preferences.h"
27 #include "mozilla/PresShell.h"
28 #include "mozilla/StaticPrefs_dom.h"
29 #include "mozilla/TextEditor.h"
30 #include "nsNodeInfoManager.h"
31 #include "nsContentCreatorFunctions.h"
32 #include "nsContentUtils.h"
33 #include "nsIFile.h"
34 #include "nsLayoutUtils.h"
35 #include "nsTextNode.h"
36 #include "nsTextFrame.h"
37 #include "gfxContext.h"
38
39 using namespace mozilla;
40 using namespace mozilla::dom;
41
NS_NewFileControlFrame(PresShell * aPresShell,ComputedStyle * aStyle)42 nsIFrame* NS_NewFileControlFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
43 return new (aPresShell)
44 nsFileControlFrame(aStyle, aPresShell->GetPresContext());
45 }
46
NS_IMPL_FRAMEARENA_HELPERS(nsFileControlFrame)47 NS_IMPL_FRAMEARENA_HELPERS(nsFileControlFrame)
48
49 nsFileControlFrame::nsFileControlFrame(ComputedStyle* aStyle,
50 nsPresContext* aPresContext)
51 : nsBlockFrame(aStyle, aPresContext, kClassID) {
52 AddStateBits(NS_BLOCK_FLOAT_MGR);
53 }
54
Init(nsIContent * aContent,nsContainerFrame * aParent,nsIFrame * aPrevInFlow)55 void nsFileControlFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
56 nsIFrame* aPrevInFlow) {
57 nsBlockFrame::Init(aContent, aParent, aPrevInFlow);
58
59 mMouseListener = new DnDListener(this);
60 }
61
CropTextToWidth(gfxContext & aRenderingContext,const nsIFrame * aFrame,nscoord aWidth,nsString & aText)62 bool nsFileControlFrame::CropTextToWidth(gfxContext& aRenderingContext,
63 const nsIFrame* aFrame, nscoord aWidth,
64 nsString& aText) {
65 if (aText.IsEmpty()) {
66 return false;
67 }
68
69 RefPtr<nsFontMetrics> fm =
70 nsLayoutUtils::GetFontMetricsForFrame(aFrame, 1.0f);
71
72 // see if the text will completely fit in the width given
73 if (const nscoord textWidth = nsLayoutUtils::AppUnitWidthOfStringBidi(
74 aText, aFrame, *fm, aRenderingContext);
75 textWidth <= aWidth) {
76 return false;
77 }
78
79 DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
80 const nsDependentString& kEllipsis = nsContentUtils::GetLocalizedEllipsis();
81
82 // see if the width is even smaller than the ellipsis
83 fm->SetTextRunRTL(false);
84 const nscoord ellipsisWidth =
85 nsLayoutUtils::AppUnitWidthOfString(kEllipsis, *fm, drawTarget);
86 if (ellipsisWidth >= aWidth) {
87 aText = kEllipsis;
88 return true;
89 }
90
91 // determine how much of the string will fit in the max width
92 nscoord totalWidth = ellipsisWidth;
93 const Span text(aText);
94 intl::GraphemeClusterBreakIteratorUtf16 leftIter(text);
95 intl::GraphemeClusterBreakReverseIteratorUtf16 rightIter(text);
96 uint32_t leftPos = 0;
97 uint32_t rightPos = aText.Length();
98 nsAutoString leftString, rightString;
99
100 while (leftPos < rightPos) {
101 Maybe<uint32_t> pos = leftIter.Next();
102 Span chars = text.FromTo(leftPos, *pos);
103 nscoord charWidth =
104 nsLayoutUtils::AppUnitWidthOfString(chars, *fm, drawTarget);
105 if (totalWidth + charWidth > aWidth) {
106 break;
107 }
108
109 leftString.Append(chars);
110 leftPos = *pos;
111 totalWidth += charWidth;
112
113 if (leftPos >= rightPos) {
114 break;
115 }
116
117 pos = rightIter.Next();
118 chars = text.FromTo(*pos, rightPos);
119 charWidth = nsLayoutUtils::AppUnitWidthOfString(chars, *fm, drawTarget);
120 if (totalWidth + charWidth > aWidth) {
121 break;
122 }
123
124 rightString.Insert(chars, 0);
125 rightPos = *pos;
126 totalWidth += charWidth;
127 }
128
129 aText = leftString + kEllipsis + rightString;
130 return true;
131 }
132
Reflow(nsPresContext * aPresContext,ReflowOutput & aMetrics,const ReflowInput & aReflowInput,nsReflowStatus & aStatus)133 void nsFileControlFrame::Reflow(nsPresContext* aPresContext,
134 ReflowOutput& aMetrics,
135 const ReflowInput& aReflowInput,
136 nsReflowStatus& aStatus) {
137 // Restore the uncropped filename.
138 nsAutoString filename;
139 HTMLInputElement::FromNode(mContent)->GetDisplayFileName(filename);
140
141 bool done = false;
142 while (true) {
143 UpdateDisplayedValue(filename, false); // update the text node
144 AddStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
145 LinesBegin()->MarkDirty();
146 nsBlockFrame::Reflow(aPresContext, aMetrics, aReflowInput, aStatus);
147 if (done) {
148 break;
149 }
150 nscoord lineISize = LinesBegin()->ISize();
151 const auto cbWM = aMetrics.GetWritingMode();
152 const auto wm = GetWritingMode();
153 nscoord iSize =
154 wm.IsOrthogonalTo(cbWM) ? aMetrics.BSize(cbWM) : aMetrics.ISize(cbWM);
155 auto bp = GetLogicalUsedBorderAndPadding(wm);
156 nscoord contentISize = iSize - bp.IStartEnd(wm);
157 if (lineISize > contentISize) {
158 // The filename overflows - crop it and reflow again (once).
159 // NOTE: the label frame might have bidi-continuations
160 auto* labelFrame = mTextContent->GetPrimaryFrame();
161 nscoord labelBP =
162 labelFrame->GetLogicalUsedBorderAndPadding(wm).IStartEnd(wm);
163 auto* lastLabelCont = labelFrame->LastContinuation();
164 if (lastLabelCont != labelFrame) {
165 labelBP +=
166 lastLabelCont->GetLogicalUsedBorderAndPadding(wm).IStartEnd(wm);
167 }
168 nscoord availableISizeForLabel = contentISize;
169 if (auto* buttonFrame = mBrowseFilesOrDirs->GetPrimaryFrame()) {
170 availableISizeForLabel -=
171 buttonFrame->ISize(wm) +
172 buttonFrame->GetLogicalUsedMargin(wm).IStartEnd(wm);
173 }
174 if (CropTextToWidth(*aReflowInput.mRenderingContext, labelFrame,
175 availableISizeForLabel - labelBP, filename)) {
176 nsBlockFrame::DidReflow(aPresContext, &aReflowInput);
177 aStatus.Reset();
178 labelFrame->MarkSubtreeDirty();
179 labelFrame->AddStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
180 mCachedMinISize = NS_INTRINSIC_ISIZE_UNKNOWN;
181 mCachedPrefISize = NS_INTRINSIC_ISIZE_UNKNOWN;
182 done = true;
183 continue;
184 }
185 }
186 break;
187 }
188 }
189
DestroyFrom(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)190 void nsFileControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
191 PostDestroyData& aPostDestroyData) {
192 NS_ENSURE_TRUE_VOID(mContent);
193
194 // Remove the events.
195 if (mContent) {
196 mContent->RemoveSystemEventListener(u"drop"_ns, mMouseListener, false);
197 mContent->RemoveSystemEventListener(u"dragover"_ns, mMouseListener, false);
198 }
199
200 aPostDestroyData.AddAnonymousContent(mTextContent.forget());
201 aPostDestroyData.AddAnonymousContent(mBrowseFilesOrDirs.forget());
202
203 mMouseListener->ForgetFrame();
204 nsBlockFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
205 }
206
MakeAnonButton(Document * aDoc,const char * labelKey,HTMLInputElement * aInputElement,const nsAString & aAccessKey)207 static already_AddRefed<Element> MakeAnonButton(Document* aDoc,
208 const char* labelKey,
209 HTMLInputElement* aInputElement,
210 const nsAString& aAccessKey) {
211 RefPtr<Element> button = aDoc->CreateHTMLElement(nsGkAtoms::button);
212 // NOTE: SetIsNativeAnonymousRoot() has to be called before setting any
213 // attribute.
214 button->SetIsNativeAnonymousRoot();
215 button->SetPseudoElementType(PseudoStyleType::fileSelectorButton);
216
217 // Set the file picking button text depending on the current locale.
218 nsAutoString buttonTxt;
219 nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
220 labelKey, aDoc, buttonTxt);
221
222 // Set the browse button text. It's a bit of a pain to do because we want to
223 // make sure we are not notifying.
224 RefPtr<nsTextNode> textContent = new (button->NodeInfo()->NodeInfoManager())
225 nsTextNode(button->NodeInfo()->NodeInfoManager());
226
227 textContent->SetText(buttonTxt, false);
228
229 IgnoredErrorResult error;
230 button->AppendChildTo(textContent, false, error);
231 if (error.Failed()) {
232 return nullptr;
233 }
234
235 // Make sure access key and tab order for the element actually redirect to the
236 // file picking button.
237 auto* buttonElement = HTMLButtonElement::FromNode(button);
238 if (!aAccessKey.IsEmpty()) {
239 buttonElement->SetAccessKey(aAccessKey, IgnoreErrors());
240 }
241
242 // We allow tabbing over the input itself, not the button.
243 buttonElement->SetTabIndex(-1, IgnoreErrors());
244 return button.forget();
245 }
246
CreateAnonymousContent(nsTArray<ContentInfo> & aElements)247 nsresult nsFileControlFrame::CreateAnonymousContent(
248 nsTArray<ContentInfo>& aElements) {
249 nsCOMPtr<Document> doc = mContent->GetComposedDoc();
250
251 RefPtr<HTMLInputElement> fileContent =
252 HTMLInputElement::FromNodeOrNull(mContent);
253
254 // The access key is transferred to the "Choose files..." button only. In
255 // effect that access key allows access to the control via that button, then
256 // the user can tab between the two buttons.
257 nsAutoString accessKey;
258 fileContent->GetAccessKey(accessKey);
259
260 mBrowseFilesOrDirs = MakeAnonButton(doc, "Browse", fileContent, accessKey);
261 if (!mBrowseFilesOrDirs) {
262 return NS_ERROR_OUT_OF_MEMORY;
263 }
264 // XXX(Bug 1631371) Check if this should use a fallible operation as it
265 // pretended earlier, or change the return type to void.
266 aElements.AppendElement(mBrowseFilesOrDirs);
267
268 // Create and setup the text showing the selected files.
269 mTextContent = doc->CreateHTMLElement(nsGkAtoms::label);
270 // NOTE: SetIsNativeAnonymousRoot() has to be called before setting any
271 // attribute.
272 mTextContent->SetIsNativeAnonymousRoot();
273 RefPtr<nsTextNode> text =
274 new (doc->NodeInfoManager()) nsTextNode(doc->NodeInfoManager());
275 mTextContent->AppendChildTo(text, false, IgnoreErrors());
276
277 // Update the displayed text to reflect the current element's value.
278 nsAutoString value;
279 fileContent->GetDisplayFileName(value);
280 UpdateDisplayedValue(value, false);
281
282 aElements.AppendElement(mTextContent);
283
284 // We should be able to interact with the element by doing drag and drop.
285 mContent->AddSystemEventListener(u"drop"_ns, mMouseListener, false);
286 mContent->AddSystemEventListener(u"dragover"_ns, mMouseListener, false);
287
288 SyncDisabledState();
289
290 return NS_OK;
291 }
292
AppendAnonymousContentTo(nsTArray<nsIContent * > & aElements,uint32_t aFilter)293 void nsFileControlFrame::AppendAnonymousContentTo(
294 nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
295 if (mBrowseFilesOrDirs) {
296 aElements.AppendElement(mBrowseFilesOrDirs);
297 }
298
299 if (mTextContent) {
300 aElements.AppendElement(mTextContent);
301 }
302 }
303
304 NS_QUERYFRAME_HEAD(nsFileControlFrame)
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)305 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
306 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
307 NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
308
309 void nsFileControlFrame::SetFocus(bool aOn, bool aRepaint) {}
310
AppendBlobImplAsDirectory(nsTArray<OwningFileOrDirectory> & aArray,BlobImpl * aBlobImpl,nsIContent * aContent)311 static void AppendBlobImplAsDirectory(nsTArray<OwningFileOrDirectory>& aArray,
312 BlobImpl* aBlobImpl,
313 nsIContent* aContent) {
314 MOZ_ASSERT(aBlobImpl);
315 MOZ_ASSERT(aBlobImpl->IsDirectory());
316
317 nsAutoString fullpath;
318 ErrorResult err;
319 aBlobImpl->GetMozFullPath(fullpath, SystemCallerGuarantee(), err);
320 if (err.Failed()) {
321 err.SuppressException();
322 return;
323 }
324
325 nsCOMPtr<nsIFile> file;
326 nsresult rv = NS_NewLocalFile(fullpath, true, getter_AddRefs(file));
327 if (NS_WARN_IF(NS_FAILED(rv))) {
328 return;
329 }
330
331 nsPIDOMWindowInner* inner = aContent->OwnerDoc()->GetInnerWindow();
332 if (!inner || !inner->IsCurrentInnerWindow()) {
333 return;
334 }
335
336 RefPtr<Directory> directory = Directory::Create(inner->AsGlobal(), file);
337 MOZ_ASSERT(directory);
338
339 OwningFileOrDirectory* element = aArray.AppendElement();
340 element->SetAsDirectory() = directory;
341 }
342
343 /**
344 * This is called when we receive a drop or a dragover.
345 */
346 NS_IMETHODIMP
HandleEvent(Event * aEvent)347 nsFileControlFrame::DnDListener::HandleEvent(Event* aEvent) {
348 NS_ASSERTION(mFrame, "We should have been unregistered");
349
350 if (aEvent->DefaultPrevented()) {
351 return NS_OK;
352 }
353
354 DragEvent* dragEvent = aEvent->AsDragEvent();
355 if (!dragEvent) {
356 return NS_OK;
357 }
358
359 RefPtr<DataTransfer> dataTransfer = dragEvent->GetDataTransfer();
360 if (!IsValidDropData(dataTransfer)) {
361 return NS_OK;
362 }
363
364 RefPtr<HTMLInputElement> inputElement =
365 HTMLInputElement::FromNode(mFrame->GetContent());
366 bool supportsMultiple =
367 inputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::multiple);
368 if (!CanDropTheseFiles(dataTransfer, supportsMultiple)) {
369 dataTransfer->SetDropEffect(u"none"_ns);
370 aEvent->StopPropagation();
371 return NS_OK;
372 }
373
374 nsAutoString eventType;
375 aEvent->GetType(eventType);
376 if (eventType.EqualsLiteral("dragover")) {
377 // Prevent default if we can accept this drag data
378 aEvent->PreventDefault();
379 return NS_OK;
380 }
381
382 if (eventType.EqualsLiteral("drop")) {
383 aEvent->StopPropagation();
384 aEvent->PreventDefault();
385
386 RefPtr<FileList> fileList =
387 dataTransfer->GetFiles(*nsContentUtils::GetSystemPrincipal());
388
389 RefPtr<BlobImpl> webkitDir;
390 nsresult rv =
391 GetBlobImplForWebkitDirectory(fileList, getter_AddRefs(webkitDir));
392 NS_ENSURE_SUCCESS(rv, NS_OK);
393
394 nsTArray<OwningFileOrDirectory> array;
395 if (webkitDir) {
396 AppendBlobImplAsDirectory(array, webkitDir, inputElement);
397 inputElement->MozSetDndFilesAndDirectories(array);
398 } else {
399 bool blinkFileSystemEnabled =
400 StaticPrefs::dom_webkitBlink_filesystem_enabled();
401 bool dirPickerEnabled = StaticPrefs::dom_input_dirpicker();
402 if (blinkFileSystemEnabled || dirPickerEnabled) {
403 FileList* files = static_cast<FileList*>(fileList.get());
404 if (files) {
405 for (uint32_t i = 0; i < files->Length(); ++i) {
406 File* file = files->Item(i);
407 if (file) {
408 if (file->Impl() && file->Impl()->IsDirectory()) {
409 AppendBlobImplAsDirectory(array, file->Impl(), inputElement);
410 } else {
411 OwningFileOrDirectory* element = array.AppendElement();
412 element->SetAsFile() = file;
413 }
414 }
415 }
416 }
417 }
418
419 // Entries API.
420 if (blinkFileSystemEnabled) {
421 // This is rather ugly. Pass the directories as Files using SetFiles,
422 // but then if blink filesystem API is enabled, it wants
423 // FileOrDirectory array.
424 inputElement->SetFiles(fileList, true);
425 inputElement->UpdateEntries(array);
426 }
427 // Directory Upload API
428 else if (dirPickerEnabled) {
429 inputElement->SetFilesOrDirectories(array, true);
430 }
431 // Normal DnD
432 else {
433 inputElement->SetFiles(fileList, true);
434 }
435
436 RefPtr<TextEditor> textEditor;
437 DebugOnly<nsresult> rvIgnored =
438 nsContentUtils::DispatchInputEvent(inputElement);
439 NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
440 "Failed to dispatch input event");
441 nsContentUtils::DispatchTrustedEvent(
442 inputElement->OwnerDoc(), static_cast<nsINode*>(inputElement),
443 u"change"_ns, CanBubble::eYes, Cancelable::eNo);
444 }
445 }
446
447 return NS_OK;
448 }
449
GetBlobImplForWebkitDirectory(FileList * aFileList,BlobImpl ** aBlobImpl)450 nsresult nsFileControlFrame::DnDListener::GetBlobImplForWebkitDirectory(
451 FileList* aFileList, BlobImpl** aBlobImpl) {
452 *aBlobImpl = nullptr;
453
454 HTMLInputElement* inputElement =
455 HTMLInputElement::FromNode(mFrame->GetContent());
456 bool webkitDirPicker =
457 StaticPrefs::dom_webkitBlink_dirPicker_enabled() &&
458 inputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::webkitdirectory);
459 if (!webkitDirPicker) {
460 return NS_OK;
461 }
462
463 if (!aFileList) {
464 return NS_ERROR_FAILURE;
465 }
466
467 // webkitdirectory doesn't care about the length of the file list but
468 // only about the first item on it.
469 uint32_t len = aFileList->Length();
470 if (len) {
471 File* file = aFileList->Item(0);
472 if (file) {
473 BlobImpl* impl = file->Impl();
474 if (impl && impl->IsDirectory()) {
475 RefPtr<BlobImpl> retVal = impl;
476 retVal.swap(*aBlobImpl);
477 return NS_OK;
478 }
479 }
480 }
481
482 return NS_ERROR_FAILURE;
483 }
484
IsValidDropData(DataTransfer * aDataTransfer)485 bool nsFileControlFrame::DnDListener::IsValidDropData(
486 DataTransfer* aDataTransfer) {
487 if (!aDataTransfer) {
488 return false;
489 }
490
491 // We only support dropping files onto a file upload control
492 return aDataTransfer->HasFile();
493 }
494
CanDropTheseFiles(DataTransfer * aDataTransfer,bool aSupportsMultiple)495 bool nsFileControlFrame::DnDListener::CanDropTheseFiles(
496 DataTransfer* aDataTransfer, bool aSupportsMultiple) {
497 RefPtr<FileList> fileList =
498 aDataTransfer->GetFiles(*nsContentUtils::GetSystemPrincipal());
499
500 RefPtr<BlobImpl> webkitDir;
501 nsresult rv =
502 GetBlobImplForWebkitDirectory(fileList, getter_AddRefs(webkitDir));
503 // Just check if either there isn't webkitdirectory attribute, or
504 // fileList has a directory which can be dropped to the element.
505 // No need to use webkitDir for anything here.
506 NS_ENSURE_SUCCESS(rv, false);
507
508 uint32_t listLength = 0;
509 if (fileList) {
510 listLength = fileList->Length();
511 }
512 return listLength <= 1 || aSupportsMultiple;
513 }
514
GetMinISize(gfxContext * aRenderingContext)515 nscoord nsFileControlFrame::GetMinISize(gfxContext* aRenderingContext) {
516 nscoord result;
517 DISPLAY_MIN_INLINE_SIZE(this, result);
518
519 // Our min inline size is our pref inline size
520 result = GetPrefISize(aRenderingContext);
521 return result;
522 }
523
GetPrefISize(gfxContext * aRenderingContext)524 nscoord nsFileControlFrame::GetPrefISize(gfxContext* aRenderingContext) {
525 nscoord result;
526 DISPLAY_PREF_INLINE_SIZE(this, result);
527
528 // Make sure we measure with the uncropped filename.
529 if (mCachedPrefISize == NS_INTRINSIC_ISIZE_UNKNOWN) {
530 nsAutoString filename;
531 HTMLInputElement::FromNode(mContent)->GetDisplayFileName(filename);
532 UpdateDisplayedValue(filename, false);
533 }
534
535 result = nsBlockFrame::GetPrefISize(aRenderingContext);
536 return result;
537 }
538
SyncDisabledState()539 void nsFileControlFrame::SyncDisabledState() {
540 EventStates eventStates = mContent->AsElement()->State();
541 if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
542 mBrowseFilesOrDirs->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled, u""_ns,
543 true);
544 } else {
545 mBrowseFilesOrDirs->UnsetAttr(kNameSpaceID_None, nsGkAtoms::disabled, true);
546 }
547 }
548
ContentStatesChanged(EventStates aStates)549 void nsFileControlFrame::ContentStatesChanged(EventStates aStates) {
550 if (aStates.HasState(NS_EVENT_STATE_DISABLED)) {
551 nsContentUtils::AddScriptRunner(new SyncDisabledStateEvent(this));
552 }
553 }
554
555 #ifdef DEBUG_FRAME_DUMP
GetFrameName(nsAString & aResult) const556 nsresult nsFileControlFrame::GetFrameName(nsAString& aResult) const {
557 return MakeFrameName(u"FileControl"_ns, aResult);
558 }
559 #endif
560
UpdateDisplayedValue(const nsAString & aValue,bool aNotify)561 void nsFileControlFrame::UpdateDisplayedValue(const nsAString& aValue,
562 bool aNotify) {
563 auto* text = Text::FromNode(mTextContent->GetFirstChild());
564 uint32_t oldLength = aNotify ? 0 : text->TextLength();
565 text->SetText(aValue, aNotify);
566 if (!aNotify) {
567 // We can't notify during Reflow so we need to tell the text frame
568 // about the text content change we just did.
569 if (auto* textFrame = static_cast<nsTextFrame*>(text->GetPrimaryFrame())) {
570 textFrame->NotifyNativeAnonymousTextnodeChange(oldLength);
571 }
572 nsBlockFrame* label = do_QueryFrame(mTextContent->GetPrimaryFrame());
573 if (label && label->LinesBegin() != label->LinesEnd()) {
574 label->AddStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
575 label->LinesBegin()->MarkDirty();
576 }
577 }
578 }
579
SetFormProperty(nsAtom * aName,const nsAString & aValue)580 nsresult nsFileControlFrame::SetFormProperty(nsAtom* aName,
581 const nsAString& aValue) {
582 if (nsGkAtoms::value == aName) {
583 UpdateDisplayedValue(aValue, true);
584 }
585 return NS_OK;
586 }
587
BuildDisplayList(nsDisplayListBuilder * aBuilder,const nsDisplayListSet & aLists)588 void nsFileControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
589 const nsDisplayListSet& aLists) {
590 BuildDisplayListForInline(aBuilder, aLists);
591 }
592
593 #ifdef ACCESSIBILITY
AccessibleType()594 a11y::AccType nsFileControlFrame::AccessibleType() {
595 return a11y::eHTMLFileInputType;
596 }
597 #endif
598
599 ////////////////////////////////////////////////////////////
600 // Mouse listener implementation
601
602 NS_IMPL_ISUPPORTS(nsFileControlFrame::MouseListener, nsIDOMEventListener)
603