Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/parser/html/nsHtml5DocumentBuilder.h
Line
Count
Source (jump to first uncovered line)
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
{
19
  eNotFlushing = 0, // not flushing
20
  eInFlush = 1,     // the Flush() method is on the call stack
21
  eInDocUpdate = 2, // inside an update batch on the document
22
};
23
24
class nsHtml5DocumentBuilder : public nsContentSink
25
{
26
  using Encoding = mozilla::Encoding;
27
  template<typename T>
28
  using NotNull = mozilla::NotNull<T>;
29
30
public:
31
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHtml5DocumentBuilder,
32
                                           nsContentSink)
33
34
  NS_DECL_ISUPPORTS_INHERITED
35
36
  inline void HoldElement(already_AddRefed<nsIContent> aContent)
37
0
  {
38
0
    *(mOwnedElements.AppendElement()) = aContent;
39
0
  }
40
41
  nsresult Init(nsIDocument* aDoc,
42
                nsIURI* aURI,
43
                nsISupports* aContainer,
44
                nsIChannel* aChannel);
45
46
  // Getters and setters for fields from nsContentSink
47
0
  nsIDocument* GetDocument() { return mDocument; }
48
49
0
  nsNodeInfoManager* GetNodeInfoManager() { return mNodeInfoManager; }
50
51
  /**
52
   * Marks this parser as broken and tells the stream parser (if any) to
53
   * terminate.
54
   *
55
   * @return aReason for convenience
56
   */
57
  virtual nsresult MarkAsBroken(nsresult aReason);
58
59
  /**
60
   * Checks if this parser is broken. Returns a non-NS_OK (i.e. non-0)
61
   * value if broken.
62
   */
63
0
  inline nsresult IsBroken() { return mBroken; }
64
65
0
  inline bool IsComplete() { return !mParser; }
66
67
  inline void BeginDocUpdate()
68
0
  {
69
0
    MOZ_RELEASE_ASSERT(IsInFlush(), "Tried to double-open doc update.");
70
0
    MOZ_RELEASE_ASSERT(mParser, "Started doc update without parser.");
71
0
    mFlushState = eInDocUpdate;
72
0
    mDocument->BeginUpdate();
73
0
  }
74
75
  inline void EndDocUpdate()
76
0
  {
77
0
    MOZ_RELEASE_ASSERT(IsInDocUpdate(),
78
0
                       "Tried to end doc update without one open.");
79
0
    mFlushState = eInFlush;
80
0
    mDocument->EndUpdate();
81
0
  }
82
83
  inline void BeginFlush()
84
0
  {
85
0
    MOZ_RELEASE_ASSERT(mFlushState == eNotFlushing,
86
0
                       "Tried to start a flush when already flushing.");
87
0
    MOZ_RELEASE_ASSERT(mParser, "Started a flush without parser.");
88
0
    mFlushState = eInFlush;
89
0
  }
90
91
  inline void EndFlush()
92
0
  {
93
0
    MOZ_RELEASE_ASSERT(IsInFlush(), "Tried to end flush when not flushing.");
94
0
    mFlushState = eNotFlushing;
95
0
  }
96
97
0
  inline bool IsInDocUpdate() { return mFlushState == eInDocUpdate; }
98
99
0
  inline bool IsInFlush() { return mFlushState == eInFlush; }
100
101
  void SetDocumentCharsetAndSource(NotNull<const Encoding*> aEncoding,
102
                                   int32_t aCharsetSource);
103
104
  /**
105
   * Sets up style sheet load / parse
106
   */
107
  void UpdateStyleSheet(nsIContent* aElement);
108
109
  void SetDocumentMode(nsHtml5DocumentMode m);
110
111
  void SetNodeInfoManager(nsNodeInfoManager* aManager)
112
0
  {
113
0
    mNodeInfoManager = aManager;
114
0
  }
115
116
  // nsContentSink methods
117
  virtual void UpdateChildCounts() override;
118
  virtual nsresult FlushTags() override;
119
120
protected:
121
  explicit nsHtml5DocumentBuilder(bool aRunsToCompletion);
122
  virtual ~nsHtml5DocumentBuilder();
123
124
protected:
125
  AutoTArray<nsCOMPtr<nsIContent>, 32> mOwnedElements;
126
  /**
127
   * Non-NS_OK if this parser should refuse to process any more input.
128
   * For example, the parser needs to be marked as broken if it drops some
129
   * input due to a memory allocation failure. In such a case, the whole
130
   * parser needs to be marked as broken, because some input has been lost
131
   * and parsing more input could lead to a DOM where pieces of HTML source
132
   * that weren't supposed to become scripts become scripts.
133
   */
134
  nsresult mBroken;
135
  eHtml5FlushState mFlushState;
136
};
137
138
#endif // nsHtml5DocumentBuilder_h