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