]>
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
39 wxString::const_iterator Key
;
44 Type_Normal
, // normal tag with a matching ending tag
45 Type_NoMatchingEndingTag
, // there's no ending tag for this tag
46 Type_EndingTag
// this is ending tag </..>
50 // end positions for the tag:
51 // end1 is '<' of ending tag,
52 // end2 is '>' or both are
53 wxString::const_iterator End1
, End2
;
59 // NB: this is an empty class and not typedef because of forward declaration
60 class wxHtmlTagsCacheData
: public wxVector
<wxHtmlCacheItem
>
64 bool wxIsCDATAElement(const wxChar
*tag
)
66 return (wxStrcmp(tag
, wxT("SCRIPT")) == 0) ||
67 (wxStrcmp(tag
, wxT("STYLE")) == 0);
70 bool wxIsCDATAElement(const wxString
& tag
)
72 return (wxStrcmp(tag
.wx_str(), wxS("SCRIPT")) == 0) ||
73 (wxStrcmp(tag
.wx_str(), wxS("STYLE")) == 0);
76 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
78 m_Cache
= new wxHtmlTagsCacheData
;
81 wxChar tagBuffer
[256];
83 const wxString::const_iterator end
= source
.end();
84 for ( wxString::const_iterator pos
= source
.begin(); pos
< end
; ++pos
)
86 if (*pos
== wxT('<')) // tag found:
88 // don't cache comment tags
89 if ( wxHtmlParser::SkipCommentTag(pos
, source
.end()) )
92 size_t tg
= Cache().size();
93 Cache().push_back(wxHtmlCacheItem());
95 wxString::const_iterator stpos
= pos
++;
96 Cache()[tg
].Key
= stpos
;
100 pos
< end
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
101 *pos
!= wxT('>') && !wxIsspace(*pos
);
104 tagBuffer
[i
] = (wxChar
)wxToupper(*pos
);
106 tagBuffer
[i
] = wxT('\0');
108 Cache()[tg
].Name
= new wxChar
[i
+1];
109 memcpy(Cache()[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
111 while (pos
< end
&& *pos
!= wxT('>'))
114 if ((stpos
+1) < end
&& *(stpos
+1) == wxT('/')) // ending tag:
116 Cache()[tg
].type
= wxHtmlCacheItem::Type_EndingTag
;
117 // find matching begin tag:
118 for (i
= tg
; i
>= 0; i
--)
120 if ((Cache()[i
].type
== wxHtmlCacheItem::Type_NoMatchingEndingTag
) && (wxStrcmp(Cache()[i
].Name
, tagBuffer
+1) == 0))
122 Cache()[i
].type
= wxHtmlCacheItem::Type_Normal
;
123 Cache()[i
].End1
= stpos
;
124 Cache()[i
].End2
= pos
+ 1;
131 Cache()[tg
].type
= wxHtmlCacheItem::Type_NoMatchingEndingTag
;
133 if (wxIsCDATAElement(tagBuffer
))
135 // store the orig pos in case we are missing the closing
137 const wxString::const_iterator old_pos
= pos
;
138 bool foundCloseTag
= false;
140 // find next matching tag
141 int tag_len
= wxStrlen(tagBuffer
);
144 // find the ending tag
145 while (pos
+ 1 < end
&&
146 (*pos
!= '<' || *(pos
+1) != '/'))
153 while (pos
< end
&& match_pos
< tag_len
)
156 if ( c
== '>' || c
== '<' )
159 // cast to wxChar needed to suppress warning in
161 if ((wxChar
)wxToupper(c
) == tagBuffer
[match_pos
])
165 else if (c
== wxT(' ') || c
== wxT('\n') ||
166 c
== wxT('\r') || c
== wxT('\t'))
168 // need to skip over these
178 if (match_pos
== tag_len
)
180 pos
= pos
- tag_len
- 3;
181 foundCloseTag
= true;
184 else // keep looking for the closing tag
191 // we didn't find closing tag; this means the markup
192 // is incorrect and the best thing we can do is to
193 // ignore the unclosed tag and continue parsing as if
202 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
203 for ( wxHtmlTagsCacheData::iterator i
= Cache().begin();
204 i
!= Cache().end(); ++i
)
211 wxHtmlTagsCache::~wxHtmlTagsCache()
216 void wxHtmlTagsCache::QueryTag(const wxString::const_iterator
& at
,
217 const wxString::const_iterator
& inputEnd
,
218 wxString::const_iterator
*end1
,
219 wxString::const_iterator
*end2
,
225 if (Cache()[m_CachePos
].Key
!= at
)
227 int delta
= (at
< Cache()[m_CachePos
].Key
) ? -1 : 1;
232 if ( m_CachePos
< 0 || m_CachePos
>= (int)Cache().size() )
234 if ( m_CachePos
< 0 )
237 m_CachePos
= Cache().size() - 1;
238 // something is very wrong with HTML, give up by returning an
239 // impossibly large value which is going to be ignored by the
247 while (Cache()[m_CachePos
].Key
!= at
);
250 switch ( Cache()[m_CachePos
].type
)
252 case wxHtmlCacheItem::Type_Normal
:
253 *end1
= Cache()[m_CachePos
].End1
;
254 *end2
= Cache()[m_CachePos
].End2
;
258 case wxHtmlCacheItem::Type_EndingTag
:
259 wxFAIL_MSG("QueryTag called for ending tag - can't be");
260 // but if it does happen, fall through, better than crashing
262 case wxHtmlCacheItem::Type_NoMatchingEndingTag
:
263 // If input HTML is invalid and there's no closing tag for this
264 // one, pretend that it runs all the way to the end of input
275 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
279 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
280 const wxString
*source
,
281 const wxString::const_iterator
& pos
,
282 const wxString::const_iterator
& end_pos
,
283 wxHtmlTagsCache
*cache
,
284 wxHtmlEntitiesParser
*entParser
)
286 /* Setup DOM relations */
289 m_FirstChild
= m_LastChild
= NULL
;
293 m_Prev
= m_Parent
->m_LastChild
;
295 m_Parent
->m_FirstChild
= this;
297 m_Prev
->m_Next
= this;
298 m_Parent
->m_LastChild
= this;
303 /* Find parameters and their values: */
305 wxChar c
wxDUMMY_INITIALIZE(0);
307 // fill-in name, params and begin pos:
308 wxString::const_iterator
i(pos
+1);
310 // find tag's name and convert it to uppercase:
311 while ((i
< end_pos
) &&
312 ((c
= *(i
++)) != wxT(' ') && c
!= wxT('\r') &&
313 c
!= wxT('\n') && c
!= wxT('\t') &&
314 c
!= wxT('>') && c
!= wxT('/')))
316 if ((c
>= wxT('a')) && (c
<= wxT('z')))
317 c
-= (wxT('a') - wxT('A'));
321 // if the tag has parameters, read them and "normalize" them,
322 // i.e. convert to uppercase, replace whitespaces by spaces and
323 // remove whitespaces around '=':
324 if (*(i
-1) != wxT('>'))
326 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
327 c == wxT('\n') || c == wxT('\t'))
328 wxString pname
, pvalue
;
340 state
= ST_BEFORE_NAME
;
345 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
347 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
349 m_ParamNames
.Add(pname
);
350 m_ParamValues
.Add(wxGetEmptyString());
352 else if (state
== ST_VALUE
&& quote
== 0)
354 m_ParamNames
.Add(pname
);
356 m_ParamValues
.Add(entParser
->Parse(pvalue
));
358 m_ParamValues
.Add(pvalue
);
373 state
= ST_BEFORE_EQ
;
374 else if (c
== wxT('='))
375 state
= ST_BEFORE_VALUE
;
381 state
= ST_BEFORE_VALUE
;
382 else if (!IS_WHITE(c
))
384 m_ParamNames
.Add(pname
);
385 m_ParamValues
.Add(wxGetEmptyString());
390 case ST_BEFORE_VALUE
:
393 if (c
== wxT('"') || c
== wxT('\''))
394 quote
= c
, pvalue
= wxGetEmptyString();
396 quote
= 0, pvalue
= c
;
401 if ((quote
!= 0 && c
== quote
) ||
402 (quote
== 0 && IS_WHITE(c
)))
404 m_ParamNames
.Add(pname
);
407 // VS: backward compatibility, no real reason,
408 // but wxHTML code relies on this... :(
412 m_ParamValues
.Add(entParser
->Parse(pvalue
));
414 m_ParamValues
.Add(pvalue
);
415 state
= ST_BEFORE_NAME
;
426 cache
->QueryTag(pos
, source
->end(), &m_End1
, &m_End2
, &m_hasEnding
);
427 if (m_End1
> end_pos
) m_End1
= end_pos
;
428 if (m_End2
> end_pos
) m_End2
= end_pos
;
430 #if WXWIN_COMPATIBILITY_2_8
431 m_sourceStart
= source
->begin();
435 wxHtmlTag::~wxHtmlTag()
441 t2
= t1
->GetNextSibling();
447 bool wxHtmlTag::HasParam(const wxString
& par
) const
449 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
452 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_quotes
) const
454 int index
= m_ParamNames
.Index(par
, false);
455 if (index
== wxNOT_FOUND
)
456 return wxGetEmptyString();
459 // VS: backward compatibility, seems to be never used by wxHTML...
461 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
465 return m_ParamValues
[index
];
468 int wxHtmlTag::ScanParam(const wxString
& par
,
472 wxString parval
= GetParam(par
);
473 return wxSscanf(parval
, format
, param
);
476 int wxHtmlTag::ScanParam(const wxString
& par
,
477 const wchar_t *format
,
480 wxString parval
= GetParam(par
);
481 return wxSscanf(parval
, format
, param
);
484 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
486 wxCHECK_MSG( clr
, false, wxT("invalid colour argument") );
488 wxString str
= GetParam(par
);
490 // handle colours defined in HTML 4.0 first:
491 if (str
.length() > 1 && str
[0] != wxT('#'))
493 #define HTML_COLOUR(name, r, g, b) \
494 if (str.IsSameAs(wxS(name), false)) \
495 { clr->Set(r, g, b); return true; }
496 HTML_COLOUR("black", 0x00,0x00,0x00)
497 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
498 HTML_COLOUR("gray", 0x80,0x80,0x80)
499 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
500 HTML_COLOUR("maroon", 0x80,0x00,0x00)
501 HTML_COLOUR("red", 0xFF,0x00,0x00)
502 HTML_COLOUR("purple", 0x80,0x00,0x80)
503 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
504 HTML_COLOUR("green", 0x00,0x80,0x00)
505 HTML_COLOUR("lime", 0x00,0xFF,0x00)
506 HTML_COLOUR("olive", 0x80,0x80,0x00)
507 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
508 HTML_COLOUR("navy", 0x00,0x00,0x80)
509 HTML_COLOUR("blue", 0x00,0x00,0xFF)
510 HTML_COLOUR("teal", 0x00,0x80,0x80)
511 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
515 // then try to parse #rrggbb representations or set from other well
516 // known names (note that this doesn't strictly conform to HTML spec,
517 // but it doesn't do real harm -- but it *must* be done after the standard
518 // colors are handled above):
525 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
527 if ( !HasParam(par
) )
531 if ( !GetParam(par
).ToLong(&i
) )
538 wxString
wxHtmlTag::GetAllParams() const
540 // VS: this function is for backward compatibility only,
541 // never used by wxHTML
543 size_t cnt
= m_ParamNames
.GetCount();
544 for (size_t i
= 0; i
< cnt
; i
++)
546 s
<< m_ParamNames
[i
];
548 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
549 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
551 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
556 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
559 return m_Parent
->m_FirstChild
;
562 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
569 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
572 return m_Parent
->m_LastChild
;
575 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
582 wxHtmlTag
*wxHtmlTag::GetNextTag() const
584 if (m_FirstChild
) return m_FirstChild
;
585 if (m_Next
) return m_Next
;
586 wxHtmlTag
*cur
= m_Parent
;
587 if (!cur
) return NULL
;
588 while (cur
->m_Parent
&& !cur
->m_Next
)