Bug 1877662 - expose mozconfig as an artifact from build-fat-aar. r=glandium,geckovie...
[gecko.git] / layout / forms / nsTextControlFrame.cpp
blobc51b94c56a2fa04d1ecd09f172f090d1071173ea
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/. */
7 #include "mozilla/DebugOnly.h"
9 #include "gfxContext.h"
10 #include "nsCOMPtr.h"
11 #include "nsFontMetrics.h"
12 #include "nsTextControlFrame.h"
13 #include "nsIEditor.h"
14 #include "nsCaret.h"
15 #include "nsCSSPseudoElements.h"
16 #include "nsDisplayList.h"
17 #include "nsGenericHTMLElement.h"
18 #include "nsTextFragment.h"
19 #include "nsNameSpaceManager.h"
21 #include "nsIContent.h"
22 #include "nsIScrollableFrame.h"
23 #include "nsPresContext.h"
24 #include "nsGkAtoms.h"
25 #include "nsLayoutUtils.h"
27 #include <algorithm>
28 #include "nsRange.h" //for selection setting helper func
29 #include "nsINode.h"
30 #include "nsPIDOMWindow.h" //needed for notify selection changed to update the menus ect.
31 #include "nsQueryObject.h"
32 #include "nsILayoutHistoryState.h"
34 #include "nsFocusManager.h"
35 #include "mozilla/EventStateManager.h"
36 #include "mozilla/PresShell.h"
37 #include "mozilla/PresState.h"
38 #include "mozilla/TextEditor.h"
39 #include "nsAttrValueInlines.h"
40 #include "mozilla/dom/Selection.h"
41 #include "nsContentUtils.h"
42 #include "nsTextNode.h"
43 #include "mozilla/dom/HTMLInputElement.h"
44 #include "mozilla/dom/HTMLTextAreaElement.h"
45 #include "mozilla/dom/ScriptSettings.h"
46 #include "mozilla/dom/Text.h"
47 #include "mozilla/MathAlgorithms.h"
48 #include "mozilla/StaticPrefs_layout.h"
49 #include "mozilla/Try.h"
50 #include "nsFrameSelection.h"
52 #define DEFAULT_COLUMN_WIDTH 20
54 using namespace mozilla;
55 using namespace mozilla::dom;
57 nsIFrame* NS_NewTextControlFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
58 return new (aPresShell)
59 nsTextControlFrame(aStyle, aPresShell->GetPresContext());
62 NS_IMPL_FRAMEARENA_HELPERS(nsTextControlFrame)
64 NS_QUERYFRAME_HEAD(nsTextControlFrame)
65 NS_QUERYFRAME_ENTRY(nsTextControlFrame)
66 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
67 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
68 NS_QUERYFRAME_ENTRY(nsITextControlFrame)
69 NS_QUERYFRAME_ENTRY(nsIStatefulFrame)
70 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
72 #ifdef ACCESSIBILITY
73 a11y::AccType nsTextControlFrame::AccessibleType() {
74 return a11y::eHTMLTextFieldType;
76 #endif
78 #ifdef DEBUG
79 class EditorInitializerEntryTracker {
80 public:
81 explicit EditorInitializerEntryTracker(nsTextControlFrame& frame)
82 : mFrame(frame), mFirstEntry(false) {
83 if (!mFrame.mInEditorInitialization) {
84 mFrame.mInEditorInitialization = true;
85 mFirstEntry = true;
88 ~EditorInitializerEntryTracker() {
89 if (mFirstEntry) {
90 mFrame.mInEditorInitialization = false;
93 bool EnteredMoreThanOnce() const { return !mFirstEntry; }
95 private:
96 nsTextControlFrame& mFrame;
97 bool mFirstEntry;
99 #endif
101 class nsTextControlFrame::nsAnonDivObserver final
102 : public nsStubMutationObserver {
103 public:
104 explicit nsAnonDivObserver(nsTextControlFrame& aFrame) : mFrame(aFrame) {}
105 NS_DECL_ISUPPORTS
106 NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
107 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
108 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
109 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
111 private:
112 ~nsAnonDivObserver() = default;
113 nsTextControlFrame& mFrame;
116 nsTextControlFrame::nsTextControlFrame(ComputedStyle* aStyle,
117 nsPresContext* aPresContext,
118 nsIFrame::ClassID aClassID)
119 : nsContainerFrame(aStyle, aPresContext, aClassID) {}
121 nsTextControlFrame::~nsTextControlFrame() = default;
123 nsIScrollableFrame* nsTextControlFrame::GetScrollTargetFrame() const {
124 if (!mRootNode) {
125 return nullptr;
127 return do_QueryFrame(mRootNode->GetPrimaryFrame());
130 void nsTextControlFrame::Destroy(DestroyContext& aContext) {
131 RemoveProperty(TextControlInitializer());
133 // Unbind the text editor state object from the frame. The editor will live
134 // on, but things like controllers will be released.
135 RefPtr textControlElement = ControlElement();
136 if (mMutationObserver) {
137 textControlElement->UnbindFromFrame(this);
138 mRootNode->RemoveMutationObserver(mMutationObserver);
139 mMutationObserver = nullptr;
142 // If there is a drag session, user may be dragging selection in removing
143 // text node in the text control. If so, we should set source node to the
144 // text control because another text node may be recreated soon if the text
145 // control is just reframed.
146 if (nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession()) {
147 if (dragSession->IsDraggingTextInTextControl() && mRootNode &&
148 mRootNode->GetFirstChild()) {
149 nsCOMPtr<nsINode> sourceNode;
150 if (NS_SUCCEEDED(
151 dragSession->GetSourceNode(getter_AddRefs(sourceNode))) &&
152 mRootNode->Contains(sourceNode)) {
153 MOZ_ASSERT(sourceNode->IsText());
154 dragSession->UpdateSource(textControlElement, nullptr);
158 // Otherwise, EventStateManager may track gesture to start drag with native
159 // anonymous nodes in the text control element.
160 else if (textControlElement->GetPresContext(Element::eForComposedDoc)) {
161 textControlElement->GetPresContext(Element::eForComposedDoc)
162 ->EventStateManager()
163 ->TextControlRootWillBeRemoved(*textControlElement);
166 // If we're a subclass like nsNumberControlFrame, then it owns the root of the
167 // anonymous subtree where mRootNode is.
168 aContext.AddAnonymousContent(mRootNode.forget());
169 aContext.AddAnonymousContent(mPlaceholderDiv.forget());
170 aContext.AddAnonymousContent(mPreviewDiv.forget());
171 aContext.AddAnonymousContent(mRevealButton.forget());
173 nsContainerFrame::Destroy(aContext);
176 LogicalSize nsTextControlFrame::CalcIntrinsicSize(
177 gfxContext* aRenderingContext, WritingMode aWM,
178 float aFontSizeInflation) const {
179 LogicalSize intrinsicSize(aWM);
180 RefPtr<nsFontMetrics> fontMet =
181 nsLayoutUtils::GetFontMetricsForFrame(this, aFontSizeInflation);
182 const nscoord lineHeight =
183 ReflowInput::CalcLineHeight(*Style(), PresContext(), GetContent(),
184 NS_UNCONSTRAINEDSIZE, aFontSizeInflation);
185 // Use the larger of the font's "average" char width or the width of the
186 // zero glyph (if present) as the basis for resolving the size attribute.
187 const nscoord charWidth =
188 std::max(fontMet->ZeroOrAveCharWidth(), fontMet->AveCharWidth());
189 const nscoord charMaxAdvance = fontMet->MaxAdvance();
191 // Initialize based on the width in characters.
192 const int32_t cols = GetCols();
193 intrinsicSize.ISize(aWM) = cols * charWidth;
195 // If we do not have what appears to be a fixed-width font, add a "slop"
196 // amount based on the max advance of the font (clamped to twice charWidth,
197 // because some fonts have a few extremely-wide outliers that would result
198 // in excessive width here; e.g. the triple-emdash ligature in SFNS Text),
199 // minus 4px. This helps avoid input fields becoming unusably narrow with
200 // small size values.
201 if (charMaxAdvance - charWidth > AppUnitsPerCSSPixel()) {
202 nscoord internalPadding =
203 std::max(0, std::min(charMaxAdvance, charWidth * 2) -
204 nsPresContext::CSSPixelsToAppUnits(4));
205 internalPadding = RoundToMultiple(internalPadding, AppUnitsPerCSSPixel());
206 intrinsicSize.ISize(aWM) += internalPadding;
207 } else {
208 // This is to account for the anonymous <br> having a 1 twip width
209 // in Full Standards mode, see BRFrame::Reflow and bug 228752.
210 if (PresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
211 intrinsicSize.ISize(aWM) += 1;
215 // Increment width with cols * letter-spacing.
217 const StyleLength& letterSpacing = StyleText()->mLetterSpacing;
218 if (!letterSpacing.IsZero()) {
219 intrinsicSize.ISize(aWM) += cols * letterSpacing.ToAppUnits();
223 // Set the height equal to total number of rows (times the height of each
224 // line, of course)
225 intrinsicSize.BSize(aWM) = lineHeight * GetRows();
227 // Add in the size of the scrollbars for textarea
228 if (IsTextArea()) {
229 nsIScrollableFrame* scrollableFrame = GetScrollTargetFrame();
230 NS_ASSERTION(scrollableFrame, "Child must be scrollable");
231 if (scrollableFrame) {
232 LogicalMargin scrollbarSizes(aWM,
233 scrollableFrame->GetDesiredScrollbarSizes());
234 intrinsicSize.ISize(aWM) += scrollbarSizes.IStartEnd(aWM);
235 intrinsicSize.BSize(aWM) += scrollbarSizes.BStartEnd(aWM);
238 return intrinsicSize;
241 nsresult nsTextControlFrame::EnsureEditorInitialized() {
242 // This method initializes our editor, if needed.
244 // This code used to be called from CreateAnonymousContent(), but
245 // when the editor set the initial string, it would trigger a
246 // PresShell listener which called FlushPendingNotifications()
247 // during frame construction. This was causing other form controls
248 // to display wrong values. Additionally, calling this every time
249 // a text frame control is instantiated means that we're effectively
250 // instantiating the editor for all text fields, even if they
251 // never get used. So, now this method is being called lazily only
252 // when we actually need an editor.
254 if (mEditorHasBeenInitialized) return NS_OK;
256 Document* doc = mContent->GetComposedDoc();
257 NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
259 AutoWeakFrame weakFrame(this);
261 // Flush out content on our document. Have to do this, because script
262 // blockers don't prevent the sink flushing out content and notifying in the
263 // process, which can destroy frames.
264 doc->FlushPendingNotifications(FlushType::ContentAndNotify);
265 NS_ENSURE_TRUE(weakFrame.IsAlive(), NS_ERROR_FAILURE);
267 // Make sure that editor init doesn't do things that would kill us off
268 // (especially off the script blockers it'll create for its DOM mutations).
270 RefPtr<TextControlElement> textControlElement = ControlElement();
272 // Hide selection changes during the initialization, as webpages should not
273 // be aware of these initializations
274 AutoHideSelectionChanges hideSelectionChanges(
275 textControlElement->GetConstFrameSelection());
277 nsAutoScriptBlocker scriptBlocker;
279 // Time to mess with our security context... See comments in GetValue()
280 // for why this is needed.
281 mozilla::dom::AutoNoJSAPI nojsapi;
283 // Make sure that we try to focus the content even if the method fails
284 class EnsureSetFocus {
285 public:
286 explicit EnsureSetFocus(nsTextControlFrame* aFrame) : mFrame(aFrame) {}
287 ~EnsureSetFocus() {
288 if (nsContentUtils::IsFocusedContent(mFrame->GetContent()))
289 mFrame->SetFocus(true, false);
292 private:
293 nsTextControlFrame* mFrame;
295 EnsureSetFocus makeSureSetFocusHappens(this);
297 #ifdef DEBUG
298 // Make sure we are not being called again until we're finished.
299 // If reentrancy happens, just pretend that we don't have an editor.
300 const EditorInitializerEntryTracker tracker(*this);
301 NS_ASSERTION(!tracker.EnteredMoreThanOnce(),
302 "EnsureEditorInitialized has been called while a previous "
303 "call was in progress");
304 #endif
306 // Create an editor for the frame, if one doesn't already exist
307 nsresult rv = textControlElement->CreateEditor();
308 NS_ENSURE_SUCCESS(rv, rv);
309 NS_ENSURE_STATE(weakFrame.IsAlive());
311 // Set mEditorHasBeenInitialized so that subsequent calls will use the
312 // editor.
313 mEditorHasBeenInitialized = true;
315 if (weakFrame.IsAlive()) {
316 uint32_t position = 0;
318 // Set the selection to the end of the text field (bug 1287655),
319 // but only if the contents has changed (bug 1337392).
320 if (textControlElement->ValueChanged()) {
321 nsAutoString val;
322 textControlElement->GetTextEditorValue(val);
323 position = val.Length();
326 SetSelectionEndPoints(position, position);
329 NS_ENSURE_STATE(weakFrame.IsAlive());
330 return NS_OK;
333 already_AddRefed<Element> nsTextControlFrame::MakeAnonElement(
334 PseudoStyleType aPseudoType, Element* aParent, nsAtom* aTag) const {
335 MOZ_ASSERT(aPseudoType != PseudoStyleType::NotPseudo);
336 Document* doc = PresContext()->Document();
337 RefPtr<Element> element = doc->CreateHTMLElement(aTag);
338 element->SetPseudoElementType(aPseudoType);
339 if (aPseudoType == PseudoStyleType::mozTextControlEditingRoot) {
340 // Make our root node editable
341 element->SetFlags(NODE_IS_EDITABLE);
344 if (aPseudoType == PseudoStyleType::mozNumberSpinDown ||
345 aPseudoType == PseudoStyleType::mozNumberSpinUp) {
346 element->SetAttr(kNameSpaceID_None, nsGkAtoms::aria_hidden, u"true"_ns,
347 false);
350 if (aParent) {
351 aParent->AppendChildTo(element, false, IgnoreErrors());
354 return element.forget();
357 already_AddRefed<Element> nsTextControlFrame::MakeAnonDivWithTextNode(
358 PseudoStyleType aPseudoType) const {
359 RefPtr<Element> div = MakeAnonElement(aPseudoType);
361 // Create the text node for the anonymous <div> element.
362 nsNodeInfoManager* nim = div->OwnerDoc()->NodeInfoManager();
363 RefPtr<nsTextNode> textNode = new (nim) nsTextNode(nim);
364 // If the anonymous div element is not for the placeholder, we should
365 // mark the text node as "maybe modified frequently" for avoiding ASCII
366 // range checks at every input.
367 if (aPseudoType != PseudoStyleType::placeholder) {
368 textNode->MarkAsMaybeModifiedFrequently();
369 // Additionally, this is a password field, the text node needs to be
370 // marked as "maybe masked" unless it's in placeholder.
371 if (IsPasswordTextControl()) {
372 textNode->MarkAsMaybeMasked();
375 div->AppendChildTo(textNode, false, IgnoreErrors());
376 return div.forget();
379 nsresult nsTextControlFrame::CreateAnonymousContent(
380 nsTArray<ContentInfo>& aElements) {
381 MOZ_ASSERT(!nsContentUtils::IsSafeToRunScript());
382 MOZ_ASSERT(mContent, "We should have a content!");
384 AddStateBits(NS_FRAME_INDEPENDENT_SELECTION);
386 RefPtr<TextControlElement> textControlElement = ControlElement();
387 mRootNode = MakeAnonElement(PseudoStyleType::mozTextControlEditingRoot);
388 if (NS_WARN_IF(!mRootNode)) {
389 return NS_ERROR_FAILURE;
392 mMutationObserver = new nsAnonDivObserver(*this);
393 mRootNode->AddMutationObserver(mMutationObserver);
395 // Bind the frame to its text control.
397 // This can realistically fail in paginated mode, where we may replicate
398 // fixed-positioned elements and the replicated frame will not get the chance
399 // to get an editor.
400 nsresult rv = textControlElement->BindToFrame(this);
401 if (NS_WARN_IF(NS_FAILED(rv))) {
402 mRootNode->RemoveMutationObserver(mMutationObserver);
403 mMutationObserver = nullptr;
404 mRootNode = nullptr;
405 return rv;
408 CreatePlaceholderIfNeeded();
409 if (mPlaceholderDiv) {
410 aElements.AppendElement(mPlaceholderDiv);
412 CreatePreviewIfNeeded();
413 if (mPreviewDiv) {
414 aElements.AppendElement(mPreviewDiv);
417 // NOTE(emilio): We want the root node always after the placeholder so that
418 // background on the placeholder doesn't obscure the caret.
419 aElements.AppendElement(mRootNode);
421 if (StaticPrefs::layout_forms_reveal_password_button_enabled() &&
422 IsPasswordTextControl()) {
423 mRevealButton =
424 MakeAnonElement(PseudoStyleType::mozReveal, nullptr, nsGkAtoms::button);
425 mRevealButton->SetAttr(kNameSpaceID_None, nsGkAtoms::aria_hidden,
426 u"true"_ns, false);
427 mRevealButton->SetAttr(kNameSpaceID_None, nsGkAtoms::tabindex, u"-1"_ns,
428 false);
429 aElements.AppendElement(mRevealButton);
432 rv = UpdateValueDisplay(false);
433 NS_ENSURE_SUCCESS(rv, rv);
435 InitializeEagerlyIfNeeded();
436 return NS_OK;
439 bool nsTextControlFrame::ShouldInitializeEagerly() const {
440 // textareas are eagerly initialized.
441 if (!IsSingleLineTextControl()) {
442 return true;
445 // Also, input elements which have a cached selection should get eager
446 // editor initialization.
447 TextControlElement* textControlElement = ControlElement();
448 if (textControlElement->HasCachedSelection()) {
449 return true;
452 // So do input text controls with spellcheck=true
453 if (auto* htmlElement = nsGenericHTMLElement::FromNode(mContent)) {
454 if (htmlElement->Spellcheck()) {
455 return true;
459 // If text in the editor is being dragged, we need the editor to create
460 // new source node for the drag session (TextEditor creates the text node
461 // in the anonymous <div> element.
462 if (nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession()) {
463 if (dragSession->IsDraggingTextInTextControl()) {
464 nsCOMPtr<nsINode> sourceNode;
465 if (NS_SUCCEEDED(
466 dragSession->GetSourceNode(getter_AddRefs(sourceNode))) &&
467 sourceNode == textControlElement) {
468 return true;
473 return false;
476 void nsTextControlFrame::InitializeEagerlyIfNeeded() {
477 MOZ_ASSERT(!nsContentUtils::IsSafeToRunScript(),
478 "Someone forgot a script blocker?");
479 if (!ShouldInitializeEagerly()) {
480 return;
483 EditorInitializer* initializer = new EditorInitializer(this);
484 SetProperty(TextControlInitializer(), initializer);
485 nsContentUtils::AddScriptRunner(initializer);
488 void nsTextControlFrame::CreatePlaceholderIfNeeded() {
489 MOZ_ASSERT(!mPlaceholderDiv);
491 // Do we need a placeholder node?
492 nsAutoString placeholder;
493 if (!mContent->AsElement()->GetAttr(nsGkAtoms::placeholder, placeholder)) {
494 return;
497 mPlaceholderDiv = MakeAnonDivWithTextNode(PseudoStyleType::placeholder);
498 UpdatePlaceholderText(placeholder, false);
501 void nsTextControlFrame::PlaceholderChanged(const nsAttrValue* aOld,
502 const nsAttrValue* aNew) {
503 if (!aOld || !aNew) {
504 return; // This should be handled by GetAttributeChangeHint.
507 // If we've changed the attribute but we still haven't reframed, there's
508 // nothing to do either.
509 if (!mPlaceholderDiv) {
510 return;
513 nsAutoString placeholder;
514 aNew->ToString(placeholder);
515 UpdatePlaceholderText(placeholder, true);
518 void nsTextControlFrame::UpdatePlaceholderText(nsString& aPlaceholder,
519 bool aNotify) {
520 MOZ_DIAGNOSTIC_ASSERT(mPlaceholderDiv);
521 MOZ_DIAGNOSTIC_ASSERT(mPlaceholderDiv->GetFirstChild());
523 if (IsTextArea()) { // <textarea>s preserve newlines...
524 nsContentUtils::PlatformToDOMLineBreaks(aPlaceholder);
525 } else { // ...<input>s don't
526 nsContentUtils::RemoveNewlines(aPlaceholder);
529 mPlaceholderDiv->GetFirstChild()->AsText()->SetText(aPlaceholder, aNotify);
532 void nsTextControlFrame::CreatePreviewIfNeeded() {
533 if (!ControlElement()->IsPreviewEnabled()) {
534 return;
536 mPreviewDiv = MakeAnonDivWithTextNode(PseudoStyleType::mozTextControlPreview);
539 void nsTextControlFrame::AppendAnonymousContentTo(
540 nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
541 if (mPlaceholderDiv && !(aFilter & nsIContent::eSkipPlaceholderContent)) {
542 aElements.AppendElement(mPlaceholderDiv);
545 if (mPreviewDiv) {
546 aElements.AppendElement(mPreviewDiv);
549 if (mRevealButton) {
550 aElements.AppendElement(mRevealButton);
553 aElements.AppendElement(mRootNode);
556 nscoord nsTextControlFrame::GetPrefISize(gfxContext* aRenderingContext) {
557 nscoord result = 0;
558 DISPLAY_PREF_INLINE_SIZE(this, result);
559 float inflation = nsLayoutUtils::FontSizeInflationFor(this);
560 WritingMode wm = GetWritingMode();
561 result = CalcIntrinsicSize(aRenderingContext, wm, inflation).ISize(wm);
562 return result;
565 nscoord nsTextControlFrame::GetMinISize(gfxContext* aRenderingContext) {
566 // Our min inline size is just our preferred width if we have auto inline size
567 nscoord result;
568 DISPLAY_MIN_INLINE_SIZE(this, result);
569 result = GetPrefISize(aRenderingContext);
570 return result;
573 LogicalSize nsTextControlFrame::ComputeAutoSize(
574 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
575 nscoord aAvailableISize, const LogicalSize& aMargin,
576 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
577 ComputeSizeFlags aFlags) {
578 float inflation = nsLayoutUtils::FontSizeInflationFor(this);
579 LogicalSize autoSize = CalcIntrinsicSize(aRenderingContext, aWM, inflation);
581 // Note: nsContainerFrame::ComputeAutoSize only computes the inline-size (and
582 // only for 'auto'), the block-size it returns is always NS_UNCONSTRAINEDSIZE.
583 const auto& styleISize = aSizeOverrides.mStyleISize
584 ? *aSizeOverrides.mStyleISize
585 : StylePosition()->ISize(aWM);
586 if (styleISize.IsAuto()) {
587 if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
588 // CalcIntrinsicSize isn't aware of grid-item margin-box clamping, so we
589 // fall back to nsContainerFrame's ComputeAutoSize to handle that.
590 // XXX maybe a font-inflation issue here? (per the assertion below).
591 autoSize.ISize(aWM) =
592 nsContainerFrame::ComputeAutoSize(
593 aRenderingContext, aWM, aCBSize, aAvailableISize, aMargin,
594 aBorderPadding, aSizeOverrides, aFlags)
595 .ISize(aWM);
597 #ifdef DEBUG
598 else {
599 LogicalSize ancestorAutoSize = nsContainerFrame::ComputeAutoSize(
600 aRenderingContext, aWM, aCBSize, aAvailableISize, aMargin,
601 aBorderPadding, aSizeOverrides, aFlags);
602 MOZ_ASSERT(inflation != 1.0f ||
603 ancestorAutoSize.ISize(aWM) == autoSize.ISize(aWM),
604 "Incorrect size computed by ComputeAutoSize?");
606 #endif
608 return autoSize;
611 Maybe<nscoord> nsTextControlFrame::ComputeBaseline(
612 const nsIFrame* aFrame, const ReflowInput& aReflowInput,
613 bool aForSingleLineControl) {
614 // If we're layout-contained, we have no baseline.
615 if (aReflowInput.mStyleDisplay->IsContainLayout()) {
616 return Nothing();
618 WritingMode wm = aReflowInput.GetWritingMode();
620 nscoord lineHeight = aReflowInput.ComputedBSize();
621 if (!aForSingleLineControl || lineHeight == NS_UNCONSTRAINEDSIZE) {
622 lineHeight = aReflowInput.ApplyMinMaxBSize(aReflowInput.GetLineHeight());
624 RefPtr<nsFontMetrics> fontMet =
625 nsLayoutUtils::GetInflatedFontMetricsForFrame(aFrame);
626 return Some(nsLayoutUtils::GetCenteredFontBaseline(fontMet, lineHeight,
627 wm.IsLineInverted()) +
628 aReflowInput.ComputedLogicalBorderPadding(wm).BStart(wm));
631 static bool IsButtonBox(const nsIFrame* aFrame) {
632 auto pseudoType = aFrame->Style()->GetPseudoType();
633 return pseudoType == PseudoStyleType::mozNumberSpinBox ||
634 pseudoType == PseudoStyleType::mozSearchClearButton ||
635 pseudoType == PseudoStyleType::mozReveal;
638 void nsTextControlFrame::Reflow(nsPresContext* aPresContext,
639 ReflowOutput& aDesiredSize,
640 const ReflowInput& aReflowInput,
641 nsReflowStatus& aStatus) {
642 MarkInReflow();
643 DO_GLOBAL_REFLOW_COUNT("nsTextControlFrame");
644 DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
645 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
647 // set values of reflow's out parameters
648 WritingMode wm = aReflowInput.GetWritingMode();
649 aDesiredSize.SetSize(wm, aReflowInput.ComputedSizeWithBorderPadding(wm));
652 // Calculate the baseline and store it in mFirstBaseline.
653 auto baseline =
654 ComputeBaseline(this, aReflowInput, IsSingleLineTextControl());
655 mFirstBaseline = baseline.valueOr(NS_INTRINSIC_ISIZE_UNKNOWN);
656 if (baseline) {
657 aDesiredSize.SetBlockStartAscent(*baseline);
661 // overflow handling
662 aDesiredSize.SetOverflowAreasToDesiredBounds();
664 nsIFrame* buttonBox = [&]() -> nsIFrame* {
665 nsIFrame* last = mFrames.LastChild();
666 if (!last || !IsButtonBox(last)) {
667 return nullptr;
669 return last;
670 }();
672 // Reflow the button box first, so that we can use its size for the other
673 // frames.
674 nscoord buttonBoxISize = 0;
675 if (buttonBox) {
676 ReflowTextControlChild(buttonBox, aPresContext, aReflowInput, aStatus,
677 aDesiredSize, buttonBoxISize);
680 // perform reflow on all kids
681 nsIFrame* kid = mFrames.FirstChild();
682 while (kid) {
683 if (kid != buttonBox) {
684 MOZ_ASSERT(!IsButtonBox(kid),
685 "Should only have one button box, and should be last");
686 ReflowTextControlChild(kid, aPresContext, aReflowInput, aStatus,
687 aDesiredSize, buttonBoxISize);
689 kid = kid->GetNextSibling();
692 // take into account css properties that affect overflow handling
693 FinishAndStoreOverflow(&aDesiredSize);
695 aStatus.Reset(); // This type of frame can't be split.
698 void nsTextControlFrame::ReflowTextControlChild(
699 nsIFrame* aKid, nsPresContext* aPresContext,
700 const ReflowInput& aReflowInput, nsReflowStatus& aStatus,
701 ReflowOutput& aParentDesiredSize, nscoord& aButtonBoxISize) {
702 const WritingMode outerWM = aReflowInput.GetWritingMode();
703 // compute available size and frame offsets for child
704 const WritingMode wm = aKid->GetWritingMode();
705 LogicalSize availSize = aReflowInput.ComputedSizeWithPadding(wm);
706 availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
708 bool isButtonBox = IsButtonBox(aKid);
710 ReflowInput kidReflowInput(aPresContext, aReflowInput, aKid, availSize,
711 Nothing(), ReflowInput::InitFlag::CallerWillInit);
713 // Override padding with our computed padding in case we got it from theming
714 // or percentage, if we're not the button box.
715 auto overridePadding =
716 isButtonBox ? Nothing() : Some(aReflowInput.ComputedLogicalPadding(wm));
717 if (!isButtonBox && aButtonBoxISize) {
718 // Button box respects inline-end-padding, so we don't need to.
719 overridePadding->IEnd(outerWM) = 0;
722 // We want to let our button box fill the frame in the block axis, up to the
723 // edge of the control's border. So, we use the control's padding-box as the
724 // containing block size for our button box.
725 auto overrideCBSize =
726 isButtonBox ? Some(aReflowInput.ComputedSizeWithPadding(wm)) : Nothing();
727 kidReflowInput.Init(aPresContext, overrideCBSize, Nothing(), overridePadding);
729 LogicalPoint position(wm);
730 if (!isButtonBox) {
731 MOZ_ASSERT(wm == outerWM,
732 "Shouldn't have to care about orthogonal "
733 "writing-modes and such inside the control, "
734 "except for the number spin-box which forces "
735 "horizontal-tb");
737 const auto& border = aReflowInput.ComputedLogicalBorder(wm);
739 // Offset the frame by the size of the parent's border. Note that we don't
740 // have to account for the parent's padding here, because this child
741 // actually "inherits" that padding and manages it on behalf of the parent.
742 position.B(wm) = border.BStart(wm);
743 position.I(wm) = border.IStart(wm);
745 // Set computed width and computed height for the child (the button box is
746 // the only exception, which has an auto size).
747 kidReflowInput.SetComputedISize(
748 std::max(0, aReflowInput.ComputedISize() - aButtonBoxISize));
749 kidReflowInput.SetComputedBSize(aReflowInput.ComputedBSize());
752 // reflow the child
753 ReflowOutput desiredSize(aReflowInput);
754 const nsSize containerSize =
755 aReflowInput.ComputedSizeWithBorderPadding(outerWM).GetPhysicalSize(
756 outerWM);
757 ReflowChild(aKid, aPresContext, desiredSize, kidReflowInput, wm, position,
758 containerSize, ReflowChildFlags::Default, aStatus);
760 if (isButtonBox) {
761 const auto& bp = aReflowInput.ComputedLogicalBorderPadding(outerWM);
762 auto size = desiredSize.Size(outerWM);
763 // Center button in the block axis of our content box. We do this
764 // computation in terms of outerWM for simplicity.
765 LogicalRect buttonRect(outerWM);
766 buttonRect.BSize(outerWM) = size.BSize(outerWM);
767 buttonRect.ISize(outerWM) = size.ISize(outerWM);
768 buttonRect.BStart(outerWM) =
769 bp.BStart(outerWM) +
770 (aReflowInput.ComputedBSize() - size.BSize(outerWM)) / 2;
771 // Align to the inline-end of the content box.
772 buttonRect.IStart(outerWM) =
773 bp.IStart(outerWM) + aReflowInput.ComputedISize() - size.ISize(outerWM);
774 buttonRect = buttonRect.ConvertTo(wm, outerWM, containerSize);
775 position = buttonRect.Origin(wm);
776 aButtonBoxISize = size.ISize(outerWM);
779 // place the child
780 FinishReflowChild(aKid, aPresContext, desiredSize, &kidReflowInput, wm,
781 position, containerSize, ReflowChildFlags::Default);
783 // consider the overflow
784 aParentDesiredSize.mOverflowAreas.UnionWith(desiredSize.mOverflowAreas);
787 // IMPLEMENTING NS_IFORMCONTROLFRAME
788 void nsTextControlFrame::SetFocus(bool aOn, bool aRepaint) {
789 // If 'dom.placeholeder.show_on_focus' preference is 'false', focusing or
790 // blurring the frame can have an impact on the placeholder visibility.
791 if (!aOn) {
792 return;
795 nsISelectionController* selCon = GetSelectionController();
796 if (!selCon) {
797 return;
800 RefPtr<Selection> ourSel =
801 selCon->GetSelection(nsISelectionController::SELECTION_NORMAL);
802 if (!ourSel) {
803 return;
806 mozilla::PresShell* presShell = PresShell();
807 RefPtr<nsCaret> caret = presShell->GetCaret();
808 if (!caret) {
809 return;
812 // Tell the caret to use our selection
813 caret->SetSelection(ourSel);
815 // mutual-exclusion: the selection is either controlled by the
816 // document or by the text input/area. Clear any selection in the
817 // document since the focus is now on our independent selection.
819 RefPtr<Selection> docSel =
820 presShell->GetSelection(nsISelectionController::SELECTION_NORMAL);
821 if (!docSel) {
822 return;
825 if (!docSel->IsCollapsed()) {
826 docSel->RemoveAllRanges(IgnoreErrors());
829 // If the focus moved to a text control during text selection by pointer
830 // device, stop extending the selection.
831 if (RefPtr<nsFrameSelection> frameSelection = presShell->FrameSelection()) {
832 frameSelection->SetDragState(false);
836 nsresult nsTextControlFrame::SetFormProperty(nsAtom* aName,
837 const nsAString& aValue) {
838 if (!mIsProcessing) { // some kind of lock.
839 mIsProcessing = true;
840 if (nsGkAtoms::select == aName) {
841 // Select all the text.
843 // XXX: This is lame, we can't call editor's SelectAll method
844 // because that triggers AutoCopies in unix builds.
845 // Instead, we have to call our own homegrown version
846 // of select all which merely builds a range that selects
847 // all of the content and adds that to the selection.
849 AutoWeakFrame weakThis = this;
850 SelectAllOrCollapseToEndOfText(true); // NOTE: can destroy the world
851 if (!weakThis.IsAlive()) {
852 return NS_OK;
855 mIsProcessing = false;
857 return NS_OK;
860 already_AddRefed<TextEditor> nsTextControlFrame::GetTextEditor() {
861 if (NS_WARN_IF(NS_FAILED(EnsureEditorInitialized()))) {
862 return nullptr;
864 RefPtr el = ControlElement();
865 return do_AddRef(el->GetTextEditor());
868 nsresult nsTextControlFrame::SetSelectionInternal(
869 nsINode* aStartNode, uint32_t aStartOffset, nsINode* aEndNode,
870 uint32_t aEndOffset, SelectionDirection aDirection) {
871 // Get the selection, clear it and add the new range to it!
872 nsISelectionController* selCon = GetSelectionController();
873 NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
875 RefPtr<Selection> selection =
876 selCon->GetSelection(nsISelectionController::SELECTION_NORMAL);
877 NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
879 nsDirection direction;
880 if (aDirection == SelectionDirection::None) {
881 // Preserve the direction
882 direction = selection->GetDirection();
883 } else {
884 direction =
885 aDirection == SelectionDirection::Backward ? eDirPrevious : eDirNext;
888 MOZ_TRY(selection->SetStartAndEndInLimiter(*aStartNode, aStartOffset,
889 *aEndNode, aEndOffset, direction,
890 nsISelectionListener::JS_REASON));
891 return NS_OK;
894 void nsTextControlFrame::ScrollSelectionIntoViewAsync(
895 ScrollAncestors aScrollAncestors) {
896 nsISelectionController* selCon = GetSelectionController();
897 if (!selCon) {
898 return;
901 int16_t flags = aScrollAncestors == ScrollAncestors::Yes
903 : nsISelectionController::SCROLL_FIRST_ANCESTOR_ONLY;
905 // Scroll the selection into view (see bug 231389).
906 selCon->ScrollSelectionIntoView(
907 nsISelectionController::SELECTION_NORMAL,
908 nsISelectionController::SELECTION_FOCUS_REGION, flags);
911 nsresult nsTextControlFrame::SelectAllOrCollapseToEndOfText(bool aSelect) {
912 nsresult rv = EnsureEditorInitialized();
913 if (NS_WARN_IF(NS_FAILED(rv))) {
914 return rv;
917 RefPtr<nsINode> rootNode = mRootNode;
918 NS_ENSURE_TRUE(rootNode, NS_ERROR_FAILURE);
920 RefPtr<Text> text = Text::FromNodeOrNull(rootNode->GetFirstChild());
921 MOZ_ASSERT(text);
923 uint32_t length = text->Length();
925 rv = SetSelectionInternal(text, aSelect ? 0 : length, text, length);
926 NS_ENSURE_SUCCESS(rv, rv);
928 ScrollSelectionIntoViewAsync();
929 return NS_OK;
932 nsresult nsTextControlFrame::SetSelectionEndPoints(
933 uint32_t aSelStart, uint32_t aSelEnd,
934 nsITextControlFrame::SelectionDirection aDirection) {
935 NS_ASSERTION(aSelStart <= aSelEnd, "Invalid selection offsets!");
937 if (aSelStart > aSelEnd) return NS_ERROR_FAILURE;
939 nsCOMPtr<nsINode> startNode, endNode;
940 uint32_t startOffset, endOffset;
942 // Calculate the selection start point.
944 nsresult rv =
945 OffsetToDOMPoint(aSelStart, getter_AddRefs(startNode), &startOffset);
947 NS_ENSURE_SUCCESS(rv, rv);
949 if (aSelStart == aSelEnd) {
950 // Collapsed selection, so start and end are the same!
951 endNode = startNode;
952 endOffset = startOffset;
953 } else {
954 // Selection isn't collapsed so we have to calculate
955 // the end point too.
957 rv = OffsetToDOMPoint(aSelEnd, getter_AddRefs(endNode), &endOffset);
959 NS_ENSURE_SUCCESS(rv, rv);
962 return SetSelectionInternal(startNode, startOffset, endNode, endOffset,
963 aDirection);
966 NS_IMETHODIMP
967 nsTextControlFrame::SetSelectionRange(
968 uint32_t aSelStart, uint32_t aSelEnd,
969 nsITextControlFrame::SelectionDirection aDirection) {
970 nsresult rv = EnsureEditorInitialized();
971 NS_ENSURE_SUCCESS(rv, rv);
973 if (aSelStart > aSelEnd) {
974 // Simulate what we'd see SetSelectionStart() was called, followed
975 // by a SetSelectionEnd().
977 aSelStart = aSelEnd;
980 return SetSelectionEndPoints(aSelStart, aSelEnd, aDirection);
983 nsresult nsTextControlFrame::OffsetToDOMPoint(uint32_t aOffset,
984 nsINode** aResult,
985 uint32_t* aPosition) {
986 NS_ENSURE_ARG_POINTER(aResult && aPosition);
988 *aResult = nullptr;
989 *aPosition = 0;
991 nsresult rv = EnsureEditorInitialized();
992 if (NS_WARN_IF(NS_FAILED(rv))) {
993 return rv;
996 RefPtr<Element> rootNode = mRootNode;
997 NS_ENSURE_TRUE(rootNode, NS_ERROR_FAILURE);
999 nsCOMPtr<nsINodeList> nodeList = rootNode->ChildNodes();
1000 uint32_t length = nodeList->Length();
1002 NS_ASSERTION(length <= 2,
1003 "We should have one text node and one mozBR at most");
1005 nsCOMPtr<nsINode> firstNode = nodeList->Item(0);
1006 Text* textNode = firstNode ? firstNode->GetAsText() : nullptr;
1008 if (length == 0) {
1009 rootNode.forget(aResult);
1010 *aPosition = 0;
1011 } else if (textNode) {
1012 uint32_t textLength = textNode->Length();
1013 firstNode.forget(aResult);
1014 *aPosition = std::min(aOffset, textLength);
1015 } else {
1016 rootNode.forget(aResult);
1017 *aPosition = 0;
1020 return NS_OK;
1023 /////END INTERFACE IMPLEMENTATIONS
1025 ////NSIFRAME
1026 nsresult nsTextControlFrame::AttributeChanged(int32_t aNameSpaceID,
1027 nsAtom* aAttribute,
1028 int32_t aModType) {
1029 if (aAttribute == nsGkAtoms::value && !mEditorHasBeenInitialized) {
1030 UpdateValueDisplay(true);
1031 return NS_OK;
1034 if (aAttribute == nsGkAtoms::maxlength) {
1035 if (RefPtr<TextEditor> textEditor = GetTextEditor()) {
1036 textEditor->SetMaxTextLength(ControlElement()->UsedMaxLength());
1037 return NS_OK;
1040 return nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
1043 void nsTextControlFrame::HandleReadonlyOrDisabledChange() {
1044 RefPtr<TextControlElement> el = ControlElement();
1045 RefPtr<TextEditor> editor = el->GetTextEditorWithoutCreation();
1046 if (!editor) {
1047 return;
1049 nsISelectionController* selCon = el->GetSelectionController();
1050 if (!selCon) {
1051 return;
1053 if (el->IsDisabledOrReadOnly()) {
1054 if (nsContentUtils::IsFocusedContent(el)) {
1055 selCon->SetCaretEnabled(false);
1057 editor->AddFlags(nsIEditor::eEditorReadonlyMask);
1058 } else {
1059 if (nsContentUtils::IsFocusedContent(el)) {
1060 selCon->SetCaretEnabled(true);
1062 editor->RemoveFlags(nsIEditor::eEditorReadonlyMask);
1066 void nsTextControlFrame::ElementStateChanged(dom::ElementState aStates) {
1067 if (aStates.HasAtLeastOneOfStates(dom::ElementState::READONLY |
1068 dom::ElementState::DISABLED)) {
1069 HandleReadonlyOrDisabledChange();
1071 return nsContainerFrame::ElementStateChanged(aStates);
1074 /// END NSIFRAME OVERLOADS
1076 // NOTE(emilio): This is needed because the root->primary frame map is not set
1077 // up by the time this is called.
1078 static nsIFrame* FindRootNodeFrame(const nsFrameList& aChildList,
1079 const nsIContent* aRoot) {
1080 for (nsIFrame* f : aChildList) {
1081 if (f->GetContent() == aRoot) {
1082 return f;
1084 if (nsIFrame* root = FindRootNodeFrame(f->PrincipalChildList(), aRoot)) {
1085 return root;
1088 return nullptr;
1091 void nsTextControlFrame::SetInitialChildList(ChildListID aListID,
1092 nsFrameList&& aChildList) {
1093 nsContainerFrame::SetInitialChildList(aListID, std::move(aChildList));
1094 if (aListID != FrameChildListID::Principal) {
1095 return;
1098 // Mark the scroll frame as being a reflow root. This will allow incremental
1099 // reflows to be initiated at the scroll frame, rather than descending from
1100 // the root frame of the frame hierarchy.
1101 if (nsIFrame* frame = FindRootNodeFrame(PrincipalChildList(), mRootNode)) {
1102 frame->AddStateBits(NS_FRAME_REFLOW_ROOT);
1104 ControlElement()->InitializeKeyboardEventListeners();
1106 bool hasProperty;
1107 nsPoint contentScrollPos = TakeProperty(ContentScrollPos(), &hasProperty);
1108 if (hasProperty) {
1109 // If we have a scroll pos stored to be passed to our anonymous
1110 // div, do it here!
1111 nsIStatefulFrame* statefulFrame = do_QueryFrame(frame);
1112 NS_ASSERTION(statefulFrame,
1113 "unexpected type of frame for the anonymous div");
1114 UniquePtr<PresState> fakePresState = NewPresState();
1115 fakePresState->scrollState() = contentScrollPos;
1116 statefulFrame->RestoreState(fakePresState.get());
1118 } else {
1119 MOZ_ASSERT(!mRootNode || PrincipalChildList().IsEmpty());
1123 nsresult nsTextControlFrame::UpdateValueDisplay(bool aNotify,
1124 bool aBeforeEditorInit,
1125 const nsAString* aValue) {
1126 if (!IsSingleLineTextControl()) { // textareas don't use this
1127 return NS_OK;
1130 MOZ_ASSERT(mRootNode, "Must have a div content\n");
1131 MOZ_ASSERT(!mEditorHasBeenInitialized,
1132 "Do not call this after editor has been initialized");
1134 nsIContent* childContent = mRootNode->GetFirstChild();
1135 Text* textContent;
1136 if (!childContent) {
1137 // Set up a textnode with our value
1138 RefPtr<nsTextNode> textNode = new (mContent->NodeInfo()->NodeInfoManager())
1139 nsTextNode(mContent->NodeInfo()->NodeInfoManager());
1140 textNode->MarkAsMaybeModifiedFrequently();
1141 if (IsPasswordTextControl()) {
1142 textNode->MarkAsMaybeMasked();
1144 mRootNode->AppendChildTo(textNode, aNotify, IgnoreErrors());
1145 textContent = textNode;
1146 } else {
1147 textContent = childContent->GetAsText();
1150 NS_ENSURE_TRUE(textContent, NS_ERROR_UNEXPECTED);
1152 // Get the current value of the textfield from the content.
1153 nsAutoString value;
1154 if (aValue) {
1155 value = *aValue;
1156 } else {
1157 ControlElement()->GetTextEditorValue(value);
1160 return textContent->SetText(value, aNotify);
1163 NS_IMETHODIMP
1164 nsTextControlFrame::GetOwnedSelectionController(
1165 nsISelectionController** aSelCon) {
1166 NS_ENSURE_ARG_POINTER(aSelCon);
1167 NS_IF_ADDREF(*aSelCon = GetSelectionController());
1168 return NS_OK;
1171 UniquePtr<PresState> nsTextControlFrame::SaveState() {
1172 if (nsIStatefulFrame* scrollStateFrame =
1173 do_QueryFrame(GetScrollTargetFrame())) {
1174 return scrollStateFrame->SaveState();
1177 return nullptr;
1180 NS_IMETHODIMP
1181 nsTextControlFrame::RestoreState(PresState* aState) {
1182 NS_ENSURE_ARG_POINTER(aState);
1184 // Query the nsIStatefulFrame from the HTMLScrollFrame
1185 if (nsIStatefulFrame* scrollStateFrame =
1186 do_QueryFrame(GetScrollTargetFrame())) {
1187 return scrollStateFrame->RestoreState(aState);
1190 // Most likely, we don't have our anonymous content constructed yet, which
1191 // would cause us to end up here. In this case, we'll just store the scroll
1192 // pos ourselves, and forward it to the scroll frame later when it's created.
1193 SetProperty(ContentScrollPos(), aState->scrollState());
1194 return NS_OK;
1197 nsresult nsTextControlFrame::PeekOffset(PeekOffsetStruct* aPos) {
1198 return NS_ERROR_FAILURE;
1201 void nsTextControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
1202 const nsDisplayListSet& aLists) {
1203 DO_GLOBAL_REFLOW_COUNT_DSP("nsTextControlFrame");
1205 DisplayBorderBackgroundOutline(aBuilder, aLists);
1207 // Redirect all lists to the Content list so that nothing can escape, ie
1208 // opacity creating stacking contexts that then get sorted with stacking
1209 // contexts external to us.
1210 nsDisplayList* content = aLists.Content();
1211 nsDisplayListSet set(content, content, content, content, content, content);
1213 for (auto* kid : mFrames) {
1214 BuildDisplayListForChild(aBuilder, kid, set);
1218 NS_IMETHODIMP
1219 nsTextControlFrame::EditorInitializer::Run() {
1220 if (!mFrame) {
1221 return NS_OK;
1224 // Need to block script to avoid bug 669767.
1225 nsAutoScriptBlocker scriptBlocker;
1227 RefPtr<mozilla::PresShell> presShell = mFrame->PresShell();
1228 bool observes = presShell->ObservesNativeAnonMutationsForPrint();
1229 presShell->ObserveNativeAnonMutationsForPrint(true);
1230 // This can cause the frame to be destroyed (and call Revoke()).
1231 mFrame->EnsureEditorInitialized();
1232 presShell->ObserveNativeAnonMutationsForPrint(observes);
1234 // The frame can *still* be destroyed even though we have a scriptblocker,
1235 // bug 682684.
1236 if (!mFrame) {
1237 return NS_ERROR_FAILURE;
1240 // If there is a drag session which is for dragging text in a text control
1241 // and its source node is the text control element, we're being reframed.
1242 // In this case we should restore the source node of the drag session to
1243 // new text node because it's required for dispatching `dragend` event.
1244 if (nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession()) {
1245 if (dragSession->IsDraggingTextInTextControl()) {
1246 nsCOMPtr<nsINode> sourceNode;
1247 if (NS_SUCCEEDED(
1248 dragSession->GetSourceNode(getter_AddRefs(sourceNode))) &&
1249 mFrame->GetContent() == sourceNode) {
1250 if (TextEditor* textEditor =
1251 mFrame->ControlElement()->GetTextEditorWithoutCreation()) {
1252 if (Element* anonymousDivElement = textEditor->GetRoot()) {
1253 if (anonymousDivElement && anonymousDivElement->GetFirstChild()) {
1254 MOZ_ASSERT(anonymousDivElement->GetFirstChild()->IsText());
1255 dragSession->UpdateSource(anonymousDivElement->GetFirstChild(),
1256 textEditor->GetSelection());
1263 // Otherwise, EventStateManager may be tracking gesture to start a drag.
1264 else {
1265 TextControlElement* textControlElement = mFrame->ControlElement();
1266 if (nsPresContext* presContext =
1267 textControlElement->GetPresContext(Element::eForComposedDoc)) {
1268 if (TextEditor* textEditor =
1269 textControlElement->GetTextEditorWithoutCreation()) {
1270 if (Element* anonymousDivElement = textEditor->GetRoot()) {
1271 presContext->EventStateManager()->TextControlRootAdded(
1272 *anonymousDivElement, *textControlElement);
1278 mFrame->FinishedInitializer();
1279 return NS_OK;
1282 NS_IMPL_ISUPPORTS(nsTextControlFrame::nsAnonDivObserver, nsIMutationObserver)
1284 void nsTextControlFrame::nsAnonDivObserver::CharacterDataChanged(
1285 nsIContent* aContent, const CharacterDataChangeInfo&) {
1286 mFrame.ClearCachedValue();
1289 void nsTextControlFrame::nsAnonDivObserver::ContentAppended(
1290 nsIContent* aFirstNewContent) {
1291 mFrame.ClearCachedValue();
1294 void nsTextControlFrame::nsAnonDivObserver::ContentInserted(
1295 nsIContent* aChild) {
1296 mFrame.ClearCachedValue();
1299 void nsTextControlFrame::nsAnonDivObserver::ContentRemoved(
1300 nsIContent* aChild, nsIContent* aPreviousSibling) {
1301 mFrame.ClearCachedValue();
1304 Maybe<nscoord> nsTextControlFrame::GetNaturalBaselineBOffset(
1305 mozilla::WritingMode aWM, BaselineSharingGroup aBaselineGroup,
1306 BaselineExportContext aExportContext) const {
1307 if (!IsSingleLineTextControl()) {
1308 if (StyleDisplay()->IsContainLayout()) {
1309 return Nothing{};
1312 if (aBaselineGroup == BaselineSharingGroup::First) {
1313 return Some(std::clamp(mFirstBaseline, 0, BSize(aWM)));
1315 // This isn't great, but the content of the root NAC isn't guaranteed
1316 // to be loaded, so the best we can do is the edge of the border-box.
1317 if (aWM.IsCentralBaseline()) {
1318 return Some(BSize(aWM) / 2);
1320 return Some(0);
1322 NS_ASSERTION(!IsSubtreeDirty(), "frame must not be dirty");
1323 return GetSingleLineTextControlBaseline(this, mFirstBaseline, aWM,
1324 aBaselineGroup);