9758d6e1f15e68c05fdad3ed8fad4cab3ad7de09
[claws.git] / src / html.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "html.h"
26 #include "codeconv.h"
27 #include "utils.h"
28
29 #define HTMLBUFSIZE     8192
30 #define HR_STR          "------------------------------------------------"
31
32 typedef struct _HTMLSymbol      HTMLSymbol;
33
34 struct _HTMLSymbol
35 {
36         gchar *const key;
37         gchar *const val;
38 };
39
40 static HTMLSymbol symbol_list[] = {
41         {"&lt;"    , "<"},
42         {"&gt;"    , ">"},
43         {"&amp;"   , "&"},
44         {"&quot;"  , "\""},
45         {"&nbsp;"  , " "},
46         {"&trade;" , "(TM)"},
47
48         {"&#153;", "(TM)"},
49 };
50
51 static HTMLSymbol ascii_symbol_list[] = {
52         {"&iexcl;" , "^!"},
53         {"&brvbar;", "|"},
54         {"&copy;"  , "(C)"},
55         {"&laquo;" , "<<"},
56         {"&reg;"   , "(R)"},
57
58         {"&sup2;"  , "^2"},
59         {"&sup3;"  , "^3"},
60         {"&acute;" , "'"},
61         {"&cedil;" , ","},
62         {"&sup1;"  , "^1"},
63         {"&raquo;" , ">>"},
64         {"&frac14;", "1/4"},
65         {"&frac12;", "1/2"},
66         {"&frac34;", "3/4"},
67         {"&iquest;", "^?"},
68
69         {"&Agrave;", "A`"},
70         {"&Aacute;", "A'"},
71         {"&Acirc;" , "A^"},
72         {"&Atilde;", "A~"},
73         {"&AElig;" , "AE"},
74         {"&Egrave;", "E`"},
75         {"&Eacute;", "E'"},
76         {"&Ecirc;" , "E^"},
77         {"&Igrave;", "I`"},
78         {"&Iacute;", "I'"},
79         {"&Icirc;" , "I^"},
80
81         {"&Ntilde;", "N~"},
82         {"&Ograve;", "O`"},
83         {"&Oacute;", "O'"},
84         {"&Ocirc;" , "O^"},
85         {"&Otilde;", "O~"},
86         {"&Ugrave;", "U`"},
87         {"&Uacute;", "U'"},
88         {"&Ucirc;" , "U^"},
89         {"&Yacute;", "Y'"},
90
91         {"&agrave;", "a`"},
92         {"&aacute;", "a'"},
93         {"&acirc;" , "a^"},
94         {"&atilde;", "a~"},
95         {"&aelig;" , "ae"},
96         {"&egrave;", "e`"},
97         {"&eacute;", "e'"},
98         {"&ecirc;" , "e^"},
99         {"&igrave;", "i`"},
100         {"&iacute;", "i'"},
101         {"&icirc;" , "i^"},
102
103         {"&ntilde;", "n~"},
104         {"&ograve;", "o`"},
105         {"&oacute;", "o'"},
106         {"&ocirc;" , "o^"},
107         {"&otilde;", "o~"},
108         {"&ugrave;", "u`"},
109         {"&uacute;", "u'"},
110         {"&ucirc;" , "u^"},
111         {"&yacute;", "y'"},
112 };
113
114 static HTMLSymbol eucjp_symbol_list[] = {
115         {"&iexcl;" , "^!"},
116         {"&cent;"  , "\xa1\xf1"},
117         {"&pound;" , "\xa1\xf2"},
118         {"&yen;"   , "\xa1\xef"},
119         {"&brvbar;", "|"},
120         {"&sect;"  , "\xa1\xf8"},
121         {"&uml;"   , "\xa1\xaf"},
122         {"&copy;"  , "(C)"},
123         {"&laquo;" , "<<"},
124         {"&reg;"   , "(R)"},
125
126         {"&deg;"   , "\xa1\xeb"},
127         {"&plusmn;", "\xa1\xde"},
128         {"&sup2;"  , "^2"},
129         {"&sup3;"  , "^3"},
130         {"&acute;" , "'"},
131         {"&micro;" , "\xa6\xcc"},
132         {"&para;"  , "\xa2\xf9"},
133         {"&middot;", "\xa1\xa6"},
134         {"&cedil;" , ","},
135         {"&sup1;"  , "^1"},
136         {"&raquo;" , ">>"},
137         {"&frac14;", "1/4"},
138         {"&frac12;", "1/2"},
139         {"&frac34;", "3/4"},
140         {"&iquest;", "^?"},
141
142         {"&Agrave;", "A`"},
143         {"&Aacute;", "A'"},
144         {"&Acirc;" , "A^"},
145         {"&Atilde;", "A~"},
146         {"&Auml;"  , "A\xa1\xaf"},
147         {"&Aring;" , "A\xa1\xeb"},
148         {"&AElig;" , "AE"},
149         {"&Egrave;", "E`"},
150         {"&Eacute;", "E'"},
151         {"&Ecirc;" , "E^"},
152         {"&Euml;"  , "E\xa1\xaf"},
153         {"&Igrave;", "I`"},
154         {"&Iacute;", "I'"},
155         {"&Icirc;" , "I^"},
156         {"&Iuml;"  , "I\xa1\xaf"},
157
158         {"&Ntilde;", "N~"},
159         {"&Ograve;", "O`"},
160         {"&Oacute;", "O'"},
161         {"&Ocirc;" , "O^"},
162         {"&Otilde;", "O~"},
163         {"&Ouml;"  , "O\xa1\xaf"},
164         {"&times;" , "\xa1\xdf"},
165         {"&Ugrave;", "U`"},
166         {"&Uacute;", "U'"},
167         {"&Ucirc;" , "U^"},
168         {"&Uuml;"  , "U\xa1\xaf"},
169         {"&Yacute;", "Y'"},
170
171         {"&agrave;", "a`"},
172         {"&aacute;", "a'"},
173         {"&acirc;" , "a^"},
174         {"&atilde;", "a~"},
175         {"&auml;"  , "a\xa1\xaf"},
176         {"&aring;" , "a\xa1\xeb"},
177         {"&aelig;" , "ae"},
178         {"&egrave;", "e`"},
179         {"&eacute;", "e'"},
180         {"&ecirc;" , "e^"},
181         {"&euml;"  , "e\xa1\xaf"},
182         {"&igrave;", "i`"},
183         {"&iacute;", "i'"},
184         {"&icirc;" , "i^"},
185         {"&iuml;"  , "i\xa1\xaf"},
186
187         {"&eth;"   , "\xa2\xdf"},
188         {"&ntilde;", "n~"},
189         {"&ograve;", "o`"},
190         {"&oacute;", "o'"},
191         {"&ocirc;" , "o^"},
192         {"&otilde;", "o~"},
193         {"&ouml;"  , "o\xa1\xaf"},
194         {"&divide;", "\xa1\xe0"},
195         {"&ugrave;", "u`"},
196         {"&uacute;", "u'"},
197         {"&ucirc;" , "u^"},
198         {"&uuml;"  , "u\xa1\xaf"},
199         {"&yacute;", "y'"},
200         {"&yuml;"  , "y\xa1\xaf"},
201 };
202
203 static HTMLSymbol latin_symbol_list[] = {
204         {"&iexcl;" , "\xa1"},
205         {"&cent;"  , "\xa2"},
206         {"&pound;" , "\xa3"},
207         {"&curren;", "\xa4"},
208         {"&yen;"   , "\xa5"},
209         {"&brvbar;", "\xa6"},
210         {"&sect;"  , "\xa7"},
211         {"&uml;"   , "\xa8"},
212         {"&copy;"  , "\xa9"},
213         {"&ordf;"  , "\xaa"},
214         {"&laquo;" , "\xab"},
215         {"&not;"   , "\xac"},
216         {"&shy;"   , "\xad"},
217         {"&reg;"   , "\xae"},
218         {"&macr;"  , "\xaf"},
219
220         {"&deg;"   , "\xb0"},
221         {"&plusmn;", "\xb1"},
222         {"&sup2;"  , "\xb2"},
223         {"&sup3;"  , "\xb3"},
224         {"&acute;" , "\xb4"},
225         {"&micro;" , "\xb5"},
226         {"&para;"  , "\xb6"},
227         {"&middot;", "\xb7"},
228         {"&cedil;" , "\xb8"},
229         {"&sup1;"  , "\xb9"},
230         {"&ordm;"  , "\xba"},
231         {"&raquo;" , "\xbb"},
232         {"&frac14;", "\xbc"},
233         {"&frac12;", "\xbd"},
234         {"&frac34;", "\xbe"},
235         {"&iquest;", "\xbf"},
236
237         {"&Agrave;", "\xc0"},
238         {"&Aacute;", "\xc1"},
239         {"&Acirc;" , "\xc2"},
240         {"&Atilde;", "\xc3"},
241         {"&Auml;"  , "\xc4"},
242         {"&Aring;" , "\xc5"},
243         {"&AElig;" , "\xc6"},
244         {"&Ccedil;", "\xc7"},
245         {"&Egrave;", "\xc8"},
246         {"&Eacute;", "\xc9"},
247         {"&Ecirc;" , "\xca"},
248         {"&Euml;"  , "\xcb"},
249         {"&Igrave;", "\xcc"},
250         {"&Iacute;", "\xcd"},
251         {"&Icirc;" , "\xce"},
252         {"&Iuml;"  , "\xcf"},
253
254         {"&ETH;"   , "\xd0"},
255         {"&Ntilde;", "\xd1"},
256         {"&Ograve;", "\xd2"},
257         {"&Oacute;", "\xd3"},
258         {"&Ocirc;" , "\xd4"},
259         {"&Otilde;", "\xd5"},
260         {"&Ouml;"  , "\xd6"},
261         {"&times;" , "\xd7"},
262         {"&Oslash;", "\xd8"},
263         {"&Ugrave;", "\xd9"},
264         {"&Uacute;", "\xda"},
265         {"&Ucirc;" , "\xdb"},
266         {"&Uuml;"  , "\xdc"},
267         {"&Yacute;", "\xdd"},
268         {"&THORN;" , "\xde"},
269         {"&szlig;" , "\xdf"},
270
271         {"&agrave;", "\xe0"},
272         {"&aacute;", "\xe1"},
273         {"&acirc;" , "\xe2"},
274         {"&atilde;", "\xe3"},
275         {"&auml;"  , "\xe4"},
276         {"&aring;" , "\xe5"},
277         {"&aelig;" , "\xe6"},
278         {"&ccedil;", "\xe7"},
279         {"&egrave;", "\xe8"},
280         {"&eacute;", "\xe9"},
281         {"&ecirc;" , "\xea"},
282         {"&euml;"  , "\xeb"},
283         {"&igrave;", "\xec"},
284         {"&iacute;", "\xed"},
285         {"&icirc;" , "\xee"},
286         {"&iuml;"  , "\xef"},
287
288         {"&eth;"   , "\xf0"},
289         {"&ntilde;", "\xf1"},
290         {"&ograve;", "\xf2"},
291         {"&oacute;", "\xf3"},
292         {"&ocirc;" , "\xf4"},
293         {"&otilde;", "\xf5"},
294         {"&ouml;"  , "\xf6"},
295         {"&divide;", "\xf7"},
296         {"&oslash;", "\xf8"},
297         {"&ugrave;", "\xf9"},
298         {"&uacute;", "\xfa"},
299         {"&ucirc;" , "\xfb"},
300         {"&uuml;"  , "\xfc"},
301         {"&yacute;", "\xfd"},
302         {"&thorn;" , "\xfe"},
303         {"&yuml;"  , "\xff"},
304 };
305
306 static GHashTable *default_symbol_table;
307 static GHashTable *eucjp_symbol_table;
308 static GHashTable *latin_symbol_table;
309
310 static HTMLState html_read_line         (HTMLParser     *parser);
311 static void html_append_char            (HTMLParser     *parser,
312                                          gchar           ch);
313 static void html_append_str             (HTMLParser     *parser,
314                                          const gchar    *str,
315                                          gint            len);
316 static HTMLState html_parse_tag         (HTMLParser     *parser);
317 static void html_parse_special          (HTMLParser     *parser);
318 static void html_get_parenthesis        (HTMLParser     *parser,
319                                          gchar          *buf,
320                                          gint            len);
321
322 #if 0
323 static gint g_str_case_equal            (gconstpointer   v,
324                                          gconstpointer   v2);
325 static guint g_str_case_hash            (gconstpointer   key);
326 #endif
327
328 HTMLParser *html_parser_new(FILE *fp, CodeConverter *conv)
329 {
330         HTMLParser *parser;
331
332         g_return_val_if_fail(fp != NULL, NULL);
333         g_return_val_if_fail(conv != NULL, NULL);
334
335         parser = g_new0(HTMLParser, 1);
336         parser->fp = fp;
337         parser->conv = conv;
338         parser->str = g_string_new(NULL);
339         parser->buf = g_string_new(NULL);
340         parser->bufp = parser->buf->str;
341         parser->newline = TRUE;
342         parser->empty_line = TRUE;
343         parser->space = FALSE;
344         parser->pre = FALSE;
345
346 #define SYMBOL_TABLE_ADD(table, list) \
347 { \
348         gint i; \
349  \
350         for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
351                 g_hash_table_insert(table, list[i].key, list[i].val); \
352 }
353
354         if (!default_symbol_table) {
355                 default_symbol_table =
356                         g_hash_table_new(g_str_hash, g_str_equal);
357                 SYMBOL_TABLE_ADD(default_symbol_table, symbol_list);
358                 SYMBOL_TABLE_ADD(default_symbol_table, ascii_symbol_list);
359         }
360         if (!eucjp_symbol_table) {
361                 eucjp_symbol_table =
362                         g_hash_table_new(g_str_hash, g_str_equal);
363                 SYMBOL_TABLE_ADD(eucjp_symbol_table, symbol_list);
364                 SYMBOL_TABLE_ADD(eucjp_symbol_table, eucjp_symbol_list);
365         }
366         if (!latin_symbol_table) {
367                 latin_symbol_table =
368                         g_hash_table_new(g_str_hash, g_str_equal);
369                 SYMBOL_TABLE_ADD(latin_symbol_table, symbol_list);
370                 SYMBOL_TABLE_ADD(latin_symbol_table, latin_symbol_list);
371         }
372
373 #undef SYMBOL_TABLE_ADD
374
375         if (conv->charset == C_ISO_8859_1)
376                 parser->symbol_table = latin_symbol_table;
377         else if ((conv->charset == C_ISO_2022_JP   ||
378                   conv->charset == C_ISO_2022_JP_2 ||
379                   conv->charset == C_EUC_JP        ||
380                   conv->charset == C_SHIFT_JIS) &&
381                  conv_get_current_charset() == C_EUC_JP)
382                 parser->symbol_table = eucjp_symbol_table;
383         else
384                 parser->symbol_table = default_symbol_table;
385
386         return parser;
387 }
388
389 void html_parser_destroy(HTMLParser *parser)
390 {
391         g_string_free(parser->str, TRUE);
392         g_string_free(parser->buf, TRUE);
393         g_free(parser);
394 }
395
396 gchar *html_parse(HTMLParser *parser)
397 {
398         parser->state = HTML_NORMAL;
399         g_string_truncate(parser->str, 0);
400
401         if (*parser->bufp == '\0') {
402                 g_string_truncate(parser->buf, 0);
403                 parser->bufp = parser->buf->str;
404                 if (html_read_line(parser) == HTML_EOF)
405                         return NULL;
406         }
407
408         while (*parser->bufp != '\0') {
409                 switch (*parser->bufp) {
410                 case '<':
411                         if (parser->str->len == 0)
412                                 html_parse_tag(parser);
413                         else
414                                 return parser->str->str;
415                         break;
416                 case '&':
417                         html_parse_special(parser);
418                         break;
419                 case ' ':
420                 case '\t':
421                 case '\r':
422                 case '\n':
423                         if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n')
424                                 parser->bufp++;
425
426                         if (!parser->pre) {
427                                 if (!parser->newline)
428                                         parser->space = TRUE;
429
430                                 parser->bufp++;
431                                 break;
432                         }
433                         /* fallthrough */
434                 default:
435                         html_append_char(parser, *parser->bufp++);
436                 }
437         }
438
439         return parser->str->str;
440 }
441
442 static HTMLState html_read_line(HTMLParser *parser)
443 {
444         gchar buf[HTMLBUFSIZE];
445         gchar buf2[HTMLBUFSIZE];
446         gint index;
447
448         if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
449                 parser->state = HTML_EOF;
450                 return HTML_EOF;
451         }
452
453         if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
454                 index = parser->bufp - parser->buf->str;
455
456                 g_string_append(parser->buf, buf);
457
458                 parser->bufp = parser->buf->str + index;
459
460                 return HTML_CONV_FAILED;
461         }
462
463         index = parser->bufp - parser->buf->str;
464
465         g_string_append(parser->buf, buf2);
466
467         parser->bufp = parser->buf->str + index;
468
469         return HTML_NORMAL;
470 }
471
472 static void html_append_char(HTMLParser *parser, gchar ch)
473 {
474         GString *str = parser->str;
475
476         if (!parser->pre && parser->space) {
477                 g_string_append_c(str, ' ');
478                 parser->space = FALSE;
479         }
480
481         g_string_append_c(str, ch);
482
483         parser->empty_line = FALSE;
484         if (ch == '\n') {
485                 parser->newline = TRUE;
486                 if (str->len > 1 && str->str[str->len - 2] == '\n')
487                         parser->empty_line = TRUE;
488         } else
489                 parser->newline = FALSE;
490 }
491
492 static void html_append_str(HTMLParser *parser, const gchar *str, gint len)
493 {
494         GString *string = parser->str;
495
496         if (!parser->pre && parser->space) {
497                 g_string_append_c(string, ' ');
498                 parser->space = FALSE;
499         }
500
501         if (len == 0) return;
502         if (len < 0)
503                 g_string_append(string, str);
504         else {
505                 gchar *s;
506                 Xstrndup_a(s, str, len, return);
507                 g_string_append(string, s);
508         }
509
510         parser->empty_line = FALSE;
511         if (string->len > 0 && string->str[string->len - 1] == '\n') {
512                 parser->newline = TRUE;
513                 if (string->len > 1 && string->str[string->len - 2] == '\n')
514                         parser->empty_line = TRUE;
515         } else
516                 parser->newline = FALSE;
517 }
518
519 static HTMLState html_parse_tag(HTMLParser *parser)
520 {
521         gchar buf[HTMLBUFSIZE];
522         gchar *p;
523         static gboolean is_in_href = FALSE;
524
525         html_get_parenthesis(parser, buf, sizeof(buf));
526
527         for (p = buf; *p != '\0'; p++) {
528                 if (isspace(*p)) {
529                         *p = '\0';
530                         break;
531                 }
532         }
533
534         parser->state = HTML_UNKNOWN;
535         if (buf[0] == '\0') return parser->state;
536
537         g_strdown(buf);
538
539         if (!strcmp(buf, "br")) {
540                 parser->space = FALSE;
541                 html_append_char(parser, '\n');
542                 parser->state = HTML_BR;
543         } else if (!strcmp(buf, "a")) {
544                 /* look for tokens separated by space or = */
545                 char* href_token = strtok(++p, " =");
546                 parser->state = HTML_NORMAL;
547                 while (href_token != NULL) {
548                         /* look for href */
549                         if (!strcmp(href_token, "href")) {
550                                 /* the next token is the url, between double
551                                          * quotes */
552                                 char* url = strtok(NULL, "\"");
553                                         if (url && url[0] == '\'')
554                                           url = strtok(url,"\'");
555
556                                 if (!url) break;
557                                 html_append_str(parser, url, strlen(url));
558                                 html_append_char(parser, ' ');
559                                 /* start enforcing html link */
560                                 parser->state = HTML_HREF;
561                                 is_in_href = TRUE;
562                                 break;
563                         }
564                         /* or get next token */
565                         href_token = strtok(NULL, " =");
566                 }
567         } else if (!strcmp(buf, "/a")) {
568                 /* stop enforcing html link */
569                 parser->state = HTML_NORMAL;
570                 is_in_href = FALSE;
571         } else if (!strcmp(buf, "p")) {
572                 parser->space = FALSE;
573                 if (!parser->empty_line) {
574                         parser->space = FALSE;
575                         if (!parser->newline) html_append_char(parser, '\n');
576                         html_append_char(parser, '\n');
577                 }
578                 parser->state = HTML_PAR;
579         } else if (!strcmp(buf, "pre")) {
580                 parser->pre = TRUE;
581                 parser->state = HTML_PRE;
582         } else if (!strcmp(buf, "/pre")) {
583                 parser->pre = FALSE;
584                 parser->state = HTML_NORMAL;
585         } else if (!strcmp(buf, "hr")) {
586                 if (!parser->newline) {
587                         parser->space = FALSE;
588                         html_append_char(parser, '\n');
589                 }
590                 html_append_str(parser, HR_STR "\n", -1);
591                 parser->state = HTML_HR;
592         } else if (!strcmp(buf, "div")    ||
593                    !strcmp(buf, "ul")     ||
594                    !strcmp(buf, "li")     ||
595                    !strcmp(buf, "table")  ||
596                    !strcmp(buf, "tr")     ||
597                    (buf[0] == 'h' && isdigit(buf[1]))) {
598                 if (!parser->newline) {
599                         parser->space = FALSE;
600                         html_append_char(parser, '\n');
601                 }
602                 parser->state = HTML_NORMAL;
603         } else if (!strcmp(buf, "/table") ||
604                    (buf[0] == '/' && buf[1] == 'h' && isdigit(buf[1]))) {
605                 if (!parser->empty_line) {
606                         parser->space = FALSE;
607                         if (!parser->newline) html_append_char(parser, '\n');
608                         html_append_char(parser, '\n');
609                 }
610                 parser->state = HTML_NORMAL;
611         } else if (!strcmp(buf, "/div")   ||
612                    !strcmp(buf, "/ul")    ||
613                    !strcmp(buf, "/li")) {
614                 if (!parser->newline) {
615                         parser->space = FALSE;
616                         html_append_char(parser, '\n');
617                 }
618                 parser->state = HTML_NORMAL;
619         }
620         
621         if (is_in_href == TRUE) {
622                 /* when inside a link, everything will be written as
623                  * clickable (see textview_show_thml in textview.c) */
624                 parser->state = HTML_HREF;
625         }
626
627         return parser->state;
628 }
629
630 static void html_parse_special(HTMLParser *parser)
631 {
632         gchar symbol_name[9];
633         gint n;
634         const gchar *val;
635
636         parser->state = HTML_UNKNOWN;
637         g_return_if_fail(*parser->bufp == '&');
638
639         /* &foo; */
640         for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++)
641                 ;
642         if (n > 7 || parser->bufp[n] != ';') {
643                 /* output literal `&' */
644                 html_append_char(parser, *parser->bufp++);
645                 parser->state = HTML_NORMAL;
646                 return;
647         }
648         strncpy2(symbol_name, parser->bufp, n + 2);
649         parser->bufp += n + 1;
650
651         if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name))
652             != NULL) {
653                 html_append_str(parser, val, -1);
654                 parser->state = HTML_NORMAL;
655                 return;
656         } else if (symbol_name[1] == '#' && isdigit(symbol_name[2])) {
657                 gint ch;
658
659                 ch = atoi(symbol_name + 2);
660                 if ((ch > 0 && ch <= 127) ||
661                     (ch >= 128 && ch <= 255 &&
662                      parser->conv->charset == C_ISO_8859_1)) {
663                         html_append_char(parser, ch);
664                         parser->state = HTML_NORMAL;
665                         return;
666                 }
667         }
668
669         html_append_str(parser, symbol_name, -1);
670 }
671
672 static void html_get_parenthesis(HTMLParser *parser, gchar *buf, gint len)
673 {
674         gchar *p;
675
676         buf[0] = '\0';
677         g_return_if_fail(*parser->bufp == '<');
678
679         /* ignore comment / CSS / script stuff */
680         if (!strncmp(parser->bufp, "<!--", 4)) {
681                 parser->bufp += 4;
682                 while ((p = strstr(parser->bufp, "-->")) == NULL)
683                         if (html_read_line(parser) == HTML_EOF) return;
684                 parser->bufp = p + 3;
685                 return;
686         }
687         if (!g_strncasecmp(parser->bufp, "<style", 6)) {
688                 parser->bufp += 6;
689                 while ((p = strcasestr(parser->bufp, "</style>")) == NULL)
690                         if (html_read_line(parser) == HTML_EOF) return;
691                 parser->bufp = p + 8;
692                 return;
693         }
694         if (!g_strncasecmp(parser->bufp, "<script", 7)) {
695                 parser->bufp += 7;
696                 while ((p = strcasestr(parser->bufp, "</script>")) == NULL)
697                         if (html_read_line(parser) == HTML_EOF) return;
698                 parser->bufp = p + 9;
699                 return;
700         }
701
702         parser->bufp++;
703         while ((p = strchr(parser->bufp, '>')) == NULL)
704                 if (html_read_line(parser) == HTML_EOF) return;
705
706         strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
707         parser->bufp = p + 1;
708 }
709
710 /* these hash functions were taken from gstring.c in glib */
711 #if 0
712 static gint g_str_case_equal(gconstpointer v, gconstpointer v2)
713 {
714         return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
715 }
716
717 static guint g_str_case_hash(gconstpointer key)
718 {
719         const gchar *p = key;
720         guint h = *p;
721
722         if (h) {
723                 h = tolower(h);
724                 for (p += 1; *p != '\0'; p++)
725                         h = (h << 5) - h + tolower(*p);
726         }
727
728         return h;
729 }
730 #endif