]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
33607f7adae6a8d6ff54554caa39b0af393d7b0c
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 "htmltag.h"
15 #include "wx/wxprec.h"
27 #include "wx/html/htmltag.h"
28 #include "wx/html/htmlpars.h"
29 #include "wx/colour.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();
64 int lng
= source
.Length();
65 wxChar tagBuffer
[256];
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
++;
83 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
84 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
87 tagBuffer
[i
] = wxToupper(src
[pos
]);
89 tagBuffer
[i
] = _T('\0');
91 m_Cache
[tg
].Name
= new wxChar
[i
+1];
92 memcpy(m_Cache
[tg
].Name
, tagBuffer
, (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
, tagBuffer
+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 (int 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(wxHtmlTag
*parent
,
151 const wxString
& source
, int pos
, int end_pos
,
152 wxHtmlTagsCache
*cache
,
153 wxHtmlEntitiesParser
*entParser
) : wxObject()
155 /* Setup DOM relations */
158 m_FirstChild
= m_LastChild
= NULL
;
162 m_Prev
= m_Parent
->m_LastChild
;
164 m_Parent
->m_FirstChild
= this;
166 m_Prev
->m_Next
= this;
167 m_Parent
->m_LastChild
= this;
172 /* Find parameters and their values: */
177 // fill-in name, params and begin pos:
180 // find tag's name and convert it to uppercase:
181 while ((i
< end_pos
) &&
182 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
183 c
!= wxT('\n') && c
!= wxT('\t') &&
186 if ((c
>= wxT('a')) && (c
<= wxT('z')))
187 c
-= (wxT('a') - wxT('A'));
191 // if the tag has parameters, read them and "normalize" them,
192 // i.e. convert to uppercase, replace whitespaces by spaces and
193 // remove whitespaces around '=':
194 if (source
[i
-1] != wxT('>'))
196 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
197 c == wxT('\n') || c == wxT('\t'))
198 wxString pname
, pvalue
;
210 state
= ST_BEFORE_NAME
;
215 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
217 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
219 m_ParamNames
.Add(pname
);
220 m_ParamValues
.Add(wxEmptyString
);
222 else if (state
== ST_VALUE
&& quote
== 0)
224 m_ParamNames
.Add(pname
);
226 m_ParamValues
.Add(entParser
->Parse(pvalue
));
228 m_ParamValues
.Add(pvalue
);
243 state
= ST_BEFORE_EQ
;
244 else if (c
== wxT('='))
245 state
= ST_BEFORE_VALUE
;
251 state
= ST_BEFORE_VALUE
;
252 else if (!IS_WHITE(c
))
254 m_ParamNames
.Add(pname
);
255 m_ParamValues
.Add(wxEmptyString
);
260 case ST_BEFORE_VALUE
:
263 if (c
== wxT('"') || c
== wxT('\''))
264 quote
= c
, pvalue
= wxEmptyString
;
266 quote
= 0, pvalue
= c
;
271 if ((quote
!= 0 && c
== quote
) ||
272 (quote
== 0 && IS_WHITE(c
)))
274 m_ParamNames
.Add(pname
);
277 // VS: backward compatibility, no real reason,
278 // but wxHTML code relies on this... :(
282 m_ParamValues
.Add(entParser
->Parse(pvalue
));
284 m_ParamValues
.Add(pvalue
);
285 state
= ST_BEFORE_NAME
;
297 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
298 if (m_End1
> end_pos
) m_End1
= end_pos
;
299 if (m_End2
> end_pos
) m_End2
= end_pos
;
302 wxHtmlTag::~wxHtmlTag()
308 t2
= t1
->GetNextSibling();
314 bool wxHtmlTag::HasParam(const wxString
& par
) const
316 return (m_ParamNames
.Index(par
, FALSE
) != wxNOT_FOUND
);
319 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
321 int index
= m_ParamNames
.Index(par
, FALSE
);
322 if (index
== wxNOT_FOUND
)
323 return wxEmptyString
;
326 // VS: backward compatibility, seems to be never used by wxHTML...
328 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
332 return m_ParamValues
[index
];
335 int wxHtmlTag::ScanParam(const wxString
& par
,
336 const wxChar
*format
,
339 wxString parval
= GetParam(par
);
340 return wxSscanf(parval
, format
, param
);
343 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
345 wxString str
= GetParam(par
);
347 if (str
.IsEmpty()) return FALSE
;
348 if (str
.GetChar(0) == wxT('#'))
351 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
353 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
354 (unsigned char)((tmp
& 0x00FF00) >> 8),
355 (unsigned char)(tmp
& 0x0000FF));
360 // Handle colours defined in HTML 4.0:
361 #define HTML_COLOUR(name,r,g,b) \
362 if (str.IsSameAs(wxT(name), FALSE)) \
363 { *clr = wxColour(r,g,b); return TRUE; }
364 HTML_COLOUR("black", 0x00,0x00,0x00)
365 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
366 HTML_COLOUR("gray", 0x80,0x80,0x80)
367 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
368 HTML_COLOUR("maroon", 0x80,0x00,0x00)
369 HTML_COLOUR("red", 0xFF,0x00,0x00)
370 HTML_COLOUR("purple", 0x80,0x00,0x80)
371 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
372 HTML_COLOUR("green", 0x00,0x80,0x00)
373 HTML_COLOUR("lime", 0x00,0xFF,0x00)
374 HTML_COLOUR("olive", 0x80,0x80,0x00)
375 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
376 HTML_COLOUR("navy", 0x00,0x00,0x80)
377 HTML_COLOUR("blue", 0x00,0x00,0xFF)
378 HTML_COLOUR("teal", 0x00,0x80,0x80)
379 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
386 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
388 if (!HasParam(par
)) return FALSE
;
390 bool succ
= GetParam(par
).ToLong(&i
);
395 wxString
wxHtmlTag::GetAllParams() const
397 // VS: this function is for backward compatiblity only,
398 // never used by wxHTML
400 size_t cnt
= m_ParamNames
.GetCount();
401 for (size_t i
= 0; i
< cnt
; i
++)
403 s
<< m_ParamNames
[i
];
405 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
406 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
408 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
413 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
416 return m_Parent
->m_FirstChild
;
419 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
426 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
429 return m_Parent
->m_LastChild
;
432 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
439 wxHtmlTag
*wxHtmlTag::GetNextTag() const
441 if (m_FirstChild
) return m_FirstChild
;
442 if (m_Next
) return m_Next
;
443 wxHtmlTag
*cur
= m_Parent
;
444 if (!cur
) return NULL
;
445 while (cur
->m_Parent
&& !cur
->m_Next
)