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