Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 sw=2 et tw=78: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef nsHtml5DocumentBuilder_h |
michael@0 | 8 | #define nsHtml5DocumentBuilder_h |
michael@0 | 9 | |
michael@0 | 10 | #include "nsHtml5PendingNotification.h" |
michael@0 | 11 | #include "nsContentSink.h" |
michael@0 | 12 | #include "nsHtml5DocumentMode.h" |
michael@0 | 13 | #include "nsIDocument.h" |
michael@0 | 14 | |
michael@0 | 15 | typedef nsIContent* nsIContentPtr; |
michael@0 | 16 | |
michael@0 | 17 | enum eHtml5FlushState { |
michael@0 | 18 | eNotFlushing = 0, // not flushing |
michael@0 | 19 | eInFlush = 1, // the Flush() method is on the call stack |
michael@0 | 20 | eInDocUpdate = 2, // inside an update batch on the document |
michael@0 | 21 | eNotifying = 3 // flushing pending append notifications |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | class nsHtml5DocumentBuilder : public nsContentSink |
michael@0 | 25 | { |
michael@0 | 26 | public: |
michael@0 | 27 | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHtml5DocumentBuilder, |
michael@0 | 28 | nsContentSink) |
michael@0 | 29 | |
michael@0 | 30 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 31 | |
michael@0 | 32 | inline void HoldElement(nsIContent* aContent) |
michael@0 | 33 | { |
michael@0 | 34 | mOwnedElements.AppendElement(aContent); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | inline bool HaveNotified(nsIContent* aNode) |
michael@0 | 38 | { |
michael@0 | 39 | NS_PRECONDITION(aNode, "HaveNotified called with null argument."); |
michael@0 | 40 | const nsHtml5PendingNotification* start = mPendingNotifications.Elements(); |
michael@0 | 41 | const nsHtml5PendingNotification* end = start + mPendingNotifications.Length(); |
michael@0 | 42 | for (;;) { |
michael@0 | 43 | nsIContent* parent = aNode->GetParent(); |
michael@0 | 44 | if (!parent) { |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | for (nsHtml5PendingNotification* iter = (nsHtml5PendingNotification*)start; iter < end; ++iter) { |
michael@0 | 48 | if (iter->Contains(parent)) { |
michael@0 | 49 | return iter->HaveNotifiedIndex(parent->IndexOf(aNode)); |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | aNode = parent; |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | void PostPendingAppendNotification(nsIContent* aParent, nsIContent* aChild) |
michael@0 | 57 | { |
michael@0 | 58 | bool newParent = true; |
michael@0 | 59 | const nsIContentPtr* first = mElementsSeenInThisAppendBatch.Elements(); |
michael@0 | 60 | const nsIContentPtr* last = first + mElementsSeenInThisAppendBatch.Length() - 1; |
michael@0 | 61 | for (const nsIContentPtr* iter = last; iter >= first; --iter) { |
michael@0 | 62 | #ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH |
michael@0 | 63 | sAppendBatchSlotsExamined++; |
michael@0 | 64 | #endif |
michael@0 | 65 | if (*iter == aParent) { |
michael@0 | 66 | newParent = false; |
michael@0 | 67 | break; |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | if (aChild->IsElement()) { |
michael@0 | 71 | mElementsSeenInThisAppendBatch.AppendElement(aChild); |
michael@0 | 72 | } |
michael@0 | 73 | mElementsSeenInThisAppendBatch.AppendElement(aParent); |
michael@0 | 74 | if (newParent) { |
michael@0 | 75 | mPendingNotifications.AppendElement(aParent); |
michael@0 | 76 | } |
michael@0 | 77 | #ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH |
michael@0 | 78 | sAppendBatchExaminations++; |
michael@0 | 79 | #endif |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void FlushPendingAppendNotifications() |
michael@0 | 83 | { |
michael@0 | 84 | NS_PRECONDITION(mFlushState == eInDocUpdate, "Notifications flushed outside update"); |
michael@0 | 85 | mFlushState = eNotifying; |
michael@0 | 86 | const nsHtml5PendingNotification* start = mPendingNotifications.Elements(); |
michael@0 | 87 | const nsHtml5PendingNotification* end = start + mPendingNotifications.Length(); |
michael@0 | 88 | for (nsHtml5PendingNotification* iter = (nsHtml5PendingNotification*)start; iter < end; ++iter) { |
michael@0 | 89 | iter->Fire(); |
michael@0 | 90 | } |
michael@0 | 91 | mPendingNotifications.Clear(); |
michael@0 | 92 | #ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH |
michael@0 | 93 | if (mElementsSeenInThisAppendBatch.Length() > sAppendBatchMaxSize) { |
michael@0 | 94 | sAppendBatchMaxSize = mElementsSeenInThisAppendBatch.Length(); |
michael@0 | 95 | } |
michael@0 | 96 | #endif |
michael@0 | 97 | mElementsSeenInThisAppendBatch.Clear(); |
michael@0 | 98 | NS_ASSERTION(mFlushState == eNotifying, "mFlushState out of sync"); |
michael@0 | 99 | mFlushState = eInDocUpdate; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | nsresult Init(nsIDocument* aDoc, nsIURI* aURI, |
michael@0 | 103 | nsISupports* aContainer, nsIChannel* aChannel); |
michael@0 | 104 | |
michael@0 | 105 | // Getters and setters for fields from nsContentSink |
michael@0 | 106 | nsIDocument* GetDocument() |
michael@0 | 107 | { |
michael@0 | 108 | return mDocument; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | nsNodeInfoManager* GetNodeInfoManager() |
michael@0 | 112 | { |
michael@0 | 113 | return mNodeInfoManager; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Marks this parser as broken and tells the stream parser (if any) to |
michael@0 | 118 | * terminate. |
michael@0 | 119 | * |
michael@0 | 120 | * @return aReason for convenience |
michael@0 | 121 | */ |
michael@0 | 122 | virtual nsresult MarkAsBroken(nsresult aReason); |
michael@0 | 123 | |
michael@0 | 124 | /** |
michael@0 | 125 | * Checks if this parser is broken. Returns a non-NS_OK (i.e. non-0) |
michael@0 | 126 | * value if broken. |
michael@0 | 127 | */ |
michael@0 | 128 | inline nsresult IsBroken() |
michael@0 | 129 | { |
michael@0 | 130 | return mBroken; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | inline void BeginDocUpdate() |
michael@0 | 134 | { |
michael@0 | 135 | NS_PRECONDITION(mFlushState == eInFlush, "Tried to double-open update."); |
michael@0 | 136 | NS_PRECONDITION(mParser, "Started update without parser."); |
michael@0 | 137 | mFlushState = eInDocUpdate; |
michael@0 | 138 | mDocument->BeginUpdate(UPDATE_CONTENT_MODEL); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | inline void EndDocUpdate() |
michael@0 | 142 | { |
michael@0 | 143 | NS_PRECONDITION(mFlushState != eNotifying, "mFlushState out of sync"); |
michael@0 | 144 | if (mFlushState == eInDocUpdate) { |
michael@0 | 145 | FlushPendingAppendNotifications(); |
michael@0 | 146 | mFlushState = eInFlush; |
michael@0 | 147 | mDocument->EndUpdate(UPDATE_CONTENT_MODEL); |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | void SetDocumentCharsetAndSource(nsACString& aCharset, int32_t aCharsetSource); |
michael@0 | 152 | |
michael@0 | 153 | /** |
michael@0 | 154 | * Sets up style sheet load / parse |
michael@0 | 155 | */ |
michael@0 | 156 | void UpdateStyleSheet(nsIContent* aElement); |
michael@0 | 157 | |
michael@0 | 158 | void SetDocumentMode(nsHtml5DocumentMode m); |
michael@0 | 159 | |
michael@0 | 160 | void SetNodeInfoManager(nsNodeInfoManager* aManager) |
michael@0 | 161 | { |
michael@0 | 162 | mNodeInfoManager = aManager; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | // nsContentSink methods |
michael@0 | 166 | virtual void UpdateChildCounts(); |
michael@0 | 167 | virtual nsresult FlushTags(); |
michael@0 | 168 | |
michael@0 | 169 | protected: |
michael@0 | 170 | inline void SetAppendBatchCapacity(uint32_t aCapacity) |
michael@0 | 171 | { |
michael@0 | 172 | mElementsSeenInThisAppendBatch.SetCapacity(aCapacity); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | nsHtml5DocumentBuilder(bool aRunsToCompletion); |
michael@0 | 176 | virtual ~nsHtml5DocumentBuilder(); |
michael@0 | 177 | |
michael@0 | 178 | private: |
michael@0 | 179 | nsTArray<nsHtml5PendingNotification> mPendingNotifications; |
michael@0 | 180 | nsTArray<nsIContentPtr> mElementsSeenInThisAppendBatch; |
michael@0 | 181 | protected: |
michael@0 | 182 | nsTArray<nsCOMPtr<nsIContent> > mOwnedElements; |
michael@0 | 183 | /** |
michael@0 | 184 | * Non-NS_OK if this parser should refuse to process any more input. |
michael@0 | 185 | * For example, the parser needs to be marked as broken if it drops some |
michael@0 | 186 | * input due to a memory allocation failure. In such a case, the whole |
michael@0 | 187 | * parser needs to be marked as broken, because some input has been lost |
michael@0 | 188 | * and parsing more input could lead to a DOM where pieces of HTML source |
michael@0 | 189 | * that weren't supposed to become scripts become scripts. |
michael@0 | 190 | * |
michael@0 | 191 | * Since NS_OK is actually 0, zeroing operator new takes care of |
michael@0 | 192 | * initializing this. |
michael@0 | 193 | */ |
michael@0 | 194 | nsresult mBroken; |
michael@0 | 195 | eHtml5FlushState mFlushState; |
michael@0 | 196 | }; |
michael@0 | 197 | |
michael@0 | 198 | #endif // nsHtml5DocumentBuilder_h |