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