1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #define NS_HTML5_TREE_BUILDER_HANDLE_ARRAY_LENGTH 512
6 private:
7 using Encoding = mozilla::Encoding;
8 template <typename T>
9 using NotNull = mozilla::NotNull<T>;
10 
11 nsHtml5OplessBuilder* mBuilder;
12 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13 // If mBuilder is not null, the tree op machinery is not in use and
14 // the fields below aren't in use, either.
15 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16 nsHtml5Highlighter* mViewSource;
17 mozilla::ImportScanner mImportScanner;
18 nsTArray<nsHtml5TreeOperation> mOpQueue;
19 nsTArray<nsHtml5SpeculativeLoad> mSpeculativeLoadQueue;
20 nsAHtml5TreeOpSink* mOpSink;
21 mozilla::UniquePtr<nsIContent*[]> mHandles;
22 int32_t mHandlesUsed;
23 nsTArray<mozilla::UniquePtr<nsIContent*[]>> mOldHandles;
24 nsHtml5TreeOpStage* mSpeculativeLoadStage;
25 nsresult mBroken;
26 bool mCurrentHtmlScriptIsAsyncOrDefer;
27 bool mPreventScriptExecution;
28 #ifdef DEBUG
29 bool mActive;
30 #endif
31 
32 // DocumentModeHandler
33 /**
34  * Tree builder uses this to report quirkiness of the document
35  */
36 void documentMode(nsHtml5DocumentMode m);
37 
38 nsIContentHandle* getDocumentFragmentForTemplate(nsIContentHandle* aTemplate);
39 
40 nsIContentHandle* getFormPointerForContext(nsIContentHandle* aContext);
41 
42 /**
43  * Using nsIContent** instead of nsIContent* is the parser deals with DOM
44  * nodes in a way that works off the main thread. Non-main-thread code
45  * can't refcount or otherwise touch nsIContent objects in any way.
46  * Yet, the off-the-main-thread code needs to have a way to hold onto a
47  * particular node and repeatedly operate on the same node.
48  *
49  * The way this works is that the off-the-main-thread code has an
50  * nsIContent** for each DOM node and a given nsIContent** is only ever
51  * actually dereferenced into an actual nsIContent* on the main thread.
52  * When the off-the-main-thread code requests a new node, it gets an
53  * nsIContent** immediately and a tree op is enqueued for later allocating
54  * an actual nsIContent object and writing a pointer to it into the memory
55  * location pointed to by the nsIContent**.
56  *
57  * Since tree ops are in a queue, the node creating tree op will always
58  * run before tree ops that try to further operate on the node that the
59  * nsIContent** is a handle to.
60  *
61  * On-the-main-thread parts of the parser use nsIContent* instead of
62  * nsIContent**. Since both cases share the same parser core, the parser
63  * core casts both to nsIContentHandle*.
64  */
65 nsIContentHandle* AllocateContentHandle();
66 
accumulateCharactersForced(const char16_t * aBuf,int32_t aStart,int32_t aLength)67 void accumulateCharactersForced(const char16_t* aBuf, int32_t aStart,
68                                 int32_t aLength) {
69   accumulateCharacters(aBuf, aStart, aLength);
70 }
71 
MarkAsBrokenAndRequestSuspensionWithBuilder(nsresult aRv)72 void MarkAsBrokenAndRequestSuspensionWithBuilder(nsresult aRv) {
73   mBuilder->MarkAsBroken(aRv);
74   requestSuspension();
75 }
76 
MarkAsBrokenAndRequestSuspensionWithoutBuilder(nsresult aRv)77 void MarkAsBrokenAndRequestSuspensionWithoutBuilder(nsresult aRv) {
78   MarkAsBroken(aRv);
79   requestSuspension();
80 }
81 
82 void MarkAsBrokenFromPortability(nsresult aRv);
83 
84 public:
85 explicit nsHtml5TreeBuilder(nsHtml5OplessBuilder* aBuilder);
86 
87 nsHtml5TreeBuilder(nsAHtml5TreeOpSink* aOpSink, nsHtml5TreeOpStage* aStage);
88 
89 ~nsHtml5TreeBuilder();
90 
91 void StartPlainTextViewSource(const nsAutoString& aTitle);
92 
93 void StartPlainText();
94 
95 void StartPlainTextBody();
96 
97 bool HasScript();
98 
SetOpSink(nsAHtml5TreeOpSink * aOpSink)99 void SetOpSink(nsAHtml5TreeOpSink* aOpSink) { mOpSink = aOpSink; }
100 
ClearOps()101 void ClearOps() { mOpQueue.Clear(); }
102 
103 bool Flush(bool aDiscretionary = false);
104 
105 void FlushLoads();
106 
107 void SetDocumentCharset(NotNull<const Encoding*> aEncoding,
108                         int32_t aCharsetSource);
109 
110 void StreamEnded();
111 
112 void NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding, int32_t aSource,
113                           int32_t aLineNumber);
114 
115 void MaybeComplainAboutCharset(const char* aMsgId, bool aError,
116                                int32_t aLineNumber);
117 
118 void TryToEnableEncodingMenu();
119 
120 void AddSnapshotToScript(nsAHtml5TreeBuilderState* aSnapshot, int32_t aLine);
121 
122 void DropHandles();
123 
SetPreventScriptExecution(bool aPrevent)124 void SetPreventScriptExecution(bool aPrevent) {
125   mPreventScriptExecution = aPrevent;
126 }
127 
HasBuilder()128 bool HasBuilder() { return mBuilder; }
129 
130 /**
131  * Makes sure the buffers are large enough to be able to tokenize aLength
132  * UTF-16 code units before having to make the buffers larger.
133  *
134  * @param aLength the number of UTF-16 code units to be tokenized before the
135  *                next call to this method.
136  * @return true if successful; false if out of memory
137  */
138 bool EnsureBufferSpace(int32_t aLength);
139 
140 void EnableViewSource(nsHtml5Highlighter* aHighlighter);
141 
142 void errDeepTree();
143 
144 void errStrayStartTag(nsAtom* aName);
145 
146 void errStrayEndTag(nsAtom* aName);
147 
148 void errUnclosedElements(int32_t aIndex, nsAtom* aName);
149 
150 void errUnclosedElementsImplied(int32_t aIndex, nsAtom* aName);
151 
152 void errUnclosedElementsCell(int32_t aIndex);
153 
154 void errStrayDoctype();
155 
156 void errAlmostStandardsDoctype();
157 
158 void errQuirkyDoctype();
159 
160 void errNonSpaceInTrailer();
161 
162 void errNonSpaceAfterFrameset();
163 
164 void errNonSpaceInFrameset();
165 
166 void errNonSpaceAfterBody();
167 
168 void errNonSpaceInColgroupInFragment();
169 
170 void errNonSpaceInNoscriptInHead();
171 
172 void errFooBetweenHeadAndBody(nsAtom* aName);
173 
174 void errStartTagWithoutDoctype();
175 
176 void errNoSelectInTableScope();
177 
178 void errStartSelectWhereEndSelectExpected();
179 
180 void errStartTagWithSelectOpen(nsAtom* aName);
181 
182 void errBadStartTagInNoscriptInHead(nsAtom* aName);
183 
184 void errImage();
185 
186 void errIsindex();
187 
188 void errFooSeenWhenFooOpen(nsAtom* aName);
189 
190 void errHeadingWhenHeadingOpen();
191 
192 void errFramesetStart();
193 
194 void errNoCellToClose();
195 
196 void errStartTagInTable(nsAtom* aName);
197 
198 void errFormWhenFormOpen();
199 
200 void errTableSeenWhileTableOpen();
201 
202 void errStartTagInTableBody(nsAtom* aName);
203 
204 void errEndTagSeenWithoutDoctype();
205 
206 void errEndTagAfterBody();
207 
208 void errEndTagSeenWithSelectOpen(nsAtom* aName);
209 
210 void errGarbageInColgroup();
211 
212 void errEndTagBr();
213 
214 void errNoElementToCloseButEndTagSeen(nsAtom* aName);
215 
216 void errHtmlStartTagInForeignContext(nsAtom* aName);
217 
218 void errNoTableRowToClose();
219 
220 void errNonSpaceInTable();
221 
222 void errUnclosedChildrenInRuby();
223 
224 void errStartTagSeenWithoutRuby(nsAtom* aName);
225 
226 void errSelfClosing();
227 
228 void errNoCheckUnclosedElementsOnStack();
229 
230 void errEndTagDidNotMatchCurrentOpenElement(nsAtom* aName, nsAtom* aOther);
231 
232 void errEndTagViolatesNestingRules(nsAtom* aName);
233 
234 void errEndWithUnclosedElements(nsAtom* aName);
235 
236 void MarkAsBroken(nsresult aRv);
237 
238 /**
239  * Checks if this parser is broken. Returns a non-NS_OK (i.e. non-0)
240  * value if broken.
241  */
IsBroken()242 nsresult IsBroken() { return mBroken; }
243