1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
* Copyright (C) 2010 Google, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "HTML5DocumentParser.h"
#include "Element.h"
#include "Frame.h"
#include "HTML5Lexer.h"
#include "HTML5ScriptRunner.h"
#include "HTML5TreeBuilder.h"
#include "HTMLDocument.h"
#include "Node.h"
#include "NotImplemented.h"
#include "XSSAuditor.h"
#if ENABLE(INSPECTOR)
#include "InspectorTimelineAgent.h"
#endif
namespace WebCore {
namespace {
class NestingLevelIncrementer : public Noncopyable {
public:
NestingLevelIncrementer(int& counter)
: m_counter(&counter)
{
++(*m_counter);
}
~NestingLevelIncrementer()
{
--(*m_counter);
}
private:
int* m_counter;
};
} // namespace
HTML5DocumentParser::HTML5DocumentParser(HTMLDocument* document, bool reportErrors)
: DocumentParser()
, m_document(document)
, m_lexer(new HTML5Lexer)
, m_scriptRunner(new HTML5ScriptRunner(document, this))
, m_treeConstructor(new HTML5TreeBuilder(m_lexer.get(), document, reportErrors))
, m_endWasDelayed(false)
, m_writeNestingLevel(0)
{
begin();
}
HTML5DocumentParser::~HTML5DocumentParser()
{
// FIXME: We'd like to ASSERT that normal operation of this class clears
// out any delayed actions, but we can't because we're unceremoniously
// deleted. If there were a required call to some sort of cancel function,
// then we could ASSERT some invariants here.
}
void HTML5DocumentParser::begin()
{
// FIXME: Should we reset the lexer?
}
void HTML5DocumentParser::pumpLexerIfPossible()
{
if (m_parserStopped || m_treeConstructor->isPaused())
return;
pumpLexer();
}
void HTML5DocumentParser::pumpLexer()
{
// We tell the InspectorTimelineAgent about every pump, even if we
// end up pumping nothing. It can filter out empty pumps itself.
willPumpLexer();
ASSERT(!m_parserStopped);
ASSERT(!m_treeConstructor->isPaused());
while (!m_parserStopped && m_lexer->nextToken(m_input.current(), m_token)) {
if (ScriptController* scriptController = script())
scriptController->setEventHandlerLineNumber(lineNumber() + 1);
m_treeConstructor->constructTreeFromToken(m_token);
m_token.clear();
if (ScriptController* scriptController = script())
scriptController->setEventHandlerLineNumber(0);
if (!m_treeConstructor->isPaused())
continue;
// The parser will pause itself when waiting on a script to load or run.
// ScriptRunner executes scripts at the right times and handles reentrancy.
int scriptStartLine = 0;
RefPtr<Element> scriptElement = m_treeConstructor->takeScriptToProcess(scriptStartLine);
bool shouldContinueParsing = m_scriptRunner->execute(scriptElement.release(), scriptStartLine);
m_treeConstructor->setPaused(!shouldContinueParsing);
if (!shouldContinueParsing)
return;
}
didPumpLexer();
}
void HTML5DocumentParser::willPumpLexer()
{
#if ENABLE(INSPECTOR)
// FIXME: m_input.current().length() is only accurate if we
// end up parsing the whole buffer in this pump. We should pass how
// much we parsed as part of didWriteHTML instead of willWriteHTML.
if (InspectorTimelineAgent* timelineAgent = m_document->inspectorTimelineAgent())
timelineAgent->willWriteHTML(m_input.current().length(), m_lexer->lineNumber());
#endif
}
void HTML5DocumentParser::didPumpLexer()
{
#if ENABLE(INSPECTOR)
if (InspectorTimelineAgent* timelineAgent = m_document->inspectorTimelineAgent())
timelineAgent->didWriteHTML(m_lexer->lineNumber());
#endif
}
void HTML5DocumentParser::write(const SegmentedString& source, bool appendData)
{
if (m_parserStopped)
return;
NestingLevelIncrementer nestingLevelIncrementer(m_writeNestingLevel);
if (appendData) {
m_input.appendToEnd(source);
if (m_writeNestingLevel > 1) {
// We've gotten data off the network in a nested call to write().
// We don't want to consume any more of the input stream now. Do
// not worry. We'll consume this data in a less-nested write().
return;
}
} else
m_input.insertAtCurrentInsertionPoint(source);
pumpLexerIfPossible();
endIfDelayed();
}
void HTML5DocumentParser::end()
{
pumpLexerIfPossible();
// Informs the the rest of WebCore that parsing is really finished.
m_treeConstructor->finished();
}
void HTML5DocumentParser::attemptToEnd()
{
// finish() indicates we will not receive any more data. If we are waiting on
// an external script to load, we can't finish parsing quite yet.
if (inWrite() || isWaitingForScripts() || executingScript()) {
m_endWasDelayed = true;
return;
}
end();
}
void HTML5DocumentParser::endIfDelayed()
{
if (!m_endWasDelayed || isWaitingForScripts() || executingScript())
return;
m_endWasDelayed = false;
end();
}
void HTML5DocumentParser::finish()
{
// We're not going to get any more data off the network, so we close the
// input stream to indicate EOF.
m_input.close();
attemptToEnd();
}
int HTML5DocumentParser::executingScript() const
{
return m_scriptRunner->inScriptExecution();
}
int HTML5DocumentParser::lineNumber() const
{
return m_lexer->lineNumber();
}
int HTML5DocumentParser::columnNumber() const
{
return m_lexer->columnNumber();
}
LegacyHTMLTreeConstructor* HTML5DocumentParser::htmlTreeConstructor() const
{
return m_treeConstructor->legacyTreeConstructor();
}
bool HTML5DocumentParser::isWaitingForScripts() const
{
return m_treeConstructor->isPaused();
}
void HTML5DocumentParser::resumeParsingAfterScriptExecution()
{
ASSERT(!m_scriptRunner->inScriptExecution());
ASSERT(!m_treeConstructor->isPaused());
pumpLexerIfPossible();
// The document already finished parsing we were just waiting on scripts when finished() was called.
endIfDelayed();
}
void HTML5DocumentParser::watchForLoad(CachedResource* cachedScript)
{
ASSERT(!cachedScript->isLoaded());
// addClient would call notifyFinished if the load were complete.
// Callers do not expect to be re-entered from this call, so they should
// not an already-loaded CachedResource.
cachedScript->addClient(this);
}
void HTML5DocumentParser::stopWatchingForLoad(CachedResource* cachedScript)
{
cachedScript->removeClient(this);
}
bool HTML5DocumentParser::shouldLoadExternalScriptFromSrc(const AtomicString& srcValue)
{
if (!m_XSSAuditor)
return true;
return m_XSSAuditor->canLoadExternalScriptFromSrc(srcValue);
}
void HTML5DocumentParser::executeScript(const ScriptSourceCode& sourceCode)
{
ASSERT(m_scriptRunner->inScriptExecution());
if (!m_document->frame())
return;
InsertionPointRecord savedInsertionPoint(m_input);
m_document->frame()->script()->executeScript(sourceCode);
}
void HTML5DocumentParser::notifyFinished(CachedResource* cachedResource)
{
ASSERT(!m_scriptRunner->inScriptExecution());
ASSERT(m_treeConstructor->isPaused());
// Note: We only ever wait on one script at a time, so we always know this
// is the one we were waiting on and can un-pause the tree builder.
m_treeConstructor->setPaused(false);
bool shouldContinueParsing = m_scriptRunner->executeScriptsWaitingForLoad(cachedResource);
m_treeConstructor->setPaused(!shouldContinueParsing);
if (shouldContinueParsing)
resumeParsingAfterScriptExecution();
}
void HTML5DocumentParser::executeScriptsWaitingForStylesheets()
{
// Ignore calls unless we have a script blocking the parser waiting on a
// stylesheet load. Otherwise we are currently parsing and this
// is a re-entrant call from encountering a </ style> tag.
if (!m_scriptRunner->hasScriptsWaitingForStylesheets())
return;
ASSERT(!m_scriptRunner->inScriptExecution());
ASSERT(m_treeConstructor->isPaused());
// Note: We only ever wait on one script at a time, so we always know this
// is the one we were waiting on and can un-pause the tree builder.
m_treeConstructor->setPaused(false);
bool shouldContinueParsing = m_scriptRunner->executeScriptsWaitingForStylesheets();
m_treeConstructor->setPaused(!shouldContinueParsing);
if (shouldContinueParsing)
resumeParsingAfterScriptExecution();
}
ScriptController* HTML5DocumentParser::script() const
{
return m_document->frame() ? m_document->frame()->script() : 0;
}
}
|