fixed wxHTML parsing to run in O(n) even in UTF8 build
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48390 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -73,7 +73,8 @@ public:
|
|||||||
|
|
||||||
// Parses the m_Source from begin_pos to end_pos-1.
|
// Parses the m_Source from begin_pos to end_pos-1.
|
||||||
// (in noparams version it parses whole m_Source)
|
// (in noparams version it parses whole m_Source)
|
||||||
void DoParsing(int begin_pos, int end_pos);
|
void DoParsing(const wxString::const_iterator& begin_pos,
|
||||||
|
const wxString::const_iterator& end_pos);
|
||||||
void DoParsing();
|
void DoParsing();
|
||||||
|
|
||||||
// Returns pointer to the tag at parser's current position
|
// Returns pointer to the tag at parser's current position
|
||||||
@@ -104,7 +105,7 @@ public:
|
|||||||
// Restores state before last call to PushTagHandler
|
// Restores state before last call to PushTagHandler
|
||||||
void PopTagHandler();
|
void PopTagHandler();
|
||||||
|
|
||||||
wxString* GetSource() {return &m_Source;}
|
const wxString* GetSource() {return m_Source;}
|
||||||
void SetSource(const wxString& src);
|
void SetSource(const wxString& src);
|
||||||
|
|
||||||
// Sets HTML source and remembers current parser's state so that it can
|
// Sets HTML source and remembers current parser's state so that it can
|
||||||
@@ -140,7 +141,8 @@ protected:
|
|||||||
void CreateDOMTree();
|
void CreateDOMTree();
|
||||||
void DestroyDOMTree();
|
void DestroyDOMTree();
|
||||||
void CreateDOMSubTree(wxHtmlTag *cur,
|
void CreateDOMSubTree(wxHtmlTag *cur,
|
||||||
int begin_pos, int end_pos,
|
const wxString::const_iterator& begin_pos,
|
||||||
|
const wxString::const_iterator& end_pos,
|
||||||
wxHtmlTagsCache *cache);
|
wxHtmlTagsCache *cache);
|
||||||
|
|
||||||
// Adds text to the output.
|
// Adds text to the output.
|
||||||
@@ -163,7 +165,7 @@ protected:
|
|||||||
wxHtmlTextPieces *m_TextPieces;
|
wxHtmlTextPieces *m_TextPieces;
|
||||||
size_t m_CurTextPiece;
|
size_t m_CurTextPiece;
|
||||||
|
|
||||||
wxString m_Source;
|
const wxString *m_Source;
|
||||||
|
|
||||||
wxHtmlParserState *m_SavedStates;
|
wxHtmlParserState *m_SavedStates;
|
||||||
|
|
||||||
@@ -232,7 +234,7 @@ protected:
|
|||||||
// parses input between beginning and ending tag.
|
// parses input between beginning and ending tag.
|
||||||
// m_Parser must be set.
|
// m_Parser must be set.
|
||||||
void ParseInner(const wxHtmlTag& tag)
|
void ParseInner(const wxHtmlTag& tag)
|
||||||
{ m_Parser->DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); }
|
{ m_Parser->DoParsing(tag.GetBeginIter(), tag.GetEndIter1()); }
|
||||||
|
|
||||||
// Parses given source as if it was tag's inner code (see
|
// Parses given source as if it was tag's inner code (see
|
||||||
// wxHtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
|
// wxHtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
|
||||||
|
@@ -31,7 +31,7 @@ class WXDLLIMPEXP_HTML wxHtmlTagsCache
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
wxHtmlTagsCacheData *m_Cache;
|
wxHtmlTagsCacheData *m_Cache;
|
||||||
size_t m_CachePos;
|
int m_CachePos;
|
||||||
|
|
||||||
wxHtmlTagsCacheData& Cache() { return *m_Cache; }
|
wxHtmlTagsCacheData& Cache() { return *m_Cache; }
|
||||||
|
|
||||||
@@ -41,7 +41,11 @@ public:
|
|||||||
virtual ~wxHtmlTagsCache();
|
virtual ~wxHtmlTagsCache();
|
||||||
|
|
||||||
// Finds parameters for tag starting at at and fills the variables
|
// Finds parameters for tag starting at at and fills the variables
|
||||||
void QueryTag(int at, int* end1, int* end2);
|
void QueryTag(const wxString::const_iterator& at,
|
||||||
|
const wxString::const_iterator& inputEnd,
|
||||||
|
wxString::const_iterator *end1,
|
||||||
|
wxString::const_iterator *end2,
|
||||||
|
bool *hasEnding);
|
||||||
|
|
||||||
DECLARE_NO_COPY_CLASS(wxHtmlTagsCache)
|
DECLARE_NO_COPY_CLASS(wxHtmlTagsCache)
|
||||||
};
|
};
|
||||||
@@ -60,7 +64,9 @@ protected:
|
|||||||
// The tag begins (with '<' character) at position pos in source
|
// The tag begins (with '<' character) at position pos in source
|
||||||
// end_pos is position where parsing ends (usually end of document)
|
// end_pos is position where parsing ends (usually end of document)
|
||||||
wxHtmlTag(wxHtmlTag *parent,
|
wxHtmlTag(wxHtmlTag *parent,
|
||||||
const wxString& source, int pos, int end_pos,
|
const wxString *source,
|
||||||
|
const wxString::const_iterator& pos,
|
||||||
|
const wxString::const_iterator& end_pos,
|
||||||
wxHtmlTagsCache *cache,
|
wxHtmlTagsCache *cache,
|
||||||
wxHtmlEntitiesParser *entParser);
|
wxHtmlEntitiesParser *entParser);
|
||||||
friend class wxHtmlParser;
|
friend class wxHtmlParser;
|
||||||
@@ -108,24 +114,37 @@ public:
|
|||||||
// Returns string containing all params.
|
// Returns string containing all params.
|
||||||
wxString GetAllParams() const;
|
wxString GetAllParams() const;
|
||||||
|
|
||||||
// return true if this there is matching ending tag
|
// return true if there is matching ending tag
|
||||||
inline bool HasEnding() const {return m_End1 >= 0;}
|
inline bool HasEnding() const {return m_hasEnding;}
|
||||||
|
|
||||||
// returns beginning position of _internal_ block of text
|
// returns beginning position of _internal_ block of text
|
||||||
// See explanation (returned value is marked with *):
|
// See explanation (returned value is marked with *):
|
||||||
// bla bla bla <MYTAG>* bla bla intenal text</MYTAG> bla bla
|
// bla bla bla <MYTAG>* bla bla intenal text</MYTAG> bla bla
|
||||||
inline int GetBeginPos() const {return m_Begin;}
|
wxString::const_iterator GetBeginIter() const
|
||||||
|
{ return m_Begin; }
|
||||||
// returns ending position of _internal_ block of text.
|
// returns ending position of _internal_ block of text.
|
||||||
// bla bla bla <MYTAG> bla bla intenal text*</MYTAG> bla bla
|
// bla bla bla <MYTAG> bla bla intenal text*</MYTAG> bla bla
|
||||||
inline int GetEndPos1() const {return m_End1;}
|
wxString::const_iterator GetEndIter1() const
|
||||||
|
{ wxASSERT(m_hasEnding); return m_End1; }
|
||||||
// returns end position 2 :
|
// returns end position 2 :
|
||||||
// bla bla bla <MYTAG> bla bla internal text</MYTAG>* bla bla
|
// bla bla bla <MYTAG> bla bla internal text</MYTAG>* bla bla
|
||||||
inline int GetEndPos2() const {return m_End2;}
|
wxString::const_iterator GetEndIter2() const
|
||||||
|
{ wxASSERT(m_hasEnding); return m_End2; }
|
||||||
|
|
||||||
|
#if WXWIN_COMPATIBILITY_2_8
|
||||||
|
wxDEPRECATED( inline int GetBeginPos() const );
|
||||||
|
wxDEPRECATED( inline int GetEndPos1() const );
|
||||||
|
wxDEPRECATED( inline int GetEndPos2() const );
|
||||||
|
#endif // WXWIN_COMPATIBILITY_2_8
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxString m_Name;
|
wxString m_Name;
|
||||||
int m_Begin, m_End1, m_End2;
|
bool m_hasEnding;
|
||||||
|
wxString::const_iterator m_Begin, m_End1, m_End2;
|
||||||
wxArrayString m_ParamNames, m_ParamValues;
|
wxArrayString m_ParamNames, m_ParamValues;
|
||||||
|
#if WXWIN_COMPATIBILITY_2_8
|
||||||
|
wxString::const_iterator m_sourceStart;
|
||||||
|
#endif
|
||||||
|
|
||||||
// DOM tree relations:
|
// DOM tree relations:
|
||||||
wxHtmlTag *m_Next;
|
wxHtmlTag *m_Next;
|
||||||
@@ -137,10 +156,16 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#if WXWIN_COMPATIBILITY_2_8
|
||||||
|
inline int wxHtmlTag::GetBeginPos() const { return m_Begin - m_sourceStart; }
|
||||||
|
inline int wxHtmlTag::GetEndPos1() const { return m_End1 - m_sourceStart; }
|
||||||
|
inline int wxHtmlTag::GetEndPos2() const { return m_End2 - m_sourceStart; }
|
||||||
|
#endif // WXWIN_COMPATIBILITY_2_8
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
#endif // wxUSE_HTML
|
||||||
|
|
||||||
#endif // _WX_HTMLTAG_H_
|
#endif // _WX_HTMLTAG_H_
|
||||||
|
|
||||||
|
@@ -48,8 +48,10 @@ class wxHtmlTextPiece
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxHtmlTextPiece() {}
|
wxHtmlTextPiece() {}
|
||||||
wxHtmlTextPiece(int pos, int lng) : m_pos(pos), m_lng(lng) {}
|
wxHtmlTextPiece(const wxString::const_iterator& start,
|
||||||
int m_pos, m_lng;
|
const wxString::const_iterator& end)
|
||||||
|
: m_start(start), m_end(end) {}
|
||||||
|
wxString::const_iterator m_start, m_end;
|
||||||
};
|
};
|
||||||
|
|
||||||
// NB: this is an empty class and not typedef because of forward declaration
|
// NB: this is an empty class and not typedef because of forward declaration
|
||||||
@@ -64,7 +66,7 @@ public:
|
|||||||
wxHtmlTag *m_tags;
|
wxHtmlTag *m_tags;
|
||||||
wxHtmlTextPieces *m_textPieces;
|
wxHtmlTextPieces *m_textPieces;
|
||||||
int m_curTextPiece;
|
int m_curTextPiece;
|
||||||
wxString m_source;
|
const wxString *m_source;
|
||||||
wxHtmlParserState *m_nextState;
|
wxHtmlParserState *m_nextState;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -78,6 +80,7 @@ wxHtmlParser::wxHtmlParser()
|
|||||||
: wxObject(), m_HandlersHash(wxKEY_STRING),
|
: wxObject(), m_HandlersHash(wxKEY_STRING),
|
||||||
m_FS(NULL), m_HandlersStack(NULL)
|
m_FS(NULL), m_HandlersStack(NULL)
|
||||||
{
|
{
|
||||||
|
m_Source = NULL;
|
||||||
m_entitiesParser = new wxHtmlEntitiesParser;
|
m_entitiesParser = new wxHtmlEntitiesParser;
|
||||||
m_Tags = NULL;
|
m_Tags = NULL;
|
||||||
m_CurTag = NULL;
|
m_CurTag = NULL;
|
||||||
@@ -103,6 +106,7 @@ wxHtmlParser::~wxHtmlParser()
|
|||||||
m_HandlersHash.Clear();
|
m_HandlersHash.Clear();
|
||||||
WX_CLEAR_LIST(wxList, m_HandlersList);
|
WX_CLEAR_LIST(wxList, m_HandlersList);
|
||||||
delete m_entitiesParser;
|
delete m_entitiesParser;
|
||||||
|
delete m_Source;
|
||||||
}
|
}
|
||||||
|
|
||||||
wxObject* wxHtmlParser::Parse(const wxString& source)
|
wxObject* wxHtmlParser::Parse(const wxString& source)
|
||||||
@@ -128,7 +132,10 @@ void wxHtmlParser::DoneParser()
|
|||||||
void wxHtmlParser::SetSource(const wxString& src)
|
void wxHtmlParser::SetSource(const wxString& src)
|
||||||
{
|
{
|
||||||
DestroyDOMTree();
|
DestroyDOMTree();
|
||||||
m_Source = src;
|
// NB: this is allocated on heap because wxHtmlTag keeps a pointer to
|
||||||
|
// this string if WXWIN_COMPATIBILITY_2_8
|
||||||
|
delete m_Source;
|
||||||
|
m_Source = new wxString(src);
|
||||||
CreateDOMTree();
|
CreateDOMTree();
|
||||||
m_CurTag = NULL;
|
m_CurTag = NULL;
|
||||||
m_CurTextPiece = 0;
|
m_CurTextPiece = 0;
|
||||||
@@ -136,54 +143,53 @@ void wxHtmlParser::SetSource(const wxString& src)
|
|||||||
|
|
||||||
void wxHtmlParser::CreateDOMTree()
|
void wxHtmlParser::CreateDOMTree()
|
||||||
{
|
{
|
||||||
wxHtmlTagsCache cache(m_Source);
|
wxHtmlTagsCache cache(*m_Source);
|
||||||
m_TextPieces = new wxHtmlTextPieces;
|
m_TextPieces = new wxHtmlTextPieces;
|
||||||
CreateDOMSubTree(NULL, 0, m_Source.length(), &cache);
|
CreateDOMSubTree(NULL, m_Source->begin(), m_Source->end(), &cache);
|
||||||
m_CurTextPiece = 0;
|
m_CurTextPiece = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern bool wxIsCDATAElement(const wxChar *tag);
|
extern bool wxIsCDATAElement(const wxString& tag);
|
||||||
|
|
||||||
void wxHtmlParser::CreateDOMSubTree(wxHtmlTag *cur,
|
void wxHtmlParser::CreateDOMSubTree(wxHtmlTag *cur,
|
||||||
int begin_pos, int end_pos,
|
const wxString::const_iterator& begin_pos,
|
||||||
|
const wxString::const_iterator& end_pos,
|
||||||
wxHtmlTagsCache *cache)
|
wxHtmlTagsCache *cache)
|
||||||
{
|
{
|
||||||
if (end_pos <= begin_pos) return;
|
if (end_pos <= begin_pos)
|
||||||
|
return;
|
||||||
|
|
||||||
wxChar c;
|
wxChar c;
|
||||||
int i = begin_pos;
|
wxString::const_iterator i = begin_pos;
|
||||||
int textBeginning = begin_pos;
|
wxString::const_iterator textBeginning = begin_pos;
|
||||||
|
|
||||||
// If the tag contains CDATA text, we include the text between beginning
|
// If the tag contains CDATA text, we include the text between beginning
|
||||||
// and ending tag verbosely. Setting i=end_pos will skip to the very
|
// and ending tag verbosely. Setting i=end_pos will skip to the very
|
||||||
// end of this function where text piece is added, bypassing any child
|
// end of this function where text piece is added, bypassing any child
|
||||||
// tags parsing (CDATA element can't have child elements by definition):
|
// tags parsing (CDATA element can't have child elements by definition):
|
||||||
if (cur != NULL && wxIsCDATAElement(cur->GetName().c_str()))
|
if (cur != NULL && wxIsCDATAElement(cur->GetName()))
|
||||||
{
|
{
|
||||||
i = end_pos;
|
i = end_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i < end_pos)
|
while (i < end_pos)
|
||||||
{
|
{
|
||||||
c = m_Source.GetChar(i);
|
c = *i;
|
||||||
|
|
||||||
if (c == wxT('<'))
|
if (c == wxT('<'))
|
||||||
{
|
{
|
||||||
// add text to m_TextPieces:
|
// add text to m_TextPieces:
|
||||||
if (i - textBeginning > 0)
|
if (i > textBeginning)
|
||||||
m_TextPieces->push_back(
|
m_TextPieces->push_back(wxHtmlTextPiece(textBeginning, i));
|
||||||
wxHtmlTextPiece(textBeginning, i - textBeginning));
|
|
||||||
|
|
||||||
// if it is a comment, skip it:
|
// if it is a comment, skip it:
|
||||||
wxString::const_iterator iter = m_Source.begin() + i;
|
if ( SkipCommentTag(i, m_Source->end()) )
|
||||||
if ( SkipCommentTag(iter, m_Source.end()) )
|
|
||||||
{
|
{
|
||||||
textBeginning =
|
textBeginning = i = i + 1; // skip closing '>' too
|
||||||
i = iter - m_Source.begin() + 1; // skip closing '>' too
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add another tag to the tree:
|
// add another tag to the tree:
|
||||||
else if (i < end_pos-1 && m_Source.GetChar(i+1) != wxT('/'))
|
else if (i < end_pos-1 && *(i+1) != wxT('/'))
|
||||||
{
|
{
|
||||||
wxHtmlTag *chd;
|
wxHtmlTag *chd;
|
||||||
if (cur)
|
if (cur)
|
||||||
@@ -211,12 +217,12 @@ void wxHtmlParser::CreateDOMSubTree(wxHtmlTag *cur,
|
|||||||
if (chd->HasEnding())
|
if (chd->HasEnding())
|
||||||
{
|
{
|
||||||
CreateDOMSubTree(chd,
|
CreateDOMSubTree(chd,
|
||||||
chd->GetBeginPos(), chd->GetEndPos1(),
|
chd->GetBeginIter(), chd->GetEndIter1(),
|
||||||
cache);
|
cache);
|
||||||
i = chd->GetEndPos2();
|
i = chd->GetEndIter2();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
i = chd->GetBeginPos();
|
i = chd->GetBeginIter();
|
||||||
|
|
||||||
textBeginning = i;
|
textBeginning = i;
|
||||||
}
|
}
|
||||||
@@ -224,17 +230,16 @@ void wxHtmlParser::CreateDOMSubTree(wxHtmlTag *cur,
|
|||||||
// ... or skip ending tag:
|
// ... or skip ending tag:
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (i < end_pos && m_Source.GetChar(i) != wxT('>')) i++;
|
while (i < end_pos && *i != wxT('>')) ++i;
|
||||||
textBeginning = i+1;
|
textBeginning = i+1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else i++;
|
else ++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add remaining text to m_TextPieces:
|
// add remaining text to m_TextPieces:
|
||||||
if (end_pos - textBeginning > 0)
|
if (end_pos > textBeginning)
|
||||||
m_TextPieces->push_back(
|
m_TextPieces->push_back(wxHtmlTextPiece(textBeginning, end_pos));
|
||||||
wxHtmlTextPiece(textBeginning, end_pos - textBeginning));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxHtmlParser::DestroyDOMTree()
|
void wxHtmlParser::DestroyDOMTree()
|
||||||
@@ -257,42 +262,45 @@ void wxHtmlParser::DoParsing()
|
|||||||
{
|
{
|
||||||
m_CurTag = m_Tags;
|
m_CurTag = m_Tags;
|
||||||
m_CurTextPiece = 0;
|
m_CurTextPiece = 0;
|
||||||
DoParsing(0, m_Source.length());
|
DoParsing(m_Source->begin(), m_Source->end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
|
void wxHtmlParser::DoParsing(const wxString::const_iterator& begin_pos_,
|
||||||
|
const wxString::const_iterator& end_pos)
|
||||||
{
|
{
|
||||||
if (end_pos <= begin_pos) return;
|
wxString::const_iterator begin_pos(begin_pos_);
|
||||||
|
|
||||||
|
if (end_pos <= begin_pos)
|
||||||
|
return;
|
||||||
|
|
||||||
wxHtmlTextPieces& pieces = *m_TextPieces;
|
wxHtmlTextPieces& pieces = *m_TextPieces;
|
||||||
size_t piecesCnt = pieces.size();
|
size_t piecesCnt = pieces.size();
|
||||||
|
|
||||||
while (begin_pos < end_pos)
|
while (begin_pos < end_pos)
|
||||||
{
|
{
|
||||||
while (m_CurTag && m_CurTag->GetBeginPos() < begin_pos)
|
while (m_CurTag && m_CurTag->GetBeginIter() < begin_pos)
|
||||||
m_CurTag = m_CurTag->GetNextTag();
|
m_CurTag = m_CurTag->GetNextTag();
|
||||||
while (m_CurTextPiece < piecesCnt &&
|
while (m_CurTextPiece < piecesCnt &&
|
||||||
pieces[m_CurTextPiece].m_pos < begin_pos)
|
pieces[m_CurTextPiece].m_start < begin_pos)
|
||||||
m_CurTextPiece++;
|
m_CurTextPiece++;
|
||||||
|
|
||||||
if (m_CurTextPiece < piecesCnt &&
|
if (m_CurTextPiece < piecesCnt &&
|
||||||
(!m_CurTag ||
|
(!m_CurTag ||
|
||||||
pieces[m_CurTextPiece].m_pos < m_CurTag->GetBeginPos()))
|
pieces[m_CurTextPiece].m_start < m_CurTag->GetBeginIter()))
|
||||||
{
|
{
|
||||||
// Add text:
|
// Add text:
|
||||||
AddText(GetEntitiesParser()->Parse(
|
AddText(GetEntitiesParser()->Parse(
|
||||||
m_Source.Mid(pieces[m_CurTextPiece].m_pos,
|
wxString(pieces[m_CurTextPiece].m_start,
|
||||||
pieces[m_CurTextPiece].m_lng)));
|
pieces[m_CurTextPiece].m_end)));
|
||||||
begin_pos = pieces[m_CurTextPiece].m_pos +
|
begin_pos = pieces[m_CurTextPiece].m_end;
|
||||||
pieces[m_CurTextPiece].m_lng;
|
|
||||||
m_CurTextPiece++;
|
m_CurTextPiece++;
|
||||||
}
|
}
|
||||||
else if (m_CurTag)
|
else if (m_CurTag)
|
||||||
{
|
{
|
||||||
if (m_CurTag->HasEnding())
|
if (m_CurTag->HasEnding())
|
||||||
begin_pos = m_CurTag->GetEndPos2();
|
begin_pos = m_CurTag->GetEndIter2();
|
||||||
else
|
else
|
||||||
begin_pos = m_CurTag->GetBeginPos();
|
begin_pos = m_CurTag->GetBeginIter();
|
||||||
wxHtmlTag *t = m_CurTag;
|
wxHtmlTag *t = m_CurTag;
|
||||||
m_CurTag = m_CurTag->GetNextTag();
|
m_CurTag = m_CurTag->GetNextTag();
|
||||||
AddTag(*t);
|
AddTag(*t);
|
||||||
@@ -318,7 +326,7 @@ void wxHtmlParser::AddTag(const wxHtmlTag& tag)
|
|||||||
if (!inner)
|
if (!inner)
|
||||||
{
|
{
|
||||||
if (tag.HasEnding())
|
if (tag.HasEnding())
|
||||||
DoParsing(tag.GetBeginPos(), tag.GetEndPos1());
|
DoParsing(tag.GetBeginIter(), tag.GetEndIter1());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +401,7 @@ void wxHtmlParser::SetSourceAndSaveState(const wxString& src)
|
|||||||
m_Tags = NULL;
|
m_Tags = NULL;
|
||||||
m_TextPieces = NULL;
|
m_TextPieces = NULL;
|
||||||
m_CurTextPiece = 0;
|
m_CurTextPiece = 0;
|
||||||
m_Source = wxEmptyString;
|
m_Source = NULL;
|
||||||
|
|
||||||
SetSource(src);
|
SetSource(src);
|
||||||
}
|
}
|
||||||
@@ -419,8 +427,7 @@ bool wxHtmlParser::RestoreState()
|
|||||||
|
|
||||||
wxString wxHtmlParser::GetInnerSource(const wxHtmlTag& tag)
|
wxString wxHtmlParser::GetInnerSource(const wxHtmlTag& tag)
|
||||||
{
|
{
|
||||||
return GetSource()->Mid(tag.GetBeginPos(),
|
return wxString(tag.GetBeginIter(), tag.GetEndIter1());
|
||||||
tag.GetEndPos1() - tag.GetBeginPos());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@@ -501,11 +508,16 @@ wxString wxHtmlEntitiesParser::Parse(const wxString& input) const
|
|||||||
const wxString::const_iterator ent_s = c;
|
const wxString::const_iterator ent_s = c;
|
||||||
wxChar entity_char;
|
wxChar entity_char;
|
||||||
|
|
||||||
for (; c != end &&
|
for ( ; c != end; ++c )
|
||||||
((*c >= wxT('a') && *c <= wxT('z')) ||
|
{
|
||||||
(*c >= wxT('A') && *c <= wxT('Z')) ||
|
wxChar ch = *c;
|
||||||
(*c >= wxT('0') && *c <= wxT('9')) ||
|
if ( !((ch >= wxT('a') && ch <= wxT('z')) ||
|
||||||
*c == wxT('_') || *c == wxT('#')); ++c) {}
|
(ch >= wxT('A') && ch <= wxT('Z')) ||
|
||||||
|
(ch >= wxT('0') && ch <= wxT('9')) ||
|
||||||
|
ch == wxT('_') || ch == wxT('#')) )
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
entity.append(ent_s, c);
|
entity.append(ent_s, c);
|
||||||
if (c == end || *c != wxT(';')) --c;
|
if (c == end || *c != wxT(';')) --c;
|
||||||
last = c+1;
|
last = c+1;
|
||||||
|
@@ -36,14 +36,21 @@ struct wxHtmlCacheItem
|
|||||||
{
|
{
|
||||||
// this is "pos" value passed to wxHtmlTag's constructor.
|
// this is "pos" value passed to wxHtmlTag's constructor.
|
||||||
// it is position of '<' character of the tag
|
// it is position of '<' character of the tag
|
||||||
int Key;
|
wxString::const_iterator Key;
|
||||||
|
|
||||||
|
// Tag type
|
||||||
|
enum Type
|
||||||
|
{
|
||||||
|
Type_Normal, // normal tag with a matching ending tag
|
||||||
|
Type_NoMatchingEndingTag, // there's no ending tag for this tag
|
||||||
|
Type_EndingTag // this is ending tag </..>
|
||||||
|
};
|
||||||
|
Type type;
|
||||||
|
|
||||||
// end positions for the tag:
|
// end positions for the tag:
|
||||||
// end1 is '<' of ending tag,
|
// end1 is '<' of ending tag,
|
||||||
// end2 is '>' or both are
|
// end2 is '>' or both are
|
||||||
// -1 if there is no ending tag for this one...
|
wxString::const_iterator End1, End2;
|
||||||
// or -2 if this is ending tag </...>
|
|
||||||
int End1, End2;
|
|
||||||
|
|
||||||
// name of this tag
|
// name of this tag
|
||||||
wxChar *Name;
|
wxChar *Name;
|
||||||
@@ -60,95 +67,108 @@ bool wxIsCDATAElement(const wxChar *tag)
|
|||||||
(wxStrcmp(tag, _T("STYLE")) == 0);
|
(wxStrcmp(tag, _T("STYLE")) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool wxIsCDATAElement(const wxString& tag)
|
||||||
|
{
|
||||||
|
return (wxStrcmp(tag.wx_str(), wxSTRING_TEXT("SCRIPT")) == 0) ||
|
||||||
|
(wxStrcmp(tag.wx_str(), wxSTRING_TEXT("STYLE")) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source)
|
wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source)
|
||||||
{
|
{
|
||||||
m_Cache = new wxHtmlTagsCacheData;
|
m_Cache = new wxHtmlTagsCacheData;
|
||||||
m_CachePos = 0;
|
m_CachePos = 0;
|
||||||
|
|
||||||
const wxChar *src = source.c_str();
|
|
||||||
int lng = source.length();
|
|
||||||
wxChar tagBuffer[256];
|
wxChar tagBuffer[256];
|
||||||
|
|
||||||
for ( int pos = 0; pos < lng; pos++ )
|
const wxString::const_iterator end = source.end();
|
||||||
|
for ( wxString::const_iterator pos = source.begin(); pos < end; ++pos )
|
||||||
{
|
{
|
||||||
if (src[pos] == wxT('<')) // tag found:
|
if (*pos == wxT('<')) // tag found:
|
||||||
{
|
{
|
||||||
// don't cache comment tags
|
// don't cache comment tags
|
||||||
wxString::const_iterator iter = source.begin() + pos;
|
if ( wxHtmlParser::SkipCommentTag(pos, source.end()) )
|
||||||
if ( wxHtmlParser::SkipCommentTag(iter, source.end()) )
|
|
||||||
{
|
|
||||||
pos = iter - source.begin();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
size_t tg = Cache().size();
|
size_t tg = Cache().size();
|
||||||
Cache().push_back(wxHtmlCacheItem());
|
Cache().push_back(wxHtmlCacheItem());
|
||||||
|
|
||||||
int stpos = pos++;
|
wxString::const_iterator stpos = pos++;
|
||||||
Cache()[tg].Key = stpos;
|
Cache()[tg].Key = stpos;
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for ( i = 0;
|
for ( i = 0;
|
||||||
pos < lng && i < (int)WXSIZEOF(tagBuffer) - 1 &&
|
pos < end && i < (int)WXSIZEOF(tagBuffer) - 1 &&
|
||||||
src[pos] != wxT('>') && !wxIsspace(src[pos]);
|
*pos != wxT('>') && !wxIsspace(*pos);
|
||||||
i++, pos++ )
|
++i, ++pos )
|
||||||
{
|
{
|
||||||
tagBuffer[i] = (wxChar)wxToupper(src[pos]);
|
tagBuffer[i] = (wxChar)wxToupper(*pos);
|
||||||
}
|
}
|
||||||
tagBuffer[i] = _T('\0');
|
tagBuffer[i] = _T('\0');
|
||||||
|
|
||||||
Cache()[tg].Name = new wxChar[i+1];
|
Cache()[tg].Name = new wxChar[i+1];
|
||||||
memcpy(Cache()[tg].Name, tagBuffer, (i+1)*sizeof(wxChar));
|
memcpy(Cache()[tg].Name, tagBuffer, (i+1)*sizeof(wxChar));
|
||||||
|
|
||||||
while (pos < lng && src[pos] != wxT('>')) pos++;
|
while (pos < end && *pos != wxT('>'))
|
||||||
|
++pos;
|
||||||
|
|
||||||
if (src[stpos+1] == wxT('/')) // ending tag:
|
if ((stpos+1) < end && *(stpos+1) == wxT('/')) // ending tag:
|
||||||
{
|
{
|
||||||
Cache()[tg].End1 = Cache()[tg].End2 = -2;
|
Cache()[tg].type = wxHtmlCacheItem::Type_EndingTag;
|
||||||
// find matching begin tag:
|
// find matching begin tag:
|
||||||
for (i = tg; i >= 0; i--)
|
for (i = tg; i >= 0; i--)
|
||||||
if ((Cache()[i].End1 == -1) && (wxStrcmp(Cache()[i].Name, tagBuffer+1) == 0))
|
{
|
||||||
|
if ((Cache()[i].type == wxHtmlCacheItem::Type_NoMatchingEndingTag) && (wxStrcmp(Cache()[i].Name, tagBuffer+1) == 0))
|
||||||
{
|
{
|
||||||
|
Cache()[i].type = wxHtmlCacheItem::Type_Normal;
|
||||||
Cache()[i].End1 = stpos;
|
Cache()[i].End1 = stpos;
|
||||||
Cache()[i].End2 = pos + 1;
|
Cache()[i].End2 = pos + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Cache()[tg].End1 = Cache()[tg].End2 = -1;
|
Cache()[tg].type = wxHtmlCacheItem::Type_NoMatchingEndingTag;
|
||||||
|
|
||||||
if (wxIsCDATAElement(tagBuffer))
|
if (wxIsCDATAElement(tagBuffer))
|
||||||
{
|
{
|
||||||
// store the orig pos in case we are missing the closing
|
// store the orig pos in case we are missing the closing
|
||||||
// tag (see below)
|
// tag (see below)
|
||||||
wxInt32 old_pos = pos;
|
const wxString::const_iterator old_pos = pos;
|
||||||
bool foundCloseTag = false;
|
bool foundCloseTag = false;
|
||||||
|
|
||||||
// find next matching tag
|
// find next matching tag
|
||||||
int tag_len = wxStrlen(tagBuffer);
|
int tag_len = wxStrlen(tagBuffer);
|
||||||
while (pos < lng)
|
while (pos < end)
|
||||||
{
|
{
|
||||||
// find the ending tag
|
// find the ending tag
|
||||||
while (pos + 1 < lng &&
|
while (pos + 1 < end &&
|
||||||
(src[pos] != '<' || src[pos+1] != '/'))
|
(*pos != '<' || *(pos+1) != '/'))
|
||||||
++pos;
|
++pos;
|
||||||
if (src[pos] == '<')
|
if (*pos == '<')
|
||||||
++pos;
|
++pos;
|
||||||
|
|
||||||
// see if it matches
|
// see if it matches
|
||||||
int match_pos = 0;
|
int match_pos = 0;
|
||||||
while (pos < lng && match_pos < tag_len && src[pos] != '>' && src[pos] != '<') {
|
while (pos < end && match_pos < tag_len )
|
||||||
|
{
|
||||||
|
wxChar c = *pos;
|
||||||
|
if ( c == '>' || c == '<' )
|
||||||
|
break;
|
||||||
|
|
||||||
// cast to wxChar needed to suppress warning in
|
// cast to wxChar needed to suppress warning in
|
||||||
// Unicode build
|
// Unicode build
|
||||||
if ((wxChar)wxToupper(src[pos]) == tagBuffer[match_pos]) {
|
if ((wxChar)wxToupper(c) == tagBuffer[match_pos])
|
||||||
|
{
|
||||||
++match_pos;
|
++match_pos;
|
||||||
}
|
}
|
||||||
else if (src[pos] == wxT(' ') || src[pos] == wxT('\n') ||
|
else if (c == wxT(' ') || c == wxT('\n') ||
|
||||||
src[pos] == wxT('\r') || src[pos] == wxT('\t')) {
|
c == wxT('\r') || c == wxT('\t'))
|
||||||
|
{
|
||||||
// need to skip over these
|
// need to skip over these
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
match_pos = 0;
|
match_pos = 0;
|
||||||
}
|
}
|
||||||
++pos;
|
++pos;
|
||||||
@@ -193,7 +213,11 @@ wxHtmlTagsCache::~wxHtmlTagsCache()
|
|||||||
delete m_Cache;
|
delete m_Cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
|
void wxHtmlTagsCache::QueryTag(const wxString::const_iterator& at,
|
||||||
|
const wxString::const_iterator& inputEnd,
|
||||||
|
wxString::const_iterator *end1,
|
||||||
|
wxString::const_iterator *end2,
|
||||||
|
bool *hasEnding)
|
||||||
{
|
{
|
||||||
if (Cache().empty())
|
if (Cache().empty())
|
||||||
return;
|
return;
|
||||||
@@ -203,22 +227,28 @@ void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
|
|||||||
int delta = (at < Cache()[m_CachePos].Key) ? -1 : 1;
|
int delta = (at < Cache()[m_CachePos].Key) ? -1 : 1;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if ( m_CachePos < 0 || m_CachePos == Cache().size() )
|
m_CachePos += delta;
|
||||||
|
|
||||||
|
if ( m_CachePos < 0 || m_CachePos >= (int)Cache().size() )
|
||||||
{
|
{
|
||||||
|
if ( m_CachePos < 0 )
|
||||||
|
m_CachePos = 0;
|
||||||
|
else
|
||||||
|
m_CachePos = Cache().size() - 1;
|
||||||
// something is very wrong with HTML, give up by returning an
|
// something is very wrong with HTML, give up by returning an
|
||||||
// impossibly large value which is going to be ignored by the
|
// impossibly large value which is going to be ignored by the
|
||||||
// caller
|
// caller
|
||||||
*end1 =
|
*end1 =
|
||||||
*end2 = INT_MAX;
|
*end2 = inputEnd;
|
||||||
|
*hasEnding = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_CachePos += delta;
|
|
||||||
}
|
}
|
||||||
while (Cache()[m_CachePos].Key != at);
|
while (Cache()[m_CachePos].Key != at);
|
||||||
}
|
}
|
||||||
*end1 = Cache()[m_CachePos].End1;
|
*end1 = Cache()[m_CachePos].End1;
|
||||||
*end2 = Cache()[m_CachePos].End2;
|
*end2 = Cache()[m_CachePos].End2;
|
||||||
|
*hasEnding = (Cache()[m_CachePos].type == wxHtmlCacheItem::Type_Normal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -229,7 +259,9 @@ void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
||||||
const wxString& source, int pos, int end_pos,
|
const wxString *source,
|
||||||
|
const wxString::const_iterator& pos,
|
||||||
|
const wxString::const_iterator& end_pos,
|
||||||
wxHtmlTagsCache *cache,
|
wxHtmlTagsCache *cache,
|
||||||
wxHtmlEntitiesParser *entParser)
|
wxHtmlEntitiesParser *entParser)
|
||||||
{
|
{
|
||||||
@@ -252,15 +284,14 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
|||||||
|
|
||||||
/* Find parameters and their values: */
|
/* Find parameters and their values: */
|
||||||
|
|
||||||
int i;
|
|
||||||
wxChar c;
|
wxChar c;
|
||||||
|
|
||||||
// fill-in name, params and begin pos:
|
// fill-in name, params and begin pos:
|
||||||
i = pos+1;
|
wxString::const_iterator i(pos+1);
|
||||||
|
|
||||||
// find tag's name and convert it to uppercase:
|
// find tag's name and convert it to uppercase:
|
||||||
while ((i < end_pos) &&
|
while ((i < end_pos) &&
|
||||||
((c = source[i++]) != wxT(' ') && c != wxT('\r') &&
|
((c = *(i++)) != wxT(' ') && c != wxT('\r') &&
|
||||||
c != wxT('\n') && c != wxT('\t') &&
|
c != wxT('\n') && c != wxT('\t') &&
|
||||||
c != wxT('>')))
|
c != wxT('>')))
|
||||||
{
|
{
|
||||||
@@ -272,7 +303,7 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
|||||||
// if the tag has parameters, read them and "normalize" them,
|
// if the tag has parameters, read them and "normalize" them,
|
||||||
// i.e. convert to uppercase, replace whitespaces by spaces and
|
// i.e. convert to uppercase, replace whitespaces by spaces and
|
||||||
// remove whitespaces around '=':
|
// remove whitespaces around '=':
|
||||||
if (source[i-1] != wxT('>'))
|
if (*(i-1) != wxT('>'))
|
||||||
{
|
{
|
||||||
#define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
|
#define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
|
||||||
c == wxT('\n') || c == wxT('\t'))
|
c == wxT('\n') || c == wxT('\t'))
|
||||||
@@ -291,14 +322,14 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
|||||||
state = ST_BEFORE_NAME;
|
state = ST_BEFORE_NAME;
|
||||||
while (i < end_pos)
|
while (i < end_pos)
|
||||||
{
|
{
|
||||||
c = source[i++];
|
c = *(i++);
|
||||||
|
|
||||||
if (c == wxT('>') && !(state == ST_VALUE && quote != 0))
|
if (c == wxT('>') && !(state == ST_VALUE && quote != 0))
|
||||||
{
|
{
|
||||||
if (state == ST_BEFORE_EQ || state == ST_NAME)
|
if (state == ST_BEFORE_EQ || state == ST_NAME)
|
||||||
{
|
{
|
||||||
m_ParamNames.Add(pname);
|
m_ParamNames.Add(pname);
|
||||||
m_ParamValues.Add(wxEmptyString);
|
m_ParamValues.Add(wxGetEmptyString());
|
||||||
}
|
}
|
||||||
else if (state == ST_VALUE && quote == 0)
|
else if (state == ST_VALUE && quote == 0)
|
||||||
{
|
{
|
||||||
@@ -333,7 +364,7 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
|||||||
else if (!IS_WHITE(c))
|
else if (!IS_WHITE(c))
|
||||||
{
|
{
|
||||||
m_ParamNames.Add(pname);
|
m_ParamNames.Add(pname);
|
||||||
m_ParamValues.Add(wxEmptyString);
|
m_ParamValues.Add(wxGetEmptyString());
|
||||||
pname = c;
|
pname = c;
|
||||||
state = ST_NAME;
|
state = ST_NAME;
|
||||||
}
|
}
|
||||||
@@ -342,7 +373,7 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
|||||||
if (!IS_WHITE(c))
|
if (!IS_WHITE(c))
|
||||||
{
|
{
|
||||||
if (c == wxT('"') || c == wxT('\''))
|
if (c == wxT('"') || c == wxT('\''))
|
||||||
quote = c, pvalue = wxEmptyString;
|
quote = c, pvalue = wxGetEmptyString();
|
||||||
else
|
else
|
||||||
quote = 0, pvalue = c;
|
quote = 0, pvalue = c;
|
||||||
state = ST_VALUE;
|
state = ST_VALUE;
|
||||||
@@ -374,10 +405,13 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
|
|||||||
#undef IS_WHITE
|
#undef IS_WHITE
|
||||||
}
|
}
|
||||||
m_Begin = i;
|
m_Begin = i;
|
||||||
|
cache->QueryTag(pos, source->end(), &m_End1, &m_End2, &m_hasEnding);
|
||||||
cache->QueryTag(pos, &m_End1, &m_End2);
|
|
||||||
if (m_End1 > end_pos) m_End1 = end_pos;
|
if (m_End1 > end_pos) m_End1 = end_pos;
|
||||||
if (m_End2 > end_pos) m_End2 = end_pos;
|
if (m_End2 > end_pos) m_End2 = end_pos;
|
||||||
|
|
||||||
|
#if WXWIN_COMPATIBILITY_2_8
|
||||||
|
m_sourceStart = source->begin();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
wxHtmlTag::~wxHtmlTag()
|
wxHtmlTag::~wxHtmlTag()
|
||||||
@@ -401,7 +435,7 @@ wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const
|
|||||||
{
|
{
|
||||||
int index = m_ParamNames.Index(par, false);
|
int index = m_ParamNames.Index(par, false);
|
||||||
if (index == wxNOT_FOUND)
|
if (index == wxNOT_FOUND)
|
||||||
return wxEmptyString;
|
return wxGetEmptyString();
|
||||||
if (with_commas)
|
if (with_commas)
|
||||||
{
|
{
|
||||||
// VS: backward compatibility, seems to be never used by wxHTML...
|
// VS: backward compatibility, seems to be never used by wxHTML...
|
||||||
@@ -438,8 +472,8 @@ bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
|
|||||||
// handle colours defined in HTML 4.0 first:
|
// handle colours defined in HTML 4.0 first:
|
||||||
if (str.length() > 1 && str[0] != _T('#'))
|
if (str.length() > 1 && str[0] != _T('#'))
|
||||||
{
|
{
|
||||||
#define HTML_COLOUR(name, r, g, b) \
|
#define HTML_COLOUR(name, r, g, b) \
|
||||||
if (str.IsSameAs(wxT(name), false)) \
|
if (str.IsSameAs(wxSTRING_TEXT(name), false)) \
|
||||||
{ clr->Set(r, g, b); return true; }
|
{ clr->Set(r, g, b); return true; }
|
||||||
HTML_COLOUR("black", 0x00,0x00,0x00)
|
HTML_COLOUR("black", 0x00,0x00,0x00)
|
||||||
HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
|
HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
|
||||||
|
@@ -289,9 +289,7 @@ TAG_HANDLER_BEGIN(TITLE, "TITLE")
|
|||||||
wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
|
wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
|
||||||
if (winIface)
|
if (winIface)
|
||||||
{
|
{
|
||||||
wxString title = m_WParser->GetSource()->Mid(
|
wxString title(tag.GetBeginIter(), tag.GetEndIter1());
|
||||||
tag.GetBeginPos(),
|
|
||||||
tag.GetEndPos1()-tag.GetBeginPos());
|
|
||||||
#if !wxUSE_UNICODE && wxUSE_WCHAR_T
|
#if !wxUSE_UNICODE && wxUSE_WCHAR_T
|
||||||
const wxFontEncoding enc = m_WParser->GetInputEncoding();
|
const wxFontEncoding enc = m_WParser->GetInputEncoding();
|
||||||
if ( enc != wxFONTENCODING_DEFAULT )
|
if ( enc != wxFONTENCODING_DEFAULT )
|
||||||
|
Reference in New Issue
Block a user