]> git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/htmlparser/htmlpars.cpp
Don't reset bullet number and outline number when applying style sheet.
[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_UNICODE
463 : m_conv(NULL), m_encoding(wxFONTENCODING_SYSTEM)
464 #endif
465 {
466 }
467
468 wx28HtmlEntitiesParser::~wx28HtmlEntitiesParser()
469 {
470 #if !wxUSE_UNICODE
471 delete m_conv;
472 #endif
473 }
474
475 void wx28HtmlEntitiesParser::SetEncoding(wxFontEncoding encoding)
476 {
477 #if !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 char buf[2];
556 wchar_t wbuf[2];
557 wbuf[0] = (wchar_t)code;
558 wbuf[1] = 0;
559 wxMBConv *conv = m_conv ? m_conv : &wxConvLocal;
560 if (conv->WC2MB(buf, wbuf, 2) == (size_t)-1)
561 return '?';
562 return buf[0];
563 }
564 #endif
565
566 wxChar wx28HtmlEntitiesParser::GetEntityChar(const wxString& entity)
567 {
568 unsigned code = 0;
569
570 if (entity[0] == wxT('#'))
571 {
572 const wxChar *ent_s = entity.c_str();
573 const wxChar *format;
574
575 if (ent_s[1] == wxT('x') || ent_s[1] == wxT('X'))
576 {
577 format = wxT("%x");
578 ent_s++;
579 }
580 else
581 format = wxT("%u");
582 ent_s++;
583
584 if (wxSscanf(ent_s, format, &code) != 1)
585 code = 0;
586 }
587 else
588 {
589 static wx28HtmlEntityInfo substitutions[] = {
590 { wxT("AElig"),198 },
591 { wxT("Aacute"),193 },
592 { wxT("Acirc"),194 },
593 { wxT("Agrave"),192 },
594 { wxT("Alpha"),913 },
595 { wxT("Aring"),197 },
596 { wxT("Atilde"),195 },
597 { wxT("Auml"),196 },
598 { wxT("Beta"),914 },
599 { wxT("Ccedil"),199 },
600 { wxT("Chi"),935 },
601 { wxT("Dagger"),8225 },
602 { wxT("Delta"),916 },
603 { wxT("ETH"),208 },
604 { wxT("Eacute"),201 },
605 { wxT("Ecirc"),202 },
606 { wxT("Egrave"),200 },
607 { wxT("Epsilon"),917 },
608 { wxT("Eta"),919 },
609 { wxT("Euml"),203 },
610 { wxT("Gamma"),915 },
611 { wxT("Iacute"),205 },
612 { wxT("Icirc"),206 },
613 { wxT("Igrave"),204 },
614 { wxT("Iota"),921 },
615 { wxT("Iuml"),207 },
616 { wxT("Kappa"),922 },
617 { wxT("Lambda"),923 },
618 { wxT("Mu"),924 },
619 { wxT("Ntilde"),209 },
620 { wxT("Nu"),925 },
621 { wxT("OElig"),338 },
622 { wxT("Oacute"),211 },
623 { wxT("Ocirc"),212 },
624 { wxT("Ograve"),210 },
625 { wxT("Omega"),937 },
626 { wxT("Omicron"),927 },
627 { wxT("Oslash"),216 },
628 { wxT("Otilde"),213 },
629 { wxT("Ouml"),214 },
630 { wxT("Phi"),934 },
631 { wxT("Pi"),928 },
632 { wxT("Prime"),8243 },
633 { wxT("Psi"),936 },
634 { wxT("Rho"),929 },
635 { wxT("Scaron"),352 },
636 { wxT("Sigma"),931 },
637 { wxT("THORN"),222 },
638 { wxT("Tau"),932 },
639 { wxT("Theta"),920 },
640 { wxT("Uacute"),218 },
641 { wxT("Ucirc"),219 },
642 { wxT("Ugrave"),217 },
643 { wxT("Upsilon"),933 },
644 { wxT("Uuml"),220 },
645 { wxT("Xi"),926 },
646 { wxT("Yacute"),221 },
647 { wxT("Yuml"),376 },
648 { wxT("Zeta"),918 },
649 { wxT("aacute"),225 },
650 { wxT("acirc"),226 },
651 { wxT("acute"),180 },
652 { wxT("aelig"),230 },
653 { wxT("agrave"),224 },
654 { wxT("alefsym"),8501 },
655 { wxT("alpha"),945 },
656 { wxT("amp"),38 },
657 { wxT("and"),8743 },
658 { wxT("ang"),8736 },
659 { wxT("aring"),229 },
660 { wxT("asymp"),8776 },
661 { wxT("atilde"),227 },
662 { wxT("auml"),228 },
663 { wxT("bdquo"),8222 },
664 { wxT("beta"),946 },
665 { wxT("brvbar"),166 },
666 { wxT("bull"),8226 },
667 { wxT("cap"),8745 },
668 { wxT("ccedil"),231 },
669 { wxT("cedil"),184 },
670 { wxT("cent"),162 },
671 { wxT("chi"),967 },
672 { wxT("circ"),710 },
673 { wxT("clubs"),9827 },
674 { wxT("cong"),8773 },
675 { wxT("copy"),169 },
676 { wxT("crarr"),8629 },
677 { wxT("cup"),8746 },
678 { wxT("curren"),164 },
679 { wxT("dArr"),8659 },
680 { wxT("dagger"),8224 },
681 { wxT("darr"),8595 },
682 { wxT("deg"),176 },
683 { wxT("delta"),948 },
684 { wxT("diams"),9830 },
685 { wxT("divide"),247 },
686 { wxT("eacute"),233 },
687 { wxT("ecirc"),234 },
688 { wxT("egrave"),232 },
689 { wxT("empty"),8709 },
690 { wxT("emsp"),8195 },
691 { wxT("ensp"),8194 },
692 { wxT("epsilon"),949 },
693 { wxT("equiv"),8801 },
694 { wxT("eta"),951 },
695 { wxT("eth"),240 },
696 { wxT("euml"),235 },
697 { wxT("euro"),8364 },
698 { wxT("exist"),8707 },
699 { wxT("fnof"),402 },
700 { wxT("forall"),8704 },
701 { wxT("frac12"),189 },
702 { wxT("frac14"),188 },
703 { wxT("frac34"),190 },
704 { wxT("frasl"),8260 },
705 { wxT("gamma"),947 },
706 { wxT("ge"),8805 },
707 { wxT("gt"),62 },
708 { wxT("hArr"),8660 },
709 { wxT("harr"),8596 },
710 { wxT("hearts"),9829 },
711 { wxT("hellip"),8230 },
712 { wxT("iacute"),237 },
713 { wxT("icirc"),238 },
714 { wxT("iexcl"),161 },
715 { wxT("igrave"),236 },
716 { wxT("image"),8465 },
717 { wxT("infin"),8734 },
718 { wxT("int"),8747 },
719 { wxT("iota"),953 },
720 { wxT("iquest"),191 },
721 { wxT("isin"),8712 },
722 { wxT("iuml"),239 },
723 { wxT("kappa"),954 },
724 { wxT("lArr"),8656 },
725 { wxT("lambda"),955 },
726 { wxT("lang"),9001 },
727 { wxT("laquo"),171 },
728 { wxT("larr"),8592 },
729 { wxT("lceil"),8968 },
730 { wxT("ldquo"),8220 },
731 { wxT("le"),8804 },
732 { wxT("lfloor"),8970 },
733 { wxT("lowast"),8727 },
734 { wxT("loz"),9674 },
735 { wxT("lrm"),8206 },
736 { wxT("lsaquo"),8249 },
737 { wxT("lsquo"),8216 },
738 { wxT("lt"),60 },
739 { wxT("macr"),175 },
740 { wxT("mdash"),8212 },
741 { wxT("micro"),181 },
742 { wxT("middot"),183 },
743 { wxT("minus"),8722 },
744 { wxT("mu"),956 },
745 { wxT("nabla"),8711 },
746 { wxT("nbsp"),160 },
747 { wxT("ndash"),8211 },
748 { wxT("ne"),8800 },
749 { wxT("ni"),8715 },
750 { wxT("not"),172 },
751 { wxT("notin"),8713 },
752 { wxT("nsub"),8836 },
753 { wxT("ntilde"),241 },
754 { wxT("nu"),957 },
755 { wxT("oacute"),243 },
756 { wxT("ocirc"),244 },
757 { wxT("oelig"),339 },
758 { wxT("ograve"),242 },
759 { wxT("oline"),8254 },
760 { wxT("omega"),969 },
761 { wxT("omicron"),959 },
762 { wxT("oplus"),8853 },
763 { wxT("or"),8744 },
764 { wxT("ordf"),170 },
765 { wxT("ordm"),186 },
766 { wxT("oslash"),248 },
767 { wxT("otilde"),245 },
768 { wxT("otimes"),8855 },
769 { wxT("ouml"),246 },
770 { wxT("para"),182 },
771 { wxT("part"),8706 },
772 { wxT("permil"),8240 },
773 { wxT("perp"),8869 },
774 { wxT("phi"),966 },
775 { wxT("pi"),960 },
776 { wxT("piv"),982 },
777 { wxT("plusmn"),177 },
778 { wxT("pound"),163 },
779 { wxT("prime"),8242 },
780 { wxT("prod"),8719 },
781 { wxT("prop"),8733 },
782 { wxT("psi"),968 },
783 { wxT("quot"),34 },
784 { wxT("rArr"),8658 },
785 { wxT("radic"),8730 },
786 { wxT("rang"),9002 },
787 { wxT("raquo"),187 },
788 { wxT("rarr"),8594 },
789 { wxT("rceil"),8969 },
790 { wxT("rdquo"),8221 },
791 { wxT("real"),8476 },
792 { wxT("reg"),174 },
793 { wxT("rfloor"),8971 },
794 { wxT("rho"),961 },
795 { wxT("rlm"),8207 },
796 { wxT("rsaquo"),8250 },
797 { wxT("rsquo"),8217 },
798 { wxT("sbquo"),8218 },
799 { wxT("scaron"),353 },
800 { wxT("sdot"),8901 },
801 { wxT("sect"),167 },
802 { wxT("shy"),173 },
803 { wxT("sigma"),963 },
804 { wxT("sigmaf"),962 },
805 { wxT("sim"),8764 },
806 { wxT("spades"),9824 },
807 { wxT("sub"),8834 },
808 { wxT("sube"),8838 },
809 { wxT("sum"),8721 },
810 { wxT("sup"),8835 },
811 { wxT("sup1"),185 },
812 { wxT("sup2"),178 },
813 { wxT("sup3"),179 },
814 { wxT("supe"),8839 },
815 { wxT("szlig"),223 },
816 { wxT("tau"),964 },
817 { wxT("there4"),8756 },
818 { wxT("theta"),952 },
819 { wxT("thetasym"),977 },
820 { wxT("thinsp"),8201 },
821 { wxT("thorn"),254 },
822 { wxT("tilde"),732 },
823 { wxT("times"),215 },
824 { wxT("trade"),8482 },
825 { wxT("uArr"),8657 },
826 { wxT("uacute"),250 },
827 { wxT("uarr"),8593 },
828 { wxT("ucirc"),251 },
829 { wxT("ugrave"),249 },
830 { wxT("uml"),168 },
831 { wxT("upsih"),978 },
832 { wxT("upsilon"),965 },
833 { wxT("uuml"),252 },
834 { wxT("weierp"),8472 },
835 { wxT("xi"),958 },
836 { wxT("yacute"),253 },
837 { wxT("yen"),165 },
838 { wxT("yuml"),255 },
839 { wxT("zeta"),950 },
840 { wxT("zwj"),8205 },
841 { wxT("zwnj"),8204 },
842 {NULL, 0}};
843 static size_t substitutions_cnt = 0;
844
845 if (substitutions_cnt == 0)
846 while (substitutions[substitutions_cnt].code != 0)
847 substitutions_cnt++;
848
849 wx28HtmlEntityInfo *info = NULL;
850 #ifdef __WXWINCE__
851 // bsearch crashes under WinCE for some reason
852 size_t i;
853 for (i = 0; i < substitutions_cnt; i++)
854 {
855 if (entity == substitutions[i].name)
856 {
857 info = & substitutions[i];
858 break;
859 }
860 }
861 #else
862 info = (wx28HtmlEntityInfo*) bsearch(entity.c_str(), substitutions,
863 substitutions_cnt,
864 sizeof(wx28HtmlEntityInfo),
865 wx28HtmlEntityCompare);
866 #endif
867 if (info)
868 code = info->code;
869 }
870
871 if (code == 0)
872 return 0;
873 else
874 return GetCharForCode(code);
875 }
876
877 wxFSFile *wx28HtmlParser::OpenURL(wx28HtmlURLType WXUNUSED(type),
878 const wxString& url) const
879 {
880 return m_FS ? m_FS->OpenFile(url) : NULL;
881
882 }
883
884
885 //-----------------------------------------------------------------------------
886 // wx28HtmlParser::ExtractCharsetInformation
887 //-----------------------------------------------------------------------------
888
889 class wxMetaTagParser : public wx28HtmlParser
890 {
891 public:
892 wxMetaTagParser() { }
893
894 wxObject* GetProduct() { return NULL; }
895
896 protected:
897 virtual void AddText(const wxChar* WXUNUSED(txt)) {}
898
899 DECLARE_NO_COPY_CLASS(wxMetaTagParser)
900 };
901
902 class wxMetaTagHandler : public wx28HtmlTagHandler
903 {
904 public:
905 wxMetaTagHandler(wxString *retval) : wx28HtmlTagHandler(), m_retval(retval) {}
906 wxString GetSupportedTags() { return wxT("META,BODY"); }
907 bool HandleTag(const wx28HtmlTag& tag);
908
909 private:
910 wxString *m_retval;
911
912 DECLARE_NO_COPY_CLASS(wxMetaTagHandler)
913 };
914
915 bool wxMetaTagHandler::HandleTag(const wx28HtmlTag& tag)
916 {
917 if (tag.GetName() == wxT("BODY"))
918 {
919 m_Parser->StopParsing();
920 return false;
921 }
922
923 if (tag.HasParam(wxT("HTTP-EQUIV")) &&
924 tag.GetParam(wxT("HTTP-EQUIV")).IsSameAs(wxT("Content-Type"), false) &&
925 tag.HasParam(wxT("CONTENT")))
926 {
927 wxString content = tag.GetParam(wxT("CONTENT")).Lower();
928 if (content.Left(19) == wxT("text/html; charset="))
929 {
930 *m_retval = content.Mid(19);
931 m_Parser->StopParsing();
932 }
933 }
934 return false;
935 }
936
937
938 /*static*/
939 wxString wx28HtmlParser::ExtractCharsetInformation(const wxString& markup)
940 {
941 wxString charset;
942 wxMetaTagParser *parser = new wxMetaTagParser();
943 if(parser)
944 {
945 parser->AddTagHandler(new wxMetaTagHandler(&charset));
946 parser->Parse(markup);
947 delete parser;
948 }
949 return charset;
950 }