]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmltag.cpp
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
18 #include "wx/html/htmltag.h"
21 #include "wx/colour.h"
22 #include "wx/wxcrtvararg.h"
25 #include "wx/html/htmlpars.h"
26 #include "wx/vector.h"
28 #include <stdio.h> // for vsscanf
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 struct wxHtmlCacheItem
37 // this is "pos" value passed to wxHtmlTag's constructor.
38 // it is position of '<' character of the tag
41 // end positions for the tag:
42 // end1 is '<' of ending tag,
43 // end2 is '>' or both are
44 // -1 if there is no ending tag for this one...
45 // or -2 if this is ending tag </...>
52 // NB: this is an empty class and not typedef because of forward declaration
53 class wxHtmlTagsCacheData
: public wxVector
<wxHtmlCacheItem
>
57 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
59 bool wxIsCDATAElement(const wxChar
*tag
)
61 return (wxStrcmp(tag
, _T("SCRIPT")) == 0) ||
62 (wxStrcmp(tag
, _T("STYLE")) == 0);
65 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
67 m_Cache
= new wxHtmlTagsCacheData
;
70 const wxChar
*src
= source
.c_str();
71 int lng
= source
.length();
72 wxChar tagBuffer
[256];
74 for ( int pos
= 0; pos
< lng
; pos
++ )
76 if (src
[pos
] == wxT('<')) // tag found:
78 // don't cache comment tags
79 wxString::const_iterator iter
= source
.begin() + pos
;
80 if ( wxHtmlParser::SkipCommentTag(iter
, source
.end()) )
82 pos
= iter
- source
.begin();
86 size_t tg
= Cache().size();
87 Cache().push_back(wxHtmlCacheItem());
90 Cache()[tg
].Key
= stpos
;
94 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
95 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
98 tagBuffer
[i
] = (wxChar
)wxToupper(src
[pos
]);
100 tagBuffer
[i
] = _T('\0');
102 Cache()[tg
].Name
= new wxChar
[i
+1];
103 memcpy(Cache()[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
105 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
107 if (src
[stpos
+1] == wxT('/')) // ending tag:
109 Cache()[tg
].End1
= Cache()[tg
].End2
= -2;
110 // find matching begin tag:
111 for (i
= tg
; i
>= 0; i
--)
112 if ((Cache()[i
].End1
== -1) && (wxStrcmp(Cache()[i
].Name
, tagBuffer
+1) == 0))
114 Cache()[i
].End1
= stpos
;
115 Cache()[i
].End2
= pos
+ 1;
121 Cache()[tg
].End1
= Cache()[tg
].End2
= -1;
123 if (wxIsCDATAElement(tagBuffer
))
125 // store the orig pos in case we are missing the closing
127 wxInt32 old_pos
= pos
;
128 bool foundCloseTag
= false;
130 // find next matching tag
131 int tag_len
= wxStrlen(tagBuffer
);
134 // find the ending tag
135 while (pos
+ 1 < lng
&&
136 (src
[pos
] != '<' || src
[pos
+1] != '/'))
143 while (pos
< lng
&& match_pos
< tag_len
&& src
[pos
] != '>' && src
[pos
] != '<') {
144 // cast to wxChar needed to suppress warning in
146 if ((wxChar
)wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
149 else if (src
[pos
] == wxT(' ') || src
[pos
] == wxT('\n') ||
150 src
[pos
] == wxT('\r') || src
[pos
] == wxT('\t')) {
151 // need to skip over these
160 if (match_pos
== tag_len
)
162 pos
= pos
- tag_len
- 3;
163 foundCloseTag
= true;
166 else // keep looking for the closing tag
173 // we didn't find closing tag; this means the markup
174 // is incorrect and the best thing we can do is to
175 // ignore the unclosed tag and continue parsing as if
184 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
185 for ( wxHtmlTagsCacheData::iterator i
= Cache().begin();
186 i
!= Cache().end(); ++i
)
193 wxHtmlTagsCache::~wxHtmlTagsCache()
198 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
203 if (Cache()[m_CachePos
].Key
!= at
)
205 int delta
= (at
< Cache()[m_CachePos
].Key
) ? -1 : 1;
208 if ( m_CachePos
< 0 || m_CachePos
== Cache().size() )
210 // something is very wrong with HTML, give up by returning an
211 // impossibly large value which is going to be ignored by the
220 while (Cache()[m_CachePos
].Key
!= at
);
222 *end1
= Cache()[m_CachePos
].End1
;
223 *end2
= Cache()[m_CachePos
].End2
;
229 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
233 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
235 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
236 const wxString
& source
, int pos
, int end_pos
,
237 wxHtmlTagsCache
*cache
,
238 wxHtmlEntitiesParser
*entParser
) : wxObject()
240 /* Setup DOM relations */
243 m_FirstChild
= m_LastChild
= NULL
;
247 m_Prev
= m_Parent
->m_LastChild
;
249 m_Parent
->m_FirstChild
= this;
251 m_Prev
->m_Next
= this;
252 m_Parent
->m_LastChild
= this;
257 /* Find parameters and their values: */
262 // fill-in name, params and begin pos:
265 // find tag's name and convert it to uppercase:
266 while ((i
< end_pos
) &&
267 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
268 c
!= wxT('\n') && c
!= wxT('\t') &&
271 if ((c
>= wxT('a')) && (c
<= wxT('z')))
272 c
-= (wxT('a') - wxT('A'));
276 // if the tag has parameters, read them and "normalize" them,
277 // i.e. convert to uppercase, replace whitespaces by spaces and
278 // remove whitespaces around '=':
279 if (source
[i
-1] != wxT('>'))
281 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
282 c == wxT('\n') || c == wxT('\t'))
283 wxString pname
, pvalue
;
295 state
= ST_BEFORE_NAME
;
300 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
302 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
304 m_ParamNames
.Add(pname
);
305 m_ParamValues
.Add(wxEmptyString
);
307 else if (state
== ST_VALUE
&& quote
== 0)
309 m_ParamNames
.Add(pname
);
311 m_ParamValues
.Add(entParser
->Parse(pvalue
));
313 m_ParamValues
.Add(pvalue
);
328 state
= ST_BEFORE_EQ
;
329 else if (c
== wxT('='))
330 state
= ST_BEFORE_VALUE
;
336 state
= ST_BEFORE_VALUE
;
337 else if (!IS_WHITE(c
))
339 m_ParamNames
.Add(pname
);
340 m_ParamValues
.Add(wxEmptyString
);
345 case ST_BEFORE_VALUE
:
348 if (c
== wxT('"') || c
== wxT('\''))
349 quote
= c
, pvalue
= wxEmptyString
;
351 quote
= 0, pvalue
= c
;
356 if ((quote
!= 0 && c
== quote
) ||
357 (quote
== 0 && IS_WHITE(c
)))
359 m_ParamNames
.Add(pname
);
362 // VS: backward compatibility, no real reason,
363 // but wxHTML code relies on this... :(
367 m_ParamValues
.Add(entParser
->Parse(pvalue
));
369 m_ParamValues
.Add(pvalue
);
370 state
= ST_BEFORE_NAME
;
382 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
383 if (m_End1
> end_pos
) m_End1
= end_pos
;
384 if (m_End2
> end_pos
) m_End2
= end_pos
;
387 wxHtmlTag::~wxHtmlTag()
393 t2
= t1
->GetNextSibling();
399 bool wxHtmlTag::HasParam(const wxString
& par
) const
401 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
404 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
406 int index
= m_ParamNames
.Index(par
, false);
407 if (index
== wxNOT_FOUND
)
408 return wxEmptyString
;
411 // VS: backward compatibility, seems to be never used by wxHTML...
413 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
417 return m_ParamValues
[index
];
420 int wxHtmlTag::ScanParam(const wxString
& par
,
424 wxString parval
= GetParam(par
);
425 return wxSscanf(parval
, format
, param
);
428 int wxHtmlTag::ScanParam(const wxString
& par
,
429 const wchar_t *format
,
432 wxString parval
= GetParam(par
);
433 return wxSscanf(parval
, format
, param
);
436 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
438 wxCHECK_MSG( clr
, false, _T("invalid colour argument") );
440 wxString str
= GetParam(par
);
442 // handle colours defined in HTML 4.0 first:
443 if (str
.length() > 1 && str
[0] != _T('#'))
445 #define HTML_COLOUR(name, r, g, b) \
446 if (str.IsSameAs(wxT(name), false)) \
447 { clr->Set(r, g, b); return true; }
448 HTML_COLOUR("black", 0x00,0x00,0x00)
449 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
450 HTML_COLOUR("gray", 0x80,0x80,0x80)
451 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
452 HTML_COLOUR("maroon", 0x80,0x00,0x00)
453 HTML_COLOUR("red", 0xFF,0x00,0x00)
454 HTML_COLOUR("purple", 0x80,0x00,0x80)
455 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
456 HTML_COLOUR("green", 0x00,0x80,0x00)
457 HTML_COLOUR("lime", 0x00,0xFF,0x00)
458 HTML_COLOUR("olive", 0x80,0x80,0x00)
459 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
460 HTML_COLOUR("navy", 0x00,0x00,0x80)
461 HTML_COLOUR("blue", 0x00,0x00,0xFF)
462 HTML_COLOUR("teal", 0x00,0x80,0x80)
463 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
467 // then try to parse #rrggbb representations or set from other well
468 // known names (note that this doesn't strictly conform to HTML spec,
469 // but it doesn't do real harm -- but it *must* be done after the standard
470 // colors are handled above):
477 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
479 if (!HasParam(par
)) return false;
481 bool succ
= GetParam(par
).ToLong(&i
);
486 wxString
wxHtmlTag::GetAllParams() const
488 // VS: this function is for backward compatibility only,
489 // never used by wxHTML
491 size_t cnt
= m_ParamNames
.GetCount();
492 for (size_t i
= 0; i
< cnt
; i
++)
494 s
<< m_ParamNames
[i
];
496 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
497 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
499 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
504 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
507 return m_Parent
->m_FirstChild
;
510 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
517 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
520 return m_Parent
->m_LastChild
;
523 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
530 wxHtmlTag
*wxHtmlTag::GetNextTag() const
532 if (m_FirstChild
) return m_FirstChild
;
533 if (m_Next
) return m_Next
;
534 wxHtmlTag
*cur
= m_Parent
;
535 if (!cur
) return NULL
;
536 while (cur
->m_Parent
&& !cur
->m_Next
)