/src/mozilla-central/dom/base/DOMImplementation.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/dom/DOMImplementation.h" |
8 | | |
9 | | #include "mozilla/ContentEvents.h" |
10 | | #include "mozilla/dom/DOMImplementationBinding.h" |
11 | | #include "nsContentCreatorFunctions.h" |
12 | | #include "nsContentUtils.h" |
13 | | #include "mozilla/dom/DocumentType.h" |
14 | | #include "nsTextNode.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace dom { |
18 | | |
19 | | // QueryInterface implementation for DOMImplementation |
20 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMImplementation) |
21 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
22 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
23 | 0 | NS_INTERFACE_MAP_END |
24 | | |
25 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMImplementation, mOwner) |
26 | | |
27 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMImplementation) |
28 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMImplementation) |
29 | | |
30 | | JSObject* |
31 | | DOMImplementation::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
32 | 0 | { |
33 | 0 | return DOMImplementation_Binding::Wrap(aCx, this, aGivenProto); |
34 | 0 | } |
35 | | |
36 | | already_AddRefed<DocumentType> |
37 | | DOMImplementation::CreateDocumentType(const nsAString& aQualifiedName, |
38 | | const nsAString& aPublicId, |
39 | | const nsAString& aSystemId, |
40 | | ErrorResult& aRv) |
41 | 0 | { |
42 | 0 | if (!mOwner) { |
43 | 0 | aRv.Throw(NS_ERROR_UNEXPECTED); |
44 | 0 | return nullptr; |
45 | 0 | } |
46 | 0 | |
47 | 0 | aRv = nsContentUtils::CheckQName(aQualifiedName); |
48 | 0 | if (aRv.Failed()) { |
49 | 0 | return nullptr; |
50 | 0 | } |
51 | 0 | |
52 | 0 | RefPtr<nsAtom> name = NS_Atomize(aQualifiedName); |
53 | 0 | if (!name) { |
54 | 0 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
55 | 0 | return nullptr; |
56 | 0 | } |
57 | 0 | |
58 | 0 | // Indicate that there is no internal subset (not just an empty one) |
59 | 0 | RefPtr<DocumentType> docType = |
60 | 0 | NS_NewDOMDocumentType(mOwner->NodeInfoManager(), name, aPublicId, |
61 | 0 | aSystemId, VoidString()); |
62 | 0 | return docType.forget(); |
63 | 0 | } |
64 | | |
65 | | nsresult |
66 | | DOMImplementation::CreateDocument(const nsAString& aNamespaceURI, |
67 | | const nsAString& aQualifiedName, |
68 | | DocumentType* aDoctype, |
69 | | nsIDocument** aDocument) |
70 | 0 | { |
71 | 0 | *aDocument = nullptr; |
72 | 0 |
|
73 | 0 | nsresult rv; |
74 | 0 | if (!aQualifiedName.IsEmpty()) { |
75 | 0 | const nsString& qName = PromiseFlatString(aQualifiedName); |
76 | 0 | const char16_t *colon; |
77 | 0 | rv = nsContentUtils::CheckQName(qName, true, &colon); |
78 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
79 | 0 |
|
80 | 0 | if (colon && |
81 | 0 | (DOMStringIsNull(aNamespaceURI) || |
82 | 0 | (Substring(qName.get(), colon).EqualsLiteral("xml") && |
83 | 0 | !aNamespaceURI.EqualsLiteral("http://www.w3.org/XML/1998/namespace")))) { |
84 | 0 | return NS_ERROR_DOM_NAMESPACE_ERR; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | |
88 | 0 | nsCOMPtr<nsIGlobalObject> scriptHandlingObject = |
89 | 0 | do_QueryReferent(mScriptObject); |
90 | 0 |
|
91 | 0 | NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject); |
92 | 0 |
|
93 | 0 | nsCOMPtr<nsIDocument> doc; |
94 | 0 |
|
95 | 0 | rv = NS_NewDOMDocument(getter_AddRefs(doc), |
96 | 0 | aNamespaceURI, aQualifiedName, aDoctype, |
97 | 0 | mDocumentURI, mBaseURI, |
98 | 0 | mOwner->NodePrincipal(), |
99 | 0 | true, scriptHandlingObject, |
100 | 0 | DocumentFlavorLegacyGuess); |
101 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
102 | 0 |
|
103 | 0 | // When DOMImplementation's createDocument method is invoked with |
104 | 0 | // namespace set to HTML Namespace use the registry of the associated |
105 | 0 | // document to the new instance. |
106 | 0 |
|
107 | 0 | if (aNamespaceURI.EqualsLiteral("http://www.w3.org/1999/xhtml")) { |
108 | 0 | doc->SetContentType(NS_LITERAL_STRING("application/xhtml+xml")); |
109 | 0 | } else if (aNamespaceURI.EqualsLiteral("http://www.w3.org/2000/svg")) { |
110 | 0 | doc->SetContentType(NS_LITERAL_STRING("image/svg+xml")); |
111 | 0 | } else { |
112 | 0 | doc->SetContentType(NS_LITERAL_STRING("application/xml")); |
113 | 0 | } |
114 | 0 |
|
115 | 0 | doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE); |
116 | 0 |
|
117 | 0 | doc.forget(aDocument); |
118 | 0 | return NS_OK; |
119 | 0 | } |
120 | | |
121 | | already_AddRefed<nsIDocument> |
122 | | DOMImplementation::CreateDocument(const nsAString& aNamespaceURI, |
123 | | const nsAString& aQualifiedName, |
124 | | DocumentType* aDoctype, |
125 | | ErrorResult& aRv) |
126 | 0 | { |
127 | 0 | nsCOMPtr<nsIDocument> document; |
128 | 0 | aRv = CreateDocument(aNamespaceURI, aQualifiedName, aDoctype, |
129 | 0 | getter_AddRefs(document)); |
130 | 0 | return document.forget(); |
131 | 0 | } |
132 | | |
133 | | nsresult |
134 | | DOMImplementation::CreateHTMLDocument(const nsAString& aTitle, |
135 | | nsIDocument** aDocument) |
136 | 0 | { |
137 | 0 | *aDocument = nullptr; |
138 | 0 |
|
139 | 0 | NS_ENSURE_STATE(mOwner); |
140 | 0 |
|
141 | 0 | // Indicate that there is no internal subset (not just an empty one) |
142 | 0 | RefPtr<DocumentType> doctype = |
143 | 0 | NS_NewDOMDocumentType(mOwner->NodeInfoManager(), |
144 | 0 | nsGkAtoms::html, // aName |
145 | 0 | EmptyString(), // aPublicId |
146 | 0 | EmptyString(), // aSystemId |
147 | 0 | VoidString()); // aInternalSubset |
148 | 0 |
|
149 | 0 | nsCOMPtr<nsIGlobalObject> scriptHandlingObject = |
150 | 0 | do_QueryReferent(mScriptObject); |
151 | 0 |
|
152 | 0 | NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject); |
153 | 0 |
|
154 | 0 | nsCOMPtr<nsIDocument> doc; |
155 | 0 | nsresult rv = NS_NewDOMDocument(getter_AddRefs(doc), |
156 | 0 | EmptyString(), EmptyString(), |
157 | 0 | doctype, mDocumentURI, mBaseURI, |
158 | 0 | mOwner->NodePrincipal(), |
159 | 0 | true, scriptHandlingObject, |
160 | 0 | DocumentFlavorLegacyGuess); |
161 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
162 | 0 |
|
163 | 0 | nsCOMPtr<Element> root = doc->CreateElem(NS_LITERAL_STRING("html"), nullptr, |
164 | 0 | kNameSpaceID_XHTML); |
165 | 0 | rv = doc->AppendChildTo(root, false); |
166 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
167 | 0 |
|
168 | 0 | nsCOMPtr<Element> head = doc->CreateElem(NS_LITERAL_STRING("head"), nullptr, |
169 | 0 | kNameSpaceID_XHTML); |
170 | 0 | rv = root->AppendChildTo(head, false); |
171 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
172 | 0 |
|
173 | 0 | if (!DOMStringIsNull(aTitle)) { |
174 | 0 | nsCOMPtr<Element> title = doc->CreateElem(NS_LITERAL_STRING("title"), |
175 | 0 | nullptr, kNameSpaceID_XHTML); |
176 | 0 | rv = head->AppendChildTo(title, false); |
177 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
178 | 0 |
|
179 | 0 | RefPtr<nsTextNode> titleText = new nsTextNode(doc->NodeInfoManager()); |
180 | 0 | rv = titleText->SetText(aTitle, false); |
181 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
182 | 0 | rv = title->AppendChildTo(titleText, false); |
183 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
184 | 0 | } |
185 | 0 |
|
186 | 0 | nsCOMPtr<Element> body = doc->CreateElem(NS_LITERAL_STRING("body"), nullptr, |
187 | 0 | kNameSpaceID_XHTML); |
188 | 0 | rv = root->AppendChildTo(body, false); |
189 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
190 | 0 |
|
191 | 0 | doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE); |
192 | 0 |
|
193 | 0 | doc.forget(aDocument); |
194 | 0 | return NS_OK; |
195 | 0 | } |
196 | | |
197 | | already_AddRefed<nsIDocument> |
198 | | DOMImplementation::CreateHTMLDocument(const Optional<nsAString>& aTitle, |
199 | | ErrorResult& aRv) |
200 | 0 | { |
201 | 0 | nsCOMPtr<nsIDocument> document; |
202 | 0 | aRv = CreateHTMLDocument(aTitle.WasPassed() ? aTitle.Value() : VoidString(), |
203 | 0 | getter_AddRefs(document)); |
204 | 0 | return document.forget(); |
205 | 0 | } |
206 | | |
207 | | } // namespace dom |
208 | | } // namespace mozilla |