summaryrefslogtreecommitdiffstats
path: root/parser/html/nsHtml5TreeBuilderHSupplement.h
blob: 813654cb24bb73808b9436e0720a51ec514d1155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#define NS_HTML5_TREE_BUILDER_HANDLE_ARRAY_LENGTH 512
private:
using Encoding = mozilla::Encoding;
template <typename T>
using NotNull = mozilla::NotNull<T>;

nsHtml5OplessBuilder* mBuilder;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// If mBuilder is not null, the tree op machinery is not in use and
// the fields below aren't in use, either.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
nsHtml5Highlighter* mViewSource;
mozilla::ImportScanner mImportScanner;
nsTArray<nsHtml5TreeOperation> mOpQueue;
nsTArray<nsHtml5SpeculativeLoad> mSpeculativeLoadQueue;
nsAHtml5TreeOpSink* mOpSink;
mozilla::UniquePtr<nsIContent*[]> mHandles;
int32_t mHandlesUsed;
nsTArray<mozilla::UniquePtr<nsIContent*[]>> mOldHandles;
nsHtml5TreeOpStage* mSpeculativeLoadStage;
nsresult mBroken;
// Controls whether the current HTML script goes through the more complex
// path that accommodates the possibility of the script becoming a
// parser-blocking script and the possibility of the script inserting
// content into this parse using document.write (as it is observable from
// the Web).
//
// Notably, in some cases scripts that do NOT NEED the more complex path
// BREAK the parse if they incorrectly go onto the complex path as their
// other handling doesn't necessarily take care of the responsibilities
// associated with the more complex path. See comments in
// `nsHtml5TreeBuilder::createElement` in the CppSupplement for details.
bool mCurrentHtmlScriptCannotDocumentWriteOrBlock;
bool mPreventScriptExecution;
/**
 * Whether to actually generate speculative load operations that actually
 * represent speculative loads as opposed to other operations traveling
 * in the same queue. True for normal loads and false for XHR, plain text,
 * and View Source. Needed, because we can't just null-check
 * mSpeculativeLoadStage, since it is used for transferring encoding
 * information even in the XHR/plain text/View Source cases.
 */
bool mGenerateSpeculativeLoads;

bool mHasSeenImportMap;
#ifdef DEBUG
bool mActive;
#endif

// DocumentModeHandler
/**
 * Tree builder uses this to report quirkiness of the document
 */
void documentMode(nsHtml5DocumentMode m);

nsIContentHandle* getDocumentFragmentForTemplate(nsIContentHandle* aTemplate);
void setDocumentFragmentForTemplate(nsIContentHandle* aTemplate,
                                    nsIContentHandle* aFragment);

nsIContentHandle* getShadowRootFromHost(nsIContentHandle* aHost,
                                        nsIContentHandle* aTemplateNode,
                                        nsHtml5String aShadowRootMode,
                                        bool aShadowRootDelegatesFocus);

nsIContentHandle* getFormPointerForContext(nsIContentHandle* aContext);

/**
 * Using nsIContent** instead of nsIContent* is the parser deals with DOM
 * nodes in a way that works off the main thread. Non-main-thread code
 * can't refcount or otherwise touch nsIContent objects in any way.
 * Yet, the off-the-main-thread code needs to have a way to hold onto a
 * particular node and repeatedly operate on the same node.
 *
 * The way this works is that the off-the-main-thread code has an
 * nsIContent** for each DOM node and a given nsIContent** is only ever
 * actually dereferenced into an actual nsIContent* on the main thread.
 * When the off-the-main-thread code requests a new node, it gets an
 * nsIContent** immediately and a tree op is enqueued for later allocating
 * an actual nsIContent object and writing a pointer to it into the memory
 * location pointed to by the nsIContent**.
 *
 * Since tree ops are in a queue, the node creating tree op will always
 * run before tree ops that try to further operate on the node that the
 * nsIContent** is a handle to.
 *
 * On-the-main-thread parts of the parser use nsIContent* instead of
 * nsIContent**. Since both cases share the same parser core, the parser
 * core casts both to nsIContentHandle*.
 */
nsIContentHandle* AllocateContentHandle();

void accumulateCharactersForced(const char16_t* aBuf, int32_t aStart,
                                int32_t aLength) {
  accumulateCharacters(aBuf, aStart, aLength);
}

void MarkAsBrokenAndRequestSuspensionWithBuilder(nsresult aRv) {
  mBuilder->MarkAsBroken(aRv);
  requestSuspension();
}

void MarkAsBrokenAndRequestSuspensionWithoutBuilder(nsresult aRv) {
  MarkAsBroken(aRv);
  requestSuspension();
}

void MarkAsBrokenFromPortability(nsresult aRv);

public:
explicit nsHtml5TreeBuilder(nsHtml5OplessBuilder* aBuilder);

nsHtml5TreeBuilder(nsAHtml5TreeOpSink* aOpSink, nsHtml5TreeOpStage* aStage,
                   bool aGenerateSpeculativeLoads);

