2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-09-23 21:16:51 +04:00
|
|
|
|
|
|
|
#include "nsFileControlFrame.h"
|
2000-05-02 07:40:44 +04:00
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
1999-02-18 03:13:39 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2014-06-20 06:01:40 +04:00
|
|
|
#include "mozilla/dom/NodeInfo.h"
|
2013-12-03 18:40:10 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-03-13 23:23:59 +03:00
|
|
|
#include "mozilla/dom/DOMStringList.h"
|
|
|
|
#include "mozilla/dom/DataTransfer.h"
|
|
|
|
#include "mozilla/dom/Directory.h"
|
|
|
|
#include "mozilla/dom/DragEvent.h"
|
2018-04-20 07:49:29 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2018-03-13 23:23:59 +03:00
|
|
|
#include "mozilla/dom/FileList.h"
|
2013-08-08 00:23:08 +04:00
|
|
|
#include "mozilla/dom/HTMLButtonElement.h"
|
2013-03-28 23:41:32 +04:00
|
|
|
#include "mozilla/dom/HTMLInputElement.h"
|
2018-02-09 19:17:10 +03:00
|
|
|
#include "mozilla/dom/MutationEventBinding.h"
|
2015-07-10 20:56:36 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2019-04-16 10:24:49 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
2004-06-25 16:26:02 +04:00
|
|
|
#include "nsNodeInfoManager.h"
|
|
|
|
#include "nsContentCreatorFunctions.h"
|
2004-12-17 23:40:48 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-04-02 20:00:49 +03:00
|
|
|
#include "nsUnicodeProperties.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-04-04 16:01:08 +04:00
|
|
|
#include "nsTextNode.h"
|
2018-10-06 20:31:51 +03:00
|
|
|
#include "nsTextFrame.h"
|
2011-06-01 10:06:38 +04:00
|
|
|
|
2012-10-16 02:44:34 +04:00
|
|
|
using namespace mozilla;
|
2013-03-28 23:41:32 +04:00
|
|
|
using namespace mozilla::dom;
|
2010-10-25 16:17:38 +04:00
|
|
|
|
2019-04-16 10:24:49 +03:00
|
|
|
nsIFrame* NS_NewFileControlFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
|
2019-02-05 19:45:54 +03:00
|
|
|
return new (aPresShell)
|
|
|
|
nsFileControlFrame(aStyle, aPresShell->GetPresContext());
|
1998-09-23 21:16:51 +04:00
|
|
|
}
|
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsFileControlFrame)
|
|
|
|
|
2019-02-05 19:45:54 +03:00
|
|
|
nsFileControlFrame::nsFileControlFrame(ComputedStyle* aStyle,
|
|
|
|
nsPresContext* aPresContext)
|
|
|
|
: nsBlockFrame(aStyle, aPresContext, kClassID) {
|
2009-01-19 21:31:32 +03:00
|
|
|
AddStateBits(NS_BLOCK_FLOAT_MGR);
|
1999-07-01 02:17:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-25 02:20:40 +04:00
|
|
|
void nsFileControlFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow) {
|
2013-03-20 05:47:48 +04:00
|
|
|
nsBlockFrame::Init(aContent, aParent, aPrevInFlow);
|
2006-11-14 01:05:25 +03:00
|
|
|
|
2013-03-27 15:32:00 +04:00
|
|
|
mMouseListener = new DnDListener(this);
|
2006-11-14 01:05:25 +03:00
|
|
|
}
|
|
|
|
|
2018-10-06 20:31:51 +03:00
|
|
|
bool nsFileControlFrame::CropTextToWidth(gfxContext& aRenderingContext,
|
|
|
|
const nsIFrame* aFrame, nscoord aWidth,
|
|
|
|
nsString& aText) {
|
|
|
|
if (aText.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsFontMetrics> fm =
|
|
|
|
nsLayoutUtils::GetFontMetricsForFrame(aFrame, 1.0f);
|
|
|
|
|
|
|
|
// see if the text will completely fit in the width given
|
|
|
|
nscoord textWidth = nsLayoutUtils::AppUnitWidthOfStringBidi(
|
|
|
|
aText, aFrame, *fm, aRenderingContext);
|
|
|
|
if (textWidth <= aWidth) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
|
|
|
|
const nsDependentString& kEllipsis = nsContentUtils::GetLocalizedEllipsis();
|
|
|
|
|
|
|
|
// see if the width is even smaller than the ellipsis
|
|
|
|
fm->SetTextRunRTL(false);
|
|
|
|
textWidth = nsLayoutUtils::AppUnitWidthOfString(kEllipsis, *fm, drawTarget);
|
|
|
|
if (textWidth >= aWidth) {
|
|
|
|
aText = kEllipsis;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine how much of the string will fit in the max width
|
|
|
|
nscoord totalWidth = textWidth;
|
|
|
|
using mozilla::unicode::ClusterIterator;
|
|
|
|
using mozilla::unicode::ClusterReverseIterator;
|
|
|
|
ClusterIterator leftIter(aText.Data(), aText.Length());
|
|
|
|
ClusterReverseIterator rightIter(aText.Data(), aText.Length());
|
|
|
|
const char16_t* leftPos = leftIter;
|
|
|
|
const char16_t* rightPos = rightIter;
|
|
|
|
const char16_t* pos;
|
|
|
|
ptrdiff_t length;
|
|
|
|
nsAutoString leftString, rightString;
|
|
|
|
|
|
|
|
while (leftPos < rightPos) {
|
|
|
|
leftIter.Next();
|
|
|
|
pos = leftIter;
|
|
|
|
length = pos - leftPos;
|
|
|
|
textWidth =
|
|
|
|
nsLayoutUtils::AppUnitWidthOfString(leftPos, length, *fm, drawTarget);
|
|
|
|
if (totalWidth + textWidth > aWidth) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
leftString.Append(leftPos, length);
|
|
|
|
leftPos = pos;
|
|
|
|
totalWidth += textWidth;
|
|
|
|
|
|
|
|
if (leftPos >= rightPos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rightIter.Next();
|
|
|
|
pos = rightIter;
|
|
|
|
length = rightPos - pos;
|
|
|
|
textWidth =
|
|
|
|
nsLayoutUtils::AppUnitWidthOfString(pos, length, *fm, drawTarget);
|
|
|
|
if (totalWidth + textWidth > aWidth) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rightString.Insert(pos, 0, length);
|
|
|
|
rightPos = pos;
|
|
|
|
totalWidth += textWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
aText = leftString + kEllipsis + rightString;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFileControlFrame::Reflow(nsPresContext* aPresContext,
|
|
|
|
ReflowOutput& aMetrics,
|
|
|
|
const ReflowInput& aReflowInput,
|
|
|
|
nsReflowStatus& aStatus) {
|
|
|
|
// Restore the uncropped filename.
|
|
|
|
nsAutoString filename;
|
|
|
|
HTMLInputElement::FromNode(mContent)->GetDisplayFileName(filename);
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while (true) {
|
|
|
|
UpdateDisplayedValue(filename, false); // update the text node
|
|
|
|
AddStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
|
|
|
|
LinesBegin()->MarkDirty();
|
|
|
|
nsBlockFrame::Reflow(aPresContext, aMetrics, aReflowInput, aStatus);
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nscoord lineISize = LinesBegin()->ISize();
|
|
|
|
const auto cbWM = aMetrics.GetWritingMode();
|
|
|
|
const auto wm = GetWritingMode();
|
|
|
|
nscoord iSize =
|
|
|
|
wm.IsOrthogonalTo(cbWM) ? aMetrics.BSize(cbWM) : aMetrics.ISize(cbWM);
|
|
|
|
auto bp = GetLogicalUsedBorderAndPadding(wm);
|
|
|
|
nscoord contentISize = iSize - bp.IStartEnd(wm);
|
|
|
|
if (lineISize > contentISize) {
|
|
|
|
// The filename overflows - crop it and reflow again (once).
|
|
|
|
// NOTE: the label frame might have bidi-continuations
|
|
|
|
auto* labelFrame = mTextContent->GetPrimaryFrame();
|
|
|
|
nscoord labelBP =
|
|
|
|
labelFrame->GetLogicalUsedBorderAndPadding(wm).IStartEnd(wm);
|
|
|
|
auto* lastLabelCont = labelFrame->LastContinuation();
|
|
|
|
if (lastLabelCont != labelFrame) {
|
|
|
|
labelBP +=
|
|
|
|
lastLabelCont->GetLogicalUsedBorderAndPadding(wm).IStartEnd(wm);
|
|
|
|
}
|
|
|
|
auto* buttonFrame = mBrowseFilesOrDirs->GetPrimaryFrame();
|
|
|
|
nscoord availableISizeForLabel =
|
|
|
|
contentISize - buttonFrame->ISize(wm) -
|
|
|
|
buttonFrame->GetLogicalUsedMargin(wm).IStartEnd(wm);
|
|
|
|
if (CropTextToWidth(*aReflowInput.mRenderingContext, labelFrame,
|
|
|
|
availableISizeForLabel - labelBP, filename)) {
|
|
|
|
nsBlockFrame::DidReflow(aPresContext, &aReflowInput);
|
|
|
|
aStatus.Reset();
|
2019-07-02 00:56:43 +03:00
|
|
|
labelFrame->MarkSubtreeDirty();
|
|
|
|
labelFrame->AddStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
|
2019-05-03 20:34:46 +03:00
|
|
|
mCachedMinISize = NS_INTRINSIC_ISIZE_UNKNOWN;
|
2019-05-03 20:34:55 +03:00
|
|
|
mCachedPrefISize = NS_INTRINSIC_ISIZE_UNKNOWN;
|
2018-10-06 20:31:51 +03:00
|
|
|
done = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
void nsFileControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
|
|
|
|
PostDestroyData& aPostDestroyData) {
|
2018-08-13 12:42:00 +03:00
|
|
|
NS_ENSURE_TRUE_VOID(mContent);
|
2008-01-11 00:55:38 +03:00
|
|
|
|
2013-03-22 21:15:13 +04:00
|
|
|
// Remove the events.
|
2011-11-29 08:39:21 +04:00
|
|
|
if (mContent) {
|
|
|
|
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("drop"),
|
|
|
|
mMouseListener, false);
|
|
|
|
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("dragover"),
|
|
|
|
mMouseListener, false);
|
2011-06-01 10:06:38 +04:00
|
|
|
}
|
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
aPostDestroyData.AddAnonymousContent(mTextContent.forget());
|
|
|
|
aPostDestroyData.AddAnonymousContent(mBrowseFilesOrDirs.forget());
|
2006-11-14 01:05:25 +03:00
|
|
|
|
|
|
|
mMouseListener->ForgetFrame();
|
2017-11-07 03:20:33 +03:00
|
|
|
nsBlockFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
|
2002-12-20 08:40:37 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
static already_AddRefed<Element> MakeAnonButton(Document* aDoc,
|
2015-07-10 20:56:36 +03:00
|
|
|
const char* labelKey,
|
|
|
|
HTMLInputElement* aInputElement,
|
|
|
|
const nsAString& aAccessKey) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Element> button = aDoc->CreateHTMLElement(nsGkAtoms::button);
|
2013-09-25 00:29:27 +04:00
|
|
|
// NOTE: SetIsNativeAnonymousRoot() has to be called before setting any
|
|
|
|
// attribute.
|
2015-07-10 20:56:36 +03:00
|
|
|
button->SetIsNativeAnonymousRoot();
|
2001-02-20 00:50:04 +03:00
|
|
|
|
2013-02-09 14:48:20 +04:00
|
|
|
// Set the file picking button text depending on the current locale.
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString buttonTxt;
|
2019-11-04 19:56:27 +03:00
|
|
|
nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
|
|
|
|
labelKey, aDoc, buttonTxt);
|
2013-03-29 16:32:47 +04:00
|
|
|
|
|
|
|
// Set the browse button text. It's a bit of a pain to do because we want to
|
|
|
|
// make sure we are not notifying.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsTextNode> textContent =
|
2015-07-10 20:56:36 +03:00
|
|
|
new nsTextNode(button->NodeInfo()->NodeInfoManager());
|
2013-03-29 16:32:47 +04:00
|
|
|
|
|
|
|
textContent->SetText(buttonTxt, false);
|
|
|
|
|
2015-07-10 20:56:36 +03:00
|
|
|
nsresult rv = button->AppendChildTo(textContent, false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-02-09 14:48:20 +04:00
|
|
|
|
2013-02-09 19:09:14 +04:00
|
|
|
// Make sure access key and tab order for the element actually redirect to the
|
|
|
|
// file picking button.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLButtonElement> buttonElement =
|
2018-03-22 00:39:04 +03:00
|
|
|
HTMLButtonElement::FromNodeOrNull(button);
|
2015-07-10 20:56:36 +03:00
|
|
|
|
|
|
|
if (!aAccessKey.IsEmpty()) {
|
2018-02-01 22:21:14 +03:00
|
|
|
buttonElement->SetAccessKey(aAccessKey, IgnoreErrors());
|
2015-07-10 20:56:36 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 19:22:03 +03:00
|
|
|
// We allow tabbing over the input itself, not the button.
|
|
|
|
buttonElement->SetTabIndex(-1, IgnoreErrors());
|
2015-07-10 20:56:36 +03:00
|
|
|
return button.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsFileControlFrame::CreateAnonymousContent(
|
|
|
|
nsTArray<ContentInfo>& aElements) {
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mContent->GetComposedDoc();
|
2015-07-10 20:56:36 +03:00
|
|
|
|
2018-03-22 00:39:04 +03:00
|
|
|
RefPtr<HTMLInputElement> fileContent =
|
|
|
|
HTMLInputElement::FromNodeOrNull(mContent);
|
2013-02-09 19:09:14 +04:00
|
|
|
|
2015-07-10 20:56:36 +03:00
|
|
|
// The access key is transferred to the "Choose files..." button only. In
|
|
|
|
// effect that access key allows access to the control via that button, then
|
|
|
|
// the user can tab between the two buttons.
|
2013-02-09 19:09:14 +04:00
|
|
|
nsAutoString accessKey;
|
|
|
|
fileContent->GetAccessKey(accessKey);
|
|
|
|
|
2016-06-29 19:18:42 +03:00
|
|
|
mBrowseFilesOrDirs = MakeAnonButton(doc, "Browse", fileContent, accessKey);
|
|
|
|
if (!mBrowseFilesOrDirs || !aElements.AppendElement(mBrowseFilesOrDirs)) {
|
2007-02-18 20:34:09 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-02-09 19:09:14 +04:00
|
|
|
}
|
2007-02-18 20:34:09 +03:00
|
|
|
|
2013-02-09 19:09:14 +04:00
|
|
|
// Create and setup the text showing the selected files.
|
2018-10-06 20:31:51 +03:00
|
|
|
mTextContent = doc->CreateHTMLElement(nsGkAtoms::label);
|
2013-09-25 00:29:27 +04:00
|
|
|
// NOTE: SetIsNativeAnonymousRoot() has to be called before setting any
|
|
|
|
// attribute.
|
|
|
|
mTextContent->SetIsNativeAnonymousRoot();
|
2018-10-06 20:31:51 +03:00
|
|
|
RefPtr<nsTextNode> text = new nsTextNode(doc->NodeInfoManager());
|
|
|
|
mTextContent->AppendChildTo(text, false);
|
2007-02-18 20:34:09 +03:00
|
|
|
|
2013-02-09 19:09:14 +04:00
|
|
|
// Update the displayed text to reflect the current element's value.
|
2010-09-05 22:00:05 +04:00
|
|
|
nsAutoString value;
|
2019-04-09 19:05:18 +03:00
|
|
|
fileContent->GetDisplayFileName(value);
|
2013-02-09 18:57:30 +04:00
|
|
|
UpdateDisplayedValue(value, false);
|
1999-07-01 02:17:43 +04:00
|
|
|
|
2018-10-06 20:31:51 +03:00
|
|
|
aElements.AppendElement(mTextContent);
|
1999-07-01 02:17:43 +04:00
|
|
|
|
2013-03-27 15:32:00 +04:00
|
|
|
// We should be able to interact with the element by doing drag and drop.
|
2011-11-29 08:39:21 +04:00
|
|
|
mContent->AddSystemEventListener(NS_LITERAL_STRING("drop"), mMouseListener,
|
|
|
|
false);
|
|
|
|
mContent->AddSystemEventListener(NS_LITERAL_STRING("dragover"),
|
|
|
|
mMouseListener, false);
|
2007-02-18 20:34:09 +03:00
|
|
|
|
2010-12-14 21:00:57 +03:00
|
|
|
SyncDisabledState();
|
2000-01-14 02:34:01 +03:00
|
|
|
|
1999-07-01 02:17:43 +04:00
|
|
|
return NS_OK;
|
1998-09-23 21:16:51 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
void nsFileControlFrame::AppendAnonymousContentTo(
|
|
|
|
nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
|
2016-06-29 19:18:42 +03:00
|
|
|
if (mBrowseFilesOrDirs) {
|
|
|
|
aElements.AppendElement(mBrowseFilesOrDirs);
|
2014-07-16 22:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTextContent) {
|
|
|
|
aElements.AppendElement(mTextContent);
|
|
|
|
}
|
2010-02-11 20:34:01 +03:00
|
|
|
}
|
|
|
|
|
2009-01-12 22:20:59 +03:00
|
|
|
NS_QUERYFRAME_HEAD(nsFileControlFrame)
|
2018-12-07 23:00:18 +03:00
|
|
|
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
|
|
|
|
NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
|
2009-01-12 22:20:59 +03:00
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
|
1998-09-23 21:16:51 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsFileControlFrame::SetFocus(bool aOn, bool aRepaint) {}
|
1998-10-23 03:00:37 +04:00
|
|
|
|
2016-07-29 14:41:38 +03:00
|
|
|
static void AppendBlobImplAsDirectory(nsTArray<OwningFileOrDirectory>& aArray,
|
|
|
|
BlobImpl* aBlobImpl,
|
|
|
|
nsIContent* aContent) {
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
|
|
|
MOZ_ASSERT(aBlobImpl->IsDirectory());
|
|
|
|
|
|
|
|
nsAutoString fullpath;
|
|
|
|
ErrorResult err;
|
2017-02-01 23:43:37 +03:00
|
|
|
aBlobImpl->GetMozFullPath(fullpath, SystemCallerGuarantee(), err);
|
2016-07-29 14:41:38 +03:00
|
|
|
if (err.Failed()) {
|
|
|
|
err.SuppressException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2017-01-21 09:36:10 +03:00
|
|
|
nsresult rv = NS_NewLocalFile(fullpath, true, getter_AddRefs(file));
|
2016-07-29 14:41:38 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindowInner* inner = aContent->OwnerDoc()->GetInnerWindow();
|
|
|
|
if (!inner || !inner->IsCurrentInnerWindow()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
RefPtr<Directory> directory = Directory::Create(inner->AsGlobal(), file);
|
2016-07-29 14:41:38 +03:00
|
|
|
MOZ_ASSERT(directory);
|
|
|
|
|
|
|
|
OwningFileOrDirectory* element = aArray.AppendElement();
|
|
|
|
element->SetAsDirectory() = directory;
|
|
|
|
}
|
|
|
|
|
2011-06-01 10:06:38 +04:00
|
|
|
/**
|
2013-03-27 15:32:00 +04:00
|
|
|
* This is called when we receive a drop or a dragover.
|
2011-06-01 10:06:38 +04:00
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
nsFileControlFrame::DnDListener::HandleEvent(Event* aEvent) {
|
2011-06-01 10:06:38 +04:00
|
|
|
NS_ASSERTION(mFrame, "We should have been unregistered");
|
2011-06-29 22:07:57 +04:00
|
|
|
|
2018-04-20 07:49:29 +03:00
|
|
|
if (aEvent->DefaultPrevented()) {
|
2011-06-01 10:06:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-27 15:32:00 +04:00
|
|
|
|
2018-04-20 07:49:29 +03:00
|
|
|
DragEvent* dragEvent = aEvent->AsDragEvent();
|
2014-11-18 18:58:08 +03:00
|
|
|
if (!dragEvent) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:23:59 +03:00
|
|
|
RefPtr<DataTransfer> dataTransfer = dragEvent->GetDataTransfer();
|
2014-11-18 18:58:08 +03:00
|
|
|
if (!IsValidDropData(dataTransfer)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:13:50 +03:00
|
|
|
RefPtr<HTMLInputElement> inputElement =
|
2018-03-22 00:39:04 +03:00
|
|
|
HTMLInputElement::FromNode(mFrame->GetContent());
|
2017-12-07 21:13:50 +03:00
|
|
|
bool supportsMultiple =
|
|
|
|
inputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::multiple);
|
2014-11-18 18:58:08 +03:00
|
|
|
if (!CanDropTheseFiles(dataTransfer, supportsMultiple)) {
|
|
|
|
dataTransfer->SetDropEffect(NS_LITERAL_STRING("none"));
|
|
|
|
aEvent->StopPropagation();
|
2011-06-01 10:06:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-27 15:32:00 +04:00
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
2011-06-01 10:06:38 +04:00
|
|
|
if (eventType.EqualsLiteral("dragover")) {
|
|
|
|
// Prevent default if we can accept this drag data
|
|
|
|
aEvent->PreventDefault();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventType.EqualsLiteral("drop")) {
|
|
|
|
aEvent->StopPropagation();
|
|
|
|
aEvent->PreventDefault();
|
|
|
|
|
2018-03-13 23:24:00 +03:00
|
|
|
RefPtr<FileList> fileList =
|
|
|
|
dataTransfer->GetFiles(*nsContentUtils::GetSystemPrincipal());
|
2011-06-10 16:44:53 +04:00
|
|
|
|
2016-07-29 14:41:38 +03:00
|
|
|
RefPtr<BlobImpl> webkitDir;
|
|
|
|
nsresult rv =
|
|
|
|
GetBlobImplForWebkitDirectory(fileList, getter_AddRefs(webkitDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
|
|
|
|
nsTArray<OwningFileOrDirectory> array;
|
|
|
|
if (webkitDir) {
|
2017-12-07 21:13:50 +03:00
|
|
|
AppendBlobImplAsDirectory(array, webkitDir, inputElement);
|
2016-07-29 14:41:38 +03:00
|
|
|
inputElement->MozSetDndFilesAndDirectories(array);
|
|
|
|
} else {
|
|
|
|
bool blinkFileSystemEnabled =
|
2019-06-13 12:00:59 +03:00
|
|
|
StaticPrefs::dom_webkitBlink_filesystem_enabled();
|
|
|
|
bool dirPickerEnabled = StaticPrefs::dom_input_dirpicker();
|
2017-02-16 20:23:33 +03:00
|
|
|
if (blinkFileSystemEnabled || dirPickerEnabled) {
|
2016-07-29 14:41:38 +03:00
|
|
|
FileList* files = static_cast<FileList*>(fileList.get());
|
|
|
|
if (files) {
|
|
|
|
for (uint32_t i = 0; i < files->Length(); ++i) {
|
|
|
|
File* file = files->Item(i);
|
|
|
|
if (file) {
|
|
|
|
if (file->Impl() && file->Impl()->IsDirectory()) {
|
2017-12-07 21:13:50 +03:00
|
|
|
AppendBlobImplAsDirectory(array, file->Impl(), inputElement);
|
2016-07-29 14:41:38 +03:00
|
|
|
} else {
|
|
|
|
OwningFileOrDirectory* element = array.AppendElement();
|
|
|
|
element->SetAsFile() = file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:23:33 +03:00
|
|
|
// Entries API.
|
2016-07-29 14:41:38 +03:00
|
|
|
if (blinkFileSystemEnabled) {
|
2017-02-16 20:23:33 +03:00
|
|
|
// This is rather ugly. Pass the directories as Files using SetFiles,
|
|
|
|
// but then if blink filesystem API is enabled, it wants
|
|
|
|
// FileOrDirectory array.
|
|
|
|
inputElement->SetFiles(fileList, true);
|
2016-07-29 14:41:38 +03:00
|
|
|
inputElement->UpdateEntries(array);
|
|
|
|
}
|
2017-02-16 20:23:33 +03:00
|
|
|
// Directory Upload API
|
|
|
|
else if (dirPickerEnabled) {
|
|
|
|
inputElement->SetFilesOrDirectories(array, true);
|
|
|
|
}
|
|
|
|
// Normal DnD
|
|
|
|
else {
|
|
|
|
inputElement->SetFiles(fileList, true);
|
|
|
|
}
|
|
|
|
|
2018-11-21 06:59:02 +03:00
|
|
|
RefPtr<TextEditor> textEditor;
|
|
|
|
DebugOnly<nsresult> rvIgnored =
|
|
|
|
nsContentUtils::DispatchInputEvent(inputElement);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
|
|
|
|
"Failed to dispatch input event");
|
2017-12-07 21:13:50 +03:00
|
|
|
nsContentUtils::DispatchTrustedEvent(
|
|
|
|
inputElement->OwnerDoc(), static_cast<nsINode*>(inputElement),
|
2018-06-25 19:23:50 +03:00
|
|
|
NS_LITERAL_STRING("change"), CanBubble::eYes, Cancelable::eNo);
|
2016-07-29 14:41:38 +03:00
|
|
|
}
|
2011-06-01 10:06:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-16 22:55:32 +03:00
|
|
|
nsresult nsFileControlFrame::DnDListener::GetBlobImplForWebkitDirectory(
|
2016-07-29 14:41:38 +03:00
|
|
|
FileList* aFileList, BlobImpl** aBlobImpl) {
|
|
|
|
*aBlobImpl = nullptr;
|
|
|
|
|
|
|
|
HTMLInputElement* inputElement =
|
2018-03-22 00:39:04 +03:00
|
|
|
HTMLInputElement::FromNode(mFrame->GetContent());
|
2016-07-29 14:41:38 +03:00
|
|
|
bool webkitDirPicker =
|
2018-09-10 21:36:16 +03:00
|
|
|
StaticPrefs::dom_webkitBlink_dirPicker_enabled() &&
|
2016-07-29 14:41:38 +03:00
|
|
|
inputElement->HasAttr(kNameSpaceID_None, nsGkAtoms::webkitdirectory);
|
|
|
|
if (!webkitDirPicker) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aFileList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// webkitdirectory doesn't care about the length of the file list but
|
|
|
|
// only about the first item on it.
|
2018-03-16 22:55:32 +03:00
|
|
|
uint32_t len = aFileList->Length();
|
2016-07-29 14:41:38 +03:00
|
|
|
if (len) {
|
2018-03-16 22:55:32 +03:00
|
|
|
File* file = aFileList->Item(0);
|
2016-07-29 14:41:38 +03:00
|
|
|
if (file) {
|
|
|
|
BlobImpl* impl = file->Impl();
|
|
|
|
if (impl && impl->IsDirectory()) {
|
|
|
|
RefPtr<BlobImpl> retVal = impl;
|
|
|
|
retVal.swap(*aBlobImpl);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:24:00 +03:00
|
|
|
bool nsFileControlFrame::DnDListener::IsValidDropData(
|
|
|
|
DataTransfer* aDataTransfer) {
|
2018-06-27 19:04:26 +03:00
|
|
|
if (!aDataTransfer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-01 10:06:38 +04:00
|
|
|
// We only support dropping files onto a file upload control
|
2016-10-11 04:07:47 +03:00
|
|
|
nsTArray<nsString> types;
|
2018-03-13 23:24:00 +03:00
|
|
|
aDataTransfer->GetTypes(types, CallerType::System);
|
2015-08-28 22:21:08 +03:00
|
|
|
|
2016-10-11 04:07:47 +03:00
|
|
|
return types.Contains(NS_LITERAL_STRING("Files"));
|
2011-06-01 10:06:38 +04:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:24:00 +03:00
|
|
|
bool nsFileControlFrame::DnDListener::CanDropTheseFiles(
|
|
|
|
DataTransfer* aDataTransfer, bool aSupportsMultiple) {
|
2018-03-13 23:24:00 +03:00
|
|
|
RefPtr<FileList> fileList =
|
2018-03-13 23:24:00 +03:00
|
|
|
aDataTransfer->GetFiles(*nsContentUtils::GetSystemPrincipal());
|
2014-11-18 18:58:08 +03:00
|
|
|
|
2016-07-29 14:41:38 +03:00
|
|
|
RefPtr<BlobImpl> webkitDir;
|
|
|
|
nsresult rv =
|
|
|
|
GetBlobImplForWebkitDirectory(fileList, getter_AddRefs(webkitDir));
|
|
|
|
// Just check if either there isn't webkitdirectory attribute, or
|
|
|
|
// fileList has a directory which can be dropped to the element.
|
|
|
|
// No need to use webkitDir for anything here.
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
2014-11-18 18:58:08 +03:00
|
|
|
uint32_t listLength = 0;
|
|
|
|
if (fileList) {
|
2018-03-16 22:55:32 +03:00
|
|
|
listLength = fileList->Length();
|
2014-11-18 18:58:08 +03:00
|
|
|
}
|
|
|
|
return listLength <= 1 || aSupportsMultiple;
|
|
|
|
}
|
|
|
|
|
2018-10-06 20:31:51 +03:00
|
|
|
nscoord nsFileControlFrame::GetMinISize(gfxContext* aRenderingContext) {
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
nscoord result;
|
2018-09-12 15:35:47 +03:00
|
|
|
DISPLAY_MIN_INLINE_SIZE(this, result);
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
|
2018-09-12 15:35:47 +03:00
|
|
|
// Our min inline size is our pref inline size
|
2014-07-24 21:03:25 +04:00
|
|
|
result = GetPrefISize(aRenderingContext);
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
return result;
|
|
|
|
}
|
1998-09-23 21:16:51 +04:00
|
|
|
|
2018-10-06 20:31:51 +03:00
|
|
|
nscoord nsFileControlFrame::GetPrefISize(gfxContext* aRenderingContext) {
|
|
|
|
nscoord result;
|
2019-05-03 20:35:00 +03:00
|
|
|
DISPLAY_PREF_INLINE_SIZE(this, result);
|
2018-10-06 20:31:51 +03:00
|
|
|
|
|
|
|
// Make sure we measure with the uncropped filename.
|
2019-05-03 20:34:55 +03:00
|
|
|
if (mCachedPrefISize == NS_INTRINSIC_ISIZE_UNKNOWN) {
|
2018-10-06 20:31:51 +03:00
|
|
|
nsAutoString filename;
|
|
|
|
HTMLInputElement::FromNode(mContent)->GetDisplayFileName(filename);
|
|
|
|
UpdateDisplayedValue(filename, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = nsBlockFrame::GetPrefISize(aRenderingContext);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:00:57 +03:00
|
|
|
void nsFileControlFrame::SyncDisabledState() {
|
2014-04-03 08:18:36 +04:00
|
|
|
EventStates eventStates = mContent->AsElement()->State();
|
2010-12-14 21:00:57 +03:00
|
|
|
if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
|
2016-06-29 19:18:42 +03:00
|
|
|
mBrowseFilesOrDirs->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
EmptyString(), true);
|
2010-12-14 21:00:57 +03:00
|
|
|
} else {
|
2016-06-29 19:18:42 +03:00
|
|
|
mBrowseFilesOrDirs->UnsetAttr(kNameSpaceID_None, nsGkAtoms::disabled, true);
|
2010-12-14 21:00:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 18:57:30 +04:00
|
|
|
nsresult nsFileControlFrame::AttributeChanged(int32_t aNameSpaceID,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aAttribute,
|
2013-02-09 18:57:30 +04:00
|
|
|
int32_t aModType) {
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::tabindex) {
|
2018-06-26 00:20:54 +03:00
|
|
|
if (aModType == MutationEvent_Binding::REMOVAL) {
|
2016-06-29 19:18:42 +03:00
|
|
|
mBrowseFilesOrDirs->UnsetAttr(aNameSpaceID, aAttribute, true);
|
2013-02-09 18:57:30 +04:00
|
|
|
} else {
|
|
|
|
nsAutoString value;
|
2017-12-07 21:13:50 +03:00
|
|
|
mContent->AsElement()->GetAttr(aNameSpaceID, aAttribute, value);
|
2016-06-29 19:18:42 +03:00
|
|
|
mBrowseFilesOrDirs->SetAttr(aNameSpaceID, aAttribute, value, true);
|
2008-12-17 10:33:33 +03:00
|
|
|
}
|
1999-08-19 18:03:53 +04:00
|
|
|
}
|
|
|
|
|
2008-12-29 18:07:38 +03:00
|
|
|
return nsBlockFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
|
1999-08-19 18:03:53 +04:00
|
|
|
}
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
void nsFileControlFrame::ContentStatesChanged(EventStates aStates) {
|
2010-12-14 21:00:57 +03:00
|
|
|
if (aStates.HasState(NS_EVENT_STATE_DISABLED)) {
|
|
|
|
nsContentUtils::AddScriptRunner(new SyncDisabledStateEvent(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 03:31:14 +04:00
|
|
|
#ifdef DEBUG_FRAME_DUMP
|
2001-11-14 04:33:42 +03:00
|
|
|
nsresult nsFileControlFrame::GetFrameName(nsAString& aResult) const {
|
|
|
|
return MakeFrameName(NS_LITERAL_STRING("FileControl"), aResult);
|
1998-11-19 20:22:29 +03:00
|
|
|
}
|
1999-11-02 01:12:45 +03:00
|
|
|
#endif
|
1999-01-22 18:32:57 +03:00
|
|
|
|
2013-02-09 18:57:30 +04:00
|
|
|
void nsFileControlFrame::UpdateDisplayedValue(const nsAString& aValue,
|
|
|
|
bool aNotify) {
|
2018-10-06 20:31:51 +03:00
|
|
|
auto* text = Text::FromNode(mTextContent->GetFirstChild());
|
2018-10-06 20:31:51 +03:00
|
|
|
uint32_t oldLength = aNotify ? 0 : text->TextLength();
|
2018-10-06 20:31:51 +03:00
|
|
|
text->SetText(aValue, aNotify);
|
2018-10-06 20:31:51 +03:00
|
|
|
if (!aNotify) {
|
|
|
|
// We can't notify during Reflow so we need to tell the text frame
|
|
|
|
// about the text content change we just did.
|
|
|
|
if (auto* textFrame = static_cast<nsTextFrame*>(text->GetPrimaryFrame())) {
|
|
|
|
textFrame->NotifyNativeAnonymousTextnodeChange(oldLength);
|
|
|
|
}
|
2018-10-25 17:51:45 +03:00
|
|
|
nsBlockFrame* label = do_QueryFrame(mTextContent->GetPrimaryFrame());
|
|
|
|
if (label && label->LinesBegin() != label->LinesEnd()) {
|
|
|
|
label->AddStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
|
|
|
|
label->LinesBegin()->MarkDirty();
|
|
|
|
}
|
2018-10-06 20:31:51 +03:00
|
|
|
}
|
2013-02-09 18:57:30 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult nsFileControlFrame::SetFormProperty(nsAtom* aName,
|
2005-12-13 03:57:56 +03:00
|
|
|
const nsAString& aValue) {
|
2006-12-26 20:47:52 +03:00
|
|
|
if (nsGkAtoms::value == aName) {
|
2013-02-09 18:57:30 +04:00
|
|
|
UpdateDisplayedValue(aValue, true);
|
1999-02-11 04:13:28 +03:00
|
|
|
}
|
2005-12-13 02:53:06 +03:00
|
|
|
return NS_OK;
|
2013-02-09 18:57:30 +04:00
|
|
|
}
|
1999-01-26 01:16:27 +03:00
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
void nsFileControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsDisplayListSet& aLists) {
|
2017-08-07 05:23:35 +03:00
|
|
|
BuildDisplayListForInline(aBuilder, aLists);
|
2006-01-26 05:29:17 +03:00
|
|
|
}
|
2006-11-14 01:05:25 +03:00
|
|
|
|
2007-09-28 05:09:14 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
2012-09-29 01:53:44 +04:00
|
|
|
a11y::AccType nsFileControlFrame::AccessibleType() {
|
2012-12-18 05:25:52 +04:00
|
|
|
return a11y::eHTMLFileInputType;
|
2007-09-28 05:09:14 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-14 01:05:25 +03:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Mouse listener implementation
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsFileControlFrame::MouseListener, nsIDOMEventListener)
|