]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
adding a string for private data identification
[wxWidgets.git] / src / html / htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmltag.cpp
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "htmltag.h"
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #endif
26
27 #include "wx/html/htmltag.h"
28 #include "wx/html/htmlpars.h"
29 #include "wx/colour.h"
30 #include <stdio.h> // for vsscanf
31 #include <stdarg.h>
32
33
34 //-----------------------------------------------------------------------------
35 // wxHtmlTagsCache
36 //-----------------------------------------------------------------------------
37
38 struct wxHtmlCacheItem
39 {
40 // this is "pos" value passed to wxHtmlTag's constructor.
41 // it is position of '<' character of the tag
42 int Key;
43
44 // end positions for the tag:
45 // end1 is '<' of ending tag,
46 // end2 is '>' or both are
47 // -1 if there is no ending tag for this one...
48 // or -2 if this is ending tag </...>
49 int End1, End2;
50
51 // name of this tag
52 wxChar *Name;
53 };
54
55
56 IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject)
57
58 #define CACHE_INCREMENT 64
59
60 bool wxIsCDATAElement(const wxChar *tag)
61 {
62 return (wxStrcmp(tag, _T("SCRIPT")) == 0) ||
63 (wxStrcmp(tag, _T("STYLE")) == 0);
64 }
65
66 wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source)
67 {
68 const wxChar *src = source.c_str();
69 int lng = source.Length();
70 wxChar tagBuffer[256];
71
72 m_Cache = NULL;
73 m_CacheSize = 0;
74 m_CachePos = 0;
75
76 int pos = 0;
77 while (pos < lng)
78 {
79 if (src[pos] == wxT('<')) // tag found:
80 {
81 if (m_CacheSize % CACHE_INCREMENT == 0)
82 m_Cache = (wxHtmlCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(wxHtmlCacheItem));
83 int tg = m_CacheSize++;
84 int stpos = pos++;
85 m_Cache[tg].Key = stpos;
86
87 int i;
88 for ( i = 0;
89 pos < lng && i < (int)WXSIZEOF(tagBuffer) - 1 &&
90 src[pos] != wxT('>') && !wxIsspace(src[pos]);
91 i++, pos++ )
92 {
93 tagBuffer[i] = (wxChar)wxToupper(src[pos]);
94 }
95 tagBuffer[i] = _T('\0');
96
97 m_Cache[tg].Name = new wxChar[i+1];
98 memcpy(m_Cache[tg].Name, tagBuffer, (i+1)*sizeof(wxChar));
99
100 while (pos < lng && src[pos] != wxT('>')) pos++;
101
102 if (src[stpos+1] == wxT('/')) // ending tag:
103 {
104 m_Cache[tg].End1 = m_Cache[tg].End2 = -2;
105 // find matching begin tag:
106 for (i = tg; i >= 0; i--)
107 if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, tagBuffer+1) == 0))
108 {
109 m_Cache[i].End1 = stpos;
110 m_Cache[i].End2 = pos + 1;
111 break;
112 }
113 }
114 else
115 {
116 m_Cache[tg].End1 = m_Cache[tg].End2 = -1;
117
118 if (wxIsCDATAElement(tagBuffer))
119 {
120 // store the orig pos in case we are missing the closing
121 // tag (see below)
122 wxInt32 old_pos = pos;
123 bool foundCloseTag = false;
124
125 // find next matching tag
126 int tag_len = wxStrlen(tagBuffer);
127 while (pos < lng)
128 {
129 // find the ending tag
130 while (pos + 1 < lng &&
131 (src[pos] != '<' || src[pos+1] != '/'))
132 ++pos;
133 if (src[pos] == '<')
134 ++pos;
135
136 // see if it matches
137 int match_pos = 0;
138 while (pos < lng && match_pos < tag_len && src[pos] != '>' && src[pos] != '<') {
139 // cast to wxChar needed to suppress warning in
140 // Unicode build
141 if ((wxChar)wxToupper(src[pos]) == tagBuffer[match_pos]) {
142 ++match_pos;
143 }
144 else if (src[pos] == wxT(' ') || src[pos] == wxT('\n') ||
145 src[pos] == wxT('\r') || src[pos] == wxT('\t')) {
146 // need to skip over these
147 }
148 else {
149 match_pos = 0;
150 }
151 ++pos;
152 }
153
154 // found a match
155 if (match_pos == tag_len)
156 {
157 pos = pos - tag_len - 3;
158 foundCloseTag = true;
159 break;
160 }
161 else // keep looking for the closing tag
162 {
163 ++pos;
164 }
165 }
166 if (!foundCloseTag)
167 {
168 // we didn't find closing tag; this means the markup
169 // is incorrect and the best thing we can do is to
170 // ignore the unclosed tag and continue parsing as if
171 // it didn't exist:
172 pos = old_pos;
173 }
174 }
175 }
176 }
177
178 pos++;
179 }
180
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++)
183 {
184 delete[] m_Cache[i].Name;
185 m_Cache[i].Name = NULL;
186 }
187 }
188
189 void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
190 {
191 if (m_Cache == NULL) return;
192 if (m_Cache[m_CachePos].Key != at)
193 {
194 int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1;
195 do
196 {
197 if ( m_CachePos < 0 || m_CachePos == m_CacheSize )
198 {
199 // something is very wrong with HTML, give up by returning an
200 // impossibly large value which is going to be ignored by the
201 // caller
202 *end1 =
203 *end2 = INT_MAX;
204 return;
205 }
206
207 m_CachePos += delta;
208 }
209 while (m_Cache[m_CachePos].Key != at);
210 }
211 *end1 = m_Cache[m_CachePos].End1;
212 *end2 = m_Cache[m_CachePos].End2;
213 }
214
215
216
217
218 //-----------------------------------------------------------------------------
219 // wxHtmlTag
220 //-----------------------------------------------------------------------------
221
222 IMPLEMENT_CLASS(wxHtmlTag,wxObject)
223
224 wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
225 const wxString& source, int pos, int end_pos,
226 wxHtmlTagsCache *cache,
227 wxHtmlEntitiesParser *entParser) : wxObject()
228 {
229 /* Setup DOM relations */
230
231 m_Next = NULL;
232 m_FirstChild = m_LastChild = NULL;
233 m_Parent = parent;
234 if (parent)
235 {
236 m_Prev = m_Parent->m_LastChild;
237 if (m_Prev == NULL)
238 m_Parent->m_FirstChild = this;
239 else
240 m_Prev->m_Next = this;
241 m_Parent->m_LastChild = this;
242 }
243 else
244 m_Prev = NULL;
245
246 /* Find parameters and their values: */
247
248 int i;
249 wxChar c;
250
251 // fill-in name, params and begin pos:
252 i = pos+1;
253
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') &&
258 c != wxT('>')))
259 {
260 if ((c >= wxT('a')) && (c <= wxT('z')))
261 c -= (wxT('a') - wxT('A'));
262 m_Name << c;
263 }
264
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('>'))
269 {
270 #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \
271 c == wxT('\n') || c == wxT('\t'))
272 wxString pname, pvalue;
273 wxChar quote;
274 enum
275 {
276 ST_BEFORE_NAME = 1,
277 ST_NAME,
278 ST_BEFORE_EQ,
279 ST_BEFORE_VALUE,
280 ST_VALUE
281 } state;
282
283 quote = 0;
284 state = ST_BEFORE_NAME;
285 while (i < end_pos)
286 {
287 c = source[i++];
288
289 if (c == wxT('>') && !(state == ST_VALUE && quote != 0))
290 {
291 if (state == ST_BEFORE_EQ || state == ST_NAME)
292 {
293 m_ParamNames.Add(pname);
294 m_ParamValues.Add(wxEmptyString);
295 }
296 else if (state == ST_VALUE && quote == 0)
297 {
298 m_ParamNames.Add(pname);
299 if (entParser)
300 m_ParamValues.Add(entParser->Parse(pvalue));
301 else
302 m_ParamValues.Add(pvalue);
303 }
304 break;
305 }
306 switch (state)
307 {
308 case ST_BEFORE_NAME:
309 if (!IS_WHITE(c))
310 {
311 pname = c;
312 state = ST_NAME;
313 }
314 break;
315 case ST_NAME:
316 if (IS_WHITE(c))
317 state = ST_BEFORE_EQ;
318 else if (c == wxT('='))
319 state = ST_BEFORE_VALUE;
320 else
321 pname << c;
322 break;
323 case ST_BEFORE_EQ:
324 if (c == wxT('='))
325 state = ST_BEFORE_VALUE;
326 else if (!IS_WHITE(c))
327 {
328 m_ParamNames.Add(pname);
329 m_ParamValues.Add(wxEmptyString);
330 pname = c;
331 state = ST_NAME;
332 }
333 break;
334 case ST_BEFORE_VALUE:
335 if (!IS_WHITE(c))
336 {
337 if (c == wxT('"') || c == wxT('\''))
338 quote = c, pvalue = wxEmptyString;
339 else
340 quote = 0, pvalue = c;
341 state = ST_VALUE;
342 }
343 break;
344 case ST_VALUE:
345 if ((quote != 0 && c == quote) ||
346 (quote == 0 && IS_WHITE(c)))
347 {
348 m_ParamNames.Add(pname);
349 if (quote == 0)
350 {
351 // VS: backward compatibility, no real reason,
352 // but wxHTML code relies on this... :(
353 pvalue.MakeUpper();
354 }
355 if (entParser)
356 m_ParamValues.Add(entParser->Parse(pvalue));
357 else
358 m_ParamValues.Add(pvalue);
359 state = ST_BEFORE_NAME;
360 }
361 else
362 pvalue << c;
363 break;
364 }
365 }
366
367 #undef IS_WHITE
368 }
369 m_Begin = i;
370
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;
374 }
375
376 wxHtmlTag::~wxHtmlTag()
377 {
378 wxHtmlTag *t1, *t2;
379 t1 = m_FirstChild;
380 while (t1)
381 {
382 t2 = t1->GetNextSibling();
383 delete t1;
384 t1 = t2;
385 }
386 }
387
388 bool wxHtmlTag::HasParam(const wxString& par) const
389 {
390 return (m_ParamNames.Index(par, false) != wxNOT_FOUND);
391 }
392
393 wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const
394 {
395 int index = m_ParamNames.Index(par, false);
396 if (index == wxNOT_FOUND)
397 return wxEmptyString;
398 if (with_commas)
399 {
400 // VS: backward compatibility, seems to be never used by wxHTML...
401 wxString s;
402 s << wxT('"') << m_ParamValues[index] << wxT('"');
403 return s;
404 }
405 else
406 return m_ParamValues[index];
407 }
408
409 int wxHtmlTag::ScanParam(const wxString& par,
410 const wxChar *format,
411 void *param) const
412 {
413 wxString parval = GetParam(par);
414 return wxSscanf(parval, format, param);
415 }
416
417 bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
418 {
419 wxString str = GetParam(par);
420
421 if (str.empty()) return false;
422 if (str.GetChar(0) == wxT('#'))
423 {
424 unsigned long tmp;
425 if (ScanParam(par, wxT("#%lX"), &tmp) != 1)
426 return false;
427 *clr = wxColour((unsigned char)((tmp & 0xFF0000) >> 16),
428 (unsigned char)((tmp & 0x00FF00) >> 8),
429 (unsigned char)(tmp & 0x0000FF));
430 return true;
431 }
432 else
433 {
434 // Handle colours defined in HTML 4.0:
435 #define HTML_COLOUR(name,r,g,b) \
436 if (str.IsSameAs(wxT(name), false)) \
437 { *clr = wxColour(r,g,b); return true; }
438 HTML_COLOUR("black", 0x00,0x00,0x00)
439 HTML_COLOUR("silver", 0xC0,0xC0,0xC0)
440 HTML_COLOUR("gray", 0x80,0x80,0x80)
441 HTML_COLOUR("white", 0xFF,0xFF,0xFF)
442 HTML_COLOUR("maroon", 0x80,0x00,0x00)
443 HTML_COLOUR("red", 0xFF,0x00,0x00)
444 HTML_COLOUR("purple", 0x80,0x00,0x80)
445 HTML_COLOUR("fuchsia", 0xFF,0x00,0xFF)
446 HTML_COLOUR("green", 0x00,0x80,0x00)
447 HTML_COLOUR("lime", 0x00,0xFF,0x00)
448 HTML_COLOUR("olive", 0x80,0x80,0x00)
449 HTML_COLOUR("yellow", 0xFF,0xFF,0x00)
450 HTML_COLOUR("navy", 0x00,0x00,0x80)
451 HTML_COLOUR("blue", 0x00,0x00,0xFF)
452 HTML_COLOUR("teal", 0x00,0x80,0x80)
453 HTML_COLOUR("aqua", 0x00,0xFF,0xFF)
454 #undef HTML_COLOUR
455 }
456
457 return false;
458 }
459
460 bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
461 {
462 if (!HasParam(par)) return false;
463 long i;
464 bool succ = GetParam(par).ToLong(&i);
465 *clr = (int)i;
466 return succ;
467 }
468
469 wxString wxHtmlTag::GetAllParams() const
470 {
471 // VS: this function is for backward compatibility only,
472 // never used by wxHTML
473 wxString s;
474 size_t cnt = m_ParamNames.GetCount();
475 for (size_t i = 0; i < cnt; i++)
476 {
477 s << m_ParamNames[i];
478 s << wxT('=');
479 if (m_ParamValues[i].Find(wxT('"')) != wxNOT_FOUND)
480 s << wxT('\'') << m_ParamValues[i] << wxT('\'');
481 else
482 s << wxT('"') << m_ParamValues[i] << wxT('"');
483 }
484 return s;
485 }
486
487 wxHtmlTag *wxHtmlTag::GetFirstSibling() const
488 {
489 if (m_Parent)
490 return m_Parent->m_FirstChild;
491 else
492 {
493 wxHtmlTag *cur = (wxHtmlTag*)this;
494 while (cur->m_Prev)
495 cur = cur->m_Prev;
496 return cur;
497 }
498 }
499
500 wxHtmlTag *wxHtmlTag::GetLastSibling() const
501 {
502 if (m_Parent)
503 return m_Parent->m_LastChild;
504 else
505 {
506 wxHtmlTag *cur = (wxHtmlTag*)this;
507 while (cur->m_Next)
508 cur = cur->m_Next;
509 return cur;
510 }
511 }
512
513 wxHtmlTag *wxHtmlTag::GetNextTag() const
514 {
515 if (m_FirstChild) return m_FirstChild;
516 if (m_Next) return m_Next;
517 wxHtmlTag *cur = m_Parent;
518 if (!cur) return NULL;
519 while (cur->m_Parent && !cur->m_Next)
520 cur = cur->m_Parent;
521 return cur->m_Next;
522 }
523
524 #if WXWIN_COMPATIBILITY_2_2
525
526 bool wxHtmlTag::IsEnding() const
527 {
528 return false;
529 }
530
531 #endif // WXWIN_COMPATIBILITY_2_2
532
533 #endif