Bug 1698238 return default dictionary from GetUserMediaRequest#getConstraints() if...
[gecko.git] / dom / base / Element.h
blob072a0245c58d9b0a633a27e1dc1142ba6306bf2a
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/EventStates.h"
30 #include "mozilla/FlushType.h"
31 #include "mozilla/Maybe.h"
32 #include "mozilla/PseudoStyleType.h"
33 #include "mozilla/RefPtr.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/ShadowRootBinding.h"
43 #include "nsAtom.h"
44 #include "nsAttrValue.h"
45 #include "nsAttrValueInlines.h"
46 #include "nsCaseTreatment.h"
47 #include "nsChangeHint.h"
48 #include "nsTHashMap.h"
49 #include "nsDebug.h"
50 #include "nsError.h"
51 #include "nsGkAtoms.h"
52 #include "nsHashKeys.h"
53 #include "nsIContent.h"
54 #include "nsID.h"
55 #include "nsINode.h"
56 #include "nsLiteralString.h"
57 #include "nsRect.h"
58 #include "nsString.h"
59 #include "nsStringFlags.h"
60 #include "nsTLiteralString.h"
61 #include "nscore.h"
63 class JSObject;
64 class mozAutoDocUpdate;
65 class nsAttrName;
66 class nsAttrValueOrString;
67 class nsContentList;
68 class nsDOMAttributeMap;
69 class nsDOMCSSAttributeDeclaration;
70 class nsDOMStringMap;
71 class nsDOMTokenList;
72 class nsFocusManager;
73 class nsGlobalWindowInner;
74 class nsGlobalWindowOuter;
75 class nsIAutoCompletePopup;
76 class nsIBrowser;
77 class nsIDOMXULButtonElement;
78 class nsIDOMXULContainerElement;
79 class nsIDOMXULContainerItemElement;
80 class nsIDOMXULControlElement;
81 class nsIDOMXULMenuListElement;
82 class nsIDOMXULMultiSelectControlElement;
83 class nsIDOMXULRadioGroupElement;
84 class nsIDOMXULRelatedElement;
85 class nsIDOMXULSelectControlElement;
86 class nsIDOMXULSelectControlItemElement;
87 class nsIFrame;
88 class nsIHTMLCollection;
89 class nsIMozBrowserFrame;
90 class nsIPrincipal;
91 class nsIScrollableFrame;
92 class nsIURI;
93 class nsMappedAttributes;
94 class nsPresContext;
95 class nsWindowSizes;
96 struct JSContext;
97 struct ServoNodeData;
98 template <class E>
99 class nsTArray;
100 template <class T>
101 class nsGetterAddRefs;
103 namespace mozilla {
104 class DeclarationBlock;
105 class ErrorResult;
106 class OOMReporter;
107 class SMILAttr;
108 struct MutationClosureData;
109 class TextEditor;
110 namespace css {
111 struct URLValue;
112 } // namespace css
113 namespace dom {
114 struct CustomElementData;
115 struct GetAnimationsOptions;
116 struct ScrollIntoViewOptions;
117 struct ScrollToOptions;
118 struct FocusOptions;
119 struct ShadowRootInit;
120 struct ScrollOptions;
121 class Attr;
122 class BooleanOrScrollIntoViewOptions;
123 class Document;
124 class DOMIntersectionObserver;
125 class DOMMatrixReadOnly;
126 class Element;
127 class ElementOrCSSPseudoElement;
128 class Promise;
129 class ShadowRoot;
130 class UnrestrictedDoubleOrKeyframeAnimationOptions;
131 template <typename T>
132 class Optional;
133 enum class CallerType : uint32_t;
134 enum class ReferrerPolicy : uint8_t;
135 typedef nsTHashMap<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>
136 IntersectionObserverList;
137 } // namespace dom
138 } // namespace mozilla
140 // Declared here because of include hell.
141 extern "C" bool Servo_Element_IsDisplayContents(const mozilla::dom::Element*);
143 already_AddRefed<nsContentList> NS_GetContentList(nsINode* aRootNode,
144 int32_t aMatchNameSpaceId,
145 const nsAString& aTagname);
147 #define ELEMENT_FLAG_BIT(n_) \
148 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
150 // Element-specific flags
151 enum {
152 // Whether this node has dirty descendants for Servo's style system.
153 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(0),
154 // Whether this node has dirty descendants for animation-only restyle for
155 // Servo's style system.
156 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(1),
158 // Whether the element has been snapshotted due to attribute or state changes
159 // by the Servo restyle manager.
160 ELEMENT_HAS_SNAPSHOT = ELEMENT_FLAG_BIT(2),
162 // Whether the element has already handled its relevant snapshot.
164 // Used by the servo restyle process in order to accurately track whether the
165 // style of an element is up-to-date, even during the same restyle process.
166 ELEMENT_HANDLED_SNAPSHOT = ELEMENT_FLAG_BIT(3),
168 // Remaining bits are for subclasses
169 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET = NODE_TYPE_SPECIFIC_BITS_OFFSET + 4
172 #undef ELEMENT_FLAG_BIT
174 // Make sure we have space for our bits
175 ASSERT_NODE_FLAGS_SPACE(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
177 namespace mozilla {
178 enum class PseudoStyleType : uint8_t;
179 class EventChainPostVisitor;
180 class EventChainPreVisitor;
181 class EventChainVisitor;
182 class EventListenerManager;
183 class EventStateManager;
185 namespace dom {
187 struct CustomElementDefinition;
188 class Animation;
189 class CustomElementRegistry;
190 class Link;
191 class DOMRect;
192 class DOMRectList;
193 class Flex;
194 class Grid;
196 // IID for the dom::Element interface
197 #define NS_ELEMENT_IID \
199 0xc67ed254, 0xfd3b, 0x4b10, { \
200 0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1 \
204 #define REFLECT_DOMSTRING_ATTR(method, attr) \
205 void Get##method(nsAString& aValue) const { \
206 GetAttr(nsGkAtoms::attr, aValue); \
208 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
209 SetAttr(nsGkAtoms::attr, aValue, aRv); \
212 class Element : public FragmentOrElement {
213 public:
214 #ifdef MOZILLA_INTERNAL_API
215 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
216 : FragmentOrElement(std::move(aNodeInfo)),
217 mState(NS_EVENT_STATE_READONLY | NS_EVENT_STATE_DEFINED) {
218 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
219 "Bad NodeType in aNodeInfo");
220 SetIsElement();
223 ~Element() {
224 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
227 #endif // MOZILLA_INTERNAL_API
229 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
231 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
233 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
235 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
238 * Method to get the full state of this element. See mozilla/EventStates.h
239 * for the possible bits that could be set here.
241 EventStates State() const {
242 // mState is maintained by having whoever might have changed it
243 // call UpdateState() or one of the other mState mutators.
244 return mState;
248 * Ask this element to update its state. If aNotify is false, then
249 * state change notifications will not be dispatched; in that
250 * situation it is the caller's responsibility to dispatch them.
252 * In general, aNotify should only be false if we're guaranteed that
253 * the element can't have a frame no matter what its style is
254 * (e.g. if we're in the middle of adding it to the document or
255 * removing it from the document).
257 void UpdateState(bool aNotify);
260 * Method to update mState with link state information. This does not notify.
262 void UpdateLinkState(EventStates aState);
265 * Returns the current disabled state of the element.
267 bool IsDisabled() const { return State().HasState(NS_EVENT_STATE_DISABLED); }
269 virtual int32_t TabIndexDefault() { return -1; }
272 * Get tabIndex of this element. If not found, return TabIndexDefault.
274 int32_t TabIndex();
277 * Get the parsed value of tabindex attribute.
279 Maybe<int32_t> GetTabIndexAttrValue();
282 * Set tabIndex value to this element.
284 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
287 * Sets the ShadowRoot binding for this element. The contents of the
288 * binding is rendered in place of this node's children.
290 * @param aShadowRoot The ShadowRoot to be bound to this element.
292 void SetShadowRoot(ShadowRoot* aShadowRoot);
295 * Make focus on this element.
297 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void Focus(const FocusOptions& aOptions,
298 const CallerType aCallerType,
299 ErrorResult& aError);
302 * Show blur and clear focus.
304 virtual void Blur(mozilla::ErrorResult& aError);
307 * The style state of this element. This is the real state of the element
308 * with any style locks applied for pseudo-class inspecting.
310 EventStates StyleState() const {
311 if (!HasLockedStyleStates()) {
312 return mState;
314 return StyleStateFromLocks();
318 * StyleStateLocks is used to specify which event states should be locked,
319 * and whether they should be locked to on or off.
321 struct StyleStateLocks {
322 // mLocks tracks which event states should be locked.
323 EventStates mLocks;
324 // mValues tracks if the locked state should be on or off.
325 EventStates mValues;
329 * The style state locks applied to this element.
331 StyleStateLocks LockedStyleStates() const;
334 * Add a style state lock on this element.
335 * aEnabled is the value to lock the given state bits to.
337 void LockStyleStates(EventStates aStates, bool aEnabled);
340 * Remove a style state lock on this element.
342 void UnlockStyleStates(EventStates aStates);
345 * Clear all style state locks on this element.
347 void ClearStyleStateLocks();
350 * Accessors for the state of our dir attribute.
352 bool HasDirAuto() const {
353 return State().HasState(NS_EVENT_STATE_DIR_ATTR_LIKE_AUTO);
357 * Elements with dir="rtl" or dir="ltr".
359 bool HasFixedDir() const {
360 return State().HasAtLeastOneOfStates(NS_EVENT_STATE_DIR_ATTR_LTR |
361 NS_EVENT_STATE_DIR_ATTR_RTL);
365 * Get the inline style declaration, if any, for this element.
367 DeclarationBlock* GetInlineStyleDeclaration() const;
370 * Get the mapped attributes, if any, for this element.
372 const nsMappedAttributes* GetMappedAttributes() const;
374 void ClearMappedServoStyle() { mAttrs.ClearMappedServoStyle(); }
377 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
378 * so that the element implementation can access the old style attribute
379 * value.
381 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
384 * Set the inline style declaration for this element.
386 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
387 MutationClosureData& aData);
390 * Get the SMIL override style declaration for this element. If the
391 * rule hasn't been created, this method simply returns null.
393 DeclarationBlock* GetSMILOverrideStyleDeclaration();
396 * Set the SMIL override style declaration for this element. This method will
397 * notify the document's pres context, so that the style changes will be
398 * noticed.
400 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
403 * Returns a new SMILAttr that allows the caller to animate the given
404 * attribute on this element.
406 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
407 nsAtom* aName);
410 * Get the SMIL override style for this element. This is a style declaration
411 * that is applied *after* the inline style, and it can be used e.g. to store
412 * animated style values.
414 * Note: This method is analogous to the 'GetStyle' method in
415 * nsGenericHTMLElement and nsStyledElement.
417 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
420 * Returns if the element is labelable as per HTML specification.
422 virtual bool IsLabelable() const;
425 * Returns if the element is interactive content as per HTML specification.
427 virtual bool IsInteractiveHTMLContent() const;
430 * Returns |this| as an nsIMozBrowserFrame* if the element is a frame or
431 * iframe element.
433 * We have this method, rather than using QI, so that we can use it during
434 * the servo traversal, where we can't QI DOM nodes because of non-thread-safe
435 * refcounts.
437 virtual nsIMozBrowserFrame* GetAsMozBrowserFrame() { return nullptr; }
440 * Is the attribute named stored in the mapped attributes?
442 * // XXXbz we use this method in HasAttributeDependentStyle, so svg
443 * returns true here even though it stores nothing in the mapped
444 * attributes.
446 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
449 * Get a hint that tells the style system what to do when
450 * an attribute on this node changes, if something needs to happen
451 * in response to the change *other* than the result of what is
452 * mapped into style data via any type of style rule.
454 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
455 int32_t aModType) const;
457 inline Directionality GetDirectionality() const {
458 if (HasFlag(NODE_HAS_DIRECTION_RTL)) {
459 return eDir_RTL;
462 if (HasFlag(NODE_HAS_DIRECTION_LTR)) {
463 return eDir_LTR;
466 return eDir_NotSet;
469 inline void SetDirectionality(Directionality aDir, bool aNotify) {
470 UnsetFlags(NODE_ALL_DIRECTION_FLAGS);
471 if (!aNotify) {
472 RemoveStatesSilently(DIRECTION_STATES);
475 switch (aDir) {
476 case (eDir_RTL):
477 SetFlags(NODE_HAS_DIRECTION_RTL);
478 if (!aNotify) {
479 AddStatesSilently(NS_EVENT_STATE_RTL);
481 break;
483 case (eDir_LTR):
484 SetFlags(NODE_HAS_DIRECTION_LTR);
485 if (!aNotify) {
486 AddStatesSilently(NS_EVENT_STATE_LTR);
488 break;
490 default:
491 break;
495 * Only call UpdateState if we need to notify, because we call
496 * SetDirectionality for every element, and UpdateState is very very slow
497 * for some elements.
499 if (aNotify) {
500 UpdateState(true);
504 Directionality GetComputedDirectionality() const;
506 static const uint32_t kAllServoDescendantBits =
507 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO |
508 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO |
509 NODE_DESCENDANTS_NEED_FRAMES;
512 * Notes that something in the given subtree of this element needs dirtying,
513 * and that all the relevant dirty bits have already been propagated up to the
514 * element.
516 * This is important because `NoteDirtyForServo` uses the dirty bits to reason
517 * about the shape of the tree, so we can't just call into there.
519 void NoteDirtySubtreeForServo();
521 void NoteDirtyForServo();
522 void NoteAnimationOnlyDirtyForServo();
523 void NoteDescendantsNeedFramesForServo();
525 bool HasDirtyDescendantsForServo() const {
526 return HasFlag(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
529 void SetHasDirtyDescendantsForServo() {
530 SetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
533 void UnsetHasDirtyDescendantsForServo() {
534 UnsetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
537 bool HasAnimationOnlyDirtyDescendantsForServo() const {
538 return HasFlag(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
541 void SetHasAnimationOnlyDirtyDescendantsForServo() {
542 SetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
545 void UnsetHasAnimationOnlyDirtyDescendantsForServo() {
546 UnsetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
549 bool HasServoData() const { return !!mServoData.Get(); }
551 void ClearServoData() { ClearServoData(GetComposedDoc()); }
552 void ClearServoData(Document* aDocument);
555 * Gets the custom element data used by web components custom element.
556 * Custom element data is created at the first attempt to enqueue a callback.
558 * @return The custom element data or null if none.
560 inline CustomElementData* GetCustomElementData() const {
561 if (!HasCustomElementData()) {
562 return nullptr;
565 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
566 return slots ? slots->mCustomElementData.get() : nullptr;
570 * Sets the custom element data, ownership of the
571 * callback data is taken by this element.
573 * @param aData The custom element data.
575 void SetCustomElementData(CustomElementData* aData);
578 * Gets the custom element definition used by web components custom element.
580 * @return The custom element definition or null if element is not a custom
581 * element or custom element is not defined yet.
583 CustomElementDefinition* GetCustomElementDefinition() const;
586 * Sets the custom element definition, called when custom element is created
587 * or upgraded.
589 * @param aDefinition The custom element definition.
591 void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
593 void SetDefined(bool aSet) {
594 if (aSet) {
595 AddStates(NS_EVENT_STATE_DEFINED);
596 } else {
597 RemoveStates(NS_EVENT_STATE_DEFINED);
601 // AccessibilityRole
602 REFLECT_DOMSTRING_ATTR(Role, role)
604 // AriaAttributes
605 REFLECT_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
606 REFLECT_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
607 REFLECT_DOMSTRING_ATTR(AriaBusy, aria_busy)
608 REFLECT_DOMSTRING_ATTR(AriaChecked, aria_checked)
609 REFLECT_DOMSTRING_ATTR(AriaColCount, aria_colcount)
610 REFLECT_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
611 REFLECT_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
612 REFLECT_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
613 REFLECT_DOMSTRING_ATTR(AriaCurrent, aria_current)
614 REFLECT_DOMSTRING_ATTR(AriaDescription, aria_description)
615 REFLECT_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
616 REFLECT_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
617 REFLECT_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
618 REFLECT_DOMSTRING_ATTR(AriaHidden, aria_hidden)
619 REFLECT_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
620 REFLECT_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
621 REFLECT_DOMSTRING_ATTR(AriaLabel, aria_label)
622 REFLECT_DOMSTRING_ATTR(AriaLevel, aria_level)
623 REFLECT_DOMSTRING_ATTR(AriaLive, aria_live)
624 REFLECT_DOMSTRING_ATTR(AriaModal, aria_modal)
625 REFLECT_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
626 REFLECT_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
627 REFLECT_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
628 REFLECT_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
629 REFLECT_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
630 REFLECT_DOMSTRING_ATTR(AriaPressed, aria_pressed)
631 REFLECT_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
632 REFLECT_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
633 REFLECT_DOMSTRING_ATTR(AriaRequired, aria_required)
634 REFLECT_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
635 REFLECT_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
636 REFLECT_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
637 REFLECT_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
638 REFLECT_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
639 REFLECT_DOMSTRING_ATTR(AriaSelected, aria_selected)
640 REFLECT_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
641 REFLECT_DOMSTRING_ATTR(AriaSort, aria_sort)
642 REFLECT_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
643 REFLECT_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
644 REFLECT_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
645 REFLECT_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
647 protected:
649 * Method to get the _intrinsic_ content state of this element. This is the
650 * state that is independent of the element's presentation. To get the full
651 * content state, use State(). See mozilla/EventStates.h for
652 * the possible bits that could be set here.
654 virtual EventStates IntrinsicState() const;
657 * Method to add state bits. This should be called from subclass
658 * constructors to set up our event state correctly at construction
659 * time and other places where we don't want to notify a state
660 * change.
662 void AddStatesSilently(EventStates aStates) { mState |= aStates; }
665 * Method to remove state bits. This should be called from subclass
666 * constructors to set up our event state correctly at construction
667 * time and other places where we don't want to notify a state
668 * change.
670 void RemoveStatesSilently(EventStates aStates) { mState &= ~aStates; }
672 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
673 ErrorResult& aError);
675 public:
676 MOZ_CAN_RUN_SCRIPT
677 nsIScrollableFrame* GetScrollFrame(nsIFrame** aStyledFrame = nullptr,
678 FlushType aFlushType = FlushType::Layout);
680 private:
681 // Need to allow the ESM, nsGlobalWindow, and the focus manager
682 // and Document to set our state
683 friend class mozilla::EventStateManager;
684 friend class mozilla::dom::Document;
685 friend class ::nsGlobalWindowInner;
686 friend class ::nsGlobalWindowOuter;
687 friend class ::nsFocusManager;
689 // Allow CusomtElementRegistry to call AddStates.
690 friend class CustomElementRegistry;
692 // Also need to allow Link to call UpdateLinkState.
693 friend class Link;
695 void NotifyStateChange(EventStates aStates);
697 void NotifyStyleStateChange(EventStates aStates);
699 // Style state computed from element's state and style locks.
700 EventStates StyleStateFromLocks() const;
702 protected:
703 // Methods for the ESM, nsGlobalWindow, focus manager and Document to
704 // manage state bits.
705 // These will handle setting up script blockers when they notify, so no need
706 // to do it in the callers unless desired. States passed here must only be
707 // those in EXTERNALLY_MANAGED_STATES.
708 virtual void AddStates(EventStates aStates) {
709 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(INTRINSIC_STATES),
710 "Should only be adding externally-managed states here");
711 AddStatesSilently(aStates);
712 NotifyStateChange(aStates);
714 virtual void RemoveStates(EventStates aStates) {
715 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(INTRINSIC_STATES),
716 "Should only be removing externally-managed states here");
717 RemoveStatesSilently(aStates);
718 NotifyStateChange(aStates);
720 virtual void ToggleStates(EventStates aStates, bool aNotify) {
721 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(INTRINSIC_STATES),
722 "Should only be removing externally-managed states here");
723 mState ^= aStates;
724 if (aNotify) {
725 NotifyStateChange(aStates);
729 public:
730 // Public methods to manage state bits in MANUALLY_MANAGED_STATES.
731 void AddManuallyManagedStates(EventStates aStates) {
732 MOZ_ASSERT(MANUALLY_MANAGED_STATES.HasAllStates(aStates),
733 "Should only be adding manually-managed states here");
734 AddStates(aStates);
736 void RemoveManuallyManagedStates(EventStates aStates) {
737 MOZ_ASSERT(MANUALLY_MANAGED_STATES.HasAllStates(aStates),
738 "Should only be removing manually-managed states here");
739 RemoveStates(aStates);
742 void UpdateEditableState(bool aNotify) override;
744 nsresult BindToTree(BindContext&, nsINode& aParent) override;
746 void UnbindFromTree(bool aNullParent = true) override;
749 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
750 * with that name exists. This method is intended for character case
751 * conversion if the content object is case insensitive (e.g. HTML). Returns
752 * the nodeinfo of the attribute with the specified name if one exists or
753 * null otherwise.
755 * @param aStr the unparsed attribute string
756 * @return the node info. May be nullptr.
758 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
759 const nsAString& aStr) const;
762 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
763 * is true or there are mutation listeners that must be triggered, the
764 * attribute is currently set, and the new value that is about to be set is
765 * different to the current value. As a perf optimization the new and old
766 * values will not actually be compared if we aren't notifying and we don't
767 * have mutation listeners (in which case it's cheap to just return false
768 * and let the caller go ahead and set the value).
769 * @param aOldValue [out] Set to the old value of the attribute, but only if
770 * there are event listeners. If set, the type of aOldValue will be either
771 * nsAttrValue::eString or nsAttrValue::eAtom.
772 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
773 * MutationEvent_Binding::ADDITION, but only if this helper returns true
774 * @param aHasListeners [out] Set to true if there are mutation event
775 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
776 * @param aOldValueSet [out] Indicates whether an old attribute value has been
777 * stored in aOldValue. The bool will be set to true if a value was stored.
779 bool MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
780 const nsAtom* aPrefix,
781 const nsAttrValueOrString& aValue, bool aNotify,
782 nsAttrValue& aOldValue, uint8_t* aModType,
783 bool* aHasListeners, bool* aOldValueSet);
786 * Notifies mutation listeners if aNotify is true, there are mutation
787 * listeners, and the attribute value is changing.
789 * @param aNamespaceID The namespace of the attribute
790 * @param aName The local name of the attribute
791 * @param aPrefix The prefix of the attribute
792 * @param aValue The value that the attribute is being changed to
793 * @param aNotify If true, mutation listeners will be notified if they exist
794 * and the attribute value is changing
795 * @param aOldValue [out] Set to the old value of the attribute, but only if
796 * there are event listeners. If set, the type of aOldValue will be either
797 * nsAttrValue::eString or nsAttrValue::eAtom.
798 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
799 * MutationEvent_Binding::ADDITION, but only if this helper returns true
800 * @param aHasListeners [out] Set to true if there are mutation event
801 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
802 * @param aOldValueSet [out] Indicates whether an old attribute value has been
803 * stored in aOldValue. The bool will be set to true if a value was stored.
805 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
806 nsAtom* aPrefix,
807 const nsAttrValueOrString& aValue, bool aNotify,
808 nsAttrValue& aOldValue, uint8_t* aModType,
809 bool* aHasListeners, bool* aOldValueSet);
812 * Sets the class attribute to a value that contains no whitespace.
813 * Assumes that we are not notifying and that the attribute hasn't been
814 * set previously.
816 nsresult SetSingleClassFromParser(nsAtom* aSingleClassName);
818 // aParsedValue receives the old value of the attribute. That's useful if
819 // either the input or output value of aParsedValue is StoresOwnData.
820 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
821 nsAttrValue& aParsedValue, bool aNotify);
823 * Get the current value of the attribute. This returns a form that is
824 * suitable for passing back into SetAttr.
826 * @param aNameSpaceID the namespace of the attr (defaults to
827 kNameSpaceID_None in the overload that omits this arg)
828 * @param aName the name of the attr
829 * @param aResult the value (may legitimately be the empty string) [OUT]
830 * @returns true if the attribute was set (even when set to empty string)
831 * false when not set.
832 * GetAttr is not inlined on purpose, to keep down codesize from all the
833 * inlined nsAttrValue bits for C++ callers.
835 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
836 nsAString& aResult) const;
838 bool GetAttr(const nsAtom* aName, nsAString& aResult) const {
839 return GetAttr(kNameSpaceID_None, aName, aResult);
843 * Determine if an attribute has been set (empty string or otherwise).
845 * @param aNameSpaceId the namespace id of the attribute (defaults to
846 kNameSpaceID_None in the overload that omits this arg)
847 * @param aAttr the attribute name
848 * @return whether an attribute exists
850 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
852 bool HasAttr(const nsAtom* aAttr) const {
853 return HasAttr(kNameSpaceID_None, aAttr);
857 * Determine if an attribute has been set to a non-empty string value. If the
858 * attribute is not set at all, this will return false.
860 * @param aNameSpaceId the namespace id of the attribute (defaults to
861 * kNameSpaceID_None in the overload that omits this arg)
862 * @param aAttr the attribute name
864 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
866 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
867 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
871 * Test whether this Element's given attribute has the given value. If the
872 * attribute is not set at all, this will return false.
874 * @param aNameSpaceID The namespace ID of the attribute. Must not
875 * be kNameSpaceID_Unknown.
876 * @param aName The name atom of the attribute. Must not be null.
877 * @param aValue The value to compare to.
878 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
880 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
881 const nsAString& aValue,
882 nsCaseTreatment aCaseSensitive) const;
885 * Test whether this Element's given attribute has the given value. If the
886 * attribute is not set at all, this will return false.
888 * @param aNameSpaceID The namespace ID of the attribute. Must not
889 * be kNameSpaceID_Unknown.
890 * @param aName The name atom of the attribute. Must not be null.
891 * @param aValue The value to compare to. Must not be null.
892 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
894 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
895 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
897 enum { ATTR_MISSING = -1, ATTR_VALUE_NO_MATCH = -2 };
899 * Check whether this Element's given attribute has one of a given list of
900 * values. If there is a match, we return the index in the list of the first
901 * matching value. If there was no attribute at all, then we return
902 * ATTR_MISSING. If there was an attribute but it didn't match, we return
903 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
905 * @param aNameSpaceID The namespace ID of the attribute. Must not
906 * be kNameSpaceID_Unknown.
907 * @param aName The name atom of the attribute. Must not be null.
908 * @param aValues a nullptr-terminated array of pointers to atom values to
909 * test against.
910 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
911 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
912 * indicating the first value of aValues that matched
914 typedef nsStaticAtom* const AttrValuesArray;
915 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
916 AttrValuesArray* aValues,
917 nsCaseTreatment aCaseSensitive) const;
920 * Set attribute values. All attribute values are assumed to have a
921 * canonical string representation that can be used for these
922 * methods. The SetAttr method is assumed to perform a translation
923 * of the canonical form into the underlying content specific
924 * form.
926 * @param aNameSpaceID the namespace of the attribute
927 * @param aName the name of the attribute
928 * @param aValue the value to set
929 * @param aNotify specifies how whether or not the document should be
930 * notified of the attribute change.
932 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
933 bool aNotify) {
934 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
936 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
937 const nsAString& aValue, bool aNotify) {
938 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
940 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
941 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
942 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
943 aNotify);
947 * Set attribute values. All attribute values are assumed to have a
948 * canonical String representation that can be used for these
949 * methods. The SetAttr method is assumed to perform a translation
950 * of the canonical form into the underlying content specific
951 * form.
953 * @param aNameSpaceID the namespace of the attribute
954 * @param aName the name of the attribute
955 * @param aPrefix the prefix of the attribute
956 * @param aValue the value to set
957 * @param aMaybeScriptedPrincipal the principal of the scripted caller
958 * responsible for setting the attribute, or null if no scripted caller can be
959 * determined. A null value here does not guarantee that there is no
960 * scripted caller, but a non-null value does guarantee that a scripted
961 * caller with the given principal is directly responsible for the
962 * attribute change.
963 * @param aNotify specifies how whether or not the document should be
964 * notified of the attribute change.
966 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
967 const nsAString& aValue,
968 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
971 * Remove an attribute so that it is no longer explicitly specified.
973 * @param aNameSpaceID the namespace id of the attribute
974 * @param aAttr the name of the attribute to unset
975 * @param aNotify specifies whether or not the document should be
976 * notified of the attribute change
978 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aAttribute, bool aNotify);
981 * Get the namespace / name / prefix of a given attribute.
983 * @param aIndex the index of the attribute name
984 * @returns The name at the given index, or null if the index is
985 * out-of-bounds.
986 * @note The document returned by NodeInfo()->GetDocument() (if one is
987 * present) is *not* necessarily the owner document of the element.
988 * @note The pointer returned by this function is only valid until the
989 * next call of either GetAttrNameAt or SetAttr on the element.
991 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
992 return mAttrs.GetSafeAttrNameAt(aIndex);
996 * Same as above, but does not do out-of-bounds checks!
998 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
999 return mAttrs.AttrNameAt(aIndex);
1003 * Gets the attribute info (name and value) for this element at a given index.
1005 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
1006 if (aIndex >= mAttrs.AttrCount()) {
1007 return BorrowedAttrInfo(nullptr, nullptr);
1010 return mAttrs.AttrInfoAt(aIndex);
1014 * Get the number of all specified attributes.
1016 * @return the number of attributes
1018 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
1020 virtual bool IsNodeOfType(uint32_t aFlags) const override;
1023 * Get the class list of this element (this corresponds to the value of the
1024 * class attribute). This may be null if there are no classes, but that's not
1025 * guaranteed (e.g. we could have class="").
1027 const nsAttrValue* GetClasses() const {
1028 if (!MayHaveClass()) {
1029 return nullptr;
1032 if (IsSVGElement()) {
1033 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
1034 return value;
1038 return GetParsedAttr(nsGkAtoms::_class);
1041 #ifdef MOZ_DOM_LIST
1042 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
1043 List(out, aIndent, ""_ns);
1045 virtual void DumpContent(FILE* out, int32_t aIndent,
1046 bool aDumpAll) const override;
1047 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1048 void ListAttributes(FILE* out) const;
1049 #endif
1052 * Append to aOutDescription a short (preferably one line) string
1053 * describing the element.
1055 void Describe(nsAString& aOutDescription) const;
1058 * Attribute Mapping Helpers
1060 struct MappedAttributeEntry {
1061 const nsStaticAtom* const attribute;
1065 * A common method where you can just pass in a list of maps to check
1066 * for attribute dependence. Most implementations of
1067 * IsAttributeMapped should use this function as a default
1068 * handler.
1070 template <size_t N>
1071 static bool FindAttributeDependence(
1072 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1073 return FindAttributeDependence(aAttribute, aMaps, N);
1076 static nsStaticAtom* const* HTMLSVGPropertiesToTraverseAndUnlink();
1078 private:
1079 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1081 static bool FindAttributeDependence(const nsAtom* aAttribute,
1082 const MappedAttributeEntry* const aMaps[],
1083 uint32_t aMapCount);
1085 protected:
1086 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1087 DOMString& aResult) const {
1088 NS_ASSERTION(nullptr != aName, "must have attribute name");
1089 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
1090 "must have a real namespace ID!");
1091 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1092 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1093 if (val) {
1094 val->ToString(aResult);
1095 return true;
1097 // else DOMString comes pre-emptied.
1098 return false;
1101 public:
1102 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1104 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1105 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1106 const nsAttrValue* val = mAttrs.GetAttr(aName);
1107 if (val) {
1108 val->ToString(aResult);
1109 return true;
1111 // else DOMString comes pre-emptied.
1112 return false;
1115 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1116 void GetId(nsAString& aId) const {
1117 GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
1119 void GetId(DOMString& aId) const {
1120 GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
1122 void SetId(const nsAString& aId) {
1123 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1125 void GetClassName(nsAString& aClassName) {
1126 GetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName);
1128 void GetClassName(DOMString& aClassName) {
1129 GetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName);
1131 void SetClassName(const nsAString& aClassName) {
1132 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1135 nsDOMTokenList* ClassList();
1136 nsDOMTokenList* Part();
1138 nsDOMAttributeMap* Attributes();
1140 void GetAttributeNames(nsTArray<nsString>& aResult);
1142 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1143 DOMString str;
1144 GetAttribute(aName, str);
1145 str.ToString(aReturn);
1148 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1149 void GetAttributeNS(const nsAString& aNamespaceURI,
1150 const nsAString& aLocalName, nsAString& aReturn);
1151 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1152 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1153 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1154 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1155 void SetAttributeNS(const nsAString& aNamespaceURI,
1156 const nsAString& aLocalName, const nsAString& aValue,
1157 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1158 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1159 ErrorResult& aError) {
1160 SetAttribute(aName, aValue, nullptr, aError);
1163 * This method creates a principal that subsumes this element's NodePrincipal
1164 * and which has flags set for elevated permissions that devtools needs to
1165 * operate on this element. The principal returned by this method is used by
1166 * various devtools methods to permit otherwise blocked operations, without
1167 * changing any other restrictions the NodePrincipal might have.
1169 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1170 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1171 ErrorResult& aError);
1172 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1173 const nsAString& aLocalName,
1174 const nsAString& aValue, ErrorResult& aError);
1176 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1177 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1178 const nsAString& aLocalName, ErrorResult& aError);
1179 bool HasAttribute(const nsAString& aName) const {
1180 return InternalGetAttrNameFromQName(aName) != nullptr;
1182 bool HasAttributeNS(const nsAString& aNamespaceURI,
1183 const nsAString& aLocalName) const;
1184 bool HasAttributes() const { return HasAttrs(); }
1185 Element* Closest(const nsACString& aSelector, ErrorResult& aResult);
1186 bool Matches(const nsACString& aSelector, ErrorResult& aError);
1187 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1188 const nsAString& aQualifiedName);
1189 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1190 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1191 ErrorResult& aError);
1192 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1193 const nsAString& aClassNames);
1195 PseudoStyleType GetPseudoElementType() const {
1196 nsresult rv = NS_OK;
1197 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1198 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1199 return PseudoStyleType::NotPseudo;
1201 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1204 void SetPseudoElementType(PseudoStyleType aPseudo) {
1205 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1206 "Need to be able to store this in a void*");
1207 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1208 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1212 * Return an array of all elements in the subtree rooted at this
1213 * element that have grid container frames. This does not include
1214 * pseudo-elements.
1216 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1219 * Provide a direct way to determine if this Element has visible
1220 * scrollbars. Flushes layout.
1222 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1224 private:
1226 * Implement the algorithm specified at
1227 * https://dom.spec.whatwg.org/#insert-adjacent for both
1228 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1230 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1231 ErrorResult& aError);
1233 public:
1234 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1235 ErrorResult& aError);
1237 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1238 ErrorResult& aError);
1240 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError);
1241 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError);
1242 bool HasPointerCapture(long aPointerId);
1243 void SetCapture(bool aRetargetToElement);
1245 void SetCaptureAlways(bool aRetargetToElement);
1247 void ReleaseCapture();
1249 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1250 void RequestPointerLock(CallerType aCallerType);
1251 Attr* GetAttributeNode(const nsAString& aName);
1252 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1253 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1254 ErrorResult& aError);
1255 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1256 const nsAString& aLocalName);
1257 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1258 ErrorResult& aError);
1260 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1261 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1263 // Shadow DOM v1
1264 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1265 ErrorResult& aError);
1266 bool CanAttachShadowDOM() const;
1268 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1269 ShadowRootMode aMode);
1271 // Attach UA Shadow Root if it is not attached.
1272 enum class NotifyUAWidgetSetup : bool { No, Yes };
1273 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes);
1275 // Dispatch an event to UAWidgetsChild, triggering construction
1276 // or onchange callback on the existing widget.
1277 void NotifyUAWidgetSetupOrChange();
1279 enum class UnattachShadowRoot {
1281 Yes,
1284 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1285 // and optionally remove the shadow root.
1286 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1288 void UnattachShadow();
1290 ShadowRoot* GetShadowRootByMode() const;
1291 void SetSlot(const nsAString& aName, ErrorResult& aError);
1292 void GetSlot(nsAString& aName);
1294 ShadowRoot* GetShadowRoot() const {
1295 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1296 return slots ? slots->mShadowRoot.get() : nullptr;
1299 private:
1300 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1302 public:
1303 MOZ_CAN_RUN_SCRIPT
1304 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1305 MOZ_CAN_RUN_SCRIPT void Scroll(double aXScroll, double aYScroll);
1306 MOZ_CAN_RUN_SCRIPT void Scroll(const ScrollToOptions& aOptions);
1307 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1308 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1309 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1310 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1311 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1312 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1313 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1314 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1315 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1316 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1317 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1318 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1319 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1321 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1322 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1324 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1325 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1327 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1328 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1330 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1331 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1332 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1333 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1335 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1336 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1339 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1340 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1343 // This function will return the block size of first line box, no matter if
1344 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1345 // can't get a primary frame, we will return be zero.
1346 double FirstLineBoxBSize() const;
1348 already_AddRefed<Flex> GetAsFlexContainer();
1349 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1351 bool HasGridFragments();
1353 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1354 Element& aAncestor);
1355 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1356 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1358 already_AddRefed<Animation> Animate(
1359 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1360 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1361 ErrorResult& aError);
1363 MOZ_CAN_RUN_SCRIPT
1364 void GetAnimations(const GetAnimationsOptions& aOptions,
1365 nsTArray<RefPtr<Animation>>& aAnimations);
1367 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1368 nsTArray<RefPtr<Animation>>& aAnimations);
1370 static void GetAnimationsUnsorted(Element* aElement,
1371 PseudoStyleType aPseudoType,
1372 nsTArray<RefPtr<Animation>>& aAnimations);
1374 void CloneAnimationsFrom(const Element& aOther);
1376 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1377 virtual void SetInnerHTML(const nsAString& aInnerHTML,
1378 nsIPrincipal* aSubjectPrincipal,
1379 ErrorResult& aError);
1380 void GetOuterHTML(nsAString& aOuterHTML);
1381 void SetOuterHTML(const nsAString& aOuterHTML, ErrorResult& aError);
1382 void InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
1383 ErrorResult& aError);
1385 //----------------------------------------
1388 * Add a script event listener with the given event handler name
1389 * (like onclick) and with the value as JS
1390 * @param aEventName the event listener name
1391 * @param aValue the JS to attach
1392 * @param aDefer indicates if deferred execution is allowed
1394 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1395 bool aDefer = true);
1398 * Do whatever needs to be done when the mouse leaves a link
1400 nsresult LeaveLink(nsPresContext* aPresContext);
1402 static bool ShouldBlur(nsIContent* aContent);
1405 * Method to create and dispatch a left-click event loosely based on
1406 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1407 * through the full dispatching of the presshell of the aPresContext; if it's
1408 * false the event will be dispatched only as a DOM event.
1409 * If aPresContext is nullptr, this does nothing.
1411 * @param aFlags Extra flags for the dispatching event. The true flags
1412 * will be respected.
1414 MOZ_CAN_RUN_SCRIPT
1415 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1416 WidgetInputEvent* aSourceEvent,
1417 nsIContent* aTarget, bool aFullDispatch,
1418 const EventFlags* aFlags,
1419 nsEventStatus* aStatus);
1422 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1423 * will be dispatched through the full dispatching of the presshell of the
1424 * aPresContext; if it's false the event will be dispatched only as a DOM
1425 * event.
1426 * If aPresContext is nullptr, this does nothing.
1428 using nsIContent::DispatchEvent;
1429 MOZ_CAN_RUN_SCRIPT
1430 static nsresult DispatchEvent(nsPresContext* aPresContext,
1431 WidgetEvent* aEvent, nsIContent* aTarget,
1432 bool aFullDispatch, nsEventStatus* aStatus);
1434 bool IsDisplayContents() const {
1435 return HasServoData() && Servo_Element_IsDisplayContents(this);
1439 * https://html.spec.whatwg.org/#being-rendered
1441 * With a gotcha for display contents:
1442 * https://github.com/whatwg/html/issues/1837
1444 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1446 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1447 return mAttrs.GetAttr(aAttr);
1450 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1451 int32_t aNameSpaceID) const {
1452 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1456 * Returns the attribute map, if there is one.
1458 * @return existing attribute map or nullptr.
1460 nsDOMAttributeMap* GetAttributeMap() {
1461 nsDOMSlots* slots = GetExistingDOMSlots();
1463 return slots ? slots->mAttributeMap.get() : nullptr;
1466 virtual void RecompileScriptEventListeners() {}
1469 * Get the attr info for the given namespace ID and attribute name. The
1470 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1471 * null. Note that this can only return info on attributes that actually
1472 * live on this element (and is only virtual to handle XUL prototypes). That
1473 * is, this should only be called from methods that only care about attrs
1474 * that effectively live in mAttrs.
1476 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1477 const nsAtom* aName) const {
1478 NS_ASSERTION(aName, "must have attribute name");
1479 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1480 "must have a real namespace ID!");
1482 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1483 if (index < 0) {
1484 return BorrowedAttrInfo(nullptr, nullptr);
1487 return mAttrs.AttrInfoAt(index);
1491 * Parse a string into an nsAttrValue for a CORS attribute. This
1492 * never fails. The resulting value is an enumerated value whose
1493 * GetEnumValue() returns one of the above constants.
1495 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1498 * Return the CORS mode for a given string
1500 static CORSMode StringToCORSMode(const nsAString& aValue);
1503 * Return the CORS mode for a given nsAttrValue (which may be null,
1504 * but if not should have been parsed via ParseCORSValue).
1506 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1508 nsINode* GetScopeChainParent() const override;
1511 * Locate a TextEditor rooted at this content node, if there is one.
1513 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1516 * Gets value of boolean attribute. Only works for attributes in null
1517 * namespace.
1519 * @param aAttr name of attribute.
1520 * @param aValue Boolean value of attribute.
1522 bool GetBoolAttr(nsAtom* aAttr) const {
1523 return HasAttr(kNameSpaceID_None, aAttr);
1527 * Sets value of boolean attribute by removing attribute or setting it to
1528 * the empty string. Only works for attributes in null namespace.
1530 * @param aAttr name of attribute.
1531 * @param aValue Boolean value of attribute.
1533 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1536 * Gets the enum value string of an attribute and using a default value if
1537 * the attribute is missing or the string is an invalid enum value.
1539 * @param aType the name of the attribute.
1540 * @param aDefault the default value if the attribute is missing or invalid.
1541 * @param aResult string corresponding to the value [out].
1543 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1544 nsAString& aResult) const;
1547 * Gets the enum value string of an attribute and using the default missing
1548 * value if the attribute is missing or the default invalid value if the
1549 * string is an invalid enum value.
1551 * @param aType the name of the attribute.
1552 * @param aDefaultMissing the default value if the attribute is missing. If
1553 null and the attribute is missing, aResult will be
1554 set to the null DOMString; this only matters for
1555 cases in which we're reflecting a nullable string.
1556 * @param aDefaultInvalid the default value if the attribute is invalid.
1557 * @param aResult string corresponding to the value [out].
1559 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1560 const char* aDefaultInvalid, nsAString& aResult) const;
1563 * Unset an attribute.
1565 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1566 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1570 * Set an attribute in the simplest way possible.
1572 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1573 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1576 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1577 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1578 aError =
1579 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1583 * Set a content attribute via a reflecting nullable string IDL
1584 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1585 * this will actually remove the content attribute.
1587 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1588 ErrorResult& aError);
1591 * Retrieve the ratio of font-size-inflated text font size to computed font
1592 * size for this element. This will query the element for its primary frame,
1593 * and then use this to get font size inflation information about the frame.
1595 * @returns The font size inflation ratio (inflated font size to uninflated
1596 * font size) for the primary frame of this element. Returns 1.0
1597 * by default if font size inflation is not enabled. Returns -1
1598 * if the element does not have a primary frame.
1600 * @note The font size inflation ratio that is returned is actually the
1601 * font size inflation data for the element's _primary frame_, not the
1602 * element itself, but for most purposes, this should be sufficient.
1604 float FontSizeInflation();
1606 void GetImplementedPseudoElement(nsAString&) const;
1608 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1609 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1612 * Helpers for .dataset. This is implemented on Element, though only some
1613 * sorts of elements expose it to JS as a .dataset property
1615 // Getter, to be called from bindings.
1616 already_AddRefed<nsDOMStringMap> Dataset();
1617 // Callback for destructor of dataset to ensure to null out our weak pointer
1618 // to it.
1619 void ClearDataset();
1621 void RegisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1622 void UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1623 void UnlinkIntersectionObservers();
1624 bool UpdateIntersectionObservation(DOMIntersectionObserver* aObserver,
1625 int32_t threshold);
1627 // A number of methods to cast to various XUL interfaces. They return a
1628 // pointer only if the element implements that interface.
1629 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1630 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1631 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1632 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1633 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1634 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1635 AsXULMultiSelectControl();
1636 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1637 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1638 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1639 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1640 already_AddRefed<nsIBrowser> AsBrowser();
1641 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1644 * Get the presentation context for this content node.
1645 * @return the presentation context
1647 enum PresContextFor { eForComposedDoc, eForUncomposedDoc };
1648 nsPresContext* GetPresContext(PresContextFor aFor);
1651 * The method focuses (or activates) element that accesskey is bound to. It is
1652 * called when accesskey is activated.
1654 * @param aKeyCausesActivation - if true then element should be activated
1655 * @param aIsTrustedEvent - if true then event that is cause of accesskey
1656 * execution is trusted.
1657 * @return true if the focus was changed.
1659 MOZ_CAN_RUN_SCRIPT virtual bool PerformAccesskey(bool aKeyCausesActivation,
1660 bool aIsTrustedEvent) {
1661 return false;
1664 protected:
1666 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1667 * read.
1669 static const bool kFireMutationEvent = true;
1670 static const bool kDontFireMutationEvent = false;
1671 static const bool kNotifyDocumentObservers = true;
1672 static const bool kDontNotifyDocumentObservers = false;
1673 static const bool kCallAfterSetAttr = true;
1674 static const bool kDontCallAfterSetAttr = false;
1677 * Set attribute and (if needed) notify documentobservers and fire off
1678 * mutation events. This will send the AttributeChanged notification.
1679 * Callers of this method are responsible for calling AttributeWillChange,
1680 * since that needs to happen before the new attr value has been set, and
1681 * in particular before it has been parsed.
1683 * For the boolean parameters, consider using the named bools above to aid
1684 * code readability.
1686 * @param aNamespaceID namespace of attribute
1687 * @param aAttribute local-name of attribute
1688 * @param aPrefix aPrefix of attribute
1689 * @param aOldValue The old value of the attribute to use as a fallback
1690 * in the cases where the actual old value (i.e.
1691 * its current value) is !StoresOwnData() --- in which
1692 * case the current value is probably already useless.
1693 * If the current value is StoresOwnData() (or absent),
1694 * aOldValue will not be used. aOldValue will only be set
1695 * in certain circumstances (there are mutation
1696 * listeners, element is a custom element, attribute was
1697 * not previously unset). Otherwise it will be null.
1698 * @param aParsedValue parsed new value of attribute. Replaced by the
1699 * old value of the attribute. This old value is only
1700 * useful if either it or the new value is StoresOwnData.
1701 * @param aMaybeScriptedPrincipal
1702 * the principal of the scripted caller responsible for
1703 * setting the attribute, or null if no scripted caller
1704 * can be determined. A null value here does not
1705 * guarantee that there is no scripted caller, but a
1706 * non-null value does guarantee that a scripted caller
1707 * with the given principal is directly responsible for
1708 * the attribute change.
1709 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
1710 * needed if aFireMutation or aNotify is true.
1711 * @param aFireMutation should mutation-events be fired?
1712 * @param aNotify should we notify document-observers?
1713 * @param aCallAfterSetAttr should we call AfterSetAttr?
1714 * @param aComposedDocument The current composed document of the element.
1716 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
1717 nsAtom* aPrefix, const nsAttrValue* aOldValue,
1718 nsAttrValue& aParsedValue,
1719 nsIPrincipal* aMaybeScriptedPrincipal,
1720 uint8_t aModType, bool aFireMutation, bool aNotify,
1721 bool aCallAfterSetAttr, Document* aComposedDocument,
1722 const mozAutoDocUpdate& aGuard);
1725 * Scroll to a new position using behavior evaluated from CSS and
1726 * a CSSOM-View DOM method ScrollOptions dictionary. The scrolling may
1727 * be performed asynchronously or synchronously depending on the resolved
1728 * scroll-behavior.
1730 * @param aScroll Destination of scroll, in CSS pixels
1731 * @param aOptions Dictionary of options to be evaluated
1733 MOZ_CAN_RUN_SCRIPT
1734 void Scroll(const CSSIntPoint& aScroll, const ScrollOptions& aOptions);
1737 * Convert an attribute string value to attribute type based on the type of
1738 * attribute. Called by SetAttr(). Note that at the moment we only do this
1739 * for attributes in the null namespace (kNameSpaceID_None).
1741 * @param aNamespaceID the namespace of the attribute to convert
1742 * @param aAttribute the attribute to convert
1743 * @param aValue the string value to convert
1744 * @param aMaybeScriptedPrincipal the principal of the script setting the
1745 * attribute, if one can be determined, or null otherwise. As in
1746 * AfterSetAttr, a null value does not guarantee that the attribute was
1747 * not set by a scripted caller, but a non-null value guarantees that
1748 * the attribute was set by a scripted caller with the given principal.
1749 * @param aResult the nsAttrValue [OUT]
1750 * @return true if the parsing was successful, false otherwise
1752 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1753 const nsAString& aValue,
1754 nsIPrincipal* aMaybeScriptedPrincipal,
1755 nsAttrValue& aResult);
1758 * Try to set the attribute as a mapped attribute, if applicable. This will
1759 * only be called for attributes that are in the null namespace and only on
1760 * attributes that returned true when passed to IsAttributeMapped. The
1761 * caller will not try to set the attr in any other way if this method
1762 * returns true (the value of aRetval does not matter for that purpose).
1764 * @param aName the name of the attribute
1765 * @param aValue the nsAttrValue to set. Will be swapped with the existing
1766 * value of the attribute if the attribute already exists.
1767 * @param [out] aValueWasSet If the attribute was not set previously,
1768 * aValue will be swapped with an empty attribute
1769 * and aValueWasSet will be set to false. Otherwise,
1770 * aValueWasSet will be set to true and aValue will
1771 * contain the previous value set.
1772 * @param [out] aRetval the nsresult status of the operation, if any.
1773 * @return true if the setting was attempted, false otherwise.
1775 virtual bool SetAndSwapMappedAttribute(nsAtom* aName, nsAttrValue& aValue,
1776 bool* aValueWasSet, nsresult* aRetval);
1779 * Hook that is called by Element::SetAttr to allow subclasses to
1780 * deal with attribute sets. This will only be called after we verify that
1781 * we're actually doing an attr set and will be called before
1782 * AttributeWillChange and before ParseAttribute and hence before we've set
1783 * the new value.
1785 * @param aNamespaceID the namespace of the attr being set
1786 * @param aName the localname of the attribute being set
1787 * @param aValue the value it's being set to represented as either a string or
1788 * a parsed nsAttrValue. Alternatively, if the attr is being removed it
1789 * will be null.
1790 * @param aNotify Whether we plan to notify document observers.
1792 virtual nsresult BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1793 const nsAttrValueOrString* aValue,
1794 bool aNotify);
1797 * Hook that is called by Element::SetAttr to allow subclasses to
1798 * deal with attribute sets. This will only be called after we have called
1799 * SetAndSwapAttr (that is, after we have actually set the attr). It will
1800 * always be called under a scriptblocker.
1802 * @param aNamespaceID the namespace of the attr being set
1803 * @param aName the localname of the attribute being set
1804 * @param aValue the value it's being set to. If null, the attr is being
1805 * removed.
1806 * @param aOldValue the value that the attribute had previously. If null,
1807 * the attr was not previously set. This argument may not have the
1808 * correct value for SVG elements, or other cases in which the
1809 * attribute value doesn't store its own data
1810 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1811 * responsible for setting the attribute, or null if no scripted caller
1812 * can be determined, or the attribute is being unset. A null value
1813 * here does not guarantee that there is no scripted caller, but a
1814 * non-null value does guarantee that a scripted caller with the given
1815 * principal is directly responsible for the attribute change.
1816 * @param aNotify Whether we plan to notify document observers.
1818 virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
1819 const nsAttrValue* aValue,
1820 const nsAttrValue* aOldValue,
1821 nsIPrincipal* aMaybeScriptedPrincipal,
1822 bool aNotify);
1825 * This function shall be called just before the id attribute changes. It will
1826 * be called after BeforeSetAttr. If the attribute being changed is not the id
1827 * attribute, this function does nothing. Otherwise, it will remove the old id
1828 * from the document's id cache.
1830 * This must happen after BeforeSetAttr (rather than during) because the
1831 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
1832 * incorrectly determine whether the element had an id, the element may not be
1833 * restyled properly.
1835 * @param aNamespaceID the namespace of the attr being set
1836 * @param aName the localname of the attribute being set
1837 * @param aValue the new id value. Will be null if the id is being unset.
1839 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1840 const nsAttrValueOrString* aValue);
1843 * This function shall be called just after the id attribute changes. It will
1844 * be called before AfterSetAttr. If the attribute being changed is not the id
1845 * attribute, this function does nothing. Otherwise, it will add the new id to
1846 * the document's id cache and properly set the ElementHasID flag.
1848 * This must happen before AfterSetAttr (rather than during) because the
1849 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
1850 * incorrectly determine whether the element now has an id, the element may
1851 * not be restyled properly.
1853 * @param aNamespaceID the namespace of the attr being set
1854 * @param aName the localname of the attribute being set
1855 * @param aValue the new id value. Will be null if the id is being unset.
1857 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1858 const nsAttrValue* aValue);
1861 * Usually, setting an attribute to the value that it already has results in
1862 * no action. However, in some cases, setting an attribute to its current
1863 * value should have the effect of, for example, forcing a reload of
1864 * network data. To address that, this function will be called in this
1865 * situation to allow the handling of such a case.
1867 * @param aNamespaceID the namespace of the attr being set
1868 * @param aName the localname of the attribute being set
1869 * @param aValue the value it's being set to represented as either a string or
1870 * a parsed nsAttrValue.
1871 * @param aNotify Whether we plan to notify document observers.
1873 // Note that this is inlined so that when subclasses call it it gets
1874 // inlined. Those calls don't go through a vtable.
1875 virtual nsresult OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
1876 const nsAttrValueOrString& aValue,
1877 bool aNotify);
1880 * Hook to allow subclasses to produce a different EventListenerManager if
1881 * needed for attachment of attribute-defined handlers
1883 virtual EventListenerManager* GetEventListenerManagerForAttr(
1884 nsAtom* aAttrName, bool* aDefer);
1887 * Internal hook for converting an attribute name-string to nsAttrName in
1888 * case there is such existing attribute. aNameToUse can be passed to get
1889 * name which was used for looking for the attribute (lowercase in HTML).
1891 const nsAttrName* InternalGetAttrNameFromQName(
1892 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
1894 virtual Element* GetNameSpaceElement() override { return this; }
1896 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
1897 const nsAString& aLocalName);
1899 inline void RegisterActivityObserver();
1900 inline void UnregisterActivityObserver();
1903 * Add/remove this element to the documents id cache
1905 void AddToIdTable(nsAtom* aId);
1906 void RemoveFromIdTable();
1909 * Functions to carry out event default actions for links of all types
1910 * (HTML links, XLinks, SVG "XLinks", etc.)
1914 * Check that we meet the conditions to handle a link event
1915 * and that we are actually on a link.
1917 * @param aVisitor event visitor
1918 * @param aURI the uri of the link, set only if the return value is true [OUT]
1919 * @return true if we can handle the link event, false otherwise
1921 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor,
1922 nsIURI** aURI) const;
1925 * Handle status bar updates before they can be cancelled.
1927 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
1930 * Handle default actions for link event if the event isn't consumed yet.
1932 MOZ_CAN_RUN_SCRIPT
1933 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
1936 * Get the target of this link element. Consumers should established that
1937 * this element is a link (probably using IsLink) before calling this
1938 * function (or else why call it?)
1940 * Note: for HTML this gets the value of the 'target' attribute; for XLink
1941 * this gets the value of the xlink:_moz_target attribute, or failing that,
1942 * the value of xlink:show, converted to a suitably equivalent named target
1943 * (e.g. _blank).
1945 virtual void GetLinkTarget(nsAString& aTarget);
1947 enum class ReparseAttributes { No, Yes };
1949 * Copy attributes and state to another element
1950 * @param aDest the object to copy to
1952 nsresult CopyInnerTo(Element* aDest,
1953 ReparseAttributes = ReparseAttributes::Yes);
1956 * Some event handler content attributes have a different name (e.g. different
1957 * case) from the actual event name. This function takes an event handler
1958 * content attribute name and returns the corresponding event name, to be used
1959 * for adding the actual event listener.
1961 static nsAtom* GetEventNameForAttr(nsAtom* aAttr);
1964 * Register/unregister this element to accesskey map if it supports accesskey.
1966 virtual void RegUnRegAccessKey(bool aDoReg);
1968 private:
1969 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1970 void AssertInvariantsOnNodeInfoChange();
1971 #endif
1974 * Slow path for GetClasses, this should only be called for SVG elements.
1976 const nsAttrValue* GetSVGAnimatedClass() const;
1979 * Get this element's client area rect in app units.
1980 * @return the frame's client area
1982 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
1985 * GetCustomInterface is somewhat like a GetInterface, but it is expected
1986 * that the implementation is provided by a custom element or via the
1987 * the XBL implements keyword. To use this, create a public method that
1988 * wraps a call to GetCustomInterface.
1990 template <class T>
1991 void GetCustomInterface(nsGetterAddRefs<T> aResult);
1993 // Prevent people from doing pointless checks/casts on Element instances.
1994 void IsElement() = delete;
1995 void AsElement() = delete;
1997 // Data members
1998 EventStates mState;
1999 // Per-node data managed by Servo.
2001 // There should not be data on nodes that are not in the flattened tree, or
2002 // descendants of display: none elements.
2003 mozilla::RustCell<ServoNodeData*> mServoData;
2005 protected:
2006 // Array containing all attributes for this element
2007 AttrArray mAttrs;
2010 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
2012 inline bool Element::HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
2013 NS_ASSERTION(nullptr != aName, "must have attribute name");
2014 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
2015 "must have a real namespace ID!");
2017 return mAttrs.IndexOfAttr(aName, aNameSpaceID) >= 0;
2020 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2021 const nsAtom* aName) const {
2022 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2023 MOZ_ASSERT(aName, "Must have attribute name");
2025 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2026 return val && !val->IsEmptyString();
2029 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2030 const nsAString& aValue,
2031 nsCaseTreatment aCaseSensitive) const {
2032 NS_ASSERTION(aName, "Must have attr name");
2033 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown, "Must have namespace");
2035 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2036 return val && val->Equals(aValue, aCaseSensitive);
2039 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2040 const nsAtom* aValue,
2041 nsCaseTreatment aCaseSensitive) const {
2042 NS_ASSERTION(aName, "Must have attr name");
2043 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown, "Must have namespace");
2044 NS_ASSERTION(aValue, "Null value atom");
2046 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2047 return val && val->Equals(aValue, aCaseSensitive);
2050 } // namespace dom
2051 } // namespace mozilla
2053 inline mozilla::dom::Element* nsINode::AsElement() {
2054 MOZ_ASSERT(IsElement());
2055 return static_cast<mozilla::dom::Element*>(this);
2058 inline const mozilla::dom::Element* nsINode::AsElement() const {
2059 MOZ_ASSERT(IsElement());
2060 return static_cast<const mozilla::dom::Element*>(this);
2063 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2064 return mozilla::dom::Element::FromNodeOrNull(mParent);
2067 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2068 nsIContent* previousSibling = GetPreviousSibling();
2069 while (previousSibling) {
2070 if (previousSibling->IsElement()) {
2071 return previousSibling->AsElement();
2073 previousSibling = previousSibling->GetPreviousSibling();
2076 return nullptr;
2079 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2080 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2081 : GetParentElement();
2084 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2085 nsIContent* nextSibling = GetNextSibling();
2086 while (nextSibling) {
2087 if (nextSibling->IsElement()) {
2088 return nextSibling->AsElement();
2090 nextSibling = nextSibling->GetNextSibling();
2093 return nullptr;
2097 * Macros to implement Clone(). _elementName is the class for which to implement
2098 * Clone.
2100 #define NS_IMPL_ELEMENT_CLONE(_elementName) \
2101 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2102 nsINode** aResult) const { \
2103 *aResult = nullptr; \
2104 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2105 auto* nim = ni->NodeInfoManager(); \
2106 RefPtr<_elementName> it = new (nim) _elementName(ni.forget()); \
2107 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2108 if (NS_SUCCEEDED(rv)) { \
2109 it.forget(aResult); \
2112 return rv; \
2115 #define EXPAND(...) __VA_ARGS__
2116 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, extra_args_) \
2117 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2118 nsINode** aResult) const { \
2119 *aResult = nullptr; \
2120 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2121 auto* nim = ni->NodeInfoManager(); \
2122 RefPtr<_elementName> it = \
2123 new (nim) _elementName(ni.forget() EXPAND extra_args_); \
2124 nsresult rv = it->Init(); \
2125 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2126 if (NS_FAILED(rv2)) { \
2127 rv = rv2; \
2129 if (NS_SUCCEEDED(rv)) { \
2130 it.forget(aResult); \
2133 return rv; \
2136 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName) \
2137 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, ())
2138 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2139 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, (, NOT_FROM_PARSER))
2141 #endif // mozilla_dom_Element_h__