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