Bug 1843035: part 1) Align unbinding a popover node from the tree closer to the spec...
[gecko.git] / dom / html / nsGenericHTMLElement.h
blobb323ff980c1ca5f696c65e7a2e9fa04d2e8a6326
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 #ifndef nsGenericHTMLElement_h___
7 #define nsGenericHTMLElement_h___
9 #include "mozilla/Attributes.h"
10 #include "mozilla/EventForwards.h"
11 #include "nsNameSpaceManager.h" // for kNameSpaceID_None
12 #include "nsIFormControl.h"
13 #include "nsGkAtoms.h"
14 #include "nsContentCreatorFunctions.h"
15 #include "nsStyledElement.h"
16 #include "mozilla/dom/BindingDeclarations.h"
17 #include "mozilla/dom/Element.h"
18 #include "mozilla/dom/DOMRect.h"
19 #include "mozilla/dom/ValidityState.h"
20 #include "mozilla/dom/PopoverData.h"
21 #include "mozilla/dom/ToggleEvent.h"
23 class nsDOMTokenList;
24 class nsIFormControlFrame;
25 class nsIFrame;
26 class nsILayoutHistoryState;
27 class nsIURI;
28 struct nsSize;
30 enum nsCSSPropertyID : int32_t;
32 namespace mozilla {
33 class EditorBase;
34 class ErrorResult;
35 class EventChainPostVisitor;
36 class EventChainPreVisitor;
37 class EventChainVisitor;
38 class EventListenerManager;
39 class PresState;
40 namespace dom {
41 class ElementInternals;
42 class HTMLFormElement;
43 } // namespace dom
44 } // namespace mozilla
46 using nsGenericHTMLElementBase = nsStyledElement;
48 /**
49 * A common superclass for HTML elements
51 class nsGenericHTMLElement : public nsGenericHTMLElementBase {
52 public:
53 using Element::Focus;
54 using Element::SetTabIndex;
55 explicit nsGenericHTMLElement(
56 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
57 : nsGenericHTMLElementBase(std::move(aNodeInfo)) {
58 NS_ASSERTION(mNodeInfo->NamespaceID() == kNameSpaceID_XHTML,
59 "Unexpected namespace");
60 AddStatesSilently(mozilla::dom::ElementState::LTR);
61 SetFlags(NODE_HAS_DIRECTION_LTR);
64 NS_INLINE_DECL_REFCOUNTING_INHERITED(nsGenericHTMLElement,
65 nsGenericHTMLElementBase)
67 NS_IMPL_FROMNODE(nsGenericHTMLElement, kNameSpaceID_XHTML)
69 // From Element
70 nsresult CopyInnerTo(mozilla::dom::Element* aDest);
72 void GetTitle(mozilla::dom::DOMString& aTitle) {
73 GetHTMLAttr(nsGkAtoms::title, aTitle);
75 void SetTitle(const nsAString& aTitle) {
76 SetHTMLAttr(nsGkAtoms::title, aTitle);
78 void GetLang(mozilla::dom::DOMString& aLang) {
79 GetHTMLAttr(nsGkAtoms::lang, aLang);
81 void SetLang(const nsAString& aLang) { SetHTMLAttr(nsGkAtoms::lang, aLang); }
82 bool Translate() const override;
83 void SetTranslate(bool aTranslate, mozilla::ErrorResult& aError) {
84 SetHTMLAttr(nsGkAtoms::translate, aTranslate ? u"yes"_ns : u"no"_ns,
85 aError);
87 void GetDir(nsAString& aDir) { GetHTMLEnumAttr(nsGkAtoms::dir, aDir); }
88 void SetDir(const nsAString& aDir, mozilla::ErrorResult& aError) {
89 SetHTMLAttr(nsGkAtoms::dir, aDir, aError);
91 void GetPopover(nsString& aPopover) const;
92 void SetPopover(const nsAString& aPopover, mozilla::ErrorResult& aError) {
93 SetOrRemoveNullableStringAttr(nsGkAtoms::popover, aPopover, aError);
95 bool Hidden() const { return GetBoolAttr(nsGkAtoms::hidden); }
96 void SetHidden(bool aHidden, mozilla::ErrorResult& aError) {
97 SetHTMLBoolAttr(nsGkAtoms::hidden, aHidden, aError);
99 bool Inert() const { return GetBoolAttr(nsGkAtoms::inert); }
100 void SetInert(bool aInert, mozilla::ErrorResult& aError) {
101 SetHTMLBoolAttr(nsGkAtoms::inert, aInert, aError);
103 MOZ_CAN_RUN_SCRIPT void Click(mozilla::dom::CallerType aCallerType);
104 void GetAccessKey(nsString& aAccessKey) {
105 GetHTMLAttr(nsGkAtoms::accesskey, aAccessKey);
107 void SetAccessKey(const nsAString& aAccessKey, mozilla::ErrorResult& aError) {
108 SetHTMLAttr(nsGkAtoms::accesskey, aAccessKey, aError);
110 void GetAccessKeyLabel(nsString& aAccessKeyLabel);
111 virtual bool Draggable() const {
112 return AttrValueIs(kNameSpaceID_None, nsGkAtoms::draggable,
113 nsGkAtoms::_true, eIgnoreCase);
115 void SetDraggable(bool aDraggable, mozilla::ErrorResult& aError) {
116 SetHTMLAttr(nsGkAtoms::draggable, aDraggable ? u"true"_ns : u"false"_ns,
117 aError);
119 void GetContentEditable(nsString& aContentEditable) {
120 ContentEditableTristate value = GetContentEditableValue();
121 if (value == eTrue) {
122 aContentEditable.AssignLiteral("true");
123 } else if (value == eFalse) {
124 aContentEditable.AssignLiteral("false");
125 } else {
126 aContentEditable.AssignLiteral("inherit");
129 void SetContentEditable(const nsAString& aContentEditable,
130 mozilla::ErrorResult& aError) {
131 if (aContentEditable.LowerCaseEqualsLiteral("inherit")) {
132 UnsetHTMLAttr(nsGkAtoms::contenteditable, aError);
133 } else if (aContentEditable.LowerCaseEqualsLiteral("true")) {
134 SetHTMLAttr(nsGkAtoms::contenteditable, u"true"_ns, aError);
135 } else if (aContentEditable.LowerCaseEqualsLiteral("false")) {
136 SetHTMLAttr(nsGkAtoms::contenteditable, u"false"_ns, aError);
137 } else {
138 aError.Throw(NS_ERROR_DOM_SYNTAX_ERR);
141 bool IsContentEditable() {
142 for (nsIContent* node = this; node; node = node->GetParent()) {
143 nsGenericHTMLElement* element = FromNode(node);
144 if (element) {
145 ContentEditableTristate value = element->GetContentEditableValue();
146 if (value != eInherit) {
147 return value == eTrue;
151 return false;
154 void AssertPopoverAttributeStateCorrespondsToAttributePresence() const;
155 mozilla::dom::PopoverAttributeState GetPopoverAttributeState() const;
156 void PopoverPseudoStateUpdate(bool aOpen, bool aNotify);
157 bool PopoverOpen() const;
158 bool CheckPopoverValidity(mozilla::dom::PopoverVisibilityState aExpectedState,
159 Document* aExpectedDocument, ErrorResult& aRv);
160 /** Returns true if the event has been cancelled. */
161 MOZ_CAN_RUN_SCRIPT bool FireToggleEvent(
162 mozilla::dom::PopoverVisibilityState aOldState,
163 mozilla::dom::PopoverVisibilityState aNewState, const nsAString& aType);
164 MOZ_CAN_RUN_SCRIPT void QueuePopoverEventTask(
165 mozilla::dom::PopoverVisibilityState aOldState);
166 MOZ_CAN_RUN_SCRIPT void RunPopoverToggleEventTask(
167 mozilla::dom::PopoverToggleEventTask* aTask,
168 mozilla::dom::PopoverVisibilityState aOldState);
169 MOZ_CAN_RUN_SCRIPT void ShowPopover(ErrorResult& aRv);
170 MOZ_CAN_RUN_SCRIPT void ShowPopoverInternal(Element* aInvoker,
171 ErrorResult& aRv);
172 MOZ_CAN_RUN_SCRIPT_BOUNDARY void HidePopoverWithoutRunningScript();
173 MOZ_CAN_RUN_SCRIPT void HidePopoverInternal(bool aFocusPreviousElement,
174 bool aFireEvents,
175 ErrorResult& aRv);
176 MOZ_CAN_RUN_SCRIPT void HidePopover(ErrorResult& aRv);
177 MOZ_CAN_RUN_SCRIPT bool TogglePopover(
178 const mozilla::dom::Optional<bool>& aForce, ErrorResult& aRv);
179 MOZ_CAN_RUN_SCRIPT void FocusPopover();
180 void ForgetPreviouslyFocusedElementAfterHidingPopover();
181 MOZ_CAN_RUN_SCRIPT void FocusPreviousElementAfterHidingPopover();
183 MOZ_CAN_RUN_SCRIPT void FocusCandidate(Element&, bool aClearUpFocus);
185 void SetNonce(const nsAString& aNonce) {
186 SetProperty(nsGkAtoms::nonce, new nsString(aNonce),
187 nsINode::DeleteProperty<nsString>, /* aTransfer = */ true);
189 void RemoveNonce() { RemoveProperty(nsGkAtoms::nonce); }
190 void GetNonce(nsAString& aNonce) const {
191 nsString* cspNonce = static_cast<nsString*>(GetProperty(nsGkAtoms::nonce));
192 if (cspNonce) {
193 aNonce = *cspNonce;
197 /** Returns whether a form control should be default-focusable. */
198 bool IsFormControlDefaultFocusable(bool aWithMouse) const;
201 * Returns the count of descendants (inclusive of this node) in
202 * the uncomposed document that are explicitly set as editable.
204 uint32_t EditableInclusiveDescendantCount();
206 bool Spellcheck();
207 void SetSpellcheck(bool aSpellcheck, mozilla::ErrorResult& aError) {
208 SetHTMLAttr(nsGkAtoms::spellcheck, aSpellcheck ? u"true"_ns : u"false"_ns,
209 aError);
212 MOZ_CAN_RUN_SCRIPT
213 void GetInnerText(mozilla::dom::DOMString& aValue, ErrorResult& aError);
214 MOZ_CAN_RUN_SCRIPT
215 void GetOuterText(mozilla::dom::DOMString& aValue, ErrorResult& aError) {
216 return GetInnerText(aValue, aError);
218 MOZ_CAN_RUN_SCRIPT void SetInnerText(const nsAString& aValue);
219 MOZ_CAN_RUN_SCRIPT void SetOuterText(const nsAString& aValue,
220 ErrorResult& aRv);
222 void GetInputMode(nsAString& aValue) {
223 GetEnumAttr(nsGkAtoms::inputmode, nullptr, aValue);
225 void SetInputMode(const nsAString& aValue, ErrorResult& aRv) {
226 SetHTMLAttr(nsGkAtoms::inputmode, aValue, aRv);
228 virtual void GetAutocapitalize(nsAString& aValue) const;
229 void SetAutocapitalize(const nsAString& aValue, ErrorResult& aRv) {
230 SetHTMLAttr(nsGkAtoms::autocapitalize, aValue, aRv);
233 void GetEnterKeyHint(nsAString& aValue) const {
234 GetEnumAttr(nsGkAtoms::enterkeyhint, nullptr, aValue);
236 void SetEnterKeyHint(const nsAString& aValue, ErrorResult& aRv) {
237 SetHTMLAttr(nsGkAtoms::enterkeyhint, aValue, aRv);
241 * Determine whether an attribute is an event (onclick, etc.)
242 * @param aName the attribute
243 * @return whether the name is an event handler name
245 bool IsEventAttributeNameInternal(nsAtom* aName) override;
247 #define EVENT(name_, id_, type_, struct_) /* nothing; handled by nsINode */
248 // The using nsINode::Get/SetOn* are to avoid warnings about shadowing the XPCOM
249 // getter and setter on nsINode.
250 #define FORWARDED_EVENT(name_, id_, type_, struct_) \
251 using nsINode::GetOn##name_; \
252 using nsINode::SetOn##name_; \
253 mozilla::dom::EventHandlerNonNull* GetOn##name_(); \
254 void SetOn##name_(mozilla::dom::EventHandlerNonNull* handler);
255 #define ERROR_EVENT(name_, id_, type_, struct_) \
256 using nsINode::GetOn##name_; \
257 using nsINode::SetOn##name_; \
258 already_AddRefed<mozilla::dom::EventHandlerNonNull> GetOn##name_(); \
259 void SetOn##name_(mozilla::dom::EventHandlerNonNull* handler);
260 #include "mozilla/EventNameList.h" // IWYU pragma: keep
261 #undef ERROR_EVENT
262 #undef FORWARDED_EVENT
263 #undef EVENT
264 mozilla::dom::Element* GetOffsetParent() {
265 mozilla::CSSIntRect rcFrame;
266 return GetOffsetRect(rcFrame);
268 int32_t OffsetTop() {
269 mozilla::CSSIntRect rcFrame;
270 GetOffsetRect(rcFrame);
272 return rcFrame.y;
274 int32_t OffsetLeft() {
275 mozilla::CSSIntRect rcFrame;
276 GetOffsetRect(rcFrame);
278 return rcFrame.x;
280 int32_t OffsetWidth() {
281 mozilla::CSSIntRect rcFrame;
282 GetOffsetRect(rcFrame);
284 return rcFrame.Width();
286 int32_t OffsetHeight() {
287 mozilla::CSSIntRect rcFrame;
288 GetOffsetRect(rcFrame);
290 return rcFrame.Height();
293 // These methods are already implemented in nsIContent but we want something
294 // faster for HTMLElements ignoring the namespace checking.
295 // This is safe because we already know that we are in the HTML namespace.
296 inline bool IsHTMLElement() const { return true; }
298 inline bool IsHTMLElement(nsAtom* aTag) const {
299 return mNodeInfo->Equals(aTag);
302 template <typename First, typename... Args>
303 inline bool IsAnyOfHTMLElements(First aFirst, Args... aArgs) const {
304 return IsNodeInternal(aFirst, aArgs...);
307 // https://html.spec.whatwg.org/multipage/custom-elements.html#dom-attachinternals
308 virtual already_AddRefed<mozilla::dom::ElementInternals> AttachInternals(
309 ErrorResult& aRv);
311 mozilla::dom::ElementInternals* GetInternals() const;
313 bool IsFormAssociatedCustomElements() const;
315 // Returns true if the event should not be handled from GetEventTargetParent.
316 virtual bool IsDisabledForEvents(mozilla::WidgetEvent* aEvent) {
317 return false;
320 bool Autofocus() const { return GetBoolAttr(nsGkAtoms::autofocus); }
321 void SetAutofocus(bool aVal, ErrorResult& aRv) {
322 SetHTMLBoolAttr(nsGkAtoms::autofocus, aVal, aRv);
325 protected:
326 virtual ~nsGenericHTMLElement() = default;
328 public:
329 // Implementation for nsIContent
330 nsresult BindToTree(BindContext&, nsINode& aParent) override;
331 void UnbindFromTree(bool aNullParent = true) override;
333 bool IsFocusableInternal(int32_t* aTabIndex, bool aWithMouse) override {
334 bool isFocusable = false;
335 IsHTMLFocusable(aWithMouse, &isFocusable, aTabIndex);
336 return isFocusable;
339 * Returns true if a subclass is not allowed to override the value returned
340 * in aIsFocusable.
342 virtual bool IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
343 int32_t* aTabIndex);
344 MOZ_CAN_RUN_SCRIPT
345 mozilla::Result<bool, nsresult> PerformAccesskey(
346 bool aKeyCausesActivation, bool aIsTrustedEvent) override;
349 * Check if an event for an anchor can be handled
350 * @return true if the event can be handled, false otherwise
352 bool CheckHandleEventForAnchorsPreconditions(
353 mozilla::EventChainVisitor& aVisitor);
354 void GetEventTargetParentForAnchors(mozilla::EventChainPreVisitor& aVisitor);
355 MOZ_CAN_RUN_SCRIPT
356 nsresult PostHandleEventForAnchors(mozilla::EventChainPostVisitor& aVisitor);
357 bool IsHTMLLink(nsIURI** aURI) const;
359 // HTML element methods
360 void Compact() { mAttrs.Compact(); }
362 void UpdateEditableState(bool aNotify) override;
364 mozilla::dom::ElementState IntrinsicState() const override;
366 // Helper for setting our editable flag and notifying
367 void DoSetEditableFlag(bool aEditable, bool aNotify) {
368 SetEditableFlag(aEditable);
369 UpdateState(aNotify);
372 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
373 const nsAString& aValue,
374 nsIPrincipal* aMaybeScriptedPrincipal,
375 nsAttrValue& aResult) override;
377 bool ParseBackgroundAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
378 const nsAString& aValue, nsAttrValue& aResult);
380 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
381 nsMapRuleToAttributesFunc GetAttributeMappingFunction() const override;
384 * Get the base target for any links within this piece
385 * of content. Generally, this is the document's base target,
386 * but certain content carries a local base for backward
387 * compatibility.
389 * @param aBaseTarget the base target [OUT]
391 void GetBaseTarget(nsAString& aBaseTarget) const;
394 * Get the primary form control frame for this element. Same as
395 * GetPrimaryFrame(), except it QI's to nsIFormControlFrame.
397 * @param aFlush whether to flush out frames so that they're up to date.
398 * @return the primary frame as nsIFormControlFrame
400 nsIFormControlFrame* GetFormControlFrame(bool aFlushFrames);
402 //----------------------------------------
405 * Parse an alignment attribute (top/middle/bottom/baseline)
407 * @param aString the string to parse
408 * @param aResult the resulting HTMLValue
409 * @return whether the value was parsed
411 static bool ParseAlignValue(const nsAString& aString, nsAttrValue& aResult);
414 * Parse a div align string to value (left/right/center/middle/justify)
416 * @param aString the string to parse
417 * @param aResult the resulting HTMLValue
418 * @return whether the value was parsed
420 static bool ParseDivAlignValue(const nsAString& aString,
421 nsAttrValue& aResult);
424 * Convert a table halign string to value (left/right/center/char/justify)
426 * @param aString the string to parse
427 * @param aResult the resulting HTMLValue
428 * @return whether the value was parsed
430 static bool ParseTableHAlignValue(const nsAString& aString,
431 nsAttrValue& aResult);
434 * Convert a table cell halign string to value
436 * @param aString the string to parse
437 * @param aResult the resulting HTMLValue
438 * @return whether the value was parsed
440 static bool ParseTableCellHAlignValue(const nsAString& aString,
441 nsAttrValue& aResult);
444 * Convert a table valign string to value (left/right/center/char/justify/
445 * abscenter/absmiddle/middle)
447 * @param aString the string to parse
448 * @param aResult the resulting HTMLValue
449 * @return whether the value was parsed
451 static bool ParseTableVAlignValue(const nsAString& aString,
452 nsAttrValue& aResult);
455 * Convert an image attribute to value (width, height, hspace, vspace, border)
457 * @param aAttribute the attribute to parse
458 * @param aString the string to parse
459 * @param aResult the resulting HTMLValue
460 * @return whether the value was parsed
462 static bool ParseImageAttribute(nsAtom* aAttribute, const nsAString& aString,
463 nsAttrValue& aResult);
465 static bool ParseReferrerAttribute(const nsAString& aString,
466 nsAttrValue& aResult);
469 * Convert a frameborder string to value (yes/no/1/0)
471 * @param aString the string to parse
472 * @param aResult the resulting HTMLValue
473 * @return whether the value was parsed
475 static bool ParseFrameborderValue(const nsAString& aString,
476 nsAttrValue& aResult);
479 * Convert a scrolling string to value (yes/no/on/off/scroll/noscroll/auto)
481 * @param aString the string to parse
482 * @param aResult the resulting HTMLValue
483 * @return whether the value was parsed
485 static bool ParseScrollingValue(const nsAString& aString,
486 nsAttrValue& aResult);
489 * Attribute Mapping Helpers
493 * A style attribute mapping function for the most common attributes, to be
494 * called by subclasses' attribute mapping functions. Currently handles
495 * dir, lang and hidden, could handle others.
497 * @param aAttributes the list of attributes to map
498 * @param aData the returned rule data [INOUT]
499 * @see GetAttributeMappingFunction
501 static void MapCommonAttributesInto(mozilla::MappedDeclarationsBuilder&);
503 * Same as MapCommonAttributesInto except that it does not handle hidden.
504 * @see GetAttributeMappingFunction
506 static void MapCommonAttributesIntoExceptHidden(
507 mozilla::MappedDeclarationsBuilder&);
509 static const MappedAttributeEntry sCommonAttributeMap[];
510 static const MappedAttributeEntry sImageMarginSizeAttributeMap[];
511 static const MappedAttributeEntry sImageBorderAttributeMap[];
512 static const MappedAttributeEntry sImageAlignAttributeMap[];
513 static const MappedAttributeEntry sDivAlignAttributeMap[];
514 static const MappedAttributeEntry sBackgroundAttributeMap[];
515 static const MappedAttributeEntry sBackgroundColorAttributeMap[];
518 * Helper to map the align attribute.
519 * @see GetAttributeMappingFunction
521 static void MapImageAlignAttributeInto(mozilla::MappedDeclarationsBuilder&);
524 * Helper to map the align attribute for things like <div>, <h1>, etc.
525 * @see GetAttributeMappingFunction
527 static void MapDivAlignAttributeInto(mozilla::MappedDeclarationsBuilder&);
530 * Helper to map the valign attribute for things like <col>, <tr>, <section>.
531 * @see GetAttributeMappingFunction
533 static void MapVAlignAttributeInto(mozilla::MappedDeclarationsBuilder&);
536 * Helper to map the image border attribute.
537 * @see GetAttributeMappingFunction
539 static void MapImageBorderAttributeInto(mozilla::MappedDeclarationsBuilder&);
541 * Helper to map the image margin attribute into a style struct.
543 * @param aAttributes the list of attributes to map
544 * @param aData the returned rule data [INOUT]
545 * @see GetAttributeMappingFunction
547 static void MapImageMarginAttributeInto(mozilla::MappedDeclarationsBuilder&);
550 * Helper to map a given dimension (width/height) into the declaration
551 * block, handling percentages and numbers.
553 static void MapDimensionAttributeInto(mozilla::MappedDeclarationsBuilder&,
554 nsCSSPropertyID, const nsAttrValue&);
557 * Maps the aspect ratio given width and height attributes.
559 static void DoMapAspectRatio(const nsAttrValue& aWidth,
560 const nsAttrValue& aHeight,
561 mozilla::MappedDeclarationsBuilder&);
563 // Whether to map the width and height attributes to aspect-ratio.
564 enum class MapAspectRatio { No, Yes };
567 * Helper to map the image position attribute into a style struct.
569 static void MapImageSizeAttributesInto(mozilla::MappedDeclarationsBuilder&,
570 MapAspectRatio = MapAspectRatio::No);
573 * Helper to map the width and height attributes into the aspect-ratio
574 * property.
576 * If you also map the width/height attributes to width/height (as you should
577 * for any HTML element that isn't <canvas>) then you should use
578 * MapImageSizeAttributesInto instead, passing MapAspectRatio::Yes instead, as
579 * that'd be faster.
581 static void MapAspectRatioInto(mozilla::MappedDeclarationsBuilder&);
584 * Helper to map `width` attribute into a style struct.
586 * @param aAttributes the list of attributes to map
587 * @param aData the returned rule data [INOUT]
588 * @see GetAttributeMappingFunction
590 static void MapWidthAttributeInto(mozilla::MappedDeclarationsBuilder&);
593 * Helper to map `height` attribute.
594 * @see GetAttributeMappingFunction
596 static void MapHeightAttributeInto(mozilla::MappedDeclarationsBuilder&);
598 * Helper to map the background attribute
599 * @see GetAttributeMappingFunction
601 static void MapBackgroundInto(mozilla::MappedDeclarationsBuilder&);
603 * Helper to map the bgcolor attribute
604 * @see GetAttributeMappingFunction
606 static void MapBGColorInto(mozilla::MappedDeclarationsBuilder&);
608 * Helper to map the background attributes (currently background and bgcolor)
609 * @see GetAttributeMappingFunction
611 static void MapBackgroundAttributesInto(mozilla::MappedDeclarationsBuilder&);
613 * Helper to map the scrolling attribute on FRAME and IFRAME.
614 * @see GetAttributeMappingFunction
616 static void MapScrollingAttributeInto(mozilla::MappedDeclarationsBuilder&);
618 // Form Helper Routines
620 * Find an ancestor of this content node which is a form (could be null)
621 * @param aCurrentForm the current form for this node. If this is
622 * non-null, and no ancestor form is found, and the current form is in
623 * a connected subtree with the node, the current form will be
624 * returned. This is needed to handle cases when HTML elements have a
625 * current form that they're not descendants of.
626 * @note This method should not be called if the element has a form attribute.
628 mozilla::dom::HTMLFormElement* FindAncestorForm(
629 mozilla::dom::HTMLFormElement* aCurrentForm = nullptr);
632 * See if the document being tested has nav-quirks mode enabled.
633 * @param doc the document
635 static bool InNavQuirksMode(Document*);
638 * Gets the absolute URI value of an attribute, by resolving any relative
639 * URIs in the attribute against the baseuri of the element. If the attribute
640 * isn't a relative URI the value of the attribute is returned as is. Only
641 * works for attributes in null namespace.
643 * @param aAttr name of attribute.
644 * @param aBaseAttr name of base attribute.
645 * @param aResult result value [out]
647 void GetURIAttr(nsAtom* aAttr, nsAtom* aBaseAttr, nsAString& aResult) const;
650 * Gets the absolute URI values of an attribute, by resolving any relative
651 * URIs in the attribute against the baseuri of the element. If a substring
652 * isn't a relative URI, the substring is returned as is. Only works for
653 * attributes in null namespace.
655 bool GetURIAttr(nsAtom* aAttr, nsAtom* aBaseAttr, nsIURI** aURI) const;
657 bool IsHidden() const { return HasAttr(nsGkAtoms::hidden); }
659 bool IsLabelable() const override;
661 static bool MatchLabelsElement(Element* aElement, int32_t aNamespaceID,
662 nsAtom* aAtom, void* aData);
664 already_AddRefed<nsINodeList> Labels();
666 static bool LegacyTouchAPIEnabled(JSContext* aCx, JSObject* aObj);
668 static inline bool CanHaveName(nsAtom* aTag) {
669 return aTag == nsGkAtoms::img || aTag == nsGkAtoms::form ||
670 aTag == nsGkAtoms::embed || aTag == nsGkAtoms::object;
672 static inline bool ShouldExposeNameAsHTMLDocumentProperty(Element* aElement) {
673 return aElement->IsHTMLElement() &&
674 CanHaveName(aElement->NodeInfo()->NameAtom());
676 static inline bool ShouldExposeIdAsHTMLDocumentProperty(Element* aElement) {
677 if (aElement->IsHTMLElement(nsGkAtoms::object)) {
678 return true;
681 // Per spec, <img> is exposed by id only if it also has a nonempty
682 // name (which doesn't have to match the id or anything).
683 // HasName() is true precisely when name is nonempty.
684 return aElement->IsHTMLElement(nsGkAtoms::img) && aElement->HasName();
687 virtual inline void ResultForDialogSubmit(nsAString& aResult) {
688 GetAttr(nsGkAtoms::value, aResult);
691 protected:
693 * Add/remove this element to the documents name cache
695 void AddToNameTable(nsAtom* aName);
696 void RemoveFromNameTable();
699 * Register or unregister an access key to this element based on the
700 * accesskey attribute.
702 void RegUnRegAccessKey(bool aDoReg) override {
703 if (!HasFlag(NODE_HAS_ACCESSKEY)) {
704 return;
707 nsStyledElement::RegUnRegAccessKey(aDoReg);
710 protected:
711 void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
712 const nsAttrValue* aValue, bool aNotify) override;
713 // TODO: Convert AfterSetAttr to MOZ_CAN_RUN_SCRIPT and get rid of
714 // kungFuDeathGrip in it.
715 MOZ_CAN_RUN_SCRIPT_BOUNDARY void AfterSetAttr(
716 int32_t aNamespaceID, nsAtom* aName, const nsAttrValue* aValue,
717 const nsAttrValue* aOldValue, nsIPrincipal* aMaybeScriptedPrincipal,
718 bool aNotify) override;
720 MOZ_CAN_RUN_SCRIPT void AfterSetPopoverAttr();
722 mozilla::EventListenerManager* GetEventListenerManagerForAttr(
723 nsAtom* aAttrName, bool* aDefer) override;
726 * Handles dispatching a simulated click on `this` on space or enter.
727 * TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230)
729 MOZ_CAN_RUN_SCRIPT_BOUNDARY void HandleKeyboardActivation(
730 mozilla::EventChainPostVisitor&);
732 /** Dispatch a simulated mouse click by keyboard to the given element. */
733 MOZ_CAN_RUN_SCRIPT static nsresult DispatchSimulatedClick(
734 nsGenericHTMLElement* aElement, bool aIsTrusted,
735 nsPresContext* aPresContext);
738 * Create a URI for the given aURISpec string.
739 * Returns INVALID_STATE_ERR and nulls *aURI if aURISpec is empty
740 * and the document's URI matches the element's base URI.
742 nsresult NewURIFromString(const nsAString& aURISpec, nsIURI** aURI);
744 void GetHTMLAttr(nsAtom* aName, nsAString& aResult) const {
745 GetAttr(aName, aResult);
747 void GetHTMLAttr(nsAtom* aName, mozilla::dom::DOMString& aResult) const {
748 GetAttr(aName, aResult);
750 void GetHTMLEnumAttr(nsAtom* aName, nsAString& aResult) const {
751 GetEnumAttr(aName, nullptr, aResult);
753 void GetHTMLURIAttr(nsAtom* aName, nsAString& aResult) const {
754 GetURIAttr(aName, nullptr, aResult);
757 void SetHTMLAttr(nsAtom* aName, const nsAString& aValue) {
758 SetAttr(kNameSpaceID_None, aName, aValue, true);
760 void SetHTMLAttr(nsAtom* aName, const nsAString& aValue,
761 mozilla::ErrorResult& aError) {
762 SetAttr(aName, aValue, aError);
764 void SetHTMLAttr(nsAtom* aName, const nsAString& aValue,
765 nsIPrincipal* aTriggeringPrincipal,
766 mozilla::ErrorResult& aError) {
767 SetAttr(aName, aValue, aTriggeringPrincipal, aError);
769 void UnsetHTMLAttr(nsAtom* aName, mozilla::ErrorResult& aError) {
770 UnsetAttr(aName, aError);
772 void SetHTMLBoolAttr(nsAtom* aName, bool aValue,
773 mozilla::ErrorResult& aError) {
774 if (aValue) {
775 SetHTMLAttr(aName, u""_ns, aError);
776 } else {
777 UnsetHTMLAttr(aName, aError);
780 template <typename T>
781 void SetHTMLIntAttr(nsAtom* aName, T aValue, mozilla::ErrorResult& aError) {
782 nsAutoString value;
783 value.AppendInt(aValue);
785 SetHTMLAttr(aName, value, aError);
789 * Gets the integer-value of an attribute, returns specified default value
790 * if the attribute isn't set or isn't set to an integer. Only works for
791 * attributes in null namespace.
793 * @param aAttr name of attribute.
794 * @param aDefault default-value to return if attribute isn't set.
796 int32_t GetIntAttr(nsAtom* aAttr, int32_t aDefault) const;
799 * Sets value of attribute to specified integer. Only works for attributes
800 * in null namespace.
802 * @param aAttr name of attribute.
803 * @param aValue Integer value of attribute.
805 nsresult SetIntAttr(nsAtom* aAttr, int32_t aValue);
808 * Gets the unsigned integer-value of an attribute, returns specified default
809 * value if the attribute isn't set or isn't set to an integer. Only works for
810 * attributes in null namespace.
812 * @param aAttr name of attribute.
813 * @param aDefault default-value to return if attribute isn't set.
815 uint32_t GetUnsignedIntAttr(nsAtom* aAttr, uint32_t aDefault) const;
818 * Sets value of attribute to specified unsigned integer. Only works for
819 * attributes in null namespace.
821 * @param aAttr name of attribute.
822 * @param aValue Integer value of attribute.
823 * @param aDefault Default value (in case value is out of range). If the spec
824 * doesn't provide one, should be 1 if the value is limited to
825 * nonzero values, and 0 otherwise.
827 void SetUnsignedIntAttr(nsAtom* aName, uint32_t aValue, uint32_t aDefault,
828 mozilla::ErrorResult& aError) {
829 nsAutoString value;
830 if (aValue > INT32_MAX) {
831 value.AppendInt(aDefault);
832 } else {
833 value.AppendInt(aValue);
836 SetHTMLAttr(aName, value, aError);
840 * Gets the unsigned integer-value of an attribute that is stored as a
841 * dimension (i.e. could be an integer or a percentage), returns specified
842 * default value if the attribute isn't set or isn't set to a dimension. Only
843 * works for attributes in null namespace.
845 * @param aAttr name of attribute.
846 * @param aDefault default-value to return if attribute isn't set.
848 uint32_t GetDimensionAttrAsUnsignedInt(nsAtom* aAttr,
849 uint32_t aDefault) const;
852 * Sets value of attribute to specified double. Only works for attributes
853 * in null namespace.
855 * @param aAttr name of attribute.
856 * @param aValue Double value of attribute.
858 void SetDoubleAttr(nsAtom* aAttr, double aValue, mozilla::ErrorResult& aRv) {
859 nsAutoString value;
860 value.AppendFloat(aValue);
862 SetHTMLAttr(aAttr, value, aRv);
866 * Locates the EditorBase associated with this node. In general this is
867 * equivalent to GetEditorInternal(), but for designmode or contenteditable,
868 * this may need to get an editor that's not actually on this element's
869 * associated TextControlFrame. This is used by the spellchecking routines
870 * to get the editor affected by changing the spellcheck attribute on this
871 * node.
873 virtual already_AddRefed<mozilla::EditorBase> GetAssociatedEditor();
876 * Get the frame's offset information for offsetTop/Left/Width/Height.
877 * Returns the parent the offset is relative to.
878 * @note This method flushes pending notifications (FlushType::Layout).
879 * @param aRect the offset information [OUT]
881 mozilla::dom::Element* GetOffsetRect(mozilla::CSSIntRect& aRect);
884 * Ensures all editors associated with a subtree are synced, for purposes of
885 * spellchecking.
887 static void SyncEditorsOnSubtree(nsIContent* content);
889 enum ContentEditableTristate { eInherit = -1, eFalse = 0, eTrue = 1 };
892 * Returns eTrue if the element has a contentEditable attribute and its value
893 * is "true" or an empty string. Returns eFalse if the element has a
894 * contentEditable attribute and its value is "false". Otherwise returns
895 * eInherit.
897 ContentEditableTristate GetContentEditableValue() const {
898 static const Element::AttrValuesArray values[] = {
899 nsGkAtoms::_false, nsGkAtoms::_true, nsGkAtoms::_empty, nullptr};
901 if (!MayHaveContentEditableAttr()) return eInherit;
903 int32_t value = FindAttrValueIn(
904 kNameSpaceID_None, nsGkAtoms::contenteditable, values, eIgnoreCase);
906 return value > 0 ? eTrue : (value == 0 ? eFalse : eInherit);
909 // Used by A, AREA, LINK, and STYLE.
910 already_AddRefed<nsIURI> GetHrefURIForAnchors() const;
912 public:
914 * Returns whether this element is an editable root. There are two types of
915 * editable roots:
916 * 1) the documentElement if the whole document is editable (for example for
917 * desginMode=on)
918 * 2) an element that is marked editable with contentEditable=true and that
919 * doesn't have a parent or whose parent is not editable.
920 * Note that this doesn't return input and textarea elements that haven't been
921 * made editable through contentEditable or designMode.
923 bool IsEditableRoot() const;
925 private:
926 void ChangeEditableState(int32_t aChange);
929 namespace mozilla::dom {
930 class HTMLFieldSetElement;
931 } // namespace mozilla::dom
933 #define HTML_ELEMENT_FLAG_BIT(n_) \
934 NODE_FLAG_BIT(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET + (n_))
936 // HTMLElement specific bits
937 enum {
938 // Used to handle keyboard activation.
939 HTML_ELEMENT_ACTIVE_FOR_KEYBOARD = HTML_ELEMENT_FLAG_BIT(0),
940 // Similar to HTMLInputElement's mInhibitRestoration, used to prevent
941 // form-associated custom elements not created by a network parser from
942 // being restored.
943 HTML_ELEMENT_INHIBIT_RESTORATION = HTML_ELEMENT_FLAG_BIT(1),
945 // Remaining bits are type specific.
946 HTML_ELEMENT_TYPE_SPECIFIC_BITS_OFFSET =
947 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET + 2,
950 ASSERT_NODE_FLAGS_SPACE(HTML_ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
952 #define FORM_ELEMENT_FLAG_BIT(n_) \
953 NODE_FLAG_BIT(HTML_ELEMENT_TYPE_SPECIFIC_BITS_OFFSET + (n_))
955 // Form element specific bits
956 enum {
957 // If this flag is set on an nsGenericHTMLFormElement or an HTMLImageElement,
958 // that means that we have added ourselves to our mForm. It's possible to
959 // have a non-null mForm, but not have this flag set. That happens when the
960 // form is set via the content sink.
961 ADDED_TO_FORM = FORM_ELEMENT_FLAG_BIT(0),
963 // If this flag is set on an nsGenericHTMLFormElement or an HTMLImageElement,
964 // that means that its form is in the process of being unbound from the tree,
965 // and this form element hasn't re-found its form in
966 // nsGenericHTMLFormElement::UnbindFromTree yet.
967 MAYBE_ORPHAN_FORM_ELEMENT = FORM_ELEMENT_FLAG_BIT(1),
969 // If this flag is set on an nsGenericHTMLElement or an HTMLImageElement, then
970 // the element might be in the past names map of its form.
971 MAY_BE_IN_PAST_NAMES_MAP = FORM_ELEMENT_FLAG_BIT(2)
974 // NOTE: I don't think it's possible to have both ADDED_TO_FORM and
975 // MAYBE_ORPHAN_FORM_ELEMENT set at the same time, so if it becomes an issue we
976 // can probably merge them into the same bit. --bz
978 ASSERT_NODE_FLAGS_SPACE(HTML_ELEMENT_TYPE_SPECIFIC_BITS_OFFSET + 3);
980 #undef FORM_ELEMENT_FLAG_BIT
983 * A helper class for form elements that can contain children
985 class nsGenericHTMLFormElement : public nsGenericHTMLElement {
986 public:
987 nsGenericHTMLFormElement(
988 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
990 // nsIContent
991 void SaveSubtreeState() override;
992 nsresult BindToTree(BindContext&, nsINode& aParent) override;
993 void UnbindFromTree(bool aNullParent = true) override;
996 * This callback is called by a fieldest on all its elements whenever its
997 * disabled attribute is changed so the element knows its disabled state
998 * might have changed.
1000 * @note Classes redefining this method should not do any content
1001 * state updates themselves but should just make sure to call into
1002 * nsGenericHTMLFormElement::FieldSetDisabledChanged.
1004 virtual void FieldSetDisabledChanged(bool aNotify);
1006 void FieldSetFirstLegendChanged(bool aNotify) { UpdateFieldSet(aNotify); }
1009 * This callback is called by a fieldset on all it's elements when it's being
1010 * destroyed. When called, the elements should check that aFieldset is there
1011 * first parent fieldset and null mFieldset in that case only.
1013 * @param aFieldSet The fieldset being removed.
1015 void ForgetFieldSet(nsIContent* aFieldset);
1017 void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete);
1020 * Get the layout history object for a particular piece of content.
1022 * @param aRead if true, won't return a layout history state if the
1023 * layout history state is empty.
1024 * @return the history state object
1026 already_AddRefed<nsILayoutHistoryState> GetLayoutHistory(bool aRead);
1028 protected:
1029 virtual ~nsGenericHTMLFormElement() = default;
1031 void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1032 const nsAttrValue* aValue, bool aNotify) override;
1034 void AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
1035 const nsAttrValue* aValue, const nsAttrValue* aOldValue,
1036 nsIPrincipal* aMaybeScriptedPrincipal,
1037 bool aNotify) override;
1039 virtual void BeforeSetForm(bool aBindToTree) {}
1041 virtual void AfterClearForm(bool aUnbindOrDelete) {}
1044 * Check our disabled content attribute and fieldset's (if it exists) disabled
1045 * state to decide whether our disabled flag should be toggled.
1047 virtual void UpdateDisabledState(bool aNotify);
1049 virtual void SetFormInternal(mozilla::dom::HTMLFormElement* aForm,
1050 bool aBindToTree) {}
1052 virtual mozilla::dom::HTMLFormElement* GetFormInternal() const {
1053 return nullptr;
1056 virtual mozilla::dom::HTMLFieldSetElement* GetFieldSetInternal() const {
1057 return nullptr;
1060 virtual void SetFieldSetInternal(
1061 mozilla::dom::HTMLFieldSetElement* aFieldset) {}
1064 * This method will update the form owner, using @form or looking to a parent.
1066 * @param aBindToTree Whether the element is being attached to the tree.
1067 * @param aFormIdElement The element associated with the id in @form. If
1068 * aBindToTree is false, aFormIdElement *must* contain the element associated
1069 * with the id in @form. Otherwise, it *must* be null.
1071 * @note Callers of UpdateFormOwner have to be sure the element is in a
1072 * document (GetUncomposedDoc() != nullptr).
1074 virtual void UpdateFormOwner(bool aBindToTree, Element* aFormIdElement);
1077 * This method will update mFieldset and set it to the first fieldset parent.
1079 void UpdateFieldSet(bool aNotify);
1082 * Add a form id observer which will observe when the element with the id in
1083 * @form will change.
1085 * @return The element associated with the current id in @form (may be null).
1087 Element* AddFormIdObserver();
1090 * Remove the form id observer.
1092 void RemoveFormIdObserver();
1095 * This method is a a callback for IDTargetObserver (from Document).
1096 * It will be called each time the element associated with the id in @form
1097 * changes.
1099 static bool FormIdUpdated(Element* aOldElement, Element* aNewElement,
1100 void* aData);
1102 // Returns true if the event should not be handled from GetEventTargetParent
1103 bool IsElementDisabledForEvents(mozilla::WidgetEvent* aEvent,
1104 nsIFrame* aFrame);
1107 * Returns if the control can be disabled.
1109 virtual bool CanBeDisabled() const { return false; }
1112 * Returns if the readonly attribute applies.
1114 virtual bool DoesReadOnlyApply() const { return false; }
1117 * Returns true if the element is a form associated element.
1118 * See https://html.spec.whatwg.org/#form-associated-element.
1120 virtual bool IsFormAssociatedElement() const { return false; }
1123 * Save to presentation state. The form element will determine whether it
1124 * has anything to save and if so, create an entry in the layout history for
1125 * its pres context.
1127 virtual void SaveState() {}
1130 class nsGenericHTMLFormControlElement : public nsGenericHTMLFormElement,
1131 public nsIFormControl {
1132 public:
1133 nsGenericHTMLFormControlElement(
1134 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, FormControlType);
1136 NS_DECL_ISUPPORTS_INHERITED
1138 NS_IMPL_FROMNODE_HELPER(nsGenericHTMLFormControlElement,
1139 IsHTMLFormControlElement())
1141 // nsINode
1142 nsINode* GetScopeChainParent() const override;
1143 bool IsHTMLFormControlElement() const final { return true; }
1145 // nsIContent
1146 IMEState GetDesiredIMEState() override;
1148 // nsGenericHTMLElement
1149 // autocapitalize attribute support
1150 void GetAutocapitalize(nsAString& aValue) const override;
1151 bool IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
1152 int32_t* aTabIndex) override;
1154 // EventTarget
1155 void GetEventTargetParent(mozilla::EventChainPreVisitor& aVisitor) override;
1156 nsresult PreHandleEvent(mozilla::EventChainVisitor& aVisitor) override;
1158 // nsIFormControl
1159 mozilla::dom::HTMLFieldSetElement* GetFieldSet() override;
1160 mozilla::dom::HTMLFormElement* GetForm() const override { return mForm; }
1161 void SetForm(mozilla::dom::HTMLFormElement* aForm) override;
1162 void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) override;
1164 protected:
1165 virtual ~nsGenericHTMLFormControlElement();
1167 // Element
1168 mozilla::dom::ElementState IntrinsicState() const override;
1169 bool IsLabelable() const override;
1171 // nsGenericHTMLFormElement
1172 bool CanBeDisabled() const override;
1173 bool DoesReadOnlyApply() const override;
1174 void SetFormInternal(mozilla::dom::HTMLFormElement* aForm,
1175 bool aBindToTree) override;
1176 mozilla::dom::HTMLFormElement* GetFormInternal() const override;
1177 mozilla::dom::HTMLFieldSetElement* GetFieldSetInternal() const override;
1178 void SetFieldSetInternal(
1179 mozilla::dom::HTMLFieldSetElement* aFieldset) override;
1180 bool IsFormAssociatedElement() const override { return true; }
1183 * Update our required/optional flags to match the given aIsRequired boolean.
1185 void UpdateRequiredState(bool aIsRequired, bool aNotify);
1187 bool IsAutocapitalizeInheriting() const;
1189 nsresult SubmitDirnameDir(mozilla::dom::FormData* aFormData);
1191 /** The form that contains this control */
1192 mozilla::dom::HTMLFormElement* mForm;
1194 /* This is a pointer to our closest fieldset parent if any */
1195 mozilla::dom::HTMLFieldSetElement* mFieldSet;
1198 enum class PopoverTargetAction : uint8_t {
1199 Toggle,
1200 Show,
1201 Hide,
1204 class nsGenericHTMLFormControlElementWithState
1205 : public nsGenericHTMLFormControlElement {
1206 public:
1207 nsGenericHTMLFormControlElementWithState(
1208 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
1209 mozilla::dom::FromParser aFromParser, FormControlType);
1211 bool IsGenericHTMLFormControlElementWithState() const final { return true; }
1212 NS_IMPL_FROMNODE_HELPER(nsGenericHTMLFormControlElementWithState,
1213 IsGenericHTMLFormControlElementWithState())
1215 // Element
1216 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1217 const nsAString& aValue,
1218 nsIPrincipal* aMaybeScriptedPrincipal,
1219 nsAttrValue& aResult) override;
1221 // PopoverInvokerElement
1222 mozilla::dom::Element* GetPopoverTargetElement() const;
1223 void SetPopoverTargetElement(mozilla::dom::Element*);
1224 void GetPopoverTargetAction(nsAString& aValue) const {
1225 GetHTMLEnumAttr(nsGkAtoms::popovertargetaction, aValue);
1227 void SetPopoverTargetAction(const nsAString& aValue) {
1228 SetHTMLAttr(nsGkAtoms::popovertargetaction, aValue);
1232 * https://html.spec.whatwg.org/#popover-target-attribute-activation-behavior
1234 MOZ_CAN_RUN_SCRIPT void HandlePopoverTargetAction();
1237 * Get the presentation state for a piece of content, or create it if it does
1238 * not exist. Generally used by SaveState().
1240 mozilla::PresState* GetPrimaryPresState();
1243 * Called when we have been cloned and adopted, and the information of the
1244 * node has been changed.
1246 void NodeInfoChanged(Document* aOldDoc) override;
1248 void GetFormAction(nsString& aValue);
1250 protected:
1252 * Restore from presentation state. You pass in the presentation state for
1253 * this form control (generated with GenerateStateKey() + "-C") and the form
1254 * control will grab its state from there.
1256 * @param aState the pres state to use to restore the control
1257 * @return true if the form control was a checkbox and its
1258 * checked state was restored, false otherwise.
1260 virtual bool RestoreState(mozilla::PresState* aState) { return false; }
1263 * Restore the state for a form control in response to the element being
1264 * inserted into the document by the parser. Ends up calling RestoreState().
1266 * GenerateStateKey() must already have been called.
1268 * @return false if RestoreState() was not called, the return
1269 * value of RestoreState() otherwise.
1271 bool RestoreFormControlState();
1273 /* Generates the state key for saving the form state in the session if not
1274 computed already. The result is stored in mStateKey. */
1275 void GenerateStateKey();
1277 int32_t GetParserInsertedControlNumberForStateKey() const override {
1278 return mControlNumber;
1281 /* Used to store the key to that element in the session. Is void until
1282 GenerateStateKey has been used */
1283 nsCString mStateKey;
1285 // A number for this form control that is unique within its owner document.
1286 // This is only set to a number for elements inserted into the document by
1287 // the parser from the network. Otherwise, it is -1.
1288 int32_t mControlNumber;
1291 #define NS_INTERFACE_MAP_ENTRY_IF_TAG(_interface, _tag) \
1292 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(_interface, \
1293 mNodeInfo->Equals(nsGkAtoms::_tag))
1295 namespace mozilla::dom {
1297 using HTMLContentCreatorFunction =
1298 nsGenericHTMLElement* (*)(already_AddRefed<mozilla::dom::NodeInfo>&&,
1299 mozilla::dom::FromParser);
1301 } // namespace mozilla::dom
1304 * A macro to declare the NS_NewHTMLXXXElement() functions.
1306 #define NS_DECLARE_NS_NEW_HTML_ELEMENT(_elementName) \
1307 namespace mozilla { \
1308 namespace dom { \
1309 class HTML##_elementName##Element; \
1312 nsGenericHTMLElement* NS_NewHTML##_elementName##Element( \
1313 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
1314 mozilla::dom::FromParser aFromParser = mozilla::dom::NOT_FROM_PARSER);
1316 #define NS_DECLARE_NS_NEW_HTML_ELEMENT_AS_SHARED(_elementName) \
1317 inline nsGenericHTMLElement* NS_NewHTML##_elementName##Element( \
1318 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
1319 mozilla::dom::FromParser aFromParser = mozilla::dom::NOT_FROM_PARSER) { \
1320 return NS_NewHTMLSharedElement(std::move(aNodeInfo), aFromParser); \
1324 * A macro to implement the NS_NewHTMLXXXElement() functions.
1326 #define NS_IMPL_NS_NEW_HTML_ELEMENT(_elementName) \
1327 nsGenericHTMLElement* NS_NewHTML##_elementName##Element( \
1328 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
1329 mozilla::dom::FromParser aFromParser) { \
1330 RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo); \
1331 auto* nim = nodeInfo->NodeInfoManager(); \
1332 MOZ_ASSERT(nim); \
1333 return new (nim) \
1334 mozilla::dom::HTML##_elementName##Element(nodeInfo.forget()); \
1337 #define NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(_elementName) \
1338 nsGenericHTMLElement* NS_NewHTML##_elementName##Element( \
1339 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, \
1340 mozilla::dom::FromParser aFromParser) { \
1341 RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo); \
1342 auto* nim = nodeInfo->NodeInfoManager(); \
1343 MOZ_ASSERT(nim); \
1344 return new (nim) mozilla::dom::HTML##_elementName##Element( \
1345 nodeInfo.forget(), aFromParser); \
1348 // Here, we expand 'NS_DECLARE_NS_NEW_HTML_ELEMENT()' by hand.
1349 // (Calling the macro directly (with no args) produces compiler warnings.)
1350 nsGenericHTMLElement* NS_NewHTMLElement(
1351 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
1352 mozilla::dom::FromParser aFromParser = mozilla::dom::NOT_FROM_PARSER);
1354 // Distinct from the above in order to have function pointer that compared
1355 // unequal to a function pointer to the above.
1356 nsGenericHTMLElement* NS_NewCustomElement(
1357 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
1358 mozilla::dom::FromParser aFromParser = mozilla::dom::NOT_FROM_PARSER);
1360 NS_DECLARE_NS_NEW_HTML_ELEMENT(Shared)
1361 NS_DECLARE_NS_NEW_HTML_ELEMENT(SharedList)
1363 NS_DECLARE_NS_NEW_HTML_ELEMENT(Anchor)
1364 NS_DECLARE_NS_NEW_HTML_ELEMENT(Area)
1365 NS_DECLARE_NS_NEW_HTML_ELEMENT(Audio)
1366 NS_DECLARE_NS_NEW_HTML_ELEMENT(BR)
1367 NS_DECLARE_NS_NEW_HTML_ELEMENT(Body)
1368 NS_DECLARE_NS_NEW_HTML_ELEMENT(Button)
1369 NS_DECLARE_NS_NEW_HTML_ELEMENT(Canvas)
1370 NS_DECLARE_NS_NEW_HTML_ELEMENT(Content)
1371 NS_DECLARE_NS_NEW_HTML_ELEMENT(Mod)
1372 NS_DECLARE_NS_NEW_HTML_ELEMENT(Data)
1373 NS_DECLARE_NS_NEW_HTML_ELEMENT(DataList)
1374 NS_DECLARE_NS_NEW_HTML_ELEMENT(Details)
1375 NS_DECLARE_NS_NEW_HTML_ELEMENT(Dialog)
1376 NS_DECLARE_NS_NEW_HTML_ELEMENT(Div)
1377 NS_DECLARE_NS_NEW_HTML_ELEMENT(Embed)
1378 NS_DECLARE_NS_NEW_HTML_ELEMENT(FieldSet)
1379 NS_DECLARE_NS_NEW_HTML_ELEMENT(Font)
1380 NS_DECLARE_NS_NEW_HTML_ELEMENT(Form)
1381 NS_DECLARE_NS_NEW_HTML_ELEMENT(Frame)
1382 NS_DECLARE_NS_NEW_HTML_ELEMENT(FrameSet)
1383 NS_DECLARE_NS_NEW_HTML_ELEMENT(HR)
1384 NS_DECLARE_NS_NEW_HTML_ELEMENT_AS_SHARED(Head)
1385 NS_DECLARE_NS_NEW_HTML_ELEMENT(Heading)
1386 NS_DECLARE_NS_NEW_HTML_ELEMENT_AS_SHARED(Html)
1387 NS_DECLARE_NS_NEW_HTML_ELEMENT(IFrame)
1388 NS_DECLARE_NS_NEW_HTML_ELEMENT(Image)
1389 NS_DECLARE_NS_NEW_HTML_ELEMENT(Input)
1390 NS_DECLARE_NS_NEW_HTML_ELEMENT(LI)
1391 NS_DECLARE_NS_NEW_HTML_ELEMENT(Label)
1392 NS_DECLARE_NS_NEW_HTML_ELEMENT(Legend)
1393 NS_DECLARE_NS_NEW_HTML_ELEMENT(Link)
1394 NS_DECLARE_NS_NEW_HTML_ELEMENT(Marquee)
1395 NS_DECLARE_NS_NEW_HTML_ELEMENT(Map)
1396 NS_DECLARE_NS_NEW_HTML_ELEMENT(Menu)
1397 NS_DECLARE_NS_NEW_HTML_ELEMENT(Meta)
1398 NS_DECLARE_NS_NEW_HTML_ELEMENT(Meter)
1399 NS_DECLARE_NS_NEW_HTML_ELEMENT(Object)
1400 NS_DECLARE_NS_NEW_HTML_ELEMENT(OptGroup)
1401 NS_DECLARE_NS_NEW_HTML_ELEMENT(Option)
1402 NS_DECLARE_NS_NEW_HTML_ELEMENT(Output)
1403 NS_DECLARE_NS_NEW_HTML_ELEMENT(Paragraph)
1404 NS_DECLARE_NS_NEW_HTML_ELEMENT(Picture)
1405 NS_DECLARE_NS_NEW_HTML_ELEMENT(Pre)
1406 NS_DECLARE_NS_NEW_HTML_ELEMENT(Progress)
1407 NS_DECLARE_NS_NEW_HTML_ELEMENT(Script)
1408 NS_DECLARE_NS_NEW_HTML_ELEMENT(Select)
1409 NS_DECLARE_NS_NEW_HTML_ELEMENT(Slot)
1410 NS_DECLARE_NS_NEW_HTML_ELEMENT(Source)
1411 NS_DECLARE_NS_NEW_HTML_ELEMENT(Span)
1412 NS_DECLARE_NS_NEW_HTML_ELEMENT(Style)
1413 NS_DECLARE_NS_NEW_HTML_ELEMENT(Summary)
1414 NS_DECLARE_NS_NEW_HTML_ELEMENT(TableCaption)
1415 NS_DECLARE_NS_NEW_HTML_ELEMENT(TableCell)
1416 NS_DECLARE_NS_NEW_HTML_ELEMENT(TableCol)
1417 NS_DECLARE_NS_NEW_HTML_ELEMENT(Table)
1418 NS_DECLARE_NS_NEW_HTML_ELEMENT(TableRow)
1419 NS_DECLARE_NS_NEW_HTML_ELEMENT(TableSection)
1420 NS_DECLARE_NS_NEW_HTML_ELEMENT(Tbody)
1421 NS_DECLARE_NS_NEW_HTML_ELEMENT(Template)
1422 NS_DECLARE_NS_NEW_HTML_ELEMENT(TextArea)
1423 NS_DECLARE_NS_NEW_HTML_ELEMENT(Tfoot)
1424 NS_DECLARE_NS_NEW_HTML_ELEMENT(Thead)
1425 NS_DECLARE_NS_NEW_HTML_ELEMENT(Time)
1426 NS_DECLARE_NS_NEW_HTML_ELEMENT(Title)
1427 NS_DECLARE_NS_NEW_HTML_ELEMENT(Track)
1428 NS_DECLARE_NS_NEW_HTML_ELEMENT(Unknown)
1429 NS_DECLARE_NS_NEW_HTML_ELEMENT(Video)
1431 #endif /* nsGenericHTMLElement_h___ */