X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b1a3a964997a143de68f2e3c75fa8dd82a4e3361..0826c4d39a53f8691b9430207420e9d9e175a73f:/src/html/htmltag.cpp diff --git a/src/html/htmltag.cpp b/src/html/htmltag.cpp index 4365a26554..6646ac1ed7 100644 --- a/src/html/htmltag.cpp +++ b/src/html/htmltag.cpp @@ -23,6 +23,8 @@ #endif #include "wx/html/htmlpars.h" +#include "wx/html/styleparams.h" + #include "wx/vector.h" #include // for vsscanf @@ -63,14 +65,14 @@ class wxHtmlTagsCacheData : public wxVector bool wxIsCDATAElement(const wxChar *tag) { - return (wxStrcmp(tag, _T("SCRIPT")) == 0) || - (wxStrcmp(tag, _T("STYLE")) == 0); + return (wxStrcmp(tag, wxT("SCRIPT")) == 0) || + (wxStrcmp(tag, wxT("STYLE")) == 0); } bool wxIsCDATAElement(const wxString& tag) { - return (wxStrcmp(tag.wx_str(), wxSTRING_TEXT("SCRIPT")) == 0) || - (wxStrcmp(tag.wx_str(), wxSTRING_TEXT("STYLE")) == 0); + return (wxStrcmp(tag.wx_str(), wxS("SCRIPT")) == 0) || + (wxStrcmp(tag.wx_str(), wxS("STYLE")) == 0); } wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) @@ -103,7 +105,7 @@ wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) { tagBuffer[i] = (wxChar)wxToupper(*pos); } - tagBuffer[i] = _T('\0'); + tagBuffer[i] = wxT('\0'); Cache()[tg].Name = new wxChar[i+1]; memcpy(Cache()[tg].Name, tagBuffer, (i+1)*sizeof(wxChar)); @@ -203,8 +205,7 @@ wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) for ( wxHtmlTagsCacheData::iterator i = Cache().begin(); i != Cache().end(); ++i ) { - delete[] i->Name; - i->Name = NULL; + wxDELETEA(i->Name); } } @@ -246,9 +247,27 @@ void wxHtmlTagsCache::QueryTag(const wxString::const_iterator& at, } while (Cache()[m_CachePos].Key != at); } - *end1 = Cache()[m_CachePos].End1; - *end2 = Cache()[m_CachePos].End2; - *hasEnding = (Cache()[m_CachePos].type == wxHtmlCacheItem::Type_Normal); + + switch ( Cache()[m_CachePos].type ) + { + case wxHtmlCacheItem::Type_Normal: + *end1 = Cache()[m_CachePos].End1; + *end2 = Cache()[m_CachePos].End2; + *hasEnding = true; + break; + + case wxHtmlCacheItem::Type_EndingTag: + wxFAIL_MSG("QueryTag called for ending tag - can't be"); + // but if it does happen, fall through, better than crashing + + case wxHtmlCacheItem::Type_NoMatchingEndingTag: + // If input HTML is invalid and there's no closing tag for this + // one, pretend that it runs all the way to the end of input + *end1 = inputEnd; + *end2 = inputEnd; + *hasEnding = false; + break; + } } @@ -284,7 +303,7 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, /* Find parameters and their values: */ - wxChar c; + wxChar c wxDUMMY_INITIALIZE(0); // fill-in name, params and begin pos: wxString::const_iterator i(pos+1); @@ -293,7 +312,7 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, while ((i < end_pos) && ((c = *(i++)) != wxT(' ') && c != wxT('\r') && c != wxT('\n') && c != wxT('\t') && - c != wxT('>'))) + c != wxT('>') && c != wxT('/'))) { if ((c >= wxT('a')) && (c <= wxT('z'))) c -= (wxT('a') - wxT('A')); @@ -412,6 +431,32 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, #if WXWIN_COMPATIBILITY_2_8 m_sourceStart = source->begin(); #endif + + // Try to parse any style parameters that can be handled simply by + // converting them to the equivalent HTML 3 attributes: this is a far cry + // from perfect but better than nothing. + static const struct EquivAttr + { + const char *style; + const char *attr; + } equivAttrs[] = + { + { "text-align", "ALIGN" }, + { "width", "WIDTH" }, + { "vertical-align", "VALIGN" }, + { "background", "BGCOLOR" }, + }; + + wxHtmlStyleParams styleParams(*this); + for ( unsigned n = 0; n < WXSIZEOF(equivAttrs); n++ ) + { + const EquivAttr& ea = equivAttrs[n]; + if ( styleParams.HasParam(ea.style) && !HasParam(ea.attr) ) + { + m_ParamNames.Add(ea.attr); + m_ParamValues.Add(styleParams.GetParam(ea.style)); + } + } } wxHtmlTag::~wxHtmlTag() @@ -431,12 +476,12 @@ bool wxHtmlTag::HasParam(const wxString& par) const return (m_ParamNames.Index(par, false) != wxNOT_FOUND); } -wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const +wxString wxHtmlTag::GetParam(const wxString& par, bool with_quotes) const { int index = m_ParamNames.Index(par, false); if (index == wxNOT_FOUND) return wxGetEmptyString(); - if (with_commas) + if (with_quotes) { // VS: backward compatibility, seems to be never used by wxHTML... wxString s; @@ -463,17 +508,16 @@ int wxHtmlTag::ScanParam(const wxString& par, return wxSscanf(parval, format, param); } -bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const +/* static */ +bool wxHtmlTag::ParseAsColour(const wxString& str, wxColour *clr) { - wxCHECK_MSG( clr, false, _T("invalid colour argument") ); - - wxString str = GetParam(par); + wxCHECK_MSG( clr, false, wxT("invalid colour argument") ); // handle colours defined in HTML 4.0 first: - if (str.length() > 1 && str[0] != _T('#')) + if (str.length() > 1 && str[0] != wxT('#')) { - #define HTML_COLOUR(name, r, g, b) \ - if (str.IsSameAs(wxSTRING_TEXT(name), false)) \ + #define HTML_COLOUR(name, r, g, b) \ + if (str.IsSameAs(wxS(name), false)) \ { clr->Set(r, g, b); return true; } HTML_COLOUR("black", 0x00,0x00,0x00) HTML_COLOUR("silver", 0xC0,0xC0,0xC0) @@ -504,13 +548,23 @@ bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const return false; } +bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const +{ + const wxString str = GetParam(par); + return !str.empty() && ParseAsColour(str, clr); +} + bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const { - if (!HasParam(par)) return false; + if ( !HasParam(par) ) + return false; + long i; - bool succ = GetParam(par).ToLong(&i); + if ( !GetParam(par).ToLong(&i) ) + return false; + *clr = (int)i; - return succ; + return true; } wxString wxHtmlTag::GetAllParams() const