]>
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"
27 #include "wx/html/htmltag.h"
28 #include "wx/html/htmlpars.h"
29 #include <stdio.h> // for vsscanf
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 struct wxHtmlCacheItem
39 // this is "pos" value passed to wxHtmlTag's constructor.
40 // it is position of '<' character of the tag
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 </...>
55 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
57 #define CACHE_INCREMENT 64
59 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
61 const wxChar
*src
= source
.c_str();
62 int i
, tg
, pos
, stpos
;
63 int lng
= source
.Length();
73 if (src
[pos
] == wxT('<')) // tag found:
75 if (m_CacheSize
% CACHE_INCREMENT
== 0)
76 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
78 m_Cache
[tg
].Key
= stpos
= pos
++;
81 src
[pos
] != wxT('>') &&
82 src
[pos
] != wxT(' ') && src
[pos
] != wxT('\r') &&
83 src
[pos
] != wxT('\n') && src
[pos
] != wxT('\t'))
85 dummy
[i
] = src
[pos
++];
86 if ((dummy
[i
] >= wxT('a')) && (dummy
[i
] <= wxT('z'))) dummy
[i
] -= (wxT('a') - wxT('A'));
90 m_Cache
[tg
].Name
= new wxChar
[i
+1];
91 memcpy(m_Cache
[tg
].Name
, dummy
, (i
+1)*sizeof(wxChar
));
93 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
95 if (src
[stpos
+1] == wxT('/')) // ending tag:
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))
102 m_Cache
[i
].End1
= stpos
;
103 m_Cache
[i
].End2
= pos
+ 1;
109 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
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
++)
119 delete[] m_Cache
[i
].Name
;
120 m_Cache
[i
].Name
= NULL
;
124 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
126 if (m_Cache
== NULL
) return;
127 if (m_Cache
[m_CachePos
].Key
!= at
)
129 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
134 while (m_Cache
[m_CachePos
].Key
!= at
);
136 *end1
= m_Cache
[m_CachePos
].End1
;
137 *end2
= m_Cache
[m_CachePos
].End2
;
143 //-----------------------------------------------------------------------------
145 //-----------------------------------------------------------------------------
147 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
149 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
150 const wxString
& source
, int pos
, int end_pos
,
151 wxHtmlTagsCache
*cache
,
152 wxHtmlEntitiesParser
*entParser
) : wxObject()
154 /* Setup DOM relations */
157 m_FirstChild
= m_LastChild
= NULL
;
161 m_Prev
= m_Parent
->m_LastChild
;
163 m_Parent
->m_FirstChild
= this;
165 m_Prev
->m_Next
= this;
166 m_Parent
->m_LastChild
= this;
171 /* Find parameters and their values: */
176 // fill-in name, params and begin pos:
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') &&
185 if ((c
>= wxT('a')) && (c
<= wxT('z')))
186 c
-= (wxT('a') - wxT('A'));
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('>'))
195 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
196 c == wxT('\n') || c == wxT('\t'))
197 wxString pname
, pvalue
;
209 state
= ST_BEFORE_NAME
;
214 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
216 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
218 m_ParamNames
.Add(pname
);
219 m_ParamValues
.Add(wxEmptyString
);
221 else if (state
== ST_VALUE
&& quote
== 0)
223 m_ParamNames
.Add(pname
);
225 m_ParamValues
.Add(entParser
->Parse(pvalue
));
227 m_ParamValues
.Add(pvalue
);
242 state
= ST_BEFORE_EQ
;
243 else if (c
== wxT('='))
244 state
= ST_BEFORE_VALUE
;
250 state
= ST_BEFORE_VALUE
;
251 else if (!IS_WHITE(c
))
253 m_ParamNames
.Add(pname
);
254 m_ParamValues
.Add(wxEmptyString
);
259 case ST_BEFORE_VALUE
:
262 if (c
== wxT('"') || c
== wxT('\''))
263 quote
= c
, pvalue
= wxEmptyString
;
265 quote
= 0, pvalue
= c
;
270 if ((quote
!= 0 && c
== quote
) ||
271 (quote
== 0 && IS_WHITE(c
)))
273 m_ParamNames
.Add(pname
);
276 // VS: backward compatibility, no real reason,
277 // but wxHTML code relies on this... :(
281 m_ParamValues
.Add(entParser
->Parse(pvalue
));
283 m_ParamValues
.Add(pvalue
);
284 state
= ST_BEFORE_NAME
;
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
;
301 wxHtmlTag::~wxHtmlTag()
307 t2
= t1
->GetNextSibling();
313 bool wxHtmlTag::HasParam(const wxString
& par
) const
315 return (m_ParamNames
.Index(par
, FALSE
) != wxNOT_FOUND
);
318 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
320 int index
= m_ParamNames
.Index(par
, FALSE
);
321 if (index
== wxNOT_FOUND
)
322 return wxEmptyString
;
325 // VS: backward compatibility, seems to be never used by wxHTML...
327 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
331 return m_ParamValues
[index
];
334 int wxHtmlTag::ScanParam(const wxString
& par
, wxChar
*format
, void *param
) const
336 wxString parval
= GetParam(par
);
337 return wxSscanf(parval
, format
, param
);
340 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
342 wxString str
= GetParam(par
);
344 if (str
.IsEmpty()) return FALSE
;
345 if (str
.GetChar(0) == wxT('#'))
348 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
350 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
351 (unsigned char)((tmp
& 0x00FF00) >> 8),
352 (unsigned char)(tmp
& 0x0000FF));
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)
382 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
384 if (!HasParam(par
)) return FALSE
;
386 bool succ
= GetParam(par
).ToLong(&i
);
391 wxString
wxHtmlTag::GetAllParams() const
393 // VS: this function is for backward compatiblity only,
394 // never used by wxHTML
396 size_t cnt
= m_ParamNames
.GetCount();
397 for (size_t i
= 0; i
< cnt
; i
++)
399 s
<< m_ParamNames
[i
];
401 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
402 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
404 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
409 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
412 return m_Parent
->m_FirstChild
;
415 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
422 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
425 return m_Parent
->m_LastChild
;
428 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
435 wxHtmlTag
*wxHtmlTag::GetNextTag() const
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
)