commit 8e9f89bef30238e178d71344e5135b030933956e
parent 4d178a21c1d763945c6f27bc01814d58f7fa3dd7
Author: Ricardo Mones <ricardo@mones.org>
Date: Mon, 13 Nov 2017 21:11:33 +0100
Use entity decoding API in HTML parser
Diffstat:
| M | src/html.c | | | 319 | +++---------------------------------------------------------------------------- |
| M | src/html.h | | | 8 | ++------ |
2 files changed, 12 insertions(+), 315 deletions(-)
diff --git a/src/html.c b/src/html.c
@@ -24,288 +24,12 @@
#include "html.h"
#include "codeconv.h"
#include "utils.h"
+#include "entity.h"
#define SC_HTMLBUFSIZE 8192
#define HR_STR "────────────────────────────────────────────────"
#define LI_STR "• "
-typedef struct _SC_HTMLSymbol SC_HTMLSymbol;
-
-struct _SC_HTMLSymbol
-{
- gchar *const key;
- gchar *const val;
-};
-
-static SC_HTMLSymbol symbol_list[] = {
- {""", "\42"},
- {"&", "\46"},
- {"'", "\47"},
- {"<", "\74"},
- {">", "\76"},
- {""", "\42"},
- {"&", "\46"},
- {"'", "\47"},
- {"<", "\74"},
- {">", "\76"},
- {"’", "\47"},
- {"™", "\342\204\242"},
- {" ", "\40"},
- {"¡", "\302\241"},
- {"¢", "\302\242"},
- {"£", "\302\243"},
- {"¤", "\302\244"},
- {"¥", "\302\245"},
- {"¦", "\302\246"},
- {"§", "\302\247"},
- {"¨", "\302\250"},
- {"©", "\302\251"},
- {"ª", "\302\252"},
- {"«", "\302\253"},
- {"¬", "\302\254"},
- {"­", "\302\255"},
- {"®", "\302\256"},
- {"¯", "\302\257"},
- {"°", "\302\260"},
- {"±", "\302\261"},
- {"²", "\302\262"},
- {"³", "\302\263"},
- {"´", "\302\264"},
- {"µ", "\302\265"},
- {"¶", "\302\266"},
- {"·", "\302\267"},
- {"¸", "\302\270"},
- {"¹", "\302\271"},
- {"º", "\302\272"},
- {"»", "\302\273"},
- {"¼", "\302\274"},
- {"½", "\302\275"},
- {"¾", "\302\276"},
- {"¿", "\302\277"},
- {"À", "\303\200"},
- {"Á", "\303\201"},
- {"Â", "\303\202"},
- {"Ã", "\303\203"},
- {"Ä", "\303\204"},
- {"Å", "\303\205"},
- {"Æ", "\303\206"},
- {"Ç", "\303\207"},
- {"È", "\303\210"},
- {"É", "\303\211"},
- {"Ê", "\303\212"},
- {"Ë", "\303\213"},
- {"Ì", "\303\214"},
- {"Í", "\303\215"},
- {"Î", "\303\216"},
- {"Ï", "\303\217"},
- {"Ð", "\303\220"},
- {"Ñ", "\303\221"},
- {"Ò", "\303\222"},
- {"Ó", "\303\223"},
- {"Ô", "\303\224"},
- {"Õ", "\303\225"},
- {"Ö", "\303\226"},
- {"×", "\303\227"},
- {"Ø", "\303\230"},
- {"Ù", "\303\231"},
- {"Ú", "\303\232"},
- {"Û", "\303\233"},
- {"Ü", "\303\234"},
- {"Ý", "\303\235"},
- {"Þ", "\303\236"},
- {"ß", "\303\237"},
- {"à", "\303\240"},
- {"á", "\303\241"},
- {"â", "\303\242"},
- {"ã", "\303\243"},
- {"ä", "\303\244"},
- {"å", "\303\245"},
- {"æ", "\303\246"},
- {"ç", "\303\247"},
- {"è", "\303\250"},
- {"é", "\303\251"},
- {"ê", "\303\252"},
- {"ë", "\303\253"},
- {"ì", "\303\254"},
- {"í", "\303\255"},
- {"î", "\303\256"},
- {"ï", "\303\257"},
- {"ð", "\303\260"},
- {"ñ", "\303\261"},
- {"ò", "\303\262"},
- {"ó", "\303\263"},
- {"ô", "\303\264"},
- {"õ", "\303\265"},
- {"ö", "\303\266"},
- {"÷", "\303\267"},
- {"ø", "\303\270"},
- {"ù", "\303\271"},
- {"ú", "\303\272"},
- {"û", "\303\273"},
- {"ü", "\303\274"},
- {"ý", "\303\275"},
- {"þ", "\303\276"},
- {"ÿ", "\303\277"},
- {"Œ", "\305\222"},
- {"œ", "\305\223"},
- {"Š", "\305\240"},
- {"š", "\305\241"},
- {"Ÿ", "\305\270"},
- {"ˆ", "\313\206"},
- {"˜", "\313\234"},
- {" ", "\342\200\202"},
- {" ", "\342\200\203"},
- {" ", "\342\200\211"},
- {"–", "\342\200\223"},
- {"—", "\342\200\224"},
- {"‘", "\342\200\230"},
- {"’", "\342\200\231"},
- {"‚", "\342\200\232"},
- {"“", "\342\200\234"},
- {"”", "\342\200\235"},
- {"„", "\342\200\236"},
- {"†", "\342\200\240"},
- {"‡", "\342\200\241"},
- {"•", "\342\200\242"},
- {"…", "\342\200\246"},
- {"‰", "\342\200\260"},
- {"‹", "\342\200\271"},
- {"›", "\342\200\272"},
- {"€", "\342\202\254"},
- {"™", "\342\204\242"},
- {""", "\42"},
- {"&", "\46"},
- {"'", "\47"},
- {"<", "\74"},
- {">", "\76"},
- {"&squot;", "\47"},
- {" ", "\40"},
- {"¡", "\302\241"},
- {"¢", "\302\242"},
- {"£", "\302\243"},
- {"¤", "\302\244"},
- {"¥", "\302\245"},
- {"¦", "\302\246"},
- {"§", "\302\247"},
- {"¨", "\302\250"},
- {"©", "\302\251"},
- {"ª", "\302\252"},
- {"«", "\302\253"},
- {"¬", "\302\254"},
- {"­", "\302\255"},
- {"®", "\302\256"},
- {"¯", "\302\257"},
- {"°", "\302\260"},
- {"±", "\302\261"},
- {"²", "\302\262"},
- {"³", "\302\263"},
- {"´", "\302\264"},
- {"µ", "\302\265"},
- {"¶", "\302\266"},
- {"·", "\302\267"},
- {"¸", "\302\270"},
- {"¹", "\302\271"},
- {"º", "\302\272"},
- {"»", "\302\273"},
- {"¼", "\302\274"},
- {"½", "\302\275"},
- {"¾", "\302\276"},
- {"¿", "\302\277"},
- {"À", "\303\200"},
- {"Á", "\303\201"},
- {"Â", "\303\202"},
- {"Ã", "\303\203"},
- {"Ä", "\303\204"},
- {"Å", "\303\205"},
- {"Æ", "\303\206"},
- {"Ç", "\303\207"},
- {"È", "\303\210"},
- {"É", "\303\211"},
- {"Ê", "\303\212"},
- {"Ë", "\303\213"},
- {"Ì", "\303\214"},
- {"Í", "\303\215"},
- {"Î", "\303\216"},
- {"Ï", "\303\217"},
- {"Ð", "\303\220"},
- {"Ñ", "\303\221"},
- {"Ò", "\303\222"},
- {"Ó", "\303\223"},
- {"Ô", "\303\224"},
- {"Õ", "\303\225"},
- {"Ö", "\303\226"},
- {"×", "\303\227"},
- {"Ø", "\303\230"},
- {"Ù", "\303\231"},
- {"Ú", "\303\232"},
- {"Û", "\303\233"},
- {"Ü", "\303\234"},
- {"Ý", "\303\235"},
- {"Þ", "\303\236"},
- {"ß", "\303\237"},
- {"à", "\303\240"},
- {"á", "\303\241"},
- {"â", "\303\242"},
- {"ã", "\303\243"},
- {"ä", "\303\244"},
- {"å", "\303\245"},
- {"æ", "\303\246"},
- {"ç", "\303\247"},
- {"è", "\303\250"},
- {"é", "\303\251"},
- {"ê", "\303\252"},
- {"ë", "\303\253"},
- {"ì", "\303\254"},
- {"í", "\303\255"},
- {"î", "\303\256"},
- {"ï", "\303\257"},
- {"ð", "\303\260"},
- {"ñ", "\303\261"},
- {"ò", "\303\262"},
- {"ó", "\303\263"},
- {"ô", "\303\264"},
- {"õ", "\303\265"},
- {"ö", "\303\266"},
- {"÷", "\303\267"},
- {"ø", "\303\270"},
- {"ù", "\303\271"},
- {"ú", "\303\272"},
- {"û", "\303\273"},
- {"ü", "\303\274"},
- {"ý", "\303\275"},
- {"þ", "\303\276"},
- {"ÿ", "\303\277"},
- {"Œ", "\305\222"},
- {"œ", "\305\223"},
- {"Š", "\305\240"},
- {"š", "\305\241"},
- {"Ÿ", "\305\270"},
- {"ˆ", "\313\206"},
- {"˜", "\313\234"},
- {" ", "\342\200\202"},
- {" ", "\342\200\203"},
- {" ", "\342\200\211"},
- {"–", "\342\200\223"},
- {"—", "\342\200\224"},
- {"‘", "\342\200\230"},
- {"’", "\342\200\231"},
- {"‚", "\342\200\232"},
- {"“", "\342\200\234"},
- {"”", "\342\200\235"},
- {"„", "\342\200\236"},
- {"†", "\342\200\240"},
- {"‡", "\342\200\241"},
- {"•", "\342\200\242"},
- {"…", "\342\200\246"},
- {"‰", "\342\200\260"},
- {"‹", "\342\200\271"},
- {"›", "\342\200\272"},
- {"€", "\342\202\254"},
- {"™", "\342\204\242"}
-};
-
-static GHashTable *default_symbol_table;
-
static SC_HTMLState sc_html_read_line (SC_HTMLParser *parser);
static void sc_html_append_char (SC_HTMLParser *parser,
gchar ch);
@@ -340,16 +64,6 @@ SC_HTMLParser *sc_html_parser_new(FILE *fp, CodeConverter *conv)
parser->pre = FALSE;
parser->indent = 0;
- if (!default_symbol_table) {
- gint i;
- default_symbol_table = g_hash_table_new(g_str_hash, g_str_equal);
- for (i = 0; i < sizeof(symbol_list) / sizeof(symbol_list[0]); i++)
- g_hash_table_insert(default_symbol_table,
- symbol_list[i].key, symbol_list[i].val);
- }
-
- parser->symbol_table = default_symbol_table;
-
return parser;
}
@@ -612,8 +326,7 @@ static void decode_href(SC_HTMLParser *parser)
tparser->str = g_string_new(NULL);
tparser->buf = g_string_new(parser->href);
tparser->bufp = tparser->buf->str;
- tparser->symbol_table = default_symbol_table;
-
+
tmp = sc_html_parse(tparser);
g_free(parser->href);
@@ -725,33 +438,21 @@ static SC_HTMLState sc_html_parse_tag(SC_HTMLParser *parser)
static void sc_html_parse_special(SC_HTMLParser *parser)
{
- gchar symbol_name[9];
- gint n;
- const gchar *val;
+ gchar *entity;
parser->state = SC_HTML_UNKNOWN;
cm_return_if_fail(*parser->bufp == '&');
- /* &foo; */
- for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++)
- ;
- if (n > 7 || parser->bufp[n] != ';') {
+ entity = entity_decode(parser->bufp);
+ if (entity != NULL) {
+ sc_html_append_str(parser, entity, -1);
+ g_free(entity);
+ while (*parser->bufp++ != ';');
+ } else {
/* output literal `&' */
sc_html_append_char(parser, *parser->bufp++);
- parser->state = SC_HTML_NORMAL;
- return;
}
- strncpy2(symbol_name, parser->bufp, n + 2);
- parser->bufp += n + 1;
-
- if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name))
- != NULL) {
- sc_html_append_str(parser, val, -1);
- parser->state = SC_HTML_NORMAL;
- return;
- }
-
- sc_html_append_str(parser, symbol_name, -1);
+ parser->state = SC_HTML_NORMAL;
}
static gchar *sc_html_find_tag(SC_HTMLParser *parser, const gchar *tag)
diff --git a/src/html.h b/src/html.h
@@ -1,6 +1,6 @@
/*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2017 Hiroyuki Yamamoto and the Claws Mail team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
*/
#ifndef __HTML_H__
@@ -51,9 +50,6 @@ struct _SC_HTMLParser
FILE *fp;
CodeConverter *conv;
- GHashTable *symbol_table;
- GHashTable *alt_symbol_table;
-
GString *str;
GString *buf;