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"
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 <QDebug>
54 #include <wtf/Platform.h>
55 #include <wtf/StringExtras.h>
56 #include <wtf/Threading.h>
57 #include <wtf/Vector.h>
58
59 #if ENABLE(XHTMLMP)
60 #include "HTMLNames.h"
61 #include "HTMLScriptElement.h"
62 #endif
63
64 using namespace std;
65
66 namespace WebCore {
67
68 #if QT_VERSION >= 0x040400
69 class EntityResolver : public QXmlStreamEntityResolver {
70 virtual QString resolveUndeclaredEntity(const QString &name);
71 };
72
resolveUndeclaredEntity(const QString & name)73 QString EntityResolver::resolveUndeclaredEntity(const QString &name)
74 {
75 UChar c = decodeNamedEntity(name.toUtf8().constData());
76 return QString(c);
77 }
78 #endif
79
80 // --------------------------------
81
XMLTokenizer(Document * _doc,FrameView * _view)82 XMLTokenizer::XMLTokenizer(Document* _doc, FrameView* _view)
83 : m_doc(_doc)
84 , m_view(_view)
85 , m_wroteText(false)
86 , m_currentNode(_doc)
87 , m_currentNodeIsReferenced(false)
88 , m_sawError(false)
89 , m_sawXSLTransform(false)
90 , m_sawFirstElement(false)
91 , m_isXHTMLDocument(false)
92 #if ENABLE(XHTMLMP)
93 , m_isXHTMLMPDocument(false)
94 , m_hasDocTypeDeclaration(false)
95 #endif
96 , m_parserPaused(false)
97 , m_requestingScript(false)
98 , m_finishCalled(false)
99 , m_errorCount(0)
100 , m_lastErrorLine(0)
101 , m_lastErrorColumn(0)
102 , m_pendingScript(0)
103 , m_scriptStartLine(0)
104 , m_parsingFragment(false)
105 {
106 #if QT_VERSION >= 0x040400
107 m_stream.setEntityResolver(new EntityResolver);
108 #endif
109 }
110
XMLTokenizer(DocumentFragment * fragment,Element * parentElement)111 XMLTokenizer::XMLTokenizer(DocumentFragment* fragment, Element* parentElement)
112 : m_doc(fragment->document())
113 , m_view(0)
114 , m_wroteText(false)
115 , m_currentNode(fragment)
116 , m_currentNodeIsReferenced(fragment)
117 , m_sawError(false)
118 , m_sawXSLTransform(false)
119 , m_sawFirstElement(false)
120 , m_isXHTMLDocument(false)
121 #if ENABLE(XHTMLMP)
122 , m_isXHTMLMPDocument(false)
123 , m_hasDocTypeDeclaration(false)
124 #endif
125 , m_parserPaused(false)
126 , m_requestingScript(false)
127 , m_finishCalled(false)
128 , m_errorCount(0)
129 , m_lastErrorLine(0)
130 , m_lastErrorColumn(0)
131 , m_pendingScript(0)
132 , m_scriptStartLine(0)
133 , m_parsingFragment(true)
134 {
135 if (fragment)
136 fragment->ref();
137 if (m_doc)
138 m_doc->ref();
139
140 // Add namespaces based on the parent node
141 Vector<Element*> elemStack;
142 while (parentElement) {
143 elemStack.append(parentElement);
144
145 Node* n = parentElement->parentNode();
146 if (!n || !n->isElementNode())
147 break;
148 parentElement = static_cast<Element*>(n);
149 }
150
151 if (elemStack.isEmpty())
152 return;
153
154 #if QT_VERSION < 0x040400
155 for (Element* element = elemStack.last(); !elemStack.isEmpty(); elemStack.removeLast()) {
156 if (NamedNodeMap* attrs = element->attributes()) {
157 for (unsigned i = 0; i < attrs->length(); i++) {
158 Attribute* attr = attrs->attributeItem(i);
159 if (attr->localName() == "xmlns")
160 m_defaultNamespaceURI = attr->value();
161 else if (attr->prefix() == "xmlns")
162 m_prefixToNamespaceMap.set(attr->localName(), attr->value());
163 }
164 }
165 }
166 #else
167 QXmlStreamNamespaceDeclarations namespaces;
168 for (Element* element = elemStack.last(); !elemStack.isEmpty(); elemStack.removeLast()) {
169 if (NamedNodeMap* attrs = element->attributes()) {
170 for (unsigned i = 0; i < attrs->length(); i++) {
171 Attribute* attr = attrs->attributeItem(i);
172 if (attr->localName() == "xmlns")
173 m_defaultNamespaceURI = attr->value();
174 else if (attr->prefix() == "xmlns")
175 namespaces.append(QXmlStreamNamespaceDeclaration(attr->localName(), attr->value()));
176 }
177 }
178 }
179 m_stream.addExtraNamespaceDeclarations(namespaces);
180 m_stream.setEntityResolver(new EntityResolver);
181 #endif
182
183 // If the parent element is not in document tree, there may be no xmlns attribute; just default to the parent's namespace.
184 if (m_defaultNamespaceURI.isNull() && !parentElement->inDocument())
185 m_defaultNamespaceURI = parentElement->namespaceURI();
186 }
187
~XMLTokenizer()188 XMLTokenizer::~XMLTokenizer()
189 {
190 setCurrentNode(0);
191 if (m_parsingFragment && m_doc)
192 m_doc->deref();
193 if (m_pendingScript)
194 m_pendingScript->removeClient(this);
195 #if QT_VERSION >= 0x040400
196 delete m_stream.entityResolver();
197 #endif
198 }
199
doWrite(const String & parseString)200 void XMLTokenizer::doWrite(const String& parseString)
201 {
202 m_wroteText = true;
203
204 if (m_doc->decoder() && m_doc->decoder()->sawError()) {
205 // If the decoder saw an error, report it as fatal (stops parsing)
206 handleError(fatal, "Encoding error", lineNumber(), columnNumber());
207 return;
208 }
209
210 QString data(parseString);
211 if (!data.isEmpty()) {
212 #if QT_VERSION < 0x040400
213 if (!m_sawFirstElement) {
214 int idx = data.indexOf(QLatin1String("<?xml"));
215 if (idx != -1) {
216 int start = idx + 5;
217 int end = data.indexOf(QLatin1String("?>"), start);
218 QString content = data.mid(start, end-start);
219 bool ok = true;
220 HashMap<String, String> attrs = parseAttributes(content, ok);
221 String version = attrs.get("version");
222 String encoding = attrs.get("encoding");
223 ExceptionCode ec = 0;
224 if (!m_parsingFragment) {
225 if (!version.isEmpty())
226 m_doc->setXMLVersion(version, ec);
227 if (!encoding.isEmpty())
228 m_doc->setXMLEncoding(encoding);
229 }
230 }
231 }
232 #endif
233 m_stream.addData(data);
234 parse();
235 }
236
237 return;
238 }
239
initializeParserContext(const char * chunk)240 void XMLTokenizer::initializeParserContext(const char* chunk)
241 {
242 m_parserStopped = false;
243 m_sawError = false;
244 m_sawXSLTransform = false;
245 m_sawFirstElement = false;
246 }
247
doEnd()248 void XMLTokenizer::doEnd()
249 {
250 #if ENABLE(XSLT)
251 #warning Look at XMLTokenizerLibXml.cpp
252 #endif
253
254 if (m_stream.error() == QXmlStreamReader::PrematureEndOfDocumentError || (m_wroteText && !m_sawFirstElement)) {
255 handleError(fatal, qPrintable(m_stream.errorString()), lineNumber(),
256 columnNumber());
257 }
258 }
259
260 #if ENABLE(XSLT)
xmlDocPtrForString(DocLoader * docLoader,const String & source,const String & url)261 void* xmlDocPtrForString(DocLoader* docLoader, const String& source, const String& url)
262 {
263 if (source.isEmpty())
264 return 0;
265
266 // Parse in a single chunk into an xmlDocPtr
267 // FIXME: Hook up error handlers so that a failure to parse the main document results in
268 // good error messages.
269 const UChar BOM = 0xFEFF;
270 const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM);
271
272 xmlGenericErrorFunc oldErrorFunc = xmlGenericError;
273 void* oldErrorContext = xmlGenericErrorContext;
274
275 setLoaderForLibXMLCallbacks(docLoader);
276 xmlSetGenericErrorFunc(0, errorFunc);
277
278 xmlDocPtr sourceDoc = xmlReadMemory(reinterpret_cast<const char*>(source.characters()),
279 source.length() * sizeof(UChar),
280 url.latin1().data(),
281 BOMHighByte == 0xFF ? "UTF-16LE" : "UTF-16BE",
282 XSLT_PARSE_OPTIONS);
283
284 setLoaderForLibXMLCallbacks(0);
285 xmlSetGenericErrorFunc(oldErrorContext, oldErrorFunc);
286
287 return sourceDoc;
288 }
289 #endif
290
lineNumber() const291 int XMLTokenizer::lineNumber() const
292 {
293 return m_stream.lineNumber();
294 }
295
columnNumber() const296 int XMLTokenizer::columnNumber() const
297 {
298 return m_stream.columnNumber();
299 }
300
stopParsing()301 void XMLTokenizer::stopParsing()
302 {
303 Tokenizer::stopParsing();
304 }
305
resumeParsing()306 void XMLTokenizer::resumeParsing()
307 {
308 ASSERT(m_parserPaused);
309
310 m_parserPaused = false;
311
312 // First, execute any pending callbacks
313 parse();
314 if (m_parserPaused)
315 return;
316
317 // Then, write any pending data
318 SegmentedString rest = m_pendingSrc;
319 m_pendingSrc.clear();
320 write(rest, false);
321
322 // Finally, if finish() has been called and write() didn't result
323 // in any further callbacks being queued, call end()
324 if (m_finishCalled && !m_parserPaused && !m_pendingScript)
325 end();
326 }
327
parseXMLDocumentFragment(const String & chunk,DocumentFragment * fragment,Element * parent)328 bool parseXMLDocumentFragment(const String& chunk, DocumentFragment* fragment, Element* parent)
329 {
330 if (!chunk.length())
331 return true;
332
333 XMLTokenizer tokenizer(fragment, parent);
334
335 tokenizer.write(String("<qxmlstreamdummyelement>"), false);
336 tokenizer.write(chunk, false);
337 tokenizer.write(String("</qxmlstreamdummyelement>"), false);
338 tokenizer.finish();
339 return !tokenizer.hasError();
340 }
341
342 // --------------------------------
343
344 struct AttributeParseState {
345 HashMap<String, String> attributes;
346 bool gotAttributes;
347 };
348
attributesStartElementNsHandler(AttributeParseState * state,const QXmlStreamAttributes & attrs)349 static void attributesStartElementNsHandler(AttributeParseState* state, const QXmlStreamAttributes& attrs)
350 {
351 if (attrs.count() <= 0)
352 return;
353
354 state->gotAttributes = true;
355
356 for (int i = 0; i < attrs.count(); i++) {
357 const QXmlStreamAttribute& attr = attrs[i];
358 String attrLocalName = attr.name();
359 String attrValue = attr.value();
360 String attrURI = attr.namespaceUri();
361 String attrQName = attr.qualifiedName();
362 state->attributes.set(attrQName, attrValue);
363 }
364 }
365
parseAttributes(const String & string,bool & attrsOK)366 HashMap<String, String> parseAttributes(const String& string, bool& attrsOK)
367 {
368 AttributeParseState state;
369 state.gotAttributes = false;
370
371 QXmlStreamReader stream;
372 QString dummy = QString(QLatin1String("<?xml version=\"1.0\"?><attrs %1 />")).arg(string);
373 stream.addData(dummy);
374 while (!stream.atEnd()) {
375 stream.readNext();
376 if (stream.isStartElement()) {
377 attributesStartElementNsHandler(&state, stream.attributes());
378 }
379 }
380 attrsOK = state.gotAttributes;
381 return state.attributes;
382 }
383
prefixFromQName(const QString & qName)384 static inline String prefixFromQName(const QString& qName)
385 {
386 const int offset = qName.indexOf(QLatin1Char(':'));
387 if (offset <= 0)
388 return String();
389 else
390 return qName.left(offset);
391 }
392
handleElementNamespaces(Element * newElement,const QXmlStreamNamespaceDeclarations & ns,ExceptionCode & ec)393 static inline void handleElementNamespaces(Element* newElement, const QXmlStreamNamespaceDeclarations &ns,
394 ExceptionCode& ec)
395 {
396 for (int i = 0; i < ns.count(); ++i) {
397 const QXmlStreamNamespaceDeclaration &decl = ns[i];
398 String namespaceURI = decl.namespaceUri();
399 String namespaceQName = decl.prefix().isEmpty() ? String("xmlns") : String("xmlns:") + decl.prefix();
400 newElement->setAttributeNS("http://www.w3.org/2000/xmlns/", namespaceQName, namespaceURI, ec);
401 if (ec) // exception setting attributes
402 return;
403 }
404 }
405
handleElementAttributes(Element * newElement,const QXmlStreamAttributes & attrs,ExceptionCode & ec)406 static inline void handleElementAttributes(Element* newElement, const QXmlStreamAttributes &attrs, ExceptionCode& ec)
407 {
408 for (int i = 0; i < attrs.count(); ++i) {
409 const QXmlStreamAttribute &attr = attrs[i];
410 String attrLocalName = attr.name();
411 String attrValue = attr.value();
412 String attrURI = attr.namespaceUri().isEmpty() ? String() : String(attr.namespaceUri());
413 String attrQName = attr.qualifiedName();
414 newElement->setAttributeNS(attrURI, attrQName, attrValue, ec);
415 if (ec) // exception setting attributes
416 return;
417 }
418 }
419
parse()420 void XMLTokenizer::parse()
421 {
422 while (!m_parserStopped && !m_parserPaused && !m_stream.atEnd()) {
423 m_stream.readNext();
424 switch (m_stream.tokenType()) {
425 case QXmlStreamReader::StartDocument: {
426 startDocument();
427 }
428 break;
429 case QXmlStreamReader::EndDocument: {
430 endDocument();
431 }
432 break;
433 case QXmlStreamReader::StartElement: {
434 #if ENABLE(XHTMLMP)
435 if (m_doc->isXHTMLMPDocument() && !m_hasDocTypeDeclaration) {
436 handleError(fatal, "DOCTYPE declaration lost.", lineNumber(), columnNumber());
437 break;
438 }
439 #endif
440 parseStartElement();
441 }
442 break;
443 case QXmlStreamReader::EndElement: {
444 parseEndElement();
445 }
446 break;
447 case QXmlStreamReader::Characters: {
448 if (m_stream.isCDATA()) {
449 //cdata
450 parseCdata();
451 } else {
452 //characters
453 parseCharacters();
454 }
455 }
456 break;
457 case QXmlStreamReader::Comment: {
458 parseComment();
459 }
460 break;
461 case QXmlStreamReader::DTD: {
462 //qDebug()<<"------------- DTD";
463 parseDtd();
464 #if ENABLE(XHTMLMP)
465 m_hasDocTypeDeclaration = true;
466 #endif
467 }
468 break;
469 case QXmlStreamReader::EntityReference: {
470 //qDebug()<<"---------- ENTITY = "<<m_stream.name().toString()
471 // <<", t = "<<m_stream.text().toString();
472 if (isXHTMLDocument()
473 #if ENABLE(XHTMLMP)
474 || isXHTMLMPDocument()
475 #endif
476 #if ENABLE(WML)
477 || isWMLDocument()
478 #endif
479 ) {
480 QString entity = m_stream.name().toString();
481 UChar c = decodeNamedEntity(entity.toUtf8().constData());
482 if (m_currentNode->isTextNode() || enterText()) {
483 ExceptionCode ec = 0;
484 String str(&c, 1);
485 //qDebug()<<" ------- adding entity "<<str;
486 static_cast<Text*>(m_currentNode)->appendData(str, ec);
487 }
488 }
489 }
490 break;
491 case QXmlStreamReader::ProcessingInstruction: {
492 parseProcessingInstruction();
493 }
494 break;
495 default: {
496 if (m_stream.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
497 ErrorType type = (m_stream.error() == QXmlStreamReader::NotWellFormedError) ?
498 fatal : warning;
499 handleError(type, qPrintable(m_stream.errorString()), lineNumber(),
500 columnNumber());
501 }
502 }
503 break;
504 }
505 }
506 }
507
startDocument()508 void XMLTokenizer::startDocument()
509 {
510 initializeParserContext();
511 ExceptionCode ec = 0;
512
513 if (!m_parsingFragment) {
514 m_doc->setXMLStandalone(m_stream.isStandaloneDocument(), ec);
515
516 #if QT_VERSION >= 0x040400
517 QStringRef version = m_stream.documentVersion();
518 if (!version.isEmpty())
519 m_doc->setXMLVersion(version, ec);
520 QStringRef encoding = m_stream.documentEncoding();
521 if (!encoding.isEmpty())
522 m_doc->setXMLEncoding(encoding);
523 #endif
524 }
525 }
526
parseStartElement()527 void XMLTokenizer::parseStartElement()
528 {
529 if (!m_sawFirstElement && m_parsingFragment) {
530 // skip dummy element for fragments
531 m_sawFirstElement = true;
532 return;
533 }
534
535 exitText();
536
537 String localName = m_stream.name();
538 String uri = m_stream.namespaceUri();
539 String prefix = prefixFromQName(m_stream.qualifiedName().toString());
540
541 if (m_parsingFragment && uri.isNull()) {
542 Q_ASSERT(prefix.isNull());
543 uri = m_defaultNamespaceURI;
544 }
545
546 QualifiedName qName(prefix, localName, uri);
547 RefPtr<Element> newElement = m_doc->createElement(qName, true);
548 if (!newElement) {
549 stopParsing();
550 return;
551 }
552
553 #if ENABLE(XHTMLMP)
554 if (!m_sawFirstElement && isXHTMLMPDocument()) {
555 // As per 7.1 section of OMA-WAP-XHTMLMP-V1_1-20061020-A.pdf,
556 // we should make sure that the root element MUST be 'html' and
557 // ensure the name of the default namespace on the root elment 'html'
558 // MUST be 'http://www.w3.org/1999/xhtml'
559 if (localName != HTMLNames::htmlTag.localName()) {
560 handleError(fatal, "XHTMLMP document expects 'html' as root element.", lineNumber(), columnNumber());
561 return;
562 }
563
564 if (uri.isNull()) {
565 m_defaultNamespaceURI = HTMLNames::xhtmlNamespaceURI;
566 uri = m_defaultNamespaceURI;
567 m_stream.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration(prefix, HTMLNames::xhtmlNamespaceURI));
568 }
569 }
570 #endif
571
572 bool isFirstElement = !m_sawFirstElement;
573 m_sawFirstElement = true;
574
575 ExceptionCode ec = 0;
576 handleElementNamespaces(newElement.get(), m_stream.namespaceDeclarations(), ec);
577 if (ec) {
578 stopParsing();
579 return;
580 }
581
582 handleElementAttributes(newElement.get(), m_stream.attributes(), ec);
583 if (ec) {
584 stopParsing();
585 return;
586 }
587
588 ScriptElement* scriptElement = toScriptElement(newElement.get());
589 if (scriptElement)
590 m_scriptStartLine = lineNumber();
591
592 if (!m_currentNode->addChild(newElement.get())) {
593 stopParsing();
594 return;
595 }
596
597 setCurrentNode(newElement.get());
598 if (m_view && !newElement->attached())
599 newElement->attach();
600
601 if (isFirstElement && m_doc->frame())
602 m_doc->frame()->loader()->dispatchDocumentElementAvailable();
603 }
604
parseEndElement()605 void XMLTokenizer::parseEndElement()
606 {
607 exitText();
608
609 Node* n = m_currentNode;
610 RefPtr<Node> parent = n->parentNode();
611 n->finishParsingChildren();
612
613 if (!n->isElementNode() || !m_view) {
614 setCurrentNode(parent.get());
615 return;
616 }
617
618 Element* element = static_cast<Element*>(n);
619 ScriptElement* scriptElement = toScriptElement(element);
620 if (!scriptElement) {
621 setCurrentNode(parent.get());
622 return;
623 }
624
625 // don't load external scripts for standalone documents (for now)
626 ASSERT(!m_pendingScript);
627 m_requestingScript = true;
628
629 #if ENABLE(XHTMLMP)
630 if (!scriptElement->shouldExecuteAsJavaScript())
631 m_doc->setShouldProcessNoscriptElement(true);
632 else
633 #endif
634 {
635 String scriptHref = scriptElement->sourceAttributeValue();
636 if (!scriptHref.isEmpty()) {
637 // we have a src attribute
638 String scriptCharset = scriptElement->scriptCharset();
639 if ((m_pendingScript = m_doc->docLoader()->requestScript(scriptHref, scriptCharset))) {
640 m_scriptElement = element;
641 m_pendingScript->addClient(this);
642
643 // m_pendingScript will be 0 if script was already loaded and ref() executed it
644 if (m_pendingScript)
645 pauseParsing();
646 } else
647 m_scriptElement = 0;
648 } else
649 m_view->frame()->loader()->executeScript(ScriptSourceCode(scriptElement->scriptContent(), m_doc->url(), m_scriptStartLine));
650 }
651 m_requestingScript = false;
652 setCurrentNode(parent.get());
653 }
654
parseCharacters()655 void XMLTokenizer::parseCharacters()
656 {
657 if (m_currentNode->isTextNode() || enterText()) {
658 ExceptionCode ec = 0;
659 static_cast<Text*>(m_currentNode)->appendData(m_stream.text(), ec);
660 }
661 }
662
parseProcessingInstruction()663 void XMLTokenizer::parseProcessingInstruction()
664 {
665 exitText();
666
667 // ### handle exceptions
668 int exception = 0;
669 RefPtr<ProcessingInstruction> pi = m_doc->createProcessingInstruction(
670 m_stream.processingInstructionTarget(),
671 m_stream.processingInstructionData(), exception);
672 if (exception)
673 return;
674
675 pi->setCreatedByParser(true);
676
677 if (!m_currentNode->addChild(pi.get()))
678 return;
679 if (m_view && !pi->attached())
680 pi->attach();
681
682 pi->finishParsingChildren();
683
684 #if ENABLE(XSLT)
685 m_sawXSLTransform = !m_sawFirstElement && pi->isXSL();
686 if (m_sawXSLTransform && !m_doc->transformSourceDocument()))
687 stopParsing();
688 #endif
689 }
690
parseCdata()691 void XMLTokenizer::parseCdata()
692 {
693 exitText();
694
695 RefPtr<Node> newNode = new CDATASection(m_doc, m_stream.text());
696 if (!m_currentNode->addChild(newNode.get()))
697 return;
698 if (m_view && !newNode->attached())
699 newNode->attach();
700 }
701
parseComment()702 void XMLTokenizer::parseComment()
703 {
704 exitText();
705
706 RefPtr<Node> newNode = new Comment(m_doc, m_stream.text());
707 m_currentNode->addChild(newNode.get());
708 if (m_view && !newNode->attached())
709 newNode->attach();
710 }
711
endDocument()712 void XMLTokenizer::endDocument()
713 {
714 #if ENABLE(XHTMLMP)
715 m_hasDocTypeDeclaration = false;
716 #endif
717 }
718
hasError() const719 bool XMLTokenizer::hasError() const
720 {
721 return m_stream.hasError();
722 }
723
724 #if QT_VERSION < 0x040400
parseId(const QString & dtd,int * pos,bool * ok)725 static QString parseId(const QString &dtd, int *pos, bool *ok)
726 {
727 *ok = true;
728 int start = *pos + 1;
729 int end = start;
730 if (dtd.at(*pos) == QLatin1Char('\''))
731 while (start < dtd.length() && dtd.at(end) != QLatin1Char('\''))
732 ++end;
733 else if (dtd.at(*pos) == QLatin1Char('\"'))
734 while (start < dtd.length() && dtd.at(end) != QLatin1Char('\"'))
735 ++end;
736 else {
737 *ok = false;
738 return QString();
739 }
740 *pos = end + 1;
741 return dtd.mid(start, end - start);
742 }
743 #endif
744
parseDtd()745 void XMLTokenizer::parseDtd()
746 {
747 #if QT_VERSION >= 0x040400
748 QStringRef name = m_stream.dtdName();
749 QStringRef publicId = m_stream.dtdPublicId();
750 QStringRef systemId = m_stream.dtdSystemId();
751 #else
752 QString dtd = m_stream.text().toString();
753
754 int start = dtd.indexOf("<!DOCTYPE ") + 10;
755 while (start < dtd.length() && dtd.at(start).isSpace())
756 ++start;
757 int end = start;
758 while (start < dtd.length() && !dtd.at(end).isSpace())
759 ++end;
760 QString name = dtd.mid(start, end - start);
761
762 start = end;
763 while (start < dtd.length() && dtd.at(start).isSpace())
764 ++start;
765 end = start;
766 while (start < dtd.length() && !dtd.at(end).isSpace())
767 ++end;
768 QString id = dtd.mid(start, end - start);
769 start = end;
770 while (start < dtd.length() && dtd.at(start).isSpace())
771 ++start;
772 QString publicId;
773 QString systemId;
774 if (id == QLatin1String("PUBLIC")) {
775 bool ok;
776 publicId = parseId(dtd, &start, &ok);
777 if (!ok) {
778 handleError(fatal, "Invalid DOCTYPE", lineNumber(), columnNumber());
779 return;
780 }
781 while (start < dtd.length() && dtd.at(start).isSpace())
782 ++start;
783 systemId = parseId(dtd, &start, &ok);
784 if (!ok) {
785 handleError(fatal, "Invalid DOCTYPE", lineNumber(), columnNumber());
786 return;
787 }
788 } else if (id == QLatin1String("SYSTEM")) {
789 bool ok;
790 systemId = parseId(dtd, &start, &ok);
791 if (!ok) {
792 handleError(fatal, "Invalid DOCTYPE", lineNumber(), columnNumber());
793 return;
794 }
795 } else if (id == QLatin1String("[") || id == QLatin1String(">")) {
796 } else {
797 handleError(fatal, "Invalid DOCTYPE", lineNumber(), columnNumber());
798 return;
799 }
800 #endif
801
802 //qDebug() << dtd << name << publicId << systemId;
803 if ((publicId == QLatin1String("-//W3C//DTD XHTML 1.0 Transitional//EN"))
804 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.1//EN"))
805 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.0 Strict//EN"))
806 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.0 Frameset//EN"))
807 || (publicId == QLatin1String("-//W3C//DTD XHTML Basic 1.0//EN"))
808 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"))
809 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"))
810 #if !ENABLE(XHTMLMP)
811 || (publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.0//EN"))
812 #endif
813 )
814 setIsXHTMLDocument(true); // controls if we replace entities or not.
815 #if ENABLE(XHTMLMP)
816 else if ((publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.1//EN"))
817 || (publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.0//EN"))) {
818 if (AtomicString(name) != HTMLNames::htmlTag.localName()) {
819 handleError(fatal, "Invalid DOCTYPE declaration, expected 'html' as root element.", lineNumber(), columnNumber());
820 return;
821 }
822
823 if (m_doc->isXHTMLMPDocument()) // check if the MIME type is correct with this method
824 setIsXHTMLMPDocument(true);
825 else
826 setIsXHTMLDocument(true);
827 }
828 #endif
829 #if ENABLE(WML)
830 else if (m_doc->isWMLDocument()
831 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.3//EN")
832 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.2//EN")
833 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.1//EN")
834 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.0//EN"))
835 handleError(fatal, "Invalid DTD Public ID", lineNumber(), columnNumber());
836 #endif
837 if (!m_parsingFragment)
838 m_doc->addChild(DocumentType::create(m_doc, name, publicId, systemId));
839
840 }
841 }
842
843