From: Václav Slavík Date: Thu, 19 Jul 2001 22:31:00 +0000 (+0000) Subject: new wxHtmlParser core and changes implied by it X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/211dfeddfca5c5992db510bdaa72226bf2013c05 new wxHtmlParser core and changes implied by it git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@11112 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/html/helpdata.cpp b/src/html/helpdata.cpp index 6ba01be7f7..a3b76378e9 100644 --- a/src/html/helpdata.cpp +++ b/src/html/helpdata.cpp @@ -73,9 +73,10 @@ static int LINKAGEMODE IndexCompareFunc(const void *a, const void *b) class HP_Parser : public wxHtmlParser { - public: - void AddText(const char* WXUNUSED(text)) { } - wxObject* GetProduct() { return NULL; } +public: + wxObject* GetProduct() { return NULL; } +protected: + virtual void AddText(const wxChar* WXUNUSED(txt)) {} }; diff --git a/src/html/htmlpars.cpp b/src/html/htmlpars.cpp index 612f74bbfe..c45c2dfa29 100644 --- a/src/html/htmlpars.cpp +++ b/src/html/htmlpars.cpp @@ -32,8 +32,32 @@ #include "wx/fontmap.h" #include "wx/html/htmldefs.h" #include "wx/html/htmlpars.h" +#include "wx/dynarray.h" +#include "wx/arrimpl.cpp" +//----------------------------------------------------------------------------- +// wxHtmlParser helpers +//----------------------------------------------------------------------------- + +class wxHtmlTextPiece +{ +public: + wxHtmlTextPiece(int pos, int lng) : m_pos(pos), m_lng(lng) {} + int m_pos, m_lng; +}; + +WX_DECLARE_OBJARRAY(wxHtmlTextPiece, wxHtmlTextPieces); +WX_DEFINE_OBJARRAY(wxHtmlTextPieces); +struct wxHtmlParserState +{ + wxHtmlTag *m_curTag; + wxHtmlTag *m_tags; + wxHtmlTextPieces *m_textPieces; + int m_curTextPiece; + wxString m_source; + wxHtmlParserState *m_nextState; +}; //----------------------------------------------------------------------------- // wxHtmlParser @@ -42,14 +66,21 @@ IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject) wxHtmlParser::wxHtmlParser() - : wxObject(), m_Cache(NULL), m_HandlersHash(wxKEY_STRING), + : wxObject(), m_HandlersHash(wxKEY_STRING), m_FS(NULL), m_HandlersStack(NULL) { m_entitiesParser = new wxHtmlEntitiesParser; + m_Tags = NULL; + m_CurTag = NULL; + m_TextPieces = NULL; + m_CurTextPiece = 0; + m_SavedStates = NULL; } wxHtmlParser::~wxHtmlParser() { + while (RestoreState()) + DestroyDOMTree(); delete m_HandlersStack; m_HandlersHash.Clear(); m_HandlersList.DeleteContents(TRUE); @@ -75,62 +106,192 @@ void wxHtmlParser::InitParser(const wxString& source) void wxHtmlParser::DoneParser() { - delete m_Cache; - m_Cache = NULL; + DestroyDOMTree(); } void wxHtmlParser::SetSource(const wxString& src) { + DestroyDOMTree(); m_Source = src; - delete m_Cache; - m_Cache = new wxHtmlTagsCache(m_Source); + CreateDOMTree(); + m_CurTag = NULL; + m_CurTextPiece = 0; } -void wxHtmlParser::DoParsing(int begin_pos, int end_pos) +void wxHtmlParser::CreateDOMTree() { - if (end_pos <= begin_pos) return; - - char c; - char *temp = new char[end_pos - begin_pos + 1]; - int i; - int templen; + wxHtmlTagsCache cache(m_Source); + m_TextPieces = new wxHtmlTextPieces; + CreateDOMSubTree(NULL, 0, m_Source.Length(), &cache); + m_CurTextPiece = 0; +} - templen = 0; - i = begin_pos; +void wxHtmlParser::CreateDOMSubTree(wxHtmlTag *cur, + int begin_pos, int end_pos, + wxHtmlTagsCache *cache) +{ + if (end_pos <= begin_pos) return; + wxChar c; + int i = begin_pos; + int textBeginning = begin_pos; + while (i < end_pos) { - c = m_Source[(unsigned int) i]; - - // continue building word: - if (c != '<') - { - temp[templen++] = c; - i++; - } - - else if (c == '<') - { - wxHtmlTag tag(m_Source, i, end_pos, m_Cache, m_entitiesParser); + c = m_Source.GetChar(i); - if (templen) + if (c == wxT('<')) + { + // add text to m_TextPieces: + if (i - textBeginning > 0) + m_TextPieces->Add( + wxHtmlTextPiece(textBeginning, i - textBeginning)); + + // if it is a comment, skip it: + if (i < end_pos-6 && m_Source.GetChar(i+1) == wxT('!') && + m_Source.GetChar(i+2) == wxT('-') && + m_Source.GetChar(i+3) == wxT('-')) + { + // Comments begin with "