]>
Commit | Line | Data |
---|---|---|
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 | #endif | |
26 | ||
27 | #include "wx/html/htmltag.h" | |
28 | #include "wx/html/htmlpars.h" | |
29 | #include <stdio.h> // for vsscanf | |
30 | #include <stdarg.h> | |
31 | ||
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // wxHtmlTagsCache | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | struct wxHtmlCacheItem | |
38 | { | |
39 | // this is "pos" value passed to wxHtmlTag's constructor. | |
40 | // it is position of '<' character of the tag | |
41 | int Key; | |
42 | ||
43 | // end positions for the tag: | |
44 | // end1 is '<' of ending tag, | |
45 | // end2 is '>' or both are | |
46 | // -1 if there is no ending tag for this one... | |
47 | // or -2 if this is ending tag </...> | |
48 | int End1, End2; | |
49 | ||
50 | // name of this tag | |
51 | wxChar *Name; | |
52 | }; | |
53 | ||
54 | ||
55 | IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject) | |
56 | ||
57 | #define CACHE_INCREMENT 64 | |
58 | ||
59 | wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) | |
60 | { | |
61 | const wxChar *src = source.c_str(); | |
62 | int i, tg, pos, stpos; | |
63 | int lng = source.Length(); | |
64 | wxChar dummy[256]; | |
65 | ||
66 | m_Cache = NULL; | |
67 | m_CacheSize = 0; | |
68 | m_CachePos = 0; | |
69 | ||
70 | pos = 0; | |
71 | while (pos < lng) | |
72 | { | |
73 | if (src[pos] == wxT('<')) // tag found: | |
74 | { | |
75 | if (m_CacheSize % CACHE_INCREMENT == 0) | |
76 | m_Cache = (wxHtmlCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(wxHtmlCacheItem)); | |
77 | tg = m_CacheSize++; | |
78 | m_Cache[tg].Key = stpos = pos++; | |
79 | dummy[0] = 0; i = 0; | |
80 | while (pos < lng && | |
81 | src[pos] != wxT('>') && | |
82 | src[pos] != wxT(' ') && src[pos] != wxT('\r') && | |
83 | src[pos] != wxT('\n') && src[pos] != wxT('\t')) | |
84 | { | |
85 | dummy[i] = src[pos++]; | |
86 | if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A')); | |
87 | i++; | |
88 | } | |
89 | dummy[i] = 0; | |
90 | m_Cache[tg].Name = new wxChar[i+1]; | |
91 | memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar)); | |
92 | ||
93 | while (pos < lng && src[pos] != wxT('>')) pos++; | |
94 | ||
95 | if (src[stpos+1] == wxT('/')) // ending tag: | |
96 | { | |
97 | m_Cache[tg].End1 = m_Cache[tg].End2 = -2; | |
98 | // find matching begin tag: | |
99 | for (i = tg; i >= 0; i--) | |
100 | if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0)) | |
101 | { | |
102 | m_Cache[i].End1 = stpos; | |
103 | m_Cache[i].End2 = pos + 1; | |
104 | break; | |
105 | } | |
106 | } | |
107 | else | |
108 | { | |
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: | |
117 | for (i = 0; i < m_CacheSize; i++) | |
118 | { | |
119 | delete[] m_Cache[i].Name; | |
120 | m_Cache[i].Name = NULL; | |
121 | } | |
122 | } | |
123 | ||
124 | void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2) | |
125 | { | |
126 | if (m_Cache == NULL) return; | |
127 | if (m_Cache[m_CachePos].Key != at) | |
128 | { | |
129 | int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1; | |
130 | do | |
131 | { | |
132 | m_CachePos += delta; | |
133 | } | |
134 | while (m_Cache[m_CachePos].Key != at); | |
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 | ||
149 | wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, | |
150 | const wxString& source, int pos, int end_pos, | |
151 | wxHtmlTagsCache *cache, | |
152 | wxHtmlEntitiesParser *entParser) : wxObject() | |
153 | { | |
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: */ | |
172 | ||
173 | int i; | |
174 | wxChar c; | |
175 | ||
176 | // fill-in name, params and begin pos: | |
177 | i = pos+1; | |
178 | ||
179 | // find tag's name and convert it to uppercase: | |
180 | while ((i < end_pos) && | |
181 | ((c = source[i++]) != wxT(' ') && c != wxT('\r') && | |
182 | c != wxT('\n') && c != wxT('\t') && | |
183 | c != wxT('>'))) | |
184 | { | |
185 | if ((c >= wxT('a')) && (c <= wxT('z'))) | |
186 | c -= (wxT('a') - wxT('A')); | |
187 | m_Name << c; | |
188 | } | |
189 | ||
190 | // if the tag has parameters, read them and "normalize" them, | |
191 | // i.e. convert to uppercase, replace whitespaces by spaces and | |
192 | // remove whitespaces around '=': | |
193 | if (source[i-1] != wxT('>')) | |
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; | |
199 | enum | |
200 | { | |
201 | ST_BEFORE_NAME = 1, | |
202 | ST_NAME, | |
203 | ST_BEFORE_EQ, | |
204 | ST_BEFORE_VALUE, | |
205 | ST_VALUE | |
206 | } state; | |
207 | ||
208 | quote = 0; | |
209 | state = ST_BEFORE_NAME; | |
210 | while (i < end_pos) | |
211 | { | |
212 | c = source[i++]; | |
213 | ||
214 | if (c == wxT('>') && !(state == ST_VALUE && quote != 0)) | |
215 | { | |
216 | if (state == ST_BEFORE_EQ || state == ST_NAME) | |
217 | { | |
218 | m_ParamNames.Add(pname); | |
219 | m_ParamValues.Add(wxEmptyString); | |
220 | } | |
221 | else if (state == ST_VALUE && quote == 0) | |
222 | { | |
223 | m_ParamNames.Add(pname); | |
224 | if (entParser) | |
225 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
226 | else | |
227 | m_ParamValues.Add(pvalue); | |
228 | } | |
229 | break; | |
230 | } | |
231 | switch (state) | |
232 | { | |
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 | } | |
280 | if (entParser) | |
281 | m_ParamValues.Add(entParser->Parse(pvalue)); | |
282 | else | |
283 | m_ParamValues.Add(pvalue); | |
284 | state = ST_BEFORE_NAME; | |
285 | } | |
286 | else | |
287 | pvalue << c; | |
288 | break; | |
289 | } | |
290 | } | |
291 | ||
292 | #undef IS_WHITE | |
293 | } | |
294 | m_Begin = i; | |
295 | ||
296 | cache->QueryTag(pos, &m_End1, &m_End2); | |
297 | if (m_End1 > end_pos) m_End1 = end_pos; | |
298 | if (m_End2 > end_pos) m_End2 = end_pos; | |
299 | } | |
300 | ||
301 | wxHtmlTag::~wxHtmlTag() | |
302 | { | |
303 | wxHtmlTag *t1, *t2; | |
304 | t1 = m_FirstChild; | |
305 | while (t1) | |
306 | { | |
307 | t2 = t1->GetNextSibling(); | |
308 | delete t1; | |
309 | t1 = t2; | |
310 | } | |
311 | } | |
312 | ||
313 | bool wxHtmlTag::HasParam(const wxString& par) const | |
314 | { | |
315 | return (m_ParamNames.Index(par, FALSE) != wxNOT_FOUND); | |
316 | } | |
317 | ||
318 | wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const | |
319 | { | |
320 | int index = m_ParamNames.Index(par, FALSE); | |
321 | if (index == wxNOT_FOUND) | |
322 | return wxEmptyString; | |
323 | if (with_commas) | |
324 | { | |
325 | // VS: backward compatibility, seems to be never used by wxHTML... | |
326 | wxString s; | |
327 | s << wxT('"') << m_ParamValues[index] << wxT('"'); | |
328 | return s; | |
329 | } | |
330 | else | |
331 | return m_ParamValues[index]; | |
332 | } | |
333 | ||
334 | int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const | |
335 | { | |
336 | wxString parval = GetParam(par); | |
337 | return wxSscanf(parval, format, param); | |
338 | } | |
339 | ||
340 | bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const | |
341 | { | |
342 | wxString str = GetParam(par); | |
343 | ||
344 | if (str.IsEmpty()) return FALSE; | |
345 | if (str.GetChar(0) == wxT('#')) | |
346 | { | |
347 | unsigned long tmp; | |
348 | if (ScanParam(par, wxT("#%lX"), &tmp) != 1) | |
349 | return FALSE; | |
350 | *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16), | |
351 | (unsigned char)((tmp & 0x00FF00) >> 8), | |
352 | (unsigned char)(tmp & 0x0000FF)); | |
353 | return TRUE; | |
354 | } | |
355 | else | |
356 | { | |
357 | // Handle colours defined in HTML 4.0: | |
358 | #define HTML_COLOUR(name,r,g,b) \ | |
359 | if (str.IsSameAs(wxT(name), FALSE)) \ | |
360 | { *clr = wxColour(r,g,b); return TRUE; } | |
361 | HTML_COLOUR("black", 0x00,0x00,0x00) | |
362 | HTML_COLOUR("silver", 0xC0,0xC0,0xC0) | |
363 | HTML_COLOUR("gray", 0x80,0x80,0x80) | |
364 | HTML_COLOUR("white", 0xFF,0xFF,0xFF) | |
365 | HTML_COLOUR("maroon", 0x80,0x00,0x00) | |
366 | HTML_COLOUR("red", 0xFF,0x00,0x00) | |
367 | HTML_COLOUR("purple", 0x80,0x00,0x80) | |
368 | HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF) | |
369 | HTML_COLOUR("green", 0x00,0x80,0x00) | |
370 | HTML_COLOUR("lime", 0x00,0xFF,0x00) | |
371 | HTML_COLOUR("olive", 0x80,0x80,0x00) | |
372 | HTML_COLOUR("yellow", 0xFF,0xFF,0x00) | |
373 | HTML_COLOUR("navy", 0x00,0x00,0x80) | |
374 | HTML_COLOUR("blue", 0x00,0x00,0xFF) | |
375 | HTML_COLOUR("teal", 0x00,0x80,0x80) | |
376 | HTML_COLOUR("aqua", 0x00,0xFF,0xFF) | |
377 | #undef HTML_COLOUR | |
378 | return FALSE; | |
379 | } | |
380 | } | |
381 | ||
382 | bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const | |
383 | { | |
384 | if (!HasParam(par)) return FALSE; | |
385 | long i; | |
386 | bool succ = GetParam(par).ToLong(&i); | |
387 | *clr = (int)i; | |
388 | return succ; | |
389 | } | |
390 | ||
391 | wxString wxHtmlTag::GetAllParams() const | |
392 | { | |
393 | // VS: this function is for backward compatiblity only, | |
394 | // never used by wxHTML | |
395 | wxString s; | |
396 | size_t cnt = m_ParamNames.GetCount(); | |
397 | for (size_t i = 0; i < cnt; i++) | |
398 | { | |
399 | s << m_ParamNames[i]; | |
400 | s << wxT('='); | |
401 | if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND) | |
402 | s << wxT('\'') << m_ParamValues[i] << wxT('\''); | |
403 | else | |
404 | s << wxT('"') << m_ParamValues[i] << wxT('"'); | |
405 | } | |
406 | return s; | |
407 | } | |
408 | ||
409 | wxHtmlTag *wxHtmlTag::GetFirstSibling() const | |
410 | { | |
411 | if (m_Parent) | |
412 | return m_Parent->m_FirstChild; | |
413 | else | |
414 | { | |
415 | wxHtmlTag *cur = (wxHtmlTag*)this; | |
416 | while (cur->m_Prev) | |
417 | cur = cur->m_Prev; | |
418 | return cur; | |
419 | } | |
420 | } | |
421 | ||
422 | wxHtmlTag *wxHtmlTag::GetLastSibling() const | |
423 | { | |
424 | if (m_Parent) | |
425 | return m_Parent->m_LastChild; | |
426 | else | |
427 | { | |
428 | wxHtmlTag *cur = (wxHtmlTag*)this; | |
429 | while (cur->m_Next) | |
430 | cur = cur->m_Next; | |
431 | return cur; | |
432 | } | |
433 | } | |
434 | ||
435 | wxHtmlTag *wxHtmlTag::GetNextTag() const | |
436 | { | |
437 | if (m_FirstChild) return m_FirstChild; | |
438 | if (m_Next) return m_Next; | |
439 | wxHtmlTag *cur = m_Parent; | |
440 | if (!cur) return NULL; | |
441 | while (cur->m_Parent && !cur->m_Next) | |
442 | cur = cur->m_Parent; | |
443 | return cur->m_Next; | |
444 | } | |
445 | ||
446 | #endif |