HTMLDocumentParser.h revision ab9e7a118cf1ea2e3a93dce683b2ded3e7291ddb
1/* 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#ifndef HTMLDocumentParser_h 27#define HTMLDocumentParser_h 28 29#include "CachedResourceClient.h" 30#include "FragmentScriptingPermission.h" 31#include "HTMLInputStream.h" 32#include "HTMLScriptRunnerHost.h" 33#include "HTMLToken.h" 34#include "ScriptableDocumentParser.h" 35#include "SegmentedString.h" 36#include "Timer.h" 37#include <wtf/OwnPtr.h> 38 39namespace WebCore { 40 41class Document; 42class DocumentFragment; 43class HTMLDocument; 44class HTMLParserScheduler; 45class HTMLTokenizer; 46class HTMLScriptRunner; 47class HTMLTreeBuilder; 48class HTMLPreloadScanner; 49class ScriptController; 50class ScriptSourceCode; 51 52class HTMLDocumentParser : public ScriptableDocumentParser, HTMLScriptRunnerHost, CachedResourceClient { 53 WTF_MAKE_FAST_ALLOCATED; 54public: 55 static PassRefPtr<HTMLDocumentParser> create(HTMLDocument* document, bool reportErrors) 56 { 57 return adoptRef(new HTMLDocumentParser(document, reportErrors)); 58 } 59 static PassRefPtr<HTMLDocumentParser> create(DocumentFragment* fragment, Element* contextElement, FragmentScriptingPermission permission) 60 { 61 return adoptRef(new HTMLDocumentParser(fragment, contextElement, permission)); 62 } 63 64 virtual ~HTMLDocumentParser(); 65 66 // Exposed for HTMLParserScheduler 67 void resumeParsingAfterYield(); 68 69 static void parseDocumentFragment(const String&, DocumentFragment*, Element* contextElement, FragmentScriptingPermission = FragmentScriptingAllowed); 70 71 static bool usePreHTML5ParserQuirks(Document*); 72 73 HTMLTokenizer* tokenizer() const { return m_tokenizer.get(); } 74 75 virtual TextPosition0 textPosition() const; 76 virtual void suspendScheduledTasks(); 77 virtual void resumeScheduledTasks(); 78 79protected: 80 virtual void insert(const SegmentedString&); 81 virtual void append(const SegmentedString&); 82 virtual void finish(); 83 84 HTMLDocumentParser(HTMLDocument*, bool reportErrors); 85 HTMLDocumentParser(DocumentFragment*, Element* contextElement, FragmentScriptingPermission); 86 87 HTMLTreeBuilder* treeBuilder() const { return m_treeBuilder.get(); } 88 89private: 90 // DocumentParser 91 virtual void detach(); 92 virtual bool hasInsertionPoint(); 93 virtual bool finishWasCalled(); 94 virtual bool processingData() const; 95 virtual void prepareToStopParsing(); 96 virtual void stopParsing(); 97 virtual bool isWaitingForScripts() const; 98 virtual bool isExecutingScript() const; 99 virtual void executeScriptsWaitingForStylesheets(); 100 virtual int lineNumber() const; 101 102 // HTMLScriptRunnerHost 103 virtual void watchForLoad(CachedResource*); 104 virtual void stopWatchingForLoad(CachedResource*); 105 virtual bool shouldLoadExternalScriptFromSrc(const AtomicString&); 106 virtual HTMLInputStream& inputStream() { return m_input; } 107 108 // CachedResourceClient 109 virtual void notifyFinished(CachedResource*); 110 111 enum SynchronousMode { 112 AllowYield, 113 ForceSynchronous, 114 }; 115 void pumpTokenizer(SynchronousMode); 116 void pumpTokenizerIfPossible(SynchronousMode); 117 118 bool runScriptsForPausedTreeBuilder(); 119 void resumeParsingAfterScriptExecution(); 120 121 void begin(); 122 void attemptToEnd(); 123 void endIfDelayed(); 124 void attemptToRunDeferredScriptsAndEnd(); 125 void end(); 126 127 bool isScheduledForResume() const; 128 bool inScriptExecution() const; 129 bool inWrite() const { return m_writeNestingLevel > 0; } 130 bool shouldDelayEnd() const { return inWrite() || isWaitingForScripts() || inScriptExecution() || isScheduledForResume(); } 131 132 ScriptController* script() const; 133 134 HTMLInputStream m_input; 135 136 // We hold m_token here because it might be partially complete. 137 HTMLToken m_token; 138 139 OwnPtr<HTMLTokenizer> m_tokenizer; 140 OwnPtr<HTMLScriptRunner> m_scriptRunner; 141 OwnPtr<HTMLTreeBuilder> m_treeBuilder; 142 OwnPtr<HTMLPreloadScanner> m_preloadScanner; 143 OwnPtr<HTMLParserScheduler> m_parserScheduler; 144 145 bool m_endWasDelayed; 146 unsigned m_writeNestingLevel; 147}; 148 149} 150 151#endif 152