]>
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
)
91 // possible tag start found:
93 // don't cache comment tags
94 if ( wxHtmlParser::SkipCommentTag(pos
, end
) )
97 // Remember the starting tag position.
98 wxString::const_iterator stpos
= pos
++;
100 // And look for the ending one.
103 pos
< end
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
104 *pos
!= wxT('>') && !wxIsspace(*pos
);
107 tagBuffer
[i
] = (wxChar
)wxToupper(*pos
);
109 tagBuffer
[i
] = wxT('\0');
111 while (pos
< end
&& *pos
!= wxT('>'))
116 // We didn't find a closing bracket, this is not a valid tag after
117 // all. Notice that we need to roll back pos to avoid creating an
118 // invalid iterator when "++pos" is done in the loop statement.
124 // We have a valid tag, add it to the cache.
125 size_t tg
= Cache().size();
126 Cache().push_back(wxHtmlCacheItem());
127 Cache()[tg
].Key
= stpos
;
128 Cache()[tg
].Name
= new wxChar
[i
+1];
129 memcpy(Cache()[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
131 if ((stpos
+1) < end
&& *(stpos
+1) == wxT('/')) // ending tag:
133 Cache()[tg
].type
= wxHtmlCacheItem::Type_EndingTag
;
134 // find matching begin tag:
135 for (i
= tg
; i
>= 0; i
--)
137 if ((Cache()[i
].type
== wxHtmlCacheItem::Type_NoMatchingEndingTag
) && (wxStrcmp(Cache()[i
].Name
, tagBuffer
+1) == 0))
139 Cache()[i
].type
= wxHtmlCacheItem::Type_Normal
;
140 Cache()[i
].End1
= stpos
;
141 Cache()[i
].End2
= pos
+ 1;
148 Cache()[tg
].type
= wxHtmlCacheItem::Type_NoMatchingEndingTag
;
150 if (wxIsCDATAElement(tagBuffer
))
152 // store the orig pos in case we are missing the closing
154 const wxString::const_iterator old_pos
= pos
;
155 bool foundCloseTag
= false;
157 // find next matching tag
158 int tag_len
= wxStrlen(tagBuffer
);
161 // find the ending tag
162 while (pos
+ 1 < end
&&
163 (*pos
!= '<' || *(pos
+1) != '/'))
170 while (pos
< end
&& match_pos
< tag_len
)
173 if ( c
== '>' || c
== '<' )
176 // cast to wxChar needed to suppress warning in
178 if ((wxChar
)wxToupper(c
) == tagBuffer
[match_pos
])
182 else if (c
== wxT(' ') || c
== wxT('\n') ||
183 c
== wxT('\r') || c
== wxT('\t'))
185 // need to skip over these
195 if (match_pos
== tag_len
)
197 pos
= pos
- tag_len
- 3;
198 foundCloseTag
= true;
201 else // keep looking for the closing tag
208 // we didn't find closing tag; this means the markup
209 // is incorrect and the best thing we can do is to
210 // ignore the unclosed tag and continue parsing as if
218 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
219 for ( wxHtmlTagsCacheData::iterator i
= Cache().begin();
220 i
!= Cache().end(); ++i
)
226 wxHtmlTagsCache::~wxHtmlTagsCache()
231 void wxHtmlTagsCache::QueryTag(const wxString::const_iterator
& at
,
232 const wxString::const_iterator
& inputEnd
,
233 wxString::const_iterator
*end1
,
234 wxString::const_iterator
*end2
,
245 if (Cache()[m_CachePos
].Key
!= at
)
247 int delta
= (at
< Cache()[m_CachePos
].Key
) ? -1 : 1;
252 if ( m_CachePos
< 0 || m_CachePos
>= (int)Cache().size() )
254 if ( m_CachePos
< 0 )
257 m_CachePos
= Cache().size() - 1;
258 // something is very wrong with HTML, give up by returning an
259 // impossibly large value which is going to be ignored by the
267 while (Cache()[m_CachePos
].Key
!= at
);
270 switch ( Cache()[m_CachePos
].type
)
272 case wxHtmlCacheItem::Type_Normal
:
273 *end1
= Cache()[m_CachePos
].End1
;
274 *end2
= Cache()[m_CachePos
].End2
;
278 case wxHtmlCacheItem::Type_EndingTag
:
279 wxFAIL_MSG("QueryTag called for ending tag - can't be");
280 // but if it does happen, fall through, better than crashing
282 case wxHtmlCacheItem::Type_NoMatchingEndingTag
:
283 // If input HTML is invalid and there's no closing tag for this
284 // one, pretend that it runs all the way to the end of input
295 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------
299 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
300 const wxString
*source
,
301 const wxString::const_iterator
& pos
,
302 const wxString::const_iterator
& end_pos
,
303 wxHtmlTagsCache
*cache
,
304 wxHtmlEntitiesParser
*entParser
)
306 /* Setup DOM relations */
309 m_FirstChild
= m_LastChild
= NULL
;
313 m_Prev
= m_Parent
->m_LastChild
;
315 m_Parent
->m_FirstChild
= this;
317 m_Prev
->m_Next
= this;
318 m_Parent
->m_LastChild
= this;
323 /* Find parameters and their values: */
325 wxChar c
wxDUMMY_INITIALIZE(0);
327 // fill-in name, params and begin pos:
328 wxString::const_iterator
i(pos
+1);
330 // find tag's name and convert it to uppercase:
331 while ((i
< end_pos
) &&
332 ((c
= *(i
++)) != wxT(' ') && c
!= wxT('\r') &&
333 c
!= wxT('\n') && c
!= wxT('\t') &&
334 c
!= wxT('>') && c
!= wxT('/')))
336 if ((c
>= wxT('a')) && (c
<= wxT('z')))
337 c
-= (wxT('a') - wxT('A'));
341 // if the tag has parameters, read them and "normalize" them,
342 // i.e. convert to uppercase, replace whitespaces by spaces and
343 // remove whitespaces around '=':
344 if (*(i
-1) != wxT('>'))
346 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
347 c == wxT('\n') || c == wxT('\t'))
348 wxString pname
, pvalue
;
360 state
= ST_BEFORE_NAME
;
365 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
367 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
369 m_ParamNames
.Add(pname
);
370 m_ParamValues
.Add(wxGetEmptyString());
372 else if (state
== ST_VALUE
&& quote
== 0)
374 m_ParamNames
.Add(pname
);
376 m_ParamValues
.Add(entParser
->Parse(pvalue
));
378 m_ParamValues
.Add(pvalue
);
393 state
= ST_BEFORE_EQ
;
394 else if (c
== wxT('='))
395 state
= ST_BEFORE_VALUE
;
401 state
= ST_BEFORE_VALUE
;
402 else if (!IS_WHITE(c
))
404 m_ParamNames
.Add(pname
);
405 m_ParamValues
.Add(wxGetEmptyString());
410 case ST_BEFORE_VALUE
:
413 if (c
== wxT('"') || c
== wxT('\''))
414 quote
= c
, pvalue
= wxGetEmptyString();
416 quote
= 0, pvalue
= c
;
421 if ((quote
!= 0 && c
== quote
) ||
422 (quote
== 0 && IS_WHITE(c
)))
424 m_ParamNames
.Add(pname
);
427 // VS: backward compatibility, no real reason,
428 // but wxHTML code relies on this... :(
432 m_ParamValues
.Add(entParser
->Parse(pvalue
));
434 m_ParamValues
.Add(pvalue
);
435 state
= ST_BEFORE_NAME
;
446 cache
->QueryTag(pos
, source
->end(), &m_End1
, &m_End2
, &m_hasEnding
);
447 if (m_End1
> end_pos
) m_End1
= end_pos
;
448 if (m_End2
> end_pos
) m_End2
= end_pos
;
450 #if WXWIN_COMPATIBILITY_2_8
451 m_sourceStart
= source
->begin();
454 // Try to parse any style parameters that can be handled simply by
455 // converting them to the equivalent HTML 3 attributes: this is a far cry
456 // from perfect but better than nothing.
457 static const struct EquivAttr
463 { "text-align", "ALIGN" },
464 { "width", "WIDTH" },
465 { "vertical-align", "VALIGN" },
466 { "background", "BGCOLOR" },
467 { "background-color", "BGCOLOR" },
470 wxHtmlStyleParams
styleParams(*this);
471 for ( unsigned n
= 0; n
< WXSIZEOF(equivAttrs
); n
++ )
473 const EquivAttr
& ea
= equivAttrs
[n
];
474 if ( styleParams
.HasParam(ea
.style
) && !HasParam(ea
.attr
) )
476 m_ParamNames
.Add(ea
.attr
);
477 m_ParamValues
.Add(styleParams
.GetParam(ea
.style
));
482 wxHtmlTag::~wxHtmlTag()
488 t2
= t1
->GetNextSibling();
494 bool wxHtmlTag::HasParam(const wxString
& par
) const
496 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
499 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_quotes
) const
501 int index
= m_ParamNames
.Index(par
, false);
502 if (index
== wxNOT_FOUND
)
503 return wxGetEmptyString();
506 // VS: backward compatibility, seems to be never used by wxHTML...
508 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
512 return m_ParamValues
[index
];
515 int wxHtmlTag::ScanParam(const wxString
& par
,
519 wxString parval
= GetParam(par
);
520 return wxSscanf(parval
, format
, param
);
523 int wxHtmlTag::ScanParam(const wxString
& par
,
524 const wchar_t *format
,
527 wxString parval
= GetParam(par
);
528 return wxSscanf(parval
, format
, param
);
532 bool wxHtmlTag::ParseAsColour(const wxString
& str
, wxColour
*clr
)
534 wxCHECK_MSG( clr
, false, wxT("invalid colour argument") );
536 // handle colours defined in HTML 4.0 first:
537 if (str
.length() > 1 && str
[0] != wxT('#'))
539 #define HTML_COLOUR(name, r, g, b) \
540 if (str.IsSameAs(wxS(name), false)) \
541 { clr->Set(r, g, b); return true; }
542 HTML_COLOUR("black", 0x00,0x00,0x00)
543 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
544 HTML_COLOUR("gray", 0x80,0x80,0x80)
545 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
546 HTML_COLOUR("maroon", 0x80,0x00,0x00)
547 HTML_COLOUR("red", 0xFF,0x00,0x00)
548 HTML_COLOUR("purple", 0x80,0x00,0x80)
549 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
550 HTML_COLOUR("green", 0x00,0x80,0x00)
551 HTML_COLOUR("lime", 0x00,0xFF,0x00)
552 HTML_COLOUR("olive", 0x80,0x80,0x00)
553 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
554 HTML_COLOUR("navy", 0x00,0x00,0x80)
555 HTML_COLOUR("blue", 0x00,0x00,0xFF)
556 HTML_COLOUR("teal", 0x00,0x80,0x80)
557 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
561 // then try to parse #rrggbb representations or set from other well
562 // known names (note that this doesn't strictly conform to HTML spec,
563 // but it doesn't do real harm -- but it *must* be done after the standard
564 // colors are handled above):
571 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
573 const wxString str
= GetParam(par
);
574 return !str
.empty() && ParseAsColour(str
, clr
);
577 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
579 if ( !HasParam(par
) )
583 if ( !GetParam(par
).ToLong(&i
) )
591 wxHtmlTag::GetParamAsIntOrPercent(const wxString
& par
,
593 bool& isPercent
) const
595 const wxString param
= GetParam(par
);
600 if ( param
.EndsWith("%", &num
) )
611 if ( !num
.ToLong(&lValue
) )
614 if ( lValue
> INT_MAX
|| lValue
< INT_MIN
)
617 *value
= static_cast<int>(lValue
);
622 wxString
wxHtmlTag::GetAllParams() const
624 // VS: this function is for backward compatibility only,
625 // never used by wxHTML
627 size_t cnt
= m_ParamNames
.GetCount();
628 for (size_t i
= 0; i
< cnt
; i
++)
630 s
<< m_ParamNames
[i
];
632 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
633 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
635 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
640 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
643 return m_Parent
->m_FirstChild
;
646 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
653 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
656 return m_Parent
->m_LastChild
;
659 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
666 wxHtmlTag
*wxHtmlTag::GetNextTag() const
668 if (m_FirstChild
) return m_FirstChild
;
669 if (m_Next
) return m_Next
;
670 wxHtmlTag
*cur
= m_Parent
;
671 if (!cur
) return NULL
;
672 while (cur
->m_Parent
&& !cur
->m_Next
)