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