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