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