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/. */
8 * Implementation of DOM Core's DocumentFragment.
11 #include "mozilla/dom/DocumentFragment.h"
12 #include "mozilla/dom/Element.h" // for NS_IMPL_ELEMENT_CLONE
13 #include "mozilla/dom/NodeInfo.h"
14 #include "nsNodeInfoManager.h"
16 #include "nsGkAtoms.h"
17 #include "nsDOMString.h"
18 #include "nsContentUtils.h" // for NS_INTERFACE_MAP_ENTRY_TEAROFF
19 #include "mozilla/dom/DocumentFragmentBinding.h"
20 #include "nsPIDOMWindow.h"
21 #include "mozilla/dom/Document.h"
22 #include "mozilla/IntegerPrintfMacros.h"
24 namespace mozilla::dom
{
26 JSObject
* DocumentFragment::WrapNode(JSContext
* aCx
,
27 JS::Handle
<JSObject
*> aGivenProto
) {
28 return DocumentFragment_Binding::Wrap(aCx
, this, aGivenProto
);
31 bool DocumentFragment::IsNodeOfType(uint32_t aFlags
) const { return false; }
34 void DocumentFragment::List(FILE* out
, int32_t aIndent
) const {
36 for (indent
= aIndent
; --indent
>= 0;) {
40 fprintf(out
, "DocumentFragment@%p", (void*)this);
42 fprintf(out
, " flags=[%08x]", static_cast<unsigned int>(GetFlags()));
43 fprintf(out
, " refcount=%" PRIuPTR
"<", mRefCnt
.get());
45 nsIContent
* child
= GetFirstChild();
49 for (; child
; child
= child
->GetNextSibling()) {
50 child
->List(out
, aIndent
+ 1);
53 for (indent
= aIndent
; --indent
>= 0;) {
61 void DocumentFragment::DumpContent(FILE* out
, int32_t aIndent
,
62 bool aDumpAll
) const {
64 for (indent
= aIndent
; --indent
>= 0;) {
68 fputs("<DocumentFragment>", out
);
74 for (nsIContent
* child
= GetFirstChild(); child
;
75 child
= child
->GetNextSibling()) {
76 int32_t indent
= aIndent
? aIndent
+ 1 : 0;
77 child
->DumpContent(out
, indent
, aDumpAll
);
79 for (indent
= aIndent
; --indent
>= 0;) {
82 fputs("</DocumentFragment>", out
);
91 already_AddRefed
<DocumentFragment
> DocumentFragment::Constructor(
92 const GlobalObject
& aGlobal
, ErrorResult
& aRv
) {
93 nsCOMPtr
<nsPIDOMWindowInner
> window
=
94 do_QueryInterface(aGlobal
.GetAsSupports());
95 if (!window
|| !window
->GetDoc()) {
96 aRv
.Throw(NS_ERROR_FAILURE
);
100 return window
->GetDoc()->CreateDocumentFragment();
103 NS_IMPL_CYCLE_COLLECTION_INHERITED(DocumentFragment
, FragmentOrElement
, mHost
)
105 // QueryInterface implementation for DocumentFragment
106 NS_INTERFACE_MAP_BEGIN(DocumentFragment
)
107 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
108 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(DocumentFragment
)
109 NS_INTERFACE_MAP_ENTRY(nsIContent
)
110 NS_INTERFACE_MAP_ENTRY(nsINode
)
111 NS_INTERFACE_MAP_ENTRY(mozilla::dom::EventTarget
)
112 NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference
,
113 new nsNodeSupportsWeakRefTearoff(this))
114 // DOM bindings depend on the identity pointer being the
115 // same as nsINode (which nsIContent inherits).
116 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIContent
)
119 NS_IMPL_ADDREF_INHERITED(DocumentFragment
, FragmentOrElement
)
120 NS_IMPL_RELEASE_INHERITED(DocumentFragment
, FragmentOrElement
)
122 NS_IMPL_ELEMENT_CLONE(DocumentFragment
)
124 } // namespace mozilla::dom