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