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