#ifndef HTMLTOKENIZER_H
#define HTMLTOKENIZER_H
#include <qstring.h>
#include <qobject.h>
#include <qptrqueue.h>
#include "misc/loader_client.h"
#include "misc/htmltags.h"
#include "xml/dom_stringimpl.h"
#include "xml/xml_tokenizer.h"
#include "html/html_elementimpl.h"
#include "xml/dom_docimpl.h"
#if APPLE_CHANGES
#ifdef __OBJC__
#define id id_AVOID_KEYWORD
#endif
#endif
class KCharsets;
class KHTMLParser;
class KHTMLView;
namespace DOM {
class DocumentFragmentImpl;
}
namespace khtml {
class CachedScript;
class Token
{
public:
Token() {
id = 0;
attrs = 0;
text = 0;
flat = false;
}
~Token() {
if(attrs) attrs->deref();
if(text) text->deref();
}
void addAttribute(DOM::DocumentImpl* doc, QChar* buffer, const QString& attrName, const DOM::AtomicString& v)
{
DOM::AttributeImpl* a = 0;
if(buffer->unicode())
a = new DOM::HTMLAttributeImpl(buffer->unicode(), v);
else if ( !attrName.isEmpty() && attrName != "/" )
a = new DOM::HTMLAttributeImpl(doc->attrId(0, DOM::DOMString(attrName).implementation(), false),
v);
if (a) {
if(!attrs) {
attrs = new DOM::HTMLNamedAttrMapImpl(0);
attrs->ref();
}
attrs->insertAttribute(a);
}
}
void reset()
{
if(attrs) {
attrs->deref();
attrs = 0;
}
id = 0;
if(text) {
text->deref();
text = 0;
}
flat = false;
}
DOM::HTMLNamedAttrMapImpl* attrs;
DOM::DOMStringImpl* text;
ushort id;
bool flat;
};
class HTMLTokenizer : public Tokenizer, public CachedObjectClient
{
public:
HTMLTokenizer(DOM::DocumentImpl *, KHTMLView * = 0);
HTMLTokenizer(DOM::DocumentImpl *, DOM::DocumentFragmentImpl *frag);
virtual ~HTMLTokenizer();
static void HTMLTokenizer::setIncludesComments(bool include);
static bool HTMLTokenizer::includesComments();
virtual void write(const TokenizerString &str, bool appendData);
virtual void finish();
virtual void setOnHold(bool onHold);
virtual void setForceSynchronous(bool force);
virtual bool isWaitingForScripts() const;
virtual void stopped();
virtual bool processingData() const;
protected:
void begin();
void end();
void reset();
void addPending();
void processToken();
void processListing(TokenizerString list);
void parseComment(TokenizerString &str);
void parseServer(TokenizerString &str);
void parseText(TokenizerString &str);
void parseListing(TokenizerString &str);
void parseSpecial(TokenizerString &str);
void parseTag(TokenizerString &str);
void parseEntity(TokenizerString &str, QChar *&dest, bool start = false);
void parseProcessingInstruction(TokenizerString &str);
void scriptHandler();
void scriptExecution(const QString& script, QString scriptURL = QString(),
int baseLine = 0);
void setSrc(const TokenizerString &source);
inline void checkBuffer(int len = 10)
{
if ( (dest - buffer) > size-len )
enlargeBuffer(len);
}
inline void checkScriptBuffer(int len = 10)
{
if ( scriptCodeSize + len >= scriptCodeMaxSize )
enlargeScriptBuffer(len);
}
void enlargeBuffer(int len);
void enlargeScriptBuffer(int len);
bool continueProcessing(int& processedCount, const QTime& startTime, const KWQUIEventTime& eventTime);
void timerEvent(QTimerEvent*);
void allDataProcessed();
void notifyFinished(CachedObject *finishedObj);
protected:
QChar *buffer;
QChar *dest;
Token currToken;
int size;
enum
{
NoQuote = 0,
SingleQuote,
DoubleQuote
} tquote;
enum
{
NonePending = 0,
SpacePending,
LFPending,
} pending;
enum
{
NoneDiscard = 0,
SpaceDiscard,
LFDiscard,
AllDiscard } discard;
bool skipLF;
bool startTag;
enum {
NoTag = 0,
TagName,
SearchAttribute,
AttributeName,
SearchEqual,
SearchValue,
QuotedValue,
Value,
SearchEnd
} tag;
enum {
NoEntity = 0,
SearchEntity,
NumericSearch,
Hexadecimal,
Decimal,
EntityName,
SearchSemicolon
} Entity;
unsigned EntityUnicodeValue;
bool script;
bool style;
bool select;
bool xmp;
bool title;
bool plaintext;
bool processingInstruction;
bool comment;
bool textarea;
bool escaped;
bool server;
bool brokenServer;
QString attrName;
bool attrNamePresent;
QChar *scriptCode;
int scriptCodeSize;
int scriptCodeMaxSize;
int scriptCodeResync;
QChar searchBuffer[ 10 ];
int searchCount;
const QChar *searchFor;
const char* searchStopper;
int searchStopperLen;
bool loadingExtScript;
bool noMoreData;
QString scriptSrc;
QString scriptSrcCharset;
bool javascript;
TokenizerString pendingSrc;
TokenizerString *currentPrependingSrc;
int m_executingScript;
QPtrQueue<CachedScript> cachedScript;
bool onHold;
bool brokenComments;
int lineno;
int scriptStartLineno;
int tagStartLineno;
int timerId;
bool allowYield;
bool forceSynchronous;
bool includesCommentsInDOM;
#define CBUFLEN 1024
char cBuffer[CBUFLEN+2];
unsigned int cBufferPos;
TokenizerString src;
KCharsets *charsets;
KHTMLParser *parser;
QGuardedPtr<KHTMLView> view;
bool inWrite;
};
}
#if APPLE_CHANGES
#undef id
#endif
#endif // HTMLTOKENIZER