|
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 |
|
6 /* |
|
7 * Implementation of DOM Core's nsIDOMDocumentFragment. |
|
8 */ |
|
9 |
|
10 #include "mozilla/dom/DocumentFragment.h" |
|
11 #include "mozilla/dom/Element.h" // for NS_IMPL_ELEMENT_CLONE |
|
12 #include "nsINodeInfo.h" |
|
13 #include "nsNodeInfoManager.h" |
|
14 #include "nsError.h" |
|
15 #include "nsGkAtoms.h" |
|
16 #include "nsDOMString.h" |
|
17 #include "nsContentUtils.h" // for NS_INTERFACE_MAP_ENTRY_TEAROFF |
|
18 #include "mozilla/dom/DocumentFragmentBinding.h" |
|
19 #include "nsPIDOMWindow.h" |
|
20 #include "nsIDocument.h" |
|
21 #include "mozilla/IntegerPrintfMacros.h" |
|
22 |
|
23 namespace mozilla { |
|
24 namespace dom { |
|
25 |
|
26 JSObject* |
|
27 DocumentFragment::WrapNode(JSContext *aCx) |
|
28 { |
|
29 return DocumentFragmentBinding::Wrap(aCx, this); |
|
30 } |
|
31 |
|
32 bool |
|
33 DocumentFragment::IsNodeOfType(uint32_t aFlags) const |
|
34 { |
|
35 return !(aFlags & ~(eCONTENT | eDOCUMENT_FRAGMENT)); |
|
36 } |
|
37 |
|
38 nsIAtom* |
|
39 DocumentFragment::DoGetID() const |
|
40 { |
|
41 return nullptr; |
|
42 } |
|
43 |
|
44 nsIAtom* |
|
45 DocumentFragment::GetIDAttributeName() const |
|
46 { |
|
47 return nullptr; |
|
48 } |
|
49 |
|
50 NS_IMETHODIMP |
|
51 DocumentFragment::QuerySelector(const nsAString& aSelector, |
|
52 nsIDOMElement **aReturn) |
|
53 { |
|
54 return nsINode::QuerySelector(aSelector, aReturn); |
|
55 } |
|
56 |
|
57 NS_IMETHODIMP |
|
58 DocumentFragment::QuerySelectorAll(const nsAString& aSelector, |
|
59 nsIDOMNodeList **aReturn) |
|
60 { |
|
61 return nsINode::QuerySelectorAll(aSelector, aReturn); |
|
62 } |
|
63 |
|
64 #ifdef DEBUG |
|
65 void |
|
66 DocumentFragment::List(FILE* out, int32_t aIndent) const |
|
67 { |
|
68 int32_t indent; |
|
69 for (indent = aIndent; --indent >= 0; ) { |
|
70 fputs(" ", out); |
|
71 } |
|
72 |
|
73 fprintf(out, "DocumentFragment@%p", (void *)this); |
|
74 |
|
75 fprintf(out, " flags=[%08x]", static_cast<unsigned int>(GetFlags())); |
|
76 fprintf(out, " refcount=%" PRIuPTR "<", mRefCnt.get()); |
|
77 |
|
78 nsIContent* child = GetFirstChild(); |
|
79 if (child) { |
|
80 fputs("\n", out); |
|
81 |
|
82 for (; child; child = child->GetNextSibling()) { |
|
83 child->List(out, aIndent + 1); |
|
84 } |
|
85 |
|
86 for (indent = aIndent; --indent >= 0; ) { |
|
87 fputs(" ", out); |
|
88 } |
|
89 } |
|
90 |
|
91 fputs(">\n", out); |
|
92 } |
|
93 |
|
94 void |
|
95 DocumentFragment::DumpContent(FILE* out, int32_t aIndent, |
|
96 bool aDumpAll) const |
|
97 { |
|
98 int32_t indent; |
|
99 for (indent = aIndent; --indent >= 0; ) { |
|
100 fputs(" ", out); |
|
101 } |
|
102 |
|
103 fputs("<DocumentFragment>", out); |
|
104 |
|
105 if(aIndent) { |
|
106 fputs("\n", out); |
|
107 } |
|
108 |
|
109 for (nsIContent* child = GetFirstChild(); |
|
110 child; |
|
111 child = child->GetNextSibling()) { |
|
112 int32_t indent = aIndent ? aIndent + 1 : 0; |
|
113 child->DumpContent(out, indent, aDumpAll); |
|
114 } |
|
115 for (indent = aIndent; --indent >= 0; ) { |
|
116 fputs(" ", out); |
|
117 } |
|
118 fputs("</DocumentFragment>", out); |
|
119 |
|
120 if(aIndent) { |
|
121 fputs("\n", out); |
|
122 } |
|
123 } |
|
124 #endif |
|
125 |
|
126 /* static */ already_AddRefed<DocumentFragment> |
|
127 DocumentFragment::Constructor(const GlobalObject& aGlobal, |
|
128 ErrorResult& aRv) |
|
129 { |
|
130 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports()); |
|
131 if (!window || !window->GetDoc()) { |
|
132 aRv.Throw(NS_ERROR_FAILURE); |
|
133 return nullptr; |
|
134 } |
|
135 |
|
136 return window->GetDoc()->CreateDocumentFragment(); |
|
137 } |
|
138 |
|
139 // QueryInterface implementation for DocumentFragment |
|
140 NS_INTERFACE_MAP_BEGIN(DocumentFragment) |
|
141 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
142 NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(DocumentFragment) |
|
143 NS_INTERFACE_MAP_ENTRY(nsIContent) |
|
144 NS_INTERFACE_MAP_ENTRY(nsINode) |
|
145 NS_INTERFACE_MAP_ENTRY(nsIDOMDocumentFragment) |
|
146 NS_INTERFACE_MAP_ENTRY(nsIDOMNode) |
|
147 NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget) |
|
148 NS_INTERFACE_MAP_ENTRY(mozilla::dom::EventTarget) |
|
149 NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference, |
|
150 new nsNodeSupportsWeakRefTearoff(this)) |
|
151 // DOM bindings depend on the identity pointer being the |
|
152 // same as nsINode (which nsIContent inherits). |
|
153 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent) |
|
154 NS_INTERFACE_MAP_END |
|
155 |
|
156 NS_IMPL_ADDREF_INHERITED(DocumentFragment, FragmentOrElement) |
|
157 NS_IMPL_RELEASE_INHERITED(DocumentFragment, FragmentOrElement) |
|
158 |
|
159 NS_IMPL_ELEMENT_CLONE(DocumentFragment) |
|
160 |
|
161 } // namespace dom |
|
162 } // namespace mozilla |