2 * Copyright (C) 2000 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
5 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
6 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef XMLDocumentParser_h
26 #define XMLDocumentParser_h
28 #include "core/dom/ParserContentPolicy.h"
29 #include "core/dom/ScriptableDocumentParser.h"
30 #include "core/fetch/ResourceClient.h"
31 #include "core/fetch/ResourcePtr.h"
32 #include "core/fetch/ScriptResource.h"
33 #include "core/xml/parser/XMLErrors.h"
34 #include "platform/heap/Handle.h"
35 #include "platform/text/SegmentedString.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/OwnPtr.h"
38 #include "wtf/text/CString.h"
39 #include "wtf/text/StringHash.h"
40 #include <libxml/tree.h>
45 class ResourceFetcher
;
46 class DocumentFragment
;
52 class XMLParserContext
: public RefCounted
<XMLParserContext
> {
54 static PassRefPtr
<XMLParserContext
> createMemoryParser(xmlSAXHandlerPtr
, void* userData
, const CString
& chunk
);
55 static PassRefPtr
<XMLParserContext
> createStringParser(xmlSAXHandlerPtr
, void* userData
);
57 xmlParserCtxtPtr
context() const { return m_context
; }
60 XMLParserContext(xmlParserCtxtPtr context
)
65 xmlParserCtxtPtr m_context
;
68 class XMLDocumentParser final
: public ScriptableDocumentParser
, public ScriptResourceClient
{
69 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED(XMLDocumentParser
);
71 static PassRefPtrWillBeRawPtr
<XMLDocumentParser
> create(Document
& document
, FrameView
* view
)
73 return adoptRefWillBeNoop(new XMLDocumentParser(document
, view
));
75 static PassRefPtrWillBeRawPtr
<XMLDocumentParser
> create(DocumentFragment
* fragment
, Element
* element
, ParserContentPolicy parserContentPolicy
)
77 return adoptRefWillBeNoop(new XMLDocumentParser(fragment
, element
, parserContentPolicy
));
79 ~XMLDocumentParser() override
;
80 DECLARE_VIRTUAL_TRACE();
82 // Exposed for callbacks:
83 void handleError(XMLErrors::ErrorType
, const char* message
, TextPosition
);
85 void setIsXHTMLDocument(bool isXHTML
) { m_isXHTMLDocument
= isXHTML
; }
86 bool isXHTMLDocument() const { return m_isXHTMLDocument
; }
88 bool isCurrentlyParsing8BitChunk() { return m_isCurrentlyParsing8BitChunk
; }
90 static bool parseDocumentFragment(const String
&, DocumentFragment
*, Element
* parent
= 0, ParserContentPolicy
= AllowScriptingContent
);
92 // Used by the XMLHttpRequest to check if the responseXML was well formed.
93 bool wellFormed() const override
{ return !m_sawError
; }
95 TextPosition
textPosition() const override
;
97 static bool supportsXMLVersion(const String
&);
99 class PendingCallback
{
101 virtual ~PendingCallback() { }
102 virtual void call(XMLDocumentParser
*) = 0;
106 explicit XMLDocumentParser(Document
&, FrameView
* = 0);
107 XMLDocumentParser(DocumentFragment
*, Element
*, ParserContentPolicy
);
109 // From DocumentParser
110 void insert(const SegmentedString
&) override
;
111 void append(const String
&) override
;
112 void finish() override
;
113 bool isWaitingForScripts() const override
;
114 void stopParsing() override
;
115 void detach() override
;
116 OrdinalNumber
lineNumber() const override
;
117 OrdinalNumber
columnNumber() const;
119 // from ResourceClient
120 void notifyFinished(Resource
*) override
;
125 void resumeParsing();
127 bool appendFragmentSource(const String
&);
130 // Callbacks from parser SAX
131 void error(XMLErrors::ErrorType
, const char* message
, va_list args
) WTF_ATTRIBUTE_PRINTF(3, 0);
132 void startElementNs(const AtomicString
& localName
, const AtomicString
& prefix
, const AtomicString
& uri
, int namespaceCount
,
133 const xmlChar
** namespaces
, int attributeCount
, int defaultedCount
, const xmlChar
** libxmlAttributes
);
135 void characters(const xmlChar
* chars
, int length
);
136 void processingInstruction(const String
& target
, const String
& data
);
137 void cdataBlock(const String
&);
138 void comment(const String
&);
139 void startDocument(const String
& version
, const String
& encoding
, int standalone
);
140 void internalSubset(const String
& name
, const String
& externalID
, const String
& systemID
);
144 void initializeParserContext(const CString
& chunk
= CString());
146 void pushCurrentNode(ContainerNode
*);
147 void popCurrentNode();
148 void clearCurrentNodeStack();
150 void insertErrorMessageBlock();
152 void createLeafTextNodeIfNeeded();
153 bool updateLeafTextNode();
155 void doWrite(const String
&);
160 SegmentedString m_originalSourceForTransform
;
162 xmlParserCtxtPtr
context() const { return m_context
? m_context
->context() : 0; }
163 RefPtr
<XMLParserContext
> m_context
;
164 Deque
<OwnPtr
<PendingCallback
>> m_pendingCallbacks
;
165 Vector
<xmlChar
> m_bufferedText
;
167 RawPtrWillBeMember
<ContainerNode
> m_currentNode
;
168 WillBeHeapVector
<RawPtrWillBeMember
<ContainerNode
>> m_currentNodeStack
;
170 RefPtrWillBeMember
<Text
> m_leafTextNode
;
172 bool m_isCurrentlyParsing8BitChunk
;
175 bool m_sawXSLTransform
;
176 bool m_sawFirstElement
;
177 bool m_isXHTMLDocument
;
179 bool m_requestingScript
;
182 XMLErrors m_xmlErrors
;
184 ResourcePtr
<ScriptResource
> m_pendingScript
;
185 RefPtrWillBeMember
<Element
> m_scriptElement
;
186 TextPosition m_scriptStartPosition
;
188 bool m_parsingFragment
;
189 AtomicString m_defaultNamespaceURI
;
191 typedef HashMap
<AtomicString
, AtomicString
> PrefixForNamespaceMap
;
192 PrefixForNamespaceMap m_prefixToNamespaceMap
;
193 SegmentedString m_pendingSrc
;
196 xmlDocPtr
xmlDocPtrForString(Document
*, const String
& source
, const String
& url
);
197 HashMap
<String
, String
> parseAttributes(const String
&, bool& attrsOK
);
201 #endif // XMLDocumentParser_h