Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / BindContext.h
blob4ad0d7d9eb48a5ecf27ec6418b2782c355931358
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 /* State that is passed down to BindToTree. */
9 #ifndef mozilla_dom_BindContext_h__
10 #define mozilla_dom_BindContext_h__
12 #include "mozilla/Attributes.h"
13 #include "mozilla/dom/Element.h"
14 #include "mozilla/dom/ShadowRoot.h"
15 #include "nsINode.h"
17 namespace mozilla::dom {
19 class Document;
21 struct MOZ_STACK_CLASS BindContext final {
22 // The document that owns the tree we're getting bound to.
24 // This is mostly an optimization to avoid silly pointer-chases to get the
25 // OwnerDoc().
26 Document& OwnerDoc() const { return mDoc; }
28 // Whether we're getting connected.
30 // https://dom.spec.whatwg.org/#connected
31 bool InComposedDoc() const { return mInComposedDoc; }
33 // Whether we're getting bound to the document tree.
35 // https://dom.spec.whatwg.org/#in-a-document-tree
36 bool InUncomposedDoc() const { return mInUncomposedDoc; }
38 Document* GetComposedDoc() const { return mInComposedDoc ? &mDoc : nullptr; }
40 Document* GetUncomposedDoc() const {
41 return mInUncomposedDoc ? &mDoc : nullptr;
44 // Whether our subtree root is changing as a result of this operation.
45 bool SubtreeRootChanges() const { return mSubtreeRootChanges; }
47 // Autofocus is allowed only if the is in the same origin as the top level
48 // document.
49 // https://html.spec.whatwg.org/multipage/interaction.html#the-autofocus-attribute:same-origin
50 // In addition, the document should not be already loaded and the
51 // "browser.autofocus" preference should be 'true'.
52 bool AllowsAutoFocus() const;
54 // This constructor should be used for regular appends to content.
55 explicit BindContext(nsINode& aParent)
56 : mDoc(*aParent.OwnerDoc()),
57 mInComposedDoc(aParent.IsInComposedDoc()),
58 mInUncomposedDoc(aParent.IsInUncomposedDoc()),
59 mSubtreeRootChanges(true) {}
61 // When re-binding a shadow host into a tree, we re-bind all the shadow tree
62 // from the root. In that case, the shadow tree contents remain within the
63 // same subtree root. So children should avoid doing silly things like adding
64 // themselves to the ShadowRoot's id table twice or what not.
66 // This constructor is only meant to be used in that situation.
67 explicit BindContext(ShadowRoot& aShadowRoot)
68 : mDoc(*aShadowRoot.OwnerDoc()),
69 mInComposedDoc(aShadowRoot.IsInComposedDoc()),
70 mInUncomposedDoc(false),
71 mSubtreeRootChanges(false) {}
73 // This constructor is meant to be used when inserting native-anonymous
74 // children into a subtree.
75 enum ForNativeAnonymous { ForNativeAnonymous };
76 BindContext(Element& aParentElement, enum ForNativeAnonymous)
77 : mDoc(*aParentElement.OwnerDoc()),
78 mInComposedDoc(aParentElement.IsInComposedDoc()),
79 mInUncomposedDoc(aParentElement.IsInUncomposedDoc()),
80 mSubtreeRootChanges(true) {
81 MOZ_ASSERT(mInComposedDoc, "Binding NAC in a disconnected subtree?");
84 private:
85 // Returns true iff the document is in the same origin as the top level
86 // document.
87 bool IsSameOriginAsTop() const;
89 Document& mDoc;
91 const bool mInComposedDoc;
92 const bool mInUncomposedDoc;
94 // Whether the bind operation will change the subtree root of the content
95 // we're binding.
96 const bool mSubtreeRootChanges;
99 } // namespace mozilla::dom
101 #endif