1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef TRANSFRMX_TXSTYLESHEETCOMPILER_H
7 #define TRANSFRMX_TXSTYLESHEETCOMPILER_H
9 #include "mozilla/Attributes.h"
10 #include "mozilla/UniquePtr.h"
12 #include "txXSLTPatterns.h"
14 #include "txIXPathContext.h"
15 #include "txStylesheet.h"
18 extern bool TX_XSLTFunctionAvailable(nsAtom
* aName
, int32_t aNameSpaceID
);
21 class txElementContext
;
22 class txInstructionContainer
;
26 class txPushNewContext
;
27 class txStylesheetCompiler
;
29 class txElementContext
: public txObject
{
31 explicit txElementContext(const nsAString
& aBaseURI
);
32 txElementContext(const txElementContext
& aOther
);
34 bool mPreserveWhitespace
;
35 bool mForwardsCompatibleParsing
;
37 RefPtr
<txNamespaceMap
> mMappings
;
38 nsTArray
<int32_t> mInstructionNamespaces
;
42 using mozilla::dom::ReferrerPolicy
;
44 class txACompileObserver
{
46 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
48 virtual nsresult
loadURI(const nsAString
& aUri
, const nsAString
& aReferrerUri
,
49 ReferrerPolicy aReferrerPolicy
,
50 txStylesheetCompiler
* aCompiler
) = 0;
51 virtual void onDoneCompiling(txStylesheetCompiler
* aCompiler
,
53 const char16_t
* aErrorText
= nullptr,
54 const char16_t
* aParam
= nullptr) = 0;
57 #define TX_DECL_ACOMPILEOBSERVER \
58 nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri, \
59 ReferrerPolicy aReferrerPolicy, \
60 txStylesheetCompiler* aCompiler) override; \
61 void onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult, \
62 const char16_t* aErrorText = nullptr, \
63 const char16_t* aParam = nullptr) override;
65 class txInScopeVariable
{
67 explicit txInScopeVariable(const txExpandedName
& aName
)
68 : mName(aName
), mLevel(1) {}
73 class txStylesheetCompilerState
: public txIParseContext
{
75 explicit txStylesheetCompilerState(txACompileObserver
* aObserver
);
76 ~txStylesheetCompilerState();
78 nsresult
init(const nsAString
& aStylesheetURI
, ReferrerPolicy aReferrerPolicy
,
79 txStylesheet
* aStylesheet
, txListIterator
* aInsertPosition
);
81 // Embedded stylesheets state
82 bool handleEmbeddedSheet() { return mEmbedStatus
== eInEmbed
; }
83 void doneEmbedding() { mEmbedStatus
= eHasEmbed
; }
97 void pushHandlerTable(txHandlerTable
* aTable
);
98 void popHandlerTable();
99 void pushSorter(txPushNewContext
* aSorter
);
101 void pushChooseGotoList();
102 void popChooseGotoList();
103 void pushObject(txObject
* aObject
);
104 txObject
* popObject();
105 void pushPtr(void* aPtr
, enumStackType aType
);
106 void* popPtr(enumStackType aType
);
108 // stylesheet functions
109 void addToplevelItem(txToplevelItem
* aItem
);
110 nsresult
openInstructionContainer(txInstructionContainer
* aContainer
);
111 void closeInstructionContainer();
112 txInstruction
* addInstruction(
113 mozilla::UniquePtr
<txInstruction
>&& aInstruction
);
115 T
* addInstruction(mozilla::UniquePtr
<T
> aInstruction
) {
116 return static_cast<T
*>(addInstruction(
117 mozilla::UniquePtr
<txInstruction
>(std::move(aInstruction
))));
119 nsresult
loadIncludedStylesheet(const nsAString
& aURI
);
120 nsresult
loadImportedStylesheet(const nsAString
& aURI
,
121 txStylesheet::ImportFrame
* aFrame
);
124 void addGotoTarget(txInstruction
** aTargetPointer
);
125 void addVariable(const txExpandedName
& aName
);
128 nsresult
resolveNamespacePrefix(nsAtom
* aPrefix
, int32_t& aID
) override
;
129 nsresult
resolveFunctionCall(nsAtom
* aName
, int32_t aID
,
130 FunctionCall
** aFunction
) override
;
131 bool caseInsensitiveNameTests() override
;
134 * Should the stylesheet be parsed in forwards compatible parsing mode.
136 bool fcp() { return mElementContext
->mForwardsCompatibleParsing
; }
138 void SetErrorOffset(uint32_t aOffset
) override
;
140 bool allowed(Allowed aAllowed
) override
{ return !(mDisAllowed
& aAllowed
); }
142 bool ignoreError(nsresult aResult
) {
143 // Some errors shouldn't be ignored even in forwards compatible parsing
145 return aResult
!= NS_ERROR_XSLT_CALL_TO_KEY_NOT_ALLOWED
&& fcp();
148 RefPtr
<txStylesheet
> mStylesheet
;
149 txHandlerTable
* mHandlerTable
;
150 mozilla::UniquePtr
<txElementContext
> mElementContext
;
151 txPushNewContext
* mSorter
;
152 mozilla::UniquePtr
<txList
> mChooseGotoList
;
154 bool mSearchingForFallback
;
155 uint16_t mDisAllowed
;
158 RefPtr
<txACompileObserver
> mObserver
;
159 nsTArray
<txInScopeVariable
> mInScopeVariables
;
160 nsTArray
<txStylesheetCompiler
*> mChildCompilerList
;
161 // embed info, target information is the ID
163 enum { eNoEmbed
, eNeedEmbed
, eInEmbed
, eHasEmbed
} mEmbedStatus
;
164 nsString mStylesheetURI
;
166 bool mDoneWithThisStylesheet
;
167 txStack mObjectStack
;
169 nsTArray
<enumStackType
> mTypeStack
;
172 mozilla::UniquePtr
<txInstruction
>* mNextInstrPtr
;
173 txListIterator mToplevelIterator
;
174 nsTArray
<txInstruction
**> mGotoTargetPointers
;
175 ReferrerPolicy mReferrerPolicy
;
178 struct txStylesheetAttr
{
179 int32_t mNamespaceID
;
180 RefPtr
<nsAtom
> mLocalName
;
181 RefPtr
<nsAtom
> mPrefix
;
185 class txStylesheetCompiler final
: private txStylesheetCompilerState
,
186 public txACompileObserver
{
188 friend class txStylesheetCompilerState
;
189 friend bool TX_XSLTFunctionAvailable(nsAtom
* aName
, int32_t aNameSpaceID
);
190 txStylesheetCompiler(const nsAString
& aStylesheetURI
,
191 ReferrerPolicy aReferrerPolicy
,
192 txACompileObserver
* aObserver
);
193 txStylesheetCompiler(const nsAString
& aStylesheetURI
,
194 txStylesheet
* aStylesheet
,
195 txListIterator
* aInsertPosition
,
196 ReferrerPolicy aReferrerPolicy
,
197 txACompileObserver
* aObserver
);
199 void setBaseURI(const nsString
& aBaseURI
);
201 nsresult
startElement(int32_t aNamespaceID
, nsAtom
* aLocalName
,
202 nsAtom
* aPrefix
, txStylesheetAttr
* aAttributes
,
204 nsresult
startElement(const char16_t
* aName
, const char16_t
** aAtts
,
206 nsresult
endElement();
207 nsresult
characters(const nsAString
& aStr
);
208 nsresult
doneLoading();
210 void cancel(nsresult aError
, const char16_t
* aErrorText
= nullptr,
211 const char16_t
* aParam
= nullptr);
213 txStylesheet
* getStylesheet();
215 TX_DECL_ACOMPILEOBSERVER
216 NS_INLINE_DECL_REFCOUNTING(txStylesheetCompiler
, override
)
219 // Private destructor, to discourage deletion outside of Release():
220 ~txStylesheetCompiler() = default;
222 nsresult
startElementInternal(int32_t aNamespaceID
, nsAtom
* aLocalName
,
223 nsAtom
* aPrefix
, txStylesheetAttr
* aAttributes
,
226 nsresult
flushCharacters();
227 nsresult
ensureNewElementContext();
228 nsresult
maybeDoneCompiling();
230 nsString mCharacters
;