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(m_Source
.Mid(pieces
[m_CurTextPiece
].m_pos
,
275 pieces
[m_CurTextPiece
].m_lng
));
276 begin_pos
= pieces
[m_CurTextPiece
].m_pos
+
277 pieces
[m_CurTextPiece
].m_lng
;
285 if (m_CurTag
->HasEnding())
286 begin_pos
= m_CurTag
->GetEndPos2();
288 begin_pos
= m_CurTag
->GetBeginPos();
290 wxHtmlTag
*t
= m_CurTag
;
291 m_CurTag
= m_CurTag
->GetNextTag();
298 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
303 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
305 inner
= h
->HandleTag(tag
);
309 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
313 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
315 wxString
s(handler
->GetSupportedTags());
316 wxStringTokenizer
tokenizer(s
, wxT(", "));
318 while (tokenizer
.HasMoreTokens())
319 m_HandlersHash
.Put(tokenizer
.NextToken(), handler
);
321 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
322 m_HandlersList
.Append(handler
);
324 handler
->SetParser(this);
327 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
)
329 wxStringTokenizer
tokenizer(tags
, wxT(", "));
332 if (m_HandlersStack
== NULL
)
334 m_HandlersStack
= new wxList
;
335 m_HandlersStack
->DeleteContents(TRUE
);
338 m_HandlersStack
->Insert(new wxHashTable(m_HandlersHash
));
340 while (tokenizer
.HasMoreTokens())
342 key
= tokenizer
.NextToken();
343 m_HandlersHash
.Delete(key
);
344 m_HandlersHash
.Put(key
, handler
);
348 void wxHtmlParser::PopTagHandler()
352 if (m_HandlersStack
== NULL
||
353 (first
= m_HandlersStack
->GetFirst()) == NULL
)
355 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
358 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
359 m_HandlersStack
->DeleteNode(first
);
362 void wxHtmlParser::SetSourceAndSaveState(const wxString
& src
)
364 wxHtmlParserState
*s
= new wxHtmlParserState
;
366 s
->m_curTag
= m_CurTag
;
368 s
->m_textPieces
= m_TextPieces
;
369 s
->m_curTextPiece
= m_CurTextPiece
;
370 s
->m_source
= m_Source
;
372 s
->m_nextState
= m_SavedStates
;
379 m_Source
= wxEmptyString
;
384 bool wxHtmlParser::RestoreState()
386 if (!m_SavedStates
) return FALSE
;
388 wxHtmlParserState
*s
= m_SavedStates
;
389 m_SavedStates
= s
->m_nextState
;
391 m_CurTag
= s
->m_curTag
;
393 m_TextPieces
= s
->m_textPieces
;
394 m_CurTextPiece
= s
->m_curTextPiece
;
395 m_Source
= s
->m_source
;
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
405 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
408 //-----------------------------------------------------------------------------
409 // wxHtmlEntitiesParser
410 //-----------------------------------------------------------------------------
412 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
414 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
415 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
416 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
421 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
423 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
428 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
430 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
431 if (encoding
== m_encoding
) return;
434 m_encoding
= encoding
;
435 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
436 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
440 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
442 const wxChar
*c
, *last
;
443 const wxChar
*in_str
= input
.c_str();
446 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
451 output
.append(last
, c
- last
);
452 if (++c
== wxT('\0')) break;
454 const wxChar
*ent_s
= c
;
455 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
456 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
457 (*c
>= wxT('0') && *c
<= wxT('9')) ||
458 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
459 entity
.append(ent_s
, c
- ent_s
);
460 if (*c
!= wxT(';')) c
--;
462 output
<< GetEntityChar(entity
);
465 if (*last
!= wxT('\0'))
470 struct wxHtmlEntityInfo
476 static int LINKAGEMODE
compar_entity(const void *key
, const void *item
)
478 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
481 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
488 wbuf
[0] = (wchar_t)code
;
490 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
491 if (conv
->WC2MB(buf
, wbuf
, 2) == (size_t)-1)
495 return (code
< 256) ? (wxChar
)code
: '?';
499 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
503 if (entity
[0] == wxT('#'))
505 const wxChar
*ent_s
= entity
.c_str();
506 const wxChar
*format
;
508 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
517 if (wxSscanf(ent_s
, format
, &code
) != 1)
522 static wxHtmlEntityInfo substitutions
[] = {
523 { wxT("AElig"),198 },
524 { wxT("Aacute"),193 },
525 { wxT("Acirc"),194 },
526 { wxT("Agrave"),192 },
527 { wxT("Alpha"),913 },
528 { wxT("Aring"),197 },
529 { wxT("Atilde"),195 },
532 { wxT("Ccedil"),199 },
534 { wxT("Dagger"),8225 },
535 { wxT("Delta"),916 },
537 { wxT("Eacute"),201 },
538 { wxT("Ecirc"),202 },
539 { wxT("Egrave"),200 },
540 { wxT("Epsilon"),917 },
543 { wxT("Gamma"),915 },
544 { wxT("Iacute"),205 },
545 { wxT("Icirc"),206 },
546 { wxT("Igrave"),204 },
549 { wxT("Kappa"),922 },
550 { wxT("Lambda"),923 },
552 { wxT("Ntilde"),209 },
554 { wxT("OElig"),338 },
555 { wxT("Oacute"),211 },
556 { wxT("Ocirc"),212 },
557 { wxT("Ograve"),210 },
558 { wxT("Omega"),937 },
559 { wxT("Omicron"),927 },
560 { wxT("Oslash"),216 },
561 { wxT("Otilde"),213 },
565 { wxT("Prime"),8243 },
568 { wxT("Scaron"),352 },
569 { wxT("Sigma"),931 },
570 { wxT("THORN"),222 },
572 { wxT("Theta"),920 },
573 { wxT("Uacute"),218 },
574 { wxT("Ucirc"),219 },
575 { wxT("Ugrave"),217 },
576 { wxT("Upsilon"),933 },
579 { wxT("Yacute"),221 },
582 { wxT("aacute"),225 },
583 { wxT("acirc"),226 },
584 { wxT("acute"),180 },
585 { wxT("aelig"),230 },
586 { wxT("agrave"),224 },
587 { wxT("alefsym"),8501 },
588 { wxT("alpha"),945 },
592 { wxT("aring"),229 },
593 { wxT("asymp"),8776 },
594 { wxT("atilde"),227 },
596 { wxT("bdquo"),8222 },
598 { wxT("brvbar"),166 },
599 { wxT("bull"),8226 },
601 { wxT("ccedil"),231 },
602 { wxT("cedil"),184 },
606 { wxT("clubs"),9827 },
607 { wxT("cong"),8773 },
609 { wxT("crarr"),8629 },
611 { wxT("curren"),164 },
612 { wxT("dArr"),8659 },
613 { wxT("dagger"),8224 },
614 { wxT("darr"),8595 },
616 { wxT("delta"),948 },
617 { wxT("diams"),9830 },
618 { wxT("divide"),247 },
619 { wxT("eacute"),233 },
620 { wxT("ecirc"),234 },
621 { wxT("egrave"),232 },
622 { wxT("empty"),8709 },
623 { wxT("emsp"),8195 },
624 { wxT("ensp"),8194 },
625 { wxT("epsilon"),949 },
626 { wxT("equiv"),8801 },
630 { wxT("euro"),8364 },
631 { wxT("exist"),8707 },
633 { wxT("forall"),8704 },
634 { wxT("frac12"),189 },
635 { wxT("frac14"),188 },
636 { wxT("frac34"),190 },
637 { wxT("frasl"),8260 },
638 { wxT("gamma"),947 },
641 { wxT("hArr"),8660 },
642 { wxT("harr"),8596 },
643 { wxT("hearts"),9829 },
644 { wxT("hellip"),8230 },
645 { wxT("iacute"),237 },
646 { wxT("icirc"),238 },
647 { wxT("iexcl"),161 },
648 { wxT("igrave"),236 },
649 { wxT("image"),8465 },
650 { wxT("infin"),8734 },
653 { wxT("iquest"),191 },
654 { wxT("isin"),8712 },
656 { wxT("kappa"),954 },
657 { wxT("lArr"),8656 },
658 { wxT("lambda"),955 },
659 { wxT("lang"),9001 },
660 { wxT("laquo"),171 },
661 { wxT("larr"),8592 },
662 { wxT("lceil"),8968 },
663 { wxT("ldquo"),8220 },
665 { wxT("lfloor"),8970 },
666 { wxT("lowast"),8727 },
669 { wxT("lsaquo"),8249 },
670 { wxT("lsquo"),8216 },
673 { wxT("mdash"),8212 },
674 { wxT("micro"),181 },
675 { wxT("middot"),183 },
676 { wxT("minus"),8722 },
678 { wxT("nabla"),8711 },
680 { wxT("ndash"),8211 },
684 { wxT("notin"),8713 },
685 { wxT("nsub"),8836 },
686 { wxT("ntilde"),241 },
688 { wxT("oacute"),243 },
689 { wxT("ocirc"),244 },
690 { wxT("oelig"),339 },
691 { wxT("ograve"),242 },
692 { wxT("oline"),8254 },
693 { wxT("omega"),969 },
694 { wxT("omicron"),959 },
695 { wxT("oplus"),8853 },
699 { wxT("oslash"),248 },
700 { wxT("otilde"),245 },
701 { wxT("otimes"),8855 },
704 { wxT("part"),8706 },
705 { wxT("permil"),8240 },
706 { wxT("perp"),8869 },
710 { wxT("plusmn"),177 },
711 { wxT("pound"),163 },
712 { wxT("prime"),8242 },
713 { wxT("prod"),8719 },
714 { wxT("prop"),8733 },
717 { wxT("rArr"),8658 },
718 { wxT("radic"),8730 },
719 { wxT("rang"),9002 },
720 { wxT("raquo"),187 },
721 { wxT("rarr"),8594 },
722 { wxT("rceil"),8969 },
723 { wxT("rdquo"),8221 },
724 { wxT("real"),8476 },
726 { wxT("rfloor"),8971 },
729 { wxT("rsaquo"),8250 },
730 { wxT("rsquo"),8217 },
731 { wxT("sbquo"),8218 },
732 { wxT("scaron"),353 },
733 { wxT("sdot"),8901 },
736 { wxT("sigma"),963 },
737 { wxT("sigmaf"),962 },
739 { wxT("spades"),9824 },
741 { wxT("sube"),8838 },
747 { wxT("supe"),8839 },
748 { wxT("szlig"),223 },
750 { wxT("there4"),8756 },
751 { wxT("theta"),952 },
752 { wxT("thetasym"),977 },
753 { wxT("thinsp"),8201 },
754 { wxT("thorn"),254 },
755 { wxT("tilde"),732 },
756 { wxT("times"),215 },
757 { wxT("trade"),8482 },
758 { wxT("uArr"),8657 },
759 { wxT("uacute"),250 },
760 { wxT("uarr"),8593 },
761 { wxT("ucirc"),251 },
762 { wxT("ugrave"),249 },
764 { wxT("upsih"),978 },
765 { wxT("upsilon"),965 },
767 { wxT("weierp"),8472 },
769 { wxT("yacute"),253 },
774 { wxT("zwnj"),8204 },
776 static size_t substitutions_cnt
= 0;
778 if (substitutions_cnt
== 0)
779 while (substitutions
[substitutions_cnt
].code
!= 0)
782 wxHtmlEntityInfo
*info
;
783 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
785 sizeof(wxHtmlEntityInfo
),
794 return GetCharForCode(code
);