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