]>
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 if (wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
137 else if (src
[pos
] == wxT(' ') || src
[pos
] == wxT('\n') ||
138 src
[pos
] == wxT('\r') || src
[pos
] == wxT('\t')) {
139 // need to skip over these
148 if (match_pos
== tag_len
) {
149 pos
= pos
- tag_len
- 3;
164 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
165 for (int i
= 0; i
< m_CacheSize
; i
++)
167 delete[] m_Cache
[i
].Name
;
168 m_Cache
[i
].Name
= NULL
;
172 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
174 if (m_Cache
== NULL
) return;
175 if (m_Cache
[m_CachePos
].Key
!= at
)
177 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
182 while (m_Cache
[m_CachePos
].Key
!= at
);
184 *end1
= m_Cache
[m_CachePos
].End1
;
185 *end2
= m_Cache
[m_CachePos
].End2
;
191 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
195 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
197 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
198 const wxString
& source
, int pos
, int end_pos
,
199 wxHtmlTagsCache
*cache
,
200 wxHtmlEntitiesParser
*entParser
) : wxObject()
202 /* Setup DOM relations */
205 m_FirstChild
= m_LastChild
= NULL
;
209 m_Prev
= m_Parent
->m_LastChild
;
211 m_Parent
->m_FirstChild
= this;
213 m_Prev
->m_Next
= this;
214 m_Parent
->m_LastChild
= this;
219 /* Find parameters and their values: */
224 // fill-in name, params and begin pos:
227 // find tag's name and convert it to uppercase:
228 while ((i
< end_pos
) &&
229 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
230 c
!= wxT('\n') && c
!= wxT('\t') &&
233 if ((c
>= wxT('a')) && (c
<= wxT('z')))
234 c
-= (wxT('a') - wxT('A'));
238 // if the tag has parameters, read them and "normalize" them,
239 // i.e. convert to uppercase, replace whitespaces by spaces and
240 // remove whitespaces around '=':
241 if (source
[i
-1] != wxT('>'))
243 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
244 c == wxT('\n') || c == wxT('\t'))
245 wxString pname
, pvalue
;
257 state
= ST_BEFORE_NAME
;
262 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
264 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
266 m_ParamNames
.Add(pname
);
267 m_ParamValues
.Add(wxEmptyString
);
269 else if (state
== ST_VALUE
&& quote
== 0)
271 m_ParamNames
.Add(pname
);
273 m_ParamValues
.Add(entParser
->Parse(pvalue
));
275 m_ParamValues
.Add(pvalue
);
290 state
= ST_BEFORE_EQ
;
291 else if (c
== wxT('='))
292 state
= ST_BEFORE_VALUE
;
298 state
= ST_BEFORE_VALUE
;
299 else if (!IS_WHITE(c
))
301 m_ParamNames
.Add(pname
);
302 m_ParamValues
.Add(wxEmptyString
);
307 case ST_BEFORE_VALUE
:
310 if (c
== wxT('"') || c
== wxT('\''))
311 quote
= c
, pvalue
= wxEmptyString
;
313 quote
= 0, pvalue
= c
;
318 if ((quote
!= 0 && c
== quote
) ||
319 (quote
== 0 && IS_WHITE(c
)))
321 m_ParamNames
.Add(pname
);
324 // VS: backward compatibility, no real reason,
325 // but wxHTML code relies on this... :(
329 m_ParamValues
.Add(entParser
->Parse(pvalue
));
331 m_ParamValues
.Add(pvalue
);
332 state
= ST_BEFORE_NAME
;
344 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
345 if (m_End1
> end_pos
) m_End1
= end_pos
;
346 if (m_End2
> end_pos
) m_End2
= end_pos
;
349 wxHtmlTag::~wxHtmlTag()
355 t2
= t1
->GetNextSibling();
361 bool wxHtmlTag::HasParam(const wxString
& par
) const
363 return (m_ParamNames
.Index(par
, FALSE
) != wxNOT_FOUND
);
366 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
368 int index
= m_ParamNames
.Index(par
, FALSE
);
369 if (index
== wxNOT_FOUND
)
370 return wxEmptyString
;
373 // VS: backward compatibility, seems to be never used by wxHTML...
375 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
379 return m_ParamValues
[index
];
382 int wxHtmlTag::ScanParam(const wxString
& par
,
383 const wxChar
*format
,
386 wxString parval
= GetParam(par
);
387 return wxSscanf(parval
, format
, param
);
390 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
392 wxString str
= GetParam(par
);
394 if (str
.IsEmpty()) return FALSE
;
395 if (str
.GetChar(0) == wxT('#'))
398 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
400 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
401 (unsigned char)((tmp
& 0x00FF00) >> 8),
402 (unsigned char)(tmp
& 0x0000FF));
407 // Handle colours defined in HTML 4.0:
408 #define HTML_COLOUR(name,r,g,b) \
409 if (str.IsSameAs(wxT(name), FALSE)) \
410 { *clr = wxColour(r,g,b); return TRUE; }
411 HTML_COLOUR("black", 0x00,0x00,0x00)
412 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
413 HTML_COLOUR("gray", 0x80,0x80,0x80)
414 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
415 HTML_COLOUR("maroon", 0x80,0x00,0x00)
416 HTML_COLOUR("red", 0xFF,0x00,0x00)
417 HTML_COLOUR("purple", 0x80,0x00,0x80)
418 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
419 HTML_COLOUR("green", 0x00,0x80,0x00)
420 HTML_COLOUR("lime", 0x00,0xFF,0x00)
421 HTML_COLOUR("olive", 0x80,0x80,0x00)
422 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
423 HTML_COLOUR("navy", 0x00,0x00,0x80)
424 HTML_COLOUR("blue", 0x00,0x00,0xFF)
425 HTML_COLOUR("teal", 0x00,0x80,0x80)
426 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
433 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
435 if (!HasParam(par
)) return FALSE
;
437 bool succ
= GetParam(par
).ToLong(&i
);
442 wxString
wxHtmlTag::GetAllParams() const
444 // VS: this function is for backward compatiblity only,
445 // never used by wxHTML
447 size_t cnt
= m_ParamNames
.GetCount();
448 for (size_t i
= 0; i
< cnt
; i
++)
450 s
<< m_ParamNames
[i
];
452 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
453 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
455 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
460 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
463 return m_Parent
->m_FirstChild
;
466 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
473 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
476 return m_Parent
->m_LastChild
;
479 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
486 wxHtmlTag
*wxHtmlTag::GetNextTag() const
488 if (m_FirstChild
) return m_FirstChild
;
489 if (m_Next
) return m_Next
;
490 wxHtmlTag
*cur
= m_Parent
;
491 if (!cur
) return NULL
;
492 while (cur
->m_Parent
&& !cur
->m_Next
)