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