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