]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
added common notebookbase source file
[wxWidgets.git] / src / html / htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmltag.cpp
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/html/htmltag.h"
29 #include "wx/html/htmlpars.h"
30 #include <stdio.h> // for vsscanf
31 #include <stdarg.h>
32
33
34 //-----------------------------------------------------------------------------
35 // wxHtmlTagsCache
36 //-----------------------------------------------------------------------------
37
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
56 IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject)
57
58 #define CACHE_INCREMENT 64
59
60 wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source)
61 {
62 const wxChar *src = source.c_str();
63 int i, tg, pos, stpos;
64 int lng = source.Length();
65 wxChar dummy[256];
66
67 m_Cache = NULL;
68 m_CacheSize = 0;
69 m_CachePos = 0;
70
71 pos = 0;
72 while (pos < lng)
73 {
74 if (src[pos] == wxT('<')) // tag found:
75 {
76 if (m_CacheSize % CACHE_INCREMENT == 0)
77 m_Cache = (wxHtmlCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(wxHtmlCacheItem));
78 tg = m_CacheSize++;
79 m_Cache[tg].Key = stpos = pos++;
80 dummy[0] = 0; i = 0;
81 while (pos < lng &&
82 src[pos] != wxT('>') &&
83 src[pos] != wxT(' ') && src[pos] != wxT('\r') &&
84 src[pos] != wxT('\n') && src[pos] != wxT('\t'))
85 {
86 dummy[i] = src[pos++];
87 if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A'));
88 i++;
89 }
90 dummy[i] = 0;
91 m_Cache[tg].Name = new wxChar[i+1];
92 memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar));
93
94 while (pos < lng && src[pos] != wxT('>')) pos++;
95
96 if (src[stpos+1] == wxT('/')) // ending tag:
97 {
98 m_Cache[tg].End1 = m_Cache[tg].End2 = -2;
99 // find matching begin tag:
100 for (i = tg; i >= 0; i--)
101 if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0))
102 {
103 m_Cache[i].End1 = stpos;
104 m_Cache[i].End2 = pos + 1;
105 break;
106 }
107 }
108 else
109 {
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:
118 for (i = 0; i < m_CacheSize; i++)
119 {
120 delete[] m_Cache[i].Name;
121 m_Cache[i].Name = NULL;
122 }
123 }
124
125 void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
126 {
127 if (m_Cache == NULL) return;
128 if (m_Cache[m_CachePos].Key != at)
129 {
130 int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1;
131 do
132 {
133 m_CachePos += delta;
134 }
135 while (m_Cache[m_CachePos].Key != at);
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
150 wxHtmlTag::wxHtmlTag(const wxString& source, int pos, int end_pos,
151 wxHtmlTagsCache *cache,
152 wxHtmlEntitiesParser *entParser) : wxObject()
153 {
154 int i;
155 wxChar c;
156
157 // fill-in name, params and begin pos:
158 i = pos+1;
159 if (source[i] == wxT('/'))
160 { m_Ending = TRUE; i++; }
161 else
162 m_Ending = FALSE;
163
164 // find tag's name and convert it to uppercase:
165 while ((i < end_pos) &&
166 ((c = source[i++]) != wxT(' ') && c != wxT('\r') &&
167 c != wxT('\n') && c != wxT('\t') &&
168 c != wxT('>')))
169 {
170 if ((c >= wxT('a')) && (c <= wxT('z')))
171 c -= (wxT('a') - wxT('A'));
172 m_Name << c;
173 }
174
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 '=':
178 if (source[i-1] != wxT('>'))
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
185 {
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))
200 {
201 if (state == ST_BEFORE_EQ || state == ST_NAME)
202 {
203 m_ParamNames.Add(pname);
204 m_ParamValues.Add(wxEmptyString);
205 }
206 else if (state == ST_VALUE && quote == 0)
207 {
208 m_ParamNames.Add(pname);
209 if (entParser)
210 m_ParamValues.Add(entParser->Parse(pvalue));
211 else
212 m_ParamValues.Add(pvalue);
213 }
214 break;
215 }
216 switch (state)
217 {
218 case ST_BEFORE_NAME:
219 if (!IS_WHITE(c))
220 {
221 pname = c;
222 state = ST_NAME;
223 }
224 break;
225 case ST_NAME:
226 if (IS_WHITE(c))
227 state = ST_BEFORE_EQ;
228 else if (c == wxT('='))
229 state = ST_BEFORE_VALUE;
230 else
231 pname << c;
232 break;
233 case ST_BEFORE_EQ:
234 if (c == wxT('='))
235 state = ST_BEFORE_VALUE;
236 else if (!IS_WHITE(c))
237 {
238 m_ParamNames.Add(pname);
239 m_ParamValues.Add(wxEmptyString);
240 pname = c;
241 state = ST_NAME;
242 }
243 break;
244 case ST_BEFORE_VALUE:
245 if (!IS_WHITE(c))
246 {
247 if (c == wxT('"') || c == wxT('\''))
248 quote = c, pvalue = wxEmptyString;
249 else
250 quote = 0, pvalue = c;
251 state = ST_VALUE;
252 }
253 break;
254 case ST_VALUE:
255 if ((quote != 0 && c == quote) ||
256 (quote == 0 && IS_WHITE(c)))
257 {
258 m_ParamNames.Add(pname);
259 if (quote == 0)
260 {
261 // VS: backward compatibility, no real reason,
262 // but wxHTML code relies on this... :(
263 pvalue.MakeUpper();
264 }
265 if (entParser)
266 m_ParamValues.Add(entParser->Parse(pvalue));
267 else
268 m_ParamValues.Add(pvalue);
269 state = ST_BEFORE_NAME;
270 }
271 else
272 pvalue << c;
273 break;
274 }
275 }
276
277 #undef IS_WHITE
278 }
279 m_Begin = i;
280
281 cache->QueryTag(pos, &m_End1, &m_End2);
282 if (m_End1 > end_pos) m_End1 = end_pos;
283 if (m_End2 > end_pos) m_End2 = end_pos;
284 }
285
286 bool wxHtmlTag::HasParam(const wxString& par) const
287 {
288 return (m_ParamNames.Index(par, FALSE) != wxNOT_FOUND);
289 }
290
291 wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const
292 {
293 int index = m_ParamNames.Index(par, FALSE);
294 if (index == wxNOT_FOUND)
295 return wxEmptyString;
296 if (with_commas)
297 {
298 // VS: backward compatibility, seems to be never used by wxHTML...
299 wxString s;
300 s << wxT('"') << m_ParamValues[index] << wxT('"');
301 return s;
302 }
303 else
304 return m_ParamValues[index];
305 }
306
307 int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const
308 {
309 wxString parval = GetParam(par);
310 return wxSscanf(parval, format, param);
311 }
312
313 bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
314 {
315 wxString str = GetParam(par);
316
317 if (str.IsEmpty()) return FALSE;
318 if (str.GetChar(0) == wxT('#'))
319 {
320 unsigned long tmp;
321 if (ScanParam(par, wxT("#%lX"), &tmp) != 1)
322 return FALSE;
323 *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16),
324 (unsigned char)((tmp & 0x00FF00) >> 8),
325 (unsigned char)(tmp & 0x0000FF));
326 return TRUE;
327 }
328 else
329 {
330 // Handle colours defined in HTML 4.0:
331 #define HTML_COLOUR(name,r,g,b) \
332 if (str.IsSameAs(wxT(name), FALSE)) \
333 { *clr = wxColour(r,g,b); return TRUE; }
334 HTML_COLOUR("black", 0x00,0x00,0x00)
335 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
336 HTML_COLOUR("gray", 0x80,0x80,0x80)
337 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
338 HTML_COLOUR("maroon", 0x80,0x00,0x00)
339 HTML_COLOUR("red", 0xFF,0x00,0x00)
340 HTML_COLOUR("purple", 0x80,0x00,0x80)
341 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
342 HTML_COLOUR("green", 0x00,0x80,0x00)
343 HTML_COLOUR("lime", 0x00,0xFF,0x00)
344 HTML_COLOUR("olive", 0x80,0x80,0x00)
345 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
346 HTML_COLOUR("navy", 0x00,0x00,0x80)
347 HTML_COLOUR("blue", 0x00,0x00,0xFF)
348 HTML_COLOUR("teal", 0x00,0x80,0x80)
349 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
350 #undef HTML_COLOUR
351 return FALSE;
352 }
353 }
354
355 bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
356 {
357 if (!HasParam(par)) return FALSE;
358 long i;
359 bool succ = GetParam(par).ToLong(&i);
360 *clr = (int)i;
361 return succ;
362 }
363
364 wxString wxHtmlTag::GetAllParams() const
365 {
366 // VS: this function is for backward compatiblity only,
367 // never used by wxHTML
368 wxString s;
369 size_t cnt = m_ParamNames.GetCount();
370 for (size_t i = 0; i < cnt; i++)
371 {
372 s << m_ParamNames[i];
373 s << wxT('=');
374 if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND)
375 s << wxT('\'') << m_ParamValues[i] << wxT('\'');
376 else
377 s << wxT('"') << m_ParamValues[i] << wxT('"');
378 }
379 return s;
380 }
381
382 #endif