Bug 1854550 - pt 10. Allow LOG() with zero extra arguments r=glandium
[gecko.git] / dom / base / Element.h
blobb71c363f224fb3b8e97bd3d5a5200cb05a5d22db
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/DirectionalityUtils.h"
39 #include "mozilla/dom/FragmentOrElement.h"
40 #include "mozilla/dom/NameSpaceConstants.h"
41 #include "mozilla/dom/NodeInfo.h"
42 #include "mozilla/dom/RustTypes.h"
43 #include "mozilla/dom/ShadowRootBinding.h"
44 #include "nsAtom.h"
45 #include "nsAttrValue.h"
46 #include "nsAttrValueInlines.h"
47 #include "nsCaseTreatment.h"
48 #include "nsChangeHint.h"
49 #include "nsTHashMap.h"
50 #include "nsDebug.h"
51 #include "nsError.h"
52 #include "nsGkAtoms.h"
53 #include "nsHashKeys.h"
54 #include "nsIContent.h"
55 #include "nsID.h"
56 #include "nsINode.h"
57 #include "nsLiteralString.h"
58 #include "nsRect.h"
59 #include "nsString.h"
60 #include "nsStringFlags.h"
61 #include "nsTLiteralString.h"
62 #include "nscore.h"
64 class JSObject;
65 class mozAutoDocUpdate;
66 class nsAttrName;
67 class nsAttrValueOrString;
68 class nsContentList;
69 class nsDOMAttributeMap;
70 class nsDOMCSSAttributeDeclaration;
71 class nsDOMStringMap;
72 class nsDOMTokenList;
73 class nsFocusManager;
74 class nsGenericHTMLFormControlElementWithState;
75 class nsGlobalWindowInner;
76 class nsGlobalWindowOuter;
77 class nsImageLoadingContent;
78 class nsIAutoCompletePopup;
79 class nsIBrowser;
80 class nsIDOMXULButtonElement;
81 class nsIDOMXULContainerElement;
82 class nsIDOMXULContainerItemElement;
83 class nsIDOMXULControlElement;
84 class nsIDOMXULMenuListElement;
85 class nsIDOMXULMultiSelectControlElement;
86 class nsIDOMXULRadioGroupElement;
87 class nsIDOMXULRelatedElement;
88 class nsIDOMXULSelectControlElement;
89 class nsIDOMXULSelectControlItemElement;
90 class nsIFrame;
91 class nsIHTMLCollection;
92 class nsIMozBrowserFrame;
93 class nsIPrincipal;
94 class nsIScreen;
95 class nsIScrollableFrame;
96 class nsIURI;
97 class nsObjectLoadingContent;
98 class nsPresContext;
99 class nsWindowSizes;
100 struct JSContext;
101 struct ServoNodeData;
102 template <class E>
103 class nsTArray;
104 template <class T>
105 class nsGetterAddRefs;
107 namespace mozilla {
108 class DeclarationBlock;
109 class MappedDeclarationsBuilder;
110 class ErrorResult;
111 class OOMReporter;
112 class SMILAttr;
113 struct MutationClosureData;
114 class TextEditor;
115 namespace css {
116 struct URLValue;
117 } // namespace css
118 namespace dom {
119 struct CheckVisibilityOptions;
120 struct CustomElementData;
121 struct SetHTMLOptions;
122 struct GetAnimationsOptions;
123 struct ScrollIntoViewOptions;
124 struct ScrollToOptions;
125 struct FocusOptions;
126 struct ShadowRootInit;
127 struct ScrollOptions;
128 class Attr;
129 class BooleanOrScrollIntoViewOptions;
130 class Document;
131 class HTMLFormElement;
132 class DOMIntersectionObserver;
133 class DOMMatrixReadOnly;
134 class Element;
135 class ElementOrCSSPseudoElement;
136 class PopoverData;
137 class Promise;
138 class Sanitizer;
139 class ShadowRoot;
140 class UnrestrictedDoubleOrKeyframeAnimationOptions;
141 template <typename T>
142 class Optional;
143 enum class CallerType : uint32_t;
144 enum class ReferrerPolicy : uint8_t;
145 typedef nsTHashMap<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>
146 IntersectionObserverList;
147 } // namespace dom
148 } // namespace mozilla
150 using nsMapRuleToAttributesFunc = void (*)(mozilla::MappedDeclarationsBuilder&);
152 // Declared here because of include hell.
153 extern "C" bool Servo_Element_IsDisplayContents(const mozilla::dom::Element*);
155 already_AddRefed<nsContentList> NS_GetContentList(nsINode* aRootNode,
156 int32_t aMatchNameSpaceId,
157 const nsAString& aTagname);
159 #define ELEMENT_FLAG_BIT(n_) \
160 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
162 // Element-specific flags
163 enum : uint32_t {
164 // Whether this node has dirty descendants for Servo's style system.
165 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(0),
166 // Whether this node has dirty descendants for animation-only restyle for
167 // Servo's style system.
168 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(1),
170 // Whether the element has been snapshotted due to attribute or state changes
171 // by the Servo restyle manager.
172 ELEMENT_HAS_SNAPSHOT = ELEMENT_FLAG_BIT(2),
174 // Whether the element has already handled its relevant snapshot.
176 // Used by the servo restyle process in order to accurately track whether the
177 // style of an element is up-to-date, even during the same restyle process.
178 ELEMENT_HANDLED_SNAPSHOT = ELEMENT_FLAG_BIT(3),
180 // If this flag is set on an element, that means that it is a HTML datalist
181 // element or has a HTML datalist element ancestor.
182 ELEMENT_IS_DATALIST_OR_HAS_DATALIST_ANCESTOR = ELEMENT_FLAG_BIT(4),
184 // Remaining bits are for subclasses
185 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET = NODE_TYPE_SPECIFIC_BITS_OFFSET + 5
188 #undef ELEMENT_FLAG_BIT
190 // Make sure we have space for our bits
191 ASSERT_NODE_FLAGS_SPACE(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
193 namespace mozilla {
194 enum class PseudoStyleType : uint8_t;
195 class EventChainPostVisitor;
196 class EventChainPreVisitor;
197 class EventChainVisitor;
198 class EventListenerManager;
199 class EventStateManager;
201 namespace dom {
203 struct CustomElementDefinition;
204 class Animation;
205 class CustomElementRegistry;
206 class Link;
207 class DOMRect;
208 class DOMRectList;
209 class Flex;
210 class Grid;
212 // IID for the dom::Element interface
213 #define NS_ELEMENT_IID \
215 0xc67ed254, 0xfd3b, 0x4b10, { \
216 0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1 \
220 #define REFLECT_NULLABLE_DOMSTRING_ATTR(method, attr) \
221 void Get##method(nsAString& aValue) const { \
222 const nsAttrValue* val = mAttrs.GetAttr(nsGkAtoms::attr); \
223 if (!val) { \
224 SetDOMStringToNull(aValue); \
225 return; \
227 val->ToString(aValue); \
229 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
230 SetOrRemoveNullableStringAttr(nsGkAtoms::attr, aValue, aRv); \
233 class Element : public FragmentOrElement {
234 public:
235 #ifdef MOZILLA_INTERNAL_API
236 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
237 : FragmentOrElement(std::move(aNodeInfo)),
238 mState(ElementState::READONLY | ElementState::DEFINED) {
239 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
240 "Bad NodeType in aNodeInfo");
241 SetIsElement();
244 ~Element() {
245 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
248 #endif // MOZILLA_INTERNAL_API
250 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
252 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
254 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
256 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
259 * Method to get the full state of this element. See dom/base/rust/lib.rs for
260 * the possible bits that could be set here.
262 ElementState State() const { return mState; }
265 * Returns the current disabled state of the element.
267 bool IsDisabled() const { return State().HasState(ElementState::DISABLED); }
268 bool IsReadOnly() const { return State().HasState(ElementState::READONLY); }
269 bool IsDisabledOrReadOnly() const {
270 return State().HasAtLeastOneOfStates(ElementState::DISABLED |
271 ElementState::READONLY);
274 virtual int32_t TabIndexDefault() { return -1; }
277 * Get tabIndex of this element. If not found, return TabIndexDefault.
279 int32_t TabIndex();
282 * Get the parsed value of tabindex attribute.
284 Maybe<int32_t> GetTabIndexAttrValue();
287 * Set tabIndex value to this element.
289 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
292 * Sets the ShadowRoot binding for this element. The contents of the
293 * binding is rendered in place of this node's children.
295 * @param aShadowRoot The ShadowRoot to be bound to this element.
297 void SetShadowRoot(ShadowRoot* aShadowRoot);
299 void SetLastRememberedBSize(float aBSize);
300 void SetLastRememberedISize(float aISize);
301 void RemoveLastRememberedBSize();
302 void RemoveLastRememberedISize();
305 * Make focus on this element.
307 // TODO: Convert Focus() to MOZ_CAN_RUN_SCRIPT and get rid of the
308 // kungFuDeathGrip in it.
309 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Focus(const FocusOptions& aOptions,
310 const CallerType aCallerType,
311 ErrorResult& aError);
314 * Show blur and clear focus.
316 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Blur(mozilla::ErrorResult& aError);
319 * The style state of this element. This is the real state of the element
320 * with any style locks applied for pseudo-class inspecting.
322 ElementState StyleState() const {
323 if (!HasLockedStyleStates()) {
324 return mState;
326 return StyleStateFromLocks();
330 * StyleStateLocks is used to specify which event states should be locked,
331 * and whether they should be locked to on or off.
333 struct StyleStateLocks {
334 // mLocks tracks which event states should be locked.
335 ElementState mLocks;
336 // mValues tracks if the locked state should be on or off.
337 ElementState mValues;
341 * The style state locks applied to this element.
343 StyleStateLocks LockedStyleStates() const;
346 * Add a style state lock on this element.
347 * aEnabled is the value to lock the given state bits to.
349 void LockStyleStates(ElementState aStates, bool aEnabled);
352 * Remove a style state lock on this element.
354 void UnlockStyleStates(ElementState aStates);
357 * Clear all style state locks on this element.
359 void ClearStyleStateLocks();
362 * Accessors for the state of our dir attribute.
364 bool HasDirAuto() const {
365 return State().HasState(ElementState::HAS_DIR_ATTR_LIKE_AUTO);
369 * Elements with dir="rtl" or dir="ltr".
371 bool HasFixedDir() const {
372 return State().HasAtLeastOneOfStates(ElementState::HAS_DIR_ATTR_LTR |
373 ElementState::HAS_DIR_ATTR_RTL);
377 * Get the inline style declaration, if any, for this element.
379 DeclarationBlock* GetInlineStyleDeclaration() const;
382 * Get the mapped attributes, if any, for this element.
384 StyleLockedDeclarationBlock* GetMappedAttributeStyle() const {
385 return mAttrs.GetMappedDeclarationBlock();
388 bool IsPendingMappedAttributeEvaluation() const {
389 return mAttrs.IsPendingMappedAttributeEvaluation();
392 void SetMappedDeclarationBlock(already_AddRefed<StyleLockedDeclarationBlock>);
395 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
396 * so that the element implementation can access the old style attribute
397 * value.
399 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
402 * Set the inline style declaration for this element.
404 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
405 MutationClosureData& aData);
408 * Get the SMIL override style declaration for this element. If the
409 * rule hasn't been created, this method simply returns null.
411 DeclarationBlock* GetSMILOverrideStyleDeclaration();
414 * Set the SMIL override style declaration for this element. This method will
415 * notify the document's pres context, so that the style changes will be
416 * noticed.
418 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
421 * Returns a new SMILAttr that allows the caller to animate the given
422 * attribute on this element.
424 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
425 nsAtom* aName);
428 * Get the SMIL override style for this element. This is a style declaration
429 * that is applied *after* the inline style, and it can be used e.g. to store
430 * animated style values.
432 * Note: This method is analogous to the 'GetStyle' method in
433 * nsGenericHTMLElement and nsStyledElement.
435 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
438 * Returns if the element is labelable as per HTML specification.
440 virtual bool IsLabelable() const;
443 * Returns if the element is interactive content as per HTML specification.
445 virtual bool IsInteractiveHTMLContent() const;
448 * Returns |this| as an nsIMozBrowserFrame* if the element is a frame or
449 * iframe element.
451 * We have this method, rather than using QI, so that we can use it during
452 * the servo traversal, where we can't QI DOM nodes because of non-thread-safe
453 * refcounts.
455 virtual nsIMozBrowserFrame* GetAsMozBrowserFrame() { return nullptr; }
458 * Is the attribute named aAttribute a mapped attribute?
460 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
462 nsresult BindToTree(BindContext&, nsINode& aParent) override;
463 void UnbindFromTree(bool aNullParent = true) override;
465 virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
466 static void MapNoAttributesInto(mozilla::MappedDeclarationsBuilder&);
469 * Get a hint that tells the style system what to do when
470 * an attribute on this node changes, if something needs to happen
471 * in response to the change *other* than the result of what is
472 * mapped into style data via any type of style rule.
474 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
475 int32_t aModType) const;
477 inline Directionality GetDirectionality() const {
478 ElementState state = State();
479 if (state.HasState(ElementState::RTL)) {
480 return eDir_RTL;
482 if (state.HasState(ElementState::LTR)) {
483 return eDir_LTR;
485 return eDir_NotSet;
488 inline void SetDirectionality(Directionality aDir, bool aNotify) {
489 AutoStateChangeNotifier notifier(*this, aNotify);
490 RemoveStatesSilently(ElementState::DIR_STATES);
491 switch (aDir) {
492 case eDir_RTL:
493 AddStatesSilently(ElementState::RTL);
494 break;
495 case eDir_LTR:
496 AddStatesSilently(ElementState::LTR);
497 break;
498 default:
499 break;
503 Directionality GetComputedDirectionality() const;
505 static const uint32_t kAllServoDescendantBits =
506 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO |
507 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO |
508 NODE_DESCENDANTS_NEED_FRAMES;
511 * Notes that something in the given subtree of this element needs dirtying,
512 * and that all the relevant dirty bits have already been propagated up to the
513 * element.
515 * This is important because `NoteDirtyForServo` uses the dirty bits to reason
516 * about the shape of the tree, so we can't just call into there.
518 void NoteDirtySubtreeForServo();
520 void NoteDirtyForServo();
521 void NoteAnimationOnlyDirtyForServo();
522 void NoteDescendantsNeedFramesForServo();
524 bool HasDirtyDescendantsForServo() const {
525 return HasFlag(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
528 void SetHasDirtyDescendantsForServo() {
529 SetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
532 void UnsetHasDirtyDescendantsForServo() {
533 UnsetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
536 bool HasAnimationOnlyDirtyDescendantsForServo() const {
537 return HasFlag(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
540 void SetHasAnimationOnlyDirtyDescendantsForServo() {
541 SetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
544 void UnsetHasAnimationOnlyDirtyDescendantsForServo() {
545 UnsetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
548 bool HasServoData() const { return !!mServoData.Get(); }
550 void ClearServoData() { ClearServoData(GetComposedDoc()); }
551 void ClearServoData(Document* aDocument);
553 PopoverData* GetPopoverData() const {
554 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
555 return slots ? slots->mPopoverData.get() : nullptr;
558 PopoverData& EnsurePopoverData() {
559 if (auto* popoverData = GetPopoverData()) {
560 return *popoverData;
562 return CreatePopoverData();
565 bool IsAutoPopover() const;
566 bool IsPopoverOpen() const;
569 * https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
571 Element* GetTopmostPopoverAncestor(const Element* aInvoker) const;
573 ElementAnimationData* GetAnimationData() const {
574 if (!MayHaveAnimations()) {
575 return nullptr;
577 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
578 return slots ? slots->mAnimations.get() : nullptr;
581 ElementAnimationData& EnsureAnimationData() {
582 if (auto* anim = GetAnimationData()) {
583 return *anim;
585 return CreateAnimationData();
588 private:
589 ElementAnimationData& CreateAnimationData();
590 PopoverData& CreatePopoverData();
592 public:
593 void ClearPopoverData();
596 * Gets the custom element data used by web components custom element.
597 * Custom element data is created at the first attempt to enqueue a callback.
599 * @return The custom element data or null if none.
601 CustomElementData* GetCustomElementData() const {
602 if (!HasCustomElementData()) {
603 return nullptr;
606 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
607 return slots ? slots->mCustomElementData.get() : nullptr;
611 * Sets the custom element data, ownership of the
612 * callback data is taken by this element.
614 * @param aData The custom element data.
616 void SetCustomElementData(UniquePtr<CustomElementData> aData);
619 * Gets the custom element definition used by web components custom element.
621 * @return The custom element definition or null if element is not a custom
622 * element or custom element is not defined yet.
624 CustomElementDefinition* GetCustomElementDefinition() const;
627 * Sets the custom element definition, called when custom element is created
628 * or upgraded.
630 * @param aDefinition The custom element definition.
632 virtual void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
634 const AttrArray& GetAttrs() const { return mAttrs; }
636 void SetDefined(bool aSet) { SetStates(ElementState::DEFINED, aSet); }
638 // AccessibilityRole
639 REFLECT_NULLABLE_DOMSTRING_ATTR(Role, role)
641 // AriaAttributes
642 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
643 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
644 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBusy, aria_busy)
645 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaChecked, aria_checked)
646 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColCount, aria_colcount)
647 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
648 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
649 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
650 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaCurrent, aria_current)
651 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaDescription, aria_description)
652 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
653 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
654 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
655 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaHidden, aria_hidden)
656 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
657 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
658 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLabel, aria_label)
659 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLevel, aria_level)
660 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLive, aria_live)
661 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaModal, aria_modal)
662 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
663 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
664 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
665 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
666 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
667 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPressed, aria_pressed)
668 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
669 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
670 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRequired, aria_required)
671 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
672 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
673 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
674 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
675 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
676 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSelected, aria_selected)
677 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
678 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSort, aria_sort)
679 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
680 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
681 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
682 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
684 protected:
685 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
686 ErrorResult& aError);
688 public:
689 MOZ_CAN_RUN_SCRIPT
690 nsIScrollableFrame* GetScrollFrame(nsIFrame** aStyledFrame = nullptr,
691 FlushType aFlushType = FlushType::Layout);
693 private:
694 // Style state computed from element's state and style locks.
695 ElementState StyleStateFromLocks() const;
697 void NotifyStateChange(ElementState aStates);
698 void NotifyStyleStateChange(ElementState aStates);
700 public:
701 struct AutoStateChangeNotifier {
702 AutoStateChangeNotifier(Element& aElement, bool aNotify)
703 : mElement(aElement), mOldState(aElement.State()), mNotify(aNotify) {}
704 ~AutoStateChangeNotifier() {
705 if (!mNotify) {
706 return;
708 ElementState newState = mElement.State();
709 if (mOldState != newState) {
710 mElement.NotifyStateChange(mOldState ^ newState);
714 private:
715 Element& mElement;
716 const ElementState mOldState;
717 const bool mNotify;
720 // Method to add state bits. This should be called from subclass constructors
721 // to set up our event state correctly at construction time, and other places
722 // where we don't want to notify a state change, or there's an
723 // AutoStateChangeNotifier on the stack.
724 void AddStatesSilently(ElementState aStates) { mState |= aStates; }
725 // Method to remove state bits. This should be called from subclass
726 // constructors to set up our event state correctly at construction time and
727 // other places where we don't want to notify a state change.
728 void RemoveStatesSilently(ElementState aStates) { mState &= ~aStates; }
729 // Methods to add state bits, potentially notifying. These will handle setting
730 // up script blockers when they notify, so no need to do it in the callers
731 // unless desired. States passed here must only be those in
732 // EXTERNALLY_MANAGED_STATES.
733 void AddStates(ElementState aStates, bool aNotify = true) {
734 ElementState old = mState;
735 AddStatesSilently(aStates);
736 if (aNotify && old != mState) {
737 NotifyStateChange(old ^ mState);
740 void RemoveStates(ElementState aStates, bool aNotify = true) {
741 ElementState old = mState;
742 RemoveStatesSilently(aStates);
743 if (aNotify && old != mState) {
744 NotifyStateChange(old ^ mState);
747 void SetStates(ElementState aStates, bool aSet, bool aNotify = true) {
748 if (aSet) {
749 AddStates(aStates, aNotify);
750 } else {
751 RemoveStates(aStates, aNotify);
754 void ToggleStates(ElementState aStates, bool aNotify) {
755 mState ^= aStates;
756 if (aNotify) {
757 NotifyStateChange(aStates);
761 void UpdateEditableState(bool aNotify) override;
762 // Makes sure that the READONLY/READWRITE flags are in sync.
763 void UpdateReadOnlyState(bool aNotify);
764 // Form controls and non-form controls should have different :read-only /
765 // :read-write behavior. This is what effectively controls it.
766 virtual bool IsReadOnlyInternal() const;
769 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
770 * with that name exists. This method is intended for character case
771 * conversion if the content object is case insensitive (e.g. HTML). Returns
772 * the nodeinfo of the attribute with the specified name if one exists or
773 * null otherwise.
775 * @param aStr the unparsed attribute string
776 * @return the node info. May be nullptr.
778 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
779 const nsAString& aStr) const;
782 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
783 * is true or there are mutation listeners that must be triggered, the
784 * attribute is currently set, and the new value that is about to be set is
785 * different to the current value. As a perf optimization the new and old
786 * values will not actually be compared if we aren't notifying and we don't
787 * have mutation listeners (in which case it's cheap to just return false
788 * and let the caller go ahead and set the value).
789 * @param aOldValue [out] Set to the old value of the attribute, but only if
790 * there are event listeners. If set, the type of aOldValue will be either
791 * nsAttrValue::eString or nsAttrValue::eAtom.
792 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
793 * MutationEvent_Binding::ADDITION, but only if this helper returns true
794 * @param aHasListeners [out] Set to true if there are mutation event
795 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
796 * @param aOldValueSet [out] Indicates whether an old attribute value has been
797 * stored in aOldValue. The bool will be set to true if a value was stored.
799 bool MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
800 const nsAtom* aPrefix,
801 const nsAttrValueOrString& aValue, bool aNotify,
802 nsAttrValue& aOldValue, uint8_t* aModType,
803 bool* aHasListeners, bool* aOldValueSet);
806 * Notifies mutation listeners if aNotify is true, there are mutation
807 * listeners, and the attribute value is changing.
809 * @param aNamespaceID The namespace of the attribute
810 * @param aName The local name of the attribute
811 * @param aPrefix The prefix of the attribute
812 * @param aValue The value that the attribute is being changed to
813 * @param aNotify If true, mutation listeners will be notified if they exist
814 * and the attribute value is changing
815 * @param aOldValue [out] Set to the old value of the attribute, but only if
816 * there are event listeners. If set, the type of aOldValue will be either
817 * nsAttrValue::eString or nsAttrValue::eAtom.
818 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
819 * MutationEvent_Binding::ADDITION, but only if this helper returns true
820 * @param aHasListeners [out] Set to true if there are mutation event
821 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
822 * @param aOldValueSet [out] Indicates whether an old attribute value has been
823 * stored in aOldValue. The bool will be set to true if a value was stored.
825 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
826 nsAtom* aPrefix,
827 const nsAttrValueOrString& aValue, bool aNotify,
828 nsAttrValue& aOldValue, uint8_t* aModType,
829 bool* aHasListeners, bool* aOldValueSet);
832 * Sets the class attribute.
833 * Assumes that we are not notifying and that the attribute hasn't been
834 * set previously.
836 nsresult SetClassAttrFromParser(nsAtom* aValue);
838 // aParsedValue receives the old value of the attribute. That's useful if
839 // either the input or output value of aParsedValue is StoresOwnData.
840 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
841 nsAttrValue& aParsedValue, bool aNotify);
843 * Get the current value of the attribute. This returns a form that is
844 * suitable for passing back into SetAttr.
846 * @param aNameSpaceID the namespace of the attr (defaults to
847 kNameSpaceID_None in the overload that omits this arg)
848 * @param aName the name of the attr
849 * @param aResult the value (may legitimately be the empty string) [OUT]
850 * @returns true if the attribute was set (even when set to empty string)
851 * false when not set.
852 * GetAttr is not inlined on purpose, to keep down codesize from all the
853 * inlined nsAttrValue bits for C++ callers.
855 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
856 nsAString& aResult) const;
857 bool GetAttr(const nsAtom* aName, nsAString& aResult) const;
860 * Determine if an attribute has been set (empty string or otherwise).
862 * @param aNameSpaceId the namespace id of the attribute (defaults to
863 kNameSpaceID_None in the overload that omits this arg)
864 * @param aAttr the attribute name
865 * @return whether an attribute exists
867 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
868 return mAttrs.HasAttr(aNameSpaceID, aName);
871 bool HasAttr(const nsAtom* aAttr) const { return mAttrs.HasAttr(aAttr); }
874 * Determine if an attribute has been set to a non-empty string value. If the
875 * attribute is not set at all, this will return false.
877 * @param aNameSpaceId the namespace id of the attribute (defaults to
878 * kNameSpaceID_None in the overload that omits this arg)
879 * @param aAttr the attribute name
881 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
883 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
884 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
888 * Test whether this Element's given attribute has the given value. If the
889 * attribute is not set at all, this will return false.
891 * @param aNameSpaceID The namespace ID of the attribute. Must not
892 * be kNameSpaceID_Unknown.
893 * @param aName The name atom of the attribute. Must not be null.
894 * @param aValue The value to compare to.
895 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
897 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
898 const nsAString& aValue,
899 nsCaseTreatment aCaseSensitive) const;
902 * Test whether this Element's given attribute has the given value. If the
903 * attribute is not set at all, this will return false.
905 * @param aNameSpaceID The namespace ID of the attribute. Must not
906 * be kNameSpaceID_Unknown.
907 * @param aName The name atom of the attribute. Must not be null.
908 * @param aValue The value to compare to. Must not be null.
909 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
911 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
912 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
915 * Check whether this Element's given attribute has one of a given list of
916 * values. If there is a match, we return the index in the list of the first
917 * matching value. If there was no attribute at all, then we return
918 * ATTR_MISSING. If there was an attribute but it didn't match, we return
919 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
921 * @param aNameSpaceID The namespace ID of the attribute. Must not
922 * be kNameSpaceID_Unknown.
923 * @param aName The name atom of the attribute. Must not be null.
924 * @param aValues a nullptr-terminated array of pointers to atom values to
925 * test against.
926 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
927 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
928 * indicating the first value of aValues that matched
930 using AttrValuesArray = AttrArray::AttrValuesArray;
931 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
932 AttrArray::AttrValuesArray* aValues,
933 nsCaseTreatment aCaseSensitive) const;
936 * Set attribute values. All attribute values are assumed to have a
937 * canonical string representation that can be used for these
938 * methods. The SetAttr method is assumed to perform a translation
939 * of the canonical form into the underlying content specific
940 * form.
942 * @param aNameSpaceID the namespace of the attribute
943 * @param aName the name of the attribute
944 * @param aValue the value to set
945 * @param aNotify specifies how whether or not the document should be
946 * notified of the attribute change.
948 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
949 bool aNotify) {
950 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
952 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
953 const nsAString& aValue, bool aNotify) {
954 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
956 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
957 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
958 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
959 aNotify);
963 * Set attribute values. All attribute values are assumed to have a
964 * canonical String representation that can be used for these
965 * methods. The SetAttr method is assumed to perform a translation
966 * of the canonical form into the underlying content specific
967 * form.
969 * @param aNameSpaceID the namespace of the attribute
970 * @param aName the name of the attribute
971 * @param aPrefix the prefix of the attribute
972 * @param aValue the value to set
973 * @param aMaybeScriptedPrincipal the principal of the scripted caller
974 * responsible for setting the attribute, or null if no scripted caller can be
975 * determined. A null value here does not guarantee that there is no
976 * scripted caller, but a non-null value does guarantee that a scripted
977 * caller with the given principal is directly responsible for the
978 * attribute change.
979 * @param aNotify specifies how whether or not the document should be
980 * notified of the attribute change.
982 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
983 const nsAString& aValue,
984 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
987 * Remove an attribute so that it is no longer explicitly specified.
989 * @param aNameSpaceID the namespace id of the attribute
990 * @param aName the name of the attribute to unset
991 * @param aNotify specifies whether or not the document should be
992 * notified of the attribute change
994 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aName, bool aNotify);
997 * Get the namespace / name / prefix of a given attribute.
999 * @param aIndex the index of the attribute name
1000 * @returns The name at the given index, or null if the index is
1001 * out-of-bounds.
1002 * @note The document returned by NodeInfo()->GetDocument() (if one is
1003 * present) is *not* necessarily the owner document of the element.
1004 * @note The pointer returned by this function is only valid until the
1005 * next call of either GetAttrNameAt or SetAttr on the element.
1007 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
1008 return mAttrs.GetSafeAttrNameAt(aIndex);
1012 * Same as above, but does not do out-of-bounds checks!
1014 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
1015 return mAttrs.AttrNameAt(aIndex);
1019 * Gets the attribute info (name and value) for this element at a given index.
1021 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
1022 if (aIndex >= mAttrs.AttrCount()) {
1023 return BorrowedAttrInfo(nullptr, nullptr);
1026 return mAttrs.AttrInfoAt(aIndex);
1030 * Get the number of all specified attributes.
1032 * @return the number of attributes
1034 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
1037 * Get the class list of this element (this corresponds to the value of the
1038 * class attribute). This may be null if there are no classes, but that's not
1039 * guaranteed (e.g. we could have class="").
1041 const nsAttrValue* GetClasses() const {
1042 if (!MayHaveClass()) {
1043 return nullptr;
1046 if (IsSVGElement()) {
1047 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
1048 return value;
1052 return GetParsedAttr(nsGkAtoms::_class);
1055 #ifdef MOZ_DOM_LIST
1056 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
1057 List(out, aIndent, ""_ns);
1059 virtual void DumpContent(FILE* out, int32_t aIndent,
1060 bool aDumpAll) const override;
1061 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1062 void ListAttributes(FILE* out) const;
1063 #endif
1066 * Append to aOutDescription a string describing the element and its
1067 * attributes.
1068 * If aShort is true, only the id and class attributes will be listed.
1070 void Describe(nsAString& aOutDescription, bool aShort = false) const;
1073 * Attribute Mapping Helpers
1075 struct MappedAttributeEntry {
1076 const nsStaticAtom* const attribute;
1080 * A common method where you can just pass in a list of maps to check
1081 * for attribute dependence. Most implementations of
1082 * IsAttributeMapped should use this function as a default
1083 * handler.
1085 template <size_t N>
1086 static bool FindAttributeDependence(
1087 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1088 return FindAttributeDependence(aAttribute, aMaps, N);
1091 static nsStaticAtom* const* HTMLSVGPropertiesToTraverseAndUnlink();
1093 private:
1094 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1096 static bool FindAttributeDependence(const nsAtom* aAttribute,
1097 const MappedAttributeEntry* const aMaps[],
1098 uint32_t aMapCount);
1100 protected:
1101 inline bool GetAttr(const nsAtom* aName, DOMString& aResult) const {
1102 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1103 const nsAttrValue* val = mAttrs.GetAttr(aName);
1104 if (!val) {
1105 return false; // DOMString comes pre-emptied.
1107 val->ToString(aResult);
1108 return true;
1111 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1112 DOMString& aResult) const {
1113 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1114 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1115 if (!val) {
1116 return false; // DOMString comes pre-emptied.
1118 val->ToString(aResult);
1119 return true;
1122 public:
1123 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1125 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1126 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1127 const nsAttrValue* val = mAttrs.GetAttr(aName);
1128 if (val) {
1129 val->ToString(aResult);
1130 return true;
1132 // else DOMString comes pre-emptied.
1133 return false;
1136 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1137 void GetId(nsAString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1138 void GetId(DOMString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1139 void SetId(const nsAString& aId) {
1140 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1142 void GetClassName(nsAString& aClassName) {
1143 GetAttr(nsGkAtoms::_class, aClassName);
1145 void GetClassName(DOMString& aClassName) {
1146 GetAttr(nsGkAtoms::_class, aClassName);
1148 void SetClassName(const nsAString& aClassName) {
1149 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1152 nsDOMTokenList* ClassList();
1153 nsDOMTokenList* Part();
1155 nsDOMAttributeMap* Attributes();
1157 void GetAttributeNames(nsTArray<nsString>& aResult);
1159 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1160 DOMString str;
1161 GetAttribute(aName, str);
1162 str.ToString(aReturn);
1165 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1166 void GetAttributeNS(const nsAString& aNamespaceURI,
1167 const nsAString& aLocalName, nsAString& aReturn);
1168 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1169 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1170 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1171 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1172 void SetAttributeNS(const nsAString& aNamespaceURI,
1173 const nsAString& aLocalName, const nsAString& aValue,
1174 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1175 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1176 ErrorResult& aError) {
1177 SetAttribute(aName, aValue, nullptr, aError);
1180 * This method creates a principal that subsumes this element's NodePrincipal
1181 * and which has flags set for elevated permissions that devtools needs to
1182 * operate on this element. The principal returned by this method is used by
1183 * various devtools methods to permit otherwise blocked operations, without
1184 * changing any other restrictions the NodePrincipal might have.
1186 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1187 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1188 ErrorResult& aError);
1189 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1190 const nsAString& aLocalName,
1191 const nsAString& aValue, ErrorResult& aError);
1193 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1194 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1195 const nsAString& aLocalName, ErrorResult& aError);
1196 bool HasAttribute(const nsAString& aName) const {
1197 return InternalGetAttrNameFromQName(aName) != nullptr;
1199 bool HasAttributeNS(const nsAString& aNamespaceURI,
1200 const nsAString& aLocalName) const;
1201 bool HasAttributes() const { return HasAttrs(); }
1202 Element* Closest(const nsACString& aSelector, ErrorResult& aResult);
1203 bool Matches(const nsACString& aSelector, ErrorResult& aError);
1204 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1205 const nsAString& aQualifiedName);
1206 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1207 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1208 ErrorResult& aError);
1209 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1210 const nsAString& aClassNames);
1213 * Returns attribute associated element for the given attribute name, see
1214 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#attr-associated-element
1216 Element* GetAttrAssociatedElement(nsAtom* aAttr) const;
1219 * Sets an attribute element for the given attribute.
1220 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
1222 void ExplicitlySetAttrElement(nsAtom* aAttr, Element* aElement);
1224 PseudoStyleType GetPseudoElementType() const {
1225 nsresult rv = NS_OK;
1226 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1227 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1228 return PseudoStyleType::NotPseudo;
1230 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1233 void SetPseudoElementType(PseudoStyleType aPseudo) {
1234 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1235 "Need to be able to store this in a void*");
1236 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1237 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1241 * Return an array of all elements in the subtree rooted at this
1242 * element that have grid container frames. This does not include
1243 * pseudo-elements.
1245 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1248 * Provide a direct way to determine if this Element has visible
1249 * scrollbars. Flushes layout.
1251 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1253 private:
1255 * Implement the algorithm specified at
1256 * https://dom.spec.whatwg.org/#insert-adjacent for both
1257 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1259 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1260 ErrorResult& aError);
1262 public:
1263 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1264 ErrorResult& aError);
1266 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1267 ErrorResult& aError);
1269 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError);
1270 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError);
1271 bool HasPointerCapture(long aPointerId);
1272 void SetCapture(bool aRetargetToElement);
1274 void SetCaptureAlways(bool aRetargetToElement);
1276 void ReleaseCapture();
1278 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1279 void RequestPointerLock(CallerType aCallerType);
1280 Attr* GetAttributeNode(const nsAString& aName);
1281 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1282 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1283 ErrorResult& aError);
1284 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1285 const nsAString& aLocalName);
1286 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1287 ErrorResult& aError);
1289 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1290 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1292 enum class Loading : uint8_t {
1293 Eager,
1294 Lazy,
1297 Loading LoadingState() const;
1298 void GetLoading(nsAString& aValue) const;
1299 bool ParseLoadingAttribute(const nsAString& aValue, nsAttrValue& aResult);
1301 // Shadow DOM v1
1302 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1303 ErrorResult& aError);
1304 bool CanAttachShadowDOM() const;
1306 enum class DelegatesFocus : bool { No, Yes };
1308 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1309 ShadowRootMode aMode, DelegatesFocus = DelegatesFocus::No,
1310 SlotAssignmentMode aSlotAssignmentMode = SlotAssignmentMode::Named);
1312 // Attach UA Shadow Root if it is not attached.
1313 enum class NotifyUAWidgetSetup : bool { No, Yes };
1314 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes,
1315 DelegatesFocus = DelegatesFocus::No);
1317 // Dispatch an event to UAWidgetsChild, triggering construction
1318 // or onchange callback on the existing widget.
1319 void NotifyUAWidgetSetupOrChange();
1321 enum class UnattachShadowRoot {
1323 Yes,
1326 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1327 // and optionally remove the shadow root.
1328 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1330 void UnattachShadow();
1332 ShadowRoot* GetShadowRootByMode() const;
1333 void SetSlot(const nsAString& aName, ErrorResult& aError);
1334 void GetSlot(nsAString& aName);
1336 ShadowRoot* GetShadowRoot() const {
1337 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1338 return slots ? slots->mShadowRoot.get() : nullptr;
1341 const Maybe<float> GetLastRememberedBSize() const {
1342 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1343 return slots ? slots->mLastRememberedBSize : Nothing();
1345 const Maybe<float> GetLastRememberedISize() const {
1346 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1347 return slots ? slots->mLastRememberedISize : Nothing();
1349 bool HasLastRememberedBSize() const {
1350 return GetLastRememberedBSize().isSome();
1352 bool HasLastRememberedISize() const {
1353 return GetLastRememberedISize().isSome();
1356 const Maybe<ContentRelevancy> GetContentRelevancy() const {
1357 const auto* slots = GetExistingExtendedDOMSlots();
1358 return slots ? slots->mContentRelevancy : Nothing();
1360 void SetContentRelevancy(ContentRelevancy relevancy) {
1361 ExtendedDOMSlots()->mContentRelevancy = Some(relevancy);
1364 const Maybe<bool> GetVisibleForContentVisibility() const {
1365 const auto* slots = GetExistingExtendedDOMSlots();
1366 return slots ? slots->mVisibleForContentVisibility : Nothing();
1368 void SetVisibleForContentVisibility(bool visible) {
1369 ExtendedDOMSlots()->mVisibleForContentVisibility = Some(visible);
1372 void ClearContentRelevancy() {
1373 if (auto* slots = GetExistingExtendedDOMSlots()) {
1374 slots->mContentRelevancy.reset();
1375 slots->mVisibleForContentVisibility.reset();
1379 // https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
1380 MOZ_CAN_RUN_SCRIPT bool CheckVisibility(const CheckVisibilityOptions&);
1382 private:
1383 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1384 // called from JS. Use PresShell::ScrollContentIntoView instead.
1385 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1387 public:
1388 MOZ_CAN_RUN_SCRIPT
1389 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1390 // called from JS. Use PresShell::ScrollContentIntoView instead.
1391 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1392 MOZ_CAN_RUN_SCRIPT void Scroll(double aXScroll, double aYScroll);
1393 MOZ_CAN_RUN_SCRIPT void Scroll(const ScrollToOptions& aOptions);
1394 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1395 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1396 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1397 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1398 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1399 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1400 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1401 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1402 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1403 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1404 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1405 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1406 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1408 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1409 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1411 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1412 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1414 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1415 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1418 MOZ_CAN_RUN_SCRIPT int32_t ScreenX();
1419 MOZ_CAN_RUN_SCRIPT int32_t ScreenY();
1420 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIScreen> GetScreen();
1422 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1423 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1424 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1425 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1427 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1428 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1431 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1432 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1435 // This function will return the block size of first line box, no matter if
1436 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1437 // can't get a primary frame, we will return be zero.
1438 double FirstLineBoxBSize() const;
1440 already_AddRefed<Flex> GetAsFlexContainer();
1441 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1443 bool HasGridFragments();
1445 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1446 Element& aAncestor);
1447 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1448 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1450 already_AddRefed<Animation> Animate(
1451 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1452 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1453 ErrorResult& aError);
1455 MOZ_CAN_RUN_SCRIPT
1456 void GetAnimations(const GetAnimationsOptions& aOptions,
1457 nsTArray<RefPtr<Animation>>& aAnimations);
1459 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1460 nsTArray<RefPtr<Animation>>& aAnimations);
1462 static void GetAnimationsUnsorted(Element* aElement,
1463 PseudoStyleType aPseudoType,
1464 nsTArray<RefPtr<Animation>>& aAnimations);
1466 void CloneAnimationsFrom(const Element& aOther);
1468 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1469 virtual void SetInnerHTML(const nsAString& aInnerHTML,
1470 nsIPrincipal* aSubjectPrincipal,
1471 ErrorResult& aError);
1472 void GetOuterHTML(nsAString& aOuterHTML);
1473 void SetOuterHTML(const nsAString& aOuterHTML, ErrorResult& aError);
1474 void InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
1475 ErrorResult& aError);
1477 void SetHTML(const nsAString& aInnerHTML, const SetHTMLOptions& aOptions,
1478 ErrorResult& aError);
1480 //----------------------------------------
1483 * Add a script event listener with the given event handler name
1484 * (like onclick) and with the value as JS
1485 * @param aEventName the event listener name
1486 * @param aValue the JS to attach
1487 * @param aDefer indicates if deferred execution is allowed
1489 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1490 bool aDefer = true);
1493 * Do whatever needs to be done when the mouse leaves a link
1495 nsresult LeaveLink(nsPresContext* aPresContext);
1497 static bool ShouldBlur(nsIContent* aContent);
1500 * Method to create and dispatch a left-click event loosely based on
1501 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1502 * through the full dispatching of the presshell of the aPresContext; if it's
1503 * false the event will be dispatched only as a DOM event.
1504 * If aPresContext is nullptr, this does nothing.
1506 * @param aFlags Extra flags for the dispatching event. The true flags
1507 * will be respected.
1509 MOZ_CAN_RUN_SCRIPT
1510 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1511 WidgetInputEvent* aSourceEvent,
1512 nsIContent* aTarget, bool aFullDispatch,
1513 const EventFlags* aFlags,
1514 nsEventStatus* aStatus);
1517 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1518 * will be dispatched through the full dispatching of the presshell of the
1519 * aPresContext; if it's false the event will be dispatched only as a DOM
1520 * event.
1521 * If aPresContext is nullptr, this does nothing.
1523 using nsIContent::DispatchEvent;
1524 MOZ_CAN_RUN_SCRIPT
1525 static nsresult DispatchEvent(nsPresContext* aPresContext,
1526 WidgetEvent* aEvent, nsIContent* aTarget,
1527 bool aFullDispatch, nsEventStatus* aStatus);
1529 bool IsDisplayContents() const {
1530 return HasServoData() && Servo_Element_IsDisplayContents(this);
1534 * https://html.spec.whatwg.org/#being-rendered
1536 * With a gotcha for display contents:
1537 * https://github.com/whatwg/html/issues/1837
1539 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1541 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1542 return mAttrs.GetAttr(aAttr);
1545 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1546 int32_t aNameSpaceID) const {
1547 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1551 * Returns the attribute map, if there is one.
1553 * @return existing attribute map or nullptr.
1555 nsDOMAttributeMap* GetAttributeMap() {
1556 nsDOMSlots* slots = GetExistingDOMSlots();
1558 return slots ? slots->mAttributeMap.get() : nullptr;
1561 void RecompileScriptEventListeners();
1564 * Get the attr info for the given namespace ID and attribute name. The
1565 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1566 * null. Note that this can only return info on attributes that actually
1567 * live on this element (and is only virtual to handle XUL prototypes). That
1568 * is, this should only be called from methods that only care about attrs
1569 * that effectively live in mAttrs.
1571 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1572 const nsAtom* aName) const {
1573 NS_ASSERTION(aName, "must have attribute name");
1574 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1575 "must have a real namespace ID!");
1577 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1578 if (index < 0) {
1579 return BorrowedAttrInfo(nullptr, nullptr);
1582 return mAttrs.AttrInfoAt(index);
1586 * Parse a string into an nsAttrValue for a CORS attribute. This
1587 * never fails. The resulting value is an enumerated value whose
1588 * GetEnumValue() returns one of the above constants.
1590 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1593 * Return the CORS mode for a given string
1595 static CORSMode StringToCORSMode(const nsAString& aValue);
1598 * Return the CORS mode for a given nsAttrValue (which may be null,
1599 * but if not should have been parsed via ParseCORSValue).
1601 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1603 nsINode* GetScopeChainParent() const override;
1606 * Locate a TextEditor rooted at this content node, if there is one.
1608 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1611 * Gets value of boolean attribute. Only works for attributes in null
1612 * namespace.
1614 * @param aAttr name of attribute.
1615 * @param aValue Boolean value of attribute.
1617 bool GetBoolAttr(nsAtom* aAttr) const { return HasAttr(aAttr); }
1620 * Sets value of boolean attribute by removing attribute or setting it to
1621 * the empty string. Only works for attributes in null namespace.
1623 * @param aAttr name of attribute.
1624 * @param aValue Boolean value of attribute.
1626 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1629 * Gets the enum value string of an attribute and using a default value if
1630 * the attribute is missing or the string is an invalid enum value.
1632 * @param aType the name of the attribute.
1633 * @param aDefault the default value if the attribute is missing or invalid.
1634 * @param aResult string corresponding to the value [out].
1636 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1637 nsAString& aResult) const;
1640 * Gets the enum value string of an attribute and using the default missing
1641 * value if the attribute is missing or the default invalid value if the
1642 * string is an invalid enum value.
1644 * @param aType the name of the attribute.
1645 * @param aDefaultMissing the default value if the attribute is missing. If
1646 null and the attribute is missing, aResult will be
1647 set to the null DOMString; this only matters for
1648 cases in which we're reflecting a nullable string.
1649 * @param aDefaultInvalid the default value if the attribute is invalid.
1650 * @param aResult string corresponding to the value [out].
1652 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1653 const char* aDefaultInvalid, nsAString& aResult) const;
1656 * Unset an attribute.
1658 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1659 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1663 * Set an attribute in the simplest way possible.
1665 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1666 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1669 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1670 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1671 aError =
1672 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1676 * Preallocate space in this element's attribute array for the given
1677 * total number of attributes.
1679 void TryReserveAttributeCount(uint32_t aAttributeCount);
1682 * Set a content attribute via a reflecting nullable string IDL
1683 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1684 * this will actually remove the content attribute.
1686 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1687 ErrorResult& aError);
1690 * Retrieve the ratio of font-size-inflated text font size to computed font
1691 * size for this element. This will query the element for its primary frame,
1692 * and then use this to get font size inflation information about the frame.
1694 * @returns The font size inflation ratio (inflated font size to uninflated
1695 * font size) for the primary frame of this element. Returns 1.0
1696 * by default if font size inflation is not enabled. Returns -1
1697 * if the element does not have a primary frame.
1699 * @note The font size inflation ratio that is returned is actually the
1700 * font size inflation data for the element's _primary frame_, not the
1701 * element itself, but for most purposes, this should be sufficient.
1703 float FontSizeInflation();
1705 void GetImplementedPseudoElement(nsAString&) const;
1707 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1708 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1711 * Helpers for .dataset. This is implemented on Element, though only some
1712 * sorts of elements expose it to JS as a .dataset property
1714 // Getter, to be called from bindings.
1715 already_AddRefed<nsDOMStringMap> Dataset();
1716 // Callback for destructor of dataset to ensure to null out our weak pointer
1717 // to it.
1718 void ClearDataset();
1720 void RegisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1721 void UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1722 void UnlinkIntersectionObservers();
1723 bool UpdateIntersectionObservation(DOMIntersectionObserver* aObserver,
1724 int32_t threshold);
1726 // A number of methods to cast to various XUL interfaces. They return a
1727 // pointer only if the element implements that interface.
1728 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1729 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1730 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1731 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1732 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1733 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1734 AsXULMultiSelectControl();
1735 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1736 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1737 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1738 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1739 already_AddRefed<nsIBrowser> AsBrowser();
1740 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1743 * Get the presentation context for this content node.
1744 * @return the presentation context
1746 enum PresContextFor { eForComposedDoc, eForUncomposedDoc };
1747 nsPresContext* GetPresContext(PresContextFor aFor);
1750 * The method focuses (or activates) element that accesskey is bound to. It is
1751 * called when accesskey is activated.
1753 * @param aKeyCausesActivation - if true then element should be activated
1754 * @param aIsTrustedEvent - if true then event that is cause of accesskey
1755 * execution is trusted.
1756 * @return an error if the element isn't able to handle the accesskey (caller
1757 * would look for the next element to handle it).
1758 * a boolean indicates whether the focus moves to the element after
1759 * the element handles the accesskey.
1761 MOZ_CAN_RUN_SCRIPT
1762 virtual Result<bool, nsresult> PerformAccesskey(bool aKeyCausesActivation,
1763 bool aIsTrustedEvent) {
1764 return Err(NS_ERROR_NOT_IMPLEMENTED);
1767 protected:
1769 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1770 * read.
1772 static const bool kFireMutationEvent = true;
1773 static const bool kDontFireMutationEvent = false;
1774 static const bool kNotifyDocumentObservers = true;
1775 static const bool kDontNotifyDocumentObservers = false;
1776 static const bool kCallAfterSetAttr = true;
1777 static const bool kDontCallAfterSetAttr = false;
1780 * Set attribute and (if needed) notify documentobservers and fire off
1781 * mutation events. This will send the AttributeChanged notification.
1782 * Callers of this method are responsible for calling AttributeWillChange,
1783 * since that needs to happen before the new attr value has been set, and
1784 * in particular before it has been parsed.
1786 * For the boolean parameters, consider using the named bools above to aid
1787 * code readability.
1789 * @param aNamespaceID namespace of attribute
1790 * @param aAttribute local-name of attribute
1791 * @param aPrefix aPrefix of attribute
1792 * @param aOldValue The old value of the attribute to use as a fallback
1793 * in the cases where the actual old value (i.e.
1794 * its current value) is !StoresOwnData() --- in which
1795 * case the current value is probably already useless.
1796 * If the current value is StoresOwnData() (or absent),
1797 * aOldValue will not be used. aOldValue will only be set
1798 * in certain circumstances (there are mutation
1799 * listeners, element is a custom element, attribute was
1800 * not previously unset). Otherwise it will be null.
1801 * @param aParsedValue parsed new value of attribute. Replaced by the
1802 * old value of the attribute. This old value is only
1803 * useful if either it or the new value is StoresOwnData.
1804 * @param aSubjectPrincipal
1805 * the principal of the scripted caller responsible for
1806 * setting the attribute, or null if no scripted caller
1807 * can be determined. A null value here does not
1808 * guarantee that there is no scripted caller, but a
1809 * non-null value does guarantee that a scripted caller
1810 * with the given principal is directly responsible for
1811 * the attribute change.
1812 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
1813 * needed if aFireMutation or aNotify is true.
1814 * @param aFireMutation should mutation-events be fired?
1815 * @param aNotify should we notify document-observers?
1816 * @param aCallAfterSetAttr should we call AfterSetAttr?
1817 * @param aComposedDocument The current composed document of the element.
1818 * @param aGuard For making sure that this is called with a
1819 * mozAutoDocUpdate instance, this is here. Specify
1820 * an instance of it which you created for the call.
1822 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
1823 nsAtom* aPrefix, const nsAttrValue* aOldValue,
1824 nsAttrValue& aParsedValue,
1825 nsIPrincipal* aSubjectPrincipal, uint8_t aModType,
1826 bool aFireMutation, bool aNotify,
1827 bool aCallAfterSetAttr, Document* aComposedDocument,
1828 const mozAutoDocUpdate& aGuard);
1831 * Scroll to a new position using behavior evaluated from CSS and
1832 * a CSSOM-View DOM method ScrollOptions dictionary. The scrolling may
1833 * be performed asynchronously or synchronously depending on the resolved
1834 * scroll-behavior.
1836 * @param aScroll Destination of scroll, in CSS pixels
1837 * @param aOptions Dictionary of options to be evaluated
1839 MOZ_CAN_RUN_SCRIPT
1840 void Scroll(const CSSIntPoint& aScroll, const ScrollOptions& aOptions);
1843 * Convert an attribute string value to attribute type based on the type of
1844 * attribute. Called by SetAttr(). Note that at the moment we only do this
1845 * for attributes in the null namespace (kNameSpaceID_None).
1847 * @param aNamespaceID the namespace of the attribute to convert
1848 * @param aAttribute the attribute to convert
1849 * @param aValue the string value to convert
1850 * @param aMaybeScriptedPrincipal the principal of the script setting the
1851 * attribute, if one can be determined, or null otherwise. As in
1852 * AfterSetAttr, a null value does not guarantee that the attribute was
1853 * not set by a scripted caller, but a non-null value guarantees that
1854 * the attribute was set by a scripted caller with the given principal.
1855 * @param aResult the nsAttrValue [OUT]
1856 * @return true if the parsing was successful, false otherwise
1858 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1859 const nsAString& aValue,
1860 nsIPrincipal* aMaybeScriptedPrincipal,
1861 nsAttrValue& aResult);
1864 * Hook that is called by Element::SetAttr to allow subclasses to
1865 * deal with attribute sets. This will only be called after we verify that
1866 * we're actually doing an attr set and will be called before
1867 * AttributeWillChange and before ParseAttribute and hence before we've set
1868 * the new value.
1870 * @param aNamespaceID the namespace of the attr being set
1871 * @param aName the localname of the attribute being set
1872 * @param aValue the value it's being set to represented as either a string or
1873 * a parsed nsAttrValue. Alternatively, if the attr is being removed it
1874 * will be null.
1875 * @param aNotify Whether we plan to notify document observers.
1877 virtual void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1878 const nsAttrValue* aValue, bool aNotify);
1881 * Hook that is called by Element::SetAttr to allow subclasses to
1882 * deal with attribute sets. This will only be called after we have called
1883 * SetAndSwapAttr (that is, after we have actually set the attr). It will
1884 * always be called under a scriptblocker.
1886 * @param aNamespaceID the namespace of the attr being set
1887 * @param aName the localname of the attribute being set
1888 * @param aValue the value it's being set to. If null, the attr is being
1889 * removed.
1890 * @param aOldValue the value that the attribute had previously. If null,
1891 * the attr was not previously set. This argument may not have the
1892 * correct value for SVG elements, or other cases in which the
1893 * attribute value doesn't store its own data
1894 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1895 * responsible for setting the attribute, or null if no scripted caller
1896 * can be determined, or the attribute is being unset. A null value
1897 * here does not guarantee that there is no scripted caller, but a
1898 * non-null value does guarantee that a scripted caller with the given
1899 * principal is directly responsible for the attribute change.
1900 * @param aNotify Whether we plan to notify document observers.
1902 virtual void AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
1903 const nsAttrValue* aValue,
1904 const nsAttrValue* aOldValue,
1905 nsIPrincipal* aMaybeScriptedPrincipal,
1906 bool aNotify);
1909 * This function shall be called just before the id attribute changes. It will
1910 * be called after BeforeSetAttr. If the attribute being changed is not the id
1911 * attribute, this function does nothing. Otherwise, it will remove the old id
1912 * from the document's id cache.
1914 * This must happen after BeforeSetAttr (rather than during) because the
1915 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
1916 * incorrectly determine whether the element had an id, the element may not be
1917 * restyled properly.
1919 * @param aNamespaceID the namespace of the attr being set
1920 * @param aName the localname of the attribute being set
1921 * @param aValue the new id value. Will be null if the id is being unset.
1923 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1924 const nsAttrValue* aValue);
1927 * This function shall be called just after the id attribute changes. It will
1928 * be called before AfterSetAttr. If the attribute being changed is not the id
1929 * attribute, this function does nothing. Otherwise, it will add the new id to
1930 * the document's id cache and properly set the ElementHasID flag.
1932 * This must happen before AfterSetAttr (rather than during) because the
1933 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
1934 * incorrectly determine whether the element now has an id, the element may
1935 * not be restyled properly.
1937 * @param aNamespaceID the namespace of the attr being set
1938 * @param aName the localname of the attribute being set
1939 * @param aValue the new id value. Will be null if the id is being unset.
1941 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1942 const nsAttrValue* aValue);
1945 * Usually, setting an attribute to the value that it already has results in
1946 * no action. However, in some cases, setting an attribute to its current
1947 * value should have the effect of, for example, forcing a reload of
1948 * network data. To address that, this function will be called in this
1949 * situation to allow the handling of such a case.
1951 * @param aNamespaceID the namespace of the attr being set
1952 * @param aName the localname of the attribute being set
1953 * @param aValue the value it's being set to represented as either a string or
1954 * a parsed nsAttrValue.
1955 * @param aNotify Whether we plan to notify document observers.
1957 virtual void OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
1958 const nsAttrValueOrString& aValue,
1959 bool aNotify);
1962 * Hook to allow subclasses to produce a different EventListenerManager if
1963 * needed for attachment of attribute-defined handlers
1965 virtual EventListenerManager* GetEventListenerManagerForAttr(
1966 nsAtom* aAttrName, bool* aDefer);
1969 * Internal hook for converting an attribute name-string to nsAttrName in
1970 * case there is such existing attribute. aNameToUse can be passed to get
1971 * name which was used for looking for the attribute (lowercase in HTML).
1973 const nsAttrName* InternalGetAttrNameFromQName(
1974 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
1976 virtual Element* GetNameSpaceElement() override { return this; }
1978 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
1979 const nsAString& aLocalName);
1981 inline void RegisterActivityObserver();
1982 inline void UnregisterActivityObserver();
1985 * Add/remove this element to the documents id cache
1987 void AddToIdTable(nsAtom* aId);
1988 void RemoveFromIdTable();
1991 * Functions to carry out event default actions for links of all types
1992 * (HTML links, XLinks, SVG "XLinks", etc.)
1996 * Check that we meet the conditions to handle a link event
1997 * and that we are actually on a link.
1999 * @param aVisitor event visitor
2000 * @return true if we can handle the link event, false otherwise
2002 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor) const;
2005 * Handle status bar updates before they can be cancelled.
2007 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
2009 void DispatchChromeOnlyLinkClickEvent(EventChainPostVisitor& aVisitor);
2012 * Handle default actions for link event if the event isn't consumed yet.
2014 MOZ_CAN_RUN_SCRIPT
2015 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
2017 public:
2019 * Check if this element is a link. This matches the CSS definition of the
2020 * :any-link pseudo-class.
2022 bool IsLink() const {
2023 return mState.HasAtLeastOneOfStates(ElementState::VISITED |
2024 ElementState::UNVISITED);
2028 * Get a pointer to the full href URI (fully resolved and canonicalized, since
2029 * it's an nsIURI object) for link elements.
2031 * @return A pointer to the URI or null if the element is not a link, or it
2032 * has no HREF attribute, or the HREF attribute is an invalid URI.
2034 virtual already_AddRefed<nsIURI> GetHrefURI() const { return nullptr; }
2037 * Get the target of this link element. Consumers should established that
2038 * this element is a link (probably using IsLink) before calling this
2039 * function (or else why call it?)
2041 * Note: for HTML this gets the value of the 'target' attribute; for XLink
2042 * this gets the value of the xlink:_moz_target attribute, or failing that,
2043 * the value of xlink:show, converted to a suitably equivalent named target
2044 * (e.g. _blank).
2046 virtual void GetLinkTarget(nsAString& aTarget);
2048 virtual bool Translate() const;
2050 protected:
2051 enum class ReparseAttributes { No, Yes };
2053 * Copy attributes and state to another element
2054 * @param aDest the object to copy to
2056 nsresult CopyInnerTo(Element* aDest,
2057 ReparseAttributes = ReparseAttributes::Yes);
2060 * Some event handler content attributes have a different name (e.g. different
2061 * case) from the actual event name. This function takes an event handler
2062 * content attribute name and returns the corresponding event name, to be used
2063 * for adding the actual event listener.
2065 virtual nsAtom* GetEventNameForAttr(nsAtom* aAttr);
2068 * Register/unregister this element to accesskey map if it supports accesskey.
2070 virtual void RegUnRegAccessKey(bool aDoReg);
2072 private:
2073 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
2074 void AssertInvariantsOnNodeInfoChange();
2075 #endif
2078 * Slow path for GetClasses, this should only be called for SVG elements.
2080 const nsAttrValue* GetSVGAnimatedClass() const;
2083 * Get this element's client area rect in app units.
2084 * @return the frame's client area
2086 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
2089 * GetCustomInterface is somewhat like a GetInterface, but it is expected
2090 * that the implementation is provided by a custom element or via the
2091 * the XBL implements keyword. To use this, create a public method that
2092 * wraps a call to GetCustomInterface.
2094 template <class T>
2095 void GetCustomInterface(nsGetterAddRefs<T> aResult);
2097 // Prevent people from doing pointless checks/casts on Element instances.
2098 void IsElement() = delete;
2099 void AsElement() = delete;
2101 // Data members
2102 ElementState mState;
2103 // Per-node data managed by Servo.
2105 // There should not be data on nodes that are not in the flattened tree, or
2106 // descendants of display: none elements.
2107 mozilla::RustCell<ServoNodeData*> mServoData;
2109 protected:
2110 // Array containing all attributes for this element
2111 AttrArray mAttrs;
2114 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
2116 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2117 const nsAtom* aName) const {
2118 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2119 MOZ_ASSERT(aName, "Must have attribute name");
2121 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2122 return val && !val->IsEmptyString();
2125 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2126 const nsAString& aValue,
2127 nsCaseTreatment aCaseSensitive) const {
2128 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2131 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2132 const nsAtom* aValue,
2133 nsCaseTreatment aCaseSensitive) const {
2134 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2137 } // namespace dom
2138 } // namespace mozilla
2140 NON_VIRTUAL_ADDREF_RELEASE(mozilla::dom::Element)
2142 inline mozilla::dom::Element* nsINode::AsElement() {
2143 MOZ_ASSERT(IsElement());
2144 return static_cast<mozilla::dom::Element*>(this);
2147 inline const mozilla::dom::Element* nsINode::AsElement() const {
2148 MOZ_ASSERT(IsElement());
2149 return static_cast<const mozilla::dom::Element*>(this);
2152 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2153 return mozilla::dom::Element::FromNodeOrNull(mParent);
2156 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2157 nsIContent* previousSibling = GetPreviousSibling();
2158 while (previousSibling) {
2159 if (previousSibling->IsElement()) {
2160 return previousSibling->AsElement();
2162 previousSibling = previousSibling->GetPreviousSibling();
2165 return nullptr;
2168 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2169 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2170 : GetParentElement();
2173 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2174 nsIContent* nextSibling = GetNextSibling();
2175 while (nextSibling) {
2176 if (nextSibling->IsElement()) {
2177 return nextSibling->AsElement();
2179 nextSibling = nextSibling->GetNextSibling();
2182 return nullptr;
2186 * Macros to implement Clone(). _elementName is the class for which to implement
2187 * Clone.
2189 #define NS_IMPL_ELEMENT_CLONE(_elementName) \
2190 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2191 nsINode** aResult) const { \
2192 *aResult = nullptr; \
2193 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2194 auto* nim = ni->NodeInfoManager(); \
2195 RefPtr<_elementName> it = new (nim) _elementName(ni.forget()); \
2196 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2197 if (NS_SUCCEEDED(rv)) { \
2198 it.forget(aResult); \
2201 return rv; \
2204 #define EXPAND(...) __VA_ARGS__
2205 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, extra_args_) \
2206 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2207 nsINode** aResult) const { \
2208 *aResult = nullptr; \
2209 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2210 auto* nim = ni->NodeInfoManager(); \
2211 RefPtr<_elementName> it = \
2212 new (nim) _elementName(ni.forget() EXPAND extra_args_); \
2213 nsresult rv = it->Init(); \
2214 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2215 if (NS_FAILED(rv2)) { \
2216 rv = rv2; \
2218 if (NS_SUCCEEDED(rv)) { \
2219 it.forget(aResult); \
2222 return rv; \
2225 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName) \
2226 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, ())
2227 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2228 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, (, NOT_FROM_PARSER))
2230 #endif // mozilla_dom_Element_h__