Bug 1665252 - remove allowpaymentrequest attribute from HTMLIFrameElement r=dom-worke...
[gecko.git] / dom / base / Element.h
blobb4ce566dba4169d4e70b1eb78184387057e20f8c
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 "AttrArray.h"
17 #include "nsAttrValue.h"
18 #include "nsAttrValueInlines.h"
19 #include "nsChangeHint.h"
20 #include "nsContentUtils.h"
21 #include "nsDOMAttributeMap.h"
22 #include "nsRect.h"
23 #include "Units.h"
24 #include "mozilla/Attributes.h"
25 #include "mozilla/CORSMode.h"
26 #include "mozilla/EventStates.h"
27 #include "mozilla/FlushType.h"
28 #include "mozilla/PseudoStyleType.h"
29 #include "mozilla/RustCell.h"
30 #include "mozilla/SMILAttr.h"
31 #include "mozilla/UniquePtr.h"
32 #include "mozilla/dom/DirectionalityUtils.h"
33 #include "mozilla/dom/FragmentOrElement.h"
34 #include "mozilla/dom/PointerEventHandler.h"
35 #include "mozilla/dom/ShadowRootBinding.h"
37 class mozAutoDocUpdate;
38 class nsIFrame;
39 class nsIMozBrowserFrame;
40 class nsIScrollableFrame;
41 class nsIURI;
42 class nsAttrValueOrString;
43 class nsContentList;
44 class nsDOMTokenList;
45 class nsFocusManager;
46 class nsGlobalWindowInner;
47 class nsGlobalWindowOuter;
48 class nsDOMCSSAttributeDeclaration;
49 class nsDOMStringMap;
50 struct ServoNodeData;
52 class nsIDOMXULButtonElement;
53 class nsIDOMXULContainerElement;
54 class nsIDOMXULContainerItemElement;
55 class nsIDOMXULControlElement;
56 class nsIDOMXULMenuListElement;
57 class nsIDOMXULMultiSelectControlElement;
58 class nsIDOMXULRadioGroupElement;
59 class nsIDOMXULRelatedElement;
60 class nsIDOMXULSelectControlElement;
61 class nsIDOMXULSelectControlItemElement;
62 class nsIBrowser;
63 class nsIAutoCompletePopup;
65 namespace mozilla {
66 class DeclarationBlock;
67 struct MutationClosureData;
68 class TextEditor;
69 namespace css {
70 struct URLValue;
71 } // namespace css
72 namespace dom {
73 struct GetAnimationsOptions;
74 struct ScrollIntoViewOptions;
75 struct ScrollToOptions;
76 struct FocusOptions;
77 struct ShadowRootInit;
78 struct ScrollOptions;
79 class BooleanOrScrollIntoViewOptions;
80 class DOMIntersectionObserver;
81 class DOMMatrixReadOnly;
82 class Element;
83 class ElementOrCSSPseudoElement;
84 class UnrestrictedDoubleOrKeyframeAnimationOptions;
85 enum class CallerType : uint32_t;
86 typedef nsDataHashtable<nsRefPtrHashKey<DOMIntersectionObserver>, int32_t>
87 IntersectionObserverList;
88 } // namespace dom
89 } // namespace mozilla
91 // Declared here because of include hell.
92 extern "C" bool Servo_Element_IsDisplayContents(const mozilla::dom::Element*);
94 already_AddRefed<nsContentList> NS_GetContentList(nsINode* aRootNode,
95 int32_t aMatchNameSpaceId,
96 const nsAString& aTagname);
98 #define ELEMENT_FLAG_BIT(n_) \
99 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
101 // Element-specific flags
102 enum {
103 // Whether this node has dirty descendants for Servo's style system.
104 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(0),
105 // Whether this node has dirty descendants for animation-only restyle for
106 // Servo's style system.
107 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO = ELEMENT_FLAG_BIT(1),
109 // Whether the element has been snapshotted due to attribute or state changes
110 // by the Servo restyle manager.
111 ELEMENT_HAS_SNAPSHOT = ELEMENT_FLAG_BIT(2),
113 // Whether the element has already handled its relevant snapshot.
115 // Used by the servo restyle process in order to accurately track whether the
116 // style of an element is up-to-date, even during the same restyle process.
117 ELEMENT_HANDLED_SNAPSHOT = ELEMENT_FLAG_BIT(3),
119 // Remaining bits are for subclasses
120 ELEMENT_TYPE_SPECIFIC_BITS_OFFSET = NODE_TYPE_SPECIFIC_BITS_OFFSET + 4
123 #undef ELEMENT_FLAG_BIT
125 // Make sure we have space for our bits
126 ASSERT_NODE_FLAGS_SPACE(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET);
128 namespace mozilla {
129 enum class PseudoStyleType : uint8_t;
130 class EventChainPostVisitor;
131 class EventChainPreVisitor;
132 class EventChainVisitor;
133 class EventListenerManager;
134 class EventStateManager;
136 namespace dom {
138 struct CustomElementDefinition;
139 class Animation;
140 class CustomElementRegistry;
141 class Link;
142 class DOMRect;
143 class DOMRectList;
144 class Flex;
145 class Grid;
147 // IID for the dom::Element interface
148 #define NS_ELEMENT_IID \
150 0xc67ed254, 0xfd3b, 0x4b10, { \
151 0x96, 0xa2, 0xc5, 0x8b, 0x7b, 0x64, 0x97, 0xd1 \
155 #define REFLECT_DOMSTRING_ATTR(method, attr) \
156 void Get##method(nsAString& aValue) const { \
157 GetAttr(nsGkAtoms::attr, aValue); \
159 void Set##method(const nsAString& aValue, ErrorResult& aRv) { \
160 SetAttr(nsGkAtoms::attr, aValue, aRv); \
163 class Element : public FragmentOrElement {
164 public:
165 #ifdef MOZILLA_INTERNAL_API
166 explicit Element(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
167 : FragmentOrElement(std::move(aNodeInfo)),
168 mState(NS_EVENT_STATE_READONLY | NS_EVENT_STATE_DEFINED) {
169 MOZ_ASSERT(mNodeInfo->NodeType() == ELEMENT_NODE,
170 "Bad NodeType in aNodeInfo");
171 SetIsElement();
174 ~Element() {
175 NS_ASSERTION(!HasServoData(), "expected ServoData to be cleared earlier");
178 #endif // MOZILLA_INTERNAL_API
180 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ELEMENT_IID)
182 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
184 NS_IMPL_FROMNODE_HELPER(Element, IsElement())
186 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
189 * Method to get the full state of this element. See mozilla/EventStates.h
190 * for the possible bits that could be set here.
192 EventStates State() const {
193 // mState is maintained by having whoever might have changed it
194 // call UpdateState() or one of the other mState mutators.
195 return mState;
199 * Ask this element to update its state. If aNotify is false, then
200 * state change notifications will not be dispatched; in that
201 * situation it is the caller's responsibility to dispatch them.
203 * In general, aNotify should only be false if we're guaranteed that
204 * the element can't have a frame no matter what its style is
205 * (e.g. if we're in the middle of adding it to the document or
206 * removing it from the document).
208 void UpdateState(bool aNotify);
211 * Method to update mState with link state information. This does not notify.
213 void UpdateLinkState(EventStates aState);
216 * Returns the current disabled state of the element.
218 bool IsDisabled() const { return State().HasState(NS_EVENT_STATE_DISABLED); }
220 virtual int32_t TabIndexDefault() { return -1; }
223 * Get tabIndex of this element. If not found, return TabIndexDefault.
225 int32_t TabIndex();
228 * Get the parsed value of tabindex attribute.
230 Maybe<int32_t> GetTabIndexAttrValue();
233 * Set tabIndex value to this element.
235 void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
238 * Sets the ShadowRoot binding for this element. The contents of the
239 * binding is rendered in place of this node's children.
241 * @param aShadowRoot The ShadowRoot to be bound to this element.
243 void SetShadowRoot(ShadowRoot* aShadowRoot);
246 * Make focus on this element.
248 virtual void Focus(const FocusOptions& aOptions, const CallerType aCallerType,
249 ErrorResult& aError);
252 * Show blur and clear focus.
254 virtual void Blur(mozilla::ErrorResult& aError);
257 * The style state of this element. This is the real state of the element
258 * with any style locks applied for pseudo-class inspecting.
260 EventStates StyleState() const {
261 if (!HasLockedStyleStates()) {
262 return mState;
264 return StyleStateFromLocks();
268 * StyleStateLocks is used to specify which event states should be locked,
269 * and whether they should be locked to on or off.
271 struct StyleStateLocks {
272 // mLocks tracks which event states should be locked.
273 EventStates mLocks;
274 // mValues tracks if the locked state should be on or off.
275 EventStates mValues;
279 * The style state locks applied to this element.
281 StyleStateLocks LockedStyleStates() const;
284 * Add a style state lock on this element.
285 * aEnabled is the value to lock the given state bits to.
287 void LockStyleStates(EventStates aStates, bool aEnabled);
290 * Remove a style state lock on this element.
292 void UnlockStyleStates(EventStates aStates);
295 * Clear all style state locks on this element.
297 void ClearStyleStateLocks();
300 * Accessors for the state of our dir attribute.
302 bool HasDirAuto() const {
303 return State().HasState(NS_EVENT_STATE_DIR_ATTR_LIKE_AUTO);
307 * Elements with dir="rtl" or dir="ltr".
309 bool HasFixedDir() const {
310 return State().HasAtLeastOneOfStates(NS_EVENT_STATE_DIR_ATTR_LTR |
311 NS_EVENT_STATE_DIR_ATTR_RTL);
315 * Get the inline style declaration, if any, for this element.
317 DeclarationBlock* GetInlineStyleDeclaration() const;
320 * Get the mapped attributes, if any, for this element.
322 const nsMappedAttributes* GetMappedAttributes() const;
324 void ClearMappedServoStyle() { mAttrs.ClearMappedServoStyle(); }
327 * InlineStyleDeclarationWillChange is called before SetInlineStyleDeclaration
328 * so that the element implementation can access the old style attribute
329 * value.
331 virtual void InlineStyleDeclarationWillChange(MutationClosureData& aData);
334 * Set the inline style declaration for this element.
336 virtual nsresult SetInlineStyleDeclaration(DeclarationBlock& aDeclaration,
337 MutationClosureData& aData);
340 * Get the SMIL override style declaration for this element. If the
341 * rule hasn't been created, this method simply returns null.
343 DeclarationBlock* GetSMILOverrideStyleDeclaration();
346 * Set the SMIL override style declaration for this element. This method will
347 * notify the document's pres context, so that the style changes will be
348 * noticed.
350 void SetSMILOverrideStyleDeclaration(DeclarationBlock&);
353 * Returns a new SMILAttr that allows the caller to animate the given
354 * attribute on this element.
356 virtual UniquePtr<SMILAttr> GetAnimatedAttr(int32_t aNamespaceID,
357 nsAtom* aName) {
358 return nullptr;
362 * Get the SMIL override style for this element. This is a style declaration
363 * that is applied *after* the inline style, and it can be used e.g. to store
364 * animated style values.
366 * Note: This method is analogous to the 'GetStyle' method in
367 * nsGenericHTMLElement and nsStyledElement.
369 nsDOMCSSAttributeDeclaration* SMILOverrideStyle();
372 * Returns if the element is labelable as per HTML specification.
374 virtual bool IsLabelable() const;
377 * Returns if the element is interactive content as per HTML specification.
379 virtual bool IsInteractiveHTMLContent() const;
382 * Returns |this| as an nsIMozBrowserFrame* if the element is a frame or
383 * iframe element.
385 * We have this method, rather than using QI, so that we can use it during
386 * the servo traversal, where we can't QI DOM nodes because of non-thread-safe
387 * refcounts.
389 virtual nsIMozBrowserFrame* GetAsMozBrowserFrame() { return nullptr; }
392 * Is the attribute named stored in the mapped attributes?
394 * // XXXbz we use this method in HasAttributeDependentStyle, so svg
395 * returns true here even though it stores nothing in the mapped
396 * attributes.
398 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const;
401 * Get a hint that tells the style system what to do when
402 * an attribute on this node changes, if something needs to happen
403 * in response to the change *other* than the result of what is
404 * mapped into style data via any type of style rule.
406 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
407 int32_t aModType) const;
409 inline Directionality GetDirectionality() const {
410 if (HasFlag(NODE_HAS_DIRECTION_RTL)) {
411 return eDir_RTL;
414 if (HasFlag(NODE_HAS_DIRECTION_LTR)) {
415 return eDir_LTR;
418 return eDir_NotSet;
421 inline void SetDirectionality(Directionality aDir, bool aNotify) {
422 UnsetFlags(NODE_ALL_DIRECTION_FLAGS);
423 if (!aNotify) {
424 RemoveStatesSilently(DIRECTION_STATES);
427 switch (aDir) {
428 case (eDir_RTL):
429 SetFlags(NODE_HAS_DIRECTION_RTL);
430 if (!aNotify) {
431 AddStatesSilently(NS_EVENT_STATE_RTL);
433 break;
435 case (eDir_LTR):
436 SetFlags(NODE_HAS_DIRECTION_LTR);
437 if (!aNotify) {
438 AddStatesSilently(NS_EVENT_STATE_LTR);
440 break;
442 default:
443 break;
447 * Only call UpdateState if we need to notify, because we call
448 * SetDirectionality for every element, and UpdateState is very very slow
449 * for some elements.
451 if (aNotify) {
452 UpdateState(true);
456 Directionality GetComputedDirectionality() const;
458 static const uint32_t kAllServoDescendantBits =
459 ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO |
460 ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO |
461 NODE_DESCENDANTS_NEED_FRAMES;
464 * Notes that something in the given subtree of this element needs dirtying,
465 * and that all the relevant dirty bits have already been propagated up to the
466 * element.
468 * This is important because `NoteDirtyForServo` uses the dirty bits to reason
469 * about the shape of the tree, so we can't just call into there.
471 void NoteDirtySubtreeForServo();
473 void NoteDirtyForServo();
474 void NoteAnimationOnlyDirtyForServo();
475 void NoteDescendantsNeedFramesForServo();
477 bool HasDirtyDescendantsForServo() const {
478 return HasFlag(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
481 void SetHasDirtyDescendantsForServo() {
482 SetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
485 void UnsetHasDirtyDescendantsForServo() {
486 UnsetFlags(ELEMENT_HAS_DIRTY_DESCENDANTS_FOR_SERVO);
489 bool HasAnimationOnlyDirtyDescendantsForServo() const {
490 return HasFlag(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
493 void SetHasAnimationOnlyDirtyDescendantsForServo() {
494 SetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
497 void UnsetHasAnimationOnlyDirtyDescendantsForServo() {
498 UnsetFlags(ELEMENT_HAS_ANIMATION_ONLY_DIRTY_DESCENDANTS_FOR_SERVO);
501 bool HasServoData() const { return !!mServoData.Get(); }
503 void ClearServoData() { ClearServoData(GetComposedDoc()); }
504 void ClearServoData(Document* aDocument);
507 * Gets the custom element data used by web components custom element.
508 * Custom element data is created at the first attempt to enqueue a callback.
510 * @return The custom element data or null if none.
512 inline CustomElementData* GetCustomElementData() const {
513 if (!HasCustomElementData()) {
514 return nullptr;
517 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
518 return slots ? slots->mCustomElementData.get() : nullptr;
522 * Sets the custom element data, ownership of the
523 * callback data is taken by this element.
525 * @param aData The custom element data.
527 void SetCustomElementData(CustomElementData* aData);
530 * Gets the custom element definition used by web components custom element.
532 * @return The custom element definition or null if element is not a custom
533 * element or custom element is not defined yet.
535 CustomElementDefinition* GetCustomElementDefinition() const;
538 * Sets the custom element definition, called when custom element is created
539 * or upgraded.
541 * @param aDefinition The custom element definition.
543 void SetCustomElementDefinition(CustomElementDefinition* aDefinition);
545 void SetDefined(bool aSet) {
546 if (aSet) {
547 AddStates(NS_EVENT_STATE_DEFINED);
548 } else {
549 RemoveStates(NS_EVENT_STATE_DEFINED);
553 // AccessibilityRole
554 REFLECT_DOMSTRING_ATTR(Role, role)
556 // AriaAttributes
557 REFLECT_DOMSTRING_ATTR(AriaAtomic, aria_atomic)
558 REFLECT_DOMSTRING_ATTR(AriaAutoComplete, aria_autocomplete)
559 REFLECT_DOMSTRING_ATTR(AriaBusy, aria_busy)
560 REFLECT_DOMSTRING_ATTR(AriaChecked, aria_checked)
561 REFLECT_DOMSTRING_ATTR(AriaColCount, aria_colcount)
562 REFLECT_DOMSTRING_ATTR(AriaColIndex, aria_colindex)
563 REFLECT_DOMSTRING_ATTR(AriaColIndexText, aria_colindextext)
564 REFLECT_DOMSTRING_ATTR(AriaColSpan, aria_colspan)
565 REFLECT_DOMSTRING_ATTR(AriaCurrent, aria_current)
566 REFLECT_DOMSTRING_ATTR(AriaDescription, aria_description)
567 REFLECT_DOMSTRING_ATTR(AriaDisabled, aria_disabled)
568 REFLECT_DOMSTRING_ATTR(AriaExpanded, aria_expanded)
569 REFLECT_DOMSTRING_ATTR(AriaHasPopup, aria_haspopup)
570 REFLECT_DOMSTRING_ATTR(AriaHidden, aria_hidden)
571 REFLECT_DOMSTRING_ATTR(AriaInvalid, aria_invalid)
572 REFLECT_DOMSTRING_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
573 REFLECT_DOMSTRING_ATTR(AriaLabel, aria_label)
574 REFLECT_DOMSTRING_ATTR(AriaLevel, aria_level)
575 REFLECT_DOMSTRING_ATTR(AriaLive, aria_live)
576 REFLECT_DOMSTRING_ATTR(AriaModal, aria_modal)
577 REFLECT_DOMSTRING_ATTR(AriaMultiLine, aria_multiline)
578 REFLECT_DOMSTRING_ATTR(AriaMultiSelectable, aria_multiselectable)
579 REFLECT_DOMSTRING_ATTR(AriaOrientation, aria_orientation)
580 REFLECT_DOMSTRING_ATTR(AriaPlaceholder, aria_placeholder)
581 REFLECT_DOMSTRING_ATTR(AriaPosInSet, aria_posinset)
582 REFLECT_DOMSTRING_ATTR(AriaPressed, aria_pressed)
583 REFLECT_DOMSTRING_ATTR(AriaReadOnly, aria_readonly)
584 REFLECT_DOMSTRING_ATTR(AriaRelevant, aria_relevant)
585 REFLECT_DOMSTRING_ATTR(AriaRequired, aria_required)
586 REFLECT_DOMSTRING_ATTR(AriaRoleDescription, aria_roledescription)
587 REFLECT_DOMSTRING_ATTR(AriaRowCount, aria_rowcount)
588 REFLECT_DOMSTRING_ATTR(AriaRowIndex, aria_rowindex)
589 REFLECT_DOMSTRING_ATTR(AriaRowIndexText, aria_rowindextext)
590 REFLECT_DOMSTRING_ATTR(AriaRowSpan, aria_rowspan)
591 REFLECT_DOMSTRING_ATTR(AriaSelected, aria_selected)
592 REFLECT_DOMSTRING_ATTR(AriaSetSize, aria_setsize)
593 REFLECT_DOMSTRING_ATTR(AriaSort, aria_sort)
594 REFLECT_DOMSTRING_ATTR(AriaValueMax, aria_valuemax)
595 REFLECT_DOMSTRING_ATTR(AriaValueMin, aria_valuemin)
596 REFLECT_DOMSTRING_ATTR(AriaValueNow, aria_valuenow)
597 REFLECT_DOMSTRING_ATTR(AriaValueText, aria_valuetext)
599 protected:
601 * Method to get the _intrinsic_ content state of this element. This is the
602 * state that is independent of the element's presentation. To get the full
603 * content state, use State(). See mozilla/EventStates.h for
604 * the possible bits that could be set here.
606 virtual EventStates IntrinsicState() const;
609 * Method to add state bits. This should be called from subclass
610 * constructors to set up our event state correctly at construction
611 * time and other places where we don't want to notify a state
612 * change.
614 void AddStatesSilently(EventStates aStates) { mState |= aStates; }
617 * Method to remove state bits. This should be called from subclass
618 * constructors to set up our event state correctly at construction
619 * time and other places where we don't want to notify a state
620 * change.
622 void RemoveStatesSilently(EventStates aStates) { mState &= ~aStates; }
624 already_AddRefed<ShadowRoot> AttachShadowInternal(ShadowRootMode,
625 ErrorResult& aError);
627 public:
628 MOZ_CAN_RUN_SCRIPT
629 nsIScrollableFrame* GetScrollFrame(nsIFrame** aStyledFrame = nullptr,
630 FlushType aFlushType = FlushType::Layout);
632 private:
633 // Need to allow the ESM, nsGlobalWindow, and the focus manager
634 // and Document to set our state
635 friend class mozilla::EventStateManager;
636 friend class mozilla::dom::Document;
637 friend class ::nsGlobalWindowInner;
638 friend class ::nsGlobalWindowOuter;
639 friend class ::nsFocusManager;
641 // Allow CusomtElementRegistry to call AddStates.
642 friend class CustomElementRegistry;
644 // Also need to allow Link to call UpdateLinkState.
645 friend class Link;
647 void NotifyStateChange(EventStates aStates);
649 void NotifyStyleStateChange(EventStates aStates);
651 // Style state computed from element's state and style locks.
652 EventStates StyleStateFromLocks() const;
654 protected:
655 // Methods for the ESM, nsGlobalWindow, focus manager and Document to
656 // manage state bits.
657 // These will handle setting up script blockers when they notify, so no need
658 // to do it in the callers unless desired. States passed here must only be
659 // those in EXTERNALLY_MANAGED_STATES.
660 virtual void AddStates(EventStates aStates) {
661 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(INTRINSIC_STATES),
662 "Should only be adding externally-managed states here");
663 AddStatesSilently(aStates);
664 NotifyStateChange(aStates);
666 virtual void RemoveStates(EventStates aStates) {
667 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(INTRINSIC_STATES),
668 "Should only be removing externally-managed states here");
669 RemoveStatesSilently(aStates);
670 NotifyStateChange(aStates);
672 virtual void ToggleStates(EventStates aStates, bool aNotify) {
673 MOZ_ASSERT(!aStates.HasAtLeastOneOfStates(INTRINSIC_STATES),
674 "Should only be removing externally-managed states here");
675 mState ^= aStates;
676 if (aNotify) {
677 NotifyStateChange(aStates);
681 public:
682 // Public methods to manage state bits in MANUALLY_MANAGED_STATES.
683 void AddManuallyManagedStates(EventStates aStates) {
684 MOZ_ASSERT(MANUALLY_MANAGED_STATES.HasAllStates(aStates),
685 "Should only be adding manually-managed states here");
686 AddStates(aStates);
688 void RemoveManuallyManagedStates(EventStates aStates) {
689 MOZ_ASSERT(MANUALLY_MANAGED_STATES.HasAllStates(aStates),
690 "Should only be removing manually-managed states here");
691 RemoveStates(aStates);
694 void UpdateEditableState(bool aNotify) override;
696 nsresult BindToTree(BindContext&, nsINode& aParent) override;
698 void UnbindFromTree(bool aNullParent = true) override;
701 * Normalizes an attribute name and returns it as a nodeinfo if an attribute
702 * with that name exists. This method is intended for character case
703 * conversion if the content object is case insensitive (e.g. HTML). Returns
704 * the nodeinfo of the attribute with the specified name if one exists or
705 * null otherwise.
707 * @param aStr the unparsed attribute string
708 * @return the node info. May be nullptr.
710 already_AddRefed<mozilla::dom::NodeInfo> GetExistingAttrNameFromQName(
711 const nsAString& aStr) const;
714 * Helper for SetAttr/SetParsedAttr. This method will return true if aNotify
715 * is true or there are mutation listeners that must be triggered, the
716 * attribute is currently set, and the new value that is about to be set is
717 * different to the current value. As a perf optimization the new and old
718 * values will not actually be compared if we aren't notifying and we don't
719 * have mutation listeners (in which case it's cheap to just return false
720 * and let the caller go ahead and set the value).
721 * @param aOldValue [out] Set to the old value of the attribute, but only if
722 * there are event listeners. If set, the type of aOldValue will be either
723 * nsAttrValue::eString or nsAttrValue::eAtom.
724 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
725 * MutationEvent_Binding::ADDITION, but only if this helper returns true
726 * @param aHasListeners [out] Set to true if there are mutation event
727 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
728 * @param aOldValueSet [out] Indicates whether an old attribute value has been
729 * stored in aOldValue. The bool will be set to true if a value was stored.
731 bool MaybeCheckSameAttrVal(int32_t aNamespaceID, const nsAtom* aName,
732 const nsAtom* aPrefix,
733 const nsAttrValueOrString& aValue, bool aNotify,
734 nsAttrValue& aOldValue, uint8_t* aModType,
735 bool* aHasListeners, bool* aOldValueSet);
738 * Notifies mutation listeners if aNotify is true, there are mutation
739 * listeners, and the attribute value is changing.
741 * @param aNamespaceID The namespace of the attribute
742 * @param aName The local name of the attribute
743 * @param aPrefix The prefix of the attribute
744 * @param aValue The value that the attribute is being changed to
745 * @param aNotify If true, mutation listeners will be notified if they exist
746 * and the attribute value is changing
747 * @param aOldValue [out] Set to the old value of the attribute, but only if
748 * there are event listeners. If set, the type of aOldValue will be either
749 * nsAttrValue::eString or nsAttrValue::eAtom.
750 * @param aModType [out] Set to MutationEvent_Binding::MODIFICATION or to
751 * MutationEvent_Binding::ADDITION, but only if this helper returns true
752 * @param aHasListeners [out] Set to true if there are mutation event
753 * listeners listening for NS_EVENT_BITS_MUTATION_ATTRMODIFIED
754 * @param aOldValueSet [out] Indicates whether an old attribute value has been
755 * stored in aOldValue. The bool will be set to true if a value was stored.
757 bool OnlyNotifySameValueSet(int32_t aNamespaceID, nsAtom* aName,
758 nsAtom* aPrefix,
759 const nsAttrValueOrString& aValue, bool aNotify,
760 nsAttrValue& aOldValue, uint8_t* aModType,
761 bool* aHasListeners, bool* aOldValueSet);
764 * Sets the class attribute to a value that contains no whitespace.
765 * Assumes that we are not notifying and that the attribute hasn't been
766 * set previously.
768 nsresult SetSingleClassFromParser(nsAtom* aSingleClassName);
770 // aParsedValue receives the old value of the attribute. That's useful if
771 // either the input or output value of aParsedValue is StoresOwnData.
772 nsresult SetParsedAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
773 nsAttrValue& aParsedValue, bool aNotify);
775 * Get the current value of the attribute. This returns a form that is
776 * suitable for passing back into SetAttr.
778 * @param aNameSpaceID the namespace of the attr (defaults to
779 kNameSpaceID_None in the overload that omits this arg)
780 * @param aName the name of the attr
781 * @param aResult the value (may legitimately be the empty string) [OUT]
782 * @returns true if the attribute was set (even when set to empty string)
783 * false when not set.
784 * GetAttr is not inlined on purpose, to keep down codesize from all the
785 * inlined nsAttrValue bits for C++ callers.
787 bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
788 nsAString& aResult) const;
790 bool GetAttr(const nsAtom* aName, nsAString& aResult) const {
791 return GetAttr(kNameSpaceID_None, aName, aResult);
795 * Determine if an attribute has been set (empty string or otherwise).
797 * @param aNameSpaceId the namespace id of the attribute (defaults to
798 kNameSpaceID_None in the overload that omits this arg)
799 * @param aAttr the attribute name
800 * @return whether an attribute exists
802 inline bool HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
804 bool HasAttr(const nsAtom* aAttr) const {
805 return HasAttr(kNameSpaceID_None, aAttr);
809 * Determine if an attribute has been set to a non-empty string value. If the
810 * attribute is not set at all, this will return false.
812 * @param aNameSpaceId the namespace id of the attribute (defaults to
813 * kNameSpaceID_None in the overload that omits this arg)
814 * @param aAttr the attribute name
816 inline bool HasNonEmptyAttr(int32_t aNameSpaceID, const nsAtom* aName) const;
818 bool HasNonEmptyAttr(const nsAtom* aAttr) const {
819 return HasNonEmptyAttr(kNameSpaceID_None, aAttr);
823 * Test whether this Element's given attribute has the given value. If the
824 * attribute is not set at all, this will return false.
826 * @param aNameSpaceID The namespace ID of the attribute. Must not
827 * be kNameSpaceID_Unknown.
828 * @param aName The name atom of the attribute. Must not be null.
829 * @param aValue The value to compare to.
830 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
832 inline bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
833 const nsAString& aValue,
834 nsCaseTreatment aCaseSensitive) const;
837 * Test whether this Element's given attribute has the given value. If the
838 * attribute is not set at all, this will return false.
840 * @param aNameSpaceID The namespace ID of the attribute. Must not
841 * be kNameSpaceID_Unknown.
842 * @param aName The name atom of the attribute. Must not be null.
843 * @param aValue The value to compare to. Must not be null.
844 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
846 bool AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
847 const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const;
849 enum { ATTR_MISSING = -1, ATTR_VALUE_NO_MATCH = -2 };
851 * Check whether this Element's given attribute has one of a given list of
852 * values. If there is a match, we return the index in the list of the first
853 * matching value. If there was no attribute at all, then we return
854 * ATTR_MISSING. If there was an attribute but it didn't match, we return
855 * ATTR_VALUE_NO_MATCH. A non-negative result always indicates a match.
857 * @param aNameSpaceID The namespace ID of the attribute. Must not
858 * be kNameSpaceID_Unknown.
859 * @param aName The name atom of the attribute. Must not be null.
860 * @param aValues a nullptr-terminated array of pointers to atom values to
861 * test against.
862 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
863 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
864 * indicating the first value of aValues that matched
866 typedef nsStaticAtom* const AttrValuesArray;
867 int32_t FindAttrValueIn(int32_t aNameSpaceID, const nsAtom* aName,
868 AttrValuesArray* aValues,
869 nsCaseTreatment aCaseSensitive) const;
872 * Set attribute values. All attribute values are assumed to have a
873 * canonical string representation that can be used for these
874 * methods. The SetAttr method is assumed to perform a translation
875 * of the canonical form into the underlying content specific
876 * form.
878 * @param aNameSpaceID the namespace of the attribute
879 * @param aName the name of the attribute
880 * @param aValue the value to set
881 * @param aNotify specifies how whether or not the document should be
882 * notified of the attribute change.
884 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
885 bool aNotify) {
886 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
888 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
889 const nsAString& aValue, bool aNotify) {
890 return SetAttr(aNameSpaceID, aName, aPrefix, aValue, nullptr, aNotify);
892 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAString& aValue,
893 nsIPrincipal* aTriggeringPrincipal, bool aNotify) {
894 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aTriggeringPrincipal,
895 aNotify);
899 * Set attribute values. All attribute values are assumed to have a
900 * canonical String representation that can be used for these
901 * methods. The SetAttr method is assumed to perform a translation
902 * of the canonical form into the underlying content specific
903 * form.
905 * @param aNameSpaceID the namespace of the attribute
906 * @param aName the name of the attribute
907 * @param aPrefix the prefix of the attribute
908 * @param aValue the value to set
909 * @param aMaybeScriptedPrincipal the principal of the scripted caller
910 * responsible for setting the attribute, or null if no scripted caller can be
911 * determined. A null value here does not guarantee that there is no
912 * scripted caller, but a non-null value does guarantee that a scripted
913 * caller with the given principal is directly responsible for the
914 * attribute change.
915 * @param aNotify specifies how whether or not the document should be
916 * notified of the attribute change.
918 nsresult SetAttr(int32_t aNameSpaceID, nsAtom* aName, nsAtom* aPrefix,
919 const nsAString& aValue,
920 nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify);
923 * Remove an attribute so that it is no longer explicitly specified.
925 * @param aNameSpaceID the namespace id of the attribute
926 * @param aAttr the name of the attribute to unset
927 * @param aNotify specifies whether or not the document should be
928 * notified of the attribute change
930 nsresult UnsetAttr(int32_t aNameSpaceID, nsAtom* aAttribute, bool aNotify);
933 * Get the namespace / name / prefix of a given attribute.
935 * @param aIndex the index of the attribute name
936 * @returns The name at the given index, or null if the index is
937 * out-of-bounds.
938 * @note The document returned by NodeInfo()->GetDocument() (if one is
939 * present) is *not* necessarily the owner document of the element.
940 * @note The pointer returned by this function is only valid until the
941 * next call of either GetAttrNameAt or SetAttr on the element.
943 const nsAttrName* GetAttrNameAt(uint32_t aIndex) const {
944 return mAttrs.GetSafeAttrNameAt(aIndex);
948 * Same as above, but does not do out-of-bounds checks!
950 const nsAttrName* GetUnsafeAttrNameAt(uint32_t aIndex) const {
951 return mAttrs.AttrNameAt(aIndex);
955 * Gets the attribute info (name and value) for this element at a given index.
957 BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const {
958 if (aIndex >= mAttrs.AttrCount()) {
959 return BorrowedAttrInfo(nullptr, nullptr);
962 return mAttrs.AttrInfoAt(aIndex);
966 * Get the number of all specified attributes.
968 * @return the number of attributes
970 uint32_t GetAttrCount() const { return mAttrs.AttrCount(); }
972 virtual bool IsNodeOfType(uint32_t aFlags) const override;
975 * Get the class list of this element (this corresponds to the value of the
976 * class attribute). This may be null if there are no classes, but that's not
977 * guaranteed (e.g. we could have class="").
979 const nsAttrValue* GetClasses() const {
980 if (!MayHaveClass()) {
981 return nullptr;
984 if (IsSVGElement()) {
985 if (const nsAttrValue* value = GetSVGAnimatedClass()) {
986 return value;
990 return GetParsedAttr(nsGkAtoms::_class);
993 #ifdef DEBUG
994 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override {
995 List(out, aIndent, ""_ns);
997 virtual void DumpContent(FILE* out, int32_t aIndent,
998 bool aDumpAll) const override;
999 void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
1000 void ListAttributes(FILE* out) const;
1001 #endif
1004 * Append to aOutDescription a short (preferably one line) string
1005 * describing the element.
1007 void Describe(nsAString& aOutDescription) const;
1010 * Attribute Mapping Helpers
1012 struct MappedAttributeEntry {
1013 const nsStaticAtom* const attribute;
1017 * A common method where you can just pass in a list of maps to check
1018 * for attribute dependence. Most implementations of
1019 * IsAttributeMapped should use this function as a default
1020 * handler.
1022 template <size_t N>
1023 static bool FindAttributeDependence(
1024 const nsAtom* aAttribute, const MappedAttributeEntry* const (&aMaps)[N]) {
1025 return FindAttributeDependence(aAttribute, aMaps, N);
1028 static nsStaticAtom* const* HTMLSVGPropertiesToTraverseAndUnlink();
1030 private:
1031 void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
1033 static bool FindAttributeDependence(const nsAtom* aAttribute,
1034 const MappedAttributeEntry* const aMaps[],
1035 uint32_t aMapCount);
1037 protected:
1038 inline bool GetAttr(int32_t aNameSpaceID, const nsAtom* aName,
1039 DOMString& aResult) const {
1040 NS_ASSERTION(nullptr != aName, "must have attribute name");
1041 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
1042 "must have a real namespace ID!");
1043 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1044 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
1045 if (val) {
1046 val->ToString(aResult);
1047 return true;
1049 // else DOMString comes pre-emptied.
1050 return false;
1053 public:
1054 bool HasAttrs() const { return mAttrs.HasAttrs(); }
1056 inline bool GetAttr(const nsAString& aName, DOMString& aResult) const {
1057 MOZ_ASSERT(aResult.IsEmpty(), "Should have empty string coming in");
1058 const nsAttrValue* val = mAttrs.GetAttr(aName);
1059 if (val) {
1060 val->ToString(aResult);
1061 return true;
1063 // else DOMString comes pre-emptied.
1064 return false;
1067 void GetTagName(nsAString& aTagName) const { aTagName = NodeName(); }
1068 void GetId(nsAString& aId) const {
1069 GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
1071 void GetId(DOMString& aId) const {
1072 GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
1074 void SetId(const nsAString& aId) {
1075 SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);
1077 void GetClassName(nsAString& aClassName) {
1078 GetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName);
1080 void GetClassName(DOMString& aClassName) {
1081 GetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName);
1083 void SetClassName(const nsAString& aClassName) {
1084 SetAttr(kNameSpaceID_None, nsGkAtoms::_class, aClassName, true);
1087 nsDOMTokenList* ClassList();
1088 nsDOMTokenList* Part();
1090 nsDOMAttributeMap* Attributes() {
1091 nsDOMSlots* slots = DOMSlots();
1092 if (!slots->mAttributeMap) {
1093 slots->mAttributeMap = new nsDOMAttributeMap(this);
1096 return slots->mAttributeMap;
1099 void GetAttributeNames(nsTArray<nsString>& aResult);
1101 void GetAttribute(const nsAString& aName, nsAString& aReturn) {
1102 DOMString str;
1103 GetAttribute(aName, str);
1104 str.ToString(aReturn);
1107 void GetAttribute(const nsAString& aName, DOMString& aReturn);
1108 void GetAttributeNS(const nsAString& aNamespaceURI,
1109 const nsAString& aLocalName, nsAString& aReturn);
1110 bool ToggleAttribute(const nsAString& aName, const Optional<bool>& aForce,
1111 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1112 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1113 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1114 void SetAttributeNS(const nsAString& aNamespaceURI,
1115 const nsAString& aLocalName, const nsAString& aValue,
1116 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError);
1117 void SetAttribute(const nsAString& aName, const nsAString& aValue,
1118 ErrorResult& aError) {
1119 SetAttribute(aName, aValue, nullptr, aError);
1122 * This method creates a principal that subsumes this element's NodePrincipal
1123 * and which has flags set for elevated permissions that devtools needs to
1124 * operate on this element. The principal returned by this method is used by
1125 * various devtools methods to permit otherwise blocked operations, without
1126 * changing any other restrictions the NodePrincipal might have.
1128 already_AddRefed<nsIPrincipal> CreateDevtoolsPrincipal();
1129 void SetAttributeDevtools(const nsAString& aName, const nsAString& aValue,
1130 ErrorResult& aError);
1131 void SetAttributeDevtoolsNS(const nsAString& aNamespaceURI,
1132 const nsAString& aLocalName,
1133 const nsAString& aValue, ErrorResult& aError);
1135 void RemoveAttribute(const nsAString& aName, ErrorResult& aError);
1136 void RemoveAttributeNS(const nsAString& aNamespaceURI,
1137 const nsAString& aLocalName, ErrorResult& aError);
1138 bool HasAttribute(const nsAString& aName) const {
1139 return InternalGetAttrNameFromQName(aName) != nullptr;
1141 bool HasAttributeNS(const nsAString& aNamespaceURI,
1142 const nsAString& aLocalName) const;
1143 bool HasAttributes() const { return HasAttrs(); }
1144 Element* Closest(const nsAString& aSelector, ErrorResult& aResult);
1145 bool Matches(const nsAString& aSelector, ErrorResult& aError);
1146 already_AddRefed<nsIHTMLCollection> GetElementsByTagName(
1147 const nsAString& aQualifiedName);
1148 already_AddRefed<nsIHTMLCollection> GetElementsByTagNameNS(
1149 const nsAString& aNamespaceURI, const nsAString& aLocalName,
1150 ErrorResult& aError);
1151 already_AddRefed<nsIHTMLCollection> GetElementsByClassName(
1152 const nsAString& aClassNames);
1154 PseudoStyleType GetPseudoElementType() const {
1155 nsresult rv = NS_OK;
1156 auto raw = GetProperty(nsGkAtoms::pseudoProperty, &rv);
1157 if (rv == NS_PROPTABLE_PROP_NOT_THERE) {
1158 return PseudoStyleType::NotPseudo;
1160 return PseudoStyleType(reinterpret_cast<uintptr_t>(raw));
1163 void SetPseudoElementType(PseudoStyleType aPseudo) {
1164 static_assert(sizeof(PseudoStyleType) <= sizeof(uintptr_t),
1165 "Need to be able to store this in a void*");
1166 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aPseudo));
1167 SetProperty(nsGkAtoms::pseudoProperty, reinterpret_cast<void*>(aPseudo));
1171 * Return an array of all elements in the subtree rooted at this
1172 * element that have grid container frames. This does not include
1173 * pseudo-elements.
1175 void GetElementsWithGrid(nsTArray<RefPtr<Element>>& aElements);
1178 * Provide a direct way to determine if this Element has visible
1179 * scrollbars. Flushes layout.
1181 MOZ_CAN_RUN_SCRIPT bool HasVisibleScrollbars();
1183 private:
1185 * Implement the algorithm specified at
1186 * https://dom.spec.whatwg.org/#insert-adjacent for both
1187 * |insertAdjacentElement()| and |insertAdjacentText()| APIs.
1189 nsINode* InsertAdjacent(const nsAString& aWhere, nsINode* aNode,
1190 ErrorResult& aError);
1192 public:
1193 Element* InsertAdjacentElement(const nsAString& aWhere, Element& aElement,
1194 ErrorResult& aError);
1196 void InsertAdjacentText(const nsAString& aWhere, const nsAString& aData,
1197 ErrorResult& aError);
1199 void SetPointerCapture(int32_t aPointerId, ErrorResult& aError) {
1200 bool activeState = false;
1201 if (nsContentUtils::ShouldResistFingerprinting(GetComposedDoc()) &&
1202 aPointerId != PointerEventHandler::GetSpoofedPointerIdForRFP()) {
1203 aError.ThrowNotFoundError("Invalid pointer id");
1204 return;
1206 if (!PointerEventHandler::GetPointerInfo(aPointerId, activeState)) {
1207 aError.ThrowNotFoundError("Invalid pointer id");
1208 return;
1210 if (!IsInComposedDoc()) {
1211 aError.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
1212 return;
1214 if (OwnerDoc()->GetPointerLockElement()) {
1215 // Throw an exception 'InvalidStateError' while the page has a locked
1216 // element.
1217 aError.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
1218 return;
1220 if (!activeState) {
1221 return;
1223 PointerEventHandler::SetPointerCaptureById(aPointerId, this);
1225 void ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError) {
1226 bool activeState = false;
1227 if (nsContentUtils::ShouldResistFingerprinting(GetComposedDoc()) &&
1228 aPointerId != PointerEventHandler::GetSpoofedPointerIdForRFP()) {
1229 aError.ThrowNotFoundError("Invalid pointer id");
1230 return;
1232 if (!PointerEventHandler::GetPointerInfo(aPointerId, activeState)) {
1233 aError.ThrowNotFoundError("Invalid pointer id");
1234 return;
1236 if (HasPointerCapture(aPointerId)) {
1237 PointerEventHandler::ReleasePointerCaptureById(aPointerId);
1240 bool HasPointerCapture(long aPointerId) {
1241 PointerCaptureInfo* pointerCaptureInfo =
1242 PointerEventHandler::GetPointerCaptureInfo(aPointerId);
1243 if (pointerCaptureInfo && pointerCaptureInfo->mPendingContent == this) {
1244 return true;
1246 return false;
1248 void SetCapture(bool aRetargetToElement);
1250 void SetCaptureAlways(bool aRetargetToElement);
1252 void ReleaseCapture();
1254 already_AddRefed<Promise> RequestFullscreen(CallerType, ErrorResult&);
1255 void RequestPointerLock(CallerType aCallerType);
1256 Attr* GetAttributeNode(const nsAString& aName);
1257 already_AddRefed<Attr> SetAttributeNode(Attr& aNewAttr, ErrorResult& aError);
1258 already_AddRefed<Attr> RemoveAttributeNode(Attr& aOldAttr,
1259 ErrorResult& aError);
1260 Attr* GetAttributeNodeNS(const nsAString& aNamespaceURI,
1261 const nsAString& aLocalName);
1262 already_AddRefed<Attr> SetAttributeNodeNS(Attr& aNewAttr,
1263 ErrorResult& aError);
1265 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRectList> GetClientRects();
1266 MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMRect> GetBoundingClientRect();
1268 // Shadow DOM v1
1269 already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
1270 ErrorResult& aError);
1271 bool CanAttachShadowDOM() const;
1273 already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(
1274 ShadowRootMode aMode);
1276 // Attach UA Shadow Root if it is not attached.
1277 enum class NotifyUAWidgetSetup : bool { No, Yes };
1278 void AttachAndSetUAShadowRoot(NotifyUAWidgetSetup = NotifyUAWidgetSetup::Yes);
1280 // Dispatch an event to UAWidgetsChild, triggering construction
1281 // or onchange callback on the existing widget.
1282 void NotifyUAWidgetSetupOrChange();
1284 enum class UnattachShadowRoot {
1286 Yes,
1289 // Dispatch an event to UAWidgetsChild, triggering UA Widget destruction.
1290 // and optionally remove the shadow root.
1291 void NotifyUAWidgetTeardown(UnattachShadowRoot = UnattachShadowRoot::Yes);
1293 void UnattachShadow();
1295 ShadowRoot* GetShadowRootByMode() const;
1296 void SetSlot(const nsAString& aName, ErrorResult& aError);
1297 void GetSlot(nsAString& aName);
1299 ShadowRoot* GetShadowRoot() const {
1300 const nsExtendedDOMSlots* slots = GetExistingExtendedDOMSlots();
1301 return slots ? slots->mShadowRoot.get() : nullptr;
1304 private:
1305 MOZ_CAN_RUN_SCRIPT void ScrollIntoView(const ScrollIntoViewOptions& aOptions);
1307 public:
1308 MOZ_CAN_RUN_SCRIPT
1309 void ScrollIntoView(const BooleanOrScrollIntoViewOptions& aObject);
1310 MOZ_CAN_RUN_SCRIPT void Scroll(double aXScroll, double aYScroll);
1311 MOZ_CAN_RUN_SCRIPT void Scroll(const ScrollToOptions& aOptions);
1312 MOZ_CAN_RUN_SCRIPT void ScrollTo(double aXScroll, double aYScroll);
1313 MOZ_CAN_RUN_SCRIPT void ScrollTo(const ScrollToOptions& aOptions);
1314 MOZ_CAN_RUN_SCRIPT void ScrollBy(double aXScrollDif, double aYScrollDif);
1315 MOZ_CAN_RUN_SCRIPT void ScrollBy(const ScrollToOptions& aOptions);
1316 MOZ_CAN_RUN_SCRIPT int32_t ScrollTop();
1317 MOZ_CAN_RUN_SCRIPT void SetScrollTop(int32_t aScrollTop);
1318 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeft();
1319 MOZ_CAN_RUN_SCRIPT void SetScrollLeft(int32_t aScrollLeft);
1320 MOZ_CAN_RUN_SCRIPT int32_t ScrollWidth();
1321 MOZ_CAN_RUN_SCRIPT int32_t ScrollHeight();
1322 MOZ_CAN_RUN_SCRIPT void MozScrollSnap();
1323 MOZ_CAN_RUN_SCRIPT int32_t ClientTop() {
1324 return CSSPixel::FromAppUnits(GetClientAreaRect().y).Rounded();
1326 MOZ_CAN_RUN_SCRIPT int32_t ClientLeft() {
1327 return CSSPixel::FromAppUnits(GetClientAreaRect().x).Rounded();
1329 MOZ_CAN_RUN_SCRIPT int32_t ClientWidth() {
1330 return CSSPixel::FromAppUnits(GetClientAreaRect().Width()).Rounded();
1332 MOZ_CAN_RUN_SCRIPT int32_t ClientHeight() {
1333 return CSSPixel::FromAppUnits(GetClientAreaRect().Height()).Rounded();
1335 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMin();
1336 MOZ_CAN_RUN_SCRIPT int32_t ScrollTopMax();
1337 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMin();
1338 MOZ_CAN_RUN_SCRIPT int32_t ScrollLeftMax();
1340 MOZ_CAN_RUN_SCRIPT double ClientHeightDouble() {
1341 return CSSPixel::FromAppUnits(GetClientAreaRect().Height());
1344 MOZ_CAN_RUN_SCRIPT double ClientWidthDouble() {
1345 return CSSPixel::FromAppUnits(GetClientAreaRect().Width());
1348 // This function will return the block size of first line box, no matter if
1349 // the box is 'block' or 'inline'. The return unit is pixel. If the element
1350 // can't get a primary frame, we will return be zero.
1351 double FirstLineBoxBSize() const;
1353 already_AddRefed<Flex> GetAsFlexContainer();
1354 void GetGridFragments(nsTArray<RefPtr<Grid>>& aResult);
1356 bool HasGridFragments();
1358 already_AddRefed<DOMMatrixReadOnly> GetTransformToAncestor(
1359 Element& aAncestor);
1360 already_AddRefed<DOMMatrixReadOnly> GetTransformToParent();
1361 already_AddRefed<DOMMatrixReadOnly> GetTransformToViewport();
1363 already_AddRefed<Animation> Animate(
1364 JSContext* aContext, JS::Handle<JSObject*> aKeyframes,
1365 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
1366 ErrorResult& aError);
1368 MOZ_CAN_RUN_SCRIPT
1369 void GetAnimations(const GetAnimationsOptions& aOptions,
1370 nsTArray<RefPtr<Animation>>& aAnimations);
1372 void GetAnimationsWithoutFlush(const GetAnimationsOptions& aOptions,
1373 nsTArray<RefPtr<Animation>>& aAnimations);
1375 static void GetAnimationsUnsorted(Element* aElement,
1376 PseudoStyleType aPseudoType,
1377 nsTArray<RefPtr<Animation>>& aAnimations);
1379 void CloneAnimationsFrom(const Element& aOther);
1381 virtual void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError);
1382 virtual void SetInnerHTML(const nsAString& aInnerHTML,
1383 nsIPrincipal* aSubjectPrincipal,
1384 ErrorResult& aError);
1385 void GetOuterHTML(nsAString& aOuterHTML);
1386 void SetOuterHTML(const nsAString& aOuterHTML, ErrorResult& aError);
1387 void InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
1388 ErrorResult& aError);
1390 //----------------------------------------
1393 * Add a script event listener with the given event handler name
1394 * (like onclick) and with the value as JS
1395 * @param aEventName the event listener name
1396 * @param aValue the JS to attach
1397 * @param aDefer indicates if deferred execution is allowed
1399 void SetEventHandler(nsAtom* aEventName, const nsAString& aValue,
1400 bool aDefer = true);
1403 * Do whatever needs to be done when the mouse leaves a link
1405 nsresult LeaveLink(nsPresContext* aPresContext);
1407 static bool ShouldBlur(nsIContent* aContent);
1410 * Method to create and dispatch a left-click event loosely based on
1411 * aSourceEvent. If aFullDispatch is true, the event will be dispatched
1412 * through the full dispatching of the presshell of the aPresContext; if it's
1413 * false the event will be dispatched only as a DOM event.
1414 * If aPresContext is nullptr, this does nothing.
1416 * @param aFlags Extra flags for the dispatching event. The true flags
1417 * will be respected.
1419 MOZ_CAN_RUN_SCRIPT
1420 static nsresult DispatchClickEvent(nsPresContext* aPresContext,
1421 WidgetInputEvent* aSourceEvent,
1422 nsIContent* aTarget, bool aFullDispatch,
1423 const EventFlags* aFlags,
1424 nsEventStatus* aStatus);
1427 * Method to dispatch aEvent to aTarget. If aFullDispatch is true, the event
1428 * will be dispatched through the full dispatching of the presshell of the
1429 * aPresContext; if it's false the event will be dispatched only as a DOM
1430 * event.
1431 * If aPresContext is nullptr, this does nothing.
1433 using nsIContent::DispatchEvent;
1434 MOZ_CAN_RUN_SCRIPT
1435 static nsresult DispatchEvent(nsPresContext* aPresContext,
1436 WidgetEvent* aEvent, nsIContent* aTarget,
1437 bool aFullDispatch, nsEventStatus* aStatus);
1439 bool IsDisplayContents() const {
1440 return HasServoData() && Servo_Element_IsDisplayContents(this);
1444 * https://html.spec.whatwg.org/#being-rendered
1446 * With a gotcha for display contents:
1447 * https://github.com/whatwg/html/issues/1837
1449 bool IsRendered() const { return GetPrimaryFrame() || IsDisplayContents(); }
1451 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr) const {
1452 return mAttrs.GetAttr(aAttr);
1455 const nsAttrValue* GetParsedAttr(const nsAtom* aAttr,
1456 int32_t aNameSpaceID) const {
1457 return mAttrs.GetAttr(aAttr, aNameSpaceID);
1461 * Returns the attribute map, if there is one.
1463 * @return existing attribute map or nullptr.
1465 nsDOMAttributeMap* GetAttributeMap() {
1466 nsDOMSlots* slots = GetExistingDOMSlots();
1468 return slots ? slots->mAttributeMap.get() : nullptr;
1471 virtual void RecompileScriptEventListeners() {}
1474 * Get the attr info for the given namespace ID and attribute name. The
1475 * namespace ID must not be kNameSpaceID_Unknown and the name must not be
1476 * null. Note that this can only return info on attributes that actually
1477 * live on this element (and is only virtual to handle XUL prototypes). That
1478 * is, this should only be called from methods that only care about attrs
1479 * that effectively live in mAttrs.
1481 BorrowedAttrInfo GetAttrInfo(int32_t aNamespaceID,
1482 const nsAtom* aName) const {
1483 NS_ASSERTION(aName, "must have attribute name");
1484 NS_ASSERTION(aNamespaceID != kNameSpaceID_Unknown,
1485 "must have a real namespace ID!");
1487 int32_t index = mAttrs.IndexOfAttr(aName, aNamespaceID);
1488 if (index < 0) {
1489 return BorrowedAttrInfo(nullptr, nullptr);
1492 return mAttrs.AttrInfoAt(index);
1496 * Parse a string into an nsAttrValue for a CORS attribute. This
1497 * never fails. The resulting value is an enumerated value whose
1498 * GetEnumValue() returns one of the above constants.
1500 static void ParseCORSValue(const nsAString& aValue, nsAttrValue& aResult);
1503 * Return the CORS mode for a given string
1505 static CORSMode StringToCORSMode(const nsAString& aValue);
1508 * Return the CORS mode for a given nsAttrValue (which may be null,
1509 * but if not should have been parsed via ParseCORSValue).
1511 static CORSMode AttrValueToCORSMode(const nsAttrValue* aValue);
1513 nsINode* GetScopeChainParent() const override;
1516 * Locate a TextEditor rooted at this content node, if there is one.
1518 MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::TextEditor* GetTextEditorInternal();
1521 * Gets value of boolean attribute. Only works for attributes in null
1522 * namespace.
1524 * @param aAttr name of attribute.
1525 * @param aValue Boolean value of attribute.
1527 bool GetBoolAttr(nsAtom* aAttr) const {
1528 return HasAttr(kNameSpaceID_None, aAttr);
1532 * Sets value of boolean attribute by removing attribute or setting it to
1533 * the empty string. Only works for attributes in null namespace.
1535 * @param aAttr name of attribute.
1536 * @param aValue Boolean value of attribute.
1538 nsresult SetBoolAttr(nsAtom* aAttr, bool aValue);
1541 * Gets the enum value string of an attribute and using a default value if
1542 * the attribute is missing or the string is an invalid enum value.
1544 * @param aType the name of the attribute.
1545 * @param aDefault the default value if the attribute is missing or invalid.
1546 * @param aResult string corresponding to the value [out].
1548 void GetEnumAttr(nsAtom* aAttr, const char* aDefault,
1549 nsAString& aResult) const;
1552 * Gets the enum value string of an attribute and using the default missing
1553 * value if the attribute is missing or the default invalid value if the
1554 * string is an invalid enum value.
1556 * @param aType the name of the attribute.
1557 * @param aDefaultMissing the default value if the attribute is missing. If
1558 null and the attribute is missing, aResult will be
1559 set to the null DOMString; this only matters for
1560 cases in which we're reflecting a nullable string.
1561 * @param aDefaultInvalid the default value if the attribute is invalid.
1562 * @param aResult string corresponding to the value [out].
1564 void GetEnumAttr(nsAtom* aAttr, const char* aDefaultMissing,
1565 const char* aDefaultInvalid, nsAString& aResult) const;
1568 * Unset an attribute.
1570 void UnsetAttr(nsAtom* aAttr, ErrorResult& aError) {
1571 aError = UnsetAttr(kNameSpaceID_None, aAttr, true);
1575 * Set an attribute in the simplest way possible.
1577 void SetAttr(nsAtom* aAttr, const nsAString& aValue, ErrorResult& aError) {
1578 aError = SetAttr(kNameSpaceID_None, aAttr, aValue, true);
1581 void SetAttr(nsAtom* aAttr, const nsAString& aValue,
1582 nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError) {
1583 aError =
1584 SetAttr(kNameSpaceID_None, aAttr, aValue, aTriggeringPrincipal, true);
1588 * Set a content attribute via a reflecting nullable string IDL
1589 * attribute (e.g. a CORS attribute). If DOMStringIsNull(aValue),
1590 * this will actually remove the content attribute.
1592 void SetOrRemoveNullableStringAttr(nsAtom* aName, const nsAString& aValue,
1593 ErrorResult& aError);
1596 * Retrieve the ratio of font-size-inflated text font size to computed font
1597 * size for this element. This will query the element for its primary frame,
1598 * and then use this to get font size inflation information about the frame.
1600 * @returns The font size inflation ratio (inflated font size to uninflated
1601 * font size) for the primary frame of this element. Returns 1.0
1602 * by default if font size inflation is not enabled. Returns -1
1603 * if the element does not have a primary frame.
1605 * @note The font size inflation ratio that is returned is actually the
1606 * font size inflation data for the element's _primary frame_, not the
1607 * element itself, but for most purposes, this should be sufficient.
1609 float FontSizeInflation();
1611 void GetImplementedPseudoElement(nsAString&) const;
1613 ReferrerPolicy GetReferrerPolicyAsEnum() const;
1614 ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue) const;
1617 * Helpers for .dataset. This is implemented on Element, though only some
1618 * sorts of elements expose it to JS as a .dataset property
1620 // Getter, to be called from bindings.
1621 already_AddRefed<nsDOMStringMap> Dataset();
1622 // Callback for destructor of dataset to ensure to null out our weak pointer
1623 // to it.
1624 void ClearDataset();
1626 void RegisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1627 void UnregisterIntersectionObserver(DOMIntersectionObserver* aObserver);
1628 void UnlinkIntersectionObservers();
1629 bool UpdateIntersectionObservation(DOMIntersectionObserver* aObserver,
1630 int32_t threshold);
1632 // A number of methods to cast to various XUL interfaces. They return a
1633 // pointer only if the element implements that interface.
1634 already_AddRefed<nsIDOMXULButtonElement> AsXULButton();
1635 already_AddRefed<nsIDOMXULContainerElement> AsXULContainer();
1636 already_AddRefed<nsIDOMXULContainerItemElement> AsXULContainerItem();
1637 already_AddRefed<nsIDOMXULControlElement> AsXULControl();
1638 already_AddRefed<nsIDOMXULMenuListElement> AsXULMenuList();
1639 already_AddRefed<nsIDOMXULMultiSelectControlElement>
1640 AsXULMultiSelectControl();
1641 already_AddRefed<nsIDOMXULRadioGroupElement> AsXULRadioGroup();
1642 already_AddRefed<nsIDOMXULRelatedElement> AsXULRelated();
1643 already_AddRefed<nsIDOMXULSelectControlElement> AsXULSelectControl();
1644 already_AddRefed<nsIDOMXULSelectControlItemElement> AsXULSelectControlItem();
1645 already_AddRefed<nsIBrowser> AsBrowser();
1646 already_AddRefed<nsIAutoCompletePopup> AsAutoCompletePopup();
1648 protected:
1650 * Named-bools for use with SetAttrAndNotify to make call sites easier to
1651 * read.
1653 static const bool kFireMutationEvent = true;
1654 static const bool kDontFireMutationEvent = false;
1655 static const bool kNotifyDocumentObservers = true;
1656 static const bool kDontNotifyDocumentObservers = false;
1657 static const bool kCallAfterSetAttr = true;
1658 static const bool kDontCallAfterSetAttr = false;
1661 * Set attribute and (if needed) notify documentobservers and fire off
1662 * mutation events. This will send the AttributeChanged notification.
1663 * Callers of this method are responsible for calling AttributeWillChange,
1664 * since that needs to happen before the new attr value has been set, and
1665 * in particular before it has been parsed.
1667 * For the boolean parameters, consider using the named bools above to aid
1668 * code readability.
1670 * @param aNamespaceID namespace of attribute
1671 * @param aAttribute local-name of attribute
1672 * @param aPrefix aPrefix of attribute
1673 * @param aOldValue The old value of the attribute to use as a fallback
1674 * in the cases where the actual old value (i.e.
1675 * its current value) is !StoresOwnData() --- in which
1676 * case the current value is probably already useless.
1677 * If the current value is StoresOwnData() (or absent),
1678 * aOldValue will not be used. aOldValue will only be set
1679 * in certain circumstances (there are mutation
1680 * listeners, element is a custom element, attribute was
1681 * not previously unset). Otherwise it will be null.
1682 * @param aParsedValue parsed new value of attribute. Replaced by the
1683 * old value of the attribute. This old value is only
1684 * useful if either it or the new value is StoresOwnData.
1685 * @param aMaybeScriptedPrincipal
1686 * the principal of the scripted caller responsible for
1687 * setting the attribute, or null if no scripted caller
1688 * can be determined. A null value here does not
1689 * guarantee that there is no scripted caller, but a
1690 * non-null value does guarantee that a scripted caller
1691 * with the given principal is directly responsible for
1692 * the attribute change.
1693 * @param aModType MutationEvent_Binding::MODIFICATION or ADDITION. Only
1694 * needed if aFireMutation or aNotify is true.
1695 * @param aFireMutation should mutation-events be fired?
1696 * @param aNotify should we notify document-observers?
1697 * @param aCallAfterSetAttr should we call AfterSetAttr?
1698 * @param aComposedDocument The current composed document of the element.
1700 nsresult SetAttrAndNotify(int32_t aNamespaceID, nsAtom* aName,
1701 nsAtom* aPrefix, const nsAttrValue* aOldValue,
1702 nsAttrValue& aParsedValue,
1703 nsIPrincipal* aMaybeScriptedPrincipal,
1704 uint8_t aModType, bool aFireMutation, bool aNotify,
1705 bool aCallAfterSetAttr, Document* aComposedDocument,
1706 const mozAutoDocUpdate& aGuard);
1709 * Scroll to a new position using behavior evaluated from CSS and
1710 * a CSSOM-View DOM method ScrollOptions dictionary. The scrolling may
1711 * be performed asynchronously or synchronously depending on the resolved
1712 * scroll-behavior.
1714 * @param aScroll Destination of scroll, in CSS pixels
1715 * @param aOptions Dictionary of options to be evaluated
1717 MOZ_CAN_RUN_SCRIPT
1718 void Scroll(const CSSIntPoint& aScroll, const ScrollOptions& aOptions);
1721 * Convert an attribute string value to attribute type based on the type of
1722 * attribute. Called by SetAttr(). Note that at the moment we only do this
1723 * for attributes in the null namespace (kNameSpaceID_None).
1725 * @param aNamespaceID the namespace of the attribute to convert
1726 * @param aAttribute the attribute to convert
1727 * @param aValue the string value to convert
1728 * @param aMaybeScriptedPrincipal the principal of the script setting the
1729 * attribute, if one can be determined, or null otherwise. As in
1730 * AfterSetAttr, a null value does not guarantee that the attribute was
1731 * not set by a scripted caller, but a non-null value guarantees that
1732 * the attribute was set by a scripted caller with the given principal.
1733 * @param aResult the nsAttrValue [OUT]
1734 * @return true if the parsing was successful, false otherwise
1736 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
1737 const nsAString& aValue,
1738 nsIPrincipal* aMaybeScriptedPrincipal,
1739 nsAttrValue& aResult);
1742 * Try to set the attribute as a mapped attribute, if applicable. This will
1743 * only be called for attributes that are in the null namespace and only on
1744 * attributes that returned true when passed to IsAttributeMapped. The
1745 * caller will not try to set the attr in any other way if this method
1746 * returns true (the value of aRetval does not matter for that purpose).
1748 * @param aName the name of the attribute
1749 * @param aValue the nsAttrValue to set. Will be swapped with the existing
1750 * value of the attribute if the attribute already exists.
1751 * @param [out] aValueWasSet If the attribute was not set previously,
1752 * aValue will be swapped with an empty attribute
1753 * and aValueWasSet will be set to false. Otherwise,
1754 * aValueWasSet will be set to true and aValue will
1755 * contain the previous value set.
1756 * @param [out] aRetval the nsresult status of the operation, if any.
1757 * @return true if the setting was attempted, false otherwise.
1759 virtual bool SetAndSwapMappedAttribute(nsAtom* aName, nsAttrValue& aValue,
1760 bool* aValueWasSet, nsresult* aRetval);
1763 * Hook that is called by Element::SetAttr to allow subclasses to
1764 * deal with attribute sets. This will only be called after we verify that
1765 * we're actually doing an attr set and will be called before
1766 * AttributeWillChange and before ParseAttribute and hence before we've set
1767 * the new value.
1769 * @param aNamespaceID the namespace of the attr being set
1770 * @param aName the localname of the attribute being set
1771 * @param aValue the value it's being set to represented as either a string or
1772 * a parsed nsAttrValue. Alternatively, if the attr is being removed it
1773 * will be null.
1774 * @param aNotify Whether we plan to notify document observers.
1776 virtual nsresult BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
1777 const nsAttrValueOrString* aValue,
1778 bool aNotify);
1781 * Hook that is called by Element::SetAttr to allow subclasses to
1782 * deal with attribute sets. This will only be called after we have called
1783 * SetAndSwapAttr (that is, after we have actually set the attr). It will
1784 * always be called under a scriptblocker.
1786 * @param aNamespaceID the namespace of the attr being set
1787 * @param aName the localname of the attribute being set
1788 * @param aValue the value it's being set to. If null, the attr is being
1789 * removed.
1790 * @param aOldValue the value that the attribute had previously. If null,
1791 * the attr was not previously set. This argument may not have the
1792 * correct value for SVG elements, or other cases in which the
1793 * attribute value doesn't store its own data
1794 * @param aMaybeScriptedPrincipal the principal of the scripted caller
1795 * responsible for setting the attribute, or null if no scripted caller
1796 * can be determined, or the attribute is being unset. A null value
1797 * here does not guarantee that there is no scripted caller, but a
1798 * non-null value does guarantee that a scripted caller with the given
1799 * principal is directly responsible for the attribute change.
1800 * @param aNotify Whether we plan to notify document observers.
1802 virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
1803 const nsAttrValue* aValue,
1804 const nsAttrValue* aOldValue,
1805 nsIPrincipal* aMaybeScriptedPrincipal,
1806 bool aNotify);
1809 * This function shall be called just before the id attribute changes. It will
1810 * be called after BeforeSetAttr. If the attribute being changed is not the id
1811 * attribute, this function does nothing. Otherwise, it will remove the old id
1812 * from the document's id cache.
1814 * This must happen after BeforeSetAttr (rather than during) because the
1815 * the subclasses' calls to BeforeSetAttr may notify on state changes. If they
1816 * incorrectly determine whether the element had an id, the element may not be
1817 * restyled properly.
1819 * @param aNamespaceID the namespace of the attr being set
1820 * @param aName the localname of the attribute being set
1821 * @param aValue the new id value. Will be null if the id is being unset.
1823 void PreIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1824 const nsAttrValueOrString* aValue);
1827 * This function shall be called just after the id attribute changes. It will
1828 * be called before AfterSetAttr. If the attribute being changed is not the id
1829 * attribute, this function does nothing. Otherwise, it will add the new id to
1830 * the document's id cache and properly set the ElementHasID flag.
1832 * This must happen before AfterSetAttr (rather than during) because the
1833 * the subclasses' calls to AfterSetAttr may notify on state changes. If they
1834 * incorrectly determine whether the element now has an id, the element may
1835 * not be restyled properly.
1837 * @param aNamespaceID the namespace of the attr being set
1838 * @param aName the localname of the attribute being set
1839 * @param aValue the new id value. Will be null if the id is being unset.
1841 void PostIdMaybeChange(int32_t aNamespaceID, nsAtom* aName,
1842 const nsAttrValue* aValue);
1845 * Usually, setting an attribute to the value that it already has results in
1846 * no action. However, in some cases, setting an attribute to its current
1847 * value should have the effect of, for example, forcing a reload of
1848 * network data. To address that, this function will be called in this
1849 * situation to allow the handling of such a case.
1851 * @param aNamespaceID the namespace of the attr being set
1852 * @param aName the localname of the attribute being set
1853 * @param aValue the value it's being set to represented as either a string or
1854 * a parsed nsAttrValue.
1855 * @param aNotify Whether we plan to notify document observers.
1857 // Note that this is inlined so that when subclasses call it it gets
1858 // inlined. Those calls don't go through a vtable.
1859 virtual nsresult OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
1860 const nsAttrValueOrString& aValue,
1861 bool aNotify);
1864 * Hook to allow subclasses to produce a different EventListenerManager if
1865 * needed for attachment of attribute-defined handlers
1867 virtual EventListenerManager* GetEventListenerManagerForAttr(
1868 nsAtom* aAttrName, bool* aDefer);
1871 * Internal hook for converting an attribute name-string to nsAttrName in
1872 * case there is such existing attribute. aNameToUse can be passed to get
1873 * name which was used for looking for the attribute (lowercase in HTML).
1875 const nsAttrName* InternalGetAttrNameFromQName(
1876 const nsAString& aStr, nsAutoString* aNameToUse = nullptr) const;
1878 virtual Element* GetNameSpaceElement() override { return this; }
1880 Attr* GetAttributeNodeNSInternal(const nsAString& aNamespaceURI,
1881 const nsAString& aLocalName);
1883 inline void RegisterActivityObserver();
1884 inline void UnregisterActivityObserver();
1887 * Add/remove this element to the documents id cache
1889 void AddToIdTable(nsAtom* aId);
1890 void RemoveFromIdTable();
1893 * Functions to carry out event default actions for links of all types
1894 * (HTML links, XLinks, SVG "XLinks", etc.)
1898 * Check that we meet the conditions to handle a link event
1899 * and that we are actually on a link.
1901 * @param aVisitor event visitor
1902 * @param aURI the uri of the link, set only if the return value is true [OUT]
1903 * @return true if we can handle the link event, false otherwise
1905 bool CheckHandleEventForLinksPrecondition(EventChainVisitor& aVisitor,
1906 nsIURI** aURI) const;
1909 * Handle status bar updates before they can be cancelled.
1911 void GetEventTargetParentForLinks(EventChainPreVisitor& aVisitor);
1914 * Handle default actions for link event if the event isn't consumed yet.
1916 MOZ_CAN_RUN_SCRIPT
1917 nsresult PostHandleEventForLinks(EventChainPostVisitor& aVisitor);
1920 * Get the target of this link element. Consumers should established that
1921 * this element is a link (probably using IsLink) before calling this
1922 * function (or else why call it?)
1924 * Note: for HTML this gets the value of the 'target' attribute; for XLink
1925 * this gets the value of the xlink:_moz_target attribute, or failing that,
1926 * the value of xlink:show, converted to a suitably equivalent named target
1927 * (e.g. _blank).
1929 virtual void GetLinkTarget(nsAString& aTarget);
1931 enum class ReparseAttributes { No, Yes };
1933 * Copy attributes and state to another element
1934 * @param aDest the object to copy to
1936 nsresult CopyInnerTo(Element* aDest,
1937 ReparseAttributes = ReparseAttributes::Yes);
1940 * Some event handler content attributes have a different name (e.g. different
1941 * case) from the actual event name. This function takes an event handler
1942 * content attribute name and returns the corresponding event name, to be used
1943 * for adding the actual event listener.
1945 static nsAtom* GetEventNameForAttr(nsAtom* aAttr);
1947 private:
1948 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1949 void AssertInvariantsOnNodeInfoChange();
1950 #endif
1953 * Slow path for GetClasses, this should only be called for SVG elements.
1955 const nsAttrValue* GetSVGAnimatedClass() const;
1958 * Get this element's client area rect in app units.
1959 * @return the frame's client area
1961 MOZ_CAN_RUN_SCRIPT nsRect GetClientAreaRect();
1964 * GetCustomInterface is somewhat like a GetInterface, but it is expected
1965 * that the implementation is provided by a custom element or via the
1966 * the XBL implements keyword. To use this, create a public method that
1967 * wraps a call to GetCustomInterface.
1969 template <class T>
1970 void GetCustomInterface(nsGetterAddRefs<T> aResult);
1972 // Prevent people from doing pointless checks/casts on Element instances.
1973 void IsElement() = delete;
1974 void AsElement() = delete;
1976 // Data members
1977 EventStates mState;
1978 // Per-node data managed by Servo.
1980 // There should not be data on nodes that are not in the flattened tree, or
1981 // descendants of display: none elements.
1982 mozilla::RustCell<ServoNodeData*> mServoData;
1984 protected:
1985 // Array containing all attributes for this element
1986 AttrArray mAttrs;
1989 NS_DEFINE_STATIC_IID_ACCESSOR(Element, NS_ELEMENT_IID)
1991 inline bool Element::HasAttr(int32_t aNameSpaceID, const nsAtom* aName) const {
1992 NS_ASSERTION(nullptr != aName, "must have attribute name");
1993 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
1994 "must have a real namespace ID!");
1996 return mAttrs.IndexOfAttr(aName, aNameSpaceID) >= 0;
1999 inline bool Element::HasNonEmptyAttr(int32_t aNameSpaceID,
2000 const nsAtom* aName) const {
2001 MOZ_ASSERT(aNameSpaceID > kNameSpaceID_Unknown, "Must have namespace");
2002 MOZ_ASSERT(aName, "Must have attribute name");
2004 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2005 return val && !val->IsEmptyString();
2008 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2009 const nsAString& aValue,
2010 nsCaseTreatment aCaseSensitive) const {
2011 NS_ASSERTION(aName, "Must have attr name");
2012 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown, "Must have namespace");
2014 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2015 return val && val->Equals(aValue, aCaseSensitive);
2018 inline bool Element::AttrValueIs(int32_t aNameSpaceID, const nsAtom* aName,
2019 const nsAtom* aValue,
2020 nsCaseTreatment aCaseSensitive) const {
2021 NS_ASSERTION(aName, "Must have attr name");
2022 NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown, "Must have namespace");
2023 NS_ASSERTION(aValue, "Null value atom");
2025 const nsAttrValue* val = mAttrs.GetAttr(aName, aNameSpaceID);
2026 return val && val->Equals(aValue, aCaseSensitive);
2029 } // namespace dom
2030 } // namespace mozilla
2032 inline mozilla::dom::Element* nsINode::AsElement() {
2033 MOZ_ASSERT(IsElement());
2034 return static_cast<mozilla::dom::Element*>(this);
2037 inline const mozilla::dom::Element* nsINode::AsElement() const {
2038 MOZ_ASSERT(IsElement());
2039 return static_cast<const mozilla::dom::Element*>(this);
2042 inline mozilla::dom::Element* nsINode::GetParentElement() const {
2043 return mozilla::dom::Element::FromNodeOrNull(mParent);
2046 inline mozilla::dom::Element* nsINode::GetPreviousElementSibling() const {
2047 nsIContent* previousSibling = GetPreviousSibling();
2048 while (previousSibling) {
2049 if (previousSibling->IsElement()) {
2050 return previousSibling->AsElement();
2052 previousSibling = previousSibling->GetPreviousSibling();
2055 return nullptr;
2058 inline mozilla::dom::Element* nsINode::GetAsElementOrParentElement() const {
2059 return IsElement() ? const_cast<mozilla::dom::Element*>(AsElement())
2060 : GetParentElement();
2063 inline mozilla::dom::Element* nsINode::GetNextElementSibling() const {
2064 nsIContent* nextSibling = GetNextSibling();
2065 while (nextSibling) {
2066 if (nextSibling->IsElement()) {
2067 return nextSibling->AsElement();
2069 nextSibling = nextSibling->GetNextSibling();
2072 return nullptr;
2076 * Macros to implement Clone(). _elementName is the class for which to implement
2077 * Clone.
2079 #define NS_IMPL_ELEMENT_CLONE(_elementName) \
2080 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2081 nsINode** aResult) const { \
2082 *aResult = nullptr; \
2083 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2084 auto* nim = ni->NodeInfoManager(); \
2085 RefPtr<_elementName> it = new (nim) _elementName(ni.forget()); \
2086 nsresult rv = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2087 if (NS_SUCCEEDED(rv)) { \
2088 it.forget(aResult); \
2091 return rv; \
2094 #define EXPAND(...) __VA_ARGS__
2095 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, extra_args_) \
2096 nsresult _elementName::Clone(mozilla::dom::NodeInfo* aNodeInfo, \
2097 nsINode** aResult) const { \
2098 *aResult = nullptr; \
2099 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo); \
2100 auto* nim = ni->NodeInfoManager(); \
2101 RefPtr<_elementName> it = \
2102 new (nim) _elementName(ni.forget() EXPAND extra_args_); \
2103 nsresult rv = it->Init(); \
2104 nsresult rv2 = const_cast<_elementName*>(this)->CopyInnerTo(it); \
2105 if (NS_FAILED(rv2)) { \
2106 rv = rv2; \
2108 if (NS_SUCCEEDED(rv)) { \
2109 it.forget(aResult); \
2112 return rv; \
2115 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT(_elementName) \
2116 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, ())
2117 #define NS_IMPL_ELEMENT_CLONE_WITH_INIT_AND_PARSER(_elementName) \
2118 NS_IMPL_ELEMENT_CLONE_WITH_INIT_HELPER(_elementName, (, NOT_FROM_PARSER))
2120 #endif // mozilla_dom_Element_h__