Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / Element.h
blobfb88274a88ddfbb7ca4017f7597aee7a2e97c79a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Base class for all element classes; this provides an implementation
9 * of DOM Core's Element, implements nsIContent, provides
10 * utility methods for subclasses, and so forth.
13 #ifndef mozilla_dom_Element_h__
14 #define mozilla_dom_Element_h__
16 #include <cstdio>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <utility>
20 #include "AttrArray.h"
21 #include "ErrorList.h"
22 #include "Units.h"
23 #include "js/RootingAPI.h"
24 #include "mozilla/AlreadyAddRefed.h"
25 #include "mozilla/Assertions.h"
26 #include "mozilla/Attributes.h"
27 #include "mozilla/BasicEvents.h"
28 #include "mozilla/CORSMode.h"
29 #include "mozilla/FlushType.h"
30 #include "mozilla/Maybe.h"
31 #include "mozilla/PseudoStyleType.h"
32 #include "mozilla/RefPtr.h"
33 #include "mozilla/Result.h"
34 #include "mozilla/RustCell.h"
35 #include "mozilla/UniquePtr.h"
36 #include "mozilla/dom/BorrowedAttrInfo.h"
37 #include "mozilla/dom/DOMString.h"
38 #include "mozilla/dom/DOMTokenListSupportedTokens.h"
39 #include "mozilla/dom/DirectionalityUtils.h"
40 #include "mozilla/dom/FragmentOrElement.h"
41 #include "mozilla/dom/NameSpaceConstants.h"
42 #include "mozilla/dom/NodeInfo.h"
43 #include "mozilla/dom/RustTypes.h"
44 #include "mozilla/dom/ShadowRootBinding.h"
45 #include "nsAtom.h"
46 #include "nsAttrValue.h"
47 #include "nsAttrValueInlines.h"
48 #include "nsCaseTreatment.h"
49 #include "nsChangeHint.h"
50 #include "nsTHashMap.h"
51 #include "nsDebug.h"
52 #include "nsError.h"
53 #include "nsGkAtoms.h"
54 #include "nsHashKeys.h"
55 #include "nsIContent.h"
56 #include "nsID.h"
57 #include "nsINode.h"
58 #include "nsLiteralString.h"
59 #include "nsRect.h"
60 #include "nsString.h"
61 #include "nsStringFlags.h"
62 #include "nsTLiteralString.h"
63 #include "nscore.h"
65 class JSObject;
66 class mozAutoDocUpdate;
67 class nsAttrName;
68 class nsAttrValueOrString;
69 class nsContentList;
70 class nsDOMAttributeMap;
71 class nsDOMCSSAttributeDeclaration;
72 class nsDOMStringMap;
73 class nsDOMTokenList;
74 class nsFocusManager;
75 class nsGenericHTMLFormControlElementWithState;
76 class nsGlobalWindowInner;
77 class nsGlobalWindowOuter;
78 class nsImageLoadingContent;
79 class nsIAutoCompletePopup;
80 class nsIBrowser;
81 class nsIDOMXULButtonElement;
82 class nsIDOMXULContainerElement;
83 class nsIDOMXULContainerItemElement;
84 class nsIDOMXULControlElement;
85 class nsIDOMXULMenuListElement;
86 class nsIDOMXULMultiSelectControlElement;
87 class nsIDOMXULRadioGroupElement;
88 class nsIDOMXULRelatedElement;
89 class nsIDOMXULSelectControlElement;
90 class nsIDOMXULSelectControlItemElement;
91 class nsIFrame;
92 class nsIHTMLCollection;
93 class nsIPrincipal;
94 class nsIScreen;
95 class 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 EditorBase;
111 class ErrorResult;
112 class OOMReporter;
113 class SMILAttr;
114 struct MutationClosureData;
115 class TextEditor;
116 namespace css {
117 struct URLValue;
118 } // namespace css
119 namespace dom {
120 struct CheckVisibilityOptions;
121 struct CustomElementData;
122 struct SetHTMLOptions;
123 struct GetAnimationsOptions;
124 struct ScrollIntoViewOptions;
125 struct ScrollToOptions;
126 struct FocusOptions;
127 struct ShadowRootInit;
128 struct ScrollOptions;
129 class Attr;
130 class BooleanOrScrollIntoViewOptions;
131 class Document;
132 class HTMLFormElement;
133 class DOMIntersectionObserver;
134 class DOMMatrixReadOnly;
135 class Element;
136 class ElementOrCSSPseudoElement;
137 class PopoverData;
138 class Promise;
139 class Sanitizer;
140 class ShadowRoot;
141 class UnrestrictedDoubleOrKeyframeAnimationOptions;
142 template <typename T>
143 class Optional;
144 enum class CallerType : uint32_t;
145 enum class ReferrerPolicy : uint8_t;
146 typedef nsTHashMap<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>
147 IntersectionObserverList;
148 } // namespace dom
149 } // namespace mozilla
151 using nsMapRuleToAttributesFunc = void (*)(mozilla::MappedDeclarationsBuilder&);
153 // Declared here because of include hell.
154 extern "C" bool Servo_Element_IsDisplayContents(const mozilla::dom::Element*);
156 already_AddRefed<nsContentList> NS_GetContentList(nsINode* aRootNode,
157 int32_t aMatchNameSpaceId,
158 const nsAString& aTagname);
160 #define ELEMENT_FLAG_BIT(n_) \
161 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
163 // Element-specific flags
164 enum : uint32_t {
165 // Whether this node has dirty descendants for Servo's style system.
166 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(0),
167 // Whether this node has dirty descendants for animation-only restyle for
168 // Servo's style system.
169 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(1),
171 // Whether the element has been snapshotted due to attribute or state changes
172 // by the Servo restyle manager.
173 ELEMENT_HAS_SNAPSHOT = ELEMENT_FLAG_BIT(2),
175 // Whether the element has already handled its relevant snapshot.
177 // Used by the servo restyle process in order to accurately track whether the
178 // style of an element is up-to-date, even during the same restyle process.
179 ELEMENT_HANDLED_SNAPSHOT = ELEMENT_FLAG_BIT(3),
181 // If this flag is set on an element, that means that it is a HTML datalist
182 // element or has a HTML datalist element ancestor.
183 ELEMENT_IS_DATALIST_OR_HAS_DATALIST_ANCESTOR = ELEMENT_FLAG_BIT(4),
185 // If this flag is set on an element, that means this element
186 // has been considered by our LargestContentfulPaint algorithm and
187 // it's not going to be considered again.
188 ELEMENT_PROCESSED_BY_LCP_FOR_TEXT = ELEMENT_FLAG_BIT(5),
190 // If this flag is set on an element, this means the HTML parser encountered
191 // a duplicate attribute error:
192 // https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute
193 // This flag is used for detecting dangling markup attacks in the CSP
194 // algorithm https://w3c.github.io/webappsec-csp/#is-element-nonceable.
195 ELEMENT_PARSER_HAD_DUPLICATE_ATTR_ERROR = ELEMENT_FLAG_BIT(6),
197 // Remaining bits are for subclasses
198 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET = NODE_TYPE_SPECIFIC_BITS_OFFSET + 7
201 #undef ELEMENT_FLAG_BIT
203 // Make sure we have space for our bits
204 ASSERT_NODE_FLAGS_SPACE(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
206 namespace mozilla {
207 enum class PseudoStyleType : uint8_t;
208 class EventChainPostVisitor;
209 class EventChainPreVisitor;
210 class EventChainVisitor;
211 class EventListenerManager;
212 class EventStateManager;
214 namespace dom {
216 struct CustomElementDefinition;
217 class Animation;
218 class CustomElementRegistry;
219 class Link;
220 class DOMRect;
221 class DOMRectList;
222 class Flex;
223 class Grid;
225 // IID for the dom::Element interface
226 #define NS_ELEMENT_IID \
228 0xc67ed254, 0xfd3b, 0x4b10, { \
229 0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1 \
233 #define REFLECT_NULLABLE_DOMSTRING_ATTR(method, attr) \
234 void Get##method(nsAString& aValue) const { \
235 const nsAttrValue* val = mAttrs.GetAttr(nsGkAtoms::attr); \
236 if (!val) { \
237 SetDOMStringToNull(aValue); \
238 return; \
240 val->ToString(aValue); \
242 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
243 SetOrRemoveNullableStringAttr(nsGkAtoms::attr, aValue, aRv); \
246 #define REFLECT_NULLABLE_ELEMENT_ATTR(method, attr) \
247 Element* Get##method() const { \
248 return GetAttrAssociatedElement(nsGkAtoms::attr); \
251 void Set##method(Element* aElement) { \
252 ExplicitlySetAttrElement(nsGkAtoms::attr, aElement); \
255 class Element : public FragmentOrElement {
256 public:
257 #ifdef MOZILLA_INTERNAL_API
258 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
259 : FragmentOrElement(std::move(aNodeInfo)),
260 mState(ElementState::READONLY | ElementState::DEFINED |
261 ElementState::LTR) {
262 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
263 "Bad NodeType in aNodeInfo");
264 SetIsElement();
267 ~Element() {
268 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
271 #endif // MOZILLA_INTERNAL_API
273 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
275 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
277 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
279 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
282 * Method to get the full state of this element. See dom/base/rust/lib.rs for
283 * the possible bits that could be set here.
285 ElementState State() const { return mState; }
288 * Returns the current disabled state of the element.
290 bool IsDisabled() const { return State().HasState(ElementState::DISABLED); }
291 bool IsReadOnly() const { return State().HasState(ElementState::READONLY); }
292 bool IsDisabledOrReadOnly() const {
293 return State().HasAtLeastOneOfStates(ElementState::DISABLED |
294 ElementState::READONLY);
297 virtual int32_t TabIndexDefault() { return -1; }
300 * Get tabIndex of this element. If not found, return TabIndexDefault.
302 int32_t TabIndex();
305 * Get the parsed value of tabindex attribute.
307 Maybe<int32_t> GetTabIndexAttrValue();
310 * Set tabIndex value to this element.
312 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
315 * Sets the ShadowRoot binding for this element. The contents of the
316 * binding is rendered in place of this node's children.
318 * @param aShadowRoot The ShadowRoot to be bound to this element.
320 void SetShadowRoot(ShadowRoot* aShadowRoot);
322 void SetLastRememberedBSize(float aBSize);
323 void SetLastRememberedISize(float aISize);
324 void RemoveLastRememberedBSize();
325 void RemoveLastRememberedISize();
328 * Make focus on this element.
330 // TODO: Convert Focus() to MOZ_CAN_RUN_SCRIPT and get rid of the
331 // kungFuDeathGrip in it.
332 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Focus(const FocusOptions& aOptions,
333 const CallerType aCallerType,
334 ErrorResult& aError);
337 * Show blur and clear focus.
339 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Blur(mozilla::ErrorResult& aError);
342 * The style state of this element. This is the real state of the element
343 * with any style locks applied for pseudo-class inspecting.
345 ElementState StyleState() const {
346 if (!HasLockedStyleStates()) {
347 return mState;
349 return StyleStateFromLocks();
353 * StyleStateLocks is used to specify which event states should be locked,
354 * and whether they should be locked to on or off.
356 struct StyleStateLocks {
357 // mLocks tracks which event states should be locked.
358 ElementState mLocks;
359 // mValues tracks if the locked state should be on or off.
360 ElementState mValues;
364 * The style state locks applied to this element.
366 StyleStateLocks LockedStyleStates() const;
369 * Add a style state lock on this element.
370 * aEnabled is the value to lock the given state bits to.
372 void LockStyleStates(ElementState aStates, bool aEnabled);
375 * Remove a style state lock on this element.
377 void UnlockStyleStates(ElementState aStates);
380 * Clear all style state locks on this element.
382 void ClearStyleStateLocks();
385 * Accessors for the state of our dir attribute.
387 bool HasDirAuto() const {
388 return State().HasState(ElementState::HAS_DIR_ATTR_LIKE_AUTO);
392 * Elements with dir="rtl" or dir="ltr".
394 bool HasFixedDir() const {
395 return State().HasAtLeastOneOfStates(ElementState::HAS_DIR_ATTR_LTR |
396 ElementState::HAS_DIR_ATTR_RTL);
400 * Get the inline style declaration, if any, for this element.
402 DeclarationBlock* GetInlineStyleDeclaration() const;
405 * Get the mapped attributes, if any, for this element.
407 StyleLockedDeclarationBlock* GetMappedAttributeStyle() const {
408 return mAttrs.GetMappedDeclarationBlock();
411 bool IsPendingMappedAttributeEvaluation() const {
412 return mAttrs.IsPendingMappedAttributeEvaluation();
415 void SetMappedDeclarationBlock(already_AddRefed<StyleLockedDeclarationBlock>);
418 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
419 * so that the element implementation can access the old style attribute
420 * value.
422 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
425 * Set the inline style declaration for this element.
427 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
428 MutationClosureData& aData);
431 * Get the SMIL override style declaration for this element. If the
432 * rule hasn't been created, this method simply returns null.
434 DeclarationBlock* GetSMILOverrideStyleDeclaration();
437 * Set the SMIL override style declaration for this element. This method will
438 * notify the document's pres context, so that the style changes will be
439 * noticed.
441 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
444 * Returns a new SMILAttr that allows the caller to animate the given
445 * attribute on this element.
447 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
448 nsAtom* aName);
451 * Get the SMIL override style for this element. This is a style declaration
452 * that is applied *after* the inline style, and it can be used e.g. to store
453 * animated style values.
455 * Note: This method is analogous to the 'GetStyle' method in
456 * nsGenericHTMLElement and nsStyledElement.
458 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
461 * Returns if the element is labelable as per HTML specification.
463 virtual bool IsLabelable() const;
466 * Returns if the element is interactive content as per HTML specification.
468 virtual bool IsInteractiveHTMLContent() const;
471 * Is the attribute named aAttribute a mapped attribute?
473 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
475 nsresult BindToTree(BindContext&, nsINode& aParent) override;
476 void UnbindFromTree(UnbindContext&) override;
477 using nsIContent::UnbindFromTree;
479 virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
480 static void MapNoAttributesInto(mozilla::MappedDeclarationsBuilder&);
483 * Get a hint that tells the style system what to do when
484 * an attribute on this node changes, if something needs to happen
485 * in response to the change *other* than the result of what is
486 * mapped into style data via any type of style rule.
488 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
489 int32_t aModType) const;
491 inline Directionality GetDirectionality() const {
492 ElementState state = State();
493 if (state.HasState(ElementState::RTL)) {
494 return Directionality::Rtl;
496 if (state.HasState(ElementState::LTR)) {
497 return Directionality::Ltr;
499 return Directionality::Unset;
502 inline void SetDirectionality(Directionality aDir, bool aNotify) {
503 AutoStateChangeNotifier notifier(*this, aNotify);
504 RemoveStatesSilently(ElementState::DIR_STATES);
505 switch (aDir) {
506 case Directionality::Rtl:
507 AddStatesSilently(ElementState::RTL);
508 break;
509 case Directionality::Ltr:
510 AddStatesSilently(ElementState::LTR);
511 break;
512 case Directionality::Unset:
513 case Directionality::Auto:
514 MOZ_ASSERT_UNREACHABLE("Setting unresolved directionality?");
515 break;
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);
569 PopoverData* GetPopoverData() const {
570 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
571 return slots ? slots->mPopoverData.get() : nullptr;
574 PopoverData& EnsurePopoverData() {
575 if (auto* popoverData = GetPopoverData()) {
576 return *popoverData;
578 return CreatePopoverData();
581 bool IsAutoPopover() const;
582 bool IsPopoverOpen() const;
585 * https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
587 Element* GetTopmostPopoverAncestor(const Element* aInvoker,
588 bool isPopover) const;
590 ElementAnimationData* GetAnimationData() const {
591 if (!MayHaveAnimations()) {
592 return nullptr;
594 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
595 return slots ? slots->mAnimations.get() : nullptr;
598 ElementAnimationData& EnsureAnimationData() {
599 if (auto* anim = GetAnimationData()) {
600 return *anim;
602 return CreateAnimationData();
605 private:
606 ElementAnimationData& CreateAnimationData();
607 PopoverData& CreatePopoverData();
609 public:
610 void ClearPopoverData();
613 * Gets the custom element data used by web components custom element.
614 * Custom element data is created at the first attempt to enqueue a callback.
616 * @return The custom element data or null if none.
618 CustomElementData* GetCustomElementData() const {
619 if (!HasCustomElementData()) {
620 return nullptr;
623 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
624 return slots ? slots->mCustomElementData.get() : nullptr;
628 * Sets the custom element data, ownership of the
629 * callback data is taken by this element.
631 * @param aData The custom element data.
633 void SetCustomElementData(UniquePtr<CustomElementData> aData);
635 nsTArray<RefPtr<nsAtom>>& EnsureCustomStates();
638 * Gets the custom element definition used by web components custom element.
640 * @return The custom element definition or null if element is not a custom
641 * element or custom element is not defined yet.
643 CustomElementDefinition* GetCustomElementDefinition() const;
646 * Sets the custom element definition, called when custom element is created
647 * or upgraded.
649 * @param aDefinition The custom element definition.
651 virtual void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
653 const AttrArray& GetAttrs() const { return mAttrs; }
655 void SetDefined(bool aSet) { SetStates(ElementState::DEFINED, aSet); }
657 // AccessibilityRole
658 REFLECT_NULLABLE_DOMSTRING_ATTR(Role, role)
660 // AriaAttributes
661 REFLECT_NULLABLE_ELEMENT_ATTR(AriaActiveDescendantElement,
662 aria_activedescendant)
663 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
664 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
665 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBrailleLabel, aria_braillelabel)
666 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBrailleRoleDescription,
667 aria_brailleroledescription)
668 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaBusy, aria_busy)
669 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaChecked, aria_checked)
670 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColCount, aria_colcount)
671 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
672 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
673 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
674 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaCurrent, aria_current)
675 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaDescription, aria_description)
676 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
677 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
678 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
679 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaHidden, aria_hidden)
680 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
681 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
682 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLabel, aria_label)
683 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLevel, aria_level)
684 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaLive, aria_live)
685 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaModal, aria_modal)
686 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
687 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
688 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
689 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
690 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
691 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaPressed, aria_pressed)
692 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
693 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
694 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRequired, aria_required)
695 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
696 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
697 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
698 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
699 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
700 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSelected, aria_selected)
701 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
702 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaSort, aria_sort)
703 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
704 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
705 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
706 REFLECT_NULLABLE_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
708 protected:
709 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
710 ErrorResult& aError);
712 public:
713 MOZ_CAN_RUN_SCRIPT
714 nsIScrollableFrame* GetScrollFrame(nsIFrame** aFrame = nullptr,
715 FlushType aFlushType = FlushType::Layout);
717 private:
718 // Style state computed from element's state and style locks.
719 ElementState StyleStateFromLocks() const;
721 void NotifyStateChange(ElementState aStates);
722 void NotifyStyleStateChange(ElementState aStates);
724 public:
725 struct AutoStateChangeNotifier {
726 AutoStateChangeNotifier(Element& aElement, bool aNotify)
727 : mElement(aElement), mOldState(aElement.State()), mNotify(aNotify) {}
728 ~AutoStateChangeNotifier() {
729 if (!mNotify) {
730 return;
732 ElementState newState = mElement.State();
733 if (mOldState != newState) {
734 mElement.NotifyStateChange(mOldState ^ newState);
738 private:
739 Element& mElement;
740 const ElementState mOldState;
741 const bool mNotify;
744 // Method to add state bits. This should be called from subclass constructors
745 // to set up our event state correctly at construction time, and other places
746 // where we don't want to notify a state change, or there's an
747 // AutoStateChangeNotifier on the stack.
748 void AddStatesSilently(ElementState aStates) { mState |= aStates; }
749 // Method to remove state bits. This should be called from subclass
750 // constructors to set up our event state correctly at construction time and
751 // other places where we don't want to notify a state change.
752 void RemoveStatesSilently(ElementState aStates) { mState &= ~aStates; }
753 // Methods to add state bits, potentially notifying. These will handle setting
754 // up script blockers when they notify, so no need to do it in the callers
755 // unless desired. States passed here must only be those in
756 // EXTERNALLY_MANAGED_STATES.
757 void AddStates(ElementState aStates, bool aNotify = true) {
758 ElementState old = mState;
759 AddStatesSilently(aStates);
760 if (aNotify && old != mState) {
761 NotifyStateChange(old ^ mState);
764 void RemoveStates(ElementState aStates, bool aNotify = true) {
765 ElementState old = mState;
766 RemoveStatesSilently(aStates);
767 if (aNotify && old != mState) {
768 NotifyStateChange(old ^ mState);
771 void SetStates(ElementState aStates, bool aSet, bool aNotify = true) {
772 if (aSet) {
773 AddStates(aStates, aNotify);
774 } else {
775 RemoveStates(aStates, aNotify);
778 void ToggleStates(ElementState aStates, bool aNotify) {
779 mState ^= aStates;
780 if (aNotify) {
781 NotifyStateChange(aStates);
785 void UpdateEditableState(bool aNotify) override;
786 // Makes sure that the READONLY/READWRITE flags are in sync.
787 void UpdateReadOnlyState(bool aNotify);
788 // Form controls and non-form controls should have different :read-only /
789 // :read-write behavior. This is what effectively controls it.
790 virtual bool IsReadOnlyInternal() const;
793 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
794 * with that name exists. This method is intended for character case
795 * conversion if the content object is case insensitive (e.g. HTML). Returns
796 * the nodeinfo of the attribute with the specified name if one exists or
797 * null otherwise.
799 * @param aStr the unparsed attribute string
800 * @return the node info. May be nullptr.
802 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
803 const nsAString& aStr) const;
806 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
807 * is true or there are mutation listeners that must be triggered, the
808 * attribute is currently set, and the new value that is about to be set is
809 * different to the current value. As a perf optimization the new and old
810 * values will not actually be compared if we aren't notifying and we don't
811 * have mutation listeners (in which case it's cheap to just return false
812 * and let the caller go ahead and set the value).
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 MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
824 const nsAtom* aPrefix,
825 const nsAttrValueOrString& aValue, bool aNotify,
826 nsAttrValue& aOldValue, uint8_t* aModType,
827 bool* aHasListeners, bool* aOldValueSet);
830 * Notifies mutation listeners if aNotify is true, there are mutation
831 * listeners, and the attribute value is changing.
833 * @param aNamespaceID The namespace of the attribute
834 * @param aName The local name of the attribute
835 * @param aPrefix The prefix of the attribute
836 * @param aValue The value that the attribute is being changed to
837 * @param aNotify If true, mutation listeners will be notified if they exist
838 * and the attribute value is changing
839 * @param aOldValue [out] Set to the old value of the attribute, but only if
840 * there are event listeners. If set, the type of aOldValue will be either
841 * nsAttrValue::eString or nsAttrValue::eAtom.
842 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
843 * MutationEvent_Binding::ADDITION, but only if this helper returns true
844 * @param aHasListeners [out] Set to true if there are mutation event
845 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
846 * @param aOldValueSet [out] Indicates whether an old attribute value has been
847 * stored in aOldValue. The bool will be set to true if a value was stored.
849 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
850 nsAtom* aPrefix,
851 const nsAttrValueOrString& aValue, bool aNotify,
852 nsAttrValue& aOldValue, uint8_t* aModType,
853 bool* aHasListeners, bool* aOldValueSet);
856 * Sets the class attribute.
857 * Assumes that we are not notifying and that the attribute hasn't been
858 * set previously.
860 nsresult SetClassAttrFromParser(nsAtom* aValue);
862 // aParsedValue receives the old value of the attribute. That's useful if
863 // either the input or output value of aParsedValue is StoresOwnData.
864 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
865 nsAttrValue& aParsedValue, bool aNotify);
867 * Get the current value of the attribute. This returns a form that is
868 * suitable for passing back into SetAttr.
870 * @param aNameSpaceID the namespace of the attr (defaults to
871 kNameSpaceID_None in the overload that omits this arg)
872 * @param aName the name of the attr
873 * @param aResult the value (may legitimately be the empty string) [OUT]
874 * @returns true if the attribute was set (even when set to empty string)
875 * false when not set.
876 * GetAttr is not inlined on purpose, to keep down codesize from all the
877 * inlined nsAttrValue bits for C++ callers.
879 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
880 nsAString& aResult) const;
881 bool GetAttr(const nsAtom* aName, nsAString& aResult) const;
884 * Determine if an attribute has been set (empty string or otherwise).
886 * @param aNameSpaceId the namespace id of the attribute (defaults to
887 kNameSpaceID_None in the overload that omits this arg)
888 * @param aAttr the attribute name
889 * @return whether an attribute exists
891 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
892 return mAttrs.HasAttr(aNameSpaceID, aName);
895 bool HasAttr(const nsAtom* aAttr) const { return mAttrs.HasAttr(aAttr); }
898 * Determine if an attribute has been set to a non-empty string value. If the
899 * attribute is not set at all, this will return false.
901 * @param aNameSpaceId the namespace id of the attribute (defaults to
902 * kNameSpaceID_None in the overload that omits this arg)
903 * @param aAttr the attribute name
905 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
907 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
908 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
912 * Test whether this Element's given attribute has the given value. If the
913 * attribute is not set at all, this will return false.
915 * @param aNameSpaceID The namespace ID of the attribute. Must not
916 * be kNameSpaceID_Unknown.
917 * @param aName The name atom of the attribute. Must not be null.
918 * @param aValue The value to compare to.
919 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
921 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
922 const nsAString& aValue,
923 nsCaseTreatment aCaseSensitive) const;
926 * Test whether this Element's given attribute has the given value. If the
927 * attribute is not set at all, this will return false.
929 * @param aNameSpaceID The namespace ID of the attribute. Must not
930 * be kNameSpaceID_Unknown.
931 * @param aName The name atom of the attribute. Must not be null.
932 * @param aValue The value to compare to. Must not be null.
933 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
935 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
936 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
939 * Check whether this Element's given attribute has one of a given list of
940 * values. If there is a match, we return the index in the list of the first
941 * matching value. If there was no attribute at all, then we return
942 * ATTR_MISSING. If there was an attribute but it didn't match, we return
943 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
945 * @param aNameSpaceID The namespace ID of the attribute. Must not
946 * be kNameSpaceID_Unknown.
947 * @param aName The name atom of the attribute. Must not be null.
948 * @param aValues a nullptr-terminated array of pointers to atom values to
949 * test against.
950 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
951 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
952 * indicating the first value of aValues that matched
954 using AttrValuesArray = AttrArray::AttrValuesArray;
955 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
956 AttrArray::AttrValuesArray* aValues,
957 nsCaseTreatment aCaseSensitive) const;
960 * Set attribute values. All attribute values are assumed to have a
961 * canonical string representation that can be used for these
962 * methods. The SetAttr method is assumed to perform a translation
963 * of the canonical form into the underlying content specific
964 * form.
966 * @param aNameSpaceID the namespace of the attribute
967 * @param aName the name of the attribute
968 * @param aValue the value to set
969 * @param aNotify specifies how whether or not the document should be
970 * notified of the attribute change.
972 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
973 bool aNotify) {
974 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
976 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
977 const nsAString& aValue, bool aNotify) {
978 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
980 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
981 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
982 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
983 aNotify);
987 * Set attribute values. All attribute values are assumed to have a
988 * canonical String representation that can be used for these
989 * methods. The SetAttr method is assumed to perform a translation
990 * of the canonical form into the underlying content specific
991 * form.
993 * @param aNameSpaceID the namespace of the attribute
994 * @param aName the name of the attribute
995 * @param aPrefix the prefix of the attribute
996 * @param aValue the value to set
997 * @param aMaybeScriptedPrincipal the principal of the scripted caller
998 * responsible for setting the attribute, or null if no scripted caller can be
999 * determined. A null value here does not guarantee that there is no
1000 * scripted caller, but a non-null value does guarantee that a scripted
1001 * caller with the given principal is directly responsible for the
1002 * attribute change.
1003 * @param aNotify specifies how whether or not the document should be
1004 * notified of the attribute change.
1006 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
1007 const nsAString& aValue,
1008 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
1011 * Remove an attribute so that it is no longer explicitly specified.
1013 * @param aNameSpaceID the namespace id of the attribute
1014 * @param aName the name of the attribute to unset
1015 * @param aNotify specifies whether or not the document should be
1016 * notified of the attribute change
1018 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aName, bool aNotify);
1021 * Get the namespace / name / prefix of a given attribute.
1023 * @param aIndex the index of the attribute name
1024 * @returns The name at the given index, or null if the index is
1025 * out-of-bounds.
1026 * @note The document returned by NodeInfo()->GetDocument() (if one is
1027 * present) is *not* necessarily the owner document of the element.
1028 * @note The pointer returned by this function is only valid until the
1029 * next call of either GetAttrNameAt or SetAttr on the element.
1031 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
1032 return mAttrs.GetSafeAttrNameAt(aIndex);
1036 * Same as above, but does not do out-of-bounds checks!
1038 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
1039 return mAttrs.AttrNameAt(aIndex);
1043 * Gets the attribute info (name and value) for this element at a given index.
1045 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
1046 if (aIndex >= mAttrs.AttrCount()) {
1047 return BorrowedAttrInfo(nullptr, nullptr);
1050 return mAttrs.AttrInfoAt(aIndex);
1054 * Get the number of all specified attributes.
1056 * @return the number of attributes
1058 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
1061 * Get the class list of this element (this corresponds to the value of the
1062 * class attribute). This may be null if there are no classes, but that's not
1063 * guaranteed (e.g. we could have class="").
1065 const nsAttrValue* GetClasses() const {
1066 if (!MayHaveClass()) {
1067 return nullptr;
1070 if (IsSVGElement()) {
1071 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
1072 return value;
1076 return GetParsedAttr(nsGkAtoms::_class);
1079 #ifdef MOZ_DOM_LIST
1080 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
1081 List(out, aIndent, ""_ns);
1083 virtual void DumpContent(FILE* out, int32_t aIndent,
1084 bool aDumpAll) const override;
1085 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1086 void ListAttributes(FILE* out) const;
1087 #endif
1090 * Append to aOutDescription a string describing the element and its
1091 * attributes.
1092 * If aShort is true, only the id and class attributes will be listed.
1094 void Describe(nsAString& aOutDescription, bool aShort = false) const;
1097 * Attribute Mapping Helpers
1099 struct MappedAttributeEntry {
1100 const nsStaticAtom* const attribute;
1104 * A common method where you can just pass in a list of maps to check
1105 * for attribute dependence. Most implementations of
1106 * IsAttributeMapped should use this function as a default
1107 * handler.
1109 template <size_t N>
1110 static bool FindAttributeDependence(
1111 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1112 return FindAttributeDependence(aAttribute, aMaps, N);
1115 MOZ_CAN_RUN_SCRIPT virtual void HandleInvokeInternal(nsAtom* aAction,
1116 ErrorResult& aRv) {}
1118 private:
1119 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1121 static bool FindAttributeDependence(const nsAtom* aAttribute,
1122 const MappedAttributeEntry* const aMaps[],
1123 uint32_t aMapCount);
1125 protected:
1126 inline bool GetAttr(const nsAtom* aName, DOMString& aResult) const {
1127 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1128 const nsAttrValue* val = mAttrs.GetAttr(aName);
1129 if (!val) {
1130 return false; // DOMString comes pre-emptied.
1132 val->ToString(aResult);
1133 return true;
1136 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1137 DOMString& aResult) const {
1138 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1139 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1140 if (!val) {
1141 return false; // DOMString comes pre-emptied.
1143 val->ToString(aResult);
1144 return true;
1147 public:
1148 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1150 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1151 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1152 const nsAttrValue* val = mAttrs.GetAttr(aName);
1153 if (val) {
1154 val->ToString(aResult);
1155 return true;
1157 // else DOMString comes pre-emptied.
1158 return false;
1161 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1162 void GetId(nsAString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1163 void GetId(DOMString& aId) const { GetAttr(nsGkAtoms::id, aId); }
1164 void SetId(const nsAString& aId) {
1165 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1167 void GetClassName(nsAString& aClassName) {
1168 GetAttr(nsGkAtoms::_class, aClassName);
1170 void GetClassName(DOMString& aClassName) {
1171 GetAttr(nsGkAtoms::_class, aClassName);
1173 void SetClassName(const nsAString& aClassName) {
1174 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1177 nsDOMTokenList* ClassList();
1178 nsDOMTokenList* Part();
1180 nsDOMAttributeMap* Attributes();
1182 void GetAttributeNames(nsTArray<nsString>& aResult);
1184 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1185 DOMString str;
1186 GetAttribute(aName, str);
1187 str.ToString(aReturn);
1190 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1191 void GetAttributeNS(const nsAString& aNamespaceURI,
1192 const nsAString& aLocalName, nsAString& aReturn);
1193 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1194 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1195 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1196 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1197 void SetAttributeNS(const nsAString& aNamespaceURI,
1198 const nsAString& aLocalName, const nsAString& aValue,
1199 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1200 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1201 ErrorResult& aError) {
1202 SetAttribute(aName, aValue, nullptr, aError);
1205 * This method creates a principal that subsumes this element's NodePrincipal
1206 * and which has flags set for elevated permissions that devtools needs to
1207 * operate on this element. The principal returned by this method is used by
1208 * various devtools methods to permit otherwise blocked operations, without
1209 * changing any other restrictions the NodePrincipal might have.
1211 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1212 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1213 ErrorResult& aError);
1214 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1215 const nsAString& aLocalName,
1216 const nsAString& aValue, ErrorResult& aError);
1218 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1219 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1220 const nsAString& aLocalName, ErrorResult& aError);
1221 bool HasAttribute(const nsAString& aName) const {
1222 return InternalGetAttrNameFromQName(aName) != nullptr;
1224 bool HasAttributeNS(const nsAString& aNamespaceURI,
1225 const nsAString& aLocalName) const;
1226 bool HasAttributes() const { return HasAttrs(); }
1227 Element* Closest(const nsACString& aSelector, ErrorResult& aResult);
1228 bool Matches(const nsACString& aSelector, ErrorResult& aError);
1229 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1230 const nsAString& aQualifiedName);
1231 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1232 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1233 ErrorResult& aError);
1234 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1235 const nsAString& aClassNames);
1238 * Returns attribute associated element for the given attribute name, see
1239 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#attr-associated-element
1241 Element* GetAttrAssociatedElement(nsAtom* aAttr) const;
1244 * Sets an attribute element for the given attribute.
1245 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
1247 void ExplicitlySetAttrElement(nsAtom* aAttr, Element* aElement);
1249 void ClearExplicitlySetAttrElement(nsAtom*);
1252 * Gets the attribute element for the given attribute.
1253 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
1254 * Unlike GetAttrAssociatedElement, this returns the target even if it isn't
1255 * a descendant of any of this element's shadow-including ancestors. It also
1256 * doesn't attempt to retrieve an element using a string id set in the content
1257 * attribute.
1259 Element* GetExplicitlySetAttrElement(nsAtom* aAttr) const;
1261 PseudoStyleType GetPseudoElementType() const {
1262 nsresult rv = NS_OK;
1263 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1264 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1265 return PseudoStyleType::NotPseudo;
1267 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1270 void SetPseudoElementType(PseudoStyleType aPseudo) {
1271 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1272 "Need to be able to store this in a void*");
1273 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1274 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1278 * Return an array of all elements in the subtree rooted at this
1279 * element that have grid container frames. This does not include
1280 * pseudo-elements.
1282 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1285 * Provide a direct way to determine if this Element has visible
1286 * scrollbars. Flushes layout.
1288 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1291 * Get an editor which handles user inputs when this element has focus.
1292 * If this is a text control, return a TextEditor if it's already created.
1293 * Otherwise, return nullptr.
1294 * If this is not a text control but this is editable, return
1295 * HTMLEditor which should've already been created.
1296 * Otherwise, return nullptr.
1298 EditorBase* GetEditorWithoutCreation() const;
1300 private:
1302 * Implement the algorithm specified at
1303 * https://dom.spec.whatwg.org/#insert-adjacent for both
1304 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1306 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1307 ErrorResult& aError);
1309 public:
1310 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1311 ErrorResult& aError);
1313 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1314 ErrorResult& aError);
1316 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError);
1317 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError);
1318 bool HasPointerCapture(long aPointerId);
1319 void SetCapture(bool aRetargetToElement);
1321 void SetCaptureAlways(bool aRetargetToElement);
1323 void ReleaseCapture();
1325 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1326 void RequestPointerLock(CallerType aCallerType);
1327 Attr* GetAttributeNode(const nsAString& aName);
1328 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1329 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1330 ErrorResult& aError);
1331 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1332 const nsAString& aLocalName);
1333 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1334 ErrorResult& aError);
1336 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1337 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1339 enum class Loading : uint8_t {
1340 Eager,
1341 Lazy,
1344 Loading LoadingState() const;
1345 void GetLoading(nsAString& aValue) const;
1346 bool ParseLoadingAttribute(const nsAString& aValue, nsAttrValue& aResult);
1348 // https://html.spec.whatwg.org/#potentially-render-blocking
1349 virtual bool IsPotentiallyRenderBlocking() { return false; }
1350 bool BlockingContainsRender() const;
1352 // Shadow DOM v1
1353 enum class ShadowRootDeclarative : bool { No, Yes };
1355 MOZ_CAN_RUN_SCRIPT_BOUNDARY
1356 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1357 ErrorResult& aError);
1358 bool CanAttachShadowDOM() const;
1360 enum class DelegatesFocus : bool { No, Yes };
1361 enum class ShadowRootClonable : bool { No, Yes };
1363 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1364 ShadowRootMode aMode, DelegatesFocus = DelegatesFocus::No,
1365 SlotAssignmentMode aSlotAssignmentMode = SlotAssignmentMode::Named,
1366 ShadowRootClonable aClonable = ShadowRootClonable::No);
1368 // Attach UA Shadow Root if it is not attached.
1369 enum class NotifyUAWidgetSetup : bool { No, Yes };
1370 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes,
1371 DelegatesFocus = DelegatesFocus::No);
1373 // Dispatch an event to UAWidgetsChild, triggering construction
1374 // or onchange callback on the existing widget.
1375 void NotifyUAWidgetSetupOrChange();
1377 enum class UnattachShadowRoot {
1379 Yes,
1382 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1383 // and optionally remove the shadow root.
1384 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1386 void UnattachShadow();
1388 ShadowRoot* GetShadowRootByMode() const;
1389 void SetSlot(const nsAString& aName, ErrorResult& aError);
1390 void GetSlot(nsAString& aName);
1392 ShadowRoot* GetShadowRoot() const {
1393 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1394 return slots ? slots->mShadowRoot.get() : nullptr;
1397 const Maybe<float> GetLastRememberedBSize() const {
1398 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1399 return slots ? slots->mLastRememberedBSize : Nothing();
1401 const Maybe<float> GetLastRememberedISize() const {
1402 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1403 return slots ? slots->mLastRememberedISize : Nothing();
1405 bool HasLastRememberedBSize() const {
1406 return GetLastRememberedBSize().isSome();
1408 bool HasLastRememberedISize() const {
1409 return GetLastRememberedISize().isSome();
1412 const Maybe<ContentRelevancy> GetContentRelevancy() const {
1413 const auto* slots = GetExistingExtendedDOMSlots();
1414 return slots ? slots->mContentRelevancy : Nothing();
1416 void SetContentRelevancy(ContentRelevancy relevancy) {
1417 ExtendedDOMSlots()->mContentRelevancy = Some(relevancy);
1420 const Maybe<bool> GetVisibleForContentVisibility() const {
1421 const auto* slots = GetExistingExtendedDOMSlots();
1422 return slots ? slots->mVisibleForContentVisibility : Nothing();
1424 void SetVisibleForContentVisibility(bool visible) {
1425 ExtendedDOMSlots()->mVisibleForContentVisibility = Some(visible);
1428 void ClearContentRelevancy() {
1429 if (auto* slots = GetExistingExtendedDOMSlots()) {
1430 slots->mContentRelevancy.reset();
1431 slots->mVisibleForContentVisibility.reset();
1432 slots->mTemporarilyVisibleForScrolledIntoViewDescendant = false;
1436 bool TemporarilyVisibleForScrolledIntoViewDescendant() const {
1437 const auto* slots = GetExistingExtendedDOMSlots();
1438 return slots && slots->mTemporarilyVisibleForScrolledIntoViewDescendant;
1441 void SetTemporarilyVisibleForScrolledIntoViewDescendant(bool aVisible) {
1442 ExtendedDOMSlots()->mTemporarilyVisibleForScrolledIntoViewDescendant =
1443 aVisible;
1446 // https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
1447 MOZ_CAN_RUN_SCRIPT bool CheckVisibility(const CheckVisibilityOptions&);
1449 private:
1450 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1451 // called from JS. Use PresShell::ScrollContentIntoView instead.
1452 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1454 public:
1455 MOZ_CAN_RUN_SCRIPT
1456 // DO NOT USE THIS FUNCTION directly in C++. This function is supposed to be
1457 // called from JS. Use PresShell::ScrollContentIntoView instead.
1458 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1459 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1460 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1461 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1462 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1463 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1464 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1465 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1466 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1467 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1468 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1469 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1470 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1471 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1473 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1474 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1476 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1477 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1479 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1480 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1483 MOZ_CAN_RUN_SCRIPT int32_t ScreenX();
1484 MOZ_CAN_RUN_SCRIPT int32_t ScreenY();
1485 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIScreen> GetScreen();
1487 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1488 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1489 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1490 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1492 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1493 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1496 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1497 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1500 MOZ_CAN_RUN_SCRIPT double CurrentCSSZoom();
1502 // This function will return the block size of first line box, no matter if
1503 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1504 // can't get a primary frame, we will return be zero.
1505 double FirstLineBoxBSize() const;
1507 already_AddRefed<Flex> GetAsFlexContainer();
1508 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1510 bool HasGridFragments();
1512 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1513 Element& aAncestor);
1514 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1515 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1517 already_AddRefed<Animation> Animate(
1518 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1519 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1520 ErrorResult& aError);
1522 MOZ_CAN_RUN_SCRIPT
1523 void GetAnimations(const GetAnimationsOptions& aOptions,
1524 nsTArray<RefPtr<Animation>>& aAnimations);
1526 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1527 nsTArray<RefPtr<Animation>>& aAnimations);
1529 static void GetAnimationsUnsorted(Element* aElement,
1530 PseudoStyleType aPseudoType,
1531 nsTArray<RefPtr<Animation>>& aAnimations);
1533 void CloneAnimationsFrom(const Element& aOther);
1535 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1536 virtual void SetInnerHTML(const nsAString& aInnerHTML,
1537 nsIPrincipal* aSubjectPrincipal,
1538 ErrorResult& aError);
1539 void GetOuterHTML(nsAString& aOuterHTML);
1540 void SetOuterHTML(const nsAString& aOuterHTML, ErrorResult& aError);
1541 void InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
1542 ErrorResult& aError);
1544 void SetHTML(const nsAString& aInnerHTML, const SetHTMLOptions& aOptions,
1545 ErrorResult& aError);
1547 //----------------------------------------
1550 * Add a script event listener with the given event handler name
1551 * (like onclick) and with the value as JS
1552 * @param aEventName the event listener name
1553 * @param aValue the JS to attach
1554 * @param aDefer indicates if deferred execution is allowed
1556 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1557 bool aDefer = true);
1560 * Do whatever needs to be done when the mouse leaves a link
1562 nsresult LeaveLink(nsPresContext* aPresContext);
1564 static bool ShouldBlur(nsIContent* aContent);
1567 * Method to create and dispatch a left-click event loosely based on
1568 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1569 * through the full dispatching of the presshell of the aPresContext; if it's
1570 * false the event will be dispatched only as a DOM event.
1571 * If aPresContext is nullptr, this does nothing.
1573 * @param aFlags Extra flags for the dispatching event. The true flags
1574 * will be respected.
1576 MOZ_CAN_RUN_SCRIPT
1577 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1578 WidgetInputEvent* aSourceEvent,
1579 nsIContent* aTarget, bool aFullDispatch,
1580 const EventFlags* aFlags,
1581 nsEventStatus* aStatus);
1584 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1585 * will be dispatched through the full dispatching of the presshell of the
1586 * aPresContext; if it's false the event will be dispatched only as a DOM
1587 * event.
1588 * If aPresContext is nullptr, this does nothing.
1590 using nsIContent::DispatchEvent;
1591 MOZ_CAN_RUN_SCRIPT
1592 static nsresult DispatchEvent(nsPresContext* aPresContext,
1593 WidgetEvent* aEvent, nsIContent* aTarget,
1594 bool aFullDispatch, nsEventStatus* aStatus);
1596 bool IsDisplayContents() const {
1597 return HasServoData() && Servo_Element_IsDisplayContents(this);
1601 * https://html.spec.whatwg.org/#being-rendered
1603 * With a gotcha for display contents:
1604 * https://github.com/whatwg/html/issues/1837
1606 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1608 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1609 return mAttrs.GetAttr(aAttr);
1612 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1613 int32_t aNameSpaceID) const {
1614 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1618 * Returns the attribute map, if there is one.
1620 * @return existing attribute map or nullptr.
1622 nsDOMAttributeMap* GetAttributeMap() {
1623 nsDOMSlots* slots = GetExistingDOMSlots();
1625 return slots ? slots->mAttributeMap.get() : nullptr;
1628 void RecompileScriptEventListeners();
1631 * Get the attr info for the given namespace ID and attribute name. The
1632 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1633 * null. Note that this can only return info on attributes that actually
1634 * live on this element (and is only virtual to handle XUL prototypes). That
1635 * is, this should only be called from methods that only care about attrs
1636 * that effectively live in mAttrs.
1638 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1639 const nsAtom* aName) const {
1640 NS_ASSERTION(aName, "must have attribute name");
1641 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1642 "must have a real namespace ID!");
1644 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1645 if (index < 0) {
1646 return BorrowedAttrInfo(nullptr, nullptr);
1649 return mAttrs.AttrInfoAt(index);
1653 * Parse a string into an nsAttrValue for a CORS attribute. This
1654 * never fails. The resulting value is an enumerated value whose
1655 * GetEnumValue() returns one of the above constants.
1657 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1660 * Return the CORS mode for a given string
1662 static CORSMode StringToCORSMode(const nsAString& aValue);
1665 * Return the CORS mode for a given nsAttrValue (which may be null,
1666 * but if not should have been parsed via ParseCORSValue).
1668 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1670 nsINode* GetScopeChainParent() const override;
1673 * Locate a TextEditor rooted at this content node, if there is one.
1675 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1678 * Gets value of boolean attribute. Only works for attributes in null
1679 * namespace.
1681 * @param aAttr name of attribute.
1682 * @param aValue Boolean value of attribute.
1684 bool GetBoolAttr(nsAtom* aAttr) const { return HasAttr(aAttr); }
1687 * Sets value of boolean attribute by removing attribute or setting it to
1688 * the empty string. Only works for attributes in null namespace.
1690 * @param aAttr name of attribute.
1691 * @param aValue Boolean value of attribute.
1693 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1696 * Gets the enum value string of an attribute and using a default value if
1697 * the attribute is missing or the string is an invalid enum value.
1699 * @param aType the name of the attribute.
1700 * @param aDefault the default value if the attribute is missing or invalid.
1701 * @param aResult string corresponding to the value [out].
1703 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1704 nsAString& aResult) const;
1707 * Gets the enum value string of an attribute and using the default missing
1708 * value if the attribute is missing or the default invalid value if the
1709 * string is an invalid enum value.
1711 * @param aType the name of the attribute.
1712 * @param aDefaultMissing the default value if the attribute is missing. If
1713 null and the attribute is missing, aResult will be
1714 set to the null DOMString; this only matters for
1715 cases in which we're reflecting a nullable string.
1716 * @param aDefaultInvalid the default value if the attribute is invalid.
1717 * @param aResult string corresponding to the value [out].
1719 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1720 const char* aDefaultInvalid, nsAString& aResult) const;
1723 * Unset an attribute.
1725 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1726 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1730 * Set an attribute in the simplest way possible.
1732 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1733 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1736 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1737 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1738 aError =
1739 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1743 * Preallocate space in this element's attribute array for the given
1744 * total number of attributes.
1746 void TryReserveAttributeCount(uint32_t aAttributeCount);
1748 void SetParserHadDuplicateAttributeError() {
1749 SetFlags(ELEMENT_PARSER_HAD_DUPLICATE_ATTR_ERROR);
1753 * Set a content attribute via a reflecting nullable string IDL
1754 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1755 * this will actually remove the content attribute.
1757 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1758 ErrorResult& aError);
1761 * Retrieve the ratio of font-size-inflated text font size to computed font
1762 * size for this element. This will query the element for its primary frame,
1763 * and then use this to get font size inflation information about the frame.
1765 * @returns The font size inflation ratio (inflated font size to uninflated
1766 * font size) for the primary frame of this element. Returns 1.0
1767 * by default if font size inflation is not enabled. Returns -1
1768 * if the element does not have a primary frame.
1770 * @note The font size inflation ratio that is returned is actually the
1771 * font size inflation data for the element's _primary frame_, not the
1772 * element itself, but for most purposes, this should be sufficient.
1774 float FontSizeInflation();
1776 void GetImplementedPseudoElement(nsAString&) const;
1778 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1779 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1782 * Helpers for .dataset. This is implemented on Element, though only some
1783 * sorts of elements expose it to JS as a .dataset property
1785 // Getter, to be called from bindings.
1786 already_AddRefed<nsDOMStringMap> Dataset();
1787 // Callback for destructor of dataset to ensure to null out our weak pointer
1788 // to it.
1789 void ClearDataset();
1791 void RegisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1792 void UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1793 void UnlinkIntersectionObservers();
1794 bool UpdateIntersectionObservation(DOMIntersectionObserver* aObserver,
1795 int32_t threshold);
1797 // A number of methods to cast to various XUL interfaces. They return a
1798 // pointer only if the element implements that interface.
1799 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1800 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1801 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1802 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1803 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1804 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1805 AsXULMultiSelectControl();
1806 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1807 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1808 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1809 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1810 already_AddRefed<nsIBrowser> AsBrowser();
1811 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1814 * Get the presentation context for this content node.
1815 * @return the presentation context
1817 enum PresContextFor { eForComposedDoc, eForUncomposedDoc };
1818 nsPresContext* GetPresContext(PresContextFor aFor);
1821 * The method focuses (or activates) element that accesskey is bound to. It is
1822 * called when accesskey is activated.
1824 * @param aKeyCausesActivation - if true then element should be activated
1825 * @param aIsTrustedEvent - if true then event that is cause of accesskey
1826 * execution is trusted.
1827 * @return an error if the element isn't able to handle the accesskey (caller
1828 * would look for the next element to handle it).
1829 * a boolean indicates whether the focus moves to the element after
1830 * the element handles the accesskey.
1832 MOZ_CAN_RUN_SCRIPT
1833 virtual Result<bool, nsresult> PerformAccesskey(bool aKeyCausesActivation,
1834 bool aIsTrustedEvent) {
1835 return Err(NS_ERROR_NOT_IMPLEMENTED);
1838 protected:
1839 // Supported rel values for <form> and anchors.
1840 static const DOMTokenListSupportedToken sAnchorAndFormRelValues[];
1843 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1844 * read.
1846 static const bool kFireMutationEvent = true;
1847 static const bool kDontFireMutationEvent = false;
1848 static const bool kNotifyDocumentObservers = true;
1849 static const bool kDontNotifyDocumentObservers = false;
1850 static const bool kCallAfterSetAttr = true;
1851 static const bool kDontCallAfterSetAttr = false;
1854 * The supported values of blocking attribute for use with nsDOMTokenList.
1856 static const DOMTokenListSupportedToken sSupportedBlockingValues[];
1859 * Set attribute and (if needed) notify documentobservers and fire off
1860 * mutation events. This will send the AttributeChanged notification.
1861 * Callers of this method are responsible for calling AttributeWillChange,
1862 * since that needs to happen before the new attr value has been set, and
1863 * in particular before it has been parsed.
1865 * For the boolean parameters, consider using the named bools above to aid
1866 * code readability.
1868 * @param aNamespaceID namespace of attribute
1869 * @param aAttribute local-name of attribute
1870 * @param aPrefix aPrefix of attribute
1871 * @param aOldValue The old value of the attribute to use as a fallback
1872 * in the cases where the actual old value (i.e.
1873 * its current value) is !StoresOwnData() --- in which
1874 * case the current value is probably already useless.
1875 * If the current value is StoresOwnData() (or absent),
1876 * aOldValue will not be used. aOldValue will only be set
1877 * in certain circumstances (there are mutation
1878 * listeners, element is a custom element, attribute was
1879 * not previously unset). Otherwise it will be null.
1880 * @param aParsedValue parsed new value of attribute. Replaced by the
1881 * old value of the attribute. This old value is only
1882 * useful if either it or the new value is StoresOwnData.
1883 * @param aSubjectPrincipal
1884 * the principal of the scripted caller responsible for
1885 * setting the attribute, or null if no scripted caller
1886 * can be determined. A null value here does not
1887 * guarantee that there is no scripted caller, but a
1888 * non-null value does guarantee that a scripted caller
1889 * with the given principal is directly responsible for
1890 * the attribute change.
1891 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
1892 * needed if aFireMutation or aNotify is true.
1893 * @param aFireMutation should mutation-events be fired?
1894 * @param aNotify should we notify document-observers?
1895 * @param aCallAfterSetAttr should we call AfterSetAttr?
1896 * @param aComposedDocument The current composed document of the element.
1897 * @param aGuard For making sure that this is called with a
1898 * mozAutoDocUpdate instance, this is here. Specify
1899 * an instance of it which you created for the call.
1901 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
1902 nsAtom* aPrefix, const nsAttrValue* aOldValue,
1903 nsAttrValue& aParsedValue,
1904 nsIPrincipal* aSubjectPrincipal, uint8_t aModType,
1905 bool aFireMutation, bool aNotify,
1906 bool aCallAfterSetAttr, Document* aComposedDocument,
1907 const mozAutoDocUpdate& aGuard);
1910 * Convert an attribute string value to attribute type based on the type of
1911 * attribute. Called by SetAttr(). Note that at the moment we only do this
1912 * for attributes in the null namespace (kNameSpaceID_None).
1914 * @param aNamespaceID the namespace of the attribute to convert
1915 * @param aAttribute the attribute to convert
1916 * @param aValue the string value to convert
1917 * @param aMaybeScriptedPrincipal the principal of the script setting the
1918 * attribute, if one can be determined, or null otherwise. As in
1919 * AfterSetAttr, a null value does not guarantee that the attribute was
1920 * not set by a scripted caller, but a non-null value guarantees that
1921 * the attribute was set by a scripted caller with the given principal.
1922 * @param aResult the nsAttrValue [OUT]
1923 * @return true if the parsing was successful, false otherwise
1925 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1926 const nsAString& aValue,
1927 nsIPrincipal* aMaybeScriptedPrincipal,
1928 nsAttrValue& aResult);
1931 * Hook that is called by Element::SetAttr to allow subclasses to
1932 * deal with attribute sets. This will only be called after we verify that
1933 * we're actually doing an attr set and will be called before
1934 * AttributeWillChange and before ParseAttribute and hence before we've set
1935 * the new value.
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. Alternatively, if the attr is being removed it
1941 * will be null.
1942 * @param aNotify Whether we plan to notify document observers.
1944 virtual void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1945 const nsAttrValue* aValue, bool aNotify);
1948 * Hook that is called by Element::SetAttr to allow subclasses to
1949 * deal with attribute sets. This will only be called after we have called
1950 * SetAndSwapAttr (that is, after we have actually set the attr). It will
1951 * always be called under a scriptblocker.
1953 * @param aNamespaceID the namespace of the attr being set
1954 * @param aName the localname of the attribute being set
1955 * @param aValue the value it's being set to. If null, the attr is being
1956 * removed.
1957 * @param aOldValue the value that the attribute had previously. If null,
1958 * the attr was not previously set. This argument may not have the
1959 * correct value for SVG elements, or other cases in which the
1960 * attribute value doesn't store its own data
1961 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1962 * responsible for setting the attribute, or null if no scripted caller
1963 * can be determined, or the attribute is being unset. A null value
1964 * here does not guarantee that there is no scripted caller, but a
1965 * non-null value does guarantee that a scripted caller with the given
1966 * principal is directly responsible for the attribute change.
1967 * @param aNotify Whether we plan to notify document observers.
1969 virtual void AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
1970 const nsAttrValue* aValue,
1971 const nsAttrValue* aOldValue,
1972 nsIPrincipal* aMaybeScriptedPrincipal,
1973 bool aNotify);
1976 * This function shall be called just before the id attribute changes. It will
1977 * be called after BeforeSetAttr. If the attribute being changed is not the id
1978 * attribute, this function does nothing. Otherwise, it will remove the old id
1979 * from the document's id cache.
1981 * This must happen after BeforeSetAttr (rather than during) because the
1982 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
1983 * incorrectly determine whether the element had an id, the element may not be
1984 * restyled properly.
1986 * @param aNamespaceID the namespace of the attr being set
1987 * @param aName the localname of the attribute being set
1988 * @param aValue the new id value. Will be null if the id is being unset.
1990 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1991 const nsAttrValue* aValue);
1994 * This function shall be called just after the id attribute changes. It will
1995 * be called before AfterSetAttr. If the attribute being changed is not the id
1996 * attribute, this function does nothing. Otherwise, it will add the new id to
1997 * the document's id cache and properly set the ElementHasID flag.
1999 * This must happen before AfterSetAttr (rather than during) because the
2000 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
2001 * incorrectly determine whether the element now has an id, the element may
2002 * not be restyled properly.
2004 * @param aNamespaceID the namespace of the attr being set
2005 * @param aName the localname of the attribute being set
2006 * @param aValue the new id value. Will be null if the id is being unset.
2008 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
2009 const nsAttrValue* aValue);
2012 * Usually, setting an attribute to the value that it already has results in
2013 * no action. However, in some cases, setting an attribute to its current
2014 * value should have the effect of, for example, forcing a reload of
2015 * network data. To address that, this function will be called in this
2016 * situation to allow the handling of such a case.
2018 * @param aNamespaceID the namespace of the attr being set
2019 * @param aName the localname of the attribute being set
2020 * @param aValue the value it's being set to represented as either a string or
2021 * a parsed nsAttrValue.
2022 * @param aNotify Whether we plan to notify document observers.
2024 virtual void OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
2025 const nsAttrValueOrString& aValue,
2026 bool aNotify);
2029 * Hook to allow subclasses to produce a different EventListenerManager if
2030 * needed for attachment of attribute-defined handlers
2032 virtual EventListenerManager* GetEventListenerManagerForAttr(
2033 nsAtom* aAttrName, bool* aDefer);
2036 * Internal hook for converting an attribute name-string to nsAttrName in
2037 * case there is such existing attribute. aNameToUse can be passed to get
2038 * name which was used for looking for the attribute (lowercase in HTML).
2040 const nsAttrName* InternalGetAttrNameFromQName(
2041 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
2043 virtual Element* GetNameSpaceElement() override { return this; }
2045 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
2046 const nsAString& aLocalName);
2048 inline void RegisterActivityObserver();
2049 inline void UnregisterActivityObserver();
2052 * Add/remove this element to the documents id cache
2054 void AddToIdTable(nsAtom* aId);
2055 void RemoveFromIdTable();
2058 * Functions to carry out event default actions for links of all types
2059 * (HTML links, XLinks, SVG "XLinks", etc.)
2063 * Check that we meet the conditions to handle a link event
2064 * and that we are actually on a link.
2066 * @param aVisitor event visitor
2067 * @return true if we can handle the link event, false otherwise
2069 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor) const;
2072 * Handle status bar updates before they can be cancelled.
2074 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
2076 void DispatchChromeOnlyLinkClickEvent(EventChainPostVisitor& aVisitor);
2079 * Handle default actions for link event if the event isn't consumed yet.
2081 MOZ_CAN_RUN_SCRIPT
2082 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
2084 public:
2086 * Check if this element is a link. This matches the CSS definition of the
2087 * :any-link pseudo-class.
2089 bool IsLink() const {
2090 return mState.HasAtLeastOneOfStates(ElementState::VISITED |
2091 ElementState::UNVISITED);
2095 * Get a pointer to the full href URI (fully resolved and canonicalized, since
2096 * it's an nsIURI object) for link elements.
2098 * @return A pointer to the URI or null if the element is not a link, or it
2099 * has no HREF attribute, or the HREF attribute is an invalid URI.
2101 virtual already_AddRefed<nsIURI> GetHrefURI() const { return nullptr; }
2104 * Get the target of this link element. Consumers should established that
2105 * this element is a link (probably using IsLink) before calling this
2106 * function (or else why call it?)
2108 * Note: for HTML this gets the value of the 'target' attribute; for XLink
2109 * this gets the value of the xlink:_moz_target attribute, or failing that,
2110 * the value of xlink:show, converted to a suitably equivalent named target
2111 * (e.g. _blank).
2113 virtual void GetLinkTarget(nsAString& aTarget);
2115 virtual bool Translate() const;
2117 MOZ_CAN_RUN_SCRIPT
2118 virtual void SetHTMLUnsafe(const nsAString& aHTML);
2120 protected:
2121 enum class ReparseAttributes { No, Yes };
2123 * Copy attributes and state to another element
2124 * @param aDest the object to copy to
2126 nsresult CopyInnerTo(Element* aDest,
2127 ReparseAttributes = ReparseAttributes::Yes);
2130 * Some event handler content attributes have a different name (e.g. different
2131 * case) from the actual event name. This function takes an event handler
2132 * content attribute name and returns the corresponding event name, to be used
2133 * for adding the actual event listener.
2135 virtual nsAtom* GetEventNameForAttr(nsAtom* aAttr);
2138 * Register/unregister this element to accesskey map if it supports accesskey.
2140 virtual void RegUnRegAccessKey(bool aDoReg);
2142 private:
2143 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
2144 void AssertInvariantsOnNodeInfoChange();
2145 #endif
2148 * Slow path for GetClasses, this should only be called for SVG elements.
2150 const nsAttrValue* GetSVGAnimatedClass() const;
2153 * Get this element's client area rect in app units.
2154 * @return the frame's client area
2156 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
2158 /** Gets the scroll size as for the scroll{Width,Height} APIs */
2159 MOZ_CAN_RUN_SCRIPT nsSize GetScrollSize();
2160 /** Gets the scroll position as for the scroll{Top,Left} APIs */
2161 MOZ_CAN_RUN_SCRIPT nsPoint GetScrollOrigin();
2162 /** Gets the scroll range as for the scroll{Top,Left}{Min,Max} APIs */
2163 MOZ_CAN_RUN_SCRIPT nsRect GetScrollRange();
2166 * GetCustomInterface is somewhat like a GetInterface, but it is expected
2167 * that the implementation is provided by a custom element or via the
2168 * the XBL implements keyword. To use this, create a public method that
2169 * wraps a call to GetCustomInterface.
2171 template <class T>
2172 void GetCustomInterface(nsGetterAddRefs<T> aResult);
2174 // Prevent people from doing pointless checks/casts on Element instances.
2175 void IsElement() = delete;
2176 void AsElement() = delete;
2178 // Data members
2179 ElementState mState;
2180 // Per-node data managed by Servo.
2182 // There should not be data on nodes that are not in the flattened tree, or
2183 // descendants of display: none elements.
2184 mozilla::RustCell<ServoNodeData*> mServoData;
2186 protected:
2187 // Array containing all attributes for this element
2188 AttrArray mAttrs;
2191 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
2193 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2194 const nsAtom* aName) const {
2195 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2196 MOZ_ASSERT(aName, "Must have attribute name");
2198 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2199 return val && !val->IsEmptyString();
2202 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2203 const nsAString& aValue,
2204 nsCaseTreatment aCaseSensitive) const {
2205 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2208 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2209 const nsAtom* aValue,
2210 nsCaseTreatment aCaseSensitive) const {
2211 return mAttrs.AttrValueIs(aNameSpaceID, aName, aValue, aCaseSensitive);
2214 } // namespace dom
2215 } // namespace mozilla
2217 NON_VIRTUAL_ADDREF_RELEASE(mozilla::dom::Element)
2219 inline mozilla::dom::Element* nsINode::AsElement() {
2220 MOZ_ASSERT(IsElement());
2221 return static_cast<mozilla::dom::Element*>(this);
2224 inline const mozilla::dom::Element* nsINode::AsElement() const {
2225 MOZ_ASSERT(IsElement());
2226 return static_cast<const mozilla::dom::Element*>(this);
2229 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2230 return mozilla::dom::Element::FromNodeOrNull(mParent);
2233 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2234 nsIContent* previousSibling = GetPreviousSibling();
2235 while (previousSibling) {
2236 if (previousSibling->IsElement()) {
2237 return previousSibling->AsElement();
2239 previousSibling = previousSibling->GetPreviousSibling();
2242 return nullptr;
2245 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2246 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2247 : GetParentElement();
2250 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2251 nsIContent* nextSibling = GetNextSibling();
2252 while (nextSibling) {
2253 if (nextSibling->IsElement()) {
2254 return nextSibling->AsElement();
2256 nextSibling = nextSibling->GetNextSibling();
2259 return nullptr;
2263 * Macros to implement Clone(). _elementName is the class for which to implement
2264 * Clone.
2266 #define NS_IMPL_ELEMENT_CLONE(_elementName) \
2267 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2268 nsINode** aResult) const { \
2269 *aResult = nullptr; \
2270 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2271 auto* nim = ni->NodeInfoManager(); \
2272 RefPtr<_elementName> it = new (nim) _elementName(ni.forget()); \
2273 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2274 if (NS_SUCCEEDED(rv)) { \
2275 it.forget(aResult); \
2278 return rv; \
2281 #define EXPAND(...) __VA_ARGS__
2282 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, extra_args_) \
2283 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2284 nsINode** aResult) const { \
2285 *aResult = nullptr; \
2286 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2287 auto* nim = ni->NodeInfoManager(); \
2288 RefPtr<_elementName> it = \
2289 new (nim) _elementName(ni.forget() EXPAND extra_args_); \
2290 nsresult rv = it->Init(); \
2291 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2292 if (NS_FAILED(rv2)) { \
2293 rv = rv2; \
2295 if (NS_SUCCEEDED(rv)) { \
2296 it.forget(aResult); \
2299 return rv; \
2302 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName) \
2303 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, ())
2304 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2305 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, (, NOT_FROM_PARSER))
2307 #endif // mozilla_dom_Element_h__