Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef NS_HTML5_PARSER |
michael@0 | 7 | #define NS_HTML5_PARSER |
michael@0 | 8 | |
michael@0 | 9 | #include "nsAutoPtr.h" |
michael@0 | 10 | #include "nsIParser.h" |
michael@0 | 11 | #include "nsDeque.h" |
michael@0 | 12 | #include "nsIURL.h" |
michael@0 | 13 | #include "nsParserCIID.h" |
michael@0 | 14 | #include "nsITokenizer.h" |
michael@0 | 15 | #include "nsIContentSink.h" |
michael@0 | 16 | #include "nsIRequest.h" |
michael@0 | 17 | #include "nsIChannel.h" |
michael@0 | 18 | #include "nsCOMArray.h" |
michael@0 | 19 | #include "nsContentSink.h" |
michael@0 | 20 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 21 | #include "nsIInputStream.h" |
michael@0 | 22 | #include "nsDetectionConfident.h" |
michael@0 | 23 | #include "nsHtml5OwningUTF16Buffer.h" |
michael@0 | 24 | #include "nsHtml5TreeOpExecutor.h" |
michael@0 | 25 | #include "nsHtml5StreamParser.h" |
michael@0 | 26 | #include "nsHtml5AtomTable.h" |
michael@0 | 27 | #include "nsWeakReference.h" |
michael@0 | 28 | #include "nsHtml5StreamListener.h" |
michael@0 | 29 | |
michael@0 | 30 | class nsHtml5Parser : public nsIParser, |
michael@0 | 31 | public nsSupportsWeakReference |
michael@0 | 32 | { |
michael@0 | 33 | public: |
michael@0 | 34 | NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW |
michael@0 | 35 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 36 | |
michael@0 | 37 | NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsHtml5Parser, nsIParser) |
michael@0 | 38 | |
michael@0 | 39 | nsHtml5Parser(); |
michael@0 | 40 | virtual ~nsHtml5Parser(); |
michael@0 | 41 | |
michael@0 | 42 | /* Start nsIParser */ |
michael@0 | 43 | /** |
michael@0 | 44 | * No-op for backwards compat. |
michael@0 | 45 | */ |
michael@0 | 46 | NS_IMETHOD_(void) SetContentSink(nsIContentSink* aSink); |
michael@0 | 47 | |
michael@0 | 48 | /** |
michael@0 | 49 | * Returns the tree op executor for backwards compat. |
michael@0 | 50 | */ |
michael@0 | 51 | NS_IMETHOD_(nsIContentSink*) GetContentSink(); |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Always returns "view" for backwards compat. |
michael@0 | 55 | */ |
michael@0 | 56 | NS_IMETHOD_(void) GetCommand(nsCString& aCommand); |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * No-op for backwards compat. |
michael@0 | 60 | */ |
michael@0 | 61 | NS_IMETHOD_(void) SetCommand(const char* aCommand); |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * No-op for backwards compat. |
michael@0 | 65 | */ |
michael@0 | 66 | NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand); |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Call this method once you've created a parser, and want to instruct it |
michael@0 | 70 | * about what charset to load |
michael@0 | 71 | * |
michael@0 | 72 | * @param aCharset the charset of a document |
michael@0 | 73 | * @param aCharsetSource the source of the charset |
michael@0 | 74 | */ |
michael@0 | 75 | NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, int32_t aSource); |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * Don't call. For interface compat only. |
michael@0 | 79 | */ |
michael@0 | 80 | NS_IMETHOD_(void) GetDocumentCharset(nsACString& aCharset, int32_t& aSource) |
michael@0 | 81 | { |
michael@0 | 82 | NS_NOTREACHED("No one should call this."); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Get the channel associated with this parser |
michael@0 | 87 | * @param aChannel out param that will contain the result |
michael@0 | 88 | * @return NS_OK if successful or NS_NOT_AVAILABLE if not |
michael@0 | 89 | */ |
michael@0 | 90 | NS_IMETHOD GetChannel(nsIChannel** aChannel); |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Return |this| for backwards compat. |
michael@0 | 94 | */ |
michael@0 | 95 | NS_IMETHOD GetDTD(nsIDTD** aDTD); |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Get the stream parser for this parser |
michael@0 | 99 | */ |
michael@0 | 100 | virtual nsIStreamListener* GetStreamListener(); |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Don't call. For interface compat only. |
michael@0 | 104 | */ |
michael@0 | 105 | NS_IMETHOD ContinueInterruptedParsing(); |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Blocks the parser. |
michael@0 | 109 | */ |
michael@0 | 110 | NS_IMETHOD_(void) BlockParser(); |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Unblocks the parser. |
michael@0 | 114 | */ |
michael@0 | 115 | NS_IMETHOD_(void) UnblockParser(); |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * Asynchronously continues parsing. |
michael@0 | 119 | */ |
michael@0 | 120 | NS_IMETHOD_(void) ContinueInterruptedParsingAsync(); |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * Query whether the parser is enabled (i.e. not blocked) or not. |
michael@0 | 124 | */ |
michael@0 | 125 | NS_IMETHOD_(bool) IsParserEnabled(); |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Query whether the parser thinks it's done with parsing. |
michael@0 | 129 | */ |
michael@0 | 130 | NS_IMETHOD_(bool) IsComplete(); |
michael@0 | 131 | |
michael@0 | 132 | /** |
michael@0 | 133 | * Set up request observer. |
michael@0 | 134 | * |
michael@0 | 135 | * @param aURL used for View Source title |
michael@0 | 136 | * @param aListener a listener to forward notifications to |
michael@0 | 137 | * @param aKey the root context key (used for document.write) |
michael@0 | 138 | * @param aMode ignored (for interface compat only) |
michael@0 | 139 | */ |
michael@0 | 140 | NS_IMETHOD Parse(nsIURI* aURL, |
michael@0 | 141 | nsIRequestObserver* aListener = nullptr, |
michael@0 | 142 | void* aKey = 0, |
michael@0 | 143 | nsDTDMode aMode = eDTDMode_autodetect); |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * document.write and document.close |
michael@0 | 147 | * |
michael@0 | 148 | * @param aSourceBuffer the argument of document.write (empty for .close()) |
michael@0 | 149 | * @param aKey a key unique to the script element that caused this call |
michael@0 | 150 | * @param aContentType "text/html" for HTML mode, else text/plain mode |
michael@0 | 151 | * @param aLastCall true if .close() false if .write() |
michael@0 | 152 | * @param aMode ignored (for interface compat only) |
michael@0 | 153 | */ |
michael@0 | 154 | NS_IMETHOD Parse(const nsAString& aSourceBuffer, |
michael@0 | 155 | void* aKey, |
michael@0 | 156 | const nsACString& aContentType, |
michael@0 | 157 | bool aLastCall, |
michael@0 | 158 | nsDTDMode aMode = eDTDMode_autodetect); |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Stops the parser prematurely |
michael@0 | 162 | */ |
michael@0 | 163 | NS_IMETHOD Terminate(); |
michael@0 | 164 | |
michael@0 | 165 | /** |
michael@0 | 166 | * Don't call. For interface backwards compat only. |
michael@0 | 167 | */ |
michael@0 | 168 | NS_IMETHOD ParseFragment(const nsAString& aSourceBuffer, |
michael@0 | 169 | nsTArray<nsString>& aTagStack); |
michael@0 | 170 | |
michael@0 | 171 | /** |
michael@0 | 172 | * Don't call. For interface compat only. |
michael@0 | 173 | */ |
michael@0 | 174 | NS_IMETHOD BuildModel(); |
michael@0 | 175 | |
michael@0 | 176 | /** |
michael@0 | 177 | * Don't call. For interface compat only. |
michael@0 | 178 | */ |
michael@0 | 179 | NS_IMETHODIMP CancelParsingEvents(); |
michael@0 | 180 | |
michael@0 | 181 | /** |
michael@0 | 182 | * Don't call. For interface compat only. |
michael@0 | 183 | */ |
michael@0 | 184 | virtual void Reset(); |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * True in fragment mode and during synchronous document.write |
michael@0 | 188 | */ |
michael@0 | 189 | virtual bool CanInterrupt(); |
michael@0 | 190 | |
michael@0 | 191 | /** |
michael@0 | 192 | * True if the insertion point (per HTML5) is defined. |
michael@0 | 193 | */ |
michael@0 | 194 | virtual bool IsInsertionPointDefined(); |
michael@0 | 195 | |
michael@0 | 196 | /** |
michael@0 | 197 | * Call immediately before starting to evaluate a parser-inserted script. |
michael@0 | 198 | */ |
michael@0 | 199 | virtual void BeginEvaluatingParserInsertedScript(); |
michael@0 | 200 | |
michael@0 | 201 | /** |
michael@0 | 202 | * Call immediately after having evaluated a parser-inserted script. |
michael@0 | 203 | */ |
michael@0 | 204 | virtual void EndEvaluatingParserInsertedScript(); |
michael@0 | 205 | |
michael@0 | 206 | /** |
michael@0 | 207 | * Marks the HTML5 parser as not a script-created parser: Prepares the |
michael@0 | 208 | * parser to be able to read a stream. |
michael@0 | 209 | * |
michael@0 | 210 | * @param aCommand the parser command (Yeah, this is bad API design. Let's |
michael@0 | 211 | * make this better when retiring nsIParser) |
michael@0 | 212 | */ |
michael@0 | 213 | virtual void MarkAsNotScriptCreated(const char* aCommand); |
michael@0 | 214 | |
michael@0 | 215 | /** |
michael@0 | 216 | * True if this is a script-created HTML5 parser. |
michael@0 | 217 | */ |
michael@0 | 218 | virtual bool IsScriptCreated(); |
michael@0 | 219 | |
michael@0 | 220 | /* End nsIParser */ |
michael@0 | 221 | |
michael@0 | 222 | // Not from an external interface |
michael@0 | 223 | // Non-inherited methods |
michael@0 | 224 | |
michael@0 | 225 | public: |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Initializes the parser to load from a channel. |
michael@0 | 229 | */ |
michael@0 | 230 | virtual nsresult Initialize(nsIDocument* aDoc, |
michael@0 | 231 | nsIURI* aURI, |
michael@0 | 232 | nsISupports* aContainer, |
michael@0 | 233 | nsIChannel* aChannel); |
michael@0 | 234 | |
michael@0 | 235 | inline nsHtml5Tokenizer* GetTokenizer() { |
michael@0 | 236 | return mTokenizer; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | void InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, int32_t aLine); |
michael@0 | 240 | |
michael@0 | 241 | void DropStreamParser() |
michael@0 | 242 | { |
michael@0 | 243 | if (GetStreamParser()) { |
michael@0 | 244 | GetStreamParser()->DropTimer(); |
michael@0 | 245 | mStreamListener->DropDelegate(); |
michael@0 | 246 | mStreamListener = nullptr; |
michael@0 | 247 | } |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | void StartTokenizer(bool aScriptingEnabled); |
michael@0 | 251 | |
michael@0 | 252 | void ContinueAfterFailedCharsetSwitch(); |
michael@0 | 253 | |
michael@0 | 254 | nsHtml5StreamParser* GetStreamParser() |
michael@0 | 255 | { |
michael@0 | 256 | if (!mStreamListener) { |
michael@0 | 257 | return nullptr; |
michael@0 | 258 | } |
michael@0 | 259 | return mStreamListener->GetDelegate(); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | /** |
michael@0 | 263 | * Parse until pending data is exhausted or a script blocks the parser |
michael@0 | 264 | */ |
michael@0 | 265 | nsresult ParseUntilBlocked(); |
michael@0 | 266 | |
michael@0 | 267 | private: |
michael@0 | 268 | |
michael@0 | 269 | // State variables |
michael@0 | 270 | |
michael@0 | 271 | /** |
michael@0 | 272 | * Whether the last character tokenized was a carriage return (for CRLF) |
michael@0 | 273 | */ |
michael@0 | 274 | bool mLastWasCR; |
michael@0 | 275 | |
michael@0 | 276 | /** |
michael@0 | 277 | * Whether the last character tokenized was a carriage return (for CRLF) |
michael@0 | 278 | * when preparsing document.write. |
michael@0 | 279 | */ |
michael@0 | 280 | bool mDocWriteSpeculativeLastWasCR; |
michael@0 | 281 | |
michael@0 | 282 | /** |
michael@0 | 283 | * The parser is blocking on a script |
michael@0 | 284 | */ |
michael@0 | 285 | bool mBlocked; |
michael@0 | 286 | |
michael@0 | 287 | /** |
michael@0 | 288 | * Whether the document.write() speculator is already active. |
michael@0 | 289 | */ |
michael@0 | 290 | bool mDocWriteSpeculatorActive; |
michael@0 | 291 | |
michael@0 | 292 | /** |
michael@0 | 293 | * The number of parser-inserted script currently being evaluated. |
michael@0 | 294 | */ |
michael@0 | 295 | int32_t mParserInsertedScriptsBeingEvaluated; |
michael@0 | 296 | |
michael@0 | 297 | /** |
michael@0 | 298 | * True if document.close() has been called. |
michael@0 | 299 | */ |
michael@0 | 300 | bool mDocumentClosed; |
michael@0 | 301 | |
michael@0 | 302 | bool mInDocumentWrite; |
michael@0 | 303 | |
michael@0 | 304 | // Portable parser objects |
michael@0 | 305 | /** |
michael@0 | 306 | * The first buffer in the pending UTF-16 buffer queue |
michael@0 | 307 | */ |
michael@0 | 308 | nsRefPtr<nsHtml5OwningUTF16Buffer> mFirstBuffer; |
michael@0 | 309 | |
michael@0 | 310 | /** |
michael@0 | 311 | * The last buffer in the pending UTF-16 buffer queue. Always points |
michael@0 | 312 | * to a sentinel object with nullptr as its parser key. |
michael@0 | 313 | */ |
michael@0 | 314 | nsHtml5OwningUTF16Buffer* mLastBuffer; // weak ref; |
michael@0 | 315 | |
michael@0 | 316 | /** |
michael@0 | 317 | * The tree operation executor |
michael@0 | 318 | */ |
michael@0 | 319 | nsRefPtr<nsHtml5TreeOpExecutor> mExecutor; |
michael@0 | 320 | |
michael@0 | 321 | /** |
michael@0 | 322 | * The HTML5 tree builder |
michael@0 | 323 | */ |
michael@0 | 324 | const nsAutoPtr<nsHtml5TreeBuilder> mTreeBuilder; |
michael@0 | 325 | |
michael@0 | 326 | /** |
michael@0 | 327 | * The HTML5 tokenizer |
michael@0 | 328 | */ |
michael@0 | 329 | const nsAutoPtr<nsHtml5Tokenizer> mTokenizer; |
michael@0 | 330 | |
michael@0 | 331 | /** |
michael@0 | 332 | * Another HTML5 tree builder for preloading document.written content. |
michael@0 | 333 | */ |
michael@0 | 334 | nsAutoPtr<nsHtml5TreeBuilder> mDocWriteSpeculativeTreeBuilder; |
michael@0 | 335 | |
michael@0 | 336 | /** |
michael@0 | 337 | * Another HTML5 tokenizer for preloading document.written content. |
michael@0 | 338 | */ |
michael@0 | 339 | nsAutoPtr<nsHtml5Tokenizer> mDocWriteSpeculativeTokenizer; |
michael@0 | 340 | |
michael@0 | 341 | /** |
michael@0 | 342 | * The stream listener holding the stream parser. |
michael@0 | 343 | */ |
michael@0 | 344 | nsRefPtr<nsHtml5StreamListener> mStreamListener; |
michael@0 | 345 | |
michael@0 | 346 | /** |
michael@0 | 347 | * |
michael@0 | 348 | */ |
michael@0 | 349 | int32_t mRootContextLineNumber; |
michael@0 | 350 | |
michael@0 | 351 | /** |
michael@0 | 352 | * Whether it's OK to transfer parsing back to the stream parser |
michael@0 | 353 | */ |
michael@0 | 354 | bool mReturnToStreamParserPermitted; |
michael@0 | 355 | |
michael@0 | 356 | /** |
michael@0 | 357 | * The scoped atom table |
michael@0 | 358 | */ |
michael@0 | 359 | nsHtml5AtomTable mAtomTable; |
michael@0 | 360 | |
michael@0 | 361 | }; |
michael@0 | 362 | #endif |