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