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