content/base/src/DOMImplementation.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/dom/DOMImplementation.h"
michael@0 6
michael@0 7 #include "mozilla/ContentEvents.h"
michael@0 8 #include "mozilla/dom/DOMImplementationBinding.h"
michael@0 9 #include "nsContentCreatorFunctions.h"
michael@0 10 #include "nsContentUtils.h"
michael@0 11 #include "nsDOMClassInfoID.h"
michael@0 12 #include "nsIDOMDocument.h"
michael@0 13 #include "DocumentType.h"
michael@0 14 #include "nsTextNode.h"
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace dom {
michael@0 18
michael@0 19 // QueryInterface implementation for DOMImplementation
michael@0 20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMImplementation)
michael@0 21 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 22 NS_INTERFACE_MAP_ENTRY(nsIDOMDOMImplementation)
michael@0 23 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 24 NS_INTERFACE_MAP_END
michael@0 25
michael@0 26 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DOMImplementation, mOwner)
michael@0 27
michael@0 28 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMImplementation)
michael@0 29 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMImplementation)
michael@0 30
michael@0 31 JSObject*
michael@0 32 DOMImplementation::WrapObject(JSContext* aCx)
michael@0 33 {
michael@0 34 return DOMImplementationBinding::Wrap(aCx, this);
michael@0 35 }
michael@0 36
michael@0 37 bool
michael@0 38 DOMImplementation::HasFeature(const nsAString& aFeature,
michael@0 39 const nsAString& aVersion)
michael@0 40 {
michael@0 41 return nsContentUtils::InternalIsSupported(
michael@0 42 static_cast<nsIDOMDOMImplementation*>(this),
michael@0 43 aFeature, aVersion);
michael@0 44 }
michael@0 45
michael@0 46 NS_IMETHODIMP
michael@0 47 DOMImplementation::HasFeature(const nsAString& aFeature,
michael@0 48 const nsAString& aVersion,
michael@0 49 bool* aReturn)
michael@0 50 {
michael@0 51 *aReturn = HasFeature(aFeature, aVersion);
michael@0 52 return NS_OK;
michael@0 53 }
michael@0 54
michael@0 55 already_AddRefed<DocumentType>
michael@0 56 DOMImplementation::CreateDocumentType(const nsAString& aQualifiedName,
michael@0 57 const nsAString& aPublicId,
michael@0 58 const nsAString& aSystemId,
michael@0 59 ErrorResult& aRv)
michael@0 60 {
michael@0 61 if (!mOwner) {
michael@0 62 aRv.Throw(NS_ERROR_UNEXPECTED);
michael@0 63 return nullptr;
michael@0 64 }
michael@0 65
michael@0 66 aRv = nsContentUtils::CheckQName(aQualifiedName);
michael@0 67 if (aRv.Failed()) {
michael@0 68 return nullptr;
michael@0 69 }
michael@0 70
michael@0 71 nsCOMPtr<nsIAtom> name = do_GetAtom(aQualifiedName);
michael@0 72 if (!name) {
michael@0 73 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
michael@0 74 return nullptr;
michael@0 75 }
michael@0 76
michael@0 77 // Indicate that there is no internal subset (not just an empty one)
michael@0 78 nsRefPtr<DocumentType> docType =
michael@0 79 NS_NewDOMDocumentType(mOwner->NodeInfoManager(), name, aPublicId,
michael@0 80 aSystemId, NullString(), aRv);
michael@0 81 return docType.forget();
michael@0 82 }
michael@0 83
michael@0 84 NS_IMETHODIMP
michael@0 85 DOMImplementation::CreateDocumentType(const nsAString& aQualifiedName,
michael@0 86 const nsAString& aPublicId,
michael@0 87 const nsAString& aSystemId,
michael@0 88 nsIDOMDocumentType** aReturn)
michael@0 89 {
michael@0 90 ErrorResult rv;
michael@0 91 *aReturn =
michael@0 92 CreateDocumentType(aQualifiedName, aPublicId, aSystemId, rv).take();
michael@0 93 return rv.ErrorCode();
michael@0 94 }
michael@0 95
michael@0 96 nsresult
michael@0 97 DOMImplementation::CreateDocument(const nsAString& aNamespaceURI,
michael@0 98 const nsAString& aQualifiedName,
michael@0 99 nsIDOMDocumentType* aDoctype,
michael@0 100 nsIDocument** aDocument,
michael@0 101 nsIDOMDocument** aDOMDocument)
michael@0 102 {
michael@0 103 *aDocument = nullptr;
michael@0 104 *aDOMDocument = nullptr;
michael@0 105
michael@0 106 nsresult rv;
michael@0 107 if (!aQualifiedName.IsEmpty()) {
michael@0 108 const nsAFlatString& qName = PromiseFlatString(aQualifiedName);
michael@0 109 const char16_t *colon;
michael@0 110 rv = nsContentUtils::CheckQName(qName, true, &colon);
michael@0 111 NS_ENSURE_SUCCESS(rv, rv);
michael@0 112
michael@0 113 if (colon &&
michael@0 114 (DOMStringIsNull(aNamespaceURI) ||
michael@0 115 (Substring(qName.get(), colon).EqualsLiteral("xml") &&
michael@0 116 !aNamespaceURI.EqualsLiteral("http://www.w3.org/XML/1998/namespace")))) {
michael@0 117 return NS_ERROR_DOM_NAMESPACE_ERR;
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 nsCOMPtr<nsIGlobalObject> scriptHandlingObject =
michael@0 122 do_QueryReferent(mScriptObject);
michael@0 123
michael@0 124 NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject);
michael@0 125
michael@0 126 nsCOMPtr<nsIDOMDocument> document;
michael@0 127
michael@0 128 rv = NS_NewDOMDocument(getter_AddRefs(document),
michael@0 129 aNamespaceURI, aQualifiedName, aDoctype,
michael@0 130 mDocumentURI, mBaseURI,
michael@0 131 mOwner->NodePrincipal(),
michael@0 132 true, scriptHandlingObject,
michael@0 133 DocumentFlavorLegacyGuess);
michael@0 134 NS_ENSURE_SUCCESS(rv, rv);
michael@0 135
michael@0 136 // When DOMImplementation's createDocument method is invoked with
michael@0 137 // namespace set to HTML Namespace use the registry of the associated
michael@0 138 // document to the new instance.
michael@0 139 nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
michael@0 140 if (aNamespaceURI.EqualsLiteral("http://www.w3.org/1999/xhtml")) {
michael@0 141 doc->UseRegistryFromDocument(mOwner);
michael@0 142 }
michael@0 143
michael@0 144 doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE);
michael@0 145
michael@0 146 doc.forget(aDocument);
michael@0 147 document.forget(aDOMDocument);
michael@0 148 return NS_OK;
michael@0 149 }
michael@0 150
michael@0 151 already_AddRefed<nsIDocument>
michael@0 152 DOMImplementation::CreateDocument(const nsAString& aNamespaceURI,
michael@0 153 const nsAString& aQualifiedName,
michael@0 154 nsIDOMDocumentType* aDoctype,
michael@0 155 ErrorResult& aRv)
michael@0 156 {
michael@0 157 nsCOMPtr<nsIDocument> document;
michael@0 158 nsCOMPtr<nsIDOMDocument> domDocument;
michael@0 159 aRv = CreateDocument(aNamespaceURI, aQualifiedName, aDoctype,
michael@0 160 getter_AddRefs(document), getter_AddRefs(domDocument));
michael@0 161 return document.forget();
michael@0 162 }
michael@0 163
michael@0 164 NS_IMETHODIMP
michael@0 165 DOMImplementation::CreateDocument(const nsAString& aNamespaceURI,
michael@0 166 const nsAString& aQualifiedName,
michael@0 167 nsIDOMDocumentType* aDoctype,
michael@0 168 nsIDOMDocument** aReturn)
michael@0 169 {
michael@0 170 nsCOMPtr<nsIDocument> document;
michael@0 171 return CreateDocument(aNamespaceURI, aQualifiedName, aDoctype,
michael@0 172 getter_AddRefs(document), aReturn);
michael@0 173 }
michael@0 174
michael@0 175 nsresult
michael@0 176 DOMImplementation::CreateHTMLDocument(const nsAString& aTitle,
michael@0 177 nsIDocument** aDocument,
michael@0 178 nsIDOMDocument** aDOMDocument)
michael@0 179 {
michael@0 180 *aDocument = nullptr;
michael@0 181 *aDOMDocument = nullptr;
michael@0 182
michael@0 183 NS_ENSURE_STATE(mOwner);
michael@0 184
michael@0 185 nsCOMPtr<nsIDOMDocumentType> doctype;
michael@0 186 // Indicate that there is no internal subset (not just an empty one)
michael@0 187 nsresult rv = NS_NewDOMDocumentType(getter_AddRefs(doctype),
michael@0 188 mOwner->NodeInfoManager(),
michael@0 189 nsGkAtoms::html, // aName
michael@0 190 EmptyString(), // aPublicId
michael@0 191 EmptyString(), // aSystemId
michael@0 192 NullString()); // aInternalSubset
michael@0 193 NS_ENSURE_SUCCESS(rv, rv);
michael@0 194
michael@0 195
michael@0 196 nsCOMPtr<nsIGlobalObject> scriptHandlingObject =
michael@0 197 do_QueryReferent(mScriptObject);
michael@0 198
michael@0 199 NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject);
michael@0 200
michael@0 201 nsCOMPtr<nsIDOMDocument> document;
michael@0 202 rv = NS_NewDOMDocument(getter_AddRefs(document),
michael@0 203 EmptyString(), EmptyString(),
michael@0 204 doctype, mDocumentURI, mBaseURI,
michael@0 205 mOwner->NodePrincipal(),
michael@0 206 true, scriptHandlingObject,
michael@0 207 DocumentFlavorLegacyGuess);
michael@0 208 NS_ENSURE_SUCCESS(rv, rv);
michael@0 209 nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
michael@0 210
michael@0 211 nsCOMPtr<nsIContent> root;
michael@0 212 rv = doc->CreateElem(NS_LITERAL_STRING("html"), nullptr, kNameSpaceID_XHTML,
michael@0 213 getter_AddRefs(root));
michael@0 214 NS_ENSURE_SUCCESS(rv, rv);
michael@0 215 rv = doc->AppendChildTo(root, false);
michael@0 216 NS_ENSURE_SUCCESS(rv, rv);
michael@0 217
michael@0 218 nsCOMPtr<nsIContent> head;
michael@0 219 rv = doc->CreateElem(NS_LITERAL_STRING("head"), nullptr, kNameSpaceID_XHTML,
michael@0 220 getter_AddRefs(head));
michael@0 221 NS_ENSURE_SUCCESS(rv, rv);
michael@0 222 rv = root->AppendChildTo(head, false);
michael@0 223 NS_ENSURE_SUCCESS(rv, rv);
michael@0 224
michael@0 225 if (!DOMStringIsNull(aTitle)) {
michael@0 226 nsCOMPtr<nsIContent> title;
michael@0 227 rv = doc->CreateElem(NS_LITERAL_STRING("title"), nullptr,
michael@0 228 kNameSpaceID_XHTML, getter_AddRefs(title));
michael@0 229 NS_ENSURE_SUCCESS(rv, rv);
michael@0 230 rv = head->AppendChildTo(title, false);
michael@0 231 NS_ENSURE_SUCCESS(rv, rv);
michael@0 232
michael@0 233 nsRefPtr<nsTextNode> titleText = new nsTextNode(doc->NodeInfoManager());
michael@0 234 rv = titleText->SetText(aTitle, false);
michael@0 235 NS_ENSURE_SUCCESS(rv, rv);
michael@0 236 rv = title->AppendChildTo(titleText, false);
michael@0 237 NS_ENSURE_SUCCESS(rv, rv);
michael@0 238 }
michael@0 239
michael@0 240 nsCOMPtr<nsIContent> body;
michael@0 241 rv = doc->CreateElem(NS_LITERAL_STRING("body"), nullptr, kNameSpaceID_XHTML,
michael@0 242 getter_AddRefs(body));
michael@0 243 NS_ENSURE_SUCCESS(rv, rv);
michael@0 244 rv = root->AppendChildTo(body, false);
michael@0 245 NS_ENSURE_SUCCESS(rv, rv);
michael@0 246
michael@0 247 // When the createHTMLDocument method is invoked,
michael@0 248 // use the registry of the associated document to the new instance.
michael@0 249 doc->UseRegistryFromDocument(mOwner);
michael@0 250
michael@0 251 doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE);
michael@0 252
michael@0 253 doc.forget(aDocument);
michael@0 254 document.forget(aDOMDocument);
michael@0 255 return NS_OK;
michael@0 256 }
michael@0 257
michael@0 258 already_AddRefed<nsIDocument>
michael@0 259 DOMImplementation::CreateHTMLDocument(const Optional<nsAString>& aTitle,
michael@0 260 ErrorResult& aRv)
michael@0 261 {
michael@0 262 nsCOMPtr<nsIDocument> document;
michael@0 263 nsCOMPtr<nsIDOMDocument> domDocument;
michael@0 264 aRv = CreateHTMLDocument(aTitle.WasPassed() ? aTitle.Value()
michael@0 265 : NullString(),
michael@0 266 getter_AddRefs(document),
michael@0 267 getter_AddRefs(domDocument));
michael@0 268 return document.forget();
michael@0 269 }
michael@0 270
michael@0 271 NS_IMETHODIMP
michael@0 272 DOMImplementation::CreateHTMLDocument(const nsAString& aTitle,
michael@0 273 nsIDOMDocument** aReturn)
michael@0 274 {
michael@0 275 nsCOMPtr<nsIDocument> document;
michael@0 276 return CreateHTMLDocument(aTitle, getter_AddRefs(document), aReturn);
michael@0 277 }
michael@0 278
michael@0 279 } // namespace dom
michael@0 280 } // namespace mozilla

mercurial