Bug 1812499 [wpt PR 38184] - Simplify handling of name-to-subdir mapping in canvas...
[gecko.git] / dom / base / Element.h
blob873d5d1b36f1ad83cfc97f99f0d335a77b5e48f9
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 nsGlobalWindowInner;
75 class nsGlobalWindowOuter;
76 class nsIAutoCompletePopup;
77 class nsIBrowser;
78 class nsIDOMXULButtonElement;
79 class nsIDOMXULContainerElement;
80 class nsIDOMXULContainerItemElement;
81 class nsIDOMXULControlElement;
82 class nsIDOMXULMenuListElement;
83 class nsIDOMXULMultiSelectControlElement;
84 class nsIDOMXULRadioGroupElement;
85 class nsIDOMXULRelatedElement;
86 class nsIDOMXULSelectControlElement;
87 class nsIDOMXULSelectControlItemElement;
88 class nsIFrame;
89 class nsIHTMLCollection;
90 class nsIMozBrowserFrame;
91 class nsIPrincipal;
92 class nsIScrollableFrame;
93 class nsIURI;
94 class nsMappedAttributes;
95 class nsPresContext;
96 class nsWindowSizes;
97 struct JSContext;
98 struct ServoNodeData;
99 template <class E>
100 class nsTArray;
101 template <class T>
102 class nsGetterAddRefs;
104 namespace mozilla {
105 class DeclarationBlock;
106 class ErrorResult;
107 class OOMReporter;
108 class SMILAttr;
109 struct MutationClosureData;
110 class TextEditor;
111 namespace css {
112 struct URLValue;
113 } // namespace css
114 namespace dom {
115 struct CheckVisibilityOptions;
116 struct CustomElementData;
117 struct SetHTMLOptions;
118 struct GetAnimationsOptions;
119 struct ScrollIntoViewOptions;
120 struct ScrollToOptions;
121 struct FocusOptions;
122 struct ShadowRootInit;
123 struct ScrollOptions;
124 class Attr;
125 class BooleanOrScrollIntoViewOptions;
126 class Document;
127 class DOMIntersectionObserver;
128 class DOMMatrixReadOnly;
129 class Element;
130 class ElementOrCSSPseudoElement;
131 class Promise;
132 class Sanitizer;
133 class ShadowRoot;
134 class UnrestrictedDoubleOrKeyframeAnimationOptions;
135 template <typename T>
136 class Optional;
137 enum class CallerType : uint32_t;
138 enum class ReferrerPolicy : uint8_t;
139 typedef nsTHashMap<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>
140 IntersectionObserverList;
141 } // namespace dom
142 } // namespace mozilla
144 // Declared here because of include hell.
145 extern "C" bool Servo_Element_IsDisplayContents(const mozilla::dom::Element*);
147 already_AddRefed<nsContentList> NS_GetContentList(nsINode* aRootNode,
148 int32_t aMatchNameSpaceId,
149 const nsAString& aTagname);
151 #define ELEMENT_FLAG_BIT(n_) \
152 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
154 // Element-specific flags
155 enum {
156 // Whether this node has dirty descendants for Servo's style system.
157 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(0),
158 // Whether this node has dirty descendants for animation-only restyle for
159 // Servo's style system.
160 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(1),
162 // Whether the element has been snapshotted due to attribute or state changes
163 // by the Servo restyle manager.
164 ELEMENT_HAS_SNAPSHOT = ELEMENT_FLAG_BIT(2),
166 // Whether the element has already handled its relevant snapshot.
168 // Used by the servo restyle process in order to accurately track whether the
169 // style of an element is up-to-date, even during the same restyle process.
170 ELEMENT_HANDLED_SNAPSHOT = ELEMENT_FLAG_BIT(3),
172 // If this flag is set on an element, that means that it is a HTML datalist
173 // element or has a HTML datalist element ancestor.
174 ELEMENT_IS_DATALIST_OR_HAS_DATALIST_ANCESTOR = ELEMENT_FLAG_BIT(4),
176 // Remaining bits are for subclasses
177 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET = NODE_TYPE_SPECIFIC_BITS_OFFSET + 5
180 #undef ELEMENT_FLAG_BIT
182 // Make sure we have space for our bits
183 ASSERT_NODE_FLAGS_SPACE(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
185 namespace mozilla {
186 enum class PseudoStyleType : uint8_t;
187 class EventChainPostVisitor;
188 class EventChainPreVisitor;
189 class EventChainVisitor;
190 class EventListenerManager;
191 class EventStateManager;
193 namespace dom {
195 struct CustomElementDefinition;
196 class Animation;
197 class CustomElementRegistry;
198 class Link;
199 class DOMRect;
200 class DOMRectList;
201 class Flex;
202 class Grid;
204 // IID for the dom::Element interface
205 #define NS_ELEMENT_IID \
207 0xc67ed254, 0xfd3b, 0x4b10, { \
208 0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1 \
212 #define REFLECT_DOMSTRING_ATTR(method, attr) \
213 void Get##method(nsAString& aValue) const { \
214 GetAttr(nsGkAtoms::attr, aValue); \
216 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
217 SetAttr(nsGkAtoms::attr, aValue, aRv); \
220 class Element : public FragmentOrElement {
221 public:
222 #ifdef MOZILLA_INTERNAL_API
223 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
224 : FragmentOrElement(std::move(aNodeInfo)),
225 mState(ElementState::READONLY | ElementState::DEFINED) {
226 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
227 "Bad NodeType in aNodeInfo");
228 SetIsElement();
231 ~Element() {
232 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
235 #endif // MOZILLA_INTERNAL_API
237 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
239 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
241 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
243 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
246 * Method to get the full state of this element. See dom/base/rust/lib.rs for
247 * the possible bits that could be set here.
249 ElementState State() const {
250 // mState is maintained by having whoever might have changed it
251 // call UpdateState() or one of the other mState mutators.
252 return mState;
256 * Ask this element to update its state. If aNotify is false, then
257 * state change notifications will not be dispatched; in that
258 * situation it is the caller's responsibility to dispatch them.
260 * In general, aNotify should only be false if we're guaranteed that
261 * the element can't have a frame no matter what its style is
262 * (e.g. if we're in the middle of adding it to the document or
263 * removing it from the document).
265 void UpdateState(bool aNotify);
268 * Method to update mState with link state information. This does not notify.
270 void UpdateLinkState(ElementState aState);
273 * Returns the current disabled state of the element.
275 bool IsDisabled() const { return State().HasState(ElementState::DISABLED); }
277 virtual int32_t TabIndexDefault() { return -1; }
280 * Get tabIndex of this element. If not found, return TabIndexDefault.
282 int32_t TabIndex();
285 * Get the parsed value of tabindex attribute.
287 Maybe<int32_t> GetTabIndexAttrValue();
290 * Set tabIndex value to this element.
292 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
295 * Sets the ShadowRoot binding for this element. The contents of the
296 * binding is rendered in place of this node's children.
298 * @param aShadowRoot The ShadowRoot to be bound to this element.
300 void SetShadowRoot(ShadowRoot* aShadowRoot);
302 void SetLastRememberedBSize(float aBSize);
303 void SetLastRememberedISize(float aISize);
304 void RemoveLastRememberedBSize();
305 void RemoveLastRememberedISize();
308 * Make focus on this element.
310 // TODO: Convert Focus() to MOZ_CAN_RUN_SCRIPT and get rid of the
311 // kungFuDeathGrip in it.
312 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Focus(const FocusOptions& aOptions,
313 const CallerType aCallerType,
314 ErrorResult& aError);
317 * Show blur and clear focus.
319 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Blur(mozilla::ErrorResult& aError);
322 * The style state of this element. This is the real state of the element
323 * with any style locks applied for pseudo-class inspecting.
325 ElementState StyleState() const {
326 if (!HasLockedStyleStates()) {
327 return mState;
329 return StyleStateFromLocks();
333 * StyleStateLocks is used to specify which event states should be locked,
334 * and whether they should be locked to on or off.
336 struct StyleStateLocks {
337 // mLocks tracks which event states should be locked.
338 ElementState mLocks;
339 // mValues tracks if the locked state should be on or off.
340 ElementState mValues;
344 * The style state locks applied to this element.
346 StyleStateLocks LockedStyleStates() const;
349 * Add a style state lock on this element.
350 * aEnabled is the value to lock the given state bits to.
352 void LockStyleStates(ElementState aStates, bool aEnabled);
355 * Remove a style state lock on this element.
357 void UnlockStyleStates(ElementState aStates);
360 * Clear all style state locks on this element.
362 void ClearStyleStateLocks();
365 * Accessors for the state of our dir attribute.
367 bool HasDirAuto() const {
368 return State().HasState(ElementState::HAS_DIR_ATTR_LIKE_AUTO);
372 * Elements with dir="rtl" or dir="ltr".
374 bool HasFixedDir() const {
375 return State().HasAtLeastOneOfStates(ElementState::HAS_DIR_ATTR_LTR |
376 ElementState::HAS_DIR_ATTR_RTL);
380 * Get the inline style declaration, if any, for this element.
382 DeclarationBlock* GetInlineStyleDeclaration() const;
385 * Get the mapped attributes, if any, for this element.
387 const nsMappedAttributes* GetMappedAttributes() const;
389 void ClearMappedServoStyle() { mAttrs.ClearMappedServoStyle(); }
392 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
393 * so that the element implementation can access the old style attribute
394 * value.
396 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
399 * Set the inline style declaration for this element.
401 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
402 MutationClosureData& aData);
405 * Get the SMIL override style declaration for this element. If the
406 * rule hasn't been created, this method simply returns null.
408 DeclarationBlock* GetSMILOverrideStyleDeclaration();
411 * Set the SMIL override style declaration for this element. This method will
412 * notify the document's pres context, so that the style changes will be
413 * noticed.
415 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
418 * Returns a new SMILAttr that allows the caller to animate the given
419 * attribute on this element.
421 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
422 nsAtom* aName);
425 * Get the SMIL override style for this element. This is a style declaration
426 * that is applied *after* the inline style, and it can be used e.g. to store
427 * animated style values.
429 * Note: This method is analogous to the 'GetStyle' method in
430 * nsGenericHTMLElement and nsStyledElement.
432 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
435 * Returns if the element is labelable as per HTML specification.
437 virtual bool IsLabelable() const;
440 * Returns if the element is interactive content as per HTML specification.
442 virtual bool IsInteractiveHTMLContent() const;
445 * Returns |this| as an nsIMozBrowserFrame* if the element is a frame or
446 * iframe element.
448 * We have this method, rather than using QI, so that we can use it during
449 * the servo traversal, where we can't QI DOM nodes because of non-thread-safe
450 * refcounts.
452 virtual nsIMozBrowserFrame* GetAsMozBrowserFrame() { return nullptr; }
455 * Is the attribute named stored in the mapped attributes?
457 * // XXXbz we use this method in HasAttributeDependentStyle, so svg
458 * returns true here even though it stores nothing in the mapped
459 * attributes.
461 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
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 if (HasFlag(NODE_HAS_DIRECTION_RTL)) {
474 return eDir_RTL;
477 if (HasFlag(NODE_HAS_DIRECTION_LTR)) {
478 return eDir_LTR;
481 return eDir_NotSet;
484 inline void SetDirectionality(Directionality aDir, bool aNotify) {
485 UnsetFlags(NODE_ALL_DIRECTION_FLAGS);
486 if (!aNotify) {
487 RemoveStatesSilently(ElementState::DIR_STATES);
490 switch (aDir) {
491 case (eDir_RTL):
492 SetFlags(NODE_HAS_DIRECTION_RTL);
493 if (!aNotify) {
494 AddStatesSilently(ElementState::RTL);
496 break;
498 case (eDir_LTR):
499 SetFlags(NODE_HAS_DIRECTION_LTR);
500 if (!aNotify) {
501 AddStatesSilently(ElementState::LTR);
503 break;
505 default:
506 break;
510 * Only call UpdateState if we need to notify, because we call
511 * SetDirectionality for every element, and UpdateState is very very slow
512 * for some elements.
514 if (aNotify) {
515 UpdateState(true);
519 Directionality GetComputedDirectionality() const;
521 static const uint32_t kAllServoDescendantBits =
522 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO |
523 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO |
524 NODE_DESCENDANTS_NEED_FRAMES;
527 * Notes that something in the given subtree of this element needs dirtying,
528 * and that all the relevant dirty bits have already been propagated up to the
529 * element.
531 * This is important because `NoteDirtyForServo` uses the dirty bits to reason
532 * about the shape of the tree, so we can't just call into there.
534 void NoteDirtySubtreeForServo();
536 void NoteDirtyForServo();
537 void NoteAnimationOnlyDirtyForServo();
538 void NoteDescendantsNeedFramesForServo();
540 bool HasDirtyDescendantsForServo() const {
541 return HasFlag(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
544 void SetHasDirtyDescendantsForServo() {
545 SetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
548 void UnsetHasDirtyDescendantsForServo() {
549 UnsetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
552 bool HasAnimationOnlyDirtyDescendantsForServo() const {
553 return HasFlag(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
556 void SetHasAnimationOnlyDirtyDescendantsForServo() {
557 SetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
560 void UnsetHasAnimationOnlyDirtyDescendantsForServo() {
561 UnsetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
564 bool HasServoData() const { return !!mServoData.Get(); }
566 void ClearServoData() { ClearServoData(GetComposedDoc()); }
567 void ClearServoData(Document* aDocument);
570 * Gets the custom element data used by web components custom element.
571 * Custom element data is created at the first attempt to enqueue a callback.
573 * @return The custom element data or null if none.
575 inline CustomElementData* GetCustomElementData() const {
576 if (!HasCustomElementData()) {
577 return nullptr;
580 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
581 return slots ? slots->mCustomElementData.get() : nullptr;
585 * Sets the custom element data, ownership of the
586 * callback data is taken by this element.
588 * @param aData The custom element data.
590 void SetCustomElementData(UniquePtr<CustomElementData> aData);
593 * Gets the custom element definition used by web components custom element.
595 * @return The custom element definition or null if element is not a custom
596 * element or custom element is not defined yet.
598 CustomElementDefinition* GetCustomElementDefinition() const;
601 * Sets the custom element definition, called when custom element is created
602 * or upgraded.
604 * @param aDefinition The custom element definition.
606 virtual void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
608 const AttrArray& GetAttrs() const { return mAttrs; }
610 void SetDefined(bool aSet) {
611 if (aSet) {
612 AddStates(ElementState::DEFINED);
613 } else {
614 RemoveStates(ElementState::DEFINED);
618 // AccessibilityRole
619 REFLECT_DOMSTRING_ATTR(Role, role)
621 // AriaAttributes
622 REFLECT_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
623 REFLECT_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
624 REFLECT_DOMSTRING_ATTR(AriaBusy, aria_busy)
625 REFLECT_DOMSTRING_ATTR(AriaChecked, aria_checked)
626 REFLECT_DOMSTRING_ATTR(AriaColCount, aria_colcount)
627 REFLECT_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
628 REFLECT_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
629 REFLECT_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
630 REFLECT_DOMSTRING_ATTR(AriaCurrent, aria_current)
631 REFLECT_DOMSTRING_ATTR(AriaDescription, aria_description)
632 REFLECT_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
633 REFLECT_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
634 REFLECT_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
635 REFLECT_DOMSTRING_ATTR(AriaHidden, aria_hidden)
636 REFLECT_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
637 REFLECT_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
638 REFLECT_DOMSTRING_ATTR(AriaLabel, aria_label)
639 REFLECT_DOMSTRING_ATTR(AriaLevel, aria_level)
640 REFLECT_DOMSTRING_ATTR(AriaLive, aria_live)
641 REFLECT_DOMSTRING_ATTR(AriaModal, aria_modal)
642 REFLECT_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
643 REFLECT_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
644 REFLECT_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
645 REFLECT_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
646 REFLECT_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
647 REFLECT_DOMSTRING_ATTR(AriaPressed, aria_pressed)
648 REFLECT_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
649 REFLECT_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
650 REFLECT_DOMSTRING_ATTR(AriaRequired, aria_required)
651 REFLECT_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
652 REFLECT_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
653 REFLECT_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
654 REFLECT_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
655 REFLECT_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
656 REFLECT_DOMSTRING_ATTR(AriaSelected, aria_selected)
657 REFLECT_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
658 REFLECT_DOMSTRING_ATTR(AriaSort, aria_sort)
659 REFLECT_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
660 REFLECT_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
661 REFLECT_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
662 REFLECT_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
664 protected:
666 * Method to get the _intrinsic_ content state of this element. This is the
667 * state that is independent of the element's presentation. To get the full
668 * the possible bits that could be set here.
670 virtual ElementState IntrinsicState() const;
673 * Method to add state bits. This should be called from subclass
674 * constructors to set up our event state correctly at construction
675 * time and other places where we don't want to notify a state
676 * change.
678 void AddStatesSilently(ElementState aStates) { mState |= aStates; }
681 * Method to remove state bits. This should be called from subclass
682 * constructors to set up our event state correctly at construction
683 * time and other places where we don't want to notify a state
684 * change.
686 void RemoveStatesSilently(ElementState aStates) { mState &= ~aStates; }
688 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
689 ErrorResult& aError);
691 public:
692 MOZ_CAN_RUN_SCRIPT
693 nsIScrollableFrame* GetScrollFrame(nsIFrame** aStyledFrame = nullptr,
694 FlushType aFlushType = FlushType::Layout);
696 private:
697 // Need to allow the ESM, nsGlobalWindow, and the focus manager
698 // and Document to set our state
699 friend class mozilla::EventStateManager;
700 friend class mozilla::dom::Document;
701 friend class ::nsGlobalWindowInner;
702 friend class ::nsGlobalWindowOuter;
703 friend class ::nsFocusManager;
705 // Allow CusomtElementRegistry to call AddStates.
706 friend class CustomElementRegistry;
708 // Also need to allow Link to call UpdateLinkState.
709 friend class Link;
711 void NotifyStateChange(ElementState aStates);
713 void NotifyStyleStateChange(ElementState aStates);
715 // Style state computed from element's state and style locks.
716 ElementState StyleStateFromLocks() const;
718 protected:
719 // Methods for the ESM, nsGlobalWindow, focus manager and Document to
720 // manage state bits.
721 // These will handle setting up script blockers when they notify, so no need
722 // to do it in the callers unless desired. States passed here must only be
723 // those in EXTERNALLY_MANAGED_STATES.
724 void AddStates(ElementState aStates) {
725 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(ElementState::INTRINSIC_STATES),
726 "Should only be adding externally-managed states here");
727 ElementState old = mState;
728 AddStatesSilently(aStates);
729 NotifyStateChange(old ^ mState);
731 void RemoveStates(ElementState aStates) {
732 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(ElementState::INTRINSIC_STATES),
733 "Should only be removing externally-managed states here");
734 ElementState old = mState;
735 RemoveStatesSilently(aStates);
736 NotifyStateChange(old ^ mState);
738 void ToggleStates(ElementState aStates, bool aNotify) {
739 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(ElementState::INTRINSIC_STATES),
740 "Should only be removing externally-managed states here");
741 mState ^= aStates;
742 if (aNotify) {
743 NotifyStateChange(aStates);
747 public:
748 // Public methods to manage state bits in MANUALLY_MANAGED_STATES.
749 void AddManuallyManagedStates(ElementState aStates) {
750 MOZ_ASSERT(ElementState::MANUALLY_MANAGED_STATES.HasAllStates(aStates),
751 "Should only be adding manually-managed states here");
752 AddStates(aStates);
754 void RemoveManuallyManagedStates(ElementState aStates) {
755 MOZ_ASSERT(ElementState::MANUALLY_MANAGED_STATES.HasAllStates(aStates),
756 "Should only be removing manually-managed states here");
757 RemoveStates(aStates);
760 void UpdateEditableState(bool aNotify) override;
762 nsresult BindToTree(BindContext&, nsINode& aParent) override;
764 void UnbindFromTree(bool aNullParent = true) override;
767 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
768 * with that name exists. This method is intended for character case
769 * conversion if the content object is case insensitive (e.g. HTML). Returns
770 * the nodeinfo of the attribute with the specified name if one exists or
771 * null otherwise.
773 * @param aStr the unparsed attribute string
774 * @return the node info. May be nullptr.
776 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
777 const nsAString& aStr) const;
780 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
781 * is true or there are mutation listeners that must be triggered, the
782 * attribute is currently set, and the new value that is about to be set is
783 * different to the current value. As a perf optimization the new and old
784 * values will not actually be compared if we aren't notifying and we don't
785 * have mutation listeners (in which case it's cheap to just return false
786 * and let the caller go ahead and set the value).
787 * @param aOldValue [out] Set to the old value of the attribute, but only if
788 * there are event listeners. If set, the type of aOldValue will be either
789 * nsAttrValue::eString or nsAttrValue::eAtom.
790 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
791 * MutationEvent_Binding::ADDITION, but only if this helper returns true
792 * @param aHasListeners [out] Set to true if there are mutation event
793 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
794 * @param aOldValueSet [out] Indicates whether an old attribute value has been
795 * stored in aOldValue. The bool will be set to true if a value was stored.
797 bool MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
798 const nsAtom* aPrefix,
799 const nsAttrValueOrString& aValue, bool aNotify,
800 nsAttrValue& aOldValue, uint8_t* aModType,
801 bool* aHasListeners, bool* aOldValueSet);
804 * Notifies mutation listeners if aNotify is true, there are mutation
805 * listeners, and the attribute value is changing.
807 * @param aNamespaceID The namespace of the attribute
808 * @param aName The local name of the attribute
809 * @param aPrefix The prefix of the attribute
810 * @param aValue The value that the attribute is being changed to
811 * @param aNotify If true, mutation listeners will be notified if they exist
812 * and the attribute value is changing
813 * @param aOldValue [out] Set to the old value of the attribute, but only if
814 * there are event listeners. If set, the type of aOldValue will be either
815 * nsAttrValue::eString or nsAttrValue::eAtom.
816 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
817 * MutationEvent_Binding::ADDITION, but only if this helper returns true
818 * @param aHasListeners [out] Set to true if there are mutation event
819 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
820 * @param aOldValueSet [out] Indicates whether an old attribute value has been
821 * stored in aOldValue. The bool will be set to true if a value was stored.
823 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
824 nsAtom* aPrefix,
825 const nsAttrValueOrString& aValue, bool aNotify,
826 nsAttrValue& aOldValue, uint8_t* aModType,
827 bool* aHasListeners, bool* aOldValueSet);
830 * Sets the class attribute to a value that contains no whitespace.
831 * Assumes that we are not notifying and that the attribute hasn't been
832 * set previously.
834 nsresult SetSingleClassFromParser(nsAtom* aSingleClassName);
836 // aParsedValue receives the old value of the attribute. That's useful if
837 // either the input or output value of aParsedValue is StoresOwnData.
838 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
839 nsAttrValue& aParsedValue, bool aNotify);
841 * Get the current value of the attribute. This returns a form that is
842 * suitable for passing back into SetAttr.
844 * @param aNameSpaceID the namespace of the attr (defaults to
845 kNameSpaceID_None in the overload that omits this arg)
846 * @param aName the name of the attr
847 * @param aResult the value (may legitimately be the empty string) [OUT]
848 * @returns true if the attribute was set (even when set to empty string)
849 * false when not set.
850 * GetAttr is not inlined on purpose, to keep down codesize from all the
851 * inlined nsAttrValue bits for C++ callers.
853 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
854 nsAString& aResult) const;
856 bool GetAttr(const nsAtom* aName, nsAString& aResult) const {
857 return GetAttr(kNameSpaceID_None, aName, aResult);
861 * Determine if an attribute has been set (empty string or otherwise).
863 * @param aNameSpaceId the namespace id of the attribute (defaults to
864 kNameSpaceID_None in the overload that omits this arg)
865 * @param aAttr the attribute name
866 * @return whether an attribute exists
868 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
870 bool HasAttr(const nsAtom* aAttr) const {
871 return HasAttr(kNameSpaceID_None, aAttr);
875 * Determine if an attribute has been set to a non-empty string value. If the
876 * attribute is not set at all, this will return false.
878 * @param aNameSpaceId the namespace id of the attribute (defaults to
879 * kNameSpaceID_None in the overload that omits this arg)
880 * @param aAttr the attribute name
882 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
884 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
885 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
889 * Test whether this Element's given attribute has the given value. If the
890 * attribute is not set at all, this will return false.
892 * @param aNameSpaceID The namespace ID of the attribute. Must not
893 * be kNameSpaceID_Unknown.
894 * @param aName The name atom of the attribute. Must not be null.
895 * @param aValue The value to compare to.
896 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
898 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
899 const nsAString& aValue,
900 nsCaseTreatment aCaseSensitive) const;
903 * Test whether this Element's given attribute has the given value. If the
904 * attribute is not set at all, this will return false.
906 * @param aNameSpaceID The namespace ID of the attribute. Must not
907 * be kNameSpaceID_Unknown.
908 * @param aName The name atom of the attribute. Must not be null.
909 * @param aValue The value to compare to. Must not be null.
910 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
912 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
913 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
916 * Check whether this Element's given attribute has one of a given list of
917 * values. If there is a match, we return the index in the list of the first
918 * matching value. If there was no attribute at all, then we return
919 * ATTR_MISSING. If there was an attribute but it didn't match, we return
920 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
922 * @param aNameSpaceID The namespace ID of the attribute. Must not
923 * be kNameSpaceID_Unknown.
924 * @param aName The name atom of the attribute. Must not be null.
925 * @param aValues a nullptr-terminated array of pointers to atom values to
926 * test against.
927 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
928 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
929 * indicating the first value of aValues that matched
931 using AttrValuesArray = AttrArray::AttrValuesArray;
932 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
933 AttrArray::AttrValuesArray* aValues,
934 nsCaseTreatment aCaseSensitive) const;
937 * Set attribute values. All attribute values are assumed to have a
938 * canonical string representation that can be used for these
939 * methods. The SetAttr method is assumed to perform a translation
940 * of the canonical form into the underlying content specific
941 * form.
943 * @param aNameSpaceID the namespace of the attribute
944 * @param aName the name of the attribute
945 * @param aValue the value to set
946 * @param aNotify specifies how whether or not the document should be
947 * notified of the attribute change.
949 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
950 bool aNotify) {
951 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
953 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
954 const nsAString& aValue, bool aNotify) {
955 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
957 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
958 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
959 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
960 aNotify);
964 * Set attribute values. All attribute values are assumed to have a
965 * canonical String representation that can be used for these
966 * methods. The SetAttr method is assumed to perform a translation
967 * of the canonical form into the underlying content specific
968 * form.
970 * @param aNameSpaceID the namespace of the attribute
971 * @param aName the name of the attribute
972 * @param aPrefix the prefix of the attribute
973 * @param aValue the value to set
974 * @param aMaybeScriptedPrincipal the principal of the scripted caller
975 * responsible for setting the attribute, or null if no scripted caller can be
976 * determined. A null value here does not guarantee that there is no
977 * scripted caller, but a non-null value does guarantee that a scripted
978 * caller with the given principal is directly responsible for the
979 * attribute change.
980 * @param aNotify specifies how whether or not the document should be
981 * notified of the attribute change.
983 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
984 const nsAString& aValue,
985 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
988 * Remove an attribute so that it is no longer explicitly specified.
990 * @param aNameSpaceID the namespace id of the attribute
991 * @param aAttr the name of the attribute to unset
992 * @param aNotify specifies whether or not the document should be
993 * notified of the attribute change
995 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aAttribute, bool aNotify);
998 * Get the namespace / name / prefix of a given attribute.
1000 * @param aIndex the index of the attribute name
1001 * @returns The name at the given index, or null if the index is
1002 * out-of-bounds.
1003 * @note The document returned by NodeInfo()->GetDocument() (if one is
1004 * present) is *not* necessarily the owner document of the element.
1005 * @note The pointer returned by this function is only valid until the
1006 * next call of either GetAttrNameAt or SetAttr on the element.
1008 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
1009 return mAttrs.GetSafeAttrNameAt(aIndex);
1013 * Same as above, but does not do out-of-bounds checks!
1015 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
1016 return mAttrs.AttrNameAt(aIndex);
1020 * Gets the attribute info (name and value) for this element at a given index.
1022 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
1023 if (aIndex >= mAttrs.AttrCount()) {
1024 return BorrowedAttrInfo(nullptr, nullptr);
1027 return mAttrs.AttrInfoAt(aIndex);
1031 * Get the number of all specified attributes.
1033 * @return the number of attributes
1035 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
1037 virtual bool IsNodeOfType(uint32_t aFlags) const override;
1040 * Get the class list of this element (this corresponds to the value of the
1041 * class attribute). This may be null if there are no classes, but that's not
1042 * guaranteed (e.g. we could have class="").
1044 const nsAttrValue* GetClasses() const {
1045 if (!MayHaveClass()) {
1046 return nullptr;
1049 if (IsSVGElement()) {
1050 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
1051 return value;
1055 return GetParsedAttr(nsGkAtoms::_class);
1058 #ifdef MOZ_DOM_LIST
1059 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
1060 List(out, aIndent, ""_ns);
1062 virtual void DumpContent(FILE* out, int32_t aIndent,
1063 bool aDumpAll) const override;
1064 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1065 void ListAttributes(FILE* out) const;
1066 #endif
1069 * Append to aOutDescription a string describing the element and its
1070 * attributes.
1071 * If aShort is true, only the id and class attributes will be listed.
1073 void Describe(nsAString& aOutDescription, bool aShort = false) const;
1076 * Attribute Mapping Helpers
1078 struct MappedAttributeEntry {
1079 const nsStaticAtom* const attribute;
1083 * A common method where you can just pass in a list of maps to check
1084 * for attribute dependence. Most implementations of
1085 * IsAttributeMapped should use this function as a default
1086 * handler.
1088 template <size_t N>
1089 static bool FindAttributeDependence(
1090 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1091 return FindAttributeDependence(aAttribute, aMaps, N);
1094 static nsStaticAtom* const* HTMLSVGPropertiesToTraverseAndUnlink();
1096 private:
1097 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1099 static bool FindAttributeDependence(const nsAtom* aAttribute,
1100 const MappedAttributeEntry* const aMaps[],
1101 uint32_t aMapCount);
1103 protected:
1104 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1105 DOMString& aResult) const {
1106 NS_ASSERTION(nullptr != aName, "must have attribute name");
1107 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
1108 "must have a real namespace ID!");
1109 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1110 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1111 if (val) {
1112 val->ToString(aResult);
1113 return true;
1115 // else DOMString comes pre-emptied.
1116 return false;
1119 public:
1120 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1122 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1123 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1124 const nsAttrValue* val = mAttrs.GetAttr(aName);
1125 if (val) {
1126 val->ToString(aResult);
1127 return true;
1129 // else DOMString comes pre-emptied.
1130 return false;
1133 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1134 void GetId(nsAString& aId) const {
1135 GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
1137 void GetId(DOMString& aId) const {
1138 GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
1140 void SetId(const nsAString& aId) {
1141 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1143 void GetClassName(nsAString& aClassName) {
1144 GetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName);
1146 void GetClassName(DOMString& aClassName) {
1147 GetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName);
1149 void SetClassName(const nsAString& aClassName) {
1150 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1153 nsDOMTokenList* ClassList();
1154 nsDOMTokenList* Part();
1156 nsDOMAttributeMap* Attributes();
1158 void GetAttributeNames(nsTArray<nsString>& aResult);
1160 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1161 DOMString str;
1162 GetAttribute(aName, str);
1163 str.ToString(aReturn);
1166 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1167 void GetAttributeNS(const nsAString& aNamespaceURI,
1168 const nsAString& aLocalName, nsAString& aReturn);
1169 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1170 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1171 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1172 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1173 void SetAttributeNS(const nsAString& aNamespaceURI,
1174 const nsAString& aLocalName, const nsAString& aValue,
1175 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1176 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1177 ErrorResult& aError) {
1178 SetAttribute(aName, aValue, nullptr, aError);
1181 * This method creates a principal that subsumes this element's NodePrincipal
1182 * and which has flags set for elevated permissions that devtools needs to
1183 * operate on this element. The principal returned by this method is used by
1184 * various devtools methods to permit otherwise blocked operations, without
1185 * changing any other restrictions the NodePrincipal might have.
1187 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1188 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1189 ErrorResult& aError);
1190 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1191 const nsAString& aLocalName,
1192 const nsAString& aValue, ErrorResult& aError);
1194 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1195 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1196 const nsAString& aLocalName, ErrorResult& aError);
1197 bool HasAttribute(const nsAString& aName) const {
1198 return InternalGetAttrNameFromQName(aName) != nullptr;
1200 bool HasAttributeNS(const nsAString& aNamespaceURI,
1201 const nsAString& aLocalName) const;
1202 bool HasAttributes() const { return HasAttrs(); }
1203 Element* Closest(const nsACString& aSelector, ErrorResult& aResult);
1204 bool Matches(const nsACString& aSelector, ErrorResult& aError);
1205 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1206 const nsAString& aQualifiedName);
1207 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1208 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1209 ErrorResult& aError);
1210 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1211 const nsAString& aClassNames);
1213 PseudoStyleType GetPseudoElementType() const {
1214 nsresult rv = NS_OK;
1215 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1216 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1217 return PseudoStyleType::NotPseudo;
1219 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1222 void SetPseudoElementType(PseudoStyleType aPseudo) {
1223 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1224 "Need to be able to store this in a void*");
1225 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1226 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1230 * Return an array of all elements in the subtree rooted at this
1231 * element that have grid container frames. This does not include
1232 * pseudo-elements.
1234 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1237 * Provide a direct way to determine if this Element has visible
1238 * scrollbars. Flushes layout.
1240 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1242 private:
1244 * Implement the algorithm specified at
1245 * https://dom.spec.whatwg.org/#insert-adjacent for both
1246 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1248 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1249 ErrorResult& aError);
1251 public:
1252 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1253 ErrorResult& aError);
1255 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1256 ErrorResult& aError);
1258 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError);
1259 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError);
1260 bool HasPointerCapture(long aPointerId);
1261 void SetCapture(bool aRetargetToElement);
1263 void SetCaptureAlways(bool aRetargetToElement);
1265 void ReleaseCapture();
1267 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1268 void RequestPointerLock(CallerType aCallerType);
1269 Attr* GetAttributeNode(const nsAString& aName);
1270 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1271 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1272 ErrorResult& aError);
1273 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1274 const nsAString& aLocalName);
1275 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1276 ErrorResult& aError);
1278 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1279 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1281 // Shadow DOM v1
1282 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1283 ErrorResult& aError);
1284 bool CanAttachShadowDOM() const;
1286 enum class DelegatesFocus : bool { No, Yes };
1288 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1289 ShadowRootMode aMode, DelegatesFocus = DelegatesFocus::No,
1290 SlotAssignmentMode aSlotAssignmentMode = SlotAssignmentMode::Named);
1292 // Attach UA Shadow Root if it is not attached.
1293 enum class NotifyUAWidgetSetup : bool { No, Yes };
1294 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes,
1295 DelegatesFocus = DelegatesFocus::No);
1297 // Dispatch an event to UAWidgetsChild, triggering construction
1298 // or onchange callback on the existing widget.
1299 void NotifyUAWidgetSetupOrChange();
1301 enum class UnattachShadowRoot {
1303 Yes,
1306 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1307 // and optionally remove the shadow root.
1308 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1310 void UnattachShadow();
1312 ShadowRoot* GetShadowRootByMode() const;
1313 void SetSlot(const nsAString& aName, ErrorResult& aError);
1314 void GetSlot(nsAString& aName);
1316 ShadowRoot* GetShadowRoot() const {
1317 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1318 return slots ? slots->mShadowRoot.get() : nullptr;
1321 const Maybe<float> GetLastRememberedBSize() const {
1322 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1323 return slots ? slots->mLastRememberedBSize : Nothing();
1325 const Maybe<float> GetLastRememberedISize() const {
1326 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1327 return slots ? slots->mLastRememberedISize : Nothing();
1329 bool HasLastRememberedBSize() const {
1330 return GetLastRememberedBSize().isSome();
1332 bool HasLastRememberedISize() const {
1333 return GetLastRememberedISize().isSome();
1336 const Maybe<ContentRelevancy> GetContentRelevancy() const {
1337 const auto* slots = GetExistingExtendedDOMSlots();
1338 return slots ? slots->mContentRelevancy : Nothing();
1340 void SetContentRelevancy(ContentRelevancy relevancy) {
1341 ExtendedDOMSlots()->mContentRelevancy = Some(relevancy);
1344 const Maybe<bool> GetVisibleForContentVisibility() const {
1345 const auto* slots = GetExistingExtendedDOMSlots();
1346 return slots ? slots->mVisibleForContentVisibility : Nothing();
1348 void SetVisibleForContentVisibility(bool visible) {
1349 ExtendedDOMSlots()->mVisibleForContentVisibility = Some(visible);
1352 void ClearContentRelevancy() {
1353 if (auto* slots = GetExistingExtendedDOMSlots()) {
1354 slots->mContentRelevancy.reset();
1355 slots->mVisibleForContentVisibility.reset();
1359 // https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
1360 MOZ_CAN_RUN_SCRIPT bool CheckVisibility(const CheckVisibilityOptions&);
1362 private:
1363 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1364 // called from JS. Use PresShell::ScrollContentIntoView instead.
1365 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1367 public:
1368 MOZ_CAN_RUN_SCRIPT
1369 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1370 // called from JS. Use PresShell::ScrollContentIntoView instead.
1371 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1372 MOZ_CAN_RUN_SCRIPT void Scroll(double aXScroll, double aYScroll);
1373 MOZ_CAN_RUN_SCRIPT void Scroll(const ScrollToOptions& aOptions);
1374 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1375 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1376 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1377 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1378 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1379 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1380 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1381 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1382 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1383 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1384 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1385 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1386 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1388 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1389 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1391 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1392 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1394 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1395 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1398 MOZ_CAN_RUN_SCRIPT int32_t ScreenX();
1399 MOZ_CAN_RUN_SCRIPT int32_t ScreenY();
1400 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIScreen> GetScreen();
1402 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1403 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1404 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1405 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1407 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1408 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1411 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1412 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1415 // This function will return the block size of first line box, no matter if
1416 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1417 // can't get a primary frame, we will return be zero.
1418 double FirstLineBoxBSize() const;
1420 already_AddRefed<Flex> GetAsFlexContainer();
1421 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1423 bool HasGridFragments();
1425 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1426 Element& aAncestor);
1427 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1428 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1430 already_AddRefed<Animation> Animate(
1431 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1432 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1433 ErrorResult& aError);
1435 MOZ_CAN_RUN_SCRIPT
1436 void GetAnimations(const GetAnimationsOptions& aOptions,
1437 nsTArray<RefPtr<Animation>>& aAnimations);
1439 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1440 nsTArray<RefPtr<Animation>>& aAnimations);
1442 static void GetAnimationsUnsorted(Element* aElement,
1443 PseudoStyleType aPseudoType,
1444 nsTArray<RefPtr<Animation>>& aAnimations);
1446 void CloneAnimationsFrom(const Element& aOther);
1448 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1449 virtual void SetInnerHTML(const nsAString& aInnerHTML,
1450 nsIPrincipal* aSubjectPrincipal,
1451 ErrorResult& aError);
1452 void GetOuterHTML(nsAString& aOuterHTML);
1453 void SetOuterHTML(const nsAString& aOuterHTML, ErrorResult& aError);
1454 void InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
1455 ErrorResult& aError);
1457 void SetHTML(const nsAString& aInnerHTML, const SetHTMLOptions& aOptions,
1458 ErrorResult& aError);
1460 //----------------------------------------
1463 * Add a script event listener with the given event handler name
1464 * (like onclick) and with the value as JS
1465 * @param aEventName the event listener name
1466 * @param aValue the JS to attach
1467 * @param aDefer indicates if deferred execution is allowed
1469 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1470 bool aDefer = true);
1473 * Do whatever needs to be done when the mouse leaves a link
1475 nsresult LeaveLink(nsPresContext* aPresContext);
1477 static bool ShouldBlur(nsIContent* aContent);
1480 * Method to create and dispatch a left-click event loosely based on
1481 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1482 * through the full dispatching of the presshell of the aPresContext; if it's
1483 * false the event will be dispatched only as a DOM event.
1484 * If aPresContext is nullptr, this does nothing.
1486 * @param aFlags Extra flags for the dispatching event. The true flags
1487 * will be respected.
1489 MOZ_CAN_RUN_SCRIPT
1490 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1491 WidgetInputEvent* aSourceEvent,
1492 nsIContent* aTarget, bool aFullDispatch,
1493 const EventFlags* aFlags,
1494 nsEventStatus* aStatus);
1497 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1498 * will be dispatched through the full dispatching of the presshell of the
1499 * aPresContext; if it's false the event will be dispatched only as a DOM
1500 * event.
1501 * If aPresContext is nullptr, this does nothing.
1503 using nsIContent::DispatchEvent;
1504 MOZ_CAN_RUN_SCRIPT
1505 static nsresult DispatchEvent(nsPresContext* aPresContext,
1506 WidgetEvent* aEvent, nsIContent* aTarget,
1507 bool aFullDispatch, nsEventStatus* aStatus);
1509 bool IsDisplayContents() const {
1510 return HasServoData() && Servo_Element_IsDisplayContents(this);
1514 * https://html.spec.whatwg.org/#being-rendered
1516 * With a gotcha for display contents:
1517 * https://github.com/whatwg/html/issues/1837
1519 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1521 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1522 return mAttrs.GetAttr(aAttr);
1525 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1526 int32_t aNameSpaceID) const {
1527 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1531 * Returns the attribute map, if there is one.
1533 * @return existing attribute map or nullptr.
1535 nsDOMAttributeMap* GetAttributeMap() {
1536 nsDOMSlots* slots = GetExistingDOMSlots();
1538 return slots ? slots->mAttributeMap.get() : nullptr;
1541 void RecompileScriptEventListeners();
1544 * Get the attr info for the given namespace ID and attribute name. The
1545 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1546 * null. Note that this can only return info on attributes that actually
1547 * live on this element (and is only virtual to handle XUL prototypes). That
1548 * is, this should only be called from methods that only care about attrs
1549 * that effectively live in mAttrs.
1551 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1552 const nsAtom* aName) const {
1553 NS_ASSERTION(aName, "must have attribute name");
1554 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1555 "must have a real namespace ID!");
1557 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1558 if (index < 0) {
1559 return BorrowedAttrInfo(nullptr, nullptr);
1562 return mAttrs.AttrInfoAt(index);
1566 * Parse a string into an nsAttrValue for a CORS attribute. This
1567 * never fails. The resulting value is an enumerated value whose
1568 * GetEnumValue() returns one of the above constants.
1570 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1573 * Return the CORS mode for a given string
1575 static CORSMode StringToCORSMode(const nsAString& aValue);
1578 * Return the CORS mode for a given nsAttrValue (which may be null,
1579 * but if not should have been parsed via ParseCORSValue).
1581 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1583 nsINode* GetScopeChainParent() const override;
1586 * Locate a TextEditor rooted at this content node, if there is one.
1588 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1591 * Gets value of boolean attribute. Only works for attributes in null
1592 * namespace.
1594 * @param aAttr name of attribute.
1595 * @param aValue Boolean value of attribute.
1597 bool GetBoolAttr(nsAtom* aAttr) const {
1598 return HasAttr(kNameSpaceID_None, aAttr);
1602 * Sets value of boolean attribute by removing attribute or setting it to
1603 * the empty string. Only works for attributes in null namespace.
1605 * @param aAttr name of attribute.
1606 * @param aValue Boolean value of attribute.
1608 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1611 * Gets the enum value string of an attribute and using a default value if
1612 * the attribute is missing or the string is an invalid enum value.
1614 * @param aType the name of the attribute.
1615 * @param aDefault the default value if the attribute is missing or invalid.
1616 * @param aResult string corresponding to the value [out].
1618 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1619 nsAString& aResult) const;
1622 * Gets the enum value string of an attribute and using the default missing
1623 * value if the attribute is missing or the default invalid value if the
1624 * string is an invalid enum value.
1626 * @param aType the name of the attribute.
1627 * @param aDefaultMissing the default value if the attribute is missing. If
1628 null and the attribute is missing, aResult will be
1629 set to the null DOMString; this only matters for
1630 cases in which we're reflecting a nullable string.
1631 * @param aDefaultInvalid the default value if the attribute is invalid.
1632 * @param aResult string corresponding to the value [out].
1634 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1635 const char* aDefaultInvalid, nsAString& aResult) const;
1638 * Unset an attribute.
1640 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1641 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1645 * Set an attribute in the simplest way possible.
1647 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1648 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1651 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1652 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1653 aError =
1654 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1658 * Set a content attribute via a reflecting nullable string IDL
1659 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1660 * this will actually remove the content attribute.
1662 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1663 ErrorResult& aError);
1666 * Retrieve the ratio of font-size-inflated text font size to computed font
1667 * size for this element. This will query the element for its primary frame,
1668 * and then use this to get font size inflation information about the frame.
1670 * @returns The font size inflation ratio (inflated font size to uninflated
1671 * font size) for the primary frame of this element. Returns 1.0
1672 * by default if font size inflation is not enabled. Returns -1
1673 * if the element does not have a primary frame.
1675 * @note The font size inflation ratio that is returned is actually the
1676 * font size inflation data for the element's _primary frame_, not the
1677 * element itself, but for most purposes, this should be sufficient.
1679 float FontSizeInflation();
1681 void GetImplementedPseudoElement(nsAString&) const;
1683 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1684 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1687 * Helpers for .dataset. This is implemented on Element, though only some
1688 * sorts of elements expose it to JS as a .dataset property
1690 // Getter, to be called from bindings.
1691 already_AddRefed<nsDOMStringMap> Dataset();
1692 // Callback for destructor of dataset to ensure to null out our weak pointer
1693 // to it.
1694 void ClearDataset();
1696 void RegisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1697 void UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1698 void UnlinkIntersectionObservers();
1699 bool UpdateIntersectionObservation(DOMIntersectionObserver* aObserver,
1700 int32_t threshold);
1702 // A number of methods to cast to various XUL interfaces. They return a
1703 // pointer only if the element implements that interface.
1704 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1705 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1706 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1707 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1708 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1709 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1710 AsXULMultiSelectControl();
1711 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1712 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1713 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1714 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1715 already_AddRefed<nsIBrowser> AsBrowser();
1716 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1719 * Get the presentation context for this content node.
1720 * @return the presentation context
1722 enum PresContextFor { eForComposedDoc, eForUncomposedDoc };
1723 nsPresContext* GetPresContext(PresContextFor aFor);
1726 * The method focuses (or activates) element that accesskey is bound to. It is
1727 * called when accesskey is activated.
1729 * @param aKeyCausesActivation - if true then element should be activated
1730 * @param aIsTrustedEvent - if true then event that is cause of accesskey
1731 * execution is trusted.
1732 * @return an error if the element isn't able to handle the accesskey (caller
1733 * would look for the next element to handle it).
1734 * a boolean indicates whether the focus moves to the element after
1735 * the element handles the accesskey.
1737 MOZ_CAN_RUN_SCRIPT
1738 virtual Result<bool, nsresult> PerformAccesskey(bool aKeyCausesActivation,
1739 bool aIsTrustedEvent) {
1740 return Err(NS_ERROR_NOT_IMPLEMENTED);
1743 protected:
1745 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1746 * read.
1748 static const bool kFireMutationEvent = true;
1749 static const bool kDontFireMutationEvent = false;
1750 static const bool kNotifyDocumentObservers = true;
1751 static const bool kDontNotifyDocumentObservers = false;
1752 static const bool kCallAfterSetAttr = true;
1753 static const bool kDontCallAfterSetAttr = false;
1756 * Set attribute and (if needed) notify documentobservers and fire off
1757 * mutation events. This will send the AttributeChanged notification.
1758 * Callers of this method are responsible for calling AttributeWillChange,
1759 * since that needs to happen before the new attr value has been set, and
1760 * in particular before it has been parsed.
1762 * For the boolean parameters, consider using the named bools above to aid
1763 * code readability.
1765 * @param aNamespaceID namespace of attribute
1766 * @param aAttribute local-name of attribute
1767 * @param aPrefix aPrefix of attribute
1768 * @param aOldValue The old value of the attribute to use as a fallback
1769 * in the cases where the actual old value (i.e.
1770 * its current value) is !StoresOwnData() --- in which
1771 * case the current value is probably already useless.
1772 * If the current value is StoresOwnData() (or absent),
1773 * aOldValue will not be used. aOldValue will only be set
1774 * in certain circumstances (there are mutation
1775 * listeners, element is a custom element, attribute was
1776 * not previously unset). Otherwise it will be null.
1777 * @param aParsedValue parsed new value of attribute. Replaced by the
1778 * old value of the attribute. This old value is only
1779 * useful if either it or the new value is StoresOwnData.
1780 * @param aMaybeScriptedPrincipal
1781 * the principal of the scripted caller responsible for
1782 * setting the attribute, or null if no scripted caller
1783 * can be determined. A null value here does not
1784 * guarantee that there is no scripted caller, but a
1785 * non-null value does guarantee that a scripted caller
1786 * with the given principal is directly responsible for
1787 * the attribute change.
1788 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
1789 * needed if aFireMutation or aNotify is true.
1790 * @param aFireMutation should mutation-events be fired?
1791 * @param aNotify should we notify document-observers?
1792 * @param aCallAfterSetAttr should we call AfterSetAttr?
1793 * @param aComposedDocument The current composed document of the element.
1795 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
1796 nsAtom* aPrefix, const nsAttrValue* aOldValue,
1797 nsAttrValue& aParsedValue,
1798 nsIPrincipal* aMaybeScriptedPrincipal,
1799 uint8_t aModType, bool aFireMutation, bool aNotify,
1800 bool aCallAfterSetAttr, Document* aComposedDocument,
1801 const mozAutoDocUpdate& aGuard);
1804 * Scroll to a new position using behavior evaluated from CSS and
1805 * a CSSOM-View DOM method ScrollOptions dictionary. The scrolling may
1806 * be performed asynchronously or synchronously depending on the resolved
1807 * scroll-behavior.
1809 * @param aScroll Destination of scroll, in CSS pixels
1810 * @param aOptions Dictionary of options to be evaluated
1812 MOZ_CAN_RUN_SCRIPT
1813 void Scroll(const CSSIntPoint& aScroll, const ScrollOptions& aOptions);
1816 * Convert an attribute string value to attribute type based on the type of
1817 * attribute. Called by SetAttr(). Note that at the moment we only do this
1818 * for attributes in the null namespace (kNameSpaceID_None).
1820 * @param aNamespaceID the namespace of the attribute to convert
1821 * @param aAttribute the attribute to convert
1822 * @param aValue the string value to convert
1823 * @param aMaybeScriptedPrincipal the principal of the script setting the
1824 * attribute, if one can be determined, or null otherwise. As in
1825 * AfterSetAttr, a null value does not guarantee that the attribute was
1826 * not set by a scripted caller, but a non-null value guarantees that
1827 * the attribute was set by a scripted caller with the given principal.
1828 * @param aResult the nsAttrValue [OUT]
1829 * @return true if the parsing was successful, false otherwise
1831 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1832 const nsAString& aValue,
1833 nsIPrincipal* aMaybeScriptedPrincipal,
1834 nsAttrValue& aResult);
1837 * Try to set the attribute as a mapped attribute, if applicable. This will
1838 * only be called for attributes that are in the null namespace and only on
1839 * attributes that returned true when passed to IsAttributeMapped. The
1840 * caller will not try to set the attr in any other way if this method
1841 * returns true (the value of aRetval does not matter for that purpose).
1843 * @param aName the name of the attribute
1844 * @param aValue the nsAttrValue to set. Will be swapped with the existing
1845 * value of the attribute if the attribute already exists.
1846 * @param [out] aValueWasSet If the attribute was not set previously,
1847 * aValue will be swapped with an empty attribute
1848 * and aValueWasSet will be set to false. Otherwise,
1849 * aValueWasSet will be set to true and aValue will
1850 * contain the previous value set.
1851 * @param [out] aRetval the nsresult status of the operation, if any.
1852 * @return true if the setting was attempted, false otherwise.
1854 virtual bool SetAndSwapMappedAttribute(nsAtom* aName, nsAttrValue& aValue,
1855 bool* aValueWasSet, nsresult* aRetval);
1858 * Hook that is called by Element::SetAttr to allow subclasses to
1859 * deal with attribute sets. This will only be called after we verify that
1860 * we're actually doing an attr set and will be called before
1861 * AttributeWillChange and before ParseAttribute and hence before we've set
1862 * the new value.
1864 * @param aNamespaceID the namespace of the attr being set
1865 * @param aName the localname of the attribute being set
1866 * @param aValue the value it's being set to represented as either a string or
1867 * a parsed nsAttrValue. Alternatively, if the attr is being removed it
1868 * will be null.
1869 * @param aNotify Whether we plan to notify document observers.
1871 virtual nsresult BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1872 const nsAttrValueOrString* aValue,
1873 bool aNotify);
1876 * Hook that is called by Element::SetAttr to allow subclasses to
1877 * deal with attribute sets. This will only be called after we have called
1878 * SetAndSwapAttr (that is, after we have actually set the attr). It will
1879 * always be called under a scriptblocker.
1881 * @param aNamespaceID the namespace of the attr being set
1882 * @param aName the localname of the attribute being set
1883 * @param aValue the value it's being set to. If null, the attr is being
1884 * removed.
1885 * @param aOldValue the value that the attribute had previously. If null,
1886 * the attr was not previously set. This argument may not have the
1887 * correct value for SVG elements, or other cases in which the
1888 * attribute value doesn't store its own data
1889 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1890 * responsible for setting the attribute, or null if no scripted caller
1891 * can be determined, or the attribute is being unset. A null value
1892 * here does not guarantee that there is no scripted caller, but a
1893 * non-null value does guarantee that a scripted caller with the given
1894 * principal is directly responsible for the attribute change.
1895 * @param aNotify Whether we plan to notify document observers.
1897 virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
1898 const nsAttrValue* aValue,
1899 const nsAttrValue* aOldValue,
1900 nsIPrincipal* aMaybeScriptedPrincipal,
1901 bool aNotify);
1904 * This function shall be called just before the id attribute changes. It will
1905 * be called after BeforeSetAttr. If the attribute being changed is not the id
1906 * attribute, this function does nothing. Otherwise, it will remove the old id
1907 * from the document's id cache.
1909 * This must happen after BeforeSetAttr (rather than during) because the
1910 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
1911 * incorrectly determine whether the element had an id, the element may not be
1912 * restyled properly.
1914 * @param aNamespaceID the namespace of the attr being set
1915 * @param aName the localname of the attribute being set
1916 * @param aValue the new id value. Will be null if the id is being unset.
1918 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1919 const nsAttrValueOrString* aValue);
1922 * This function shall be called just after the id attribute changes. It will
1923 * be called before AfterSetAttr. If the attribute being changed is not the id
1924 * attribute, this function does nothing. Otherwise, it will add the new id to
1925 * the document's id cache and properly set the ElementHasID flag.
1927 * This must happen before AfterSetAttr (rather than during) because the
1928 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
1929 * incorrectly determine whether the element now has an id, the element may
1930 * not be restyled properly.
1932 * @param aNamespaceID the namespace of the attr being set
1933 * @param aName the localname of the attribute being set
1934 * @param aValue the new id value. Will be null if the id is being unset.
1936 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1937 const nsAttrValue* aValue);
1940 * Usually, setting an attribute to the value that it already has results in
1941 * no action. However, in some cases, setting an attribute to its current
1942 * value should have the effect of, for example, forcing a reload of
1943 * network data. To address that, this function will be called in this
1944 * situation to allow the handling of such a case.
1946 * @param aNamespaceID the namespace of the attr being set
1947 * @param aName the localname of the attribute being set
1948 * @param aValue the value it's being set to represented as either a string or
1949 * a parsed nsAttrValue.
1950 * @param aNotify Whether we plan to notify document observers.
1952 // Note that this is inlined so that when subclasses call it it gets
1953 // inlined. Those calls don't go through a vtable.
1954 virtual nsresult OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
1955 const nsAttrValueOrString& aValue,
1956 bool aNotify);
1959 * Hook to allow subclasses to produce a different EventListenerManager if
1960 * needed for attachment of attribute-defined handlers
1962 virtual EventListenerManager* GetEventListenerManagerForAttr(
1963 nsAtom* aAttrName, bool* aDefer);
1966 * Internal hook for converting an attribute name-string to nsAttrName in
1967 * case there is such existing attribute. aNameToUse can be passed to get
1968 * name which was used for looking for the attribute (lowercase in HTML).
1970 const nsAttrName* InternalGetAttrNameFromQName(
1971 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
1973 virtual Element* GetNameSpaceElement() override { return this; }
1975 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
1976 const nsAString& aLocalName);
1978 inline void RegisterActivityObserver();
1979 inline void UnregisterActivityObserver();
1982 * Add/remove this element to the documents id cache
1984 void AddToIdTable(nsAtom* aId);
1985 void RemoveFromIdTable();
1988 * Functions to carry out event default actions for links of all types
1989 * (HTML links, XLinks, SVG "XLinks", etc.)
1993 * Check that we meet the conditions to handle a link event
1994 * and that we are actually on a link.
1996 * @param aVisitor event visitor
1997 * @return true if we can handle the link event, false otherwise
1999 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor) const;
2002 * Handle status bar updates before they can be cancelled.
2004 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
2006 void DispatchChromeOnlyLinkClickEvent(EventChainPostVisitor& aVisitor);
2009 * Handle default actions for link event if the event isn't consumed yet.
2011 MOZ_CAN_RUN_SCRIPT
2012 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
2014 public:
2016 * Check if this element is a link. This matches the CSS definition of the
2017 * :any-link pseudo-class.
2019 bool IsLink() const {
2020 return mState.HasAtLeastOneOfStates(ElementState::VISITED |
2021 ElementState::UNVISITED);
2025 * Get a pointer to the full href URI (fully resolved and canonicalized, since
2026 * it's an nsIURI object) for link elements.
2028 * @return A pointer to the URI or null if the element is not a link, or it
2029 * has no HREF attribute, or the HREF attribute is an invalid URI.
2031 virtual already_AddRefed<nsIURI> GetHrefURI() const { return nullptr; }
2034 * Get the target of this link element. Consumers should established that
2035 * this element is a link (probably using IsLink) before calling this
2036 * function (or else why call it?)
2038 * Note: for HTML this gets the value of the 'target' attribute; for XLink
2039 * this gets the value of the xlink:_moz_target attribute, or failing that,
2040 * the value of xlink:show, converted to a suitably equivalent named target
2041 * (e.g. _blank).
2043 virtual void GetLinkTarget(nsAString& aTarget);
2045 virtual bool Translate() const;
2047 protected:
2048 enum class ReparseAttributes { No, Yes };
2050 * Copy attributes and state to another element
2051 * @param aDest the object to copy to
2053 nsresult CopyInnerTo(Element* aDest,
2054 ReparseAttributes = ReparseAttributes::Yes);
2057 * Some event handler content attributes have a different name (e.g. different
2058 * case) from the actual event name. This function takes an event handler
2059 * content attribute name and returns the corresponding event name, to be used
2060 * for adding the actual event listener.
2062 virtual nsAtom* GetEventNameForAttr(nsAtom* aAttr);
2065 * Register/unregister this element to accesskey map if it supports accesskey.
2067 virtual void RegUnRegAccessKey(bool aDoReg);
2069 private:
2070 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
2071 void AssertInvariantsOnNodeInfoChange();
2072 #endif
2075 * Slow path for GetClasses, this should only be called for SVG elements.
2077 const nsAttrValue* GetSVGAnimatedClass() const;
2080 * Get this element's client area rect in app units.
2081 * @return the frame's client area
2083 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
2086 * GetCustomInterface is somewhat like a GetInterface, but it is expected
2087 * that the implementation is provided by a custom element or via the
2088 * the XBL implements keyword. To use this, create a public method that
2089 * wraps a call to GetCustomInterface.
2091 template <class T>
2092 void GetCustomInterface(nsGetterAddRefs<T> aResult);
2094 // Prevent people from doing pointless checks/casts on Element instances.
2095 void IsElement() = delete;
2096 void AsElement() = delete;
2098 // Data members
2099 ElementState mState;
2100 // Per-node data managed by Servo.
2102 // There should not be data on nodes that are not in the flattened tree, or
2103 // descendants of display: none elements.
2104 mozilla::RustCell<ServoNodeData*> mServoData;
2106 protected:
2107 // Array containing all attributes for this element
2108 AttrArray mAttrs;
2111 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
2113 inline bool Element::HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
2114 NS_ASSERTION(nullptr != aName, "must have attribute name");
2115 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
2116 "must have a real namespace ID!");
2118 return mAttrs.IndexOfAttr(aName, aNameSpaceID) >= 0;
2121 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2122 const nsAtom* aName) const {
2123 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2124 MOZ_ASSERT(aName, "Must have attribute name");
2126 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2127 return val && !val->IsEmptyString();
2130 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2131 const nsAString& aValue,
2132 nsCaseTreatment aCaseSensitive) const {
2133 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2136 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2137 const nsAtom* aValue,
2138 nsCaseTreatment aCaseSensitive) const {
2139 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2142 } // namespace dom
2143 } // namespace mozilla
2145 inline mozilla::dom::Element* nsINode::AsElement() {
2146 MOZ_ASSERT(IsElement());
2147 return static_cast<mozilla::dom::Element*>(this);
2150 inline const mozilla::dom::Element* nsINode::AsElement() const {
2151 MOZ_ASSERT(IsElement());
2152 return static_cast<const mozilla::dom::Element*>(this);
2155 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2156 return mozilla::dom::Element::FromNodeOrNull(mParent);
2159 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2160 nsIContent* previousSibling = GetPreviousSibling();
2161 while (previousSibling) {
2162 if (previousSibling->IsElement()) {
2163 return previousSibling->AsElement();
2165 previousSibling = previousSibling->GetPreviousSibling();
2168 return nullptr;
2171 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2172 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2173 : GetParentElement();
2176 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2177 nsIContent* nextSibling = GetNextSibling();
2178 while (nextSibling) {
2179 if (nextSibling->IsElement()) {
2180 return nextSibling->AsElement();
2182 nextSibling = nextSibling->GetNextSibling();
2185 return nullptr;
2189 * Macros to implement Clone(). _elementName is the class for which to implement
2190 * Clone.
2192 #define NS_IMPL_ELEMENT_CLONE(_elementName) \
2193 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2194 nsINode** aResult) const { \
2195 *aResult = nullptr; \
2196 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2197 auto* nim = ni->NodeInfoManager(); \
2198 RefPtr<_elementName> it = new (nim) _elementName(ni.forget()); \
2199 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2200 if (NS_SUCCEEDED(rv)) { \
2201 it.forget(aResult); \
2204 return rv; \
2207 #define EXPAND(...) __VA_ARGS__
2208 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, extra_args_) \
2209 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2210 nsINode** aResult) const { \
2211 *aResult = nullptr; \
2212 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2213 auto* nim = ni->NodeInfoManager(); \
2214 RefPtr<_elementName> it = \
2215 new (nim) _elementName(ni.forget() EXPAND extra_args_); \
2216 nsresult rv = it->Init(); \
2217 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2218 if (NS_FAILED(rv2)) { \
2219 rv = rv2; \
2221 if (NS_SUCCEEDED(rv)) { \
2222 it.forget(aResult); \
2225 return rv; \
2228 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName) \
2229 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, ())
2230 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2231 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, (, NOT_FROM_PARSER))
2233 #endif // mozilla_dom_Element_h__