Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / base / Element.h
blob8deca8d1c7317385fe0c88385bd08b7aeb347dbb
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 /*
8 * Base class for all element classes; this provides an implementation
9 * of DOM Core's Element, implements nsIContent, provides
10 * utility methods for subclasses, and so forth.
13 #ifndef mozilla_dom_Element_h__
14 #define mozilla_dom_Element_h__
16 #include <cstdio>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <utility>
20 #include "AttrArray.h"
21 #include "ErrorList.h"
22 #include "Units.h"
23 #include "js/RootingAPI.h"
24 #include "mozilla/AlreadyAddRefed.h"
25 #include "mozilla/Assertions.h"
26 #include "mozilla/Attributes.h"
27 #include "mozilla/BasicEvents.h"
28 #include "mozilla/CORSMode.h"
29 #include "mozilla/FlushType.h"
30 #include "mozilla/Maybe.h"
31 #include "mozilla/PseudoStyleType.h"
32 #include "mozilla/RefPtr.h"
33 #include "mozilla/Result.h"
34 #include "mozilla/RustCell.h"
35 #include "mozilla/UniquePtr.h"
36 #include "mozilla/dom/BorrowedAttrInfo.h"
37 #include "mozilla/dom/DOMString.h"
38 #include "mozilla/dom/DOMTokenListSupportedTokens.h"
39 #include "mozilla/dom/DirectionalityUtils.h"
40 #include "mozilla/dom/FragmentOrElement.h"
41 #include "mozilla/dom/NameSpaceConstants.h"
42 #include "mozilla/dom/NodeInfo.h"
43 #include "mozilla/dom/RustTypes.h"
44 #include "mozilla/dom/ShadowRootBinding.h"
45 #include "nsAtom.h"
46 #include "nsAttrValue.h"
47 #include "nsAttrValueInlines.h"
48 #include "nsCaseTreatment.h"
49 #include "nsChangeHint.h"
50 #include "nsTHashMap.h"
51 #include "nsDebug.h"
52 #include "nsError.h"
53 #include "nsGkAtoms.h"
54 #include "nsHashKeys.h"
55 #include "nsIContent.h"
56 #include "nsID.h"
57 #include "nsINode.h"
58 #include "nsLiteralString.h"
59 #include "nsRect.h"
60 #include "nsString.h"
61 #include "nsStringFlags.h"
62 #include "nsTLiteralString.h"
63 #include "nscore.h"
65 class JSObject;
66 class mozAutoDocUpdate;
67 class nsAttrName;
68 class nsAttrValueOrString;
69 class nsContentList;
70 class nsDOMAttributeMap;
71 class nsDOMCSSAttributeDeclaration;
72 class nsDOMStringMap;
73 class nsDOMTokenList;
74 class nsFocusManager;
75 class nsGenericHTMLFormControlElementWithState;
76 class nsGlobalWindowInner;
77 class nsGlobalWindowOuter;
78 class nsImageLoadingContent;
79 class nsIAutoCompletePopup;
80 class nsIBrowser;
81 class nsIDOMXULButtonElement;
82 class nsIDOMXULContainerElement;
83 class nsIDOMXULContainerItemElement;
84 class nsIDOMXULControlElement;
85 class nsIDOMXULMenuListElement;
86 class nsIDOMXULMultiSelectControlElement;
87 class nsIDOMXULRadioGroupElement;
88 class nsIDOMXULRelatedElement;
89 class nsIDOMXULSelectControlElement;
90 class nsIDOMXULSelectControlItemElement;
91 class nsIFrame;
92 class nsIHTMLCollection;
93 class nsIPrincipal;
94 class nsIScreen;
95 class nsIURI;
96 class nsObjectLoadingContent;
97 class nsPresContext;
98 class nsWindowSizes;
99 struct JSContext;
100 struct ServoNodeData;
101 template <class E>
102 class nsTArray;
103 template <class T>
104 class nsGetterAddRefs;
106 namespace mozilla {
107 class DeclarationBlock;
108 class MappedDeclarationsBuilder;
109 class EditorBase;
110 class ErrorResult;
111 class OOMReporter;
112 class ScrollContainerFrame;
113 class SMILAttr;
114 struct MutationClosureData;
115 class TextEditor;
116 namespace css {
117 struct URLValue;
118 } // namespace css
119 namespace dom {
120 struct CheckVisibilityOptions;
121 struct CustomElementData;
122 struct SetHTMLOptions;
123 struct GetHTMLOptions;
124 struct GetAnimationsOptions;
125 struct ScrollIntoViewOptions;
126 struct ScrollToOptions;
127 struct FocusOptions;
128 struct ShadowRootInit;
129 struct ScrollOptions;
130 class Attr;
131 class BooleanOrScrollIntoViewOptions;
132 class Document;
133 class HTMLFormElement;
134 class DOMIntersectionObserver;
135 class DOMMatrixReadOnly;
136 class Element;
137 class ElementOrCSSPseudoElement;
138 class PopoverData;
139 class Promise;
140 class Sanitizer;
141 class ShadowRoot;
142 class TrustedHTMLOrString;
143 class UnrestrictedDoubleOrKeyframeAnimationOptions;
144 template <typename T>
145 class Optional;
146 enum class CallerType : uint32_t;
147 enum class ReferrerPolicy : uint8_t;
148 } // namespace dom
149 } // namespace mozilla
151 using nsMapRuleToAttributesFunc = void (*)(mozilla::MappedDeclarationsBuilder&);
153 // Declared here because of include hell.
154 extern "C" bool Servo_Element_IsDisplayContents(const mozilla::dom::Element*);
156 already_AddRefed<nsContentList> NS_GetContentList(nsINode* aRootNode,
157 int32_t aMatchNameSpaceId,
158 const nsAString& aTagname);
160 #define ELEMENT_FLAG_BIT(n_) \
161 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
163 // Element-specific flags
164 enum : uint32_t {
165 // Whether this node has dirty descendants for Servo's style system.
166 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(0),
167 // Whether this node has dirty descendants for animation-only restyle for
168 // Servo's style system.
169 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(1),
171 // Whether the element has been snapshotted due to attribute or state changes
172 // by the Servo restyle manager.
173 ELEMENT_HAS_SNAPSHOT = ELEMENT_FLAG_BIT(2),
175 // Whether the element has already handled its relevant snapshot.
177 // Used by the servo restyle process in order to accurately track whether the
178 // style of an element is up-to-date, even during the same restyle process.
179 ELEMENT_HANDLED_SNAPSHOT = ELEMENT_FLAG_BIT(3),
181 // If this flag is set on an element, that means that it is a HTML datalist
182 // element or has a HTML datalist element ancestor.
183 ELEMENT_IS_DATALIST_OR_HAS_DATALIST_ANCESTOR = ELEMENT_FLAG_BIT(4),
185 // If this flag is set on an element, that means this element
186 // has been considered by our LargestContentfulPaint algorithm and
187 // it's not going to be considered again.
188 ELEMENT_PROCESSED_BY_LCP_FOR_TEXT = ELEMENT_FLAG_BIT(5),
190 // If this flag is set on an element, this means the HTML parser encountered
191 // a duplicate attribute error:
192 // https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute
193 // This flag is used for detecting dangling markup attacks in the CSP
194 // algorithm https://w3c.github.io/webappsec-csp/#is-element-nonceable.
195 ELEMENT_PARSER_HAD_DUPLICATE_ATTR_ERROR = ELEMENT_FLAG_BIT(6),
197 // If this flag is set, this element is in
198 // Document::mContentIdentifiersForLCP.
199 ELEMENT_IN_CONTENT_IDENTIFIER_FOR_LCP = ELEMENT_FLAG_BIT(7),
201 // Remaining bits are for subclasses
202 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET = NODE_TYPE_SPECIFIC_BITS_OFFSET + 8
205 #undef ELEMENT_FLAG_BIT
207 // Make sure we have space for our bits
208 ASSERT_NODE_FLAGS_SPACE(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
210 namespace mozilla {
211 enum class PseudoStyleType : uint8_t;
212 class EventChainPostVisitor;
213 class EventChainPreVisitor;
214 class EventChainVisitor;
215 class EventListenerManager;
216 class EventStateManager;
218 enum class ContentEditableState {
219 Inherit,
220 False,
221 True,
222 PlainTextOnly,
225 namespace dom {
227 struct CustomElementDefinition;
228 class Animation;
229 class CustomElementRegistry;
230 class Link;
231 class DOMRect;
232 class DOMRectList;
233 class Flex;
234 class Grid;
235 class OwningTrustedHTMLOrNullIsEmptyString;
236 class TrustedHTML;
237 class TrustedHTMLOrNullIsEmptyString;
238 class TrustedHTMLOrTrustedScriptOrTrustedScriptURLOrString;
240 // IID for the dom::Element interface
241 #define NS_ELEMENT_IID \
242 {0xc67ed254, 0xfd3b, 0x4b10, {0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1}}
244 #define REFLECT_NULLABLE_DOMSTRING_ATTR(method, attr) \
245 void Get##method(nsAString& aValue) const { \
246 const nsAttrValue* val = mAttrs.GetAttr(nsGkAtoms::attr); \
247 if (!val) { \
248 SetDOMStringToNull(aValue); \
249 return; \
251 val->ToString(aValue); \
253 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
254 SetOrRemoveNullableStringAttr(nsGkAtoms::attr, aValue, aRv); \
257 #define REFLECT_NULLABLE_ELEMENT_ATTR(method, attr) \
258 Element* Get##method() const { \
259 return GetAttrAssociatedElement(nsGkAtoms::attr); \
262 void Set##method(Element* aElement) { \
263 ExplicitlySetAttrElement(nsGkAtoms::attr, aElement); \
266 #define REFLECT_NULLABLE_ELEMENTS_ATTR(method, attr) \
267 void Get##method(bool* aUseCachedValue, \
268 Nullable<nsTArray<RefPtr<Element>>>& aElements) { \
269 GetAttrAssociatedElements(nsGkAtoms::attr, aUseCachedValue, aElements); \
272 void Set##method( \
273 const Nullable<Sequence<OwningNonNull<Element>>>& aElements) { \
274 ExplicitlySetAttrElements(nsGkAtoms::attr, aElements); \
277 // TODO(keithamus): Reference the spec link once merged.
278 // https://github.com/whatwg/html/pull/9841/files#diff-41cf6794ba4200b839c53531555f0f3998df4cbb01a4d5cb0b94e3ca5e23947dR86024
279 enum class InvokeAction : uint8_t {
280 Invalid,
281 Custom,
282 Auto,
283 TogglePopover,
284 ShowPopover,
285 HidePopover,
286 ShowModal,
287 Toggle,
288 Close,
289 Open,
292 class Element : public FragmentOrElement {
293 public:
294 #ifdef MOZILLA_INTERNAL_API
295 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
296 : FragmentOrElement(std::move(aNodeInfo)),
297 mState(ElementState::READONLY | ElementState::DEFINED |
298 ElementState::LTR) {
299 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
300 "Bad NodeType in aNodeInfo");
301 SetIsElement();
304 ~Element() {
305 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
308 #endif // MOZILLA_INTERNAL_API
310 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
312 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
314 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
316 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
319 * Method to get the full state of this element. See dom/base/rust/lib.rs for
320 * the possible bits that could be set here.
322 ElementState State() const { return mState; }
325 * Returns the current disabled state of the element.
327 bool IsDisabled() const { return State().HasState(ElementState::DISABLED); }
328 bool IsReadOnly() const { return State().HasState(ElementState::READONLY); }
329 bool IsDisabledOrReadOnly() const {
330 return State().HasAtLeastOneOfStates(ElementState::DISABLED |
331 ElementState::READONLY);
335 * Return true if this element has contenteditable="plaintext-only".
337 [[nodiscard]] inline bool IsContentEditablePlainTextOnly() const;
339 virtual int32_t TabIndexDefault() { return -1; }
342 * Get tabIndex of this element. If not found, return TabIndexDefault.
344 int32_t TabIndex();
347 * Get the parsed value of tabindex attribute.
349 Maybe<int32_t> GetTabIndexAttrValue();
352 * Set tabIndex value to this element.
354 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
357 * Sets the ShadowRoot binding for this element. The contents of the
358 * binding is rendered in place of this node's children.
360 * @param aShadowRoot The ShadowRoot to be bound to this element.
362 void SetShadowRoot(ShadowRoot* aShadowRoot);
364 void SetLastRememberedBSize(float aBSize);
365 void SetLastRememberedISize(float aISize);
366 void RemoveLastRememberedBSize();
367 void RemoveLastRememberedISize();
370 * Make focus on this element.
372 // TODO: Convert Focus() to MOZ_CAN_RUN_SCRIPT and get rid of the
373 // kungFuDeathGrip in it.
374 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Focus(const FocusOptions& aOptions,
375 const CallerType aCallerType,
376 ErrorResult& aError);
379 * Show blur and clear focus.
381 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Blur(mozilla::ErrorResult& aError);
384 * The style state of this element. This is the real state of the element
385 * with any style locks applied for pseudo-class inspecting.
387 ElementState StyleState() const {
388 if (!HasLockedStyleStates()) {
389 return mState;
391 return StyleStateFromLocks();
395 * StyleStateLocks is used to specify which event states should be locked,
396 * and whether they should be locked to on or off.
398 struct StyleStateLocks {
399 // mLocks tracks which event states should be locked.
400 ElementState mLocks;
401 // mValues tracks if the locked state should be on or off.
402 ElementState mValues;
406 * The style state locks applied to this element.
408 StyleStateLocks LockedStyleStates() const;
411 * Add a style state lock on this element.
412 * aEnabled is the value to lock the given state bits to.
414 void LockStyleStates(ElementState aStates, bool aEnabled);
417 * Remove a style state lock on this element.
419 void UnlockStyleStates(ElementState aStates);
422 * Clear all style state locks on this element.
424 void ClearStyleStateLocks();
427 * Accessors for the state of our dir attribute.
429 bool HasDirAuto() const {
430 return State().HasState(ElementState::HAS_DIR_ATTR_LIKE_AUTO);
434 * Elements with dir="rtl" or dir="ltr".
436 bool HasFixedDir() const {
437 return State().HasAtLeastOneOfStates(ElementState::HAS_DIR_ATTR_LTR |
438 ElementState::HAS_DIR_ATTR_RTL);
442 * Get the inline style declaration, if any, for this element.
444 DeclarationBlock* GetInlineStyleDeclaration() const;
447 * Get the mapped attributes, if any, for this element.
449 StyleLockedDeclarationBlock* GetMappedAttributeStyle() const {
450 return mAttrs.GetMappedDeclarationBlock();
453 bool IsPendingMappedAttributeEvaluation() const {
454 return mAttrs.IsPendingMappedAttributeEvaluation();
457 void SetMappedDeclarationBlock(already_AddRefed<StyleLockedDeclarationBlock>);
460 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
461 * so that the element implementation can access the old style attribute
462 * value.
464 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
467 * Set the inline style declaration for this element.
469 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
470 MutationClosureData& aData);
473 * Get the SMIL override style declaration for this element. If the
474 * rule hasn't been created, this method simply returns null.
476 DeclarationBlock* GetSMILOverrideStyleDeclaration();
479 * Set the SMIL override style declaration for this element. This method will
480 * notify the document's pres context, so that the style changes will be
481 * noticed.
483 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
486 * Returns a new SMILAttr that allows the caller to animate the given
487 * attribute on this element.
489 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
490 nsAtom* aName);
493 * Get the SMIL override style for this element. This is a style declaration
494 * that is applied *after* the inline style, and it can be used e.g. to store
495 * animated style values.
497 * Note: This method is analogous to the 'GetStyle' method in
498 * nsGenericHTMLElement and nsStyledElement.
500 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
503 * Returns if the element is labelable as per HTML specification.
505 virtual bool IsLabelable() const;
508 * Returns if the element is interactive content as per HTML specification.
510 virtual bool IsInteractiveHTMLContent() const;
513 * Is the attribute named aAttribute a mapped attribute?
515 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
517 nsresult BindToTree(BindContext&, nsINode& aParent) override;
518 void UnbindFromTree(UnbindContext&) override;
519 using nsIContent::UnbindFromTree;
521 virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
522 static void MapNoAttributesInto(mozilla::MappedDeclarationsBuilder&);
525 * Get a hint that tells the style system what to do when
526 * an attribute on this node changes, if something needs to happen
527 * in response to the change *other* than the result of what is
528 * mapped into style data via any type of style rule.
530 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
531 int32_t aModType) const;
533 inline Directionality GetDirectionality() const {
534 ElementState state = State();
535 if (state.HasState(ElementState::RTL)) {
536 return Directionality::Rtl;
538 if (state.HasState(ElementState::LTR)) {
539 return Directionality::Ltr;
541 return Directionality::Unset;
544 inline void SetDirectionality(Directionality aDir, bool aNotify) {
545 AutoStateChangeNotifier notifier(*this, aNotify);
546 RemoveStatesSilently(ElementState::DIR_STATES);
547 switch (aDir) {
548 case Directionality::Rtl:
549 AddStatesSilently(ElementState::RTL);
550 break;
551 case Directionality::Ltr:
552 AddStatesSilently(ElementState::LTR);
553 break;
554 case Directionality::Unset:
555 case Directionality::Auto:
556 MOZ_ASSERT_UNREACHABLE("Setting unresolved directionality?");
557 break;
561 Directionality GetComputedDirectionality() const;
563 static const uint32_t kAllServoDescendantBits =
564 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO |
565 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO |
566 NODE_DESCENDANTS_NEED_FRAMES;
569 * Notes that something in the given subtree of this element needs dirtying,
570 * and that all the relevant dirty bits have already been propagated up to the
571 * element.
573 * This is important because `NoteDirtyForServo` uses the dirty bits to reason
574 * about the shape of the tree, so we can't just call into there.
576 void NoteDirtySubtreeForServo();
578 void NoteDirtyForServo();
579 void NoteAnimationOnlyDirtyForServo();
580 void NoteDescendantsNeedFramesForServo();
582 bool HasDirtyDescendantsForServo() const {
583 return HasFlag(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
586 void SetHasDirtyDescendantsForServo() {
587 SetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
590 void UnsetHasDirtyDescendantsForServo() {
591 UnsetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
594 bool HasAnimationOnlyDirtyDescendantsForServo() const {
595 return HasFlag(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
598 void SetHasAnimationOnlyDirtyDescendantsForServo() {
599 SetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
602 void UnsetHasAnimationOnlyDirtyDescendantsForServo() {
603 UnsetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
606 bool HasServoData() const { return !!mServoData.Get(); }
608 void ClearServoData() { ClearServoData(GetComposedDoc()); }
609 void ClearServoData(Document* aDocument);
611 PopoverData* GetPopoverData() const {
612 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
613 return slots ? slots->mPopoverData.get() : nullptr;
616 PopoverData& EnsurePopoverData() {
617 if (auto* popoverData = GetPopoverData()) {
618 return *popoverData;
620 return CreatePopoverData();
623 bool IsAutoPopover() const;
624 bool IsPopoverOpen() const;
627 * https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
629 Element* GetTopmostPopoverAncestor(const Element* aInvoker,
630 bool isPopover) const;
632 ElementAnimationData* GetAnimationData() const {
633 if (!MayHaveAnimations()) {
634 return nullptr;
636 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
637 return slots ? slots->mAnimations.get() : nullptr;
640 ElementAnimationData& EnsureAnimationData() {
641 if (auto* anim = GetAnimationData()) {
642 return *anim;
644 return CreateAnimationData();
647 private:
648 ElementAnimationData& CreateAnimationData();
649 PopoverData& CreatePopoverData();
651 public:
652 void ClearPopoverData();
655 * Gets the custom element data used by web components custom element.
656 * Custom element data is created at the first attempt to enqueue a callback.
658 * @return The custom element data or null if none.
660 CustomElementData* GetCustomElementData() const {
661 if (!HasCustomElementData()) {
662 return nullptr;
665 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
666 return slots ? slots->mCustomElementData.get() : nullptr;
670 * Sets the custom element data, ownership of the
671 * callback data is taken by this element.
673 * @param aData The custom element data.
675 void SetCustomElementData(UniquePtr<CustomElementData> aData);
677 nsTArray<RefPtr<nsAtom>>& EnsureCustomStates();
680 * Gets the custom element definition used by web components custom element.
682 * @return The custom element definition or null if element is not a custom
683 * element or custom element is not defined yet.
685 CustomElementDefinition* GetCustomElementDefinition() const;
688 * Sets the custom element definition, called when custom element is created
689 * or upgraded.
691 * @param aDefinition The custom element definition.
693 virtual void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
695 const AttrArray& GetAttrs() const { return mAttrs; }
697 void SetDefined(bool aSet) { SetStates(ElementState::DEFINED, aSet); }
699 // AccessibilityRole
700 REFLECT_NULLABLE_DOMSTRING_ATTR(Role, role)
702 // AriaAttributes
703 REFLECT_NULLABLE_ELEMENT_ATTR(AriaActiveDescendantElement,
704 aria_activedescendant)
705 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
706 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
707 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBrailleLabel, aria_braillelabel)
708 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBrailleRoleDescription,
709 aria_brailleroledescription)
710 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBusy, aria_busy)
711 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaChecked, aria_checked)
712 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColCount, aria_colcount)
713 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
714 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
715 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
716 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaControlsElements, aria_controls)
717 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaCurrent, aria_current)
718 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaDescribedByElements, aria_describedby)
719 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaDescription, aria_description)
720 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaDetailsElements, aria_details)
721 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
722 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaErrorMessageElements, aria_errormessage)
723 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
724 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaFlowToElements, aria_flowto)
725 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
726 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaHidden, aria_hidden)
727 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
728 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
729 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLabel, aria_label)
730 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaLabelledByElements, aria_labelledby)
731 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLevel, aria_level)
732 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLive, aria_live)
733 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaModal, aria_modal)
734 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
735 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
736 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
737 REFLECT_NULLABLE_ELEMENTS_ATTR(AriaOwnsElements, aria_owns)
738 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
739 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
740 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPressed, aria_pressed)
741 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
742 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
743 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRequired, aria_required)
744 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
745 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
746 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
747 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
748 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
749 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSelected, aria_selected)
750 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
751 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSort, aria_sort)
752 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
753 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
754 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
755 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
757 protected:
758 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
759 ErrorResult& aError);
761 public:
762 MOZ_CAN_RUN_SCRIPT
763 ScrollContainerFrame* GetScrollContainerFrame(
764 nsIFrame** aFrame = nullptr, FlushType aFlushType = FlushType::Layout);
766 private:
767 // Style state computed from element's state and style locks.
768 ElementState StyleStateFromLocks() const;
770 void NotifyStateChange(ElementState aStates);
771 void NotifyStyleStateChange(ElementState aStates);
773 public:
774 struct AutoStateChangeNotifier {
775 AutoStateChangeNotifier(Element& aElement, bool aNotify)
776 : mElement(aElement), mOldState(aElement.State()), mNotify(aNotify) {}
777 ~AutoStateChangeNotifier() {
778 if (!mNotify) {
779 return;
781 ElementState newState = mElement.State();
782 if (mOldState != newState) {
783 mElement.NotifyStateChange(mOldState ^ newState);
787 private:
788 Element& mElement;
789 const ElementState mOldState;
790 const bool mNotify;
793 // Method to add state bits. This should be called from subclass constructors
794 // to set up our event state correctly at construction time, and other places
795 // where we don't want to notify a state change, or there's an
796 // AutoStateChangeNotifier on the stack.
797 void AddStatesSilently(ElementState aStates) { mState |= aStates; }
798 // Method to remove state bits. This should be called from subclass
799 // constructors to set up our event state correctly at construction time and
800 // other places where we don't want to notify a state change.
801 void RemoveStatesSilently(ElementState aStates) { mState &= ~aStates; }
802 // Methods to add state bits, potentially notifying. These will handle setting
803 // up script blockers when they notify, so no need to do it in the callers
804 // unless desired. States passed here must only be those in
805 // EXTERNALLY_MANAGED_STATES.
806 void AddStates(ElementState aStates, bool aNotify = true) {
807 ElementState old = mState;
808 AddStatesSilently(aStates);
809 if (aNotify && old != mState) {
810 NotifyStateChange(old ^ mState);
813 void RemoveStates(ElementState aStates, bool aNotify = true) {
814 ElementState old = mState;
815 RemoveStatesSilently(aStates);
816 if (aNotify && old != mState) {
817 NotifyStateChange(old ^ mState);
820 void SetStates(ElementState aStates, bool aSet, bool aNotify = true) {
821 if (aSet) {
822 AddStates(aStates, aNotify);
823 } else {
824 RemoveStates(aStates, aNotify);
827 void ToggleStates(ElementState aStates, bool aNotify) {
828 mState ^= aStates;
829 if (aNotify) {
830 NotifyStateChange(aStates);
834 void UpdateEditableState(bool aNotify) override;
835 // Makes sure that the READONLY/READWRITE flags are in sync.
836 void UpdateReadOnlyState(bool aNotify);
837 // Form controls and non-form controls should have different :read-only /
838 // :read-write behavior. This is what effectively controls it.
839 virtual bool IsReadOnlyInternal() const;
842 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
843 * with that name exists. This method is intended for character case
844 * conversion if the content object is case insensitive (e.g. HTML). Returns
845 * the nodeinfo of the attribute with the specified name if one exists or
846 * null otherwise.
848 * @param aStr the unparsed attribute string
849 * @return the node info. May be nullptr.
851 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
852 const nsAString& aStr) const;
855 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
856 * is true or there are mutation listeners that must be triggered, the
857 * attribute is currently set, and the new value that is about to be set is
858 * different to the current value. As a perf optimization the new and old
859 * values will not actually be compared if we aren't notifying and we don't
860 * have mutation listeners (in which case it's cheap to just return false
861 * and let the caller go ahead and set the value).
862 * @param aOldValue [out] Set to the old value of the attribute, but only if
863 * there are event listeners. If set, the type of aOldValue will be either
864 * nsAttrValue::eString or nsAttrValue::eAtom.
865 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
866 * MutationEvent_Binding::ADDITION, but only if this helper returns true
867 * @param aHasListeners [out] Set to true if there are mutation event
868 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
869 * @param aOldValueSet [out] Indicates whether an old attribute value has been
870 * stored in aOldValue. The bool will be set to true if a value was stored.
872 bool MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
873 const nsAtom* aPrefix,
874 const nsAttrValueOrString& aValue, bool aNotify,
875 nsAttrValue& aOldValue, uint8_t* aModType,
876 bool* aHasListeners, bool* aOldValueSet);
879 * Notifies mutation listeners if aNotify is true, there are mutation
880 * listeners, and the attribute value is changing.
882 * @param aNamespaceID The namespace of the attribute
883 * @param aName The local name of the attribute
884 * @param aPrefix The prefix of the attribute
885 * @param aValue The value that the attribute is being changed to
886 * @param aNotify If true, mutation listeners will be notified if they exist
887 * and the attribute value is changing
888 * @param aOldValue [out] Set to the old value of the attribute, but only if
889 * there are event listeners. If set, the type of aOldValue will be either
890 * nsAttrValue::eString or nsAttrValue::eAtom.
891 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
892 * MutationEvent_Binding::ADDITION, but only if this helper returns true
893 * @param aHasListeners [out] Set to true if there are mutation event
894 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
895 * @param aOldValueSet [out] Indicates whether an old attribute value has been
896 * stored in aOldValue. The bool will be set to true if a value was stored.
898 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
899 nsAtom* aPrefix,
900 const nsAttrValueOrString& aValue, bool aNotify,
901 nsAttrValue& aOldValue, uint8_t* aModType,
902 bool* aHasListeners, bool* aOldValueSet);
905 * Sets the class attribute.
906 * Assumes that we are not notifying and that the attribute hasn't been
907 * set previously.
909 nsresult SetClassAttrFromParser(nsAtom* aValue);
911 // aParsedValue receives the old value of the attribute. That's useful if
912 // either the input or output value of aParsedValue is StoresOwnData.
913 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
914 nsAttrValue& aParsedValue, bool aNotify);
916 * Get the current value of the attribute. This returns a form that is
917 * suitable for passing back into SetAttr.
919 * @param aNameSpaceID the namespace of the attr (defaults to
920 kNameSpaceID_None in the overload that omits this arg)
921 * @param aName the name of the attr
922 * @param aResult the value (may legitimately be the empty string) [OUT]
923 * @returns true if the attribute was set (even when set to empty string)
924 * false when not set.
925 * GetAttr is not inlined on purpose, to keep down codesize from all the
926 * inlined nsAttrValue bits for C++ callers.
928 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
929 nsAString& aResult) const;
930 bool GetAttr(const nsAtom* aName, nsAString& aResult) const;
933 * Determine if an attribute has been set (empty string or otherwise).
935 * @param aNameSpaceId the namespace id of the attribute (defaults to
936 kNameSpaceID_None in the overload that omits this arg)
937 * @param aAttr the attribute name
938 * @return whether an attribute exists
940 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
941 return mAttrs.HasAttr(aNameSpaceID, aName);
944 bool HasAttr(const nsAtom* aAttr) const { return mAttrs.HasAttr(aAttr); }
947 * Determine if an attribute has been set to a non-empty string value. If the
948 * attribute is not set at all, this will return false.
950 * @param aNameSpaceId the namespace id of the attribute (defaults to
951 * kNameSpaceID_None in the overload that omits this arg)
952 * @param aAttr the attribute name
954 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
956 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
957 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
961 * Test whether this Element's given attribute has the given value. If the
962 * attribute is not set at all, this will return false.
964 * @param aNameSpaceID The namespace ID of the attribute. Must not
965 * be kNameSpaceID_Unknown.
966 * @param aName The name atom of the attribute. Must not be null.
967 * @param aValue The value to compare to.
968 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
970 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
971 const nsAString& aValue,
972 nsCaseTreatment aCaseSensitive) const;
975 * Test whether this Element's given attribute has the given value. If the
976 * attribute is not set at all, this will return false.
978 * @param aNameSpaceID The namespace ID of the attribute. Must not
979 * be kNameSpaceID_Unknown.
980 * @param aName The name atom of the attribute. Must not be null.
981 * @param aValue The value to compare to. Must not be null.
982 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
984 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
985 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
988 * Check whether this Element's given attribute has one of a given list of
989 * values. If there is a match, we return the index in the list of the first
990 * matching value. If there was no attribute at all, then we return
991 * ATTR_MISSING. If there was an attribute but it didn't match, we return
992 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
994 * @param aNameSpaceID The namespace ID of the attribute. Must not
995 * be kNameSpaceID_Unknown.
996 * @param aName The name atom of the attribute. Must not be null.
997 * @param aValues a nullptr-terminated array of pointers to atom values to
998 * test against.
999 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
1000 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
1001 * indicating the first value of aValues that matched
1003 using AttrValuesArray = AttrArray::AttrValuesArray;
1004 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
1005 AttrArray::AttrValuesArray* aValues,
1006 nsCaseTreatment aCaseSensitive) const;
1009 * Set attribute values. All attribute values are assumed to have a
1010 * canonical string representation that can be used for these
1011 * methods. The SetAttr method is assumed to perform a translation
1012 * of the canonical form into the underlying content specific
1013 * form.
1015 * @param aNameSpaceID the namespace of the attribute
1016 * @param aName the name of the attribute
1017 * @param aValue the value to set
1018 * @param aNotify specifies how whether or not the document should be
1019 * notified of the attribute change.
1021 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
1022 bool aNotify) {
1023 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
1025 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
1026 const nsAString& aValue, bool aNotify) {
1027 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
1029 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
1030 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
1031 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
1032 aNotify);
1036 * Set attribute values. All attribute values are assumed to have a
1037 * canonical String representation that can be used for these
1038 * methods. The SetAttr method is assumed to perform a translation
1039 * of the canonical form into the underlying content specific
1040 * form.
1042 * @param aNameSpaceID the namespace of the attribute
1043 * @param aName the name of the attribute
1044 * @param aPrefix the prefix of the attribute
1045 * @param aValue the value to set
1046 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1047 * responsible for setting the attribute, or null if no scripted caller can be
1048 * determined. A null value here does not guarantee that there is no
1049 * scripted caller, but a non-null value does guarantee that a scripted
1050 * caller with the given principal is directly responsible for the
1051 * attribute change.
1052 * @param aNotify specifies how whether or not the document should be
1053 * notified of the attribute change.
1055 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
1056 const nsAString& aValue,
1057 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
1060 * Remove an attribute so that it is no longer explicitly specified.
1062 * @param aNameSpaceID the namespace id of the attribute
1063 * @param aName the name of the attribute to unset
1064 * @param aNotify specifies whether or not the document should be
1065 * notified of the attribute change
1067 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aName, bool aNotify);
1070 * Get the namespace / name / prefix of a given attribute.
1072 * @param aIndex the index of the attribute name
1073 * @returns The name at the given index, or null if the index is
1074 * out-of-bounds.
1075 * @note The document returned by NodeInfo()->GetDocument() (if one is
1076 * present) is *not* necessarily the owner document of the element.
1077 * @note The pointer returned by this function is only valid until the
1078 * next call of either GetAttrNameAt or SetAttr on the element.
1080 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
1081 return mAttrs.GetSafeAttrNameAt(aIndex);
1085 * Same as above, but does not do out-of-bounds checks!
1087 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
1088 return mAttrs.AttrNameAt(aIndex);
1092 * Gets the attribute info (name and value) for this element at a given index.
1094 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
1095 if (aIndex >= mAttrs.AttrCount()) {
1096 return BorrowedAttrInfo(nullptr, nullptr);
1099 return mAttrs.AttrInfoAt(aIndex);
1103 * Get the number of all specified attributes.
1105 * @return the number of attributes
1107 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
1110 * Get the class list of this element (this corresponds to the value of the
1111 * class attribute). This may be null if there are no classes, but that's not
1112 * guaranteed (e.g. we could have class="").
1114 const nsAttrValue* GetClasses() const {
1115 if (!MayHaveClass()) {
1116 return nullptr;
1119 if (IsSVGElement()) {
1120 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
1121 return value;
1125 return GetParsedAttr(nsGkAtoms::_class);
1128 #ifdef MOZ_DOM_LIST
1129 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
1130 List(out, aIndent, ""_ns);
1132 virtual void DumpContent(FILE* out, int32_t aIndent,
1133 bool aDumpAll) const override;
1134 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1135 void ListAttributes(FILE* out) const;
1136 #endif
1139 * Append to aOutDescription a string describing the element and its
1140 * attributes.
1141 * If aShort is true, only the id and class attributes will be listed.
1143 void Describe(nsAString& aOutDescription, bool aShort = false) const;
1146 * Attribute Mapping Helpers
1148 struct MappedAttributeEntry {
1149 const nsStaticAtom* const attribute;
1153 * A common method where you can just pass in a list of maps to check
1154 * for attribute dependence. Most implementations of
1155 * IsAttributeMapped should use this function as a default
1156 * handler.
1158 template <size_t N>
1159 static bool FindAttributeDependence(
1160 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1161 return FindAttributeDependence(aAttribute, aMaps, N);
1164 virtual bool IsValidInvokeAction(InvokeAction aAction) const {
1165 return aAction == InvokeAction::Auto;
1169 * Elements can provide their own default behaviours for "Invoke" (see
1170 * invoketarget/invokeaction attributes).
1171 * If the action is not recognised, they can choose to ignore it and `return
1172 * false`. If an action is recognised then they should `return true` to
1173 * indicate to sub-classes that this has been handled and no further steps
1174 * should be run.
1176 MOZ_CAN_RUN_SCRIPT virtual bool HandleInvokeInternal(Element* invoker,
1177 InvokeAction aAction,
1178 ErrorResult& aRv) {
1179 return false;
1182 private:
1183 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1185 static bool FindAttributeDependence(const nsAtom* aAttribute,
1186 const MappedAttributeEntry* const aMaps[],
1187 uint32_t aMapCount);
1189 bool HasSharedRoot(const Element* aElement) const;
1191 Element* GetElementByIdInDocOrSubtree(nsAtom* aID) const;
1193 protected:
1194 inline bool GetAttr(const nsAtom* aName, DOMString& aResult) const {
1195 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1196 const nsAttrValue* val = mAttrs.GetAttr(aName);
1197 if (!val) {
1198 return false; // DOMString comes pre-emptied.
1200 val->ToString(aResult);
1201 return true;
1204 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1205 DOMString& aResult) const {
1206 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1207 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1208 if (!val) {
1209 return false; // DOMString comes pre-emptied.
1211 val->ToString(aResult);
1212 return true;
1215 public:
1216 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1218 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1219 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1220 const nsAttrValue* val = mAttrs.GetAttr(aName);
1221 if (val) {
1222 val->ToString(aResult);
1223 return true;
1225 // else DOMString comes pre-emptied.
1226 return false;
1229 // Note, this does not notify about the removal.
1230 void ClearAttributes() { mAttrs.Clear(); }
1232 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1233 void GetId(nsAString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1234 void GetId(DOMString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1235 void SetId(const nsAString& aId) {
1236 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1238 void GetClassName(nsAString& aClassName) {
1239 GetAttr(nsGkAtoms::_class, aClassName);
1241 void GetClassName(DOMString& aClassName) {
1242 GetAttr(nsGkAtoms::_class, aClassName);
1244 void SetClassName(const nsAString& aClassName) {
1245 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1248 nsDOMTokenList* ClassList();
1249 nsDOMTokenList* Part();
1251 nsDOMAttributeMap* Attributes();
1253 void GetAttributeNames(nsTArray<nsString>& aResult);
1255 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1256 DOMString str;
1257 GetAttribute(aName, str);
1258 str.ToString(aReturn);
1261 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1262 void GetAttributeNS(const nsAString& aNamespaceURI,
1263 const nsAString& aLocalName, nsAString& aReturn);
1264 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1265 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1266 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1267 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1268 void SetAttributeNS(const nsAString& aNamespaceURI,
1269 const nsAString& aLocalName, const nsAString& aValue,
1270 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1271 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1272 ErrorResult& aError) {
1273 SetAttribute(aName, aValue, nullptr, aError);
1276 MOZ_CAN_RUN_SCRIPT void SetAttribute(
1277 const nsAString& aName,
1278 const TrustedHTMLOrTrustedScriptOrTrustedScriptURLOrString& aValue,
1279 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1280 MOZ_CAN_RUN_SCRIPT void SetAttributeNS(
1281 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1282 const TrustedHTMLOrTrustedScriptOrTrustedScriptURLOrString& aValue,
1283 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1284 MOZ_CAN_RUN_SCRIPT void SetAttribute(
1285 const nsAString& aName,
1286 const TrustedHTMLOrTrustedScriptOrTrustedScriptURLOrString& aValue,
1287 ErrorResult& aError) {
1288 SetAttribute(aName, aValue, nullptr, aError);
1292 * This method creates a principal that subsumes this element's NodePrincipal
1293 * and which has flags set for elevated permissions that devtools needs to
1294 * operate on this element. The principal returned by this method is used by
1295 * various devtools methods to permit otherwise blocked operations, without
1296 * changing any other restrictions the NodePrincipal might have.
1298 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1299 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1300 ErrorResult& aError);
1301 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1302 const nsAString& aLocalName,
1303 const nsAString& aValue, ErrorResult& aError);
1305 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1306 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1307 const nsAString& aLocalName, ErrorResult& aError);
1308 bool HasAttribute(const nsAString& aName) const {
1309 return InternalGetAttrNameFromQName(aName) != nullptr;
1311 bool HasAttributeNS(const nsAString& aNamespaceURI,
1312 const nsAString& aLocalName) const;
1313 bool HasAttributes() const { return HasAttrs(); }
1314 Element* Closest(const nsACString& aSelector, ErrorResult& aResult);
1315 bool Matches(const nsACString& aSelector, ErrorResult& aError);
1316 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1317 const nsAString& aQualifiedName);
1318 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1319 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1320 ErrorResult& aError);
1321 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1322 const nsAString& aClassNames);
1325 * Returns attribute associated element for the given attribute name, see
1326 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#attr-associated-element
1328 Element* GetAttrAssociatedElement(nsAtom* aAttr) const;
1329 void GetAttrAssociatedElements(
1330 nsAtom* aAttr, bool* aUseCachedValue,
1331 Nullable<nsTArray<RefPtr<Element>>>& aElements);
1334 * Sets an attribute element for the given attribute.
1335 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
1337 void ExplicitlySetAttrElement(nsAtom* aAttr, Element* aElement);
1338 void ExplicitlySetAttrElements(
1339 nsAtom* aAttr,
1340 const Nullable<Sequence<OwningNonNull<Element>>>& aElements);
1342 void ClearExplicitlySetAttrElement(nsAtom*);
1343 void ClearExplicitlySetAttrElements(nsAtom*);
1346 * Gets the attribute element for the given attribute.
1347 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
1348 * Unlike GetAttrAssociatedElement, this returns the target even if it isn't
1349 * a descendant of any of this element's shadow-including ancestors. It also
1350 * doesn't attempt to retrieve an element using a string id set in the content
1351 * attribute.
1353 Element* GetExplicitlySetAttrElement(nsAtom* aAttr) const;
1356 * Gets the attribute elements for the given attribute. Unlike
1357 * GetAttrAssociatedElements, this returns an uncached array of explicitly set
1358 * elements without checking if they are a descendant of any of this element's
1359 * shadow-including ancestors. It also does not attempt to retrieve elements
1360 * using the ids set in the content attribute.
1362 void GetExplicitlySetAttrElements(nsAtom* aAttr,
1363 nsTArray<Element*>& aElements) const;
1365 PseudoStyleType GetPseudoElementType() const {
1366 nsresult rv = NS_OK;
1367 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1368 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1369 return PseudoStyleType::NotPseudo;
1371 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1374 void SetPseudoElementType(PseudoStyleType aPseudo) {
1375 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1376 "Need to be able to store this in a void*");
1377 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1378 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1382 * Return an array of all elements in the subtree rooted at this
1383 * element that have grid container frames. This does not include
1384 * pseudo-elements.
1386 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1389 * Provide a direct way to determine if this Element has visible
1390 * scrollbars. Flushes layout.
1392 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1395 * Get an editor which handles user inputs when this element has focus.
1396 * If this is a text control, return a TextEditor if it's already created and
1397 * it's not in the design mode.
1398 * If this is editable, return HTMLEditor which should've already been
1399 * created.
1400 * Otherwise, return nullptr.
1402 EditorBase* GetExtantEditor() const;
1404 private:
1406 * Implement the algorithm specified at
1407 * https://dom.spec.whatwg.org/#insert-adjacent for both
1408 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1410 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1411 ErrorResult& aError);
1413 public:
1414 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1415 ErrorResult& aError);
1417 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1418 ErrorResult& aError);
1420 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError);
1421 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError);
1422 bool HasPointerCapture(long aPointerId);
1423 void SetCapture(bool aRetargetToElement);
1425 void SetCaptureAlways(bool aRetargetToElement);
1427 void ReleaseCapture();
1429 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1430 void RequestPointerLock(CallerType aCallerType);
1431 Attr* GetAttributeNode(const nsAString& aName);
1432 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1433 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1434 ErrorResult& aError);
1435 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1436 const nsAString& aLocalName);
1437 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1438 ErrorResult& aError);
1440 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1441 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1443 enum class Loading : uint8_t {
1444 Eager,
1445 Lazy,
1448 Loading LoadingState() const;
1449 void GetLoading(nsAString& aValue) const;
1450 bool ParseLoadingAttribute(const nsAString& aValue, nsAttrValue& aResult);
1452 // https://html.spec.whatwg.org/#potentially-render-blocking
1453 virtual bool IsPotentiallyRenderBlocking() { return false; }
1454 bool BlockingContainsRender() const;
1456 // Shadow DOM v1
1457 enum class ShadowRootDeclarative : bool { No, Yes };
1459 MOZ_CAN_RUN_SCRIPT_BOUNDARY
1460 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1461 ErrorResult& aError);
1462 bool CanAttachShadowDOM() const;
1464 enum class DelegatesFocus : bool { No, Yes };
1465 enum class ShadowRootClonable : bool { No, Yes };
1466 enum class ShadowRootSerializable : bool { No, Yes };
1468 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1469 ShadowRootMode aMode, DelegatesFocus = DelegatesFocus::No,
1470 SlotAssignmentMode aSlotAssignmentMode = SlotAssignmentMode::Named,
1471 ShadowRootClonable aClonable = ShadowRootClonable::No,
1472 ShadowRootSerializable aSerializable = ShadowRootSerializable::No);
1474 // Attach UA Shadow Root if it is not attached.
1475 enum class NotifyUAWidgetSetup : bool { No, Yes };
1476 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes,
1477 DelegatesFocus = DelegatesFocus::No);
1479 // Dispatch an event to UAWidgetsChild, triggering construction
1480 // or onchange callback on the existing widget.
1481 void NotifyUAWidgetSetupOrChange();
1483 enum class UnattachShadowRoot {
1485 Yes,
1488 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1489 // and optionally remove the shadow root.
1490 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1492 void UnattachShadow();
1494 ShadowRoot* GetShadowRootByMode() const;
1495 void SetSlot(const nsAString& aName, ErrorResult& aError);
1496 void GetSlot(nsAString& aName);
1498 ShadowRoot* GetShadowRoot() const {
1499 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1500 return slots ? slots->mShadowRoot.get() : nullptr;
1503 const Maybe<float> GetLastRememberedBSize() const {
1504 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1505 return slots ? slots->mLastRememberedBSize : Nothing();
1507 const Maybe<float> GetLastRememberedISize() const {
1508 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1509 return slots ? slots->mLastRememberedISize : Nothing();
1511 bool HasLastRememberedBSize() const {
1512 return GetLastRememberedBSize().isSome();
1514 bool HasLastRememberedISize() const {
1515 return GetLastRememberedISize().isSome();
1518 const Maybe<ContentRelevancy> GetContentRelevancy() const {
1519 const auto* slots = GetExistingExtendedDOMSlots();
1520 return slots ? slots->mContentRelevancy : Nothing();
1522 void SetContentRelevancy(ContentRelevancy relevancy) {
1523 ExtendedDOMSlots()->mContentRelevancy = Some(relevancy);
1526 const Maybe<bool> GetVisibleForContentVisibility() const {
1527 const auto* slots = GetExistingExtendedDOMSlots();
1528 return slots ? slots->mVisibleForContentVisibility : Nothing();
1530 void SetVisibleForContentVisibility(bool visible) {
1531 ExtendedDOMSlots()->mVisibleForContentVisibility = Some(visible);
1534 void ClearContentRelevancy() {
1535 if (auto* slots = GetExistingExtendedDOMSlots()) {
1536 slots->mContentRelevancy.reset();
1537 slots->mVisibleForContentVisibility.reset();
1538 slots->mTemporarilyVisibleForScrolledIntoViewDescendant = false;
1542 bool TemporarilyVisibleForScrolledIntoViewDescendant() const {
1543 const auto* slots = GetExistingExtendedDOMSlots();
1544 return slots && slots->mTemporarilyVisibleForScrolledIntoViewDescendant;
1547 void SetTemporarilyVisibleForScrolledIntoViewDescendant(bool aVisible) {
1548 ExtendedDOMSlots()->mTemporarilyVisibleForScrolledIntoViewDescendant =
1549 aVisible;
1552 // https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
1553 MOZ_CAN_RUN_SCRIPT bool CheckVisibility(const CheckVisibilityOptions&);
1555 private:
1556 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1557 // called from JS. Use PresShell::ScrollContentIntoView instead.
1558 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1560 public:
1561 MOZ_CAN_RUN_SCRIPT
1562 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1563 // called from JS. Use PresShell::ScrollContentIntoView instead.
1564 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1565 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1566 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1567 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1568 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1569 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1570 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1571 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1572 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1573 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1574 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1575 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1576 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1577 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1579 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1580 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1582 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1583 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1585 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1586 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1589 MOZ_CAN_RUN_SCRIPT int32_t ScreenX();
1590 MOZ_CAN_RUN_SCRIPT int32_t ScreenY();
1591 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIScreen> GetScreen();
1593 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1594 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1595 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1596 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1598 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1599 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1602 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1603 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1606 MOZ_CAN_RUN_SCRIPT double CurrentCSSZoom();
1608 // This function will return the block size of first line box, no matter if
1609 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1610 // can't get a primary frame, we will return be zero.
1611 double FirstLineBoxBSize() const;
1613 already_AddRefed<Flex> GetAsFlexContainer();
1614 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1616 bool HasGridFragments();
1618 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1619 Element& aAncestor);
1620 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1621 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1623 already_AddRefed<Animation> Animate(
1624 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1625 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1626 ErrorResult& aError);
1628 MOZ_CAN_RUN_SCRIPT
1629 void GetAnimations(const GetAnimationsOptions& aOptions,
1630 nsTArray<RefPtr<Animation>>& aAnimations);
1632 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1633 nsTArray<RefPtr<Animation>>& aAnimations);
1635 void CloneAnimationsFrom(const Element& aOther);
1637 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1639 // https://html.spec.whatwg.org/#dom-parsing-and-serialization:dom-element-innerhtml
1640 // @param aInnerHTML will always be of type `NullIsEmptyString`.
1641 void GetInnerHTML(OwningTrustedHTMLOrNullIsEmptyString& aInnerHTML,
1642 OOMReporter& aError);
1644 // https://html.spec.whatwg.org/#dom-parsing-and-serialization:dom-element-innerhtml
1646 // May only run script if aInnerHTML is a string. If this behavior changes,
1647 // callees might need adjusting.
1648 MOZ_CAN_RUN_SCRIPT void SetInnerHTML(
1649 const TrustedHTMLOrNullIsEmptyString& aInnerHTML,
1650 nsIPrincipal* aSubjectPrincipal, ErrorResult& aError);
1652 // Call this method only with trusted, i.e. non-attacker-controlled, strings.
1653 virtual void SetInnerHTMLTrusted(const nsAString& aInnerHTML,
1654 nsIPrincipal* aSubjectPrincipal,
1655 ErrorResult& aError);
1657 // @param aOuterHTML will always be of type `NullIsEmptyString`.
1658 void GetOuterHTML(OwningTrustedHTMLOrNullIsEmptyString& aOuterHTML);
1660 MOZ_CAN_RUN_SCRIPT void SetOuterHTML(
1661 const TrustedHTMLOrNullIsEmptyString& aOuterHTML, ErrorResult& aError);
1663 MOZ_CAN_RUN_SCRIPT void InsertAdjacentHTML(
1664 const nsAString& aPosition,
1665 const TrustedHTMLOrString& aTrustedHTMLOrString, ErrorResult& aError);
1667 void SetHTML(const nsAString& aInnerHTML, const SetHTMLOptions& aOptions,
1668 ErrorResult& aError);
1669 void GetHTML(const GetHTMLOptions& aOptions, nsAString& aResult);
1671 //----------------------------------------
1674 * Add a script event listener with the given event handler name
1675 * (like onclick) and with the value as JS
1676 * @param aEventName the event listener name
1677 * @param aValue the JS to attach
1678 * @param aDefer indicates if deferred execution is allowed
1680 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1681 bool aDefer = true);
1684 * Do whatever needs to be done when the mouse leaves a link
1686 nsresult LeaveLink(nsPresContext* aPresContext);
1688 static bool ShouldBlur(nsIContent* aContent);
1691 * Method to create and dispatch a left-click event loosely based on
1692 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1693 * through the full dispatching of the presshell of the aPresContext; if it's
1694 * false the event will be dispatched only as a DOM event.
1695 * If aPresContext is nullptr, this does nothing.
1697 * @param aFlags Extra flags for the dispatching event. The true flags
1698 * will be respected.
1700 MOZ_CAN_RUN_SCRIPT
1701 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1702 WidgetInputEvent* aSourceEvent,
1703 nsIContent* aTarget, bool aFullDispatch,
1704 const EventFlags* aFlags,
1705 nsEventStatus* aStatus);
1708 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1709 * will be dispatched through the full dispatching of the presshell of the
1710 * aPresContext; if it's false the event will be dispatched only as a DOM
1711 * event.
1712 * If aPresContext is nullptr, this does nothing.
1714 using nsIContent::DispatchEvent;
1715 MOZ_CAN_RUN_SCRIPT
1716 static nsresult DispatchEvent(nsPresContext* aPresContext,
1717 WidgetEvent* aEvent, nsIContent* aTarget,
1718 bool aFullDispatch, nsEventStatus* aStatus);
1720 bool IsDisplayContents() const {
1721 return HasServoData() && Servo_Element_IsDisplayContents(this);
1725 * https://html.spec.whatwg.org/#being-rendered
1727 * With a gotcha for display contents:
1728 * https://github.com/whatwg/html/issues/1837
1730 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1732 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1733 return mAttrs.GetAttr(aAttr);
1736 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1737 int32_t aNameSpaceID) const {
1738 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1742 * Returns the attribute map, if there is one.
1744 * @return existing attribute map or nullptr.
1746 nsDOMAttributeMap* GetAttributeMap() {
1747 nsDOMSlots* slots = GetExistingDOMSlots();
1749 return slots ? slots->mAttributeMap.get() : nullptr;
1752 void RecompileScriptEventListeners();
1755 * Get the attr info for the given namespace ID and attribute name. The
1756 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1757 * null. Note that this can only return info on attributes that actually
1758 * live on this element (and is only virtual to handle XUL prototypes). That
1759 * is, this should only be called from methods that only care about attrs
1760 * that effectively live in mAttrs.
1762 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1763 const nsAtom* aName) const {
1764 NS_ASSERTION(aName, "must have attribute name");
1765 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1766 "must have a real namespace ID!");
1768 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1769 if (index < 0) {
1770 return BorrowedAttrInfo(nullptr, nullptr);
1773 return mAttrs.AttrInfoAt(index);
1777 * Parse a string into an nsAttrValue for a CORS attribute. This
1778 * never fails. The resulting value is an enumerated value whose
1779 * GetEnumValue() returns one of the above constants.
1781 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1784 * Return the CORS mode for a given string
1786 static CORSMode StringToCORSMode(const nsAString& aValue);
1789 * Return the CORS mode for a given nsAttrValue (which may be null,
1790 * but if not should have been parsed via ParseCORSValue).
1792 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1794 nsINode* GetScopeChainParent() const override;
1796 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) override;
1799 * Locate a TextEditor rooted at this content node, if there is one.
1801 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1804 * Gets value of boolean attribute. Only works for attributes in null
1805 * namespace.
1807 * @param aAttr name of attribute.
1808 * @param aValue Boolean value of attribute.
1810 bool GetBoolAttr(nsAtom* aAttr) const { return HasAttr(aAttr); }
1813 * Sets value of boolean attribute by removing attribute or setting it to
1814 * the empty string. Only works for attributes in null namespace.
1816 * @param aAttr name of attribute.
1817 * @param aValue Boolean value of attribute.
1819 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1822 * Gets the enum value string of an attribute and using a default value if
1823 * the attribute is missing or the string is an invalid enum value.
1825 * @param aType the name of the attribute.
1826 * @param aDefault the default value if the attribute is missing or invalid.
1827 * @param aResult string corresponding to the value [out].
1829 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1830 nsAString& aResult) const;
1833 * Gets the enum value string of an attribute and using the default missing
1834 * value if the attribute is missing or the default invalid value if the
1835 * string is an invalid enum value.
1837 * @param aType the name of the attribute.
1838 * @param aDefaultMissing the default value if the attribute is missing. If
1839 null and the attribute is missing, aResult will be
1840 set to the null DOMString; this only matters for
1841 cases in which we're reflecting a nullable string.
1842 * @param aDefaultInvalid the default value if the attribute is invalid.
1843 * @param aResult string corresponding to the value [out].
1845 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1846 const char* aDefaultInvalid, nsAString& aResult) const;
1849 * Unset an attribute.
1851 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1852 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1856 * Set an attribute in the simplest way possible.
1858 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1859 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1862 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1863 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1864 aError =
1865 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1869 * Preallocate space in this element's attribute array for the given
1870 * total number of attributes.
1872 void TryReserveAttributeCount(uint32_t aAttributeCount);
1874 void SetParserHadDuplicateAttributeError() {
1875 SetFlags(ELEMENT_PARSER_HAD_DUPLICATE_ATTR_ERROR);
1879 * Set a content attribute via a reflecting nullable string IDL
1880 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1881 * this will actually remove the content attribute.
1883 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1884 ErrorResult& aError);
1887 * Retrieve the ratio of font-size-inflated text font size to computed font
1888 * size for this element. This will query the element for its primary frame,
1889 * and then use this to get font size inflation information about the frame.
1891 * @returns The font size inflation ratio (inflated font size to uninflated
1892 * font size) for the primary frame of this element. Returns 1.0
1893 * by default if font size inflation is not enabled. Returns -1
1894 * if the element does not have a primary frame.
1896 * @note The font size inflation ratio that is returned is actually the
1897 * font size inflation data for the element's _primary frame_, not the
1898 * element itself, but for most purposes, this should be sufficient.
1900 float FontSizeInflation();
1902 void GetImplementedPseudoElement(nsAString&) const;
1905 * Get the pseudo element for this pseudo request (i.e. PseudoStyleType and
1906 * its function parameter, if any).
1908 Element* GetPseudoElement(const PseudoStyleRequest&) const;
1910 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1911 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1914 * Helpers for .dataset. This is implemented on Element, though only some
1915 * sorts of elements expose it to JS as a .dataset property
1917 // Getter, to be called from bindings.
1918 already_AddRefed<nsDOMStringMap> Dataset();
1919 // Callback for destructor of dataset to ensure to null out our weak pointer
1920 // to it.
1921 void ClearDataset();
1923 // A number of methods to cast to various XUL interfaces. They return a
1924 // pointer only if the element implements that interface.
1925 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1926 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1927 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1928 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1929 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1930 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1931 AsXULMultiSelectControl();
1932 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1933 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1934 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1935 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1936 already_AddRefed<nsIBrowser> AsBrowser();
1937 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1940 * Get the presentation context for this content node.
1941 * @return the presentation context
1943 enum PresContextFor { eForComposedDoc, eForUncomposedDoc };
1944 nsPresContext* GetPresContext(PresContextFor aFor);
1947 * The method focuses (or activates) element that accesskey is bound to. It is
1948 * called when accesskey is activated.
1950 * @param aKeyCausesActivation - if true then element should be activated
1951 * @param aIsTrustedEvent - if true then event that is cause of accesskey
1952 * execution is trusted.
1953 * @return an error if the element isn't able to handle the accesskey (caller
1954 * would look for the next element to handle it).
1955 * a boolean indicates whether the focus moves to the element after
1956 * the element handles the accesskey.
1958 MOZ_CAN_RUN_SCRIPT
1959 virtual Result<bool, nsresult> PerformAccesskey(bool aKeyCausesActivation,
1960 bool aIsTrustedEvent) {
1961 return Err(NS_ERROR_NOT_IMPLEMENTED);
1964 protected:
1965 // Supported rel values for <form> and anchors.
1966 static const DOMTokenListSupportedToken sAnchorAndFormRelValues[];
1969 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1970 * read.
1972 static const bool kFireMutationEvent = true;
1973 static const bool kDontFireMutationEvent = false;
1974 static const bool kNotifyDocumentObservers = true;
1975 static const bool kDontNotifyDocumentObservers = false;
1976 static const bool kCallAfterSetAttr = true;
1977 static const bool kDontCallAfterSetAttr = false;
1980 * The supported values of blocking attribute for use with nsDOMTokenList.
1982 static const DOMTokenListSupportedToken sSupportedBlockingValues[];
1985 * Set attribute and (if needed) notify documentobservers and fire off
1986 * mutation events. This will send the AttributeChanged notification.
1987 * Callers of this method are responsible for calling AttributeWillChange,
1988 * since that needs to happen before the new attr value has been set, and
1989 * in particular before it has been parsed.
1991 * For the boolean parameters, consider using the named bools above to aid
1992 * code readability.
1994 * @param aNamespaceID namespace of attribute
1995 * @param aAttribute local-name of attribute
1996 * @param aPrefix aPrefix of attribute
1997 * @param aOldValue The old value of the attribute to use as a fallback
1998 * in the cases where the actual old value (i.e.
1999 * its current value) is !StoresOwnData() --- in which
2000 * case the current value is probably already useless.
2001 * If the current value is StoresOwnData() (or absent),
2002 * aOldValue will not be used. aOldValue will only be set
2003 * in certain circumstances (there are mutation
2004 * listeners, element is a custom element, attribute was
2005 * not previously unset). Otherwise it will be null.
2006 * @param aParsedValue parsed new value of attribute. Replaced by the
2007 * old value of the attribute. This old value is only
2008 * useful if either it or the new value is StoresOwnData.
2009 * @param aSubjectPrincipal
2010 * the principal of the scripted caller responsible for
2011 * setting the attribute, or null if no scripted caller
2012 * can be determined. A null value here does not
2013 * guarantee that there is no scripted caller, but a
2014 * non-null value does guarantee that a scripted caller
2015 * with the given principal is directly responsible for
2016 * the attribute change.
2017 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
2018 * needed if aFireMutation or aNotify is true.
2019 * @param aFireMutation should mutation-events be fired?
2020 * @param aNotify should we notify document-observers?
2021 * @param aCallAfterSetAttr should we call AfterSetAttr?
2022 * @param aComposedDocument The current composed document of the element.
2023 * @param aGuard For making sure that this is called with a
2024 * mozAutoDocUpdate instance, this is here. Specify
2025 * an instance of it which you created for the call.
2027 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
2028 nsAtom* aPrefix, const nsAttrValue* aOldValue,
2029 nsAttrValue& aParsedValue,
2030 nsIPrincipal* aSubjectPrincipal, uint8_t aModType,
2031 bool aFireMutation, bool aNotify,
2032 bool aCallAfterSetAttr, Document* aComposedDocument,
2033 const mozAutoDocUpdate& aGuard);
2036 * Convert an attribute string value to attribute type based on the type of
2037 * attribute. Called by SetAttr(). Note that at the moment we only do this
2038 * for attributes in the null namespace (kNameSpaceID_None).
2040 * @param aNamespaceID the namespace of the attribute to convert
2041 * @param aAttribute the attribute to convert
2042 * @param aValue the string value to convert
2043 * @param aMaybeScriptedPrincipal the principal of the script setting the
2044 * attribute, if one can be determined, or null otherwise. As in
2045 * AfterSetAttr, a null value does not guarantee that the attribute was
2046 * not set by a scripted caller, but a non-null value guarantees that
2047 * the attribute was set by a scripted caller with the given principal.
2048 * @param aResult the nsAttrValue [OUT]
2049 * @return true if the parsing was successful, false otherwise
2051 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
2052 const nsAString& aValue,
2053 nsIPrincipal* aMaybeScriptedPrincipal,
2054 nsAttrValue& aResult);
2057 * Hook that is called by Element::SetAttr to allow subclasses to
2058 * deal with attribute sets. This will only be called after we verify that
2059 * we're actually doing an attr set and will be called before
2060 * AttributeWillChange and before ParseAttribute and hence before we've set
2061 * the new value.
2063 * @param aNamespaceID the namespace of the attr being set
2064 * @param aName the localname of the attribute being set
2065 * @param aValue the value it's being set to represented as either a string or
2066 * a parsed nsAttrValue. Alternatively, if the attr is being removed it
2067 * will be null.
2068 * @param aNotify Whether we plan to notify document observers.
2070 virtual void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
2071 const nsAttrValue* aValue, bool aNotify);
2074 * Hook that is called by Element::SetAttr to allow subclasses to
2075 * deal with attribute sets. This will only be called after we have called
2076 * SetAndSwapAttr (that is, after we have actually set the attr). It will
2077 * always be called under a scriptblocker.
2079 * @param aNamespaceID the namespace of the attr being set
2080 * @param aName the localname of the attribute being set
2081 * @param aValue the value it's being set to. If null, the attr is being
2082 * removed.
2083 * @param aOldValue the value that the attribute had previously. If null,
2084 * the attr was not previously set. This argument may not have the
2085 * correct value for SVG elements, or other cases in which the
2086 * attribute value doesn't store its own data
2087 * @param aMaybeScriptedPrincipal the principal of the scripted caller
2088 * responsible for setting the attribute, or null if no scripted caller
2089 * can be determined, or the attribute is being unset. A null value
2090 * here does not guarantee that there is no scripted caller, but a
2091 * non-null value does guarantee that a scripted caller with the given
2092 * principal is directly responsible for the attribute change.
2093 * @param aNotify Whether we plan to notify document observers.
2095 virtual void AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
2096 const nsAttrValue* aValue,
2097 const nsAttrValue* aOldValue,
2098 nsIPrincipal* aMaybeScriptedPrincipal,
2099 bool aNotify);
2102 * This function shall be called just before the id attribute changes. It will
2103 * be called after BeforeSetAttr. If the attribute being changed is not the id
2104 * attribute, this function does nothing. Otherwise, it will remove the old id
2105 * from the document's id cache.
2107 * This must happen after BeforeSetAttr (rather than during) because the
2108 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
2109 * incorrectly determine whether the element had an id, the element may not be
2110 * restyled properly.
2112 * @param aNamespaceID the namespace of the attr being set
2113 * @param aName the localname of the attribute being set
2114 * @param aValue the new id value. Will be null if the id is being unset.
2116 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
2117 const nsAttrValue* aValue);
2120 * This function shall be called just after the id attribute changes. It will
2121 * be called before AfterSetAttr. If the attribute being changed is not the id
2122 * attribute, this function does nothing. Otherwise, it will add the new id to
2123 * the document's id cache and properly set the ElementHasID flag.
2125 * This must happen before AfterSetAttr (rather than during) because the
2126 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
2127 * incorrectly determine whether the element now has an id, the element may
2128 * not be restyled properly.
2130 * @param aNamespaceID the namespace of the attr being set
2131 * @param aName the localname of the attribute being set
2132 * @param aValue the new id value. Will be null if the id is being unset.
2134 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
2135 const nsAttrValue* aValue);
2138 * Usually, setting an attribute to the value that it already has results in
2139 * no action. However, in some cases, setting an attribute to its current
2140 * value should have the effect of, for example, forcing a reload of
2141 * network data. To address that, this function will be called in this
2142 * situation to allow the handling of such a case.
2144 * @param aNamespaceID the namespace of the attr being set
2145 * @param aName the localname of the attribute being set
2146 * @param aValue the value it's being set to represented as either a string or
2147 * a parsed nsAttrValue.
2148 * @param aNotify Whether we plan to notify document observers.
2150 virtual void OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
2151 const nsAttrValueOrString& aValue,
2152 bool aNotify);
2155 * Hook to allow subclasses to produce a different EventListenerManager if
2156 * needed for attachment of attribute-defined handlers
2158 virtual EventListenerManager* GetEventListenerManagerForAttr(
2159 nsAtom* aAttrName, bool* aDefer);
2162 * Internal hook for converting an attribute name-string to nsAttrName in
2163 * case there is such existing attribute. aNameToUse can be passed to get
2164 * name which was used for looking for the attribute (lowercase in HTML).
2166 const nsAttrName* InternalGetAttrNameFromQName(
2167 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
2169 virtual Element* GetNameSpaceElement() override { return this; }
2171 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
2172 const nsAString& aLocalName);
2174 inline void RegisterActivityObserver();
2175 inline void UnregisterActivityObserver();
2178 * Add/remove this element to the documents id cache
2180 void AddToIdTable(nsAtom* aId);
2181 void RemoveFromIdTable();
2184 * Functions to carry out event default actions for links of all types
2185 * (HTML links, XLinks, SVG "XLinks", etc.)
2189 * Check that we meet the conditions to handle a link event
2190 * and that we are actually on a link.
2192 * @param aVisitor event visitor
2193 * @return true if we can handle the link event, false otherwise
2195 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor) const;
2198 * Handle status bar updates before they can be cancelled.
2200 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
2202 void DispatchChromeOnlyLinkClickEvent(EventChainPostVisitor& aVisitor);
2205 * Handle default actions for link event if the event isn't consumed yet.
2207 MOZ_CAN_RUN_SCRIPT
2208 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
2210 public:
2212 * Check if this element is a link. This matches the CSS definition of the
2213 * :any-link pseudo-class.
2215 bool IsLink() const {
2216 return mState.HasAtLeastOneOfStates(ElementState::VISITED |
2217 ElementState::UNVISITED);
2221 * Get a pointer to the full href URI (fully resolved and canonicalized, since
2222 * it's an nsIURI object) for link elements.
2224 * @return A pointer to the URI or null if the element is not a link, or it
2225 * has no HREF attribute, or the HREF attribute is an invalid URI.
2227 virtual already_AddRefed<nsIURI> GetHrefURI() const { return nullptr; }
2229 // Step 2. of
2230 // <https://html.spec.whatwg.org/multipage/semantics.html#get-an-element's-target>
2232 // Sanitize targets that look like they contain dangling markup.
2233 static void SanitizeLinkOrFormTarget(nsAString& aTarget);
2236 * <https://html.spec.whatwg.org/multipage/semantics.html#get-an-element's-target>
2237 * (Excluding <form>)
2239 * Get the target of this link element. Consumers should established that
2240 * this element is a link (probably using IsLink) before calling this
2241 * function (or else why call it?). This method neuters probably markup
2242 * injection attempts.
2244 * Note: for HTML this gets the value of the 'target' attribute; for XLink
2245 * this gets the value of the xlink:_moz_target attribute, or failing that,
2246 * the value of xlink:show, converted to a suitably equivalent named target
2247 * (e.g. _blank).
2249 void GetLinkTarget(nsAString& aTarget);
2251 virtual void GetLinkTargetImpl(nsAString& aTarget);
2253 virtual bool Translate() const;
2255 MOZ_CAN_RUN_SCRIPT
2256 virtual void SetHTMLUnsafe(const TrustedHTMLOrString& aHTML,
2257 ErrorResult& aError);
2259 protected:
2260 enum class ReparseAttributes { No, Yes };
2262 * Copy attributes and state to another element
2263 * @param aDest the object to copy to
2265 nsresult CopyInnerTo(Element* aDest,
2266 ReparseAttributes = ReparseAttributes::Yes);
2269 * Some event handler content attributes have a different name (e.g. different
2270 * case) from the actual event name. This function takes an event handler
2271 * content attribute name and returns the corresponding event name, to be used
2272 * for adding the actual event listener.
2274 virtual nsAtom* GetEventNameForAttr(nsAtom* aAttr);
2277 * Register/unregister this element to accesskey map if it supports accesskey.
2279 virtual void RegUnRegAccessKey(bool aDoReg);
2281 private:
2282 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
2283 void AssertInvariantsOnNodeInfoChange();
2284 #endif
2287 * Slow path for GetClasses, this should only be called for SVG elements.
2289 const nsAttrValue* GetSVGAnimatedClass() const;
2292 * Get this element's client area rect in app units.
2293 * @return the frame's client area
2295 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
2297 /** Gets the scroll size as for the scroll{Width,Height} APIs */
2298 MOZ_CAN_RUN_SCRIPT nsSize GetScrollSize();
2299 /** Gets the scroll position as for the scroll{Top,Left} APIs */
2300 MOZ_CAN_RUN_SCRIPT nsPoint GetScrollOrigin();
2301 /** Gets the scroll range as for the scroll{Top,Left}{Min,Max} APIs */
2302 MOZ_CAN_RUN_SCRIPT nsRect GetScrollRange();
2305 * GetCustomInterface is somewhat like a GetInterface, but it is expected
2306 * that the implementation is provided by a custom element or via the
2307 * the XBL implements keyword. To use this, create a public method that
2308 * wraps a call to GetCustomInterface.
2310 template <class T>
2311 void GetCustomInterface(nsGetterAddRefs<T> aResult);
2313 // Prevent people from doing pointless checks/casts on Element instances.
2314 void IsElement() = delete;
2315 void AsElement() = delete;
2317 // Data members
2318 ElementState mState;
2319 // Per-node data managed by Servo.
2321 // There should not be data on nodes that are not in the flattened tree, or
2322 // descendants of display: none elements.
2323 mozilla::RustCell<ServoNodeData*> mServoData;
2325 protected:
2326 // Array containing all attributes for this element
2327 AttrArray mAttrs;
2330 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
2332 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2333 const nsAtom* aName) const {
2334 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2335 MOZ_ASSERT(aName, "Must have attribute name");
2337 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2338 return val && !val->IsEmptyString();
2341 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2342 const nsAString& aValue,
2343 nsCaseTreatment aCaseSensitive) const {
2344 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2347 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2348 const nsAtom* aValue,
2349 nsCaseTreatment aCaseSensitive) const {
2350 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2353 } // namespace dom
2354 } // namespace mozilla
2356 NON_VIRTUAL_ADDREF_RELEASE(mozilla::dom::Element)
2358 inline mozilla::dom::Element* nsINode::AsElement() {
2359 MOZ_ASSERT(IsElement());
2360 return static_cast<mozilla::dom::Element*>(this);
2363 inline const mozilla::dom::Element* nsINode::AsElement() const {
2364 MOZ_ASSERT(IsElement());
2365 return static_cast<const mozilla::dom::Element*>(this);
2368 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2369 return mozilla::dom::Element::FromNodeOrNull(mParent);
2372 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2373 nsIContent* previousSibling = GetPreviousSibling();
2374 while (previousSibling) {
2375 if (previousSibling->IsElement()) {
2376 return previousSibling->AsElement();
2378 previousSibling = previousSibling->GetPreviousSibling();
2381 return nullptr;
2384 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2385 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2386 : GetParentElement();
2389 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2390 nsIContent* nextSibling = GetNextSibling();
2391 while (nextSibling) {
2392 if (nextSibling->IsElement()) {
2393 return nextSibling->AsElement();
2395 nextSibling = nextSibling->GetNextSibling();
2398 return nullptr;
2402 * Macros to implement Clone(). _elementName is the class for which to implement
2403 * Clone.
2405 #define NS_IMPL_ELEMENT_CLONE(_elementName, ...) \
2406 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2407 nsINode** aResult) const { \
2408 *aResult = nullptr; \
2409 RefPtr<_elementName> it = new (aNodeInfo->NodeInfoManager()) \
2410 _elementName(do_AddRef(aNodeInfo), ##__VA_ARGS__); \
2411 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2412 if (NS_SUCCEEDED(rv)) { \
2413 it.forget(aResult); \
2415 return rv; \
2418 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName, ...) \
2419 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2420 nsINode** aResult) const { \
2421 *aResult = nullptr; \
2422 RefPtr<_elementName> it = new (aNodeInfo->NodeInfoManager()) \
2423 _elementName(do_AddRef(aNodeInfo), ##__VA_ARGS__); \
2424 nsresult rv = it->Init(); \
2425 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2426 if (NS_FAILED(rv2)) { \
2427 rv = rv2; \
2429 if (NS_SUCCEEDED(rv)) { \
2430 it.forget(aResult); \
2432 return rv; \
2435 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2436 NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName, NOT_FROM_PARSER)
2438 #endif // mozilla_dom_Element_h__