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 class wxHtmlParserState
57 wxHtmlTextPieces
*m_textPieces
;
60 wxHtmlParserState
*m_nextState
;
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser
,wxObject
)
69 wxHtmlParser::wxHtmlParser()
70 : wxObject(), m_HandlersHash(wxKEY_STRING
),
71 m_FS(NULL
), m_HandlersStack(NULL
)
73 m_entitiesParser
= new wxHtmlEntitiesParser
;
81 wxHtmlParser::~wxHtmlParser()
83 while (RestoreState())
85 delete m_HandlersStack
;
86 m_HandlersHash
.Clear();
87 m_HandlersList
.DeleteContents(TRUE
);
88 m_HandlersList
.Clear();
89 delete m_entitiesParser
;
92 wxObject
* wxHtmlParser::Parse(const wxString
& source
)
98 result
= GetProduct();
103 void wxHtmlParser::InitParser(const wxString
& source
)
108 void wxHtmlParser::DoneParser()
113 void wxHtmlParser::SetSource(const wxString
& src
)
122 void wxHtmlParser::CreateDOMTree()
124 wxHtmlTagsCache
cache(m_Source
);
125 m_TextPieces
= new wxHtmlTextPieces
;
126 CreateDOMSubTree(NULL
, 0, m_Source
.Length(), &cache
);
130 void wxHtmlParser::CreateDOMSubTree(wxHtmlTag
*cur
,
131 int begin_pos
, int end_pos
,
132 wxHtmlTagsCache
*cache
)
134 if (end_pos
<= begin_pos
) return;
138 int textBeginning
= begin_pos
;
142 c
= m_Source
.GetChar(i
);
146 // add text to m_TextPieces:
147 if (i
- textBeginning
> 0)
149 wxHtmlTextPiece(textBeginning
, i
- textBeginning
));
151 // if it is a comment, skip it:
152 if (i
< end_pos
-6 && m_Source
.GetChar(i
+1) == wxT('!') &&
153 m_Source
.GetChar(i
+2) == wxT('-') &&
154 m_Source
.GetChar(i
+3) == wxT('-'))
156 // Comments begin with "<!--" and end with "--[ \t\r\n]*>"
157 // according to HTML 4.0
162 c
= m_Source
.GetChar(i
++);
163 if ((c
== wxT(' ') || c
== wxT('\n') ||
164 c
== wxT('\r') || c
== wxT('\t')) && dashes
>= 2) {}
165 else if (c
== wxT('>') && dashes
>= 2)
170 else if (c
== wxT('-'))
177 // add another tag to the tree:
178 else if (i
< end_pos
-1 && m_Source
.GetChar(i
+1) != wxT('/'))
182 chd
= new wxHtmlTag(cur
, m_Source
,
183 i
, end_pos
, cache
, m_entitiesParser
);
186 chd
= new wxHtmlTag(NULL
, m_Source
,
187 i
, end_pos
, cache
, m_entitiesParser
);
190 // if this is the first tag to be created make the root
191 // m_Tags point to it:
196 // if there is already a root tag add this tag as
198 chd
->m_Prev
= m_Tags
->GetLastSibling();
199 chd
->m_Prev
->m_Next
= chd
;
203 if (chd
->HasEnding())
205 CreateDOMSubTree(chd
,
206 chd
->GetBeginPos(), chd
->GetEndPos1(),
208 i
= chd
->GetEndPos2();
211 i
= chd
->GetBeginPos();
215 // ... or skip ending tag:
218 while (i
< end_pos
&& m_Source
.GetChar(i
) != wxT('>')) i
++;
225 // add remaining text to m_TextPieces:
226 if (end_pos
- textBeginning
> 0)
228 wxHtmlTextPiece(textBeginning
, end_pos
- textBeginning
));
231 void wxHtmlParser::DestroyDOMTree()
237 t2
= t1
->GetNextSibling();
241 m_Tags
= m_CurTag
= NULL
;
247 void wxHtmlParser::DoParsing()
251 DoParsing(0, m_Source
.Length());
254 void wxHtmlParser::DoParsing(int begin_pos
, int end_pos
)
256 if (end_pos
<= begin_pos
) return;
258 wxHtmlTextPieces
& pieces
= *m_TextPieces
;
259 size_t piecesCnt
= pieces
.GetCount();
261 while (begin_pos
< end_pos
)
263 while (m_CurTag
&& m_CurTag
->GetBeginPos() < begin_pos
)
264 m_CurTag
= m_CurTag
->GetNextTag();
265 while (m_CurTextPiece
< piecesCnt
&&
266 pieces
[m_CurTextPiece
].m_pos
< begin_pos
)
269 if (m_CurTextPiece
< piecesCnt
&&
271 pieces
[m_CurTextPiece
].m_pos
< m_CurTag
->GetBeginPos()))
274 AddText(GetEntitiesParser()->Parse(
275 m_Source
.Mid(pieces
[m_CurTextPiece
].m_pos
,
276 pieces
[m_CurTextPiece
].m_lng
)));
277 begin_pos
= pieces
[m_CurTextPiece
].m_pos
+
278 pieces
[m_CurTextPiece
].m_lng
;
286 if (m_CurTag
->HasEnding())
287 begin_pos
= m_CurTag
->GetEndPos2();
289 begin_pos
= m_CurTag
->GetBeginPos();
291 wxHtmlTag
*t
= m_CurTag
;
292 m_CurTag
= m_CurTag
->GetNextTag();
299 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
304 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
306 inner
= h
->HandleTag(tag
);
310 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
314 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
316 wxString
s(handler
->GetSupportedTags());
317 wxStringTokenizer
tokenizer(s
, wxT(", "));
319 while (tokenizer
.HasMoreTokens())
320 m_HandlersHash
.Put(tokenizer
.NextToken(), handler
);
322 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
323 m_HandlersList
.Append(handler
);
325 handler
->SetParser(this);
328 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
)
330 wxStringTokenizer
tokenizer(tags
, wxT(", "));
333 if (m_HandlersStack
== NULL
)
335 m_HandlersStack
= new wxList
;
336 m_HandlersStack
->DeleteContents(TRUE
);
339 m_HandlersStack
->Insert(new wxHashTable(m_HandlersHash
));
341 while (tokenizer
.HasMoreTokens())
343 key
= tokenizer
.NextToken();
344 m_HandlersHash
.Delete(key
);
345 m_HandlersHash
.Put(key
, handler
);
349 void wxHtmlParser::PopTagHandler()
353 if (m_HandlersStack
== NULL
||
354 (first
= m_HandlersStack
->GetFirst()) == NULL
)
356 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
359 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
360 m_HandlersStack
->DeleteNode(first
);
363 void wxHtmlParser::SetSourceAndSaveState(const wxString
& src
)
365 wxHtmlParserState
*s
= new wxHtmlParserState
;
367 s
->m_curTag
= m_CurTag
;
369 s
->m_textPieces
= m_TextPieces
;
370 s
->m_curTextPiece
= m_CurTextPiece
;
371 s
->m_source
= m_Source
;
373 s
->m_nextState
= m_SavedStates
;
380 m_Source
= wxEmptyString
;
385 bool wxHtmlParser::RestoreState()
387 if (!m_SavedStates
) return FALSE
;
389 wxHtmlParserState
*s
= m_SavedStates
;
390 m_SavedStates
= s
->m_nextState
;
392 m_CurTag
= s
->m_curTag
;
394 m_TextPieces
= s
->m_textPieces
;
395 m_CurTextPiece
= s
->m_curTextPiece
;
396 m_Source
= s
->m_source
;
402 //-----------------------------------------------------------------------------
404 //-----------------------------------------------------------------------------
406 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
409 //-----------------------------------------------------------------------------
410 // wxHtmlEntitiesParser
411 //-----------------------------------------------------------------------------
413 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
415 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
416 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
417 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
422 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
424 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
429 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
431 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
432 if (encoding
== m_encoding
) return;
435 m_encoding
= encoding
;
436 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
437 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
443 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
445 const wxChar
*c
, *last
;
446 const wxChar
*in_str
= input
.c_str();
449 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
454 output
.append(last
, c
- last
);
455 if (++c
== wxT('\0')) break;
457 const wxChar
*ent_s
= c
;
458 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
459 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
460 (*c
>= wxT('0') && *c
<= wxT('9')) ||
461 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
462 entity
.append(ent_s
, c
- ent_s
);
463 if (*c
!= wxT(';')) c
--;
465 output
<< GetEntityChar(entity
);
468 if (*last
!= wxT('\0'))
473 struct wxHtmlEntityInfo
479 static int LINKAGEMODE
compar_entity(const void *key
, const void *item
)
481 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
484 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
491 wbuf
[0] = (wchar_t)code
;
493 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
494 if (conv
->WC2MB(buf
, wbuf
, 2) == (size_t)-1)
498 return (code
< 256) ? (wxChar
)code
: '?';
502 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
506 if (entity
[0] == wxT('#'))
508 const wxChar
*ent_s
= entity
.c_str();
509 const wxChar
*format
;
511 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
520 if (wxSscanf(ent_s
, format
, &code
) != 1)
525 static wxHtmlEntityInfo substitutions
[] = {
526 { wxT("AElig"),198 },
527 { wxT("Aacute"),193 },
528 { wxT("Acirc"),194 },
529 { wxT("Agrave"),192 },
530 { wxT("Alpha"),913 },
531 { wxT("Aring"),197 },
532 { wxT("Atilde"),195 },
535 { wxT("Ccedil"),199 },
537 { wxT("Dagger"),8225 },
538 { wxT("Delta"),916 },
540 { wxT("Eacute"),201 },
541 { wxT("Ecirc"),202 },
542 { wxT("Egrave"),200 },
543 { wxT("Epsilon"),917 },
546 { wxT("Gamma"),915 },
547 { wxT("Iacute"),205 },
548 { wxT("Icirc"),206 },
549 { wxT("Igrave"),204 },
552 { wxT("Kappa"),922 },
553 { wxT("Lambda"),923 },
555 { wxT("Ntilde"),209 },
557 { wxT("OElig"),338 },
558 { wxT("Oacute"),211 },
559 { wxT("Ocirc"),212 },
560 { wxT("Ograve"),210 },
561 { wxT("Omega"),937 },
562 { wxT("Omicron"),927 },
563 { wxT("Oslash"),216 },
564 { wxT("Otilde"),213 },
568 { wxT("Prime"),8243 },
571 { wxT("Scaron"),352 },
572 { wxT("Sigma"),931 },
573 { wxT("THORN"),222 },
575 { wxT("Theta"),920 },
576 { wxT("Uacute"),218 },
577 { wxT("Ucirc"),219 },
578 { wxT("Ugrave"),217 },
579 { wxT("Upsilon"),933 },
582 { wxT("Yacute"),221 },
585 { wxT("aacute"),225 },
586 { wxT("acirc"),226 },
587 { wxT("acute"),180 },
588 { wxT("aelig"),230 },
589 { wxT("agrave"),224 },
590 { wxT("alefsym"),8501 },
591 { wxT("alpha"),945 },
595 { wxT("aring"),229 },
596 { wxT("asymp"),8776 },
597 { wxT("atilde"),227 },
599 { wxT("bdquo"),8222 },
601 { wxT("brvbar"),166 },
602 { wxT("bull"),8226 },
604 { wxT("ccedil"),231 },
605 { wxT("cedil"),184 },
609 { wxT("clubs"),9827 },
610 { wxT("cong"),8773 },
612 { wxT("crarr"),8629 },
614 { wxT("curren"),164 },
615 { wxT("dArr"),8659 },
616 { wxT("dagger"),8224 },
617 { wxT("darr"),8595 },
619 { wxT("delta"),948 },
620 { wxT("diams"),9830 },
621 { wxT("divide"),247 },
622 { wxT("eacute"),233 },
623 { wxT("ecirc"),234 },
624 { wxT("egrave"),232 },
625 { wxT("empty"),8709 },
626 { wxT("emsp"),8195 },
627 { wxT("ensp"),8194 },
628 { wxT("epsilon"),949 },
629 { wxT("equiv"),8801 },
633 { wxT("euro"),8364 },
634 { wxT("exist"),8707 },
636 { wxT("forall"),8704 },
637 { wxT("frac12"),189 },
638 { wxT("frac14"),188 },
639 { wxT("frac34"),190 },
640 { wxT("frasl"),8260 },
641 { wxT("gamma"),947 },
644 { wxT("hArr"),8660 },
645 { wxT("harr"),8596 },
646 { wxT("hearts"),9829 },
647 { wxT("hellip"),8230 },
648 { wxT("iacute"),237 },
649 { wxT("icirc"),238 },
650 { wxT("iexcl"),161 },
651 { wxT("igrave"),236 },
652 { wxT("image"),8465 },
653 { wxT("infin"),8734 },
656 { wxT("iquest"),191 },
657 { wxT("isin"),8712 },
659 { wxT("kappa"),954 },
660 { wxT("lArr"),8656 },
661 { wxT("lambda"),955 },
662 { wxT("lang"),9001 },
663 { wxT("laquo"),171 },
664 { wxT("larr"),8592 },
665 { wxT("lceil"),8968 },
666 { wxT("ldquo"),8220 },
668 { wxT("lfloor"),8970 },
669 { wxT("lowast"),8727 },
672 { wxT("lsaquo"),8249 },
673 { wxT("lsquo"),8216 },
676 { wxT("mdash"),8212 },
677 { wxT("micro"),181 },
678 { wxT("middot"),183 },
679 { wxT("minus"),8722 },
681 { wxT("nabla"),8711 },
683 { wxT("ndash"),8211 },
687 { wxT("notin"),8713 },
688 { wxT("nsub"),8836 },
689 { wxT("ntilde"),241 },
691 { wxT("oacute"),243 },
692 { wxT("ocirc"),244 },
693 { wxT("oelig"),339 },
694 { wxT("ograve"),242 },
695 { wxT("oline"),8254 },
696 { wxT("omega"),969 },
697 { wxT("omicron"),959 },
698 { wxT("oplus"),8853 },
702 { wxT("oslash"),248 },
703 { wxT("otilde"),245 },
704 { wxT("otimes"),8855 },
707 { wxT("part"),8706 },
708 { wxT("permil"),8240 },
709 { wxT("perp"),8869 },
713 { wxT("plusmn"),177 },
714 { wxT("pound"),163 },
715 { wxT("prime"),8242 },
716 { wxT("prod"),8719 },
717 { wxT("prop"),8733 },
720 { wxT("rArr"),8658 },
721 { wxT("radic"),8730 },
722 { wxT("rang"),9002 },
723 { wxT("raquo"),187 },
724 { wxT("rarr"),8594 },
725 { wxT("rceil"),8969 },
726 { wxT("rdquo"),8221 },
727 { wxT("real"),8476 },
729 { wxT("rfloor"),8971 },
732 { wxT("rsaquo"),8250 },
733 { wxT("rsquo"),8217 },
734 { wxT("sbquo"),8218 },
735 { wxT("scaron"),353 },
736 { wxT("sdot"),8901 },
739 { wxT("sigma"),963 },
740 { wxT("sigmaf"),962 },
742 { wxT("spades"),9824 },
744 { wxT("sube"),8838 },
750 { wxT("supe"),8839 },
751 { wxT("szlig"),223 },
753 { wxT("there4"),8756 },
754 { wxT("theta"),952 },
755 { wxT("thetasym"),977 },
756 { wxT("thinsp"),8201 },
757 { wxT("thorn"),254 },
758 { wxT("tilde"),732 },
759 { wxT("times"),215 },
760 { wxT("trade"),8482 },
761 { wxT("uArr"),8657 },
762 { wxT("uacute"),250 },
763 { wxT("uarr"),8593 },
764 { wxT("ucirc"),251 },
765 { wxT("ugrave"),249 },
767 { wxT("upsih"),978 },
768 { wxT("upsilon"),965 },
770 { wxT("weierp"),8472 },
772 { wxT("yacute"),253 },
777 { wxT("zwnj"),8204 },
779 static size_t substitutions_cnt
= 0;
781 if (substitutions_cnt
== 0)
782 while (substitutions
[substitutions_cnt
].code
!= 0)
785 wxHtmlEntityInfo
*info
;
786 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
788 sizeof(wxHtmlEntityInfo
),
797 return GetCharForCode(code
);