Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsIContent.h
blob1308ffcd335a32e8f462190ec85a53ebdcc4c4f8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsIContent_h___
6 #define nsIContent_h___
8 #include "mozilla/Attributes.h"
9 #include "nsCaseTreatment.h" // for enum, cannot be forward-declared
10 #include "nsINode.h"
12 // Forward declarations
13 class nsAString;
14 class nsIAtom;
15 class nsIURI;
16 class nsRuleWalker;
17 class nsAttrValue;
18 class nsAttrName;
19 class nsTextFragment;
20 class nsIFrame;
21 class nsXBLBinding;
23 namespace mozilla {
24 class EventChainPreVisitor;
25 namespace dom {
26 class ShadowRoot;
27 struct CustomElementData;
28 } // namespace dom
29 namespace widget {
30 struct IMEState;
31 } // namespace widget
32 } // namespace mozilla
34 enum nsLinkState {
35 eLinkState_Unvisited = 1,
36 eLinkState_Visited = 2,
37 eLinkState_NotLink = 3
40 // IID for the nsIContent interface
41 #define NS_ICONTENT_IID \
42 { 0x697a2fe1, 0x5549, 0x48e7, \
43 { 0x9a, 0x1a, 0xc2, 0x9d, 0xab, 0x14, 0xe2, 0x39 } }
45 /**
46 * A node of content in a document's content model. This interface
47 * is supported by all content objects.
49 class nsIContent : public nsINode {
50 public:
51 typedef mozilla::widget::IMEState IMEState;
53 #ifdef MOZILLA_INTERNAL_API
54 // If you're using the external API, the only thing you can know about
55 // nsIContent is that it exists with an IID
57 explicit nsIContent(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
58 : nsINode(aNodeInfo)
60 MOZ_ASSERT(mNodeInfo);
61 SetNodeIsContent();
63 #endif // MOZILLA_INTERNAL_API
65 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONTENT_IID)
67 /**
68 * Bind this content node to a tree. If this method throws, the caller must
69 * call UnbindFromTree() on the node. In the typical case of a node being
70 * appended to a parent, this will be called after the node has been added to
71 * the parent's child list and before nsIDocumentObserver notifications for
72 * the addition are dispatched.
73 * @param aDocument The new document for the content node. May not be null
74 * if aParent is null. Must match the current document of
75 * aParent, if aParent is not null (note that
76 * aParent->GetCurrentDoc() can be null, in which case this
77 * must also be null).
78 * @param aParent The new parent for the content node. May be null if the
79 * node is being bound as a direct child of the document.
80 * @param aBindingParent The new binding parent for the content node.
81 * This is must either be non-null if a particular
82 * binding parent is desired or match aParent's binding
83 * parent.
84 * @param aCompileEventHandlers whether to initialize the event handlers in
85 * the document (used by nsXULElement)
86 * @note either aDocument or aParent must be non-null. If both are null,
87 * this method _will_ crash.
88 * @note This method must not be called by consumers of nsIContent on a node
89 * that is already bound to a tree. Call UnbindFromTree first.
90 * @note This method will handle rebinding descendants appropriately (eg
91 * changing their binding parent as needed).
92 * @note This method does not add the content node to aParent's child list
93 * @throws NS_ERROR_OUT_OF_MEMORY if that happens
95 virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
96 nsIContent* aBindingParent,
97 bool aCompileEventHandlers) = 0;
99 /**
100 * Unbind this content node from a tree. This will set its current document
101 * and binding parent to null. In the typical case of a node being removed
102 * from a parent, this will be called after it has been removed from the
103 * parent's child list and after the nsIDocumentObserver notifications for
104 * the removal have been dispatched.
105 * @param aDeep Whether to recursively unbind the entire subtree rooted at
106 * this node. The only time false should be passed is when the
107 * parent node of the content is being destroyed.
108 * @param aNullParent Whether to null out the parent pointer as well. This
109 * is usually desirable. This argument should only be false while
110 * recursively calling UnbindFromTree when a subtree is detached.
111 * @note This method is safe to call on nodes that are not bound to a tree.
113 virtual void UnbindFromTree(bool aDeep = true,
114 bool aNullParent = true) = 0;
116 enum {
118 * All XBL flattened tree children of the node, as well as :before and
119 * :after anonymous content and native anonymous children.
121 * @note the result children order is
122 * 1. :before generated node
123 * 2. XBL flattened tree children of this node
124 * 3. native anonymous nodes
125 * 4. :after generated node
127 eAllChildren = 0,
130 * All XBL explicit children of the node (see
131 * http://www.w3.org/TR/xbl/#explicit3 ), as well as :before and :after
132 * anonymous content and native anonymous children.
134 * @note the result children order is
135 * 1. :before generated node
136 * 2. XBL explicit children of the node
137 * 3. native anonymous nodes
138 * 4. :after generated node
140 eAllButXBL = 1,
143 * Skip native anonymous content created for placeholder of HTML input,
144 * used in conjunction with eAllChildren or eAllButXBL.
146 eSkipPlaceholderContent = 2
150 * Return either the XBL explicit children of the node or the XBL flattened
151 * tree children of the node, depending on the filter, as well as
152 * native anonymous children.
154 * @note calling this method with eAllButXBL will return children that are
155 * also in the eAllButXBL and eAllChildren child lists of other descendants
156 * of this node in the tree, but those other nodes cannot be reached from the
157 * eAllButXBL child list.
159 virtual already_AddRefed<nsINodeList> GetChildren(uint32_t aFilter) = 0;
162 * Get whether this content is C++-generated anonymous content
163 * @see nsIAnonymousContentCreator
164 * @return whether this content is anonymous
166 bool IsRootOfNativeAnonymousSubtree() const
168 NS_ASSERTION(!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) ||
169 (HasFlag(NODE_IS_ANONYMOUS_ROOT) &&
170 HasFlag(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE)),
171 "Some flags seem to be missing!");
172 return HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT);
175 bool IsRootOfChromeAccessOnlySubtree() const
177 return HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT |
178 NODE_IS_ROOT_OF_CHROME_ONLY_ACCESS);
182 * Makes this content anonymous
183 * @see nsIAnonymousContentCreator
185 void SetIsNativeAnonymousRoot()
187 SetFlags(NODE_IS_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE |
188 NODE_IS_NATIVE_ANONYMOUS_ROOT);
192 * Returns |this| if it is not chrome-only/native anonymous, otherwise
193 * first non chrome-only/native anonymous ancestor.
195 virtual nsIContent* FindFirstNonChromeOnlyAccessContent() const;
198 * Returns true if and only if this node has a parent, but is not in
199 * its parent's child list.
201 bool IsRootOfAnonymousSubtree() const
203 NS_ASSERTION(!IsRootOfNativeAnonymousSubtree() ||
204 (GetParent() && GetBindingParent() == GetParent()),
205 "root of native anonymous subtree must have parent equal "
206 "to binding parent");
207 NS_ASSERTION(!GetParent() ||
208 ((GetBindingParent() == GetParent()) ==
209 HasFlag(NODE_IS_ANONYMOUS_ROOT)) ||
210 // Unfortunately default content for XBL insertion points is
211 // anonymous content that is bound with the parent of the
212 // insertion point as the parent but the bound element for the
213 // binding as the binding parent. So we have to complicate
214 // the assert a bit here.
215 (GetBindingParent() &&
216 (GetBindingParent() == GetParent()->GetBindingParent()) ==
217 HasFlag(NODE_IS_ANONYMOUS_ROOT)),
218 "For nodes with parent, flag and GetBindingParent() check "
219 "should match");
220 return HasFlag(NODE_IS_ANONYMOUS_ROOT);
224 * Returns true if there is NOT a path through child lists
225 * from the top of this node's parent chain back to this node or
226 * if the node is in native anonymous subtree without a parent.
228 bool IsInAnonymousSubtree() const
230 NS_ASSERTION(!IsInNativeAnonymousSubtree() || GetBindingParent() ||
231 (!IsInDoc() &&
232 static_cast<nsIContent*>(SubtreeRoot())->IsInNativeAnonymousSubtree()),
233 "Must have binding parent when in native anonymous subtree which is in document.\n"
234 "Native anonymous subtree which is not in document must have native anonymous root.");
235 return IsInNativeAnonymousSubtree() || (!IsInShadowTree() && GetBindingParent() != nullptr);
239 * Return true iff this node is in an HTML document (in the HTML5 sense of
240 * the term, i.e. not in an XHTML/XML document).
242 inline bool IsInHTMLDocument() const;
245 * Get the namespace that this element's tag is defined in
246 * @return the namespace
248 inline int32_t GetNameSpaceID() const
250 return mNodeInfo->NamespaceID();
254 * Get the NodeInfo for this element
255 * @return the nodes node info
257 inline mozilla::dom::NodeInfo* NodeInfo() const
259 return mNodeInfo;
262 inline bool IsInNamespace(int32_t aNamespace) const
264 return mNodeInfo->NamespaceID() == aNamespace;
267 inline bool IsHTML() const
269 return IsInNamespace(kNameSpaceID_XHTML);
272 inline bool IsHTML(nsIAtom* aTag) const
274 return mNodeInfo->Equals(aTag, kNameSpaceID_XHTML);
277 inline bool IsSVG() const
279 return IsInNamespace(kNameSpaceID_SVG);
282 inline bool IsSVG(nsIAtom* aTag) const
284 return mNodeInfo->Equals(aTag, kNameSpaceID_SVG);
287 inline bool IsXUL() const
289 return IsInNamespace(kNameSpaceID_XUL);
292 inline bool IsXUL(nsIAtom* aTag) const
294 return mNodeInfo->Equals(aTag, kNameSpaceID_XUL);
297 inline bool IsMathML() const
299 return IsInNamespace(kNameSpaceID_MathML);
302 inline bool IsMathML(nsIAtom* aTag) const
304 return mNodeInfo->Equals(aTag, kNameSpaceID_MathML);
307 inline bool IsActiveChildrenElement() const
309 return mNodeInfo->Equals(nsGkAtoms::children, kNameSpaceID_XBL) &&
310 GetBindingParent();
314 * Set attribute values. All attribute values are assumed to have a
315 * canonical string representation that can be used for these
316 * methods. The SetAttr method is assumed to perform a translation
317 * of the canonical form into the underlying content specific
318 * form.
320 * @param aNameSpaceID the namespace of the attribute
321 * @param aName the name of the attribute
322 * @param aValue the value to set
323 * @param aNotify specifies how whether or not the document should be
324 * notified of the attribute change.
326 nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
327 const nsAString& aValue, bool aNotify)
329 return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
333 * Set attribute values. All attribute values are assumed to have a
334 * canonical String representation that can be used for these
335 * methods. The SetAttr method is assumed to perform a translation
336 * of the canonical form into the underlying content specific
337 * form.
339 * @param aNameSpaceID the namespace of the attribute
340 * @param aName the name of the attribute
341 * @param aPrefix the prefix of the attribute
342 * @param aValue the value to set
343 * @param aNotify specifies how whether or not the document should be
344 * notified of the attribute change.
346 virtual nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
347 nsIAtom* aPrefix, const nsAString& aValue,
348 bool aNotify) = 0;
351 * Get the current value of the attribute. This returns a form that is
352 * suitable for passing back into SetAttr.
354 * @param aNameSpaceID the namespace of the attr
355 * @param aName the name of the attr
356 * @param aResult the value (may legitimately be the empty string) [OUT]
357 * @returns true if the attribute was set (even when set to empty string)
358 * false when not set.
360 bool GetAttr(int32_t aNameSpaceID, nsIAtom* aName,
361 nsAString& aResult) const;
364 * Determine if an attribute has been set (empty string or otherwise).
366 * @param aNameSpaceId the namespace id of the attribute
367 * @param aAttr the attribute name
368 * @return whether an attribute exists
370 bool HasAttr(int32_t aNameSpaceID, nsIAtom* aName) const;
373 * Test whether this content node's given attribute has the given value. If
374 * the attribute is not set at all, this will return false.
376 * @param aNameSpaceID The namespace ID of the attribute. Must not
377 * be kNameSpaceID_Unknown.
378 * @param aName The name atom of the attribute. Must not be null.
379 * @param aValue The value to compare to.
380 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
382 bool AttrValueIs(int32_t aNameSpaceID,
383 nsIAtom* aName,
384 const nsAString& aValue,
385 nsCaseTreatment aCaseSensitive) const;
388 * Test whether this content node's given attribute has the given value. If
389 * the attribute is not set at all, this will return false.
391 * @param aNameSpaceID The namespace ID of the attribute. Must not
392 * be kNameSpaceID_Unknown.
393 * @param aName The name atom of the attribute. Must not be null.
394 * @param aValue The value to compare to. Must not be null.
395 * @param aCaseSensitive Whether to do a case-sensitive compare on the value.
397 bool AttrValueIs(int32_t aNameSpaceID,
398 nsIAtom* aName,
399 nsIAtom* aValue,
400 nsCaseTreatment aCaseSensitive) const;
402 enum {
403 ATTR_MISSING = -1,
404 ATTR_VALUE_NO_MATCH = -2
407 * Check whether this content node's given attribute has one of a given
408 * list of values. If there is a match, we return the index in the list
409 * of the first matching value. If there was no attribute at all, then
410 * we return ATTR_MISSING. If there was an attribute but it didn't
411 * match, we return ATTR_VALUE_NO_MATCH. A non-negative result always
412 * indicates a match.
414 * @param aNameSpaceID The namespace ID of the attribute. Must not
415 * be kNameSpaceID_Unknown.
416 * @param aName The name atom of the attribute. Must not be null.
417 * @param aValues a nullptr-terminated array of pointers to atom values to test
418 * against.
419 * @param aCaseSensitive Whether to do a case-sensitive compare on the values.
420 * @return ATTR_MISSING, ATTR_VALUE_NO_MATCH or the non-negative index
421 * indicating the first value of aValues that matched
423 typedef nsIAtom* const* const AttrValuesArray;
424 virtual int32_t FindAttrValueIn(int32_t aNameSpaceID,
425 nsIAtom* aName,
426 AttrValuesArray* aValues,
427 nsCaseTreatment aCaseSensitive) const
429 return ATTR_MISSING;
433 * Remove an attribute so that it is no longer explicitly specified.
435 * @param aNameSpaceID the namespace id of the attribute
436 * @param aAttr the name of the attribute to unset
437 * @param aNotify specifies whether or not the document should be
438 * notified of the attribute change
440 virtual nsresult UnsetAttr(int32_t aNameSpaceID, nsIAtom* aAttr,
441 bool aNotify) = 0;
445 * Get the namespace / name / prefix of a given attribute.
447 * @param aIndex the index of the attribute name
448 * @returns The name at the given index, or null if the index is
449 * out-of-bounds.
450 * @note The document returned by NodeInfo()->GetDocument() (if one is
451 * present) is *not* necessarily the owner document of the element.
452 * @note The pointer returned by this function is only valid until the
453 * next call of either GetAttrNameAt or SetAttr on the element.
455 virtual const nsAttrName* GetAttrNameAt(uint32_t aIndex) const = 0;
458 * Get the number of all specified attributes.
460 * @return the number of attributes
462 virtual uint32_t GetAttrCount() const = 0;
465 * Get direct access (but read only) to the text in the text content.
466 * NOTE: For elements this is *not* the concatenation of all text children,
467 * it is simply null;
469 virtual const nsTextFragment *GetText() = 0;
472 * Get the length of the text content.
473 * NOTE: This should not be called on elements.
475 virtual uint32_t TextLength() const = 0;
478 * Determines if an event attribute name (such as onclick) is valid for
479 * a given element type.
480 * @note calls nsContentUtils::IsEventAttributeName with right flag
481 * @note overridden by subclasses as needed
482 * @param aName the event name to look up
484 virtual bool IsEventAttributeName(nsIAtom* aName)
486 return false;
490 * Set the text to the given value. If aNotify is true then
491 * the document is notified of the content change.
492 * NOTE: For elements this always ASSERTS and returns NS_ERROR_FAILURE
494 virtual nsresult SetText(const char16_t* aBuffer, uint32_t aLength,
495 bool aNotify) = 0;
498 * Append the given value to the current text. If aNotify is true then
499 * the document is notified of the content change.
500 * NOTE: For elements this always ASSERTS and returns NS_ERROR_FAILURE
502 virtual nsresult AppendText(const char16_t* aBuffer, uint32_t aLength,
503 bool aNotify) = 0;
506 * Set the text to the given value. If aNotify is true then
507 * the document is notified of the content change.
508 * NOTE: For elements this always asserts and returns NS_ERROR_FAILURE
510 nsresult SetText(const nsAString& aStr, bool aNotify)
512 return SetText(aStr.BeginReading(), aStr.Length(), aNotify);
516 * Query method to see if the frame is nothing but whitespace
517 * NOTE: Always returns false for elements
519 virtual bool TextIsOnlyWhitespace() = 0;
522 * Method to see if the text node contains data that is useful
523 * for a translation: i.e., it consists of more than just whitespace,
524 * digits and punctuation.
525 * NOTE: Always returns false for elements.
527 virtual bool HasTextForTranslation() = 0;
530 * Append the text content to aResult.
531 * NOTE: This asserts and returns for elements
533 virtual void AppendTextTo(nsAString& aResult) = 0;
536 * Append the text content to aResult.
537 * NOTE: This asserts and returns for elements
539 virtual bool AppendTextTo(nsAString& aResult,
540 const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT = 0;
543 * Check if this content is focusable and in the current tab order.
544 * Note: most callers should use nsIFrame::IsFocusable() instead as it
545 * checks visibility and other layout factors as well.
546 * Tabbable is indicated by a nonnegative tabindex & is a subset of focusable.
547 * For example, only the selected radio button in a group is in the
548 * tab order, unless the radio group has no selection in which case
549 * all of the visible, non-disabled radio buttons in the group are
550 * in the tab order. On the other hand, all of the visible, non-disabled
551 * radio buttons are always focusable via clicking or script.
552 * Also, depending on either the accessibility.tabfocus pref or
553 * a system setting (nowadays: Full keyboard access, mac only)
554 * some widgets may be focusable but removed from the tab order.
555 * @param [inout, optional] aTabIndex the computed tab index
556 * In: default tabindex for element (-1 nonfocusable, == 0 focusable)
557 * Out: computed tabindex
558 * @param [optional] aTabIndex the computed tab index
559 * < 0 if not tabbable
560 * == 0 if in normal tab order
561 * > 0 can be tabbed to in the order specified by this value
562 * @return whether the content is focusable via mouse, kbd or script.
564 bool IsFocusable(int32_t* aTabIndex = nullptr, bool aWithMouse = false);
565 virtual bool IsFocusableInternal(int32_t* aTabIndex, bool aWithMouse);
568 * The method focuses (or activates) element that accesskey is bound to. It is
569 * called when accesskey is activated.
571 * @param aKeyCausesActivation - if true then element should be activated
572 * @param aIsTrustedEvent - if true then event that is cause of accesskey
573 * execution is trusted.
575 virtual void PerformAccesskey(bool aKeyCausesActivation,
576 bool aIsTrustedEvent)
581 * Get desired IME state for the content.
583 * @return The desired IME status for the content.
584 * This is a combination of an IME enabled value and
585 * an IME open value of widget::IMEState.
586 * If you return DISABLED, you should not set the OPEN and CLOSE
587 * value.
588 * PASSWORD should be returned only from password editor, this value
589 * has a special meaning. It is used as alternative of DISABLED.
590 * PLUGIN should be returned only when plug-in has focus. When a
591 * plug-in is focused content, we should send native events directly.
592 * Because we don't process some native events, but they may be needed
593 * by the plug-in.
595 virtual IMEState GetDesiredIMEState();
598 * Gets content node with the binding (or native code, possibly on the
599 * frame) responsible for our construction (and existence). Used by
600 * anonymous content (both XBL-generated and native-anonymous).
602 * null for all explicit content (i.e., content reachable from the top
603 * of its GetParent() chain via child lists).
605 * @return the binding parent
607 virtual nsIContent *GetBindingParent() const = 0;
610 * Gets the current XBL binding that is bound to this element.
612 * @return the current binding.
614 virtual nsXBLBinding *GetXBLBinding() const = 0;
617 * Sets or unsets an XBL binding for this element. Setting a
618 * binding on an element that already has a binding will remove the
619 * old binding.
621 * @param aBinding The binding to bind to this content. If nullptr is
622 * provided as the argument, then existing binding will be
623 * removed.
625 * @param aOldBindingManager The old binding manager that contains
626 * this content if this content was adopted
627 * to another document.
629 virtual void SetXBLBinding(nsXBLBinding* aBinding,
630 nsBindingManager* aOldBindingManager = nullptr) = 0;
633 * Sets the ShadowRoot binding for this element. The contents of the
634 * binding is rendered in place of this node's children.
636 * @param aShadowRoot The ShadowRoot to be bound to this element.
638 virtual void SetShadowRoot(mozilla::dom::ShadowRoot* aShadowRoot) = 0;
641 * Gets the ShadowRoot binding for this element.
643 * @return The ShadowRoot currently bound to this element.
645 virtual mozilla::dom::ShadowRoot *GetShadowRoot() const = 0;
648 * Gets the root of the node tree for this content if it is in a shadow tree.
649 * This method is called |GetContainingShadow| instead of |GetRootShadowRoot|
650 * to avoid confusion with |GetShadowRoot|.
652 * @return The ShadowRoot that is the root of the node tree.
654 virtual mozilla::dom::ShadowRoot *GetContainingShadow() const = 0;
657 * Gets an array of destination insertion points where this content
658 * is distributed by web component distribution algorithms.
659 * The array is created if it does not already exist.
661 virtual nsTArray<nsIContent*> &DestInsertionPoints() = 0;
664 * Same as DestInsertionPoints except that this method will return
665 * null if the array of destination insertion points does not already
666 * exist.
668 virtual nsTArray<nsIContent*> *GetExistingDestInsertionPoints() const = 0;
671 * Gets the insertion parent element of the XBL binding.
672 * The insertion parent is our one true parent in the transformed DOM.
674 * @return the insertion parent element.
676 virtual nsIContent *GetXBLInsertionParent() const = 0;
679 * Sets the insertion parent element of the XBL binding.
681 * @param aContent The insertion parent element.
683 virtual void SetXBLInsertionParent(nsIContent* aContent) = 0;
686 * Returns the content node that is the parent of this node in the flattened
687 * tree. For nodes that are not filtered into an insertion point, this
688 * simply returns their DOM parent in the original DOM tree.
690 * @return the flattened tree parent
692 nsIContent *GetFlattenedTreeParent() const;
695 * Gets the custom element data used by web components custom element.
696 * Custom element data is created at the first attempt to enqueue a callback.
698 * @return The custom element data or null if none.
700 virtual mozilla::dom::CustomElementData *GetCustomElementData() const = 0;
703 * Sets the custom element data, ownership of the
704 * callback data is taken by this content.
706 * @param aCallbackData The custom element data.
708 virtual void SetCustomElementData(mozilla::dom::CustomElementData* aData) = 0;
711 * API to check if this is a link that's traversed in response to user input
712 * (e.g. a click event). Specializations for HTML/SVG/generic XML allow for
713 * different types of link in different types of content.
715 * @param aURI Required out param. If this content is a link, a new nsIURI
716 * set to this link's URI will be passed out.
718 * @note The out param, aURI, is guaranteed to be set to a non-null pointer
719 * when the return value is true.
721 * XXXjwatt: IMO IsInteractiveLink would be a better name.
723 virtual bool IsLink(nsIURI** aURI) const = 0;
726 * Get a pointer to the full href URI (fully resolved and canonicalized,
727 * since it's an nsIURI object) for link elements.
729 * @return A pointer to the URI or null if the element is not a link or it
730 * has no HREF attribute.
732 virtual already_AddRefed<nsIURI> GetHrefURI() const
734 return nullptr;
738 * This method is called when the parser finishes creating the element. This
739 * particularly means that it has done everything you would expect it to have
740 * done after it encounters the > at the end of the tag (for HTML or XML).
741 * This includes setting the attributes, setting the document / form, and
742 * placing the element into the tree at its proper place.
744 * For container elements, this is called *before* any of the children are
745 * created or added into the tree.
747 * NOTE: this is currently only called for input and button, in the HTML
748 * content sink. If you want to call it on your element, modify the content
749 * sink of your choice to do so. This is an efficiency measure.
751 * If you also need to determine whether the parser is the one creating your
752 * element (through createElement() or cloneNode() generally) then add a
753 * uint32_t aFromParser to the NS_NewXXX() constructor for your element and
754 * have the parser pass the appropriate flags. See HTMLInputElement.cpp and
755 * nsHTMLContentSink::MakeContentObject().
757 * DO NOT USE THIS METHOD to get around the fact that it's hard to deal with
758 * attributes dynamically. If you make attributes affect your element from
759 * this method, it will only happen on initialization and JavaScript will not
760 * be able to create elements (which requires them to first create the
761 * element and then call setAttribute() directly, at which point
762 * DoneCreatingElement() has already been called and is out of the picture).
764 virtual void DoneCreatingElement()
769 * This method is called when the parser begins creating the element's
770 * children, if any are present.
772 * This is only called for XTF elements currently.
774 virtual void BeginAddingChildren()
779 * This method is called when the parser finishes creating the element's children,
780 * if any are present.
782 * NOTE: this is currently only called for textarea, select, applet, and
783 * object elements in the HTML content sink. If you want
784 * to call it on your element, modify the content sink of your
785 * choice to do so. This is an efficiency measure.
787 * If you also need to determine whether the parser is the one creating your
788 * element (through createElement() or cloneNode() generally) then add a
789 * boolean aFromParser to the NS_NewXXX() constructor for your element and
790 * have the parser pass true. See HTMLInputElement.cpp and
791 * nsHTMLContentSink::MakeContentObject().
793 * @param aHaveNotified Whether there has been a
794 * ContentInserted/ContentAppended notification for this content node
795 * yet.
797 virtual void DoneAddingChildren(bool aHaveNotified)
802 * For HTML textarea, select, applet, and object elements, returns
803 * true if all children have been added OR if the element was not
804 * created by the parser. Returns true for all other elements.
805 * @returns false if the element was created by the parser and
806 * it is an HTML textarea, select, applet, or object
807 * element and not all children have been added.
808 * @returns true otherwise.
810 virtual bool IsDoneAddingChildren()
812 return true;
816 * Get the ID of this content node (the atom corresponding to the
817 * value of the id attribute). This may be null if there is no ID.
819 nsIAtom* GetID() const {
820 if (HasID()) {
821 return DoGetID();
823 return nullptr;
827 * Get the class list of this content node (this corresponds to the
828 * value of the class attribute). This may be null if there are no
829 * classes, but that's not guaranteed.
831 const nsAttrValue* GetClasses() const {
832 if (HasFlag(NODE_MAY_HAVE_CLASS)) {
833 return DoGetClasses();
835 return nullptr;
839 * Walk aRuleWalker over the content style rules (presentational
840 * hint rules) for this content node.
842 NS_IMETHOD WalkContentStyleRules(nsRuleWalker* aRuleWalker) = 0;
845 * Should be called when the node can become editable or when it can stop
846 * being editable (for example when its contentEditable attribute changes,
847 * when it is moved into an editable parent, ...). If aNotify is true and
848 * the node is an element, this will notify the state change.
850 virtual void UpdateEditableState(bool aNotify);
853 * Destroy this node and its children. Ideally this shouldn't be needed
854 * but for now we need to do it to break cycles.
856 virtual void DestroyContent() = 0;
859 * Saves the form state of this node and its children.
861 virtual void SaveSubtreeState() = 0;
864 * Getter and setter for our primary frame pointer. This is the frame that
865 * is most closely associated with the content. A frame is more closely
866 * associated with the content than another frame if the one frame contains
867 * directly or indirectly the other frame (e.g., when a frame is scrolled
868 * there is a scroll frame that contains the frame being scrolled). This
869 * frame is always the first continuation.
871 * In the case of absolutely positioned elements and floated elements, this
872 * frame is the out of flow frame, not the placeholder.
874 nsIFrame* GetPrimaryFrame() const
876 return (IsInDoc() || IsInShadowTree()) ? mPrimaryFrame : nullptr;
878 void SetPrimaryFrame(nsIFrame* aFrame) {
879 MOZ_ASSERT(IsInDoc() || IsInShadowTree(), "This will end badly!");
880 NS_PRECONDITION(!aFrame || !mPrimaryFrame || aFrame == mPrimaryFrame,
881 "Losing track of existing primary frame");
882 mPrimaryFrame = aFrame;
885 nsresult LookupNamespaceURIInternal(const nsAString& aNamespacePrefix,
886 nsAString& aNamespaceURI) const;
889 * If this content has independent selection, e.g., if this is input field
890 * or textarea, this return TRUE. Otherwise, false.
892 bool HasIndependentSelection();
895 * If the content is a part of HTML editor, this returns editing
896 * host content. When the content is in designMode, this returns its body
897 * element. Also, when the content isn't editable, this returns null.
899 mozilla::dom::Element* GetEditingHost();
902 * Determing language. Look at the nearest ancestor element that has a lang
903 * attribute in the XML namespace or is an HTML/SVG element and has a lang in
904 * no namespace attribute.
906 void GetLang(nsAString& aResult) const {
907 for (const nsIContent* content = this; content; content = content->GetParent()) {
908 if (content->GetAttrCount() > 0) {
909 // xml:lang has precedence over lang on HTML elements (see
910 // XHTML1 section C.7).
911 bool hasAttr = content->GetAttr(kNameSpaceID_XML, nsGkAtoms::lang,
912 aResult);
913 if (!hasAttr && (content->IsHTML() || content->IsSVG())) {
914 hasAttr = content->GetAttr(kNameSpaceID_None, nsGkAtoms::lang,
915 aResult);
917 NS_ASSERTION(hasAttr || aResult.IsEmpty(),
918 "GetAttr that returns false should not make string non-empty");
919 if (hasAttr) {
920 return;
926 // Overloaded from nsINode
927 virtual already_AddRefed<nsIURI> GetBaseURI(bool aTryUseXHRDocBaseURI = false) const MOZ_OVERRIDE;
929 virtual nsresult PreHandleEvent(
930 mozilla::EventChainPreVisitor& aVisitor) MOZ_OVERRIDE;
932 virtual bool IsPurple() = 0;
933 virtual void RemovePurple() = 0;
935 virtual bool OwnedOnlyByTheDOMTree() { return false; }
936 protected:
938 * Hook for implementing GetID. This is guaranteed to only be
939 * called if HasID() is true.
941 nsIAtom* DoGetID() const;
943 private:
945 * Hook for implementing GetClasses. This is guaranteed to only be
946 * called if the NODE_MAY_HAVE_CLASS flag is set.
948 const nsAttrValue* DoGetClasses() const;
950 public:
951 #ifdef DEBUG
953 * List the content (and anything it contains) out to the given
954 * file stream. Use aIndent as the base indent during formatting.
956 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0;
959 * Dump the content (and anything it contains) out to the given
960 * file stream. Use aIndent as the base indent during formatting.
962 virtual void DumpContent(FILE* out = stdout, int32_t aIndent = 0,
963 bool aDumpAll = true) const = 0;
964 #endif
967 * Append to aOutDescription a short (preferably one line) string
968 * describing the content.
969 * Currently implemented for elements only.
971 virtual void Describe(nsAString& aOutDescription) const {
972 aOutDescription = NS_LITERAL_STRING("(not an element)");
975 enum ETabFocusType {
976 //eTabFocus_textControlsMask = (1<<0), // unused - textboxes always tabbable
977 eTabFocus_formElementsMask = (1<<1), // non-text form elements
978 eTabFocus_linksMask = (1<<2), // links
979 eTabFocus_any = 1 + (1<<1) + (1<<2) // everything that can be focused
982 // Tab focus model bit field:
983 static int32_t sTabFocusModel;
985 // accessibility.tabfocus_applies_to_xul pref - if it is set to true,
986 // the tabfocus bit field applies to xul elements.
987 static bool sTabFocusModelAppliesToXUL;
990 NS_DEFINE_STATIC_IID_ACCESSOR(nsIContent, NS_ICONTENT_IID)
992 inline nsIContent* nsINode::AsContent()
994 MOZ_ASSERT(IsContent());
995 return static_cast<nsIContent*>(this);
998 #define NS_IMPL_FROMCONTENT_HELPER(_class, _check) \
999 static _class* FromContent(nsIContent* aContent) \
1001 return aContent->_check ? static_cast<_class*>(aContent) : nullptr; \
1003 static _class* FromContentOrNull(nsIContent* aContent) \
1005 return aContent ? FromContent(aContent) : nullptr; \
1008 #define NS_IMPL_FROMCONTENT(_class, _nsid) \
1009 NS_IMPL_FROMCONTENT_HELPER(_class, IsInNamespace(_nsid))
1011 #define NS_IMPL_FROMCONTENT_WITH_TAG(_class, _nsid, _tag) \
1012 NS_IMPL_FROMCONTENT_HELPER(_class, NodeInfo()->Equals(nsGkAtoms::_tag, _nsid))
1014 #define NS_IMPL_FROMCONTENT_HTML_WITH_TAG(_class, _tag) \
1015 NS_IMPL_FROMCONTENT_WITH_TAG(_class, kNameSpaceID_XHTML, _tag)
1017 #endif /* nsIContent_h___ */