]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: htmltag.cpp | |
3 | // Purpose: wxHtmlTag class (represents single tag) | |
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 | ||
3096bd2f | 10 | #include "wx/wxprec.h" |
5526e819 VS |
11 | |
12 | #include "wx/defs.h" | |
13 | #if wxUSE_HTML | |
14 | ||
2b5f62a0 | 15 | #ifdef __BORLANDC__ |
5526e819 VS |
16 | #pragma hdrstop |
17 | #endif | |
18 | ||
19 | #ifndef WXPRECOMP | |
5526e819 VS |
20 | #endif |
21 | ||
69941f05 | 22 | #include "wx/html/htmltag.h" |
daa616fc | 23 | #include "wx/html/htmlpars.h" |
fc1f2125 | 24 | #include "wx/colour.h" |
7e1e0960 | 25 | #include <stdio.h> // for vsscanf |
5526e819 VS |
26 | #include <stdarg.h> |
27 | ||
28 | ||
5526e819 VS |
29 | //----------------------------------------------------------------------------- |
30 | // wxHtmlTagsCache | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
5e8e25e7 VS |
33 | struct wxHtmlCacheItem |
34 | { | |
35 | // this is "pos" value passed to wxHtmlTag's constructor. | |
36 | // it is position of '<' character of the tag | |
37 | int Key; | |
38 | ||
39 | // end positions for the tag: | |
40 | // end1 is '<' of ending tag, | |
41 | // end2 is '>' or both are | |
42 | // -1 if there is no ending tag for this one... | |
43 | // or -2 if this is ending tag </...> | |
44 | int End1, End2; | |
45 | ||
46 | // name of this tag | |
47 | wxChar *Name; | |
48 | }; | |
49 | ||
50 | ||
5526e819 VS |
51 | IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject) |
52 | ||
53 | #define CACHE_INCREMENT 64 | |
54 | ||
07cc7ddc | 55 | bool wxIsCDATAElement(const wxChar *tag) |
7c6cd4a8 VS |
56 | { |
57 | return (wxStrcmp(tag, _T("SCRIPT")) == 0) || | |
58 | (wxStrcmp(tag, _T("STYLE")) == 0); | |
59 | } | |
60 | ||
5526e819 VS |
61 | wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) |
62 | { | |
66a77a74 | 63 | const wxChar *src = source.c_str(); |
5526e819 | 64 | int lng = source.Length(); |
8cd82622 | 65 | wxChar tagBuffer[256]; |
5526e819 VS |
66 | |
67 | m_Cache = NULL; | |
68 | m_CacheSize = 0; | |
69 | m_CachePos = 0; | |
70 | ||
14d36de8 | 71 | int pos = 0; |
8cd82622 | 72 | while (pos < lng) |
4f9297b0 VS |
73 | { |
74 | if (src[pos] == wxT('<')) // tag found: | |
a914db0f | 75 | { |
5526e819 | 76 | if (m_CacheSize % CACHE_INCREMENT == 0) |
5e8e25e7 | 77 | m_Cache = (wxHtmlCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(wxHtmlCacheItem)); |
999836aa VZ |
78 | int tg = m_CacheSize++; |
79 | int stpos = pos++; | |
80 | m_Cache[tg].Key = stpos; | |
8cd82622 | 81 | |
4f22f506 | 82 | int i; |
8cd82622 | 83 | for ( i = 0; |
4f22f506 | 84 | pos < lng && i < (int)WXSIZEOF(tagBuffer) - 1 && |
8cd82622 VZ |
85 | src[pos] != wxT('>') && !wxIsspace(src[pos]); |
86 | i++, pos++ ) | |
a914db0f | 87 | { |
42841dfc | 88 | tagBuffer[i] = (wxChar)wxToupper(src[pos]); |
5526e819 | 89 | } |
8cd82622 VZ |
90 | tagBuffer[i] = _T('\0'); |
91 | ||
66a77a74 | 92 | m_Cache[tg].Name = new wxChar[i+1]; |
8cd82622 | 93 | memcpy(m_Cache[tg].Name, tagBuffer, (i+1)*sizeof(wxChar)); |
5526e819 | 94 | |
15db3cf5 | 95 | while (pos < lng && src[pos] != wxT('>')) pos++; |
5526e819 | 96 | |
4f9297b0 | 97 | if (src[stpos+1] == wxT('/')) // ending tag: |
a914db0f | 98 | { |
5526e819 VS |
99 | m_Cache[tg].End1 = m_Cache[tg].End2 = -2; |
100 | // find matching begin tag: | |
101 | for (i = tg; i >= 0; i--) | |
8cd82622 | 102 | if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, tagBuffer+1) == 0)) |
a914db0f | 103 | { |
5526e819 VS |
104 | m_Cache[i].End1 = stpos; |
105 | m_Cache[i].End2 = pos + 1; | |
106 | break; | |
107 | } | |
108 | } | |
8cd82622 | 109 | else |
a914db0f | 110 | { |
5526e819 | 111 | m_Cache[tg].End1 = m_Cache[tg].End2 = -1; |
7448de8d | 112 | |
7c6cd4a8 VS |
113 | if (wxIsCDATAElement(tagBuffer)) |
114 | { | |
313ffa19 VS |
115 | // store the orig pos in case we are missing the closing |
116 | // tag (see below) | |
7448de8d | 117 | wxInt32 old_pos = pos; |
313ffa19 | 118 | bool foundCloseTag = false; |
7448de8d | 119 | |
7c6cd4a8 VS |
120 | // find next matching tag |
121 | int tag_len = wxStrlen(tagBuffer); | |
122 | while (pos < lng) | |
123 | { | |
124 | // find the ending tag | |
125 | while (pos + 1 < lng && | |
126 | (src[pos] != '<' || src[pos+1] != '/')) | |
127 | ++pos; | |
128 | if (src[pos] == '<') | |
129 | ++pos; | |
d1da8872 | 130 | |
7c6cd4a8 VS |
131 | // see if it matches |
132 | int match_pos = 0; | |
133 | while (pos < lng && match_pos < tag_len && src[pos] != '>' && src[pos] != '<') { | |
5447d1b4 VZ |
134 | // cast to wxChar needed to suppress warning in |
135 | // Unicode build | |
136 | if ((wxChar)wxToupper(src[pos]) == tagBuffer[match_pos]) { | |
7c6cd4a8 | 137 | ++match_pos; |
d1da8872 | 138 | } |
7c6cd4a8 VS |
139 | else if (src[pos] == wxT(' ') || src[pos] == wxT('\n') || |
140 | src[pos] == wxT('\r') || src[pos] == wxT('\t')) { | |
141 | // need to skip over these | |
142 | } | |
143 | else { | |
144 | match_pos = 0; | |
145 | } | |
146 | ++pos; | |
147 | } | |
148 | ||
149 | // found a match | |
7448de8d | 150 | if (match_pos == tag_len) |
313ffa19 | 151 | { |
b5d464b9 | 152 | pos = pos - tag_len - 3; |
313ffa19 | 153 | foundCloseTag = true; |
7c6cd4a8 VS |
154 | break; |
155 | } | |
313ffa19 VS |
156 | else // keep looking for the closing tag |
157 | { | |
7c6cd4a8 VS |
158 | ++pos; |
159 | } | |
160 | } | |
313ffa19 VS |
161 | if (!foundCloseTag) |
162 | { | |
163 | // we didn't find closing tag; this means the markup | |
164 | // is incorrect and the best thing we can do is to | |
165 | // ignore the unclosed tag and continue parsing as if | |
166 | // it didn't exist: | |
167 | pos = old_pos; | |
168 | } | |
7c6cd4a8 | 169 | } |
5526e819 VS |
170 | } |
171 | } | |
172 | ||
173 | pos++; | |
174 | } | |
175 | ||
176 | // ok, we're done, now we'll free .Name members of cache - we don't need it anymore: | |
14d36de8 | 177 | for (int i = 0; i < m_CacheSize; i++) |
4f9297b0 | 178 | { |
2776d7c3 | 179 | delete[] m_Cache[i].Name; |
5526e819 VS |
180 | m_Cache[i].Name = NULL; |
181 | } | |
182 | } | |
183 | ||
5526e819 VS |
184 | void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2) |
185 | { | |
186 | if (m_Cache == NULL) return; | |
8cd82622 | 187 | if (m_Cache[m_CachePos].Key != at) |
4f9297b0 | 188 | { |
5526e819 | 189 | int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1; |
8cd82622 VZ |
190 | do |
191 | { | |
10b9be32 VZ |
192 | if ( m_CachePos < 0 || m_CachePos == m_CacheSize ) |
193 | { | |
194 | // something is very wrong with HTML, give up by returning an | |
195 | // impossibly large value which is going to be ignored by the | |
196 | // caller | |
197 | *end1 = | |
198 | *end2 = INT_MAX; | |
199 | return; | |
200 | } | |
201 | ||
8cd82622 | 202 | m_CachePos += delta; |
daa616fc VS |
203 | } |
204 | while (m_Cache[m_CachePos].Key != at); | |
5526e819 VS |
205 | } |
206 | *end1 = m_Cache[m_CachePos].End1; | |
207 | *end2 = m_Cache[m_CachePos].End2; | |
208 | } | |
209 | ||
210 | ||
211 | ||
212 | ||
213 | //----------------------------------------------------------------------------- | |
214 | // wxHtmlTag | |
215 | //----------------------------------------------------------------------------- | |
216 | ||
217 | IMPLEMENT_CLASS(wxHtmlTag,wxObject) | |
218 | ||
211dfedd | 219 | wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, |
8cd82622 | 220 | const wxString& source, int pos, int end_pos, |
daa616fc VS |
221 | wxHtmlTagsCache *cache, |
222 | wxHtmlEntitiesParser *entParser) : wxObject() | |
5526e819 | 223 | { |
211dfedd VS |
224 | /* Setup DOM relations */ |
225 | ||
226 | m_Next = NULL; | |
227 | m_FirstChild = m_LastChild = NULL; | |
228 | m_Parent = parent; | |
229 | if (parent) | |
230 | { | |
231 | m_Prev = m_Parent->m_LastChild; | |
232 | if (m_Prev == NULL) | |
233 | m_Parent->m_FirstChild = this; | |
234 | else | |
235 | m_Prev->m_Next = this; | |
236 | m_Parent->m_LastChild = this; | |
237 | } | |
238 | else | |
239 | m_Prev = NULL; | |
240 | ||
241 | /* Find parameters and their values: */ | |
8cd82622 | 242 | |
5526e819 | 243 | int i; |
daa616fc | 244 | wxChar c; |
5526e819 VS |
245 | |
246 | // fill-in name, params and begin pos: | |
5526e819 | 247 | i = pos+1; |
5526e819 | 248 | |
b076dc01 | 249 | // find tag's name and convert it to uppercase: |
8cd82622 VZ |
250 | while ((i < end_pos) && |
251 | ((c = source[i++]) != wxT(' ') && c != wxT('\r') && | |
daa616fc | 252 | c != wxT('\n') && c != wxT('\t') && |
8cd82622 | 253 | c != wxT('>'))) |
a914db0f | 254 | { |
8cd82622 | 255 | if ((c >= wxT('a')) && (c <= wxT('z'))) |
daa616fc VS |
256 | c -= (wxT('a') - wxT('A')); |
257 | m_Name << c; | |
5526e819 VS |
258 | } |
259 | ||
b076dc01 | 260 | // if the tag has parameters, read them and "normalize" them, |
8cd82622 | 261 | // i.e. convert to uppercase, replace whitespaces by spaces and |
b076dc01 | 262 | // remove whitespaces around '=': |
c9893146 | 263 | if (source[i-1] != wxT('>')) |
daa616fc VS |
264 | { |
265 | #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \ | |
266 | c == wxT('\n') || c == wxT('\t')) | |
267 | wxString pname, pvalue; | |
268 | wxChar quote; | |
8cd82622 | 269 | enum |
a914db0f | 270 | { |
8cd82622 | 271 | ST_BEFORE_NAME = 1, |
daa616fc VS |
272 | ST_NAME, |
273 | ST_BEFORE_EQ, | |
274 | ST_BEFORE_VALUE, | |
275 | ST_VALUE | |
276 | } state; | |
8cd82622 | 277 | |
daa616fc VS |
278 | quote = 0; |
279 | state = ST_BEFORE_NAME; | |
280 | while (i < end_pos) | |
281 | { | |
282 | c = source[i++]; | |
283 | ||
8cd82622 | 284 | if (c == wxT('>') && !(state == ST_VALUE && quote != 0)) |
a914db0f | 285 | { |
daa616fc | 286 | if (state == ST_BEFORE_EQ || state == ST_NAME) |
b076dc01 | 287 | { |
daa616fc VS |
288 | m_ParamNames.Add(pname); |
289 | m_ParamValues.Add(wxEmptyString); | |
b076dc01 | 290 | } |
daa616fc VS |
291 | else if (state == ST_VALUE && quote == 0) |
292 | { | |
293 | m_ParamNames.Add(pname); | |
367c84b9 VS |
294 | if (entParser) |
295 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
296 | else | |
297 | m_ParamValues.Add(pvalue); | |
daa616fc VS |
298 | } |
299 | break; | |
5526e819 | 300 | } |
daa616fc | 301 | switch (state) |
a914db0f | 302 | { |
daa616fc VS |
303 | case ST_BEFORE_NAME: |
304 | if (!IS_WHITE(c)) | |
305 | { | |
306 | pname = c; | |
307 | state = ST_NAME; | |
308 | } | |
309 | break; | |
310 | case ST_NAME: | |
311 | if (IS_WHITE(c)) | |
312 | state = ST_BEFORE_EQ; | |
313 | else if (c == wxT('=')) | |
314 | state = ST_BEFORE_VALUE; | |
315 | else | |
316 | pname << c; | |
317 | break; | |
318 | case ST_BEFORE_EQ: | |
319 | if (c == wxT('=')) | |
320 | state = ST_BEFORE_VALUE; | |
321 | else if (!IS_WHITE(c)) | |
322 | { | |
323 | m_ParamNames.Add(pname); | |
324 | m_ParamValues.Add(wxEmptyString); | |
325 | pname = c; | |
326 | state = ST_NAME; | |
327 | } | |
328 | break; | |
329 | case ST_BEFORE_VALUE: | |
330 | if (!IS_WHITE(c)) | |
331 | { | |
332 | if (c == wxT('"') || c == wxT('\'')) | |
333 | quote = c, pvalue = wxEmptyString; | |
334 | else | |
335 | quote = 0, pvalue = c; | |
336 | state = ST_VALUE; | |
337 | } | |
338 | break; | |
339 | case ST_VALUE: | |
340 | if ((quote != 0 && c == quote) || | |
341 | (quote == 0 && IS_WHITE(c))) | |
342 | { | |
343 | m_ParamNames.Add(pname); | |
344 | if (quote == 0) | |
345 | { | |
346 | // VS: backward compatibility, no real reason, | |
347 | // but wxHTML code relies on this... :( | |
348 | pvalue.MakeUpper(); | |
349 | } | |
367c84b9 VS |
350 | if (entParser) |
351 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
352 | else | |
353 | m_ParamValues.Add(pvalue); | |
daa616fc VS |
354 | state = ST_BEFORE_NAME; |
355 | } | |
356 | else | |
357 | pvalue << c; | |
358 | break; | |
72aa4a98 | 359 | } |
5526e819 | 360 | } |
8cd82622 | 361 | |
daa616fc | 362 | #undef IS_WHITE |
7448de8d WS |
363 | } |
364 | m_Begin = i; | |
5526e819 | 365 | |
7448de8d WS |
366 | cache->QueryTag(pos, &m_End1, &m_End2); |
367 | if (m_End1 > end_pos) m_End1 = end_pos; | |
368 | if (m_End2 > end_pos) m_End2 = end_pos; | |
5526e819 VS |
369 | } |
370 | ||
211dfedd VS |
371 | wxHtmlTag::~wxHtmlTag() |
372 | { | |
0d58bb65 VS |
373 | wxHtmlTag *t1, *t2; |
374 | t1 = m_FirstChild; | |
375 | while (t1) | |
376 | { | |
377 | t2 = t1->GetNextSibling(); | |
378 | delete t1; | |
379 | t1 = t2; | |
380 | } | |
211dfedd VS |
381 | } |
382 | ||
5526e819 VS |
383 | bool wxHtmlTag::HasParam(const wxString& par) const |
384 | { | |
8703bc01 | 385 | return (m_ParamNames.Index(par, false) != wxNOT_FOUND); |
5526e819 VS |
386 | } |
387 | ||
5526e819 VS |
388 | wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const |
389 | { | |
8703bc01 | 390 | int index = m_ParamNames.Index(par, false); |
daa616fc VS |
391 | if (index == wxNOT_FOUND) |
392 | return wxEmptyString; | |
393 | if (with_commas) | |
4f9297b0 | 394 | { |
daa616fc VS |
395 | // VS: backward compatibility, seems to be never used by wxHTML... |
396 | wxString s; | |
397 | s << wxT('"') << m_ParamValues[index] << wxT('"'); | |
398 | return s; | |
5526e819 | 399 | } |
daa616fc VS |
400 | else |
401 | return m_ParamValues[index]; | |
5526e819 VS |
402 | } |
403 | ||
90350682 VZ |
404 | int wxHtmlTag::ScanParam(const wxString& par, |
405 | const wxChar *format, | |
406 | void *param) const | |
5526e819 | 407 | { |
5526e819 | 408 | wxString parval = GetParam(par); |
161f4f73 | 409 | return wxSscanf(parval, format, param); |
5526e819 VS |
410 | } |
411 | ||
8bd72d90 VS |
412 | bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const |
413 | { | |
414 | wxString str = GetParam(par); | |
8cd82622 | 415 | |
1729813a | 416 | if (str.empty()) return false; |
8bd72d90 VS |
417 | if (str.GetChar(0) == wxT('#')) |
418 | { | |
419 | unsigned long tmp; | |
420 | if (ScanParam(par, wxT("#%lX"), &tmp) != 1) | |
8703bc01 | 421 | return false; |
8bd72d90 | 422 | *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16), |
8703bc01 DS |
423 | (unsigned char)((tmp & 0x00FF00) >> 8), |
424 | (unsigned char)(tmp & 0x0000FF)); | |
425 | return true; | |
8bd72d90 VS |
426 | } |
427 | else | |
428 | { | |
429 | // Handle colours defined in HTML 4.0: | |
430 | #define HTML_COLOUR(name,r,g,b) \ | |
8703bc01 DS |
431 | if (str.IsSameAs(wxT(name), false)) \ |
432 | { *clr = wxColour(r,g,b); return true; } | |
8bd72d90 VS |
433 | HTML_COLOUR("black", 0x00,0x00,0x00) |
434 | HTML_COLOUR("silver", 0xC0,0xC0,0xC0) | |
435 | HTML_COLOUR("gray", 0x80,0x80,0x80) | |
436 | HTML_COLOUR("white", 0xFF,0xFF,0xFF) | |
437 | HTML_COLOUR("maroon", 0x80,0x00,0x00) | |
438 | HTML_COLOUR("red", 0xFF,0x00,0x00) | |
439 | HTML_COLOUR("purple", 0x80,0x00,0x80) | |
440 | HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF) | |
441 | HTML_COLOUR("green", 0x00,0x80,0x00) | |
442 | HTML_COLOUR("lime", 0x00,0xFF,0x00) | |
443 | HTML_COLOUR("olive", 0x80,0x80,0x00) | |
444 | HTML_COLOUR("yellow", 0xFF,0xFF,0x00) | |
445 | HTML_COLOUR("navy", 0x00,0x00,0x80) | |
446 | HTML_COLOUR("blue", 0x00,0x00,0xFF) | |
447 | HTML_COLOUR("teal", 0x00,0x80,0x80) | |
448 | HTML_COLOUR("aqua", 0x00,0xFF,0xFF) | |
449 | #undef HTML_COLOUR | |
8bd72d90 | 450 | } |
5716a1ab | 451 | |
8703bc01 | 452 | return false; |
8bd72d90 VS |
453 | } |
454 | ||
455 | bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const | |
456 | { | |
8703bc01 | 457 | if (!HasParam(par)) return false; |
8bd72d90 VS |
458 | long i; |
459 | bool succ = GetParam(par).ToLong(&i); | |
460 | *clr = (int)i; | |
461 | return succ; | |
462 | } | |
463 | ||
daa616fc VS |
464 | wxString wxHtmlTag::GetAllParams() const |
465 | { | |
3103e8a9 | 466 | // VS: this function is for backward compatibility only, |
daa616fc VS |
467 | // never used by wxHTML |
468 | wxString s; | |
469 | size_t cnt = m_ParamNames.GetCount(); | |
470 | for (size_t i = 0; i < cnt; i++) | |
471 | { | |
472 | s << m_ParamNames[i]; | |
473 | s << wxT('='); | |
474 | if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND) | |
475 | s << wxT('\'') << m_ParamValues[i] << wxT('\''); | |
476 | else | |
477 | s << wxT('"') << m_ParamValues[i] << wxT('"'); | |
478 | } | |
479 | return s; | |
480 | } | |
481 | ||
211dfedd VS |
482 | wxHtmlTag *wxHtmlTag::GetFirstSibling() const |
483 | { | |
484 | if (m_Parent) | |
485 | return m_Parent->m_FirstChild; | |
486 | else | |
487 | { | |
488 | wxHtmlTag *cur = (wxHtmlTag*)this; | |
8cd82622 | 489 | while (cur->m_Prev) |
211dfedd VS |
490 | cur = cur->m_Prev; |
491 | return cur; | |
492 | } | |
493 | } | |
494 | ||
495 | wxHtmlTag *wxHtmlTag::GetLastSibling() const | |
496 | { | |
497 | if (m_Parent) | |
498 | return m_Parent->m_LastChild; | |
499 | else | |
500 | { | |
501 | wxHtmlTag *cur = (wxHtmlTag*)this; | |
8cd82622 | 502 | while (cur->m_Next) |
211dfedd VS |
503 | cur = cur->m_Next; |
504 | return cur; | |
505 | } | |
506 | } | |
507 | ||
508 | wxHtmlTag *wxHtmlTag::GetNextTag() const | |
509 | { | |
510 | if (m_FirstChild) return m_FirstChild; | |
511 | if (m_Next) return m_Next; | |
512 | wxHtmlTag *cur = m_Parent; | |
513 | if (!cur) return NULL; | |
8cd82622 | 514 | while (cur->m_Parent && !cur->m_Next) |
211dfedd VS |
515 | cur = cur->m_Parent; |
516 | return cur->m_Next; | |
517 | } | |
518 | ||
1729813a WS |
519 | #if WXWIN_COMPATIBILITY_2_2 |
520 | ||
521 | bool wxHtmlTag::IsEnding() const | |
522 | { | |
523 | return false; | |
524 | } | |
525 | ||
526 | #endif // WXWIN_COMPATIBILITY_2_2 | |
527 | ||
4d223b67 | 528 | #endif |