]>
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"
21 #include "wx/html/htmltag.h"
22 #include "wx/html/htmlpars.h"
23 #include "wx/colour.h"
24 #include <stdio.h> // for vsscanf
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 struct wxHtmlCacheItem
34 // this is "pos" value passed to wxHtmlTag's constructor.
35 // it is position of '<' character of the tag
38 // end positions for the tag:
39 // end1 is '<' of ending tag,
40 // end2 is '>' or both are
41 // -1 if there is no ending tag for this one...
42 // or -2 if this is ending tag </...>
50 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
52 #define CACHE_INCREMENT 64
54 bool wxIsCDATAElement(const wxChar
*tag
)
56 return (wxStrcmp(tag
, _T("SCRIPT")) == 0) ||
57 (wxStrcmp(tag
, _T("STYLE")) == 0);
60 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
62 const wxChar
*src
= source
.c_str();
63 int lng
= source
.length();
64 wxChar tagBuffer
[256];
73 if (src
[pos
] == wxT('<')) // tag found:
75 if (m_CacheSize
% CACHE_INCREMENT
== 0)
76 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
77 int tg
= m_CacheSize
++;
79 m_Cache
[tg
].Key
= stpos
;
83 pos
< lng
&& i
< (int)WXSIZEOF(tagBuffer
) - 1 &&
84 src
[pos
] != wxT('>') && !wxIsspace(src
[pos
]);
87 tagBuffer
[i
] = (wxChar
)wxToupper(src
[pos
]);
89 tagBuffer
[i
] = _T('\0');
91 m_Cache
[tg
].Name
= new wxChar
[i
+1];
92 memcpy(m_Cache
[tg
].Name
, tagBuffer
, (i
+1)*sizeof(wxChar
));
94 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
96 if (src
[stpos
+1] == wxT('/')) // ending tag:
98 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
99 // find matching begin tag:
100 for (i
= tg
; i
>= 0; i
--)
101 if ((m_Cache
[i
].End1
== -1) && (wxStrcmp(m_Cache
[i
].Name
, tagBuffer
+1) == 0))
103 m_Cache
[i
].End1
= stpos
;
104 m_Cache
[i
].End2
= pos
+ 1;
110 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
112 if (wxIsCDATAElement(tagBuffer
))
114 // store the orig pos in case we are missing the closing
116 wxInt32 old_pos
= pos
;
117 bool foundCloseTag
= false;
119 // find next matching tag
120 int tag_len
= wxStrlen(tagBuffer
);
123 // find the ending tag
124 while (pos
+ 1 < lng
&&
125 (src
[pos
] != '<' || src
[pos
+1] != '/'))
132 while (pos
< lng
&& match_pos
< tag_len
&& src
[pos
] != '>' && src
[pos
] != '<') {
133 // cast to wxChar needed to suppress warning in
135 if ((wxChar
)wxToupper(src
[pos
]) == tagBuffer
[match_pos
]) {
138 else if (src
[pos
] == wxT(' ') || src
[pos
] == wxT('\n') ||
139 src
[pos
] == wxT('\r') || src
[pos
] == wxT('\t')) {
140 // need to skip over these
149 if (match_pos
== tag_len
)
151 pos
= pos
- tag_len
- 3;
152 foundCloseTag
= true;
155 else // keep looking for the closing tag
162 // we didn't find closing tag; this means the markup
163 // is incorrect and the best thing we can do is to
164 // ignore the unclosed tag and continue parsing as if
175 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
176 for (int i
= 0; i
< m_CacheSize
; i
++)
178 delete[] m_Cache
[i
].Name
;
179 m_Cache
[i
].Name
= NULL
;
183 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
185 if (m_Cache
== NULL
) return;
186 if (m_Cache
[m_CachePos
].Key
!= at
)
188 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
191 if ( m_CachePos
< 0 || m_CachePos
== m_CacheSize
)
193 // something is very wrong with HTML, give up by returning an
194 // impossibly large value which is going to be ignored by the
203 while (m_Cache
[m_CachePos
].Key
!= at
);
205 *end1
= m_Cache
[m_CachePos
].End1
;
206 *end2
= m_Cache
[m_CachePos
].End2
;
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
216 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
218 wxHtmlTag::wxHtmlTag(wxHtmlTag
*parent
,
219 const wxString
& source
, int pos
, int end_pos
,
220 wxHtmlTagsCache
*cache
,
221 wxHtmlEntitiesParser
*entParser
) : wxObject()
223 /* Setup DOM relations */
226 m_FirstChild
= m_LastChild
= NULL
;
230 m_Prev
= m_Parent
->m_LastChild
;
232 m_Parent
->m_FirstChild
= this;
234 m_Prev
->m_Next
= this;
235 m_Parent
->m_LastChild
= this;
240 /* Find parameters and their values: */
245 // fill-in name, params and begin pos:
248 // find tag's name and convert it to uppercase:
249 while ((i
< end_pos
) &&
250 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
251 c
!= wxT('\n') && c
!= wxT('\t') &&
254 if ((c
>= wxT('a')) && (c
<= wxT('z')))
255 c
-= (wxT('a') - wxT('A'));
259 // if the tag has parameters, read them and "normalize" them,
260 // i.e. convert to uppercase, replace whitespaces by spaces and
261 // remove whitespaces around '=':
262 if (source
[i
-1] != wxT('>'))
264 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
265 c == wxT('\n') || c == wxT('\t'))
266 wxString pname
, pvalue
;
278 state
= ST_BEFORE_NAME
;
283 if (c
== wxT('>') && !(state
== ST_VALUE
&& quote
!= 0))
285 if (state
== ST_BEFORE_EQ
|| state
== ST_NAME
)
287 m_ParamNames
.Add(pname
);
288 m_ParamValues
.Add(wxEmptyString
);
290 else if (state
== ST_VALUE
&& quote
== 0)
292 m_ParamNames
.Add(pname
);
294 m_ParamValues
.Add(entParser
->Parse(pvalue
));
296 m_ParamValues
.Add(pvalue
);
311 state
= ST_BEFORE_EQ
;
312 else if (c
== wxT('='))
313 state
= ST_BEFORE_VALUE
;
319 state
= ST_BEFORE_VALUE
;
320 else if (!IS_WHITE(c
))
322 m_ParamNames
.Add(pname
);
323 m_ParamValues
.Add(wxEmptyString
);
328 case ST_BEFORE_VALUE
:
331 if (c
== wxT('"') || c
== wxT('\''))
332 quote
= c
, pvalue
= wxEmptyString
;
334 quote
= 0, pvalue
= c
;
339 if ((quote
!= 0 && c
== quote
) ||
340 (quote
== 0 && IS_WHITE(c
)))
342 m_ParamNames
.Add(pname
);
345 // VS: backward compatibility, no real reason,
346 // but wxHTML code relies on this... :(
350 m_ParamValues
.Add(entParser
->Parse(pvalue
));
352 m_ParamValues
.Add(pvalue
);
353 state
= ST_BEFORE_NAME
;
365 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
366 if (m_End1
> end_pos
) m_End1
= end_pos
;
367 if (m_End2
> end_pos
) m_End2
= end_pos
;
370 wxHtmlTag::~wxHtmlTag()
376 t2
= t1
->GetNextSibling();
382 bool wxHtmlTag::HasParam(const wxString
& par
) const
384 return (m_ParamNames
.Index(par
, false) != wxNOT_FOUND
);
387 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
389 int index
= m_ParamNames
.Index(par
, false);
390 if (index
== wxNOT_FOUND
)
391 return wxEmptyString
;
394 // VS: backward compatibility, seems to be never used by wxHTML...
396 s
<< wxT('"') << m_ParamValues
[index
] << wxT('"');
400 return m_ParamValues
[index
];
403 int wxHtmlTag::ScanParam(const wxString
& par
,
404 const wxChar
*format
,
407 wxString parval
= GetParam(par
);
408 return wxSscanf(parval
, format
, param
);
411 bool wxHtmlTag::GetParamAsColour(const wxString
& par
, wxColour
*clr
) const
413 wxString str
= GetParam(par
);
415 if (str
.empty()) return false;
416 if (str
.GetChar(0) == wxT('#'))
419 if (ScanParam(par
, wxT("#%lX"), &tmp
) != 1)
421 *clr
= wxColour((unsigned char)((tmp
& 0xFF0000) >> 16),
422 (unsigned char)((tmp
& 0x00FF00) >> 8),
423 (unsigned char)(tmp
& 0x0000FF));
428 // Handle colours defined in HTML 4.0:
429 #define HTML_COLOUR(name,r,g,b) \
430 if (str.IsSameAs(wxT(name), false)) \
431 { *clr = wxColour(r,g,b); return true; }
432 HTML_COLOUR("black", 0x00,0x00,0x00)
433 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
434 HTML_COLOUR("gray", 0x80,0x80,0x80)
435 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
436 HTML_COLOUR("maroon", 0x80,0x00,0x00)
437 HTML_COLOUR("red", 0xFF,0x00,0x00)
438 HTML_COLOUR("purple", 0x80,0x00,0x80)
439 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
440 HTML_COLOUR("green", 0x00,0x80,0x00)
441 HTML_COLOUR("lime", 0x00,0xFF,0x00)
442 HTML_COLOUR("olive", 0x80,0x80,0x00)
443 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
444 HTML_COLOUR("navy", 0x00,0x00,0x80)
445 HTML_COLOUR("blue", 0x00,0x00,0xFF)
446 HTML_COLOUR("teal", 0x00,0x80,0x80)
447 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
454 bool wxHtmlTag::GetParamAsInt(const wxString
& par
, int *clr
) const
456 if (!HasParam(par
)) return false;
458 bool succ
= GetParam(par
).ToLong(&i
);
463 wxString
wxHtmlTag::GetAllParams() const
465 // VS: this function is for backward compatibility only,
466 // never used by wxHTML
468 size_t cnt
= m_ParamNames
.GetCount();
469 for (size_t i
= 0; i
< cnt
; i
++)
471 s
<< m_ParamNames
[i
];
473 if (m_ParamValues
[i
].Find(wxT('"')) != wxNOT_FOUND
)
474 s
<< wxT('\'') << m_ParamValues
[i
] << wxT('\'');
476 s
<< wxT('"') << m_ParamValues
[i
] << wxT('"');
481 wxHtmlTag
*wxHtmlTag::GetFirstSibling() const
484 return m_Parent
->m_FirstChild
;
487 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
494 wxHtmlTag
*wxHtmlTag::GetLastSibling() const
497 return m_Parent
->m_LastChild
;
500 wxHtmlTag
*cur
= (wxHtmlTag
*)this;
507 wxHtmlTag
*wxHtmlTag::GetNextTag() const
509 if (m_FirstChild
) return m_FirstChild
;
510 if (m_Next
) return m_Next
;
511 wxHtmlTag
*cur
= m_Parent
;
512 if (!cur
) return NULL
;
513 while (cur
->m_Parent
&& !cur
->m_Next
)