]>
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 | |
3096bd2f | 25 | #include "wx/wx.h" |
5526e819 VS |
26 | #endif |
27 | ||
69941f05 | 28 | #include "wx/html/htmltag.h" |
daa616fc | 29 | #include "wx/html/htmlpars.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(); | |
66a77a74 | 65 | wxChar dummy[256]; |
5526e819 VS |
66 | |
67 | m_Cache = NULL; | |
68 | m_CacheSize = 0; | |
69 | m_CachePos = 0; | |
70 | ||
71 | pos = 0; | |
4f9297b0 VS |
72 | while (pos < lng) |
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++; | |
80 | dummy[0] = 0; i = 0; | |
15db3cf5 VS |
81 | while (pos < lng && |
82 | src[pos] != wxT('>') && | |
f6bcfd97 | 83 | src[pos] != wxT(' ') && src[pos] != wxT('\r') && |
4f9297b0 | 84 | src[pos] != wxT('\n') && src[pos] != wxT('\t')) |
a914db0f | 85 | { |
5526e819 | 86 | dummy[i] = src[pos++]; |
66a77a74 | 87 | if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A')); |
5526e819 VS |
88 | i++; |
89 | } | |
90 | dummy[i] = 0; | |
66a77a74 OK |
91 | m_Cache[tg].Name = new wxChar[i+1]; |
92 | memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar)); | |
5526e819 | 93 | |
15db3cf5 | 94 | while (pos < lng && src[pos] != wxT('>')) pos++; |
5526e819 | 95 | |
4f9297b0 | 96 | if (src[stpos+1] == wxT('/')) // ending tag: |
a914db0f | 97 | { |
5526e819 VS |
98 | m_Cache[tg].End1 = m_Cache[tg].End2 = -2; |
99 | // find matching begin tag: | |
100 | for (i = tg; i >= 0; i--) | |
4f9297b0 | 101 | if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0)) |
a914db0f | 102 | { |
5526e819 VS |
103 | m_Cache[i].End1 = stpos; |
104 | m_Cache[i].End2 = pos + 1; | |
105 | break; | |
106 | } | |
107 | } | |
4f9297b0 | 108 | else |
a914db0f | 109 | { |
5526e819 VS |
110 | m_Cache[tg].End1 = m_Cache[tg].End2 = -1; |
111 | } | |
112 | } | |
113 | ||
114 | pos++; | |
115 | } | |
116 | ||
117 | // ok, we're done, now we'll free .Name members of cache - we don't need it anymore: | |
4f9297b0 VS |
118 | for (i = 0; i < m_CacheSize; i++) |
119 | { | |
2776d7c3 | 120 | delete[] m_Cache[i].Name; |
5526e819 VS |
121 | m_Cache[i].Name = NULL; |
122 | } | |
123 | } | |
124 | ||
5526e819 VS |
125 | void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2) |
126 | { | |
127 | if (m_Cache == NULL) return; | |
4f9297b0 VS |
128 | if (m_Cache[m_CachePos].Key != at) |
129 | { | |
5526e819 | 130 | int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1; |
daa616fc VS |
131 | do |
132 | { | |
133 | m_CachePos += delta; | |
134 | } | |
135 | while (m_Cache[m_CachePos].Key != at); | |
5526e819 VS |
136 | } |
137 | *end1 = m_Cache[m_CachePos].End1; | |
138 | *end2 = m_Cache[m_CachePos].End2; | |
139 | } | |
140 | ||
141 | ||
142 | ||
143 | ||
144 | //----------------------------------------------------------------------------- | |
145 | // wxHtmlTag | |
146 | //----------------------------------------------------------------------------- | |
147 | ||
148 | IMPLEMENT_CLASS(wxHtmlTag,wxObject) | |
149 | ||
daa616fc VS |
150 | wxHtmlTag::wxHtmlTag(const wxString& source, int pos, int end_pos, |
151 | wxHtmlTagsCache *cache, | |
152 | wxHtmlEntitiesParser *entParser) : wxObject() | |
5526e819 VS |
153 | { |
154 | int i; | |
daa616fc | 155 | wxChar c; |
5526e819 VS |
156 | |
157 | // fill-in name, params and begin pos: | |
5526e819 | 158 | i = pos+1; |
daa616fc VS |
159 | if (source[i] == wxT('/')) |
160 | { m_Ending = TRUE; i++; } | |
161 | else | |
162 | m_Ending = FALSE; | |
5526e819 | 163 | |
b076dc01 | 164 | // find tag's name and convert it to uppercase: |
f6bcfd97 | 165 | while ((i < end_pos) && |
daa616fc VS |
166 | ((c = source[i++]) != wxT(' ') && c != wxT('\r') && |
167 | c != wxT('\n') && c != wxT('\t') && | |
168 | c != wxT('>'))) | |
a914db0f | 169 | { |
daa616fc VS |
170 | if ((c >= wxT('a')) && (c <= wxT('z'))) |
171 | c -= (wxT('a') - wxT('A')); | |
172 | m_Name << c; | |
5526e819 VS |
173 | } |
174 | ||
b076dc01 VS |
175 | // if the tag has parameters, read them and "normalize" them, |
176 | // i.e. convert to uppercase, replace whitespaces by spaces and | |
177 | // remove whitespaces around '=': | |
c9893146 | 178 | if (source[i-1] != wxT('>')) |
daa616fc VS |
179 | { |
180 | #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \ | |
181 | c == wxT('\n') || c == wxT('\t')) | |
182 | wxString pname, pvalue; | |
183 | wxChar quote; | |
184 | enum | |
a914db0f | 185 | { |
daa616fc VS |
186 | ST_BEFORE_NAME = 1, |
187 | ST_NAME, | |
188 | ST_BEFORE_EQ, | |
189 | ST_BEFORE_VALUE, | |
190 | ST_VALUE | |
191 | } state; | |
192 | ||
193 | quote = 0; | |
194 | state = ST_BEFORE_NAME; | |
195 | while (i < end_pos) | |
196 | { | |
197 | c = source[i++]; | |
198 | ||
199 | if (c == wxT('>') && !(state == ST_VALUE && quote != 0)) | |
a914db0f | 200 | { |
daa616fc | 201 | if (state == ST_BEFORE_EQ || state == ST_NAME) |
b076dc01 | 202 | { |
daa616fc VS |
203 | m_ParamNames.Add(pname); |
204 | m_ParamValues.Add(wxEmptyString); | |
b076dc01 | 205 | } |
daa616fc VS |
206 | else if (state == ST_VALUE && quote == 0) |
207 | { | |
208 | m_ParamNames.Add(pname); | |
209 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
210 | } | |
211 | break; | |
5526e819 | 212 | } |
daa616fc | 213 | switch (state) |
a914db0f | 214 | { |
daa616fc VS |
215 | case ST_BEFORE_NAME: |
216 | if (!IS_WHITE(c)) | |
217 | { | |
218 | pname = c; | |
219 | state = ST_NAME; | |
220 | } | |
221 | break; | |
222 | case ST_NAME: | |
223 | if (IS_WHITE(c)) | |
224 | state = ST_BEFORE_EQ; | |
225 | else if (c == wxT('=')) | |
226 | state = ST_BEFORE_VALUE; | |
227 | else | |
228 | pname << c; | |
229 | break; | |
230 | case ST_BEFORE_EQ: | |
231 | if (c == wxT('=')) | |
232 | state = ST_BEFORE_VALUE; | |
233 | else if (!IS_WHITE(c)) | |
234 | { | |
235 | m_ParamNames.Add(pname); | |
236 | m_ParamValues.Add(wxEmptyString); | |
237 | pname = c; | |
238 | state = ST_NAME; | |
239 | } | |
240 | break; | |
241 | case ST_BEFORE_VALUE: | |
242 | if (!IS_WHITE(c)) | |
243 | { | |
244 | if (c == wxT('"') || c == wxT('\'')) | |
245 | quote = c, pvalue = wxEmptyString; | |
246 | else | |
247 | quote = 0, pvalue = c; | |
248 | state = ST_VALUE; | |
249 | } | |
250 | break; | |
251 | case ST_VALUE: | |
252 | if ((quote != 0 && c == quote) || | |
253 | (quote == 0 && IS_WHITE(c))) | |
254 | { | |
255 | m_ParamNames.Add(pname); | |
256 | if (quote == 0) | |
257 | { | |
258 | // VS: backward compatibility, no real reason, | |
259 | // but wxHTML code relies on this... :( | |
260 | pvalue.MakeUpper(); | |
261 | } | |
262 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
263 | state = ST_BEFORE_NAME; | |
264 | } | |
265 | else | |
266 | pvalue << c; | |
267 | break; | |
72aa4a98 | 268 | } |
5526e819 | 269 | } |
daa616fc VS |
270 | |
271 | #undef IS_WHITE | |
272 | } | |
5526e819 VS |
273 | m_Begin = i; |
274 | ||
4f9297b0 | 275 | cache->QueryTag(pos, &m_End1, &m_End2); |
5526e819 VS |
276 | if (m_End1 > end_pos) m_End1 = end_pos; |
277 | if (m_End2 > end_pos) m_End2 = end_pos; | |
278 | } | |
279 | ||
5526e819 VS |
280 | bool wxHtmlTag::HasParam(const wxString& par) const |
281 | { | |
daa616fc | 282 | return (m_ParamNames.Index(par, FALSE) != wxNOT_FOUND); |
5526e819 VS |
283 | } |
284 | ||
5526e819 VS |
285 | wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const |
286 | { | |
daa616fc VS |
287 | int index = m_ParamNames.Index(par, FALSE); |
288 | if (index == wxNOT_FOUND) | |
289 | return wxEmptyString; | |
290 | if (with_commas) | |
4f9297b0 | 291 | { |
daa616fc VS |
292 | // VS: backward compatibility, seems to be never used by wxHTML... |
293 | wxString s; | |
294 | s << wxT('"') << m_ParamValues[index] << wxT('"'); | |
295 | return s; | |
5526e819 | 296 | } |
daa616fc VS |
297 | else |
298 | return m_ParamValues[index]; | |
5526e819 VS |
299 | } |
300 | ||
66a77a74 | 301 | int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const |
5526e819 | 302 | { |
5526e819 | 303 | wxString parval = GetParam(par); |
161f4f73 | 304 | return wxSscanf(parval, format, param); |
5526e819 VS |
305 | } |
306 | ||
8bd72d90 VS |
307 | bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const |
308 | { | |
309 | wxString str = GetParam(par); | |
310 | ||
311 | if (str.IsEmpty()) return FALSE; | |
312 | if (str.GetChar(0) == wxT('#')) | |
313 | { | |
314 | unsigned long tmp; | |
315 | if (ScanParam(par, wxT("#%lX"), &tmp) != 1) | |
316 | return FALSE; | |
317 | *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16), | |
318 | (unsigned char)((tmp & 0x00FF00) >> 8), | |
319 | (unsigned char)(tmp & 0x0000FF)); | |
320 | return TRUE; | |
321 | } | |
322 | else | |
323 | { | |
324 | // Handle colours defined in HTML 4.0: | |
325 | #define HTML_COLOUR(name,r,g,b) \ | |
326 | if (str.IsSameAs(wxT(name), FALSE)) \ | |
327 | { *clr = wxColour(r,g,b); return TRUE; } | |
328 | HTML_COLOUR("black", 0x00,0x00,0x00) | |
329 | HTML_COLOUR("silver", 0xC0,0xC0,0xC0) | |
330 | HTML_COLOUR("gray", 0x80,0x80,0x80) | |
331 | HTML_COLOUR("white", 0xFF,0xFF,0xFF) | |
332 | HTML_COLOUR("maroon", 0x80,0x00,0x00) | |
333 | HTML_COLOUR("red", 0xFF,0x00,0x00) | |
334 | HTML_COLOUR("purple", 0x80,0x00,0x80) | |
335 | HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF) | |
336 | HTML_COLOUR("green", 0x00,0x80,0x00) | |
337 | HTML_COLOUR("lime", 0x00,0xFF,0x00) | |
338 | HTML_COLOUR("olive", 0x80,0x80,0x00) | |
339 | HTML_COLOUR("yellow", 0xFF,0xFF,0x00) | |
340 | HTML_COLOUR("navy", 0x00,0x00,0x80) | |
341 | HTML_COLOUR("blue", 0x00,0x00,0xFF) | |
342 | HTML_COLOUR("teal", 0x00,0x80,0x80) | |
343 | HTML_COLOUR("aqua", 0x00,0xFF,0xFF) | |
344 | #undef HTML_COLOUR | |
345 | return FALSE; | |
346 | } | |
347 | } | |
348 | ||
349 | bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const | |
350 | { | |
351 | if (!HasParam(par)) return FALSE; | |
352 | long i; | |
353 | bool succ = GetParam(par).ToLong(&i); | |
354 | *clr = (int)i; | |
355 | return succ; | |
356 | } | |
357 | ||
daa616fc VS |
358 | wxString wxHtmlTag::GetAllParams() const |
359 | { | |
360 | // VS: this function is for backward compatiblity only, | |
361 | // never used by wxHTML | |
362 | wxString s; | |
363 | size_t cnt = m_ParamNames.GetCount(); | |
364 | for (size_t i = 0; i < cnt; i++) | |
365 | { | |
366 | s << m_ParamNames[i]; | |
367 | s << wxT('='); | |
368 | if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND) | |
369 | s << wxT('\'') << m_ParamValues[i] << wxT('\''); | |
370 | else | |
371 | s << wxT('"') << m_ParamValues[i] << wxT('"'); | |
372 | } | |
373 | return s; | |
374 | } | |
375 | ||
4d223b67 | 376 | #endif |