]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include "wx/wxprec.h"
28 #include "wx/html/htmltag.h"
29 #include "wx/html/htmlpars.h"
30 #include <stdio.h> // for vsscanf
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 struct wxHtmlCacheItem
40 // this is "pos" value passed to wxHtmlTag's constructor.
41 // it is position of '<' character of the tag
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 </...>
56 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
58 #define CACHE_INCREMENT 64
60 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
62 const wxChar
*src
= source
.c_str();
63 int i
, tg
, pos
, stpos
;
64 int lng
= source
.Length();
74 if (src
[pos
] == wxT('<')) // tag found:
76 if (m_CacheSize
% CACHE_INCREMENT
== 0)
77 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
79 m_Cache
[tg
].Key
= stpos
= pos
++;
82 src
[pos
] != wxT('>') &&
83 src
[pos
] != wxT(' ') && src
[pos
] != wxT('\r') &&
84 src
[pos
] != wxT('\n') && src
[pos
] != wxT('\t'))
86 dummy
[i
] = src
[pos
++];
87 if ((dummy
[i
] >= wxT('a')) && (dummy
[i
] <= wxT('z'))) dummy
[i
] -= (wxT('a') - wxT('A'));
91 m_Cache
[tg
].Name
= new wxChar
[i
+1];
92 memcpy(m_Cache
[tg
].Name
, dummy
, (i
+1)*sizeof(wxChar
));
94 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
96 if (src
[stpos
+1] == wxT('/')) // ending tag:
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))
103 m_Cache
[i
].End1
= stpos
;
104 m_Cache
[i
].End2
= pos
+ 1;
110 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
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
++)
120 delete[] m_Cache
[i
].Name
;
121 m_Cache
[i
].Name
= NULL
;
125 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
127 if (m_Cache
== NULL
) return;
128 if (m_Cache
[m_CachePos
].Key
!= at
)
130 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
135 while (m_Cache
[m_CachePos
].Key
!= at
);
137 *end1
= m_Cache
[m_CachePos
].End1
;
138 *end2
= m_Cache
[m_CachePos
].End2
;
144 //-----------------------------------------------------------------------------
146 //-----------------------------------------------------------------------------
148 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
150 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
,
151 wxHtmlTagsCache
*cache
,
152 wxHtmlEntitiesParser
*entParser
) : wxObject()
157 // fill-in name, params and begin pos:
159 if (source
[i
] == wxT('/'))
160 { m_Ending
= TRUE
; i
++; }
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') &&
170 if ((c
>= wxT('a')) && (c
<= wxT('z')))
171 c
-= (wxT('a') - wxT('A'));
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('>'))
180 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
181 c == wxT('\n') || c == wxT('\t'))
182 wxString pname
, pvalue
;
194 state
= ST_BEFORE_NAME
;
199 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
201 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
203 m_ParamNames
.Add(pname
);
204 m_ParamValues
.Add(wxEmptyString
);
206 else if (state
== ST_VALUE
&& quote
== 0)
208 m_ParamNames
.Add(pname
);
209 m_ParamValues
.Add(entParser
->Parse(pvalue
));
224 state
= ST_BEFORE_EQ
;
225 else if (c
== wxT('='))
226 state
= ST_BEFORE_VALUE
;
232 state
= ST_BEFORE_VALUE
;
233 else if (!IS_WHITE(c
))
235 m_ParamNames
.Add(pname
);
236 m_ParamValues
.Add(wxEmptyString
);
241 case ST_BEFORE_VALUE
:
244 if (c
== wxT('"') || c
== wxT('\''))
245 quote
= c
, pvalue
= wxEmptyString
;
247 quote
= 0, pvalue
= c
;
252 if ((quote
!= 0 && c
== quote
) ||
253 (quote
== 0 && IS_WHITE(c
)))
255 m_ParamNames
.Add(pname
);
258 // VS: backward compatibility, no real reason,
259 // but wxHTML code relies on this... :(
262 m_ParamValues
.Add(entParser
->Parse(pvalue
));
263 state
= ST_BEFORE_NAME
;
275 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
276 if (m_End1
> end_pos
) m_End1
= end_pos
;
277 if (m_End2
> end_pos
) m_End2
= end_pos
;
280 bool wxHtmlTag::HasParam(const wxString
& par
) const
282 return (m_ParamNames
.Index(par
, FALSE
) != wxNOT_FOUND
);
285 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
287 int index
= m_ParamNames
.Index(par
, FALSE
);
288 if (index
== wxNOT_FOUND
)
289 return wxEmptyString
;
292 // VS: backward compatibility, seems to be never used by wxHTML...
294 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
298 return m_ParamValues
[index
];
301 int wxHtmlTag::ScanParam(const wxString
& par
, wxChar
*format
, void *param
) const
303 wxString parval
= GetParam(par
);
304 return wxSscanf(parval
, format
, param
);
307 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
309 wxString str
= GetParam(par
);
311 if (str
.IsEmpty()) return FALSE
;
312 if (str
.GetChar(0) == wxT('#'))
315 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
317 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
318 (unsigned char)((tmp
& 0x00FF00) >> 8),
319 (unsigned char)(tmp
& 0x0000FF));
324 // Handle colours defined in HTML 4.0:
325 #define HTML_COLOUR(name,r,g,b) \
326 if (str.IsSameAs(wxT(name), FALSE)) \
327 { *clr = wxColour(r,g,b); return TRUE; }
328 HTML_COLOUR("black", 0x00,0x00,0x00)
329 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
330 HTML_COLOUR("gray", 0x80,0x80,0x80)
331 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
332 HTML_COLOUR("maroon", 0x80,0x00,0x00)
333 HTML_COLOUR("red", 0xFF,0x00,0x00)
334 HTML_COLOUR("purple", 0x80,0x00,0x80)
335 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
336 HTML_COLOUR("green", 0x00,0x80,0x00)
337 HTML_COLOUR("lime", 0x00,0xFF,0x00)
338 HTML_COLOUR("olive", 0x80,0x80,0x00)
339 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
340 HTML_COLOUR("navy", 0x00,0x00,0x80)
341 HTML_COLOUR("blue", 0x00,0x00,0xFF)
342 HTML_COLOUR("teal", 0x00,0x80,0x80)
343 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
349 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
351 if (!HasParam(par
)) return FALSE
;
353 bool succ
= GetParam(par
).ToLong(&i
);
358 wxString
wxHtmlTag::GetAllParams() const
360 // VS: this function is for backward compatiblity only,
361 // never used by wxHTML
363 size_t cnt
= m_ParamNames
.GetCount();
364 for (size_t i
= 0; i
< cnt
; i
++)
366 s
<< m_ParamNames
[i
];
368 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
369 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
371 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');