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()) {}
86 delete m_HandlersStack
;
87 m_HandlersHash
.Clear();
88 m_HandlersList
.DeleteContents(TRUE
);
89 m_HandlersList
.Clear();
90 delete m_entitiesParser
;
93 wxObject
* wxHtmlParser::Parse(const wxString
& source
)
99 result
= GetProduct();
104 void wxHtmlParser::InitParser(const wxString
& source
)
109 void wxHtmlParser::DoneParser()
114 void wxHtmlParser::SetSource(const wxString
& src
)
123 void wxHtmlParser::CreateDOMTree()
125 wxHtmlTagsCache
cache(m_Source
);
126 m_TextPieces
= new wxHtmlTextPieces
;
127 CreateDOMSubTree(NULL
, 0, m_Source
.Length(), &cache
);
131 void wxHtmlParser::CreateDOMSubTree(wxHtmlTag
*cur
,
132 int begin_pos
, int end_pos
,
133 wxHtmlTagsCache
*cache
)
135 if (end_pos
<= begin_pos
) return;
139 int textBeginning
= begin_pos
;
143 c
= m_Source
.GetChar(i
);
147 // add text to m_TextPieces:
148 if (i
- textBeginning
> 0)
150 wxHtmlTextPiece(textBeginning
, i
- textBeginning
));
152 // if it is a comment, skip it:
153 if (i
< end_pos
-6 && m_Source
.GetChar(i
+1) == wxT('!') &&
154 m_Source
.GetChar(i
+2) == wxT('-') &&
155 m_Source
.GetChar(i
+3) == wxT('-'))
157 // Comments begin with "<!--" and end with "--[ \t\r\n]*>"
158 // according to HTML 4.0
163 c
= m_Source
.GetChar(i
++);
164 if ((c
== wxT(' ') || c
== wxT('\n') ||
165 c
== wxT('\r') || c
== wxT('\t')) && dashes
>= 2) {}
166 else if (c
== wxT('>') && dashes
>= 2)
171 else if (c
== wxT('-'))
178 // add another tag to the tree:
179 else if (i
< end_pos
-1 && m_Source
.GetChar(i
+1) != wxT('/'))
183 chd
= new wxHtmlTag(cur
, m_Source
,
184 i
, end_pos
, cache
, m_entitiesParser
);
187 chd
= new wxHtmlTag(NULL
, m_Source
,
188 i
, end_pos
, cache
, m_entitiesParser
);
191 // if this is the first tag to be created make the root
192 // m_Tags point to it:
197 // if there is already a root tag add this tag as
199 chd
->m_Prev
= m_Tags
->GetLastSibling();
200 chd
->m_Prev
->m_Next
= chd
;
204 if (chd
->HasEnding())
206 CreateDOMSubTree(chd
,
207 chd
->GetBeginPos(), chd
->GetEndPos1(),
209 i
= chd
->GetEndPos2();
212 i
= chd
->GetBeginPos();
216 // ... or skip ending tag:
219 while (i
< end_pos
&& m_Source
.GetChar(i
) != wxT('>')) i
++;
226 // add remaining text to m_TextPieces:
227 if (end_pos
- textBeginning
> 0)
229 wxHtmlTextPiece(textBeginning
, end_pos
- textBeginning
));
232 void wxHtmlParser::DestroyDOMTree()
238 t2
= t1
->GetNextSibling();
242 m_Tags
= m_CurTag
= NULL
;
248 void wxHtmlParser::DoParsing()
252 DoParsing(0, m_Source
.Length());
255 void wxHtmlParser::DoParsing(int begin_pos
, int end_pos
)
257 if (end_pos
<= begin_pos
) return;
259 wxHtmlTextPieces
& pieces
= *m_TextPieces
;
260 size_t piecesCnt
= pieces
.GetCount();
262 while (begin_pos
< end_pos
)
264 while (m_CurTag
&& m_CurTag
->GetBeginPos() < begin_pos
)
265 m_CurTag
= m_CurTag
->GetNextTag();
266 while (m_CurTextPiece
< piecesCnt
&&
267 pieces
[m_CurTextPiece
].m_pos
< begin_pos
)
270 if (m_CurTextPiece
< piecesCnt
&&
272 pieces
[m_CurTextPiece
].m_pos
< m_CurTag
->GetBeginPos()))
275 AddText(GetEntitiesParser()->Parse(
276 m_Source
.Mid(pieces
[m_CurTextPiece
].m_pos
,
277 pieces
[m_CurTextPiece
].m_lng
)));
278 begin_pos
= pieces
[m_CurTextPiece
].m_pos
+
279 pieces
[m_CurTextPiece
].m_lng
;
287 if (m_CurTag
->HasEnding())
288 begin_pos
= m_CurTag
->GetEndPos2();
290 begin_pos
= m_CurTag
->GetBeginPos();
292 wxHtmlTag
*t
= m_CurTag
;
293 m_CurTag
= m_CurTag
->GetNextTag();
300 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
305 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
307 inner
= h
->HandleTag(tag
);
311 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
315 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
317 wxString
s(handler
->GetSupportedTags());
318 wxStringTokenizer
tokenizer(s
, wxT(", "));
320 while (tokenizer
.HasMoreTokens())
321 m_HandlersHash
.Put(tokenizer
.GetNextToken(), handler
);
323 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
324 m_HandlersList
.Append(handler
);
326 handler
->SetParser(this);
329 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
)
331 wxStringTokenizer
tokenizer(tags
, wxT(", "));
334 if (m_HandlersStack
== NULL
)
336 m_HandlersStack
= new wxList
;
337 m_HandlersStack
->DeleteContents(TRUE
);
340 m_HandlersStack
->Insert(new wxHashTable(m_HandlersHash
));
342 while (tokenizer
.HasMoreTokens())
344 key
= tokenizer
.GetNextToken();
345 m_HandlersHash
.Delete(key
);
346 m_HandlersHash
.Put(key
, handler
);
350 void wxHtmlParser::PopTagHandler()
354 if (m_HandlersStack
== NULL
||
355 (first
= m_HandlersStack
->GetFirst()) == NULL
)
357 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
360 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
361 m_HandlersStack
->DeleteNode(first
);
364 void wxHtmlParser::SetSourceAndSaveState(const wxString
& src
)
366 wxHtmlParserState
*s
= new wxHtmlParserState
;
368 s
->m_curTag
= m_CurTag
;
370 s
->m_textPieces
= m_TextPieces
;
371 s
->m_curTextPiece
= m_CurTextPiece
;
372 s
->m_source
= m_Source
;
374 s
->m_nextState
= m_SavedStates
;
381 m_Source
= wxEmptyString
;
386 bool wxHtmlParser::RestoreState()
388 if (!m_SavedStates
) return FALSE
;
392 wxHtmlParserState
*s
= m_SavedStates
;
393 m_SavedStates
= s
->m_nextState
;
395 m_CurTag
= s
->m_curTag
;
397 m_TextPieces
= s
->m_textPieces
;
398 m_CurTextPiece
= s
->m_curTextPiece
;
399 m_Source
= s
->m_source
;
405 //-----------------------------------------------------------------------------
407 //-----------------------------------------------------------------------------
409 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
412 //-----------------------------------------------------------------------------
413 // wxHtmlEntitiesParser
414 //-----------------------------------------------------------------------------
416 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
418 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
419 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
420 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
425 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
427 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
432 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
434 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
435 if (encoding
== m_encoding
) return;
438 m_encoding
= encoding
;
439 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
440 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
446 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
448 const wxChar
*c
, *last
;
449 const wxChar
*in_str
= input
.c_str();
452 output
.reserve(input
.length());
454 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
459 output
.append(last
, c
- last
);
460 if (++c
== wxT('\0')) break;
463 const wxChar
*ent_s
= c
;
466 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
467 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
468 (*c
>= wxT('0') && *c
<= wxT('9')) ||
469 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
470 entity
.append(ent_s
, c
- ent_s
);
471 if (*c
!= wxT(';')) c
--;
473 entity_char
= GetEntityChar(entity
);
475 output
<< entity_char
;
478 output
.append(ent_s
-1, c
-ent_s
+2);
479 wxLogDebug(wxT("Unrecognized HTML entity: '%s'"), entity
.c_str());
483 if (*last
!= wxT('\0'))
488 struct wxHtmlEntityInfo
494 extern "C" int LINKAGEMODE
wxHtmlEntityCompare(const void *key
, const void *item
)
496 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
499 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
506 wbuf
[0] = (wchar_t)code
;
508 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
509 if (conv
->WC2MB(buf
, wbuf
, 2) == (size_t)-1)
513 return (code
< 256) ? (wxChar
)code
: '?';
517 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
521 if (entity
[0] == wxT('#'))
523 const wxChar
*ent_s
= entity
.c_str();
524 const wxChar
*format
;
526 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
535 if (wxSscanf(ent_s
, format
, &code
) != 1)
540 static wxHtmlEntityInfo substitutions
[] = {
541 { wxT("AElig"),198 },
542 { wxT("Aacute"),193 },
543 { wxT("Acirc"),194 },
544 { wxT("Agrave"),192 },
545 { wxT("Alpha"),913 },
546 { wxT("Aring"),197 },
547 { wxT("Atilde"),195 },
550 { wxT("Ccedil"),199 },
552 { wxT("Dagger"),8225 },
553 { wxT("Delta"),916 },
555 { wxT("Eacute"),201 },
556 { wxT("Ecirc"),202 },
557 { wxT("Egrave"),200 },
558 { wxT("Epsilon"),917 },
561 { wxT("Gamma"),915 },
562 { wxT("Iacute"),205 },
563 { wxT("Icirc"),206 },
564 { wxT("Igrave"),204 },
567 { wxT("Kappa"),922 },
568 { wxT("Lambda"),923 },
570 { wxT("Ntilde"),209 },
572 { wxT("OElig"),338 },
573 { wxT("Oacute"),211 },
574 { wxT("Ocirc"),212 },
575 { wxT("Ograve"),210 },
576 { wxT("Omega"),937 },
577 { wxT("Omicron"),927 },
578 { wxT("Oslash"),216 },
579 { wxT("Otilde"),213 },
583 { wxT("Prime"),8243 },
586 { wxT("Scaron"),352 },
587 { wxT("Sigma"),931 },
588 { wxT("THORN"),222 },
590 { wxT("Theta"),920 },
591 { wxT("Uacute"),218 },
592 { wxT("Ucirc"),219 },
593 { wxT("Ugrave"),217 },
594 { wxT("Upsilon"),933 },
597 { wxT("Yacute"),221 },
600 { wxT("aacute"),225 },
601 { wxT("acirc"),226 },
602 { wxT("acute"),180 },
603 { wxT("aelig"),230 },
604 { wxT("agrave"),224 },
605 { wxT("alefsym"),8501 },
606 { wxT("alpha"),945 },
610 { wxT("aring"),229 },
611 { wxT("asymp"),8776 },
612 { wxT("atilde"),227 },
614 { wxT("bdquo"),8222 },
616 { wxT("brvbar"),166 },
617 { wxT("bull"),8226 },
619 { wxT("ccedil"),231 },
620 { wxT("cedil"),184 },
624 { wxT("clubs"),9827 },
625 { wxT("cong"),8773 },
627 { wxT("crarr"),8629 },
629 { wxT("curren"),164 },
630 { wxT("dArr"),8659 },
631 { wxT("dagger"),8224 },
632 { wxT("darr"),8595 },
634 { wxT("delta"),948 },
635 { wxT("diams"),9830 },
636 { wxT("divide"),247 },
637 { wxT("eacute"),233 },
638 { wxT("ecirc"),234 },
639 { wxT("egrave"),232 },
640 { wxT("empty"),8709 },
641 { wxT("emsp"),8195 },
642 { wxT("ensp"),8194 },
643 { wxT("epsilon"),949 },
644 { wxT("equiv"),8801 },
648 { wxT("euro"),8364 },
649 { wxT("exist"),8707 },
651 { wxT("forall"),8704 },
652 { wxT("frac12"),189 },
653 { wxT("frac14"),188 },
654 { wxT("frac34"),190 },
655 { wxT("frasl"),8260 },
656 { wxT("gamma"),947 },
659 { wxT("hArr"),8660 },
660 { wxT("harr"),8596 },
661 { wxT("hearts"),9829 },
662 { wxT("hellip"),8230 },
663 { wxT("iacute"),237 },
664 { wxT("icirc"),238 },
665 { wxT("iexcl"),161 },
666 { wxT("igrave"),236 },
667 { wxT("image"),8465 },
668 { wxT("infin"),8734 },
671 { wxT("iquest"),191 },
672 { wxT("isin"),8712 },
674 { wxT("kappa"),954 },
675 { wxT("lArr"),8656 },
676 { wxT("lambda"),955 },
677 { wxT("lang"),9001 },
678 { wxT("laquo"),171 },
679 { wxT("larr"),8592 },
680 { wxT("lceil"),8968 },
681 { wxT("ldquo"),8220 },
683 { wxT("lfloor"),8970 },
684 { wxT("lowast"),8727 },
687 { wxT("lsaquo"),8249 },
688 { wxT("lsquo"),8216 },
691 { wxT("mdash"),8212 },
692 { wxT("micro"),181 },
693 { wxT("middot"),183 },
694 { wxT("minus"),8722 },
696 { wxT("nabla"),8711 },
698 { wxT("ndash"),8211 },
702 { wxT("notin"),8713 },
703 { wxT("nsub"),8836 },
704 { wxT("ntilde"),241 },
706 { wxT("oacute"),243 },
707 { wxT("ocirc"),244 },
708 { wxT("oelig"),339 },
709 { wxT("ograve"),242 },
710 { wxT("oline"),8254 },
711 { wxT("omega"),969 },
712 { wxT("omicron"),959 },
713 { wxT("oplus"),8853 },
717 { wxT("oslash"),248 },
718 { wxT("otilde"),245 },
719 { wxT("otimes"),8855 },
722 { wxT("part"),8706 },
723 { wxT("permil"),8240 },
724 { wxT("perp"),8869 },
728 { wxT("plusmn"),177 },
729 { wxT("pound"),163 },
730 { wxT("prime"),8242 },
731 { wxT("prod"),8719 },
732 { wxT("prop"),8733 },
735 { wxT("rArr"),8658 },
736 { wxT("radic"),8730 },
737 { wxT("rang"),9002 },
738 { wxT("raquo"),187 },
739 { wxT("rarr"),8594 },
740 { wxT("rceil"),8969 },
741 { wxT("rdquo"),8221 },
742 { wxT("real"),8476 },
744 { wxT("rfloor"),8971 },
747 { wxT("rsaquo"),8250 },
748 { wxT("rsquo"),8217 },
749 { wxT("sbquo"),8218 },
750 { wxT("scaron"),353 },
751 { wxT("sdot"),8901 },
754 { wxT("sigma"),963 },
755 { wxT("sigmaf"),962 },
757 { wxT("spades"),9824 },
759 { wxT("sube"),8838 },
765 { wxT("supe"),8839 },
766 { wxT("szlig"),223 },
768 { wxT("there4"),8756 },
769 { wxT("theta"),952 },
770 { wxT("thetasym"),977 },
771 { wxT("thinsp"),8201 },
772 { wxT("thorn"),254 },
773 { wxT("tilde"),732 },
774 { wxT("times"),215 },
775 { wxT("trade"),8482 },
776 { wxT("uArr"),8657 },
777 { wxT("uacute"),250 },
778 { wxT("uarr"),8593 },
779 { wxT("ucirc"),251 },
780 { wxT("ugrave"),249 },
782 { wxT("upsih"),978 },
783 { wxT("upsilon"),965 },
785 { wxT("weierp"),8472 },
787 { wxT("yacute"),253 },
792 { wxT("zwnj"),8204 },
794 static size_t substitutions_cnt
= 0;
796 if (substitutions_cnt
== 0)
797 while (substitutions
[substitutions_cnt
].code
!= 0)
800 wxHtmlEntityInfo
*info
;
801 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
803 sizeof(wxHtmlEntityInfo
),
804 wxHtmlEntityCompare
);
812 return GetCharForCode(code
);
815 wxFSFile
*wxHtmlParser::OpenURL(wxHtmlURLType
WXUNUSED(type
),
816 const wxString
& url
) const
818 return GetFS()->OpenFile(url
);