Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 sw=2 et tw=78: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef nsHtml5DocumentBuilder_h
8 : #define nsHtml5DocumentBuilder_h
9 :
10 : #include "nsContentSink.h"
11 : #include "nsHtml5DocumentMode.h"
12 : #include "nsIDocument.h"
13 : #include "nsIContent.h"
14 :
15 : typedef nsIContent* nsIContentPtr;
16 :
17 : enum eHtml5FlushState {
18 : eNotFlushing = 0, // not flushing
19 : eInFlush = 1, // the Flush() method is on the call stack
20 : eInDocUpdate = 2, // inside an update batch on the document
21 : eNotifying = 3 // flushing pending append notifications
22 : };
23 :
24 : class nsHtml5DocumentBuilder : public nsContentSink
25 : {
26 : using Encoding = mozilla::Encoding;
27 : template <typename T> using NotNull = mozilla::NotNull<T>;
28 : public:
29 2 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHtml5DocumentBuilder,
30 : nsContentSink)
31 :
32 : NS_DECL_ISUPPORTS_INHERITED
33 :
34 14 : inline void HoldElement(already_AddRefed<nsIContent> aContent)
35 : {
36 14 : *(mOwnedElements.AppendElement()) = aContent;
37 14 : }
38 :
39 : nsresult Init(nsIDocument* aDoc, nsIURI* aURI,
40 : nsISupports* aContainer, nsIChannel* aChannel);
41 :
42 : // Getters and setters for fields from nsContentSink
43 43 : nsIDocument* GetDocument()
44 : {
45 43 : return mDocument;
46 : }
47 :
48 5 : nsNodeInfoManager* GetNodeInfoManager()
49 : {
50 5 : return mNodeInfoManager;
51 : }
52 :
53 : /**
54 : * Marks this parser as broken and tells the stream parser (if any) to
55 : * terminate.
56 : *
57 : * @return aReason for convenience
58 : */
59 : virtual nsresult MarkAsBroken(nsresult aReason);
60 :
61 : /**
62 : * Checks if this parser is broken. Returns a non-NS_OK (i.e. non-0)
63 : * value if broken.
64 : */
65 31 : inline nsresult IsBroken()
66 : {
67 31 : return mBroken;
68 : }
69 :
70 11 : inline void BeginDocUpdate()
71 : {
72 11 : NS_PRECONDITION(mFlushState == eInFlush, "Tried to double-open update.");
73 11 : NS_PRECONDITION(mParser, "Started update without parser.");
74 11 : mFlushState = eInDocUpdate;
75 11 : mDocument->BeginUpdate(UPDATE_CONTENT_MODEL);
76 11 : }
77 :
78 13 : inline void EndDocUpdate()
79 : {
80 13 : NS_PRECONDITION(mFlushState != eNotifying, "mFlushState out of sync");
81 13 : if (mFlushState == eInDocUpdate) {
82 11 : mFlushState = eInFlush;
83 11 : mDocument->EndUpdate(UPDATE_CONTENT_MODEL);
84 : }
85 13 : }
86 :
87 32 : bool IsInDocUpdate()
88 : {
89 32 : return mFlushState == eInDocUpdate;
90 : }
91 :
92 : void SetDocumentCharsetAndSource(NotNull<const Encoding*> aEncoding,
93 : int32_t aCharsetSource);
94 :
95 : /**
96 : * Sets up style sheet load / parse
97 : */
98 : void UpdateStyleSheet(nsIContent* aElement);
99 :
100 : void SetDocumentMode(nsHtml5DocumentMode m);
101 :
102 0 : void SetNodeInfoManager(nsNodeInfoManager* aManager)
103 : {
104 0 : mNodeInfoManager = aManager;
105 0 : }
106 :
107 : // nsContentSink methods
108 : virtual void UpdateChildCounts() override;
109 : virtual nsresult FlushTags() override;
110 :
111 : protected:
112 :
113 : explicit nsHtml5DocumentBuilder(bool aRunsToCompletion);
114 : virtual ~nsHtml5DocumentBuilder();
115 :
116 : protected:
117 : AutoTArray<nsCOMPtr<nsIContent>, 32> mOwnedElements;
118 : /**
119 : * Non-NS_OK if this parser should refuse to process any more input.
120 : * For example, the parser needs to be marked as broken if it drops some
121 : * input due to a memory allocation failure. In such a case, the whole
122 : * parser needs to be marked as broken, because some input has been lost
123 : * and parsing more input could lead to a DOM where pieces of HTML source
124 : * that weren't supposed to become scripts become scripts.
125 : */
126 : nsresult mBroken;
127 : eHtml5FlushState mFlushState;
128 : };
129 :
130 : #endif // nsHtml5DocumentBuilder_h
|