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