Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / xml / nsXMLFragmentContentSink.cpp
blobddf96793e4c825f4421bb4d6c97a8850f62af0c0
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/. */
7 #include "nsCOMPtr.h"
8 #include "nsXMLContentSink.h"
9 #include "nsIFragmentContentSink.h"
10 #include "nsIXMLContentSink.h"
11 #include "nsContentSink.h"
12 #include "nsIExpatSink.h"
13 #include "nsIDTD.h"
14 #include "mozilla/dom/Document.h"
15 #include "nsIContent.h"
16 #include "nsGkAtoms.h"
17 #include "mozilla/dom/NodeInfo.h"
18 #include "nsContentCreatorFunctions.h"
19 #include "nsError.h"
20 #include "nsIDocShell.h"
21 #include "nsIScriptError.h"
22 #include "nsTHashtable.h"
23 #include "nsHashKeys.h"
24 #include "nsTArray.h"
25 #include "nsCycleCollectionParticipant.h"
26 #include "mozilla/css/Loader.h"
27 #include "mozilla/dom/DocumentFragment.h"
28 #include "mozilla/dom/ProcessingInstruction.h"
29 #include "mozilla/dom/ScriptLoader.h"
31 using namespace mozilla::dom;
33 class nsXMLFragmentContentSink : public nsXMLContentSink,
34 public nsIFragmentContentSink {
35 public:
36 nsXMLFragmentContentSink();
38 // nsISupports
39 NS_DECL_ISUPPORTS_INHERITED
40 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsXMLFragmentContentSink,
41 nsXMLContentSink)
43 // nsIExpatSink
44 NS_IMETHOD HandleDoctypeDecl(const nsAString& aSubset, const nsAString& aName,
45 const nsAString& aSystemId,
46 const nsAString& aPublicId,
47 nsISupports* aCatalogData) override;
48 NS_IMETHOD HandleProcessingInstruction(const char16_t* aTarget,
49 const char16_t* aData) override;
50 NS_IMETHOD HandleXMLDeclaration(const char16_t* aVersion,
51 const char16_t* aEncoding,
52 int32_t aStandalone) override;
53 NS_IMETHOD ReportError(const char16_t* aErrorText,
54 const char16_t* aSourceText, nsIScriptError* aError,
55 bool* aRetval) override;
57 // nsIContentSink
58 NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode) override;
59 NS_IMETHOD DidBuildModel(bool aTerminated) override;
60 virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override;
61 virtual nsISupports* GetTarget() override;
62 NS_IMETHOD DidProcessATokenImpl();
64 // nsIXMLContentSink
66 // nsIFragmentContentSink
67 NS_IMETHOD FinishFragmentParsing(DocumentFragment** aFragment) override;
68 NS_IMETHOD SetTargetDocument(Document* aDocument) override;
69 NS_IMETHOD WillBuildContent() override;
70 NS_IMETHOD DidBuildContent() override;
71 NS_IMETHOD IgnoreFirstContainer() override;
72 NS_IMETHOD SetPreventScriptExecution(bool aPreventScriptExecution) override;
74 protected:
75 virtual ~nsXMLFragmentContentSink();
77 virtual bool SetDocElement(int32_t aNameSpaceID, nsAtom* aTagName,
78 nsIContent* aContent) override;
79 virtual nsresult CreateElement(const char16_t** aAtts, uint32_t aAttsCount,
80 mozilla::dom::NodeInfo* aNodeInfo,
81 uint32_t aLineNumber, uint32_t aColumnNumber,
82 nsIContent** aResult, bool* aAppendContent,
83 mozilla::dom::FromParser aFromParser) override;
84 virtual nsresult CloseElement(nsIContent* aContent) override;
86 virtual void MaybeStartLayout(bool aIgnorePendingSheets) override;
88 // nsContentSink overrides
89 virtual nsresult ProcessStyleLinkFromHeader(
90 const nsAString& aHref, bool aAlternate, const nsAString& aTitle,
91 const nsAString& aIntegrity, const nsAString& aType,
92 const nsAString& aMedia, const nsAString& aReferrerPolicy) override;
94 // nsXMLContentSink overrides
95 virtual nsresult MaybeProcessXSLTLink(
96 ProcessingInstruction* aProcessingInstruction, const nsAString& aHref,
97 bool aAlternate, const nsAString& aTitle, const nsAString& aType,
98 const nsAString& aMedia, const nsAString& aReferrerPolicy,
99 bool* aWasXSLT = nullptr) override;
101 nsCOMPtr<Document> mTargetDocument;
102 // the fragment
103 RefPtr<DocumentFragment> mRoot;
104 bool mParseError;
107 static nsresult NewXMLFragmentContentSinkHelper(
108 nsIFragmentContentSink** aResult) {
109 nsXMLFragmentContentSink* it = new nsXMLFragmentContentSink();
111 NS_ADDREF(*aResult = it);
113 return NS_OK;
116 nsresult NS_NewXMLFragmentContentSink(nsIFragmentContentSink** aResult) {
117 return NewXMLFragmentContentSinkHelper(aResult);
120 nsXMLFragmentContentSink::nsXMLFragmentContentSink() : mParseError(false) {
121 mRunsToCompletion = true;
124 nsXMLFragmentContentSink::~nsXMLFragmentContentSink() = default;
126 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXMLFragmentContentSink)
127 NS_INTERFACE_MAP_ENTRY(nsIFragmentContentSink)
128 NS_INTERFACE_MAP_END_INHERITING(nsXMLContentSink)
130 NS_IMPL_ADDREF_INHERITED(nsXMLFragmentContentSink, nsXMLContentSink)
131 NS_IMPL_RELEASE_INHERITED(nsXMLFragmentContentSink, nsXMLContentSink)
133 NS_IMPL_CYCLE_COLLECTION_INHERITED(nsXMLFragmentContentSink, nsXMLContentSink,
134 mTargetDocument, mRoot)
136 NS_IMETHODIMP
137 nsXMLFragmentContentSink::WillBuildModel(nsDTDMode aDTDMode) {
138 if (mRoot) {
139 return NS_OK;
142 mState = eXMLContentSinkState_InDocumentElement;
144 NS_ASSERTION(mTargetDocument, "Need a document!");
146 mRoot = new (mNodeInfoManager) DocumentFragment(mNodeInfoManager);
148 return NS_OK;
151 NS_IMETHODIMP
152 nsXMLFragmentContentSink::DidBuildModel(bool aTerminated) {
153 // Drop our reference to the parser to get rid of a circular
154 // reference.
155 mParser = nullptr;
157 return NS_OK;
160 void nsXMLFragmentContentSink::SetDocumentCharset(
161 NotNull<const Encoding*> aEncoding) {
162 MOZ_ASSERT_UNREACHABLE("fragments shouldn't set charset");
165 nsISupports* nsXMLFragmentContentSink::GetTarget() {
166 return ToSupports(mTargetDocument);
169 ////////////////////////////////////////////////////////////////////////
171 bool nsXMLFragmentContentSink::SetDocElement(int32_t aNameSpaceID,
172 nsAtom* aTagName,
173 nsIContent* aContent) {
174 // this is a fragment, not a document
175 return false;
178 nsresult nsXMLFragmentContentSink::CreateElement(
179 const char16_t** aAtts, uint32_t aAttsCount,
180 mozilla::dom::NodeInfo* aNodeInfo, uint32_t aLineNumber,
181 uint32_t aColumnNumber, nsIContent** aResult, bool* aAppendContent,
182 FromParser /*aFromParser*/) {
183 // Claim to not be coming from parser, since we don't do any of the
184 // fancy CloseElement stuff.
185 nsresult rv = nsXMLContentSink::CreateElement(
186 aAtts, aAttsCount, aNodeInfo, aLineNumber, aColumnNumber, aResult,
187 aAppendContent, NOT_FROM_PARSER);
189 // When we aren't grabbing all of the content we, never open a doc
190 // element, we run into trouble on the first element, so we don't append,
191 // and simply push this onto the content stack.
192 if (mContentStack.Length() == 0) {
193 *aAppendContent = false;
196 return rv;
199 nsresult nsXMLFragmentContentSink::CloseElement(nsIContent* aContent) {
200 // don't do fancy stuff in nsXMLContentSink
201 if (mPreventScriptExecution && (aContent->IsHTMLElement(nsGkAtoms::script) ||
202 aContent->IsSVGElement(nsGkAtoms::script))) {
203 nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(aContent);
204 if (sele) {
205 sele->PreventExecution();
206 } else {
207 NS_ASSERTION(nsNameSpaceManager::GetInstance()->mSVGDisabled,
208 "Script did QI correctly, but wasn't a disabled SVG!");
211 return NS_OK;
214 void nsXMLFragmentContentSink::MaybeStartLayout(bool aIgnorePendingSheets) {}
216 ////////////////////////////////////////////////////////////////////////
218 NS_IMETHODIMP
219 nsXMLFragmentContentSink::HandleDoctypeDecl(const nsAString& aSubset,
220 const nsAString& aName,
221 const nsAString& aSystemId,
222 const nsAString& aPublicId,
223 nsISupports* aCatalogData) {
224 MOZ_ASSERT_UNREACHABLE("fragments shouldn't have doctype declarations");
226 return NS_OK;
229 NS_IMETHODIMP
230 nsXMLFragmentContentSink::HandleProcessingInstruction(const char16_t* aTarget,
231 const char16_t* aData) {
232 FlushText();
234 const nsDependentString target(aTarget);
235 const nsDependentString data(aData);
237 RefPtr<ProcessingInstruction> node =
238 NS_NewXMLProcessingInstruction(mNodeInfoManager, target, data);
240 // no special processing here. that should happen when the fragment moves
241 // into the document
242 return AddContentAsLeaf(node);
245 NS_IMETHODIMP
246 nsXMLFragmentContentSink::HandleXMLDeclaration(const char16_t* aVersion,
247 const char16_t* aEncoding,
248 int32_t aStandalone) {
249 MOZ_ASSERT_UNREACHABLE("fragments shouldn't have XML declarations");
250 return NS_OK;
253 NS_IMETHODIMP
254 nsXMLFragmentContentSink::ReportError(const char16_t* aErrorText,
255 const char16_t* aSourceText,
256 nsIScriptError* aError, bool* _retval) {
257 MOZ_ASSERT(aError && aSourceText && aErrorText, "Check arguments!!!");
259 // The expat driver should report the error.
260 *_retval = true;
262 mParseError = true;
264 #ifdef DEBUG
265 // Report the error to stderr.
266 fprintf(stderr, "\n%s\n%s\n\n", NS_LossyConvertUTF16toASCII(aErrorText).get(),
267 NS_LossyConvertUTF16toASCII(aSourceText).get());
268 #endif
270 // The following code is similar to the cleanup in
271 // nsXMLContentSink::ReportError()
272 mState = eXMLContentSinkState_InProlog;
274 // Clear the current content
275 while (mRoot->GetLastChild()) {
276 mRoot->GetLastChild()->Remove();
279 // Clear any buffered-up text we have. It's enough to set the length to 0.
280 // The buffer itself is allocated when we're created and deleted in our
281 // destructor, so don't mess with it.
282 mTextLength = 0;
284 return NS_OK;
287 nsresult nsXMLFragmentContentSink::ProcessStyleLinkFromHeader(
288 const nsAString& aHref, bool aAlternate, const nsAString& aTitle,
289 const nsAString& aIntegrity, const nsAString& aType,
290 const nsAString& aMedia, const nsAString& aReferrerPolicy)
293 MOZ_ASSERT_UNREACHABLE("Shouldn't have headers for a fragment sink");
294 return NS_OK;
297 nsresult nsXMLFragmentContentSink::MaybeProcessXSLTLink(
298 ProcessingInstruction* aProcessingInstruction, const nsAString& aHref,
299 bool aAlternate, const nsAString& aTitle, const nsAString& aType,
300 const nsAString& aMedia, const nsAString& aReferrerPolicy, bool* aWasXSLT) {
301 MOZ_ASSERT(!aWasXSLT, "Our one caller doesn't care about whether we're XSLT");
302 return NS_OK;
305 ////////////////////////////////////////////////////////////////////////
307 NS_IMETHODIMP
308 nsXMLFragmentContentSink::FinishFragmentParsing(DocumentFragment** aFragment) {
309 mTargetDocument = nullptr;
310 mNodeInfoManager = nullptr;
311 mScriptLoader = nullptr;
312 mCSSLoader = nullptr;
313 mContentStack.Clear();
314 mDocumentURI = nullptr;
315 mDocShell = nullptr;
316 mDocElement = nullptr;
317 mCurrentHead = nullptr;
318 if (mParseError) {
319 // XXX PARSE_ERR from DOM3 Load and Save would be more appropriate
320 mRoot = nullptr;
321 mParseError = false;
322 *aFragment = nullptr;
323 return NS_ERROR_DOM_SYNTAX_ERR;
326 mRoot.forget(aFragment);
327 return NS_OK;
330 NS_IMETHODIMP
331 nsXMLFragmentContentSink::SetTargetDocument(Document* aTargetDocument) {
332 NS_ENSURE_ARG_POINTER(aTargetDocument);
334 mTargetDocument = aTargetDocument;
335 mNodeInfoManager = aTargetDocument->NodeInfoManager();
337 return NS_OK;
340 NS_IMETHODIMP
341 nsXMLFragmentContentSink::WillBuildContent() {
342 PushContent(mRoot);
344 return NS_OK;
347 NS_IMETHODIMP
348 nsXMLFragmentContentSink::DidBuildContent() {
349 // Note: we need to FlushText() here because if we don't, we might not get
350 // an end element to do it for us, so make sure.
351 if (!mParseError) {
352 FlushText();
354 PopContent();
356 return NS_OK;
359 NS_IMETHODIMP
360 nsXMLFragmentContentSink::DidProcessATokenImpl() { return NS_OK; }
362 NS_IMETHODIMP
363 nsXMLFragmentContentSink::IgnoreFirstContainer() {
364 MOZ_ASSERT_UNREACHABLE("XML isn't as broken as HTML");
365 return NS_ERROR_FAILURE;
368 NS_IMETHODIMP
369 nsXMLFragmentContentSink::SetPreventScriptExecution(bool aPrevent) {
370 mPreventScriptExecution = aPrevent;
371 return NS_OK;