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