]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
a0f9b6af379ea5bd993cdb3ee02c385da76fc574
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 bool wxIsCDATAElement(const wxChar
*tag
)
59 return (wxStrcmp(tag
, _T("SCRIPT")) == 0) ||
60 (wxStrcmp(tag
, _T("STYLE")) == 0);
63 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
65 m_Cache
= new wxHtmlTagsCacheData
;
68 const wxChar
*src
= source
.c_str();
69 int lng
= source
.length();
70 wxChar tagBuffer
[256];
72 for ( int pos
= 0; pos
< lng
; pos
++ )
74 if (src
[pos
] == wxT('<')) // tag found:
76 // don't cache comment tags
77 wxString::const_iterator iter
= source
.begin() + pos
;
78 if ( wxHtmlParser::SkipCommentTag(iter
, source
.end()) )
80 pos
= iter
- source
.begin();
84 size_t tg
= Cache().size();
85 Cache().push_back(wxHtmlCacheItem());
88 Cache()[tg
].Key
= stpos
;
92 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
93 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
96 tagBuffer
[i
] = (wxChar
)wxToupper(src
[pos
]);
98 tagBuffer
[i
] = _T('\0');
100 Cache()[tg
].Name
= new wxChar
[i
+1];
101 memcpy(Cache()[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
103 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
105 if (src
[stpos
+1] == wxT('/')) // ending tag:
107 Cache()[tg
].End1
= Cache()[tg
].End2
= -2;
108 // find matching begin tag:
109 for (i
= tg
; i
>= 0; i
--)
110 if ((Cache()[i
].End1
== -1) && (wxStrcmp(Cache()[i
].Name
, tagBuffer
+1) == 0))
112 Cache()[i
].End1
= stpos
;
113 Cache()[i
].End2
= pos
+ 1;
119 Cache()[tg
].End1
= Cache()[tg
].End2
= -1;
121 if (wxIsCDATAElement(tagBuffer
))
123 // store the orig pos in case we are missing the closing
125 wxInt32 old_pos
= pos
;
126 bool foundCloseTag
= false;
128 // find next matching tag
129 int tag_len
= wxStrlen(tagBuffer
);
132 // find the ending tag
133 while (pos
+ 1 < lng
&&
134 (src
[pos
] != '<' || src
[pos
+1] != '/'))
141 while (pos
< lng
&& match_pos
< tag_len
&& src
[pos
] != '>' && src
[pos
] != '<') {
142 // cast to wxChar needed to suppress warning in
144 if ((wxChar
)wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
147 else if (src
[pos
] == wxT(' ') || src
[pos
] == wxT('\n') ||
148 src
[pos
] == wxT('\r') || src
[pos
] == wxT('\t')) {
149 // need to skip over these
158 if (match_pos
== tag_len
)
160 pos
= pos
- tag_len
- 3;
161 foundCloseTag
= true;
164 else // keep looking for the closing tag
171 // we didn't find closing tag; this means the markup
172 // is incorrect and the best thing we can do is to
173 // ignore the unclosed tag and continue parsing as if
182 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
183 for ( wxHtmlTagsCacheData::iterator i
= Cache().begin();
184 i
!= Cache().end(); ++i
)
191 wxHtmlTagsCache::~wxHtmlTagsCache()
196 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
201 if (Cache()[m_CachePos
].Key
!= at
)
203 int delta
= (at
< Cache()[m_CachePos
].Key
) ? -1 : 1;
206 if ( m_CachePos
< 0 || m_CachePos
== Cache().size() )
208 // something is very wrong with HTML, give up by returning an
209 // impossibly large value which is going to be ignored by the
218 while (Cache()[m_CachePos
].Key
!= at
);
220 *end1
= Cache()[m_CachePos
].End1
;
221 *end2
= Cache()[m_CachePos
].End2
;
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
232 const wxString
& source
, int pos
, int end_pos
,
233 wxHtmlTagsCache
*cache
,
234 wxHtmlEntitiesParser
*entParser
)
236 /* Setup DOM relations */
239 m_FirstChild
= m_LastChild
= NULL
;
243 m_Prev
= m_Parent
->m_LastChild
;
245 m_Parent
->m_FirstChild
= this;
247 m_Prev
->m_Next
= this;
248 m_Parent
->m_LastChild
= this;
253 /* Find parameters and their values: */
258 // fill-in name, params and begin pos:
261 // find tag's name and convert it to uppercase:
262 while ((i
< end_pos
) &&
263 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
264 c
!= wxT('\n') && c
!= wxT('\t') &&
267 if ((c
>= wxT('a')) && (c
<= wxT('z')))
268 c
-= (wxT('a') - wxT('A'));
272 // if the tag has parameters, read them and "normalize" them,
273 // i.e. convert to uppercase, replace whitespaces by spaces and
274 // remove whitespaces around '=':
275 if (source
[i
-1] != wxT('>'))
277 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
278 c == wxT('\n') || c == wxT('\t'))
279 wxString pname
, pvalue
;
291 state
= ST_BEFORE_NAME
;
296 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
298 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
300 m_ParamNames
.Add(pname
);
301 m_ParamValues
.Add(wxEmptyString
);
303 else if (state
== ST_VALUE
&& quote
== 0)
305 m_ParamNames
.Add(pname
);
307 m_ParamValues
.Add(entParser
->Parse(pvalue
));
309 m_ParamValues
.Add(pvalue
);
324 state
= ST_BEFORE_EQ
;
325 else if (c
== wxT('='))
326 state
= ST_BEFORE_VALUE
;
332 state
= ST_BEFORE_VALUE
;
333 else if (!IS_WHITE(c
))
335 m_ParamNames
.Add(pname
);
336 m_ParamValues
.Add(wxEmptyString
);
341 case ST_BEFORE_VALUE
:
344 if (c
== wxT('"') || c
== wxT('\''))
345 quote
= c
, pvalue
= wxEmptyString
;
347 quote
= 0, pvalue
= c
;
352 if ((quote
!= 0 && c
== quote
) ||
353 (quote
== 0 && IS_WHITE(c
)))
355 m_ParamNames
.Add(pname
);
358 // VS: backward compatibility, no real reason,
359 // but wxHTML code relies on this... :(
363 m_ParamValues
.Add(entParser
->Parse(pvalue
));
365 m_ParamValues
.Add(pvalue
);
366 state
= ST_BEFORE_NAME
;
378 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
379 if (m_End1
> end_pos
) m_End1
= end_pos
;
380 if (m_End2
> end_pos
) m_End2
= end_pos
;
383 wxHtmlTag::~wxHtmlTag()
389 t2
= t1
->GetNextSibling();
395 bool wxHtmlTag::HasParam(const wxString
& par
) const
397 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
400 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
402 int index
= m_ParamNames
.Index(par
, false);
403 if (index
== wxNOT_FOUND
)
404 return wxEmptyString
;
407 // VS: backward compatibility, seems to be never used by wxHTML...
409 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
413 return m_ParamValues
[index
];
416 int wxHtmlTag::ScanParam(const wxString
& par
,
420 wxString parval
= GetParam(par
);
421 return wxSscanf(parval
, format
, param
);
424 int wxHtmlTag::ScanParam(const wxString
& par
,
425 const wchar_t *format
,
428 wxString parval
= GetParam(par
);
429 return wxSscanf(parval
, format
, param
);
432 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
434 wxCHECK_MSG( clr
, false, _T("invalid colour argument") );
436 wxString str
= GetParam(par
);
438 // handle colours defined in HTML 4.0 first:
439 if (str
.length() > 1 && str
[0] != _T('#'))
441 #define HTML_COLOUR(name, r, g, b) \
442 if (str.IsSameAs(wxT(name), false)) \
443 { clr->Set(r, g, b); return true; }
444 HTML_COLOUR("black", 0x00,0x00,0x00)
445 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
446 HTML_COLOUR("gray", 0x80,0x80,0x80)
447 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
448 HTML_COLOUR("maroon", 0x80,0x00,0x00)
449 HTML_COLOUR("red", 0xFF,0x00,0x00)
450 HTML_COLOUR("purple", 0x80,0x00,0x80)
451 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
452 HTML_COLOUR("green", 0x00,0x80,0x00)
453 HTML_COLOUR("lime", 0x00,0xFF,0x00)
454 HTML_COLOUR("olive", 0x80,0x80,0x00)
455 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
456 HTML_COLOUR("navy", 0x00,0x00,0x80)
457 HTML_COLOUR("blue", 0x00,0x00,0xFF)
458 HTML_COLOUR("teal", 0x00,0x80,0x80)
459 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
463 // then try to parse #rrggbb representations or set from other well
464 // known names (note that this doesn't strictly conform to HTML spec,
465 // but it doesn't do real harm -- but it *must* be done after the standard
466 // colors are handled above):
473 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
475 if (!HasParam(par
)) return false;
477 bool succ
= GetParam(par
).ToLong(&i
);
482 wxString
wxHtmlTag::GetAllParams() const
484 // VS: this function is for backward compatibility only,
485 // never used by wxHTML
487 size_t cnt
= m_ParamNames
.GetCount();
488 for (size_t i
= 0; i
< cnt
; i
++)
490 s
<< m_ParamNames
[i
];
492 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
493 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
495 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
500 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
503 return m_Parent
->m_FirstChild
;
506 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
513 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
516 return m_Parent
->m_LastChild
;
519 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
526 wxHtmlTag
*wxHtmlTag::GetNextTag() const
528 if (m_FirstChild
) return m_FirstChild
;
529 if (m_Next
) return m_Next
;
530 wxHtmlTag
*cur
= m_Parent
;
531 if (!cur
) return NULL
;
532 while (cur
->m_Parent
&& !cur
->m_Next
)