]>
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 "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 bool wxIsCDATAElement(const wxChar
*tag
)
62 return (wxStrcmp(tag
, _T("SCRIPT")) == 0) ||
63 (wxStrcmp(tag
, _T("STYLE")) == 0);
66 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
68 const wxChar
*src
= source
.c_str();
70 int lng
= source
.Length();
71 wxChar tagBuffer
[256];
80 if (src
[pos
] == wxT('<')) // tag found:
82 if (m_CacheSize
% CACHE_INCREMENT
== 0)
83 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
85 m_Cache
[tg
].Key
= stpos
= pos
++;
89 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
90 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
93 tagBuffer
[i
] = wxToupper(src
[pos
]);
95 tagBuffer
[i
] = _T('\0');
97 m_Cache
[tg
].Name
= new wxChar
[i
+1];
98 memcpy(m_Cache
[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
100 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
102 if (src
[stpos
+1] == wxT('/')) // ending tag:
104 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
105 // find matching begin tag:
106 for (i
= tg
; i
>= 0; i
--)
107 if ((m_Cache
[i
].End1
== -1) && (wxStrcmp(m_Cache
[i
].Name
, tagBuffer
+1) == 0))
109 m_Cache
[i
].End1
= stpos
;
110 m_Cache
[i
].End2
= pos
+ 1;
116 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
118 if (wxIsCDATAElement(tagBuffer
))
120 // find next matching tag
121 int tag_len
= wxStrlen(tagBuffer
);
124 // find the ending tag
125 while (pos
+ 1 < lng
&&
126 (src
[pos
] != '<' || src
[pos
+1] != '/'))
133 while (pos
< lng
&& match_pos
< tag_len
&& src
[pos
] != '>' && src
[pos
] != '<') {
134 // cast to wxChar needed to suppress warning in
136 if ((wxChar
)wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
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
150 if (match_pos
== tag_len
) {
151 pos
= pos
- tag_len
- 3;
166 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
167 for (int i
= 0; i
< m_CacheSize
; i
++)
169 delete[] m_Cache
[i
].Name
;
170 m_Cache
[i
].Name
= NULL
;
174 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
176 if (m_Cache
== NULL
) return;
177 if (m_Cache
[m_CachePos
].Key
!= at
)
179 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
184 while (m_Cache
[m_CachePos
].Key
!= at
);
186 *end1
= m_Cache
[m_CachePos
].End1
;
187 *end2
= m_Cache
[m_CachePos
].End2
;
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
197 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
199 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
200 const wxString
& source
, int pos
, int end_pos
,
201 wxHtmlTagsCache
*cache
,
202 wxHtmlEntitiesParser
*entParser
) : wxObject()
204 /* Setup DOM relations */
207 m_FirstChild
= m_LastChild
= NULL
;
211 m_Prev
= m_Parent
->m_LastChild
;
213 m_Parent
->m_FirstChild
= this;
215 m_Prev
->m_Next
= this;
216 m_Parent
->m_LastChild
= this;
221 /* Find parameters and their values: */
226 // fill-in name, params and begin pos:
229 // find tag's name and convert it to uppercase:
230 while ((i
< end_pos
) &&
231 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
232 c
!= wxT('\n') && c
!= wxT('\t') &&
235 if ((c
>= wxT('a')) && (c
<= wxT('z')))
236 c
-= (wxT('a') - wxT('A'));
240 // if the tag has parameters, read them and "normalize" them,
241 // i.e. convert to uppercase, replace whitespaces by spaces and
242 // remove whitespaces around '=':
243 if (source
[i
-1] != wxT('>'))
245 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
246 c == wxT('\n') || c == wxT('\t'))
247 wxString pname
, pvalue
;
259 state
= ST_BEFORE_NAME
;
264 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
266 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
268 m_ParamNames
.Add(pname
);
269 m_ParamValues
.Add(wxEmptyString
);
271 else if (state
== ST_VALUE
&& quote
== 0)
273 m_ParamNames
.Add(pname
);
275 m_ParamValues
.Add(entParser
->Parse(pvalue
));
277 m_ParamValues
.Add(pvalue
);
292 state
= ST_BEFORE_EQ
;
293 else if (c
== wxT('='))
294 state
= ST_BEFORE_VALUE
;
300 state
= ST_BEFORE_VALUE
;
301 else if (!IS_WHITE(c
))
303 m_ParamNames
.Add(pname
);
304 m_ParamValues
.Add(wxEmptyString
);
309 case ST_BEFORE_VALUE
:
312 if (c
== wxT('"') || c
== wxT('\''))
313 quote
= c
, pvalue
= wxEmptyString
;
315 quote
= 0, pvalue
= c
;
320 if ((quote
!= 0 && c
== quote
) ||
321 (quote
== 0 && IS_WHITE(c
)))
323 m_ParamNames
.Add(pname
);
326 // VS: backward compatibility, no real reason,
327 // but wxHTML code relies on this... :(
331 m_ParamValues
.Add(entParser
->Parse(pvalue
));
333 m_ParamValues
.Add(pvalue
);
334 state
= ST_BEFORE_NAME
;
346 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
347 if (m_End1
> end_pos
) m_End1
= end_pos
;
348 if (m_End2
> end_pos
) m_End2
= end_pos
;
351 wxHtmlTag::~wxHtmlTag()
357 t2
= t1
->GetNextSibling();
363 bool wxHtmlTag::HasParam(const wxString
& par
) const
365 return (m_ParamNames
.Index(par
, FALSE
) != wxNOT_FOUND
);
368 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
370 int index
= m_ParamNames
.Index(par
, FALSE
);
371 if (index
== wxNOT_FOUND
)
372 return wxEmptyString
;
375 // VS: backward compatibility, seems to be never used by wxHTML...
377 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
381 return m_ParamValues
[index
];
384 int wxHtmlTag::ScanParam(const wxString
& par
,
385 const wxChar
*format
,
388 wxString parval
= GetParam(par
);
389 return wxSscanf(parval
, format
, param
);
392 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
394 wxString str
= GetParam(par
);
396 if (str
.IsEmpty()) return FALSE
;
397 if (str
.GetChar(0) == wxT('#'))
400 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
402 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
403 (unsigned char)((tmp
& 0x00FF00) >> 8),
404 (unsigned char)(tmp
& 0x0000FF));
409 // Handle colours defined in HTML 4.0:
410 #define HTML_COLOUR(name,r,g,b) \
411 if (str.IsSameAs(wxT(name), FALSE)) \
412 { *clr = wxColour(r,g,b); return TRUE; }
413 HTML_COLOUR("black", 0x00,0x00,0x00)
414 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
415 HTML_COLOUR("gray", 0x80,0x80,0x80)
416 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
417 HTML_COLOUR("maroon", 0x80,0x00,0x00)
418 HTML_COLOUR("red", 0xFF,0x00,0x00)
419 HTML_COLOUR("purple", 0x80,0x00,0x80)
420 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
421 HTML_COLOUR("green", 0x00,0x80,0x00)
422 HTML_COLOUR("lime", 0x00,0xFF,0x00)
423 HTML_COLOUR("olive", 0x80,0x80,0x00)
424 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
425 HTML_COLOUR("navy", 0x00,0x00,0x80)
426 HTML_COLOUR("blue", 0x00,0x00,0xFF)
427 HTML_COLOUR("teal", 0x00,0x80,0x80)
428 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
435 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
437 if (!HasParam(par
)) return FALSE
;
439 bool succ
= GetParam(par
).ToLong(&i
);
444 wxString
wxHtmlTag::GetAllParams() const
446 // VS: this function is for backward compatiblity only,
447 // never used by wxHTML
449 size_t cnt
= m_ParamNames
.GetCount();
450 for (size_t i
= 0; i
< cnt
; i
++)
452 s
<< m_ParamNames
[i
];
454 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
455 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
457 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
462 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
465 return m_Parent
->m_FirstChild
;
468 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
475 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
478 return m_Parent
->m_LastChild
;
481 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
488 wxHtmlTag
*wxHtmlTag::GetNextTag() const
490 if (m_FirstChild
) return m_FirstChild
;
491 if (m_Next
) return m_Next
;
492 wxHtmlTag
*cur
= m_Parent
;
493 if (!cur
) return NULL
;
494 while (cur
->m_Parent
&& !cur
->m_Next
)