]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmltag.cpp
Fixed wxscintilla compilation for wxDFB.
[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
b4f4d3dd 20#ifndef WX_PRECOMP
7cf41a5d 21 #include "wx/colour.h"
193d0c93 22 #include "wx/wxcrtvararg.h"
5526e819
VS
23#endif
24
daa616fc 25#include "wx/html/htmlpars.h"
f68e16c5
VZ
26#include "wx/html/styleparams.h"
27
4fe7567d
VS
28#include "wx/vector.h"
29
7e1e0960 30#include <stdio.h> // for vsscanf
5526e819
VS
31#include <stdarg.h>
32
5526e819
VS
33//-----------------------------------------------------------------------------
34// wxHtmlTagsCache
35//-----------------------------------------------------------------------------
36
5e8e25e7
VS
37struct wxHtmlCacheItem
38{
39 // this is "pos" value passed to wxHtmlTag's constructor.
40 // it is position of '<' character of the tag
b1a3a964
VS
41 wxString::const_iterator Key;
42
43 // Tag type
44 enum Type
45 {
46 Type_Normal, // normal tag with a matching ending tag
47 Type_NoMatchingEndingTag, // there's no ending tag for this tag
48 Type_EndingTag // this is ending tag </..>
49 };
50 Type type;
5e8e25e7
VS
51
52 // end positions for the tag:
53 // end1 is '<' of ending tag,
54 // end2 is '>' or both are
b1a3a964 55 wxString::const_iterator End1, End2;
5e8e25e7
VS
56
57 // name of this tag
58 wxChar *Name;
59};
60
4fe7567d
VS
61// NB: this is an empty class and not typedef because of forward declaration
62class wxHtmlTagsCacheData : public wxVector<wxHtmlCacheItem>
63{
64};
5e8e25e7 65
07cc7ddc 66bool wxIsCDATAElement(const wxChar *tag)
7c6cd4a8 67{
9a83f860
VZ
68 return (wxStrcmp(tag, wxT("SCRIPT")) == 0) ||
69 (wxStrcmp(tag, wxT("STYLE")) == 0);
7c6cd4a8
VS
70}
71
b1a3a964
VS
72bool wxIsCDATAElement(const wxString& tag)
73{
d9359369
VS
74 return (wxStrcmp(tag.wx_str(), wxS("SCRIPT")) == 0) ||
75 (wxStrcmp(tag.wx_str(), wxS("STYLE")) == 0);
b1a3a964
VS
76}
77
5526e819
VS
78wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source)
79{
4fe7567d
VS
80 m_Cache = new wxHtmlTagsCacheData;
81 m_CachePos = 0;
82
8cd82622 83 wxChar tagBuffer[256];
5526e819 84
b1a3a964
VS
85 const wxString::const_iterator end = source.end();
86 for ( wxString::const_iterator pos = source.begin(); pos < end; ++pos )
4f9297b0 87 {
b1a3a964 88 if (*pos == wxT('<')) // tag found:
a914db0f 89 {
4609ee2e 90 // don't cache comment tags
b1a3a964 91 if ( wxHtmlParser::SkipCommentTag(pos, source.end()) )
4609ee2e 92 continue;
4609ee2e 93
4fe7567d
VS
94 size_t tg = Cache().size();
95 Cache().push_back(wxHtmlCacheItem());
96
b1a3a964 97 wxString::const_iterator stpos = pos++;
4fe7567d 98 Cache()[tg].Key = stpos;
8cd82622 99
4f22f506 100 int i;
8cd82622 101 for ( i = 0;
b1a3a964
VS
102 pos < end && i < (int)WXSIZEOF(tagBuffer) - 1 &&
103 *pos != wxT('>') && !wxIsspace(*pos);
104 ++i, ++pos )
a914db0f 105 {
b1a3a964 106 tagBuffer[i] = (wxChar)wxToupper(*pos);
5526e819 107 }
9a83f860 108 tagBuffer[i] = wxT('\0');
8cd82622 109
4fe7567d
VS
110 Cache()[tg].Name = new wxChar[i+1];
111 memcpy(Cache()[tg].Name, tagBuffer, (i+1)*sizeof(wxChar));
5526e819 112
b1a3a964
VS
113 while (pos < end && *pos != wxT('>'))
114 ++pos;
5526e819 115
b1a3a964 116 if ((stpos+1) < end && *(stpos+1) == wxT('/')) // ending tag:
a914db0f 117 {
b1a3a964 118 Cache()[tg].type = wxHtmlCacheItem::Type_EndingTag;
5526e819
VS
119 // find matching begin tag:
120 for (i = tg; i >= 0; i--)
b1a3a964
VS
121 {
122 if ((Cache()[i].type == wxHtmlCacheItem::Type_NoMatchingEndingTag) && (wxStrcmp(Cache()[i].Name, tagBuffer+1) == 0))
a914db0f 123 {
b1a3a964 124 Cache()[i].type = wxHtmlCacheItem::Type_Normal;
4fe7567d
VS
125 Cache()[i].End1 = stpos;
126 Cache()[i].End2 = pos + 1;
5526e819
VS
127 break;
128 }
b1a3a964 129 }
5526e819 130 }
8cd82622 131 else
a914db0f 132 {
b1a3a964 133 Cache()[tg].type = wxHtmlCacheItem::Type_NoMatchingEndingTag;
7448de8d 134
7c6cd4a8
VS
135 if (wxIsCDATAElement(tagBuffer))
136 {
313ffa19
VS
137 // store the orig pos in case we are missing the closing
138 // tag (see below)
b1a3a964 139 const wxString::const_iterator old_pos = pos;
313ffa19 140 bool foundCloseTag = false;
7448de8d 141
7c6cd4a8
VS
142 // find next matching tag
143 int tag_len = wxStrlen(tagBuffer);
b1a3a964 144 while (pos < end)
7c6cd4a8
VS
145 {
146 // find the ending tag
b1a3a964
VS
147 while (pos + 1 < end &&
148 (*pos != '<' || *(pos+1) != '/'))
7c6cd4a8 149 ++pos;
b1a3a964 150 if (*pos == '<')
7c6cd4a8 151 ++pos;
d1da8872 152
7c6cd4a8
VS
153 // see if it matches
154 int match_pos = 0;
b1a3a964
VS
155 while (pos < end && match_pos < tag_len )
156 {
157 wxChar c = *pos;
158 if ( c == '>' || c == '<' )
159 break;
160
5447d1b4
VZ
161 // cast to wxChar needed to suppress warning in
162 // Unicode build
b1a3a964
VS
163 if ((wxChar)wxToupper(c) == tagBuffer[match_pos])
164 {
7c6cd4a8 165 ++match_pos;
d1da8872 166 }
b1a3a964
VS
167 else if (c == wxT(' ') || c == wxT('\n') ||
168 c == wxT('\r') || c == wxT('\t'))
169 {
7c6cd4a8
VS
170 // need to skip over these
171 }
b1a3a964
VS
172 else
173 {
7c6cd4a8
VS
174 match_pos = 0;
175 }
176 ++pos;
177 }
178
179 // found a match
7448de8d 180 if (match_pos == tag_len)
313ffa19 181 {
b5d464b9 182 pos = pos - tag_len - 3;
313ffa19 183 foundCloseTag = true;
7c6cd4a8
VS
184 break;
185 }
313ffa19
VS
186 else // keep looking for the closing tag
187 {
7c6cd4a8
VS
188 ++pos;
189 }
190 }
313ffa19
VS
191 if (!foundCloseTag)
192 {
193 // we didn't find closing tag; this means the markup
194 // is incorrect and the best thing we can do is to
195 // ignore the unclosed tag and continue parsing as if
196 // it didn't exist:
197 pos = old_pos;
198 }
7c6cd4a8 199 }
5526e819
VS
200 }
201 }
5526e819
VS
202 }
203
204 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
4fe7567d
VS
205 for ( wxHtmlTagsCacheData::iterator i = Cache().begin();
206 i != Cache().end(); ++i )
4f9297b0 207 {
5276b0a5 208 wxDELETEA(i->Name);
5526e819
VS
209 }
210}
211
4fe7567d
VS
212wxHtmlTagsCache::~wxHtmlTagsCache()
213{
214 delete m_Cache;
215}
216
b1a3a964
VS
217void wxHtmlTagsCache::QueryTag(const wxString::const_iterator& at,
218 const wxString::const_iterator& inputEnd,
219 wxString::const_iterator *end1,
220 wxString::const_iterator *end2,
221 bool *hasEnding)
5526e819 222{
4fe7567d
VS
223 if (Cache().empty())
224 return;
225
226 if (Cache()[m_CachePos].Key != at)
4f9297b0 227 {
4fe7567d 228 int delta = (at < Cache()[m_CachePos].Key) ? -1 : 1;
8cd82622
VZ
229 do
230 {
b1a3a964
VS
231 m_CachePos += delta;
232
233 if ( m_CachePos < 0 || m_CachePos >= (int)Cache().size() )
10b9be32 234 {
b1a3a964
VS
235 if ( m_CachePos < 0 )
236 m_CachePos = 0;
237 else
238 m_CachePos = Cache().size() - 1;
10b9be32
VZ
239 // something is very wrong with HTML, give up by returning an
240 // impossibly large value which is going to be ignored by the
241 // caller
242 *end1 =
b1a3a964
VS
243 *end2 = inputEnd;
244 *hasEnding = true;
10b9be32
VZ
245 return;
246 }
daa616fc 247 }
4fe7567d 248 while (Cache()[m_CachePos].Key != at);
5526e819 249 }
61679695
VS
250
251 switch ( Cache()[m_CachePos].type )
252 {
253 case wxHtmlCacheItem::Type_Normal:
254 *end1 = Cache()[m_CachePos].End1;
255 *end2 = Cache()[m_CachePos].End2;
256 *hasEnding = true;
257 break;
258
259 case wxHtmlCacheItem::Type_EndingTag:
260 wxFAIL_MSG("QueryTag called for ending tag - can't be");
261 // but if it does happen, fall through, better than crashing
262
263 case wxHtmlCacheItem::Type_NoMatchingEndingTag:
264 // If input HTML is invalid and there's no closing tag for this
265 // one, pretend that it runs all the way to the end of input
266 *end1 = inputEnd;
267 *end2 = inputEnd;
268 *hasEnding = false;
269 break;
270 }
5526e819
VS
271}
272
273
274
275
276//-----------------------------------------------------------------------------
277// wxHtmlTag
278//-----------------------------------------------------------------------------
279
211dfedd 280wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
b1a3a964
VS
281 const wxString *source,
282 const wxString::const_iterator& pos,
283 const wxString::const_iterator& end_pos,
daa616fc 284 wxHtmlTagsCache *cache,
7da48d49 285 wxHtmlEntitiesParser *entParser)
5526e819 286{
211dfedd
VS
287 /* Setup DOM relations */
288
289 m_Next = NULL;
290 m_FirstChild = m_LastChild = NULL;
291 m_Parent = parent;
292 if (parent)
293 {
294 m_Prev = m_Parent->m_LastChild;
295 if (m_Prev == NULL)
296 m_Parent->m_FirstChild = this;
297 else
298 m_Prev->m_Next = this;
299 m_Parent->m_LastChild = this;
300 }
301 else
302 m_Prev = NULL;
303
304 /* Find parameters and their values: */
8cd82622 305
76de2296 306 wxChar c wxDUMMY_INITIALIZE(0);
5526e819
VS
307
308 // fill-in name, params and begin pos:
b1a3a964 309 wxString::const_iterator i(pos+1);
5526e819 310
b076dc01 311 // find tag's name and convert it to uppercase:
8cd82622 312 while ((i < end_pos) &&
b1a3a964 313 ((c = *(i++)) != wxT(' ') && c != wxT('\r') &&
daa616fc 314 c != wxT('\n') && c != wxT('\t') &&
ad20c567 315 c != wxT('>') && c != wxT('/')))
a914db0f 316 {
8cd82622 317 if ((c >= wxT('a')) && (c <= wxT('z')))
daa616fc
VS
318 c -= (wxT('a') - wxT('A'));
319 m_Name << c;
5526e819
VS
320 }
321
b076dc01 322 // if the tag has parameters, read them and "normalize" them,
8cd82622 323 // i.e. convert to uppercase, replace whitespaces by spaces and
b076dc01 324 // remove whitespaces around '=':
b1a3a964 325 if (*(i-1) != wxT('>'))
daa616fc
VS
326 {
327 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
328 c == wxT('\n') || c == wxT('\t'))
329 wxString pname, pvalue;
330 wxChar quote;
8cd82622 331 enum
a914db0f 332 {
8cd82622 333 ST_BEFORE_NAME = 1,
daa616fc
VS
334 ST_NAME,
335 ST_BEFORE_EQ,
336 ST_BEFORE_VALUE,
337 ST_VALUE
338 } state;
8cd82622 339
daa616fc
VS
340 quote = 0;
341 state = ST_BEFORE_NAME;
342 while (i < end_pos)
343 {
b1a3a964 344 c = *(i++);
daa616fc 345
8cd82622 346 if (c == wxT('>') && !(state == ST_VALUE && quote != 0))
a914db0f 347 {
daa616fc 348 if (state == ST_BEFORE_EQ || state == ST_NAME)
b076dc01 349 {
daa616fc 350 m_ParamNames.Add(pname);
b1a3a964 351 m_ParamValues.Add(wxGetEmptyString());
b076dc01 352 }
daa616fc
VS
353 else if (state == ST_VALUE && quote == 0)
354 {
355 m_ParamNames.Add(pname);
367c84b9
VS
356 if (entParser)
357 m_ParamValues.Add(entParser->Parse(pvalue));
358 else
359 m_ParamValues.Add(pvalue);
daa616fc
VS
360 }
361 break;
5526e819 362 }
daa616fc 363 switch (state)
a914db0f 364 {
daa616fc
VS
365 case ST_BEFORE_NAME:
366 if (!IS_WHITE(c))
367 {
368 pname = c;
369 state = ST_NAME;
370 }
371 break;
372 case ST_NAME:
373 if (IS_WHITE(c))
374 state = ST_BEFORE_EQ;
375 else if (c == wxT('='))
376 state = ST_BEFORE_VALUE;
377 else
378 pname << c;
379 break;
380 case ST_BEFORE_EQ:
381 if (c == wxT('='))
382 state = ST_BEFORE_VALUE;
383 else if (!IS_WHITE(c))
384 {
385 m_ParamNames.Add(pname);
b1a3a964 386 m_ParamValues.Add(wxGetEmptyString());
daa616fc
VS
387 pname = c;
388 state = ST_NAME;
389 }
390 break;
391 case ST_BEFORE_VALUE:
392 if (!IS_WHITE(c))
393 {
394 if (c == wxT('"') || c == wxT('\''))
b1a3a964 395 quote = c, pvalue = wxGetEmptyString();
daa616fc
VS
396 else
397 quote = 0, pvalue = c;
398 state = ST_VALUE;
399 }
400 break;
401 case ST_VALUE:
402 if ((quote != 0 && c == quote) ||
403 (quote == 0 && IS_WHITE(c)))
404 {
405 m_ParamNames.Add(pname);
406 if (quote == 0)
407 {
408 // VS: backward compatibility, no real reason,
409 // but wxHTML code relies on this... :(
410 pvalue.MakeUpper();
411 }
367c84b9
VS
412 if (entParser)
413 m_ParamValues.Add(entParser->Parse(pvalue));
414 else
415 m_ParamValues.Add(pvalue);
daa616fc
VS
416 state = ST_BEFORE_NAME;
417 }
418 else
419 pvalue << c;
420 break;
72aa4a98 421 }
5526e819 422 }
8cd82622 423
daa616fc 424 #undef IS_WHITE
7448de8d
WS
425 }
426 m_Begin = i;
b1a3a964 427 cache->QueryTag(pos, source->end(), &m_End1, &m_End2, &m_hasEnding);
7448de8d
WS
428 if (m_End1 > end_pos) m_End1 = end_pos;
429 if (m_End2 > end_pos) m_End2 = end_pos;
b1a3a964
VS
430
431#if WXWIN_COMPATIBILITY_2_8
432 m_sourceStart = source->begin();
433#endif
f68e16c5
VZ
434
435 // Try to parse any style parameters that can be handled simply by
436 // converting them to the equivalent HTML 3 attributes: this is a far cry
437 // from perfect but better than nothing.
438 static const struct EquivAttr
439 {
440 const char *style;
441 const char *attr;
442 } equivAttrs[] =
443 {
444 { "text-align", "ALIGN" },
445 { "width", "WIDTH" },
446 { "vertical-align", "VALIGN" },
447 { "background", "BGCOLOR" },
448 };
449
450 wxHtmlStyleParams styleParams(*this);
451 for ( unsigned n = 0; n < WXSIZEOF(equivAttrs); n++ )
452 {
453 const EquivAttr& ea = equivAttrs[n];
454 if ( styleParams.HasParam(ea.style) && !HasParam(ea.attr) )
455 {
456 m_ParamNames.Add(ea.attr);
457 m_ParamValues.Add(styleParams.GetParam(ea.style));
458 }
459 }
5526e819
VS
460}
461
211dfedd
VS
462wxHtmlTag::~wxHtmlTag()
463{
0d58bb65
VS
464 wxHtmlTag *t1, *t2;
465 t1 = m_FirstChild;
466 while (t1)
467 {
468 t2 = t1->GetNextSibling();
469 delete t1;
470 t1 = t2;
471 }
211dfedd
VS
472}
473
5526e819
VS
474bool wxHtmlTag::HasParam(const wxString& par) const
475{
8703bc01 476 return (m_ParamNames.Index(par, false) != wxNOT_FOUND);
5526e819
VS
477}
478
614f9713 479wxString wxHtmlTag::GetParam(const wxString& par, bool with_quotes) const
5526e819 480{
8703bc01 481 int index = m_ParamNames.Index(par, false);
daa616fc 482 if (index == wxNOT_FOUND)
b1a3a964 483 return wxGetEmptyString();
614f9713 484 if (with_quotes)
4f9297b0 485 {
daa616fc
VS
486 // VS: backward compatibility, seems to be never used by wxHTML...
487 wxString s;
488 s << wxT('"') << m_ParamValues[index] << wxT('"');
489 return s;
5526e819 490 }
daa616fc
VS
491 else
492 return m_ParamValues[index];
5526e819
VS
493}
494
90350682 495int wxHtmlTag::ScanParam(const wxString& par,
d7640339
VS
496 const char *format,
497 void *param) const
498{
499 wxString parval = GetParam(par);
500 return wxSscanf(parval, format, param);
501}
502
503int wxHtmlTag::ScanParam(const wxString& par,
504 const wchar_t *format,
90350682 505 void *param) const
5526e819 506{
5526e819 507 wxString parval = GetParam(par);
161f4f73 508 return wxSscanf(parval, format, param);
5526e819
VS
509}
510
f68e16c5
VZ
511/* static */
512bool wxHtmlTag::ParseAsColour(const wxString& str, wxColour *clr)
8bd72d90 513{
9a83f860 514 wxCHECK_MSG( clr, false, wxT("invalid colour argument") );
8cd82622 515
86766dfd 516 // handle colours defined in HTML 4.0 first:
9a83f860 517 if (str.length() > 1 && str[0] != wxT('#'))
8bd72d90 518 {
d9359369
VS
519 #define HTML_COLOUR(name, r, g, b) \
520 if (str.IsSameAs(wxS(name), false)) \
86766dfd 521 { clr->Set(r, g, b); return true; }
8bd72d90
VS
522 HTML_COLOUR("black", 0x00,0x00,0x00)
523 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
524 HTML_COLOUR("gray", 0x80,0x80,0x80)
525 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
526 HTML_COLOUR("maroon", 0x80,0x00,0x00)
527 HTML_COLOUR("red", 0xFF,0x00,0x00)
528 HTML_COLOUR("purple", 0x80,0x00,0x80)
529 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
530 HTML_COLOUR("green", 0x00,0x80,0x00)
531 HTML_COLOUR("lime", 0x00,0xFF,0x00)
532 HTML_COLOUR("olive", 0x80,0x80,0x00)
533 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
534 HTML_COLOUR("navy", 0x00,0x00,0x80)
535 HTML_COLOUR("blue", 0x00,0x00,0xFF)
536 HTML_COLOUR("teal", 0x00,0x80,0x80)
537 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
538 #undef HTML_COLOUR
8bd72d90 539 }
5716a1ab 540
86766dfd
VS
541 // then try to parse #rrggbb representations or set from other well
542 // known names (note that this doesn't strictly conform to HTML spec,
543 // but it doesn't do real harm -- but it *must* be done after the standard
544 // colors are handled above):
545 if (clr->Set(str))
546 return true;
547
8703bc01 548 return false;
8bd72d90
VS
549}
550
f68e16c5
VZ
551bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
552{
553 const wxString str = GetParam(par);
554 return !str.empty() && ParseAsColour(str, clr);
555}
556
8bd72d90
VS
557bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
558{
3f6901ad
VZ
559 if ( !HasParam(par) )
560 return false;
561
8bd72d90 562 long i;
3f6901ad
VZ
563 if ( !GetParam(par).ToLong(&i) )
564 return false;
565
8bd72d90 566 *clr = (int)i;
3f6901ad 567 return true;
8bd72d90
VS
568}
569
daa616fc
VS
570wxString wxHtmlTag::GetAllParams() const
571{
3103e8a9 572 // VS: this function is for backward compatibility only,
daa616fc
VS
573 // never used by wxHTML
574 wxString s;
575 size_t cnt = m_ParamNames.GetCount();
576 for (size_t i = 0; i < cnt; i++)
577 {
578 s << m_ParamNames[i];
579 s << wxT('=');
580 if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND)
581 s << wxT('\'') << m_ParamValues[i] << wxT('\'');
582 else
583 s << wxT('"') << m_ParamValues[i] << wxT('"');
584 }
585 return s;
586}
587
211dfedd
VS
588wxHtmlTag *wxHtmlTag::GetFirstSibling() const
589{
590 if (m_Parent)
591 return m_Parent->m_FirstChild;
592 else
593 {
594 wxHtmlTag *cur = (wxHtmlTag*)this;
8cd82622 595 while (cur->m_Prev)
211dfedd
VS
596 cur = cur->m_Prev;
597 return cur;
598 }
599}
600
601wxHtmlTag *wxHtmlTag::GetLastSibling() const
602{
603 if (m_Parent)
604 return m_Parent->m_LastChild;
605 else
606 {
607 wxHtmlTag *cur = (wxHtmlTag*)this;
8cd82622 608 while (cur->m_Next)
211dfedd
VS
609 cur = cur->m_Next;
610 return cur;
611 }
612}
613
614wxHtmlTag *wxHtmlTag::GetNextTag() const
615{
616 if (m_FirstChild) return m_FirstChild;
617 if (m_Next) return m_Next;
618 wxHtmlTag *cur = m_Parent;
619 if (!cur) return NULL;
8cd82622 620 while (cur->m_Parent && !cur->m_Next)
211dfedd
VS
621 cur = cur->m_Parent;
622 return cur->m_Next;
623}
624
4d223b67 625#endif