forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsParser.cpp
2012 lines (1777 loc) · 69.3 KB
/
nsParser.cpp
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=2 et tw=79: */
/* 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/. */
#include "nsIAtom.h"
#include "nsParser.h"
#include "nsString.h"
#include "nsCRT.h"
#include "nsScanner.h"
#include "plstr.h"
#include "nsIStringStream.h"
#include "nsIChannel.h"
#include "nsICachingChannel.h"
#include "nsIInputStream.h"
#include "CNavDTD.h"
#include "prenv.h"
#include "prlock.h"
#include "prcvar.h"
#include "nsParserCIID.h"
#include "nsReadableUtils.h"
#include "nsCOMPtr.h"
#include "nsExpatDriver.h"
#include "nsIServiceManager.h"
#include "nsICategoryManager.h"
#include "nsISupportsPrimitives.h"
#include "nsIFragmentContentSink.h"
#include "nsStreamUtils.h"
#include "nsHTMLTokenizer.h"
#include "nsScriptLoader.h"
#include "nsDataHashtable.h"
#include "nsXPCOMCIDInternal.h"
#include "nsMimeTypes.h"
#include "mozilla/CondVar.h"
#include "mozilla/Mutex.h"
#include "nsParserConstants.h"
#include "nsCharsetSource.h"
#include "nsContentUtils.h"
#include "nsThreadUtils.h"
#include "nsIHTMLContentSink.h"
#include "mozilla/dom/EncodingUtils.h"
#include "mozilla/BinarySearch.h"
using namespace mozilla;
using mozilla::dom::EncodingUtils;
#define NS_PARSER_FLAG_PARSER_ENABLED 0x00000002
#define NS_PARSER_FLAG_OBSERVERS_ENABLED 0x00000004
#define NS_PARSER_FLAG_PENDING_CONTINUE_EVENT 0x00000008
#define NS_PARSER_FLAG_FLUSH_TOKENS 0x00000020
#define NS_PARSER_FLAG_CAN_TOKENIZE 0x00000040
//-------------- Begin ParseContinue Event Definition ------------------------
/*
The parser can be explicitly interrupted by passing a return value of
NS_ERROR_HTMLPARSER_INTERRUPTED from BuildModel on the DTD. This will cause
the parser to stop processing and allow the application to return to the event
loop. The data which was left at the time of interruption will be processed
the next time OnDataAvailable is called. If the parser has received its final
chunk of data then OnDataAvailable will no longer be called by the networking
module, so the parser will schedule a nsParserContinueEvent which will call
the parser to process the remaining data after returning to the event loop.
If the parser is interrupted while processing the remaining data it will
schedule another ParseContinueEvent. The processing of data followed by
scheduling of the continue events will proceed until either:
1) All of the remaining data can be processed without interrupting
2) The parser has been cancelled.
This capability is currently used in CNavDTD and nsHTMLContentSink. The
nsHTMLContentSink is notified by CNavDTD when a chunk of tokens is going to be
processed and when each token is processed. The nsHTML content sink records
the time when the chunk has started processing and will return
NS_ERROR_HTMLPARSER_INTERRUPTED if the token processing time has exceeded a
threshold called max tokenizing processing time. This allows the content sink
to limit how much data is processed in a single chunk which in turn gates how
much time is spent away from the event loop. Processing smaller chunks of data
also reduces the time spent in subsequent reflows.
This capability is most apparent when loading large documents. If the maximum
token processing time is set small enough the application will remain
responsive during document load.
A side-effect of this capability is that document load is not complete when
the last chunk of data is passed to OnDataAvailable since the parser may have
been interrupted when the last chunk of data arrived. The document is complete
when all of the document has been tokenized and there aren't any pending
nsParserContinueEvents. This can cause problems if the application assumes
that it can monitor the load requests to determine when the document load has
been completed. This is what happens in Mozilla. The document is considered
completely loaded when all of the load requests have been satisfied. To delay
the document load until all of the parsing has been completed the
nsHTMLContentSink adds a dummy parser load request which is not removed until
the nsHTMLContentSink's DidBuildModel is called. The CNavDTD will not call
DidBuildModel until the final chunk of data has been passed to the parser
through the OnDataAvailable and there aren't any pending
nsParserContineEvents.
Currently the parser is ignores requests to be interrupted during the
processing of script. This is because a document.write followed by JavaScript
calls to manipulate the DOM may fail if the parser was interrupted during the
document.write.
For more details @see bugzilla bug 76722
*/
class nsParserContinueEvent : public Runnable
{
public:
RefPtr<nsParser> mParser;
explicit nsParserContinueEvent(nsParser* aParser)
: mParser(aParser)
{}
NS_IMETHOD Run() override
{
mParser->HandleParserContinueEvent(this);
return NS_OK;
}
};
//-------------- End ParseContinue Event Definition ------------------------
/**
* default constructor
*/
nsParser::nsParser()
{
Initialize(true);
}
nsParser::~nsParser()
{
Cleanup();
}
void
nsParser::Initialize(bool aConstructor)
{
if (aConstructor) {
// Raw pointer
mParserContext = 0;
}
else {
// nsCOMPtrs
mObserver = nullptr;
mUnusedInput.Truncate();
}
mContinueEvent = nullptr;
mCharsetSource = kCharsetUninitialized;
mCharset.AssignLiteral("ISO-8859-1");
mInternalState = NS_OK;
mStreamStatus = NS_OK;
mCommand = eViewNormal;
mFlags = NS_PARSER_FLAG_OBSERVERS_ENABLED |
NS_PARSER_FLAG_PARSER_ENABLED |
NS_PARSER_FLAG_CAN_TOKENIZE;
mProcessingNetworkData = false;
mIsAboutBlank = false;
}
void
nsParser::Cleanup()
{
#ifdef DEBUG
if (mParserContext && mParserContext->mPrevContext) {
NS_WARNING("Extra parser contexts still on the parser stack");
}
#endif
while (mParserContext) {
CParserContext *pc = mParserContext->mPrevContext;
delete mParserContext;
mParserContext = pc;
}
// It should not be possible for this flag to be set when we are getting
// destroyed since this flag implies a pending nsParserContinueEvent, which
// has an owning reference to |this|.
NS_ASSERTION(!(mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT), "bad");
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsParser)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsParser)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDTD)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSink)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mObserver)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsParser)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDTD)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSink)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObserver)
CParserContext *pc = tmp->mParserContext;
while (pc) {
cb.NoteXPCOMChild(pc->mTokenizer);
pc = pc->mPrevContext;
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsParser)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsParser)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsParser)
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIParser)
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIParser)
NS_INTERFACE_MAP_END
// The parser continue event is posted only if
// all of the data to parse has been passed to ::OnDataAvailable
// and the parser has been interrupted by the content sink
// because the processing of tokens took too long.
nsresult
nsParser::PostContinueEvent()
{
if (!(mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT)) {
// If this flag isn't set, then there shouldn't be a live continue event!
NS_ASSERTION(!mContinueEvent, "bad");
// This creates a reference cycle between this and the event that is
// broken when the event fires.
nsCOMPtr<nsIRunnable> event = new nsParserContinueEvent(this);
if (NS_FAILED(NS_DispatchToCurrentThread(event))) {
NS_WARNING("failed to dispatch parser continuation event");
} else {
mFlags |= NS_PARSER_FLAG_PENDING_CONTINUE_EVENT;
mContinueEvent = event;
}
}
return NS_OK;
}
NS_IMETHODIMP_(void)
nsParser::GetCommand(nsCString& aCommand)
{
aCommand = mCommandStr;
}
/**
* Call this method once you've created a parser, and want to instruct it
* about the command which caused the parser to be constructed. For example,
* this allows us to select a DTD which can do, say, view-source.
*
* @param aCommand the command string to set
*/
NS_IMETHODIMP_(void)
nsParser::SetCommand(const char* aCommand)
{
mCommandStr.Assign(aCommand);
if (mCommandStr.EqualsLiteral("view-source")) {
mCommand = eViewSource;
} else if (mCommandStr.EqualsLiteral("view-fragment")) {
mCommand = eViewFragment;
} else {
mCommand = eViewNormal;
}
}
/**
* Call this method once you've created a parser, and want to instruct it
* about the command which caused the parser to be constructed. For example,
* this allows us to select a DTD which can do, say, view-source.
*
* @param aParserCommand the command to set
*/
NS_IMETHODIMP_(void)
nsParser::SetCommand(eParserCommands aParserCommand)
{
mCommand = aParserCommand;
}
/**
* Call this method once you've created a parser, and want to instruct it
* about what charset to load
*
* @param aCharset- the charset of a document
* @param aCharsetSource- the source of the charset
*/
NS_IMETHODIMP_(void)
nsParser::SetDocumentCharset(const nsACString& aCharset, int32_t aCharsetSource)
{
mCharset = aCharset;
mCharsetSource = aCharsetSource;
if (mParserContext && mParserContext->mScanner) {
mParserContext->mScanner->SetDocumentCharset(aCharset, aCharsetSource);
}
}
void
nsParser::SetSinkCharset(nsACString& aCharset)
{
if (mSink) {
mSink->SetDocumentCharset(aCharset);
}
}
/**
* This method gets called in order to set the content
* sink for this parser to dump nodes to.
*
* @param nsIContentSink interface for node receiver
*/
NS_IMETHODIMP_(void)
nsParser::SetContentSink(nsIContentSink* aSink)
{
NS_PRECONDITION(aSink, "sink cannot be null!");
mSink = aSink;
if (mSink) {
mSink->SetParser(this);
nsCOMPtr<nsIHTMLContentSink> htmlSink = do_QueryInterface(mSink);
if (htmlSink) {
mIsAboutBlank = true;
}
}
}
/**
* retrieve the sink set into the parser
* @return current sink
*/
NS_IMETHODIMP_(nsIContentSink*)
nsParser::GetContentSink()
{
return mSink;
}
/**
* Determine what DTD mode (and thus what layout nsCompatibility mode)
* to use for this document based on the first chunk of data received
* from the network (each parsercontext can have its own mode). (No,
* this is not an optimal solution -- we really don't need to know until
* after we've received the DOCTYPE, and this could easily be part of
* the regular parsing process if the parser were designed in a way that
* made such modifications easy.)
*/
// Parse the PS production in the SGML spec (excluding the part dealing
// with entity references) starting at theIndex into theBuffer, and
// return the first index after the end of the production.
static int32_t
ParsePS(const nsString& aBuffer, int32_t aIndex)
{
for (;;) {
char16_t ch = aBuffer.CharAt(aIndex);
if ((ch == char16_t(' ')) || (ch == char16_t('\t')) ||
(ch == char16_t('\n')) || (ch == char16_t('\r'))) {
++aIndex;
} else if (ch == char16_t('-')) {
int32_t tmpIndex;
if (aBuffer.CharAt(aIndex+1) == char16_t('-') &&
kNotFound != (tmpIndex=aBuffer.Find("--",false,aIndex+2,-1))) {
aIndex = tmpIndex + 2;
} else {
return aIndex;
}
} else {
return aIndex;
}
}
}
#define PARSE_DTD_HAVE_DOCTYPE (1<<0)
#define PARSE_DTD_HAVE_PUBLIC_ID (1<<1)
#define PARSE_DTD_HAVE_SYSTEM_ID (1<<2)
#define PARSE_DTD_HAVE_INTERNAL_SUBSET (1<<3)
// return true on success (includes not present), false on failure
static bool
ParseDocTypeDecl(const nsString &aBuffer,
int32_t *aResultFlags,
nsString &aPublicID,
nsString &aSystemID)
{
bool haveDoctype = false;
*aResultFlags = 0;
// Skip through any comments and processing instructions
// The PI-skipping is a bit of a hack.
int32_t theIndex = 0;
do {
theIndex = aBuffer.FindChar('<', theIndex);
if (theIndex == kNotFound) break;
char16_t nextChar = aBuffer.CharAt(theIndex+1);
if (nextChar == char16_t('!')) {
int32_t tmpIndex = theIndex + 2;
if (kNotFound !=
(theIndex=aBuffer.Find("DOCTYPE", true, tmpIndex, 0))) {
haveDoctype = true;
theIndex += 7; // skip "DOCTYPE"
break;
}
theIndex = ParsePS(aBuffer, tmpIndex);
theIndex = aBuffer.FindChar('>', theIndex);
} else if (nextChar == char16_t('?')) {
theIndex = aBuffer.FindChar('>', theIndex);
} else {
break;
}
} while (theIndex != kNotFound);
if (!haveDoctype)
return true;
*aResultFlags |= PARSE_DTD_HAVE_DOCTYPE;
theIndex = ParsePS(aBuffer, theIndex);
theIndex = aBuffer.Find("HTML", true, theIndex, 0);
if (kNotFound == theIndex)
return false;
theIndex = ParsePS(aBuffer, theIndex+4);
int32_t tmpIndex = aBuffer.Find("PUBLIC", true, theIndex, 0);
if (kNotFound != tmpIndex) {
theIndex = ParsePS(aBuffer, tmpIndex+6);
// We get here only if we've read <!DOCTYPE HTML PUBLIC
// (not case sensitive) possibly with comments within.
// Now find the beginning and end of the public identifier
// and the system identifier (if present).
char16_t lit = aBuffer.CharAt(theIndex);
if ((lit != char16_t('\"')) && (lit != char16_t('\'')))
return false;
// Start is the first character, excluding the quote, and End is
// the final quote, so there are (end-start) characters.
int32_t PublicIDStart = theIndex + 1;
int32_t PublicIDEnd = aBuffer.FindChar(lit, PublicIDStart);
if (kNotFound == PublicIDEnd)
return false;
theIndex = ParsePS(aBuffer, PublicIDEnd + 1);
char16_t next = aBuffer.CharAt(theIndex);
if (next == char16_t('>')) {
// There was a public identifier, but no system
// identifier,
// so do nothing.
// This is needed to avoid the else at the end, and it's
// also the most common case.
} else if ((next == char16_t('\"')) ||
(next == char16_t('\''))) {
// We found a system identifier.
*aResultFlags |= PARSE_DTD_HAVE_SYSTEM_ID;
int32_t SystemIDStart = theIndex + 1;
int32_t SystemIDEnd = aBuffer.FindChar(next, SystemIDStart);
if (kNotFound == SystemIDEnd)
return false;
aSystemID =
Substring(aBuffer, SystemIDStart, SystemIDEnd - SystemIDStart);
} else if (next == char16_t('[')) {
// We found an internal subset.
*aResultFlags |= PARSE_DTD_HAVE_INTERNAL_SUBSET;
} else {
// Something's wrong.
return false;
}
// Since a public ID is a minimum literal, we must trim
// and collapse whitespace
aPublicID = Substring(aBuffer, PublicIDStart, PublicIDEnd - PublicIDStart);
aPublicID.CompressWhitespace(true, true);
*aResultFlags |= PARSE_DTD_HAVE_PUBLIC_ID;
} else {
tmpIndex=aBuffer.Find("SYSTEM", true, theIndex, 0);
if (kNotFound != tmpIndex) {
// DOCTYPES with system ID but no Public ID
*aResultFlags |= PARSE_DTD_HAVE_SYSTEM_ID;
theIndex = ParsePS(aBuffer, tmpIndex+6);
char16_t next = aBuffer.CharAt(theIndex);
if (next != char16_t('\"') && next != char16_t('\''))
return false;
int32_t SystemIDStart = theIndex + 1;
int32_t SystemIDEnd = aBuffer.FindChar(next, SystemIDStart);
if (kNotFound == SystemIDEnd)
return false;
aSystemID =
Substring(aBuffer, SystemIDStart, SystemIDEnd - SystemIDStart);
theIndex = ParsePS(aBuffer, SystemIDEnd + 1);
}
char16_t nextChar = aBuffer.CharAt(theIndex);
if (nextChar == char16_t('['))
*aResultFlags |= PARSE_DTD_HAVE_INTERNAL_SUBSET;
else if (nextChar != char16_t('>'))
return false;
}
return true;
}
struct PubIDInfo
{
enum eMode {
eQuirks, /* always quirks mode, unless there's an internal subset */
eAlmostStandards,/* eCompatibility_AlmostStandards */
eFullStandards /* eCompatibility_FullStandards */
/*
* public IDs that should trigger strict mode are not listed
* since we want all future public IDs to trigger strict mode as
* well
*/
};
const char* name;
eMode mode_if_no_sysid;
eMode mode_if_sysid;
};
#define ELEMENTS_OF(array_) (sizeof(array_)/sizeof(array_[0]))
// These must be in nsCRT::strcmp order so binary-search can be used.
// This is verified, |#ifdef DEBUG|, below.
// Even though public identifiers should be case sensitive, we will do
// all comparisons after converting to lower case in order to do
// case-insensitive comparison since there are a number of existing web
// sites that use the incorrect case. Therefore all of the public
// identifiers below are in lower case (with the correct case following,
// in comments). The case is verified, |#ifdef DEBUG|, below.
static const PubIDInfo kPublicIDs[] = {
{"+//silmaril//dtd html pro v0r11 19970101//en" /* "+//Silmaril//dtd html Pro v0r11 19970101//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//advasoft ltd//dtd html 3.0 aswedit + extensions//en" /* "-//AdvaSoft Ltd//DTD HTML 3.0 asWedit + extensions//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//as//dtd html 3.0 aswedit + extensions//en" /* "-//AS//DTD HTML 3.0 asWedit + extensions//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.0 level 1//en" /* "-//IETF//DTD HTML 2.0 Level 1//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.0 level 2//en" /* "-//IETF//DTD HTML 2.0 Level 2//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.0 strict level 1//en" /* "-//IETF//DTD HTML 2.0 Strict Level 1//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.0 strict level 2//en" /* "-//IETF//DTD HTML 2.0 Strict Level 2//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.0 strict//en" /* "-//IETF//DTD HTML 2.0 Strict//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.0//en" /* "-//IETF//DTD HTML 2.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 2.1e//en" /* "-//IETF//DTD HTML 2.1E//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 3.0//en" /* "-//IETF//DTD HTML 3.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 3.0//en//" /* "-//IETF//DTD HTML 3.0//EN//" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 3.2 final//en" /* "-//IETF//DTD HTML 3.2 Final//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 3.2//en" /* "-//IETF//DTD HTML 3.2//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html 3//en" /* "-//IETF//DTD HTML 3//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 0//en" /* "-//IETF//DTD HTML Level 0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 0//en//2.0" /* "-//IETF//DTD HTML Level 0//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 1//en" /* "-//IETF//DTD HTML Level 1//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 1//en//2.0" /* "-//IETF//DTD HTML Level 1//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 2//en" /* "-//IETF//DTD HTML Level 2//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 2//en//2.0" /* "-//IETF//DTD HTML Level 2//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 3//en" /* "-//IETF//DTD HTML Level 3//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html level 3//en//3.0" /* "-//IETF//DTD HTML Level 3//EN//3.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 0//en" /* "-//IETF//DTD HTML Strict Level 0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 0//en//2.0" /* "-//IETF//DTD HTML Strict Level 0//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 1//en" /* "-//IETF//DTD HTML Strict Level 1//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 1//en//2.0" /* "-//IETF//DTD HTML Strict Level 1//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 2//en" /* "-//IETF//DTD HTML Strict Level 2//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 2//en//2.0" /* "-//IETF//DTD HTML Strict Level 2//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 3//en" /* "-//IETF//DTD HTML Strict Level 3//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict level 3//en//3.0" /* "-//IETF//DTD HTML Strict Level 3//EN//3.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict//en" /* "-//IETF//DTD HTML Strict//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict//en//2.0" /* "-//IETF//DTD HTML Strict//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html strict//en//3.0" /* "-//IETF//DTD HTML Strict//EN//3.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html//en" /* "-//IETF//DTD HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html//en//2.0" /* "-//IETF//DTD HTML//EN//2.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//ietf//dtd html//en//3.0" /* "-//IETF//DTD HTML//EN//3.0" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//metrius//dtd metrius presentational//en" /* "-//Metrius//DTD Metrius Presentational//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//microsoft//dtd internet explorer 2.0 html strict//en" /* "-//Microsoft//DTD Internet Explorer 2.0 HTML Strict//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//microsoft//dtd internet explorer 2.0 html//en" /* "-//Microsoft//DTD Internet Explorer 2.0 HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//microsoft//dtd internet explorer 2.0 tables//en" /* "-//Microsoft//DTD Internet Explorer 2.0 Tables//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//microsoft//dtd internet explorer 3.0 html strict//en" /* "-//Microsoft//DTD Internet Explorer 3.0 HTML Strict//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//microsoft//dtd internet explorer 3.0 html//en" /* "-//Microsoft//DTD Internet Explorer 3.0 HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//microsoft//dtd internet explorer 3.0 tables//en" /* "-//Microsoft//DTD Internet Explorer 3.0 Tables//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//netscape comm. corp.//dtd html//en" /* "-//Netscape Comm. Corp.//DTD HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//netscape comm. corp.//dtd strict html//en" /* "-//Netscape Comm. Corp.//DTD Strict HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//o'reilly and associates//dtd html 2.0//en" /* "-//O'Reilly and Associates//DTD HTML 2.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//o'reilly and associates//dtd html extended 1.0//en" /* "-//O'Reilly and Associates//DTD HTML Extended 1.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//o'reilly and associates//dtd html extended relaxed 1.0//en" /* "-//O'Reilly and Associates//DTD HTML Extended Relaxed 1.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//en" /* "-//SoftQuad Software//DTD HoTMetaL PRO 6.0::19990601::extensions to HTML 4.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//en" /* "-//SoftQuad//DTD HoTMetaL PRO 4.0::19971010::extensions to HTML 4.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//spyglass//dtd html 2.0 extended//en" /* "-//Spyglass//DTD HTML 2.0 Extended//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//sq//dtd html 2.0 hotmetal + extensions//en" /* "-//SQ//DTD HTML 2.0 HoTMetaL + extensions//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//sun microsystems corp.//dtd hotjava html//en" /* "-//Sun Microsystems Corp.//DTD HotJava HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//sun microsystems corp.//dtd hotjava strict html//en" /* "-//Sun Microsystems Corp.//DTD HotJava Strict HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 3 1995-03-24//en" /* "-//W3C//DTD HTML 3 1995-03-24//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 3.2 draft//en" /* "-//W3C//DTD HTML 3.2 Draft//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 3.2 final//en" /* "-//W3C//DTD HTML 3.2 Final//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 3.2//en" /* "-//W3C//DTD HTML 3.2//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 3.2s draft//en" /* "-//W3C//DTD HTML 3.2S Draft//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 4.0 frameset//en" /* "-//W3C//DTD HTML 4.0 Frameset//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 4.0 transitional//en" /* "-//W3C//DTD HTML 4.0 Transitional//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html 4.01 frameset//en" /* "-//W3C//DTD HTML 4.01 Frameset//EN" */, PubIDInfo::eQuirks, PubIDInfo::eAlmostStandards},
{"-//w3c//dtd html 4.01 transitional//en" /* "-//W3C//DTD HTML 4.01 Transitional//EN" */, PubIDInfo::eQuirks, PubIDInfo::eAlmostStandards},
{"-//w3c//dtd html experimental 19960712//en" /* "-//W3C//DTD HTML Experimental 19960712//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd html experimental 970421//en" /* "-//W3C//DTD HTML Experimental 970421//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd w3 html//en" /* "-//W3C//DTD W3 HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3c//dtd xhtml 1.0 frameset//en" /* "-//W3C//DTD XHTML 1.0 Frameset//EN" */, PubIDInfo::eAlmostStandards, PubIDInfo::eAlmostStandards},
{"-//w3c//dtd xhtml 1.0 transitional//en" /* "-//W3C//DTD XHTML 1.0 Transitional//EN" */, PubIDInfo::eAlmostStandards, PubIDInfo::eAlmostStandards},
{"-//w3o//dtd w3 html 3.0//en" /* "-//W3O//DTD W3 HTML 3.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3o//dtd w3 html 3.0//en//" /* "-//W3O//DTD W3 HTML 3.0//EN//" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//w3o//dtd w3 html strict 3.0//en//" /* "-//W3O//DTD W3 HTML Strict 3.0//EN//" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//webtechs//dtd mozilla html 2.0//en" /* "-//WebTechs//DTD Mozilla HTML 2.0//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-//webtechs//dtd mozilla html//en" /* "-//WebTechs//DTD Mozilla HTML//EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"-/w3c/dtd html 4.0 transitional/en" /* "-/W3C/DTD HTML 4.0 Transitional/EN" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
{"html" /* "HTML" */, PubIDInfo::eQuirks, PubIDInfo::eQuirks},
};
#ifdef DEBUG
static void
VerifyPublicIDs()
{
static bool gVerified = false;
if (!gVerified) {
gVerified = true;
uint32_t i;
for (i = 0; i < ELEMENTS_OF(kPublicIDs) - 1; ++i) {
if (nsCRT::strcmp(kPublicIDs[i].name, kPublicIDs[i+1].name) >= 0) {
NS_NOTREACHED("doctypes out of order");
printf("Doctypes %s and %s out of order.\n",
kPublicIDs[i].name, kPublicIDs[i+1].name);
}
}
for (i = 0; i < ELEMENTS_OF(kPublicIDs); ++i) {
nsAutoCString lcPubID(kPublicIDs[i].name);
ToLowerCase(lcPubID);
if (nsCRT::strcmp(kPublicIDs[i].name, lcPubID.get()) != 0) {
NS_NOTREACHED("doctype not lower case");
printf("Doctype %s not lower case.\n", kPublicIDs[i].name);
}
}
}
}
#endif
namespace {
struct PublicIdComparator
{
const nsAutoCString& mPublicId;
explicit PublicIdComparator(const nsAutoCString& aPublicId)
: mPublicId(aPublicId) {}
int operator()(const PubIDInfo& aInfo) const {
return nsCRT::strcmp(mPublicId.get(), aInfo.name);
}
};
} // namespace
static void
DetermineHTMLParseMode(const nsString& aBuffer,
nsDTDMode& aParseMode,
eParserDocType& aDocType)
{
#ifdef DEBUG
VerifyPublicIDs();
#endif
int32_t resultFlags;
nsAutoString publicIDUCS2, sysIDUCS2;
if (ParseDocTypeDecl(aBuffer, &resultFlags, publicIDUCS2, sysIDUCS2)) {
if (!(resultFlags & PARSE_DTD_HAVE_DOCTYPE)) {
// no DOCTYPE
aParseMode = eDTDMode_quirks;
aDocType = eHTML_Quirks;
} else if ((resultFlags & PARSE_DTD_HAVE_INTERNAL_SUBSET) ||
!(resultFlags & PARSE_DTD_HAVE_PUBLIC_ID)) {
// A doctype with an internal subset is always full_standards.
// A doctype without a public ID is always full_standards.
aDocType = eHTML_Strict;
aParseMode = eDTDMode_full_standards;
// Special hack for IBM's custom DOCTYPE.
if (!(resultFlags & PARSE_DTD_HAVE_INTERNAL_SUBSET) &&
sysIDUCS2.EqualsLiteral(
"http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd")) {
aParseMode = eDTDMode_quirks;
aDocType = eHTML_Quirks;
}
} else {
// We have to check our list of public IDs to see what to do.
// Yes, we want UCS2 to ASCII lossy conversion.
nsAutoCString publicID;
publicID.AssignWithConversion(publicIDUCS2);
// See comment above definition of kPublicIDs about case
// sensitivity.
ToLowerCase(publicID);
// Binary search to see if we can find the correct public ID.
size_t index;
bool found = BinarySearchIf(kPublicIDs, 0, ArrayLength(kPublicIDs),
PublicIdComparator(publicID), &index);
if (!found) {
// The DOCTYPE is not in our list, so it must be full_standards.
aParseMode = eDTDMode_full_standards;
aDocType = eHTML_Strict;
return;
}
switch ((resultFlags & PARSE_DTD_HAVE_SYSTEM_ID)
? kPublicIDs[index].mode_if_sysid
: kPublicIDs[index].mode_if_no_sysid)
{
case PubIDInfo::eQuirks:
aParseMode = eDTDMode_quirks;
aDocType = eHTML_Quirks;
break;
case PubIDInfo::eAlmostStandards:
aParseMode = eDTDMode_almost_standards;
aDocType = eHTML_Strict;
break;
case PubIDInfo::eFullStandards:
aParseMode = eDTDMode_full_standards;
aDocType = eHTML_Strict;
break;
default:
NS_NOTREACHED("no other cases!");
}
}
} else {
// badly formed DOCTYPE -> quirks
aParseMode = eDTDMode_quirks;
aDocType = eHTML_Quirks;
}
}
static void
DetermineParseMode(const nsString& aBuffer, nsDTDMode& aParseMode,
eParserDocType& aDocType, const nsACString& aMimeType)
{
if (aMimeType.EqualsLiteral(TEXT_HTML)) {
DetermineHTMLParseMode(aBuffer, aParseMode, aDocType);
} else if (nsContentUtils::IsPlainTextType(aMimeType)) {
aDocType = ePlainText;
aParseMode = eDTDMode_quirks;
} else { // Some form of XML
aDocType = eXML;
aParseMode = eDTDMode_full_standards;
}
}
static nsIDTD*
FindSuitableDTD(CParserContext& aParserContext)
{
// We always find a DTD.
aParserContext.mAutoDetectStatus = ePrimaryDetect;
// Quick check for view source.
MOZ_ASSERT(aParserContext.mParserCommand != eViewSource,
"The old parser is not supposed to be used for View Source "
"anymore.");
// Now see if we're parsing HTML (which, as far as we're concerned, simply
// means "not XML").
if (aParserContext.mDocType != eXML) {
return new CNavDTD();
}
// If we're here, then we'd better be parsing XML.
NS_ASSERTION(aParserContext.mDocType == eXML, "What are you trying to send me, here?");
return new nsExpatDriver();
}
NS_IMETHODIMP
nsParser::CancelParsingEvents()
{
if (mFlags & NS_PARSER_FLAG_PENDING_CONTINUE_EVENT) {
NS_ASSERTION(mContinueEvent, "mContinueEvent is null");
// Revoke the pending continue parsing event
mContinueEvent = nullptr;
mFlags &= ~NS_PARSER_FLAG_PENDING_CONTINUE_EVENT;
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
/**
* Evalutes EXPR1 and EXPR2 exactly once each, in that order. Stores the value
* of EXPR2 in RV is EXPR2 fails, otherwise RV contains the result of EXPR1
* (which could be success or failure).
*
* To understand the motivation for this construct, consider these example
* methods:
*
* nsresult nsSomething::DoThatThing(nsIWhatever* obj) {
* nsresult rv = NS_OK;
* ...
* return obj->DoThatThing();
* NS_ENSURE_SUCCESS(rv, rv);
* ...
* return rv;
* }
*
* void nsCaller::MakeThingsHappen() {
* return mSomething->DoThatThing(mWhatever);
* }
*
* Suppose, for whatever reason*, we want to shift responsibility for calling
* mWhatever->DoThatThing() from nsSomething::DoThatThing up to
* nsCaller::MakeThingsHappen. We might rewrite the two methods as follows:
*
* nsresult nsSomething::DoThatThing() {
* nsresult rv = NS_OK;
* ...
* ...
* return rv;
* }
*
* void nsCaller::MakeThingsHappen() {
* nsresult rv;
* PREFER_LATTER_ERROR_CODE(mSomething->DoThatThing(),
* mWhatever->DoThatThing(),
* rv);
* return rv;
* }
*
* *Possible reasons include: nsCaller doesn't want to give mSomething access
* to mWhatever, nsCaller wants to guarantee that mWhatever->DoThatThing() will
* be called regardless of how nsSomething::DoThatThing behaves, &c.
*/
#define PREFER_LATTER_ERROR_CODE(EXPR1, EXPR2, RV) { \
nsresult RV##__temp = EXPR1; \
RV = EXPR2; \
if (NS_FAILED(RV)) { \
RV = RV##__temp; \
} \
}
/**
* This gets called just prior to the model actually
* being constructed. It's important to make this the
* last thing that happens right before parsing, so we
* can delay until the last moment the resolution of
* which DTD to use (unless of course we're assigned one).
*/
nsresult
nsParser::WillBuildModel(nsString& aFilename)
{
if (!mParserContext)
return kInvalidParserContext;
if (eUnknownDetect != mParserContext->mAutoDetectStatus)
return NS_OK;
if (eDTDMode_unknown == mParserContext->mDTDMode ||
eDTDMode_autodetect == mParserContext->mDTDMode) {
char16_t buf[1025];
nsFixedString theBuffer(buf, 1024, 0);
// Grab 1024 characters, starting at the first non-whitespace
// character, to look for the doctype in.
mParserContext->mScanner->Peek(theBuffer, 1024, mParserContext->mScanner->FirstNonWhitespacePosition());
DetermineParseMode(theBuffer, mParserContext->mDTDMode,
mParserContext->mDocType, mParserContext->mMimeType);
}
NS_ASSERTION(!mDTD || !mParserContext->mPrevContext,
"Clobbering DTD for non-root parser context!");
mDTD = FindSuitableDTD(*mParserContext);
NS_ENSURE_TRUE(mDTD, NS_ERROR_OUT_OF_MEMORY);
nsITokenizer* tokenizer;
nsresult rv = mParserContext->GetTokenizer(mDTD, mSink, tokenizer);
NS_ENSURE_SUCCESS(rv, rv);
rv = mDTD->WillBuildModel(*mParserContext, tokenizer, mSink);
nsresult sinkResult = mSink->WillBuildModel(mDTD->GetMode());
// nsIDTD::WillBuildModel used to be responsible for calling
// nsIContentSink::WillBuildModel, but that obligation isn't expressible
// in the nsIDTD interface itself, so it's sounder and simpler to give that
// responsibility back to the parser. The former behavior of the DTD was to
// NS_ENSURE_SUCCESS the sink WillBuildModel call, so if the sink returns
// failure we should use sinkResult instead of rv, to preserve the old error
// handling behavior of the DTD:
return NS_FAILED(sinkResult) ? sinkResult : rv;
}
/**
* This gets called when the parser is done with its input.
* Note that the parser may have been called recursively, so we
* have to check for a prev. context before closing out the DTD/sink.
*/
nsresult
nsParser::DidBuildModel(nsresult anErrorCode)
{
nsresult result = anErrorCode;
if (IsComplete()) {
if (mParserContext && !mParserContext->mPrevContext) {
// Let sink know if we're about to end load because we've been terminated.
// In that case we don't want it to run deferred scripts.
bool terminated = mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING;
if (mDTD && mSink) {
nsresult dtdResult = mDTD->DidBuildModel(anErrorCode),
sinkResult = mSink->DidBuildModel(terminated);
// nsIDTD::DidBuildModel used to be responsible for calling
// nsIContentSink::DidBuildModel, but that obligation isn't expressible
// in the nsIDTD interface itself, so it's sounder and simpler to give
// that responsibility back to the parser. The former behavior of the
// DTD was to NS_ENSURE_SUCCESS the sink DidBuildModel call, so if the
// sink returns failure we should use sinkResult instead of dtdResult,
// to preserve the old error handling behavior of the DTD:
result = NS_FAILED(sinkResult) ? sinkResult : dtdResult;
}
//Ref. to bug 61462.
mParserContext->mRequest = 0;
}
}
return result;
}
/**
* This method adds a new parser context to the list,
* pushing the current one to the next position.
*
* @param ptr to new context
*/
void
nsParser::PushContext(CParserContext& aContext)
{
NS_ASSERTION(aContext.mPrevContext == mParserContext,
"Trying to push a context whose previous context differs from "
"the current parser context.");
mParserContext = &aContext;
}
/**
* This method pops the topmost context off the stack,
* returning it to the user. The next context (if any)
* becomes the current context.
* @update gess7/22/98
* @return prev. context
*/
CParserContext*
nsParser::PopContext()
{
CParserContext* oldContext = mParserContext;
if (oldContext) {
mParserContext = oldContext->mPrevContext;
if (mParserContext) {
// If the old context was blocked, propagate the blocked state
// back to the new one. Also, propagate the stream listener state
// but don't override onStop state to guarantee the call to DidBuildModel().
if (mParserContext->mStreamListenerState != eOnStop) {
mParserContext->mStreamListenerState = oldContext->mStreamListenerState;
}
}
}
return oldContext;
}
/**
* Call this when you want control whether or not the parser will parse
* and tokenize input (TRUE), or whether it just caches input to be
* parsed later (FALSE).
*
* @param aState determines whether we parse/tokenize or just cache.
* @return current state
*/
void
nsParser::SetUnusedInput(nsString& aBuffer)
{
mUnusedInput = aBuffer;
}
/**
* Call this when you want to *force* the parser to terminate the
* parsing process altogether. This is binary -- so once you terminate
* you can't resume without restarting altogether.
*/
NS_IMETHODIMP
nsParser::Terminate(void)
{
// We should only call DidBuildModel once, so don't do anything if this is
// the second time that Terminate has been called.
if (mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING) {
return NS_OK;
}
nsresult result = NS_OK;
// XXX - [ until we figure out a way to break parser-sink circularity ]
// Hack - Hold a reference until we are completely done...
nsCOMPtr<nsIParser> kungFuDeathGrip(this);
mInternalState = result = NS_ERROR_HTMLPARSER_STOPPARSING;
// CancelParsingEvents must be called to avoid leaking the nsParser object
// @see bug 108049
// If NS_PARSER_FLAG_PENDING_CONTINUE_EVENT is set then CancelParsingEvents
// will reset it so DidBuildModel will call DidBuildModel on the DTD. Note:
// The IsComplete() call inside of DidBuildModel looks at the pendingContinueEvents flag.
CancelParsingEvents();