blob: ea244f522ab544feed7c6bd6af2596a05d849b11 [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
Bjorn Reese70a9da52001-04-21 16:57:29 +00009#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000010#ifdef LIBXML_HTML_ENABLED
Bjorn Reese70a9da52001-04-21 16:57:29 +000011
Owen Taylor3473f882001-02-23 17:55:21 +000012#include <string.h>
13#ifdef HAVE_CTYPE_H
14#include <ctype.h>
15#endif
16#ifdef HAVE_STDLIB_H
17#include <stdlib.h>
18#endif
19#ifdef HAVE_SYS_STAT_H
20#include <sys/stat.h>
21#endif
22#ifdef HAVE_FCNTL_H
23#include <fcntl.h>
24#endif
25#ifdef HAVE_UNISTD_H
26#include <unistd.h>
27#endif
28#ifdef HAVE_ZLIB_H
29#include <zlib.h>
30#endif
31
32#include <libxml/xmlmemory.h>
33#include <libxml/tree.h>
34#include <libxml/parser.h>
35#include <libxml/parserInternals.h>
36#include <libxml/xmlerror.h>
37#include <libxml/HTMLparser.h>
Daniel Veillard56a4cb82001-03-24 17:00:36 +000038#include <libxml/HTMLtree.h>
Owen Taylor3473f882001-02-23 17:55:21 +000039#include <libxml/entities.h>
40#include <libxml/encoding.h>
41#include <libxml/valid.h>
42#include <libxml/xmlIO.h>
43
44#define HTML_MAX_NAMELEN 1000
45#define HTML_PARSER_BIG_BUFFER_SIZE 1000
46#define HTML_PARSER_BUFFER_SIZE 100
47
48/* #define DEBUG */
49/* #define DEBUG_PUSH */
50
51int htmlOmittedDefaultValue = 1;
52
Daniel Veillard56a4cb82001-03-24 17:00:36 +000053xmlChar * htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
54 xmlChar end, xmlChar end2, xmlChar end3);
55
56/************************************************************************
57 * *
Owen Taylor3473f882001-02-23 17:55:21 +000058 * Parser stacks related functions and macros *
59 * *
60 ************************************************************************/
61
62/*
63 * Generic function for accessing stacks in the Parser Context
64 */
65
66#define PUSH_AND_POP(scope, type, name) \
67scope int html##name##Push(htmlParserCtxtPtr ctxt, type value) { \
68 if (ctxt->name##Nr >= ctxt->name##Max) { \
69 ctxt->name##Max *= 2; \
70 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
71 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
72 if (ctxt->name##Tab == NULL) { \
73 xmlGenericError(xmlGenericErrorContext, \
74 "realloc failed !\n"); \
75 return(0); \
76 } \
77 } \
78 ctxt->name##Tab[ctxt->name##Nr] = value; \
79 ctxt->name = value; \
80 return(ctxt->name##Nr++); \
81} \
82scope type html##name##Pop(htmlParserCtxtPtr ctxt) { \
83 type ret; \
84 if (ctxt->name##Nr < 0) return(0); \
85 ctxt->name##Nr--; \
86 if (ctxt->name##Nr < 0) return(0); \
87 if (ctxt->name##Nr > 0) \
88 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
89 else \
90 ctxt->name = NULL; \
91 ret = ctxt->name##Tab[ctxt->name##Nr]; \
92 ctxt->name##Tab[ctxt->name##Nr] = 0; \
93 return(ret); \
94} \
95
Daniel Veillard56a4cb82001-03-24 17:00:36 +000096/* PUSH_AND_POP(static, xmlNodePtr, node) */
97PUSH_AND_POP(static, xmlChar*, name)
Owen Taylor3473f882001-02-23 17:55:21 +000098
99/*
100 * Macros for accessing the content. Those should be used only by the parser,
101 * and not exported.
102 *
103 * Dirty macros, i.e. one need to make assumption on the context to use them
104 *
105 * CUR_PTR return the current pointer to the xmlChar to be parsed.
106 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
107 * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
108 * in UNICODE mode. This should be used internally by the parser
109 * only to compare to ASCII values otherwise it would break when
110 * running with UTF-8 encoding.
111 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
112 * to compare on ASCII based substring.
113 * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
114 * it should be used only to compare on ASCII based substring.
115 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
116 * strings within the parser.
117 *
118 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
119 *
120 * CURRENT Returns the current char value, with the full decoding of
121 * UTF-8 if we are using this mode. It returns an int.
122 * NEXT Skip to the next character, this does the proper decoding
123 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
124 * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
125 */
126
127#define UPPER (toupper(*ctxt->input->cur))
128
129#define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val)
130
131#define NXT(val) ctxt->input->cur[(val)]
132
133#define UPP(val) (toupper(ctxt->input->cur[(val)]))
134
135#define CUR_PTR ctxt->input->cur
136
137#define SHRINK xmlParserInputShrink(ctxt->input)
138
139#define GROW xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
140
141#define CURRENT ((int) (*ctxt->input->cur))
142
143#define SKIP_BLANKS htmlSkipBlankChars(ctxt)
144
145/* Inported from XML */
146
147/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
148#define CUR ((int) (*ctxt->input->cur))
149#define NEXT xmlNextChar(ctxt),ctxt->nbChars++
150
151#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
152#define NXT(val) ctxt->input->cur[(val)]
153#define CUR_PTR ctxt->input->cur
154
155
156#define NEXTL(l) do { \
157 if (*(ctxt->input->cur) == '\n') { \
158 ctxt->input->line++; ctxt->input->col = 1; \
159 } else ctxt->input->col++; \
160 ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++; \
161 } while (0)
162
163/************
164 \
165 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
166 if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
167 ************/
168
169#define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
170#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
171
172#define COPY_BUF(l,b,i,v) \
173 if (l == 1) b[i++] = (xmlChar) v; \
174 else i += xmlCopyChar(l,&b[i],v)
175
176/**
177 * htmlCurrentChar:
178 * @ctxt: the HTML parser context
179 * @len: pointer to the length of the char read
180 *
181 * The current char value, if using UTF-8 this may actaully span multiple
182 * bytes in the input buffer. Implement the end of line normalization:
183 * 2.11 End-of-Line Handling
184 * If the encoding is unspecified, in the case we find an ISO-Latin-1
185 * char, then the encoding converter is plugged in automatically.
186 *
187 * Returns the current char value and its lenght
188 */
189
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000190static int
Owen Taylor3473f882001-02-23 17:55:21 +0000191htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
192 if (ctxt->instate == XML_PARSER_EOF)
193 return(0);
194
195 if (ctxt->token != 0) {
196 *len = 0;
197 return(ctxt->token);
198 }
199 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
200 /*
201 * We are supposed to handle UTF8, check it's valid
202 * From rfc2044: encoding of the Unicode values on UTF-8:
203 *
204 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
205 * 0000 0000-0000 007F 0xxxxxxx
206 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
207 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
208 *
209 * Check for the 0x110000 limit too
210 */
211 const unsigned char *cur = ctxt->input->cur;
212 unsigned char c;
213 unsigned int val;
214
215 c = *cur;
216 if (c & 0x80) {
217 if (cur[1] == 0)
218 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
219 if ((cur[1] & 0xc0) != 0x80)
220 goto encoding_error;
221 if ((c & 0xe0) == 0xe0) {
222
223 if (cur[2] == 0)
224 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
225 if ((cur[2] & 0xc0) != 0x80)
226 goto encoding_error;
227 if ((c & 0xf0) == 0xf0) {
228 if (cur[3] == 0)
229 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
230 if (((c & 0xf8) != 0xf0) ||
231 ((cur[3] & 0xc0) != 0x80))
232 goto encoding_error;
233 /* 4-byte code */
234 *len = 4;
235 val = (cur[0] & 0x7) << 18;
236 val |= (cur[1] & 0x3f) << 12;
237 val |= (cur[2] & 0x3f) << 6;
238 val |= cur[3] & 0x3f;
239 } else {
240 /* 3-byte code */
241 *len = 3;
242 val = (cur[0] & 0xf) << 12;
243 val |= (cur[1] & 0x3f) << 6;
244 val |= cur[2] & 0x3f;
245 }
246 } else {
247 /* 2-byte code */
248 *len = 2;
249 val = (cur[0] & 0x1f) << 6;
250 val |= cur[1] & 0x3f;
251 }
252 if (!IS_CHAR(val)) {
253 ctxt->errNo = XML_ERR_INVALID_ENCODING;
254 if ((ctxt->sax != NULL) &&
255 (ctxt->sax->error != NULL))
256 ctxt->sax->error(ctxt->userData,
257 "Char 0x%X out of allowed range\n", val);
258 ctxt->wellFormed = 0;
259 ctxt->disableSAX = 1;
260 }
261 return(val);
262 } else {
263 /* 1-byte code */
264 *len = 1;
265 return((int) *ctxt->input->cur);
266 }
267 }
268 /*
269 * Assume it's a fixed lenght encoding (1) with
270 * a compatibke encoding for the ASCII set, since
271 * XML constructs only use < 128 chars
272 */
273 *len = 1;
274 if ((int) *ctxt->input->cur < 0x80)
275 return((int) *ctxt->input->cur);
276
277 /*
278 * Humm this is bad, do an automatic flow conversion
279 */
280 xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
281 ctxt->charset = XML_CHAR_ENCODING_UTF8;
282 return(xmlCurrentChar(ctxt, len));
283
284encoding_error:
285 /*
286 * If we detect an UTF8 error that probably mean that the
287 * input encoding didn't get properly advertized in the
288 * declaration header. Report the error and switch the encoding
289 * to ISO-Latin-1 (if you don't like this policy, just declare the
290 * encoding !)
291 */
292 ctxt->errNo = XML_ERR_INVALID_ENCODING;
293 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
294 ctxt->sax->error(ctxt->userData,
295 "Input is not proper UTF-8, indicate encoding !\n");
296 ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
297 ctxt->input->cur[0], ctxt->input->cur[1],
298 ctxt->input->cur[2], ctxt->input->cur[3]);
299 }
300
301 ctxt->charset = XML_CHAR_ENCODING_8859_1;
302 *len = 1;
303 return((int) *ctxt->input->cur);
304}
305
306/**
Owen Taylor3473f882001-02-23 17:55:21 +0000307 * htmlSkipBlankChars:
308 * @ctxt: the HTML parser context
309 *
310 * skip all blanks character found at that point in the input streams.
311 *
312 * Returns the number of space chars skipped
313 */
314
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000315static int
Owen Taylor3473f882001-02-23 17:55:21 +0000316htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
317 int res = 0;
318
319 while (IS_BLANK(*(ctxt->input->cur))) {
320 if ((*ctxt->input->cur == 0) &&
321 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
322 xmlPopInput(ctxt);
323 } else {
324 if (*(ctxt->input->cur) == '\n') {
325 ctxt->input->line++; ctxt->input->col = 1;
326 } else ctxt->input->col++;
327 ctxt->input->cur++;
328 ctxt->nbChars++;
329 if (*ctxt->input->cur == 0)
330 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
331 }
332 res++;
333 }
334 return(res);
335}
336
337
338
339/************************************************************************
340 * *
341 * The list of HTML elements and their properties *
342 * *
343 ************************************************************************/
344
345/*
346 * Start Tag: 1 means the start tag can be ommited
347 * End Tag: 1 means the end tag can be ommited
348 * 2 means it's forbidden (empty elements)
Daniel Veillard56098d42001-04-24 12:51:09 +0000349 * 3 means the tag is stylistic and should be closed easilly
Owen Taylor3473f882001-02-23 17:55:21 +0000350 * Depr: this element is deprecated
351 * DTD: 1 means that this element is valid only in the Loose DTD
352 * 2 means that this element is valid only in the Frameset DTD
353 *
Daniel Veillard02bb1702001-06-13 21:11:59 +0000354 * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
Owen Taylor3473f882001-02-23 17:55:21 +0000355 */
356htmlElemDesc html40ElementTable[] = {
Daniel Veillard02bb1702001-06-13 21:11:59 +0000357{ "a", 0, 0, 0, 0, 0, 0, 1, "anchor " },
358{ "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form" },
359{ "acronym", 0, 0, 0, 0, 0, 0, 1, "" },
360{ "address", 0, 0, 0, 0, 0, 0, 0, "information on author " },
361{ "applet", 0, 0, 0, 0, 1, 1, 2, "java applet " },
362{ "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area " },
363{ "b", 0, 3, 0, 0, 0, 0, 1, "bold text style" },
364{ "base", 0, 2, 2, 1, 0, 0, 0, "document base uri " },
365{ "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " },
366{ "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride " },
367{ "big", 0, 3, 0, 0, 0, 0, 1, "large text style" },
368{ "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation " },
369{ "body", 1, 1, 0, 0, 0, 0, 0, "document body " },
370{ "br", 0, 2, 2, 1, 0, 0, 1, "forced line break " },
371{ "button", 0, 0, 0, 0, 0, 0, 2, "push button " },
372{ "caption", 0, 0, 0, 0, 0, 0, 0, "table caption " },
373{ "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center " },
374{ "cite", 0, 0, 0, 0, 0, 0, 1, "citation" },
375{ "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment" },
376{ "col", 0, 2, 2, 1, 0, 0, 0, "table column " },
377{ "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group " },
378{ "dd", 0, 1, 0, 0, 0, 0, 0, "definition description " },
379{ "del", 0, 0, 0, 0, 0, 0, 2, "deleted text " },
380{ "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition" },
381{ "dir", 0, 0, 0, 0, 1, 1, 0, "directory list" },
382{ "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container"},
383{ "dl", 0, 0, 0, 0, 0, 0, 0, "definition list " },
384{ "dt", 0, 1, 0, 0, 0, 0, 0, "definition term " },
385{ "em", 0, 3, 0, 0, 0, 0, 1, "emphasis" },
386{ "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group " },
387{ "font", 0, 3, 0, 0, 1, 1, 1, "local change to font " },
388{ "form", 0, 0, 0, 0, 0, 0, 0, "interactive form " },
389{ "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " },
390{ "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" },
391{ "h1", 0, 0, 0, 0, 0, 0, 0, "heading " },
392{ "h2", 0, 0, 0, 0, 0, 0, 0, "heading " },
393{ "h3", 0, 0, 0, 0, 0, 0, 0, "heading " },
394{ "h4", 0, 0, 0, 0, 0, 0, 0, "heading " },
395{ "h5", 0, 0, 0, 0, 0, 0, 0, "heading " },
396{ "h6", 0, 0, 0, 0, 0, 0, 0, "heading " },
397{ "head", 1, 1, 0, 0, 0, 0, 0, "document head " },
398{ "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " },
399{ "html", 1, 1, 0, 0, 0, 0, 0, "document root element " },
400{ "i", 0, 3, 0, 0, 0, 0, 1, "italic text style" },
401{ "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow " },
402{ "img", 0, 2, 2, 1, 0, 0, 1, "embedded image " },
403{ "input", 0, 2, 2, 1, 0, 0, 1, "form control " },
404{ "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text" },
405{ "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt " },
406{ "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user" },
407{ "label", 0, 0, 0, 0, 0, 0, 1, "form field label text " },
408{ "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend " },
409{ "li", 0, 1, 1, 0, 0, 0, 0, "list item " },
410{ "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link " },
411{ "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map " },
412{ "menu", 0, 0, 0, 0, 1, 1, 0, "menu list " },
413{ "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation " },
414{ "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering " },
415{ "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering " },
416{ "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object " },
417{ "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list " },
418{ "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group " },
419{ "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " },
420{ "p", 0, 1, 1, 0, 0, 0, 0, "paragraph " },
421{ "param", 0, 2, 2, 1, 0, 0, 0, "named property value " },
422{ "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text " },
423{ "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation " },
424{ "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style" },
425{ "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc." },
426{ "script", 0, 0, 0, 0, 0, 0, 2, "script statements " },
427{ "select", 0, 0, 0, 0, 0, 0, 1, "option selector " },
428{ "small", 0, 3, 0, 0, 0, 0, 1, "small text style" },
429{ "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container " },
430{ "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text" },
431{ "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis" },
432{ "style", 0, 0, 0, 0, 0, 0, 0, "style info " },
433{ "sub", 0, 3, 0, 0, 0, 0, 1, "subscript" },
434{ "sup", 0, 3, 0, 0, 0, 0, 1, "superscript " },
435{ "table", 0, 0, 0, 0, 0, 0, 0, "&#160;" },
436{ "tbody", 1, 0, 0, 0, 0, 0, 0, "table body " },
437{ "td", 0, 0, 0, 0, 0, 0, 0, "table data cell" },
438{ "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field " },
439{ "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer " },
440{ "th", 0, 1, 0, 0, 0, 0, 0, "table header cell" },
441{ "thead", 0, 1, 0, 0, 0, 0, 0, "table header " },
442{ "title", 0, 0, 0, 0, 0, 0, 0, "document title " },
443{ "tr", 0, 0, 0, 0, 0, 0, 0, "table row " },
444{ "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style" },
445{ "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style" },
446{ "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list " },
447{ "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument" },
Owen Taylor3473f882001-02-23 17:55:21 +0000448};
449
450/*
451 * start tags that imply the end of a current element
452 * any tag of each line implies the end of the current element if the type of
453 * that element is in the same line
454 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000455const char *htmlEquEnd[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000456"dt", "dd", "li", "option", NULL,
457"h1", "h2", "h3", "h4", "h5", "h6", NULL,
458"ol", "menu", "dir", "address", "pre", "listing", "xmp", NULL,
459NULL
460};
461/*
462 * acording the HTML DTD, HR should be added to the 2nd line above, as it
463 * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
464 * because many documents contain rules in headings...
465 */
466
467/*
468 * start tags that imply the end of current element
469 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000470const char *htmlStartClose[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000471"form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
472 "dl", "ul", "ol", "menu", "dir", "address", "pre",
473 "listing", "xmp", "head", NULL,
474"head", "p", NULL,
475"title", "p", NULL,
476"body", "head", "style", "link", "title", "p", NULL,
477"li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
478 "pre", "listing", "xmp", "head", "li", NULL,
479"hr", "p", "head", NULL,
480"h1", "p", "head", NULL,
481"h2", "p", "head", NULL,
482"h3", "p", "head", NULL,
483"h4", "p", "head", NULL,
484"h5", "p", "head", NULL,
485"h6", "p", "head", NULL,
486"dir", "p", "head", NULL,
487"address", "p", "head", "ul", NULL,
488"pre", "p", "head", "ul", NULL,
489"listing", "p", "head", NULL,
490"xmp", "p", "head", NULL,
491"blockquote", "p", "head", NULL,
492"dl", "p", "dt", "menu", "dir", "address", "pre", "listing",
493 "xmp", "head", NULL,
494"dt", "p", "menu", "dir", "address", "pre", "listing", "xmp",
495 "head", "dd", NULL,
496"dd", "p", "menu", "dir", "address", "pre", "listing", "xmp",
497 "head", "dt", NULL,
498"ul", "p", "head", "ol", "menu", "dir", "address", "pre",
499 "listing", "xmp", NULL,
500"ol", "p", "head", "ul", NULL,
501"menu", "p", "head", "ul", NULL,
502"p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL,
503"div", "p", "head", NULL,
504"noscript", "p", "head", NULL,
505"center", "font", "b", "i", "p", "head", NULL,
506"a", "a", NULL,
507"caption", "p", NULL,
508"colgroup", "caption", "colgroup", "col", "p", NULL,
509"col", "caption", "col", "p", NULL,
510"table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
511 "listing", "xmp", "a", NULL,
Daniel Veillard43dadeb2001-04-24 11:23:35 +0000512"th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
513"td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000514"tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
515"thead", "caption", "col", "colgroup", NULL,
516"tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead",
517 "tbody", "p", NULL,
518"tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead",
519 "tfoot", "tbody", "p", NULL,
520"optgroup", "option", NULL,
521"option", "option", NULL,
522"fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
523 "pre", "listing", "xmp", "a", NULL,
524NULL
525};
526
527/*
528 * The list of HTML elements which are supposed not to have
529 * CDATA content and where a p element will be implied
530 *
531 * TODO: extend that list by reading the HTML SGML DtD on
532 * implied paragraph
533 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000534static const char *htmlNoContentElements[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000535 "html",
536 "head",
537 "body",
538 NULL
539};
540
541/*
542 * The list of HTML attributes which are of content %Script;
543 * NOTE: when adding ones, check htmlIsScriptAttribute() since
544 * it assumes the name starts with 'on'
545 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000546static const char *htmlScriptAttributes[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000547 "onclick",
548 "ondblclick",
549 "onmousedown",
550 "onmouseup",
551 "onmouseover",
552 "onmousemove",
553 "onmouseout",
554 "onkeypress",
555 "onkeydown",
556 "onkeyup",
557 "onload",
558 "onunload",
559 "onfocus",
560 "onblur",
561 "onsubmit",
562 "onrest",
563 "onchange",
564 "onselect"
565};
566
Daniel Veillarda2bc3682001-05-03 08:27:20 +0000567/*
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000568 * This table is used by the htmlparser to know what to do with
569 * broken html pages. By assigning different priorities to different
570 * elements the parser can decide how to handle extra endtags.
571 * Endtags are only allowed to close elements with lower or equal
572 * priority.
573 */
Daniel Veillarda2bc3682001-05-03 08:27:20 +0000574
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000575typedef struct {
576 const char *name;
577 int priority;
578} elementPriority;
579
580const elementPriority htmlEndPriority[] = {
581 {"div", 150},
582 {"td", 160},
583 {"th", 160},
584 {"tr", 170},
585 {"thead", 180},
586 {"tbody", 180},
587 {"tfoot", 180},
588 {"table", 190},
589 {"head", 200},
590 {"body", 200},
591 {"html", 220},
592 {NULL, 100} /* Default priority */
593};
Owen Taylor3473f882001-02-23 17:55:21 +0000594
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000595static const char** htmlStartCloseIndex[100];
Owen Taylor3473f882001-02-23 17:55:21 +0000596static int htmlStartCloseIndexinitialized = 0;
597
598/************************************************************************
599 * *
600 * functions to handle HTML specific data *
601 * *
602 ************************************************************************/
603
604/**
605 * htmlInitAutoClose:
606 *
607 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
608 * This is not reentrant. Call xmlInitParser() once before processing in
609 * case of use in multithreaded programs.
610 */
611void
612htmlInitAutoClose(void) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000613 int indx, i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000614
615 if (htmlStartCloseIndexinitialized) return;
616
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000617 for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL;
618 indx = 0;
619 while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) {
620 htmlStartCloseIndex[indx++] = &htmlStartClose[i];
Owen Taylor3473f882001-02-23 17:55:21 +0000621 while (htmlStartClose[i] != NULL) i++;
622 i++;
623 }
624 htmlStartCloseIndexinitialized = 1;
625}
626
627/**
628 * htmlTagLookup:
629 * @tag: The tag name in lowercase
630 *
631 * Lookup the HTML tag in the ElementTable
632 *
633 * Returns the related htmlElemDescPtr or NULL if not found.
634 */
635htmlElemDescPtr
636htmlTagLookup(const xmlChar *tag) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000637 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +0000638
639 for (i = 0; i < (sizeof(html40ElementTable) /
640 sizeof(html40ElementTable[0]));i++) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000641 if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name))
Owen Taylor3473f882001-02-23 17:55:21 +0000642 return(&html40ElementTable[i]);
643 }
644 return(NULL);
645}
646
647/**
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000648 * htmlGetEndPriority:
649 * @name: The name of the element to look up the priority for.
650 *
651 * Return value: The "endtag" priority.
652 **/
653static int
654htmlGetEndPriority (const xmlChar *name) {
655 int i = 0;
656
657 while ((htmlEndPriority[i].name != NULL) &&
658 (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
659 i++;
660
661 return(htmlEndPriority[i].priority);
662}
663
664/**
Owen Taylor3473f882001-02-23 17:55:21 +0000665 * htmlCheckAutoClose:
666 * @newtag: The new tag name
667 * @oldtag: The old tag name
668 *
669 * Checks wether the new tag is one of the registered valid tags for closing old.
670 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
671 *
672 * Returns 0 if no, 1 if yes.
673 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000674static int
Owen Taylor3473f882001-02-23 17:55:21 +0000675htmlCheckAutoClose(const xmlChar *newtag, const xmlChar *oldtag) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000676 int i, indx;
677 const char **closed = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000678
679 if (htmlStartCloseIndexinitialized == 0) htmlInitAutoClose();
680
681 /* inefficient, but not a big deal */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000682 for (indx = 0; indx < 100;indx++) {
683 closed = htmlStartCloseIndex[indx];
684 if (closed == NULL) return(0);
685 if (xmlStrEqual(BAD_CAST *closed, newtag)) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000686 }
687
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000688 i = closed - htmlStartClose;
Owen Taylor3473f882001-02-23 17:55:21 +0000689 i++;
690 while (htmlStartClose[i] != NULL) {
691 if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) {
692 return(1);
693 }
694 i++;
695 }
696 return(0);
697}
698
699/**
700 * htmlAutoCloseOnClose:
701 * @ctxt: an HTML parser context
702 * @newtag: The new tag name
Daniel Veillarda3bfca52001-04-12 15:42:58 +0000703 * @force: force the tag closure
Owen Taylor3473f882001-02-23 17:55:21 +0000704 *
705 * The HTmL DtD allows an ending tag to implicitely close other tags.
706 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000707static void
Owen Taylor3473f882001-02-23 17:55:21 +0000708htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
709 htmlElemDescPtr info;
710 xmlChar *oldname;
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000711 int i, priority;
Owen Taylor3473f882001-02-23 17:55:21 +0000712
713#ifdef DEBUG
714 xmlGenericError(xmlGenericErrorContext,"Close of %s stack: %d elements\n", newtag, ctxt->nameNr);
715 for (i = 0;i < ctxt->nameNr;i++)
716 xmlGenericError(xmlGenericErrorContext,"%d : %s\n", i, ctxt->nameTab[i]);
717#endif
718
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000719 priority = htmlGetEndPriority (newtag);
720
Owen Taylor3473f882001-02-23 17:55:21 +0000721 for (i = (ctxt->nameNr - 1);i >= 0;i--) {
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000722
Owen Taylor3473f882001-02-23 17:55:21 +0000723 if (xmlStrEqual(newtag, ctxt->nameTab[i])) break;
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000724 /*
725 * A missplaced endtagad can only close elements with lower
726 * or equal priority, so if we find an element with higher
727 * priority before we find an element with
728 * matching name, we just ignore this endtag
729 */
730 if (htmlGetEndPriority (ctxt->nameTab[i]) > priority) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000731 }
732 if (i < 0) return;
733
734 while (!xmlStrEqual(newtag, ctxt->name)) {
735 info = htmlTagLookup(ctxt->name);
736 if ((info == NULL) || (info->endTag == 1)) {
737#ifdef DEBUG
738 xmlGenericError(xmlGenericErrorContext,"htmlAutoCloseOnClose: %s closes %s\n", newtag, ctxt->name);
739#endif
Daniel Veillard56098d42001-04-24 12:51:09 +0000740 } else if (info->endTag == 3) {
741#ifdef DEBUG
Daniel Veillardf69bb4b2001-05-19 13:24:56 +0000742 xmlGenericError(xmlGenericErrorContext,"End of tag %s: expecting %s\n", newtag, ctxt->name);
Daniel Veillard56098d42001-04-24 12:51:09 +0000743#endif
744 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
745 ctxt->sax->error(ctxt->userData,
746 "Opening and ending tag mismatch: %s and %s\n",
747 newtag, ctxt->name);
748 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000749 }
750 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
751 ctxt->sax->endElement(ctxt->userData, ctxt->name);
752 oldname = htmlnamePop(ctxt);
753 if (oldname != NULL) {
754#ifdef DEBUG
755 xmlGenericError(xmlGenericErrorContext,"htmlAutoCloseOnClose: popped %s\n", oldname);
756#endif
757 xmlFree(oldname);
758 }
759 }
760}
761
762/**
Daniel Veillarda3bfca52001-04-12 15:42:58 +0000763 * htmlAutoCloseOnEnd:
764 * @ctxt: an HTML parser context
765 *
766 * Close all remaining tags at the end of the stream
767 */
768static void
769htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt) {
770 xmlChar *oldname;
771 int i;
772
773 if (ctxt->nameNr == 0)
774 return;
775#ifdef DEBUG
776 xmlGenericError(xmlGenericErrorContext,"Close of stack: %d elements\n", ctxt->nameNr);
777#endif
778
779 for (i = (ctxt->nameNr - 1);i >= 0;i--) {
780#ifdef DEBUG
781 xmlGenericError(xmlGenericErrorContext,"%d : %s\n", i, ctxt->nameTab[i]);
782#endif
783 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
784 ctxt->sax->endElement(ctxt->userData, ctxt->name);
785 oldname = htmlnamePop(ctxt);
786 if (oldname != NULL) {
787#ifdef DEBUG
788 xmlGenericError(xmlGenericErrorContext,"htmlAutoCloseOnEnd: popped %s\n", oldname);
789#endif
790 xmlFree(oldname);
791 }
792 }
793}
794
795/**
Owen Taylor3473f882001-02-23 17:55:21 +0000796 * htmlAutoClose:
797 * @ctxt: an HTML parser context
798 * @newtag: The new tag name or NULL
799 *
800 * The HTmL DtD allows a tag to implicitely close other tags.
801 * The list is kept in htmlStartClose array. This function is
802 * called when a new tag has been detected and generates the
803 * appropriates closes if possible/needed.
804 * If newtag is NULL this mean we are at the end of the resource
805 * and we should check
806 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000807static void
Owen Taylor3473f882001-02-23 17:55:21 +0000808htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
809 xmlChar *oldname;
810 while ((newtag != NULL) && (ctxt->name != NULL) &&
811 (htmlCheckAutoClose(newtag, ctxt->name))) {
812#ifdef DEBUG
813 xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: %s closes %s\n", newtag, ctxt->name);
814#endif
815 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
816 ctxt->sax->endElement(ctxt->userData, ctxt->name);
817 oldname = htmlnamePop(ctxt);
818 if (oldname != NULL) {
819#ifdef DEBUG
820 xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: popped %s\n", oldname);
821#endif
822 xmlFree(oldname);
823 }
824 }
825 if (newtag == NULL) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +0000826 htmlAutoCloseOnEnd(ctxt);
827 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000828 }
829 while ((newtag == NULL) && (ctxt->name != NULL) &&
830 ((xmlStrEqual(ctxt->name, BAD_CAST"head")) ||
831 (xmlStrEqual(ctxt->name, BAD_CAST"body")) ||
832 (xmlStrEqual(ctxt->name, BAD_CAST"html")))) {
833#ifdef DEBUG
834 xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: EOF closes %s\n", ctxt->name);
835#endif
836 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
837 ctxt->sax->endElement(ctxt->userData, ctxt->name);
838 oldname = htmlnamePop(ctxt);
839 if (oldname != NULL) {
840#ifdef DEBUG
841 xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: popped %s\n", oldname);
842#endif
843 xmlFree(oldname);
844 }
845 }
846
847}
848
849/**
850 * htmlAutoCloseTag:
851 * @doc: the HTML document
852 * @name: The tag name
853 * @elem: the HTML element
854 *
855 * The HTmL DtD allows a tag to implicitely close other tags.
856 * The list is kept in htmlStartClose array. This function checks
857 * if the element or one of it's children would autoclose the
858 * given tag.
859 *
860 * Returns 1 if autoclose, 0 otherwise
861 */
862int
863htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
864 htmlNodePtr child;
865
866 if (elem == NULL) return(1);
867 if (xmlStrEqual(name, elem->name)) return(0);
868 if (htmlCheckAutoClose(elem->name, name)) return(1);
869 child = elem->children;
870 while (child != NULL) {
871 if (htmlAutoCloseTag(doc, name, child)) return(1);
872 child = child->next;
873 }
874 return(0);
875}
876
877/**
878 * htmlIsAutoClosed:
879 * @doc: the HTML document
880 * @elem: the HTML element
881 *
882 * The HTmL DtD allows a tag to implicitely close other tags.
883 * The list is kept in htmlStartClose array. This function checks
884 * if a tag is autoclosed by one of it's child
885 *
886 * Returns 1 if autoclosed, 0 otherwise
887 */
888int
889htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
890 htmlNodePtr child;
891
892 if (elem == NULL) return(1);
893 child = elem->children;
894 while (child != NULL) {
895 if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
896 child = child->next;
897 }
898 return(0);
899}
900
901/**
902 * htmlCheckImplied:
903 * @ctxt: an HTML parser context
904 * @newtag: The new tag name
905 *
906 * The HTML DtD allows a tag to exists only implicitely
907 * called when a new tag has been detected and generates the
908 * appropriates implicit tags if missing
909 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000910static void
Owen Taylor3473f882001-02-23 17:55:21 +0000911htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
912 if (!htmlOmittedDefaultValue)
913 return;
914 if (xmlStrEqual(newtag, BAD_CAST"html"))
915 return;
916 if (ctxt->nameNr <= 0) {
917#ifdef DEBUG
918 xmlGenericError(xmlGenericErrorContext,"Implied element html: pushed html\n");
919#endif
920 htmlnamePush(ctxt, xmlStrdup(BAD_CAST"html"));
921 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
922 ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
923 }
924 if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
925 return;
926 if ((ctxt->nameNr <= 1) &&
927 ((xmlStrEqual(newtag, BAD_CAST"script")) ||
928 (xmlStrEqual(newtag, BAD_CAST"style")) ||
929 (xmlStrEqual(newtag, BAD_CAST"meta")) ||
930 (xmlStrEqual(newtag, BAD_CAST"link")) ||
931 (xmlStrEqual(newtag, BAD_CAST"title")) ||
932 (xmlStrEqual(newtag, BAD_CAST"base")))) {
933 /*
934 * dropped OBJECT ... i you put it first BODY will be
935 * assumed !
936 */
937#ifdef DEBUG
938 xmlGenericError(xmlGenericErrorContext,"Implied element head: pushed head\n");
939#endif
940 htmlnamePush(ctxt, xmlStrdup(BAD_CAST"head"));
941 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
942 ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
943 } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
944 (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
945 (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
946 int i;
947 for (i = 0;i < ctxt->nameNr;i++) {
948 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
949 return;
950 }
951 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
952 return;
953 }
954 }
955
956#ifdef DEBUG
957 xmlGenericError(xmlGenericErrorContext,"Implied element body: pushed body\n");
958#endif
959 htmlnamePush(ctxt, xmlStrdup(BAD_CAST"body"));
960 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
961 ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
962 }
963}
964
965/**
966 * htmlCheckParagraph
967 * @ctxt: an HTML parser context
968 *
969 * Check whether a p element need to be implied before inserting
970 * characters in the current element.
971 *
972 * Returns 1 if a paragraph has been inserted, 0 if not and -1
973 * in case of error.
974 */
975
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000976static int
Owen Taylor3473f882001-02-23 17:55:21 +0000977htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
978 const xmlChar *tag;
979 int i;
980
981 if (ctxt == NULL)
982 return(-1);
983 tag = ctxt->name;
984 if (tag == NULL) {
985 htmlAutoClose(ctxt, BAD_CAST"p");
986 htmlCheckImplied(ctxt, BAD_CAST"p");
987 htmlnamePush(ctxt, xmlStrdup(BAD_CAST"p"));
988 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
989 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
990 return(1);
991 }
992 if (!htmlOmittedDefaultValue)
993 return(0);
994 for (i = 0; htmlNoContentElements[i] != NULL; i++) {
995 if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
996#ifdef DEBUG
997 xmlGenericError(xmlGenericErrorContext,"Implied element paragraph\n");
998#endif
999 htmlAutoClose(ctxt, BAD_CAST"p");
1000 htmlCheckImplied(ctxt, BAD_CAST"p");
1001 htmlnamePush(ctxt, xmlStrdup(BAD_CAST"p"));
1002 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1003 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1004 return(1);
1005 }
1006 }
1007 return(0);
1008}
1009
1010/**
1011 * htmlIsScriptAttribute:
1012 * @name: an attribute name
1013 *
1014 * Check if an attribute is of content type Script
1015 *
1016 * Returns 1 is the attribute is a script 0 otherwise
1017 */
1018int
1019htmlIsScriptAttribute(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001020 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001021
1022 if (name == NULL)
1023 return(0);
1024 /*
1025 * all script attributes start with 'on'
1026 */
1027 if ((name[0] != 'o') || (name[1] != 'n'))
1028 return(0);
1029 for (i = 0;
1030 i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
1031 i++) {
1032 if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
1033 return(1);
1034 }
1035 return(0);
1036}
1037
1038/************************************************************************
1039 * *
1040 * The list of HTML predefined entities *
1041 * *
1042 ************************************************************************/
1043
1044
1045htmlEntityDesc html40EntitiesTable[] = {
1046/*
1047 * the 4 absolute ones, plus apostrophe.
1048 */
1049{ 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1050{ 38, "amp", "ampersand, U+0026 ISOnum" },
1051{ 39, "apos", "single quote" },
1052{ 60, "lt", "less-than sign, U+003C ISOnum" },
1053{ 62, "gt", "greater-than sign, U+003E ISOnum" },
1054
1055/*
1056 * A bunch still in the 128-255 range
1057 * Replacing them depend really on the charset used.
1058 */
1059{ 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1060{ 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1061{ 162, "cent", "cent sign, U+00A2 ISOnum" },
1062{ 163, "pound","pound sign, U+00A3 ISOnum" },
1063{ 164, "curren","currency sign, U+00A4 ISOnum" },
1064{ 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
1065{ 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1066{ 167, "sect", "section sign, U+00A7 ISOnum" },
1067{ 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1068{ 169, "copy", "copyright sign, U+00A9 ISOnum" },
1069{ 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1070{ 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1071{ 172, "not", "not sign, U+00AC ISOnum" },
1072{ 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1073{ 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
1074{ 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1075{ 176, "deg", "degree sign, U+00B0 ISOnum" },
1076{ 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1077{ 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1078{ 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1079{ 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1080{ 181, "micro","micro sign, U+00B5 ISOnum" },
1081{ 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1082{ 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1083{ 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1084{ 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1085{ 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1086{ 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1087{ 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1088{ 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1089{ 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1090{ 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1091{ 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1092{ 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1093{ 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1094{ 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1095{ 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1096{ 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1097{ 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1098{ 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1099{ 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1100{ 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1101{ 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1102{ 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1103{ 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1104{ 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1105{ 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1106{ 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1107{ 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
1108{ 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1109{ 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1110{ 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1111{ 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1112{ 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1113{ 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1114{ 215, "times","multiplication sign, U+00D7 ISOnum" },
1115{ 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1116{ 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1117{ 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1118{ 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1119{ 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1120{ 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1121{ 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1122{ 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1123{ 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1124{ 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1125{ 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1126{ 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1127{ 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1128{ 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1129{ 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1130{ 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1131{ 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1132{ 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1133{ 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1134{ 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1135{ 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1136{ 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1137{ 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1138{ 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1139{ 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
1140{ 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1141{ 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1142{ 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1143{ 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1144{ 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1145{ 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1146{ 247, "divide","division sign, U+00F7 ISOnum" },
1147{ 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1148{ 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1149{ 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1150{ 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1151{ 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1152{ 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1153{ 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1154{ 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1155
1156{ 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1157{ 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
1158{ 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1159{ 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1160{ 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1161
1162/*
1163 * Anything below should really be kept as entities references
1164 */
1165{ 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1166
1167{ 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1168{ 732, "tilde","small tilde, U+02DC ISOdia" },
1169
1170{ 913, "Alpha","greek capital letter alpha, U+0391" },
1171{ 914, "Beta", "greek capital letter beta, U+0392" },
1172{ 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1173{ 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1174{ 917, "Epsilon","greek capital letter epsilon, U+0395" },
1175{ 918, "Zeta", "greek capital letter zeta, U+0396" },
1176{ 919, "Eta", "greek capital letter eta, U+0397" },
1177{ 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1178{ 921, "Iota", "greek capital letter iota, U+0399" },
1179{ 922, "Kappa","greek capital letter kappa, U+039A" },
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001180{ 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
Owen Taylor3473f882001-02-23 17:55:21 +00001181{ 924, "Mu", "greek capital letter mu, U+039C" },
1182{ 925, "Nu", "greek capital letter nu, U+039D" },
1183{ 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1184{ 927, "Omicron","greek capital letter omicron, U+039F" },
1185{ 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1186{ 929, "Rho", "greek capital letter rho, U+03A1" },
1187{ 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1188{ 932, "Tau", "greek capital letter tau, U+03A4" },
1189{ 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1190{ 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
1191{ 935, "Chi", "greek capital letter chi, U+03A7" },
1192{ 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
1193{ 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1194
1195{ 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1196{ 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1197{ 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1198{ 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
1199{ 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1200{ 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1201{ 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
1202{ 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
1203{ 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1204{ 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1205{ 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1206{ 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
1207{ 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
1208{ 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
1209{ 959, "omicron","greek small letter omicron, U+03BF NEW" },
1210{ 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1211{ 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
1212{ 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1213{ 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1214{ 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
1215{ 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1216{ 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
1217{ 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
1218{ 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
1219{ 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
1220{ 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1221{ 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1222{ 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
1223
1224{ 8194, "ensp", "en space, U+2002 ISOpub" },
1225{ 8195, "emsp", "em space, U+2003 ISOpub" },
1226{ 8201, "thinsp","thin space, U+2009 ISOpub" },
1227{ 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1228{ 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
1229{ 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
1230{ 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
1231{ 8211, "ndash","en dash, U+2013 ISOpub" },
1232{ 8212, "mdash","em dash, U+2014 ISOpub" },
1233{ 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1234{ 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1235{ 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1236{ 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1237{ 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1238{ 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1239{ 8224, "dagger","dagger, U+2020 ISOpub" },
1240{ 8225, "Dagger","double dagger, U+2021 ISOpub" },
1241
1242{ 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1243{ 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1244
1245{ 8240, "permil","per mille sign, U+2030 ISOtech" },
1246
1247{ 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1248{ 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1249
1250{ 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1251{ 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1252
1253{ 8254, "oline","overline = spacing overscore, U+203E NEW" },
1254{ 8260, "frasl","fraction slash, U+2044 NEW" },
1255
1256{ 8364, "euro", "euro sign, U+20AC NEW" },
1257
1258{ 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1259{ 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1260{ 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
1261{ 8482, "trade","trade mark sign, U+2122 ISOnum" },
1262{ 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
1263{ 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
1264{ 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
1265{ 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
1266{ 8595, "darr", "downwards arrow, U+2193 ISOnum" },
1267{ 8596, "harr", "left right arrow, U+2194 ISOamsa" },
1268{ 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
1269{ 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
1270{ 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
1271{ 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
1272{ 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
1273{ 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
1274
1275{ 8704, "forall","for all, U+2200 ISOtech" },
1276{ 8706, "part", "partial differential, U+2202 ISOtech" },
1277{ 8707, "exist","there exists, U+2203 ISOtech" },
1278{ 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
1279{ 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
1280{ 8712, "isin", "element of, U+2208 ISOtech" },
1281{ 8713, "notin","not an element of, U+2209 ISOtech" },
1282{ 8715, "ni", "contains as member, U+220B ISOtech" },
1283{ 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
1284{ 8721, "sum", "n-ary sumation, U+2211 ISOamsb" },
1285{ 8722, "minus","minus sign, U+2212 ISOtech" },
1286{ 8727, "lowast","asterisk operator, U+2217 ISOtech" },
1287{ 8730, "radic","square root = radical sign, U+221A ISOtech" },
1288{ 8733, "prop", "proportional to, U+221D ISOtech" },
1289{ 8734, "infin","infinity, U+221E ISOtech" },
1290{ 8736, "ang", "angle, U+2220 ISOamso" },
1291{ 8743, "and", "logical and = wedge, U+2227 ISOtech" },
1292{ 8744, "or", "logical or = vee, U+2228 ISOtech" },
1293{ 8745, "cap", "intersection = cap, U+2229 ISOtech" },
1294{ 8746, "cup", "union = cup, U+222A ISOtech" },
1295{ 8747, "int", "integral, U+222B ISOtech" },
1296{ 8756, "there4","therefore, U+2234 ISOtech" },
1297{ 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
1298{ 8773, "cong", "approximately equal to, U+2245 ISOtech" },
1299{ 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
1300{ 8800, "ne", "not equal to, U+2260 ISOtech" },
1301{ 8801, "equiv","identical to, U+2261 ISOtech" },
1302{ 8804, "le", "less-than or equal to, U+2264 ISOtech" },
1303{ 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
1304{ 8834, "sub", "subset of, U+2282 ISOtech" },
1305{ 8835, "sup", "superset of, U+2283 ISOtech" },
1306{ 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
1307{ 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
1308{ 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
1309{ 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
1310{ 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
1311{ 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
1312{ 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
1313{ 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
1314{ 8969, "rceil","right ceiling, U+2309 ISOamsc" },
1315{ 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
1316{ 8971, "rfloor","right floor, U+230B ISOamsc" },
1317{ 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
1318{ 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
1319{ 9674, "loz", "lozenge, U+25CA ISOpub" },
1320
1321{ 9824, "spades","black spade suit, U+2660 ISOpub" },
1322{ 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
1323{ 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
1324{ 9830, "diams","black diamond suit, U+2666 ISOpub" },
1325
1326};
1327
1328/************************************************************************
1329 * *
1330 * Commodity functions to handle entities *
1331 * *
1332 ************************************************************************/
1333
1334/*
1335 * Macro used to grow the current buffer.
1336 */
1337#define growBuffer(buffer) { \
1338 buffer##_size *= 2; \
1339 buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
1340 if (buffer == NULL) { \
1341 perror("realloc failed"); \
1342 return(NULL); \
1343 } \
1344}
1345
1346/**
1347 * htmlEntityLookup:
1348 * @name: the entity name
1349 *
1350 * Lookup the given entity in EntitiesTable
1351 *
1352 * TODO: the linear scan is really ugly, an hash table is really needed.
1353 *
1354 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1355 */
1356htmlEntityDescPtr
1357htmlEntityLookup(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001358 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001359
1360 for (i = 0;i < (sizeof(html40EntitiesTable)/
1361 sizeof(html40EntitiesTable[0]));i++) {
1362 if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
1363#ifdef DEBUG
1364 xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", name);
1365#endif
1366 return(&html40EntitiesTable[i]);
1367 }
1368 }
1369 return(NULL);
1370}
1371
1372/**
1373 * htmlEntityValueLookup:
1374 * @value: the entity's unicode value
1375 *
1376 * Lookup the given entity in EntitiesTable
1377 *
1378 * TODO: the linear scan is really ugly, an hash table is really needed.
1379 *
1380 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1381 */
1382htmlEntityDescPtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001383htmlEntityValueLookup(unsigned int value) {
1384 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001385#ifdef DEBUG
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00001386 unsigned int lv = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001387#endif
1388
1389 for (i = 0;i < (sizeof(html40EntitiesTable)/
1390 sizeof(html40EntitiesTable[0]));i++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001391 if (html40EntitiesTable[i].value >= value) {
1392 if (html40EntitiesTable[i].value > value)
Owen Taylor3473f882001-02-23 17:55:21 +00001393 break;
1394#ifdef DEBUG
1395 xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", html40EntitiesTable[i].name);
1396#endif
1397 return(&html40EntitiesTable[i]);
1398 }
1399#ifdef DEBUG
1400 if (lv > html40EntitiesTable[i].value) {
1401 xmlGenericError(xmlGenericErrorContext,
1402 "html40EntitiesTable[] is not sorted (%d > %d)!\n",
1403 lv, html40EntitiesTable[i].value);
1404 }
1405 lv = html40EntitiesTable[i].value;
1406#endif
1407 }
1408 return(NULL);
1409}
1410
1411/**
1412 * UTF8ToHtml:
1413 * @out: a pointer to an array of bytes to store the result
1414 * @outlen: the length of @out
1415 * @in: a pointer to an array of UTF-8 chars
1416 * @inlen: the length of @in
1417 *
1418 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1419 * plus HTML entities block of chars out.
1420 *
1421 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1422 * The value of @inlen after return is the number of octets consumed
1423 * as the return value is positive, else unpredictiable.
1424 * The value of @outlen after return is the number of octets consumed.
1425 */
1426int
1427UTF8ToHtml(unsigned char* out, int *outlen,
1428 const unsigned char* in, int *inlen) {
1429 const unsigned char* processed = in;
1430 const unsigned char* outend;
1431 const unsigned char* outstart = out;
1432 const unsigned char* instart = in;
1433 const unsigned char* inend;
1434 unsigned int c, d;
1435 int trailing;
1436
1437 if (in == NULL) {
1438 /*
1439 * initialization nothing to do
1440 */
1441 *outlen = 0;
1442 *inlen = 0;
1443 return(0);
1444 }
1445 inend = in + (*inlen);
1446 outend = out + (*outlen);
1447 while (in < inend) {
1448 d = *in++;
1449 if (d < 0x80) { c= d; trailing= 0; }
1450 else if (d < 0xC0) {
1451 /* trailing byte in leading position */
1452 *outlen = out - outstart;
1453 *inlen = processed - instart;
1454 return(-2);
1455 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1456 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1457 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1458 else {
1459 /* no chance for this in Ascii */
1460 *outlen = out - outstart;
1461 *inlen = processed - instart;
1462 return(-2);
1463 }
1464
1465 if (inend - in < trailing) {
1466 break;
1467 }
1468
1469 for ( ; trailing; trailing--) {
1470 if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
1471 break;
1472 c <<= 6;
1473 c |= d & 0x3F;
1474 }
1475
1476 /* assertion: c is a single UTF-4 value */
1477 if (c < 0x80) {
1478 if (out + 1 >= outend)
1479 break;
1480 *out++ = c;
1481 } else {
1482 int len;
1483 htmlEntityDescPtr ent;
1484
1485 /*
1486 * Try to lookup a predefined HTML entity for it
1487 */
1488
1489 ent = htmlEntityValueLookup(c);
1490 if (ent == NULL) {
1491 /* no chance for this in Ascii */
1492 *outlen = out - outstart;
1493 *inlen = processed - instart;
1494 return(-2);
1495 }
1496 len = strlen(ent->name);
1497 if (out + 2 + len >= outend)
1498 break;
1499 *out++ = '&';
1500 memcpy(out, ent->name, len);
1501 out += len;
1502 *out++ = ';';
1503 }
1504 processed = in;
1505 }
1506 *outlen = out - outstart;
1507 *inlen = processed - instart;
1508 return(0);
1509}
1510
1511/**
1512 * htmlEncodeEntities:
1513 * @out: a pointer to an array of bytes to store the result
1514 * @outlen: the length of @out
1515 * @in: a pointer to an array of UTF-8 chars
1516 * @inlen: the length of @in
1517 * @quoteChar: the quote character to escape (' or ") or zero.
1518 *
1519 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1520 * plus HTML entities block of chars out.
1521 *
1522 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1523 * The value of @inlen after return is the number of octets consumed
1524 * as the return value is positive, else unpredictiable.
1525 * The value of @outlen after return is the number of octets consumed.
1526 */
1527int
1528htmlEncodeEntities(unsigned char* out, int *outlen,
1529 const unsigned char* in, int *inlen, int quoteChar) {
1530 const unsigned char* processed = in;
1531 const unsigned char* outend = out + (*outlen);
1532 const unsigned char* outstart = out;
1533 const unsigned char* instart = in;
1534 const unsigned char* inend = in + (*inlen);
1535 unsigned int c, d;
1536 int trailing;
1537
1538 while (in < inend) {
1539 d = *in++;
1540 if (d < 0x80) { c= d; trailing= 0; }
1541 else if (d < 0xC0) {
1542 /* trailing byte in leading position */
1543 *outlen = out - outstart;
1544 *inlen = processed - instart;
1545 return(-2);
1546 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1547 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1548 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1549 else {
1550 /* no chance for this in Ascii */
1551 *outlen = out - outstart;
1552 *inlen = processed - instart;
1553 return(-2);
1554 }
1555
1556 if (inend - in < trailing)
1557 break;
1558
1559 while (trailing--) {
1560 if (((d= *in++) & 0xC0) != 0x80) {
1561 *outlen = out - outstart;
1562 *inlen = processed - instart;
1563 return(-2);
1564 }
1565 c <<= 6;
1566 c |= d & 0x3F;
1567 }
1568
1569 /* assertion: c is a single UTF-4 value */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001570 if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
1571 (c != '&') && (c != '<') && (c != '>')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001572 if (out >= outend)
1573 break;
1574 *out++ = c;
1575 } else {
1576 htmlEntityDescPtr ent;
1577 const char *cp;
1578 char nbuf[16];
1579 int len;
1580
1581 /*
1582 * Try to lookup a predefined HTML entity for it
1583 */
1584 ent = htmlEntityValueLookup(c);
1585 if (ent == NULL) {
1586 sprintf(nbuf, "#%u", c);
1587 cp = nbuf;
1588 }
1589 else
1590 cp = ent->name;
1591 len = strlen(cp);
1592 if (out + 2 + len > outend)
1593 break;
1594 *out++ = '&';
1595 memcpy(out, cp, len);
1596 out += len;
1597 *out++ = ';';
1598 }
1599 processed = in;
1600 }
1601 *outlen = out - outstart;
1602 *inlen = processed - instart;
1603 return(0);
1604}
1605
1606/**
1607 * htmlDecodeEntities:
1608 * @ctxt: the parser context
1609 * @len: the len to decode (in bytes !), -1 for no size limit
1610 * @end: an end marker xmlChar, 0 if none
1611 * @end2: an end marker xmlChar, 0 if none
1612 * @end3: an end marker xmlChar, 0 if none
1613 *
1614 * Subtitute the HTML entities by their value
1615 *
1616 * DEPRECATED !!!!
1617 *
1618 * Returns A newly allocated string with the substitution done. The caller
1619 * must deallocate it !
1620 */
1621xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001622htmlDecodeEntities(htmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED,
1623 xmlChar end ATTRIBUTE_UNUSED, xmlChar end2 ATTRIBUTE_UNUSED, xmlChar end3 ATTRIBUTE_UNUSED) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001624 static int deprecated = 0;
1625 if (!deprecated) {
1626 xmlGenericError(xmlGenericErrorContext,
1627 "htmlDecodeEntities() deprecated function reached\n");
1628 deprecated = 1;
1629 }
1630 return(NULL);
1631#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00001632 xmlChar *name = NULL;
1633 xmlChar *buffer = NULL;
1634 unsigned int buffer_size = 0;
1635 unsigned int nbchars = 0;
1636 htmlEntityDescPtr ent;
1637 unsigned int max = (unsigned int) len;
1638 int c,l;
1639
1640 if (ctxt->depth > 40) {
1641 ctxt->errNo = XML_ERR_ENTITY_LOOP;
1642 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1643 ctxt->sax->error(ctxt->userData,
1644 "Detected entity reference loop\n");
1645 ctxt->wellFormed = 0;
1646 ctxt->disableSAX = 1;
1647 return(NULL);
1648 }
1649
1650 /*
1651 * allocate a translation buffer.
1652 */
1653 buffer_size = HTML_PARSER_BIG_BUFFER_SIZE;
1654 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
1655 if (buffer == NULL) {
1656 perror("xmlDecodeEntities: malloc failed");
1657 return(NULL);
1658 }
1659
1660 /*
1661 * Ok loop until we reach one of the ending char or a size limit.
1662 */
1663 c = CUR_CHAR(l);
1664 while ((nbchars < max) && (c != end) &&
1665 (c != end2) && (c != end3)) {
1666
1667 if (c == 0) break;
1668 if (((c == '&') && (ctxt->token != '&')) && (NXT(1) == '#')) {
1669 int val = htmlParseCharRef(ctxt);
1670 COPY_BUF(0,buffer,nbchars,val);
1671 NEXTL(l);
1672 } else if ((c == '&') && (ctxt->token != '&')) {
1673 ent = htmlParseEntityRef(ctxt, &name);
1674 if (name != NULL) {
1675 if (ent != NULL) {
1676 int val = ent->value;
1677 COPY_BUF(0,buffer,nbchars,val);
1678 NEXTL(l);
1679 } else {
1680 const xmlChar *cur = name;
1681
1682 buffer[nbchars++] = '&';
1683 if (nbchars > buffer_size - HTML_PARSER_BUFFER_SIZE) {
1684 growBuffer(buffer);
1685 }
1686 while (*cur != 0) {
1687 buffer[nbchars++] = *cur++;
1688 }
1689 buffer[nbchars++] = ';';
1690 }
1691 }
1692 } else {
1693 COPY_BUF(l,buffer,nbchars,c);
1694 NEXTL(l);
1695 if (nbchars > buffer_size - HTML_PARSER_BUFFER_SIZE) {
1696 growBuffer(buffer);
1697 }
1698 }
1699 c = CUR_CHAR(l);
1700 }
1701 buffer[nbchars++] = 0;
1702 return(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001703#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001704}
1705
1706/************************************************************************
1707 * *
1708 * Commodity functions to handle streams *
1709 * *
1710 ************************************************************************/
1711
1712/**
Owen Taylor3473f882001-02-23 17:55:21 +00001713 * htmlNewInputStream:
1714 * @ctxt: an HTML parser context
1715 *
1716 * Create a new input stream structure
1717 * Returns the new input stream or NULL
1718 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001719static htmlParserInputPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001720htmlNewInputStream(htmlParserCtxtPtr ctxt) {
1721 htmlParserInputPtr input;
1722
1723 input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1724 if (input == NULL) {
1725 ctxt->errNo = XML_ERR_NO_MEMORY;
1726 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1727 ctxt->sax->error(ctxt->userData,
1728 "malloc: couldn't allocate a new input stream\n");
1729 return(NULL);
1730 }
1731 memset(input, 0, sizeof(htmlParserInput));
1732 input->filename = NULL;
1733 input->directory = NULL;
1734 input->base = NULL;
1735 input->cur = NULL;
1736 input->buf = NULL;
1737 input->line = 1;
1738 input->col = 1;
1739 input->buf = NULL;
1740 input->free = NULL;
1741 input->version = NULL;
1742 input->consumed = 0;
1743 input->length = 0;
1744 return(input);
1745}
1746
1747
1748/************************************************************************
1749 * *
1750 * Commodity functions, cleanup needed ? *
1751 * *
1752 ************************************************************************/
1753
1754/**
1755 * areBlanks:
1756 * @ctxt: an HTML parser context
1757 * @str: a xmlChar *
1758 * @len: the size of @str
1759 *
1760 * Is this a sequence of blank chars that one can ignore ?
1761 *
1762 * Returns 1 if ignorable 0 otherwise.
1763 */
1764
1765static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1766 int i;
1767 xmlNodePtr lastChild;
1768
1769 for (i = 0;i < len;i++)
1770 if (!(IS_BLANK(str[i]))) return(0);
1771
1772 if (CUR == 0) return(1);
1773 if (CUR != '<') return(0);
1774 if (ctxt->name == NULL)
1775 return(1);
1776 if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
1777 return(1);
1778 if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
1779 return(1);
1780 if (xmlStrEqual(ctxt->name, BAD_CAST"body"))
1781 return(1);
1782 if (ctxt->node == NULL) return(0);
1783 lastChild = xmlGetLastChild(ctxt->node);
1784 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001785 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
1786 (ctxt->node->content != NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001787 } else if (xmlNodeIsText(lastChild)) {
1788 return(0);
1789 } else if (xmlStrEqual(lastChild->name, BAD_CAST"b")) {
1790 return(0);
1791 } else if (xmlStrEqual(lastChild->name, BAD_CAST"bold")) {
1792 return(0);
1793 } else if (xmlStrEqual(lastChild->name, BAD_CAST"em")) {
1794 return(0);
1795 }
1796 return(1);
1797}
1798
1799/**
Owen Taylor3473f882001-02-23 17:55:21 +00001800 * htmlNewDocNoDtD:
1801 * @URI: URI for the dtd, or NULL
1802 * @ExternalID: the external ID of the DTD, or NULL
1803 *
1804 * Returns a new document, do not intialize the DTD if not provided
1805 */
1806htmlDocPtr
1807htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
1808 xmlDocPtr cur;
1809
1810 /*
1811 * Allocate a new document and fill the fields.
1812 */
1813 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1814 if (cur == NULL) {
1815 xmlGenericError(xmlGenericErrorContext,
1816 "xmlNewDoc : malloc failed\n");
1817 return(NULL);
1818 }
1819 memset(cur, 0, sizeof(xmlDoc));
1820
1821 cur->type = XML_HTML_DOCUMENT_NODE;
1822 cur->version = NULL;
1823 cur->intSubset = NULL;
1824 if ((ExternalID != NULL) ||
1825 (URI != NULL))
1826 xmlCreateIntSubset(cur, BAD_CAST "HTML", ExternalID, URI);
1827 cur->doc = cur;
1828 cur->name = NULL;
1829 cur->children = NULL;
1830 cur->extSubset = NULL;
1831 cur->oldNs = NULL;
1832 cur->encoding = NULL;
1833 cur->standalone = 1;
1834 cur->compression = 0;
1835 cur->ids = NULL;
1836 cur->refs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001837 cur->_private = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001838 return(cur);
1839}
1840
1841/**
1842 * htmlNewDoc:
1843 * @URI: URI for the dtd, or NULL
1844 * @ExternalID: the external ID of the DTD, or NULL
1845 *
1846 * Returns a new document
1847 */
1848htmlDocPtr
1849htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
1850 if ((URI == NULL) && (ExternalID == NULL))
1851 return(htmlNewDocNoDtD(
Daniel Veillard64269352001-05-04 17:52:34 +00001852 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
1853 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
Owen Taylor3473f882001-02-23 17:55:21 +00001854
1855 return(htmlNewDocNoDtD(URI, ExternalID));
1856}
1857
1858
1859/************************************************************************
1860 * *
1861 * The parser itself *
1862 * Relates to http://www.w3.org/TR/html40 *
1863 * *
1864 ************************************************************************/
1865
1866/************************************************************************
1867 * *
1868 * The parser itself *
1869 * *
1870 ************************************************************************/
1871
1872/**
1873 * htmlParseHTMLName:
1874 * @ctxt: an HTML parser context
1875 *
1876 * parse an HTML tag or attribute name, note that we convert it to lowercase
1877 * since HTML names are not case-sensitive.
1878 *
1879 * Returns the Tag Name parsed or NULL
1880 */
1881
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001882static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00001883htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
1884 xmlChar *ret = NULL;
1885 int i = 0;
1886 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
1887
1888 if (!IS_LETTER(CUR) && (CUR != '_') &&
1889 (CUR != ':')) return(NULL);
1890
1891 while ((i < HTML_PARSER_BUFFER_SIZE) &&
1892 ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1893 (CUR == ':') || (CUR == '-') || (CUR == '_'))) {
1894 if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
1895 else loc[i] = CUR;
1896 i++;
1897
1898 NEXT;
1899 }
1900
1901 ret = xmlStrndup(loc, i);
1902
1903 return(ret);
1904}
1905
1906/**
1907 * htmlParseName:
1908 * @ctxt: an HTML parser context
1909 *
1910 * parse an HTML name, this routine is case sensistive.
1911 *
1912 * Returns the Name parsed or NULL
1913 */
1914
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001915static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00001916htmlParseName(htmlParserCtxtPtr ctxt) {
1917 xmlChar buf[HTML_MAX_NAMELEN];
1918 int len = 0;
1919
1920 GROW;
1921 if (!IS_LETTER(CUR) && (CUR != '_')) {
1922 return(NULL);
1923 }
1924
1925 while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1926 (CUR == '.') || (CUR == '-') ||
1927 (CUR == '_') || (CUR == ':') ||
1928 (IS_COMBINING(CUR)) ||
1929 (IS_EXTENDER(CUR))) {
1930 buf[len++] = CUR;
1931 NEXT;
1932 if (len >= HTML_MAX_NAMELEN) {
1933 xmlGenericError(xmlGenericErrorContext,
1934 "htmlParseName: reached HTML_MAX_NAMELEN limit\n");
1935 while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1936 (CUR == '.') || (CUR == '-') ||
1937 (CUR == '_') || (CUR == ':') ||
1938 (IS_COMBINING(CUR)) ||
1939 (IS_EXTENDER(CUR)))
1940 NEXT;
1941 break;
1942 }
1943 }
1944 return(xmlStrndup(buf, len));
1945}
1946
1947/**
1948 * htmlParseHTMLAttribute:
1949 * @ctxt: an HTML parser context
1950 * @stop: a char stop value
1951 *
1952 * parse an HTML attribute value till the stop (quote), if
1953 * stop is 0 then it stops at the first space
1954 *
1955 * Returns the attribute parsed or NULL
1956 */
1957
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001958static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00001959htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
1960 xmlChar *buffer = NULL;
1961 int buffer_size = 0;
1962 xmlChar *out = NULL;
1963 xmlChar *name = NULL;
1964
1965 xmlChar *cur = NULL;
1966 htmlEntityDescPtr ent;
1967
1968 /*
1969 * allocate a translation buffer.
1970 */
1971 buffer_size = HTML_PARSER_BUFFER_SIZE;
1972 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
1973 if (buffer == NULL) {
1974 perror("htmlParseHTMLAttribute: malloc failed");
1975 return(NULL);
1976 }
1977 out = buffer;
1978
1979 /*
1980 * Ok loop until we reach one of the ending chars
1981 */
1982 while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
1983 if ((stop == 0) && (IS_BLANK(CUR))) break;
1984 if (CUR == '&') {
1985 if (NXT(1) == '#') {
1986 unsigned int c;
1987 int bits;
1988
1989 c = htmlParseCharRef(ctxt);
1990 if (c < 0x80)
1991 { *out++ = c; bits= -6; }
1992 else if (c < 0x800)
1993 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
1994 else if (c < 0x10000)
1995 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
1996 else
1997 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
1998
1999 for ( ; bits >= 0; bits-= 6) {
2000 *out++ = ((c >> bits) & 0x3F) | 0x80;
2001 }
2002 } else {
2003 ent = htmlParseEntityRef(ctxt, &name);
2004 if (name == NULL) {
2005 *out++ = '&';
2006 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002007 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002008
2009 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002010 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002011 }
2012 } else if (ent == NULL) {
2013 *out++ = '&';
2014 cur = name;
2015 while (*cur != 0) {
2016 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002017 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002018
2019 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002020 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002021 }
2022 *out++ = *cur++;
2023 }
2024 xmlFree(name);
2025 } else {
2026 unsigned int c;
2027 int bits;
2028
2029 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002030 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002031
2032 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002033 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002034 }
2035 c = (xmlChar)ent->value;
2036 if (c < 0x80)
2037 { *out++ = c; bits= -6; }
2038 else if (c < 0x800)
2039 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2040 else if (c < 0x10000)
2041 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2042 else
2043 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2044
2045 for ( ; bits >= 0; bits-= 6) {
2046 *out++ = ((c >> bits) & 0x3F) | 0x80;
2047 }
2048 xmlFree(name);
2049 }
2050 }
2051 } else {
2052 unsigned int c;
2053 int bits, l;
2054
2055 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002056 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002057
2058 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002059 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002060 }
2061 c = CUR_CHAR(l);
2062 if (c < 0x80)
2063 { *out++ = c; bits= -6; }
2064 else if (c < 0x800)
2065 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2066 else if (c < 0x10000)
2067 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2068 else
2069 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2070
2071 for ( ; bits >= 0; bits-= 6) {
2072 *out++ = ((c >> bits) & 0x3F) | 0x80;
2073 }
2074 NEXT;
2075 }
2076 }
2077 *out++ = 0;
2078 return(buffer);
2079}
2080
2081/**
Owen Taylor3473f882001-02-23 17:55:21 +00002082 * htmlParseEntityRef:
2083 * @ctxt: an HTML parser context
2084 * @str: location to store the entity name
2085 *
2086 * parse an HTML ENTITY references
2087 *
2088 * [68] EntityRef ::= '&' Name ';'
2089 *
2090 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2091 * if non-NULL *str will have to be freed by the caller.
2092 */
2093htmlEntityDescPtr
2094htmlParseEntityRef(htmlParserCtxtPtr ctxt, xmlChar **str) {
2095 xmlChar *name;
2096 htmlEntityDescPtr ent = NULL;
2097 *str = NULL;
2098
2099 if (CUR == '&') {
2100 NEXT;
2101 name = htmlParseName(ctxt);
2102 if (name == NULL) {
2103 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2104 ctxt->sax->error(ctxt->userData, "htmlParseEntityRef: no name\n");
2105 ctxt->wellFormed = 0;
2106 } else {
2107 GROW;
2108 if (CUR == ';') {
2109 *str = name;
2110
2111 /*
2112 * Lookup the entity in the table.
2113 */
2114 ent = htmlEntityLookup(name);
2115 if (ent != NULL) /* OK that's ugly !!! */
2116 NEXT;
2117 } else {
2118 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2119 ctxt->sax->error(ctxt->userData,
2120 "htmlParseEntityRef: expecting ';'\n");
2121 *str = name;
2122 }
2123 }
2124 }
2125 return(ent);
2126}
2127
2128/**
2129 * htmlParseAttValue:
2130 * @ctxt: an HTML parser context
2131 *
2132 * parse a value for an attribute
2133 * Note: the parser won't do substitution of entities here, this
2134 * will be handled later in xmlStringGetNodeList, unless it was
2135 * asked for ctxt->replaceEntities != 0
2136 *
2137 * Returns the AttValue parsed or NULL.
2138 */
2139
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002140static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002141htmlParseAttValue(htmlParserCtxtPtr ctxt) {
2142 xmlChar *ret = NULL;
2143
2144 if (CUR == '"') {
2145 NEXT;
2146 ret = htmlParseHTMLAttribute(ctxt, '"');
2147 if (CUR != '"') {
2148 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2149 ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
2150 ctxt->wellFormed = 0;
2151 } else
2152 NEXT;
2153 } else if (CUR == '\'') {
2154 NEXT;
2155 ret = htmlParseHTMLAttribute(ctxt, '\'');
2156 if (CUR != '\'') {
2157 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2158 ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
2159 ctxt->wellFormed = 0;
2160 } else
2161 NEXT;
2162 } else {
2163 /*
2164 * That's an HTMLism, the attribute value may not be quoted
2165 */
2166 ret = htmlParseHTMLAttribute(ctxt, 0);
2167 if (ret == NULL) {
2168 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2169 ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
2170 ctxt->wellFormed = 0;
2171 }
2172 }
2173 return(ret);
2174}
2175
2176/**
2177 * htmlParseSystemLiteral:
2178 * @ctxt: an HTML parser context
2179 *
2180 * parse an HTML Literal
2181 *
2182 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2183 *
2184 * Returns the SystemLiteral parsed or NULL
2185 */
2186
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002187static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002188htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
2189 const xmlChar *q;
2190 xmlChar *ret = NULL;
2191
2192 if (CUR == '"') {
2193 NEXT;
2194 q = CUR_PTR;
2195 while ((IS_CHAR(CUR)) && (CUR != '"'))
2196 NEXT;
2197 if (!IS_CHAR(CUR)) {
2198 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2199 ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
2200 ctxt->wellFormed = 0;
2201 } else {
2202 ret = xmlStrndup(q, CUR_PTR - q);
2203 NEXT;
2204 }
2205 } else if (CUR == '\'') {
2206 NEXT;
2207 q = CUR_PTR;
2208 while ((IS_CHAR(CUR)) && (CUR != '\''))
2209 NEXT;
2210 if (!IS_CHAR(CUR)) {
2211 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2212 ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
2213 ctxt->wellFormed = 0;
2214 } else {
2215 ret = xmlStrndup(q, CUR_PTR - q);
2216 NEXT;
2217 }
2218 } else {
2219 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2220 ctxt->sax->error(ctxt->userData,
2221 "SystemLiteral \" or ' expected\n");
2222 ctxt->wellFormed = 0;
2223 }
2224
2225 return(ret);
2226}
2227
2228/**
2229 * htmlParsePubidLiteral:
2230 * @ctxt: an HTML parser context
2231 *
2232 * parse an HTML public literal
2233 *
2234 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2235 *
2236 * Returns the PubidLiteral parsed or NULL.
2237 */
2238
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002239static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002240htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
2241 const xmlChar *q;
2242 xmlChar *ret = NULL;
2243 /*
2244 * Name ::= (Letter | '_') (NameChar)*
2245 */
2246 if (CUR == '"') {
2247 NEXT;
2248 q = CUR_PTR;
2249 while (IS_PUBIDCHAR(CUR)) NEXT;
2250 if (CUR != '"') {
2251 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2252 ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
2253 ctxt->wellFormed = 0;
2254 } else {
2255 ret = xmlStrndup(q, CUR_PTR - q);
2256 NEXT;
2257 }
2258 } else if (CUR == '\'') {
2259 NEXT;
2260 q = CUR_PTR;
2261 while ((IS_LETTER(CUR)) && (CUR != '\''))
2262 NEXT;
2263 if (!IS_LETTER(CUR)) {
2264 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2265 ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
2266 ctxt->wellFormed = 0;
2267 } else {
2268 ret = xmlStrndup(q, CUR_PTR - q);
2269 NEXT;
2270 }
2271 } else {
2272 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2273 ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
2274 ctxt->wellFormed = 0;
2275 }
2276
2277 return(ret);
2278}
2279
2280/**
2281 * htmlParseScript:
2282 * @ctxt: an HTML parser context
2283 *
2284 * parse the content of an HTML SCRIPT or STYLE element
2285 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
2286 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
2287 * http://www.w3.org/TR/html4/types.html#type-script
2288 * http://www.w3.org/TR/html4/types.html#h-6.15
2289 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
2290 *
2291 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
2292 * element and the value of intrinsic event attributes. User agents must
2293 * not evaluate script data as HTML markup but instead must pass it on as
2294 * data to a script engine.
2295 * NOTES:
2296 * - The content is passed like CDATA
2297 * - the attributes for style and scripting "onXXX" are also described
2298 * as CDATA but SGML allows entities references in attributes so their
2299 * processing is identical as other attributes
2300 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002301static void
Owen Taylor3473f882001-02-23 17:55:21 +00002302htmlParseScript(htmlParserCtxtPtr ctxt) {
2303 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 1];
2304 int nbchar = 0;
2305 xmlChar cur;
2306
2307 SHRINK;
2308 cur = CUR;
2309 while (IS_CHAR(cur)) {
2310 if ((cur == '<') && (NXT(1) == '/')) {
2311 /*
2312 * One should break here, the specification is clear:
2313 * Authors should therefore escape "</" within the content.
2314 * Escape mechanisms are specific to each scripting or
2315 * style sheet language.
2316 */
2317 if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
2318 ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
2319 break; /* while */
2320 }
2321 buf[nbchar++] = cur;
2322 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2323 if (ctxt->sax->cdataBlock!= NULL) {
2324 /*
2325 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2326 */
2327 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
2328 }
2329 nbchar = 0;
2330 }
2331 NEXT;
2332 cur = CUR;
2333 }
2334 if (!(IS_CHAR(cur))) {
2335 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2336 ctxt->sax->error(ctxt->userData,
2337 "Invalid char in CDATA 0x%X\n", cur);
2338 ctxt->wellFormed = 0;
2339 NEXT;
2340 }
2341
2342 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2343 if (ctxt->sax->cdataBlock!= NULL) {
2344 /*
2345 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2346 */
2347 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
2348 }
2349 }
2350}
2351
2352
2353/**
2354 * htmlParseCharData:
2355 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002356 *
2357 * parse a CharData section.
2358 * if we are within a CDATA section ']]>' marks an end of section.
2359 *
2360 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2361 */
2362
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002363static void
2364htmlParseCharData(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002365 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
2366 int nbchar = 0;
2367 int cur, l;
2368
2369 SHRINK;
2370 cur = CUR_CHAR(l);
2371 while (((cur != '<') || (ctxt->token == '<')) &&
2372 ((cur != '&') || (ctxt->token == '&')) &&
2373 (IS_CHAR(cur))) {
2374 COPY_BUF(l,buf,nbchar,cur);
2375 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2376 /*
2377 * Ok the segment is to be consumed as chars.
2378 */
2379 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2380 if (areBlanks(ctxt, buf, nbchar)) {
2381 if (ctxt->sax->ignorableWhitespace != NULL)
2382 ctxt->sax->ignorableWhitespace(ctxt->userData,
2383 buf, nbchar);
2384 } else {
2385 htmlCheckParagraph(ctxt);
2386 if (ctxt->sax->characters != NULL)
2387 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2388 }
2389 }
2390 nbchar = 0;
2391 }
2392 NEXTL(l);
2393 cur = CUR_CHAR(l);
2394 }
2395 if (nbchar != 0) {
2396 /*
2397 * Ok the segment is to be consumed as chars.
2398 */
2399 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2400 if (areBlanks(ctxt, buf, nbchar)) {
2401 if (ctxt->sax->ignorableWhitespace != NULL)
2402 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2403 } else {
2404 htmlCheckParagraph(ctxt);
2405 if (ctxt->sax->characters != NULL)
2406 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2407 }
2408 }
2409 }
2410}
2411
2412/**
2413 * htmlParseExternalID:
2414 * @ctxt: an HTML parser context
2415 * @publicID: a xmlChar** receiving PubidLiteral
Owen Taylor3473f882001-02-23 17:55:21 +00002416 *
2417 * Parse an External ID or a Public ID
2418 *
Owen Taylor3473f882001-02-23 17:55:21 +00002419 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
2420 * | 'PUBLIC' S PubidLiteral S SystemLiteral
2421 *
2422 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
2423 *
2424 * Returns the function returns SystemLiteral and in the second
2425 * case publicID receives PubidLiteral, is strict is off
2426 * it is possible to return NULL and have publicID set.
2427 */
2428
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002429static xmlChar *
2430htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
Owen Taylor3473f882001-02-23 17:55:21 +00002431 xmlChar *URI = NULL;
2432
2433 if ((UPPER == 'S') && (UPP(1) == 'Y') &&
2434 (UPP(2) == 'S') && (UPP(3) == 'T') &&
2435 (UPP(4) == 'E') && (UPP(5) == 'M')) {
2436 SKIP(6);
2437 if (!IS_BLANK(CUR)) {
2438 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2439 ctxt->sax->error(ctxt->userData,
2440 "Space required after 'SYSTEM'\n");
2441 ctxt->wellFormed = 0;
2442 }
2443 SKIP_BLANKS;
2444 URI = htmlParseSystemLiteral(ctxt);
2445 if (URI == NULL) {
2446 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2447 ctxt->sax->error(ctxt->userData,
2448 "htmlParseExternalID: SYSTEM, no URI\n");
2449 ctxt->wellFormed = 0;
2450 }
2451 } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
2452 (UPP(2) == 'B') && (UPP(3) == 'L') &&
2453 (UPP(4) == 'I') && (UPP(5) == 'C')) {
2454 SKIP(6);
2455 if (!IS_BLANK(CUR)) {
2456 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2457 ctxt->sax->error(ctxt->userData,
2458 "Space required after 'PUBLIC'\n");
2459 ctxt->wellFormed = 0;
2460 }
2461 SKIP_BLANKS;
2462 *publicID = htmlParsePubidLiteral(ctxt);
2463 if (*publicID == NULL) {
2464 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2465 ctxt->sax->error(ctxt->userData,
2466 "htmlParseExternalID: PUBLIC, no Public Identifier\n");
2467 ctxt->wellFormed = 0;
2468 }
2469 SKIP_BLANKS;
2470 if ((CUR == '"') || (CUR == '\'')) {
2471 URI = htmlParseSystemLiteral(ctxt);
2472 }
2473 }
2474 return(URI);
2475}
2476
2477/**
2478 * htmlParseComment:
2479 * @ctxt: an HTML parser context
2480 *
2481 * Parse an XML (SGML) comment <!-- .... -->
2482 *
2483 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
2484 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002485static void
Owen Taylor3473f882001-02-23 17:55:21 +00002486htmlParseComment(htmlParserCtxtPtr ctxt) {
2487 xmlChar *buf = NULL;
2488 int len;
2489 int size = HTML_PARSER_BUFFER_SIZE;
2490 int q, ql;
2491 int r, rl;
2492 int cur, l;
2493 xmlParserInputState state;
2494
2495 /*
2496 * Check that there is a comment right here.
2497 */
2498 if ((RAW != '<') || (NXT(1) != '!') ||
2499 (NXT(2) != '-') || (NXT(3) != '-')) return;
2500
2501 state = ctxt->instate;
2502 ctxt->instate = XML_PARSER_COMMENT;
2503 SHRINK;
2504 SKIP(4);
2505 buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
2506 if (buf == NULL) {
2507 xmlGenericError(xmlGenericErrorContext,
2508 "malloc of %d byte failed\n", size);
2509 ctxt->instate = state;
2510 return;
2511 }
2512 q = CUR_CHAR(ql);
2513 NEXTL(ql);
2514 r = CUR_CHAR(rl);
2515 NEXTL(rl);
2516 cur = CUR_CHAR(l);
2517 len = 0;
2518 while (IS_CHAR(cur) &&
2519 ((cur != '>') ||
2520 (r != '-') || (q != '-'))) {
2521 if (len + 5 >= size) {
2522 size *= 2;
2523 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2524 if (buf == NULL) {
2525 xmlGenericError(xmlGenericErrorContext,
2526 "realloc of %d byte failed\n", size);
2527 ctxt->instate = state;
2528 return;
2529 }
2530 }
2531 COPY_BUF(ql,buf,len,q);
2532 q = r;
2533 ql = rl;
2534 r = cur;
2535 rl = l;
2536 NEXTL(l);
2537 cur = CUR_CHAR(l);
2538 if (cur == 0) {
2539 SHRINK;
2540 GROW;
2541 cur = CUR_CHAR(l);
2542 }
2543 }
2544 buf[len] = 0;
2545 if (!IS_CHAR(cur)) {
2546 ctxt->errNo = XML_ERR_COMMENT_NOT_FINISHED;
2547 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2548 ctxt->sax->error(ctxt->userData,
2549 "Comment not terminated \n<!--%.50s\n", buf);
2550 ctxt->wellFormed = 0;
2551 xmlFree(buf);
2552 } else {
2553 NEXT;
2554 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
2555 (!ctxt->disableSAX))
2556 ctxt->sax->comment(ctxt->userData, buf);
2557 xmlFree(buf);
2558 }
2559 ctxt->instate = state;
2560}
2561
2562/**
2563 * htmlParseCharRef:
2564 * @ctxt: an HTML parser context
2565 *
2566 * parse Reference declarations
2567 *
2568 * [66] CharRef ::= '&#' [0-9]+ ';' |
2569 * '&#x' [0-9a-fA-F]+ ';'
2570 *
2571 * Returns the value parsed (as an int)
2572 */
2573int
2574htmlParseCharRef(htmlParserCtxtPtr ctxt) {
2575 int val = 0;
2576
2577 if ((CUR == '&') && (NXT(1) == '#') &&
2578 (NXT(2) == 'x')) {
2579 SKIP(3);
2580 while (CUR != ';') {
2581 if ((CUR >= '0') && (CUR <= '9'))
2582 val = val * 16 + (CUR - '0');
2583 else if ((CUR >= 'a') && (CUR <= 'f'))
2584 val = val * 16 + (CUR - 'a') + 10;
2585 else if ((CUR >= 'A') && (CUR <= 'F'))
2586 val = val * 16 + (CUR - 'A') + 10;
2587 else {
2588 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2589 ctxt->sax->error(ctxt->userData,
2590 "htmlParseCharRef: invalid hexadecimal value\n");
2591 ctxt->wellFormed = 0;
2592 return(0);
2593 }
2594 NEXT;
2595 }
2596 if (CUR == ';')
2597 NEXT;
2598 } else if ((CUR == '&') && (NXT(1) == '#')) {
2599 SKIP(2);
2600 while (CUR != ';') {
2601 if ((CUR >= '0') && (CUR <= '9'))
2602 val = val * 10 + (CUR - '0');
2603 else {
2604 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2605 ctxt->sax->error(ctxt->userData,
2606 "htmlParseCharRef: invalid decimal value\n");
2607 ctxt->wellFormed = 0;
2608 return(0);
2609 }
2610 NEXT;
2611 }
2612 if (CUR == ';')
2613 NEXT;
2614 } else {
2615 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2616 ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid value\n");
2617 ctxt->wellFormed = 0;
2618 }
2619 /*
2620 * Check the value IS_CHAR ...
2621 */
2622 if (IS_CHAR(val)) {
2623 return(val);
2624 } else {
2625 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2626 ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid xmlChar value %d\n",
2627 val);
2628 ctxt->wellFormed = 0;
2629 }
2630 return(0);
2631}
2632
2633
2634/**
2635 * htmlParseDocTypeDecl :
2636 * @ctxt: an HTML parser context
2637 *
2638 * parse a DOCTYPE declaration
2639 *
2640 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
2641 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
2642 */
2643
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002644static void
Owen Taylor3473f882001-02-23 17:55:21 +00002645htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
2646 xmlChar *name;
2647 xmlChar *ExternalID = NULL;
2648 xmlChar *URI = NULL;
2649
2650 /*
2651 * We know that '<!DOCTYPE' has been detected.
2652 */
2653 SKIP(9);
2654
2655 SKIP_BLANKS;
2656
2657 /*
2658 * Parse the DOCTYPE name.
2659 */
2660 name = htmlParseName(ctxt);
2661 if (name == NULL) {
2662 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2663 ctxt->sax->error(ctxt->userData, "htmlParseDocTypeDecl : no DOCTYPE name !\n");
2664 ctxt->wellFormed = 0;
2665 }
2666 /*
2667 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
2668 */
2669
2670 SKIP_BLANKS;
2671
2672 /*
2673 * Check for SystemID and ExternalID
2674 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002675 URI = htmlParseExternalID(ctxt, &ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00002676 SKIP_BLANKS;
2677
2678 /*
2679 * We should be at the end of the DOCTYPE declaration.
2680 */
2681 if (CUR != '>') {
2682 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2683 ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
2684 ctxt->wellFormed = 0;
2685 /* We shouldn't try to resynchronize ... */
2686 }
2687 NEXT;
2688
2689 /*
2690 * Create or update the document accordingly to the DOCTYPE
2691 */
2692 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
2693 (!ctxt->disableSAX))
2694 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
2695
2696 /*
2697 * Cleanup, since we don't use all those identifiers
2698 */
2699 if (URI != NULL) xmlFree(URI);
2700 if (ExternalID != NULL) xmlFree(ExternalID);
2701 if (name != NULL) xmlFree(name);
2702}
2703
2704/**
2705 * htmlParseAttribute:
2706 * @ctxt: an HTML parser context
2707 * @value: a xmlChar ** used to store the value of the attribute
2708 *
2709 * parse an attribute
2710 *
2711 * [41] Attribute ::= Name Eq AttValue
2712 *
2713 * [25] Eq ::= S? '=' S?
2714 *
2715 * With namespace:
2716 *
2717 * [NS 11] Attribute ::= QName Eq AttValue
2718 *
2719 * Also the case QName == xmlns:??? is handled independently as a namespace
2720 * definition.
2721 *
2722 * Returns the attribute name, and the value in *value.
2723 */
2724
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002725static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002726htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
2727 xmlChar *name, *val = NULL;
2728
2729 *value = NULL;
2730 name = htmlParseHTMLName(ctxt);
2731 if (name == NULL) {
2732 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2733 ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
2734 ctxt->wellFormed = 0;
2735 return(NULL);
2736 }
2737
2738 /*
2739 * read the value
2740 */
2741 SKIP_BLANKS;
2742 if (CUR == '=') {
2743 NEXT;
2744 SKIP_BLANKS;
2745 val = htmlParseAttValue(ctxt);
2746 /******
2747 } else {
2748 * TODO : some attribute must have values, some may not
2749 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2750 ctxt->sax->warning(ctxt->userData,
2751 "No value for attribute %s\n", name); */
2752 }
2753
2754 *value = val;
2755 return(name);
2756}
2757
2758/**
2759 * htmlCheckEncoding:
2760 * @ctxt: an HTML parser context
2761 * @attvalue: the attribute value
2762 *
2763 * Checks an http-equiv attribute from a Meta tag to detect
2764 * the encoding
2765 * If a new encoding is detected the parser is switched to decode
2766 * it and pass UTF8
2767 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002768static void
Owen Taylor3473f882001-02-23 17:55:21 +00002769htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
2770 const xmlChar *encoding;
2771
2772 if ((ctxt == NULL) || (attvalue == NULL))
2773 return;
2774
2775 /* do not change encoding */
2776 if (ctxt->input->encoding != NULL)
2777 return;
2778
2779 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
2780 if (encoding != NULL) {
2781 encoding += 8;
2782 } else {
2783 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
2784 if (encoding != NULL)
2785 encoding += 9;
2786 }
2787 if (encoding != NULL) {
2788 xmlCharEncoding enc;
2789 xmlCharEncodingHandlerPtr handler;
2790
2791 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
2792
2793 if (ctxt->input->encoding != NULL)
2794 xmlFree((xmlChar *) ctxt->input->encoding);
2795 ctxt->input->encoding = xmlStrdup(encoding);
2796
2797 enc = xmlParseCharEncoding((const char *) encoding);
2798 /*
2799 * registered set of known encodings
2800 */
2801 if (enc != XML_CHAR_ENCODING_ERROR) {
2802 xmlSwitchEncoding(ctxt, enc);
2803 ctxt->charset = XML_CHAR_ENCODING_UTF8;
2804 } else {
2805 /*
2806 * fallback for unknown encodings
2807 */
2808 handler = xmlFindCharEncodingHandler((const char *) encoding);
2809 if (handler != NULL) {
2810 xmlSwitchToEncoding(ctxt, handler);
2811 ctxt->charset = XML_CHAR_ENCODING_UTF8;
2812 } else {
2813 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
2814 }
2815 }
2816
2817 if ((ctxt->input->buf != NULL) &&
2818 (ctxt->input->buf->encoder != NULL) &&
2819 (ctxt->input->buf->raw != NULL) &&
2820 (ctxt->input->buf->buffer != NULL)) {
2821 int nbchars;
2822 int processed;
2823
2824 /*
2825 * convert as much as possible to the parser reading buffer.
2826 */
2827 processed = ctxt->input->cur - ctxt->input->base;
2828 xmlBufferShrink(ctxt->input->buf->buffer, processed);
2829 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
2830 ctxt->input->buf->buffer,
2831 ctxt->input->buf->raw);
2832 if (nbchars < 0) {
2833 ctxt->errNo = XML_ERR_INVALID_ENCODING;
2834 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2835 ctxt->sax->error(ctxt->userData,
2836 "htmlCheckEncoding: encoder error\n");
2837 }
2838 ctxt->input->base =
2839 ctxt->input->cur = ctxt->input->buf->buffer->content;
2840 }
2841 }
2842}
2843
2844/**
2845 * htmlCheckMeta:
2846 * @ctxt: an HTML parser context
2847 * @atts: the attributes values
2848 *
2849 * Checks an attributes from a Meta tag
2850 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002851static void
Owen Taylor3473f882001-02-23 17:55:21 +00002852htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
2853 int i;
2854 const xmlChar *att, *value;
2855 int http = 0;
2856 const xmlChar *content = NULL;
2857
2858 if ((ctxt == NULL) || (atts == NULL))
2859 return;
2860
2861 i = 0;
2862 att = atts[i++];
2863 while (att != NULL) {
2864 value = atts[i++];
2865 if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
2866 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
2867 http = 1;
2868 else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
2869 content = value;
2870 att = atts[i++];
2871 }
2872 if ((http) && (content != NULL))
2873 htmlCheckEncoding(ctxt, content);
2874
2875}
2876
2877/**
2878 * htmlParseStartTag:
2879 * @ctxt: an HTML parser context
2880 *
2881 * parse a start of tag either for rule element or
2882 * EmptyElement. In both case we don't parse the tag closing chars.
2883 *
2884 * [40] STag ::= '<' Name (S Attribute)* S? '>'
2885 *
2886 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
2887 *
2888 * With namespace:
2889 *
2890 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
2891 *
2892 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
2893 *
2894 */
2895
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002896static void
Owen Taylor3473f882001-02-23 17:55:21 +00002897htmlParseStartTag(htmlParserCtxtPtr ctxt) {
2898 xmlChar *name;
2899 xmlChar *attname;
2900 xmlChar *attvalue;
2901 const xmlChar **atts = NULL;
2902 int nbatts = 0;
2903 int maxatts = 0;
2904 int meta = 0;
2905 int i;
2906
2907 if (CUR != '<') return;
2908 NEXT;
2909
2910 GROW;
2911 name = htmlParseHTMLName(ctxt);
2912 if (name == NULL) {
2913 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2914 ctxt->sax->error(ctxt->userData,
2915 "htmlParseStartTag: invalid element name\n");
2916 ctxt->wellFormed = 0;
2917 /* Dump the bogus tag like browsers do */
2918 while ((IS_CHAR(CUR)) && (CUR != '>'))
2919 NEXT;
2920 return;
2921 }
2922 if (xmlStrEqual(name, BAD_CAST"meta"))
2923 meta = 1;
2924
2925 /*
2926 * Check for auto-closure of HTML elements.
2927 */
2928 htmlAutoClose(ctxt, name);
2929
2930 /*
2931 * Check for implied HTML elements.
2932 */
2933 htmlCheckImplied(ctxt, name);
2934
2935 /*
2936 * Avoid html at any level > 0, head at any level != 1
2937 * or any attempt to recurse body
2938 */
2939 if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
2940 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2941 ctxt->sax->error(ctxt->userData,
2942 "htmlParseStartTag: misplaced <html> tag\n");
2943 ctxt->wellFormed = 0;
2944 xmlFree(name);
2945 return;
2946 }
2947 if ((ctxt->nameNr != 1) &&
2948 (xmlStrEqual(name, BAD_CAST"head"))) {
2949 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2950 ctxt->sax->error(ctxt->userData,
2951 "htmlParseStartTag: misplaced <head> tag\n");
2952 ctxt->wellFormed = 0;
2953 xmlFree(name);
2954 return;
2955 }
2956 if (xmlStrEqual(name, BAD_CAST"body")) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002957 int indx;
2958 for (indx = 0;indx < ctxt->nameNr;indx++) {
2959 if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
Owen Taylor3473f882001-02-23 17:55:21 +00002960 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2961 ctxt->sax->error(ctxt->userData,
2962 "htmlParseStartTag: misplaced <body> tag\n");
2963 ctxt->wellFormed = 0;
2964 xmlFree(name);
2965 return;
2966 }
2967 }
2968 }
2969
2970 /*
2971 * Now parse the attributes, it ends up with the ending
2972 *
2973 * (S Attribute)* S?
2974 */
2975 SKIP_BLANKS;
2976 while ((IS_CHAR(CUR)) &&
2977 (CUR != '>') &&
2978 ((CUR != '/') || (NXT(1) != '>'))) {
2979 long cons = ctxt->nbChars;
2980
2981 GROW;
2982 attname = htmlParseAttribute(ctxt, &attvalue);
2983 if (attname != NULL) {
2984
2985 /*
2986 * Well formedness requires at most one declaration of an attribute
2987 */
2988 for (i = 0; i < nbatts;i += 2) {
2989 if (xmlStrEqual(atts[i], attname)) {
2990 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2991 ctxt->sax->error(ctxt->userData,
2992 "Attribute %s redefined\n",
2993 attname);
2994 ctxt->wellFormed = 0;
2995 xmlFree(attname);
2996 if (attvalue != NULL)
2997 xmlFree(attvalue);
2998 goto failed;
2999 }
3000 }
3001
3002 /*
3003 * Add the pair to atts
3004 */
3005 if (atts == NULL) {
3006 maxatts = 10;
3007 atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
3008 if (atts == NULL) {
3009 xmlGenericError(xmlGenericErrorContext,
3010 "malloc of %ld byte failed\n",
3011 maxatts * (long)sizeof(xmlChar *));
3012 if (name != NULL) xmlFree(name);
3013 return;
3014 }
3015 } else if (nbatts + 4 > maxatts) {
3016 maxatts *= 2;
3017 atts = (const xmlChar **) xmlRealloc((void *) atts,
3018 maxatts * sizeof(xmlChar *));
3019 if (atts == NULL) {
3020 xmlGenericError(xmlGenericErrorContext,
3021 "realloc of %ld byte failed\n",
3022 maxatts * (long)sizeof(xmlChar *));
3023 if (name != NULL) xmlFree(name);
3024 return;
3025 }
3026 }
3027 atts[nbatts++] = attname;
3028 atts[nbatts++] = attvalue;
3029 atts[nbatts] = NULL;
3030 atts[nbatts + 1] = NULL;
3031 }
3032 else {
3033 /* Dump the bogus attribute string up to the next blank or
3034 * the end of the tag. */
3035 while ((IS_CHAR(CUR)) && !(IS_BLANK(CUR)) && (CUR != '>')
3036 && ((CUR != '/') || (NXT(1) != '>')))
3037 NEXT;
3038 }
3039
3040failed:
3041 SKIP_BLANKS;
3042 if (cons == ctxt->nbChars) {
3043 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3044 ctxt->sax->error(ctxt->userData,
3045 "htmlParseStartTag: problem parsing attributes\n");
3046 ctxt->wellFormed = 0;
3047 break;
3048 }
3049 }
3050
3051 /*
3052 * Handle specific association to the META tag
3053 */
3054 if (meta)
3055 htmlCheckMeta(ctxt, atts);
3056
3057 /*
3058 * SAX: Start of Element !
3059 */
3060 htmlnamePush(ctxt, xmlStrdup(name));
3061#ifdef DEBUG
3062 xmlGenericError(xmlGenericErrorContext,"Start of element %s: pushed %s\n", name, ctxt->name);
3063#endif
3064 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
3065 ctxt->sax->startElement(ctxt->userData, name, atts);
3066
3067 if (atts != NULL) {
3068 for (i = 0;i < nbatts;i++) {
3069 if (atts[i] != NULL)
3070 xmlFree((xmlChar *) atts[i]);
3071 }
3072 xmlFree((void *) atts);
3073 }
3074 if (name != NULL) xmlFree(name);
3075}
3076
3077/**
3078 * htmlParseEndTag:
3079 * @ctxt: an HTML parser context
3080 *
3081 * parse an end of tag
3082 *
3083 * [42] ETag ::= '</' Name S? '>'
3084 *
3085 * With namespace
3086 *
3087 * [NS 9] ETag ::= '</' QName S? '>'
Daniel Veillardf420ac52001-07-04 16:04:09 +00003088 *
3089 * Returns 1 if the current level should be closed.
Owen Taylor3473f882001-02-23 17:55:21 +00003090 */
3091
Daniel Veillardf420ac52001-07-04 16:04:09 +00003092static int
Owen Taylor3473f882001-02-23 17:55:21 +00003093htmlParseEndTag(htmlParserCtxtPtr ctxt) {
3094 xmlChar *name;
3095 xmlChar *oldname;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003096 int i, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00003097
3098 if ((CUR != '<') || (NXT(1) != '/')) {
3099 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3100 ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
3101 ctxt->wellFormed = 0;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003102 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003103 }
3104 SKIP(2);
3105
3106 name = htmlParseHTMLName(ctxt);
Daniel Veillardf420ac52001-07-04 16:04:09 +00003107 if (name == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003108
3109 /*
3110 * We should definitely be at the ending "S? '>'" part
3111 */
3112 SKIP_BLANKS;
3113 if ((!IS_CHAR(CUR)) || (CUR != '>')) {
3114 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3115 ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
3116 ctxt->wellFormed = 0;
3117 } else
3118 NEXT;
3119
3120 /*
3121 * If the name read is not one of the element in the parsing stack
3122 * then return, it's just an error.
3123 */
3124 for (i = (ctxt->nameNr - 1);i >= 0;i--) {
3125 if (xmlStrEqual(name, ctxt->nameTab[i])) break;
3126 }
3127 if (i < 0) {
3128 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3129 ctxt->sax->error(ctxt->userData,
3130 "Unexpected end tag : %s\n", name);
3131 xmlFree(name);
3132 ctxt->wellFormed = 0;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003133 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003134 }
3135
3136
3137 /*
3138 * Check for auto-closure of HTML elements.
3139 */
3140
3141 htmlAutoCloseOnClose(ctxt, name);
3142
3143 /*
3144 * Well formedness constraints, opening and closing must match.
3145 * With the exception that the autoclose may have popped stuff out
3146 * of the stack.
3147 */
3148 if (!xmlStrEqual(name, ctxt->name)) {
3149#ifdef DEBUG
3150 xmlGenericError(xmlGenericErrorContext,"End of tag %s: expecting %s\n", name, ctxt->name);
3151#endif
3152 if ((ctxt->name != NULL) &&
3153 (!xmlStrEqual(ctxt->name, name))) {
3154 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3155 ctxt->sax->error(ctxt->userData,
3156 "Opening and ending tag mismatch: %s and %s\n",
3157 name, ctxt->name);
3158 ctxt->wellFormed = 0;
3159 }
3160 }
3161
3162 /*
3163 * SAX: End of Tag
3164 */
3165 oldname = ctxt->name;
3166 if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
3167 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3168 ctxt->sax->endElement(ctxt->userData, name);
3169 oldname = htmlnamePop(ctxt);
3170 if (oldname != NULL) {
3171#ifdef DEBUG
3172 xmlGenericError(xmlGenericErrorContext,"End of tag %s: popping out %s\n", name, oldname);
3173#endif
3174 xmlFree(oldname);
3175#ifdef DEBUG
3176 } else {
3177 xmlGenericError(xmlGenericErrorContext,"End of tag %s: stack empty !!!\n", name);
3178#endif
3179 }
Daniel Veillardf420ac52001-07-04 16:04:09 +00003180 ret = 1;
3181 } else {
3182 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003183 }
3184
3185 if (name != NULL)
3186 xmlFree(name);
3187
Daniel Veillardf420ac52001-07-04 16:04:09 +00003188 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003189}
3190
3191
3192/**
3193 * htmlParseReference:
3194 * @ctxt: an HTML parser context
3195 *
3196 * parse and handle entity references in content,
3197 * this will end-up in a call to character() since this is either a
3198 * CharRef, or a predefined entity.
3199 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003200static void
Owen Taylor3473f882001-02-23 17:55:21 +00003201htmlParseReference(htmlParserCtxtPtr ctxt) {
3202 htmlEntityDescPtr ent;
3203 xmlChar out[6];
3204 xmlChar *name;
3205 if (CUR != '&') return;
3206
3207 if (NXT(1) == '#') {
3208 unsigned int c;
3209 int bits, i = 0;
3210
3211 c = htmlParseCharRef(ctxt);
3212 if (c == 0)
3213 return;
3214
3215 if (c < 0x80) { out[i++]= c; bits= -6; }
3216 else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3217 else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3218 else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3219
3220 for ( ; bits >= 0; bits-= 6) {
3221 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3222 }
3223 out[i] = 0;
3224
3225 htmlCheckParagraph(ctxt);
3226 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3227 ctxt->sax->characters(ctxt->userData, out, i);
3228 } else {
3229 ent = htmlParseEntityRef(ctxt, &name);
3230 if (name == NULL) {
3231 htmlCheckParagraph(ctxt);
3232 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3233 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3234 return;
3235 }
3236 if ((ent == NULL) || (ent->value <= 0)) {
3237 htmlCheckParagraph(ctxt);
3238 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3239 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3240 ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3241 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3242 }
3243 } else {
3244 unsigned int c;
3245 int bits, i = 0;
3246
3247 c = ent->value;
3248 if (c < 0x80)
3249 { out[i++]= c; bits= -6; }
3250 else if (c < 0x800)
3251 { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3252 else if (c < 0x10000)
3253 { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3254 else
3255 { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3256
3257 for ( ; bits >= 0; bits-= 6) {
3258 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3259 }
3260 out[i] = 0;
3261
3262 htmlCheckParagraph(ctxt);
3263 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3264 ctxt->sax->characters(ctxt->userData, out, i);
3265 }
3266 xmlFree(name);
3267 }
3268}
3269
3270/**
3271 * htmlParseContent:
3272 * @ctxt: an HTML parser context
3273 * @name: the node name
3274 *
3275 * Parse a content: comment, sub-element, reference or text.
3276 *
3277 */
3278
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003279static void
Owen Taylor3473f882001-02-23 17:55:21 +00003280htmlParseContent(htmlParserCtxtPtr ctxt) {
3281 xmlChar *currentNode;
3282 int depth;
3283
3284 currentNode = xmlStrdup(ctxt->name);
3285 depth = ctxt->nameNr;
3286 while (1) {
3287 long cons = ctxt->nbChars;
3288
3289 GROW;
3290 /*
3291 * Our tag or one of it's parent or children is ending.
3292 */
3293 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00003294 if (htmlParseEndTag(ctxt) &&
3295 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
3296 if (currentNode != NULL)
3297 xmlFree(currentNode);
3298 return;
3299 }
3300 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00003301 }
3302
3303 /*
3304 * Has this node been popped out during parsing of
3305 * the next element
3306 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00003307 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
3308 (!xmlStrEqual(currentNode, ctxt->name)))
3309 {
Owen Taylor3473f882001-02-23 17:55:21 +00003310 if (currentNode != NULL) xmlFree(currentNode);
3311 return;
3312 }
3313
Daniel Veillardf9533d12001-03-03 10:04:57 +00003314 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
3315 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00003316 /*
3317 * Handle SCRIPT/STYLE separately
3318 */
3319 htmlParseScript(ctxt);
3320 } else {
3321 /*
3322 * Sometimes DOCTYPE arrives in the middle of the document
3323 */
3324 if ((CUR == '<') && (NXT(1) == '!') &&
3325 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3326 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3327 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3328 (UPP(8) == 'E')) {
3329 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3330 ctxt->sax->error(ctxt->userData,
3331 "Misplaced DOCTYPE declaration\n");
3332 ctxt->wellFormed = 0;
3333 htmlParseDocTypeDecl(ctxt);
3334 }
3335
3336 /*
3337 * First case : a comment
3338 */
3339 if ((CUR == '<') && (NXT(1) == '!') &&
3340 (NXT(2) == '-') && (NXT(3) == '-')) {
3341 htmlParseComment(ctxt);
3342 }
3343
3344 /*
3345 * Second case : a sub-element.
3346 */
3347 else if (CUR == '<') {
3348 htmlParseElement(ctxt);
3349 }
3350
3351 /*
3352 * Third case : a reference. If if has not been resolved,
3353 * parsing returns it's Name, create the node
3354 */
3355 else if (CUR == '&') {
3356 htmlParseReference(ctxt);
3357 }
3358
3359 /*
3360 * Fourth : end of the resource
3361 */
3362 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003363 htmlAutoCloseOnEnd(ctxt);
3364 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003365 }
3366
3367 /*
3368 * Last case, text. Note that References are handled directly.
3369 */
3370 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003371 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003372 }
3373
3374 if (cons == ctxt->nbChars) {
3375 if (ctxt->node != NULL) {
3376 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3377 ctxt->sax->error(ctxt->userData,
3378 "detected an error in element content\n");
3379 ctxt->wellFormed = 0;
3380 }
3381 break;
3382 }
3383 }
3384 GROW;
3385 }
3386 if (currentNode != NULL) xmlFree(currentNode);
3387}
3388
3389/**
3390 * htmlParseElement:
3391 * @ctxt: an HTML parser context
3392 *
3393 * parse an HTML element, this is highly recursive
3394 *
3395 * [39] element ::= EmptyElemTag | STag content ETag
3396 *
3397 * [41] Attribute ::= Name Eq AttValue
3398 */
3399
3400void
3401htmlParseElement(htmlParserCtxtPtr ctxt) {
3402 xmlChar *name;
3403 xmlChar *currentNode = NULL;
3404 htmlElemDescPtr info;
3405 htmlParserNodeInfo node_info;
3406 xmlChar *oldname;
3407 int depth = ctxt->nameNr;
3408
3409 /* Capture start position */
3410 if (ctxt->record_info) {
3411 node_info.begin_pos = ctxt->input->consumed +
3412 (CUR_PTR - ctxt->input->base);
3413 node_info.begin_line = ctxt->input->line;
3414 }
3415
3416 oldname = xmlStrdup(ctxt->name);
3417 htmlParseStartTag(ctxt);
3418 name = ctxt->name;
3419#ifdef DEBUG
3420 if (oldname == NULL)
3421 xmlGenericError(xmlGenericErrorContext,
3422 "Start of element %s\n", name);
3423 else if (name == NULL)
3424 xmlGenericError(xmlGenericErrorContext,
3425 "Start of element failed, was %s\n", oldname);
3426 else
3427 xmlGenericError(xmlGenericErrorContext,
3428 "Start of element %s, was %s\n", name, oldname);
3429#endif
3430 if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
3431 (name == NULL)) {
3432 if (CUR == '>')
3433 NEXT;
3434 if (oldname != NULL)
3435 xmlFree(oldname);
3436 return;
3437 }
3438 if (oldname != NULL)
3439 xmlFree(oldname);
3440
3441 /*
3442 * Lookup the info for that element.
3443 */
3444 info = htmlTagLookup(name);
3445 if (info == NULL) {
3446 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3447 ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
3448 name);
3449 ctxt->wellFormed = 0;
3450 } else if (info->depr) {
3451/***************************
3452 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
3453 ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
3454 name);
3455 ***************************/
3456 }
3457
3458 /*
3459 * Check for an Empty Element labelled the XML/SGML way
3460 */
3461 if ((CUR == '/') && (NXT(1) == '>')) {
3462 SKIP(2);
3463 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3464 ctxt->sax->endElement(ctxt->userData, name);
3465 oldname = htmlnamePop(ctxt);
3466#ifdef DEBUG
3467 xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n", oldname);
3468#endif
3469 if (oldname != NULL)
3470 xmlFree(oldname);
3471 return;
3472 }
3473
3474 if (CUR == '>') {
3475 NEXT;
3476 } else {
3477 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3478 ctxt->sax->error(ctxt->userData,
3479 "Couldn't find end of Start Tag %s\n",
3480 name);
3481 ctxt->wellFormed = 0;
3482
3483 /*
3484 * end of parsing of this node.
3485 */
3486 if (xmlStrEqual(name, ctxt->name)) {
3487 nodePop(ctxt);
3488 oldname = htmlnamePop(ctxt);
3489#ifdef DEBUG
3490 xmlGenericError(xmlGenericErrorContext,"End of start tag problem: popping out %s\n", oldname);
3491#endif
3492 if (oldname != NULL)
3493 xmlFree(oldname);
3494 }
3495
3496 /*
3497 * Capture end position and add node
3498 */
3499 if ( currentNode != NULL && ctxt->record_info ) {
3500 node_info.end_pos = ctxt->input->consumed +
3501 (CUR_PTR - ctxt->input->base);
3502 node_info.end_line = ctxt->input->line;
3503 node_info.node = ctxt->node;
3504 xmlParserAddNodeInfo(ctxt, &node_info);
3505 }
3506 return;
3507 }
3508
3509 /*
3510 * Check for an Empty Element from DTD definition
3511 */
3512 if ((info != NULL) && (info->empty)) {
3513 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3514 ctxt->sax->endElement(ctxt->userData, name);
3515 oldname = htmlnamePop(ctxt);
3516#ifdef DEBUG
3517 xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
3518#endif
3519 if (oldname != NULL)
3520 xmlFree(oldname);
3521 return;
3522 }
3523
3524 /*
3525 * Parse the content of the element:
3526 */
3527 currentNode = xmlStrdup(ctxt->name);
3528 depth = ctxt->nameNr;
3529 while (IS_CHAR(CUR)) {
3530 htmlParseContent(ctxt);
3531 if (ctxt->nameNr < depth) break;
3532 }
3533
Owen Taylor3473f882001-02-23 17:55:21 +00003534 /*
3535 * Capture end position and add node
3536 */
3537 if ( currentNode != NULL && ctxt->record_info ) {
3538 node_info.end_pos = ctxt->input->consumed +
3539 (CUR_PTR - ctxt->input->base);
3540 node_info.end_line = ctxt->input->line;
3541 node_info.node = ctxt->node;
3542 xmlParserAddNodeInfo(ctxt, &node_info);
3543 }
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003544 if (!IS_CHAR(CUR)) {
3545 htmlAutoCloseOnEnd(ctxt);
3546 }
3547
Owen Taylor3473f882001-02-23 17:55:21 +00003548 if (currentNode != NULL)
3549 xmlFree(currentNode);
3550}
3551
3552/**
3553 * htmlParseDocument :
3554 * @ctxt: an HTML parser context
3555 *
3556 * parse an HTML document (and build a tree if using the standard SAX
3557 * interface).
3558 *
3559 * Returns 0, -1 in case of error. the parser context is augmented
3560 * as a result of the parsing.
3561 */
3562
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003563static int
Owen Taylor3473f882001-02-23 17:55:21 +00003564htmlParseDocument(htmlParserCtxtPtr ctxt) {
3565 xmlDtdPtr dtd;
3566
3567 htmlDefaultSAXHandlerInit();
3568 ctxt->html = 1;
3569
3570 GROW;
3571 /*
3572 * SAX: beginning of the document processing.
3573 */
3574 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
3575 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
3576
3577 /*
3578 * Wipe out everything which is before the first '<'
3579 */
3580 SKIP_BLANKS;
3581 if (CUR == 0) {
3582 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3583 ctxt->sax->error(ctxt->userData, "Document is empty\n");
3584 ctxt->wellFormed = 0;
3585 }
3586
3587 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
3588 ctxt->sax->startDocument(ctxt->userData);
3589
3590
3591 /*
3592 * Parse possible comments before any content
3593 */
3594 while ((CUR == '<') && (NXT(1) == '!') &&
3595 (NXT(2) == '-') && (NXT(3) == '-')) {
3596 htmlParseComment(ctxt);
3597 SKIP_BLANKS;
3598 }
3599
3600
3601 /*
3602 * Then possibly doc type declaration(s) and more Misc
3603 * (doctypedecl Misc*)?
3604 */
3605 if ((CUR == '<') && (NXT(1) == '!') &&
3606 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3607 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3608 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3609 (UPP(8) == 'E')) {
3610 htmlParseDocTypeDecl(ctxt);
3611 }
3612 SKIP_BLANKS;
3613
3614 /*
3615 * Parse possible comments before any content
3616 */
3617 while ((CUR == '<') && (NXT(1) == '!') &&
3618 (NXT(2) == '-') && (NXT(3) == '-')) {
3619 htmlParseComment(ctxt);
3620 SKIP_BLANKS;
3621 }
3622
3623 /*
3624 * Time to start parsing the tree itself
3625 */
3626 htmlParseContent(ctxt);
3627
3628 /*
3629 * autoclose
3630 */
3631 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003632 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003633
3634
3635 /*
3636 * SAX: end of the document processing.
3637 */
3638 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
3639 ctxt->sax->endDocument(ctxt->userData);
3640
3641 if (ctxt->myDoc != NULL) {
3642 dtd = xmlGetIntSubset(ctxt->myDoc);
3643 if (dtd == NULL)
3644 ctxt->myDoc->intSubset =
3645 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML",
3646 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
3647 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
3648 }
3649 if (! ctxt->wellFormed) return(-1);
3650 return(0);
3651}
3652
3653
3654/************************************************************************
3655 * *
3656 * Parser contexts handling *
3657 * *
3658 ************************************************************************/
3659
3660/**
3661 * xmlInitParserCtxt:
3662 * @ctxt: an HTML parser context
3663 *
3664 * Initialize a parser context
3665 */
3666
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003667static void
Owen Taylor3473f882001-02-23 17:55:21 +00003668htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
3669{
3670 htmlSAXHandler *sax;
3671
3672 if (ctxt == NULL) return;
3673 memset(ctxt, 0, sizeof(htmlParserCtxt));
3674
3675 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
3676 if (sax == NULL) {
3677 xmlGenericError(xmlGenericErrorContext,
3678 "htmlInitParserCtxt: out of memory\n");
3679 }
3680 else
3681 memset(sax, 0, sizeof(htmlSAXHandler));
3682
3683 /* Allocate the Input stack */
3684 ctxt->inputTab = (htmlParserInputPtr *)
3685 xmlMalloc(5 * sizeof(htmlParserInputPtr));
3686 if (ctxt->inputTab == NULL) {
3687 xmlGenericError(xmlGenericErrorContext,
3688 "htmlInitParserCtxt: out of memory\n");
3689 ctxt->inputNr = 0;
3690 ctxt->inputMax = 0;
3691 ctxt->input = NULL;
3692 return;
3693 }
3694 ctxt->inputNr = 0;
3695 ctxt->inputMax = 5;
3696 ctxt->input = NULL;
3697 ctxt->version = NULL;
3698 ctxt->encoding = NULL;
3699 ctxt->standalone = -1;
3700 ctxt->instate = XML_PARSER_START;
3701
3702 /* Allocate the Node stack */
3703 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
3704 if (ctxt->nodeTab == NULL) {
3705 xmlGenericError(xmlGenericErrorContext,
3706 "htmlInitParserCtxt: out of memory\n");
3707 ctxt->nodeNr = 0;
3708 ctxt->nodeMax = 0;
3709 ctxt->node = NULL;
3710 ctxt->inputNr = 0;
3711 ctxt->inputMax = 0;
3712 ctxt->input = NULL;
3713 return;
3714 }
3715 ctxt->nodeNr = 0;
3716 ctxt->nodeMax = 10;
3717 ctxt->node = NULL;
3718
3719 /* Allocate the Name stack */
3720 ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
3721 if (ctxt->nameTab == NULL) {
3722 xmlGenericError(xmlGenericErrorContext,
3723 "htmlInitParserCtxt: out of memory\n");
3724 ctxt->nameNr = 0;
3725 ctxt->nameMax = 10;
3726 ctxt->name = NULL;
3727 ctxt->nodeNr = 0;
3728 ctxt->nodeMax = 0;
3729 ctxt->node = NULL;
3730 ctxt->inputNr = 0;
3731 ctxt->inputMax = 0;
3732 ctxt->input = NULL;
3733 return;
3734 }
3735 ctxt->nameNr = 0;
3736 ctxt->nameMax = 10;
3737 ctxt->name = NULL;
3738
3739 if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
3740 else {
3741 ctxt->sax = sax;
3742 memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
3743 }
3744 ctxt->userData = ctxt;
3745 ctxt->myDoc = NULL;
3746 ctxt->wellFormed = 1;
3747 ctxt->replaceEntities = 0;
3748 ctxt->html = 1;
3749 ctxt->record_info = 0;
3750 ctxt->validate = 0;
3751 ctxt->nbChars = 0;
3752 ctxt->checkIndex = 0;
3753 xmlInitNodeInfoSeq(&ctxt->node_seq);
3754}
3755
3756/**
3757 * htmlFreeParserCtxt:
3758 * @ctxt: an HTML parser context
3759 *
3760 * Free all the memory used by a parser context. However the parsed
3761 * document in ctxt->myDoc is not freed.
3762 */
3763
3764void
3765htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
3766{
3767 xmlFreeParserCtxt(ctxt);
3768}
3769
3770/**
3771 * htmlCreateDocParserCtxt :
3772 * @cur: a pointer to an array of xmlChar
3773 * @encoding: a free form C string describing the HTML document encoding, or NULL
3774 *
3775 * Create a parser context for an HTML document.
3776 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003777 * TODO: check the need to add encoding handling there
3778 *
Owen Taylor3473f882001-02-23 17:55:21 +00003779 * Returns the new parser context or NULL
3780 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003781static htmlParserCtxtPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003782htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003783 htmlParserCtxtPtr ctxt;
3784 htmlParserInputPtr input;
3785 /* htmlCharEncoding enc; */
3786
3787 ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
3788 if (ctxt == NULL) {
3789 perror("malloc");
3790 return(NULL);
3791 }
3792 htmlInitParserCtxt(ctxt);
3793 input = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
3794 if (input == NULL) {
3795 perror("malloc");
3796 xmlFree(ctxt);
3797 return(NULL);
3798 }
3799 memset(input, 0, sizeof(htmlParserInput));
3800
3801 input->line = 1;
3802 input->col = 1;
3803 input->base = cur;
3804 input->cur = cur;
3805
3806 inputPush(ctxt, input);
3807 return(ctxt);
3808}
3809
3810/************************************************************************
3811 * *
3812 * Progressive parsing interfaces *
3813 * *
3814 ************************************************************************/
3815
3816/**
3817 * htmlParseLookupSequence:
3818 * @ctxt: an HTML parser context
3819 * @first: the first char to lookup
3820 * @next: the next char to lookup or zero
3821 * @third: the next char to lookup or zero
3822 *
3823 * Try to find if a sequence (first, next, third) or just (first next) or
3824 * (first) is available in the input stream.
3825 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
3826 * to avoid rescanning sequences of bytes, it DOES change the state of the
3827 * parser, do not use liberally.
3828 * This is basically similar to xmlParseLookupSequence()
3829 *
3830 * Returns the index to the current parsing point if the full sequence
3831 * is available, -1 otherwise.
3832 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003833static int
Owen Taylor3473f882001-02-23 17:55:21 +00003834htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
3835 xmlChar next, xmlChar third) {
3836 int base, len;
3837 htmlParserInputPtr in;
3838 const xmlChar *buf;
3839
3840 in = ctxt->input;
3841 if (in == NULL) return(-1);
3842 base = in->cur - in->base;
3843 if (base < 0) return(-1);
3844 if (ctxt->checkIndex > base)
3845 base = ctxt->checkIndex;
3846 if (in->buf == NULL) {
3847 buf = in->base;
3848 len = in->length;
3849 } else {
3850 buf = in->buf->buffer->content;
3851 len = in->buf->buffer->use;
3852 }
3853 /* take into account the sequence length */
3854 if (third) len -= 2;
3855 else if (next) len --;
3856 for (;base < len;base++) {
3857 if (buf[base] == first) {
3858 if (third != 0) {
3859 if ((buf[base + 1] != next) ||
3860 (buf[base + 2] != third)) continue;
3861 } else if (next != 0) {
3862 if (buf[base + 1] != next) continue;
3863 }
3864 ctxt->checkIndex = 0;
3865#ifdef DEBUG_PUSH
3866 if (next == 0)
3867 xmlGenericError(xmlGenericErrorContext,
3868 "HPP: lookup '%c' found at %d\n",
3869 first, base);
3870 else if (third == 0)
3871 xmlGenericError(xmlGenericErrorContext,
3872 "HPP: lookup '%c%c' found at %d\n",
3873 first, next, base);
3874 else
3875 xmlGenericError(xmlGenericErrorContext,
3876 "HPP: lookup '%c%c%c' found at %d\n",
3877 first, next, third, base);
3878#endif
3879 return(base - (in->cur - in->base));
3880 }
3881 }
3882 ctxt->checkIndex = base;
3883#ifdef DEBUG_PUSH
3884 if (next == 0)
3885 xmlGenericError(xmlGenericErrorContext,
3886 "HPP: lookup '%c' failed\n", first);
3887 else if (third == 0)
3888 xmlGenericError(xmlGenericErrorContext,
3889 "HPP: lookup '%c%c' failed\n", first, next);
3890 else
3891 xmlGenericError(xmlGenericErrorContext,
3892 "HPP: lookup '%c%c%c' failed\n", first, next, third);
3893#endif
3894 return(-1);
3895}
3896
3897/**
3898 * htmlParseTryOrFinish:
3899 * @ctxt: an HTML parser context
3900 * @terminate: last chunk indicator
3901 *
3902 * Try to progress on parsing
3903 *
3904 * Returns zero if no parsing was possible
3905 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003906static int
Owen Taylor3473f882001-02-23 17:55:21 +00003907htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
3908 int ret = 0;
3909 htmlParserInputPtr in;
3910 int avail = 0;
3911 xmlChar cur, next;
3912
3913#ifdef DEBUG_PUSH
3914 switch (ctxt->instate) {
3915 case XML_PARSER_EOF:
3916 xmlGenericError(xmlGenericErrorContext,
3917 "HPP: try EOF\n"); break;
3918 case XML_PARSER_START:
3919 xmlGenericError(xmlGenericErrorContext,
3920 "HPP: try START\n"); break;
3921 case XML_PARSER_MISC:
3922 xmlGenericError(xmlGenericErrorContext,
3923 "HPP: try MISC\n");break;
3924 case XML_PARSER_COMMENT:
3925 xmlGenericError(xmlGenericErrorContext,
3926 "HPP: try COMMENT\n");break;
3927 case XML_PARSER_PROLOG:
3928 xmlGenericError(xmlGenericErrorContext,
3929 "HPP: try PROLOG\n");break;
3930 case XML_PARSER_START_TAG:
3931 xmlGenericError(xmlGenericErrorContext,
3932 "HPP: try START_TAG\n");break;
3933 case XML_PARSER_CONTENT:
3934 xmlGenericError(xmlGenericErrorContext,
3935 "HPP: try CONTENT\n");break;
3936 case XML_PARSER_CDATA_SECTION:
3937 xmlGenericError(xmlGenericErrorContext,
3938 "HPP: try CDATA_SECTION\n");break;
3939 case XML_PARSER_END_TAG:
3940 xmlGenericError(xmlGenericErrorContext,
3941 "HPP: try END_TAG\n");break;
3942 case XML_PARSER_ENTITY_DECL:
3943 xmlGenericError(xmlGenericErrorContext,
3944 "HPP: try ENTITY_DECL\n");break;
3945 case XML_PARSER_ENTITY_VALUE:
3946 xmlGenericError(xmlGenericErrorContext,
3947 "HPP: try ENTITY_VALUE\n");break;
3948 case XML_PARSER_ATTRIBUTE_VALUE:
3949 xmlGenericError(xmlGenericErrorContext,
3950 "HPP: try ATTRIBUTE_VALUE\n");break;
3951 case XML_PARSER_DTD:
3952 xmlGenericError(xmlGenericErrorContext,
3953 "HPP: try DTD\n");break;
3954 case XML_PARSER_EPILOG:
3955 xmlGenericError(xmlGenericErrorContext,
3956 "HPP: try EPILOG\n");break;
3957 case XML_PARSER_PI:
3958 xmlGenericError(xmlGenericErrorContext,
3959 "HPP: try PI\n");break;
3960 case XML_PARSER_SYSTEM_LITERAL:
3961 xmlGenericError(xmlGenericErrorContext,
3962 "HPP: try SYSTEM_LITERAL\n");break;
3963 }
3964#endif
3965
3966 while (1) {
3967
3968 in = ctxt->input;
3969 if (in == NULL) break;
3970 if (in->buf == NULL)
3971 avail = in->length - (in->cur - in->base);
3972 else
3973 avail = in->buf->buffer->use - (in->cur - in->base);
3974 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003975 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003976 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
3977 /*
3978 * SAX: end of the document processing.
3979 */
3980 ctxt->instate = XML_PARSER_EOF;
3981 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
3982 ctxt->sax->endDocument(ctxt->userData);
3983 }
3984 }
3985 if (avail < 1)
3986 goto done;
3987 switch (ctxt->instate) {
3988 case XML_PARSER_EOF:
3989 /*
3990 * Document parsing is done !
3991 */
3992 goto done;
3993 case XML_PARSER_START:
3994 /*
3995 * Very first chars read from the document flow.
3996 */
3997 cur = in->cur[0];
3998 if (IS_BLANK(cur)) {
3999 SKIP_BLANKS;
4000 if (in->buf == NULL)
4001 avail = in->length - (in->cur - in->base);
4002 else
4003 avail = in->buf->buffer->use - (in->cur - in->base);
4004 }
4005 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4006 ctxt->sax->setDocumentLocator(ctxt->userData,
4007 &xmlDefaultSAXLocator);
4008 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4009 (!ctxt->disableSAX))
4010 ctxt->sax->startDocument(ctxt->userData);
4011
4012 cur = in->cur[0];
4013 next = in->cur[1];
4014 if ((cur == '<') && (next == '!') &&
4015 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4016 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4017 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4018 (UPP(8) == 'E')) {
4019 if ((!terminate) &&
4020 (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
4021 goto done;
4022#ifdef DEBUG_PUSH
4023 xmlGenericError(xmlGenericErrorContext,
4024 "HPP: Parsing internal subset\n");
4025#endif
4026 htmlParseDocTypeDecl(ctxt);
4027 ctxt->instate = XML_PARSER_PROLOG;
4028#ifdef DEBUG_PUSH
4029 xmlGenericError(xmlGenericErrorContext,
4030 "HPP: entering PROLOG\n");
4031#endif
4032 } else {
4033 ctxt->instate = XML_PARSER_MISC;
4034 }
4035#ifdef DEBUG_PUSH
4036 xmlGenericError(xmlGenericErrorContext,
4037 "HPP: entering MISC\n");
4038#endif
4039 break;
4040 case XML_PARSER_MISC:
4041 SKIP_BLANKS;
4042 if (in->buf == NULL)
4043 avail = in->length - (in->cur - in->base);
4044 else
4045 avail = in->buf->buffer->use - (in->cur - in->base);
4046 if (avail < 2)
4047 goto done;
4048 cur = in->cur[0];
4049 next = in->cur[1];
4050 if ((cur == '<') && (next == '!') &&
4051 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4052 if ((!terminate) &&
4053 (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
4054 goto done;
4055#ifdef DEBUG_PUSH
4056 xmlGenericError(xmlGenericErrorContext,
4057 "HPP: Parsing Comment\n");
4058#endif
4059 htmlParseComment(ctxt);
4060 ctxt->instate = XML_PARSER_MISC;
4061 } else if ((cur == '<') && (next == '!') &&
4062 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4063 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4064 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4065 (UPP(8) == 'E')) {
4066 if ((!terminate) &&
4067 (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
4068 goto done;
4069#ifdef DEBUG_PUSH
4070 xmlGenericError(xmlGenericErrorContext,
4071 "HPP: Parsing internal subset\n");
4072#endif
4073 htmlParseDocTypeDecl(ctxt);
4074 ctxt->instate = XML_PARSER_PROLOG;
4075#ifdef DEBUG_PUSH
4076 xmlGenericError(xmlGenericErrorContext,
4077 "HPP: entering PROLOG\n");
4078#endif
4079 } else if ((cur == '<') && (next == '!') &&
4080 (avail < 9)) {
4081 goto done;
4082 } else {
4083 ctxt->instate = XML_PARSER_START_TAG;
4084#ifdef DEBUG_PUSH
4085 xmlGenericError(xmlGenericErrorContext,
4086 "HPP: entering START_TAG\n");
4087#endif
4088 }
4089 break;
4090 case XML_PARSER_PROLOG:
4091 SKIP_BLANKS;
4092 if (in->buf == NULL)
4093 avail = in->length - (in->cur - in->base);
4094 else
4095 avail = in->buf->buffer->use - (in->cur - in->base);
4096 if (avail < 2)
4097 goto done;
4098 cur = in->cur[0];
4099 next = in->cur[1];
4100 if ((cur == '<') && (next == '!') &&
4101 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4102 if ((!terminate) &&
4103 (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
4104 goto done;
4105#ifdef DEBUG_PUSH
4106 xmlGenericError(xmlGenericErrorContext,
4107 "HPP: Parsing Comment\n");
4108#endif
4109 htmlParseComment(ctxt);
4110 ctxt->instate = XML_PARSER_PROLOG;
4111 } else if ((cur == '<') && (next == '!') &&
4112 (avail < 4)) {
4113 goto done;
4114 } else {
4115 ctxt->instate = XML_PARSER_START_TAG;
4116#ifdef DEBUG_PUSH
4117 xmlGenericError(xmlGenericErrorContext,
4118 "HPP: entering START_TAG\n");
4119#endif
4120 }
4121 break;
4122 case XML_PARSER_EPILOG:
4123 if (in->buf == NULL)
4124 avail = in->length - (in->cur - in->base);
4125 else
4126 avail = in->buf->buffer->use - (in->cur - in->base);
4127 if (avail < 1)
4128 goto done;
4129 cur = in->cur[0];
4130 if (IS_BLANK(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004131 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004132 goto done;
4133 }
4134 if (avail < 2)
4135 goto done;
4136 next = in->cur[1];
4137 if ((cur == '<') && (next == '!') &&
4138 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4139 if ((!terminate) &&
4140 (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
4141 goto done;
4142#ifdef DEBUG_PUSH
4143 xmlGenericError(xmlGenericErrorContext,
4144 "HPP: Parsing Comment\n");
4145#endif
4146 htmlParseComment(ctxt);
4147 ctxt->instate = XML_PARSER_EPILOG;
4148 } else if ((cur == '<') && (next == '!') &&
4149 (avail < 4)) {
4150 goto done;
4151 } else {
4152 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004153 ctxt->wellFormed = 0;
4154 ctxt->instate = XML_PARSER_EOF;
4155#ifdef DEBUG_PUSH
4156 xmlGenericError(xmlGenericErrorContext,
4157 "HPP: entering EOF\n");
4158#endif
4159 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4160 ctxt->sax->endDocument(ctxt->userData);
4161 goto done;
4162 }
4163 break;
4164 case XML_PARSER_START_TAG: {
4165 xmlChar *name, *oldname;
4166 int depth = ctxt->nameNr;
4167 htmlElemDescPtr info;
4168
4169 if (avail < 2)
4170 goto done;
4171 cur = in->cur[0];
4172 if (cur != '<') {
4173 ctxt->instate = XML_PARSER_CONTENT;
4174#ifdef DEBUG_PUSH
4175 xmlGenericError(xmlGenericErrorContext,
4176 "HPP: entering CONTENT\n");
4177#endif
4178 break;
4179 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004180 if (in->cur[1] == '/') {
4181 ctxt->instate = XML_PARSER_END_TAG;
4182 ctxt->checkIndex = 0;
4183#ifdef DEBUG_PUSH
4184 xmlGenericError(xmlGenericErrorContext,
4185 "HPP: entering END_TAG\n");
4186#endif
4187 break;
4188 }
Owen Taylor3473f882001-02-23 17:55:21 +00004189 if ((!terminate) &&
4190 (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
4191 goto done;
4192
4193 oldname = xmlStrdup(ctxt->name);
4194 htmlParseStartTag(ctxt);
4195 name = ctxt->name;
4196#ifdef DEBUG
4197 if (oldname == NULL)
4198 xmlGenericError(xmlGenericErrorContext,
4199 "Start of element %s\n", name);
4200 else if (name == NULL)
4201 xmlGenericError(xmlGenericErrorContext,
4202 "Start of element failed, was %s\n",
4203 oldname);
4204 else
4205 xmlGenericError(xmlGenericErrorContext,
4206 "Start of element %s, was %s\n",
4207 name, oldname);
4208#endif
4209 if (((depth == ctxt->nameNr) &&
4210 (xmlStrEqual(oldname, ctxt->name))) ||
4211 (name == NULL)) {
4212 if (CUR == '>')
4213 NEXT;
4214 if (oldname != NULL)
4215 xmlFree(oldname);
4216 break;
4217 }
4218 if (oldname != NULL)
4219 xmlFree(oldname);
4220
4221 /*
4222 * Lookup the info for that element.
4223 */
4224 info = htmlTagLookup(name);
4225 if (info == NULL) {
4226 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4227 ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
4228 name);
4229 ctxt->wellFormed = 0;
4230 } else if (info->depr) {
4231 /***************************
4232 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
4233 ctxt->sax->warning(ctxt->userData,
4234 "Tag %s is deprecated\n",
4235 name);
4236 ***************************/
4237 }
4238
4239 /*
4240 * Check for an Empty Element labelled the XML/SGML way
4241 */
4242 if ((CUR == '/') && (NXT(1) == '>')) {
4243 SKIP(2);
4244 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4245 ctxt->sax->endElement(ctxt->userData, name);
4246 oldname = htmlnamePop(ctxt);
4247#ifdef DEBUG
4248 xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n",
4249 oldname);
4250#endif
4251 if (oldname != NULL)
4252 xmlFree(oldname);
4253 ctxt->instate = XML_PARSER_CONTENT;
4254#ifdef DEBUG_PUSH
4255 xmlGenericError(xmlGenericErrorContext,
4256 "HPP: entering CONTENT\n");
4257#endif
4258 break;
4259 }
4260
4261 if (CUR == '>') {
4262 NEXT;
4263 } else {
4264 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4265 ctxt->sax->error(ctxt->userData,
4266 "Couldn't find end of Start Tag %s\n",
4267 name);
4268 ctxt->wellFormed = 0;
4269
4270 /*
4271 * end of parsing of this node.
4272 */
4273 if (xmlStrEqual(name, ctxt->name)) {
4274 nodePop(ctxt);
4275 oldname = htmlnamePop(ctxt);
4276#ifdef DEBUG
4277 xmlGenericError(xmlGenericErrorContext,
4278 "End of start tag problem: popping out %s\n", oldname);
4279#endif
4280 if (oldname != NULL)
4281 xmlFree(oldname);
4282 }
4283
4284 ctxt->instate = XML_PARSER_CONTENT;
4285#ifdef DEBUG_PUSH
4286 xmlGenericError(xmlGenericErrorContext,
4287 "HPP: entering CONTENT\n");
4288#endif
4289 break;
4290 }
4291
4292 /*
4293 * Check for an Empty Element from DTD definition
4294 */
4295 if ((info != NULL) && (info->empty)) {
4296 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4297 ctxt->sax->endElement(ctxt->userData, name);
4298 oldname = htmlnamePop(ctxt);
4299#ifdef DEBUG
4300 xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
4301#endif
4302 if (oldname != NULL)
4303 xmlFree(oldname);
4304 }
4305 ctxt->instate = XML_PARSER_CONTENT;
4306#ifdef DEBUG_PUSH
4307 xmlGenericError(xmlGenericErrorContext,
4308 "HPP: entering CONTENT\n");
4309#endif
4310 break;
4311 }
4312 case XML_PARSER_CONTENT: {
4313 long cons;
4314 /*
4315 * Handle preparsed entities and charRef
4316 */
4317 if (ctxt->token != 0) {
4318 xmlChar chr[2] = { 0 , 0 } ;
4319
4320 chr[0] = (xmlChar) ctxt->token;
4321 htmlCheckParagraph(ctxt);
4322 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4323 ctxt->sax->characters(ctxt->userData, chr, 1);
4324 ctxt->token = 0;
4325 ctxt->checkIndex = 0;
4326 }
4327 if ((avail == 1) && (terminate)) {
4328 cur = in->cur[0];
4329 if ((cur != '<') && (cur != '&')) {
4330 if (ctxt->sax != NULL) {
4331 if (IS_BLANK(cur)) {
4332 if (ctxt->sax->ignorableWhitespace != NULL)
4333 ctxt->sax->ignorableWhitespace(
4334 ctxt->userData, &cur, 1);
4335 } else {
4336 htmlCheckParagraph(ctxt);
4337 if (ctxt->sax->characters != NULL)
4338 ctxt->sax->characters(
4339 ctxt->userData, &cur, 1);
4340 }
4341 }
4342 ctxt->token = 0;
4343 ctxt->checkIndex = 0;
4344 NEXT;
4345 }
4346 break;
4347 }
4348 if (avail < 2)
4349 goto done;
4350 cur = in->cur[0];
4351 next = in->cur[1];
4352 cons = ctxt->nbChars;
4353 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
4354 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
4355 /*
4356 * Handle SCRIPT/STYLE separately
4357 */
4358 if ((!terminate) &&
4359 (htmlParseLookupSequence(ctxt, '<', '/', 0) < 0))
4360 goto done;
4361 htmlParseScript(ctxt);
4362 if ((cur == '<') && (next == '/')) {
4363 ctxt->instate = XML_PARSER_END_TAG;
4364 ctxt->checkIndex = 0;
4365#ifdef DEBUG_PUSH
4366 xmlGenericError(xmlGenericErrorContext,
4367 "HPP: entering END_TAG\n");
4368#endif
4369 break;
4370 }
4371 } else {
4372 /*
4373 * Sometimes DOCTYPE arrives in the middle of the document
4374 */
4375 if ((cur == '<') && (next == '!') &&
4376 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4377 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4378 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4379 (UPP(8) == 'E')) {
4380 if ((!terminate) &&
4381 (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
4382 goto done;
4383 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4384 ctxt->sax->error(ctxt->userData,
4385 "Misplaced DOCTYPE declaration\n");
4386 ctxt->wellFormed = 0;
4387 htmlParseDocTypeDecl(ctxt);
4388 } else if ((cur == '<') && (next == '!') &&
4389 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4390 if ((!terminate) &&
4391 (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
4392 goto done;
4393#ifdef DEBUG_PUSH
4394 xmlGenericError(xmlGenericErrorContext,
4395 "HPP: Parsing Comment\n");
4396#endif
4397 htmlParseComment(ctxt);
4398 ctxt->instate = XML_PARSER_CONTENT;
4399 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
4400 goto done;
4401 } else if ((cur == '<') && (next == '/')) {
4402 ctxt->instate = XML_PARSER_END_TAG;
4403 ctxt->checkIndex = 0;
4404#ifdef DEBUG_PUSH
4405 xmlGenericError(xmlGenericErrorContext,
4406 "HPP: entering END_TAG\n");
4407#endif
4408 break;
4409 } else if (cur == '<') {
4410 ctxt->instate = XML_PARSER_START_TAG;
4411 ctxt->checkIndex = 0;
4412#ifdef DEBUG_PUSH
4413 xmlGenericError(xmlGenericErrorContext,
4414 "HPP: entering START_TAG\n");
4415#endif
4416 break;
4417 } else if (cur == '&') {
4418 if ((!terminate) &&
4419 (htmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
4420 goto done;
4421#ifdef DEBUG_PUSH
4422 xmlGenericError(xmlGenericErrorContext,
4423 "HPP: Parsing Reference\n");
4424#endif
4425 /* TODO: check generation of subtrees if noent !!! */
4426 htmlParseReference(ctxt);
4427 } else {
4428 /* TODO Avoid the extra copy, handle directly !!!!!! */
4429 /*
4430 * Goal of the following test is :
4431 * - minimize calls to the SAX 'character' callback
4432 * when they are mergeable
4433 */
4434 if ((ctxt->inputNr == 1) &&
4435 (avail < HTML_PARSER_BIG_BUFFER_SIZE)) {
4436 if ((!terminate) &&
4437 (htmlParseLookupSequence(ctxt, '<', 0, 0) < 0))
4438 goto done;
4439 }
4440 ctxt->checkIndex = 0;
4441#ifdef DEBUG_PUSH
4442 xmlGenericError(xmlGenericErrorContext,
4443 "HPP: Parsing char data\n");
4444#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004445 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004446 }
4447 }
4448 if (cons == ctxt->nbChars) {
4449 if (ctxt->node != NULL) {
4450 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4451 ctxt->sax->error(ctxt->userData,
4452 "detected an error in element content\n");
4453 ctxt->wellFormed = 0;
4454 }
4455 NEXT;
4456 break;
4457 }
4458
4459 break;
4460 }
4461 case XML_PARSER_END_TAG:
4462 if (avail < 2)
4463 goto done;
4464 if ((!terminate) &&
4465 (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
4466 goto done;
4467 htmlParseEndTag(ctxt);
4468 if (ctxt->nameNr == 0) {
4469 ctxt->instate = XML_PARSER_EPILOG;
4470 } else {
4471 ctxt->instate = XML_PARSER_CONTENT;
4472 }
4473 ctxt->checkIndex = 0;
4474#ifdef DEBUG_PUSH
4475 xmlGenericError(xmlGenericErrorContext,
4476 "HPP: entering CONTENT\n");
4477#endif
4478 break;
4479 case XML_PARSER_CDATA_SECTION:
4480 xmlGenericError(xmlGenericErrorContext,
4481 "HPP: internal error, state == CDATA\n");
4482 ctxt->instate = XML_PARSER_CONTENT;
4483 ctxt->checkIndex = 0;
4484#ifdef DEBUG_PUSH
4485 xmlGenericError(xmlGenericErrorContext,
4486 "HPP: entering CONTENT\n");
4487#endif
4488 break;
4489 case XML_PARSER_DTD:
4490 xmlGenericError(xmlGenericErrorContext,
4491 "HPP: internal error, state == DTD\n");
4492 ctxt->instate = XML_PARSER_CONTENT;
4493 ctxt->checkIndex = 0;
4494#ifdef DEBUG_PUSH
4495 xmlGenericError(xmlGenericErrorContext,
4496 "HPP: entering CONTENT\n");
4497#endif
4498 break;
4499 case XML_PARSER_COMMENT:
4500 xmlGenericError(xmlGenericErrorContext,
4501 "HPP: internal error, state == COMMENT\n");
4502 ctxt->instate = XML_PARSER_CONTENT;
4503 ctxt->checkIndex = 0;
4504#ifdef DEBUG_PUSH
4505 xmlGenericError(xmlGenericErrorContext,
4506 "HPP: entering CONTENT\n");
4507#endif
4508 break;
4509 case XML_PARSER_PI:
4510 xmlGenericError(xmlGenericErrorContext,
4511 "HPP: internal error, state == PI\n");
4512 ctxt->instate = XML_PARSER_CONTENT;
4513 ctxt->checkIndex = 0;
4514#ifdef DEBUG_PUSH
4515 xmlGenericError(xmlGenericErrorContext,
4516 "HPP: entering CONTENT\n");
4517#endif
4518 break;
4519 case XML_PARSER_ENTITY_DECL:
4520 xmlGenericError(xmlGenericErrorContext,
4521 "HPP: internal error, state == ENTITY_DECL\n");
4522 ctxt->instate = XML_PARSER_CONTENT;
4523 ctxt->checkIndex = 0;
4524#ifdef DEBUG_PUSH
4525 xmlGenericError(xmlGenericErrorContext,
4526 "HPP: entering CONTENT\n");
4527#endif
4528 break;
4529 case XML_PARSER_ENTITY_VALUE:
4530 xmlGenericError(xmlGenericErrorContext,
4531 "HPP: internal error, state == ENTITY_VALUE\n");
4532 ctxt->instate = XML_PARSER_CONTENT;
4533 ctxt->checkIndex = 0;
4534#ifdef DEBUG_PUSH
4535 xmlGenericError(xmlGenericErrorContext,
4536 "HPP: entering DTD\n");
4537#endif
4538 break;
4539 case XML_PARSER_ATTRIBUTE_VALUE:
4540 xmlGenericError(xmlGenericErrorContext,
4541 "HPP: internal error, state == ATTRIBUTE_VALUE\n");
4542 ctxt->instate = XML_PARSER_START_TAG;
4543 ctxt->checkIndex = 0;
4544#ifdef DEBUG_PUSH
4545 xmlGenericError(xmlGenericErrorContext,
4546 "HPP: entering START_TAG\n");
4547#endif
4548 break;
4549 case XML_PARSER_SYSTEM_LITERAL:
4550 xmlGenericError(xmlGenericErrorContext,
4551 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
4552 ctxt->instate = XML_PARSER_CONTENT;
4553 ctxt->checkIndex = 0;
4554#ifdef DEBUG_PUSH
4555 xmlGenericError(xmlGenericErrorContext,
4556 "HPP: entering CONTENT\n");
4557#endif
4558 break;
4559 case XML_PARSER_IGNORE:
4560 xmlGenericError(xmlGenericErrorContext,
4561 "HPP: internal error, state == XML_PARSER_IGNORE\n");
4562 ctxt->instate = XML_PARSER_CONTENT;
4563 ctxt->checkIndex = 0;
4564#ifdef DEBUG_PUSH
4565 xmlGenericError(xmlGenericErrorContext,
4566 "HPP: entering CONTENT\n");
4567#endif
4568 break;
4569 }
4570 }
4571done:
4572 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004573 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004574 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4575 /*
4576 * SAX: end of the document processing.
4577 */
4578 ctxt->instate = XML_PARSER_EOF;
4579 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4580 ctxt->sax->endDocument(ctxt->userData);
4581 }
4582 }
4583 if ((ctxt->myDoc != NULL) &&
4584 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
4585 (ctxt->instate == XML_PARSER_EPILOG))) {
4586 xmlDtdPtr dtd;
4587 dtd = xmlGetIntSubset(ctxt->myDoc);
4588 if (dtd == NULL)
4589 ctxt->myDoc->intSubset =
4590 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML",
4591 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4592 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4593 }
4594#ifdef DEBUG_PUSH
4595 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
4596#endif
4597 return(ret);
4598}
4599
4600/**
Owen Taylor3473f882001-02-23 17:55:21 +00004601 * htmlParseChunk:
4602 * @ctxt: an XML parser context
4603 * @chunk: an char array
4604 * @size: the size in byte of the chunk
4605 * @terminate: last chunk indicator
4606 *
4607 * Parse a Chunk of memory
4608 *
4609 * Returns zero if no error, the xmlParserErrors otherwise.
4610 */
4611int
4612htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
4613 int terminate) {
4614 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
4615 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
4616 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
4617 int cur = ctxt->input->cur - ctxt->input->base;
4618
4619 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
4620 ctxt->input->base = ctxt->input->buf->buffer->content + base;
4621 ctxt->input->cur = ctxt->input->base + cur;
4622#ifdef DEBUG_PUSH
4623 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
4624#endif
4625
4626 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
4627 htmlParseTryOrFinish(ctxt, terminate);
4628 } else if (ctxt->instate != XML_PARSER_EOF) {
4629 xmlParserInputBufferPush(ctxt->input->buf, 0, "");
4630 htmlParseTryOrFinish(ctxt, terminate);
4631 }
4632 if (terminate) {
4633 if ((ctxt->instate != XML_PARSER_EOF) &&
4634 (ctxt->instate != XML_PARSER_EPILOG) &&
4635 (ctxt->instate != XML_PARSER_MISC)) {
4636 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004637 ctxt->wellFormed = 0;
4638 }
4639 if (ctxt->instate != XML_PARSER_EOF) {
4640 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4641 ctxt->sax->endDocument(ctxt->userData);
4642 }
4643 ctxt->instate = XML_PARSER_EOF;
4644 }
4645 return((xmlParserErrors) ctxt->errNo);
4646}
4647
4648/************************************************************************
4649 * *
4650 * User entry points *
4651 * *
4652 ************************************************************************/
4653
4654/**
4655 * htmlCreatePushParserCtxt :
4656 * @sax: a SAX handler
4657 * @user_data: The user data returned on SAX callbacks
4658 * @chunk: a pointer to an array of chars
4659 * @size: number of chars in the array
4660 * @filename: an optional file name or URI
4661 * @enc: an optional encoding
4662 *
4663 * Create a parser context for using the HTML parser in push mode
4664 * To allow content encoding detection, @size should be >= 4
4665 * The value of @filename is used for fetching external entities
4666 * and error/warning reports.
4667 *
4668 * Returns the new parser context or NULL
4669 */
4670htmlParserCtxtPtr
4671htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
4672 const char *chunk, int size, const char *filename,
4673 xmlCharEncoding enc) {
4674 htmlParserCtxtPtr ctxt;
4675 htmlParserInputPtr inputStream;
4676 xmlParserInputBufferPtr buf;
4677
4678 buf = xmlAllocParserInputBuffer(enc);
4679 if (buf == NULL) return(NULL);
4680
4681 ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
4682 if (ctxt == NULL) {
4683 xmlFree(buf);
4684 return(NULL);
4685 }
4686 memset(ctxt, 0, sizeof(htmlParserCtxt));
4687 htmlInitParserCtxt(ctxt);
4688 if (sax != NULL) {
4689 if (ctxt->sax != &htmlDefaultSAXHandler)
4690 xmlFree(ctxt->sax);
4691 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
4692 if (ctxt->sax == NULL) {
4693 xmlFree(buf);
4694 xmlFree(ctxt);
4695 return(NULL);
4696 }
4697 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
4698 if (user_data != NULL)
4699 ctxt->userData = user_data;
4700 }
4701 if (filename == NULL) {
4702 ctxt->directory = NULL;
4703 } else {
4704 ctxt->directory = xmlParserGetDirectory(filename);
4705 }
4706
4707 inputStream = htmlNewInputStream(ctxt);
4708 if (inputStream == NULL) {
4709 xmlFreeParserCtxt(ctxt);
4710 return(NULL);
4711 }
4712
4713 if (filename == NULL)
4714 inputStream->filename = NULL;
4715 else
4716 inputStream->filename = xmlMemStrdup(filename);
4717 inputStream->buf = buf;
4718 inputStream->base = inputStream->buf->buffer->content;
4719 inputStream->cur = inputStream->buf->buffer->content;
4720
4721 inputPush(ctxt, inputStream);
4722
4723 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
4724 (ctxt->input->buf != NULL)) {
4725 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
4726#ifdef DEBUG_PUSH
4727 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
4728#endif
4729 }
4730
4731 return(ctxt);
4732}
4733
4734/**
4735 * htmlSAXParseDoc :
4736 * @cur: a pointer to an array of xmlChar
4737 * @encoding: a free form C string describing the HTML document encoding, or NULL
4738 * @sax: the SAX handler block
4739 * @userData: if using SAX, this pointer will be provided on callbacks.
4740 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00004741 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
4742 * to handle parse events. If sax is NULL, fallback to the default DOM
4743 * behavior and return a tree.
Owen Taylor3473f882001-02-23 17:55:21 +00004744 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00004745 * Returns the resulting document tree unless SAX is NULL or the document is
4746 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00004747 */
4748
4749htmlDocPtr
4750htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
4751 htmlDocPtr ret;
4752 htmlParserCtxtPtr ctxt;
4753
4754 if (cur == NULL) return(NULL);
4755
4756
4757 ctxt = htmlCreateDocParserCtxt(cur, encoding);
4758 if (ctxt == NULL) return(NULL);
4759 if (sax != NULL) {
4760 ctxt->sax = sax;
4761 ctxt->userData = userData;
4762 }
4763
4764 htmlParseDocument(ctxt);
4765 ret = ctxt->myDoc;
4766 if (sax != NULL) {
4767 ctxt->sax = NULL;
4768 ctxt->userData = NULL;
4769 }
4770 htmlFreeParserCtxt(ctxt);
4771
4772 return(ret);
4773}
4774
4775/**
4776 * htmlParseDoc :
4777 * @cur: a pointer to an array of xmlChar
4778 * @encoding: a free form C string describing the HTML document encoding, or NULL
4779 *
4780 * parse an HTML in-memory document and build a tree.
4781 *
4782 * Returns the resulting document tree
4783 */
4784
4785htmlDocPtr
4786htmlParseDoc(xmlChar *cur, const char *encoding) {
4787 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
4788}
4789
4790
4791/**
4792 * htmlCreateFileParserCtxt :
4793 * @filename: the filename
4794 * @encoding: a free form C string describing the HTML document encoding, or NULL
4795 *
4796 * Create a parser context for a file content.
4797 * Automatic support for ZLIB/Compress compressed document is provided
4798 * by default if found at compile-time.
4799 *
4800 * Returns the new parser context or NULL
4801 */
4802htmlParserCtxtPtr
4803htmlCreateFileParserCtxt(const char *filename, const char *encoding)
4804{
4805 htmlParserCtxtPtr ctxt;
4806 htmlParserInputPtr inputStream;
4807 xmlParserInputBufferPtr buf;
4808 /* htmlCharEncoding enc; */
4809 xmlChar *content, *content_line = (xmlChar *) "charset=";
4810
4811 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
4812 if (buf == NULL) return(NULL);
4813
4814 ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
4815 if (ctxt == NULL) {
4816 perror("malloc");
4817 return(NULL);
4818 }
4819 memset(ctxt, 0, sizeof(htmlParserCtxt));
4820 htmlInitParserCtxt(ctxt);
4821 inputStream = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
4822 if (inputStream == NULL) {
4823 perror("malloc");
4824 xmlFree(ctxt);
4825 return(NULL);
4826 }
4827 memset(inputStream, 0, sizeof(htmlParserInput));
4828
4829 inputStream->filename = xmlMemStrdup(filename);
4830 inputStream->line = 1;
4831 inputStream->col = 1;
4832 inputStream->buf = buf;
4833 inputStream->directory = NULL;
4834
4835 inputStream->base = inputStream->buf->buffer->content;
4836 inputStream->cur = inputStream->buf->buffer->content;
4837 inputStream->free = NULL;
4838
4839 inputPush(ctxt, inputStream);
4840
4841 /* set encoding */
4842 if (encoding) {
4843 content = xmlMalloc (xmlStrlen(content_line) + strlen(encoding) + 1);
4844 if (content) {
4845 strcpy ((char *)content, (char *)content_line);
4846 strcat ((char *)content, (char *)encoding);
4847 htmlCheckEncoding (ctxt, content);
4848 xmlFree (content);
4849 }
4850 }
4851
4852 return(ctxt);
4853}
4854
4855/**
4856 * htmlSAXParseFile :
4857 * @filename: the filename
4858 * @encoding: a free form C string describing the HTML document encoding, or NULL
4859 * @sax: the SAX handler block
4860 * @userData: if using SAX, this pointer will be provided on callbacks.
4861 *
4862 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
4863 * compressed document is provided by default if found at compile-time.
4864 * It use the given SAX function block to handle the parsing callback.
4865 * If sax is NULL, fallback to the default DOM tree building routines.
4866 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00004867 * Returns the resulting document tree unless SAX is NULL or the document is
4868 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00004869 */
4870
4871htmlDocPtr
4872htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
4873 void *userData) {
4874 htmlDocPtr ret;
4875 htmlParserCtxtPtr ctxt;
4876 htmlSAXHandlerPtr oldsax = NULL;
4877
4878 ctxt = htmlCreateFileParserCtxt(filename, encoding);
4879 if (ctxt == NULL) return(NULL);
4880 if (sax != NULL) {
4881 oldsax = ctxt->sax;
4882 ctxt->sax = sax;
4883 ctxt->userData = userData;
4884 }
4885
4886 htmlParseDocument(ctxt);
4887
4888 ret = ctxt->myDoc;
4889 if (sax != NULL) {
4890 ctxt->sax = oldsax;
4891 ctxt->userData = NULL;
4892 }
4893 htmlFreeParserCtxt(ctxt);
4894
4895 return(ret);
4896}
4897
4898/**
4899 * htmlParseFile :
4900 * @filename: the filename
4901 * @encoding: a free form C string describing the HTML document encoding, or NULL
4902 *
4903 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
4904 * compressed document is provided by default if found at compile-time.
4905 *
4906 * Returns the resulting document tree
4907 */
4908
4909htmlDocPtr
4910htmlParseFile(const char *filename, const char *encoding) {
4911 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
4912}
4913
4914/**
4915 * htmlHandleOmittedElem:
4916 * @val: int 0 or 1
4917 *
4918 * Set and return the previous value for handling HTML omitted tags.
4919 *
4920 * Returns the last value for 0 for no handling, 1 for auto insertion.
4921 */
4922
4923int
4924htmlHandleOmittedElem(int val) {
4925 int old = htmlOmittedDefaultValue;
4926
4927 htmlOmittedDefaultValue = val;
4928 return(old);
4929}
4930
4931#endif /* LIBXML_HTML_ENABLED */