blob: bf671a11736c312f568e20614fe243b0bdbfea14 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * HTMLparser.c : an HTML 4.0 non-verifying parser
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 */
8
Daniel Veillard34ce8be2002-03-18 19:37:11 +00009#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011#ifdef LIBXML_HTML_ENABLED
Bjorn Reese70a9da52001-04-21 16:57:29 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14#ifdef HAVE_CTYPE_H
15#include <ctype.h>
16#endif
17#ifdef HAVE_STDLIB_H
18#include <stdlib.h>
19#endif
20#ifdef HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#ifdef HAVE_FCNTL_H
24#include <fcntl.h>
25#endif
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29#ifdef HAVE_ZLIB_H
30#include <zlib.h>
31#endif
32
33#include <libxml/xmlmemory.h>
34#include <libxml/tree.h>
35#include <libxml/parser.h>
36#include <libxml/parserInternals.h>
37#include <libxml/xmlerror.h>
38#include <libxml/HTMLparser.h>
Daniel Veillard56a4cb82001-03-24 17:00:36 +000039#include <libxml/HTMLtree.h>
Owen Taylor3473f882001-02-23 17:55:21 +000040#include <libxml/entities.h>
41#include <libxml/encoding.h>
42#include <libxml/valid.h>
43#include <libxml/xmlIO.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000044#include <libxml/globals.h>
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000045#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000046
47#define HTML_MAX_NAMELEN 1000
48#define HTML_PARSER_BIG_BUFFER_SIZE 1000
49#define HTML_PARSER_BUFFER_SIZE 100
50
51/* #define DEBUG */
52/* #define DEBUG_PUSH */
53
Daniel Veillard22090732001-07-16 00:06:07 +000054static int htmlOmittedDefaultValue = 1;
Owen Taylor3473f882001-02-23 17:55:21 +000055
Daniel Veillard56a4cb82001-03-24 17:00:36 +000056xmlChar * htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
57 xmlChar end, xmlChar end2, xmlChar end3);
Daniel Veillardc1f78342001-11-10 11:43:05 +000058static void htmlParseComment(htmlParserCtxtPtr ctxt);
Daniel Veillard56a4cb82001-03-24 17:00:36 +000059
60/************************************************************************
61 * *
Daniel Veillardf403d292003-10-05 13:51:35 +000062 * Some factorized error routines *
63 * *
64 ************************************************************************/
65
66/**
William M. Brackedb65a72004-02-06 07:36:04 +000067 * htmlErrMemory:
Daniel Veillardf403d292003-10-05 13:51:35 +000068 * @ctxt: an HTML parser context
69 * @extra: extra informations
70 *
71 * Handle a redefinition of attribute error
72 */
73static void
74htmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
75{
Daniel Veillard157fee02003-10-31 10:36:03 +000076 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
77 (ctxt->instate == XML_PARSER_EOF))
78 return;
Daniel Veillardf403d292003-10-05 13:51:35 +000079 if (ctxt != NULL) {
80 ctxt->errNo = XML_ERR_NO_MEMORY;
81 ctxt->instate = XML_PARSER_EOF;
82 ctxt->disableSAX = 1;
83 }
84 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +000085 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardf403d292003-10-05 13:51:35 +000086 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
87 NULL, NULL, 0, 0,
88 "Memory allocation failed : %s\n", extra);
89 else
Daniel Veillard659e71e2003-10-10 14:10:40 +000090 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardf403d292003-10-05 13:51:35 +000091 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
92 NULL, NULL, 0, 0, "Memory allocation failed\n");
93}
94
95/**
96 * htmlParseErr:
97 * @ctxt: an HTML parser context
98 * @error: the error number
99 * @msg: the error message
100 * @str1: string infor
101 * @str2: string infor
102 *
103 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
104 */
105static void
106htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
107 const char *msg, const xmlChar *str1, const xmlChar *str2)
108{
Daniel Veillard157fee02003-10-31 10:36:03 +0000109 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
110 (ctxt->instate == XML_PARSER_EOF))
111 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000112 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000113 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
Daniel Veillardf403d292003-10-05 13:51:35 +0000114 XML_ERR_ERROR, NULL, 0,
115 (const char *) str1, (const char *) str2,
116 NULL, 0, 0,
117 msg, str1, str2);
118 ctxt->wellFormed = 0;
119}
120
121/**
122 * htmlParseErrInt:
123 * @ctxt: an HTML parser context
124 * @error: the error number
125 * @msg: the error message
126 * @val: integer info
127 *
128 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
129 */
130static void
131htmlParseErrInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
132 const char *msg, int val)
133{
Daniel Veillard157fee02003-10-31 10:36:03 +0000134 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
135 (ctxt->instate == XML_PARSER_EOF))
136 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000137 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000138 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
Daniel Veillardf403d292003-10-05 13:51:35 +0000139 XML_ERR_ERROR, NULL, 0, NULL, NULL,
140 NULL, val, 0, msg, val);
141 ctxt->wellFormed = 0;
142}
143
144/************************************************************************
145 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000146 * Parser stacks related functions and macros *
147 * *
148 ************************************************************************/
149
Daniel Veillard1c732d22002-11-30 11:22:59 +0000150/**
151 * htmlnamePush:
152 * @ctxt: an HTML parser context
153 * @value: the element name
154 *
155 * Pushes a new element name on top of the name stack
156 *
157 * Returns 0 in case of error, the index in the stack otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000158 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000159static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000160htmlnamePush(htmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +0000161{
162 if (ctxt->nameNr >= ctxt->nameMax) {
163 ctxt->nameMax *= 2;
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000164 ctxt->nameTab = (const xmlChar * *)
Igor Zlatkovicd37c1392003-08-28 10:34:33 +0000165 xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +0000166 ctxt->nameMax *
167 sizeof(ctxt->nameTab[0]));
168 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +0000169 htmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000170 return (0);
171 }
172 }
173 ctxt->nameTab[ctxt->nameNr] = value;
174 ctxt->name = value;
175 return (ctxt->nameNr++);
176}
177/**
178 * htmlnamePop:
179 * @ctxt: an HTML parser context
180 *
181 * Pops the top element name from the name stack
182 *
183 * Returns the name just removed
184 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000185static const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000186htmlnamePop(htmlParserCtxtPtr ctxt)
187{
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000188 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000189
Daniel Veillard1c732d22002-11-30 11:22:59 +0000190 if (ctxt->nameNr <= 0)
191 return (0);
192 ctxt->nameNr--;
193 if (ctxt->nameNr < 0)
194 return (0);
195 if (ctxt->nameNr > 0)
196 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
197 else
198 ctxt->name = NULL;
199 ret = ctxt->nameTab[ctxt->nameNr];
200 ctxt->nameTab[ctxt->nameNr] = 0;
201 return (ret);
202}
Owen Taylor3473f882001-02-23 17:55:21 +0000203
204/*
205 * Macros for accessing the content. Those should be used only by the parser,
206 * and not exported.
207 *
208 * Dirty macros, i.e. one need to make assumption on the context to use them
209 *
210 * CUR_PTR return the current pointer to the xmlChar to be parsed.
211 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
212 * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
213 * in UNICODE mode. This should be used internally by the parser
214 * only to compare to ASCII values otherwise it would break when
215 * running with UTF-8 encoding.
216 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
217 * to compare on ASCII based substring.
218 * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
219 * it should be used only to compare on ASCII based substring.
220 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +0000221 * strings without newlines within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +0000222 *
223 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
224 *
225 * CURRENT Returns the current char value, with the full decoding of
226 * UTF-8 if we are using this mode. It returns an int.
227 * NEXT Skip to the next character, this does the proper decoding
228 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +0000229 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +0000230 * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
231 */
232
233#define UPPER (toupper(*ctxt->input->cur))
234
Daniel Veillard77a90a72003-03-22 00:04:05 +0000235#define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val)
Owen Taylor3473f882001-02-23 17:55:21 +0000236
237#define NXT(val) ctxt->input->cur[(val)]
238
239#define UPP(val) (toupper(ctxt->input->cur[(val)]))
240
241#define CUR_PTR ctxt->input->cur
242
Daniel Veillard652f9aa2003-10-28 22:04:45 +0000243#define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
244 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
245 xmlParserInputShrink(ctxt->input)
Owen Taylor3473f882001-02-23 17:55:21 +0000246
Daniel Veillard652f9aa2003-10-28 22:04:45 +0000247#define GROW if ((ctxt->progressive == 0) && \
248 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
249 xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
Owen Taylor3473f882001-02-23 17:55:21 +0000250
251#define CURRENT ((int) (*ctxt->input->cur))
252
253#define SKIP_BLANKS htmlSkipBlankChars(ctxt)
254
255/* Inported from XML */
256
Daniel Veillard561b7f82002-03-20 21:55:57 +0000257/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
258#define CUR ((int) (*ctxt->input->cur))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000259#define NEXT xmlNextChar(ctxt)
Owen Taylor3473f882001-02-23 17:55:21 +0000260
Daniel Veillard561b7f82002-03-20 21:55:57 +0000261#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
Owen Taylor3473f882001-02-23 17:55:21 +0000262#define NXT(val) ctxt->input->cur[(val)]
263#define CUR_PTR ctxt->input->cur
264
265
266#define NEXTL(l) do { \
267 if (*(ctxt->input->cur) == '\n') { \
268 ctxt->input->line++; ctxt->input->col = 1; \
269 } else ctxt->input->col++; \
270 ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++; \
271 } while (0)
272
273/************
274 \
275 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
276 if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
277 ************/
278
279#define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
280#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
281
282#define COPY_BUF(l,b,i,v) \
283 if (l == 1) b[i++] = (xmlChar) v; \
284 else i += xmlCopyChar(l,&b[i],v)
285
286/**
287 * htmlCurrentChar:
288 * @ctxt: the HTML parser context
289 * @len: pointer to the length of the char read
290 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000291 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000292 * bytes in the input buffer. Implement the end of line normalization:
293 * 2.11 End-of-Line Handling
294 * If the encoding is unspecified, in the case we find an ISO-Latin-1
295 * char, then the encoding converter is plugged in automatically.
296 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000297 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000298 */
299
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000300static int
Owen Taylor3473f882001-02-23 17:55:21 +0000301htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
302 if (ctxt->instate == XML_PARSER_EOF)
303 return(0);
304
305 if (ctxt->token != 0) {
306 *len = 0;
307 return(ctxt->token);
308 }
309 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
310 /*
311 * We are supposed to handle UTF8, check it's valid
312 * From rfc2044: encoding of the Unicode values on UTF-8:
313 *
314 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
315 * 0000 0000-0000 007F 0xxxxxxx
316 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
317 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
318 *
319 * Check for the 0x110000 limit too
320 */
321 const unsigned char *cur = ctxt->input->cur;
322 unsigned char c;
323 unsigned int val;
324
325 c = *cur;
326 if (c & 0x80) {
327 if (cur[1] == 0)
328 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
329 if ((cur[1] & 0xc0) != 0x80)
330 goto encoding_error;
331 if ((c & 0xe0) == 0xe0) {
332
333 if (cur[2] == 0)
334 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
335 if ((cur[2] & 0xc0) != 0x80)
336 goto encoding_error;
337 if ((c & 0xf0) == 0xf0) {
338 if (cur[3] == 0)
339 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
340 if (((c & 0xf8) != 0xf0) ||
341 ((cur[3] & 0xc0) != 0x80))
342 goto encoding_error;
343 /* 4-byte code */
344 *len = 4;
345 val = (cur[0] & 0x7) << 18;
346 val |= (cur[1] & 0x3f) << 12;
347 val |= (cur[2] & 0x3f) << 6;
348 val |= cur[3] & 0x3f;
349 } else {
350 /* 3-byte code */
351 *len = 3;
352 val = (cur[0] & 0xf) << 12;
353 val |= (cur[1] & 0x3f) << 6;
354 val |= cur[2] & 0x3f;
355 }
356 } else {
357 /* 2-byte code */
358 *len = 2;
359 val = (cur[0] & 0x1f) << 6;
360 val |= cur[1] & 0x3f;
361 }
362 if (!IS_CHAR(val)) {
Daniel Veillardf403d292003-10-05 13:51:35 +0000363 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
364 "Char 0x%X out of allowed range\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000365 }
366 return(val);
367 } else {
368 /* 1-byte code */
369 *len = 1;
370 return((int) *ctxt->input->cur);
371 }
372 }
373 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000374 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000375 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000376 * XML constructs only use < 128 chars
377 */
378 *len = 1;
379 if ((int) *ctxt->input->cur < 0x80)
380 return((int) *ctxt->input->cur);
381
382 /*
383 * Humm this is bad, do an automatic flow conversion
384 */
385 xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
386 ctxt->charset = XML_CHAR_ENCODING_UTF8;
387 return(xmlCurrentChar(ctxt, len));
388
389encoding_error:
390 /*
391 * If we detect an UTF8 error that probably mean that the
392 * input encoding didn't get properly advertized in the
393 * declaration header. Report the error and switch the encoding
394 * to ISO-Latin-1 (if you don't like this policy, just declare the
395 * encoding !)
396 */
Daniel Veillardf403d292003-10-05 13:51:35 +0000397 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
398 "Input is not proper UTF-8, indicate encoding !\n",
399 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000400 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000401 ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
402 ctxt->input->cur[0], ctxt->input->cur[1],
403 ctxt->input->cur[2], ctxt->input->cur[3]);
404 }
405
406 ctxt->charset = XML_CHAR_ENCODING_8859_1;
407 *len = 1;
408 return((int) *ctxt->input->cur);
409}
410
411/**
Owen Taylor3473f882001-02-23 17:55:21 +0000412 * htmlSkipBlankChars:
413 * @ctxt: the HTML parser context
414 *
415 * skip all blanks character found at that point in the input streams.
416 *
417 * Returns the number of space chars skipped
418 */
419
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000420static int
Owen Taylor3473f882001-02-23 17:55:21 +0000421htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
422 int res = 0;
423
William M. Brack76e95df2003-10-18 16:20:14 +0000424 while (IS_BLANK_CH(*(ctxt->input->cur))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000425 if ((*ctxt->input->cur == 0) &&
426 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
427 xmlPopInput(ctxt);
428 } else {
429 if (*(ctxt->input->cur) == '\n') {
430 ctxt->input->line++; ctxt->input->col = 1;
431 } else ctxt->input->col++;
432 ctxt->input->cur++;
433 ctxt->nbChars++;
434 if (*ctxt->input->cur == 0)
435 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
436 }
437 res++;
438 }
439 return(res);
440}
441
442
443
444/************************************************************************
445 * *
446 * The list of HTML elements and their properties *
447 * *
448 ************************************************************************/
449
450/*
451 * Start Tag: 1 means the start tag can be ommited
452 * End Tag: 1 means the end tag can be ommited
453 * 2 means it's forbidden (empty elements)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000454 * 3 means the tag is stylistic and should be closed easily
Owen Taylor3473f882001-02-23 17:55:21 +0000455 * Depr: this element is deprecated
456 * DTD: 1 means that this element is valid only in the Loose DTD
457 * 2 means that this element is valid only in the Frameset DTD
458 *
Daniel Veillard02bb1702001-06-13 21:11:59 +0000459 * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
Daniel Veillard930dfb62003-02-05 10:17:38 +0000460 , subElements , impliedsubelt , Attributes, userdata
Owen Taylor3473f882001-02-23 17:55:21 +0000461 */
Daniel Veillard930dfb62003-02-05 10:17:38 +0000462
463/* Definitions and a couple of vars for HTML Elements */
464
465#define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small"
466#define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym"
467#define SPECIAL "a", "img", "applet", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe"
468#define INLINE PCDATA FONTSTYLE PHRASE SPECIAL FORMCTRL
469#define BLOCK HEADING LIST "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address"
470#define FORMCTRL "input", "select", "textarea", "label", "button"
471#define PCDATA
472#define HEADING "h1", "h2", "h3", "h4", "h5", "h6"
473#define LIST "ul", "ol", "dir", "menu"
474#define MODIFIER
475#define FLOW BLOCK,INLINE
476#define EMPTY NULL
477
478
479static const char* html_flow[] = { FLOW, NULL } ;
480static const char* html_inline[] = { INLINE, NULL } ;
481
482/* placeholders: elts with content but no subelements */
483static const char* html_pcdata[] = { NULL } ;
484#define html_cdata html_pcdata
485
486
487/* ... and for HTML Attributes */
488
489#define COREATTRS "id", "class", "style", "title"
490#define I18N "lang", "dir"
491#define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"
492#define ATTRS COREATTRS,I18N,EVENTS
493#define CELLHALIGN "align", "char", "charoff"
494#define CELLVALIGN "valign"
495
496static const char* html_attrs[] = { ATTRS, NULL } ;
497static const char* core_i18n_attrs[] = { COREATTRS, I18N, NULL } ;
498static const char* core_attrs[] = { COREATTRS, NULL } ;
499static const char* i18n_attrs[] = { I18N, NULL } ;
500
501
502/* Other declarations that should go inline ... */
503static const char* a_attrs[] = { ATTRS, "charset", "type", "name",
504 "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
505 "tabindex", "onfocus", "onblur", NULL } ;
506static const char* target_attr[] = { "target", NULL } ;
507static const char* rows_cols_attr[] = { "rows", "cols", NULL } ;
508static const char* alt_attr[] = { "alt", NULL } ;
509static const char* src_alt_attrs[] = { "src", "alt", NULL } ;
510static const char* href_attrs[] = { "href", NULL } ;
511static const char* clear_attrs[] = { "clear", NULL } ;
512static const char* inline_p[] = { INLINE, "p", NULL } ;
513static const char* flow_param[] = { FLOW, "param", NULL } ;
514static const char* applet_attrs[] = { COREATTRS , "codebase",
515 "archive", "alt", "name", "height", "width", "align",
516 "hspace", "vspace", NULL } ;
517static const char* area_attrs[] = { "shape", "coords", "href", "nohref",
518 "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
519static const char* basefont_attrs[] =
520 { "id", "size", "color", "face", NULL } ;
521static const char* quote_attrs[] = { ATTRS, "cite", NULL } ;
522static const char* body_contents[] = { FLOW, "ins", "del", NULL } ;
523static const char* body_attrs[] = { ATTRS, "onload", "onunload", NULL } ;
524static const char* body_depr[] = { "background", "bgcolor", "text",
525 "link", "vlink", "alink", NULL } ;
526static const char* button_attrs[] = { ATTRS, "name", "value", "type",
527 "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
528
529
530static const char* col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ;
531static const char* col_elt[] = { "col", NULL } ;
532static const char* edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ;
533static const char* compact_attrs[] = { ATTRS, "compact", NULL } ;
534static const char* dl_contents[] = { "dt", "dd", NULL } ;
535static const char* compact_attr[] = { "compact", NULL } ;
536static const char* label_attr[] = { "label", NULL } ;
537static const char* fieldset_contents[] = { FLOW, "legend" } ;
538static const char* font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ;
539static const char* form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ;
540static const char* form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ;
541static const char* frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ;
542static const char* frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ;
543static const char* frameset_contents[] = { "frameset", "frame", "noframes", NULL } ;
544static const char* head_attrs[] = { I18N, "profile", NULL } ;
545static const char* head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ;
546static const char* hr_depr[] = { "align", "noshade", "size", "width", NULL } ;
547static const char* version_attr[] = { "version", NULL } ;
548static const char* html_content[] = { "head", "body", "frameset", NULL } ;
549static const char* iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ;
550static const char* img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ;
551static const char* input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ;
552static const char* prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ;
553static const char* label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ;
554static const char* legend_attrs[] = { ATTRS, "accesskey", NULL } ;
555static const char* align_attr[] = { "align", NULL } ;
556static const char* link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ;
557static const char* map_contents[] = { BLOCK, "area", NULL } ;
558static const char* name_attr[] = { "name", NULL } ;
559static const char* action_attr[] = { "action", NULL } ;
560static const char* blockli_elt[] = { BLOCK, "li", NULL } ;
561static const char* meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ;
562static const char* content_attr[] = { "content", NULL } ;
563static const char* type_attr[] = { "type", NULL } ;
564static const char* noframes_content[] = { "body", FLOW MODIFIER, NULL } ;
565static const char* object_contents[] = { FLOW, "param", NULL } ;
566static const char* object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ;
567static const char* object_depr[] = { "align", "border", "hspace", "vspace", NULL } ;
568static const char* ol_attrs[] = { "type", "compact", "start", NULL} ;
569static const char* option_elt[] = { "option", NULL } ;
570static const char* optgroup_attrs[] = { ATTRS, "disabled", NULL } ;
571static const char* option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ;
572static const char* param_attrs[] = { "id", "value", "valuetype", "type", NULL } ;
573static const char* width_attr[] = { "width", NULL } ;
574static const char* pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ;
575static const char* script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ;
576static const char* language_attr[] = { "language", NULL } ;
577static const char* select_content[] = { "optgroup", "option", NULL } ;
578static const char* select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ;
579static const char* style_attrs[] = { I18N, "media", "title", NULL } ;
580static const char* table_attrs[] = { ATTRS "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ;
581static const char* table_depr[] = { "align", "bgcolor", NULL } ;
582static const char* table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ;
583static const char* tr_elt[] = { "tr", NULL } ;
584static const char* talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ;
585static const char* th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ;
586static const char* th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ;
587static const char* textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ;
588static const char* tr_contents[] = { "th", "td", NULL } ;
589static const char* bgcolor_attr[] = { "bgcolor", NULL } ;
590static const char* li_elt[] = { "li", NULL } ;
591static const char* ul_depr[] = { "type", "compact", NULL} ;
592static const char* dir_attr[] = { "dir", NULL} ;
593
594#define DECL (const char**)
595
Daniel Veillard22090732001-07-16 00:06:07 +0000596static const htmlElemDesc
597html40ElementTable[] = {
Daniel Veillard930dfb62003-02-05 10:17:38 +0000598{ "a", 0, 0, 0, 0, 0, 0, 1, "anchor ",
599 DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL
600},
601{ "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
602 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
603},
604{ "acronym", 0, 0, 0, 0, 0, 0, 1, "",
605 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
606},
607{ "address", 0, 0, 0, 0, 0, 0, 0, "information on author ",
608 DECL inline_p , NULL , DECL html_attrs, NULL, NULL
609},
610{ "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
611 DECL flow_param , NULL , NULL , DECL applet_attrs, NULL
612},
613{ "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
614 EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr
615},
616{ "b", 0, 3, 0, 0, 0, 0, 1, "bold text style",
617 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
618},
619{ "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
620 EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs
621},
622{ "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
623 EMPTY , NULL , NULL, DECL basefont_attrs, NULL
624},
625{ "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
626 DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr
627},
628{ "big", 0, 3, 0, 0, 0, 0, 1, "large text style",
629 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
630},
631{ "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
632 DECL html_flow , NULL , DECL quote_attrs , NULL, NULL
633},
634{ "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
635 DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL
636},
637{ "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ",
638 EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL
639},
640{ "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
641 DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL
642},
643{ "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ",
644 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
645},
646{ "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
647 DECL html_flow , NULL , NULL, DECL html_attrs, NULL
648},
649{ "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
650 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
651},
652{ "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
653 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
654},
655{ "col", 0, 2, 2, 1, 0, 0, 0, "table column ",
656 EMPTY , NULL , DECL col_attrs , NULL, NULL
657},
658{ "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
659 DECL col_elt , "col" , DECL col_attrs , NULL, NULL
660},
661{ "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ",
662 DECL html_flow , NULL , DECL html_attrs, NULL, NULL
663},
664{ "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ",
665 DECL html_flow , NULL , DECL edit_attrs , NULL, NULL
666},
667{ "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition",
668 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
669},
670{ "dir", 0, 0, 0, 0, 1, 1, 0, "directory list",
671 DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL
672},
673{ "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container",
674 DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL
675},
676{ "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ",
677 DECL dl_contents , "dd" , html_attrs, DECL compact_attr, NULL
678},
679{ "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ",
680 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
681},
682{ "em", 0, 3, 0, 0, 0, 0, 1, "emphasis",
683 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
684},
685{ "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
686 DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL
687},
688{ "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
689 DECL html_inline, NULL, NULL, DECL font_attrs, NULL
690},
691{ "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
692 DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr
693},
694{ "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " ,
695 EMPTY, NULL, NULL, DECL frame_attrs, NULL
696},
697{ "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
698 DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL
699},
700{ "h1", 0, 0, 0, 0, 0, 0, 0, "heading ",
701 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
702},
703{ "h2", 0, 0, 0, 0, 0, 0, 0, "heading ",
704 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
705},
706{ "h3", 0, 0, 0, 0, 0, 0, 0, "heading ",
707 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
708},
709{ "h4", 0, 0, 0, 0, 0, 0, 0, "heading ",
710 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
711},
712{ "h5", 0, 0, 0, 0, 0, 0, 0, "heading ",
713 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
714},
715{ "h6", 0, 0, 0, 0, 0, 0, 0, "heading ",
716 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
717},
718{ "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
719 DECL head_contents, NULL, DECL head_attrs, NULL, NULL
720},
721{ "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
722 EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL
723},
724{ "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
725 DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL
726},
727{ "i", 0, 3, 0, 0, 0, 0, 1, "italic text style",
728 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
729},
730{ "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
731 DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL
732},
733{ "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ",
734 EMPTY, NULL, DECL img_attrs, DECL align_attr, src_alt_attrs
735},
736{ "input", 0, 2, 2, 1, 0, 0, 1, "form control ",
737 EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL
738},
739{ "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text",
740 DECL html_flow, NULL, DECL edit_attrs, NULL, NULL
741},
742{ "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ",
743 EMPTY, NULL, NULL, DECL prompt_attrs, NULL
744},
745{ "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
746 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
747},
748{ "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ",
749 DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL
750},
751{ "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
752 DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL
753},
754{ "li", 0, 1, 1, 0, 0, 0, 0, "list item ",
755 DECL html_flow, NULL, DECL html_attrs, NULL, NULL
756},
757{ "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
758 EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL
759},
760{ "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ",
761 DECL map_contents , NULL, DECL html_attrs , NULL, name_attr
762},
763{ "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
764 DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL
765},
766{ "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
767 EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr
768},
769{ "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
770 DECL noframes_content, "body" , DECL html_attrs, NULL, NULL
771},
772{ "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
773 DECL html_flow, "div", DECL html_attrs, NULL, NULL
774},
775{ "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
776 DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL
777},
778{ "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ",
779 DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL
780},
781{ "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
782 option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr
783},
784{ "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
785 DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL
786},
787{ "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ",
788 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
789},
790{ "param", 0, 2, 2, 1, 0, 0, 0, "named property value ",
791 EMPTY, NULL, DECL param_attrs, NULL, name_attr
792},
793{ "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ",
794 DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL
795},
796{ "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
797 DECL html_inline, NULL, DECL quote_attrs, NULL, NULL
798},
799{ "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style",
800 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
801},
802{ "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
803 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
804},
805{ "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
806 DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr
807},
808{ "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
809 DECL select_content, NULL, DECL select_attrs, NULL, NULL
810},
811{ "small", 0, 3, 0, 0, 0, 0, 1, "small text style",
812 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
813},
814{ "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
815 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
816},
817{ "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
818 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
819},
820{ "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
821 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
822},
823{ "style", 0, 0, 0, 0, 0, 0, 0, "style info ",
824 DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr
825},
826{ "sub", 0, 3, 0, 0, 0, 0, 1, "subscript",
827 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
828},
829{ "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ",
830 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
831},
832{ "table", 0, 0, 0, 0, 0, 0, 0, "",
833 DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL
834},
835{ "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ",
836 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
837},
838{ "td", 0, 0, 0, 0, 0, 0, 0, "table data cell",
839 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
840},
841{ "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
842 DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr
843},
844{ "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ",
845 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
846},
847{ "th", 0, 1, 0, 0, 0, 0, 0, "table header cell",
848 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
849},
850{ "thead", 0, 1, 0, 0, 0, 0, 0, "table header ",
851 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
852},
853{ "title", 0, 0, 0, 0, 0, 0, 0, "document title ",
854 DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL
855},
856{ "tr", 0, 0, 0, 0, 0, 0, 0, "table row ",
857 DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL
858},
859{ "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
860 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
861},
862{ "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style",
863 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
864},
865{ "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ",
866 DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL
867},
868{ "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
869 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
870}
Owen Taylor3473f882001-02-23 17:55:21 +0000871};
872
873/*
Owen Taylor3473f882001-02-23 17:55:21 +0000874 * start tags that imply the end of current element
875 */
Daniel Veillard22090732001-07-16 00:06:07 +0000876static const char *htmlStartClose[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000877"form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
878 "dl", "ul", "ol", "menu", "dir", "address", "pre",
879 "listing", "xmp", "head", NULL,
880"head", "p", NULL,
881"title", "p", NULL,
882"body", "head", "style", "link", "title", "p", NULL,
Daniel Veillard25d5d9a2004-04-05 07:08:42 +0000883"frameset", "head", "style", "link", "title", "p", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000884"li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
885 "pre", "listing", "xmp", "head", "li", NULL,
886"hr", "p", "head", NULL,
887"h1", "p", "head", NULL,
888"h2", "p", "head", NULL,
889"h3", "p", "head", NULL,
890"h4", "p", "head", NULL,
891"h5", "p", "head", NULL,
892"h6", "p", "head", NULL,
893"dir", "p", "head", NULL,
894"address", "p", "head", "ul", NULL,
895"pre", "p", "head", "ul", NULL,
896"listing", "p", "head", NULL,
897"xmp", "p", "head", NULL,
898"blockquote", "p", "head", NULL,
899"dl", "p", "dt", "menu", "dir", "address", "pre", "listing",
900 "xmp", "head", NULL,
901"dt", "p", "menu", "dir", "address", "pre", "listing", "xmp",
902 "head", "dd", NULL,
903"dd", "p", "menu", "dir", "address", "pre", "listing", "xmp",
904 "head", "dt", NULL,
905"ul", "p", "head", "ol", "menu", "dir", "address", "pre",
906 "listing", "xmp", NULL,
907"ol", "p", "head", "ul", NULL,
908"menu", "p", "head", "ul", NULL,
909"p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL,
910"div", "p", "head", NULL,
911"noscript", "p", "head", NULL,
912"center", "font", "b", "i", "p", "head", NULL,
913"a", "a", NULL,
914"caption", "p", NULL,
915"colgroup", "caption", "colgroup", "col", "p", NULL,
916"col", "caption", "col", "p", NULL,
917"table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
918 "listing", "xmp", "a", NULL,
Daniel Veillard43dadeb2001-04-24 11:23:35 +0000919"th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
920"td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000921"tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
922"thead", "caption", "col", "colgroup", NULL,
923"tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead",
924 "tbody", "p", NULL,
925"tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead",
926 "tfoot", "tbody", "p", NULL,
927"optgroup", "option", NULL,
928"option", "option", NULL,
929"fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
930 "pre", "listing", "xmp", "a", NULL,
931NULL
932};
933
934/*
935 * The list of HTML elements which are supposed not to have
936 * CDATA content and where a p element will be implied
937 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000938 * TODO: extend that list by reading the HTML SGML DTD on
Owen Taylor3473f882001-02-23 17:55:21 +0000939 * implied paragraph
940 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000941static const char *htmlNoContentElements[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000942 "html",
943 "head",
944 "body",
945 NULL
946};
947
948/*
949 * The list of HTML attributes which are of content %Script;
950 * NOTE: when adding ones, check htmlIsScriptAttribute() since
951 * it assumes the name starts with 'on'
952 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000953static const char *htmlScriptAttributes[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000954 "onclick",
955 "ondblclick",
956 "onmousedown",
957 "onmouseup",
958 "onmouseover",
959 "onmousemove",
960 "onmouseout",
961 "onkeypress",
962 "onkeydown",
963 "onkeyup",
964 "onload",
965 "onunload",
966 "onfocus",
967 "onblur",
968 "onsubmit",
969 "onrest",
970 "onchange",
971 "onselect"
972};
973
Daniel Veillarda2bc3682001-05-03 08:27:20 +0000974/*
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000975 * This table is used by the htmlparser to know what to do with
976 * broken html pages. By assigning different priorities to different
977 * elements the parser can decide how to handle extra endtags.
978 * Endtags are only allowed to close elements with lower or equal
979 * priority.
980 */
Daniel Veillarda2bc3682001-05-03 08:27:20 +0000981
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000982typedef struct {
983 const char *name;
984 int priority;
985} elementPriority;
986
Daniel Veillard22090732001-07-16 00:06:07 +0000987static const elementPriority htmlEndPriority[] = {
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000988 {"div", 150},
989 {"td", 160},
990 {"th", 160},
991 {"tr", 170},
992 {"thead", 180},
993 {"tbody", 180},
994 {"tfoot", 180},
995 {"table", 190},
996 {"head", 200},
997 {"body", 200},
998 {"html", 220},
999 {NULL, 100} /* Default priority */
1000};
Owen Taylor3473f882001-02-23 17:55:21 +00001001
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001002static const char** htmlStartCloseIndex[100];
Owen Taylor3473f882001-02-23 17:55:21 +00001003static int htmlStartCloseIndexinitialized = 0;
1004
1005/************************************************************************
1006 * *
1007 * functions to handle HTML specific data *
1008 * *
1009 ************************************************************************/
1010
1011/**
1012 * htmlInitAutoClose:
1013 *
1014 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1015 * This is not reentrant. Call xmlInitParser() once before processing in
1016 * case of use in multithreaded programs.
1017 */
1018void
1019htmlInitAutoClose(void) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001020 int indx, i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001021
1022 if (htmlStartCloseIndexinitialized) return;
1023
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001024 for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL;
1025 indx = 0;
1026 while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) {
1027 htmlStartCloseIndex[indx++] = &htmlStartClose[i];
Owen Taylor3473f882001-02-23 17:55:21 +00001028 while (htmlStartClose[i] != NULL) i++;
1029 i++;
1030 }
1031 htmlStartCloseIndexinitialized = 1;
1032}
1033
1034/**
1035 * htmlTagLookup:
1036 * @tag: The tag name in lowercase
1037 *
1038 * Lookup the HTML tag in the ElementTable
1039 *
1040 * Returns the related htmlElemDescPtr or NULL if not found.
1041 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001042const htmlElemDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001043htmlTagLookup(const xmlChar *tag) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001044 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001045
1046 for (i = 0; i < (sizeof(html40ElementTable) /
1047 sizeof(html40ElementTable[0]));i++) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +00001048 if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name))
William M. Brack78637da2003-07-31 14:47:38 +00001049 return((htmlElemDescPtr) &html40ElementTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001050 }
1051 return(NULL);
1052}
1053
1054/**
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001055 * htmlGetEndPriority:
1056 * @name: The name of the element to look up the priority for.
1057 *
1058 * Return value: The "endtag" priority.
1059 **/
1060static int
1061htmlGetEndPriority (const xmlChar *name) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001062 int i = 0;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001063
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001064 while ((htmlEndPriority[i].name != NULL) &&
1065 (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
1066 i++;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001067
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001068 return(htmlEndPriority[i].priority);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001069}
1070
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001071
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001072/**
Owen Taylor3473f882001-02-23 17:55:21 +00001073 * htmlCheckAutoClose:
1074 * @newtag: The new tag name
1075 * @oldtag: The old tag name
1076 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001077 * Checks whether the new tag is one of the registered valid tags for
1078 * closing old.
Owen Taylor3473f882001-02-23 17:55:21 +00001079 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1080 *
1081 * Returns 0 if no, 1 if yes.
1082 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001083static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001084htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag)
1085{
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001086 int i, indx;
1087 const char **closed = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001088
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001089 if (htmlStartCloseIndexinitialized == 0)
1090 htmlInitAutoClose();
Owen Taylor3473f882001-02-23 17:55:21 +00001091
1092 /* inefficient, but not a big deal */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001093 for (indx = 0; indx < 100; indx++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001094 closed = htmlStartCloseIndex[indx];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001095 if (closed == NULL)
1096 return (0);
1097 if (xmlStrEqual(BAD_CAST * closed, newtag))
1098 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001099 }
1100
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001101 i = closed - htmlStartClose;
Owen Taylor3473f882001-02-23 17:55:21 +00001102 i++;
1103 while (htmlStartClose[i] != NULL) {
1104 if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001105 return (1);
1106 }
1107 i++;
Owen Taylor3473f882001-02-23 17:55:21 +00001108 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001109 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001110}
1111
1112/**
1113 * htmlAutoCloseOnClose:
1114 * @ctxt: an HTML parser context
1115 * @newtag: The new tag name
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001116 * @force: force the tag closure
Owen Taylor3473f882001-02-23 17:55:21 +00001117 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001118 * The HTML DTD allows an ending tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001119 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001120static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001121htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1122{
1123 const htmlElemDesc *info;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001124 int i, priority;
Owen Taylor3473f882001-02-23 17:55:21 +00001125
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001126 priority = htmlGetEndPriority(newtag);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001127
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001128 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001129
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001130 if (xmlStrEqual(newtag, ctxt->nameTab[i]))
1131 break;
1132 /*
1133 * A missplaced endtag can only close elements with lower
1134 * or equal priority, so if we find an element with higher
1135 * priority before we find an element with
1136 * matching name, we just ignore this endtag
1137 */
1138 if (htmlGetEndPriority(ctxt->nameTab[i]) > priority)
1139 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001140 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001141 if (i < 0)
1142 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001143
1144 while (!xmlStrEqual(newtag, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001145 info = htmlTagLookup(ctxt->name);
Daniel Veillardf403d292003-10-05 13:51:35 +00001146 if ((info != NULL) && (info->endTag == 3)) {
1147 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
1148 "Opening and ending tag mismatch: %s and %s\n",
Daniel Veillard05bcb7e2003-10-19 14:26:34 +00001149 newtag, ctxt->name);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001150 }
1151 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1152 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001153 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001154 }
1155}
1156
1157/**
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001158 * htmlAutoCloseOnEnd:
1159 * @ctxt: an HTML parser context
1160 *
1161 * Close all remaining tags at the end of the stream
1162 */
1163static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001164htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt)
1165{
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001166 int i;
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001167
William M. Brack899e64a2003-09-26 18:03:42 +00001168 if (ctxt->nameNr == 0)
1169 return;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001170 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001171 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1172 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001173 htmlnamePop(ctxt);
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001174 }
1175}
1176
1177/**
Owen Taylor3473f882001-02-23 17:55:21 +00001178 * htmlAutoClose:
1179 * @ctxt: an HTML parser context
1180 * @newtag: The new tag name or NULL
1181 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001182 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001183 * The list is kept in htmlStartClose array. This function is
1184 * called when a new tag has been detected and generates the
1185 * appropriates closes if possible/needed.
1186 * If newtag is NULL this mean we are at the end of the resource
1187 * and we should check
1188 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001189static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001190htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1191{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001192 while ((newtag != NULL) && (ctxt->name != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001193 (htmlCheckAutoClose(newtag, ctxt->name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001194 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1195 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001196 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001197 }
1198 if (newtag == NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001199 htmlAutoCloseOnEnd(ctxt);
1200 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001201 }
1202 while ((newtag == NULL) && (ctxt->name != NULL) &&
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001203 ((xmlStrEqual(ctxt->name, BAD_CAST "head")) ||
1204 (xmlStrEqual(ctxt->name, BAD_CAST "body")) ||
1205 (xmlStrEqual(ctxt->name, BAD_CAST "html")))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001206 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1207 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001208 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001209 }
Owen Taylor3473f882001-02-23 17:55:21 +00001210}
1211
1212/**
1213 * htmlAutoCloseTag:
1214 * @doc: the HTML document
1215 * @name: The tag name
1216 * @elem: the HTML element
1217 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001218 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001219 * The list is kept in htmlStartClose array. This function checks
1220 * if the element or one of it's children would autoclose the
1221 * given tag.
1222 *
1223 * Returns 1 if autoclose, 0 otherwise
1224 */
1225int
1226htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
1227 htmlNodePtr child;
1228
1229 if (elem == NULL) return(1);
1230 if (xmlStrEqual(name, elem->name)) return(0);
1231 if (htmlCheckAutoClose(elem->name, name)) return(1);
1232 child = elem->children;
1233 while (child != NULL) {
1234 if (htmlAutoCloseTag(doc, name, child)) return(1);
1235 child = child->next;
1236 }
1237 return(0);
1238}
1239
1240/**
1241 * htmlIsAutoClosed:
1242 * @doc: the HTML document
1243 * @elem: the HTML element
1244 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001245 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001246 * The list is kept in htmlStartClose array. This function checks
1247 * if a tag is autoclosed by one of it's child
1248 *
1249 * Returns 1 if autoclosed, 0 otherwise
1250 */
1251int
1252htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
1253 htmlNodePtr child;
1254
1255 if (elem == NULL) return(1);
1256 child = elem->children;
1257 while (child != NULL) {
1258 if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
1259 child = child->next;
1260 }
1261 return(0);
1262}
1263
1264/**
1265 * htmlCheckImplied:
1266 * @ctxt: an HTML parser context
1267 * @newtag: The new tag name
1268 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001269 * The HTML DTD allows a tag to exists only implicitly
Owen Taylor3473f882001-02-23 17:55:21 +00001270 * called when a new tag has been detected and generates the
1271 * appropriates implicit tags if missing
1272 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001273static void
Owen Taylor3473f882001-02-23 17:55:21 +00001274htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
1275 if (!htmlOmittedDefaultValue)
1276 return;
1277 if (xmlStrEqual(newtag, BAD_CAST"html"))
1278 return;
1279 if (ctxt->nameNr <= 0) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001280 htmlnamePush(ctxt, BAD_CAST"html");
Owen Taylor3473f882001-02-23 17:55:21 +00001281 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1282 ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
1283 }
1284 if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
1285 return;
1286 if ((ctxt->nameNr <= 1) &&
1287 ((xmlStrEqual(newtag, BAD_CAST"script")) ||
1288 (xmlStrEqual(newtag, BAD_CAST"style")) ||
1289 (xmlStrEqual(newtag, BAD_CAST"meta")) ||
1290 (xmlStrEqual(newtag, BAD_CAST"link")) ||
1291 (xmlStrEqual(newtag, BAD_CAST"title")) ||
1292 (xmlStrEqual(newtag, BAD_CAST"base")))) {
1293 /*
1294 * dropped OBJECT ... i you put it first BODY will be
1295 * assumed !
1296 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001297 htmlnamePush(ctxt, BAD_CAST"head");
Owen Taylor3473f882001-02-23 17:55:21 +00001298 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1299 ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
1300 } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
1301 (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
1302 (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
1303 int i;
1304 for (i = 0;i < ctxt->nameNr;i++) {
1305 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
1306 return;
1307 }
1308 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
1309 return;
1310 }
1311 }
1312
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001313 htmlnamePush(ctxt, BAD_CAST"body");
Owen Taylor3473f882001-02-23 17:55:21 +00001314 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1315 ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
1316 }
1317}
1318
1319/**
1320 * htmlCheckParagraph
1321 * @ctxt: an HTML parser context
1322 *
1323 * Check whether a p element need to be implied before inserting
1324 * characters in the current element.
1325 *
1326 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1327 * in case of error.
1328 */
1329
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001330static int
Owen Taylor3473f882001-02-23 17:55:21 +00001331htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
1332 const xmlChar *tag;
1333 int i;
1334
1335 if (ctxt == NULL)
1336 return(-1);
1337 tag = ctxt->name;
1338 if (tag == NULL) {
1339 htmlAutoClose(ctxt, BAD_CAST"p");
1340 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001341 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001342 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1343 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1344 return(1);
1345 }
1346 if (!htmlOmittedDefaultValue)
1347 return(0);
1348 for (i = 0; htmlNoContentElements[i] != NULL; i++) {
1349 if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
Owen Taylor3473f882001-02-23 17:55:21 +00001350 htmlAutoClose(ctxt, BAD_CAST"p");
1351 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001352 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001353 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1354 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1355 return(1);
1356 }
1357 }
1358 return(0);
1359}
1360
1361/**
1362 * htmlIsScriptAttribute:
1363 * @name: an attribute name
1364 *
1365 * Check if an attribute is of content type Script
1366 *
1367 * Returns 1 is the attribute is a script 0 otherwise
1368 */
1369int
1370htmlIsScriptAttribute(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001371 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001372
1373 if (name == NULL)
1374 return(0);
1375 /*
1376 * all script attributes start with 'on'
1377 */
1378 if ((name[0] != 'o') || (name[1] != 'n'))
1379 return(0);
1380 for (i = 0;
1381 i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
1382 i++) {
1383 if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
1384 return(1);
1385 }
1386 return(0);
1387}
1388
1389/************************************************************************
1390 * *
1391 * The list of HTML predefined entities *
1392 * *
1393 ************************************************************************/
1394
1395
Daniel Veillard22090732001-07-16 00:06:07 +00001396static const htmlEntityDesc html40EntitiesTable[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001397/*
1398 * the 4 absolute ones, plus apostrophe.
1399 */
1400{ 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1401{ 38, "amp", "ampersand, U+0026 ISOnum" },
1402{ 39, "apos", "single quote" },
1403{ 60, "lt", "less-than sign, U+003C ISOnum" },
1404{ 62, "gt", "greater-than sign, U+003E ISOnum" },
1405
1406/*
1407 * A bunch still in the 128-255 range
1408 * Replacing them depend really on the charset used.
1409 */
1410{ 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1411{ 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1412{ 162, "cent", "cent sign, U+00A2 ISOnum" },
1413{ 163, "pound","pound sign, U+00A3 ISOnum" },
1414{ 164, "curren","currency sign, U+00A4 ISOnum" },
1415{ 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
1416{ 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1417{ 167, "sect", "section sign, U+00A7 ISOnum" },
1418{ 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1419{ 169, "copy", "copyright sign, U+00A9 ISOnum" },
1420{ 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1421{ 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1422{ 172, "not", "not sign, U+00AC ISOnum" },
1423{ 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1424{ 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
1425{ 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1426{ 176, "deg", "degree sign, U+00B0 ISOnum" },
1427{ 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1428{ 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1429{ 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1430{ 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1431{ 181, "micro","micro sign, U+00B5 ISOnum" },
1432{ 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1433{ 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1434{ 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1435{ 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1436{ 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1437{ 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1438{ 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1439{ 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1440{ 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1441{ 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1442{ 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1443{ 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1444{ 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1445{ 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1446{ 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1447{ 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1448{ 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1449{ 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1450{ 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1451{ 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1452{ 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1453{ 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1454{ 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1455{ 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1456{ 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1457{ 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1458{ 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
1459{ 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1460{ 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1461{ 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1462{ 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1463{ 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1464{ 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1465{ 215, "times","multiplication sign, U+00D7 ISOnum" },
1466{ 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1467{ 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1468{ 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1469{ 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1470{ 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1471{ 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1472{ 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1473{ 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1474{ 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1475{ 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1476{ 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1477{ 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1478{ 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1479{ 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1480{ 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1481{ 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1482{ 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1483{ 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1484{ 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1485{ 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1486{ 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1487{ 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1488{ 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1489{ 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1490{ 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
1491{ 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1492{ 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1493{ 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1494{ 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1495{ 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1496{ 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1497{ 247, "divide","division sign, U+00F7 ISOnum" },
1498{ 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1499{ 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1500{ 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1501{ 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1502{ 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1503{ 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1504{ 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1505{ 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1506
1507{ 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1508{ 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
1509{ 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1510{ 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1511{ 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1512
1513/*
1514 * Anything below should really be kept as entities references
1515 */
1516{ 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1517
1518{ 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1519{ 732, "tilde","small tilde, U+02DC ISOdia" },
1520
1521{ 913, "Alpha","greek capital letter alpha, U+0391" },
1522{ 914, "Beta", "greek capital letter beta, U+0392" },
1523{ 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1524{ 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1525{ 917, "Epsilon","greek capital letter epsilon, U+0395" },
1526{ 918, "Zeta", "greek capital letter zeta, U+0396" },
1527{ 919, "Eta", "greek capital letter eta, U+0397" },
1528{ 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1529{ 921, "Iota", "greek capital letter iota, U+0399" },
1530{ 922, "Kappa","greek capital letter kappa, U+039A" },
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001531{ 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
Owen Taylor3473f882001-02-23 17:55:21 +00001532{ 924, "Mu", "greek capital letter mu, U+039C" },
1533{ 925, "Nu", "greek capital letter nu, U+039D" },
1534{ 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1535{ 927, "Omicron","greek capital letter omicron, U+039F" },
1536{ 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1537{ 929, "Rho", "greek capital letter rho, U+03A1" },
1538{ 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1539{ 932, "Tau", "greek capital letter tau, U+03A4" },
1540{ 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1541{ 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
1542{ 935, "Chi", "greek capital letter chi, U+03A7" },
1543{ 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
1544{ 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1545
1546{ 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1547{ 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1548{ 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1549{ 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
1550{ 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1551{ 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1552{ 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
1553{ 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
1554{ 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1555{ 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1556{ 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1557{ 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
1558{ 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
1559{ 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
1560{ 959, "omicron","greek small letter omicron, U+03BF NEW" },
1561{ 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1562{ 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
1563{ 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1564{ 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1565{ 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
1566{ 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1567{ 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
1568{ 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
1569{ 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
1570{ 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
1571{ 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1572{ 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1573{ 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
1574
1575{ 8194, "ensp", "en space, U+2002 ISOpub" },
1576{ 8195, "emsp", "em space, U+2003 ISOpub" },
1577{ 8201, "thinsp","thin space, U+2009 ISOpub" },
1578{ 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1579{ 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
1580{ 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
1581{ 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
1582{ 8211, "ndash","en dash, U+2013 ISOpub" },
1583{ 8212, "mdash","em dash, U+2014 ISOpub" },
1584{ 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1585{ 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1586{ 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1587{ 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1588{ 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1589{ 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1590{ 8224, "dagger","dagger, U+2020 ISOpub" },
1591{ 8225, "Dagger","double dagger, U+2021 ISOpub" },
1592
1593{ 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1594{ 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1595
1596{ 8240, "permil","per mille sign, U+2030 ISOtech" },
1597
1598{ 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1599{ 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1600
1601{ 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1602{ 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1603
1604{ 8254, "oline","overline = spacing overscore, U+203E NEW" },
1605{ 8260, "frasl","fraction slash, U+2044 NEW" },
1606
1607{ 8364, "euro", "euro sign, U+20AC NEW" },
1608
1609{ 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1610{ 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1611{ 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
1612{ 8482, "trade","trade mark sign, U+2122 ISOnum" },
1613{ 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
1614{ 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
1615{ 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
1616{ 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
1617{ 8595, "darr", "downwards arrow, U+2193 ISOnum" },
1618{ 8596, "harr", "left right arrow, U+2194 ISOamsa" },
1619{ 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
1620{ 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
1621{ 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
1622{ 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
1623{ 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
1624{ 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
1625
1626{ 8704, "forall","for all, U+2200 ISOtech" },
1627{ 8706, "part", "partial differential, U+2202 ISOtech" },
1628{ 8707, "exist","there exists, U+2203 ISOtech" },
1629{ 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
1630{ 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
1631{ 8712, "isin", "element of, U+2208 ISOtech" },
1632{ 8713, "notin","not an element of, U+2209 ISOtech" },
1633{ 8715, "ni", "contains as member, U+220B ISOtech" },
1634{ 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001635{ 8721, "sum", "n-ary summation, U+2211 ISOamsb" },
Owen Taylor3473f882001-02-23 17:55:21 +00001636{ 8722, "minus","minus sign, U+2212 ISOtech" },
1637{ 8727, "lowast","asterisk operator, U+2217 ISOtech" },
1638{ 8730, "radic","square root = radical sign, U+221A ISOtech" },
1639{ 8733, "prop", "proportional to, U+221D ISOtech" },
1640{ 8734, "infin","infinity, U+221E ISOtech" },
1641{ 8736, "ang", "angle, U+2220 ISOamso" },
1642{ 8743, "and", "logical and = wedge, U+2227 ISOtech" },
1643{ 8744, "or", "logical or = vee, U+2228 ISOtech" },
1644{ 8745, "cap", "intersection = cap, U+2229 ISOtech" },
1645{ 8746, "cup", "union = cup, U+222A ISOtech" },
1646{ 8747, "int", "integral, U+222B ISOtech" },
1647{ 8756, "there4","therefore, U+2234 ISOtech" },
1648{ 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
1649{ 8773, "cong", "approximately equal to, U+2245 ISOtech" },
1650{ 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
1651{ 8800, "ne", "not equal to, U+2260 ISOtech" },
1652{ 8801, "equiv","identical to, U+2261 ISOtech" },
1653{ 8804, "le", "less-than or equal to, U+2264 ISOtech" },
1654{ 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
1655{ 8834, "sub", "subset of, U+2282 ISOtech" },
1656{ 8835, "sup", "superset of, U+2283 ISOtech" },
1657{ 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
1658{ 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
1659{ 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
1660{ 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
1661{ 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
1662{ 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
1663{ 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
1664{ 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
1665{ 8969, "rceil","right ceiling, U+2309 ISOamsc" },
1666{ 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
1667{ 8971, "rfloor","right floor, U+230B ISOamsc" },
1668{ 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
1669{ 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
1670{ 9674, "loz", "lozenge, U+25CA ISOpub" },
1671
1672{ 9824, "spades","black spade suit, U+2660 ISOpub" },
1673{ 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
1674{ 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
1675{ 9830, "diams","black diamond suit, U+2666 ISOpub" },
1676
1677};
1678
1679/************************************************************************
1680 * *
1681 * Commodity functions to handle entities *
1682 * *
1683 ************************************************************************/
1684
1685/*
1686 * Macro used to grow the current buffer.
1687 */
1688#define growBuffer(buffer) { \
1689 buffer##_size *= 2; \
Daniel Veillard3487c8d2002-09-05 11:33:25 +00001690 buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
Owen Taylor3473f882001-02-23 17:55:21 +00001691 if (buffer == NULL) { \
Daniel Veillardf403d292003-10-05 13:51:35 +00001692 htmlErrMemory(ctxt, "growing buffer\n"); \
Owen Taylor3473f882001-02-23 17:55:21 +00001693 return(NULL); \
1694 } \
1695}
1696
1697/**
1698 * htmlEntityLookup:
1699 * @name: the entity name
1700 *
1701 * Lookup the given entity in EntitiesTable
1702 *
1703 * TODO: the linear scan is really ugly, an hash table is really needed.
1704 *
1705 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1706 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001707const htmlEntityDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001708htmlEntityLookup(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001709 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001710
1711 for (i = 0;i < (sizeof(html40EntitiesTable)/
1712 sizeof(html40EntitiesTable[0]));i++) {
1713 if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
William M. Brack78637da2003-07-31 14:47:38 +00001714 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001715 }
1716 }
1717 return(NULL);
1718}
1719
1720/**
1721 * htmlEntityValueLookup:
1722 * @value: the entity's unicode value
1723 *
1724 * Lookup the given entity in EntitiesTable
1725 *
1726 * TODO: the linear scan is really ugly, an hash table is really needed.
1727 *
1728 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1729 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001730const htmlEntityDesc *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001731htmlEntityValueLookup(unsigned int value) {
1732 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001733
1734 for (i = 0;i < (sizeof(html40EntitiesTable)/
1735 sizeof(html40EntitiesTable[0]));i++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001736 if (html40EntitiesTable[i].value >= value) {
1737 if (html40EntitiesTable[i].value > value)
Owen Taylor3473f882001-02-23 17:55:21 +00001738 break;
William M. Brack78637da2003-07-31 14:47:38 +00001739 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001740 }
Owen Taylor3473f882001-02-23 17:55:21 +00001741 }
1742 return(NULL);
1743}
1744
1745/**
1746 * UTF8ToHtml:
1747 * @out: a pointer to an array of bytes to store the result
1748 * @outlen: the length of @out
1749 * @in: a pointer to an array of UTF-8 chars
1750 * @inlen: the length of @in
1751 *
1752 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1753 * plus HTML entities block of chars out.
1754 *
1755 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1756 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001757 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001758 * The value of @outlen after return is the number of octets consumed.
1759 */
1760int
1761UTF8ToHtml(unsigned char* out, int *outlen,
1762 const unsigned char* in, int *inlen) {
1763 const unsigned char* processed = in;
1764 const unsigned char* outend;
1765 const unsigned char* outstart = out;
1766 const unsigned char* instart = in;
1767 const unsigned char* inend;
1768 unsigned int c, d;
1769 int trailing;
1770
1771 if (in == NULL) {
1772 /*
1773 * initialization nothing to do
1774 */
1775 *outlen = 0;
1776 *inlen = 0;
1777 return(0);
1778 }
1779 inend = in + (*inlen);
1780 outend = out + (*outlen);
1781 while (in < inend) {
1782 d = *in++;
1783 if (d < 0x80) { c= d; trailing= 0; }
1784 else if (d < 0xC0) {
1785 /* trailing byte in leading position */
1786 *outlen = out - outstart;
1787 *inlen = processed - instart;
1788 return(-2);
1789 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1790 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1791 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1792 else {
1793 /* no chance for this in Ascii */
1794 *outlen = out - outstart;
1795 *inlen = processed - instart;
1796 return(-2);
1797 }
1798
1799 if (inend - in < trailing) {
1800 break;
1801 }
1802
1803 for ( ; trailing; trailing--) {
1804 if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
1805 break;
1806 c <<= 6;
1807 c |= d & 0x3F;
1808 }
1809
1810 /* assertion: c is a single UTF-4 value */
1811 if (c < 0x80) {
1812 if (out + 1 >= outend)
1813 break;
1814 *out++ = c;
1815 } else {
1816 int len;
Daniel Veillardbb371292001-08-16 23:26:59 +00001817 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00001818
1819 /*
1820 * Try to lookup a predefined HTML entity for it
1821 */
1822
1823 ent = htmlEntityValueLookup(c);
1824 if (ent == NULL) {
1825 /* no chance for this in Ascii */
1826 *outlen = out - outstart;
1827 *inlen = processed - instart;
1828 return(-2);
1829 }
1830 len = strlen(ent->name);
1831 if (out + 2 + len >= outend)
1832 break;
1833 *out++ = '&';
1834 memcpy(out, ent->name, len);
1835 out += len;
1836 *out++ = ';';
1837 }
1838 processed = in;
1839 }
1840 *outlen = out - outstart;
1841 *inlen = processed - instart;
1842 return(0);
1843}
1844
1845/**
1846 * htmlEncodeEntities:
1847 * @out: a pointer to an array of bytes to store the result
1848 * @outlen: the length of @out
1849 * @in: a pointer to an array of UTF-8 chars
1850 * @inlen: the length of @in
1851 * @quoteChar: the quote character to escape (' or ") or zero.
1852 *
1853 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1854 * plus HTML entities block of chars out.
1855 *
1856 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1857 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001858 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001859 * The value of @outlen after return is the number of octets consumed.
1860 */
1861int
1862htmlEncodeEntities(unsigned char* out, int *outlen,
1863 const unsigned char* in, int *inlen, int quoteChar) {
1864 const unsigned char* processed = in;
1865 const unsigned char* outend = out + (*outlen);
1866 const unsigned char* outstart = out;
1867 const unsigned char* instart = in;
1868 const unsigned char* inend = in + (*inlen);
1869 unsigned int c, d;
1870 int trailing;
1871
1872 while (in < inend) {
1873 d = *in++;
1874 if (d < 0x80) { c= d; trailing= 0; }
1875 else if (d < 0xC0) {
1876 /* trailing byte in leading position */
1877 *outlen = out - outstart;
1878 *inlen = processed - instart;
1879 return(-2);
1880 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1881 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1882 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1883 else {
1884 /* no chance for this in Ascii */
1885 *outlen = out - outstart;
1886 *inlen = processed - instart;
1887 return(-2);
1888 }
1889
1890 if (inend - in < trailing)
1891 break;
1892
1893 while (trailing--) {
1894 if (((d= *in++) & 0xC0) != 0x80) {
1895 *outlen = out - outstart;
1896 *inlen = processed - instart;
1897 return(-2);
1898 }
1899 c <<= 6;
1900 c |= d & 0x3F;
1901 }
1902
1903 /* assertion: c is a single UTF-4 value */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001904 if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
1905 (c != '&') && (c != '<') && (c != '>')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001906 if (out >= outend)
1907 break;
1908 *out++ = c;
1909 } else {
Daniel Veillardbb371292001-08-16 23:26:59 +00001910 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00001911 const char *cp;
1912 char nbuf[16];
1913 int len;
1914
1915 /*
1916 * Try to lookup a predefined HTML entity for it
1917 */
1918 ent = htmlEntityValueLookup(c);
1919 if (ent == NULL) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001920 snprintf(nbuf, sizeof(nbuf), "#%u", c);
Owen Taylor3473f882001-02-23 17:55:21 +00001921 cp = nbuf;
1922 }
1923 else
1924 cp = ent->name;
1925 len = strlen(cp);
1926 if (out + 2 + len > outend)
1927 break;
1928 *out++ = '&';
1929 memcpy(out, cp, len);
1930 out += len;
1931 *out++ = ';';
1932 }
1933 processed = in;
1934 }
1935 *outlen = out - outstart;
1936 *inlen = processed - instart;
1937 return(0);
1938}
1939
Owen Taylor3473f882001-02-23 17:55:21 +00001940/************************************************************************
1941 * *
1942 * Commodity functions to handle streams *
1943 * *
1944 ************************************************************************/
1945
1946/**
Owen Taylor3473f882001-02-23 17:55:21 +00001947 * htmlNewInputStream:
1948 * @ctxt: an HTML parser context
1949 *
1950 * Create a new input stream structure
1951 * Returns the new input stream or NULL
1952 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001953static htmlParserInputPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001954htmlNewInputStream(htmlParserCtxtPtr ctxt) {
1955 htmlParserInputPtr input;
1956
1957 input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1958 if (input == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00001959 htmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001960 return(NULL);
1961 }
1962 memset(input, 0, sizeof(htmlParserInput));
1963 input->filename = NULL;
1964 input->directory = NULL;
1965 input->base = NULL;
1966 input->cur = NULL;
1967 input->buf = NULL;
1968 input->line = 1;
1969 input->col = 1;
1970 input->buf = NULL;
1971 input->free = NULL;
1972 input->version = NULL;
1973 input->consumed = 0;
1974 input->length = 0;
1975 return(input);
1976}
1977
1978
1979/************************************************************************
1980 * *
1981 * Commodity functions, cleanup needed ? *
1982 * *
1983 ************************************************************************/
Daniel Veillard8c9872c2002-07-05 18:17:10 +00001984/*
1985 * all tags allowing pc data from the html 4.01 loose dtd
1986 * NOTE: it might be more apropriate to integrate this information
1987 * into the html40ElementTable array but I don't want to risk any
1988 * binary incomptibility
1989 */
1990static const char *allowPCData[] = {
1991 "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
1992 "blockquote", "body", "button", "caption", "center", "cite", "code",
1993 "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
1994 "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
1995 "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
1996 "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
1997};
Owen Taylor3473f882001-02-23 17:55:21 +00001998
1999/**
2000 * areBlanks:
2001 * @ctxt: an HTML parser context
2002 * @str: a xmlChar *
2003 * @len: the size of @str
2004 *
2005 * Is this a sequence of blank chars that one can ignore ?
2006 *
2007 * Returns 1 if ignorable 0 otherwise.
2008 */
2009
2010static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002011 unsigned int i;
2012 int j;
Owen Taylor3473f882001-02-23 17:55:21 +00002013 xmlNodePtr lastChild;
2014
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002015 for (j = 0;j < len;j++)
William M. Brack76e95df2003-10-18 16:20:14 +00002016 if (!(IS_BLANK_CH(str[j]))) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002017
2018 if (CUR == 0) return(1);
2019 if (CUR != '<') return(0);
2020 if (ctxt->name == NULL)
2021 return(1);
2022 if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
2023 return(1);
2024 if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
2025 return(1);
2026 if (xmlStrEqual(ctxt->name, BAD_CAST"body"))
2027 return(1);
2028 if (ctxt->node == NULL) return(0);
2029 lastChild = xmlGetLastChild(ctxt->node);
2030 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002031 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2032 (ctxt->node->content != NULL)) return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002033 /* keep ws in constructs like ...<b> </b>...
2034 for all tags "b" allowing PCDATA */
2035 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2036 if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) {
2037 return(0);
2038 }
2039 }
Owen Taylor3473f882001-02-23 17:55:21 +00002040 } else if (xmlNodeIsText(lastChild)) {
2041 return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002042 } else {
2043 /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
2044 for all tags "p" allowing PCDATA */
2045 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2046 if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) {
2047 return(0);
2048 }
2049 }
Owen Taylor3473f882001-02-23 17:55:21 +00002050 }
2051 return(1);
2052}
2053
2054/**
Owen Taylor3473f882001-02-23 17:55:21 +00002055 * htmlNewDocNoDtD:
2056 * @URI: URI for the dtd, or NULL
2057 * @ExternalID: the external ID of the DTD, or NULL
2058 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002059 * Creates a new HTML document without a DTD node if @URI and @ExternalID
2060 * are NULL
2061 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002062 * Returns a new document, do not initialize the DTD if not provided
Owen Taylor3473f882001-02-23 17:55:21 +00002063 */
2064htmlDocPtr
2065htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
2066 xmlDocPtr cur;
2067
2068 /*
2069 * Allocate a new document and fill the fields.
2070 */
2071 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
2072 if (cur == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002073 htmlErrMemory(NULL, "HTML document creation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002074 return(NULL);
2075 }
2076 memset(cur, 0, sizeof(xmlDoc));
2077
2078 cur->type = XML_HTML_DOCUMENT_NODE;
2079 cur->version = NULL;
2080 cur->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002081 cur->doc = cur;
2082 cur->name = NULL;
2083 cur->children = NULL;
2084 cur->extSubset = NULL;
2085 cur->oldNs = NULL;
2086 cur->encoding = NULL;
2087 cur->standalone = 1;
2088 cur->compression = 0;
2089 cur->ids = NULL;
2090 cur->refs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002091 cur->_private = NULL;
Daniel Veillardb6b0fd82001-10-22 12:31:11 +00002092 if ((ExternalID != NULL) ||
2093 (URI != NULL))
Daniel Veillard40412cd2003-09-03 13:28:32 +00002094 xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002095 return(cur);
2096}
2097
2098/**
2099 * htmlNewDoc:
2100 * @URI: URI for the dtd, or NULL
2101 * @ExternalID: the external ID of the DTD, or NULL
2102 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002103 * Creates a new HTML document
2104 *
Owen Taylor3473f882001-02-23 17:55:21 +00002105 * Returns a new document
2106 */
2107htmlDocPtr
2108htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
2109 if ((URI == NULL) && (ExternalID == NULL))
2110 return(htmlNewDocNoDtD(
Daniel Veillard64269352001-05-04 17:52:34 +00002111 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
2112 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
Owen Taylor3473f882001-02-23 17:55:21 +00002113
2114 return(htmlNewDocNoDtD(URI, ExternalID));
2115}
2116
2117
2118/************************************************************************
2119 * *
2120 * The parser itself *
2121 * Relates to http://www.w3.org/TR/html40 *
2122 * *
2123 ************************************************************************/
2124
2125/************************************************************************
2126 * *
2127 * The parser itself *
2128 * *
2129 ************************************************************************/
2130
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002131static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002132
Owen Taylor3473f882001-02-23 17:55:21 +00002133/**
2134 * htmlParseHTMLName:
2135 * @ctxt: an HTML parser context
2136 *
2137 * parse an HTML tag or attribute name, note that we convert it to lowercase
2138 * since HTML names are not case-sensitive.
2139 *
2140 * Returns the Tag Name parsed or NULL
2141 */
2142
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002143static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002144htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002145 int i = 0;
2146 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2147
William M. Brack76e95df2003-10-18 16:20:14 +00002148 if (!IS_LETTER_CH(CUR) && (CUR != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002149 (CUR != ':')) return(NULL);
2150
2151 while ((i < HTML_PARSER_BUFFER_SIZE) &&
William M. Brack76e95df2003-10-18 16:20:14 +00002152 ((IS_LETTER_CH(CUR)) || (IS_DIGIT_CH(CUR)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002153 (CUR == ':') || (CUR == '-') || (CUR == '_'))) {
2154 if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2155 else loc[i] = CUR;
2156 i++;
2157
2158 NEXT;
2159 }
2160
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002161 return(xmlDictLookup(ctxt->dict, loc, i));
Owen Taylor3473f882001-02-23 17:55:21 +00002162}
2163
2164/**
2165 * htmlParseName:
2166 * @ctxt: an HTML parser context
2167 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002168 * parse an HTML name, this routine is case sensitive.
Owen Taylor3473f882001-02-23 17:55:21 +00002169 *
2170 * Returns the Name parsed or NULL
2171 */
2172
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002173static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002174htmlParseName(htmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002175 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002176 const xmlChar *ret;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002177 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002178
2179 GROW;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002180
2181 /*
2182 * Accelerator for simple ASCII names
2183 */
2184 in = ctxt->input->cur;
2185 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2186 ((*in >= 0x41) && (*in <= 0x5A)) ||
2187 (*in == '_') || (*in == ':')) {
2188 in++;
2189 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2190 ((*in >= 0x41) && (*in <= 0x5A)) ||
2191 ((*in >= 0x30) && (*in <= 0x39)) ||
2192 (*in == '_') || (*in == '-') ||
2193 (*in == ':') || (*in == '.'))
2194 in++;
2195 if ((*in > 0) && (*in < 0x80)) {
2196 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002197 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002198 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002199 ctxt->nbChars += count;
2200 ctxt->input->col += count;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002201 return(ret);
2202 }
2203 }
2204 return(htmlParseNameComplex(ctxt));
2205}
2206
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002207static const xmlChar *
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002208htmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002209 int len = 0, l;
2210 int c;
2211 int count = 0;
2212
2213 /*
2214 * Handler for more complex cases
2215 */
2216 GROW;
2217 c = CUR_CHAR(l);
2218 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2219 (!IS_LETTER(c) && (c != '_') &&
2220 (c != ':'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002221 return(NULL);
2222 }
2223
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002224 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
2225 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2226 (c == '.') || (c == '-') ||
2227 (c == '_') || (c == ':') ||
2228 (IS_COMBINING(c)) ||
2229 (IS_EXTENDER(c)))) {
2230 if (count++ > 100) {
2231 count = 0;
2232 GROW;
2233 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002234 len += l;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002235 NEXTL(l);
2236 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002237 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002238 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002239}
2240
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002241
Owen Taylor3473f882001-02-23 17:55:21 +00002242/**
2243 * htmlParseHTMLAttribute:
2244 * @ctxt: an HTML parser context
2245 * @stop: a char stop value
2246 *
2247 * parse an HTML attribute value till the stop (quote), if
2248 * stop is 0 then it stops at the first space
2249 *
2250 * Returns the attribute parsed or NULL
2251 */
2252
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002253static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002254htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
2255 xmlChar *buffer = NULL;
2256 int buffer_size = 0;
2257 xmlChar *out = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002258 const xmlChar *name = NULL;
2259 const xmlChar *cur = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00002260 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00002261
2262 /*
2263 * allocate a translation buffer.
2264 */
2265 buffer_size = HTML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002266 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002267 if (buffer == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002268 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002269 return(NULL);
2270 }
2271 out = buffer;
2272
2273 /*
2274 * Ok loop until we reach one of the ending chars
2275 */
Daniel Veillard957fdcf2001-11-06 22:50:19 +00002276 while ((CUR != 0) && (CUR != stop)) {
2277 if ((stop == 0) && (CUR == '>')) break;
William M. Brack76e95df2003-10-18 16:20:14 +00002278 if ((stop == 0) && (IS_BLANK_CH(CUR))) break;
Owen Taylor3473f882001-02-23 17:55:21 +00002279 if (CUR == '&') {
2280 if (NXT(1) == '#') {
2281 unsigned int c;
2282 int bits;
2283
2284 c = htmlParseCharRef(ctxt);
2285 if (c < 0x80)
2286 { *out++ = c; bits= -6; }
2287 else if (c < 0x800)
2288 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2289 else if (c < 0x10000)
2290 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2291 else
2292 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2293
2294 for ( ; bits >= 0; bits-= 6) {
2295 *out++ = ((c >> bits) & 0x3F) | 0x80;
2296 }
Daniel Veillardce02dbc2002-10-22 19:14:58 +00002297
2298 if (out - buffer > buffer_size - 100) {
2299 int indx = out - buffer;
2300
2301 growBuffer(buffer);
2302 out = &buffer[indx];
2303 }
Owen Taylor3473f882001-02-23 17:55:21 +00002304 } else {
2305 ent = htmlParseEntityRef(ctxt, &name);
2306 if (name == NULL) {
2307 *out++ = '&';
2308 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002309 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002310
2311 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002312 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002313 }
2314 } else if (ent == NULL) {
2315 *out++ = '&';
2316 cur = name;
2317 while (*cur != 0) {
2318 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002319 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002320
2321 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002322 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002323 }
2324 *out++ = *cur++;
2325 }
Owen Taylor3473f882001-02-23 17:55:21 +00002326 } else {
2327 unsigned int c;
2328 int bits;
2329
2330 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002331 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002332
2333 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002334 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002335 }
2336 c = (xmlChar)ent->value;
2337 if (c < 0x80)
2338 { *out++ = c; bits= -6; }
2339 else if (c < 0x800)
2340 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2341 else if (c < 0x10000)
2342 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2343 else
2344 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2345
2346 for ( ; bits >= 0; bits-= 6) {
2347 *out++ = ((c >> bits) & 0x3F) | 0x80;
2348 }
Owen Taylor3473f882001-02-23 17:55:21 +00002349 }
2350 }
2351 } else {
2352 unsigned int c;
2353 int bits, l;
2354
2355 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002356 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002357
2358 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002359 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002360 }
2361 c = CUR_CHAR(l);
2362 if (c < 0x80)
2363 { *out++ = c; bits= -6; }
2364 else if (c < 0x800)
2365 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2366 else if (c < 0x10000)
2367 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2368 else
2369 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2370
2371 for ( ; bits >= 0; bits-= 6) {
2372 *out++ = ((c >> bits) & 0x3F) | 0x80;
2373 }
2374 NEXT;
2375 }
2376 }
2377 *out++ = 0;
2378 return(buffer);
2379}
2380
2381/**
Owen Taylor3473f882001-02-23 17:55:21 +00002382 * htmlParseEntityRef:
2383 * @ctxt: an HTML parser context
2384 * @str: location to store the entity name
2385 *
2386 * parse an HTML ENTITY references
2387 *
2388 * [68] EntityRef ::= '&' Name ';'
2389 *
2390 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2391 * if non-NULL *str will have to be freed by the caller.
2392 */
Daniel Veillardbb371292001-08-16 23:26:59 +00002393const htmlEntityDesc *
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002394htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) {
2395 const xmlChar *name;
Daniel Veillardbb371292001-08-16 23:26:59 +00002396 const htmlEntityDesc * ent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002397 *str = NULL;
2398
2399 if (CUR == '&') {
2400 NEXT;
2401 name = htmlParseName(ctxt);
2402 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002403 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
2404 "htmlParseEntityRef: no name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002405 } else {
2406 GROW;
2407 if (CUR == ';') {
2408 *str = name;
2409
2410 /*
2411 * Lookup the entity in the table.
2412 */
2413 ent = htmlEntityLookup(name);
2414 if (ent != NULL) /* OK that's ugly !!! */
2415 NEXT;
2416 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002417 htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING,
2418 "htmlParseEntityRef: expecting ';'\n",
2419 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002420 *str = name;
2421 }
2422 }
2423 }
2424 return(ent);
2425}
2426
2427/**
2428 * htmlParseAttValue:
2429 * @ctxt: an HTML parser context
2430 *
2431 * parse a value for an attribute
2432 * Note: the parser won't do substitution of entities here, this
2433 * will be handled later in xmlStringGetNodeList, unless it was
2434 * asked for ctxt->replaceEntities != 0
2435 *
2436 * Returns the AttValue parsed or NULL.
2437 */
2438
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002439static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002440htmlParseAttValue(htmlParserCtxtPtr ctxt) {
2441 xmlChar *ret = NULL;
2442
2443 if (CUR == '"') {
2444 NEXT;
2445 ret = htmlParseHTMLAttribute(ctxt, '"');
2446 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002447 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2448 "AttValue: \" expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002449 } else
2450 NEXT;
2451 } else if (CUR == '\'') {
2452 NEXT;
2453 ret = htmlParseHTMLAttribute(ctxt, '\'');
2454 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002455 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2456 "AttValue: ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002457 } else
2458 NEXT;
2459 } else {
2460 /*
2461 * That's an HTMLism, the attribute value may not be quoted
2462 */
2463 ret = htmlParseHTMLAttribute(ctxt, 0);
2464 if (ret == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002465 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
2466 "AttValue: no value found\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002467 }
2468 }
2469 return(ret);
2470}
2471
2472/**
2473 * htmlParseSystemLiteral:
2474 * @ctxt: an HTML parser context
2475 *
2476 * parse an HTML Literal
2477 *
2478 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2479 *
2480 * Returns the SystemLiteral parsed or NULL
2481 */
2482
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002483static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002484htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
2485 const xmlChar *q;
2486 xmlChar *ret = NULL;
2487
2488 if (CUR == '"') {
2489 NEXT;
2490 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002491 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00002492 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002493 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002494 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2495 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002496 } else {
2497 ret = xmlStrndup(q, CUR_PTR - q);
2498 NEXT;
2499 }
2500 } else if (CUR == '\'') {
2501 NEXT;
2502 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002503 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002504 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002505 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002506 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2507 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002508 } else {
2509 ret = xmlStrndup(q, CUR_PTR - q);
2510 NEXT;
2511 }
2512 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002513 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2514 " or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002515 }
2516
2517 return(ret);
2518}
2519
2520/**
2521 * htmlParsePubidLiteral:
2522 * @ctxt: an HTML parser context
2523 *
2524 * parse an HTML public literal
2525 *
2526 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2527 *
2528 * Returns the PubidLiteral parsed or NULL.
2529 */
2530
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002531static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002532htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
2533 const xmlChar *q;
2534 xmlChar *ret = NULL;
2535 /*
2536 * Name ::= (Letter | '_') (NameChar)*
2537 */
2538 if (CUR == '"') {
2539 NEXT;
2540 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002541 while (IS_PUBIDCHAR_CH(CUR)) NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00002542 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002543 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2544 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002545 } else {
2546 ret = xmlStrndup(q, CUR_PTR - q);
2547 NEXT;
2548 }
2549 } else if (CUR == '\'') {
2550 NEXT;
2551 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002552 while ((IS_PUBIDCHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002553 NEXT;
Daniel Veillard6560a422003-03-27 21:25:38 +00002554 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002555 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2556 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002557 } else {
2558 ret = xmlStrndup(q, CUR_PTR - q);
2559 NEXT;
2560 }
2561 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002562 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2563 "PubidLiteral \" or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002564 }
2565
2566 return(ret);
2567}
2568
2569/**
2570 * htmlParseScript:
2571 * @ctxt: an HTML parser context
2572 *
2573 * parse the content of an HTML SCRIPT or STYLE element
2574 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
2575 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
2576 * http://www.w3.org/TR/html4/types.html#type-script
2577 * http://www.w3.org/TR/html4/types.html#h-6.15
2578 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
2579 *
2580 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
2581 * element and the value of intrinsic event attributes. User agents must
2582 * not evaluate script data as HTML markup but instead must pass it on as
2583 * data to a script engine.
2584 * NOTES:
2585 * - The content is passed like CDATA
2586 * - the attributes for style and scripting "onXXX" are also described
2587 * as CDATA but SGML allows entities references in attributes so their
2588 * processing is identical as other attributes
2589 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002590static void
Owen Taylor3473f882001-02-23 17:55:21 +00002591htmlParseScript(htmlParserCtxtPtr ctxt) {
2592 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 1];
2593 int nbchar = 0;
2594 xmlChar cur;
2595
2596 SHRINK;
2597 cur = CUR;
William M. Brack76e95df2003-10-18 16:20:14 +00002598 while (IS_CHAR_CH(cur)) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00002599 if ((cur == '<') && (NXT(1) == '!') && (NXT(2) == '-') &&
2600 (NXT(3) == '-')) {
2601 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2602 if (ctxt->sax->cdataBlock!= NULL) {
2603 /*
2604 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2605 */
2606 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002607 } else if (ctxt->sax->characters != NULL) {
2608 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Daniel Veillardc1f78342001-11-10 11:43:05 +00002609 }
2610 }
2611 nbchar = 0;
2612 htmlParseComment(ctxt);
2613 cur = CUR;
2614 continue;
2615 } else if ((cur == '<') && (NXT(1) == '/')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002616 /*
2617 * One should break here, the specification is clear:
2618 * Authors should therefore escape "</" within the content.
2619 * Escape mechanisms are specific to each scripting or
2620 * style sheet language.
2621 */
2622 if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
2623 ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
2624 break; /* while */
2625 }
2626 buf[nbchar++] = cur;
2627 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2628 if (ctxt->sax->cdataBlock!= NULL) {
2629 /*
2630 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2631 */
2632 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002633 } else if (ctxt->sax->characters != NULL) {
2634 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002635 }
2636 nbchar = 0;
2637 }
2638 NEXT;
2639 cur = CUR;
2640 }
William M. Brack76e95df2003-10-18 16:20:14 +00002641 if (!(IS_CHAR_CH(cur))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002642 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2643 "Invalid char in CDATA 0x%X\n", cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002644 NEXT;
2645 }
2646
2647 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2648 if (ctxt->sax->cdataBlock!= NULL) {
2649 /*
2650 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2651 */
2652 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002653 } else if (ctxt->sax->characters != NULL) {
2654 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002655 }
2656 }
2657}
2658
2659
2660/**
2661 * htmlParseCharData:
2662 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002663 *
2664 * parse a CharData section.
2665 * if we are within a CDATA section ']]>' marks an end of section.
2666 *
2667 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2668 */
2669
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002670static void
2671htmlParseCharData(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002672 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
2673 int nbchar = 0;
2674 int cur, l;
2675
2676 SHRINK;
2677 cur = CUR_CHAR(l);
2678 while (((cur != '<') || (ctxt->token == '<')) &&
2679 ((cur != '&') || (ctxt->token == '&')) &&
2680 (IS_CHAR(cur))) {
2681 COPY_BUF(l,buf,nbchar,cur);
2682 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2683 /*
2684 * Ok the segment is to be consumed as chars.
2685 */
2686 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2687 if (areBlanks(ctxt, buf, nbchar)) {
2688 if (ctxt->sax->ignorableWhitespace != NULL)
2689 ctxt->sax->ignorableWhitespace(ctxt->userData,
2690 buf, nbchar);
2691 } else {
2692 htmlCheckParagraph(ctxt);
2693 if (ctxt->sax->characters != NULL)
2694 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2695 }
2696 }
2697 nbchar = 0;
2698 }
2699 NEXTL(l);
2700 cur = CUR_CHAR(l);
Daniel Veillard358a9892003-02-04 15:22:32 +00002701 if (cur == 0) {
2702 SHRINK;
2703 GROW;
2704 cur = CUR_CHAR(l);
2705 }
Owen Taylor3473f882001-02-23 17:55:21 +00002706 }
2707 if (nbchar != 0) {
2708 /*
2709 * Ok the segment is to be consumed as chars.
2710 */
2711 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2712 if (areBlanks(ctxt, buf, nbchar)) {
2713 if (ctxt->sax->ignorableWhitespace != NULL)
2714 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2715 } else {
2716 htmlCheckParagraph(ctxt);
2717 if (ctxt->sax->characters != NULL)
2718 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2719 }
2720 }
Daniel Veillard7cc95c02001-10-17 15:45:12 +00002721 } else {
2722 /*
2723 * Loop detection
2724 */
2725 if (cur == 0)
2726 ctxt->instate = XML_PARSER_EOF;
Owen Taylor3473f882001-02-23 17:55:21 +00002727 }
2728}
2729
2730/**
2731 * htmlParseExternalID:
2732 * @ctxt: an HTML parser context
2733 * @publicID: a xmlChar** receiving PubidLiteral
Owen Taylor3473f882001-02-23 17:55:21 +00002734 *
2735 * Parse an External ID or a Public ID
2736 *
Owen Taylor3473f882001-02-23 17:55:21 +00002737 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
2738 * | 'PUBLIC' S PubidLiteral S SystemLiteral
2739 *
2740 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
2741 *
2742 * Returns the function returns SystemLiteral and in the second
2743 * case publicID receives PubidLiteral, is strict is off
2744 * it is possible to return NULL and have publicID set.
2745 */
2746
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002747static xmlChar *
2748htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
Owen Taylor3473f882001-02-23 17:55:21 +00002749 xmlChar *URI = NULL;
2750
2751 if ((UPPER == 'S') && (UPP(1) == 'Y') &&
2752 (UPP(2) == 'S') && (UPP(3) == 'T') &&
2753 (UPP(4) == 'E') && (UPP(5) == 'M')) {
2754 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002755 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002756 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2757 "Space required after 'SYSTEM'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002758 }
2759 SKIP_BLANKS;
2760 URI = htmlParseSystemLiteral(ctxt);
2761 if (URI == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002762 htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
2763 "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002764 }
2765 } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
2766 (UPP(2) == 'B') && (UPP(3) == 'L') &&
2767 (UPP(4) == 'I') && (UPP(5) == 'C')) {
2768 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002769 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002770 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2771 "Space required after 'PUBLIC'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002772 }
2773 SKIP_BLANKS;
2774 *publicID = htmlParsePubidLiteral(ctxt);
2775 if (*publicID == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002776 htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
2777 "htmlParseExternalID: PUBLIC, no Public Identifier\n",
2778 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002779 }
2780 SKIP_BLANKS;
2781 if ((CUR == '"') || (CUR == '\'')) {
2782 URI = htmlParseSystemLiteral(ctxt);
2783 }
2784 }
2785 return(URI);
2786}
2787
2788/**
2789 * htmlParseComment:
2790 * @ctxt: an HTML parser context
2791 *
2792 * Parse an XML (SGML) comment <!-- .... -->
2793 *
2794 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
2795 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002796static void
Owen Taylor3473f882001-02-23 17:55:21 +00002797htmlParseComment(htmlParserCtxtPtr ctxt) {
2798 xmlChar *buf = NULL;
2799 int len;
2800 int size = HTML_PARSER_BUFFER_SIZE;
2801 int q, ql;
2802 int r, rl;
2803 int cur, l;
2804 xmlParserInputState state;
2805
2806 /*
2807 * Check that there is a comment right here.
2808 */
2809 if ((RAW != '<') || (NXT(1) != '!') ||
2810 (NXT(2) != '-') || (NXT(3) != '-')) return;
2811
2812 state = ctxt->instate;
2813 ctxt->instate = XML_PARSER_COMMENT;
2814 SHRINK;
2815 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002816 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002817 if (buf == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002818 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002819 ctxt->instate = state;
2820 return;
2821 }
2822 q = CUR_CHAR(ql);
2823 NEXTL(ql);
2824 r = CUR_CHAR(rl);
2825 NEXTL(rl);
2826 cur = CUR_CHAR(l);
2827 len = 0;
2828 while (IS_CHAR(cur) &&
2829 ((cur != '>') ||
2830 (r != '-') || (q != '-'))) {
2831 if (len + 5 >= size) {
2832 size *= 2;
2833 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2834 if (buf == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002835 htmlErrMemory(ctxt, "growing buffer failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002836 ctxt->instate = state;
2837 return;
2838 }
2839 }
2840 COPY_BUF(ql,buf,len,q);
2841 q = r;
2842 ql = rl;
2843 r = cur;
2844 rl = l;
2845 NEXTL(l);
2846 cur = CUR_CHAR(l);
2847 if (cur == 0) {
2848 SHRINK;
2849 GROW;
2850 cur = CUR_CHAR(l);
2851 }
2852 }
2853 buf[len] = 0;
2854 if (!IS_CHAR(cur)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002855 htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
2856 "Comment not terminated \n<!--%.50s\n", buf, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002857 xmlFree(buf);
2858 } else {
2859 NEXT;
2860 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
2861 (!ctxt->disableSAX))
2862 ctxt->sax->comment(ctxt->userData, buf);
2863 xmlFree(buf);
2864 }
2865 ctxt->instate = state;
2866}
2867
2868/**
2869 * htmlParseCharRef:
2870 * @ctxt: an HTML parser context
2871 *
2872 * parse Reference declarations
2873 *
2874 * [66] CharRef ::= '&#' [0-9]+ ';' |
2875 * '&#x' [0-9a-fA-F]+ ';'
2876 *
2877 * Returns the value parsed (as an int)
2878 */
2879int
2880htmlParseCharRef(htmlParserCtxtPtr ctxt) {
2881 int val = 0;
2882
2883 if ((CUR == '&') && (NXT(1) == '#') &&
Daniel Veillardc59d8262003-11-20 21:59:12 +00002884 ((NXT(2) == 'x') || NXT(2) == 'X')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002885 SKIP(3);
2886 while (CUR != ';') {
2887 if ((CUR >= '0') && (CUR <= '9'))
2888 val = val * 16 + (CUR - '0');
2889 else if ((CUR >= 'a') && (CUR <= 'f'))
2890 val = val * 16 + (CUR - 'a') + 10;
2891 else if ((CUR >= 'A') && (CUR <= 'F'))
2892 val = val * 16 + (CUR - 'A') + 10;
2893 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002894 htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
2895 "htmlParseCharRef: invalid hexadecimal value\n",
2896 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002897 return(0);
2898 }
2899 NEXT;
2900 }
2901 if (CUR == ';')
2902 NEXT;
2903 } else if ((CUR == '&') && (NXT(1) == '#')) {
2904 SKIP(2);
2905 while (CUR != ';') {
2906 if ((CUR >= '0') && (CUR <= '9'))
2907 val = val * 10 + (CUR - '0');
2908 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002909 htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
2910 "htmlParseCharRef: invalid decimal value\n",
2911 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002912 return(0);
2913 }
2914 NEXT;
2915 }
2916 if (CUR == ';')
2917 NEXT;
2918 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002919 htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
2920 "htmlParseCharRef: invalid value\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002921 }
2922 /*
2923 * Check the value IS_CHAR ...
2924 */
2925 if (IS_CHAR(val)) {
2926 return(val);
2927 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002928 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2929 "htmlParseCharRef: invalid xmlChar value %d\n",
2930 val);
Owen Taylor3473f882001-02-23 17:55:21 +00002931 }
2932 return(0);
2933}
2934
2935
2936/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002937 * htmlParseDocTypeDecl:
Owen Taylor3473f882001-02-23 17:55:21 +00002938 * @ctxt: an HTML parser context
2939 *
2940 * parse a DOCTYPE declaration
2941 *
2942 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
2943 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
2944 */
2945
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002946static void
Owen Taylor3473f882001-02-23 17:55:21 +00002947htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002948 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00002949 xmlChar *ExternalID = NULL;
2950 xmlChar *URI = NULL;
2951
2952 /*
2953 * We know that '<!DOCTYPE' has been detected.
2954 */
2955 SKIP(9);
2956
2957 SKIP_BLANKS;
2958
2959 /*
2960 * Parse the DOCTYPE name.
2961 */
2962 name = htmlParseName(ctxt);
2963 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002964 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
2965 "htmlParseDocTypeDecl : no DOCTYPE name !\n",
2966 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002967 }
2968 /*
2969 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
2970 */
2971
2972 SKIP_BLANKS;
2973
2974 /*
2975 * Check for SystemID and ExternalID
2976 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002977 URI = htmlParseExternalID(ctxt, &ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00002978 SKIP_BLANKS;
2979
2980 /*
2981 * We should be at the end of the DOCTYPE declaration.
2982 */
2983 if (CUR != '>') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002984 htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
2985 "DOCTYPE improperly terminated\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002986 /* We shouldn't try to resynchronize ... */
2987 }
2988 NEXT;
2989
2990 /*
2991 * Create or update the document accordingly to the DOCTYPE
2992 */
2993 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
2994 (!ctxt->disableSAX))
2995 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
2996
2997 /*
2998 * Cleanup, since we don't use all those identifiers
2999 */
3000 if (URI != NULL) xmlFree(URI);
3001 if (ExternalID != NULL) xmlFree(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003002}
3003
3004/**
3005 * htmlParseAttribute:
3006 * @ctxt: an HTML parser context
3007 * @value: a xmlChar ** used to store the value of the attribute
3008 *
3009 * parse an attribute
3010 *
3011 * [41] Attribute ::= Name Eq AttValue
3012 *
3013 * [25] Eq ::= S? '=' S?
3014 *
3015 * With namespace:
3016 *
3017 * [NS 11] Attribute ::= QName Eq AttValue
3018 *
3019 * Also the case QName == xmlns:??? is handled independently as a namespace
3020 * definition.
3021 *
3022 * Returns the attribute name, and the value in *value.
3023 */
3024
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003025static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003026htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003027 const xmlChar *name;
3028 xmlChar *val = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003029
3030 *value = NULL;
3031 name = htmlParseHTMLName(ctxt);
3032 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003033 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3034 "error parsing attribute name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003035 return(NULL);
3036 }
3037
3038 /*
3039 * read the value
3040 */
3041 SKIP_BLANKS;
3042 if (CUR == '=') {
3043 NEXT;
3044 SKIP_BLANKS;
3045 val = htmlParseAttValue(ctxt);
3046 /******
3047 } else {
3048 * TODO : some attribute must have values, some may not
3049 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3050 ctxt->sax->warning(ctxt->userData,
3051 "No value for attribute %s\n", name); */
3052 }
3053
3054 *value = val;
3055 return(name);
3056}
3057
3058/**
3059 * htmlCheckEncoding:
3060 * @ctxt: an HTML parser context
3061 * @attvalue: the attribute value
3062 *
3063 * Checks an http-equiv attribute from a Meta tag to detect
3064 * the encoding
3065 * If a new encoding is detected the parser is switched to decode
3066 * it and pass UTF8
3067 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003068static void
Owen Taylor3473f882001-02-23 17:55:21 +00003069htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
3070 const xmlChar *encoding;
3071
3072 if ((ctxt == NULL) || (attvalue == NULL))
3073 return;
3074
3075 /* do not change encoding */
3076 if (ctxt->input->encoding != NULL)
3077 return;
3078
3079 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
3080 if (encoding != NULL) {
3081 encoding += 8;
3082 } else {
3083 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
3084 if (encoding != NULL)
3085 encoding += 9;
3086 }
3087 if (encoding != NULL) {
3088 xmlCharEncoding enc;
3089 xmlCharEncodingHandlerPtr handler;
3090
3091 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
3092
3093 if (ctxt->input->encoding != NULL)
3094 xmlFree((xmlChar *) ctxt->input->encoding);
3095 ctxt->input->encoding = xmlStrdup(encoding);
3096
3097 enc = xmlParseCharEncoding((const char *) encoding);
3098 /*
3099 * registered set of known encodings
3100 */
3101 if (enc != XML_CHAR_ENCODING_ERROR) {
3102 xmlSwitchEncoding(ctxt, enc);
3103 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3104 } else {
3105 /*
3106 * fallback for unknown encodings
3107 */
3108 handler = xmlFindCharEncodingHandler((const char *) encoding);
3109 if (handler != NULL) {
3110 xmlSwitchToEncoding(ctxt, handler);
3111 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3112 } else {
3113 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3114 }
3115 }
3116
3117 if ((ctxt->input->buf != NULL) &&
3118 (ctxt->input->buf->encoder != NULL) &&
3119 (ctxt->input->buf->raw != NULL) &&
3120 (ctxt->input->buf->buffer != NULL)) {
3121 int nbchars;
3122 int processed;
3123
3124 /*
3125 * convert as much as possible to the parser reading buffer.
3126 */
3127 processed = ctxt->input->cur - ctxt->input->base;
3128 xmlBufferShrink(ctxt->input->buf->buffer, processed);
3129 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
3130 ctxt->input->buf->buffer,
3131 ctxt->input->buf->raw);
3132 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003133 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3134 "htmlCheckEncoding: encoder error\n",
3135 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003136 }
3137 ctxt->input->base =
3138 ctxt->input->cur = ctxt->input->buf->buffer->content;
3139 }
3140 }
3141}
3142
3143/**
3144 * htmlCheckMeta:
3145 * @ctxt: an HTML parser context
3146 * @atts: the attributes values
3147 *
3148 * Checks an attributes from a Meta tag
3149 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003150static void
Owen Taylor3473f882001-02-23 17:55:21 +00003151htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
3152 int i;
3153 const xmlChar *att, *value;
3154 int http = 0;
3155 const xmlChar *content = NULL;
3156
3157 if ((ctxt == NULL) || (atts == NULL))
3158 return;
3159
3160 i = 0;
3161 att = atts[i++];
3162 while (att != NULL) {
3163 value = atts[i++];
3164 if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
3165 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
3166 http = 1;
3167 else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
3168 content = value;
3169 att = atts[i++];
3170 }
3171 if ((http) && (content != NULL))
3172 htmlCheckEncoding(ctxt, content);
3173
3174}
3175
3176/**
3177 * htmlParseStartTag:
3178 * @ctxt: an HTML parser context
3179 *
3180 * parse a start of tag either for rule element or
3181 * EmptyElement. In both case we don't parse the tag closing chars.
3182 *
3183 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3184 *
3185 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3186 *
3187 * With namespace:
3188 *
3189 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3190 *
3191 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3192 *
3193 */
3194
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003195static void
Owen Taylor3473f882001-02-23 17:55:21 +00003196htmlParseStartTag(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003197 const xmlChar *name;
3198 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00003199 xmlChar *attvalue;
Daniel Veillardf403d292003-10-05 13:51:35 +00003200 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00003201 int nbatts = 0;
Daniel Veillardf403d292003-10-05 13:51:35 +00003202 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003203 int meta = 0;
3204 int i;
3205
3206 if (CUR != '<') return;
3207 NEXT;
3208
3209 GROW;
3210 name = htmlParseHTMLName(ctxt);
3211 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003212 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3213 "htmlParseStartTag: invalid element name\n",
3214 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003215 /* Dump the bogus tag like browsers do */
William M. Brack76e95df2003-10-18 16:20:14 +00003216 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
Owen Taylor3473f882001-02-23 17:55:21 +00003217 NEXT;
3218 return;
3219 }
3220 if (xmlStrEqual(name, BAD_CAST"meta"))
3221 meta = 1;
3222
3223 /*
3224 * Check for auto-closure of HTML elements.
3225 */
3226 htmlAutoClose(ctxt, name);
3227
3228 /*
3229 * Check for implied HTML elements.
3230 */
3231 htmlCheckImplied(ctxt, name);
3232
3233 /*
3234 * Avoid html at any level > 0, head at any level != 1
3235 * or any attempt to recurse body
3236 */
3237 if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003238 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3239 "htmlParseStartTag: misplaced <html> tag\n",
3240 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003241 return;
3242 }
3243 if ((ctxt->nameNr != 1) &&
3244 (xmlStrEqual(name, BAD_CAST"head"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003245 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3246 "htmlParseStartTag: misplaced <head> tag\n",
3247 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003248 return;
3249 }
3250 if (xmlStrEqual(name, BAD_CAST"body")) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003251 int indx;
3252 for (indx = 0;indx < ctxt->nameNr;indx++) {
3253 if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003254 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3255 "htmlParseStartTag: misplaced <body> tag\n",
3256 name, NULL);
Daniel Veillardc59d8262003-11-20 21:59:12 +00003257 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
3258 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003259 return;
3260 }
3261 }
3262 }
3263
3264 /*
3265 * Now parse the attributes, it ends up with the ending
3266 *
3267 * (S Attribute)* S?
3268 */
3269 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003270 while ((IS_CHAR_CH(CUR)) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003271 (CUR != '>') &&
3272 ((CUR != '/') || (NXT(1) != '>'))) {
3273 long cons = ctxt->nbChars;
3274
3275 GROW;
3276 attname = htmlParseAttribute(ctxt, &attvalue);
3277 if (attname != NULL) {
3278
3279 /*
3280 * Well formedness requires at most one declaration of an attribute
3281 */
3282 for (i = 0; i < nbatts;i += 2) {
3283 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003284 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
3285 "Attribute %s redefined\n", attname, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003286 if (attvalue != NULL)
3287 xmlFree(attvalue);
3288 goto failed;
3289 }
3290 }
3291
3292 /*
3293 * Add the pair to atts
3294 */
3295 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003296 maxatts = 22; /* allow for 10 attrs by default */
3297 atts = (const xmlChar **)
3298 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00003299 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003300 htmlErrMemory(ctxt, NULL);
3301 if (attvalue != NULL)
3302 xmlFree(attvalue);
3303 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003304 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003305 ctxt->atts = atts;
3306 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003307 } else if (nbatts + 4 > maxatts) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003308 const xmlChar **n;
3309
Owen Taylor3473f882001-02-23 17:55:21 +00003310 maxatts *= 2;
Daniel Veillardf403d292003-10-05 13:51:35 +00003311 n = (const xmlChar **) xmlRealloc((void *) atts,
3312 maxatts * sizeof(const xmlChar *));
3313 if (n == NULL) {
3314 htmlErrMemory(ctxt, NULL);
3315 if (attvalue != NULL)
3316 xmlFree(attvalue);
3317 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003318 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003319 atts = n;
3320 ctxt->atts = atts;
3321 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003322 }
3323 atts[nbatts++] = attname;
3324 atts[nbatts++] = attvalue;
3325 atts[nbatts] = NULL;
3326 atts[nbatts + 1] = NULL;
3327 }
3328 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003329 if (attvalue != NULL)
3330 xmlFree(attvalue);
Owen Taylor3473f882001-02-23 17:55:21 +00003331 /* Dump the bogus attribute string up to the next blank or
3332 * the end of the tag. */
William M. Brack76e95df2003-10-18 16:20:14 +00003333 while ((IS_CHAR_CH(CUR)) &&
3334 !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
Daniel Veillard34ba3872003-07-15 13:34:05 +00003335 ((CUR != '/') || (NXT(1) != '>')))
Owen Taylor3473f882001-02-23 17:55:21 +00003336 NEXT;
3337 }
3338
3339failed:
3340 SKIP_BLANKS;
3341 if (cons == ctxt->nbChars) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003342 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3343 "htmlParseStartTag: problem parsing attributes\n",
3344 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003345 break;
3346 }
3347 }
3348
3349 /*
3350 * Handle specific association to the META tag
3351 */
3352 if (meta)
3353 htmlCheckMeta(ctxt, atts);
3354
3355 /*
3356 * SAX: Start of Element !
3357 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003358 htmlnamePush(ctxt, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003359 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
3360 if (nbatts != 0)
3361 ctxt->sax->startElement(ctxt->userData, name, atts);
3362 else
3363 ctxt->sax->startElement(ctxt->userData, name, NULL);
3364 }
Owen Taylor3473f882001-02-23 17:55:21 +00003365
3366 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003367 for (i = 1;i < nbatts;i += 2) {
Owen Taylor3473f882001-02-23 17:55:21 +00003368 if (atts[i] != NULL)
3369 xmlFree((xmlChar *) atts[i]);
3370 }
Owen Taylor3473f882001-02-23 17:55:21 +00003371 }
Owen Taylor3473f882001-02-23 17:55:21 +00003372}
3373
3374/**
3375 * htmlParseEndTag:
3376 * @ctxt: an HTML parser context
3377 *
3378 * parse an end of tag
3379 *
3380 * [42] ETag ::= '</' Name S? '>'
3381 *
3382 * With namespace
3383 *
3384 * [NS 9] ETag ::= '</' QName S? '>'
Daniel Veillardf420ac52001-07-04 16:04:09 +00003385 *
3386 * Returns 1 if the current level should be closed.
Owen Taylor3473f882001-02-23 17:55:21 +00003387 */
3388
Daniel Veillardf420ac52001-07-04 16:04:09 +00003389static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003390htmlParseEndTag(htmlParserCtxtPtr ctxt)
3391{
3392 const xmlChar *name;
3393 const xmlChar *oldname;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003394 int i, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00003395
3396 if ((CUR != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003397 htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
3398 "htmlParseEndTag: '</' not found\n", NULL, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003399 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003400 }
3401 SKIP(2);
3402
3403 name = htmlParseHTMLName(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003404 if (name == NULL)
3405 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003406
3407 /*
3408 * We should definitely be at the ending "S? '>'" part
3409 */
3410 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003411 if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003412 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3413 "End tag : expected '>'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003414 } else
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003415 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003416
3417 /*
3418 * If the name read is not one of the element in the parsing stack
3419 * then return, it's just an error.
3420 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003421 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
3422 if (xmlStrEqual(name, ctxt->nameTab[i]))
3423 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003424 }
3425 if (i < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003426 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3427 "Unexpected end tag : %s\n", name, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003428 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003429 }
3430
3431
3432 /*
3433 * Check for auto-closure of HTML elements.
3434 */
3435
3436 htmlAutoCloseOnClose(ctxt, name);
3437
3438 /*
3439 * Well formedness constraints, opening and closing must match.
3440 * With the exception that the autoclose may have popped stuff out
3441 * of the stack.
3442 */
3443 if (!xmlStrEqual(name, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003444 if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003445 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3446 "Opening and ending tag mismatch: %s and %s\n",
3447 name, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003448 }
3449 }
3450
3451 /*
3452 * SAX: End of Tag
3453 */
3454 oldname = ctxt->name;
3455 if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003456 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3457 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003458 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003459 ret = 1;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003460 } else {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003461 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003462 }
3463
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003464 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003465}
3466
3467
3468/**
3469 * htmlParseReference:
3470 * @ctxt: an HTML parser context
3471 *
3472 * parse and handle entity references in content,
3473 * this will end-up in a call to character() since this is either a
3474 * CharRef, or a predefined entity.
3475 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003476static void
Owen Taylor3473f882001-02-23 17:55:21 +00003477htmlParseReference(htmlParserCtxtPtr ctxt) {
Daniel Veillardbb371292001-08-16 23:26:59 +00003478 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00003479 xmlChar out[6];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003480 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003481 if (CUR != '&') return;
3482
3483 if (NXT(1) == '#') {
3484 unsigned int c;
3485 int bits, i = 0;
3486
3487 c = htmlParseCharRef(ctxt);
3488 if (c == 0)
3489 return;
3490
3491 if (c < 0x80) { out[i++]= c; bits= -6; }
3492 else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3493 else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3494 else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3495
3496 for ( ; bits >= 0; bits-= 6) {
3497 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3498 }
3499 out[i] = 0;
3500
3501 htmlCheckParagraph(ctxt);
3502 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3503 ctxt->sax->characters(ctxt->userData, out, i);
3504 } else {
3505 ent = htmlParseEntityRef(ctxt, &name);
3506 if (name == NULL) {
3507 htmlCheckParagraph(ctxt);
3508 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3509 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3510 return;
3511 }
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003512 if ((ent == NULL) || !(ent->value > 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003513 htmlCheckParagraph(ctxt);
3514 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3515 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3516 ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3517 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3518 }
3519 } else {
3520 unsigned int c;
3521 int bits, i = 0;
3522
3523 c = ent->value;
3524 if (c < 0x80)
3525 { out[i++]= c; bits= -6; }
3526 else if (c < 0x800)
3527 { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3528 else if (c < 0x10000)
3529 { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3530 else
3531 { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3532
3533 for ( ; bits >= 0; bits-= 6) {
3534 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3535 }
3536 out[i] = 0;
3537
3538 htmlCheckParagraph(ctxt);
3539 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3540 ctxt->sax->characters(ctxt->userData, out, i);
3541 }
Owen Taylor3473f882001-02-23 17:55:21 +00003542 }
3543}
3544
3545/**
3546 * htmlParseContent:
3547 * @ctxt: an HTML parser context
3548 * @name: the node name
3549 *
3550 * Parse a content: comment, sub-element, reference or text.
3551 *
3552 */
3553
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003554static void
Owen Taylor3473f882001-02-23 17:55:21 +00003555htmlParseContent(htmlParserCtxtPtr ctxt) {
3556 xmlChar *currentNode;
3557 int depth;
3558
3559 currentNode = xmlStrdup(ctxt->name);
3560 depth = ctxt->nameNr;
3561 while (1) {
3562 long cons = ctxt->nbChars;
3563
3564 GROW;
3565 /*
3566 * Our tag or one of it's parent or children is ending.
3567 */
3568 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00003569 if (htmlParseEndTag(ctxt) &&
3570 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
3571 if (currentNode != NULL)
3572 xmlFree(currentNode);
3573 return;
3574 }
3575 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00003576 }
3577
3578 /*
3579 * Has this node been popped out during parsing of
3580 * the next element
3581 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00003582 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
3583 (!xmlStrEqual(currentNode, ctxt->name)))
3584 {
Owen Taylor3473f882001-02-23 17:55:21 +00003585 if (currentNode != NULL) xmlFree(currentNode);
3586 return;
3587 }
3588
Daniel Veillardf9533d12001-03-03 10:04:57 +00003589 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
3590 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00003591 /*
3592 * Handle SCRIPT/STYLE separately
3593 */
3594 htmlParseScript(ctxt);
3595 } else {
3596 /*
3597 * Sometimes DOCTYPE arrives in the middle of the document
3598 */
3599 if ((CUR == '<') && (NXT(1) == '!') &&
3600 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3601 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3602 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3603 (UPP(8) == 'E')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003604 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3605 "Misplaced DOCTYPE declaration\n",
3606 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003607 htmlParseDocTypeDecl(ctxt);
3608 }
3609
3610 /*
3611 * First case : a comment
3612 */
3613 if ((CUR == '<') && (NXT(1) == '!') &&
3614 (NXT(2) == '-') && (NXT(3) == '-')) {
3615 htmlParseComment(ctxt);
3616 }
3617
3618 /*
3619 * Second case : a sub-element.
3620 */
3621 else if (CUR == '<') {
3622 htmlParseElement(ctxt);
3623 }
3624
3625 /*
3626 * Third case : a reference. If if has not been resolved,
3627 * parsing returns it's Name, create the node
3628 */
3629 else if (CUR == '&') {
3630 htmlParseReference(ctxt);
3631 }
3632
3633 /*
3634 * Fourth : end of the resource
3635 */
3636 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003637 htmlAutoCloseOnEnd(ctxt);
3638 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003639 }
3640
3641 /*
3642 * Last case, text. Note that References are handled directly.
3643 */
3644 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003645 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003646 }
3647
3648 if (cons == ctxt->nbChars) {
3649 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003650 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3651 "detected an error in element content\n",
3652 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003653 }
3654 break;
3655 }
3656 }
3657 GROW;
3658 }
3659 if (currentNode != NULL) xmlFree(currentNode);
3660}
3661
3662/**
3663 * htmlParseElement:
3664 * @ctxt: an HTML parser context
3665 *
3666 * parse an HTML element, this is highly recursive
3667 *
3668 * [39] element ::= EmptyElemTag | STag content ETag
3669 *
3670 * [41] Attribute ::= Name Eq AttValue
3671 */
3672
3673void
3674htmlParseElement(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003675 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003676 xmlChar *currentNode = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00003677 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00003678 htmlParserNodeInfo node_info;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003679 const xmlChar *oldname;
Owen Taylor3473f882001-02-23 17:55:21 +00003680 int depth = ctxt->nameNr;
Daniel Veillard3fbe8e32001-10-06 13:30:33 +00003681 const xmlChar *oldptr;
Owen Taylor3473f882001-02-23 17:55:21 +00003682
3683 /* Capture start position */
3684 if (ctxt->record_info) {
3685 node_info.begin_pos = ctxt->input->consumed +
3686 (CUR_PTR - ctxt->input->base);
3687 node_info.begin_line = ctxt->input->line;
3688 }
3689
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003690 oldname = ctxt->name;
Owen Taylor3473f882001-02-23 17:55:21 +00003691 htmlParseStartTag(ctxt);
3692 name = ctxt->name;
Owen Taylor3473f882001-02-23 17:55:21 +00003693 if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
3694 (name == NULL)) {
3695 if (CUR == '>')
3696 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003697 return;
3698 }
Owen Taylor3473f882001-02-23 17:55:21 +00003699
3700 /*
3701 * Lookup the info for that element.
3702 */
3703 info = htmlTagLookup(name);
3704 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003705 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
3706 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003707 }
3708
3709 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003710 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00003711 */
3712 if ((CUR == '/') && (NXT(1) == '>')) {
3713 SKIP(2);
3714 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3715 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003716 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003717 return;
3718 }
3719
3720 if (CUR == '>') {
3721 NEXT;
3722 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003723 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3724 "Couldn't find end of Start Tag %s\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003725
3726 /*
3727 * end of parsing of this node.
3728 */
3729 if (xmlStrEqual(name, ctxt->name)) {
3730 nodePop(ctxt);
Daniel Veillardf403d292003-10-05 13:51:35 +00003731 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003732 }
3733
3734 /*
3735 * Capture end position and add node
3736 */
3737 if ( currentNode != NULL && ctxt->record_info ) {
3738 node_info.end_pos = ctxt->input->consumed +
3739 (CUR_PTR - ctxt->input->base);
3740 node_info.end_line = ctxt->input->line;
3741 node_info.node = ctxt->node;
3742 xmlParserAddNodeInfo(ctxt, &node_info);
3743 }
3744 return;
3745 }
3746
3747 /*
3748 * Check for an Empty Element from DTD definition
3749 */
3750 if ((info != NULL) && (info->empty)) {
3751 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3752 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003753 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003754 return;
3755 }
3756
3757 /*
3758 * Parse the content of the element:
3759 */
3760 currentNode = xmlStrdup(ctxt->name);
3761 depth = ctxt->nameNr;
William M. Brack76e95df2003-10-18 16:20:14 +00003762 while (IS_CHAR_CH(CUR)) {
William M. Brackd28e48a2001-09-23 01:55:08 +00003763 oldptr = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00003764 htmlParseContent(ctxt);
William M. Brackd28e48a2001-09-23 01:55:08 +00003765 if (oldptr==ctxt->input->cur) break;
Owen Taylor3473f882001-02-23 17:55:21 +00003766 if (ctxt->nameNr < depth) break;
3767 }
3768
Owen Taylor3473f882001-02-23 17:55:21 +00003769 /*
3770 * Capture end position and add node
3771 */
3772 if ( currentNode != NULL && ctxt->record_info ) {
3773 node_info.end_pos = ctxt->input->consumed +
3774 (CUR_PTR - ctxt->input->base);
3775 node_info.end_line = ctxt->input->line;
3776 node_info.node = ctxt->node;
3777 xmlParserAddNodeInfo(ctxt, &node_info);
3778 }
William M. Brack76e95df2003-10-18 16:20:14 +00003779 if (!IS_CHAR_CH(CUR)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003780 htmlAutoCloseOnEnd(ctxt);
3781 }
3782
Owen Taylor3473f882001-02-23 17:55:21 +00003783 if (currentNode != NULL)
3784 xmlFree(currentNode);
3785}
3786
3787/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003788 * htmlParseDocument:
Owen Taylor3473f882001-02-23 17:55:21 +00003789 * @ctxt: an HTML parser context
3790 *
3791 * parse an HTML document (and build a tree if using the standard SAX
3792 * interface).
3793 *
3794 * Returns 0, -1 in case of error. the parser context is augmented
3795 * as a result of the parsing.
3796 */
3797
Daniel Veillard1b31e4a2002-05-27 14:44:50 +00003798int
Owen Taylor3473f882001-02-23 17:55:21 +00003799htmlParseDocument(htmlParserCtxtPtr ctxt) {
3800 xmlDtdPtr dtd;
3801
Daniel Veillardd0463562001-10-13 09:15:48 +00003802 xmlInitParser();
3803
Owen Taylor3473f882001-02-23 17:55:21 +00003804 htmlDefaultSAXHandlerInit();
3805 ctxt->html = 1;
3806
3807 GROW;
3808 /*
3809 * SAX: beginning of the document processing.
3810 */
3811 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
3812 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
3813
3814 /*
3815 * Wipe out everything which is before the first '<'
3816 */
3817 SKIP_BLANKS;
3818 if (CUR == 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003819 htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
3820 "Document is empty\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003821 }
3822
3823 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
3824 ctxt->sax->startDocument(ctxt->userData);
3825
3826
3827 /*
3828 * Parse possible comments before any content
3829 */
3830 while ((CUR == '<') && (NXT(1) == '!') &&
3831 (NXT(2) == '-') && (NXT(3) == '-')) {
3832 htmlParseComment(ctxt);
3833 SKIP_BLANKS;
3834 }
3835
3836
3837 /*
3838 * Then possibly doc type declaration(s) and more Misc
3839 * (doctypedecl Misc*)?
3840 */
3841 if ((CUR == '<') && (NXT(1) == '!') &&
3842 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3843 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3844 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3845 (UPP(8) == 'E')) {
3846 htmlParseDocTypeDecl(ctxt);
3847 }
3848 SKIP_BLANKS;
3849
3850 /*
3851 * Parse possible comments before any content
3852 */
3853 while ((CUR == '<') && (NXT(1) == '!') &&
3854 (NXT(2) == '-') && (NXT(3) == '-')) {
3855 htmlParseComment(ctxt);
3856 SKIP_BLANKS;
3857 }
3858
3859 /*
3860 * Time to start parsing the tree itself
3861 */
3862 htmlParseContent(ctxt);
3863
3864 /*
3865 * autoclose
3866 */
3867 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003868 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003869
3870
3871 /*
3872 * SAX: end of the document processing.
3873 */
3874 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
3875 ctxt->sax->endDocument(ctxt->userData);
3876
3877 if (ctxt->myDoc != NULL) {
3878 dtd = xmlGetIntSubset(ctxt->myDoc);
3879 if (dtd == NULL)
3880 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00003881 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00003882 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
3883 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
3884 }
3885 if (! ctxt->wellFormed) return(-1);
3886 return(0);
3887}
3888
3889
3890/************************************************************************
3891 * *
3892 * Parser contexts handling *
3893 * *
3894 ************************************************************************/
3895
3896/**
William M. Brackedb65a72004-02-06 07:36:04 +00003897 * htmlInitParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00003898 * @ctxt: an HTML parser context
3899 *
3900 * Initialize a parser context
Daniel Veillardf403d292003-10-05 13:51:35 +00003901 *
3902 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00003903 */
3904
Daniel Veillardf403d292003-10-05 13:51:35 +00003905static int
Owen Taylor3473f882001-02-23 17:55:21 +00003906htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
3907{
3908 htmlSAXHandler *sax;
3909
Daniel Veillardf403d292003-10-05 13:51:35 +00003910 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003911 memset(ctxt, 0, sizeof(htmlParserCtxt));
3912
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003913 ctxt->dict = xmlDictCreate();
3914 if (ctxt->dict == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003915 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
3916 return(-1);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003917 }
Owen Taylor3473f882001-02-23 17:55:21 +00003918 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
3919 if (sax == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003920 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
3921 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003922 }
3923 else
3924 memset(sax, 0, sizeof(htmlSAXHandler));
3925
3926 /* Allocate the Input stack */
3927 ctxt->inputTab = (htmlParserInputPtr *)
3928 xmlMalloc(5 * sizeof(htmlParserInputPtr));
3929 if (ctxt->inputTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003930 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003931 ctxt->inputNr = 0;
3932 ctxt->inputMax = 0;
3933 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00003934 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003935 }
3936 ctxt->inputNr = 0;
3937 ctxt->inputMax = 5;
3938 ctxt->input = NULL;
3939 ctxt->version = NULL;
3940 ctxt->encoding = NULL;
3941 ctxt->standalone = -1;
3942 ctxt->instate = XML_PARSER_START;
3943
3944 /* Allocate the Node stack */
3945 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
3946 if (ctxt->nodeTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003947 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003948 ctxt->nodeNr = 0;
3949 ctxt->nodeMax = 0;
3950 ctxt->node = NULL;
3951 ctxt->inputNr = 0;
3952 ctxt->inputMax = 0;
3953 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00003954 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003955 }
3956 ctxt->nodeNr = 0;
3957 ctxt->nodeMax = 10;
3958 ctxt->node = NULL;
3959
3960 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003961 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00003962 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003963 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003964 ctxt->nameNr = 0;
3965 ctxt->nameMax = 10;
3966 ctxt->name = NULL;
3967 ctxt->nodeNr = 0;
3968 ctxt->nodeMax = 0;
3969 ctxt->node = NULL;
3970 ctxt->inputNr = 0;
3971 ctxt->inputMax = 0;
3972 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00003973 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003974 }
3975 ctxt->nameNr = 0;
3976 ctxt->nameMax = 10;
3977 ctxt->name = NULL;
3978
Daniel Veillard092643b2003-09-25 14:29:29 +00003979 if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
Owen Taylor3473f882001-02-23 17:55:21 +00003980 else {
3981 ctxt->sax = sax;
Daniel Veillard092643b2003-09-25 14:29:29 +00003982 memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +00003983 }
3984 ctxt->userData = ctxt;
3985 ctxt->myDoc = NULL;
3986 ctxt->wellFormed = 1;
3987 ctxt->replaceEntities = 0;
Daniel Veillard635ef722001-10-29 11:48:19 +00003988 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00003989 ctxt->html = 1;
William M. Brackedb65a72004-02-06 07:36:04 +00003990 ctxt->vctxt.userData = ctxt;
3991 ctxt->vctxt.error = xmlParserValidityError;
3992 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00003993 ctxt->record_info = 0;
3994 ctxt->validate = 0;
3995 ctxt->nbChars = 0;
3996 ctxt->checkIndex = 0;
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003997 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003998 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillardf403d292003-10-05 13:51:35 +00003999 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004000}
4001
4002/**
4003 * htmlFreeParserCtxt:
4004 * @ctxt: an HTML parser context
4005 *
4006 * Free all the memory used by a parser context. However the parsed
4007 * document in ctxt->myDoc is not freed.
4008 */
4009
4010void
4011htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
4012{
4013 xmlFreeParserCtxt(ctxt);
4014}
4015
4016/**
Daniel Veillard1d995272002-07-22 16:43:32 +00004017 * htmlNewParserCtxt:
4018 *
4019 * Allocate and initialize a new parser context.
4020 *
4021 * Returns the xmlParserCtxtPtr or NULL
4022 */
4023
4024static htmlParserCtxtPtr
4025htmlNewParserCtxt(void)
4026{
4027 xmlParserCtxtPtr ctxt;
4028
4029 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
4030 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004031 htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
Daniel Veillard1d995272002-07-22 16:43:32 +00004032 return(NULL);
4033 }
4034 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillardf403d292003-10-05 13:51:35 +00004035 if (htmlInitParserCtxt(ctxt) < 0) {
4036 htmlFreeParserCtxt(ctxt);
4037 return(NULL);
4038 }
Daniel Veillard1d995272002-07-22 16:43:32 +00004039 return(ctxt);
4040}
4041
4042/**
4043 * htmlCreateMemoryParserCtxt:
4044 * @buffer: a pointer to a char array
4045 * @size: the size of the array
4046 *
4047 * Create a parser context for an HTML in-memory document.
4048 *
4049 * Returns the new parser context or NULL
4050 */
Daniel Veillard02ea1412003-04-09 12:08:47 +00004051htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004052htmlCreateMemoryParserCtxt(const char *buffer, int size) {
4053 xmlParserCtxtPtr ctxt;
4054 xmlParserInputPtr input;
4055 xmlParserInputBufferPtr buf;
4056
4057 if (buffer == NULL)
4058 return(NULL);
4059 if (size <= 0)
4060 return(NULL);
4061
4062 ctxt = htmlNewParserCtxt();
4063 if (ctxt == NULL)
4064 return(NULL);
4065
4066 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
4067 if (buf == NULL) return(NULL);
4068
4069 input = xmlNewInputStream(ctxt);
4070 if (input == NULL) {
4071 xmlFreeParserCtxt(ctxt);
4072 return(NULL);
4073 }
4074
4075 input->filename = NULL;
4076 input->buf = buf;
4077 input->base = input->buf->buffer->content;
4078 input->cur = input->buf->buffer->content;
4079 input->end = &input->buf->buffer->content[input->buf->buffer->use];
4080
4081 inputPush(ctxt, input);
4082 return(ctxt);
4083}
4084
4085/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004086 * htmlCreateDocParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004087 * @cur: a pointer to an array of xmlChar
4088 * @encoding: a free form C string describing the HTML document encoding, or NULL
4089 *
4090 * Create a parser context for an HTML document.
4091 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004092 * TODO: check the need to add encoding handling there
4093 *
Owen Taylor3473f882001-02-23 17:55:21 +00004094 * Returns the new parser context or NULL
4095 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004096static htmlParserCtxtPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004097htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) {
Daniel Veillard1d995272002-07-22 16:43:32 +00004098 int len;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004099 htmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004100
Daniel Veillard1d995272002-07-22 16:43:32 +00004101 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004102 return(NULL);
Daniel Veillard1d995272002-07-22 16:43:32 +00004103 len = xmlStrlen(cur);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004104 ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
4105
4106 if (encoding != NULL) {
4107 xmlCharEncoding enc;
4108 xmlCharEncodingHandlerPtr handler;
4109
4110 if (ctxt->input->encoding != NULL)
4111 xmlFree((xmlChar *) ctxt->input->encoding);
Daniel Veillarde8ed6202003-08-14 23:39:01 +00004112 ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004113
4114 enc = xmlParseCharEncoding(encoding);
4115 /*
4116 * registered set of known encodings
4117 */
4118 if (enc != XML_CHAR_ENCODING_ERROR) {
4119 xmlSwitchEncoding(ctxt, enc);
4120 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004121 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4122 "Unsupported encoding %s\n",
4123 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004124 }
4125 } else {
4126 /*
4127 * fallback for unknown encodings
4128 */
4129 handler = xmlFindCharEncodingHandler((const char *) encoding);
4130 if (handler != NULL) {
4131 xmlSwitchToEncoding(ctxt, handler);
4132 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004133 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4134 "Unsupported encoding %s\n",
4135 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004136 }
4137 }
4138 }
4139 return(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004140}
4141
Daniel Veillard73b013f2003-09-30 12:36:01 +00004142#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004143/************************************************************************
4144 * *
4145 * Progressive parsing interfaces *
4146 * *
4147 ************************************************************************/
4148
4149/**
4150 * htmlParseLookupSequence:
4151 * @ctxt: an HTML parser context
4152 * @first: the first char to lookup
4153 * @next: the next char to lookup or zero
4154 * @third: the next char to lookup or zero
William M. Brack78637da2003-07-31 14:47:38 +00004155 * @comment: flag to force checking inside comments
Owen Taylor3473f882001-02-23 17:55:21 +00004156 *
4157 * Try to find if a sequence (first, next, third) or just (first next) or
4158 * (first) is available in the input stream.
4159 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4160 * to avoid rescanning sequences of bytes, it DOES change the state of the
4161 * parser, do not use liberally.
4162 * This is basically similar to xmlParseLookupSequence()
4163 *
4164 * Returns the index to the current parsing point if the full sequence
4165 * is available, -1 otherwise.
4166 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004167static int
Owen Taylor3473f882001-02-23 17:55:21 +00004168htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
William M. Brackc1939562003-08-05 15:52:22 +00004169 xmlChar next, xmlChar third, int iscomment) {
Owen Taylor3473f882001-02-23 17:55:21 +00004170 int base, len;
4171 htmlParserInputPtr in;
4172 const xmlChar *buf;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004173 int incomment = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004174
4175 in = ctxt->input;
4176 if (in == NULL) return(-1);
4177 base = in->cur - in->base;
4178 if (base < 0) return(-1);
4179 if (ctxt->checkIndex > base)
4180 base = ctxt->checkIndex;
4181 if (in->buf == NULL) {
4182 buf = in->base;
4183 len = in->length;
4184 } else {
4185 buf = in->buf->buffer->content;
4186 len = in->buf->buffer->use;
4187 }
4188 /* take into account the sequence length */
4189 if (third) len -= 2;
4190 else if (next) len --;
4191 for (;base < len;base++) {
William M. Brackc1939562003-08-05 15:52:22 +00004192 if (!incomment && (base + 4 < len) && !iscomment) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00004193 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
4194 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
4195 incomment = 1;
Daniel Veillard97e01882003-07-30 18:59:19 +00004196 /* do not increment past <! - some people use <!--> */
4197 base += 2;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004198 }
Daniel Veillardc1f78342001-11-10 11:43:05 +00004199 }
4200 if (incomment) {
William M. Brack4a557d92003-07-29 04:28:04 +00004201 if (base + 3 > len)
Daniel Veillardc1f78342001-11-10 11:43:05 +00004202 return(-1);
4203 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
4204 (buf[base + 2] == '>')) {
4205 incomment = 0;
4206 base += 2;
4207 }
4208 continue;
4209 }
Owen Taylor3473f882001-02-23 17:55:21 +00004210 if (buf[base] == first) {
4211 if (third != 0) {
4212 if ((buf[base + 1] != next) ||
4213 (buf[base + 2] != third)) continue;
4214 } else if (next != 0) {
4215 if (buf[base + 1] != next) continue;
4216 }
4217 ctxt->checkIndex = 0;
4218#ifdef DEBUG_PUSH
4219 if (next == 0)
4220 xmlGenericError(xmlGenericErrorContext,
4221 "HPP: lookup '%c' found at %d\n",
4222 first, base);
4223 else if (third == 0)
4224 xmlGenericError(xmlGenericErrorContext,
4225 "HPP: lookup '%c%c' found at %d\n",
4226 first, next, base);
4227 else
4228 xmlGenericError(xmlGenericErrorContext,
4229 "HPP: lookup '%c%c%c' found at %d\n",
4230 first, next, third, base);
4231#endif
4232 return(base - (in->cur - in->base));
4233 }
4234 }
4235 ctxt->checkIndex = base;
4236#ifdef DEBUG_PUSH
4237 if (next == 0)
4238 xmlGenericError(xmlGenericErrorContext,
4239 "HPP: lookup '%c' failed\n", first);
4240 else if (third == 0)
4241 xmlGenericError(xmlGenericErrorContext,
4242 "HPP: lookup '%c%c' failed\n", first, next);
4243 else
4244 xmlGenericError(xmlGenericErrorContext,
4245 "HPP: lookup '%c%c%c' failed\n", first, next, third);
4246#endif
4247 return(-1);
4248}
4249
4250/**
4251 * htmlParseTryOrFinish:
4252 * @ctxt: an HTML parser context
4253 * @terminate: last chunk indicator
4254 *
4255 * Try to progress on parsing
4256 *
4257 * Returns zero if no parsing was possible
4258 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004259static int
Owen Taylor3473f882001-02-23 17:55:21 +00004260htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
4261 int ret = 0;
4262 htmlParserInputPtr in;
4263 int avail = 0;
4264 xmlChar cur, next;
4265
4266#ifdef DEBUG_PUSH
4267 switch (ctxt->instate) {
4268 case XML_PARSER_EOF:
4269 xmlGenericError(xmlGenericErrorContext,
4270 "HPP: try EOF\n"); break;
4271 case XML_PARSER_START:
4272 xmlGenericError(xmlGenericErrorContext,
4273 "HPP: try START\n"); break;
4274 case XML_PARSER_MISC:
4275 xmlGenericError(xmlGenericErrorContext,
4276 "HPP: try MISC\n");break;
4277 case XML_PARSER_COMMENT:
4278 xmlGenericError(xmlGenericErrorContext,
4279 "HPP: try COMMENT\n");break;
4280 case XML_PARSER_PROLOG:
4281 xmlGenericError(xmlGenericErrorContext,
4282 "HPP: try PROLOG\n");break;
4283 case XML_PARSER_START_TAG:
4284 xmlGenericError(xmlGenericErrorContext,
4285 "HPP: try START_TAG\n");break;
4286 case XML_PARSER_CONTENT:
4287 xmlGenericError(xmlGenericErrorContext,
4288 "HPP: try CONTENT\n");break;
4289 case XML_PARSER_CDATA_SECTION:
4290 xmlGenericError(xmlGenericErrorContext,
4291 "HPP: try CDATA_SECTION\n");break;
4292 case XML_PARSER_END_TAG:
4293 xmlGenericError(xmlGenericErrorContext,
4294 "HPP: try END_TAG\n");break;
4295 case XML_PARSER_ENTITY_DECL:
4296 xmlGenericError(xmlGenericErrorContext,
4297 "HPP: try ENTITY_DECL\n");break;
4298 case XML_PARSER_ENTITY_VALUE:
4299 xmlGenericError(xmlGenericErrorContext,
4300 "HPP: try ENTITY_VALUE\n");break;
4301 case XML_PARSER_ATTRIBUTE_VALUE:
4302 xmlGenericError(xmlGenericErrorContext,
4303 "HPP: try ATTRIBUTE_VALUE\n");break;
4304 case XML_PARSER_DTD:
4305 xmlGenericError(xmlGenericErrorContext,
4306 "HPP: try DTD\n");break;
4307 case XML_PARSER_EPILOG:
4308 xmlGenericError(xmlGenericErrorContext,
4309 "HPP: try EPILOG\n");break;
4310 case XML_PARSER_PI:
4311 xmlGenericError(xmlGenericErrorContext,
4312 "HPP: try PI\n");break;
4313 case XML_PARSER_SYSTEM_LITERAL:
4314 xmlGenericError(xmlGenericErrorContext,
4315 "HPP: try SYSTEM_LITERAL\n");break;
4316 }
4317#endif
4318
4319 while (1) {
4320
4321 in = ctxt->input;
4322 if (in == NULL) break;
4323 if (in->buf == NULL)
4324 avail = in->length - (in->cur - in->base);
4325 else
4326 avail = in->buf->buffer->use - (in->cur - in->base);
4327 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004328 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004329 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4330 /*
4331 * SAX: end of the document processing.
4332 */
4333 ctxt->instate = XML_PARSER_EOF;
4334 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4335 ctxt->sax->endDocument(ctxt->userData);
4336 }
4337 }
4338 if (avail < 1)
4339 goto done;
Daniel Veillard45269b82003-04-22 13:21:57 +00004340 cur = in->cur[0];
4341 if (cur == 0) {
4342 SKIP(1);
4343 continue;
4344 }
4345
Owen Taylor3473f882001-02-23 17:55:21 +00004346 switch (ctxt->instate) {
4347 case XML_PARSER_EOF:
4348 /*
4349 * Document parsing is done !
4350 */
4351 goto done;
4352 case XML_PARSER_START:
4353 /*
4354 * Very first chars read from the document flow.
4355 */
4356 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004357 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004358 SKIP_BLANKS;
4359 if (in->buf == NULL)
4360 avail = in->length - (in->cur - in->base);
4361 else
4362 avail = in->buf->buffer->use - (in->cur - in->base);
4363 }
4364 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4365 ctxt->sax->setDocumentLocator(ctxt->userData,
4366 &xmlDefaultSAXLocator);
4367 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4368 (!ctxt->disableSAX))
4369 ctxt->sax->startDocument(ctxt->userData);
4370
4371 cur = in->cur[0];
4372 next = in->cur[1];
4373 if ((cur == '<') && (next == '!') &&
4374 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4375 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4376 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4377 (UPP(8) == 'E')) {
4378 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004379 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004380 goto done;
4381#ifdef DEBUG_PUSH
4382 xmlGenericError(xmlGenericErrorContext,
4383 "HPP: Parsing internal subset\n");
4384#endif
4385 htmlParseDocTypeDecl(ctxt);
4386 ctxt->instate = XML_PARSER_PROLOG;
4387#ifdef DEBUG_PUSH
4388 xmlGenericError(xmlGenericErrorContext,
4389 "HPP: entering PROLOG\n");
4390#endif
4391 } else {
4392 ctxt->instate = XML_PARSER_MISC;
4393 }
4394#ifdef DEBUG_PUSH
4395 xmlGenericError(xmlGenericErrorContext,
4396 "HPP: entering MISC\n");
4397#endif
4398 break;
4399 case XML_PARSER_MISC:
4400 SKIP_BLANKS;
4401 if (in->buf == NULL)
4402 avail = in->length - (in->cur - in->base);
4403 else
4404 avail = in->buf->buffer->use - (in->cur - in->base);
4405 if (avail < 2)
4406 goto done;
4407 cur = in->cur[0];
4408 next = in->cur[1];
4409 if ((cur == '<') && (next == '!') &&
4410 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4411 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004412 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004413 goto done;
4414#ifdef DEBUG_PUSH
4415 xmlGenericError(xmlGenericErrorContext,
4416 "HPP: Parsing Comment\n");
4417#endif
4418 htmlParseComment(ctxt);
4419 ctxt->instate = XML_PARSER_MISC;
4420 } else if ((cur == '<') && (next == '!') &&
4421 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4422 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4423 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4424 (UPP(8) == 'E')) {
4425 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004426 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004427 goto done;
4428#ifdef DEBUG_PUSH
4429 xmlGenericError(xmlGenericErrorContext,
4430 "HPP: Parsing internal subset\n");
4431#endif
4432 htmlParseDocTypeDecl(ctxt);
4433 ctxt->instate = XML_PARSER_PROLOG;
4434#ifdef DEBUG_PUSH
4435 xmlGenericError(xmlGenericErrorContext,
4436 "HPP: entering PROLOG\n");
4437#endif
4438 } else if ((cur == '<') && (next == '!') &&
4439 (avail < 9)) {
4440 goto done;
4441 } else {
4442 ctxt->instate = XML_PARSER_START_TAG;
4443#ifdef DEBUG_PUSH
4444 xmlGenericError(xmlGenericErrorContext,
4445 "HPP: entering START_TAG\n");
4446#endif
4447 }
4448 break;
4449 case XML_PARSER_PROLOG:
4450 SKIP_BLANKS;
4451 if (in->buf == NULL)
4452 avail = in->length - (in->cur - in->base);
4453 else
4454 avail = in->buf->buffer->use - (in->cur - in->base);
4455 if (avail < 2)
4456 goto done;
4457 cur = in->cur[0];
4458 next = in->cur[1];
4459 if ((cur == '<') && (next == '!') &&
4460 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4461 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004462 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004463 goto done;
4464#ifdef DEBUG_PUSH
4465 xmlGenericError(xmlGenericErrorContext,
4466 "HPP: Parsing Comment\n");
4467#endif
4468 htmlParseComment(ctxt);
4469 ctxt->instate = XML_PARSER_PROLOG;
4470 } else if ((cur == '<') && (next == '!') &&
4471 (avail < 4)) {
4472 goto done;
4473 } else {
4474 ctxt->instate = XML_PARSER_START_TAG;
4475#ifdef DEBUG_PUSH
4476 xmlGenericError(xmlGenericErrorContext,
4477 "HPP: entering START_TAG\n");
4478#endif
4479 }
4480 break;
4481 case XML_PARSER_EPILOG:
4482 if (in->buf == NULL)
4483 avail = in->length - (in->cur - in->base);
4484 else
4485 avail = in->buf->buffer->use - (in->cur - in->base);
4486 if (avail < 1)
4487 goto done;
4488 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004489 if (IS_BLANK_CH(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004490 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004491 goto done;
4492 }
4493 if (avail < 2)
4494 goto done;
4495 next = in->cur[1];
4496 if ((cur == '<') && (next == '!') &&
4497 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4498 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004499 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004500 goto done;
4501#ifdef DEBUG_PUSH
4502 xmlGenericError(xmlGenericErrorContext,
4503 "HPP: Parsing Comment\n");
4504#endif
4505 htmlParseComment(ctxt);
4506 ctxt->instate = XML_PARSER_EPILOG;
4507 } else if ((cur == '<') && (next == '!') &&
4508 (avail < 4)) {
4509 goto done;
4510 } else {
4511 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004512 ctxt->wellFormed = 0;
4513 ctxt->instate = XML_PARSER_EOF;
4514#ifdef DEBUG_PUSH
4515 xmlGenericError(xmlGenericErrorContext,
4516 "HPP: entering EOF\n");
4517#endif
4518 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4519 ctxt->sax->endDocument(ctxt->userData);
4520 goto done;
4521 }
4522 break;
4523 case XML_PARSER_START_TAG: {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004524 const xmlChar *name, *oldname;
Owen Taylor3473f882001-02-23 17:55:21 +00004525 int depth = ctxt->nameNr;
Daniel Veillardbb371292001-08-16 23:26:59 +00004526 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00004527
4528 if (avail < 2)
4529 goto done;
4530 cur = in->cur[0];
4531 if (cur != '<') {
4532 ctxt->instate = XML_PARSER_CONTENT;
4533#ifdef DEBUG_PUSH
4534 xmlGenericError(xmlGenericErrorContext,
4535 "HPP: entering CONTENT\n");
4536#endif
4537 break;
4538 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004539 if (in->cur[1] == '/') {
4540 ctxt->instate = XML_PARSER_END_TAG;
4541 ctxt->checkIndex = 0;
4542#ifdef DEBUG_PUSH
4543 xmlGenericError(xmlGenericErrorContext,
4544 "HPP: entering END_TAG\n");
4545#endif
4546 break;
4547 }
Owen Taylor3473f882001-02-23 17:55:21 +00004548 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004549 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004550 goto done;
4551
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004552 oldname = ctxt->name;
Owen Taylor3473f882001-02-23 17:55:21 +00004553 htmlParseStartTag(ctxt);
4554 name = ctxt->name;
Owen Taylor3473f882001-02-23 17:55:21 +00004555 if (((depth == ctxt->nameNr) &&
4556 (xmlStrEqual(oldname, ctxt->name))) ||
4557 (name == NULL)) {
4558 if (CUR == '>')
4559 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00004560 break;
4561 }
Owen Taylor3473f882001-02-23 17:55:21 +00004562
4563 /*
4564 * Lookup the info for that element.
4565 */
4566 info = htmlTagLookup(name);
4567 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004568 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4569 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004570 }
4571
4572 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004573 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00004574 */
4575 if ((CUR == '/') && (NXT(1) == '>')) {
4576 SKIP(2);
4577 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4578 ctxt->sax->endElement(ctxt->userData, name);
4579 oldname = htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004580 ctxt->instate = XML_PARSER_CONTENT;
4581#ifdef DEBUG_PUSH
4582 xmlGenericError(xmlGenericErrorContext,
4583 "HPP: entering CONTENT\n");
4584#endif
4585 break;
4586 }
4587
4588 if (CUR == '>') {
4589 NEXT;
4590 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004591 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4592 "Couldn't find end of Start Tag %s\n",
4593 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004594
4595 /*
4596 * end of parsing of this node.
4597 */
4598 if (xmlStrEqual(name, ctxt->name)) {
4599 nodePop(ctxt);
4600 oldname = htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004601 }
4602
4603 ctxt->instate = XML_PARSER_CONTENT;
4604#ifdef DEBUG_PUSH
4605 xmlGenericError(xmlGenericErrorContext,
4606 "HPP: entering CONTENT\n");
4607#endif
4608 break;
4609 }
4610
4611 /*
4612 * Check for an Empty Element from DTD definition
4613 */
4614 if ((info != NULL) && (info->empty)) {
4615 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4616 ctxt->sax->endElement(ctxt->userData, name);
4617 oldname = htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004618 }
4619 ctxt->instate = XML_PARSER_CONTENT;
4620#ifdef DEBUG_PUSH
4621 xmlGenericError(xmlGenericErrorContext,
4622 "HPP: entering CONTENT\n");
4623#endif
4624 break;
4625 }
4626 case XML_PARSER_CONTENT: {
4627 long cons;
4628 /*
4629 * Handle preparsed entities and charRef
4630 */
4631 if (ctxt->token != 0) {
4632 xmlChar chr[2] = { 0 , 0 } ;
4633
4634 chr[0] = (xmlChar) ctxt->token;
4635 htmlCheckParagraph(ctxt);
4636 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4637 ctxt->sax->characters(ctxt->userData, chr, 1);
4638 ctxt->token = 0;
4639 ctxt->checkIndex = 0;
4640 }
4641 if ((avail == 1) && (terminate)) {
4642 cur = in->cur[0];
4643 if ((cur != '<') && (cur != '&')) {
4644 if (ctxt->sax != NULL) {
William M. Brack76e95df2003-10-18 16:20:14 +00004645 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004646 if (ctxt->sax->ignorableWhitespace != NULL)
4647 ctxt->sax->ignorableWhitespace(
4648 ctxt->userData, &cur, 1);
4649 } else {
4650 htmlCheckParagraph(ctxt);
4651 if (ctxt->sax->characters != NULL)
4652 ctxt->sax->characters(
4653 ctxt->userData, &cur, 1);
4654 }
4655 }
4656 ctxt->token = 0;
4657 ctxt->checkIndex = 0;
Daniel Veillardbc6e1a32002-11-18 15:07:25 +00004658 in->cur++;
William M. Brack1633d182001-10-05 15:41:19 +00004659 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004660 }
Owen Taylor3473f882001-02-23 17:55:21 +00004661 }
4662 if (avail < 2)
4663 goto done;
4664 cur = in->cur[0];
4665 next = in->cur[1];
4666 cons = ctxt->nbChars;
4667 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
4668 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
4669 /*
4670 * Handle SCRIPT/STYLE separately
4671 */
4672 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004673 (htmlParseLookupSequence(ctxt, '<', '/', 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004674 goto done;
4675 htmlParseScript(ctxt);
4676 if ((cur == '<') && (next == '/')) {
4677 ctxt->instate = XML_PARSER_END_TAG;
4678 ctxt->checkIndex = 0;
4679#ifdef DEBUG_PUSH
4680 xmlGenericError(xmlGenericErrorContext,
4681 "HPP: entering END_TAG\n");
4682#endif
4683 break;
4684 }
4685 } else {
4686 /*
4687 * Sometimes DOCTYPE arrives in the middle of the document
4688 */
4689 if ((cur == '<') && (next == '!') &&
4690 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4691 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4692 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4693 (UPP(8) == 'E')) {
4694 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004695 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004696 goto done;
Daniel Veillardf403d292003-10-05 13:51:35 +00004697 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4698 "Misplaced DOCTYPE declaration\n",
4699 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004700 htmlParseDocTypeDecl(ctxt);
4701 } else if ((cur == '<') && (next == '!') &&
4702 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4703 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004704 (htmlParseLookupSequence(
4705 ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004706 goto done;
4707#ifdef DEBUG_PUSH
4708 xmlGenericError(xmlGenericErrorContext,
4709 "HPP: Parsing Comment\n");
4710#endif
4711 htmlParseComment(ctxt);
4712 ctxt->instate = XML_PARSER_CONTENT;
4713 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
4714 goto done;
4715 } else if ((cur == '<') && (next == '/')) {
4716 ctxt->instate = XML_PARSER_END_TAG;
4717 ctxt->checkIndex = 0;
4718#ifdef DEBUG_PUSH
4719 xmlGenericError(xmlGenericErrorContext,
4720 "HPP: entering END_TAG\n");
4721#endif
4722 break;
4723 } else if (cur == '<') {
4724 ctxt->instate = XML_PARSER_START_TAG;
4725 ctxt->checkIndex = 0;
4726#ifdef DEBUG_PUSH
4727 xmlGenericError(xmlGenericErrorContext,
4728 "HPP: entering START_TAG\n");
4729#endif
4730 break;
4731 } else if (cur == '&') {
4732 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004733 (htmlParseLookupSequence(ctxt, ';', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004734 goto done;
4735#ifdef DEBUG_PUSH
4736 xmlGenericError(xmlGenericErrorContext,
4737 "HPP: Parsing Reference\n");
4738#endif
4739 /* TODO: check generation of subtrees if noent !!! */
4740 htmlParseReference(ctxt);
4741 } else {
Daniel Veillard14f752c2003-08-09 11:44:50 +00004742 /*
4743 * check that the text sequence is complete
4744 * before handing out the data to the parser
4745 * to avoid problems with erroneous end of
4746 * data detection.
Owen Taylor3473f882001-02-23 17:55:21 +00004747 */
Daniel Veillard14f752c2003-08-09 11:44:50 +00004748 if ((!terminate) &&
4749 (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
4750 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00004751 ctxt->checkIndex = 0;
4752#ifdef DEBUG_PUSH
4753 xmlGenericError(xmlGenericErrorContext,
4754 "HPP: Parsing char data\n");
4755#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004756 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004757 }
4758 }
4759 if (cons == ctxt->nbChars) {
4760 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004761 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4762 "detected an error in element content\n",
4763 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004764 }
4765 NEXT;
4766 break;
4767 }
4768
4769 break;
4770 }
4771 case XML_PARSER_END_TAG:
4772 if (avail < 2)
4773 goto done;
4774 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004775 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004776 goto done;
4777 htmlParseEndTag(ctxt);
4778 if (ctxt->nameNr == 0) {
4779 ctxt->instate = XML_PARSER_EPILOG;
4780 } else {
4781 ctxt->instate = XML_PARSER_CONTENT;
4782 }
4783 ctxt->checkIndex = 0;
4784#ifdef DEBUG_PUSH
4785 xmlGenericError(xmlGenericErrorContext,
4786 "HPP: entering CONTENT\n");
4787#endif
4788 break;
4789 case XML_PARSER_CDATA_SECTION:
Daniel Veillardf403d292003-10-05 13:51:35 +00004790 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4791 "HPP: internal error, state == CDATA\n",
4792 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004793 ctxt->instate = XML_PARSER_CONTENT;
4794 ctxt->checkIndex = 0;
4795#ifdef DEBUG_PUSH
4796 xmlGenericError(xmlGenericErrorContext,
4797 "HPP: entering CONTENT\n");
4798#endif
4799 break;
4800 case XML_PARSER_DTD:
Daniel Veillardf403d292003-10-05 13:51:35 +00004801 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4802 "HPP: internal error, state == DTD\n",
4803 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004804 ctxt->instate = XML_PARSER_CONTENT;
4805 ctxt->checkIndex = 0;
4806#ifdef DEBUG_PUSH
4807 xmlGenericError(xmlGenericErrorContext,
4808 "HPP: entering CONTENT\n");
4809#endif
4810 break;
4811 case XML_PARSER_COMMENT:
Daniel Veillardf403d292003-10-05 13:51:35 +00004812 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4813 "HPP: internal error, state == COMMENT\n",
4814 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004815 ctxt->instate = XML_PARSER_CONTENT;
4816 ctxt->checkIndex = 0;
4817#ifdef DEBUG_PUSH
4818 xmlGenericError(xmlGenericErrorContext,
4819 "HPP: entering CONTENT\n");
4820#endif
4821 break;
4822 case XML_PARSER_PI:
Daniel Veillardf403d292003-10-05 13:51:35 +00004823 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4824 "HPP: internal error, state == PI\n",
4825 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004826 ctxt->instate = XML_PARSER_CONTENT;
4827 ctxt->checkIndex = 0;
4828#ifdef DEBUG_PUSH
4829 xmlGenericError(xmlGenericErrorContext,
4830 "HPP: entering CONTENT\n");
4831#endif
4832 break;
4833 case XML_PARSER_ENTITY_DECL:
Daniel Veillardf403d292003-10-05 13:51:35 +00004834 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4835 "HPP: internal error, state == ENTITY_DECL\n",
4836 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004837 ctxt->instate = XML_PARSER_CONTENT;
4838 ctxt->checkIndex = 0;
4839#ifdef DEBUG_PUSH
4840 xmlGenericError(xmlGenericErrorContext,
4841 "HPP: entering CONTENT\n");
4842#endif
4843 break;
4844 case XML_PARSER_ENTITY_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00004845 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4846 "HPP: internal error, state == ENTITY_VALUE\n",
4847 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004848 ctxt->instate = XML_PARSER_CONTENT;
4849 ctxt->checkIndex = 0;
4850#ifdef DEBUG_PUSH
4851 xmlGenericError(xmlGenericErrorContext,
4852 "HPP: entering DTD\n");
4853#endif
4854 break;
4855 case XML_PARSER_ATTRIBUTE_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00004856 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4857 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
4858 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004859 ctxt->instate = XML_PARSER_START_TAG;
4860 ctxt->checkIndex = 0;
4861#ifdef DEBUG_PUSH
4862 xmlGenericError(xmlGenericErrorContext,
4863 "HPP: entering START_TAG\n");
4864#endif
4865 break;
4866 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00004867 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4868 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
4869 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004870 ctxt->instate = XML_PARSER_CONTENT;
4871 ctxt->checkIndex = 0;
4872#ifdef DEBUG_PUSH
4873 xmlGenericError(xmlGenericErrorContext,
4874 "HPP: entering CONTENT\n");
4875#endif
4876 break;
4877 case XML_PARSER_IGNORE:
Daniel Veillardf403d292003-10-05 13:51:35 +00004878 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4879 "HPP: internal error, state == XML_PARSER_IGNORE\n",
4880 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004881 ctxt->instate = XML_PARSER_CONTENT;
4882 ctxt->checkIndex = 0;
4883#ifdef DEBUG_PUSH
4884 xmlGenericError(xmlGenericErrorContext,
4885 "HPP: entering CONTENT\n");
4886#endif
4887 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00004888 case XML_PARSER_PUBLIC_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00004889 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4890 "HPP: internal error, state == XML_PARSER_LITERAL\n",
4891 NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00004892 ctxt->instate = XML_PARSER_CONTENT;
4893 ctxt->checkIndex = 0;
4894#ifdef DEBUG_PUSH
4895 xmlGenericError(xmlGenericErrorContext,
4896 "HPP: entering CONTENT\n");
4897#endif
4898 break;
4899
Owen Taylor3473f882001-02-23 17:55:21 +00004900 }
4901 }
4902done:
4903 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004904 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004905 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4906 /*
4907 * SAX: end of the document processing.
4908 */
4909 ctxt->instate = XML_PARSER_EOF;
4910 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4911 ctxt->sax->endDocument(ctxt->userData);
4912 }
4913 }
4914 if ((ctxt->myDoc != NULL) &&
4915 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
4916 (ctxt->instate == XML_PARSER_EPILOG))) {
4917 xmlDtdPtr dtd;
4918 dtd = xmlGetIntSubset(ctxt->myDoc);
4919 if (dtd == NULL)
4920 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00004921 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00004922 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4923 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4924 }
4925#ifdef DEBUG_PUSH
4926 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
4927#endif
4928 return(ret);
4929}
4930
4931/**
Owen Taylor3473f882001-02-23 17:55:21 +00004932 * htmlParseChunk:
Daniel Veillardf403d292003-10-05 13:51:35 +00004933 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00004934 * @chunk: an char array
4935 * @size: the size in byte of the chunk
4936 * @terminate: last chunk indicator
4937 *
4938 * Parse a Chunk of memory
4939 *
4940 * Returns zero if no error, the xmlParserErrors otherwise.
4941 */
4942int
4943htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
4944 int terminate) {
4945 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
4946 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
4947 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
4948 int cur = ctxt->input->cur - ctxt->input->base;
4949
4950 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
4951 ctxt->input->base = ctxt->input->buf->buffer->content + base;
4952 ctxt->input->cur = ctxt->input->base + cur;
4953#ifdef DEBUG_PUSH
4954 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
4955#endif
4956
Daniel Veillard14f752c2003-08-09 11:44:50 +00004957#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00004958 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
4959 htmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14f752c2003-08-09 11:44:50 +00004960#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004961 } else if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard14f752c2003-08-09 11:44:50 +00004962 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
4963 xmlParserInputBufferPtr in = ctxt->input->buf;
4964 if ((in->encoder != NULL) && (in->buffer != NULL) &&
4965 (in->raw != NULL)) {
4966 int nbchars;
4967
4968 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
4969 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004970 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
4971 "encoder error\n", NULL, NULL);
Daniel Veillard14f752c2003-08-09 11:44:50 +00004972 return(XML_ERR_INVALID_ENCODING);
4973 }
4974 }
4975 }
Owen Taylor3473f882001-02-23 17:55:21 +00004976 }
Daniel Veillard14f752c2003-08-09 11:44:50 +00004977 htmlParseTryOrFinish(ctxt, terminate);
Owen Taylor3473f882001-02-23 17:55:21 +00004978 if (terminate) {
4979 if ((ctxt->instate != XML_PARSER_EOF) &&
4980 (ctxt->instate != XML_PARSER_EPILOG) &&
4981 (ctxt->instate != XML_PARSER_MISC)) {
4982 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004983 ctxt->wellFormed = 0;
4984 }
4985 if (ctxt->instate != XML_PARSER_EOF) {
4986 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4987 ctxt->sax->endDocument(ctxt->userData);
4988 }
4989 ctxt->instate = XML_PARSER_EOF;
4990 }
4991 return((xmlParserErrors) ctxt->errNo);
4992}
Daniel Veillard73b013f2003-09-30 12:36:01 +00004993#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004994
4995/************************************************************************
4996 * *
4997 * User entry points *
4998 * *
4999 ************************************************************************/
5000
5001/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005002 * htmlCreatePushParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005003 * @sax: a SAX handler
5004 * @user_data: The user data returned on SAX callbacks
5005 * @chunk: a pointer to an array of chars
5006 * @size: number of chars in the array
5007 * @filename: an optional file name or URI
5008 * @enc: an optional encoding
5009 *
5010 * Create a parser context for using the HTML parser in push mode
Owen Taylor3473f882001-02-23 17:55:21 +00005011 * The value of @filename is used for fetching external entities
5012 * and error/warning reports.
5013 *
5014 * Returns the new parser context or NULL
5015 */
5016htmlParserCtxtPtr
5017htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
5018 const char *chunk, int size, const char *filename,
5019 xmlCharEncoding enc) {
5020 htmlParserCtxtPtr ctxt;
5021 htmlParserInputPtr inputStream;
5022 xmlParserInputBufferPtr buf;
5023
Daniel Veillardd0463562001-10-13 09:15:48 +00005024 xmlInitParser();
5025
Owen Taylor3473f882001-02-23 17:55:21 +00005026 buf = xmlAllocParserInputBuffer(enc);
5027 if (buf == NULL) return(NULL);
5028
Daniel Veillardf403d292003-10-05 13:51:35 +00005029 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005030 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005031 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005032 return(NULL);
5033 }
Daniel Veillard77a90a72003-03-22 00:04:05 +00005034 if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
5035 ctxt->charset=XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +00005036 if (sax != NULL) {
Daniel Veillard092643b2003-09-25 14:29:29 +00005037 if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
Owen Taylor3473f882001-02-23 17:55:21 +00005038 xmlFree(ctxt->sax);
5039 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
5040 if (ctxt->sax == NULL) {
5041 xmlFree(buf);
5042 xmlFree(ctxt);
5043 return(NULL);
5044 }
5045 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
5046 if (user_data != NULL)
5047 ctxt->userData = user_data;
5048 }
5049 if (filename == NULL) {
5050 ctxt->directory = NULL;
5051 } else {
5052 ctxt->directory = xmlParserGetDirectory(filename);
5053 }
5054
5055 inputStream = htmlNewInputStream(ctxt);
5056 if (inputStream == NULL) {
5057 xmlFreeParserCtxt(ctxt);
Daniel Veillard77a90a72003-03-22 00:04:05 +00005058 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005059 return(NULL);
5060 }
5061
5062 if (filename == NULL)
5063 inputStream->filename = NULL;
5064 else
Daniel Veillard5f704af2003-03-05 10:01:43 +00005065 inputStream->filename = (char *)
5066 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00005067 inputStream->buf = buf;
5068 inputStream->base = inputStream->buf->buffer->content;
5069 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard5f704af2003-03-05 10:01:43 +00005070 inputStream->end =
5071 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005072
5073 inputPush(ctxt, inputStream);
5074
5075 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5076 (ctxt->input->buf != NULL)) {
Daniel Veillard5f704af2003-03-05 10:01:43 +00005077 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5078 int cur = ctxt->input->cur - ctxt->input->base;
5079
Owen Taylor3473f882001-02-23 17:55:21 +00005080 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillard5f704af2003-03-05 10:01:43 +00005081
5082 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5083 ctxt->input->cur = ctxt->input->base + cur;
5084 ctxt->input->end =
5085 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005086#ifdef DEBUG_PUSH
5087 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5088#endif
5089 }
5090
5091 return(ctxt);
5092}
5093
5094/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005095 * htmlSAXParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005096 * @cur: a pointer to an array of xmlChar
5097 * @encoding: a free form C string describing the HTML document encoding, or NULL
5098 * @sax: the SAX handler block
5099 * @userData: if using SAX, this pointer will be provided on callbacks.
5100 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005101 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
5102 * to handle parse events. If sax is NULL, fallback to the default DOM
5103 * behavior and return a tree.
Owen Taylor3473f882001-02-23 17:55:21 +00005104 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005105 * Returns the resulting document tree unless SAX is NULL or the document is
5106 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005107 */
5108
5109htmlDocPtr
5110htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
5111 htmlDocPtr ret;
5112 htmlParserCtxtPtr ctxt;
5113
Daniel Veillardd0463562001-10-13 09:15:48 +00005114 xmlInitParser();
5115
Owen Taylor3473f882001-02-23 17:55:21 +00005116 if (cur == NULL) return(NULL);
5117
5118
5119 ctxt = htmlCreateDocParserCtxt(cur, encoding);
5120 if (ctxt == NULL) return(NULL);
5121 if (sax != NULL) {
Daniel Veillardb19ba832003-08-14 00:33:46 +00005122 if (ctxt->sax != NULL) xmlFree (ctxt->sax);
Owen Taylor3473f882001-02-23 17:55:21 +00005123 ctxt->sax = sax;
5124 ctxt->userData = userData;
5125 }
5126
5127 htmlParseDocument(ctxt);
5128 ret = ctxt->myDoc;
5129 if (sax != NULL) {
5130 ctxt->sax = NULL;
5131 ctxt->userData = NULL;
5132 }
5133 htmlFreeParserCtxt(ctxt);
5134
5135 return(ret);
5136}
5137
5138/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005139 * htmlParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005140 * @cur: a pointer to an array of xmlChar
5141 * @encoding: a free form C string describing the HTML document encoding, or NULL
5142 *
5143 * parse an HTML in-memory document and build a tree.
5144 *
5145 * Returns the resulting document tree
5146 */
5147
5148htmlDocPtr
5149htmlParseDoc(xmlChar *cur, const char *encoding) {
5150 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
5151}
5152
5153
5154/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005155 * htmlCreateFileParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005156 * @filename: the filename
5157 * @encoding: a free form C string describing the HTML document encoding, or NULL
5158 *
5159 * Create a parser context for a file content.
5160 * Automatic support for ZLIB/Compress compressed document is provided
5161 * by default if found at compile-time.
5162 *
5163 * Returns the new parser context or NULL
5164 */
5165htmlParserCtxtPtr
5166htmlCreateFileParserCtxt(const char *filename, const char *encoding)
5167{
5168 htmlParserCtxtPtr ctxt;
5169 htmlParserInputPtr inputStream;
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005170 char *canonicFilename;
Owen Taylor3473f882001-02-23 17:55:21 +00005171 /* htmlCharEncoding enc; */
5172 xmlChar *content, *content_line = (xmlChar *) "charset=";
5173
Daniel Veillardf403d292003-10-05 13:51:35 +00005174 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005175 if (ctxt == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005176 return(NULL);
5177 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005178 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
5179 if (canonicFilename == NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00005180#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005181 if (xmlDefaultSAXHandler.error != NULL) {
5182 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
5183 }
Daniel Veillard87247e82004-01-13 20:42:02 +00005184#endif
Daniel Veillard104caa32003-05-13 22:54:05 +00005185 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005186 return(NULL);
5187 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005188
5189 inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
5190 xmlFree(canonicFilename);
5191 if (inputStream == NULL) {
5192 xmlFreeParserCtxt(ctxt);
5193 return(NULL);
5194 }
Owen Taylor3473f882001-02-23 17:55:21 +00005195
5196 inputPush(ctxt, inputStream);
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005197
Owen Taylor3473f882001-02-23 17:55:21 +00005198 /* set encoding */
5199 if (encoding) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005200 content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
Owen Taylor3473f882001-02-23 17:55:21 +00005201 if (content) {
5202 strcpy ((char *)content, (char *)content_line);
5203 strcat ((char *)content, (char *)encoding);
5204 htmlCheckEncoding (ctxt, content);
5205 xmlFree (content);
5206 }
5207 }
5208
5209 return(ctxt);
5210}
5211
5212/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005213 * htmlSAXParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005214 * @filename: the filename
5215 * @encoding: a free form C string describing the HTML document encoding, or NULL
5216 * @sax: the SAX handler block
5217 * @userData: if using SAX, this pointer will be provided on callbacks.
5218 *
5219 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5220 * compressed document is provided by default if found at compile-time.
5221 * It use the given SAX function block to handle the parsing callback.
5222 * If sax is NULL, fallback to the default DOM tree building routines.
5223 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005224 * Returns the resulting document tree unless SAX is NULL or the document is
5225 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005226 */
5227
5228htmlDocPtr
5229htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
5230 void *userData) {
5231 htmlDocPtr ret;
5232 htmlParserCtxtPtr ctxt;
5233 htmlSAXHandlerPtr oldsax = NULL;
5234
Daniel Veillardd0463562001-10-13 09:15:48 +00005235 xmlInitParser();
5236
Owen Taylor3473f882001-02-23 17:55:21 +00005237 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5238 if (ctxt == NULL) return(NULL);
5239 if (sax != NULL) {
5240 oldsax = ctxt->sax;
5241 ctxt->sax = sax;
5242 ctxt->userData = userData;
5243 }
5244
5245 htmlParseDocument(ctxt);
5246
5247 ret = ctxt->myDoc;
5248 if (sax != NULL) {
5249 ctxt->sax = oldsax;
5250 ctxt->userData = NULL;
5251 }
5252 htmlFreeParserCtxt(ctxt);
5253
5254 return(ret);
5255}
5256
5257/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005258 * htmlParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005259 * @filename: the filename
5260 * @encoding: a free form C string describing the HTML document encoding, or NULL
5261 *
5262 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5263 * compressed document is provided by default if found at compile-time.
5264 *
5265 * Returns the resulting document tree
5266 */
5267
5268htmlDocPtr
5269htmlParseFile(const char *filename, const char *encoding) {
5270 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
5271}
5272
5273/**
5274 * htmlHandleOmittedElem:
5275 * @val: int 0 or 1
5276 *
5277 * Set and return the previous value for handling HTML omitted tags.
5278 *
5279 * Returns the last value for 0 for no handling, 1 for auto insertion.
5280 */
5281
5282int
5283htmlHandleOmittedElem(int val) {
5284 int old = htmlOmittedDefaultValue;
5285
5286 htmlOmittedDefaultValue = val;
5287 return(old);
5288}
5289
Daniel Veillard930dfb62003-02-05 10:17:38 +00005290/**
5291 * htmlElementAllowedHere:
5292 * @parent: HTML parent element
5293 * @elt: HTML element
5294 *
5295 * Checks whether an HTML element may be a direct child of a parent element.
5296 * Note - doesn't check for deprecated elements
5297 *
5298 * Returns 1 if allowed; 0 otherwise.
5299 */
5300int
5301htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
5302 const char** p ;
5303
5304 if ( ! elt || ! parent || ! parent->subelts )
5305 return 0 ;
5306
5307 for ( p = parent->subelts; *p; ++p )
5308 if ( !xmlStrcmp((const xmlChar *)*p, elt) )
5309 return 1 ;
5310
5311 return 0 ;
5312}
5313/**
5314 * htmlElementStatusHere:
5315 * @parent: HTML parent element
5316 * @elt: HTML element
5317 *
5318 * Checks whether an HTML element may be a direct child of a parent element.
5319 * and if so whether it is valid or deprecated.
5320 *
5321 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5322 */
5323htmlStatus
5324htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
5325 if ( ! parent || ! elt )
5326 return HTML_INVALID ;
5327 if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
5328 return HTML_INVALID ;
5329
5330 return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
5331}
5332/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005333 * htmlAttrAllowed:
Daniel Veillard930dfb62003-02-05 10:17:38 +00005334 * @elt: HTML element
5335 * @attr: HTML attribute
5336 * @legacy: whether to allow deprecated attributes
5337 *
5338 * Checks whether an attribute is valid for an element
5339 * Has full knowledge of Required and Deprecated attributes
5340 *
5341 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5342 */
5343htmlStatus
5344htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
5345 const char** p ;
5346
5347 if ( !elt || ! attr )
5348 return HTML_INVALID ;
5349
5350 if ( elt->attrs_req )
5351 for ( p = elt->attrs_req; *p; ++p)
5352 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5353 return HTML_REQUIRED ;
5354
5355 if ( elt->attrs_opt )
5356 for ( p = elt->attrs_opt; *p; ++p)
5357 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5358 return HTML_VALID ;
5359
5360 if ( legacy && elt->attrs_depr )
5361 for ( p = elt->attrs_depr; *p; ++p)
5362 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5363 return HTML_DEPRECATED ;
5364
5365 return HTML_INVALID ;
5366}
5367/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005368 * htmlNodeStatus:
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005369 * @node: an htmlNodePtr in a tree
5370 * @legacy: whether to allow deprecated elements (YES is faster here
Daniel Veillard930dfb62003-02-05 10:17:38 +00005371 * for Element nodes)
5372 *
5373 * Checks whether the tree node is valid. Experimental (the author
5374 * only uses the HTML enhancements in a SAX parser)
5375 *
5376 * Return: for Element nodes, a return from htmlElementAllowedHere (if
5377 * legacy allowed) or htmlElementStatusHere (otherwise).
5378 * for Attribute nodes, a return from htmlAttrAllowed
5379 * for other nodes, HTML_NA (no checks performed)
5380 */
5381htmlStatus
5382htmlNodeStatus(const htmlNodePtr node, int legacy) {
5383 if ( ! node )
5384 return HTML_INVALID ;
5385
5386 switch ( node->type ) {
5387 case XML_ELEMENT_NODE:
5388 return legacy
5389 ? ( htmlElementAllowedHere (
5390 htmlTagLookup(node->parent->name) , node->name
5391 ) ? HTML_VALID : HTML_INVALID )
5392 : htmlElementStatusHere(
5393 htmlTagLookup(node->parent->name) ,
5394 htmlTagLookup(node->name) )
5395 ;
5396 case XML_ATTRIBUTE_NODE:
5397 return htmlAttrAllowed(
5398 htmlTagLookup(node->parent->name) , node->name, legacy) ;
5399 default: return HTML_NA ;
5400 }
5401}
Daniel Veillard9475a352003-09-26 12:47:50 +00005402/************************************************************************
5403 * *
5404 * New set (2.6.0) of simpler and more flexible APIs *
5405 * *
5406 ************************************************************************/
5407/**
5408 * DICT_FREE:
5409 * @str: a string
5410 *
5411 * Free a string if it is not owned by the "dict" dictionnary in the
5412 * current scope
5413 */
5414#define DICT_FREE(str) \
5415 if ((str) && ((!dict) || \
5416 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
5417 xmlFree((char *)(str));
5418
5419/**
5420 * htmlCtxtReset:
Daniel Veillardf403d292003-10-05 13:51:35 +00005421 * @ctxt: an HTML parser context
Daniel Veillard9475a352003-09-26 12:47:50 +00005422 *
5423 * Reset a parser context
5424 */
5425void
5426htmlCtxtReset(htmlParserCtxtPtr ctxt)
5427{
5428 xmlParserInputPtr input;
5429 xmlDictPtr dict = ctxt->dict;
5430
5431 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
5432 xmlFreeInputStream(input);
5433 }
5434 ctxt->inputNr = 0;
5435 ctxt->input = NULL;
5436
5437 ctxt->spaceNr = 0;
5438 ctxt->spaceTab[0] = -1;
5439 ctxt->space = &ctxt->spaceTab[0];
5440
5441
5442 ctxt->nodeNr = 0;
5443 ctxt->node = NULL;
5444
5445 ctxt->nameNr = 0;
5446 ctxt->name = NULL;
5447
5448 DICT_FREE(ctxt->version);
5449 ctxt->version = NULL;
5450 DICT_FREE(ctxt->encoding);
5451 ctxt->encoding = NULL;
5452 DICT_FREE(ctxt->directory);
5453 ctxt->directory = NULL;
5454 DICT_FREE(ctxt->extSubURI);
5455 ctxt->extSubURI = NULL;
5456 DICT_FREE(ctxt->extSubSystem);
5457 ctxt->extSubSystem = NULL;
5458 if (ctxt->myDoc != NULL)
5459 xmlFreeDoc(ctxt->myDoc);
5460 ctxt->myDoc = NULL;
5461
5462 ctxt->standalone = -1;
5463 ctxt->hasExternalSubset = 0;
5464 ctxt->hasPErefs = 0;
5465 ctxt->html = 1;
5466 ctxt->external = 0;
5467 ctxt->instate = XML_PARSER_START;
5468 ctxt->token = 0;
5469
5470 ctxt->wellFormed = 1;
5471 ctxt->nsWellFormed = 1;
5472 ctxt->valid = 1;
5473 ctxt->vctxt.userData = ctxt;
5474 ctxt->vctxt.error = xmlParserValidityError;
5475 ctxt->vctxt.warning = xmlParserValidityWarning;
5476 ctxt->record_info = 0;
5477 ctxt->nbChars = 0;
5478 ctxt->checkIndex = 0;
5479 ctxt->inSubset = 0;
5480 ctxt->errNo = XML_ERR_OK;
5481 ctxt->depth = 0;
5482 ctxt->charset = XML_CHAR_ENCODING_UTF8;
5483 ctxt->catalogs = NULL;
5484 xmlInitNodeInfoSeq(&ctxt->node_seq);
5485
5486 if (ctxt->attsDefault != NULL) {
5487 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
5488 ctxt->attsDefault = NULL;
5489 }
5490 if (ctxt->attsSpecial != NULL) {
5491 xmlHashFree(ctxt->attsSpecial, NULL);
5492 ctxt->attsSpecial = NULL;
5493 }
5494}
5495
5496/**
5497 * htmlCtxtUseOptions:
5498 * @ctxt: an HTML parser context
5499 * @options: a combination of htmlParserOption(s)
5500 *
5501 * Applies the options to the parser context
5502 *
5503 * Returns 0 in case of success, the set of unknown or unimplemented options
5504 * in case of error.
5505 */
5506int
5507htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
5508{
5509 if (options & HTML_PARSE_NOWARNING) {
5510 ctxt->sax->warning = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005511 ctxt->vctxt.warning = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005512 options -= XML_PARSE_NOWARNING;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005513 ctxt->options |= XML_PARSE_NOWARNING;
Daniel Veillard9475a352003-09-26 12:47:50 +00005514 }
5515 if (options & HTML_PARSE_NOERROR) {
5516 ctxt->sax->error = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005517 ctxt->vctxt.error = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005518 ctxt->sax->fatalError = NULL;
5519 options -= XML_PARSE_NOERROR;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005520 ctxt->options |= XML_PARSE_NOERROR;
Daniel Veillard9475a352003-09-26 12:47:50 +00005521 }
5522 if (options & HTML_PARSE_PEDANTIC) {
5523 ctxt->pedantic = 1;
5524 options -= XML_PARSE_PEDANTIC;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005525 ctxt->options |= XML_PARSE_PEDANTIC;
Daniel Veillard9475a352003-09-26 12:47:50 +00005526 } else
5527 ctxt->pedantic = 0;
5528 if (options & XML_PARSE_NOBLANKS) {
5529 ctxt->keepBlanks = 0;
5530 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
5531 options -= XML_PARSE_NOBLANKS;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005532 ctxt->options |= XML_PARSE_NOBLANKS;
Daniel Veillard9475a352003-09-26 12:47:50 +00005533 } else
5534 ctxt->keepBlanks = 1;
5535 ctxt->dictNames = 0;
5536 return (options);
5537}
5538
5539/**
5540 * htmlDoRead:
5541 * @ctxt: an HTML parser context
5542 * @URL: the base URL to use for the document
5543 * @encoding: the document encoding, or NULL
5544 * @options: a combination of htmlParserOption(s)
5545 * @reuse: keep the context for reuse
5546 *
5547 * Common front-end for the htmlRead functions
5548 *
5549 * Returns the resulting document tree or NULL
5550 */
5551static htmlDocPtr
5552htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
5553 int options, int reuse)
5554{
5555 htmlDocPtr ret;
5556
5557 htmlCtxtUseOptions(ctxt, options);
5558 ctxt->html = 1;
5559 if (encoding != NULL) {
5560 xmlCharEncodingHandlerPtr hdlr;
5561
5562 hdlr = xmlFindCharEncodingHandler(encoding);
5563 if (hdlr != NULL)
5564 xmlSwitchToEncoding(ctxt, hdlr);
5565 }
5566 if ((URL != NULL) && (ctxt->input != NULL) &&
5567 (ctxt->input->filename == NULL))
5568 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
5569 htmlParseDocument(ctxt);
5570 ret = ctxt->myDoc;
5571 ctxt->myDoc = NULL;
5572 if (!reuse) {
5573 if ((ctxt->dictNames) &&
5574 (ret != NULL) &&
5575 (ret->dict == ctxt->dict))
5576 ctxt->dict = NULL;
5577 xmlFreeParserCtxt(ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00005578 }
5579 return (ret);
5580}
5581
5582/**
5583 * htmlReadDoc:
5584 * @cur: a pointer to a zero terminated string
5585 * @URL: the base URL to use for the document
5586 * @encoding: the document encoding, or NULL
5587 * @options: a combination of htmlParserOption(s)
5588 *
5589 * parse an XML in-memory document and build a tree.
5590 *
5591 * Returns the resulting document tree
5592 */
5593htmlDocPtr
5594htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
5595{
5596 htmlParserCtxtPtr ctxt;
5597
5598 if (cur == NULL)
5599 return (NULL);
5600
5601 ctxt = xmlCreateDocParserCtxt(cur);
5602 if (ctxt == NULL)
5603 return (NULL);
5604 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5605}
5606
5607/**
5608 * htmlReadFile:
5609 * @filename: a file or URL
5610 * @encoding: the document encoding, or NULL
5611 * @options: a combination of htmlParserOption(s)
5612 *
5613 * parse an XML file from the filesystem or the network.
5614 *
5615 * Returns the resulting document tree
5616 */
5617htmlDocPtr
5618htmlReadFile(const char *filename, const char *encoding, int options)
5619{
5620 htmlParserCtxtPtr ctxt;
5621
5622 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5623 if (ctxt == NULL)
5624 return (NULL);
5625 return (htmlDoRead(ctxt, NULL, NULL, options, 0));
5626}
5627
5628/**
5629 * htmlReadMemory:
5630 * @buffer: a pointer to a char array
5631 * @size: the size of the array
5632 * @URL: the base URL to use for the document
5633 * @encoding: the document encoding, or NULL
5634 * @options: a combination of htmlParserOption(s)
5635 *
5636 * parse an XML in-memory document and build a tree.
5637 *
5638 * Returns the resulting document tree
5639 */
5640htmlDocPtr
5641htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
5642{
5643 htmlParserCtxtPtr ctxt;
5644
5645 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
5646 if (ctxt == NULL)
5647 return (NULL);
5648 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5649}
5650
5651/**
5652 * htmlReadFd:
5653 * @fd: an open file descriptor
5654 * @URL: the base URL to use for the document
5655 * @encoding: the document encoding, or NULL
5656 * @options: a combination of htmlParserOption(s)
5657 *
5658 * parse an XML from a file descriptor and build a tree.
5659 *
5660 * Returns the resulting document tree
5661 */
5662htmlDocPtr
5663htmlReadFd(int fd, const char *URL, const char *encoding, int options)
5664{
5665 htmlParserCtxtPtr ctxt;
5666 xmlParserInputBufferPtr input;
5667 xmlParserInputPtr stream;
5668
5669 if (fd < 0)
5670 return (NULL);
5671
5672 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5673 if (input == NULL)
5674 return (NULL);
5675 ctxt = xmlNewParserCtxt();
5676 if (ctxt == NULL) {
5677 xmlFreeParserInputBuffer(input);
5678 return (NULL);
5679 }
5680 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5681 if (stream == NULL) {
5682 xmlFreeParserInputBuffer(input);
5683 xmlFreeParserCtxt(ctxt);
5684 return (NULL);
5685 }
5686 inputPush(ctxt, stream);
5687 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5688}
5689
5690/**
5691 * htmlReadIO:
5692 * @ioread: an I/O read function
5693 * @ioclose: an I/O close function
5694 * @ioctx: an I/O handler
5695 * @URL: the base URL to use for the document
5696 * @encoding: the document encoding, or NULL
5697 * @options: a combination of htmlParserOption(s)
5698 *
5699 * parse an HTML document from I/O functions and source and build a tree.
5700 *
5701 * Returns the resulting document tree
5702 */
5703htmlDocPtr
5704htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
5705 void *ioctx, const char *URL, const char *encoding, int options)
5706{
5707 htmlParserCtxtPtr ctxt;
5708 xmlParserInputBufferPtr input;
5709 xmlParserInputPtr stream;
5710
5711 if (ioread == NULL)
5712 return (NULL);
5713
5714 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
5715 XML_CHAR_ENCODING_NONE);
5716 if (input == NULL)
5717 return (NULL);
5718 ctxt = xmlNewParserCtxt();
5719 if (ctxt == NULL) {
5720 xmlFreeParserInputBuffer(input);
5721 return (NULL);
5722 }
5723 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5724 if (stream == NULL) {
5725 xmlFreeParserInputBuffer(input);
5726 xmlFreeParserCtxt(ctxt);
5727 return (NULL);
5728 }
5729 inputPush(ctxt, stream);
5730 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5731}
5732
5733/**
5734 * htmlCtxtReadDoc:
5735 * @ctxt: an HTML parser context
5736 * @cur: a pointer to a zero terminated string
5737 * @URL: the base URL to use for the document
5738 * @encoding: the document encoding, or NULL
5739 * @options: a combination of htmlParserOption(s)
5740 *
5741 * parse an XML in-memory document and build a tree.
5742 * This reuses the existing @ctxt parser context
5743 *
5744 * Returns the resulting document tree
5745 */
5746htmlDocPtr
5747htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
5748 const char *URL, const char *encoding, int options)
5749{
5750 xmlParserInputPtr stream;
5751
5752 if (cur == NULL)
5753 return (NULL);
5754 if (ctxt == NULL)
5755 return (NULL);
5756
5757 htmlCtxtReset(ctxt);
5758
5759 stream = xmlNewStringInputStream(ctxt, cur);
5760 if (stream == NULL) {
5761 return (NULL);
5762 }
5763 inputPush(ctxt, stream);
5764 return (htmlDoRead(ctxt, URL, encoding, options, 1));
5765}
5766
5767/**
5768 * htmlCtxtReadFile:
5769 * @ctxt: an HTML parser context
5770 * @filename: a file or URL
5771 * @encoding: the document encoding, or NULL
5772 * @options: a combination of htmlParserOption(s)
5773 *
5774 * parse an XML file from the filesystem or the network.
5775 * This reuses the existing @ctxt parser context
5776 *
5777 * Returns the resulting document tree
5778 */
5779htmlDocPtr
5780htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
5781 const char *encoding, int options)
5782{
5783 xmlParserInputPtr stream;
5784
5785 if (filename == NULL)
5786 return (NULL);
5787 if (ctxt == NULL)
5788 return (NULL);
5789
5790 htmlCtxtReset(ctxt);
5791
5792 stream = xmlNewInputFromFile(ctxt, filename);
5793 if (stream == NULL) {
5794 return (NULL);
5795 }
5796 inputPush(ctxt, stream);
5797 return (htmlDoRead(ctxt, NULL, encoding, options, 1));
5798}
5799
5800/**
5801 * htmlCtxtReadMemory:
5802 * @ctxt: an HTML parser context
5803 * @buffer: a pointer to a char array
5804 * @size: the size of the array
5805 * @URL: the base URL to use for the document
5806 * @encoding: the document encoding, or NULL
5807 * @options: a combination of htmlParserOption(s)
5808 *
5809 * parse an XML in-memory document and build a tree.
5810 * This reuses the existing @ctxt parser context
5811 *
5812 * Returns the resulting document tree
5813 */
5814htmlDocPtr
5815htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
5816 const char *URL, const char *encoding, int options)
5817{
5818 xmlParserInputBufferPtr input;
5819 xmlParserInputPtr stream;
5820
5821 if (ctxt == NULL)
5822 return (NULL);
5823 if (buffer == NULL)
5824 return (NULL);
5825
5826 htmlCtxtReset(ctxt);
5827
5828 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
5829 if (input == NULL) {
5830 return(NULL);
5831 }
5832
5833 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5834 if (stream == NULL) {
5835 xmlFreeParserInputBuffer(input);
5836 return(NULL);
5837 }
5838
5839 inputPush(ctxt, stream);
5840 return (htmlDoRead(ctxt, URL, encoding, options, 1));
5841}
5842
5843/**
5844 * htmlCtxtReadFd:
5845 * @ctxt: an HTML parser context
5846 * @fd: an open file descriptor
5847 * @URL: the base URL to use for the document
5848 * @encoding: the document encoding, or NULL
5849 * @options: a combination of htmlParserOption(s)
5850 *
5851 * parse an XML from a file descriptor and build a tree.
5852 * This reuses the existing @ctxt parser context
5853 *
5854 * Returns the resulting document tree
5855 */
5856htmlDocPtr
5857htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
5858 const char *URL, const char *encoding, int options)
5859{
5860 xmlParserInputBufferPtr input;
5861 xmlParserInputPtr stream;
5862
5863 if (fd < 0)
5864 return (NULL);
5865 if (ctxt == NULL)
5866 return (NULL);
5867
5868 htmlCtxtReset(ctxt);
5869
5870
5871 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5872 if (input == NULL)
5873 return (NULL);
5874 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5875 if (stream == NULL) {
5876 xmlFreeParserInputBuffer(input);
5877 return (NULL);
5878 }
5879 inputPush(ctxt, stream);
5880 return (htmlDoRead(ctxt, URL, encoding, options, 1));
5881}
5882
5883/**
5884 * htmlCtxtReadIO:
5885 * @ctxt: an HTML parser context
5886 * @ioread: an I/O read function
5887 * @ioclose: an I/O close function
5888 * @ioctx: an I/O handler
5889 * @URL: the base URL to use for the document
5890 * @encoding: the document encoding, or NULL
5891 * @options: a combination of htmlParserOption(s)
5892 *
5893 * parse an HTML document from I/O functions and source and build a tree.
5894 * This reuses the existing @ctxt parser context
5895 *
5896 * Returns the resulting document tree
5897 */
5898htmlDocPtr
5899htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
5900 xmlInputCloseCallback ioclose, void *ioctx,
5901 const char *URL,
5902 const char *encoding, int options)
5903{
5904 xmlParserInputBufferPtr input;
5905 xmlParserInputPtr stream;
5906
5907 if (ioread == NULL)
5908 return (NULL);
5909 if (ctxt == NULL)
5910 return (NULL);
5911
5912 htmlCtxtReset(ctxt);
5913
5914 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
5915 XML_CHAR_ENCODING_NONE);
5916 if (input == NULL)
5917 return (NULL);
5918 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5919 if (stream == NULL) {
5920 xmlFreeParserInputBuffer(input);
5921 return (NULL);
5922 }
5923 inputPush(ctxt, stream);
5924 return (htmlDoRead(ctxt, URL, encoding, options, 1));
5925}
5926
Owen Taylor3473f882001-02-23 17:55:21 +00005927#endif /* LIBXML_HTML_ENABLED */