Backed out 6 changesets (bug 1843477) for causing hazards failures. CLOSED TREE
[gecko.git] / dom / base / Element.h
blobea1cc883ed7143181e4b04ebdb5567ae9b59a8e0
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_DOMSTRING_ATTR(method, attr) \
221 void Get##method(nsAString& aValue) const { \
222 GetAttr(nsGkAtoms::attr, aValue); \
224 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
225 SetAttr(nsGkAtoms::attr, aValue, aRv); \
228 class Element : public FragmentOrElement {
229 public:
230 #ifdef MOZILLA_INTERNAL_API
231 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
232 : FragmentOrElement(std::move(aNodeInfo)),
233 mState(ElementState::READONLY | ElementState::DEFINED) {
234 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
235 "Bad NodeType in aNodeInfo");
236 SetIsElement();
239 ~Element() {
240 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
243 #endif // MOZILLA_INTERNAL_API
245 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
247 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
249 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
251 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
254 * Method to get the full state of this element. See dom/base/rust/lib.rs for
255 * the possible bits that could be set here.
257 ElementState State() const { return mState; }
260 * Returns the current disabled state of the element.
262 bool IsDisabled() const { return State().HasState(ElementState::DISABLED); }
263 bool IsReadOnly() const { return State().HasState(ElementState::READONLY); }
264 bool IsDisabledOrReadOnly() const {
265 return State().HasAtLeastOneOfStates(ElementState::DISABLED |
266 ElementState::READONLY);
269 virtual int32_t TabIndexDefault() { return -1; }
272 * Get tabIndex of this element. If not found, return TabIndexDefault.
274 int32_t TabIndex();
277 * Get the parsed value of tabindex attribute.
279 Maybe<int32_t> GetTabIndexAttrValue();
282 * Set tabIndex value to this element.
284 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
287 * Sets the ShadowRoot binding for this element. The contents of the
288 * binding is rendered in place of this node's children.
290 * @param aShadowRoot The ShadowRoot to be bound to this element.
292 void SetShadowRoot(ShadowRoot* aShadowRoot);
294 void SetLastRememberedBSize(float aBSize);
295 void SetLastRememberedISize(float aISize);
296 void RemoveLastRememberedBSize();
297 void RemoveLastRememberedISize();
300 * Make focus on this element.
302 // TODO: Convert Focus() to MOZ_CAN_RUN_SCRIPT and get rid of the
303 // kungFuDeathGrip in it.
304 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Focus(const FocusOptions& aOptions,
305 const CallerType aCallerType,
306 ErrorResult& aError);
309 * Show blur and clear focus.
311 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Blur(mozilla::ErrorResult& aError);
314 * The style state of this element. This is the real state of the element
315 * with any style locks applied for pseudo-class inspecting.
317 ElementState StyleState() const {
318 if (!HasLockedStyleStates()) {
319 return mState;
321 return StyleStateFromLocks();
325 * StyleStateLocks is used to specify which event states should be locked,
326 * and whether they should be locked to on or off.
328 struct StyleStateLocks {
329 // mLocks tracks which event states should be locked.
330 ElementState mLocks;
331 // mValues tracks if the locked state should be on or off.
332 ElementState mValues;
336 * The style state locks applied to this element.
338 StyleStateLocks LockedStyleStates() const;
341 * Add a style state lock on this element.
342 * aEnabled is the value to lock the given state bits to.
344 void LockStyleStates(ElementState aStates, bool aEnabled);
347 * Remove a style state lock on this element.
349 void UnlockStyleStates(ElementState aStates);
352 * Clear all style state locks on this element.
354 void ClearStyleStateLocks();
357 * Accessors for the state of our dir attribute.
359 bool HasDirAuto() const {
360 return State().HasState(ElementState::HAS_DIR_ATTR_LIKE_AUTO);
364 * Elements with dir="rtl" or dir="ltr".
366 bool HasFixedDir() const {
367 return State().HasAtLeastOneOfStates(ElementState::HAS_DIR_ATTR_LTR |
368 ElementState::HAS_DIR_ATTR_RTL);
372 * Get the inline style declaration, if any, for this element.
374 DeclarationBlock* GetInlineStyleDeclaration() const;
377 * Get the mapped attributes, if any, for this element.
379 StyleLockedDeclarationBlock* GetMappedAttributeStyle() const {
380 return mAttrs.GetMappedDeclarationBlock();
383 bool IsPendingMappedAttributeEvaluation() const {
384 return mAttrs.IsPendingMappedAttributeEvaluation();
387 void SetMappedDeclarationBlock(already_AddRefed<StyleLockedDeclarationBlock>);
390 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
391 * so that the element implementation can access the old style attribute
392 * value.
394 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
397 * Set the inline style declaration for this element.
399 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
400 MutationClosureData& aData);
403 * Get the SMIL override style declaration for this element. If the
404 * rule hasn't been created, this method simply returns null.
406 DeclarationBlock* GetSMILOverrideStyleDeclaration();
409 * Set the SMIL override style declaration for this element. This method will
410 * notify the document's pres context, so that the style changes will be
411 * noticed.
413 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
416 * Returns a new SMILAttr that allows the caller to animate the given
417 * attribute on this element.
419 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
420 nsAtom* aName);
423 * Get the SMIL override style for this element. This is a style declaration
424 * that is applied *after* the inline style, and it can be used e.g. to store
425 * animated style values.
427 * Note: This method is analogous to the 'GetStyle' method in
428 * nsGenericHTMLElement and nsStyledElement.
430 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
433 * Returns if the element is labelable as per HTML specification.
435 virtual bool IsLabelable() const;
438 * Returns if the element is interactive content as per HTML specification.
440 virtual bool IsInteractiveHTMLContent() const;
443 * Returns |this| as an nsIMozBrowserFrame* if the element is a frame or
444 * iframe element.
446 * We have this method, rather than using QI, so that we can use it during
447 * the servo traversal, where we can't QI DOM nodes because of non-thread-safe
448 * refcounts.
450 virtual nsIMozBrowserFrame* GetAsMozBrowserFrame() { return nullptr; }
453 * Is the attribute named aAttribute a mapped attribute?
455 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
457 nsresult BindToTree(BindContext&, nsINode& aParent) override;
458 void UnbindFromTree(bool aNullParent = true) override;
460 virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
461 static void MapNoAttributesInto(mozilla::MappedDeclarationsBuilder&);
464 * Get a hint that tells the style system what to do when
465 * an attribute on this node changes, if something needs to happen
466 * in response to the change *other* than the result of what is
467 * mapped into style data via any type of style rule.
469 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
470 int32_t aModType) const;
472 inline Directionality GetDirectionality() const {
473 ElementState state = State();
474 if (state.HasState(ElementState::RTL)) {
475 return eDir_RTL;
477 if (state.HasState(ElementState::LTR)) {
478 return eDir_LTR;
480 return eDir_NotSet;
483 inline void SetDirectionality(Directionality aDir, bool aNotify) {
484 AutoStateChangeNotifier notifier(*this, aNotify);
485 RemoveStatesSilently(ElementState::DIR_STATES);
486 switch (aDir) {
487 case eDir_RTL:
488 AddStatesSilently(ElementState::RTL);
489 break;
490 case eDir_LTR:
491 AddStatesSilently(ElementState::LTR);
492 break;
493 default:
494 break;
498 Directionality GetComputedDirectionality() const;
500 static const uint32_t kAllServoDescendantBits =
501 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO |
502 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO |
503 NODE_DESCENDANTS_NEED_FRAMES;
506 * Notes that something in the given subtree of this element needs dirtying,
507 * and that all the relevant dirty bits have already been propagated up to the
508 * element.
510 * This is important because `NoteDirtyForServo` uses the dirty bits to reason
511 * about the shape of the tree, so we can't just call into there.
513 void NoteDirtySubtreeForServo();
515 void NoteDirtyForServo();
516 void NoteAnimationOnlyDirtyForServo();
517 void NoteDescendantsNeedFramesForServo();
519 bool HasDirtyDescendantsForServo() const {
520 return HasFlag(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
523 void SetHasDirtyDescendantsForServo() {
524 SetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
527 void UnsetHasDirtyDescendantsForServo() {
528 UnsetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
531 bool HasAnimationOnlyDirtyDescendantsForServo() const {
532 return HasFlag(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
535 void SetHasAnimationOnlyDirtyDescendantsForServo() {
536 SetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
539 void UnsetHasAnimationOnlyDirtyDescendantsForServo() {
540 UnsetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
543 bool HasServoData() const { return !!mServoData.Get(); }
545 void ClearServoData() { ClearServoData(GetComposedDoc()); }
546 void ClearServoData(Document* aDocument);
548 PopoverData* GetPopoverData() const {
549 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
550 return slots ? slots->mPopoverData.get() : nullptr;
553 PopoverData& EnsurePopoverData() {
554 if (auto* popoverData = GetPopoverData()) {
555 return *popoverData;
557 return CreatePopoverData();
560 bool IsAutoPopover() const;
561 bool IsPopoverOpen() const;
564 * https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
566 Element* GetTopmostPopoverAncestor(const Element* aInvoker) const;
568 ElementAnimationData* GetAnimationData() const {
569 if (!MayHaveAnimations()) {
570 return nullptr;
572 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
573 return slots ? slots->mAnimations.get() : nullptr;
576 ElementAnimationData& EnsureAnimationData() {
577 if (auto* anim = GetAnimationData()) {
578 return *anim;
580 return CreateAnimationData();
583 private:
584 ElementAnimationData& CreateAnimationData();
585 PopoverData& CreatePopoverData();
587 public:
588 void ClearPopoverData();
591 * Gets the custom element data used by web components custom element.
592 * Custom element data is created at the first attempt to enqueue a callback.
594 * @return The custom element data or null if none.
596 CustomElementData* GetCustomElementData() const {
597 if (!HasCustomElementData()) {
598 return nullptr;
601 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
602 return slots ? slots->mCustomElementData.get() : nullptr;
606 * Sets the custom element data, ownership of the
607 * callback data is taken by this element.
609 * @param aData The custom element data.
611 void SetCustomElementData(UniquePtr<CustomElementData> aData);
614 * Gets the custom element definition used by web components custom element.
616 * @return The custom element definition or null if element is not a custom
617 * element or custom element is not defined yet.
619 CustomElementDefinition* GetCustomElementDefinition() const;
622 * Sets the custom element definition, called when custom element is created
623 * or upgraded.
625 * @param aDefinition The custom element definition.
627 virtual void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
629 const AttrArray& GetAttrs() const { return mAttrs; }
631 void SetDefined(bool aSet) { SetStates(ElementState::DEFINED, aSet); }
633 // AccessibilityRole
634 REFLECT_DOMSTRING_ATTR(Role, role)
636 // AriaAttributes
637 REFLECT_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
638 REFLECT_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
639 REFLECT_DOMSTRING_ATTR(AriaBusy, aria_busy)
640 REFLECT_DOMSTRING_ATTR(AriaChecked, aria_checked)
641 REFLECT_DOMSTRING_ATTR(AriaColCount, aria_colcount)
642 REFLECT_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
643 REFLECT_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
644 REFLECT_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
645 REFLECT_DOMSTRING_ATTR(AriaCurrent, aria_current)
646 REFLECT_DOMSTRING_ATTR(AriaDescription, aria_description)
647 REFLECT_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
648 REFLECT_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
649 REFLECT_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
650 REFLECT_DOMSTRING_ATTR(AriaHidden, aria_hidden)
651 REFLECT_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
652 REFLECT_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
653 REFLECT_DOMSTRING_ATTR(AriaLabel, aria_label)
654 REFLECT_DOMSTRING_ATTR(AriaLevel, aria_level)
655 REFLECT_DOMSTRING_ATTR(AriaLive, aria_live)
656 REFLECT_DOMSTRING_ATTR(AriaModal, aria_modal)
657 REFLECT_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
658 REFLECT_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
659 REFLECT_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
660 REFLECT_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
661 REFLECT_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
662 REFLECT_DOMSTRING_ATTR(AriaPressed, aria_pressed)
663 REFLECT_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
664 REFLECT_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
665 REFLECT_DOMSTRING_ATTR(AriaRequired, aria_required)
666 REFLECT_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
667 REFLECT_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
668 REFLECT_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
669 REFLECT_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
670 REFLECT_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
671 REFLECT_DOMSTRING_ATTR(AriaSelected, aria_selected)
672 REFLECT_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
673 REFLECT_DOMSTRING_ATTR(AriaSort, aria_sort)
674 REFLECT_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
675 REFLECT_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
676 REFLECT_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
677 REFLECT_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
679 protected:
680 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
681 ErrorResult& aError);
683 public:
684 MOZ_CAN_RUN_SCRIPT
685 nsIScrollableFrame* GetScrollFrame(nsIFrame** aStyledFrame = nullptr,
686 FlushType aFlushType = FlushType::Layout);
688 private:
689 // Style state computed from element's state and style locks.
690 ElementState StyleStateFromLocks() const;
692 void NotifyStateChange(ElementState aStates);
693 void NotifyStyleStateChange(ElementState aStates);
695 public:
696 struct AutoStateChangeNotifier {
697 AutoStateChangeNotifier(Element& aElement, bool aNotify)
698 : mElement(aElement), mOldState(aElement.State()), mNotify(aNotify) {}
699 ~AutoStateChangeNotifier() {
700 if (!mNotify) {
701 return;
703 ElementState newState = mElement.State();
704 if (mOldState != newState) {
705 mElement.NotifyStateChange(mOldState ^ newState);
709 private:
710 Element& mElement;
711 const ElementState mOldState;
712 const bool mNotify;
715 // Method to add state bits. This should be called from subclass constructors
716 // to set up our event state correctly at construction time, and other places
717 // where we don't want to notify a state change, or there's an
718 // AutoStateChangeNotifier on the stack.
719 void AddStatesSilently(ElementState aStates) { mState |= aStates; }
720 // Method to remove state bits. This should be called from subclass
721 // constructors to set up our event state correctly at construction time and
722 // other places where we don't want to notify a state change.
723 void RemoveStatesSilently(ElementState aStates) { mState &= ~aStates; }
724 // Methods to add state bits, potentially notifying. These will handle setting
725 // up script blockers when they notify, so no need to do it in the callers
726 // unless desired. States passed here must only be those in
727 // EXTERNALLY_MANAGED_STATES.
728 void AddStates(ElementState aStates, bool aNotify = true) {
729 ElementState old = mState;
730 AddStatesSilently(aStates);
731 if (aNotify && old != mState) {
732 NotifyStateChange(old ^ mState);
735 void RemoveStates(ElementState aStates, bool aNotify = true) {
736 ElementState old = mState;
737 RemoveStatesSilently(aStates);
738 if (aNotify && old != mState) {
739 NotifyStateChange(old ^ mState);
742 void SetStates(ElementState aStates, bool aSet, bool aNotify = true) {
743 if (aSet) {
744 AddStates(aStates, aNotify);
745 } else {
746 RemoveStates(aStates, aNotify);
749 void ToggleStates(ElementState aStates, bool aNotify) {
750 mState ^= aStates;
751 if (aNotify) {
752 NotifyStateChange(aStates);
756 void UpdateEditableState(bool aNotify) override;
757 // Makes sure that the READONLY/READWRITE flags are in sync.
758 void UpdateReadOnlyState(bool aNotify);
759 // Form controls and non-form controls should have different :read-only /
760 // :read-write behavior. This is what effectively controls it.
761 virtual bool IsReadOnlyInternal() const;
764 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
765 * with that name exists. This method is intended for character case
766 * conversion if the content object is case insensitive (e.g. HTML). Returns
767 * the nodeinfo of the attribute with the specified name if one exists or
768 * null otherwise.
770 * @param aStr the unparsed attribute string
771 * @return the node info. May be nullptr.
773 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
774 const nsAString& aStr) const;
777 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
778 * is true or there are mutation listeners that must be triggered, the
779 * attribute is currently set, and the new value that is about to be set is
780 * different to the current value. As a perf optimization the new and old
781 * values will not actually be compared if we aren't notifying and we don't
782 * have mutation listeners (in which case it's cheap to just return false
783 * and let the caller go ahead and set the value).
784 * @param aOldValue [out] Set to the old value of the attribute, but only if
785 * there are event listeners. If set, the type of aOldValue will be either
786 * nsAttrValue::eString or nsAttrValue::eAtom.
787 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
788 * MutationEvent_Binding::ADDITION, but only if this helper returns true
789 * @param aHasListeners [out] Set to true if there are mutation event
790 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
791 * @param aOldValueSet [out] Indicates whether an old attribute value has been
792 * stored in aOldValue. The bool will be set to true if a value was stored.
794 bool MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
795 const nsAtom* aPrefix,
796 const nsAttrValueOrString& aValue, bool aNotify,
797 nsAttrValue& aOldValue, uint8_t* aModType,
798 bool* aHasListeners, bool* aOldValueSet);
801 * Notifies mutation listeners if aNotify is true, there are mutation
802 * listeners, and the attribute value is changing.
804 * @param aNamespaceID The namespace of the attribute
805 * @param aName The local name of the attribute
806 * @param aPrefix The prefix of the attribute
807 * @param aValue The value that the attribute is being changed to
808 * @param aNotify If true, mutation listeners will be notified if they exist
809 * and the attribute value is changing
810 * @param aOldValue [out] Set to the old value of the attribute, but only if
811 * there are event listeners. If set, the type of aOldValue will be either
812 * nsAttrValue::eString or nsAttrValue::eAtom.
813 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
814 * MutationEvent_Binding::ADDITION, but only if this helper returns true
815 * @param aHasListeners [out] Set to true if there are mutation event
816 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
817 * @param aOldValueSet [out] Indicates whether an old attribute value has been
818 * stored in aOldValue. The bool will be set to true if a value was stored.
820 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
821 nsAtom* aPrefix,
822 const nsAttrValueOrString& aValue, bool aNotify,
823 nsAttrValue& aOldValue, uint8_t* aModType,
824 bool* aHasListeners, bool* aOldValueSet);
827 * Sets the class attribute.
828 * Assumes that we are not notifying and that the attribute hasn't been
829 * set previously.
831 nsresult SetClassAttrFromParser(nsAtom* aValue);
833 // aParsedValue receives the old value of the attribute. That's useful if
834 // either the input or output value of aParsedValue is StoresOwnData.
835 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
836 nsAttrValue& aParsedValue, bool aNotify);
838 * Get the current value of the attribute. This returns a form that is
839 * suitable for passing back into SetAttr.
841 * @param aNameSpaceID the namespace of the attr (defaults to
842 kNameSpaceID_None in the overload that omits this arg)
843 * @param aName the name of the attr
844 * @param aResult the value (may legitimately be the empty string) [OUT]
845 * @returns true if the attribute was set (even when set to empty string)
846 * false when not set.
847 * GetAttr is not inlined on purpose, to keep down codesize from all the
848 * inlined nsAttrValue bits for C++ callers.
850 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
851 nsAString& aResult) const;
852 bool GetAttr(const nsAtom* aName, nsAString& aResult) const;
855 * Determine if an attribute has been set (empty string or otherwise).
857 * @param aNameSpaceId the namespace id of the attribute (defaults to
858 kNameSpaceID_None in the overload that omits this arg)
859 * @param aAttr the attribute name
860 * @return whether an attribute exists
862 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
863 return mAttrs.HasAttr(aNameSpaceID, aName);
866 bool HasAttr(const nsAtom* aAttr) const { return mAttrs.HasAttr(aAttr); }
869 * Determine if an attribute has been set to a non-empty string value. If the
870 * attribute is not set at all, this will return false.
872 * @param aNameSpaceId the namespace id of the attribute (defaults to
873 * kNameSpaceID_None in the overload that omits this arg)
874 * @param aAttr the attribute name
876 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
878 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
879 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
883 * Test whether this Element's given attribute has the given value. If the
884 * attribute is not set at all, this will return false.
886 * @param aNameSpaceID The namespace ID of the attribute. Must not
887 * be kNameSpaceID_Unknown.
888 * @param aName The name atom of the attribute. Must not be null.
889 * @param aValue The value to compare to.
890 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
892 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
893 const nsAString& aValue,
894 nsCaseTreatment aCaseSensitive) const;
897 * Test whether this Element's given attribute has the given value. If the
898 * attribute is not set at all, this will return false.
900 * @param aNameSpaceID The namespace ID of the attribute. Must not
901 * be kNameSpaceID_Unknown.
902 * @param aName The name atom of the attribute. Must not be null.
903 * @param aValue The value to compare to. Must not be null.
904 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
906 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
907 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
910 * Check whether this Element's given attribute has one of a given list of
911 * values. If there is a match, we return the index in the list of the first
912 * matching value. If there was no attribute at all, then we return
913 * ATTR_MISSING. If there was an attribute but it didn't match, we return
914 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
916 * @param aNameSpaceID The namespace ID of the attribute. Must not
917 * be kNameSpaceID_Unknown.
918 * @param aName The name atom of the attribute. Must not be null.
919 * @param aValues a nullptr-terminated array of pointers to atom values to
920 * test against.
921 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
922 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
923 * indicating the first value of aValues that matched
925 using AttrValuesArray = AttrArray::AttrValuesArray;
926 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
927 AttrArray::AttrValuesArray* aValues,
928 nsCaseTreatment aCaseSensitive) const;
931 * Set attribute values. All attribute values are assumed to have a
932 * canonical string representation that can be used for these
933 * methods. The SetAttr method is assumed to perform a translation
934 * of the canonical form into the underlying content specific
935 * form.
937 * @param aNameSpaceID the namespace of the attribute
938 * @param aName the name of the attribute
939 * @param aValue the value to set
940 * @param aNotify specifies how whether or not the document should be
941 * notified of the attribute change.
943 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
944 bool aNotify) {
945 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
947 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
948 const nsAString& aValue, bool aNotify) {
949 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
951 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
952 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
953 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
954 aNotify);
958 * Set attribute values. All attribute values are assumed to have a
959 * canonical String representation that can be used for these
960 * methods. The SetAttr method is assumed to perform a translation
961 * of the canonical form into the underlying content specific
962 * form.
964 * @param aNameSpaceID the namespace of the attribute
965 * @param aName the name of the attribute
966 * @param aPrefix the prefix of the attribute
967 * @param aValue the value to set
968 * @param aMaybeScriptedPrincipal the principal of the scripted caller
969 * responsible for setting the attribute, or null if no scripted caller can be
970 * determined. A null value here does not guarantee that there is no
971 * scripted caller, but a non-null value does guarantee that a scripted
972 * caller with the given principal is directly responsible for the
973 * attribute change.
974 * @param aNotify specifies how whether or not the document should be
975 * notified of the attribute change.
977 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
978 const nsAString& aValue,
979 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
982 * Remove an attribute so that it is no longer explicitly specified.
984 * @param aNameSpaceID the namespace id of the attribute
985 * @param aName the name of the attribute to unset
986 * @param aNotify specifies whether or not the document should be
987 * notified of the attribute change
989 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aName, bool aNotify);
992 * Get the namespace / name / prefix of a given attribute.
994 * @param aIndex the index of the attribute name
995 * @returns The name at the given index, or null if the index is
996 * out-of-bounds.
997 * @note The document returned by NodeInfo()->GetDocument() (if one is
998 * present) is *not* necessarily the owner document of the element.
999 * @note The pointer returned by this function is only valid until the
1000 * next call of either GetAttrNameAt or SetAttr on the element.
1002 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
1003 return mAttrs.GetSafeAttrNameAt(aIndex);
1007 * Same as above, but does not do out-of-bounds checks!
1009 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
1010 return mAttrs.AttrNameAt(aIndex);
1014 * Gets the attribute info (name and value) for this element at a given index.
1016 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
1017 if (aIndex >= mAttrs.AttrCount()) {
1018 return BorrowedAttrInfo(nullptr, nullptr);
1021 return mAttrs.AttrInfoAt(aIndex);
1025 * Get the number of all specified attributes.
1027 * @return the number of attributes
1029 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
1032 * Get the class list of this element (this corresponds to the value of the
1033 * class attribute). This may be null if there are no classes, but that's not
1034 * guaranteed (e.g. we could have class="").
1036 const nsAttrValue* GetClasses() const {
1037 if (!MayHaveClass()) {
1038 return nullptr;
1041 if (IsSVGElement()) {
1042 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
1043 return value;
1047 return GetParsedAttr(nsGkAtoms::_class);
1050 #ifdef MOZ_DOM_LIST
1051 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
1052 List(out, aIndent, ""_ns);
1054 virtual void DumpContent(FILE* out, int32_t aIndent,
1055 bool aDumpAll) const override;
1056 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1057 void ListAttributes(FILE* out) const;
1058 #endif
1061 * Append to aOutDescription a string describing the element and its
1062 * attributes.
1063 * If aShort is true, only the id and class attributes will be listed.
1065 void Describe(nsAString& aOutDescription, bool aShort = false) const;
1068 * Attribute Mapping Helpers
1070 struct MappedAttributeEntry {
1071 const nsStaticAtom* const attribute;
1075 * A common method where you can just pass in a list of maps to check
1076 * for attribute dependence. Most implementations of
1077 * IsAttributeMapped should use this function as a default
1078 * handler.
1080 template <size_t N>
1081 static bool FindAttributeDependence(
1082 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1083 return FindAttributeDependence(aAttribute, aMaps, N);
1086 static nsStaticAtom* const* HTMLSVGPropertiesToTraverseAndUnlink();
1088 private:
1089 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1091 static bool FindAttributeDependence(const nsAtom* aAttribute,
1092 const MappedAttributeEntry* const aMaps[],
1093 uint32_t aMapCount);
1095 protected:
1096 inline bool GetAttr(const nsAtom* aName, DOMString& aResult) const {
1097 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1098 const nsAttrValue* val = mAttrs.GetAttr(aName);
1099 if (!val) {
1100 return false; // DOMString comes pre-emptied.
1102 val->ToString(aResult);
1103 return true;
1106 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1107 DOMString& aResult) const {
1108 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1109 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1110 if (!val) {
1111 return false; // DOMString comes pre-emptied.
1113 val->ToString(aResult);
1114 return true;
1117 public:
1118 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1120 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1121 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1122 const nsAttrValue* val = mAttrs.GetAttr(aName);
1123 if (val) {
1124 val->ToString(aResult);
1125 return true;
1127 // else DOMString comes pre-emptied.
1128 return false;
1131 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1132 void GetId(nsAString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1133 void GetId(DOMString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1134 void SetId(const nsAString& aId) {
1135 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1137 void GetClassName(nsAString& aClassName) {
1138 GetAttr(nsGkAtoms::_class, aClassName);
1140 void GetClassName(DOMString& aClassName) {
1141 GetAttr(nsGkAtoms::_class, aClassName);
1143 void SetClassName(const nsAString& aClassName) {
1144 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1147 nsDOMTokenList* ClassList();
1148 nsDOMTokenList* Part();
1150 nsDOMAttributeMap* Attributes();
1152 void GetAttributeNames(nsTArray<nsString>& aResult);
1154 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1155 DOMString str;
1156 GetAttribute(aName, str);
1157 str.ToString(aReturn);
1160 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1161 void GetAttributeNS(const nsAString& aNamespaceURI,
1162 const nsAString& aLocalName, nsAString& aReturn);
1163 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1164 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1165 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1166 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1167 void SetAttributeNS(const nsAString& aNamespaceURI,
1168 const nsAString& aLocalName, const nsAString& aValue,
1169 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1170 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1171 ErrorResult& aError) {
1172 SetAttribute(aName, aValue, nullptr, aError);
1175 * This method creates a principal that subsumes this element's NodePrincipal
1176 * and which has flags set for elevated permissions that devtools needs to
1177 * operate on this element. The principal returned by this method is used by
1178 * various devtools methods to permit otherwise blocked operations, without
1179 * changing any other restrictions the NodePrincipal might have.
1181 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1182 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1183 ErrorResult& aError);
1184 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1185 const nsAString& aLocalName,
1186 const nsAString& aValue, ErrorResult& aError);
1188 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1189 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1190 const nsAString& aLocalName, ErrorResult& aError);
1191 bool HasAttribute(const nsAString& aName) const {
1192 return InternalGetAttrNameFromQName(aName) != nullptr;
1194 bool HasAttributeNS(const nsAString& aNamespaceURI,
1195 const nsAString& aLocalName) const;
1196 bool HasAttributes() const { return HasAttrs(); }
1197 Element* Closest(const nsACString& aSelector, ErrorResult& aResult);
1198 bool Matches(const nsACString& aSelector, ErrorResult& aError);
1199 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1200 const nsAString& aQualifiedName);
1201 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1202 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1203 ErrorResult& aError);
1204 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1205 const nsAString& aClassNames);
1208 * Returns attribute associated element for the given attribute name, see
1209 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#attr-associated-element
1211 Element* GetAttrAssociatedElement(nsAtom* aAttr) const;
1214 * Sets an attribute element for the given attribute.
1215 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
1217 void ExplicitlySetAttrElement(nsAtom* aAttr, Element* aElement);
1219 PseudoStyleType GetPseudoElementType() const {
1220 nsresult rv = NS_OK;
1221 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1222 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1223 return PseudoStyleType::NotPseudo;
1225 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1228 void SetPseudoElementType(PseudoStyleType aPseudo) {
1229 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1230 "Need to be able to store this in a void*");
1231 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1232 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1236 * Return an array of all elements in the subtree rooted at this
1237 * element that have grid container frames. This does not include
1238 * pseudo-elements.
1240 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1243 * Provide a direct way to determine if this Element has visible
1244 * scrollbars. Flushes layout.
1246 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1248 private:
1250 * Implement the algorithm specified at
1251 * https://dom.spec.whatwg.org/#insert-adjacent for both
1252 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1254 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1255 ErrorResult& aError);
1257 public:
1258 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1259 ErrorResult& aError);
1261 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1262 ErrorResult& aError);
1264 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError);
1265 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError);
1266 bool HasPointerCapture(long aPointerId);
1267 void SetCapture(bool aRetargetToElement);
1269 void SetCaptureAlways(bool aRetargetToElement);
1271 void ReleaseCapture();
1273 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1274 void RequestPointerLock(CallerType aCallerType);
1275 Attr* GetAttributeNode(const nsAString& aName);
1276 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1277 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1278 ErrorResult& aError);
1279 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1280 const nsAString& aLocalName);
1281 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1282 ErrorResult& aError);
1284 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1285 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1287 // Shadow DOM v1
1288 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1289 ErrorResult& aError);
1290 bool CanAttachShadowDOM() const;
1292 enum class DelegatesFocus : bool { No, Yes };
1294 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1295 ShadowRootMode aMode, DelegatesFocus = DelegatesFocus::No,
1296 SlotAssignmentMode aSlotAssignmentMode = SlotAssignmentMode::Named);
1298 // Attach UA Shadow Root if it is not attached.
1299 enum class NotifyUAWidgetSetup : bool { No, Yes };
1300 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes,
1301 DelegatesFocus = DelegatesFocus::No);
1303 // Dispatch an event to UAWidgetsChild, triggering construction
1304 // or onchange callback on the existing widget.
1305 void NotifyUAWidgetSetupOrChange();
1307 enum class UnattachShadowRoot {
1309 Yes,
1312 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1313 // and optionally remove the shadow root.
1314 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1316 void UnattachShadow();
1318 ShadowRoot* GetShadowRootByMode() const;
1319 void SetSlot(const nsAString& aName, ErrorResult& aError);
1320 void GetSlot(nsAString& aName);
1322 ShadowRoot* GetShadowRoot() const {
1323 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1324 return slots ? slots->mShadowRoot.get() : nullptr;
1327 const Maybe<float> GetLastRememberedBSize() const {
1328 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1329 return slots ? slots->mLastRememberedBSize : Nothing();
1331 const Maybe<float> GetLastRememberedISize() const {
1332 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1333 return slots ? slots->mLastRememberedISize : Nothing();
1335 bool HasLastRememberedBSize() const {
1336 return GetLastRememberedBSize().isSome();
1338 bool HasLastRememberedISize() const {
1339 return GetLastRememberedISize().isSome();
1342 const Maybe<ContentRelevancy> GetContentRelevancy() const {
1343 const auto* slots = GetExistingExtendedDOMSlots();
1344 return slots ? slots->mContentRelevancy : Nothing();
1346 void SetContentRelevancy(ContentRelevancy relevancy) {
1347 ExtendedDOMSlots()->mContentRelevancy = Some(relevancy);
1350 const Maybe<bool> GetVisibleForContentVisibility() const {
1351 const auto* slots = GetExistingExtendedDOMSlots();
1352 return slots ? slots->mVisibleForContentVisibility : Nothing();
1354 void SetVisibleForContentVisibility(bool visible) {
1355 ExtendedDOMSlots()->mVisibleForContentVisibility = Some(visible);
1358 void ClearContentRelevancy() {
1359 if (auto* slots = GetExistingExtendedDOMSlots()) {
1360 slots->mContentRelevancy.reset();
1361 slots->mVisibleForContentVisibility.reset();
1365 // https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
1366 MOZ_CAN_RUN_SCRIPT bool CheckVisibility(const CheckVisibilityOptions&);
1368 private:
1369 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1370 // called from JS. Use PresShell::ScrollContentIntoView instead.
1371 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1373 public:
1374 MOZ_CAN_RUN_SCRIPT
1375 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1376 // called from JS. Use PresShell::ScrollContentIntoView instead.
1377 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1378 MOZ_CAN_RUN_SCRIPT void Scroll(double aXScroll, double aYScroll);
1379 MOZ_CAN_RUN_SCRIPT void Scroll(const ScrollToOptions& aOptions);
1380 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1381 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1382 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1383 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1384 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1385 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1386 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1387 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1388 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1389 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1390 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1391 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1392 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1394 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1395 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1397 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1398 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1400 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1401 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1404 MOZ_CAN_RUN_SCRIPT int32_t ScreenX();
1405 MOZ_CAN_RUN_SCRIPT int32_t ScreenY();
1406 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIScreen> GetScreen();
1408 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1409 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1410 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1411 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1413 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1414 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1417 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1418 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1421 // This function will return the block size of first line box, no matter if
1422 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1423 // can't get a primary frame, we will return be zero.
1424 double FirstLineBoxBSize() const;
1426 already_AddRefed<Flex> GetAsFlexContainer();
1427 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1429 bool HasGridFragments();
1431 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1432 Element& aAncestor);
1433 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1434 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1436 already_AddRefed<Animation> Animate(
1437 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1438 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1439 ErrorResult& aError);
1441 MOZ_CAN_RUN_SCRIPT
1442 void GetAnimations(const GetAnimationsOptions& aOptions,
1443 nsTArray<RefPtr<Animation>>& aAnimations);
1445 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1446 nsTArray<RefPtr<Animation>>& aAnimations);
1448 static void GetAnimationsUnsorted(Element* aElement,
1449 PseudoStyleType aPseudoType,
1450 nsTArray<RefPtr<Animation>>& aAnimations);
1452 void CloneAnimationsFrom(const Element& aOther);
1454 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1455 virtual void SetInnerHTML(const nsAString& aInnerHTML,
1456 nsIPrincipal* aSubjectPrincipal,
1457 ErrorResult& aError);
1458 void GetOuterHTML(nsAString& aOuterHTML);
1459 void SetOuterHTML(const nsAString& aOuterHTML, ErrorResult& aError);
1460 void InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
1461 ErrorResult& aError);
1463 void SetHTML(const nsAString& aInnerHTML, const SetHTMLOptions& aOptions,
1464 ErrorResult& aError);
1466 //----------------------------------------
1469 * Add a script event listener with the given event handler name
1470 * (like onclick) and with the value as JS
1471 * @param aEventName the event listener name
1472 * @param aValue the JS to attach
1473 * @param aDefer indicates if deferred execution is allowed
1475 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1476 bool aDefer = true);
1479 * Do whatever needs to be done when the mouse leaves a link
1481 nsresult LeaveLink(nsPresContext* aPresContext);
1483 static bool ShouldBlur(nsIContent* aContent);
1486 * Method to create and dispatch a left-click event loosely based on
1487 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1488 * through the full dispatching of the presshell of the aPresContext; if it's
1489 * false the event will be dispatched only as a DOM event.
1490 * If aPresContext is nullptr, this does nothing.
1492 * @param aFlags Extra flags for the dispatching event. The true flags
1493 * will be respected.
1495 MOZ_CAN_RUN_SCRIPT
1496 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1497 WidgetInputEvent* aSourceEvent,
1498 nsIContent* aTarget, bool aFullDispatch,
1499 const EventFlags* aFlags,
1500 nsEventStatus* aStatus);
1503 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1504 * will be dispatched through the full dispatching of the presshell of the
1505 * aPresContext; if it's false the event will be dispatched only as a DOM
1506 * event.
1507 * If aPresContext is nullptr, this does nothing.
1509 using nsIContent::DispatchEvent;
1510 MOZ_CAN_RUN_SCRIPT
1511 static nsresult DispatchEvent(nsPresContext* aPresContext,
1512 WidgetEvent* aEvent, nsIContent* aTarget,
1513 bool aFullDispatch, nsEventStatus* aStatus);
1515 bool IsDisplayContents() const {
1516 return HasServoData() && Servo_Element_IsDisplayContents(this);
1520 * https://html.spec.whatwg.org/#being-rendered
1522 * With a gotcha for display contents:
1523 * https://github.com/whatwg/html/issues/1837
1525 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1527 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1528 return mAttrs.GetAttr(aAttr);
1531 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1532 int32_t aNameSpaceID) const {
1533 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1537 * Returns the attribute map, if there is one.
1539 * @return existing attribute map or nullptr.
1541 nsDOMAttributeMap* GetAttributeMap() {
1542 nsDOMSlots* slots = GetExistingDOMSlots();
1544 return slots ? slots->mAttributeMap.get() : nullptr;
1547 void RecompileScriptEventListeners();
1550 * Get the attr info for the given namespace ID and attribute name. The
1551 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1552 * null. Note that this can only return info on attributes that actually
1553 * live on this element (and is only virtual to handle XUL prototypes). That
1554 * is, this should only be called from methods that only care about attrs
1555 * that effectively live in mAttrs.
1557 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1558 const nsAtom* aName) const {
1559 NS_ASSERTION(aName, "must have attribute name");
1560 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1561 "must have a real namespace ID!");
1563 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1564 if (index < 0) {
1565 return BorrowedAttrInfo(nullptr, nullptr);
1568 return mAttrs.AttrInfoAt(index);
1572 * Parse a string into an nsAttrValue for a CORS attribute. This
1573 * never fails. The resulting value is an enumerated value whose
1574 * GetEnumValue() returns one of the above constants.
1576 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1579 * Return the CORS mode for a given string
1581 static CORSMode StringToCORSMode(const nsAString& aValue);
1584 * Return the CORS mode for a given nsAttrValue (which may be null,
1585 * but if not should have been parsed via ParseCORSValue).
1587 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1589 nsINode* GetScopeChainParent() const override;
1592 * Locate a TextEditor rooted at this content node, if there is one.
1594 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1597 * Gets value of boolean attribute. Only works for attributes in null
1598 * namespace.
1600 * @param aAttr name of attribute.
1601 * @param aValue Boolean value of attribute.
1603 bool GetBoolAttr(nsAtom* aAttr) const { return HasAttr(aAttr); }
1606 * Sets value of boolean attribute by removing attribute or setting it to
1607 * the empty string. Only works for attributes in null namespace.
1609 * @param aAttr name of attribute.
1610 * @param aValue Boolean value of attribute.
1612 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1615 * Gets the enum value string of an attribute and using a default value if
1616 * the attribute is missing or the string is an invalid enum value.
1618 * @param aType the name of the attribute.
1619 * @param aDefault the default value if the attribute is missing or invalid.
1620 * @param aResult string corresponding to the value [out].
1622 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1623 nsAString& aResult) const;
1626 * Gets the enum value string of an attribute and using the default missing
1627 * value if the attribute is missing or the default invalid value if the
1628 * string is an invalid enum value.
1630 * @param aType the name of the attribute.
1631 * @param aDefaultMissing the default value if the attribute is missing. If
1632 null and the attribute is missing, aResult will be
1633 set to the null DOMString; this only matters for
1634 cases in which we're reflecting a nullable string.
1635 * @param aDefaultInvalid the default value if the attribute is invalid.
1636 * @param aResult string corresponding to the value [out].
1638 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1639 const char* aDefaultInvalid, nsAString& aResult) const;
1642 * Unset an attribute.
1644 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1645 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1649 * Set an attribute in the simplest way possible.
1651 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1652 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1655 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1656 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1657 aError =
1658 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1662 * Preallocate space in this element's attribute array for the given
1663 * total number of attributes.
1665 void TryReserveAttributeCount(uint32_t aAttributeCount);
1668 * Set a content attribute via a reflecting nullable string IDL
1669 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1670 * this will actually remove the content attribute.
1672 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1673 ErrorResult& aError);
1676 * Retrieve the ratio of font-size-inflated text font size to computed font
1677 * size for this element. This will query the element for its primary frame,
1678 * and then use this to get font size inflation information about the frame.
1680 * @returns The font size inflation ratio (inflated font size to uninflated
1681 * font size) for the primary frame of this element. Returns 1.0
1682 * by default if font size inflation is not enabled. Returns -1
1683 * if the element does not have a primary frame.
1685 * @note The font size inflation ratio that is returned is actually the
1686 * font size inflation data for the element's _primary frame_, not the
1687 * element itself, but for most purposes, this should be sufficient.
1689 float FontSizeInflation();
1691 void GetImplementedPseudoElement(nsAString&) const;
1693 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1694 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1697 * Helpers for .dataset. This is implemented on Element, though only some
1698 * sorts of elements expose it to JS as a .dataset property
1700 // Getter, to be called from bindings.
1701 already_AddRefed<nsDOMStringMap> Dataset();
1702 // Callback for destructor of dataset to ensure to null out our weak pointer
1703 // to it.
1704 void ClearDataset();
1706 void RegisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1707 void UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1708 void UnlinkIntersectionObservers();
1709 bool UpdateIntersectionObservation(DOMIntersectionObserver* aObserver,
1710 int32_t threshold);
1712 // A number of methods to cast to various XUL interfaces. They return a
1713 // pointer only if the element implements that interface.
1714 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1715 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1716 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1717 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1718 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1719 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1720 AsXULMultiSelectControl();
1721 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1722 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1723 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1724 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1725 already_AddRefed<nsIBrowser> AsBrowser();
1726 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1729 * Get the presentation context for this content node.
1730 * @return the presentation context
1732 enum PresContextFor { eForComposedDoc, eForUncomposedDoc };
1733 nsPresContext* GetPresContext(PresContextFor aFor);
1736 * The method focuses (or activates) element that accesskey is bound to. It is
1737 * called when accesskey is activated.
1739 * @param aKeyCausesActivation - if true then element should be activated
1740 * @param aIsTrustedEvent - if true then event that is cause of accesskey
1741 * execution is trusted.
1742 * @return an error if the element isn't able to handle the accesskey (caller
1743 * would look for the next element to handle it).
1744 * a boolean indicates whether the focus moves to the element after
1745 * the element handles the accesskey.
1747 MOZ_CAN_RUN_SCRIPT
1748 virtual Result<bool, nsresult> PerformAccesskey(bool aKeyCausesActivation,
1749 bool aIsTrustedEvent) {
1750 return Err(NS_ERROR_NOT_IMPLEMENTED);
1753 protected:
1755 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1756 * read.
1758 static const bool kFireMutationEvent = true;
1759 static const bool kDontFireMutationEvent = false;
1760 static const bool kNotifyDocumentObservers = true;
1761 static const bool kDontNotifyDocumentObservers = false;
1762 static const bool kCallAfterSetAttr = true;
1763 static const bool kDontCallAfterSetAttr = false;
1766 * Set attribute and (if needed) notify documentobservers and fire off
1767 * mutation events. This will send the AttributeChanged notification.
1768 * Callers of this method are responsible for calling AttributeWillChange,
1769 * since that needs to happen before the new attr value has been set, and
1770 * in particular before it has been parsed.
1772 * For the boolean parameters, consider using the named bools above to aid
1773 * code readability.
1775 * @param aNamespaceID namespace of attribute
1776 * @param aAttribute local-name of attribute
1777 * @param aPrefix aPrefix of attribute
1778 * @param aOldValue The old value of the attribute to use as a fallback
1779 * in the cases where the actual old value (i.e.
1780 * its current value) is !StoresOwnData() --- in which
1781 * case the current value is probably already useless.
1782 * If the current value is StoresOwnData() (or absent),
1783 * aOldValue will not be used. aOldValue will only be set
1784 * in certain circumstances (there are mutation
1785 * listeners, element is a custom element, attribute was
1786 * not previously unset). Otherwise it will be null.
1787 * @param aParsedValue parsed new value of attribute. Replaced by the
1788 * old value of the attribute. This old value is only
1789 * useful if either it or the new value is StoresOwnData.
1790 * @param aSubjectPrincipal
1791 * the principal of the scripted caller responsible for
1792 * setting the attribute, or null if no scripted caller
1793 * can be determined. A null value here does not
1794 * guarantee that there is no scripted caller, but a
1795 * non-null value does guarantee that a scripted caller
1796 * with the given principal is directly responsible for
1797 * the attribute change.
1798 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
1799 * needed if aFireMutation or aNotify is true.
1800 * @param aFireMutation should mutation-events be fired?
1801 * @param aNotify should we notify document-observers?
1802 * @param aCallAfterSetAttr should we call AfterSetAttr?
1803 * @param aComposedDocument The current composed document of the element.
1804 * @param aGuard For making sure that this is called with a
1805 * mozAutoDocUpdate instance, this is here. Specify
1806 * an instance of it which you created for the call.
1808 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
1809 nsAtom* aPrefix, const nsAttrValue* aOldValue,
1810 nsAttrValue& aParsedValue,
1811 nsIPrincipal* aSubjectPrincipal, uint8_t aModType,
1812 bool aFireMutation, bool aNotify,
1813 bool aCallAfterSetAttr, Document* aComposedDocument,
1814 const mozAutoDocUpdate& aGuard);
1817 * Scroll to a new position using behavior evaluated from CSS and
1818 * a CSSOM-View DOM method ScrollOptions dictionary. The scrolling may
1819 * be performed asynchronously or synchronously depending on the resolved
1820 * scroll-behavior.
1822 * @param aScroll Destination of scroll, in CSS pixels
1823 * @param aOptions Dictionary of options to be evaluated
1825 MOZ_CAN_RUN_SCRIPT
1826 void Scroll(const CSSIntPoint& aScroll, const ScrollOptions& aOptions);
1829 * Convert an attribute string value to attribute type based on the type of
1830 * attribute. Called by SetAttr(). Note that at the moment we only do this
1831 * for attributes in the null namespace (kNameSpaceID_None).
1833 * @param aNamespaceID the namespace of the attribute to convert
1834 * @param aAttribute the attribute to convert
1835 * @param aValue the string value to convert
1836 * @param aMaybeScriptedPrincipal the principal of the script setting the
1837 * attribute, if one can be determined, or null otherwise. As in
1838 * AfterSetAttr, a null value does not guarantee that the attribute was
1839 * not set by a scripted caller, but a non-null value guarantees that
1840 * the attribute was set by a scripted caller with the given principal.
1841 * @param aResult the nsAttrValue [OUT]
1842 * @return true if the parsing was successful, false otherwise
1844 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1845 const nsAString& aValue,
1846 nsIPrincipal* aMaybeScriptedPrincipal,
1847 nsAttrValue& aResult);
1850 * Hook that is called by Element::SetAttr to allow subclasses to
1851 * deal with attribute sets. This will only be called after we verify that
1852 * we're actually doing an attr set and will be called before
1853 * AttributeWillChange and before ParseAttribute and hence before we've set
1854 * the new value.
1856 * @param aNamespaceID the namespace of the attr being set
1857 * @param aName the localname of the attribute being set
1858 * @param aValue the value it's being set to represented as either a string or
1859 * a parsed nsAttrValue. Alternatively, if the attr is being removed it
1860 * will be null.
1861 * @param aNotify Whether we plan to notify document observers.
1863 virtual void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1864 const nsAttrValue* aValue, bool aNotify);
1867 * Hook that is called by Element::SetAttr to allow subclasses to
1868 * deal with attribute sets. This will only be called after we have called
1869 * SetAndSwapAttr (that is, after we have actually set the attr). It will
1870 * always be called under a scriptblocker.
1872 * @param aNamespaceID the namespace of the attr being set
1873 * @param aName the localname of the attribute being set
1874 * @param aValue the value it's being set to. If null, the attr is being
1875 * removed.
1876 * @param aOldValue the value that the attribute had previously. If null,
1877 * the attr was not previously set. This argument may not have the
1878 * correct value for SVG elements, or other cases in which the
1879 * attribute value doesn't store its own data
1880 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1881 * responsible for setting the attribute, or null if no scripted caller
1882 * can be determined, or the attribute is being unset. A null value
1883 * here does not guarantee that there is no scripted caller, but a
1884 * non-null value does guarantee that a scripted caller with the given
1885 * principal is directly responsible for the attribute change.
1886 * @param aNotify Whether we plan to notify document observers.
1888 virtual void AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
1889 const nsAttrValue* aValue,
1890 const nsAttrValue* aOldValue,
1891 nsIPrincipal* aMaybeScriptedPrincipal,
1892 bool aNotify);
1895 * This function shall be called just before the id attribute changes. It will
1896 * be called after BeforeSetAttr. If the attribute being changed is not the id
1897 * attribute, this function does nothing. Otherwise, it will remove the old id
1898 * from the document's id cache.
1900 * This must happen after BeforeSetAttr (rather than during) because the
1901 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
1902 * incorrectly determine whether the element had an id, the element may not be
1903 * restyled properly.
1905 * @param aNamespaceID the namespace of the attr being set
1906 * @param aName the localname of the attribute being set
1907 * @param aValue the new id value. Will be null if the id is being unset.
1909 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1910 const nsAttrValue* aValue);
1913 * This function shall be called just after the id attribute changes. It will
1914 * be called before AfterSetAttr. If the attribute being changed is not the id
1915 * attribute, this function does nothing. Otherwise, it will add the new id to
1916 * the document's id cache and properly set the ElementHasID flag.
1918 * This must happen before AfterSetAttr (rather than during) because the
1919 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
1920 * incorrectly determine whether the element now has an id, the element may
1921 * not be restyled properly.
1923 * @param aNamespaceID the namespace of the attr being set
1924 * @param aName the localname of the attribute being set
1925 * @param aValue the new id value. Will be null if the id is being unset.
1927 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1928 const nsAttrValue* aValue);
1931 * Usually, setting an attribute to the value that it already has results in
1932 * no action. However, in some cases, setting an attribute to its current
1933 * value should have the effect of, for example, forcing a reload of
1934 * network data. To address that, this function will be called in this
1935 * situation to allow the handling of such a case.
1937 * @param aNamespaceID the namespace of the attr being set
1938 * @param aName the localname of the attribute being set
1939 * @param aValue the value it's being set to represented as either a string or
1940 * a parsed nsAttrValue.
1941 * @param aNotify Whether we plan to notify document observers.
1943 virtual void OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
1944 const nsAttrValueOrString& aValue,
1945 bool aNotify);
1948 * Hook to allow subclasses to produce a different EventListenerManager if
1949 * needed for attachment of attribute-defined handlers
1951 virtual EventListenerManager* GetEventListenerManagerForAttr(
1952 nsAtom* aAttrName, bool* aDefer);
1955 * Internal hook for converting an attribute name-string to nsAttrName in
1956 * case there is such existing attribute. aNameToUse can be passed to get
1957 * name which was used for looking for the attribute (lowercase in HTML).
1959 const nsAttrName* InternalGetAttrNameFromQName(
1960 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
1962 virtual Element* GetNameSpaceElement() override { return this; }
1964 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
1965 const nsAString& aLocalName);
1967 inline void RegisterActivityObserver();
1968 inline void UnregisterActivityObserver();
1971 * Add/remove this element to the documents id cache
1973 void AddToIdTable(nsAtom* aId);
1974 void RemoveFromIdTable();
1977 * Functions to carry out event default actions for links of all types
1978 * (HTML links, XLinks, SVG "XLinks", etc.)
1982 * Check that we meet the conditions to handle a link event
1983 * and that we are actually on a link.
1985 * @param aVisitor event visitor
1986 * @return true if we can handle the link event, false otherwise
1988 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor) const;
1991 * Handle status bar updates before they can be cancelled.
1993 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
1995 void DispatchChromeOnlyLinkClickEvent(EventChainPostVisitor& aVisitor);
1998 * Handle default actions for link event if the event isn't consumed yet.
2000 MOZ_CAN_RUN_SCRIPT
2001 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
2003 public:
2005 * Check if this element is a link. This matches the CSS definition of the
2006 * :any-link pseudo-class.
2008 bool IsLink() const {
2009 return mState.HasAtLeastOneOfStates(ElementState::VISITED |
2010 ElementState::UNVISITED);
2014 * Get a pointer to the full href URI (fully resolved and canonicalized, since
2015 * it's an nsIURI object) for link elements.
2017 * @return A pointer to the URI or null if the element is not a link, or it
2018 * has no HREF attribute, or the HREF attribute is an invalid URI.
2020 virtual already_AddRefed<nsIURI> GetHrefURI() const { return nullptr; }
2023 * Get the target of this link element. Consumers should established that
2024 * this element is a link (probably using IsLink) before calling this
2025 * function (or else why call it?)
2027 * Note: for HTML this gets the value of the 'target' attribute; for XLink
2028 * this gets the value of the xlink:_moz_target attribute, or failing that,
2029 * the value of xlink:show, converted to a suitably equivalent named target
2030 * (e.g. _blank).
2032 virtual void GetLinkTarget(nsAString& aTarget);
2034 virtual bool Translate() const;
2036 protected:
2037 enum class ReparseAttributes { No, Yes };
2039 * Copy attributes and state to another element
2040 * @param aDest the object to copy to
2042 nsresult CopyInnerTo(Element* aDest,
2043 ReparseAttributes = ReparseAttributes::Yes);
2046 * Some event handler content attributes have a different name (e.g. different
2047 * case) from the actual event name. This function takes an event handler
2048 * content attribute name and returns the corresponding event name, to be used
2049 * for adding the actual event listener.
2051 virtual nsAtom* GetEventNameForAttr(nsAtom* aAttr);
2054 * Register/unregister this element to accesskey map if it supports accesskey.
2056 virtual void RegUnRegAccessKey(bool aDoReg);
2058 private:
2059 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
2060 void AssertInvariantsOnNodeInfoChange();
2061 #endif
2064 * Slow path for GetClasses, this should only be called for SVG elements.
2066 const nsAttrValue* GetSVGAnimatedClass() const;
2069 * Get this element's client area rect in app units.
2070 * @return the frame's client area
2072 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
2075 * GetCustomInterface is somewhat like a GetInterface, but it is expected
2076 * that the implementation is provided by a custom element or via the
2077 * the XBL implements keyword. To use this, create a public method that
2078 * wraps a call to GetCustomInterface.
2080 template <class T>
2081 void GetCustomInterface(nsGetterAddRefs<T> aResult);
2083 // Prevent people from doing pointless checks/casts on Element instances.
2084 void IsElement() = delete;
2085 void AsElement() = delete;
2087 // Data members
2088 ElementState mState;
2089 // Per-node data managed by Servo.
2091 // There should not be data on nodes that are not in the flattened tree, or
2092 // descendants of display: none elements.
2093 mozilla::RustCell<ServoNodeData*> mServoData;
2095 protected:
2096 // Array containing all attributes for this element
2097 AttrArray mAttrs;
2100 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
2102 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2103 const nsAtom* aName) const {
2104 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2105 MOZ_ASSERT(aName, "Must have attribute name");
2107 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2108 return val && !val->IsEmptyString();
2111 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2112 const nsAString& aValue,
2113 nsCaseTreatment aCaseSensitive) const {
2114 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2117 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2118 const nsAtom* aValue,
2119 nsCaseTreatment aCaseSensitive) const {
2120 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2123 } // namespace dom
2124 } // namespace mozilla
2126 inline mozilla::dom::Element* nsINode::AsElement() {
2127 MOZ_ASSERT(IsElement());
2128 return static_cast<mozilla::dom::Element*>(this);
2131 inline const mozilla::dom::Element* nsINode::AsElement() const {
2132 MOZ_ASSERT(IsElement());
2133 return static_cast<const mozilla::dom::Element*>(this);
2136 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2137 return mozilla::dom::Element::FromNodeOrNull(mParent);
2140 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2141 nsIContent* previousSibling = GetPreviousSibling();
2142 while (previousSibling) {
2143 if (previousSibling->IsElement()) {
2144 return previousSibling->AsElement();
2146 previousSibling = previousSibling->GetPreviousSibling();
2149 return nullptr;
2152 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2153 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2154 : GetParentElement();
2157 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2158 nsIContent* nextSibling = GetNextSibling();
2159 while (nextSibling) {
2160 if (nextSibling->IsElement()) {
2161 return nextSibling->AsElement();
2163 nextSibling = nextSibling->GetNextSibling();
2166 return nullptr;
2170 * Macros to implement Clone(). _elementName is the class for which to implement
2171 * Clone.
2173 #define NS_IMPL_ELEMENT_CLONE(_elementName) \
2174 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2175 nsINode** aResult) const { \
2176 *aResult = nullptr; \
2177 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2178 auto* nim = ni->NodeInfoManager(); \
2179 RefPtr<_elementName> it = new (nim) _elementName(ni.forget()); \
2180 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2181 if (NS_SUCCEEDED(rv)) { \
2182 it.forget(aResult); \
2185 return rv; \
2188 #define EXPAND(...) __VA_ARGS__
2189 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, extra_args_) \
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 = \
2196 new (nim) _elementName(ni.forget() EXPAND extra_args_); \
2197 nsresult rv = it->Init(); \
2198 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2199 if (NS_FAILED(rv2)) { \
2200 rv = rv2; \
2202 if (NS_SUCCEEDED(rv)) { \
2203 it.forget(aResult); \
2206 return rv; \
2209 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName) \
2210 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, ())
2211 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2212 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, (, NOT_FROM_PARSER))
2214 #endif // mozilla_dom_Element_h__