1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
29 #include "wx/tokenzr.h"
30 #include "wx/wfstream.h"
32 #include "wx/fontmap.h"
33 #include "wx/html/htmldefs.h"
34 #include "wx/html/htmlpars.h"
35 #include "wx/dynarray.h"
36 #include "wx/arrimpl.cpp"
38 //-----------------------------------------------------------------------------
39 // wxHtmlParser helpers
40 //-----------------------------------------------------------------------------
45 wxHtmlTextPiece(int pos
, int lng
) : m_pos(pos
), m_lng(lng
) {}
49 WX_DECLARE_OBJARRAY(wxHtmlTextPiece
, wxHtmlTextPieces
);
50 WX_DEFINE_OBJARRAY(wxHtmlTextPieces
);
52 struct wxHtmlParserState
56 wxHtmlTextPieces
*m_textPieces
;
59 wxHtmlParserState
*m_nextState
;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser
,wxObject
)
68 wxHtmlParser::wxHtmlParser()
69 : wxObject(), m_HandlersHash(wxKEY_STRING
),
70 m_FS(NULL
), m_HandlersStack(NULL
)
72 m_entitiesParser
= new wxHtmlEntitiesParser
;
80 wxHtmlParser::~wxHtmlParser()
82 while (RestoreState())
84 delete m_HandlersStack
;
85 m_HandlersHash
.Clear();
86 m_HandlersList
.DeleteContents(TRUE
);
87 m_HandlersList
.Clear();
88 delete m_entitiesParser
;
91 wxObject
* wxHtmlParser::Parse(const wxString
& source
)
97 result
= GetProduct();
102 void wxHtmlParser::InitParser(const wxString
& source
)
107 void wxHtmlParser::DoneParser()
112 void wxHtmlParser::SetSource(const wxString
& src
)
121 void wxHtmlParser::CreateDOMTree()
123 wxHtmlTagsCache
cache(m_Source
);
124 m_TextPieces
= new wxHtmlTextPieces
;
125 CreateDOMSubTree(NULL
, 0, m_Source
.Length(), &cache
);
129 void wxHtmlParser::CreateDOMSubTree(wxHtmlTag
*cur
,
130 int begin_pos
, int end_pos
,
131 wxHtmlTagsCache
*cache
)
133 if (end_pos
<= begin_pos
) return;
137 int textBeginning
= begin_pos
;
141 c
= m_Source
.GetChar(i
);
145 // add text to m_TextPieces:
146 if (i
- textBeginning
> 0)
148 wxHtmlTextPiece(textBeginning
, i
- textBeginning
));
150 // if it is a comment, skip it:
151 if (i
< end_pos
-6 && m_Source
.GetChar(i
+1) == wxT('!') &&
152 m_Source
.GetChar(i
+2) == wxT('-') &&
153 m_Source
.GetChar(i
+3) == wxT('-'))
155 // Comments begin with "<!--" and end with "--[ \t\r\n]*>"
156 // according to HTML 4.0
161 c
= m_Source
.GetChar(i
++);
162 if ((c
== wxT(' ') || c
== wxT('\n') ||
163 c
== wxT('\r') || c
== wxT('\t')) && dashes
>= 2) {}
164 else if (c
== wxT('>') && dashes
>= 2)
169 else if (c
== wxT('-'))
176 // add another tag to the tree:
177 else if (i
< end_pos
-1 && m_Source
.GetChar(i
+1) != wxT('/'))
181 chd
= new wxHtmlTag(cur
, m_Source
,
182 i
, end_pos
, cache
, m_entitiesParser
);
185 chd
= new wxHtmlTag(NULL
, m_Source
,
186 i
, end_pos
, cache
, m_entitiesParser
);
189 // if this is the first tag to be created make the root
190 // m_Tags point to it:
195 // if there is already a root tag add this tag as
197 chd
->m_Prev
= m_Tags
->GetLastSibling();
198 chd
->m_Prev
->m_Next
= chd
;
202 if (chd
->HasEnding())
204 CreateDOMSubTree(chd
,
205 chd
->GetBeginPos(), chd
->GetEndPos1(),
207 i
= chd
->GetEndPos2();
210 i
= chd
->GetBeginPos();
214 // ... or skip ending tag:
217 while (i
< end_pos
&& m_Source
.GetChar(i
) != wxT('>')) i
++;
224 // add remaining text to m_TextPieces:
225 if (end_pos
- textBeginning
> 0)
227 wxHtmlTextPiece(textBeginning
, end_pos
- textBeginning
));
230 void wxHtmlParser::DestroyDOMTree()
236 t2
= t1
->GetNextSibling();
240 m_Tags
= m_CurTag
= NULL
;
246 void wxHtmlParser::DoParsing()
250 DoParsing(0, m_Source
.Length());
253 void wxHtmlParser::DoParsing(int begin_pos
, int end_pos
)
255 if (end_pos
<= begin_pos
) return;
257 wxHtmlTextPieces
& pieces
= *m_TextPieces
;
258 size_t piecesCnt
= pieces
.GetCount();
260 while (begin_pos
< end_pos
)
262 while (m_CurTag
&& m_CurTag
->GetBeginPos() < begin_pos
)
263 m_CurTag
= m_CurTag
->GetNextTag();
264 while (m_CurTextPiece
< piecesCnt
&&
265 pieces
[m_CurTextPiece
].m_pos
< begin_pos
)
268 if (m_CurTextPiece
< piecesCnt
&&
270 pieces
[m_CurTextPiece
].m_pos
< m_CurTag
->GetBeginPos()))
273 AddText(m_Source
.Mid(pieces
[m_CurTextPiece
].m_pos
,
274 pieces
[m_CurTextPiece
].m_lng
));
275 begin_pos
= pieces
[m_CurTextPiece
].m_pos
+
276 pieces
[m_CurTextPiece
].m_lng
;
284 if (m_CurTag
->HasEnding())
285 begin_pos
= m_CurTag
->GetEndPos2();
287 begin_pos
= m_CurTag
->GetBeginPos();
289 wxHtmlTag
*t
= m_CurTag
;
290 m_CurTag
= m_CurTag
->GetNextTag();
297 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
302 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
304 inner
= h
->HandleTag(tag
);
308 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
312 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
314 wxString
s(handler
->GetSupportedTags());
315 wxStringTokenizer
tokenizer(s
, wxT(", "));
317 while (tokenizer
.HasMoreTokens())
318 m_HandlersHash
.Put(tokenizer
.NextToken(), handler
);
320 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
321 m_HandlersList
.Append(handler
);
323 handler
->SetParser(this);
326 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
)
328 wxStringTokenizer
tokenizer(tags
, wxT(", "));
331 if (m_HandlersStack
== NULL
)
333 m_HandlersStack
= new wxList
;
334 m_HandlersStack
->DeleteContents(TRUE
);
337 m_HandlersStack
->Insert(new wxHashTable(m_HandlersHash
));
339 while (tokenizer
.HasMoreTokens())
341 key
= tokenizer
.NextToken();
342 m_HandlersHash
.Delete(key
);
343 m_HandlersHash
.Put(key
, handler
);
347 void wxHtmlParser::PopTagHandler()
351 if (m_HandlersStack
== NULL
||
352 (first
= m_HandlersStack
->GetFirst()) == NULL
)
354 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
357 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
358 m_HandlersStack
->DeleteNode(first
);
361 void wxHtmlParser::SetSourceAndSaveState(const wxString
& src
)
363 wxHtmlParserState
*s
= new wxHtmlParserState
;
365 s
->m_curTag
= m_CurTag
;
367 s
->m_textPieces
= m_TextPieces
;
368 s
->m_curTextPiece
= m_CurTextPiece
;
369 s
->m_source
= m_Source
;
371 s
->m_nextState
= m_SavedStates
;
378 m_Source
= wxEmptyString
;
383 bool wxHtmlParser::RestoreState()
385 if (!m_SavedStates
) return FALSE
;
387 wxHtmlParserState
*s
= m_SavedStates
;
388 m_SavedStates
= s
->m_nextState
;
390 m_CurTag
= s
->m_curTag
;
392 m_TextPieces
= s
->m_textPieces
;
393 m_CurTextPiece
= s
->m_curTextPiece
;
394 m_Source
= s
->m_source
;
400 //-----------------------------------------------------------------------------
402 //-----------------------------------------------------------------------------
404 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
407 //-----------------------------------------------------------------------------
408 // wxHtmlEntitiesParser
409 //-----------------------------------------------------------------------------
411 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
413 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
414 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
415 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
420 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
422 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
427 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
429 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
430 if (encoding
== m_encoding
) return;
433 m_encoding
= encoding
;
434 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
435 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
439 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
441 const wxChar
*c
, *last
;
442 const wxChar
*in_str
= input
.c_str();
445 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
450 output
.append(last
, c
- last
);
451 if (++c
== wxT('\0')) break;
453 const wxChar
*ent_s
= c
;
454 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
455 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
456 (*c
>= wxT('0') && *c
<= wxT('9')) ||
457 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
458 entity
.append(ent_s
, c
- ent_s
);
459 if (*c
!= wxT(';')) c
--;
461 output
<< GetEntityChar(entity
);
464 if (*last
!= wxT('\0'))
469 struct wxHtmlEntityInfo
475 static int LINKAGEMODE
compar_entity(const void *key
, const void *item
)
477 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
480 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
487 wbuf
[0] = (wchar_t)code
;
489 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
490 if (conv
->WC2MB(buf
, wbuf
, 1) == (size_t)-1)
494 return (code
< 256) ? (wxChar
)code
: '?';
498 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
502 if (entity
[0] == wxT('#'))
504 const wxChar
*ent_s
= entity
.c_str();
505 const wxChar
*format
;
507 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
516 if (wxSscanf(ent_s
, format
, &code
) != 1)
521 static wxHtmlEntityInfo substitutions
[] = {
522 { wxT("AElig"),198 },
523 { wxT("Aacute"),193 },
524 { wxT("Acirc"),194 },
525 { wxT("Agrave"),192 },
526 { wxT("Alpha"),913 },
527 { wxT("Aring"),197 },
528 { wxT("Atilde"),195 },
531 { wxT("Ccedil"),199 },
533 { wxT("Dagger"),8225 },
534 { wxT("Delta"),916 },
536 { wxT("Eacute"),201 },
537 { wxT("Ecirc"),202 },
538 { wxT("Egrave"),200 },
539 { wxT("Epsilon"),917 },
542 { wxT("Gamma"),915 },
543 { wxT("Iacute"),205 },
544 { wxT("Icirc"),206 },
545 { wxT("Igrave"),204 },
548 { wxT("Kappa"),922 },
549 { wxT("Lambda"),923 },
551 { wxT("Ntilde"),209 },
553 { wxT("OElig"),338 },
554 { wxT("Oacute"),211 },
555 { wxT("Ocirc"),212 },
556 { wxT("Ograve"),210 },
557 { wxT("Omega"),937 },
558 { wxT("Omicron"),927 },
559 { wxT("Oslash"),216 },
560 { wxT("Otilde"),213 },
564 { wxT("Prime"),8243 },
567 { wxT("Scaron"),352 },
568 { wxT("Sigma"),931 },
569 { wxT("THORN"),222 },
571 { wxT("Theta"),920 },
572 { wxT("Uacute"),218 },
573 { wxT("Ucirc"),219 },
574 { wxT("Ugrave"),217 },
575 { wxT("Upsilon"),933 },
578 { wxT("Yacute"),221 },
581 { wxT("aacute"),225 },
582 { wxT("acirc"),226 },
583 { wxT("acute"),180 },
584 { wxT("aelig"),230 },
585 { wxT("agrave"),224 },
586 { wxT("alefsym"),8501 },
587 { wxT("alpha"),945 },
591 { wxT("aring"),229 },
592 { wxT("asymp"),8776 },
593 { wxT("atilde"),227 },
595 { wxT("bdquo"),8222 },
597 { wxT("brvbar"),166 },
598 { wxT("bull"),8226 },
600 { wxT("ccedil"),231 },
601 { wxT("cedil"),184 },
605 { wxT("clubs"),9827 },
606 { wxT("cong"),8773 },
608 { wxT("crarr"),8629 },
610 { wxT("curren"),164 },
611 { wxT("dArr"),8659 },
612 { wxT("dagger"),8224 },
613 { wxT("darr"),8595 },
615 { wxT("delta"),948 },
616 { wxT("diams"),9830 },
617 { wxT("divide"),247 },
618 { wxT("eacute"),233 },
619 { wxT("ecirc"),234 },
620 { wxT("egrave"),232 },
621 { wxT("empty"),8709 },
622 { wxT("emsp"),8195 },
623 { wxT("ensp"),8194 },
624 { wxT("epsilon"),949 },
625 { wxT("equiv"),8801 },
629 { wxT("euro"),8364 },
630 { wxT("exist"),8707 },
632 { wxT("forall"),8704 },
633 { wxT("frac12"),189 },
634 { wxT("frac14"),188 },
635 { wxT("frac34"),190 },
636 { wxT("frasl"),8260 },
637 { wxT("gamma"),947 },
640 { wxT("hArr"),8660 },
641 { wxT("harr"),8596 },
642 { wxT("hearts"),9829 },
643 { wxT("hellip"),8230 },
644 { wxT("iacute"),237 },
645 { wxT("icirc"),238 },
646 { wxT("iexcl"),161 },
647 { wxT("igrave"),236 },
648 { wxT("image"),8465 },
649 { wxT("infin"),8734 },
652 { wxT("iquest"),191 },
653 { wxT("isin"),8712 },
655 { wxT("kappa"),954 },
656 { wxT("lArr"),8656 },
657 { wxT("lambda"),955 },
658 { wxT("lang"),9001 },
659 { wxT("laquo"),171 },
660 { wxT("larr"),8592 },
661 { wxT("lceil"),8968 },
662 { wxT("ldquo"),8220 },
664 { wxT("lfloor"),8970 },
665 { wxT("lowast"),8727 },
668 { wxT("lsaquo"),8249 },
669 { wxT("lsquo"),8216 },
672 { wxT("mdash"),8212 },
673 { wxT("micro"),181 },
674 { wxT("middot"),183 },
675 { wxT("minus"),8722 },
677 { wxT("nabla"),8711 },
679 { wxT("ndash"),8211 },
683 { wxT("notin"),8713 },
684 { wxT("nsub"),8836 },
685 { wxT("ntilde"),241 },
687 { wxT("oacute"),243 },
688 { wxT("ocirc"),244 },
689 { wxT("oelig"),339 },
690 { wxT("ograve"),242 },
691 { wxT("oline"),8254 },
692 { wxT("omega"),969 },
693 { wxT("omicron"),959 },
694 { wxT("oplus"),8853 },
698 { wxT("oslash"),248 },
699 { wxT("otilde"),245 },
700 { wxT("otimes"),8855 },
703 { wxT("part"),8706 },
704 { wxT("permil"),8240 },
705 { wxT("perp"),8869 },
709 { wxT("plusmn"),177 },
710 { wxT("pound"),163 },
711 { wxT("prime"),8242 },
712 { wxT("prod"),8719 },
713 { wxT("prop"),8733 },
716 { wxT("rArr"),8658 },
717 { wxT("radic"),8730 },
718 { wxT("rang"),9002 },
719 { wxT("raquo"),187 },
720 { wxT("rarr"),8594 },
721 { wxT("rceil"),8969 },
722 { wxT("rdquo"),8221 },
723 { wxT("real"),8476 },
725 { wxT("rfloor"),8971 },
728 { wxT("rsaquo"),8250 },
729 { wxT("rsquo"),8217 },
730 { wxT("sbquo"),8218 },
731 { wxT("scaron"),353 },
732 { wxT("sdot"),8901 },
735 { wxT("sigma"),963 },
736 { wxT("sigmaf"),962 },
738 { wxT("spades"),9824 },
740 { wxT("sube"),8838 },
746 { wxT("supe"),8839 },
747 { wxT("szlig"),223 },
749 { wxT("there4"),8756 },
750 { wxT("theta"),952 },
751 { wxT("thetasym"),977 },
752 { wxT("thinsp"),8201 },
753 { wxT("thorn"),254 },
754 { wxT("tilde"),732 },
755 { wxT("times"),215 },
756 { wxT("trade"),8482 },
757 { wxT("uArr"),8657 },
758 { wxT("uacute"),250 },
759 { wxT("uarr"),8593 },
760 { wxT("ucirc"),251 },
761 { wxT("ugrave"),249 },
763 { wxT("upsih"),978 },
764 { wxT("upsilon"),965 },
766 { wxT("weierp"),8472 },
768 { wxT("yacute"),253 },
773 { wxT("zwnj"),8204 },
775 static size_t substitutions_cnt
= 0;
777 if (substitutions_cnt
== 0)
778 while (substitutions
[substitutions_cnt
].code
!= 0)
781 wxHtmlEntityInfo
*info
;
782 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
784 sizeof(wxHtmlEntityInfo
),
793 return GetCharForCode(code
);