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