forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nsHtml5TreeOperation.h
636 lines (510 loc) · 19.4 KB
/
nsHtml5TreeOperation.h
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* 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/. */
#ifndef nsHtml5TreeOperation_h
#define nsHtml5TreeOperation_h
#include "nsHtml5DocumentMode.h"
#include "nsHtml5HtmlAttributes.h"
#include "mozilla/dom/FromParser.h"
#include "mozilla/NotNull.h"
#include "mozilla/Variant.h"
class nsIContent;
class nsHtml5TreeOpExecutor;
class nsHtml5DocumentBuilder;
namespace mozilla {
class Encoding;
namespace dom {
class Text;
} // namespace dom
} // namespace mozilla
struct uninitialized {};
// main HTML5 ops
struct opDetach {
nsIContent** mElement;
explicit opDetach(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opAppend {
nsIContent** mChild;
nsIContent** mParent;
mozilla::dom::FromParser mFromNetwork;
explicit opAppend(nsIContentHandle* aChild, nsIContentHandle* aParent,
mozilla::dom::FromParser aFromNetwork)
: mFromNetwork(aFromNetwork) {
mChild = static_cast<nsIContent**>(aChild);
mParent = static_cast<nsIContent**>(aParent);
};
};
struct opAppendChildrenToNewParent {
nsIContent** mOldParent;
nsIContent** mNewParent;
explicit opAppendChildrenToNewParent(nsIContentHandle* aOldParent,
nsIContentHandle* aNewParent) {
mOldParent = static_cast<nsIContent**>(aOldParent);
mNewParent = static_cast<nsIContent**>(aNewParent);
};
};
struct opFosterParent {
nsIContent** mChild;
nsIContent** mStackParent;
nsIContent** mTable;
explicit opFosterParent(nsIContentHandle* aChild,
nsIContentHandle* aStackParent,
nsIContentHandle* aTable) {
mChild = static_cast<nsIContent**>(aChild);
mStackParent = static_cast<nsIContent**>(aStackParent);
mTable = static_cast<nsIContent**>(aTable);
};
};
struct opAppendToDocument {
nsIContent** mContent;
explicit opAppendToDocument(nsIContentHandle* aContent) {
mContent = static_cast<nsIContent**>(aContent);
};
};
struct opAddAttributes {
nsIContent** mElement;
nsHtml5HtmlAttributes* mAttributes;
explicit opAddAttributes(nsIContentHandle* aElement,
nsHtml5HtmlAttributes* aAttributes)
: mAttributes(aAttributes) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opCreateHTMLElement {
nsIContent** mContent;
nsAtom* mName;
nsHtml5HtmlAttributes* mAttributes;
mozilla::dom::HTMLContentCreatorFunction mCreator;
nsIContent** mIntendedParent;
mozilla::dom::FromParser mFromNetwork;
explicit opCreateHTMLElement(
nsIContentHandle* aContent, nsAtom* aName,
nsHtml5HtmlAttributes* aAttributes,
mozilla::dom::HTMLContentCreatorFunction aCreator,
nsIContentHandle* aIntendedParent, mozilla::dom::FromParser mFromNetwork)
: mName(aName),
mAttributes(aAttributes),
mCreator(aCreator),
mFromNetwork(mFromNetwork) {
mContent = static_cast<nsIContent**>(aContent);
mIntendedParent = static_cast<nsIContent**>(aIntendedParent);
aName->AddRef();
if (mAttributes == nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES) {
mAttributes = nullptr;
}
};
};
struct opCreateSVGElement {
nsIContent** mContent;
nsAtom* mName;
nsHtml5HtmlAttributes* mAttributes;
mozilla::dom::SVGContentCreatorFunction mCreator;
nsIContent** mIntendedParent;
mozilla::dom::FromParser mFromNetwork;
explicit opCreateSVGElement(nsIContentHandle* aContent, nsAtom* aName,
nsHtml5HtmlAttributes* aAttributes,
mozilla::dom::SVGContentCreatorFunction aCreator,
nsIContentHandle* aIntendedParent,
mozilla::dom::FromParser mFromNetwork)
: mName(aName),
mAttributes(aAttributes),
mCreator(aCreator),
mFromNetwork(mFromNetwork) {
mContent = static_cast<nsIContent**>(aContent);
mIntendedParent = static_cast<nsIContent**>(aIntendedParent);
aName->AddRef();
if (mAttributes == nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES) {
mAttributes = nullptr;
}
};
};
struct opCreateMathMLElement {
nsIContent** mContent;
nsAtom* mName;
nsHtml5HtmlAttributes* mAttributes;
nsIContent** mIntendedParent;
explicit opCreateMathMLElement(nsIContentHandle* aContent, nsAtom* aName,
nsHtml5HtmlAttributes* aAttributes,
nsIContentHandle* aIntendedParent)
: mName(aName), mAttributes(aAttributes) {
mContent = static_cast<nsIContent**>(aContent);
mIntendedParent = static_cast<nsIContent**>(aIntendedParent);
aName->AddRef();
if (mAttributes == nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES) {
mAttributes = nullptr;
}
};
};
struct opSetFormElement {
nsIContent** mContent;
nsIContent** mFormElement;
explicit opSetFormElement(nsIContentHandle* aContent,
nsIContentHandle* aFormElement) {
mContent = static_cast<nsIContent**>(aContent);
mFormElement = static_cast<nsIContent**>(aFormElement);
};
};
struct opAppendText {
nsIContent** mParent;
char16_t* mBuffer;
int32_t mLength;
explicit opAppendText(nsIContentHandle* aParent, char16_t* aBuffer,
int32_t aLength)
: mBuffer(aBuffer), mLength(aLength) {
mParent = static_cast<nsIContent**>(aParent);
};
};
struct opFosterParentText {
nsIContent** mStackParent;
char16_t* mBuffer;
nsIContent** mTable;
int32_t mLength;
explicit opFosterParentText(nsIContentHandle* aStackParent, char16_t* aBuffer,
nsIContentHandle* aTable, int32_t aLength)
: mBuffer(aBuffer), mLength(aLength) {
mStackParent = static_cast<nsIContent**>(aStackParent);
mTable = static_cast<nsIContent**>(aTable);
};
};
struct opAppendComment {
nsIContent** mParent;
char16_t* mBuffer;
int32_t mLength;
explicit opAppendComment(nsIContentHandle* aParent, char16_t* aBuffer,
int32_t aLength)
: mBuffer(aBuffer), mLength(aLength) {
mParent = static_cast<nsIContent**>(aParent);
};
};
struct opAppendCommentToDocument {
char16_t* mBuffer;
int32_t mLength;
explicit opAppendCommentToDocument(char16_t* aBuffer, int32_t aLength)
: mBuffer(aBuffer), mLength(aLength){};
};
class nsHtml5TreeOperationStringPair {
private:
nsString mPublicId;
nsString mSystemId;
public:
nsHtml5TreeOperationStringPair(const nsAString& aPublicId,
const nsAString& aSystemId)
: mPublicId(aPublicId), mSystemId(aSystemId) {
MOZ_COUNT_CTOR(nsHtml5TreeOperationStringPair);
}
~nsHtml5TreeOperationStringPair() {
MOZ_COUNT_DTOR(nsHtml5TreeOperationStringPair);
}
inline void Get(nsAString& aPublicId, nsAString& aSystemId) {
aPublicId.Assign(mPublicId);
aSystemId.Assign(mSystemId);
}
};
struct opAppendDoctypeToDocument {
nsAtom* mName;
nsHtml5TreeOperationStringPair* mStringPair;
explicit opAppendDoctypeToDocument(nsAtom* aName, const nsAString& aPublicId,
const nsAString& aSystemId) {
mName = aName;
aName->AddRef();
mStringPair = new nsHtml5TreeOperationStringPair(aPublicId, aSystemId);
}
};
struct opGetDocumentFragmentForTemplate {
nsIContent** mTemplate;
nsIContent** mFragHandle;
explicit opGetDocumentFragmentForTemplate(nsIContentHandle* aTemplate,
nsIContentHandle* aFragHandle) {
mTemplate = static_cast<nsIContent**>(aTemplate);
mFragHandle = static_cast<nsIContent**>(aFragHandle);
}
};
struct opGetFosterParent {
nsIContent** mTable;
nsIContent** mStackParent;
nsIContent** mParentHandle;
explicit opGetFosterParent(nsIContentHandle* aTable,
nsIContentHandle* aStackParent,
nsIContentHandle* aParentHandle) {
mTable = static_cast<nsIContent**>(aTable);
mStackParent = static_cast<nsIContent**>(aStackParent);
mParentHandle = static_cast<nsIContent**>(aParentHandle);
};
};
// Gecko-specific on-pop ops
struct opMarkAsBroken {
nsresult mResult;
explicit opMarkAsBroken(nsresult aResult) : mResult(aResult){};
};
struct opRunScript {
nsIContent** mElement;
nsAHtml5TreeBuilderState* mBuilderState;
int32_t mLineNumber;
explicit opRunScript(nsIContentHandle* aElement,
nsAHtml5TreeBuilderState* aBuilderState)
: mBuilderState(aBuilderState), mLineNumber(0) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opRunScriptAsyncDefer {
nsIContent** mElement;
explicit opRunScriptAsyncDefer(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opPreventScriptExecution {
nsIContent** mElement;
explicit opPreventScriptExecution(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opDoneAddingChildren {
nsIContent** mElement;
explicit opDoneAddingChildren(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opDoneCreatingElement {
nsIContent** mElement;
explicit opDoneCreatingElement(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opSetDocumentCharset {
const mozilla::Encoding* mEncoding;
int32_t mCharsetSource;
explicit opSetDocumentCharset(const mozilla::Encoding* aEncoding,
int32_t aCharsetSource)
: mEncoding(aEncoding), mCharsetSource(aCharsetSource){};
};
struct opCharsetSwitchTo {
const mozilla::Encoding* mEncoding;
int32_t mCharsetSource;
int32_t mLineNumber;
explicit opCharsetSwitchTo(const mozilla::Encoding* aEncoding,
int32_t aCharsetSource, int32_t aLineNumber)
: mEncoding(aEncoding),
mCharsetSource(aCharsetSource),
mLineNumber(aLineNumber){};
};
struct opUpdateStyleSheet {
nsIContent** mElement;
explicit opUpdateStyleSheet(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opProcessOfflineManifest {
char16_t* mUrl;
explicit opProcessOfflineManifest(char16_t* aUrl) : mUrl(aUrl){};
};
struct opMarkMalformedIfScript {
nsIContent** mElement;
explicit opMarkMalformedIfScript(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
}
};
struct opStreamEnded {};
struct opSetStyleLineNumber {
nsIContent** mContent;
int32_t mLineNumber;
explicit opSetStyleLineNumber(nsIContentHandle* aContent, int32_t aLineNumber)
: mLineNumber(aLineNumber) {
mContent = static_cast<nsIContent**>(aContent);
};
};
struct opSetScriptLineNumberAndFreeze {
nsIContent** mContent;
int32_t mLineNumber;
explicit opSetScriptLineNumberAndFreeze(nsIContentHandle* aContent,
int32_t aLineNumber)
: mLineNumber(aLineNumber) {
mContent = static_cast<nsIContent**>(aContent);
};
};
struct opSvgLoad {
nsIContent** mElement;
explicit opSvgLoad(nsIContentHandle* aElement) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opMaybeComplainAboutCharset {
char* mMsgId;
bool mError;
int32_t mLineNumber;
explicit opMaybeComplainAboutCharset(char* aMsgId, bool aError,
int32_t aLineNumber)
: mMsgId(aMsgId), mError(aError), mLineNumber(aLineNumber){};
};
struct opMaybeComplainAboutDeepTree {
int32_t mLineNumber;
explicit opMaybeComplainAboutDeepTree(int32_t aLineNumber)
: mLineNumber(aLineNumber){};
};
struct opAddClass {
nsIContent** mElement;
char16_t* mClass;
explicit opAddClass(nsIContentHandle* aElement, char16_t* aClass)
: mClass(aClass) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opAddViewSourceHref {
nsIContent** mElement;
char16_t* mBuffer;
int32_t mLength;
explicit opAddViewSourceHref(nsIContentHandle* aElement, char16_t* aBuffer,
int32_t aLength)
: mBuffer(aBuffer), mLength(aLength) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opAddViewSourceBase {
char16_t* mBuffer;
int32_t mLength;
explicit opAddViewSourceBase(char16_t* aBuffer, int32_t aLength)
: mBuffer(aBuffer), mLength(aLength){};
};
struct opAddErrorType {
nsIContent** mElement;
char* mMsgId;
nsAtom* mName;
nsAtom* mOther;
explicit opAddErrorType(nsIContentHandle* aElement, char* aMsgId,
nsAtom* aName = nullptr, nsAtom* aOther = nullptr)
: mMsgId(aMsgId), mName(aName), mOther(aOther) {
mElement = static_cast<nsIContent**>(aElement);
if (aName) {
aName->AddRef();
}
if (aOther) {
aOther->AddRef();
}
};
};
struct opAddLineNumberId {
nsIContent** mElement;
int32_t mLineNumber;
explicit opAddLineNumberId(nsIContentHandle* aElement, int32_t aLineNumber)
: mLineNumber(aLineNumber) {
mElement = static_cast<nsIContent**>(aElement);
};
};
struct opStartLayout {};
struct opEnableEncodingMenu {};
typedef mozilla::Variant<
uninitialized,
// main HTML5 ops
opAppend, opDetach, opAppendChildrenToNewParent, opFosterParent,
opAppendToDocument, opAddAttributes, nsHtml5DocumentMode,
opCreateHTMLElement, opCreateSVGElement, opCreateMathMLElement,
opSetFormElement, opAppendText, opFosterParentText, opAppendComment,
opAppendCommentToDocument, opAppendDoctypeToDocument,
opGetDocumentFragmentForTemplate, opGetFosterParent,
// Gecko-specific on-pop ops
opMarkAsBroken, opRunScript, opRunScriptAsyncDefer,
opPreventScriptExecution, opDoneAddingChildren, opDoneCreatingElement,
opSetDocumentCharset, opCharsetSwitchTo, opUpdateStyleSheet,
opProcessOfflineManifest, opMarkMalformedIfScript, opStreamEnded,
opSetStyleLineNumber, opSetScriptLineNumberAndFreeze, opSvgLoad,
opMaybeComplainAboutCharset, opMaybeComplainAboutDeepTree, opAddClass,
opAddViewSourceHref, opAddViewSourceBase, opAddErrorType, opAddLineNumberId,
opStartLayout, opEnableEncodingMenu>
treeOperation;
class nsHtml5TreeOperation final {
template <typename T>
using NotNull = mozilla::NotNull<T>;
using Encoding = mozilla::Encoding;
public:
static nsresult AppendTextToTextNode(const char16_t* aBuffer,
uint32_t aLength,
mozilla::dom::Text* aTextNode,
nsHtml5DocumentBuilder* aBuilder);
static nsresult AppendText(const char16_t* aBuffer, uint32_t aLength,
nsIContent* aParent,
nsHtml5DocumentBuilder* aBuilder);
static nsresult Append(nsIContent* aNode, nsIContent* aParent,
nsHtml5DocumentBuilder* aBuilder);
static nsresult Append(nsIContent* aNode, nsIContent* aParent,
mozilla::dom::FromParser aFromParser,
nsHtml5DocumentBuilder* aBuilder);
static nsresult AppendToDocument(nsIContent* aNode,
nsHtml5DocumentBuilder* aBuilder);
static void Detach(nsIContent* aNode, nsHtml5DocumentBuilder* aBuilder);
static nsresult AppendChildrenToNewParent(nsIContent* aNode,
nsIContent* aParent,
nsHtml5DocumentBuilder* aBuilder);
static nsresult FosterParent(nsIContent* aNode, nsIContent* aParent,
nsIContent* aTable,
nsHtml5DocumentBuilder* aBuilder);
static nsresult AddAttributes(nsIContent* aNode,
nsHtml5HtmlAttributes* aAttributes,
nsHtml5DocumentBuilder* aBuilder);
static void SetHTMLElementAttributes(mozilla::dom::Element* aElement,
nsAtom* aName,
nsHtml5HtmlAttributes* aAttributes);
static nsIContent* CreateHTMLElement(
nsAtom* aName, nsHtml5HtmlAttributes* aAttributes,
mozilla::dom::FromParser aFromParser, nsNodeInfoManager* aNodeInfoManager,
nsHtml5DocumentBuilder* aBuilder,
mozilla::dom::HTMLContentCreatorFunction aCreator);
static nsIContent* CreateSVGElement(
nsAtom* aName, nsHtml5HtmlAttributes* aAttributes,
mozilla::dom::FromParser aFromParser, nsNodeInfoManager* aNodeInfoManager,
nsHtml5DocumentBuilder* aBuilder,
mozilla::dom::SVGContentCreatorFunction aCreator);
static nsIContent* CreateMathMLElement(nsAtom* aName,
nsHtml5HtmlAttributes* aAttributes,
nsNodeInfoManager* aNodeInfoManager,
nsHtml5DocumentBuilder* aBuilder);
static void SetFormElement(nsIContent* aNode, nsIContent* aParent);
static nsresult AppendIsindexPrompt(nsIContent* parent,
nsHtml5DocumentBuilder* aBuilder);
static nsresult FosterParentText(nsIContent* aStackParent, char16_t* aBuffer,
uint32_t aLength, nsIContent* aTable,
nsHtml5DocumentBuilder* aBuilder);
static nsresult AppendComment(nsIContent* aParent, char16_t* aBuffer,
int32_t aLength,
nsHtml5DocumentBuilder* aBuilder);
static nsresult AppendCommentToDocument(char16_t* aBuffer, int32_t aLength,
nsHtml5DocumentBuilder* aBuilder);
static nsresult AppendDoctypeToDocument(nsAtom* aName,
const nsAString& aPublicId,
const nsAString& aSystemId,
nsHtml5DocumentBuilder* aBuilder);
static nsIContent* GetDocumentFragmentForTemplate(nsIContent* aNode);
static nsIContent* GetFosterParent(nsIContent* aTable,
nsIContent* aStackParent);
static void PreventScriptExecution(nsIContent* aNode);
static void DoneAddingChildren(nsIContent* aNode);
static void DoneCreatingElement(nsIContent* aNode);
static void SvgLoad(nsIContent* aNode);
static void MarkMalformedIfScript(nsIContent* aNode);
nsHtml5TreeOperation();
~nsHtml5TreeOperation();
inline void Init(const treeOperation& aOperation) {
NS_ASSERTION(mOperation.is<uninitialized>(),
"Op code must be uninitialized when initializing.");
mOperation = aOperation;
}
inline bool IsRunScript() { return mOperation.is<opRunScript>(); }
inline bool IsMarkAsBroken() { return mOperation.is<opMarkAsBroken>(); }
inline void SetSnapshot(nsAHtml5TreeBuilderState* aSnapshot, int32_t aLine) {
NS_ASSERTION(
IsRunScript(),
"Setting a snapshot for a tree operation other than eTreeOpRunScript!");
MOZ_ASSERT(aSnapshot, "Initialized tree op with null snapshot.");
opRunScript data = mOperation.as<opRunScript>();
data.mBuilderState = aSnapshot;
data.mLineNumber = aLine;
mOperation = mozilla::AsVariant(data);
}
nsresult Perform(nsHtml5TreeOpExecutor* aBuilder, nsIContent** aScriptElement,
bool* aInterrupted, bool* aStreamEnded);
private:
nsHtml5TreeOperation(const nsHtml5TreeOperation&) = delete;
nsHtml5TreeOperation& operator=(const nsHtml5TreeOperation&) = delete;
treeOperation mOperation;
};
#endif // nsHtml5TreeOperation_h