~nsHtml5TreeBuilder();

bool WantsLineAndColumn() {
  // Perhaps just checking mBuilder would be sufficient.
  // For createContextualFragment, we have non-null mBuilder and
  // false for mPreventScriptExecution. However, do the line and
  // column that get attached to script elements make any sense
  // anyway in that case?
  return !(mBuilder && mPreventScriptExecution);
}

void StartPlainTextViewSource(const nsAutoString& aTitle);

void StartPlainText();

void StartPlainTextBody();

bool HasScriptThatMayDocumentWriteOrBlock();

void SetOpSink(nsAHtml5TreeOpSink* aOpSink) { mOpSink = aOpSink; }

void ClearOps() { mOpQueue.Clear(); }

/**
 * Flushes tree ops.
 * @return Ok(true) if there were ops to flush, Ok(false)
 *         if there were no ops to flush and Err() on OOM.
 */
mozilla::Result<bool, nsresult> Flush(bool aDiscretionary = false);

void FlushLoads();

/**
 * Sets the document charset via the speculation queue.
 *
 * @param aCommitEncodingSpeculation true iff the main thread should
 *                           treat the first speculation as an
 *                           encoding speculation.
 */
void SetDocumentCharset(NotNull<const Encoding*> aEncoding,
                        nsCharsetSource aCharsetSource,
                        bool aCommitEncodingSpeculation);

/**
 * Updates the charset source via the op queue.
 */
void UpdateCharsetSource(nsCharsetSource aCharsetSource);

void StreamEnded();

void NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding, int32_t aSource,
                          int32_t aLineNumber);

void MaybeComplainAboutCharset(const char* aMsgId, bool aError,
                               int32_t aLineNumber);

void TryToEnableEncodingMenu();

void AddSnapshotToScript(nsAHtml5TreeBuilderState* aSnapshot, int32_t aLine);

void DropHandles();

void SetPreventScriptExecution(bool aPrevent) {
  mPreventScriptExecution = aPrevent;
}

bool HasBuilder() { return mBuilder; }

/**
 * Makes sure the buffers are large enough to be able to tokenize aLength
 * UTF-16 code units before having to make the buffers larger.
 *
 * @param aLength the number of UTF-16 code units to be tokenized before the
 *                next call to this method.
 * @return true if successful; false if out of memory
 */
bool EnsureBufferSpace(int32_t aLength);

void EnableViewSource(nsHtml5Highlighter* aHighlighter);

void errDeepTree();

void errStrayStartTag(nsAtom* aName);

void errStrayEndTag(nsAtom* aName);

void errUnclosedElements(int32_t aIndex, nsAtom* aName);

void errUnclosedElementsImplied(int32_t aIndex, nsAtom* aName);

void errUnclosedElementsCell(int32_t aIndex);

void errStrayDoctype();

void errAlmostStandardsDoctype();

void errQuirkyDoctype();

void errNonSpaceInTrailer();

void errNonSpaceAfterFrameset();

void errNonSpaceInFrameset();

void errNonSpaceAfterBody();

void errNonSpaceInColgroupInFragment();

void errNonSpaceInNoscriptInHead();

void errFooBetweenHeadAndBody(nsAtom* aName);

void errStartTagWithoutDoctype();

void errNoSelectInTableScope();

void errStartSelectWhereEndSelectExpected();

void errStartTagWithSelectOpen(nsAtom* aName);

void errBadStartTagInNoscriptInHead(nsAtom* aName);

void errImage();

void errIsindex();

void errFooSeenWhenFooOpen(nsAtom* aName);

void errHeadingWhenHeadingOpen();

void errFramesetStart();

void errNoCellToClose();

void errStartTagInTable(nsAtom* aName);

void errFormWhenFormOpen();

void errTableSeenWhileTableOpen();

void errStartTagInTableBody(nsAtom* aName);

void errEndTagSeenWithoutDoctype();

void errEndTagAfterBody();

void errEndTagSeenWithSelectOpen(nsAtom* aName);

void errGarbageInColgroup();

void errEndTagBr();

void errNoElementToCloseButEndTagSeen(nsAtom* aName);

void errHtmlStartTagInForeignContext(nsAtom* aName);

void errNoTableRowToClose();

void errNonSpaceInTable();

void errUnclosedChildrenInRuby();

void errStartTagSeenWithoutRuby(nsAtom* aName);

void errSelfClosing();

void errNoCheckUnclosedElementsOnStack();

void errEndTagDidNotMatchCurrentOpenElement(nsAtom* aName, nsAtom* aOther);

void errEndTagViolatesNestingRules(nsAtom* aName);

void errEndWithUnclosedElements(nsAtom* aName);

void errListUnclosedStartTags(int32_t aIgnored);

void MarkAsBroken(nsresult aRv);

/**
 * Checks if this parser is broken. Returns a non-NS_OK (i.e. non-0)
 * value if broken.
 */
nsresult IsBroken() { return mBroken; }