]>
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/html/styleparams.h"
28 #include "wx/vector.h"
30 #include <stdio.h> // for vsscanf
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 struct wxHtmlCacheItem
39 // this is "pos" value passed to wxHtmlTag's constructor.
40 // it is position of '<' character of the tag
41 wxString::const_iterator Key
;
46 Type_Normal
, // normal tag with a matching ending tag
47 Type_NoMatchingEndingTag
, // there's no ending tag for this tag
48 Type_EndingTag
// this is ending tag </..>
52 // end positions for the tag:
53 // end1 is '<' of ending tag,
54 // end2 is '>' or both are
55 wxString::const_iterator End1
, End2
;
61 // NB: this is an empty class and not typedef because of forward declaration
62 class wxHtmlTagsCacheData
: public wxVector
<wxHtmlCacheItem
>
66 bool wxIsCDATAElement(const wxChar
*tag
)
68 return (wxStrcmp(tag
, wxT("SCRIPT")) == 0) ||
69 (wxStrcmp(tag
, wxT("STYLE")) == 0);
72 bool wxIsCDATAElement(const wxString
& tag
)
74 return (wxStrcmp(tag
.wx_str(), wxS("SCRIPT")) == 0) ||
75 (wxStrcmp(tag
.wx_str(), wxS("STYLE")) == 0);
78 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
80 m_Cache
= new wxHtmlTagsCacheData
;
83 wxChar tagBuffer
[256];
85 const wxString::const_iterator end
= source
.end();
86 for ( wxString::const_iterator pos
= source
.begin(); pos
< end
; ++pos
)
88 if (*pos
== wxT('<')) // tag found:
90 // don't cache comment tags
91 if ( wxHtmlParser::SkipCommentTag(pos
, source
.end()) )
94 size_t tg
= Cache().size();
95 Cache().push_back(wxHtmlCacheItem());
97 wxString::const_iterator stpos
= pos
++;
98 Cache()[tg
].Key
= stpos
;
102 pos
< end
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
103 *pos
!= wxT('>') && !wxIsspace(*pos
);
106 tagBuffer
[i
] = (wxChar
)wxToupper(*pos
);
108 tagBuffer
[i
] = wxT('\0');
110 Cache()[tg
].Name
= new wxChar
[i
+1];
111 memcpy(Cache()[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
113 while (pos
< end
&& *pos
!= wxT('>'))
116 if ((stpos
+1) < end
&& *(stpos
+1) == wxT('/')) // ending tag:
118 Cache()[tg
].type
= wxHtmlCacheItem::Type_EndingTag
;
119 // find matching begin tag:
120 for (i
= tg
; i
>= 0; i
--)
122 if ((Cache()[i
].type
== wxHtmlCacheItem::Type_NoMatchingEndingTag
) && (wxStrcmp(Cache()[i
].Name
, tagBuffer
+1) == 0))
124 Cache()[i
].type
= wxHtmlCacheItem::Type_Normal
;
125 Cache()[i
].End1
= stpos
;
126 Cache()[i
].End2
= pos
+ 1;
133 Cache()[tg
].type
= wxHtmlCacheItem::Type_NoMatchingEndingTag
;
135 if (wxIsCDATAElement(tagBuffer
))
137 // store the orig pos in case we are missing the closing
139 const wxString::const_iterator old_pos
= pos
;
140 bool foundCloseTag
= false;
142 // find next matching tag
143 int tag_len
= wxStrlen(tagBuffer
);
146 // find the ending tag
147 while (pos
+ 1 < end
&&
148 (*pos
!= '<' || *(pos
+1) != '/'))
155 while (pos
< end
&& match_pos
< tag_len
)
158 if ( c
== '>' || c
== '<' )
161 // cast to wxChar needed to suppress warning in
163 if ((wxChar
)wxToupper(c
) == tagBuffer
[match_pos
])
167 else if (c
== wxT(' ') || c
== wxT('\n') ||
168 c
== wxT('\r') || c
== wxT('\t'))
170 // need to skip over these
180 if (match_pos
== tag_len
)
182 pos
= pos
- tag_len
- 3;
183 foundCloseTag
= true;
186 else // keep looking for the closing tag
193 // we didn't find closing tag; this means the markup
194 // is incorrect and the best thing we can do is to
195 // ignore the unclosed tag and continue parsing as if
204 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
205 for ( wxHtmlTagsCacheData::iterator i
= Cache().begin();
206 i
!= Cache().end(); ++i
)
212 wxHtmlTagsCache::~wxHtmlTagsCache()
217 void wxHtmlTagsCache::QueryTag(const wxString::const_iterator
& at
,
218 const wxString::const_iterator
& inputEnd
,
219 wxString::const_iterator
*end1
,
220 wxString::const_iterator
*end2
,
226 if (Cache()[m_CachePos
].Key
!= at
)
228 int delta
= (at
< Cache()[m_CachePos
].Key
) ? -1 : 1;
233 if ( m_CachePos
< 0 || m_CachePos
>= (int)Cache().size() )
235 if ( m_CachePos
< 0 )
238 m_CachePos
= Cache().size() - 1;
239 // something is very wrong with HTML, give up by returning an
240 // impossibly large value which is going to be ignored by the
248 while (Cache()[m_CachePos
].Key
!= at
);
251 switch ( Cache()[m_CachePos
].type
)
253 case wxHtmlCacheItem::Type_Normal
:
254 *end1
= Cache()[m_CachePos
].End1
;
255 *end2
= Cache()[m_CachePos
].End2
;
259 case wxHtmlCacheItem::Type_EndingTag
:
260 wxFAIL_MSG("QueryTag called for ending tag - can't be");
261 // but if it does happen, fall through, better than crashing
263 case wxHtmlCacheItem::Type_NoMatchingEndingTag
:
264 // If input HTML is invalid and there's no closing tag for this
265 // one, pretend that it runs all the way to the end of input
276 //-----------------------------------------------------------------------------
278 //-----------------------------------------------------------------------------
280 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
281 const wxString
*source
,
282 const wxString::const_iterator
& pos
,
283 const wxString::const_iterator
& end_pos
,
284 wxHtmlTagsCache
*cache
,
285 wxHtmlEntitiesParser
*entParser
)
287 /* Setup DOM relations */
290 m_FirstChild
= m_LastChild
= NULL
;
294 m_Prev
= m_Parent
->m_LastChild
;
296 m_Parent
->m_FirstChild
= this;
298 m_Prev
->m_Next
= this;
299 m_Parent
->m_LastChild
= this;
304 /* Find parameters and their values: */
306 wxChar c
wxDUMMY_INITIALIZE(0);
308 // fill-in name, params and begin pos:
309 wxString::const_iterator
i(pos
+1);
311 // find tag's name and convert it to uppercase:
312 while ((i
< end_pos
) &&
313 ((c
= *(i
++)) != wxT(' ') && c
!= wxT('\r') &&
314 c
!= wxT('\n') && c
!= wxT('\t') &&
315 c
!= wxT('>') && c
!= wxT('/')))
317 if ((c
>= wxT('a')) && (c
<= wxT('z')))
318 c
-= (wxT('a') - wxT('A'));
322 // if the tag has parameters, read them and "normalize" them,
323 // i.e. convert to uppercase, replace whitespaces by spaces and
324 // remove whitespaces around '=':
325 if (*(i
-1) != wxT('>'))
327 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
328 c == wxT('\n') || c == wxT('\t'))
329 wxString pname
, pvalue
;
341 state
= ST_BEFORE_NAME
;
346 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
348 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
350 m_ParamNames
.Add(pname
);
351 m_ParamValues
.Add(wxGetEmptyString());
353 else if (state
== ST_VALUE
&& quote
== 0)
355 m_ParamNames
.Add(pname
);
357 m_ParamValues
.Add(entParser
->Parse(pvalue
));
359 m_ParamValues
.Add(pvalue
);
374 state
= ST_BEFORE_EQ
;
375 else if (c
== wxT('='))
376 state
= ST_BEFORE_VALUE
;
382 state
= ST_BEFORE_VALUE
;
383 else if (!IS_WHITE(c
))
385 m_ParamNames
.Add(pname
);
386 m_ParamValues
.Add(wxGetEmptyString());
391 case ST_BEFORE_VALUE
:
394 if (c
== wxT('"') || c
== wxT('\''))
395 quote
= c
, pvalue
= wxGetEmptyString();
397 quote
= 0, pvalue
= c
;
402 if ((quote
!= 0 && c
== quote
) ||
403 (quote
== 0 && IS_WHITE(c
)))
405 m_ParamNames
.Add(pname
);
408 // VS: backward compatibility, no real reason,
409 // but wxHTML code relies on this... :(
413 m_ParamValues
.Add(entParser
->Parse(pvalue
));
415 m_ParamValues
.Add(pvalue
);
416 state
= ST_BEFORE_NAME
;
427 cache
->QueryTag(pos
, source
->end(), &m_End1
, &m_End2
, &m_hasEnding
);
428 if (m_End1
> end_pos
) m_End1
= end_pos
;
429 if (m_End2
> end_pos
) m_End2
= end_pos
;
431 #if WXWIN_COMPATIBILITY_2_8
432 m_sourceStart
= source
->begin();
435 // Try to parse any style parameters that can be handled simply by
436 // converting them to the equivalent HTML 3 attributes: this is a far cry
437 // from perfect but better than nothing.
438 static const struct EquivAttr
444 { "text-align", "ALIGN" },
445 { "width", "WIDTH" },
446 { "vertical-align", "VALIGN" },
447 { "background", "BGCOLOR" },
450 wxHtmlStyleParams
styleParams(*this);
451 for ( unsigned n
= 0; n
< WXSIZEOF(equivAttrs
); n
++ )
453 const EquivAttr
& ea
= equivAttrs
[n
];
454 if ( styleParams
.HasParam(ea
.style
) && !HasParam(ea
.attr
) )
456 m_ParamNames
.Add(ea
.attr
);
457 m_ParamValues
.Add(styleParams
.GetParam(ea
.style
));
462 wxHtmlTag::~wxHtmlTag()
468 t2
= t1
->GetNextSibling();
474 bool wxHtmlTag::HasParam(const wxString
& par
) const
476 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
479 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_quotes
) const
481 int index
= m_ParamNames
.Index(par
, false);
482 if (index
== wxNOT_FOUND
)
483 return wxGetEmptyString();
486 // VS: backward compatibility, seems to be never used by wxHTML...
488 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
492 return m_ParamValues
[index
];
495 int wxHtmlTag::ScanParam(const wxString
& par
,
499 wxString parval
= GetParam(par
);
500 return wxSscanf(parval
, format
, param
);
503 int wxHtmlTag::ScanParam(const wxString
& par
,
504 const wchar_t *format
,
507 wxString parval
= GetParam(par
);
508 return wxSscanf(parval
, format
, param
);
512 bool wxHtmlTag::ParseAsColour(const wxString
& str
, wxColour
*clr
)
514 wxCHECK_MSG( clr
, false, wxT("invalid colour argument") );
516 // handle colours defined in HTML 4.0 first:
517 if (str
.length() > 1 && str
[0] != wxT('#'))
519 #define HTML_COLOUR(name, r, g, b) \
520 if (str.IsSameAs(wxS(name), false)) \
521 { clr->Set(r, g, b); return true; }
522 HTML_COLOUR("black", 0x00,0x00,0x00)
523 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
524 HTML_COLOUR("gray", 0x80,0x80,0x80)
525 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
526 HTML_COLOUR("maroon", 0x80,0x00,0x00)
527 HTML_COLOUR("red", 0xFF,0x00,0x00)
528 HTML_COLOUR("purple", 0x80,0x00,0x80)
529 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
530 HTML_COLOUR("green", 0x00,0x80,0x00)
531 HTML_COLOUR("lime", 0x00,0xFF,0x00)
532 HTML_COLOUR("olive", 0x80,0x80,0x00)
533 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
534 HTML_COLOUR("navy", 0x00,0x00,0x80)
535 HTML_COLOUR("blue", 0x00,0x00,0xFF)
536 HTML_COLOUR("teal", 0x00,0x80,0x80)
537 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
541 // then try to parse #rrggbb representations or set from other well
542 // known names (note that this doesn't strictly conform to HTML spec,
543 // but it doesn't do real harm -- but it *must* be done after the standard
544 // colors are handled above):
551 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
553 const wxString str
= GetParam(par
);
554 return !str
.empty() && ParseAsColour(str
, clr
);
557 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
559 if ( !HasParam(par
) )
563 if ( !GetParam(par
).ToLong(&i
) )
570 wxString
wxHtmlTag::GetAllParams() const
572 // VS: this function is for backward compatibility only,
573 // never used by wxHTML
575 size_t cnt
= m_ParamNames
.GetCount();
576 for (size_t i
= 0; i
< cnt
; i
++)
578 s
<< m_ParamNames
[i
];
580 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
581 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
583 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
588 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
591 return m_Parent
->m_FirstChild
;
594 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
601 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
604 return m_Parent
->m_LastChild
;
607 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
614 wxHtmlTag
*wxHtmlTag::GetNextTag() const
616 if (m_FirstChild
) return m_FirstChild
;
617 if (m_Next
) return m_Next
;
618 wxHtmlTag
*cur
= m_Parent
;
619 if (!cur
) return NULL
;
620 while (cur
->m_Parent
&& !cur
->m_Next
)