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/)
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.
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.
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.
27 #include "XMLDocumentParser.h"
29 #include "CDATASection.h"
30 #include "CachedScript.h"
32 #include "CachedResourceLoader.h"
34 #include "DocumentFragment.h"
35 #include "DocumentType.h"
37 #include "FrameLoader.h"
38 #include "FrameView.h"
39 #include "HTMLEntityParser.h"
40 #include "HTMLHtmlElement.h"
41 #include "HTMLLinkElement.h"
42 #include "HTMLNames.h"
43 #include "HTMLStyleElement.h"
44 #include "ProcessingInstruction.h"
45 #include "ResourceError.h"
46 #include "ResourceHandle.h"
47 #include "ResourceRequest.h"
48 #include "ResourceResponse.h"
49 #include "ScriptableDocumentParser.h"
50 #include "ScriptController.h"
51 #include "ScriptElement.h"
52 #include "ScriptSourceCode.h"
53 #include "ScriptValue.h"
54 #include "TextResourceDecoder.h"
55 #include "TransformSource.h"
57 #include <wtf/StringExtras.h>
58 #include <wtf/Threading.h>
59 #include <wtf/Vector.h>
60 #include <wtf/text/CString.h>
63 #include "HTMLNames.h"
64 #include "HTMLScriptElement.h"
71 class EntityResolver : public QXmlStreamEntityResolver {
72 virtual QString resolveUndeclaredEntity(const QString &name);
75 QString EntityResolver::resolveUndeclaredEntity(const QString &name)
77 UChar c = decodeNamedEntity(name.toUtf8().constData());
81 // --------------------------------
83 bool XMLDocumentParser::supportsXMLVersion(const String& version)
85 return version == "1.0";
88 XMLDocumentParser::XMLDocumentParser(Document* document, FrameView* frameView)
89 : ScriptableDocumentParser(document)
92 , m_currentNode(document)
95 , m_sawXSLTransform(false)
96 , m_sawFirstElement(false)
97 , m_isXHTMLDocument(false)
99 , m_isXHTMLMPDocument(false)
100 , m_hasDocTypeDeclaration(false)
102 , m_parserPaused(false)
103 , m_requestingScript(false)
104 , m_finishCalled(false)
106 , m_lastErrorPosition(TextPosition1::belowRangePosition())
108 , m_scriptStartPosition(TextPosition1::belowRangePosition())
109 , m_parsingFragment(false)
110 , m_scriptingPermission(FragmentScriptingAllowed)
112 m_stream.setEntityResolver(new EntityResolver);
115 XMLDocumentParser::XMLDocumentParser(DocumentFragment* fragment, Element* parentElement, FragmentScriptingPermission permission)
116 : ScriptableDocumentParser(fragment->document())
119 , m_currentNode(fragment)
122 , m_sawXSLTransform(false)
123 , m_sawFirstElement(false)
124 , m_isXHTMLDocument(false)
126 , m_isXHTMLMPDocument(false)
127 , m_hasDocTypeDeclaration(false)
129 , m_parserPaused(false)
130 , m_requestingScript(false)
131 , m_finishCalled(false)
133 , m_lastErrorPosition(TextPosition1::belowRangePosition())
135 , m_scriptStartPosition(TextPosition1::belowRangePosition())
136 , m_parsingFragment(true)
137 , m_scriptingPermission(permission)
141 // Add namespaces based on the parent node
142 Vector<Element*> elemStack;
143 while (parentElement) {
144 elemStack.append(parentElement);
146 Node* n = parentElement->parentNode();
147 if (!n || !n->isElementNode())
149 parentElement = static_cast<Element*>(n);
152 if (elemStack.isEmpty())
155 QXmlStreamNamespaceDeclarations namespaces;
156 for (Element* element = elemStack.last(); !elemStack.isEmpty(); elemStack.removeLast()) {
157 if (NamedNodeMap* 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()));
167 m_stream.addExtraNamespaceDeclarations(namespaces);
168 m_stream.setEntityResolver(new EntityResolver);
170 // If the parent element is not in document tree, there may be no xmlns attribute; just default to the parent's namespace.
171 if (m_defaultNamespaceURI.isNull() && !parentElement->inDocument())
172 m_defaultNamespaceURI = parentElement->namespaceURI();
175 XMLDocumentParser::~XMLDocumentParser()
177 clearCurrentNodeStack();
179 m_pendingScript->removeClient(this);
180 delete m_stream.entityResolver();
183 void XMLDocumentParser::doWrite(const String& parseString)
187 if (document()->decoder() && document()->decoder()->sawError()) {
188 // If the decoder saw an error, report it as fatal (stops parsing)
189 handleError(fatal, "Encoding error", lineNumber(), columnNumber());
193 QString data(parseString);
194 if (!data.isEmpty()) {
195 // JavaScript may cause the parser to detach,
196 // keep this alive until this function is done.
197 RefPtr<XMLDocumentParser> protect(this);
199 m_stream.addData(data);
206 void XMLDocumentParser::initializeParserContext(const char*)
208 DocumentParser::startParsing();
211 m_sawXSLTransform = false;
212 m_sawFirstElement = false;
215 void XMLDocumentParser::doEnd()
218 if (m_sawXSLTransform) {
219 document()->setTransformSource(new TransformSource(m_originalSourceForTransform));
220 document()->setParsing(false); // Make the doc think it's done, so it will apply xsl sheets.
221 document()->styleSelectorChanged(RecalcStyleImmediately);
222 document()->setParsing(true);
223 DocumentParser::stopParsing();
227 if (m_stream.error() == QXmlStreamReader::PrematureEndOfDocumentError
228 || (m_wroteText && !m_sawFirstElement && !m_sawXSLTransform && !m_sawError))
229 handleError(fatal, qPrintable(m_stream.errorString()), lineNumber(), columnNumber());
232 int XMLDocumentParser::lineNumber() const
234 return m_stream.lineNumber();
237 int XMLDocumentParser::columnNumber() const
239 return m_stream.columnNumber();
242 TextPosition0 XMLDocumentParser::textPosition() const
244 return TextPosition0(WTF::ZeroBasedNumber::fromZeroBasedInt(lineNumber()), WTF::ZeroBasedNumber::fromZeroBasedInt(columnNumber()));
247 // This method incorrectly reinterprets zero-base lineNumber method as one-based number.
248 // FIXME: This error is kept for compatibility. We should fix it eventually.
249 TextPosition1 XMLDocumentParser::textPositionOneBased() const
251 return TextPosition1(WTF::OneBasedNumber::fromOneBasedInt(lineNumber()), WTF::OneBasedNumber::fromOneBasedInt(columnNumber()));
254 void XMLDocumentParser::stopParsing()
256 ScriptableDocumentParser::stopParsing();
259 void XMLDocumentParser::resumeParsing()
261 ASSERT(m_parserPaused);
263 m_parserPaused = false;
265 // First, execute any pending callbacks
270 // Then, write any pending data
271 SegmentedString rest = m_pendingSrc;
272 m_pendingSrc.clear();
275 // Finally, if finish() has been called and append() didn't result
276 // in any further callbacks being queued, call end()
277 if (m_finishCalled && !m_parserPaused && !m_pendingScript)
281 bool XMLDocumentParser::appendFragmentSource(const String& source)
283 ASSERT(!m_sawFirstElement);
284 append(String("<qxmlstreamdummyelement>"));
286 append(String("</qxmlstreamdummyelement>"));
290 // --------------------------------
292 struct AttributeParseState {
293 HashMap<String, String> attributes;
297 static void attributesStartElementNsHandler(AttributeParseState* state, const QXmlStreamAttributes& attrs)
299 if (attrs.count() <= 0)
302 state->gotAttributes = true;
304 for (int i = 0; i < attrs.count(); i++) {
305 const QXmlStreamAttribute& attr = attrs[i];
306 String attrLocalName = attr.name();
307 String attrValue = attr.value();
308 String attrURI = attr.namespaceUri();
309 String attrQName = attr.qualifiedName();
310 state->attributes.set(attrQName, attrValue);
314 HashMap<String, String> parseAttributes(const String& string, bool& attrsOK)
316 AttributeParseState state;
317 state.gotAttributes = false;
319 QXmlStreamReader stream;
320 QString dummy = QString(QLatin1String("<?xml version=\"1.0\"?><attrs %1 />")).arg(string);
321 stream.addData(dummy);
322 while (!stream.atEnd()) {
324 if (stream.isStartElement()) {
325 attributesStartElementNsHandler(&state, stream.attributes());
328 attrsOK = state.gotAttributes;
329 return state.attributes;
332 static inline String prefixFromQName(const QString& qName)
334 const int offset = qName.indexOf(QLatin1Char(':'));
338 return qName.left(offset);
341 static inline void handleElementNamespaces(Element* newElement, const QXmlStreamNamespaceDeclarations &ns,
342 ExceptionCode& ec, FragmentScriptingPermission scriptingPermission)
344 for (int i = 0; i < ns.count(); ++i) {
345 const QXmlStreamNamespaceDeclaration &decl = ns[i];
346 String namespaceURI = decl.namespaceUri();
347 String namespaceQName = decl.prefix().isEmpty() ? String("xmlns") : String("xmlns:");
348 namespaceQName.append(decl.prefix());
349 newElement->setAttributeNS("http://www.w3.org/2000/xmlns/", namespaceQName, namespaceURI, ec, scriptingPermission);
350 if (ec) // exception setting attributes
355 static inline void handleElementAttributes(Element* newElement, const QXmlStreamAttributes &attrs, ExceptionCode& ec,
356 FragmentScriptingPermission scriptingPermission)
358 for (int i = 0; i < attrs.count(); ++i) {
359 const QXmlStreamAttribute &attr = attrs[i];
360 String attrLocalName = attr.name();
361 String attrValue = attr.value();
362 String attrURI = attr.namespaceUri().isEmpty() ? String() : String(attr.namespaceUri());
363 String attrQName = attr.qualifiedName();
364 newElement->setAttributeNS(attrURI, attrQName, attrValue, ec, scriptingPermission);
365 if (ec) // exception setting attributes
370 void XMLDocumentParser::parse()
372 while (!isStopped() && !m_parserPaused && !m_stream.atEnd()) {
374 switch (m_stream.tokenType()) {
375 case QXmlStreamReader::StartDocument: {
379 case QXmlStreamReader::EndDocument: {
383 case QXmlStreamReader::StartElement: {
385 if (document()->isXHTMLMPDocument() && !m_hasDocTypeDeclaration) {
386 handleError(fatal, "DOCTYPE declaration lost.", lineNumber(), columnNumber());
393 case QXmlStreamReader::EndElement: {
397 case QXmlStreamReader::Characters: {
398 if (m_stream.isCDATA()) {
407 case QXmlStreamReader::Comment: {
411 case QXmlStreamReader::DTD: {
412 //qDebug()<<"------------- DTD";
415 m_hasDocTypeDeclaration = true;
419 case QXmlStreamReader::EntityReference: {
420 //qDebug()<<"---------- ENTITY = "<<m_stream.name().toString()
421 // <<", t = "<<m_stream.text().toString();
422 if (isXHTMLDocument()
424 || isXHTMLMPDocument()
430 QString entity = m_stream.name().toString();
431 UChar c = decodeNamedEntity(entity.toUtf8().constData());
432 if (!m_currentNode->isTextNode())
434 ExceptionCode ec = 0;
436 // qDebug()<<" ------- adding entity "<<str;
437 static_cast<Text*>(m_currentNode)->appendData(str, ec);
441 case QXmlStreamReader::ProcessingInstruction: {
442 parseProcessingInstruction();
446 if (m_stream.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
447 ErrorType type = (m_stream.error() == QXmlStreamReader::NotWellFormedError) ?
449 handleError(type, qPrintable(m_stream.errorString()), lineNumber(),
458 void XMLDocumentParser::startDocument()
460 initializeParserContext();
461 ExceptionCode ec = 0;
463 if (!m_parsingFragment) {
464 document()->setXMLStandalone(m_stream.isStandaloneDocument(), ec);
466 QStringRef version = m_stream.documentVersion();
467 if (!version.isEmpty())
468 document()->setXMLVersion(version, ec);
469 QStringRef encoding = m_stream.documentEncoding();
470 if (!encoding.isEmpty())
471 document()->setXMLEncoding(encoding);
475 void XMLDocumentParser::parseStartElement()
477 if (!m_sawFirstElement && m_parsingFragment) {
478 // skip dummy element for fragments
479 m_sawFirstElement = true;
485 String localName = m_stream.name();
486 String uri = m_stream.namespaceUri();
487 String prefix = prefixFromQName(m_stream.qualifiedName().toString());
489 if (m_parsingFragment && uri.isNull()) {
490 Q_ASSERT(prefix.isNull());
491 uri = m_defaultNamespaceURI;
494 QualifiedName qName(prefix, localName, uri);
495 RefPtr<Element> newElement = document()->createElement(qName, true);
502 if (!m_sawFirstElement && isXHTMLMPDocument()) {
503 // As per 7.1 section of OMA-WAP-XHTMLMP-V1_1-20061020-A.pdf,
504 // we should make sure that the root element MUST be 'html' and
505 // ensure the name of the default namespace on the root elment 'html'
506 // MUST be 'http://www.w3.org/1999/xhtml'
507 if (localName != HTMLNames::htmlTag.localName()) {
508 handleError(fatal, "XHTMLMP document expects 'html' as root element.", lineNumber(), columnNumber());
513 m_defaultNamespaceURI = HTMLNames::xhtmlNamespaceURI;
514 uri = m_defaultNamespaceURI;
515 m_stream.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration(prefix, HTMLNames::xhtmlNamespaceURI));
520 bool isFirstElement = !m_sawFirstElement;
521 m_sawFirstElement = true;
523 ExceptionCode ec = 0;
524 handleElementNamespaces(newElement.get(), m_stream.namespaceDeclarations(), ec, m_scriptingPermission);
530 handleElementAttributes(newElement.get(), m_stream.attributes(), ec, m_scriptingPermission);
536 ScriptElement* scriptElement = toScriptElement(newElement.get());
538 m_scriptStartPosition = textPositionOneBased();
540 m_currentNode->deprecatedParserAddChild(newElement.get());
542 pushCurrentNode(newElement.get());
543 if (m_view && !newElement->attached())
544 newElement->attach();
546 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
547 if (newElement->hasTagName(HTMLNames::htmlTag))
548 static_cast<HTMLHtmlElement*>(newElement.get())->insertedByParser();
551 if (isFirstElement && document()->frame())
552 document()->frame()->loader()->dispatchDocumentElementAvailable();
555 void XMLDocumentParser::parseEndElement()
559 RefPtr<Node> n = m_currentNode;
560 n->finishParsingChildren();
562 if (m_scriptingPermission == FragmentScriptingNotAllowed && n->isElementNode() && toScriptElement(static_cast<Element*>(n.get()))) {
569 if (!n->isElementNode() || !m_view) {
570 if (!m_currentNodeStack.isEmpty())
575 Element* element = static_cast<Element*>(n.get());
577 // The element's parent may have already been removed from document.
578 // Parsing continues in this case, but scripts aren't executed.
579 if (!element->inDocument()) {
584 ScriptElement* scriptElement = toScriptElement(element);
585 if (!scriptElement) {
590 // don't load external scripts for standalone documents (for now)
591 ASSERT(!m_pendingScript);
592 m_requestingScript = true;
594 bool successfullyPrepared = scriptElement->prepareScript(m_scriptStartPosition, ScriptElement::AllowLegacyTypeInTypeAttribute);
595 if (!successfullyPrepared) {
597 if (!scriptElement->isScriptTypeSupported(ScriptElement::AllowLegacyTypeInTypeAttribute))
598 document()->setShouldProcessNoscriptElement(true);
601 if (scriptElement->readyToBeParserExecuted())
602 scriptElement->executeScript(ScriptSourceCode(scriptElement->scriptContent(), document()->url(), m_scriptStartPosition));
603 else if (scriptElement->willBeParserExecuted()) {
604 m_pendingScript = scriptElement->cachedScript();
605 m_scriptElement = element;
606 m_pendingScript->addClient(this);
608 // m_pendingScript will be 0 if script was already loaded and addClient() executed it.
614 m_requestingScript = false;
618 void XMLDocumentParser::parseCharacters()
620 if (!m_currentNode->isTextNode())
622 ExceptionCode ec = 0;
623 static_cast<Text*>(m_currentNode)->appendData(m_stream.text(), ec);
626 void XMLDocumentParser::parseProcessingInstruction()
630 // ### handle exceptions
632 RefPtr<ProcessingInstruction> pi = document()->createProcessingInstruction(
633 m_stream.processingInstructionTarget(),
634 m_stream.processingInstructionData(), exception);
638 pi->setCreatedByParser(true);
640 m_currentNode->deprecatedParserAddChild(pi.get());
641 if (m_view && !pi->attached())
644 pi->finishParsingChildren();
649 m_sawXSLTransform = !m_sawFirstElement && pi->isXSL();
650 if (m_sawXSLTransform && !document()->transformSourceDocument())
655 void XMLDocumentParser::parseCdata()
659 RefPtr<Node> newNode = CDATASection::create(document(), m_stream.text());
661 m_currentNode->deprecatedParserAddChild(newNode.get());
662 if (m_view && !newNode->attached())
666 void XMLDocumentParser::parseComment()
670 RefPtr<Node> newNode = Comment::create(document(), m_stream.text());
672 m_currentNode->deprecatedParserAddChild(newNode.get());
673 if (m_view && !newNode->attached())
677 void XMLDocumentParser::endDocument()
680 m_hasDocTypeDeclaration = false;
684 bool XMLDocumentParser::hasError() const
686 return m_stream.hasError();
689 void XMLDocumentParser::parseDtd()
691 QStringRef name = m_stream.dtdName();
692 QStringRef publicId = m_stream.dtdPublicId();
693 QStringRef systemId = m_stream.dtdSystemId();
695 //qDebug() << dtd << name << publicId << systemId;
696 if ((publicId == QLatin1String("-//W3C//DTD XHTML 1.0 Transitional//EN"))
697 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.1//EN"))
698 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.0 Strict//EN"))
699 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.0 Frameset//EN"))
700 || (publicId == QLatin1String("-//W3C//DTD XHTML Basic 1.0//EN"))
701 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"))
702 || (publicId == QLatin1String("-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"))
704 || (publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.0//EN"))
707 setIsXHTMLDocument(true); // controls if we replace entities or not.
709 else if ((publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.1//EN"))
710 || (publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.0//EN"))) {
711 if (AtomicString(name) != HTMLNames::htmlTag.localName()) {
712 handleError(fatal, "Invalid DOCTYPE declaration, expected 'html' as root element.", lineNumber(), columnNumber());
716 if (document()->isXHTMLMPDocument()) // check if the MIME type is correct with this method
717 setIsXHTMLMPDocument(true);
719 setIsXHTMLDocument(true);
723 else if (document()->isWMLDocument()
724 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.3//EN")
725 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.2//EN")
726 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.1//EN")
727 && publicId != QLatin1String("-//WAPFORUM//DTD WML 1.0//EN"))
728 handleError(fatal, "Invalid DTD Public ID", lineNumber(), columnNumber());
730 if (!m_parsingFragment)
731 document()->parserAddChild(DocumentType::create(document(), name, publicId, systemId));