author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 03 May 2010 13:17:34 +0300 | |
changeset 19 | fcece45ef507 |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 2000 Peter Kelly (pmk@post.com) |
|
3 |
* Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. |
|
4 |
* Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
|
5 |
* Copyright (C) 2007 Samuel Weinig (sam@webkit.org) |
|
6 |
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
|
7 |
* Copyright (C) 2008 Holger Hans Peter Freyther |
|
8 |
* Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) |
|
9 |
* |
|
10 |
* This library is free software; you can redistribute it and/or |
|
11 |
* modify it under the terms of the GNU Library General Public |
|
12 |
* License as published by the Free Software Foundation; either |
|
13 |
* version 2 of the License, or (at your option) any later version. |
|
14 |
* |
|
15 |
* This library is distributed in the hope that it will be useful, |
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 |
* Library General Public License for more details. |
|
19 |
* |
|
20 |
* You should have received a copy of the GNU Library General Public License |
|
21 |
* along with this library; see the file COPYING.LIB. If not, write to |
|
22 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
23 |
* Boston, MA 02110-1301, USA. |
|
24 |
*/ |
|
25 |
||
26 |
#include "config.h" |
|
27 |
#include "XMLTokenizer.h" |
|
28 |
||
29 |
#include "CDATASection.h" |
|
30 |
#include "CString.h" |
|
31 |
#include "CachedScript.h" |
|
32 |
#include "Comment.h" |
|
33 |
#include "DocLoader.h" |
|
34 |
#include "Document.h" |
|
35 |
#include "DocumentFragment.h" |
|
36 |
#include "DocumentType.h" |
|
37 |
#include "Frame.h" |
|
38 |
#include "FrameLoader.h" |
|
39 |
#include "FrameView.h" |
|
40 |
#include "HTMLLinkElement.h" |
|
41 |
#include "HTMLNames.h" |
|
42 |
#include "HTMLStyleElement.h" |
|
43 |
#include "ImageLoader.h" |
|
44 |
#include "ProcessingInstruction.h" |
|
45 |
#include "ResourceError.h" |
|
46 |
#include "ResourceHandle.h" |
|
47 |
#include "ResourceRequest.h" |
|
48 |
#include "ResourceResponse.h" |
|
49 |
#include "ScriptController.h" |
|
50 |
#include "ScriptElement.h" |
|
51 |
#include "ScriptSourceCode.h" |
|
52 |
#include "ScriptValue.h" |
|
53 |
#include "TextResourceDecoder.h" |
|
54 |
#include <wtf/Platform.h> |
|
55 |
#include <wtf/StringExtras.h> |
|
56 |
#include <wtf/Threading.h> |
|
57 |
#include <wtf/Vector.h> |
|
58 |
||
59 |
#if ENABLE(SVG) |
|
60 |
#include "SVGNames.h" |
|
61 |
#include "SVGStyleElement.h" |
|
62 |
#endif |
|
63 |
||
64 |
using namespace std; |
|
65 |
||
66 |
namespace WebCore { |
|
67 |
||
68 |
using namespace HTMLNames; |
|
69 |
||
70 |
const int maxErrors = 25; |
|
71 |
||
72 |
#if ENABLE(WML) |
|
73 |
bool XMLTokenizer::isWMLDocument() const |
|
74 |
{ |
|
75 |
if (m_doc) |
|
76 |
return m_doc->isWMLDocument(); |
|
77 |
||
78 |
return false; |
|
79 |
} |
|
80 |
#endif |
|
81 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
void XMLTokenizer::pushCurrentNode(Node* n) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
ASSERT(n); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
ASSERT(m_currentNode); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
if (n != m_doc) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
87 |
n->ref(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
m_currentNodeStack.append(m_currentNode); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
m_currentNode = n; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
void XMLTokenizer::popCurrentNode() |
0 | 93 |
{ |
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
94 |
if (!m_currentNode) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
95 |
return; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
ASSERT(m_currentNodeStack.size()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
97 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
98 |
if (m_currentNode != m_doc) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
m_currentNode->deref(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
101 |
m_currentNode = m_currentNodeStack.last(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
m_currentNodeStack.removeLast(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
103 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
104 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
105 |
void XMLTokenizer::clearCurrentNodeStack() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
if (m_currentNode && m_currentNode != m_doc) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
m_currentNode->deref(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
109 |
m_currentNode = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
110 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
111 |
if (m_currentNodeStack.size()) { // Aborted parsing. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
112 |
for (size_t i = m_currentNodeStack.size() - 1; i != 0; --i) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
m_currentNodeStack[i]->deref(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
114 |
if (m_currentNodeStack[0] && m_currentNodeStack[0] != m_doc) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
m_currentNodeStack[0]->deref(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
m_currentNodeStack.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
} |
0 | 118 |
} |
119 |
||
120 |
void XMLTokenizer::write(const SegmentedString& s, bool /*appendData*/) |
|
121 |
{ |
|
122 |
String parseString = s.toString(); |
|
123 |
||
124 |
if (m_sawXSLTransform || !m_sawFirstElement) |
|
125 |
m_originalSourceForTransform += parseString; |
|
126 |
||
127 |
if (m_parserStopped || m_sawXSLTransform) |
|
128 |
return; |
|
129 |
||
130 |
if (m_parserPaused) { |
|
131 |
m_pendingSrc.append(s); |
|
132 |
return; |
|
133 |
} |
|
134 |
||
135 |
doWrite(s.toString()); |
|
136 |
||
137 |
// After parsing, go ahead and dispatch image beforeload/load events. |
|
138 |
ImageLoader::dispatchPendingEvents(); |
|
139 |
} |
|
140 |
||
141 |
void XMLTokenizer::handleError(ErrorType type, const char* m, int lineNumber, int columnNumber) |
|
142 |
{ |
|
143 |
if (type == fatal || (m_errorCount < maxErrors && m_lastErrorLine != lineNumber && m_lastErrorColumn != columnNumber)) { |
|
144 |
switch (type) { |
|
145 |
case warning: |
|
146 |
m_errorMessages += String::format("warning on line %d at column %d: %s", lineNumber, columnNumber, m); |
|
147 |
break; |
|
148 |
case fatal: |
|
149 |
case nonFatal: |
|
150 |
m_errorMessages += String::format("error on line %d at column %d: %s", lineNumber, columnNumber, m); |
|
151 |
} |
|
152 |
||
153 |
m_lastErrorLine = lineNumber; |
|
154 |
m_lastErrorColumn = columnNumber; |
|
155 |
++m_errorCount; |
|
156 |
} |
|
157 |
||
158 |
if (type != warning) |
|
159 |
m_sawError = true; |
|
160 |
||
161 |
if (type == fatal) |
|
162 |
stopParsing(); |
|
163 |
} |
|
164 |
||
165 |
bool XMLTokenizer::enterText() |
|
166 |
{ |
|
167 |
#if !USE(QXMLSTREAM) |
|
168 |
ASSERT(m_bufferedText.size() == 0); |
|
169 |
#endif |
|
170 |
RefPtr<Node> newNode = Text::create(m_doc, ""); |
|
171 |
if (!m_currentNode->addChild(newNode.get())) |
|
172 |
return false; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
pushCurrentNode(newNode.get()); |
0 | 174 |
return true; |
175 |
} |
|
176 |
||
177 |
#if !USE(QXMLSTREAM) |
|
178 |
static inline String toString(const xmlChar* str, unsigned len) |
|
179 |
{ |
|
180 |
return UTF8Encoding().decode(reinterpret_cast<const char*>(str), len); |
|
181 |
} |
|
182 |
#endif |
|
183 |
||
184 |
||
185 |
void XMLTokenizer::exitText() |
|
186 |
{ |
|
187 |
if (m_parserStopped) |
|
188 |
return; |
|
189 |
||
190 |
if (!m_currentNode || !m_currentNode->isTextNode()) |
|
191 |
return; |
|
192 |
||
193 |
#if !USE(QXMLSTREAM) |
|
194 |
ExceptionCode ec = 0; |
|
195 |
static_cast<Text*>(m_currentNode)->appendData(toString(m_bufferedText.data(), m_bufferedText.size()), ec); |
|
196 |
Vector<xmlChar> empty; |
|
197 |
m_bufferedText.swap(empty); |
|
198 |
#endif |
|
199 |
||
200 |
if (m_view && m_currentNode && !m_currentNode->attached()) |
|
201 |
m_currentNode->attach(); |
|
202 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
popCurrentNode(); |
0 | 204 |
} |
205 |
||
206 |
void XMLTokenizer::end() |
|
207 |
{ |
|
208 |
doEnd(); |
|
209 |
||
210 |
if (m_sawError) |
|
211 |
insertErrorMessageBlock(); |
|
212 |
else { |
|
213 |
exitText(); |
|
214 |
m_doc->updateStyleSelector(); |
|
215 |
} |
|
216 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
clearCurrentNodeStack(); |
0 | 218 |
if (!m_parsingFragment) |
219 |
m_doc->finishedParsing(); |
|
220 |
} |
|
221 |
||
222 |
void XMLTokenizer::finish() |
|
223 |
{ |
|
224 |
if (m_parserPaused) |
|
225 |
m_finishCalled = true; |
|
226 |
else |
|
227 |
end(); |
|
228 |
} |
|
229 |
||
230 |
static inline RefPtr<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages) |
|
231 |
{ |
|
232 |
RefPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), false); |
|
233 |
reportElement->setAttribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"); |
|
234 |
||
235 |
ExceptionCode ec = 0; |
|
236 |
RefPtr<Element> h3 = doc->createElement(h3Tag, false); |
|
237 |
reportElement->appendChild(h3.get(), ec); |
|
238 |
h3->appendChild(doc->createTextNode("This page contains the following errors:"), ec); |
|
239 |
||
240 |
RefPtr<Element> fixed = doc->createElement(divTag, false); |
|
241 |
reportElement->appendChild(fixed.get(), ec); |
|
242 |
fixed->setAttribute(styleAttr, "font-family:monospace;font-size:12px"); |
|
243 |
fixed->appendChild(doc->createTextNode(errorMessages), ec); |
|
244 |
||
245 |
h3 = doc->createElement(h3Tag, false); |
|
246 |
reportElement->appendChild(h3.get(), ec); |
|
247 |
h3->appendChild(doc->createTextNode("Below is a rendering of the page up to the first error."), ec); |
|
248 |
||
249 |
return reportElement; |
|
250 |
} |
|
251 |
||
252 |
void XMLTokenizer::insertErrorMessageBlock() |
|
253 |
{ |
|
254 |
#if USE(QXMLSTREAM) |
|
255 |
if (m_parsingFragment) |
|
256 |
return; |
|
257 |
#endif |
|
258 |
// One or more errors occurred during parsing of the code. Display an error block to the user above |
|
259 |
// the normal content (the DOM tree is created manually and includes line/col info regarding |
|
260 |
// where the errors are located) |
|
261 |
||
262 |
// Create elements for display |
|
263 |
ExceptionCode ec = 0; |
|
264 |
Document* doc = m_doc; |
|
265 |
Node* documentElement = doc->documentElement(); |
|
266 |
if (!documentElement) { |
|
267 |
RefPtr<Node> rootElement = doc->createElement(htmlTag, false); |
|
268 |
doc->appendChild(rootElement, ec); |
|
269 |
RefPtr<Node> body = doc->createElement(bodyTag, false); |
|
270 |
rootElement->appendChild(body, ec); |
|
271 |
documentElement = body.get(); |
|
272 |
} |
|
273 |
#if ENABLE(SVG) |
|
274 |
else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) { |
|
275 |
RefPtr<Node> rootElement = doc->createElement(htmlTag, false); |
|
276 |
RefPtr<Node> body = doc->createElement(bodyTag, false); |
|
277 |
rootElement->appendChild(body, ec); |
|
278 |
body->appendChild(documentElement, ec); |
|
279 |
doc->appendChild(rootElement.get(), ec); |
|
280 |
documentElement = body.get(); |
|
281 |
} |
|
282 |
#endif |
|
283 |
#if ENABLE(WML) |
|
284 |
else if (isWMLDocument()) { |
|
285 |
RefPtr<Node> rootElement = doc->createElement(htmlTag, false); |
|
286 |
RefPtr<Node> body = doc->createElement(bodyTag, false); |
|
287 |
rootElement->appendChild(body, ec); |
|
288 |
body->appendChild(documentElement, ec); |
|
289 |
doc->appendChild(rootElement.get(), ec); |
|
290 |
documentElement = body.get(); |
|
291 |
} |
|
292 |
#endif |
|
293 |
||
294 |
RefPtr<Element> reportElement = createXHTMLParserErrorHeader(doc, m_errorMessages); |
|
295 |
documentElement->insertBefore(reportElement, documentElement->firstChild(), ec); |
|
296 |
#if ENABLE(XSLT) |
|
297 |
if (doc->transformSourceDocument()) { |
|
298 |
RefPtr<Element> par = doc->createElement(pTag, false); |
|
299 |
reportElement->appendChild(par, ec); |
|
300 |
par->setAttribute(styleAttr, "white-space: normal"); |
|
301 |
par->appendChild(doc->createTextNode("This document was created as the result of an XSL transformation. The line and column numbers given are from the transformed result."), ec); |
|
302 |
} |
|
303 |
#endif |
|
304 |
doc->updateStyleIfNeeded(); |
|
305 |
} |
|
306 |
||
307 |
void XMLTokenizer::notifyFinished(CachedResource* unusedResource) |
|
308 |
{ |
|
309 |
ASSERT_UNUSED(unusedResource, unusedResource == m_pendingScript); |
|
310 |
ASSERT(m_pendingScript->accessCount() > 0); |
|
311 |
||
312 |
ScriptSourceCode sourceCode(m_pendingScript.get()); |
|
313 |
bool errorOccurred = m_pendingScript->errorOccurred(); |
|
314 |
||
315 |
m_pendingScript->removeClient(this); |
|
316 |
m_pendingScript = 0; |
|
317 |
||
318 |
RefPtr<Element> e = m_scriptElement; |
|
319 |
m_scriptElement = 0; |
|
320 |
||
321 |
ScriptElement* scriptElement = toScriptElement(e.get()); |
|
322 |
ASSERT(scriptElement); |
|
323 |
||
324 |
if (errorOccurred) |
|
325 |
scriptElement->dispatchErrorEvent(); |
|
326 |
else { |
|
327 |
m_view->frame()->script()->executeScript(sourceCode); |
|
328 |
scriptElement->dispatchLoadEvent(); |
|
329 |
} |
|
330 |
||
331 |
m_scriptElement = 0; |
|
332 |
||
333 |
if (!m_requestingScript) |
|
334 |
resumeParsing(); |
|
335 |
} |
|
336 |
||
337 |
bool XMLTokenizer::isWaitingForScripts() const |
|
338 |
{ |
|
339 |
return m_pendingScript; |
|
340 |
} |
|
341 |
||
342 |
void XMLTokenizer::pauseParsing() |
|
343 |
{ |
|
344 |
if (m_parsingFragment) |
|
345 |
return; |
|
346 |
||
347 |
m_parserPaused = true; |
|
348 |
} |
|
349 |
||
350 |
} |