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