]>
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 | { | |
902725ee WS |
312 | if (m_CurTag->HasEnding()) |
313 | begin_pos = m_CurTag->GetEndPos2(); | |
314 | else | |
315 | begin_pos = m_CurTag->GetBeginPos(); | |
211dfedd VS |
316 | wxHtmlTag *t = m_CurTag; |
317 | m_CurTag = m_CurTag->GetNextTag(); | |
318 | AddTag(*t); | |
2b5f62a0 VZ |
319 | if (m_stopParsing) |
320 | return; | |
211dfedd VS |
321 | } |
322 | else break; | |
5526e819 VS |
323 | } |
324 | } | |
325 | ||
5526e819 VS |
326 | void wxHtmlParser::AddTag(const wxHtmlTag& tag) |
327 | { | |
328 | wxHtmlTagHandler *h; | |
d1da8872 | 329 | bool inner = false; |
5526e819 VS |
330 | |
331 | h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName()); | |
332 | if (h) | |
2b5f62a0 | 333 | { |
4f9297b0 | 334 | inner = h->HandleTag(tag); |
2b5f62a0 VZ |
335 | if (m_stopParsing) |
336 | return; | |
337 | } | |
04dbb646 | 338 | if (!inner) |
4f9297b0 | 339 | { |
5526e819 VS |
340 | if (tag.HasEnding()) |
341 | DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); | |
342 | } | |
343 | } | |
344 | ||
5526e819 VS |
345 | void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler) |
346 | { | |
4f9297b0 | 347 | wxString s(handler->GetSupportedTags()); |
211dfedd | 348 | wxStringTokenizer tokenizer(s, wxT(", ")); |
5526e819 | 349 | |
5526e819 | 350 | while (tokenizer.HasMoreTokens()) |
470252df | 351 | m_HandlersHash.Put(tokenizer.GetNextToken(), handler); |
5526e819 VS |
352 | |
353 | if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND) | |
354 | m_HandlersList.Append(handler); | |
355 | ||
4f9297b0 | 356 | handler->SetParser(this); |
5526e819 VS |
357 | } |
358 | ||
a7a4d01b VS |
359 | void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags) |
360 | { | |
211dfedd | 361 | wxStringTokenizer tokenizer(tags, wxT(", ")); |
a7a4d01b VS |
362 | wxString key; |
363 | ||
04dbb646 | 364 | if (m_HandlersStack == NULL) |
4f9297b0 | 365 | { |
a7a4d01b | 366 | m_HandlersStack = new wxList; |
a7a4d01b VS |
367 | } |
368 | ||
222ed1d6 | 369 | m_HandlersStack->Insert((wxObject*)new wxHashTable(m_HandlersHash)); |
a7a4d01b | 370 | |
04dbb646 | 371 | while (tokenizer.HasMoreTokens()) |
4f9297b0 | 372 | { |
470252df | 373 | key = tokenizer.GetNextToken(); |
a7a4d01b VS |
374 | m_HandlersHash.Delete(key); |
375 | m_HandlersHash.Put(key, handler); | |
376 | } | |
377 | } | |
378 | ||
a7a4d01b VS |
379 | void wxHtmlParser::PopTagHandler() |
380 | { | |
222ed1d6 | 381 | wxList::compatibility_iterator first; |
04dbb646 | 382 | |
dfa4a244 | 383 | if ( !m_HandlersStack || |
28b4db7f VZ |
384 | #if wxUSE_STL |
385 | !(first = m_HandlersStack->GetFirst()) | |
386 | #else // !wxUSE_STL | |
387 | ((first = m_HandlersStack->GetFirst()) == NULL) | |
388 | #endif // wxUSE_STL/!wxUSE_STL | |
389 | ) | |
f3c82859 VS |
390 | { |
391 | wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack.")); | |
392 | return; | |
393 | } | |
4f9297b0 | 394 | m_HandlersHash = *((wxHashTable*) first->GetData()); |
222ed1d6 MB |
395 | delete (wxHashTable*) first->GetData(); |
396 | m_HandlersStack->Erase(first); | |
a7a4d01b VS |
397 | } |
398 | ||
211dfedd VS |
399 | void wxHtmlParser::SetSourceAndSaveState(const wxString& src) |
400 | { | |
401 | wxHtmlParserState *s = new wxHtmlParserState; | |
402 | ||
403 | s->m_curTag = m_CurTag; | |
404 | s->m_tags = m_Tags; | |
405 | s->m_textPieces = m_TextPieces; | |
406 | s->m_curTextPiece = m_CurTextPiece; | |
407 | s->m_source = m_Source; | |
408 | ||
409 | s->m_nextState = m_SavedStates; | |
410 | m_SavedStates = s; | |
411 | ||
412 | m_CurTag = NULL; | |
413 | m_Tags = NULL; | |
414 | m_TextPieces = NULL; | |
415 | m_CurTextPiece = 0; | |
416 | m_Source = wxEmptyString; | |
d699f48b | 417 | |
211dfedd VS |
418 | SetSource(src); |
419 | } | |
420 | ||
421 | bool wxHtmlParser::RestoreState() | |
422 | { | |
d1da8872 | 423 | if (!m_SavedStates) return false; |
d699f48b | 424 | |
0beefa20 VS |
425 | DestroyDOMTree(); |
426 | ||
211dfedd VS |
427 | wxHtmlParserState *s = m_SavedStates; |
428 | m_SavedStates = s->m_nextState; | |
d699f48b | 429 | |
211dfedd VS |
430 | m_CurTag = s->m_curTag; |
431 | m_Tags = s->m_tags; | |
432 | m_TextPieces = s->m_textPieces; | |
433 | m_CurTextPiece = s->m_curTextPiece; | |
434 | m_Source = s->m_source; | |
d699f48b | 435 | |
211dfedd | 436 | delete s; |
d1da8872 | 437 | return true; |
211dfedd VS |
438 | } |
439 | ||
5526e819 VS |
440 | //----------------------------------------------------------------------------- |
441 | // wxHtmlTagHandler | |
442 | //----------------------------------------------------------------------------- | |
443 | ||
444 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject) | |
daa616fc VS |
445 | |
446 | ||
447 | //----------------------------------------------------------------------------- | |
448 | // wxHtmlEntitiesParser | |
449 | //----------------------------------------------------------------------------- | |
450 | ||
451 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser,wxObject) | |
452 | ||
453 | wxHtmlEntitiesParser::wxHtmlEntitiesParser() | |
454 | #if wxUSE_WCHAR_T && !wxUSE_UNICODE | |
455 | : m_conv(NULL), m_encoding(wxFONTENCODING_SYSTEM) | |
223d09f6 | 456 | #endif |
daa616fc VS |
457 | { |
458 | } | |
459 | ||
460 | wxHtmlEntitiesParser::~wxHtmlEntitiesParser() | |
461 | { | |
5438a566 | 462 | #if wxUSE_WCHAR_T && !wxUSE_UNICODE |
daa616fc | 463 | delete m_conv; |
5438a566 | 464 | #endif |
daa616fc | 465 | } |
5526e819 | 466 | |
daa616fc VS |
467 | void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding) |
468 | { | |
469 | #if wxUSE_WCHAR_T && !wxUSE_UNICODE | |
2b5f62a0 VZ |
470 | if (encoding == m_encoding) |
471 | return; | |
472 | ||
daa616fc | 473 | delete m_conv; |
2b5f62a0 | 474 | |
daa616fc | 475 | m_encoding = encoding; |
2b5f62a0 VZ |
476 | if (m_encoding == wxFONTENCODING_SYSTEM) |
477 | m_conv = NULL; | |
478 | else | |
daa616fc | 479 | m_conv = new wxCSConv(wxFontMapper::GetEncodingName(m_encoding)); |
d699f48b KB |
480 | #else |
481 | (void) encoding; | |
daa616fc VS |
482 | #endif |
483 | } | |
484 | ||
485 | wxString wxHtmlEntitiesParser::Parse(const wxString& input) | |
486 | { | |
487 | const wxChar *c, *last; | |
488 | const wxChar *in_str = input.c_str(); | |
489 | wxString output; | |
d1da8872 | 490 | |
470252df | 491 | output.reserve(input.length()); |
04dbb646 | 492 | |
daa616fc VS |
493 | for (c = in_str, last = in_str; *c != wxT('\0'); c++) |
494 | { | |
495 | if (*c == wxT('&')) | |
496 | { | |
497 | if (c - last > 0) | |
498 | output.append(last, c - last); | |
499 | if (++c == wxT('\0')) break; | |
d1da8872 | 500 | |
daa616fc VS |
501 | wxString entity; |
502 | const wxChar *ent_s = c; | |
470252df | 503 | wxChar entity_char; |
d1da8872 | 504 | |
daa616fc VS |
505 | for (; (*c >= wxT('a') && *c <= wxT('z')) || |
506 | (*c >= wxT('A') && *c <= wxT('Z')) || | |
507 | (*c >= wxT('0') && *c <= wxT('9')) || | |
508 | *c == wxT('_') || *c == wxT('#'); c++) {} | |
509 | entity.append(ent_s, c - ent_s); | |
211dfedd VS |
510 | if (*c != wxT(';')) c--; |
511 | last = c+1; | |
470252df VS |
512 | entity_char = GetEntityChar(entity); |
513 | if (entity_char) | |
514 | output << entity_char; | |
515 | else | |
516 | { | |
517 | output.append(ent_s-1, c-ent_s+2); | |
25271309 VZ |
518 | wxLogTrace(wxTRACE_HTML_DEBUG, |
519 | wxT("Unrecognized HTML entity: '%s'"), | |
520 | entity.c_str()); | |
470252df | 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 | ||
d1da8872 | 856 | wxFSFile *wxHtmlParser::OpenURL(wxHtmlURLType WXUNUSED(type), |
6cc4e6b8 VS |
857 | const wxString& url) const |
858 | { | |
e02ecf7c | 859 | return m_FS ? m_FS->OpenFile(url) : NULL; |
d1da8872 | 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(); | |
d1da8872 | 899 | return false; |
2b5f62a0 VZ |
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 | } | |
d1da8872 | 913 | return false; |
2b5f62a0 VZ |
914 | } |
915 | ||
916 | ||
917 | /*static*/ | |
918 | wxString wxHtmlParser::ExtractCharsetInformation(const wxString& markup) | |
919 | { | |
920 | wxString charset; | |
e7274ba2 WS |
921 | wxMetaTagParser *parser = new wxMetaTagParser(); |
922 | if(parser) | |
923 | { | |
924 | parser->AddTagHandler(new wxMetaTagHandler(&charset)); | |
925 | parser->Parse(markup); | |
926 | delete parser; | |
927 | } | |
2b5f62a0 VZ |
928 | return charset; |
929 | } | |
930 | ||
daa616fc | 931 | #endif |