DocumentFragment.cpp (3632B)
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 /* 8 * Implementation of DOM Core's DocumentFragment. 9 */ 10 11 #include "mozilla/dom/DocumentFragment.h" 12 13 #include "mozilla/IntegerPrintfMacros.h" 14 #include "mozilla/dom/Document.h" 15 #include "mozilla/dom/DocumentFragmentBinding.h" 16 #include "mozilla/dom/Element.h" // for NS_IMPL_ELEMENT_CLONE 17 #include "mozilla/dom/NodeInfo.h" 18 #include "nsContentUtils.h" // for NS_INTERFACE_MAP_ENTRY_TEAROFF 19 #include "nsDOMString.h" 20 #include "nsError.h" 21 #include "nsGkAtoms.h" 22 #include "nsNodeInfoManager.h" 23 #include "nsPIDOMWindow.h" 24 25 namespace mozilla::dom { 26 27 JSObject* DocumentFragment::WrapNode(JSContext* aCx, 28 JS::Handle<JSObject*> aGivenProto) { 29 return DocumentFragment_Binding::Wrap(aCx, this, aGivenProto); 30 } 31 32 #ifdef MOZ_DOM_LIST 33 void DocumentFragment::List(FILE* out, int32_t aIndent) const { 34 int32_t indent; 35 for (indent = aIndent; --indent >= 0;) { 36 fputs(" ", out); 37 } 38 39 fprintf(out, "DocumentFragment@%p", (void*)this); 40 41 fprintf(out, " flags=[%08x]", static_cast<unsigned int>(GetFlags())); 42 fprintf(out, " refcount=%" PRIuPTR "<", mRefCnt.get()); 43 44 nsIContent* child = GetFirstChild(); 45 if (child) { 46 fputs("\n", out); 47 48 for (; child; child = child->GetNextSibling()) { 49 child->List(out, aIndent + 1); 50 } 51 52 for (indent = aIndent; --indent >= 0;) { 53 fputs(" ", out); 54 } 55 } 56 57 fputs(">\n", out); 58 } 59 60 void DocumentFragment::DumpContent(FILE* out, int32_t aIndent, 61 bool aDumpAll) const { 62 int32_t indent; 63 for (indent = aIndent; --indent >= 0;) { 64 fputs(" ", out); 65 } 66 67 fputs("<DocumentFragment>", out); 68 69 if (aIndent) { 70 fputs("\n", out); 71 } 72 73 for (nsIContent* child = GetFirstChild(); child; 74 child = child->GetNextSibling()) { 75 int32_t indent = aIndent ? aIndent + 1 : 0; 76 child->DumpContent(out, indent, aDumpAll); 77 } 78 for (indent = aIndent; --indent >= 0;) { 79 fputs(" ", out); 80 } 81 fputs("</DocumentFragment>", out); 82 83 if (aIndent) { 84 fputs("\n", out); 85 } 86 } 87 #endif 88 89 /* static */ 90 already_AddRefed<DocumentFragment> DocumentFragment::Constructor( 91 const GlobalObject& aGlobal, ErrorResult& aRv) { 92 nsCOMPtr<nsPIDOMWindowInner> window = 93 do_QueryInterface(aGlobal.GetAsSupports()); 94 if (!window || !window->GetDoc()) { 95 aRv.Throw(NS_ERROR_FAILURE); 96 return nullptr; 97 } 98 99 return window->GetDoc()->CreateDocumentFragment(); 100 } 101 102 NS_IMPL_CYCLE_COLLECTION_INHERITED(DocumentFragment, FragmentOrElement, mHost) 103 104 // QueryInterface implementation for DocumentFragment 105 NS_INTERFACE_MAP_BEGIN(DocumentFragment) 106 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 107 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(DocumentFragment) 108 NS_INTERFACE_MAP_ENTRY(nsIContent) 109 NS_INTERFACE_MAP_ENTRY(nsINode) 110 NS_INTERFACE_MAP_ENTRY(mozilla::dom::EventTarget) 111 NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference, 112 new nsNodeSupportsWeakRefTearoff(this)) 113 // DOM bindings depend on the identity pointer being the 114 // same as nsINode (which nsIContent inherits). 115 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent) 116 NS_INTERFACE_MAP_END 117 118 NS_IMPL_ADDREF_INHERITED(DocumentFragment, FragmentOrElement) 119 NS_IMPL_RELEASE_INHERITED(DocumentFragment, FragmentOrElement) 120 121 NS_IMPL_ELEMENT_CLONE(DocumentFragment) 122 123 } // namespace mozilla::dom