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