2 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4 * Authors: Michael Zucchi <notzed@ximian.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
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 GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 * DO NOT USE THIS CODE OUTSIDE OF CAMEL
25 * IT IS SUBJECT TO CHANGE OR MAY VANISH AT ANY TIME
32 #include "camel-html-parser.h"
34 /* if defined, must also compile in dump_tag() below somewhere */
37 /* Parser definitions, see below object code for details */
39 struct _CamelHTMLParserPrivate {
44 enum _camel_html_parser_t state;
56 static void tokenize_setup (void);
57 static CamelHTMLParserPrivate *tokenize_init (void);
58 static void tokenize_free (CamelHTMLParserPrivate *p);
59 static gint tokenize_step (CamelHTMLParserPrivate *p, gchar **datap, gint *lenp);
61 G_DEFINE_TYPE (CamelHTMLParser, camel_html_parser, CAMEL_TYPE_OBJECT)
63 /* ********************************************************************** */
66 html_parser_finalize (GObject *object)
68 CamelHTMLParser *parser = CAMEL_HTML_PARSER (object);
70 tokenize_free (parser->priv);
72 /* Chain up to parent's finalize() method. */
73 G_OBJECT_CLASS (camel_html_parser_parent_class)->finalize (object);
77 camel_html_parser_class_init (CamelHTMLParserClass *class)
79 GObjectClass *object_class;
81 object_class = G_OBJECT_CLASS (class);
82 object_class->finalize = html_parser_finalize;
88 camel_html_parser_init (CamelHTMLParser *parser)
90 parser->priv = tokenize_init ();
94 * camel_html_parser_new:
96 * Create a new CamelHTMLParser object.
98 * Returns: A new CamelHTMLParser widget.
101 camel_html_parser_new (void)
103 return g_object_new (CAMEL_TYPE_HTML_PARSER, NULL);
106 void camel_html_parser_set_data (CamelHTMLParser *hp, const gchar *start, gint len, gint last)
108 CamelHTMLParserPrivate *p = hp->priv;
110 p->inptr = p->inbuf = (gchar *) start;
111 p->inend = (gchar *) start + len;
115 camel_html_parser_t camel_html_parser_step (CamelHTMLParser *hp, const gchar **datap, gint *lenp)
117 return tokenize_step (hp->priv, (gchar **) datap, lenp);
120 const gchar *camel_html_parser_left (CamelHTMLParser *hp, gint *lenp)
122 CamelHTMLParserPrivate *p = hp->priv;
125 *lenp = p->inend - p->inptr;
130 const gchar *camel_html_parser_tag (CamelHTMLParser *hp)
132 return hp->priv->tag->str;
135 const gchar *camel_html_parser_attr (CamelHTMLParser *hp, const gchar *name)
138 CamelHTMLParserPrivate *p = hp->priv;
140 for (i = 0; i < p->attrs->len; i++) {
141 if (!g_ascii_strcasecmp (((GString *) p->attrs->pdata[i])->str, name)) {
142 return ((GString *) p->values->pdata[i])->str;
149 const GPtrArray *camel_html_parser_attr_list (CamelHTMLParser *hp, const GPtrArray **values)
152 *values = hp->priv->values;
154 return hp->priv->attrs;
157 /* this map taken out of libxml */
163 * the 4 absolute ones,
165 { 34, "quot", /* quotation mark = APL quote, U+0022 ISOnum */ },
166 { 38, "amp", /* ampersand, U+0026 ISOnum */ },
167 { 60, "lt", /* less-than sign, U+003C ISOnum */ },
168 { 62, "gt", /* greater-than sign, U+003E ISOnum */ },
171 * A bunch still in the 128-255 range
172 * Replacing them depend really on the charset used.
174 { 39, "apos", /* single quote */ },
175 { 160, "nbsp", /* no-break space = non-breaking space, U+00A0 ISOnum */ },
176 { 161, "iexcl",/* inverted exclamation mark, U+00A1 ISOnum */ },
177 { 162, "cent", /* cent sign, U+00A2 ISOnum */ },
178 { 163, "pound",/* pound sign, U+00A3 ISOnum */ },
179 { 164, "curren",/* currency sign, U+00A4 ISOnum */ },
180 { 165, "yen", /* yen sign = yuan sign, U+00A5 ISOnum */ },
181 { 166, "brvbar",/* broken bar = broken vertical bar, U+00A6 ISOnum */ },
182 { 167, "sect", /* section sign, U+00A7 ISOnum */ },
183 { 168, "uml", /* diaeresis = spacing diaeresis, U+00A8 ISOdia */ },
184 { 169, "copy", /* copyright sign, U+00A9 ISOnum */ },
185 { 170, "ordf", /* feminine ordinal indicator, U+00AA ISOnum */ },
186 { 171, "laquo",/* left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum */ },
187 { 172, "not", /* not sign, U+00AC ISOnum */ },
188 { 173, "shy", /* soft hyphen = discretionary hyphen, U+00AD ISOnum */ },
189 { 174, "reg", /* registered sign = registered trade mark sign, U+00AE ISOnum */ },
190 { 175, "macr", /* macron = spacing macron = overline = APL overbar, U+00AF ISOdia */ },
191 { 176, "deg", /* degree sign, U+00B0 ISOnum */ },
192 { 177, "plusmn",/* plus-minus sign = plus-or-minus sign, U+00B1 ISOnum */ },
193 { 178, "sup2", /* superscript two = superscript digit two = squared, U+00B2 ISOnum */ },
194 { 179, "sup3", /* superscript three = superscript digit three = cubed, U+00B3 ISOnum */ },
195 { 180, "acute",/* acute accent = spacing acute, U+00B4 ISOdia */ },
196 { 181, "micro",/* micro sign, U+00B5 ISOnum */ },
197 { 182, "para", /* pilcrow sign = paragraph sign, U+00B6 ISOnum */ },
198 { 183, "middot",/* middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum */ },
199 { 184, "cedil",/* cedilla = spacing cedilla, U+00B8 ISOdia */ },
200 { 185, "sup1", /* superscript one = superscript digit one, U+00B9 ISOnum */ },
201 { 186, "ordm", /* masculine ordinal indicator, U+00BA ISOnum */ },
202 { 187, "raquo",/* right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum */ },
203 { 188, "frac14",/* vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum */ },
204 { 189, "frac12",/* vulgar fraction one half = fraction one half, U+00BD ISOnum */ },
205 { 190, "frac34",/* vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum */ },
206 { 191, "iquest",/* inverted question mark = turned question mark, U+00BF ISOnum */ },
207 { 192, "Agrave",/* latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1 */ },
208 { 193, "Aacute",/* latin capital letter A with acute, U+00C1 ISOlat1 */ },
209 { 194, "Acirc",/* latin capital letter A with circumflex, U+00C2 ISOlat1 */ },
210 { 195, "Atilde",/* latin capital letter A with tilde, U+00C3 ISOlat1 */ },
211 { 196, "Auml", /* latin capital letter A with diaeresis, U+00C4 ISOlat1 */ },
212 { 197, "Aring",/* latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1 */ },
213 { 198, "AElig",/* latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1 */ },
214 { 199, "Ccedil",/* latin capital letter C with cedilla, U+00C7 ISOlat1 */ },
215 { 200, "Egrave",/* latin capital letter E with grave, U+00C8 ISOlat1 */ },
216 { 201, "Eacute",/* latin capital letter E with acute, U+00C9 ISOlat1 */ },
217 { 202, "Ecirc",/* latin capital letter E with circumflex, U+00CA ISOlat1 */ },
218 { 203, "Euml", /* latin capital letter E with diaeresis, U+00CB ISOlat1 */ },
219 { 204, "Igrave",/* latin capital letter I with grave, U+00CC ISOlat1 */ },
220 { 205, "Iacute",/* latin capital letter I with acute, U+00CD ISOlat1 */ },
221 { 206, "Icirc",/* latin capital letter I with circumflex, U+00CE ISOlat1 */ },
222 { 207, "Iuml", /* latin capital letter I with diaeresis, U+00CF ISOlat1 */ },
223 { 208, "ETH", /* latin capital letter ETH, U+00D0 ISOlat1 */ },
224 { 209, "Ntilde",/* latin capital letter N with tilde, U+00D1 ISOlat1 */ },
225 { 210, "Ograve",/* latin capital letter O with grave, U+00D2 ISOlat1 */ },
226 { 211, "Oacute",/* latin capital letter O with acute, U+00D3 ISOlat1 */ },
227 { 212, "Ocirc",/* latin capital letter O with circumflex, U+00D4 ISOlat1 */ },
228 { 213, "Otilde",/* latin capital letter O with tilde, U+00D5 ISOlat1 */ },
229 { 214, "Ouml", /* latin capital letter O with diaeresis, U+00D6 ISOlat1 */ },
230 { 215, "times",/* multiplication sign, U+00D7 ISOnum */ },
231 { 216, "Oslash",/* latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1 */ },
232 { 217, "Ugrave",/* latin capital letter U with grave, U+00D9 ISOlat1 */ },
233 { 218, "Uacute",/* latin capital letter U with acute, U+00DA ISOlat1 */ },
234 { 219, "Ucirc",/* latin capital letter U with circumflex, U+00DB ISOlat1 */ },
235 { 220, "Uuml", /* latin capital letter U with diaeresis, U+00DC ISOlat1 */ },
236 { 221, "Yacute",/* latin capital letter Y with acute, U+00DD ISOlat1 */ },
237 { 222, "THORN",/* latin capital letter THORN, U+00DE ISOlat1 */ },
238 { 223, "szlig",/* latin small letter sharp s = ess-zed, U+00DF ISOlat1 */ },
239 { 224, "agrave",/* latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1 */ },
240 { 225, "aacute",/* latin small letter a with acute, U+00E1 ISOlat1 */ },
241 { 226, "acirc",/* latin small letter a with circumflex, U+00E2 ISOlat1 */ },
242 { 227, "atilde",/* latin small letter a with tilde, U+00E3 ISOlat1 */ },
243 { 228, "auml", /* latin small letter a with diaeresis, U+00E4 ISOlat1 */ },
244 { 229, "aring",/* latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1 */ },
245 { 230, "aelig",/* latin small letter ae = latin small ligature ae, U+00E6 ISOlat1 */ },
246 { 231, "ccedil",/* latin small letter c with cedilla, U+00E7 ISOlat1 */ },
247 { 232, "egrave",/* latin small letter e with grave, U+00E8 ISOlat1 */ },
248 { 233, "eacute",/* latin small letter e with acute, U+00E9 ISOlat1 */ },
249 { 234, "ecirc",/* latin small letter e with circumflex, U+00EA ISOlat1 */ },
250 { 235, "euml", /* latin small letter e with diaeresis, U+00EB ISOlat1 */ },
251 { 236, "igrave",/* latin small letter i with grave, U+00EC ISOlat1 */ },
252 { 237, "iacute",/* latin small letter i with acute, U+00ED ISOlat1 */ },
253 { 238, "icirc",/* latin small letter i with circumflex, U+00EE ISOlat1 */ },
254 { 239, "iuml", /* latin small letter i with diaeresis, U+00EF ISOlat1 */ },
255 { 240, "eth", /* latin small letter eth, U+00F0 ISOlat1 */ },
256 { 241, "ntilde",/* latin small letter n with tilde, U+00F1 ISOlat1 */ },
257 { 242, "ograve",/* latin small letter o with grave, U+00F2 ISOlat1 */ },
258 { 243, "oacute",/* latin small letter o with acute, U+00F3 ISOlat1 */ },
259 { 244, "ocirc",/* latin small letter o with circumflex, U+00F4 ISOlat1 */ },
260 { 245, "otilde",/* latin small letter o with tilde, U+00F5 ISOlat1 */ },
261 { 246, "ouml", /* latin small letter o with diaeresis, U+00F6 ISOlat1 */ },
262 { 247, "divide",/* division sign, U+00F7 ISOnum */ },
263 { 248, "oslash",/* latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1 */ },
264 { 249, "ugrave",/* latin small letter u with grave, U+00F9 ISOlat1 */ },
265 { 250, "uacute",/* latin small letter u with acute, U+00FA ISOlat1 */ },
266 { 251, "ucirc",/* latin small letter u with circumflex, U+00FB ISOlat1 */ },
267 { 252, "uuml", /* latin small letter u with diaeresis, U+00FC ISOlat1 */ },
268 { 253, "yacute",/* latin small letter y with acute, U+00FD ISOlat1 */ },
269 { 254, "thorn",/* latin small letter thorn with, U+00FE ISOlat1 */ },
270 { 255, "yuml", /* latin small letter y with diaeresis, U+00FF ISOlat1 */ },
273 * Anything below should really be kept as entities references
275 { 402, "fnof", /* latin small f with hook = function = florin, U+0192 ISOtech */ },
277 { 913, "Alpha",/* greek capital letter alpha, U+0391 */ },
278 { 914, "Beta", /* greek capital letter beta, U+0392 */ },
279 { 915, "Gamma",/* greek capital letter gamma, U+0393 ISOgrk3 */ },
280 { 916, "Delta",/* greek capital letter delta, U+0394 ISOgrk3 */ },
281 { 917, "Epsilon",/* greek capital letter epsilon, U+0395 */ },
282 { 918, "Zeta", /* greek capital letter zeta, U+0396 */ },
283 { 919, "Eta", /* greek capital letter eta, U+0397 */ },
284 { 920, "Theta",/* greek capital letter theta, U+0398 ISOgrk3 */ },
285 { 921, "Iota", /* greek capital letter iota, U+0399 */ },
286 { 922, "Kappa",/* greek capital letter kappa, U+039A */ },
287 { 923, "Lambda"/* greek capital letter lambda, U+039B ISOgrk3 */ },
288 { 924, "Mu", /* greek capital letter mu, U+039C */ },
289 { 925, "Nu", /* greek capital letter nu, U+039D */ },
290 { 926, "Xi", /* greek capital letter xi, U+039E ISOgrk3 */ },
291 { 927, "Omicron",/* greek capital letter omicron, U+039F */ },
292 { 928, "Pi", /* greek capital letter pi, U+03A0 ISOgrk3 */ },
293 { 929, "Rho", /* greek capital letter rho, U+03A1 */ },
294 { 931, "Sigma",/* greek capital letter sigma, U+03A3 ISOgrk3 */ },
295 { 932, "Tau", /* greek capital letter tau, U+03A4 */ },
296 { 933, "Upsilon",/* greek capital letter upsilon, U+03A5 ISOgrk3 */ },
297 { 934, "Phi", /* greek capital letter phi, U+03A6 ISOgrk3 */ },
298 { 935, "Chi", /* greek capital letter chi, U+03A7 */ },
299 { 936, "Psi", /* greek capital letter psi, U+03A8 ISOgrk3 */ },
300 { 937, "Omega",/* greek capital letter omega, U+03A9 ISOgrk3 */ },
302 { 945, "alpha",/* greek small letter alpha, U+03B1 ISOgrk3 */ },
303 { 946, "beta", /* greek small letter beta, U+03B2 ISOgrk3 */ },
304 { 947, "gamma",/* greek small letter gamma, U+03B3 ISOgrk3 */ },
305 { 948, "delta",/* greek small letter delta, U+03B4 ISOgrk3 */ },
306 { 949, "epsilon",/* greek small letter epsilon, U+03B5 ISOgrk3 */ },
307 { 950, "zeta", /* greek small letter zeta, U+03B6 ISOgrk3 */ },
308 { 951, "eta", /* greek small letter eta, U+03B7 ISOgrk3 */ },
309 { 952, "theta",/* greek small letter theta, U+03B8 ISOgrk3 */ },
310 { 953, "iota", /* greek small letter iota, U+03B9 ISOgrk3 */ },
311 { 954, "kappa",/* greek small letter kappa, U+03BA ISOgrk3 */ },
312 { 955, "lambda",/* greek small letter lambda, U+03BB ISOgrk3 */ },
313 { 956, "mu", /* greek small letter mu, U+03BC ISOgrk3 */ },
314 { 957, "nu", /* greek small letter nu, U+03BD ISOgrk3 */ },
315 { 958, "xi", /* greek small letter xi, U+03BE ISOgrk3 */ },
316 { 959, "omicron",/* greek small letter omicron, U+03BF NEW */ },
317 { 960, "pi", /* greek small letter pi, U+03C0 ISOgrk3 */ },
318 { 961, "rho", /* greek small letter rho, U+03C1 ISOgrk3 */ },
319 { 962, "sigmaf",/* greek small letter final sigma, U+03C2 ISOgrk3 */ },
320 { 963, "sigma",/* greek small letter sigma, U+03C3 ISOgrk3 */ },
321 { 964, "tau", /* greek small letter tau, U+03C4 ISOgrk3 */ },
322 { 965, "upsilon",/* greek small letter upsilon, U+03C5 ISOgrk3 */ },
323 { 966, "phi", /* greek small letter phi, U+03C6 ISOgrk3 */ },
324 { 967, "chi", /* greek small letter chi, U+03C7 ISOgrk3 */ },
325 { 968, "psi", /* greek small letter psi, U+03C8 ISOgrk3 */ },
326 { 969, "omega",/* greek small letter omega, U+03C9 ISOgrk3 */ },
327 { 977, "thetasym",/* greek small letter theta symbol, U+03D1 NEW */ },
328 { 978, "upsih",/* greek upsilon with hook symbol, U+03D2 NEW */ },
329 { 982, "piv", /* greek pi symbol, U+03D6 ISOgrk3 */ },
331 { 8226, "bull", /* bullet = black small circle, U+2022 ISOpub */ },
332 { 8230, "hellip",/* horizontal ellipsis = three dot leader, U+2026 ISOpub */ },
333 { 8242, "prime",/* prime = minutes = feet, U+2032 ISOtech */ },
334 { 8243, "Prime",/* double prime = seconds = inches, U+2033 ISOtech */ },
335 { 8254, "oline",/* overline = spacing overscore, U+203E NEW */ },
336 { 8260, "frasl",/* fraction slash, U+2044 NEW */ },
338 { 8472, "weierp",/* script capital P = power set = Weierstrass p, U+2118 ISOamso */ },
339 { 8465, "image",/* blackletter capital I = imaginary part, U+2111 ISOamso */ },
340 { 8476, "real", /* blackletter capital R = real part symbol, U+211C ISOamso */ },
341 { 8482, "trade",/* trade mark sign, U+2122 ISOnum */ },
342 { 8501, "alefsym",/* alef symbol = first transfinite cardinal, U+2135 NEW */ },
343 { 8592, "larr", /* leftwards arrow, U+2190 ISOnum */ },
344 { 8593, "uarr", /* upwards arrow, U+2191 ISOnum */ },
345 { 8594, "rarr", /* rightwards arrow, U+2192 ISOnum */ },
346 { 8595, "darr", /* downwards arrow, U+2193 ISOnum */ },
347 { 8596, "harr", /* left right arrow, U+2194 ISOamsa */ },
348 { 8629, "crarr",/* downwards arrow with corner leftwards = carriage return, U+21B5 NEW */ },
349 { 8656, "lArr", /* leftwards double arrow, U+21D0 ISOtech */ },
350 { 8657, "uArr", /* upwards double arrow, U+21D1 ISOamsa */ },
351 { 8658, "rArr", /* rightwards double arrow, U+21D2 ISOtech */ },
352 { 8659, "dArr", /* downwards double arrow, U+21D3 ISOamsa */ },
353 { 8660, "hArr", /* left right double arrow, U+21D4 ISOamsa */ },
355 { 8704, "forall",/* for all, U+2200 ISOtech */ },
356 { 8706, "part", /* partial differential, U+2202 ISOtech */ },
357 { 8707, "exist",/* there exists, U+2203 ISOtech */ },
358 { 8709, "empty",/* empty set = null set = diameter, U+2205 ISOamso */ },
359 { 8711, "nabla",/* nabla = backward difference, U+2207 ISOtech */ },
360 { 8712, "isin", /* element of, U+2208 ISOtech */ },
361 { 8713, "notin",/* not an element of, U+2209 ISOtech */ },
362 { 8715, "ni", /* contains as member, U+220B ISOtech */ },
363 { 8719, "prod", /* n-ary product = product sign, U+220F ISOamsb */ },
364 { 8721, "sum", /* n-ary sumation, U+2211 ISOamsb */ },
365 { 8722, "minus",/* minus sign, U+2212 ISOtech */ },
366 { 8727, "lowast",/* asterisk operator, U+2217 ISOtech */ },
367 { 8730, "radic",/* square root = radical sign, U+221A ISOtech */ },
368 { 8733, "prop", /* proportional to, U+221D ISOtech */ },
369 { 8734, "infin",/* infinity, U+221E ISOtech */ },
370 { 8736, "ang", /* angle, U+2220 ISOamso */ },
371 { 8743, "and", /* logical and = wedge, U+2227 ISOtech */ },
372 { 8744, "or", /* logical or = vee, U+2228 ISOtech */ },
373 { 8745, "cap", /* intersection = cap, U+2229 ISOtech */ },
374 { 8746, "cup", /* union = cup, U+222A ISOtech */ },
375 { 8747, "int", /* integral, U+222B ISOtech */ },
376 { 8756, "there4",/* therefore, U+2234 ISOtech */ },
377 { 8764, "sim", /* tilde operator = varies with = similar to, U+223C ISOtech */ },
378 { 8773, "cong", /* approximately equal to, U+2245 ISOtech */ },
379 { 8776, "asymp",/* almost equal to = asymptotic to, U+2248 ISOamsr */ },
380 { 8800, "ne", /* not equal to, U+2260 ISOtech */ },
381 { 8801, "equiv",/* identical to, U+2261 ISOtech */ },
382 { 8804, "le", /* less-than or equal to, U+2264 ISOtech */ },
383 { 8805, "ge", /* greater-than or equal to, U+2265 ISOtech */ },
384 { 8834, "sub", /* subset of, U+2282 ISOtech */ },
385 { 8835, "sup", /* superset of, U+2283 ISOtech */ },
386 { 8836, "nsub", /* not a subset of, U+2284 ISOamsn */ },
387 { 8838, "sube", /* subset of or equal to, U+2286 ISOtech */ },
388 { 8839, "supe", /* superset of or equal to, U+2287 ISOtech */ },
389 { 8853, "oplus",/* circled plus = direct sum, U+2295 ISOamsb */ },
390 { 8855, "otimes",/* circled times = vector product, U+2297 ISOamsb */ },
391 { 8869, "perp", /* up tack = orthogonal to = perpendicular, U+22A5 ISOtech */ },
392 { 8901, "sdot", /* dot operator, U+22C5 ISOamsb */ },
393 { 8968, "lceil",/* left ceiling = apl upstile, U+2308 ISOamsc */ },
394 { 8969, "rceil",/* right ceiling, U+2309 ISOamsc */ },
395 { 8970, "lfloor",/* left floor = apl downstile, U+230A ISOamsc */ },
396 { 8971, "rfloor",/* right floor, U+230B ISOamsc */ },
397 { 9001, "lang", /* left-pointing angle bracket = bra, U+2329 ISOtech */ },
398 { 9002, "rang", /* right-pointing angle bracket = ket, U+232A ISOtech */ },
399 { 9674, "loz", /* lozenge, U+25CA ISOpub */ },
401 { 9824, "spades",/* black spade suit, U+2660 ISOpub */ },
402 { 9827, "clubs",/* black club suit = shamrock, U+2663 ISOpub */ },
403 { 9829, "hearts",/* black heart suit = valentine, U+2665 ISOpub */ },
404 { 9830, "diams",/* black diamond suit, U+2666 ISOpub */ },
406 { 338, "OElig",/* latin capital ligature OE, U+0152 ISOlat2 */ },
407 { 339, "oelig",/* latin small ligature oe, U+0153 ISOlat2 */ },
408 { 352, "Scaron",/* latin capital letter S with caron, U+0160 ISOlat2 */ },
409 { 353, "scaron",/* latin small letter s with caron, U+0161 ISOlat2 */ },
410 { 376, "Yuml", /* latin capital letter Y with diaeresis, U+0178 ISOlat2 */ },
411 { 710, "circ", /* modifier letter circumflex accent, U+02C6 ISOpub */ },
412 { 732, "tilde",/* small tilde, U+02DC ISOdia */ },
414 { 8194, "ensp", /* en space, U+2002 ISOpub */ },
415 { 8195, "emsp", /* em space, U+2003 ISOpub */ },
416 { 8201, "thinsp",/* thin space, U+2009 ISOpub */ },
417 { 8204, "zwnj", /* zero width non-joiner, U+200C NEW RFC 2070 */ },
418 { 8205, "zwj", /* zero width joiner, U+200D NEW RFC 2070 */ },
419 { 8206, "lrm", /* left-to-right mark, U+200E NEW RFC 2070 */ },
420 { 8207, "rlm", /* right-to-left mark, U+200F NEW RFC 2070 */ },
421 { 8211, "ndash",/* en dash, U+2013 ISOpub */ },
422 { 8212, "mdash",/* em dash, U+2014 ISOpub */ },
423 { 8216, "lsquo",/* left single quotation mark, U+2018 ISOnum */ },
424 { 8217, "rsquo",/* right single quotation mark, U+2019 ISOnum */ },
425 { 8218, "sbquo",/* single low-9 quotation mark, U+201A NEW */ },
426 { 8220, "ldquo",/* left double quotation mark, U+201C ISOnum */ },
427 { 8221, "rdquo",/* right double quotation mark, U+201D ISOnum */ },
428 { 8222, "bdquo",/* double low-9 quotation mark, U+201E NEW */ },
429 { 8224, "dagger",/* dagger, U+2020 ISOpub */ },
430 { 8225, "Dagger",/* double dagger, U+2021 ISOpub */ },
431 { 8240, "permil",/* per mille sign, U+2030 ISOtech */ },
432 { 8249, "lsaquo",/* single left-pointing angle quotation mark, U+2039 ISO proposed */ },
433 { 8250, "rsaquo",/* single right-pointing angle quotation mark, U+203A ISO proposed */ },
434 { 8364, "euro", /* euro sign, U+20AC NEW */ }
437 static GHashTable *entities;
439 /* this cannot be called in a thread context */
440 static void tokenize_setup (void)
444 if (entities == NULL) {
445 entities = g_hash_table_new (g_str_hash, g_str_equal);
446 for (i = 0; i < G_N_ELEMENTS (entity_map); i++) {
447 g_hash_table_insert (entities, (gchar *) entity_map[i].name, GUINT_TO_POINTER (entity_map[i].val));
452 static CamelHTMLParserPrivate *tokenize_init (void)
454 CamelHTMLParserPrivate *p;
456 p = g_malloc (sizeof (*p));
457 p->state = CAMEL_HTML_PARSER_DATA;
460 p->attrs = g_ptr_array_new ();
461 p->values = g_ptr_array_new ();
462 p->tag = g_string_new ("");
463 p->ent = g_string_new ("");
466 if (entities == NULL)
472 static void tokenize_free (CamelHTMLParserPrivate *p)
476 g_string_free (p->tag, TRUE);
477 g_string_free (p->ent, TRUE);
480 for (i = 0; i < p->attrs->len; i++)
481 g_string_free (p->attrs->pdata[i], TRUE);
483 for (i = 0; i < p->values->len; i++)
484 g_string_free (p->values->pdata[i], TRUE);
489 static gint convert_entity (const gchar *e, gchar *ent)
494 return g_unichar_to_utf8 (atoi (e + 1), ent);
496 val = GPOINTER_TO_UINT (g_hash_table_lookup (entities, e));
498 return g_unichar_to_utf8 (val, ent);
504 static void dump_tag (CamelHTMLParserPrivate *p)
508 printf ("got tag: %s\n", p->tag->str);
509 printf ("%d attributes:\n", p->attr);
510 for (i = 0; i < p->attr; i++) {
511 printf (" %s = '%s'\n", ((GString *) p->attrs->pdata[i])->str, ((GString *) p->values->pdata[i])->str);
516 static gint tokenize_step (CamelHTMLParserPrivate *p, gchar **datap, gint *lenp)
518 gchar *in = p->inptr;
519 gchar *inend = p->inend;
521 gint state = p->state, ret, len;
522 gchar *start = p->inptr;
524 d (printf ("Tokenise step\n"));
529 case CAMEL_HTML_PARSER_DATA:
532 state = CAMEL_HTML_PARSER_TAG;
534 g_string_truncate (p->tag, 0);
535 d (printf ("got data '%.*s'\n", in - start - 1, start));
539 } else if (c == '&') {
541 state = CAMEL_HTML_PARSER_ENT;
542 g_string_truncate (p->ent, 0);
543 g_string_append_c (p->ent, c);
544 d (printf ("got data '%.*s'\n", in - start - 1, start));
550 case CAMEL_HTML_PARSER_ENT:
552 len = convert_entity (p->ent->str + 1, p->ent_utf8);
554 /* handle broken entity */
555 g_string_append_c (p->ent, c);
556 ret = state = CAMEL_HTML_PARSER_DATA;
557 *datap = p->ent->str;
561 d (printf ("got entity: %s = %s\n", p->ent->str, p->ent_utf8));
563 state = CAMEL_HTML_PARSER_DATA;
564 *datap = p->ent_utf8;
568 } else if (isalnum (c) || c=='#') { /* FIXME: right type */
569 g_string_append_c (p->ent, c);
571 /* handle broken entity */
572 g_string_append_c (p->ent, c);
573 ret = state = CAMEL_HTML_PARSER_DATA;
574 *datap = p->ent->str;
579 case CAMEL_HTML_PARSER_TAG:
581 state = CAMEL_HTML_PARSER_COMMENT0;
582 g_string_append_c (p->tag, c);
583 } else if (c == '>') {
585 ret = CAMEL_HTML_PARSER_ELEMENT;
586 state = CAMEL_HTML_PARSER_DATA;
588 } else if (c == ' ' || c == '\n' || c == '\t') {
589 state = CAMEL_HTML_PARSER_ATTR0;
591 g_string_append_c (p->tag, c);
595 case CAMEL_HTML_PARSER_COMMENT0:
597 g_string_append_c (p->tag, c);
598 if (p->tag->len == 3) {
599 g_string_truncate (p->tag, 0);
600 state = CAMEL_HTML_PARSER_COMMENT;
603 /* got something else, probbly dtd entity */
604 state = CAMEL_HTML_PARSER_DTDENT;
607 case CAMEL_HTML_PARSER_DTDENT:
609 ret = CAMEL_HTML_PARSER_DTDENT;
610 state = CAMEL_HTML_PARSER_DATA;
616 case CAMEL_HTML_PARSER_COMMENT:
617 if (c == '>' && p->tag->len == 2) {
618 ret = CAMEL_HTML_PARSER_COMMENT;
619 state = CAMEL_HTML_PARSER_DATA;
623 } else if (c == '-') {
624 /* we dont care if we get 'n' --'s before the > */
626 g_string_append_c (p->tag, c);
628 g_string_truncate (p->tag, 0);
631 case CAMEL_HTML_PARSER_ATTR0: /* pre-attribute whitespace */
634 ret = CAMEL_HTML_PARSER_ELEMENT;
635 state = CAMEL_HTML_PARSER_DATA;
637 } else if (c == ' ' || c == '\n' || c == '\t') {
639 if (p->attrs->len <= p->attr) {
640 g_ptr_array_add (p->attrs, g_string_new (""));
641 g_ptr_array_add (p->values, g_string_new (""));
643 g_string_truncate (p->attrs->pdata[p->attr], 0);
644 g_string_truncate (p->values->pdata[p->attr], 0);
646 g_string_append_c (p->attrs->pdata[p->attr], c);
647 state = CAMEL_HTML_PARSER_ATTR;
650 case CAMEL_HTML_PARSER_ATTR:
653 ret = CAMEL_HTML_PARSER_ELEMENT;
654 state = CAMEL_HTML_PARSER_DATA;
656 } else if (c == '=') {
657 state = CAMEL_HTML_PARSER_VAL0;
658 } else if (c == ' ' || c == '\n' || c == '\t') {
659 state = CAMEL_HTML_PARSER_ATTR0;
662 g_string_append_c (p->attrs->pdata[p->attr], c);
665 case CAMEL_HTML_PARSER_VAL0:
667 d (printf ("value truncated\n"));
669 ret = CAMEL_HTML_PARSER_ELEMENT;
670 state = CAMEL_HTML_PARSER_DATA;
672 } else if (c == '\'' || c == '\"') {
674 state = CAMEL_HTML_PARSER_VAL;
675 } else if (c == ' ' || c == '\n' || c == '\t') {
677 g_string_append_c (p->values->pdata[p->attr], c);
679 state = CAMEL_HTML_PARSER_VAL;
682 case CAMEL_HTML_PARSER_VAL:
686 d (printf ("value truncated\n"));
688 ret = CAMEL_HTML_PARSER_ELEMENT;
689 state = CAMEL_HTML_PARSER_DATA;
692 } else if (c == p->quote) {
693 state = CAMEL_HTML_PARSER_ATTR0;
695 } else if (c == '&') {
696 state = CAMEL_HTML_PARSER_VAL_ENT;
697 g_string_truncate (p->ent, 0);
699 g_string_append_c (p->values->pdata[p->attr], c);
701 } else if (c == '>') {
703 ret = CAMEL_HTML_PARSER_ELEMENT;
704 state = CAMEL_HTML_PARSER_DATA;
707 } else if (c == ' ' || c == '\n' || c == '\t') {
708 state = CAMEL_HTML_PARSER_ATTR0;
710 } else if (c == '&') {
711 state = CAMEL_HTML_PARSER_VAL_ENT;
712 g_string_truncate (p->ent, 0);
714 g_string_append_c (p->values->pdata[p->attr], c);
717 case CAMEL_HTML_PARSER_VAL_ENT:
719 state = CAMEL_HTML_PARSER_VAL;
720 len = convert_entity (p->ent->str + 1, p->ent_utf8);
722 /* fallback; broken entity, just output it and see why we ended */
723 g_string_append (p->values->pdata[p->attr], p->ent->str);
724 g_string_append_c (p->values->pdata[p->attr], ';');
726 d (printf ("got entity: %s = %s\n", p->ent->str, p->ent_utf8));
727 g_string_append_len (p->values->pdata[p->attr], p->ent_utf8, len);
729 } else if (isalnum (c) || c=='#') { /* FIXME: right type */
730 g_string_append_c (p->ent, c);
732 /* fallback; broken entity, just output it and see why we ended */
733 g_string_append (p->values->pdata[p->attr], p->ent->str);
741 /* FIXME: what about other truncated states? */
743 case CAMEL_HTML_PARSER_DATA:
744 case CAMEL_HTML_PARSER_COMMENT:
750 ret = CAMEL_HTML_PARSER_EOF;
751 state = CAMEL_HTML_PARSER_EOF;
755 ret = CAMEL_HTML_PARSER_EOF;
756 state = CAMEL_HTML_PARSER_EOF;
759 /* we only care about remaining data for this buffer, everything else has its own copy */
761 case CAMEL_HTML_PARSER_DATA:
762 case CAMEL_HTML_PARSER_COMMENT:
768 ret = CAMEL_HTML_PARSER_EOD;
772 ret = CAMEL_HTML_PARSER_EOD;