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
,
93 const nsAString
& aFetchPriority
) override
;
95 // nsXMLContentSink overrides
96 virtual nsresult
MaybeProcessXSLTLink(
97 ProcessingInstruction
* aProcessingInstruction
, const nsAString
& aHref
,
98 bool aAlternate
, const nsAString
& aTitle
, const nsAString
& aType
,
99 const nsAString
& aMedia
, const nsAString
& aReferrerPolicy
,
100 bool* aWasXSLT
= nullptr) override
;
102 nsCOMPtr
<Document
> mTargetDocument
;
104 RefPtr
<DocumentFragment
> mRoot
;
108 static nsresult
NewXMLFragmentContentSinkHelper(
109 nsIFragmentContentSink
** aResult
) {
110 nsXMLFragmentContentSink
* it
= new nsXMLFragmentContentSink();
112 NS_ADDREF(*aResult
= it
);
117 nsresult
NS_NewXMLFragmentContentSink(nsIFragmentContentSink
** aResult
) {
118 return NewXMLFragmentContentSinkHelper(aResult
);
121 nsXMLFragmentContentSink::nsXMLFragmentContentSink() : mParseError(false) {
122 mRunsToCompletion
= true;
125 nsXMLFragmentContentSink::~nsXMLFragmentContentSink() = default;
127 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXMLFragmentContentSink
)
128 NS_INTERFACE_MAP_ENTRY(nsIFragmentContentSink
)
129 NS_INTERFACE_MAP_END_INHERITING(nsXMLContentSink
)
131 NS_IMPL_ADDREF_INHERITED(nsXMLFragmentContentSink
, nsXMLContentSink
)
132 NS_IMPL_RELEASE_INHERITED(nsXMLFragmentContentSink
, nsXMLContentSink
)
134 NS_IMPL_CYCLE_COLLECTION_INHERITED(nsXMLFragmentContentSink
, nsXMLContentSink
,
135 mTargetDocument
, mRoot
)
138 nsXMLFragmentContentSink::WillBuildModel(nsDTDMode aDTDMode
) {
143 mState
= eXMLContentSinkState_InDocumentElement
;
145 NS_ASSERTION(mTargetDocument
, "Need a document!");
147 mRoot
= new (mNodeInfoManager
) DocumentFragment(mNodeInfoManager
);
153 nsXMLFragmentContentSink::DidBuildModel(bool aTerminated
) {
154 // Drop our reference to the parser to get rid of a circular
161 void nsXMLFragmentContentSink::SetDocumentCharset(
162 NotNull
<const Encoding
*> aEncoding
) {
163 MOZ_ASSERT_UNREACHABLE("fragments shouldn't set charset");
166 nsISupports
* nsXMLFragmentContentSink::GetTarget() {
167 return ToSupports(mTargetDocument
);
170 ////////////////////////////////////////////////////////////////////////
172 bool nsXMLFragmentContentSink::SetDocElement(int32_t aNameSpaceID
,
174 nsIContent
* aContent
) {
175 // this is a fragment, not a document
179 nsresult
nsXMLFragmentContentSink::CreateElement(
180 const char16_t
** aAtts
, uint32_t aAttsCount
,
181 mozilla::dom::NodeInfo
* aNodeInfo
, uint32_t aLineNumber
,
182 uint32_t aColumnNumber
, nsIContent
** aResult
, bool* aAppendContent
,
183 FromParser
/*aFromParser*/) {
184 // Claim to not be coming from parser, since we don't do any of the
185 // fancy CloseElement stuff.
186 nsresult rv
= nsXMLContentSink::CreateElement(
187 aAtts
, aAttsCount
, aNodeInfo
, aLineNumber
, aColumnNumber
, aResult
,
188 aAppendContent
, NOT_FROM_PARSER
);
190 // When we aren't grabbing all of the content we, never open a doc
191 // element, we run into trouble on the first element, so we don't append,
192 // and simply push this onto the content stack.
193 if (mContentStack
.Length() == 0) {
194 *aAppendContent
= false;
200 nsresult
nsXMLFragmentContentSink::CloseElement(nsIContent
* aContent
) {
201 // don't do fancy stuff in nsXMLContentSink
202 if (mPreventScriptExecution
&& (aContent
->IsHTMLElement(nsGkAtoms::script
) ||
203 aContent
->IsSVGElement(nsGkAtoms::script
))) {
204 nsCOMPtr
<nsIScriptElement
> sele
= do_QueryInterface(aContent
);
206 sele
->PreventExecution();
208 NS_ASSERTION(nsNameSpaceManager::GetInstance()->mSVGDisabled
,
209 "Script did QI correctly, but wasn't a disabled SVG!");
215 void nsXMLFragmentContentSink::MaybeStartLayout(bool aIgnorePendingSheets
) {}
217 ////////////////////////////////////////////////////////////////////////
220 nsXMLFragmentContentSink::HandleDoctypeDecl(const nsAString
& aSubset
,
221 const nsAString
& aName
,
222 const nsAString
& aSystemId
,
223 const nsAString
& aPublicId
,
224 nsISupports
* aCatalogData
) {
225 MOZ_ASSERT_UNREACHABLE("fragments shouldn't have doctype declarations");
231 nsXMLFragmentContentSink::HandleProcessingInstruction(const char16_t
* aTarget
,
232 const char16_t
* aData
) {
235 const nsDependentString
target(aTarget
);
236 const nsDependentString
data(aData
);
238 RefPtr
<ProcessingInstruction
> node
=
239 NS_NewXMLProcessingInstruction(mNodeInfoManager
, target
, data
);
241 // no special processing here. that should happen when the fragment moves
243 return AddContentAsLeaf(node
);
247 nsXMLFragmentContentSink::HandleXMLDeclaration(const char16_t
* aVersion
,
248 const char16_t
* aEncoding
,
249 int32_t aStandalone
) {
250 MOZ_ASSERT_UNREACHABLE("fragments shouldn't have XML declarations");
255 nsXMLFragmentContentSink::ReportError(const char16_t
* aErrorText
,
256 const char16_t
* aSourceText
,
257 nsIScriptError
* aError
, bool* _retval
) {
258 MOZ_ASSERT(aError
&& aSourceText
&& aErrorText
, "Check arguments!!!");
260 // The expat driver should report the error.
266 // Report the error to stderr.
267 fprintf(stderr
, "\n%s\n%s\n\n", NS_LossyConvertUTF16toASCII(aErrorText
).get(),
268 NS_LossyConvertUTF16toASCII(aSourceText
).get());
271 // The following code is similar to the cleanup in
272 // nsXMLContentSink::ReportError()
273 mState
= eXMLContentSinkState_InProlog
;
275 // Clear the current content
276 while (mRoot
->GetLastChild()) {
277 mRoot
->GetLastChild()->Remove();
280 // Clear any buffered-up text we have. It's enough to set the length to 0.
281 // The buffer itself is allocated when we're created and deleted in our
282 // destructor, so don't mess with it.
288 nsresult
nsXMLFragmentContentSink::ProcessStyleLinkFromHeader(
289 const nsAString
& aHref
, bool aAlternate
, const nsAString
& aTitle
,
290 const nsAString
& aIntegrity
, const nsAString
& aType
,
291 const nsAString
& aMedia
, const nsAString
& aReferrerPolicy
,
292 const nsAString
& aFetchPriority
)
295 MOZ_ASSERT_UNREACHABLE("Shouldn't have headers for a fragment sink");
299 nsresult
nsXMLFragmentContentSink::MaybeProcessXSLTLink(
300 ProcessingInstruction
* aProcessingInstruction
, const nsAString
& aHref
,
301 bool aAlternate
, const nsAString
& aTitle
, const nsAString
& aType
,
302 const nsAString
& aMedia
, const nsAString
& aReferrerPolicy
, bool* aWasXSLT
) {
303 MOZ_ASSERT(!aWasXSLT
, "Our one caller doesn't care about whether we're XSLT");
307 ////////////////////////////////////////////////////////////////////////
310 nsXMLFragmentContentSink::FinishFragmentParsing(DocumentFragment
** aFragment
) {
311 mTargetDocument
= nullptr;
312 mNodeInfoManager
= nullptr;
313 mScriptLoader
= nullptr;
314 mCSSLoader
= nullptr;
315 mContentStack
.Clear();
316 mDocumentURI
= nullptr;
318 mDocElement
= nullptr;
319 mCurrentHead
= nullptr;
321 // XXX PARSE_ERR from DOM3 Load and Save would be more appropriate
324 *aFragment
= nullptr;
325 return NS_ERROR_DOM_SYNTAX_ERR
;
328 mRoot
.forget(aFragment
);
333 nsXMLFragmentContentSink::SetTargetDocument(Document
* aTargetDocument
) {
334 NS_ENSURE_ARG_POINTER(aTargetDocument
);
336 mTargetDocument
= aTargetDocument
;
337 mNodeInfoManager
= aTargetDocument
->NodeInfoManager();
343 nsXMLFragmentContentSink::WillBuildContent() {
350 nsXMLFragmentContentSink::DidBuildContent() {
351 // Note: we need to FlushText() here because if we don't, we might not get
352 // an end element to do it for us, so make sure.
362 nsXMLFragmentContentSink::DidProcessATokenImpl() { return NS_OK
; }
365 nsXMLFragmentContentSink::IgnoreFirstContainer() {
366 MOZ_ASSERT_UNREACHABLE("XML isn't as broken as HTML");
367 return NS_ERROR_FAILURE
;
371 nsXMLFragmentContentSink::SetPreventScriptExecution(bool aPrevent
) {
372 mPreventScriptExecution
= aPrevent
;