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