]>
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 <stdio.h> // for vsscanf
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 struct wxHtmlCacheItem
35 // this is "pos" value passed to wxHtmlTag's constructor.
36 // it is position of '<' character of the tag
39 // end positions for the tag:
40 // end1 is '<' of ending tag,
41 // end2 is '>' or both are
42 // -1 if there is no ending tag for this one...
43 // or -2 if this is ending tag </...>
51 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
53 #define CACHE_INCREMENT 64
55 bool wxIsCDATAElement(const wxChar
*tag
)
57 return (wxStrcmp(tag
, _T("SCRIPT")) == 0) ||
58 (wxStrcmp(tag
, _T("STYLE")) == 0);
61 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
63 const wxChar
*src
= source
.c_str();
64 int lng
= source
.length();
65 wxChar tagBuffer
[256];
71 for ( int pos
= 0; pos
< lng
; pos
++ )
73 if (src
[pos
] == wxT('<')) // tag found:
75 // don't cache comment tags
76 wxString::const_iterator iter
= source
.begin() + pos
;
77 if ( wxHtmlParser::SkipCommentTag(iter
, source
.end()) )
79 pos
= iter
- source
.begin();
83 if (m_CacheSize
% CACHE_INCREMENT
== 0)
84 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
85 int tg
= m_CacheSize
++;
87 m_Cache
[tg
].Key
= stpos
;
91 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
92 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
95 tagBuffer
[i
] = (wxChar
)wxToupper(src
[pos
]);
97 tagBuffer
[i
] = _T('\0');
99 m_Cache
[tg
].Name
= new wxChar
[i
+1];
100 memcpy(m_Cache
[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
102 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
104 if (src
[stpos
+1] == wxT('/')) // ending tag:
106 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
107 // find matching begin tag:
108 for (i
= tg
; i
>= 0; i
--)
109 if ((m_Cache
[i
].End1
== -1) && (wxStrcmp(m_Cache
[i
].Name
, tagBuffer
+1) == 0))
111 m_Cache
[i
].End1
= stpos
;
112 m_Cache
[i
].End2
= pos
+ 1;
118 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
120 if (wxIsCDATAElement(tagBuffer
))
122 // store the orig pos in case we are missing the closing
124 wxInt32 old_pos
= pos
;
125 bool foundCloseTag
= false;
127 // find next matching tag
128 int tag_len
= wxStrlen(tagBuffer
);
131 // find the ending tag
132 while (pos
+ 1 < lng
&&
133 (src
[pos
] != '<' || src
[pos
+1] != '/'))
140 while (pos
< lng
&& match_pos
< tag_len
&& src
[pos
] != '>' && src
[pos
] != '<') {
141 // cast to wxChar needed to suppress warning in
143 if ((wxChar
)wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
146 else if (src
[pos
] == wxT(' ') || src
[pos
] == wxT('\n') ||
147 src
[pos
] == wxT('\r') || src
[pos
] == wxT('\t')) {
148 // need to skip over these
157 if (match_pos
== tag_len
)
159 pos
= pos
- tag_len
- 3;
160 foundCloseTag
= true;
163 else // keep looking for the closing tag
170 // we didn't find closing tag; this means the markup
171 // is incorrect and the best thing we can do is to
172 // ignore the unclosed tag and continue parsing as if
181 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
182 for (int i
= 0; i
< m_CacheSize
; i
++)
184 delete[] m_Cache
[i
].Name
;
185 m_Cache
[i
].Name
= NULL
;
189 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
191 if (m_Cache
== NULL
) return;
192 if (m_Cache
[m_CachePos
].Key
!= at
)
194 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
197 if ( m_CachePos
< 0 || m_CachePos
== m_CacheSize
)
199 // something is very wrong with HTML, give up by returning an
200 // impossibly large value which is going to be ignored by the
209 while (m_Cache
[m_CachePos
].Key
!= at
);
211 *end1
= m_Cache
[m_CachePos
].End1
;
212 *end2
= m_Cache
[m_CachePos
].End2
;
218 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
222 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
224 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
225 const wxString
& source
, int pos
, int end_pos
,
226 wxHtmlTagsCache
*cache
,
227 wxHtmlEntitiesParser
*entParser
) : wxObject()
229 /* Setup DOM relations */
232 m_FirstChild
= m_LastChild
= NULL
;
236 m_Prev
= m_Parent
->m_LastChild
;
238 m_Parent
->m_FirstChild
= this;
240 m_Prev
->m_Next
= this;
241 m_Parent
->m_LastChild
= this;
246 /* Find parameters and their values: */
251 // fill-in name, params and begin pos:
254 // find tag's name and convert it to uppercase:
255 while ((i
< end_pos
) &&
256 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
257 c
!= wxT('\n') && c
!= wxT('\t') &&
260 if ((c
>= wxT('a')) && (c
<= wxT('z')))
261 c
-= (wxT('a') - wxT('A'));
265 // if the tag has parameters, read them and "normalize" them,
266 // i.e. convert to uppercase, replace whitespaces by spaces and
267 // remove whitespaces around '=':
268 if (source
[i
-1] != wxT('>'))
270 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
271 c == wxT('\n') || c == wxT('\t'))
272 wxString pname
, pvalue
;
284 state
= ST_BEFORE_NAME
;
289 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
291 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
293 m_ParamNames
.Add(pname
);
294 m_ParamValues
.Add(wxEmptyString
);
296 else if (state
== ST_VALUE
&& quote
== 0)
298 m_ParamNames
.Add(pname
);
300 m_ParamValues
.Add(entParser
->Parse(pvalue
));
302 m_ParamValues
.Add(pvalue
);
317 state
= ST_BEFORE_EQ
;
318 else if (c
== wxT('='))
319 state
= ST_BEFORE_VALUE
;
325 state
= ST_BEFORE_VALUE
;
326 else if (!IS_WHITE(c
))
328 m_ParamNames
.Add(pname
);
329 m_ParamValues
.Add(wxEmptyString
);
334 case ST_BEFORE_VALUE
:
337 if (c
== wxT('"') || c
== wxT('\''))
338 quote
= c
, pvalue
= wxEmptyString
;
340 quote
= 0, pvalue
= c
;
345 if ((quote
!= 0 && c
== quote
) ||
346 (quote
== 0 && IS_WHITE(c
)))
348 m_ParamNames
.Add(pname
);
351 // VS: backward compatibility, no real reason,
352 // but wxHTML code relies on this... :(
356 m_ParamValues
.Add(entParser
->Parse(pvalue
));
358 m_ParamValues
.Add(pvalue
);
359 state
= ST_BEFORE_NAME
;
371 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
372 if (m_End1
> end_pos
) m_End1
= end_pos
;
373 if (m_End2
> end_pos
) m_End2
= end_pos
;
376 wxHtmlTag::~wxHtmlTag()
382 t2
= t1
->GetNextSibling();
388 bool wxHtmlTag::HasParam(const wxString
& par
) const
390 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
393 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
395 int index
= m_ParamNames
.Index(par
, false);
396 if (index
== wxNOT_FOUND
)
397 return wxEmptyString
;
400 // VS: backward compatibility, seems to be never used by wxHTML...
402 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
406 return m_ParamValues
[index
];
409 int wxHtmlTag::ScanParam(const wxString
& par
,
410 const wxChar
*format
,
413 wxString parval
= GetParam(par
);
414 return wxSscanf(parval
, format
, param
);
417 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
419 wxCHECK_MSG( clr
, false, _T("invalid colour argument") );
421 wxString str
= GetParam(par
);
423 // handle colours defined in HTML 4.0 first:
424 if (str
.length() > 1 && str
[0] != _T('#'))
426 #define HTML_COLOUR(name, r, g, b) \
427 if (str.IsSameAs(wxT(name), false)) \
428 { clr->Set(r, g, b); return true; }
429 HTML_COLOUR("black", 0x00,0x00,0x00)
430 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
431 HTML_COLOUR("gray", 0x80,0x80,0x80)
432 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
433 HTML_COLOUR("maroon", 0x80,0x00,0x00)
434 HTML_COLOUR("red", 0xFF,0x00,0x00)
435 HTML_COLOUR("purple", 0x80,0x00,0x80)
436 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
437 HTML_COLOUR("green", 0x00,0x80,0x00)
438 HTML_COLOUR("lime", 0x00,0xFF,0x00)
439 HTML_COLOUR("olive", 0x80,0x80,0x00)
440 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
441 HTML_COLOUR("navy", 0x00,0x00,0x80)
442 HTML_COLOUR("blue", 0x00,0x00,0xFF)
443 HTML_COLOUR("teal", 0x00,0x80,0x80)
444 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
448 // then try to parse #rrggbb representations or set from other well
449 // known names (note that this doesn't strictly conform to HTML spec,
450 // but it doesn't do real harm -- but it *must* be done after the standard
451 // colors are handled above):
458 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
460 if (!HasParam(par
)) return false;
462 bool succ
= GetParam(par
).ToLong(&i
);
467 wxString
wxHtmlTag::GetAllParams() const
469 // VS: this function is for backward compatibility only,
470 // never used by wxHTML
472 size_t cnt
= m_ParamNames
.GetCount();
473 for (size_t i
= 0; i
< cnt
; i
++)
475 s
<< m_ParamNames
[i
];
477 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
478 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
480 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
485 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
488 return m_Parent
->m_FirstChild
;
491 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
498 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
501 return m_Parent
->m_LastChild
;
504 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
511 wxHtmlTag
*wxHtmlTag::GetNextTag() const
513 if (m_FirstChild
) return m_FirstChild
;
514 if (m_Next
) return m_Next
;
515 wxHtmlTag
*cur
= m_Parent
;
516 if (!cur
) return NULL
;
517 while (cur
->m_Parent
&& !cur
->m_Next
)