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