]>
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 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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();
69 int lng
= source
.Length();
70 wxChar tagBuffer
[256];
79 if (src
[pos
] == wxT('<')) // tag found:
81 if (m_CacheSize
% CACHE_INCREMENT
== 0)
82 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
83 int tg
= m_CacheSize
++;
85 m_Cache
[tg
].Key
= stpos
;
89 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
90 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
93 tagBuffer
[i
] = (wxChar
)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 // store the orig pos in case we are missing the closing
122 wxInt32 old_pos
= pos
;
123 bool foundCloseTag
= false;
125 // find next matching tag
126 int tag_len
= wxStrlen(tagBuffer
);
129 // find the ending tag
130 while (pos
+ 1 < lng
&&
131 (src
[pos
] != '<' || src
[pos
+1] != '/'))
138 while (pos
< lng
&& match_pos
< tag_len
&& src
[pos
] != '>' && src
[pos
] != '<') {
139 // cast to wxChar needed to suppress warning in
141 if ((wxChar
)wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
144 else if (src
[pos
] == wxT(' ') || src
[pos
] == wxT('\n') ||
145 src
[pos
] == wxT('\r') || src
[pos
] == wxT('\t')) {
146 // need to skip over these
155 if (match_pos
== tag_len
)
157 pos
= pos
- tag_len
- 3;
158 foundCloseTag
= true;
161 else // keep looking for the closing tag
168 // we didn't find closing tag; this means the markup
169 // is incorrect and the best thing we can do is to
170 // ignore the unclosed tag and continue parsing as if
181 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
182 for (int i
= 0; i
< m_CacheSize
; i
++)
184 delete[] m_Cache
[i
].Name
;
185 m_Cache
[i
].Name
= NULL
;
189 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
191 if (m_Cache
== NULL
) return;
192 if (m_Cache
[m_CachePos
].Key
!= at
)
194 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
199 while (m_Cache
[m_CachePos
].Key
!= at
);
201 *end1
= m_Cache
[m_CachePos
].End1
;
202 *end2
= m_Cache
[m_CachePos
].End2
;
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
214 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
215 const wxString
& source
, int pos
, int end_pos
,
216 wxHtmlTagsCache
*cache
,
217 wxHtmlEntitiesParser
*entParser
) : wxObject()
219 /* Setup DOM relations */
222 m_FirstChild
= m_LastChild
= NULL
;
226 m_Prev
= m_Parent
->m_LastChild
;
228 m_Parent
->m_FirstChild
= this;
230 m_Prev
->m_Next
= this;
231 m_Parent
->m_LastChild
= this;
236 /* Find parameters and their values: */
241 // fill-in name, params and begin pos:
244 // find tag's name and convert it to uppercase:
245 while ((i
< end_pos
) &&
246 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
247 c
!= wxT('\n') && c
!= wxT('\t') &&
250 if ((c
>= wxT('a')) && (c
<= wxT('z')))
251 c
-= (wxT('a') - wxT('A'));
255 // if the tag has parameters, read them and "normalize" them,
256 // i.e. convert to uppercase, replace whitespaces by spaces and
257 // remove whitespaces around '=':
258 if (source
[i
-1] != wxT('>'))
260 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
261 c == wxT('\n') || c == wxT('\t'))
262 wxString pname
, pvalue
;
274 state
= ST_BEFORE_NAME
;
279 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
281 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
283 m_ParamNames
.Add(pname
);
284 m_ParamValues
.Add(wxEmptyString
);
286 else if (state
== ST_VALUE
&& quote
== 0)
288 m_ParamNames
.Add(pname
);
290 m_ParamValues
.Add(entParser
->Parse(pvalue
));
292 m_ParamValues
.Add(pvalue
);
307 state
= ST_BEFORE_EQ
;
308 else if (c
== wxT('='))
309 state
= ST_BEFORE_VALUE
;
315 state
= ST_BEFORE_VALUE
;
316 else if (!IS_WHITE(c
))
318 m_ParamNames
.Add(pname
);
319 m_ParamValues
.Add(wxEmptyString
);
324 case ST_BEFORE_VALUE
:
327 if (c
== wxT('"') || c
== wxT('\''))
328 quote
= c
, pvalue
= wxEmptyString
;
330 quote
= 0, pvalue
= c
;
335 if ((quote
!= 0 && c
== quote
) ||
336 (quote
== 0 && IS_WHITE(c
)))
338 m_ParamNames
.Add(pname
);
341 // VS: backward compatibility, no real reason,
342 // but wxHTML code relies on this... :(
346 m_ParamValues
.Add(entParser
->Parse(pvalue
));
348 m_ParamValues
.Add(pvalue
);
349 state
= ST_BEFORE_NAME
;
361 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
362 if (m_End1
> end_pos
) m_End1
= end_pos
;
363 if (m_End2
> end_pos
) m_End2
= end_pos
;
366 wxHtmlTag::~wxHtmlTag()
372 t2
= t1
->GetNextSibling();
378 bool wxHtmlTag::HasParam(const wxString
& par
) const
380 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
383 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
385 int index
= m_ParamNames
.Index(par
, false);
386 if (index
== wxNOT_FOUND
)
387 return wxEmptyString
;
390 // VS: backward compatibility, seems to be never used by wxHTML...
392 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
396 return m_ParamValues
[index
];
399 int wxHtmlTag::ScanParam(const wxString
& par
,
400 const wxChar
*format
,
403 wxString parval
= GetParam(par
);
404 return wxSscanf(parval
, format
, param
);
407 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
409 wxString str
= GetParam(par
);
411 if (str
.empty()) return false;
412 if (str
.GetChar(0) == wxT('#'))
415 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
417 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
418 (unsigned char)((tmp
& 0x00FF00) >> 8),
419 (unsigned char)(tmp
& 0x0000FF));
424 // Handle colours defined in HTML 4.0:
425 #define HTML_COLOUR(name,r,g,b) \
426 if (str.IsSameAs(wxT(name), false)) \
427 { *clr = wxColour(r,g,b); return true; }
428 HTML_COLOUR("black", 0x00,0x00,0x00)
429 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
430 HTML_COLOUR("gray", 0x80,0x80,0x80)
431 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
432 HTML_COLOUR("maroon", 0x80,0x00,0x00)
433 HTML_COLOUR("red", 0xFF,0x00,0x00)
434 HTML_COLOUR("purple", 0x80,0x00,0x80)
435 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
436 HTML_COLOUR("green", 0x00,0x80,0x00)
437 HTML_COLOUR("lime", 0x00,0xFF,0x00)
438 HTML_COLOUR("olive", 0x80,0x80,0x00)
439 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
440 HTML_COLOUR("navy", 0x00,0x00,0x80)
441 HTML_COLOUR("blue", 0x00,0x00,0xFF)
442 HTML_COLOUR("teal", 0x00,0x80,0x80)
443 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
450 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
452 if (!HasParam(par
)) return false;
454 bool succ
= GetParam(par
).ToLong(&i
);
459 wxString
wxHtmlTag::GetAllParams() const
461 // VS: this function is for backward compatiblity only,
462 // never used by wxHTML
464 size_t cnt
= m_ParamNames
.GetCount();
465 for (size_t i
= 0; i
< cnt
; i
++)
467 s
<< m_ParamNames
[i
];
469 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
470 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
472 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
477 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
480 return m_Parent
->m_FirstChild
;
483 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
490 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
493 return m_Parent
->m_LastChild
;
496 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
503 wxHtmlTag
*wxHtmlTag::GetNextTag() const
505 if (m_FirstChild
) return m_FirstChild
;
506 if (m_Next
) return m_Next
;
507 wxHtmlTag
*cur
= m_Parent
;
508 if (!cur
) return NULL
;
509 while (cur
->m_Parent
&& !cur
->m_Next
)
514 #if WXWIN_COMPATIBILITY_2_2
516 bool wxHtmlTag::IsEnding() const
521 #endif // WXWIN_COMPATIBILITY_2_2