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