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