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 #include "nsXMLContentSink.h"
9 #include "nsIFragmentContentSink.h"
10 #include "nsIXMLContentSink.h"
11 #include "nsContentSink.h"
12 #include "nsIExpatSink.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"
20 #include "nsIDocShell.h"
21 #include "nsIScriptError.h"
22 #include "nsTHashtable.h"
23 #include "nsHashKeys.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
{
36 nsXMLFragmentContentSink();
39 NS_DECL_ISUPPORTS_INHERITED
40 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsXMLFragmentContentSink
,
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
;
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();
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
;
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
;
103 RefPtr
<DocumentFragment
> mRoot
;
107 static nsresult
NewXMLFragmentContentSinkHelper(
108 nsIFragmentContentSink
** aResult
) {
109 nsXMLFragmentContentSink
* it
= new nsXMLFragmentContentSink();
111 NS_ADDREF(*aResult
= it
);
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
)
137 nsXMLFragmentContentSink::WillBuildModel(nsDTDMode aDTDMode
) {
142 mState
= eXMLContentSinkState_InDocumentElement
;
144 NS_ASSERTION(mTargetDocument
, "Need a document!");
146 mRoot
= new (mNodeInfoManager
) DocumentFragment(mNodeInfoManager
);
152 nsXMLFragmentContentSink::DidBuildModel(bool aTerminated
) {
153 // Drop our reference to the parser to get rid of a circular
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
,
173 nsIContent
* aContent
) {
174 // this is a fragment, not a document
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;
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
);
205 sele
->PreventExecution();
207 NS_ASSERTION(nsNameSpaceManager::GetInstance()->mSVGDisabled
,
208 "Script did QI correctly, but wasn't a disabled SVG!");
214 void nsXMLFragmentContentSink::MaybeStartLayout(bool aIgnorePendingSheets
) {}
216 ////////////////////////////////////////////////////////////////////////
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");
230 nsXMLFragmentContentSink::HandleProcessingInstruction(const char16_t
* aTarget
,
231 const char16_t
* aData
) {
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
242 return AddContentAsLeaf(node
);
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");
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.
265 // Report the error to stderr.
266 fprintf(stderr
, "\n%s\n%s\n\n", NS_LossyConvertUTF16toASCII(aErrorText
).get(),
267 NS_LossyConvertUTF16toASCII(aSourceText
).get());
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.
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");
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");
305 ////////////////////////////////////////////////////////////////////////
308 nsXMLFragmentContentSink::FinishFragmentParsing(DocumentFragment
** aFragment
) {
309 mTargetDocument
= nullptr;
310 mNodeInfoManager
= nullptr;
311 mScriptLoader
= nullptr;
312 mCSSLoader
= nullptr;
313 mContentStack
.Clear();
314 mDocumentURI
= nullptr;
316 mDocElement
= nullptr;
317 mCurrentHead
= nullptr;
319 // XXX PARSE_ERR from DOM3 Load and Save would be more appropriate
322 *aFragment
= nullptr;
323 return NS_ERROR_DOM_SYNTAX_ERR
;
326 mRoot
.forget(aFragment
);
331 nsXMLFragmentContentSink::SetTargetDocument(Document
* aTargetDocument
) {
332 NS_ENSURE_ARG_POINTER(aTargetDocument
);
334 mTargetDocument
= aTargetDocument
;
335 mNodeInfoManager
= aTargetDocument
->NodeInfoManager();
341 nsXMLFragmentContentSink::WillBuildContent() {
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.
360 nsXMLFragmentContentSink::DidProcessATokenImpl() { return NS_OK
; }
363 nsXMLFragmentContentSink::IgnoreFirstContainer() {
364 MOZ_ASSERT_UNREACHABLE("XML isn't as broken as HTML");
365 return NS_ERROR_FAILURE
;
369 nsXMLFragmentContentSink::SetPreventScriptExecution(bool aPrevent
) {
370 mPreventScriptExecution
= aPrevent
;