BindContext.h (3783B)
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/. */ 6 7 /* State that is passed down to BindToTree. */ 8 9 #ifndef mozilla_dom_BindContext_h__ 10 #define mozilla_dom_BindContext_h__ 11 12 #include "mozilla/Attributes.h" 13 #include "mozilla/dom/Element.h" 14 #include "mozilla/dom/ShadowRoot.h" 15 #include "nsINode.h" 16 17 namespace mozilla::dom { 18 19 class Document; 20 21 struct MOZ_STACK_CLASS BindContext final { 22 // The document that owns the tree we're getting bound to. 23 // 24 // This is mostly an optimization to avoid silly pointer-chases to get the 25 // OwnerDoc(). 26 Document& OwnerDoc() const { return mDoc; } 27 28 // Whether we're getting connected. 29 // 30 // https://dom.spec.whatwg.org/#connected 31 bool InComposedDoc() const { return mInComposedDoc; } 32 33 // Whether we're getting bound to the document tree. 34 // 35 // https://dom.spec.whatwg.org/#in-a-document-tree 36 bool InUncomposedDoc() const { return mInUncomposedDoc; } 37 38 Document* GetComposedDoc() const { return mInComposedDoc ? &mDoc : nullptr; } 39 40 Document* GetUncomposedDoc() const { 41 return mInUncomposedDoc ? &mDoc : nullptr; 42 } 43 44 // Whether our subtree root is changing as a result of this operation. 45 bool SubtreeRootChanges() const { return mSubtreeRootChanges; } 46 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; 53 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) {} 60 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. 65 // 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) {} 72 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?"); 82 } 83 84 void SetIsMove(bool aIsMove) { mIsMove = aIsMove; } 85 86 bool IsMove() const { return mIsMove; } 87 88 private: 89 // Returns true iff the document is in the same origin as the top level 90 // document. 91 bool IsSameOriginAsTop() const; 92 93 Document& mDoc; 94 95 // If set, we're moving the shadow-including inclusive ancestor. 96 bool mIsMove = false; 97 98 const bool mInComposedDoc; 99 const bool mInUncomposedDoc; 100 101 // Whether the bind operation will change the subtree root of the content 102 // we're binding. 103 const bool mSubtreeRootChanges; 104 }; 105 106 } // namespace mozilla::dom 107 108 #endif