]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlpars.cpp
Applied part of [ 1575767 ] Init/size/check fixes for sockets
[wxWidgets.git] / src / html / htmlpars.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/html/htmlpars.cpp
5526e819
VS
3// Purpose: wxHtmlParser class (generic parser)
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819 6// Copyright: (c) 1999 Vaclav Slavik
65571936 7// Licence: wxWindows licence
5526e819
VS
8/////////////////////////////////////////////////////////////////////////////
9
3096bd2f 10#include "wx/wxprec.h"
5526e819 11
2b5f62a0 12#ifdef __BORLANDC__
93763ad5 13 #pragma hdrstop
5526e819
VS
14#endif
15
93763ad5
WS
16#if wxUSE_HTML && wxUSE_STREAMS
17
5526e819 18#ifndef WXPRECOMP
ad9835c9 19 #include "wx/dynarray.h"
04dbb646
VZ
20 #include "wx/log.h"
21 #include "wx/intl.h"
670f9935 22 #include "wx/app.h"
5526e819
VS
23#endif
24
69941f05
VS
25#include "wx/tokenzr.h"
26#include "wx/wfstream.h"
27#include "wx/url.h"
daa616fc 28#include "wx/fontmap.h"
69941f05
VS
29#include "wx/html/htmldefs.h"
30#include "wx/html/htmlpars.h"
211dfedd 31#include "wx/arrimpl.cpp"
5526e819 32
7127d129
RR
33#ifdef __WXWINCE__
34 #include "wx/msw/wince/missing.h" // for bsearch()
35#endif
34fdf762
VS
36
37// DLL options compatibility check:
34fdf762 38WX_CHECK_BUILD_OPTIONS("wxHTML")
34fdf762 39
25271309
VZ
40const wxChar *wxTRACE_HTML_DEBUG = _T("htmldebug");
41
211dfedd
VS
42//-----------------------------------------------------------------------------
43// wxHtmlParser helpers
44//-----------------------------------------------------------------------------
45
46class wxHtmlTextPiece
47{
48public:
49 wxHtmlTextPiece(int pos, int lng) : m_pos(pos), m_lng(lng) {}
50 int m_pos, m_lng;
51};
52
53WX_DECLARE_OBJARRAY(wxHtmlTextPiece, wxHtmlTextPieces);
17a1ebd1 54WX_DEFINE_OBJARRAY(wxHtmlTextPieces)
5526e819 55
481c879b 56class wxHtmlParserState
211dfedd 57{
481c879b 58public:
211dfedd
VS
59 wxHtmlTag *m_curTag;
60 wxHtmlTag *m_tags;
61 wxHtmlTextPieces *m_textPieces;
62 int m_curTextPiece;
63 wxString m_source;
64 wxHtmlParserState *m_nextState;
65};
5526e819
VS
66
67//-----------------------------------------------------------------------------
68// wxHtmlParser
69//-----------------------------------------------------------------------------
70
71IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject)
72
04dbb646 73wxHtmlParser::wxHtmlParser()
211dfedd 74 : wxObject(), m_HandlersHash(wxKEY_STRING),
daa616fc
VS
75 m_FS(NULL), m_HandlersStack(NULL)
76{
77 m_entitiesParser = new wxHtmlEntitiesParser;
211dfedd
VS
78 m_Tags = NULL;
79 m_CurTag = NULL;
80 m_TextPieces = NULL;
81 m_CurTextPiece = 0;
82 m_SavedStates = NULL;
daa616fc
VS
83}
84
85wxHtmlParser::~wxHtmlParser()
86{
0beefa20
VS
87 while (RestoreState()) {}
88 DestroyDOMTree();
222ed1d6
MB
89
90 if (m_HandlersStack)
91 {
92 wxList& tmp = *m_HandlersStack;
93 wxList::iterator it, en;
94 for( it = tmp.begin(), en = tmp.end(); it != en; ++it )
95 delete (wxHashTable*)*it;
96 tmp.clear();
97 }
daa616fc
VS
98 delete m_HandlersStack;
99 m_HandlersHash.Clear();
222ed1d6 100 WX_CLEAR_LIST(wxList, m_HandlersList);
daa616fc
VS
101 delete m_entitiesParser;
102}
5526e819
VS
103
104wxObject* wxHtmlParser::Parse(const wxString& source)
105{
5526e819
VS
106 InitParser(source);
107 DoParsing();
2b5f62a0 108 wxObject *result = GetProduct();
5526e819
VS
109 DoneParser();
110 return result;
111}
112
5526e819
VS
113void wxHtmlParser::InitParser(const wxString& source)
114{
1309ba6c 115 SetSource(source);
d1da8872 116 m_stopParsing = false;
5526e819 117}
1309ba6c 118
5526e819
VS
119void wxHtmlParser::DoneParser()
120{
211dfedd 121 DestroyDOMTree();
5526e819
VS
122}
123
1309ba6c
VS
124void wxHtmlParser::SetSource(const wxString& src)
125{
211dfedd 126 DestroyDOMTree();
1309ba6c 127 m_Source = src;
211dfedd
VS
128 CreateDOMTree();
129 m_CurTag = NULL;
130 m_CurTextPiece = 0;
1309ba6c 131}
5526e819 132
211dfedd 133void wxHtmlParser::CreateDOMTree()
5526e819 134{
211dfedd
VS
135 wxHtmlTagsCache cache(m_Source);
136 m_TextPieces = new wxHtmlTextPieces;
93763ad5 137 CreateDOMSubTree(NULL, 0, m_Source.length(), &cache);
211dfedd
VS
138 m_CurTextPiece = 0;
139}
5526e819 140
7c6cd4a8
VS
141extern bool wxIsCDATAElement(const wxChar *tag);
142
211dfedd
VS
143void wxHtmlParser::CreateDOMSubTree(wxHtmlTag *cur,
144 int begin_pos, int end_pos,
145 wxHtmlTagsCache *cache)
146{
147 if (end_pos <= begin_pos) return;
5526e819 148
211dfedd
VS
149 wxChar c;
150 int i = begin_pos;
151 int textBeginning = begin_pos;
d699f48b 152
7c6cd4a8
VS
153 // If the tag contains CDATA text, we include the text between beginning
154 // and ending tag verbosely. Setting i=end_pos will skip to the very
155 // end of this function where text piece is added, bypassing any child
156 // tags parsing (CDATA element can't have child elements by definition):
157 if (cur != NULL && wxIsCDATAElement(cur->GetName().c_str()))
158 {
159 i = end_pos;
160 }
161
04dbb646 162 while (i < end_pos)
4f9297b0 163 {
211dfedd 164 c = m_Source.GetChar(i);
5526e819 165
211dfedd
VS
166 if (c == wxT('<'))
167 {
168 // add text to m_TextPieces:
169 if (i - textBeginning > 0)
170 m_TextPieces->Add(
171 wxHtmlTextPiece(textBeginning, i - textBeginning));
172
173 // if it is a comment, skip it:
174 if (i < end_pos-6 && m_Source.GetChar(i+1) == wxT('!') &&
175 m_Source.GetChar(i+2) == wxT('-') &&
176 m_Source.GetChar(i+3) == wxT('-'))
177 {
178 // Comments begin with "<!--" and end with "--[ \t\r\n]*>"
179 // according to HTML 4.0
180 int dashes = 0;
181 i += 4;
182 while (i < end_pos)
183 {
184 c = m_Source.GetChar(i++);
d699f48b 185 if ((c == wxT(' ') || c == wxT('\n') ||
211dfedd
VS
186 c == wxT('\r') || c == wxT('\t')) && dashes >= 2) {}
187 else if (c == wxT('>') && dashes >= 2)
188 {
189 textBeginning = i;
190 break;
191 }
192 else if (c == wxT('-'))
193 dashes++;
d699f48b 194 else
211dfedd
VS
195 dashes = 0;
196 }
197 }
d699f48b 198
211dfedd
VS
199 // add another tag to the tree:
200 else if (i < end_pos-1 && m_Source.GetChar(i+1) != wxT('/'))
d1da8872 201 {
211dfedd 202 wxHtmlTag *chd;
d699f48b
KB
203 if (cur)
204 chd = new wxHtmlTag(cur, m_Source,
211dfedd 205 i, end_pos, cache, m_entitiesParser);
d699f48b 206 else
211dfedd
VS
207 {
208 chd = new wxHtmlTag(NULL, m_Source,
209 i, end_pos, cache, m_entitiesParser);
d699f48b 210 if (!m_Tags)
211dfedd 211 {
d699f48b 212 // if this is the first tag to be created make the root
211dfedd
VS
213 // m_Tags point to it:
214 m_Tags = chd;
215 }
216 else
217 {
d699f48b 218 // if there is already a root tag add this tag as
211dfedd
VS
219 // the last sibling:
220 chd->m_Prev = m_Tags->GetLastSibling();
221 chd->m_Prev->m_Next = chd;
222 }
223 }
224
225 if (chd->HasEnding())
226 {
227 CreateDOMSubTree(chd,
d699f48b 228 chd->GetBeginPos(), chd->GetEndPos1(),
211dfedd
VS
229 cache);
230 i = chd->GetEndPos2();
231 }
232 else
233 i = chd->GetBeginPos();
d1da8872 234
211dfedd
VS
235 textBeginning = i;
236 }
237
238 // ... or skip ending tag:
d699f48b 239 else
211dfedd
VS
240 {
241 while (i < end_pos && m_Source.GetChar(i) != wxT('>')) i++;
242 textBeginning = i+1;
5526e819 243 }
5526e819 244 }
211dfedd 245 else i++;
5526e819
VS
246 }
247
211dfedd
VS
248 // add remaining text to m_TextPieces:
249 if (end_pos - textBeginning > 0)
250 m_TextPieces->Add(
251 wxHtmlTextPiece(textBeginning, end_pos - textBeginning));
252}
253
254void wxHtmlParser::DestroyDOMTree()
255{
256 wxHtmlTag *t1, *t2;
257 t1 = m_Tags;
258 while (t1)
259 {
260 t2 = t1->GetNextSibling();
261 delete t1;
262 t1 = t2;
263 }
264 m_Tags = m_CurTag = NULL;
265
266 delete m_TextPieces;
267 m_TextPieces = NULL;
268}
269
d699f48b 270void wxHtmlParser::DoParsing()
211dfedd
VS
271{
272 m_CurTag = m_Tags;
273 m_CurTextPiece = 0;
93763ad5 274 DoParsing(0, m_Source.length());
211dfedd
VS
275}
276
277void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
278{
279 if (end_pos <= begin_pos) return;
d699f48b 280
211dfedd
VS
281 wxHtmlTextPieces& pieces = *m_TextPieces;
282 size_t piecesCnt = pieces.GetCount();
d699f48b 283
211dfedd
VS
284 while (begin_pos < end_pos)
285 {
286 while (m_CurTag && m_CurTag->GetBeginPos() < begin_pos)
287 m_CurTag = m_CurTag->GetNextTag();
d699f48b 288 while (m_CurTextPiece < piecesCnt &&
211dfedd
VS
289 pieces[m_CurTextPiece].m_pos < begin_pos)
290 m_CurTextPiece++;
291
d699f48b
KB
292 if (m_CurTextPiece < piecesCnt &&
293 (!m_CurTag ||
211dfedd
VS
294 pieces[m_CurTextPiece].m_pos < m_CurTag->GetBeginPos()))
295 {
296 // Add text:
f23e92e7
VS
297 AddText(GetEntitiesParser()->Parse(
298 m_Source.Mid(pieces[m_CurTextPiece].m_pos,
299 pieces[m_CurTextPiece].m_lng)));
d699f48b 300 begin_pos = pieces[m_CurTextPiece].m_pos +
211dfedd
VS
301 pieces[m_CurTextPiece].m_lng;
302 m_CurTextPiece++;
303 }
304 else if (m_CurTag)
305 {
902725ee
WS
306 if (m_CurTag->HasEnding())
307 begin_pos = m_CurTag->GetEndPos2();
308 else
309 begin_pos = m_CurTag->GetBeginPos();
211dfedd
VS
310 wxHtmlTag *t = m_CurTag;
311 m_CurTag = m_CurTag->GetNextTag();
312 AddTag(*t);
2b5f62a0
VZ
313 if (m_stopParsing)
314 return;
211dfedd
VS
315 }
316 else break;
5526e819
VS
317 }
318}
319
5526e819
VS
320void wxHtmlParser::AddTag(const wxHtmlTag& tag)
321{
322 wxHtmlTagHandler *h;
d1da8872 323 bool inner = false;
5526e819
VS
324
325 h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName());
326 if (h)
2b5f62a0 327 {
4f9297b0 328 inner = h->HandleTag(tag);
2b5f62a0
VZ
329 if (m_stopParsing)
330 return;
331 }
04dbb646 332 if (!inner)
4f9297b0 333 {
5526e819
VS
334 if (tag.HasEnding())
335 DoParsing(tag.GetBeginPos(), tag.GetEndPos1());
336 }
337}
338
5526e819
VS
339void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
340{
4f9297b0 341 wxString s(handler->GetSupportedTags());
211dfedd 342 wxStringTokenizer tokenizer(s, wxT(", "));
5526e819 343
5526e819 344 while (tokenizer.HasMoreTokens())
470252df 345 m_HandlersHash.Put(tokenizer.GetNextToken(), handler);
5526e819
VS
346
347 if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND)
348 m_HandlersList.Append(handler);
349
4f9297b0 350 handler->SetParser(this);
5526e819
VS
351}
352
fbfb8bcc 353void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, const wxString& tags)
a7a4d01b 354{
211dfedd 355 wxStringTokenizer tokenizer(tags, wxT(", "));
a7a4d01b
VS
356 wxString key;
357
04dbb646 358 if (m_HandlersStack == NULL)
4f9297b0 359 {
a7a4d01b 360 m_HandlersStack = new wxList;
a7a4d01b
VS
361 }
362
222ed1d6 363 m_HandlersStack->Insert((wxObject*)new wxHashTable(m_HandlersHash));
a7a4d01b 364
04dbb646 365 while (tokenizer.HasMoreTokens())
4f9297b0 366 {
470252df 367 key = tokenizer.GetNextToken();
a7a4d01b
VS
368 m_HandlersHash.Delete(key);
369 m_HandlersHash.Put(key, handler);
370 }
371}
372
a7a4d01b
VS
373void wxHtmlParser::PopTagHandler()
374{
222ed1d6 375 wxList::compatibility_iterator first;
04dbb646 376
dfa4a244 377 if ( !m_HandlersStack ||
28b4db7f
VZ
378#if wxUSE_STL
379 !(first = m_HandlersStack->GetFirst())
380#else // !wxUSE_STL
381 ((first = m_HandlersStack->GetFirst()) == NULL)
382#endif // wxUSE_STL/!wxUSE_STL
383 )
f3c82859
VS
384 {
385 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
386 return;
387 }
4f9297b0 388 m_HandlersHash = *((wxHashTable*) first->GetData());
222ed1d6
MB
389 delete (wxHashTable*) first->GetData();
390 m_HandlersStack->Erase(first);
a7a4d01b
VS
391}
392
211dfedd
VS
393void wxHtmlParser::SetSourceAndSaveState(const wxString& src)
394{
395 wxHtmlParserState *s = new wxHtmlParserState;
396
397 s->m_curTag = m_CurTag;
398 s->m_tags = m_Tags;
399 s->m_textPieces = m_TextPieces;
400 s->m_curTextPiece = m_CurTextPiece;
401 s->m_source = m_Source;
402
403 s->m_nextState = m_SavedStates;
404 m_SavedStates = s;
405
406 m_CurTag = NULL;
407 m_Tags = NULL;
408 m_TextPieces = NULL;
409 m_CurTextPiece = 0;
410 m_Source = wxEmptyString;
d699f48b 411
211dfedd
VS
412 SetSource(src);
413}
414
415bool wxHtmlParser::RestoreState()
416{
d1da8872 417 if (!m_SavedStates) return false;
d699f48b 418
0beefa20
VS
419 DestroyDOMTree();
420
211dfedd
VS
421 wxHtmlParserState *s = m_SavedStates;
422 m_SavedStates = s->m_nextState;
d699f48b 423
211dfedd
VS
424 m_CurTag = s->m_curTag;
425 m_Tags = s->m_tags;
426 m_TextPieces = s->m_textPieces;
427 m_CurTextPiece = s->m_curTextPiece;
428 m_Source = s->m_source;
d699f48b 429
211dfedd 430 delete s;
d1da8872 431 return true;
211dfedd
VS
432}
433
e7feeafa
VS
434wxString wxHtmlParser::GetInnerSource(const wxHtmlTag& tag)
435{
436 return GetSource()->Mid(tag.GetBeginPos(),
437 tag.GetEndPos1() - tag.GetBeginPos());
438}
439
5526e819
VS
440//-----------------------------------------------------------------------------
441// wxHtmlTagHandler
442//-----------------------------------------------------------------------------
443
444IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject)
daa616fc 445
e7feeafa
VS
446void wxHtmlTagHandler::ParseInnerSource(const wxString& source)
447{
448 // It is safe to temporarily change the source being parsed,
449 // provided we restore the state back after parsing
450 m_Parser->SetSourceAndSaveState(source);
451 m_Parser->DoParsing();
452 m_Parser->RestoreState();
453}
454
daa616fc
VS
455
456//-----------------------------------------------------------------------------
457// wxHtmlEntitiesParser
458//-----------------------------------------------------------------------------
459
460IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser,wxObject)
461
462wxHtmlEntitiesParser::wxHtmlEntitiesParser()
463#if wxUSE_WCHAR_T && !wxUSE_UNICODE
464 : m_conv(NULL), m_encoding(wxFONTENCODING_SYSTEM)
223d09f6 465#endif
daa616fc
VS
466{
467}
468
469wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
470{
5438a566 471#if wxUSE_WCHAR_T && !wxUSE_UNICODE
daa616fc 472 delete m_conv;
5438a566 473#endif
daa616fc 474}
5526e819 475
daa616fc
VS
476void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding)
477{
478#if wxUSE_WCHAR_T && !wxUSE_UNICODE
2b5f62a0
VZ
479 if (encoding == m_encoding)
480 return;
481
daa616fc 482 delete m_conv;
2b5f62a0 483
daa616fc 484 m_encoding = encoding;
2b5f62a0
VZ
485 if (m_encoding == wxFONTENCODING_SYSTEM)
486 m_conv = NULL;
487 else
daa616fc 488 m_conv = new wxCSConv(wxFontMapper::GetEncodingName(m_encoding));
d699f48b
KB
489#else
490 (void) encoding;
daa616fc
VS
491#endif
492}
493
494wxString wxHtmlEntitiesParser::Parse(const wxString& input)
495{
496 const wxChar *c, *last;
497 const wxChar *in_str = input.c_str();
498 wxString output;
d1da8872 499
470252df 500 output.reserve(input.length());
04dbb646 501
daa616fc
VS
502 for (c = in_str, last = in_str; *c != wxT('\0'); c++)
503 {
504 if (*c == wxT('&'))
505 {
506 if (c - last > 0)
507 output.append(last, c - last);
9e2bd135
VZ
508 if ( *++c == wxT('\0') )
509 break;
d1da8872 510
daa616fc
VS
511 wxString entity;
512 const wxChar *ent_s = c;
470252df 513 wxChar entity_char;
d1da8872 514
daa616fc
VS
515 for (; (*c >= wxT('a') && *c <= wxT('z')) ||
516 (*c >= wxT('A') && *c <= wxT('Z')) ||
517 (*c >= wxT('0') && *c <= wxT('9')) ||
518 *c == wxT('_') || *c == wxT('#'); c++) {}
519 entity.append(ent_s, c - ent_s);
211dfedd
VS
520 if (*c != wxT(';')) c--;
521 last = c+1;
470252df
VS
522 entity_char = GetEntityChar(entity);
523 if (entity_char)
524 output << entity_char;
525 else
526 {
527 output.append(ent_s-1, c-ent_s+2);
25271309
VZ
528 wxLogTrace(wxTRACE_HTML_DEBUG,
529 wxT("Unrecognized HTML entity: '%s'"),
530 entity.c_str());
470252df 531 }
daa616fc
VS
532 }
533 }
534 if (*last != wxT('\0'))
535 output.append(last);
536 return output;
537}
538
539struct wxHtmlEntityInfo
540{
541 const wxChar *name;
542 unsigned code;
543};
544
90350682 545extern "C" int LINKAGEMODE wxHtmlEntityCompare(const void *key, const void *item)
daa616fc
VS
546{
547 return wxStrcmp((wxChar*)key, ((wxHtmlEntityInfo*)item)->name);
548}
549
2b5f62a0 550#if !wxUSE_UNICODE
daa616fc
VS
551wxChar wxHtmlEntitiesParser::GetCharForCode(unsigned code)
552{
2b5f62a0 553#if wxUSE_WCHAR_T
daa616fc
VS
554 char buf[2];
555 wchar_t wbuf[2];
556 wbuf[0] = (wchar_t)code;
557 wbuf[1] = 0;
558 wxMBConv *conv = m_conv ? m_conv : &wxConvLocal;
eaf1a1d9 559 if (conv->WC2MB(buf, wbuf, 2) == (size_t)-1)
daa616fc
VS
560 return '?';
561 return buf[0];
562#else
563 return (code < 256) ? (wxChar)code : '?';
564#endif
565}
2b5f62a0 566#endif
daa616fc
VS
567
568wxChar wxHtmlEntitiesParser::GetEntityChar(const wxString& entity)
569{
570 unsigned code = 0;
04dbb646 571
daa616fc
VS
572 if (entity[0] == wxT('#'))
573 {
574 const wxChar *ent_s = entity.c_str();
575 const wxChar *format;
04dbb646 576
daa616fc
VS
577 if (ent_s[1] == wxT('x') || ent_s[1] == wxT('X'))
578 {
579 format = wxT("%x");
580 ent_s++;
581 }
582 else
583 format = wxT("%u");
584 ent_s++;
585
586 if (wxSscanf(ent_s, format, &code) != 1)
587 code = 0;
588 }
589 else
590 {
591 static wxHtmlEntityInfo substitutions[] = {
592 { wxT("AElig"),198 },
593 { wxT("Aacute"),193 },
594 { wxT("Acirc"),194 },
595 { wxT("Agrave"),192 },
596 { wxT("Alpha"),913 },
597 { wxT("Aring"),197 },
598 { wxT("Atilde"),195 },
599 { wxT("Auml"),196 },
600 { wxT("Beta"),914 },
601 { wxT("Ccedil"),199 },
602 { wxT("Chi"),935 },
603 { wxT("Dagger"),8225 },
604 { wxT("Delta"),916 },
605 { wxT("ETH"),208 },
606 { wxT("Eacute"),201 },
607 { wxT("Ecirc"),202 },
608 { wxT("Egrave"),200 },
609 { wxT("Epsilon"),917 },
610 { wxT("Eta"),919 },
611 { wxT("Euml"),203 },
612 { wxT("Gamma"),915 },
613 { wxT("Iacute"),205 },
614 { wxT("Icirc"),206 },
615 { wxT("Igrave"),204 },
616 { wxT("Iota"),921 },
617 { wxT("Iuml"),207 },
618 { wxT("Kappa"),922 },
619 { wxT("Lambda"),923 },
620 { wxT("Mu"),924 },
621 { wxT("Ntilde"),209 },
622 { wxT("Nu"),925 },
623 { wxT("OElig"),338 },
624 { wxT("Oacute"),211 },
625 { wxT("Ocirc"),212 },
626 { wxT("Ograve"),210 },
627 { wxT("Omega"),937 },
628 { wxT("Omicron"),927 },
629 { wxT("Oslash"),216 },
630 { wxT("Otilde"),213 },
631 { wxT("Ouml"),214 },
632 { wxT("Phi"),934 },
633 { wxT("Pi"),928 },
634 { wxT("Prime"),8243 },
635 { wxT("Psi"),936 },
636 { wxT("Rho"),929 },
637 { wxT("Scaron"),352 },
638 { wxT("Sigma"),931 },
639 { wxT("THORN"),222 },
640 { wxT("Tau"),932 },
641 { wxT("Theta"),920 },
642 { wxT("Uacute"),218 },
643 { wxT("Ucirc"),219 },
644 { wxT("Ugrave"),217 },
645 { wxT("Upsilon"),933 },
646 { wxT("Uuml"),220 },
647 { wxT("Xi"),926 },
648 { wxT("Yacute"),221 },
649 { wxT("Yuml"),376 },
650 { wxT("Zeta"),918 },
651 { wxT("aacute"),225 },
652 { wxT("acirc"),226 },
653 { wxT("acute"),180 },
654 { wxT("aelig"),230 },
655 { wxT("agrave"),224 },
656 { wxT("alefsym"),8501 },
657 { wxT("alpha"),945 },
658 { wxT("amp"),38 },
659 { wxT("and"),8743 },
660 { wxT("ang"),8736 },
661 { wxT("aring"),229 },
662 { wxT("asymp"),8776 },
663 { wxT("atilde"),227 },
664 { wxT("auml"),228 },
665 { wxT("bdquo"),8222 },
666 { wxT("beta"),946 },
667 { wxT("brvbar"),166 },
668 { wxT("bull"),8226 },
669 { wxT("cap"),8745 },
670 { wxT("ccedil"),231 },
671 { wxT("cedil"),184 },
672 { wxT("cent"),162 },
673 { wxT("chi"),967 },
674 { wxT("circ"),710 },
675 { wxT("clubs"),9827 },
676 { wxT("cong"),8773 },
677 { wxT("copy"),169 },
678 { wxT("crarr"),8629 },
679 { wxT("cup"),8746 },
680 { wxT("curren"),164 },
681 { wxT("dArr"),8659 },
682 { wxT("dagger"),8224 },
683 { wxT("darr"),8595 },
684 { wxT("deg"),176 },
685 { wxT("delta"),948 },
686 { wxT("diams"),9830 },
687 { wxT("divide"),247 },
688 { wxT("eacute"),233 },
689 { wxT("ecirc"),234 },
690 { wxT("egrave"),232 },
691 { wxT("empty"),8709 },
692 { wxT("emsp"),8195 },
693 { wxT("ensp"),8194 },
694 { wxT("epsilon"),949 },
695 { wxT("equiv"),8801 },
696 { wxT("eta"),951 },
697 { wxT("eth"),240 },
698 { wxT("euml"),235 },
699 { wxT("euro"),8364 },
700 { wxT("exist"),8707 },
701 { wxT("fnof"),402 },
702 { wxT("forall"),8704 },
703 { wxT("frac12"),189 },
704 { wxT("frac14"),188 },
705 { wxT("frac34"),190 },
706 { wxT("frasl"),8260 },
707 { wxT("gamma"),947 },
708 { wxT("ge"),8805 },
709 { wxT("gt"),62 },
710 { wxT("hArr"),8660 },
711 { wxT("harr"),8596 },
712 { wxT("hearts"),9829 },
713 { wxT("hellip"),8230 },
714 { wxT("iacute"),237 },
715 { wxT("icirc"),238 },
716 { wxT("iexcl"),161 },
717 { wxT("igrave"),236 },
718 { wxT("image"),8465 },
719 { wxT("infin"),8734 },
720 { wxT("int"),8747 },
721 { wxT("iota"),953 },
722 { wxT("iquest"),191 },
723 { wxT("isin"),8712 },
724 { wxT("iuml"),239 },
725 { wxT("kappa"),954 },
726 { wxT("lArr"),8656 },
727 { wxT("lambda"),955 },
728 { wxT("lang"),9001 },
729 { wxT("laquo"),171 },
730 { wxT("larr"),8592 },
731 { wxT("lceil"),8968 },
732 { wxT("ldquo"),8220 },
733 { wxT("le"),8804 },
734 { wxT("lfloor"),8970 },
735 { wxT("lowast"),8727 },
736 { wxT("loz"),9674 },
737 { wxT("lrm"),8206 },
738 { wxT("lsaquo"),8249 },
739 { wxT("lsquo"),8216 },
740 { wxT("lt"),60 },
741 { wxT("macr"),175 },
742 { wxT("mdash"),8212 },
743 { wxT("micro"),181 },
744 { wxT("middot"),183 },
745 { wxT("minus"),8722 },
746 { wxT("mu"),956 },
747 { wxT("nabla"),8711 },
748 { wxT("nbsp"),160 },
749 { wxT("ndash"),8211 },
750 { wxT("ne"),8800 },
751 { wxT("ni"),8715 },
752 { wxT("not"),172 },
753 { wxT("notin"),8713 },
754 { wxT("nsub"),8836 },
755 { wxT("ntilde"),241 },
756 { wxT("nu"),957 },
757 { wxT("oacute"),243 },
758 { wxT("ocirc"),244 },
759 { wxT("oelig"),339 },
760 { wxT("ograve"),242 },
761 { wxT("oline"),8254 },
762 { wxT("omega"),969 },
763 { wxT("omicron"),959 },
764 { wxT("oplus"),8853 },
765 { wxT("or"),8744 },
766 { wxT("ordf"),170 },
767 { wxT("ordm"),186 },
768 { wxT("oslash"),248 },
769 { wxT("otilde"),245 },
770 { wxT("otimes"),8855 },
771 { wxT("ouml"),246 },
772 { wxT("para"),182 },
773 { wxT("part"),8706 },
774 { wxT("permil"),8240 },
775 { wxT("perp"),8869 },
776 { wxT("phi"),966 },
777 { wxT("pi"),960 },
778 { wxT("piv"),982 },
779 { wxT("plusmn"),177 },
780 { wxT("pound"),163 },
781 { wxT("prime"),8242 },
782 { wxT("prod"),8719 },
783 { wxT("prop"),8733 },
784 { wxT("psi"),968 },
785 { wxT("quot"),34 },
786 { wxT("rArr"),8658 },
787 { wxT("radic"),8730 },
788 { wxT("rang"),9002 },
789 { wxT("raquo"),187 },
790 { wxT("rarr"),8594 },
791 { wxT("rceil"),8969 },
792 { wxT("rdquo"),8221 },
793 { wxT("real"),8476 },
794 { wxT("reg"),174 },
795 { wxT("rfloor"),8971 },
796 { wxT("rho"),961 },
797 { wxT("rlm"),8207 },
798 { wxT("rsaquo"),8250 },
799 { wxT("rsquo"),8217 },
800 { wxT("sbquo"),8218 },
801 { wxT("scaron"),353 },
802 { wxT("sdot"),8901 },
803 { wxT("sect"),167 },
804 { wxT("shy"),173 },
805 { wxT("sigma"),963 },
806 { wxT("sigmaf"),962 },
807 { wxT("sim"),8764 },
808 { wxT("spades"),9824 },
809 { wxT("sub"),8834 },
810 { wxT("sube"),8838 },
811 { wxT("sum"),8721 },
812 { wxT("sup"),8835 },
813 { wxT("sup1"),185 },
814 { wxT("sup2"),178 },
815 { wxT("sup3"),179 },
816 { wxT("supe"),8839 },
817 { wxT("szlig"),223 },
818 { wxT("tau"),964 },
819 { wxT("there4"),8756 },
820 { wxT("theta"),952 },
821 { wxT("thetasym"),977 },
822 { wxT("thinsp"),8201 },
823 { wxT("thorn"),254 },
824 { wxT("tilde"),732 },
825 { wxT("times"),215 },
826 { wxT("trade"),8482 },
827 { wxT("uArr"),8657 },
828 { wxT("uacute"),250 },
829 { wxT("uarr"),8593 },
830 { wxT("ucirc"),251 },
831 { wxT("ugrave"),249 },
832 { wxT("uml"),168 },
833 { wxT("upsih"),978 },
834 { wxT("upsilon"),965 },
835 { wxT("uuml"),252 },
836 { wxT("weierp"),8472 },
837 { wxT("xi"),958 },
838 { wxT("yacute"),253 },
839 { wxT("yen"),165 },
840 { wxT("yuml"),255 },
841 { wxT("zeta"),950 },
842 { wxT("zwj"),8205 },
843 { wxT("zwnj"),8204 },
844 {NULL, 0}};
845 static size_t substitutions_cnt = 0;
04dbb646 846
daa616fc
VS
847 if (substitutions_cnt == 0)
848 while (substitutions[substitutions_cnt].code != 0)
849 substitutions_cnt++;
850
3919d530
JS
851 wxHtmlEntityInfo *info = NULL;
852#ifdef __WXWINCE__
853 // bsearch crashes under WinCE for some reason
854 size_t i;
855 for (i = 0; i < substitutions_cnt; i++)
856 {
857 if (entity == substitutions[i].name)
858 {
859 info = & substitutions[i];
860 break;
861 }
862 }
863#else
04dbb646 864 info = (wxHtmlEntityInfo*) bsearch(entity.c_str(), substitutions,
daa616fc
VS
865 substitutions_cnt,
866 sizeof(wxHtmlEntityInfo),
90350682 867 wxHtmlEntityCompare);
3919d530 868#endif
daa616fc
VS
869 if (info)
870 code = info->code;
871 }
04dbb646 872
daa616fc 873 if (code == 0)
470252df 874 return 0;
daa616fc
VS
875 else
876 return GetCharForCode(code);
877}
878
d1da8872 879wxFSFile *wxHtmlParser::OpenURL(wxHtmlURLType WXUNUSED(type),
6cc4e6b8
VS
880 const wxString& url) const
881{
e02ecf7c 882 return m_FS ? m_FS->OpenFile(url) : NULL;
d1da8872 883
6cc4e6b8
VS
884}
885
2b5f62a0
VZ
886
887//-----------------------------------------------------------------------------
888// wxHtmlParser::ExtractCharsetInformation
889//-----------------------------------------------------------------------------
890
891class wxMetaTagParser : public wxHtmlParser
892{
893public:
2eb10e2a
VZ
894 wxMetaTagParser() { }
895
2b5f62a0 896 wxObject* GetProduct() { return NULL; }
2eb10e2a 897
2b5f62a0
VZ
898protected:
899 virtual void AddText(const wxChar* WXUNUSED(txt)) {}
2eb10e2a
VZ
900
901 DECLARE_NO_COPY_CLASS(wxMetaTagParser)
2b5f62a0
VZ
902};
903
904class wxMetaTagHandler : public wxHtmlTagHandler
905{
906public:
907 wxMetaTagHandler(wxString *retval) : wxHtmlTagHandler(), m_retval(retval) {}
908 wxString GetSupportedTags() { return wxT("META,BODY"); }
909 bool HandleTag(const wxHtmlTag& tag);
910
911private:
912 wxString *m_retval;
2eb10e2a
VZ
913
914 DECLARE_NO_COPY_CLASS(wxMetaTagHandler)
2b5f62a0
VZ
915};
916
917bool wxMetaTagHandler::HandleTag(const wxHtmlTag& tag)
918{
919 if (tag.GetName() == _T("BODY"))
920 {
921 m_Parser->StopParsing();
d1da8872 922 return false;
2b5f62a0
VZ
923 }
924
925 if (tag.HasParam(_T("HTTP-EQUIV")) &&
13fd234c 926 tag.GetParam(_T("HTTP-EQUIV")).IsSameAs(_T("Content-Type"), false) &&
2b5f62a0
VZ
927 tag.HasParam(_T("CONTENT")))
928 {
5af11a94 929 wxString content = tag.GetParam(_T("CONTENT")).Lower();
2b5f62a0
VZ
930 if (content.Left(19) == _T("text/html; charset="))
931 {
932 *m_retval = content.Mid(19);
933 m_Parser->StopParsing();
934 }
935 }
d1da8872 936 return false;
2b5f62a0
VZ
937}
938
939
940/*static*/
941wxString wxHtmlParser::ExtractCharsetInformation(const wxString& markup)
942{
943 wxString charset;
e7274ba2
WS
944 wxMetaTagParser *parser = new wxMetaTagParser();
945 if(parser)
946 {
947 parser->AddTagHandler(new wxMetaTagHandler(&charset));
948 parser->Parse(markup);
949 delete parser;
950 }
2b5f62a0
VZ
951 return charset;
952}
953
daa616fc 954#endif