2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef HTMLDocumentParser_h
27 #define HTMLDocumentParser_h
29 #include "CachedResourceClient.h"
30 #include "FragmentScriptingPermission.h"
31 #include "HTMLInputStream.h"
32 #include "HTMLScriptRunnerHost.h"
33 #include "HTMLSourceTracker.h"
34 #include "HTMLToken.h"
35 #include "ScriptableDocumentParser.h"
36 #include "SegmentedString.h"
38 #include "XSSFilter.h"
39 #include <wtf/OwnPtr.h>
44 class DocumentFragment;
46 class HTMLParserScheduler;
48 class HTMLScriptRunner;
49 class HTMLTreeBuilder;
50 class HTMLPreloadScanner;
51 class ScriptController;
52 class ScriptSourceCode;
54 class HTMLDocumentParser : public ScriptableDocumentParser, HTMLScriptRunnerHost, CachedResourceClient {
55 WTF_MAKE_FAST_ALLOCATED;
57 static PassRefPtr<HTMLDocumentParser> create(HTMLDocument* document, bool reportErrors)
59 return adoptRef(new HTMLDocumentParser(document, reportErrors));
61 static PassRefPtr<HTMLDocumentParser> create(DocumentFragment* fragment, Element* contextElement, FragmentScriptingPermission permission)
63 return adoptRef(new HTMLDocumentParser(fragment, contextElement, permission));
66 virtual ~HTMLDocumentParser();
68 // Exposed for HTMLParserScheduler
69 void resumeParsingAfterYield();
71 static void parseDocumentFragment(const String&, DocumentFragment*, Element* contextElement, FragmentScriptingPermission = FragmentScriptingAllowed);
73 static bool usePreHTML5ParserQuirks(Document*);
75 HTMLTokenizer* tokenizer() const { return m_tokenizer.get(); }
76 String sourceForToken(const HTMLToken&);
78 virtual TextPosition0 textPosition() const;
79 virtual int lineNumber() const;
81 virtual void suspendScheduledTasks();
82 virtual void resumeScheduledTasks();
85 virtual void insert(const SegmentedString&);
86 virtual void append(const SegmentedString&);
87 virtual void finish();
89 HTMLDocumentParser(HTMLDocument*, bool reportErrors);
90 HTMLDocumentParser(DocumentFragment*, Element* contextElement, FragmentScriptingPermission);
92 HTMLTreeBuilder* treeBuilder() const { return m_treeBuilder.get(); }
96 virtual void detach();
97 virtual bool hasInsertionPoint();
98 virtual bool finishWasCalled();
99 virtual bool processingData() const;
100 virtual void prepareToStopParsing();
101 virtual void stopParsing();
102 virtual bool isWaitingForScripts() const;
103 virtual bool isExecutingScript() const;
104 virtual void executeScriptsWaitingForStylesheets();
106 // HTMLScriptRunnerHost
107 virtual void watchForLoad(CachedResource*);
108 virtual void stopWatchingForLoad(CachedResource*);
109 virtual bool shouldLoadExternalScriptFromSrc(const AtomicString&);
110 virtual HTMLInputStream& inputStream() { return m_input; }
112 // CachedResourceClient
113 virtual void notifyFinished(CachedResource*);
115 enum SynchronousMode {
119 void pumpTokenizer(SynchronousMode);
120 void pumpTokenizerIfPossible(SynchronousMode);
122 bool runScriptsForPausedTreeBuilder();
123 void resumeParsingAfterScriptExecution();
128 void attemptToRunDeferredScriptsAndEnd();
131 bool isScheduledForResume() const;
132 bool inScriptExecution() const;
133 bool inWrite() const { return m_writeNestingLevel > 0; }
134 bool shouldDelayEnd() const { return inWrite() || isWaitingForScripts() || inScriptExecution() || isScheduledForResume(); }
136 ScriptController* script() const;
138 HTMLInputStream m_input;
140 // We hold m_token here because it might be partially complete.
143 OwnPtr<HTMLTokenizer> m_tokenizer;
144 OwnPtr<HTMLScriptRunner> m_scriptRunner;
145 OwnPtr<HTMLTreeBuilder> m_treeBuilder;
146 OwnPtr<HTMLPreloadScanner> m_preloadScanner;
147 OwnPtr<HTMLParserScheduler> m_parserScheduler;
148 HTMLSourceTracker m_sourceTracker;
149 XSSFilter m_xssFilter;
151 bool m_endWasDelayed;
152 unsigned m_writeNestingLevel;