]>
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 VS |
6 | // Copyright: (c) 1999 Vaclav Slavik |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation | |
13 | #endif | |
14 | ||
3096bd2f | 15 | #include "wx/wxprec.h" |
5526e819 VS |
16 | |
17 | #include "wx/defs.h" | |
18 | #if wxUSE_HTML | |
19 | ||
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
5526e819 VS |
25 | #endif |
26 | ||
69941f05 | 27 | #include "wx/html/htmltag.h" |
daa616fc | 28 | #include "wx/html/htmlpars.h" |
fc1f2125 | 29 | #include "wx/colour.h" |
7e1e0960 | 30 | #include <stdio.h> // for vsscanf |
5526e819 VS |
31 | #include <stdarg.h> |
32 | ||
33 | ||
5526e819 VS |
34 | //----------------------------------------------------------------------------- |
35 | // wxHtmlTagsCache | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
5e8e25e7 VS |
38 | struct wxHtmlCacheItem |
39 | { | |
40 | // this is "pos" value passed to wxHtmlTag's constructor. | |
41 | // it is position of '<' character of the tag | |
42 | int Key; | |
43 | ||
44 | // end positions for the tag: | |
45 | // end1 is '<' of ending tag, | |
46 | // end2 is '>' or both are | |
47 | // -1 if there is no ending tag for this one... | |
48 | // or -2 if this is ending tag </...> | |
49 | int End1, End2; | |
50 | ||
51 | // name of this tag | |
52 | wxChar *Name; | |
53 | }; | |
54 | ||
55 | ||
5526e819 VS |
56 | IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject) |
57 | ||
58 | #define CACHE_INCREMENT 64 | |
59 | ||
60 | wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) | |
61 | { | |
66a77a74 | 62 | const wxChar *src = source.c_str(); |
5526e819 VS |
63 | int i, tg, pos, stpos; |
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 | ||
71 | 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)); |
5526e819 VS |
78 | tg = m_CacheSize++; |
79 | m_Cache[tg].Key = stpos = pos++; | |
8cd82622 VZ |
80 | |
81 | for ( i = 0; | |
82 | pos < lng && i < WXSIZEOF(tagBuffer) - 1 && | |
83 | src[pos] != wxT('>') && !wxIsspace(src[pos]); | |
84 | i++, pos++ ) | |
a914db0f | 85 | { |
8cd82622 | 86 | tagBuffer[i] = wxToupper(src[pos]); |
5526e819 | 87 | } |
8cd82622 VZ |
88 | tagBuffer[i] = _T('\0'); |
89 | ||
66a77a74 | 90 | m_Cache[tg].Name = new wxChar[i+1]; |
8cd82622 | 91 | memcpy(m_Cache[tg].Name, tagBuffer, (i+1)*sizeof(wxChar)); |
5526e819 | 92 | |
15db3cf5 | 93 | while (pos < lng && src[pos] != wxT('>')) pos++; |
5526e819 | 94 | |
4f9297b0 | 95 | if (src[stpos+1] == wxT('/')) // ending tag: |
a914db0f | 96 | { |
5526e819 VS |
97 | m_Cache[tg].End1 = m_Cache[tg].End2 = -2; |
98 | // find matching begin tag: | |
99 | for (i = tg; i >= 0; i--) | |
8cd82622 | 100 | if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, tagBuffer+1) == 0)) |
a914db0f | 101 | { |
5526e819 VS |
102 | m_Cache[i].End1 = stpos; |
103 | m_Cache[i].End2 = pos + 1; | |
104 | break; | |
105 | } | |
106 | } | |
8cd82622 | 107 | else |
a914db0f | 108 | { |
5526e819 VS |
109 | m_Cache[tg].End1 = m_Cache[tg].End2 = -1; |
110 | } | |
111 | } | |
112 | ||
113 | pos++; | |
114 | } | |
115 | ||
116 | // ok, we're done, now we'll free .Name members of cache - we don't need it anymore: | |
8cd82622 | 117 | for (i = 0; i < m_CacheSize; i++) |
4f9297b0 | 118 | { |
2776d7c3 | 119 | delete[] m_Cache[i].Name; |
5526e819 VS |
120 | m_Cache[i].Name = NULL; |
121 | } | |
122 | } | |
123 | ||
5526e819 VS |
124 | void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2) |
125 | { | |
126 | if (m_Cache == NULL) return; | |
8cd82622 | 127 | if (m_Cache[m_CachePos].Key != at) |
4f9297b0 | 128 | { |
5526e819 | 129 | int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1; |
8cd82622 VZ |
130 | do |
131 | { | |
132 | m_CachePos += delta; | |
daa616fc VS |
133 | } |
134 | while (m_Cache[m_CachePos].Key != at); | |
5526e819 VS |
135 | } |
136 | *end1 = m_Cache[m_CachePos].End1; | |
137 | *end2 = m_Cache[m_CachePos].End2; | |
138 | } | |
139 | ||
140 | ||
141 | ||
142 | ||
143 | //----------------------------------------------------------------------------- | |
144 | // wxHtmlTag | |
145 | //----------------------------------------------------------------------------- | |
146 | ||
147 | IMPLEMENT_CLASS(wxHtmlTag,wxObject) | |
148 | ||
211dfedd | 149 | wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, |
8cd82622 | 150 | const wxString& source, int pos, int end_pos, |
daa616fc VS |
151 | wxHtmlTagsCache *cache, |
152 | wxHtmlEntitiesParser *entParser) : wxObject() | |
5526e819 | 153 | { |
211dfedd VS |
154 | /* Setup DOM relations */ |
155 | ||
156 | m_Next = NULL; | |
157 | m_FirstChild = m_LastChild = NULL; | |
158 | m_Parent = parent; | |
159 | if (parent) | |
160 | { | |
161 | m_Prev = m_Parent->m_LastChild; | |
162 | if (m_Prev == NULL) | |
163 | m_Parent->m_FirstChild = this; | |
164 | else | |
165 | m_Prev->m_Next = this; | |
166 | m_Parent->m_LastChild = this; | |
167 | } | |
168 | else | |
169 | m_Prev = NULL; | |
170 | ||
171 | /* Find parameters and their values: */ | |
8cd82622 | 172 | |
5526e819 | 173 | int i; |
daa616fc | 174 | wxChar c; |
5526e819 VS |
175 | |
176 | // fill-in name, params and begin pos: | |
5526e819 | 177 | i = pos+1; |
5526e819 | 178 | |
b076dc01 | 179 | // find tag's name and convert it to uppercase: |
8cd82622 VZ |
180 | while ((i < end_pos) && |
181 | ((c = source[i++]) != wxT(' ') && c != wxT('\r') && | |
daa616fc | 182 | c != wxT('\n') && c != wxT('\t') && |
8cd82622 | 183 | c != wxT('>'))) |
a914db0f | 184 | { |
8cd82622 | 185 | if ((c >= wxT('a')) && (c <= wxT('z'))) |
daa616fc VS |
186 | c -= (wxT('a') - wxT('A')); |
187 | m_Name << c; | |
5526e819 VS |
188 | } |
189 | ||
b076dc01 | 190 | // if the tag has parameters, read them and "normalize" them, |
8cd82622 | 191 | // i.e. convert to uppercase, replace whitespaces by spaces and |
b076dc01 | 192 | // remove whitespaces around '=': |
c9893146 | 193 | if (source[i-1] != wxT('>')) |
daa616fc VS |
194 | { |
195 | #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \ | |
196 | c == wxT('\n') || c == wxT('\t')) | |
197 | wxString pname, pvalue; | |
198 | wxChar quote; | |
8cd82622 | 199 | enum |
a914db0f | 200 | { |
8cd82622 | 201 | ST_BEFORE_NAME = 1, |
daa616fc VS |
202 | ST_NAME, |
203 | ST_BEFORE_EQ, | |
204 | ST_BEFORE_VALUE, | |
205 | ST_VALUE | |
206 | } state; | |
8cd82622 | 207 | |
daa616fc VS |
208 | quote = 0; |
209 | state = ST_BEFORE_NAME; | |
210 | while (i < end_pos) | |
211 | { | |
212 | c = source[i++]; | |
213 | ||
8cd82622 | 214 | if (c == wxT('>') && !(state == ST_VALUE && quote != 0)) |
a914db0f | 215 | { |
daa616fc | 216 | if (state == ST_BEFORE_EQ || state == ST_NAME) |
b076dc01 | 217 | { |
daa616fc VS |
218 | m_ParamNames.Add(pname); |
219 | m_ParamValues.Add(wxEmptyString); | |
b076dc01 | 220 | } |
daa616fc VS |
221 | else if (state == ST_VALUE && quote == 0) |
222 | { | |
223 | m_ParamNames.Add(pname); | |
367c84b9 VS |
224 | if (entParser) |
225 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
226 | else | |
227 | m_ParamValues.Add(pvalue); | |
daa616fc VS |
228 | } |
229 | break; | |
5526e819 | 230 | } |
daa616fc | 231 | switch (state) |
a914db0f | 232 | { |
daa616fc VS |
233 | case ST_BEFORE_NAME: |
234 | if (!IS_WHITE(c)) | |
235 | { | |
236 | pname = c; | |
237 | state = ST_NAME; | |
238 | } | |
239 | break; | |
240 | case ST_NAME: | |
241 | if (IS_WHITE(c)) | |
242 | state = ST_BEFORE_EQ; | |
243 | else if (c == wxT('=')) | |
244 | state = ST_BEFORE_VALUE; | |
245 | else | |
246 | pname << c; | |
247 | break; | |
248 | case ST_BEFORE_EQ: | |
249 | if (c == wxT('=')) | |
250 | state = ST_BEFORE_VALUE; | |
251 | else if (!IS_WHITE(c)) | |
252 | { | |
253 | m_ParamNames.Add(pname); | |
254 | m_ParamValues.Add(wxEmptyString); | |
255 | pname = c; | |
256 | state = ST_NAME; | |
257 | } | |
258 | break; | |
259 | case ST_BEFORE_VALUE: | |
260 | if (!IS_WHITE(c)) | |
261 | { | |
262 | if (c == wxT('"') || c == wxT('\'')) | |
263 | quote = c, pvalue = wxEmptyString; | |
264 | else | |
265 | quote = 0, pvalue = c; | |
266 | state = ST_VALUE; | |
267 | } | |
268 | break; | |
269 | case ST_VALUE: | |
270 | if ((quote != 0 && c == quote) || | |
271 | (quote == 0 && IS_WHITE(c))) | |
272 | { | |
273 | m_ParamNames.Add(pname); | |
274 | if (quote == 0) | |
275 | { | |
276 | // VS: backward compatibility, no real reason, | |
277 | // but wxHTML code relies on this... :( | |
278 | pvalue.MakeUpper(); | |
279 | } | |
367c84b9 VS |
280 | if (entParser) |
281 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
282 | else | |
283 | m_ParamValues.Add(pvalue); | |
daa616fc VS |
284 | state = ST_BEFORE_NAME; |
285 | } | |
286 | else | |
287 | pvalue << c; | |
288 | break; | |
72aa4a98 | 289 | } |
5526e819 | 290 | } |
8cd82622 | 291 | |
daa616fc VS |
292 | #undef IS_WHITE |
293 | } | |
5526e819 VS |
294 | m_Begin = i; |
295 | ||
4f9297b0 | 296 | cache->QueryTag(pos, &m_End1, &m_End2); |
5526e819 VS |
297 | if (m_End1 > end_pos) m_End1 = end_pos; |
298 | if (m_End2 > end_pos) m_End2 = end_pos; | |
299 | } | |
300 | ||
211dfedd VS |
301 | wxHtmlTag::~wxHtmlTag() |
302 | { | |
0d58bb65 VS |
303 | wxHtmlTag *t1, *t2; |
304 | t1 = m_FirstChild; | |
305 | while (t1) | |
306 | { | |
307 | t2 = t1->GetNextSibling(); | |
308 | delete t1; | |
309 | t1 = t2; | |
310 | } | |
211dfedd VS |
311 | } |
312 | ||
5526e819 VS |
313 | bool wxHtmlTag::HasParam(const wxString& par) const |
314 | { | |
daa616fc | 315 | return (m_ParamNames.Index(par, FALSE) != wxNOT_FOUND); |
5526e819 VS |
316 | } |
317 | ||
5526e819 VS |
318 | wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const |
319 | { | |
daa616fc VS |
320 | int index = m_ParamNames.Index(par, FALSE); |
321 | if (index == wxNOT_FOUND) | |
322 | return wxEmptyString; | |
323 | if (with_commas) | |
4f9297b0 | 324 | { |
daa616fc VS |
325 | // VS: backward compatibility, seems to be never used by wxHTML... |
326 | wxString s; | |
327 | s << wxT('"') << m_ParamValues[index] << wxT('"'); | |
328 | return s; | |
5526e819 | 329 | } |
daa616fc VS |
330 | else |
331 | return m_ParamValues[index]; | |
5526e819 VS |
332 | } |
333 | ||
90350682 VZ |
334 | int wxHtmlTag::ScanParam(const wxString& par, |
335 | const wxChar *format, | |
336 | void *param) const | |
5526e819 | 337 | { |
5526e819 | 338 | wxString parval = GetParam(par); |
161f4f73 | 339 | return wxSscanf(parval, format, param); |
5526e819 VS |
340 | } |
341 | ||
8bd72d90 VS |
342 | bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const |
343 | { | |
344 | wxString str = GetParam(par); | |
8cd82622 | 345 | |
8bd72d90 VS |
346 | if (str.IsEmpty()) return FALSE; |
347 | if (str.GetChar(0) == wxT('#')) | |
348 | { | |
349 | unsigned long tmp; | |
350 | if (ScanParam(par, wxT("#%lX"), &tmp) != 1) | |
351 | return FALSE; | |
352 | *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16), | |
353 | (unsigned char)((tmp & 0x00FF00) >> 8), | |
354 | (unsigned char)(tmp & 0x0000FF)); | |
355 | return TRUE; | |
356 | } | |
357 | else | |
358 | { | |
359 | // Handle colours defined in HTML 4.0: | |
360 | #define HTML_COLOUR(name,r,g,b) \ | |
361 | if (str.IsSameAs(wxT(name), FALSE)) \ | |
362 | { *clr = wxColour(r,g,b); return TRUE; } | |
363 | HTML_COLOUR("black", 0x00,0x00,0x00) | |
364 | HTML_COLOUR("silver", 0xC0,0xC0,0xC0) | |
365 | HTML_COLOUR("gray", 0x80,0x80,0x80) | |
366 | HTML_COLOUR("white", 0xFF,0xFF,0xFF) | |
367 | HTML_COLOUR("maroon", 0x80,0x00,0x00) | |
368 | HTML_COLOUR("red", 0xFF,0x00,0x00) | |
369 | HTML_COLOUR("purple", 0x80,0x00,0x80) | |
370 | HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF) | |
371 | HTML_COLOUR("green", 0x00,0x80,0x00) | |
372 | HTML_COLOUR("lime", 0x00,0xFF,0x00) | |
373 | HTML_COLOUR("olive", 0x80,0x80,0x00) | |
374 | HTML_COLOUR("yellow", 0xFF,0xFF,0x00) | |
375 | HTML_COLOUR("navy", 0x00,0x00,0x80) | |
376 | HTML_COLOUR("blue", 0x00,0x00,0xFF) | |
377 | HTML_COLOUR("teal", 0x00,0x80,0x80) | |
378 | HTML_COLOUR("aqua", 0x00,0xFF,0xFF) | |
379 | #undef HTML_COLOUR | |
8bd72d90 | 380 | } |
5716a1ab VZ |
381 | |
382 | return FALSE; | |
8bd72d90 VS |
383 | } |
384 | ||
385 | bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const | |
386 | { | |
387 | if (!HasParam(par)) return FALSE; | |
388 | long i; | |
389 | bool succ = GetParam(par).ToLong(&i); | |
390 | *clr = (int)i; | |
391 | return succ; | |
392 | } | |
393 | ||
daa616fc VS |
394 | wxString wxHtmlTag::GetAllParams() const |
395 | { | |
8cd82622 | 396 | // VS: this function is for backward compatiblity only, |
daa616fc VS |
397 | // never used by wxHTML |
398 | wxString s; | |
399 | size_t cnt = m_ParamNames.GetCount(); | |
400 | for (size_t i = 0; i < cnt; i++) | |
401 | { | |
402 | s << m_ParamNames[i]; | |
403 | s << wxT('='); | |
404 | if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND) | |
405 | s << wxT('\'') << m_ParamValues[i] << wxT('\''); | |
406 | else | |
407 | s << wxT('"') << m_ParamValues[i] << wxT('"'); | |
408 | } | |
409 | return s; | |
410 | } | |
411 | ||
211dfedd VS |
412 | wxHtmlTag *wxHtmlTag::GetFirstSibling() const |
413 | { | |
414 | if (m_Parent) | |
415 | return m_Parent->m_FirstChild; | |
416 | else | |
417 | { | |
418 | wxHtmlTag *cur = (wxHtmlTag*)this; | |
8cd82622 | 419 | while (cur->m_Prev) |
211dfedd VS |
420 | cur = cur->m_Prev; |
421 | return cur; | |
422 | } | |
423 | } | |
424 | ||
425 | wxHtmlTag *wxHtmlTag::GetLastSibling() const | |
426 | { | |
427 | if (m_Parent) | |
428 | return m_Parent->m_LastChild; | |
429 | else | |
430 | { | |
431 | wxHtmlTag *cur = (wxHtmlTag*)this; | |
8cd82622 | 432 | while (cur->m_Next) |
211dfedd VS |
433 | cur = cur->m_Next; |
434 | return cur; | |
435 | } | |
436 | } | |
437 | ||
438 | wxHtmlTag *wxHtmlTag::GetNextTag() const | |
439 | { | |
440 | if (m_FirstChild) return m_FirstChild; | |
441 | if (m_Next) return m_Next; | |
442 | wxHtmlTag *cur = m_Parent; | |
443 | if (!cur) return NULL; | |
8cd82622 | 444 | while (cur->m_Parent && !cur->m_Next) |
211dfedd VS |
445 | cur = cur->m_Parent; |
446 | return cur->m_Next; | |
447 | } | |
448 | ||
4d223b67 | 449 | #endif |