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, 2009 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 "HTMLStyleElement.h" |
|
42 |
#include "HTMLTokenizer.h" // for decodeNamedEntity |
|
43 |
#include "ProcessingInstruction.h" |
|
44 |
#include "ResourceError.h" |
|
45 |
#include "ResourceHandle.h" |
|
46 |
#include "ResourceRequest.h" |
|
47 |
#include "ResourceResponse.h" |
|
48 |
#include "ScriptController.h" |
|
49 |
#include "ScriptElement.h" |
|
50 |
#include "ScriptSourceCode.h" |
|
51 |
#include "ScriptValue.h" |
|
52 |
#include "TextResourceDecoder.h" |
|
53 |
#include "TransformSource.h" |
|
54 |
#include "XMLTokenizerScope.h" |
|
55 |
#include <libxml/parser.h> |
|
56 |
#include <libxml/parserInternals.h> |
|
57 |
#include <wtf/Platform.h> |
|
58 |
#include <wtf/StringExtras.h> |
|
59 |
#include <wtf/Threading.h> |
|
60 |
#include <wtf/UnusedParam.h> |
|
61 |
#include <wtf/Vector.h> |
|
62 |
||
63 |
#if ENABLE(XSLT) |
|
64 |
#include <libxslt/xslt.h> |
|
65 |
#endif |
|
66 |
||
67 |
#if ENABLE(XHTMLMP) |
|
68 |
#include "HTMLNames.h" |
|
69 |
#include "HTMLScriptElement.h" |
|
70 |
#endif |
|
71 |
||
72 |
using namespace std; |
|
73 |
||
74 |
namespace WebCore { |
|
75 |
||
76 |
class PendingCallbacks : public Noncopyable { |
|
77 |
public: |
|
78 |
~PendingCallbacks() |
|
79 |
{ |
|
80 |
deleteAllValues(m_callbacks); |
|
81 |
} |
|
82 |
||
83 |
void appendStartElementNSCallback(const xmlChar* xmlLocalName, const xmlChar* xmlPrefix, const xmlChar* xmlURI, int nb_namespaces, |
|
84 |
const xmlChar** namespaces, int nb_attributes, int nb_defaulted, const xmlChar** attributes) |
|
85 |
{ |
|
86 |
PendingStartElementNSCallback* callback = new PendingStartElementNSCallback; |
|
87 |
||
88 |
callback->xmlLocalName = xmlStrdup(xmlLocalName); |
|
89 |
callback->xmlPrefix = xmlStrdup(xmlPrefix); |
|
90 |
callback->xmlURI = xmlStrdup(xmlURI); |
|
91 |
callback->nb_namespaces = nb_namespaces; |
|
92 |
callback->namespaces = static_cast<xmlChar**>(xmlMalloc(sizeof(xmlChar*) * nb_namespaces * 2)); |
|
93 |
for (int i = 0; i < nb_namespaces * 2 ; i++) |
|
94 |
callback->namespaces[i] = xmlStrdup(namespaces[i]); |
|
95 |
callback->nb_attributes = nb_attributes; |
|
96 |
callback->nb_defaulted = nb_defaulted; |
|
97 |
callback->attributes = static_cast<xmlChar**>(xmlMalloc(sizeof(xmlChar*) * nb_attributes * 5)); |
|
98 |
for (int i = 0; i < nb_attributes; i++) { |
|
99 |
// Each attribute has 5 elements in the array: |
|
100 |
// name, prefix, uri, value and an end pointer. |
|
101 |
||
102 |
for (int j = 0; j < 3; j++) |
|
103 |
callback->attributes[i * 5 + j] = xmlStrdup(attributes[i * 5 + j]); |
|
104 |
||
105 |
int len = attributes[i * 5 + 4] - attributes[i * 5 + 3]; |
|
106 |
||
107 |
callback->attributes[i * 5 + 3] = xmlStrndup(attributes[i * 5 + 3], len); |
|
108 |
callback->attributes[i * 5 + 4] = callback->attributes[i * 5 + 3] + len; |
|
109 |
} |
|
110 |
||
111 |
m_callbacks.append(callback); |
|
112 |
} |
|
113 |
||
114 |
void appendEndElementNSCallback() |
|
115 |
{ |
|
116 |
PendingEndElementNSCallback* callback = new PendingEndElementNSCallback; |
|
117 |
||
118 |
m_callbacks.append(callback); |
|
119 |
} |
|
120 |
||
121 |
void appendCharactersCallback(const xmlChar* s, int len) |
|
122 |
{ |
|
123 |
PendingCharactersCallback* callback = new PendingCharactersCallback; |
|
124 |
||
125 |
callback->s = xmlStrndup(s, len); |
|
126 |
callback->len = len; |
|
127 |
||
128 |
m_callbacks.append(callback); |
|
129 |
} |
|
130 |
||
131 |
void appendProcessingInstructionCallback(const xmlChar* target, const xmlChar* data) |
|
132 |
{ |
|
133 |
PendingProcessingInstructionCallback* callback = new PendingProcessingInstructionCallback; |
|
134 |
||
135 |
callback->target = xmlStrdup(target); |
|
136 |
callback->data = xmlStrdup(data); |
|
137 |
||
138 |
m_callbacks.append(callback); |
|
139 |
} |
|
140 |
||
141 |
void appendCDATABlockCallback(const xmlChar* s, int len) |
|
142 |
{ |
|
143 |
PendingCDATABlockCallback* callback = new PendingCDATABlockCallback; |
|
144 |
||
145 |
callback->s = xmlStrndup(s, len); |
|
146 |
callback->len = len; |
|
147 |
||
148 |
m_callbacks.append(callback); |
|
149 |
} |
|
150 |
||
151 |
void appendCommentCallback(const xmlChar* s) |
|
152 |
{ |
|
153 |
PendingCommentCallback* callback = new PendingCommentCallback; |
|
154 |
||
155 |
callback->s = xmlStrdup(s); |
|
156 |
||
157 |
m_callbacks.append(callback); |
|
158 |
} |
|
159 |
||
160 |
void appendInternalSubsetCallback(const xmlChar* name, const xmlChar* externalID, const xmlChar* systemID) |
|
161 |
{ |
|
162 |
PendingInternalSubsetCallback* callback = new PendingInternalSubsetCallback; |
|
163 |
||
164 |
callback->name = xmlStrdup(name); |
|
165 |
callback->externalID = xmlStrdup(externalID); |
|
166 |
callback->systemID = xmlStrdup(systemID); |
|
167 |
||
168 |
m_callbacks.append(callback); |
|
169 |
} |
|
170 |
||
171 |
void appendErrorCallback(XMLTokenizer::ErrorType type, const char* message, int lineNumber, int columnNumber) |
|
172 |
{ |
|
173 |
PendingErrorCallback* callback = new PendingErrorCallback; |
|
174 |
||
175 |
callback->message = strdup(message); |
|
176 |
callback->type = type; |
|
177 |
callback->lineNumber = lineNumber; |
|
178 |
callback->columnNumber = columnNumber; |
|
179 |
||
180 |
m_callbacks.append(callback); |
|
181 |
} |
|
182 |
||
183 |
void callAndRemoveFirstCallback(XMLTokenizer* tokenizer) |
|
184 |
{ |
|
185 |
OwnPtr<PendingCallback> callback(m_callbacks.first()); |
|
186 |
m_callbacks.removeFirst(); |
|
187 |
callback->call(tokenizer); |
|
188 |
} |
|
189 |
||
190 |
bool isEmpty() const { return m_callbacks.isEmpty(); } |
|
191 |
||
192 |
private: |
|
193 |
struct PendingCallback { |
|
194 |
virtual ~PendingCallback() { } |
|
195 |
virtual void call(XMLTokenizer* tokenizer) = 0; |
|
196 |
}; |
|
197 |
||
198 |
struct PendingStartElementNSCallback : public PendingCallback { |
|
199 |
virtual ~PendingStartElementNSCallback() |
|
200 |
{ |
|
201 |
xmlFree(xmlLocalName); |
|
202 |
xmlFree(xmlPrefix); |
|
203 |
xmlFree(xmlURI); |
|
204 |
for (int i = 0; i < nb_namespaces * 2; i++) |
|
205 |
xmlFree(namespaces[i]); |
|
206 |
xmlFree(namespaces); |
|
207 |
for (int i = 0; i < nb_attributes; i++) |
|
208 |
for (int j = 0; j < 4; j++) |
|
209 |
xmlFree(attributes[i * 5 + j]); |
|
210 |
xmlFree(attributes); |
|
211 |
} |
|
212 |
||
213 |
virtual void call(XMLTokenizer* tokenizer) |
|
214 |
{ |
|
215 |
tokenizer->startElementNs(xmlLocalName, xmlPrefix, xmlURI, |
|
216 |
nb_namespaces, const_cast<const xmlChar**>(namespaces), |
|
217 |
nb_attributes, nb_defaulted, const_cast<const xmlChar**>(attributes)); |
|
218 |
} |
|
219 |
||
220 |
xmlChar* xmlLocalName; |
|
221 |
xmlChar* xmlPrefix; |
|
222 |
xmlChar* xmlURI; |
|
223 |
int nb_namespaces; |
|
224 |
xmlChar** namespaces; |
|
225 |
int nb_attributes; |
|
226 |
int nb_defaulted; |
|
227 |
xmlChar** attributes; |
|
228 |
}; |
|
229 |
||
230 |
struct PendingEndElementNSCallback : public PendingCallback { |
|
231 |
virtual void call(XMLTokenizer* tokenizer) |
|
232 |
{ |
|
233 |
tokenizer->endElementNs(); |
|
234 |
} |
|
235 |
}; |
|
236 |
||
237 |
struct PendingCharactersCallback : public PendingCallback { |
|
238 |
virtual ~PendingCharactersCallback() |
|
239 |
{ |
|
240 |
xmlFree(s); |
|
241 |
} |
|
242 |
||
243 |
virtual void call(XMLTokenizer* tokenizer) |
|
244 |
{ |
|
245 |
tokenizer->characters(s, len); |
|
246 |
} |
|
247 |
||
248 |
xmlChar* s; |
|
249 |
int len; |
|
250 |
}; |
|
251 |
||
252 |
struct PendingProcessingInstructionCallback : public PendingCallback { |
|
253 |
virtual ~PendingProcessingInstructionCallback() |
|
254 |
{ |
|
255 |
xmlFree(target); |
|
256 |
xmlFree(data); |
|
257 |
} |
|
258 |
||
259 |
virtual void call(XMLTokenizer* tokenizer) |
|
260 |
{ |
|
261 |
tokenizer->processingInstruction(target, data); |
|
262 |
} |
|
263 |
||
264 |
xmlChar* target; |
|
265 |
xmlChar* data; |
|
266 |
}; |
|
267 |
||
268 |
struct PendingCDATABlockCallback : public PendingCallback { |
|
269 |
virtual ~PendingCDATABlockCallback() |
|
270 |
{ |
|
271 |
xmlFree(s); |
|
272 |
} |
|
273 |
||
274 |
virtual void call(XMLTokenizer* tokenizer) |
|
275 |
{ |
|
276 |
tokenizer->cdataBlock(s, len); |
|
277 |
} |
|
278 |
||
279 |
xmlChar* s; |
|
280 |
int len; |
|
281 |
}; |
|
282 |
||
283 |
struct PendingCommentCallback : public PendingCallback { |
|
284 |
virtual ~PendingCommentCallback() |
|
285 |
{ |
|
286 |
xmlFree(s); |
|
287 |
} |
|
288 |
||
289 |
virtual void call(XMLTokenizer* tokenizer) |
|
290 |
{ |
|
291 |
tokenizer->comment(s); |
|
292 |
} |
|
293 |
||
294 |
xmlChar* s; |
|
295 |
}; |
|
296 |
||
297 |
struct PendingInternalSubsetCallback : public PendingCallback { |
|
298 |
virtual ~PendingInternalSubsetCallback() |
|
299 |
{ |
|
300 |
xmlFree(name); |
|
301 |
xmlFree(externalID); |
|
302 |
xmlFree(systemID); |
|
303 |
} |
|
304 |
||
305 |
virtual void call(XMLTokenizer* tokenizer) |
|
306 |
{ |
|
307 |
tokenizer->internalSubset(name, externalID, systemID); |
|
308 |
} |
|
309 |
||
310 |
xmlChar* name; |
|
311 |
xmlChar* externalID; |
|
312 |
xmlChar* systemID; |
|
313 |
}; |
|
314 |
||
315 |
struct PendingErrorCallback: public PendingCallback { |
|
316 |
virtual ~PendingErrorCallback() |
|
317 |
{ |
|
318 |
free(message); |
|
319 |
} |
|
320 |
||
321 |
virtual void call(XMLTokenizer* tokenizer) |
|
322 |
{ |
|
323 |
tokenizer->handleError(type, message, lineNumber, columnNumber); |
|
324 |
} |
|
325 |
||
326 |
XMLTokenizer::ErrorType type; |
|
327 |
char* message; |
|
328 |
int lineNumber; |
|
329 |
int columnNumber; |
|
330 |
}; |
|
331 |
||
332 |
Deque<PendingCallback*> m_callbacks; |
|
333 |
}; |
|
334 |
// -------------------------------- |
|
335 |
||
336 |
static int globalDescriptor = 0; |
|
337 |
static ThreadIdentifier libxmlLoaderThread = 0; |
|
338 |
||
339 |
static int matchFunc(const char*) |
|
340 |
{ |
|
341 |
// Only match loads initiated due to uses of libxml2 from within XMLTokenizer to avoid |
|
342 |
// interfering with client applications that also use libxml2. http://bugs.webkit.org/show_bug.cgi?id=17353 |
|
343 |
return XMLTokenizerScope::currentDocLoader && currentThread() == libxmlLoaderThread; |
|
344 |
} |
|
345 |
||
346 |
class OffsetBuffer { |
|
347 |
public: |
|
348 |
OffsetBuffer(const Vector<char>& b) : m_buffer(b), m_currentOffset(0) { } |
|
349 |
||
350 |
int readOutBytes(char* outputBuffer, unsigned askedToRead) |
|
351 |
{ |
|
352 |
unsigned bytesLeft = m_buffer.size() - m_currentOffset; |
|
353 |
unsigned lenToCopy = min(askedToRead, bytesLeft); |
|
354 |
if (lenToCopy) { |
|
355 |
memcpy(outputBuffer, m_buffer.data() + m_currentOffset, lenToCopy); |
|
356 |
m_currentOffset += lenToCopy; |
|
357 |
} |
|
358 |
return lenToCopy; |
|
359 |
} |
|
360 |
||
361 |
private: |
|
362 |
Vector<char> m_buffer; |
|
363 |
unsigned m_currentOffset; |
|
364 |
}; |
|
365 |
||
366 |
static bool shouldAllowExternalLoad(const KURL& url) |
|
367 |
{ |
|
368 |
String urlString = url.string(); |
|
369 |
||
370 |
// On non-Windows platforms libxml asks for this URL, the |
|
371 |
// "XML_XML_DEFAULT_CATALOG", on initialization. |
|
372 |
if (urlString == "file:///etc/xml/catalog") |
|
373 |
return false; |
|
374 |
||
375 |
// On Windows, libxml computes a URL relative to where its DLL resides. |
|
376 |
if (urlString.startsWith("file:///", false) && urlString.endsWith("/etc/catalog", false)) |
|
377 |
return false; |
|
378 |
||
379 |
// The most common DTD. There isn't much point in hammering www.w3c.org |
|
380 |
// by requesting this URL for every XHTML document. |
|
381 |
if (urlString.startsWith("http://www.w3.org/TR/xhtml", false)) |
|
382 |
return false; |
|
383 |
||
384 |
// Similarly, there isn't much point in requesting the SVG DTD. |
|
385 |
if (urlString.startsWith("http://www.w3.org/Graphics/SVG", false)) |
|
386 |
return false; |
|
387 |
||
388 |
// The libxml doesn't give us a lot of context for deciding whether to |
|
389 |
// allow this request. In the worst case, this load could be for an |
|
390 |
// external entity and the resulting document could simply read the |
|
391 |
// retrieved content. If we had more context, we could potentially allow |
|
392 |
// the parser to load a DTD. As things stand, we take the conservative |
|
393 |
// route and allow same-origin requests only. |
|
394 |
if (!XMLTokenizerScope::currentDocLoader->doc()->securityOrigin()->canRequest(url)) { |
|
395 |
XMLTokenizerScope::currentDocLoader->printAccessDeniedMessage(url); |
|
396 |
return false; |
|
397 |
} |
|
398 |
||
399 |
return true; |
|
400 |
} |
|
401 |
||
402 |
static void* openFunc(const char* uri) |
|
403 |
{ |
|
404 |
ASSERT(XMLTokenizerScope::currentDocLoader); |
|
405 |
ASSERT(currentThread() == libxmlLoaderThread); |
|
406 |
||
407 |
KURL url(KURL(), uri); |
|
408 |
||
409 |
if (!shouldAllowExternalLoad(url)) |
|
410 |
return &globalDescriptor; |
|
411 |
||
412 |
ResourceError error; |
|
413 |
ResourceResponse response; |
|
414 |
Vector<char> data; |
|
415 |
||
416 |
||
417 |
{ |
|
418 |
DocLoader* docLoader = XMLTokenizerScope::currentDocLoader; |
|
419 |
XMLTokenizerScope scope(0); |
|
420 |
// FIXME: We should restore the original global error handler as well. |
|
421 |
||
422 |
if (docLoader->frame()) |
|
423 |
docLoader->frame()->loader()->loadResourceSynchronously(url, AllowStoredCredentials, error, response, data); |
|
424 |
} |
|
425 |
||
426 |
// We have to check the URL again after the load to catch redirects. |
|
427 |
// See <https://bugs.webkit.org/show_bug.cgi?id=21963>. |
|
428 |
if (!shouldAllowExternalLoad(response.url())) |
|
429 |
return &globalDescriptor; |
|
430 |
||
431 |
return new OffsetBuffer(data); |
|
432 |
} |
|
433 |
||
434 |
static int readFunc(void* context, char* buffer, int len) |
|
435 |
{ |
|
436 |
// Do 0-byte reads in case of a null descriptor |
|
437 |
if (context == &globalDescriptor) |
|
438 |
return 0; |
|
439 |
||
440 |
OffsetBuffer* data = static_cast<OffsetBuffer*>(context); |
|
441 |
return data->readOutBytes(buffer, len); |
|
442 |
} |
|
443 |
||
444 |
static int writeFunc(void*, const char*, int) |
|
445 |
{ |
|
446 |
// Always just do 0-byte writes |
|
447 |
return 0; |
|
448 |
} |
|
449 |
||
450 |
static int closeFunc(void* context) |
|
451 |
{ |
|
452 |
if (context != &globalDescriptor) { |
|
453 |
OffsetBuffer* data = static_cast<OffsetBuffer*>(context); |
|
454 |
delete data; |
|
455 |
} |
|
456 |
return 0; |
|
457 |
} |
|
458 |
||
459 |
#if ENABLE(XSLT) |
|
460 |
static void errorFunc(void*, const char*, ...) |
|
461 |
{ |
|
462 |
// FIXME: It would be nice to display error messages somewhere. |
|
463 |
} |
|
464 |
#endif |
|
465 |
||
466 |
static bool didInit = false; |
|
467 |
||
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
468 |
PassRefPtr<XMLParserContext> XMLParserContext::createStringParser(xmlSAXHandlerPtr handlers, void* userData) |
0 | 469 |
{ |
470 |
if (!didInit) { |
|
471 |
xmlInitParser(); |
|
472 |
xmlRegisterInputCallbacks(matchFunc, openFunc, readFunc, closeFunc); |
|
473 |
xmlRegisterOutputCallbacks(matchFunc, openFunc, writeFunc, closeFunc); |
|
474 |
libxmlLoaderThread = currentThread(); |
|
475 |
didInit = true; |
|
476 |
} |
|
477 |
||
478 |
xmlParserCtxtPtr parser = xmlCreatePushParserCtxt(handlers, 0, 0, 0, 0); |
|
479 |
parser->_private = userData; |
|
480 |
parser->replaceEntities = true; |
|
481 |
const UChar BOM = 0xFEFF; |
|
482 |
const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM); |
|
483 |
xmlSwitchEncoding(parser, BOMHighByte == 0xFF ? XML_CHAR_ENCODING_UTF16LE : XML_CHAR_ENCODING_UTF16BE); |
|
484 |
||
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
485 |
return adoptRef(new XMLParserContext(parser)); |
0 | 486 |
} |
487 |
||
488 |
||
489 |
// Chunk should be encoded in UTF-8 |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
490 |
PassRefPtr<XMLParserContext> XMLParserContext::createMemoryParser(xmlSAXHandlerPtr handlers, void* userData, const char* chunk) |
0 | 491 |
{ |
492 |
if (!didInit) { |
|
493 |
xmlInitParser(); |
|
494 |
xmlRegisterInputCallbacks(matchFunc, openFunc, readFunc, closeFunc); |
|
495 |
xmlRegisterOutputCallbacks(matchFunc, openFunc, writeFunc, closeFunc); |
|
496 |
libxmlLoaderThread = currentThread(); |
|
497 |
didInit = true; |
|
498 |
} |
|
499 |
||
500 |
xmlParserCtxtPtr parser = xmlCreateMemoryParserCtxt(chunk, xmlStrlen((const xmlChar*)chunk)); |
|
501 |
||
502 |
if (!parser) |
|
503 |
return 0; |
|
504 |
||
505 |
// Copy the sax handler |
|
506 |
memcpy(parser->sax, handlers, sizeof(xmlSAXHandler)); |
|
507 |
||
508 |
// Set parser options. |
|
509 |
// XML_PARSE_NODICT: default dictionary option. |
|
510 |
// XML_PARSE_NOENT: force entities substitutions. |
|
511 |
xmlCtxtUseOptions(parser, XML_PARSE_NODICT | XML_PARSE_NOENT); |
|
512 |
||
513 |
// Internal initialization |
|
514 |
parser->sax2 = 1; |
|
515 |
parser->instate = XML_PARSER_CONTENT; // We are parsing a CONTENT |
|
516 |
parser->depth = 0; |
|
517 |
parser->str_xml = xmlDictLookup(parser->dict, BAD_CAST "xml", 3); |
|
518 |
parser->str_xmlns = xmlDictLookup(parser->dict, BAD_CAST "xmlns", 5); |
|
519 |
parser->str_xml_ns = xmlDictLookup(parser->dict, XML_XML_NAMESPACE, 36); |
|
520 |
parser->_private = userData; |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
521 |
|
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
522 |
return adoptRef(new XMLParserContext(parser)); |
0 | 523 |
} |
524 |
||
525 |
// -------------------------------- |
|
526 |
||
527 |
XMLTokenizer::XMLTokenizer(Document* _doc, FrameView* _view) |
|
528 |
: m_doc(_doc) |
|
529 |
, m_view(_view) |
|
530 |
, m_context(0) |
|
531 |
, m_pendingCallbacks(new PendingCallbacks) |
|
532 |
, m_currentNode(_doc) |
|
533 |
, m_sawError(false) |
|
534 |
, m_sawXSLTransform(false) |
|
535 |
, m_sawFirstElement(false) |
|
536 |
, m_isXHTMLDocument(false) |
|
537 |
#if ENABLE(XHTMLMP) |
|
538 |
, m_isXHTMLMPDocument(false) |
|
539 |
, m_hasDocTypeDeclaration(false) |
|
540 |
#endif |
|
541 |
, m_parserPaused(false) |
|
542 |
, m_requestingScript(false) |
|
543 |
, m_finishCalled(false) |
|
544 |
, m_errorCount(0) |
|
545 |
, m_lastErrorLine(0) |
|
546 |
, m_lastErrorColumn(0) |
|
547 |
, m_pendingScript(0) |
|
548 |
, m_scriptStartLine(0) |
|
549 |
, m_parsingFragment(false) |
|
550 |
{ |
|
551 |
} |
|
552 |
||
553 |
XMLTokenizer::XMLTokenizer(DocumentFragment* fragment, Element* parentElement) |
|
554 |
: m_doc(fragment->document()) |
|
555 |
, m_view(0) |
|
556 |
, m_context(0) |
|
557 |
, m_pendingCallbacks(new PendingCallbacks) |
|
558 |
, m_currentNode(fragment) |
|
559 |
, m_sawError(false) |
|
560 |
, m_sawXSLTransform(false) |
|
561 |
, m_sawFirstElement(false) |
|
562 |
, m_isXHTMLDocument(false) |
|
563 |
#if ENABLE(XHTMLMP) |
|
564 |
, m_isXHTMLMPDocument(false) |
|
565 |
, m_hasDocTypeDeclaration(false) |
|
566 |
#endif |
|
567 |
, m_parserPaused(false) |
|
568 |
, m_requestingScript(false) |
|
569 |
, m_finishCalled(false) |
|
570 |
, m_errorCount(0) |
|
571 |
, m_lastErrorLine(0) |
|
572 |
, m_lastErrorColumn(0) |
|
573 |
, m_pendingScript(0) |
|
574 |
, m_scriptStartLine(0) |
|
575 |
, m_parsingFragment(true) |
|
576 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
577 |
fragment->ref(); |
0 | 578 |
if (m_doc) |
579 |
m_doc->ref(); |
|
580 |
||
581 |
// Add namespaces based on the parent node |
|
582 |
Vector<Element*> elemStack; |
|
583 |
while (parentElement) { |
|
584 |
elemStack.append(parentElement); |
|
585 |
||
586 |
Node* n = parentElement->parentNode(); |
|
587 |
if (!n || !n->isElementNode()) |
|
588 |
break; |
|
589 |
parentElement = static_cast<Element*>(n); |
|
590 |
} |
|
591 |
||
592 |
if (elemStack.isEmpty()) |
|
593 |
return; |
|
594 |
||
595 |
for (Element* element = elemStack.last(); !elemStack.isEmpty(); elemStack.removeLast()) { |
|
596 |
if (NamedNodeMap* attrs = element->attributes()) { |
|
597 |
for (unsigned i = 0; i < attrs->length(); i++) { |
|
598 |
Attribute* attr = attrs->attributeItem(i); |
|
599 |
if (attr->localName() == "xmlns") |
|
600 |
m_defaultNamespaceURI = attr->value(); |
|
601 |
else if (attr->prefix() == "xmlns") |
|
602 |
m_prefixToNamespaceMap.set(attr->localName(), attr->value()); |
|
603 |
} |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
// If the parent element is not in document tree, there may be no xmlns attribute; just default to the parent's namespace. |
|
608 |
if (m_defaultNamespaceURI.isNull() && !parentElement->inDocument()) |
|
609 |
m_defaultNamespaceURI = parentElement->namespaceURI(); |
|
610 |
} |
|
611 |
||
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
612 |
XMLParserContext::~XMLParserContext() |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
613 |
{ |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
614 |
if (m_context->myDoc) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
615 |
xmlFreeDoc(m_context->myDoc); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
616 |
xmlFreeParserCtxt(m_context); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
617 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
618 |
|
0 | 619 |
XMLTokenizer::~XMLTokenizer() |
620 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
621 |
clearCurrentNodeStack(); |
0 | 622 |
if (m_parsingFragment && m_doc) |
623 |
m_doc->deref(); |
|
624 |
if (m_pendingScript) |
|
625 |
m_pendingScript->removeClient(this); |
|
626 |
} |
|
627 |
||
628 |
void XMLTokenizer::doWrite(const String& parseString) |
|
629 |
{ |
|
630 |
if (!m_context) |
|
631 |
initializeParserContext(); |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
632 |
|
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
633 |
// Protect the libxml context from deletion during a callback |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
634 |
RefPtr<XMLParserContext> context = m_context; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
635 |
|
0 | 636 |
// libXML throws an error if you try to switch the encoding for an empty string. |
637 |
if (parseString.length()) { |
|
638 |
// Hack around libxml2's lack of encoding overide support by manually |
|
639 |
// resetting the encoding to UTF-16 before every chunk. Otherwise libxml |
|
640 |
// will detect <?xml version="1.0" encoding="<encoding name>"?> blocks |
|
641 |
// and switch encodings, causing the parse to fail. |
|
642 |
const UChar BOM = 0xFEFF; |
|
643 |
const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM); |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
644 |
xmlSwitchEncoding(context->context(), BOMHighByte == 0xFF ? XML_CHAR_ENCODING_UTF16LE : XML_CHAR_ENCODING_UTF16BE); |
0 | 645 |
|
646 |
XMLTokenizerScope scope(m_doc->docLoader()); |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
647 |
xmlParseChunk(context->context(), reinterpret_cast<const char*>(parseString.characters()), sizeof(UChar) * parseString.length(), 0); |
0 | 648 |
} |
649 |
||
650 |
if (m_doc->decoder() && m_doc->decoder()->sawError()) { |
|
651 |
// If the decoder saw an error, report it as fatal (stops parsing) |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
652 |
handleError(fatal, "Encoding error", context->context()->input->line, context->context()->input->col); |
0 | 653 |
} |
654 |
||
655 |
return; |
|
656 |
} |
|
657 |
||
658 |
static inline String toString(const xmlChar* str, unsigned len) |
|
659 |
{ |
|
660 |
return UTF8Encoding().decode(reinterpret_cast<const char*>(str), len); |
|
661 |
} |
|
662 |
||
663 |
static inline String toString(const xmlChar* str) |
|
664 |
{ |
|
665 |
if (!str) |
|
666 |
return String(); |
|
667 |
||
668 |
return UTF8Encoding().decode(reinterpret_cast<const char*>(str), strlen(reinterpret_cast<const char*>(str))); |
|
669 |
} |
|
670 |
||
671 |
struct _xmlSAX2Namespace { |
|
672 |
const xmlChar* prefix; |
|
673 |
const xmlChar* uri; |
|
674 |
}; |
|
675 |
typedef struct _xmlSAX2Namespace xmlSAX2Namespace; |
|
676 |
||
677 |
static inline void handleElementNamespaces(Element* newElement, const xmlChar** libxmlNamespaces, int nb_namespaces, ExceptionCode& ec) |
|
678 |
{ |
|
679 |
xmlSAX2Namespace* namespaces = reinterpret_cast<xmlSAX2Namespace*>(libxmlNamespaces); |
|
680 |
for (int i = 0; i < nb_namespaces; i++) { |
|
681 |
String namespaceQName = "xmlns"; |
|
682 |
String namespaceURI = toString(namespaces[i].uri); |
|
683 |
if (namespaces[i].prefix) |
|
684 |
namespaceQName = "xmlns:" + toString(namespaces[i].prefix); |
|
685 |
newElement->setAttributeNS("http://www.w3.org/2000/xmlns/", namespaceQName, namespaceURI, ec); |
|
686 |
if (ec) // exception setting attributes |
|
687 |
return; |
|
688 |
} |
|
689 |
} |
|
690 |
||
691 |
struct _xmlSAX2Attributes { |
|
692 |
const xmlChar* localname; |
|
693 |
const xmlChar* prefix; |
|
694 |
const xmlChar* uri; |
|
695 |
const xmlChar* value; |
|
696 |
const xmlChar* end; |
|
697 |
}; |
|
698 |
typedef struct _xmlSAX2Attributes xmlSAX2Attributes; |
|
699 |
||
700 |
static inline void handleElementAttributes(Element* newElement, const xmlChar** libxmlAttributes, int nb_attributes, ExceptionCode& ec) |
|
701 |
{ |
|
702 |
xmlSAX2Attributes* attributes = reinterpret_cast<xmlSAX2Attributes*>(libxmlAttributes); |
|
703 |
for (int i = 0; i < nb_attributes; i++) { |
|
704 |
String attrLocalName = toString(attributes[i].localname); |
|
705 |
int valueLength = (int) (attributes[i].end - attributes[i].value); |
|
706 |
String attrValue = toString(attributes[i].value, valueLength); |
|
707 |
String attrPrefix = toString(attributes[i].prefix); |
|
708 |
String attrURI = attrPrefix.isEmpty() ? String() : toString(attributes[i].uri); |
|
709 |
String attrQName = attrPrefix.isEmpty() ? attrLocalName : attrPrefix + ":" + attrLocalName; |
|
710 |
||
711 |
newElement->setAttributeNS(attrURI, attrQName, attrValue, ec); |
|
712 |
if (ec) // exception setting attributes |
|
713 |
return; |
|
714 |
} |
|
715 |
} |
|
716 |
||
717 |
void XMLTokenizer::startElementNs(const xmlChar* xmlLocalName, const xmlChar* xmlPrefix, const xmlChar* xmlURI, int nb_namespaces, |
|
718 |
const xmlChar** libxmlNamespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes) |
|
719 |
{ |
|
720 |
if (m_parserStopped) |
|
721 |
return; |
|
722 |
||
723 |
if (m_parserPaused) { |
|
724 |
m_pendingCallbacks->appendStartElementNSCallback(xmlLocalName, xmlPrefix, xmlURI, nb_namespaces, libxmlNamespaces, |
|
725 |
nb_attributes, nb_defaulted, libxmlAttributes); |
|
726 |
return; |
|
727 |
} |
|
728 |
||
729 |
#if ENABLE(XHTMLMP) |
|
730 |
// check if the DOCTYPE Declaration of XHTMLMP document exists |
|
731 |
if (!m_hasDocTypeDeclaration && m_doc->isXHTMLMPDocument()) { |
|
732 |
handleError(fatal, "DOCTYPE declaration lost.", lineNumber(), columnNumber()); |
|
733 |
return; |
|
734 |
} |
|
735 |
#endif |
|
736 |
||
737 |
exitText(); |
|
738 |
||
739 |
String localName = toString(xmlLocalName); |
|
740 |
String uri = toString(xmlURI); |
|
741 |
String prefix = toString(xmlPrefix); |
|
742 |
||
743 |
if (m_parsingFragment && uri.isNull()) { |
|
744 |
if (!prefix.isNull()) |
|
745 |
uri = m_prefixToNamespaceMap.get(prefix); |
|
746 |
else |
|
747 |
uri = m_defaultNamespaceURI; |
|
748 |
} |
|
749 |
||
750 |
#if ENABLE(XHTMLMP) |
|
751 |
if (!m_sawFirstElement && isXHTMLMPDocument()) { |
|
752 |
// As per the section 7.1 of OMA-WAP-XHTMLMP-V1_1-20061020-A.pdf, |
|
753 |
// we should make sure that the root element MUST be 'html' and |
|
754 |
// ensure the name of the default namespace on the root elment 'html' |
|
755 |
// MUST be 'http://www.w3.org/1999/xhtml' |
|
756 |
if (localName != HTMLNames::htmlTag.localName()) { |
|
757 |
handleError(fatal, "XHTMLMP document expects 'html' as root element.", lineNumber(), columnNumber()); |
|
758 |
return; |
|
759 |
} |
|
760 |
||
761 |
if (uri.isNull()) { |
|
762 |
m_defaultNamespaceURI = HTMLNames::xhtmlNamespaceURI; |
|
763 |
uri = m_defaultNamespaceURI; |
|
764 |
} |
|
765 |
} |
|
766 |
#endif |
|
767 |
||
768 |
bool isFirstElement = !m_sawFirstElement; |
|
769 |
m_sawFirstElement = true; |
|
770 |
||
771 |
QualifiedName qName(prefix, localName, uri); |
|
772 |
RefPtr<Element> newElement = m_doc->createElement(qName, true); |
|
773 |
if (!newElement) { |
|
774 |
stopParsing(); |
|
775 |
return; |
|
776 |
} |
|
777 |
||
778 |
ExceptionCode ec = 0; |
|
779 |
handleElementNamespaces(newElement.get(), libxmlNamespaces, nb_namespaces, ec); |
|
780 |
if (ec) { |
|
781 |
stopParsing(); |
|
782 |
return; |
|
783 |
} |
|
784 |
||
785 |
ScriptController* jsProxy = m_doc->frame() ? m_doc->frame()->script() : 0; |
|
786 |
if (jsProxy && m_doc->frame()->script()->isEnabled()) |
|
787 |
jsProxy->setEventHandlerLineNumber(lineNumber()); |
|
788 |
||
789 |
handleElementAttributes(newElement.get(), libxmlAttributes, nb_attributes, ec); |
|
790 |
if (ec) { |
|
791 |
stopParsing(); |
|
792 |
return; |
|
793 |
} |
|
794 |
||
795 |
if (jsProxy) |
|
796 |
jsProxy->setEventHandlerLineNumber(0); |
|
797 |
||
798 |
newElement->beginParsingChildren(); |
|
799 |
||
800 |
ScriptElement* scriptElement = toScriptElement(newElement.get()); |
|
801 |
if (scriptElement) |
|
802 |
m_scriptStartLine = lineNumber(); |
|
803 |
||
804 |
if (!m_currentNode->addChild(newElement.get())) { |
|
805 |
stopParsing(); |
|
806 |
return; |
|
807 |
} |
|
808 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
809 |
pushCurrentNode(newElement.get()); |
0 | 810 |
if (m_view && !newElement->attached()) |
811 |
newElement->attach(); |
|
812 |
||
813 |
if (isFirstElement && m_doc->frame()) |
|
814 |
m_doc->frame()->loader()->dispatchDocumentElementAvailable(); |
|
815 |
} |
|
816 |
||
817 |
void XMLTokenizer::endElementNs() |
|
818 |
{ |
|
819 |
if (m_parserStopped) |
|
820 |
return; |
|
821 |
||
822 |
if (m_parserPaused) { |
|
823 |
m_pendingCallbacks->appendEndElementNSCallback(); |
|
824 |
return; |
|
825 |
} |
|
826 |
||
827 |
exitText(); |
|
828 |
||
829 |
Node* n = m_currentNode; |
|
830 |
n->finishParsingChildren(); |
|
831 |
||
832 |
if (!n->isElementNode() || !m_view) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
833 |
popCurrentNode(); |
0 | 834 |
return; |
835 |
} |
|
836 |
||
837 |
Element* element = static_cast<Element*>(n); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
838 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
839 |
// The element's parent may have already been removed from document. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
840 |
// Parsing continues in this case, but scripts aren't executed. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
841 |
if (!element->inDocument()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
842 |
popCurrentNode(); |
0 | 843 |
return; |
844 |
} |
|
845 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
846 |
ScriptElement* scriptElement = toScriptElement(element); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
847 |
if (!scriptElement) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
848 |
popCurrentNode(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
849 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
850 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
851 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
852 |
// Don't load external scripts for standalone documents (for now). |
0 | 853 |
ASSERT(!m_pendingScript); |
854 |
m_requestingScript = true; |
|
855 |
||
856 |
#if ENABLE(XHTMLMP) |
|
857 |
if (!scriptElement->shouldExecuteAsJavaScript()) |
|
858 |
m_doc->setShouldProcessNoscriptElement(true); |
|
859 |
else |
|
860 |
#endif |
|
861 |
{ |
|
862 |
String scriptHref = scriptElement->sourceAttributeValue(); |
|
863 |
if (!scriptHref.isEmpty()) { |
|
864 |
// we have a src attribute |
|
865 |
String scriptCharset = scriptElement->scriptCharset(); |
|
866 |
if (element->dispatchBeforeLoadEvent(scriptHref) && |
|
867 |
(m_pendingScript = m_doc->docLoader()->requestScript(scriptHref, scriptCharset))) { |
|
868 |
m_scriptElement = element; |
|
869 |
m_pendingScript->addClient(this); |
|
870 |
||
871 |
// m_pendingScript will be 0 if script was already loaded and ref() executed it |
|
872 |
if (m_pendingScript) |
|
873 |
pauseParsing(); |
|
874 |
} else |
|
875 |
m_scriptElement = 0; |
|
876 |
} else |
|
877 |
m_view->frame()->script()->executeScript(ScriptSourceCode(scriptElement->scriptContent(), m_doc->url(), m_scriptStartLine)); |
|
878 |
} |
|
879 |
m_requestingScript = false; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
880 |
popCurrentNode(); |
0 | 881 |
} |
882 |
||
883 |
void XMLTokenizer::characters(const xmlChar* s, int len) |
|
884 |
{ |
|
885 |
if (m_parserStopped) |
|
886 |
return; |
|
887 |
||
888 |
if (m_parserPaused) { |
|
889 |
m_pendingCallbacks->appendCharactersCallback(s, len); |
|
890 |
return; |
|
891 |
} |
|
892 |
||
893 |
if (m_currentNode->isTextNode() || enterText()) |
|
894 |
m_bufferedText.append(s, len); |
|
895 |
} |
|
896 |
||
897 |
void XMLTokenizer::error(ErrorType type, const char* message, va_list args) |
|
898 |
{ |
|
899 |
if (m_parserStopped) |
|
900 |
return; |
|
901 |
||
902 |
#if COMPILER(MSVC) |
|
903 |
char m[1024]; |
|
904 |
vsnprintf(m, sizeof(m) - 1, message, args); |
|
905 |
#else |
|
906 |
char* m; |
|
907 |
if (vasprintf(&m, message, args) == -1) |
|
908 |
return; |
|
909 |
#endif |
|
910 |
||
911 |
if (m_parserPaused) |
|
912 |
m_pendingCallbacks->appendErrorCallback(type, m, lineNumber(), columnNumber()); |
|
913 |
else |
|
914 |
handleError(type, m, lineNumber(), columnNumber()); |
|
915 |
||
916 |
#if !COMPILER(MSVC) |
|
917 |
free(m); |
|
918 |
#endif |
|
919 |
} |
|
920 |
||
921 |
void XMLTokenizer::processingInstruction(const xmlChar* target, const xmlChar* data) |
|
922 |
{ |
|
923 |
if (m_parserStopped) |
|
924 |
return; |
|
925 |
||
926 |
if (m_parserPaused) { |
|
927 |
m_pendingCallbacks->appendProcessingInstructionCallback(target, data); |
|
928 |
return; |
|
929 |
} |
|
930 |
||
931 |
exitText(); |
|
932 |
||
933 |
// ### handle exceptions |
|
934 |
int exception = 0; |
|
935 |
RefPtr<ProcessingInstruction> pi = m_doc->createProcessingInstruction( |
|
936 |
toString(target), toString(data), exception); |
|
937 |
if (exception) |
|
938 |
return; |
|
939 |
||
940 |
pi->setCreatedByParser(true); |
|
941 |
||
942 |
if (!m_currentNode->addChild(pi.get())) |
|
943 |
return; |
|
944 |
if (m_view && !pi->attached()) |
|
945 |
pi->attach(); |
|
946 |
||
947 |
pi->finishParsingChildren(); |
|
948 |
||
949 |
#if ENABLE(XSLT) |
|
950 |
m_sawXSLTransform = !m_sawFirstElement && pi->isXSL(); |
|
951 |
if (m_sawXSLTransform && !m_doc->transformSourceDocument()) |
|
952 |
stopParsing(); |
|
953 |
#endif |
|
954 |
} |
|
955 |
||
956 |
void XMLTokenizer::cdataBlock(const xmlChar* s, int len) |
|
957 |
{ |
|
958 |
if (m_parserStopped) |
|
959 |
return; |
|
960 |
||
961 |
if (m_parserPaused) { |
|
962 |
m_pendingCallbacks->appendCDATABlockCallback(s, len); |
|
963 |
return; |
|
964 |
} |
|
965 |
||
966 |
exitText(); |
|
967 |
||
968 |
RefPtr<Node> newNode = CDATASection::create(m_doc, toString(s, len)); |
|
969 |
if (!m_currentNode->addChild(newNode.get())) |
|
970 |
return; |
|
971 |
if (m_view && !newNode->attached()) |
|
972 |
newNode->attach(); |
|
973 |
} |
|
974 |
||
975 |
void XMLTokenizer::comment(const xmlChar* s) |
|
976 |
{ |
|
977 |
if (m_parserStopped) |
|
978 |
return; |
|
979 |
||
980 |
if (m_parserPaused) { |
|
981 |
m_pendingCallbacks->appendCommentCallback(s); |
|
982 |
return; |
|
983 |
} |
|
984 |
||
985 |
exitText(); |
|
986 |
||
987 |
RefPtr<Node> newNode = Comment::create(m_doc, toString(s)); |
|
988 |
m_currentNode->addChild(newNode.get()); |
|
989 |
if (m_view && !newNode->attached()) |
|
990 |
newNode->attach(); |
|
991 |
} |
|
992 |
||
993 |
void XMLTokenizer::startDocument(const xmlChar* version, const xmlChar* encoding, int standalone) |
|
994 |
{ |
|
995 |
ExceptionCode ec = 0; |
|
996 |
||
997 |
if (version) |
|
998 |
m_doc->setXMLVersion(toString(version), ec); |
|
999 |
m_doc->setXMLStandalone(standalone == 1, ec); // possible values are 0, 1, and -1 |
|
1000 |
if (encoding) |
|
1001 |
m_doc->setXMLEncoding(toString(encoding)); |
|
1002 |
} |
|
1003 |
||
1004 |
void XMLTokenizer::endDocument() |
|
1005 |
{ |
|
1006 |
exitText(); |
|
1007 |
#if ENABLE(XHTMLMP) |
|
1008 |
m_hasDocTypeDeclaration = false; |
|
1009 |
#endif |
|
1010 |
} |
|
1011 |
||
1012 |
void XMLTokenizer::internalSubset(const xmlChar* name, const xmlChar* externalID, const xmlChar* systemID) |
|
1013 |
{ |
|
1014 |
if (m_parserStopped) |
|
1015 |
return; |
|
1016 |
||
1017 |
if (m_parserPaused) { |
|
1018 |
m_pendingCallbacks->appendInternalSubsetCallback(name, externalID, systemID); |
|
1019 |
return; |
|
1020 |
} |
|
1021 |
||
1022 |
if (m_doc) { |
|
1023 |
#if ENABLE(WML) || ENABLE(XHTMLMP) |
|
1024 |
String extId = toString(externalID); |
|
1025 |
#endif |
|
1026 |
#if ENABLE(WML) |
|
1027 |
if (isWMLDocument() |
|
1028 |
&& extId != "-//WAPFORUM//DTD WML 1.3//EN" |
|
1029 |
&& extId != "-//WAPFORUM//DTD WML 1.2//EN" |
|
1030 |
&& extId != "-//WAPFORUM//DTD WML 1.1//EN" |
|
1031 |
&& extId != "-//WAPFORUM//DTD WML 1.0//EN") |
|
1032 |
handleError(fatal, "Invalid DTD Public ID", lineNumber(), columnNumber()); |
|
1033 |
#endif |
|
1034 |
#if ENABLE(XHTMLMP) |
|
1035 |
String dtdName = toString(name); |
|
1036 |
if (extId == "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" |
|
1037 |
|| extId == "-//WAPFORUM//DTD XHTML Mobile 1.1//EN") { |
|
1038 |
if (dtdName != HTMLNames::htmlTag.localName()) { |
|
1039 |
handleError(fatal, "Invalid DOCTYPE declaration, expected 'html' as root element.", lineNumber(), columnNumber()); |
|
1040 |
return; |
|
1041 |
} |
|
1042 |
||
1043 |
if (m_doc->isXHTMLMPDocument()) |
|
1044 |
setIsXHTMLMPDocument(true); |
|
1045 |
else |
|
1046 |
setIsXHTMLDocument(true); |
|
1047 |
||
1048 |
m_hasDocTypeDeclaration = true; |
|
1049 |
} |
|
1050 |
#endif |
|
1051 |
||
1052 |
#if ENABLE(XHTMLMP) |
|
1053 |
m_doc->addChild(DocumentType::create(m_doc, dtdName, extId, toString(systemID))); |
|
1054 |
#elif ENABLE(WML) |
|
1055 |
m_doc->addChild(DocumentType::create(m_doc, toString(name), extId, toString(systemID))); |
|
1056 |
#else |
|
1057 |
m_doc->addChild(DocumentType::create(m_doc, toString(name), toString(externalID), toString(systemID))); |
|
1058 |
#endif |
|
1059 |
} |
|
1060 |
} |
|
1061 |
||
1062 |
static inline XMLTokenizer* getTokenizer(void* closure) |
|
1063 |
{ |
|
1064 |
xmlParserCtxtPtr ctxt = static_cast<xmlParserCtxtPtr>(closure); |
|
1065 |
return static_cast<XMLTokenizer*>(ctxt->_private); |
|
1066 |
} |
|
1067 |
||
1068 |
// This is a hack around http://bugzilla.gnome.org/show_bug.cgi?id=159219 |
|
1069 |
// Otherwise libxml seems to call all the SAX callbacks twice for any replaced entity. |
|
1070 |
static inline bool hackAroundLibXMLEntityBug(void* closure) |
|
1071 |
{ |
|
1072 |
#if LIBXML_VERSION >= 20627 |
|
1073 |
UNUSED_PARAM(closure); |
|
1074 |
||
1075 |
// This bug has been fixed in libxml 2.6.27. |
|
1076 |
return false; |
|
1077 |
#else |
|
1078 |
return static_cast<xmlParserCtxtPtr>(closure)->node; |
|
1079 |
#endif |
|
1080 |
} |
|
1081 |
||
1082 |
static void startElementNsHandler(void* closure, const xmlChar* localname, const xmlChar* prefix, const xmlChar* uri, int nb_namespaces, const xmlChar** namespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes) |
|
1083 |
{ |
|
1084 |
if (hackAroundLibXMLEntityBug(closure)) |
|
1085 |
return; |
|
1086 |
||
1087 |
getTokenizer(closure)->startElementNs(localname, prefix, uri, nb_namespaces, namespaces, nb_attributes, nb_defaulted, libxmlAttributes); |
|
1088 |
} |
|
1089 |
||
1090 |
static void endElementNsHandler(void* closure, const xmlChar*, const xmlChar*, const xmlChar*) |
|
1091 |
{ |
|
1092 |
if (hackAroundLibXMLEntityBug(closure)) |
|
1093 |
return; |
|
1094 |
||
1095 |
getTokenizer(closure)->endElementNs(); |
|
1096 |
} |
|
1097 |
||
1098 |
static void charactersHandler(void* closure, const xmlChar* s, int len) |
|
1099 |
{ |
|
1100 |
if (hackAroundLibXMLEntityBug(closure)) |
|
1101 |
return; |
|
1102 |
||
1103 |
getTokenizer(closure)->characters(s, len); |
|
1104 |
} |
|
1105 |
||
1106 |
static void processingInstructionHandler(void* closure, const xmlChar* target, const xmlChar* data) |
|
1107 |
{ |
|
1108 |
if (hackAroundLibXMLEntityBug(closure)) |
|
1109 |
return; |
|
1110 |
||
1111 |
getTokenizer(closure)->processingInstruction(target, data); |
|
1112 |
} |
|
1113 |
||
1114 |
static void cdataBlockHandler(void* closure, const xmlChar* s, int len) |
|
1115 |
{ |
|
1116 |
if (hackAroundLibXMLEntityBug(closure)) |
|
1117 |
return; |
|
1118 |
||
1119 |
getTokenizer(closure)->cdataBlock(s, len); |
|
1120 |
} |
|
1121 |
||
1122 |
static void commentHandler(void* closure, const xmlChar* comment) |
|
1123 |
{ |
|
1124 |
if (hackAroundLibXMLEntityBug(closure)) |
|
1125 |
return; |
|
1126 |
||
1127 |
getTokenizer(closure)->comment(comment); |
|
1128 |
} |
|
1129 |
||
1130 |
WTF_ATTRIBUTE_PRINTF(2, 3) |
|
1131 |
static void warningHandler(void* closure, const char* message, ...) |
|
1132 |
{ |
|
1133 |
va_list args; |
|
1134 |
va_start(args, message); |
|
1135 |
getTokenizer(closure)->error(XMLTokenizer::warning, message, args); |
|
1136 |
va_end(args); |
|
1137 |
} |
|
1138 |
||
1139 |
WTF_ATTRIBUTE_PRINTF(2, 3) |
|
1140 |
static void fatalErrorHandler(void* closure, const char* message, ...) |
|
1141 |
{ |
|
1142 |
va_list args; |
|
1143 |
va_start(args, message); |
|
1144 |
getTokenizer(closure)->error(XMLTokenizer::fatal, message, args); |
|
1145 |
va_end(args); |
|
1146 |
} |
|
1147 |
||
1148 |
WTF_ATTRIBUTE_PRINTF(2, 3) |
|
1149 |
static void normalErrorHandler(void* closure, const char* message, ...) |
|
1150 |
{ |
|
1151 |
va_list args; |
|
1152 |
va_start(args, message); |
|
1153 |
getTokenizer(closure)->error(XMLTokenizer::nonFatal, message, args); |
|
1154 |
va_end(args); |
|
1155 |
} |
|
1156 |
||
1157 |
// Using a static entity and marking it XML_INTERNAL_PREDEFINED_ENTITY is |
|
1158 |
// a hack to avoid malloc/free. Using a global variable like this could cause trouble |
|
1159 |
// if libxml implementation details were to change |
|
1160 |
static xmlChar sharedXHTMLEntityResult[5] = {0, 0, 0, 0, 0}; |
|
1161 |
||
1162 |
static xmlEntityPtr sharedXHTMLEntity() |
|
1163 |
{ |
|
1164 |
static xmlEntity entity; |
|
1165 |
if (!entity.type) { |
|
1166 |
entity.type = XML_ENTITY_DECL; |
|
1167 |
entity.orig = sharedXHTMLEntityResult; |
|
1168 |
entity.content = sharedXHTMLEntityResult; |
|
1169 |
entity.etype = XML_INTERNAL_PREDEFINED_ENTITY; |
|
1170 |
} |
|
1171 |
return &entity; |
|
1172 |
} |
|
1173 |
||
1174 |
static xmlEntityPtr getXHTMLEntity(const xmlChar* name) |
|
1175 |
{ |
|
1176 |
UChar c = decodeNamedEntity(reinterpret_cast<const char*>(name)); |
|
1177 |
if (!c) |
|
1178 |
return 0; |
|
1179 |
||
1180 |
CString value = String(&c, 1).utf8(); |
|
1181 |
ASSERT(value.length() < 5); |
|
1182 |
xmlEntityPtr entity = sharedXHTMLEntity(); |
|
1183 |
entity->length = value.length(); |
|
1184 |
entity->name = name; |
|
1185 |
memcpy(sharedXHTMLEntityResult, value.data(), entity->length + 1); |
|
1186 |
||
1187 |
return entity; |
|
1188 |
} |
|
1189 |
||
1190 |
static xmlEntityPtr getEntityHandler(void* closure, const xmlChar* name) |
|
1191 |
{ |
|
1192 |
xmlParserCtxtPtr ctxt = static_cast<xmlParserCtxtPtr>(closure); |
|
1193 |
xmlEntityPtr ent = xmlGetPredefinedEntity(name); |
|
1194 |
if (ent) { |
|
1195 |
ent->etype = XML_INTERNAL_PREDEFINED_ENTITY; |
|
1196 |
return ent; |
|
1197 |
} |
|
1198 |
||
1199 |
ent = xmlGetDocEntity(ctxt->myDoc, name); |
|
1200 |
if (!ent && (getTokenizer(closure)->isXHTMLDocument() |
|
1201 |
#if ENABLE(XHTMLMP) |
|
1202 |
|| getTokenizer(closure)->isXHTMLMPDocument() |
|
1203 |
#endif |
|
1204 |
#if ENABLE(WML) |
|
1205 |
|| getTokenizer(closure)->isWMLDocument() |
|
1206 |
#endif |
|
1207 |
)) { |
|
1208 |
ent = getXHTMLEntity(name); |
|
1209 |
if (ent) |
|
1210 |
ent->etype = XML_INTERNAL_GENERAL_ENTITY; |
|
1211 |
} |
|
1212 |
||
1213 |
return ent; |
|
1214 |
} |
|
1215 |
||
1216 |
static void startDocumentHandler(void* closure) |
|
1217 |
{ |
|
1218 |
xmlParserCtxt* ctxt = static_cast<xmlParserCtxt*>(closure); |
|
1219 |
getTokenizer(closure)->startDocument(ctxt->version, ctxt->encoding, ctxt->standalone); |
|
1220 |
xmlSAX2StartDocument(closure); |
|
1221 |
} |
|
1222 |
||
1223 |
static void endDocumentHandler(void* closure) |
|
1224 |
{ |
|
1225 |
getTokenizer(closure)->endDocument(); |
|
1226 |
xmlSAX2EndDocument(closure); |
|
1227 |
} |
|
1228 |
||
1229 |
static void internalSubsetHandler(void* closure, const xmlChar* name, const xmlChar* externalID, const xmlChar* systemID) |
|
1230 |
{ |
|
1231 |
getTokenizer(closure)->internalSubset(name, externalID, systemID); |
|
1232 |
xmlSAX2InternalSubset(closure, name, externalID, systemID); |
|
1233 |
} |
|
1234 |
||
1235 |
static void externalSubsetHandler(void* closure, const xmlChar*, const xmlChar* externalId, const xmlChar*) |
|
1236 |
{ |
|
1237 |
String extId = toString(externalId); |
|
1238 |
if ((extId == "-//W3C//DTD XHTML 1.0 Transitional//EN") |
|
1239 |
|| (extId == "-//W3C//DTD XHTML 1.1//EN") |
|
1240 |
|| (extId == "-//W3C//DTD XHTML 1.0 Strict//EN") |
|
1241 |
|| (extId == "-//W3C//DTD XHTML 1.0 Frameset//EN") |
|
1242 |
|| (extId == "-//W3C//DTD XHTML Basic 1.0//EN") |
|
1243 |
|| (extId == "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN") |
|
1244 |
|| (extId == "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN") |
|
1245 |
#if !ENABLE(XHTMLMP) |
|
1246 |
|| (extId == "-//WAPFORUM//DTD XHTML Mobile 1.0//EN") |
|
1247 |
#endif |
|
1248 |
) |
|
1249 |
getTokenizer(closure)->setIsXHTMLDocument(true); // controls if we replace entities or not. |
|
1250 |
} |
|
1251 |
||
1252 |
static void ignorableWhitespaceHandler(void*, const xmlChar*, int) |
|
1253 |
{ |
|
1254 |
// nothing to do, but we need this to work around a crasher |
|
1255 |
// http://bugzilla.gnome.org/show_bug.cgi?id=172255 |
|
1256 |
// http://bugs.webkit.org/show_bug.cgi?id=5792 |
|
1257 |
} |
|
1258 |
||
1259 |
void XMLTokenizer::initializeParserContext(const char* chunk) |
|
1260 |
{ |
|
1261 |
xmlSAXHandler sax; |
|
1262 |
memset(&sax, 0, sizeof(sax)); |
|
1263 |
||
1264 |
sax.error = normalErrorHandler; |
|
1265 |
sax.fatalError = fatalErrorHandler; |
|
1266 |
sax.characters = charactersHandler; |
|
1267 |
sax.processingInstruction = processingInstructionHandler; |
|
1268 |
sax.cdataBlock = cdataBlockHandler; |
|
1269 |
sax.comment = commentHandler; |
|
1270 |
sax.warning = warningHandler; |
|
1271 |
sax.startElementNs = startElementNsHandler; |
|
1272 |
sax.endElementNs = endElementNsHandler; |
|
1273 |
sax.getEntity = getEntityHandler; |
|
1274 |
sax.startDocument = startDocumentHandler; |
|
1275 |
sax.endDocument = endDocumentHandler; |
|
1276 |
sax.internalSubset = internalSubsetHandler; |
|
1277 |
sax.externalSubset = externalSubsetHandler; |
|
1278 |
sax.ignorableWhitespace = ignorableWhitespaceHandler; |
|
1279 |
sax.entityDecl = xmlSAX2EntityDecl; |
|
1280 |
sax.initialized = XML_SAX2_MAGIC; |
|
1281 |
m_parserStopped = false; |
|
1282 |
m_sawError = false; |
|
1283 |
m_sawXSLTransform = false; |
|
1284 |
m_sawFirstElement = false; |
|
1285 |
||
1286 |
XMLTokenizerScope scope(m_doc->docLoader()); |
|
1287 |
if (m_parsingFragment) |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1288 |
m_context = XMLParserContext::createMemoryParser(&sax, this, chunk); |
0 | 1289 |
else |
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1290 |
m_context = XMLParserContext::createStringParser(&sax, this); |
0 | 1291 |
} |
1292 |
||
1293 |
void XMLTokenizer::doEnd() |
|
1294 |
{ |
|
1295 |
#if ENABLE(XSLT) |
|
1296 |
if (m_sawXSLTransform) { |
|
1297 |
void* doc = xmlDocPtrForString(m_doc->docLoader(), m_originalSourceForTransform, m_doc->url().string()); |
|
1298 |
m_doc->setTransformSource(new TransformSource(doc)); |
|
1299 |
||
1300 |
m_doc->setParsing(false); // Make the doc think it's done, so it will apply xsl sheets. |
|
1301 |
m_doc->updateStyleSelector(); |
|
1302 |
m_doc->setParsing(true); |
|
1303 |
m_parserStopped = true; |
|
1304 |
} |
|
1305 |
#endif |
|
1306 |
||
1307 |
if (m_context) { |
|
1308 |
// Tell libxml we're done. |
|
1309 |
{ |
|
1310 |
XMLTokenizerScope scope(m_doc->docLoader()); |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1311 |
xmlParseChunk(context(), 0, 0, 1); |
0 | 1312 |
} |
1313 |
||
1314 |
m_context = 0; |
|
1315 |
} |
|
1316 |
} |
|
1317 |
||
1318 |
#if ENABLE(XSLT) |
|
1319 |
void* xmlDocPtrForString(DocLoader* docLoader, const String& source, const String& url) |
|
1320 |
{ |
|
1321 |
if (source.isEmpty()) |
|
1322 |
return 0; |
|
1323 |
||
1324 |
// Parse in a single chunk into an xmlDocPtr |
|
1325 |
// FIXME: Hook up error handlers so that a failure to parse the main document results in |
|
1326 |
// good error messages. |
|
1327 |
const UChar BOM = 0xFEFF; |
|
1328 |
const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM); |
|
1329 |
||
1330 |
XMLTokenizerScope scope(docLoader, errorFunc, 0); |
|
1331 |
xmlDocPtr sourceDoc = xmlReadMemory(reinterpret_cast<const char*>(source.characters()), |
|
1332 |
source.length() * sizeof(UChar), |
|
1333 |
url.latin1().data(), |
|
1334 |
BOMHighByte == 0xFF ? "UTF-16LE" : "UTF-16BE", |
|
1335 |
XSLT_PARSE_OPTIONS); |
|
1336 |
return sourceDoc; |
|
1337 |
} |
|
1338 |
#endif |
|
1339 |
||
1340 |
int XMLTokenizer::lineNumber() const |
|
1341 |
{ |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1342 |
return context() ? context()->input->line : 1; |
0 | 1343 |
} |
1344 |
||
1345 |
int XMLTokenizer::columnNumber() const |
|
1346 |
{ |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1347 |
return context() ? context()->input->col : 1; |
0 | 1348 |
} |
1349 |
||
1350 |
void XMLTokenizer::stopParsing() |
|
1351 |
{ |
|
1352 |
Tokenizer::stopParsing(); |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1353 |
if (context()) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1354 |
xmlStopParser(context()); |
0 | 1355 |
} |
1356 |
||
1357 |
void XMLTokenizer::resumeParsing() |
|
1358 |
{ |
|
1359 |
ASSERT(m_parserPaused); |
|
1360 |
||
1361 |
m_parserPaused = false; |
|
1362 |
||
1363 |
// First, execute any pending callbacks |
|
1364 |
while (!m_pendingCallbacks->isEmpty()) { |
|
1365 |
m_pendingCallbacks->callAndRemoveFirstCallback(this); |
|
1366 |
||
1367 |
// A callback paused the parser |
|
1368 |
if (m_parserPaused) |
|
1369 |
return; |
|
1370 |
} |
|
1371 |
||
1372 |
// Then, write any pending data |
|
1373 |
SegmentedString rest = m_pendingSrc; |
|
1374 |
m_pendingSrc.clear(); |
|
1375 |
write(rest, false); |
|
1376 |
||
1377 |
// Finally, if finish() has been called and write() didn't result |
|
1378 |
// in any further callbacks being queued, call end() |
|
1379 |
if (m_finishCalled && m_pendingCallbacks->isEmpty()) |
|
1380 |
end(); |
|
1381 |
} |
|
1382 |
||
1383 |
bool parseXMLDocumentFragment(const String& chunk, DocumentFragment* fragment, Element* parent) |
|
1384 |
{ |
|
1385 |
if (!chunk.length()) |
|
1386 |
return true; |
|
1387 |
||
1388 |
XMLTokenizer tokenizer(fragment, parent); |
|
1389 |
||
1390 |
CString chunkAsUtf8 = chunk.utf8(); |
|
1391 |
tokenizer.initializeParserContext(chunkAsUtf8.data()); |
|
1392 |
||
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1393 |
xmlParseContent(tokenizer.context()); |
0 | 1394 |
|
1395 |
tokenizer.endDocument(); |
|
1396 |
||
1397 |
// Check if all the chunk has been processed. |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1398 |
long bytesProcessed = xmlByteConsumed(tokenizer.context()); |
0 | 1399 |
if (bytesProcessed == -1 || ((unsigned long)bytesProcessed) != chunkAsUtf8.length()) |
1400 |
return false; |
|
1401 |
||
1402 |
// No error if the chunk is well formed or it is not but we have no error. |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1403 |
return tokenizer.context()->wellFormed || xmlCtxtGetLastError(tokenizer.context()) == 0; |
0 | 1404 |
} |
1405 |
||
1406 |
// -------------------------------- |
|
1407 |
||
1408 |
struct AttributeParseState { |
|
1409 |
HashMap<String, String> attributes; |
|
1410 |
bool gotAttributes; |
|
1411 |
}; |
|
1412 |
||
1413 |
static void attributesStartElementNsHandler(void* closure, const xmlChar* xmlLocalName, const xmlChar* /*xmlPrefix*/, |
|
1414 |
const xmlChar* /*xmlURI*/, int /*nb_namespaces*/, const xmlChar** /*namespaces*/, |
|
1415 |
int nb_attributes, int /*nb_defaulted*/, const xmlChar** libxmlAttributes) |
|
1416 |
{ |
|
1417 |
if (strcmp(reinterpret_cast<const char*>(xmlLocalName), "attrs") != 0) |
|
1418 |
return; |
|
1419 |
||
1420 |
xmlParserCtxtPtr ctxt = static_cast<xmlParserCtxtPtr>(closure); |
|
1421 |
AttributeParseState* state = static_cast<AttributeParseState*>(ctxt->_private); |
|
1422 |
||
1423 |
state->gotAttributes = true; |
|
1424 |
||
1425 |
xmlSAX2Attributes* attributes = reinterpret_cast<xmlSAX2Attributes*>(libxmlAttributes); |
|
1426 |
for (int i = 0; i < nb_attributes; i++) { |
|
1427 |
String attrLocalName = toString(attributes[i].localname); |
|
1428 |
int valueLength = (int) (attributes[i].end - attributes[i].value); |
|
1429 |
String attrValue = toString(attributes[i].value, valueLength); |
|
1430 |
String attrPrefix = toString(attributes[i].prefix); |
|
1431 |
String attrQName = attrPrefix.isEmpty() ? attrLocalName : attrPrefix + ":" + attrLocalName; |
|
1432 |
||
1433 |
state->attributes.set(attrQName, attrValue); |
|
1434 |
} |
|
1435 |
} |
|
1436 |
||
1437 |
HashMap<String, String> parseAttributes(const String& string, bool& attrsOK) |
|
1438 |
{ |
|
1439 |
AttributeParseState state; |
|
1440 |
state.gotAttributes = false; |
|
1441 |
||
1442 |
xmlSAXHandler sax; |
|
1443 |
memset(&sax, 0, sizeof(sax)); |
|
1444 |
sax.startElementNs = attributesStartElementNsHandler; |
|
1445 |
sax.initialized = XML_SAX2_MAGIC; |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1446 |
RefPtr<XMLParserContext> parser = XMLParserContext::createStringParser(&sax, &state); |
0 | 1447 |
String parseString = "<?xml version=\"1.0\"?><attrs " + string + " />"; |
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1448 |
xmlParseChunk(parser->context(), reinterpret_cast<const char*>(parseString.characters()), parseString.length() * sizeof(UChar), 1); |
0 | 1449 |
attrsOK = state.gotAttributes; |
1450 |
return state.attributes; |
|
1451 |
} |
|
1452 |
||
1453 |
} |