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