1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlpars.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_HTML && wxUSE_STREAMS
19 #include "wx/dynarray.h"
23 #include "wx/wxcrtvararg.h"
26 #include "wx/tokenzr.h"
27 #include "wx/wfstream.h"
29 #include "wx/fontmap.h"
30 #include "wx/html/htmldefs.h"
31 #include "wx/html/htmlpars.h"
32 #include "wx/arrimpl.cpp"
35 #include "wx/msw/wince/missing.h" // for bsearch()
38 // DLL options compatibility check:
39 WX_CHECK_BUILD_OPTIONS("wxHTML")
41 const wxChar
*wxTRACE_HTML_DEBUG
= _T("htmldebug");
43 //-----------------------------------------------------------------------------
44 // wxHtmlParser helpers
45 //-----------------------------------------------------------------------------
50 wxHtmlTextPiece(int pos
, int lng
) : m_pos(pos
), m_lng(lng
) {}
54 WX_DECLARE_OBJARRAY(wxHtmlTextPiece
, wxHtmlTextPieces
);
55 WX_DEFINE_OBJARRAY(wxHtmlTextPieces
)
57 class wxHtmlParserState
62 wxHtmlTextPieces
*m_textPieces
;
65 wxHtmlParserState
*m_nextState
;
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser
,wxObject
)
74 wxHtmlParser::wxHtmlParser()
75 : wxObject(), m_HandlersHash(wxKEY_STRING
),
76 m_FS(NULL
), m_HandlersStack(NULL
)
78 m_entitiesParser
= new wxHtmlEntitiesParser
;
86 wxHtmlParser::~wxHtmlParser()
88 while (RestoreState()) {}
93 wxList
& tmp
= *m_HandlersStack
;
94 wxList::iterator it
, en
;
95 for( it
= tmp
.begin(), en
= tmp
.end(); it
!= en
; ++it
)
96 delete (wxHashTable
*)*it
;
99 delete m_HandlersStack
;
100 m_HandlersHash
.Clear();
101 WX_CLEAR_LIST(wxList
, m_HandlersList
);
102 delete m_entitiesParser
;
105 wxObject
* wxHtmlParser::Parse(const wxString
& source
)
109 wxObject
*result
= GetProduct();
114 void wxHtmlParser::InitParser(const wxString
& source
)
117 m_stopParsing
= false;
120 void wxHtmlParser::DoneParser()
125 void wxHtmlParser::SetSource(const wxString
& src
)
134 void wxHtmlParser::CreateDOMTree()
136 wxHtmlTagsCache
cache(m_Source
);
137 m_TextPieces
= new wxHtmlTextPieces
;
138 CreateDOMSubTree(NULL
, 0, m_Source
.length(), &cache
);
142 extern bool wxIsCDATAElement(const wxChar
*tag
);
144 void wxHtmlParser::CreateDOMSubTree(wxHtmlTag
*cur
,
145 int begin_pos
, int end_pos
,
146 wxHtmlTagsCache
*cache
)
148 if (end_pos
<= begin_pos
) return;
152 int textBeginning
= begin_pos
;
154 // If the tag contains CDATA text, we include the text between beginning
155 // and ending tag verbosely. Setting i=end_pos will skip to the very
156 // end of this function where text piece is added, bypassing any child
157 // tags parsing (CDATA element can't have child elements by definition):
158 if (cur
!= NULL
&& wxIsCDATAElement(cur
->GetName().c_str()))
165 c
= m_Source
.GetChar(i
);
169 // add text to m_TextPieces:
170 if (i
- textBeginning
> 0)
172 wxHtmlTextPiece(textBeginning
, i
- textBeginning
));
174 // if it is a comment, skip it:
175 wxString::const_iterator iter
= m_Source
.begin() + i
;
176 if ( SkipCommentTag(iter
, m_Source
.end()) )
179 i
= iter
- m_Source
.begin() + 1; // skip closing '>' too
182 // add another tag to the tree:
183 else if (i
< end_pos
-1 && m_Source
.GetChar(i
+1) != wxT('/'))
187 chd
= new wxHtmlTag(cur
, m_Source
,
188 i
, end_pos
, cache
, m_entitiesParser
);
191 chd
= new wxHtmlTag(NULL
, m_Source
,
192 i
, end_pos
, cache
, m_entitiesParser
);
195 // if this is the first tag to be created make the root
196 // m_Tags point to it:
201 // if there is already a root tag add this tag as
203 chd
->m_Prev
= m_Tags
->GetLastSibling();
204 chd
->m_Prev
->m_Next
= chd
;
208 if (chd
->HasEnding())
210 CreateDOMSubTree(chd
,
211 chd
->GetBeginPos(), chd
->GetEndPos1(),
213 i
= chd
->GetEndPos2();
216 i
= chd
->GetBeginPos();
221 // ... or skip ending tag:
224 while (i
< end_pos
&& m_Source
.GetChar(i
) != wxT('>')) i
++;
231 // add remaining text to m_TextPieces:
232 if (end_pos
- textBeginning
> 0)
234 wxHtmlTextPiece(textBeginning
, end_pos
- textBeginning
));
237 void wxHtmlParser::DestroyDOMTree()
243 t2
= t1
->GetNextSibling();
247 m_Tags
= m_CurTag
= NULL
;
253 void wxHtmlParser::DoParsing()
257 DoParsing(0, m_Source
.length());
260 void wxHtmlParser::DoParsing(int begin_pos
, int end_pos
)
262 if (end_pos
<= begin_pos
) return;
264 wxHtmlTextPieces
& pieces
= *m_TextPieces
;
265 size_t piecesCnt
= pieces
.GetCount();
267 while (begin_pos
< end_pos
)
269 while (m_CurTag
&& m_CurTag
->GetBeginPos() < begin_pos
)
270 m_CurTag
= m_CurTag
->GetNextTag();
271 while (m_CurTextPiece
< piecesCnt
&&
272 pieces
[m_CurTextPiece
].m_pos
< begin_pos
)
275 if (m_CurTextPiece
< piecesCnt
&&
277 pieces
[m_CurTextPiece
].m_pos
< m_CurTag
->GetBeginPos()))
280 AddText(GetEntitiesParser()->Parse(
281 m_Source
.Mid(pieces
[m_CurTextPiece
].m_pos
,
282 pieces
[m_CurTextPiece
].m_lng
)));
283 begin_pos
= pieces
[m_CurTextPiece
].m_pos
+
284 pieces
[m_CurTextPiece
].m_lng
;
289 if (m_CurTag
->HasEnding())
290 begin_pos
= m_CurTag
->GetEndPos2();
292 begin_pos
= m_CurTag
->GetBeginPos();
293 wxHtmlTag
*t
= m_CurTag
;
294 m_CurTag
= m_CurTag
->GetNextTag();
303 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
308 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
311 inner
= h
->HandleTag(tag
);
318 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
322 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
324 wxString
s(handler
->GetSupportedTags());
325 wxStringTokenizer
tokenizer(s
, wxT(", "));
327 while (tokenizer
.HasMoreTokens())
328 m_HandlersHash
.Put(tokenizer
.GetNextToken(), handler
);
330 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
331 m_HandlersList
.Append(handler
);
333 handler
->SetParser(this);
336 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, const wxString
& tags
)
338 wxStringTokenizer
tokenizer(tags
, wxT(", "));
341 if (m_HandlersStack
== NULL
)
343 m_HandlersStack
= new wxList
;
346 m_HandlersStack
->Insert((wxObject
*)new wxHashTable(m_HandlersHash
));
348 while (tokenizer
.HasMoreTokens())
350 key
= tokenizer
.GetNextToken();
351 m_HandlersHash
.Delete(key
);
352 m_HandlersHash
.Put(key
, handler
);
356 void wxHtmlParser::PopTagHandler()
358 wxList::compatibility_iterator first
;
360 if ( !m_HandlersStack
||
362 !(first
= m_HandlersStack
->GetFirst())
364 ((first
= m_HandlersStack
->GetFirst()) == NULL
)
365 #endif // wxUSE_STL/!wxUSE_STL
368 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
371 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
372 delete (wxHashTable
*) first
->GetData();
373 m_HandlersStack
->Erase(first
);
376 void wxHtmlParser::SetSourceAndSaveState(const wxString
& src
)
378 wxHtmlParserState
*s
= new wxHtmlParserState
;
380 s
->m_curTag
= m_CurTag
;
382 s
->m_textPieces
= m_TextPieces
;
383 s
->m_curTextPiece
= m_CurTextPiece
;
384 s
->m_source
= m_Source
;
386 s
->m_nextState
= m_SavedStates
;
393 m_Source
= wxEmptyString
;
398 bool wxHtmlParser::RestoreState()
400 if (!m_SavedStates
) return false;
404 wxHtmlParserState
*s
= m_SavedStates
;
405 m_SavedStates
= s
->m_nextState
;
407 m_CurTag
= s
->m_curTag
;
409 m_TextPieces
= s
->m_textPieces
;
410 m_CurTextPiece
= s
->m_curTextPiece
;
411 m_Source
= s
->m_source
;
417 wxString
wxHtmlParser::GetInnerSource(const wxHtmlTag
& tag
)
419 return GetSource()->Mid(tag
.GetBeginPos(),
420 tag
.GetEndPos1() - tag
.GetBeginPos());
423 //-----------------------------------------------------------------------------
425 //-----------------------------------------------------------------------------
427 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
429 void wxHtmlTagHandler::ParseInnerSource(const wxString
& source
)
431 // It is safe to temporarily change the source being parsed,
432 // provided we restore the state back after parsing
433 m_Parser
->SetSourceAndSaveState(source
);
434 m_Parser
->DoParsing();
435 m_Parser
->RestoreState();
439 //-----------------------------------------------------------------------------
440 // wxHtmlEntitiesParser
441 //-----------------------------------------------------------------------------
443 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
445 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
446 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
447 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
452 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
454 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
459 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
461 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
462 if (encoding
== m_encoding
)
467 m_encoding
= encoding
;
468 if (m_encoding
== wxFONTENCODING_SYSTEM
)
471 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
477 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
479 const wxChar
*c
, *last
;
480 const wxChar
*in_str
= input
.c_str();
483 output
.reserve(input
.length());
485 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
490 output
.append(last
, c
- last
);
491 if ( *++c
== wxT('\0') )
495 const wxChar
*ent_s
= c
;
498 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
499 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
500 (*c
>= wxT('0') && *c
<= wxT('9')) ||
501 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
502 entity
.append(ent_s
, c
- ent_s
);
503 if (*c
!= wxT(';')) c
--;
505 entity_char
= GetEntityChar(entity
);
507 output
<< entity_char
;
510 output
.append(ent_s
-1, c
-ent_s
+2);
511 wxLogTrace(wxTRACE_HTML_DEBUG
,
512 wxT("Unrecognized HTML entity: '%s'"),
517 if (*last
!= wxT('\0'))
522 struct wxHtmlEntityInfo
528 extern "C" int LINKAGEMODE
wxHtmlEntityCompare(const void *key
, const void *item
)
530 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
534 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
539 wbuf
[0] = (wchar_t)code
;
541 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
542 if (conv
->WC2MB(buf
, wbuf
, 2) == (size_t)-1)
546 return (code
< 256) ? (wxChar
)code
: '?';
551 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
555 if (entity
[0] == wxT('#'))
557 const wxChar
*ent_s
= entity
.c_str();
558 const wxChar
*format
;
560 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
569 if (wxSscanf(ent_s
, format
, &code
) != 1)
574 static wxHtmlEntityInfo substitutions
[] = {
575 { wxT("AElig"),198 },
576 { wxT("Aacute"),193 },
577 { wxT("Acirc"),194 },
578 { wxT("Agrave"),192 },
579 { wxT("Alpha"),913 },
580 { wxT("Aring"),197 },
581 { wxT("Atilde"),195 },
584 { wxT("Ccedil"),199 },
586 { wxT("Dagger"),8225 },
587 { wxT("Delta"),916 },
589 { wxT("Eacute"),201 },
590 { wxT("Ecirc"),202 },
591 { wxT("Egrave"),200 },
592 { wxT("Epsilon"),917 },
595 { wxT("Gamma"),915 },
596 { wxT("Iacute"),205 },
597 { wxT("Icirc"),206 },
598 { wxT("Igrave"),204 },
601 { wxT("Kappa"),922 },
602 { wxT("Lambda"),923 },
604 { wxT("Ntilde"),209 },
606 { wxT("OElig"),338 },
607 { wxT("Oacute"),211 },
608 { wxT("Ocirc"),212 },
609 { wxT("Ograve"),210 },
610 { wxT("Omega"),937 },
611 { wxT("Omicron"),927 },
612 { wxT("Oslash"),216 },
613 { wxT("Otilde"),213 },
617 { wxT("Prime"),8243 },
620 { wxT("Scaron"),352 },
621 { wxT("Sigma"),931 },
622 { wxT("THORN"),222 },
624 { wxT("Theta"),920 },
625 { wxT("Uacute"),218 },
626 { wxT("Ucirc"),219 },
627 { wxT("Ugrave"),217 },
628 { wxT("Upsilon"),933 },
631 { wxT("Yacute"),221 },
634 { wxT("aacute"),225 },
635 { wxT("acirc"),226 },
636 { wxT("acute"),180 },
637 { wxT("aelig"),230 },
638 { wxT("agrave"),224 },
639 { wxT("alefsym"),8501 },
640 { wxT("alpha"),945 },
644 { wxT("aring"),229 },
645 { wxT("asymp"),8776 },
646 { wxT("atilde"),227 },
648 { wxT("bdquo"),8222 },
650 { wxT("brvbar"),166 },
651 { wxT("bull"),8226 },
653 { wxT("ccedil"),231 },
654 { wxT("cedil"),184 },
658 { wxT("clubs"),9827 },
659 { wxT("cong"),8773 },
661 { wxT("crarr"),8629 },
663 { wxT("curren"),164 },
664 { wxT("dArr"),8659 },
665 { wxT("dagger"),8224 },
666 { wxT("darr"),8595 },
668 { wxT("delta"),948 },
669 { wxT("diams"),9830 },
670 { wxT("divide"),247 },
671 { wxT("eacute"),233 },
672 { wxT("ecirc"),234 },
673 { wxT("egrave"),232 },
674 { wxT("empty"),8709 },
675 { wxT("emsp"),8195 },
676 { wxT("ensp"),8194 },
677 { wxT("epsilon"),949 },
678 { wxT("equiv"),8801 },
682 { wxT("euro"),8364 },
683 { wxT("exist"),8707 },
685 { wxT("forall"),8704 },
686 { wxT("frac12"),189 },
687 { wxT("frac14"),188 },
688 { wxT("frac34"),190 },
689 { wxT("frasl"),8260 },
690 { wxT("gamma"),947 },
693 { wxT("hArr"),8660 },
694 { wxT("harr"),8596 },
695 { wxT("hearts"),9829 },
696 { wxT("hellip"),8230 },
697 { wxT("iacute"),237 },
698 { wxT("icirc"),238 },
699 { wxT("iexcl"),161 },
700 { wxT("igrave"),236 },
701 { wxT("image"),8465 },
702 { wxT("infin"),8734 },
705 { wxT("iquest"),191 },
706 { wxT("isin"),8712 },
708 { wxT("kappa"),954 },
709 { wxT("lArr"),8656 },
710 { wxT("lambda"),955 },
711 { wxT("lang"),9001 },
712 { wxT("laquo"),171 },
713 { wxT("larr"),8592 },
714 { wxT("lceil"),8968 },
715 { wxT("ldquo"),8220 },
717 { wxT("lfloor"),8970 },
718 { wxT("lowast"),8727 },
721 { wxT("lsaquo"),8249 },
722 { wxT("lsquo"),8216 },
725 { wxT("mdash"),8212 },
726 { wxT("micro"),181 },
727 { wxT("middot"),183 },
728 { wxT("minus"),8722 },
730 { wxT("nabla"),8711 },
732 { wxT("ndash"),8211 },
736 { wxT("notin"),8713 },
737 { wxT("nsub"),8836 },
738 { wxT("ntilde"),241 },
740 { wxT("oacute"),243 },
741 { wxT("ocirc"),244 },
742 { wxT("oelig"),339 },
743 { wxT("ograve"),242 },
744 { wxT("oline"),8254 },
745 { wxT("omega"),969 },
746 { wxT("omicron"),959 },
747 { wxT("oplus"),8853 },
751 { wxT("oslash"),248 },
752 { wxT("otilde"),245 },
753 { wxT("otimes"),8855 },
756 { wxT("part"),8706 },
757 { wxT("permil"),8240 },
758 { wxT("perp"),8869 },
762 { wxT("plusmn"),177 },
763 { wxT("pound"),163 },
764 { wxT("prime"),8242 },
765 { wxT("prod"),8719 },
766 { wxT("prop"),8733 },
769 { wxT("rArr"),8658 },
770 { wxT("radic"),8730 },
771 { wxT("rang"),9002 },
772 { wxT("raquo"),187 },
773 { wxT("rarr"),8594 },
774 { wxT("rceil"),8969 },
775 { wxT("rdquo"),8221 },
776 { wxT("real"),8476 },
778 { wxT("rfloor"),8971 },
781 { wxT("rsaquo"),8250 },
782 { wxT("rsquo"),8217 },
783 { wxT("sbquo"),8218 },
784 { wxT("scaron"),353 },
785 { wxT("sdot"),8901 },
788 { wxT("sigma"),963 },
789 { wxT("sigmaf"),962 },
791 { wxT("spades"),9824 },
793 { wxT("sube"),8838 },
799 { wxT("supe"),8839 },
800 { wxT("szlig"),223 },
802 { wxT("there4"),8756 },
803 { wxT("theta"),952 },
804 { wxT("thetasym"),977 },
805 { wxT("thinsp"),8201 },
806 { wxT("thorn"),254 },
807 { wxT("tilde"),732 },
808 { wxT("times"),215 },
809 { wxT("trade"),8482 },
810 { wxT("uArr"),8657 },
811 { wxT("uacute"),250 },
812 { wxT("uarr"),8593 },
813 { wxT("ucirc"),251 },
814 { wxT("ugrave"),249 },
816 { wxT("upsih"),978 },
817 { wxT("upsilon"),965 },
819 { wxT("weierp"),8472 },
821 { wxT("yacute"),253 },
826 { wxT("zwnj"),8204 },
828 static size_t substitutions_cnt
= 0;
830 if (substitutions_cnt
== 0)
831 while (substitutions
[substitutions_cnt
].code
!= 0)
834 wxHtmlEntityInfo
*info
= NULL
;
836 // bsearch crashes under WinCE for some reason
838 for (i
= 0; i
< substitutions_cnt
; i
++)
840 if (entity
== substitutions
[i
].name
)
842 info
= & substitutions
[i
];
847 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
849 sizeof(wxHtmlEntityInfo
),
850 wxHtmlEntityCompare
);
859 return GetCharForCode(code
);
862 wxFSFile
*wxHtmlParser::OpenURL(wxHtmlURLType
WXUNUSED(type
),
863 const wxString
& url
) const
865 return m_FS
? m_FS
->OpenFile(url
) : NULL
;
870 //-----------------------------------------------------------------------------
871 // wxHtmlParser::ExtractCharsetInformation
872 //-----------------------------------------------------------------------------
874 class wxMetaTagParser
: public wxHtmlParser
877 wxMetaTagParser() { }
879 wxObject
* GetProduct() { return NULL
; }
882 virtual void AddText(const wxChar
* WXUNUSED(txt
)) {}
884 DECLARE_NO_COPY_CLASS(wxMetaTagParser
)
887 class wxMetaTagHandler
: public wxHtmlTagHandler
890 wxMetaTagHandler(wxString
*retval
) : wxHtmlTagHandler(), m_retval(retval
) {}
891 wxString
GetSupportedTags() { return wxT("META,BODY"); }
892 bool HandleTag(const wxHtmlTag
& tag
);
897 DECLARE_NO_COPY_CLASS(wxMetaTagHandler
)
900 bool wxMetaTagHandler::HandleTag(const wxHtmlTag
& tag
)
902 if (tag
.GetName() == _T("BODY"))
904 m_Parser
->StopParsing();
908 if (tag
.HasParam(_T("HTTP-EQUIV")) &&
909 tag
.GetParam(_T("HTTP-EQUIV")).IsSameAs(_T("Content-Type"), false) &&
910 tag
.HasParam(_T("CONTENT")))
912 wxString content
= tag
.GetParam(_T("CONTENT")).Lower();
913 if (content
.Left(19) == _T("text/html; charset="))
915 *m_retval
= content
.Mid(19);
916 m_Parser
->StopParsing();
924 wxString
wxHtmlParser::ExtractCharsetInformation(const wxString
& markup
)
927 wxMetaTagParser
*parser
= new wxMetaTagParser();
930 parser
->AddTagHandler(new wxMetaTagHandler(&charset
));
931 parser
->Parse(markup
);
939 wxHtmlParser::SkipCommentTag(wxString::const_iterator
& start
,
940 wxString::const_iterator end
)
942 wxASSERT_MSG( *start
== '<', _T("should be called on the tag start") );
944 wxString::const_iterator p
= start
;
946 // comments begin with "<!--" in HTML 4.0
947 if ( end
- p
< 3 || *++p
!= '!' || *++p
!= '-' || *++p
!= '-' )
949 // not a comment at all
953 // skip the start of the comment tag in any case, if we don't find the
954 // closing tag we should ignore broken markup
957 // comments end with "--[ \t\r\n]*>", i.e. white space is allowed between
958 // comment delimiter and the closing tag character (section 3.2.4 of
959 // http://www.w3.org/TR/html401/)
965 if ( (c
== wxT(' ') || c
== wxT('\n') ||
966 c
== wxT('\r') || c
== wxT('\t')) && dashes
>= 2 )
968 // ignore white space before potential tag end
972 if ( c
== wxT('>') && dashes
>= 2 )
974 // found end of comment