2 Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 (C) 1997 Torben Weis (weis@kde.org)
4 (C) 1998 Waldo Bastian (bastian@kde.org)
5 (C) 2001 Dirk Mueller (mueller@kde.org)
6 Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
24 #ifndef HTMLTokenizer_h
25 #define HTMLTokenizer_h
27 #include "DeprecatedPtrQueue.h"
28 #include "NamedMappedAttrMap.h"
29 #include "SegmentedString.h"
31 #include "Tokenizer.h"
32 #include "CachedResourceClient.h"
33 #include <wtf/Vector.h>
34 #include <wtf/OwnPtr.h>
39 class DocumentFragment;
42 class HTMLViewSourceDocument;
49 * represents one HTML tag. Consists of a numerical id, and the list
50 * of attributes. Can also represent text. In this case the id = 0 and
51 * text contains the text.
55 Token() : beginTag(true), flat(false), brokenXMLStyle(false), m_sourceInfo(0) { }
58 void addAttribute(Document*, AtomicString& attrName, const AtomicString& v, bool viewSourceMode);
60 bool isOpenTag(const QualifiedName& fullName) const { return beginTag && fullName.localName() == tagName; }
61 bool isCloseTag(const QualifiedName& fullName) const { return !beginTag && fullName.localName() == tagName; }
70 brokenXMLStyle = false;
72 m_sourceInfo->clear();
75 void addViewSourceChar(UChar c) { if (!m_sourceInfo.get()) m_sourceInfo.set(new Vector<UChar>); m_sourceInfo->append(c); }
77 RefPtr<NamedMappedAttrMap> attrs;
78 RefPtr<StringImpl> text;
83 OwnPtr<Vector<UChar> > m_sourceInfo;
86 //-----------------------------------------------------------------------------
88 class HTMLTokenizer : public Tokenizer, public CachedResourceClient {
90 HTMLTokenizer(HTMLDocument*, bool reportErrors);
91 HTMLTokenizer(HTMLViewSourceDocument*);
92 HTMLTokenizer(DocumentFragment*);
93 virtual ~HTMLTokenizer();
95 virtual bool write(const SegmentedString&, bool appendData);
96 virtual void finish();
97 virtual void setForceSynchronous(bool force);
98 virtual bool isWaitingForScripts() const;
99 virtual void stopParsing();
100 virtual bool processingData() const;
101 virtual int executingScript() const { return m_executingScript; }
103 virtual int lineNumber() const { return lineno; }
104 virtual int columnNumber() const { return 1; }
106 int* lineNumberPtr() { return &lineno; }
108 bool processingContentWrittenByScript() const { return src.excludeLineNumbers(); }
110 virtual void executeScriptsWaitingForStylesheets();
115 // Where we are in parsing a tag
120 PassRefPtr<Node> processToken();
122 State processListing(SegmentedString, State);
123 State parseComment(SegmentedString&, State);
124 State parseServer(SegmentedString&, State);
125 State parseText(SegmentedString&, State);
126 State parseSpecial(SegmentedString&, State);
127 State parseTag(SegmentedString&, State);
128 State parseEntity(SegmentedString&, UChar*& dest, State, unsigned& _cBufferPos, bool start, bool parsingTag);
129 State parseProcessingInstruction(SegmentedString&, State);
130 State scriptHandler(State);
131 State scriptExecution(const DeprecatedString& script, State, DeprecatedString scriptURL, int baseLine = 0);
132 void setSrc(const SegmentedString&);
134 // check if we have enough space in the buffer.
136 inline void checkBuffer(int len = 10)
138 if ((dest - buffer) > size - len)
142 inline void checkScriptBuffer(int len = 10)
144 if (scriptCodeSize + len >= scriptCodeMaxSize)
145 enlargeScriptBuffer(len);
148 void enlargeBuffer(int len);
149 void enlargeScriptBuffer(int len);
151 bool continueProcessing(int& processedCount, double startTime, State&);
152 void timerFired(Timer<HTMLTokenizer>*);
153 void allDataProcessed();
155 // from CachedResourceClient
156 void notifyFinished(CachedResource *finishedObj);
165 // the size of buffer
170 // are we in quotes within a html tag
171 enum { NoQuote, SingleQuote, DoubleQuote } tquote;
173 // Are we in a &... character entity description?
183 unsigned EntityUnicodeValue;
199 State() : m_bits(0) { }
201 TagState tagState() const { return static_cast<TagState>(m_bits & TagMask); }
202 void setTagState(TagState t) { m_bits = (m_bits & ~TagMask) | t; }
203 EntityState entityState() const { return static_cast<EntityState>((m_bits & EntityMask) >> EntityShift); }
204 void setEntityState(EntityState e) { m_bits = (m_bits & ~EntityMask) | (e << EntityShift); }
206 bool inScript() const { return testBit(InScript); }
207 void setInScript(bool v) { setBit(InScript, v); }
208 bool inStyle() const { return testBit(InStyle); }
209 void setInStyle(bool v) { setBit(InStyle, v); }
210 bool inXmp() const { return testBit(InXmp); }
211 void setInXmp(bool v) { setBit(InXmp, v); }
212 bool inTitle() const { return testBit(InTitle); }
213 void setInTitle(bool v) { setBit(InTitle, v); }
214 bool inPlainText() const { return testBit(InPlainText); }
215 void setInPlainText(bool v) { setBit(InPlainText, v); }
216 bool inProcessingInstruction() const { return testBit(InProcessingInstruction); }
217 void setInProcessingInstruction(bool v) { return setBit(InProcessingInstruction, v); }
218 bool inComment() const { return testBit(InComment); }
219 void setInComment(bool v) { setBit(InComment, v); }
220 bool inTextArea() const { return testBit(InTextArea); }
221 void setInTextArea(bool v) { setBit(InTextArea, v); }
222 bool escaped() const { return testBit(Escaped); }
223 void setEscaped(bool v) { setBit(Escaped, v); }
224 bool inServer() const { return testBit(InServer); }
225 void setInServer(bool v) { setBit(InServer, v); }
226 bool skipLF() const { return testBit(SkipLF); }
227 void setSkipLF(bool v) { setBit(SkipLF, v); }
228 bool startTag() const { return testBit(StartTag); }
229 void setStartTag(bool v) { setBit(StartTag, v); }
230 bool discardLF() const { return testBit(DiscardLF); }
231 void setDiscardLF(bool v) { setBit(DiscardLF, v); }
232 bool allowYield() const { return testBit(AllowYield); }
233 void setAllowYield(bool v) { setBit(AllowYield, v); }
234 bool loadingExtScript() const { return testBit(LoadingExtScript); }
235 void setLoadingExtScript(bool v) { setBit(LoadingExtScript, v); }
236 bool forceSynchronous() const { return testBit(ForceSynchronous); }
237 void setForceSynchronous(bool v) { setBit(ForceSynchronous, v); }
239 bool inAnySpecial() const { return m_bits & (InScript | InStyle | InXmp | InTextArea | InTitle); }
240 bool hasTagState() const { return m_bits & TagMask; }
241 bool hasEntityState() const { return m_bits & EntityMask; }
243 bool needsSpecialWriteHandling() const { return m_bits & (InScript | InStyle | InXmp | InTextArea | InTitle | TagMask | EntityMask | InPlainText | InComment | InServer | InProcessingInstruction | StartTag); }
246 static const int EntityShift = 4;
248 TagMask = (1 << 4) - 1,
249 EntityMask = (1 << 7) - (1 << 4),
255 InPlainText = 1 << 12,
256 InProcessingInstruction = 1 << 13,
258 InTextArea = 1 << 15,
263 DiscardLF = 1 << 20, // FIXME: should clarify difference between skip and discard
264 AllowYield = 1 << 21,
265 LoadingExtScript = 1 << 22,
266 ForceSynchronous = 1 << 23
269 void setBit(StateBits bit, bool value)
276 bool testBit(StateBits bit) const { return m_bits & bit; }
285 // Name of an attribute that we just scanned.
286 AtomicString attrName;
288 // Used to store the code of a srcipting sequence
290 // Size of the script sequenze stored in @ref #scriptCode
292 // Maximal size that can be stored in @ref #scriptCode
293 int scriptCodeMaxSize;
294 // resync point of script code size
295 int scriptCodeResync;
297 // Stores characters if we are scanning for a string like "</script>"
298 UChar searchBuffer[10];
299 // Counts where we are in the string we are scanning for
301 // The string we are searching for
302 const UChar* searchFor;
303 // the stopper string
304 const char* searchStopper;
306 int searchStopperLen;
307 // if no more data is coming, just parse what we have (including ext scripts that
308 // may be still downloading) and finish
310 // URL to get source code of script from
312 String scriptSrcCharset;
313 // the HTML code we will parse after the external script we are waiting for has loaded
314 SegmentedString pendingSrc;
316 // the HTML code we will parse after this particular script has
317 // loaded, but before all pending HTML
318 SegmentedString *currentPrependingSrc;
320 // true if we are executing a script while parsing a document. This causes the parsing of
321 // the output of the script to be postponed until after the script has finished executing
322 int m_executingScript;
323 DeprecatedPtrQueue<CachedScript> pendingScripts;
324 RefPtr<Node> scriptNode;
326 bool m_requestingScript;
327 bool m_hasScriptsWaitingForStylesheets;
329 // if we found one broken comment, there are most likely others as well
330 // store a flag to get rid of the O(n^2) behaviour in such a case.
332 // current line number
334 // line number at which the current <script> started
335 int scriptStartLineno;
338 // The timer for continued processing.
339 Timer<HTMLTokenizer> m_timer;
341 // This buffer can hold arbitrarily long user-defined attribute names, such as in EMBED tags.
342 // So any fixed number might be too small, but rather than rewriting all usage of this buffer
343 // we'll just make it large enough to handle all imaginable cases.
345 char cBuffer[CBUFLEN + 2];
346 unsigned int m_cBufferPos;
355 void parseHTMLDocumentFragment(const String&, DocumentFragment*);
357 UChar decodeNamedEntity(const char*);
359 } // namespace WebCore
361 #endif // HTMLTokenizer_h