Annotation of XML/HTMLparser.c, revision 1.84

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, "&#160;" },
                    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);
1.82      veillard 1715:     } else if (xmlNodeIsText(lastChild)) {
1.1       daniel   1716:         return(0);
1.82      veillard 1717:     } else if (xmlStrEqual(lastChild->name, BAD_CAST"b")) {
                   1718:         return(0);
                   1719:     } else if (xmlStrEqual(lastChild->name, BAD_CAST"bold")) {
                   1720:         return(0);
                   1721:     } else if (xmlStrEqual(lastChild->name, BAD_CAST"em")) {
                   1722:         return(0);
                   1723:     }
1.1       daniel   1724:     return(1);
                   1725: }
                   1726: 
                   1727: /**
                   1728:  * htmlHandleEntity:
                   1729:  * @ctxt:  an HTML parser context
                   1730:  * @entity:  an XML entity pointer.
                   1731:  *
                   1732:  * Default handling of an HTML entity, call the parser with the
                   1733:  * substitution string
                   1734:  */
                   1735: 
                   1736: void
                   1737: htmlHandleEntity(htmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
                   1738:     int len;
                   1739: 
                   1740:     if (entity->content == NULL) {
                   1741:         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1742:            ctxt->sax->error(ctxt->userData, "htmlHandleEntity %s: content == NULL\n",
                   1743:                       entity->name);
                   1744:        ctxt->wellFormed = 0;
                   1745:         return;
                   1746:     }
                   1747:     len = xmlStrlen(entity->content);
                   1748: 
                   1749:     /*
                   1750:      * Just handle the content as a set of chars.
                   1751:      */
1.59      veillard 1752:     htmlCheckParagraph(ctxt);
1.1       daniel   1753:     if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   1754:        ctxt->sax->characters(ctxt->userData, entity->content, len);
                   1755: 
                   1756: }
                   1757: 
                   1758: /**
1.59      veillard 1759:  * htmlNewDocNoDtD:
1.1       daniel   1760:  * @URI:  URI for the dtd, or NULL
                   1761:  * @ExternalID:  the external ID of the DTD, or NULL
                   1762:  *
1.59      veillard 1763:  * Returns a new document, do not intialize the DTD if not provided
1.1       daniel   1764:  */
                   1765: htmlDocPtr
1.59      veillard 1766: htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
1.1       daniel   1767:     xmlDocPtr cur;
                   1768: 
                   1769:     /*
                   1770:      * Allocate a new document and fill the fields.
                   1771:      */
1.11      daniel   1772:     cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1.1       daniel   1773:     if (cur == NULL) {
1.81      veillard 1774:         xmlGenericError(xmlGenericErrorContext,
                   1775:                "xmlNewDoc : malloc failed\n");
1.1       daniel   1776:        return(NULL);
                   1777:     }
1.10      daniel   1778:     memset(cur, 0, sizeof(xmlDoc));
1.1       daniel   1779: 
1.20      daniel   1780:     cur->type = XML_HTML_DOCUMENT_NODE;
1.1       daniel   1781:     cur->version = NULL;
                   1782:     cur->intSubset = NULL;
1.59      veillard 1783:     if ((ExternalID != NULL) ||
                   1784:        (URI != NULL))
1.28      daniel   1785:        xmlCreateIntSubset(cur, BAD_CAST "HTML", ExternalID, URI);
1.41      daniel   1786:     cur->doc = cur;
1.1       daniel   1787:     cur->name = NULL;
1.37      daniel   1788:     cur->children = NULL; 
1.1       daniel   1789:     cur->extSubset = NULL;
                   1790:     cur->oldNs = NULL;
                   1791:     cur->encoding = NULL;
                   1792:     cur->standalone = 1;
                   1793:     cur->compression = 0;
1.12      daniel   1794:     cur->ids = NULL;
                   1795:     cur->refs = NULL;
1.1       daniel   1796: #ifndef XML_WITHOUT_CORBA
                   1797:     cur->_private = NULL;
                   1798: #endif
                   1799:     return(cur);
                   1800: }
                   1801: 
1.59      veillard 1802: /**
                   1803:  * htmlNewDoc:
                   1804:  * @URI:  URI for the dtd, or NULL
                   1805:  * @ExternalID:  the external ID of the DTD, or NULL
                   1806:  *
                   1807:  * Returns a new document
                   1808:  */
                   1809: htmlDocPtr
                   1810: htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
                   1811:     if ((URI == NULL) && (ExternalID == NULL))
                   1812:        return(htmlNewDocNoDtD(
                   1813:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   1814:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"));
                   1815: 
                   1816:     return(htmlNewDocNoDtD(URI, ExternalID));
                   1817: }
                   1818: 
1.1       daniel   1819: 
                   1820: /************************************************************************
                   1821:  *                                                                     *
                   1822:  *                     The parser itself                               *
                   1823:  *     Relates to http://www.w3.org/TR/html40                          *
                   1824:  *                                                                     *
                   1825:  ************************************************************************/
                   1826: 
                   1827: /************************************************************************
                   1828:  *                                                                     *
                   1829:  *                     The parser itself                               *
                   1830:  *                                                                     *
                   1831:  ************************************************************************/
                   1832: 
                   1833: /**
                   1834:  * htmlParseHTMLName:
                   1835:  * @ctxt:  an HTML parser context
                   1836:  *
1.26      daniel   1837:  * parse an HTML tag or attribute name, note that we convert it to lowercase
1.1       daniel   1838:  * since HTML names are not case-sensitive.
                   1839:  *
                   1840:  * Returns the Tag Name parsed or NULL
                   1841:  */
                   1842: 
1.14      daniel   1843: xmlChar *
1.1       daniel   1844: htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
1.14      daniel   1845:     xmlChar *ret = NULL;
1.1       daniel   1846:     int i = 0;
1.31      daniel   1847:     xmlChar loc[HTML_PARSER_BUFFER_SIZE];
1.1       daniel   1848: 
                   1849:     if (!IS_LETTER(CUR) && (CUR != '_') &&
                   1850:         (CUR != ':')) return(NULL);
                   1851: 
1.31      daniel   1852:     while ((i < HTML_PARSER_BUFFER_SIZE) &&
1.45      daniel   1853:            ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1.76      veillard 1854:           (CUR == ':') || (CUR == '-') || (CUR == '_'))) {
1.26      daniel   1855:        if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
1.1       daniel   1856:         else loc[i] = CUR;
                   1857:        i++;
                   1858:        
                   1859:        NEXT;
                   1860:     }
                   1861:     
                   1862:     ret = xmlStrndup(loc, i);
                   1863: 
                   1864:     return(ret);
                   1865: }
                   1866: 
                   1867: /**
                   1868:  * htmlParseName:
                   1869:  * @ctxt:  an HTML parser context
                   1870:  *
                   1871:  * parse an HTML name, this routine is case sensistive.
                   1872:  *
                   1873:  * Returns the Name parsed or NULL
                   1874:  */
                   1875: 
1.14      daniel   1876: xmlChar *
1.1       daniel   1877: htmlParseName(htmlParserCtxtPtr ctxt) {
1.14      daniel   1878:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   1879:     int len = 0;
1.1       daniel   1880: 
1.5       daniel   1881:     GROW;
                   1882:     if (!IS_LETTER(CUR) && (CUR != '_')) {
                   1883:        return(NULL);
                   1884:     }
1.1       daniel   1885: 
                   1886:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1887:            (CUR == '.') || (CUR == '-') ||
                   1888:           (CUR == '_') || (CUR == ':') || 
                   1889:           (IS_COMBINING(CUR)) ||
1.5       daniel   1890:           (IS_EXTENDER(CUR))) {
                   1891:        buf[len++] = CUR;
1.1       daniel   1892:        NEXT;
1.5       daniel   1893:        if (len >= HTML_MAX_NAMELEN) {
1.81      veillard 1894:            xmlGenericError(xmlGenericErrorContext, 
1.5       daniel   1895:               "htmlParseName: reached HTML_MAX_NAMELEN limit\n");
                   1896:            while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1897:                   (CUR == '.') || (CUR == '-') ||
                   1898:                   (CUR == '_') || (CUR == ':') || 
                   1899:                   (IS_COMBINING(CUR)) ||
                   1900:                   (IS_EXTENDER(CUR)))
                   1901:                 NEXT;
                   1902:            break;
                   1903:        }
                   1904:     }
                   1905:     return(xmlStrndup(buf, len));
1.1       daniel   1906: }
                   1907: 
                   1908: /**
                   1909:  * htmlParseHTMLAttribute:
                   1910:  * @ctxt:  an HTML parser context
1.19      daniel   1911:  * @stop:  a char stop value
1.1       daniel   1912:  * 
1.19      daniel   1913:  * parse an HTML attribute value till the stop (quote), if
                   1914:  * stop is 0 then it stops at the first space
1.1       daniel   1915:  *
1.19      daniel   1916:  * Returns the attribute parsed or NULL
1.1       daniel   1917:  */
                   1918: 
1.14      daniel   1919: xmlChar *
1.19      daniel   1920: htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
1.32      daniel   1921:     xmlChar *buffer = NULL;
                   1922:     int buffer_size = 0;
                   1923:     xmlChar *out = NULL;
                   1924:     xmlChar *name = NULL;
                   1925: 
                   1926:     xmlChar *cur = NULL;
                   1927:     htmlEntityDescPtr ent;
                   1928: 
                   1929:     /*
                   1930:      * allocate a translation buffer.
                   1931:      */
1.77      veillard 1932:     buffer_size = HTML_PARSER_BUFFER_SIZE;
1.32      daniel   1933:     buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
                   1934:     if (buffer == NULL) {
                   1935:        perror("htmlParseHTMLAttribute: malloc failed");
                   1936:        return(NULL);
                   1937:     }
                   1938:     out = buffer;
                   1939: 
                   1940:     /*
                   1941:      * Ok loop until we reach one of the ending chars
                   1942:      */
                   1943:     while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
                   1944:        if ((stop == 0) && (IS_BLANK(CUR))) break;
                   1945:         if (CUR == '&') {
                   1946:            if (NXT(1) == '#') {
1.52      veillard 1947:                unsigned int c;
                   1948:                int bits;
                   1949: 
                   1950:                c = htmlParseCharRef(ctxt);
                   1951:                if      (c <    0x80)
                   1952:                        { *out++  = c;                bits= -6; }
                   1953:                else if (c <   0x800)
                   1954:                        { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   1955:                else if (c < 0x10000)
                   1956:                        { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   1957:                else                 
                   1958:                        { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   1959:         
                   1960:                for ( ; bits >= 0; bits-= 6) {
                   1961:                    *out++  = ((c >> bits) & 0x3F) | 0x80;
                   1962:                }
1.32      daniel   1963:            } else {
                   1964:                ent = htmlParseEntityRef(ctxt, &name);
                   1965:                if (name == NULL) {
                   1966:                    *out++ = '&';
                   1967:                    if (out - buffer > buffer_size - 100) {
                   1968:                        int index = out - buffer;
                   1969: 
                   1970:                        growBuffer(buffer);
                   1971:                        out = &buffer[index];
                   1972:                    }
1.52      veillard 1973:                } else if (ent == NULL) {
1.32      daniel   1974:                    *out++ = '&';
                   1975:                    cur = name;
                   1976:                    while (*cur != 0) {
                   1977:                        if (out - buffer > buffer_size - 100) {
                   1978:                            int index = out - buffer;
                   1979: 
                   1980:                            growBuffer(buffer);
                   1981:                            out = &buffer[index];
                   1982:                        }
                   1983:                        *out++ = *cur++;
                   1984:                    }
                   1985:                    xmlFree(name);
                   1986:                } else {
1.52      veillard 1987:                    unsigned int c;
                   1988:                    int bits;
                   1989: 
1.32      daniel   1990:                    if (out - buffer > buffer_size - 100) {
                   1991:                        int index = out - buffer;
                   1992: 
                   1993:                        growBuffer(buffer);
                   1994:                        out = &buffer[index];
                   1995:                    }
1.52      veillard 1996:                    c = (xmlChar)ent->value;
                   1997:                    if      (c <    0x80)
                   1998:                        { *out++  = c;                bits= -6; }
                   1999:                    else if (c <   0x800)
                   2000:                        { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   2001:                    else if (c < 0x10000)
                   2002:                        { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   2003:                    else                 
                   2004:                        { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   2005:             
                   2006:                    for ( ; bits >= 0; bits-= 6) {
                   2007:                        *out++  = ((c >> bits) & 0x3F) | 0x80;
                   2008:                    }
1.32      daniel   2009:                    xmlFree(name);
                   2010:                }
                   2011:            }
                   2012:        } else {
1.52      veillard 2013:            unsigned int c;
1.68      veillard 2014:            int bits, l;
1.52      veillard 2015: 
1.32      daniel   2016:            if (out - buffer > buffer_size - 100) {
1.52      veillard 2017:                int index = out - buffer;
                   2018: 
                   2019:                growBuffer(buffer);
                   2020:                out = &buffer[index];
                   2021:            }
1.68      veillard 2022:            c = CUR_CHAR(l);
1.52      veillard 2023:            if      (c <    0x80)
                   2024:                    { *out++  = c;                bits= -6; }
                   2025:            else if (c <   0x800)
                   2026:                    { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   2027:            else if (c < 0x10000)
                   2028:                    { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   2029:            else                 
                   2030:                    { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   2031:      
                   2032:            for ( ; bits >= 0; bits-= 6) {
                   2033:                *out++  = ((c >> bits) & 0x3F) | 0x80;
1.32      daniel   2034:            }
                   2035:            NEXT;
                   2036:        }
                   2037:     }
                   2038:     *out++ = 0;
                   2039:     return(buffer);
1.1       daniel   2040: }
                   2041: 
                   2042: /**
                   2043:  * htmlParseNmtoken:
                   2044:  * @ctxt:  an HTML parser context
                   2045:  * 
                   2046:  * parse an HTML Nmtoken.
                   2047:  *
                   2048:  * Returns the Nmtoken parsed or NULL
                   2049:  */
                   2050: 
1.14      daniel   2051: xmlChar *
1.1       daniel   2052: htmlParseNmtoken(htmlParserCtxtPtr ctxt) {
1.14      daniel   2053:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   2054:     int len = 0;
1.1       daniel   2055: 
1.5       daniel   2056:     GROW;
1.1       daniel   2057:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   2058:            (CUR == '.') || (CUR == '-') ||
                   2059:           (CUR == '_') || (CUR == ':') || 
                   2060:           (IS_COMBINING(CUR)) ||
1.5       daniel   2061:           (IS_EXTENDER(CUR))) {
                   2062:        buf[len++] = CUR;
1.1       daniel   2063:        NEXT;
1.5       daniel   2064:        if (len >= HTML_MAX_NAMELEN) {
1.81      veillard 2065:            xmlGenericError(xmlGenericErrorContext, 
1.5       daniel   2066:               "htmlParseNmtoken: reached HTML_MAX_NAMELEN limit\n");
                   2067:            while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   2068:                   (CUR == '.') || (CUR == '-') ||
                   2069:                   (CUR == '_') || (CUR == ':') || 
                   2070:                   (IS_COMBINING(CUR)) ||
                   2071:                   (IS_EXTENDER(CUR)))
                   2072:                 NEXT;
                   2073:            break;
                   2074:        }
                   2075:     }
                   2076:     return(xmlStrndup(buf, len));
1.1       daniel   2077: }
                   2078: 
                   2079: /**
                   2080:  * htmlParseEntityRef:
                   2081:  * @ctxt:  an HTML parser context
                   2082:  * @str:  location to store the entity name
                   2083:  *
                   2084:  * parse an HTML ENTITY references
                   2085:  *
                   2086:  * [68] EntityRef ::= '&' Name ';'
                   2087:  *
                   2088:  * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
                   2089:  *         if non-NULL *str will have to be freed by the caller.
                   2090:  */
                   2091: htmlEntityDescPtr
1.14      daniel   2092: htmlParseEntityRef(htmlParserCtxtPtr ctxt, xmlChar **str) {
                   2093:     xmlChar *name;
1.1       daniel   2094:     htmlEntityDescPtr ent = NULL;
                   2095:     *str = NULL;
                   2096: 
                   2097:     if (CUR == '&') {
                   2098:         NEXT;
                   2099:         name = htmlParseName(ctxt);
                   2100:        if (name == NULL) {
                   2101:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2102:                ctxt->sax->error(ctxt->userData, "htmlParseEntityRef: no name\n");
                   2103:            ctxt->wellFormed = 0;
                   2104:        } else {
1.5       daniel   2105:            GROW;
1.1       daniel   2106:            if (CUR == ';') {
                   2107:                *str = name;
                   2108: 
                   2109:                /*
                   2110:                 * Lookup the entity in the table.
                   2111:                 */
                   2112:                ent = htmlEntityLookup(name);
1.32      daniel   2113:                if (ent != NULL) /* OK that's ugly !!! */
                   2114:                    NEXT;
1.1       daniel   2115:            } else {
                   2116:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2117:                    ctxt->sax->error(ctxt->userData,
                   2118:                                     "htmlParseEntityRef: expecting ';'\n");
1.32      daniel   2119:                *str = name;
1.1       daniel   2120:            }
                   2121:        }
                   2122:     }
                   2123:     return(ent);
                   2124: }
                   2125: 
                   2126: /**
                   2127:  * htmlParseAttValue:
                   2128:  * @ctxt:  an HTML parser context
                   2129:  *
                   2130:  * parse a value for an attribute
                   2131:  * Note: the parser won't do substitution of entities here, this
                   2132:  * will be handled later in xmlStringGetNodeList, unless it was
                   2133:  * asked for ctxt->replaceEntities != 0 
                   2134:  *
                   2135:  * Returns the AttValue parsed or NULL.
                   2136:  */
                   2137: 
1.14      daniel   2138: xmlChar *
1.1       daniel   2139: htmlParseAttValue(htmlParserCtxtPtr ctxt) {
1.14      daniel   2140:     xmlChar *ret = NULL;
1.1       daniel   2141: 
                   2142:     if (CUR == '"') {
                   2143:         NEXT;
1.19      daniel   2144:        ret = htmlParseHTMLAttribute(ctxt, '"');
1.1       daniel   2145:         if (CUR != '"') {
                   2146:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2147:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   2148:            ctxt->wellFormed = 0;
                   2149:        } else
                   2150:            NEXT;
                   2151:     } else if (CUR == '\'') {
                   2152:         NEXT;
1.19      daniel   2153:        ret = htmlParseHTMLAttribute(ctxt, '\'');
1.1       daniel   2154:         if (CUR != '\'') {
                   2155:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2156:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   2157:            ctxt->wellFormed = 0;
                   2158:        } else
                   2159:            NEXT;
                   2160:     } else {
                   2161:         /*
                   2162:         * That's an HTMLism, the attribute value may not be quoted
                   2163:         */
1.19      daniel   2164:        ret = htmlParseHTMLAttribute(ctxt, 0);
1.1       daniel   2165:        if (ret == NULL) {
                   2166:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2167:                ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
                   2168:            ctxt->wellFormed = 0;
                   2169:        }
                   2170:     }
                   2171:     return(ret);
                   2172: }
                   2173: 
                   2174: /**
                   2175:  * htmlParseSystemLiteral:
                   2176:  * @ctxt:  an HTML parser context
                   2177:  * 
                   2178:  * parse an HTML Literal
                   2179:  *
                   2180:  * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
                   2181:  *
                   2182:  * Returns the SystemLiteral parsed or NULL
                   2183:  */
                   2184: 
1.14      daniel   2185: xmlChar *
1.1       daniel   2186: htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
1.14      daniel   2187:     const xmlChar *q;
                   2188:     xmlChar *ret = NULL;
1.1       daniel   2189: 
                   2190:     if (CUR == '"') {
                   2191:         NEXT;
                   2192:        q = CUR_PTR;
                   2193:        while ((IS_CHAR(CUR)) && (CUR != '"'))
                   2194:            NEXT;
                   2195:        if (!IS_CHAR(CUR)) {
                   2196:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2197:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   2198:            ctxt->wellFormed = 0;
                   2199:        } else {
                   2200:            ret = xmlStrndup(q, CUR_PTR - q);
                   2201:            NEXT;
                   2202:         }
                   2203:     } else if (CUR == '\'') {
                   2204:         NEXT;
                   2205:        q = CUR_PTR;
                   2206:        while ((IS_CHAR(CUR)) && (CUR != '\''))
                   2207:            NEXT;
                   2208:        if (!IS_CHAR(CUR)) {
                   2209:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2210:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   2211:            ctxt->wellFormed = 0;
                   2212:        } else {
                   2213:            ret = xmlStrndup(q, CUR_PTR - q);
                   2214:            NEXT;
                   2215:         }
                   2216:     } else {
                   2217:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.38      daniel   2218:            ctxt->sax->error(ctxt->userData,
                   2219:                             "SystemLiteral \" or ' expected\n");
1.1       daniel   2220:        ctxt->wellFormed = 0;
                   2221:     }
                   2222:     
                   2223:     return(ret);
                   2224: }
                   2225: 
                   2226: /**
                   2227:  * htmlParsePubidLiteral:
                   2228:  * @ctxt:  an HTML parser context
                   2229:  *
                   2230:  * parse an HTML public literal
                   2231:  *
                   2232:  * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
                   2233:  *
                   2234:  * Returns the PubidLiteral parsed or NULL.
                   2235:  */
                   2236: 
1.14      daniel   2237: xmlChar *
1.1       daniel   2238: htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
1.14      daniel   2239:     const xmlChar *q;
                   2240:     xmlChar *ret = NULL;
1.1       daniel   2241:     /*
                   2242:      * Name ::= (Letter | '_') (NameChar)*
                   2243:      */
                   2244:     if (CUR == '"') {
                   2245:         NEXT;
                   2246:        q = CUR_PTR;
                   2247:        while (IS_PUBIDCHAR(CUR)) NEXT;
                   2248:        if (CUR != '"') {
                   2249:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2250:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   2251:            ctxt->wellFormed = 0;
                   2252:        } else {
                   2253:            ret = xmlStrndup(q, CUR_PTR - q);
                   2254:            NEXT;
                   2255:        }
                   2256:     } else if (CUR == '\'') {
                   2257:         NEXT;
                   2258:        q = CUR_PTR;
                   2259:        while ((IS_LETTER(CUR)) && (CUR != '\''))
                   2260:            NEXT;
                   2261:        if (!IS_LETTER(CUR)) {
                   2262:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2263:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   2264:            ctxt->wellFormed = 0;
                   2265:        } else {
                   2266:            ret = xmlStrndup(q, CUR_PTR - q);
                   2267:            NEXT;
                   2268:        }
                   2269:     } else {
                   2270:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2271:            ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
                   2272:        ctxt->wellFormed = 0;
                   2273:     }
                   2274:     
                   2275:     return(ret);
                   2276: }
                   2277: 
                   2278: /**
1.77      veillard 2279:  * htmlParseScript:
                   2280:  * @ctxt:  an HTML parser context
                   2281:  *
                   2282:  * parse the content of an HTML SCRIPT or STYLE element
                   2283:  * http://www.w3.org/TR/html4/sgml/dtd.html#Script
                   2284:  * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
                   2285:  * http://www.w3.org/TR/html4/types.html#type-script
                   2286:  * http://www.w3.org/TR/html4/types.html#h-6.15
                   2287:  * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
                   2288:  *
                   2289:  * Script data ( %Script; in the DTD) can be the content of the SCRIPT
                   2290:  * element and the value of intrinsic event attributes. User agents must
                   2291:  * not evaluate script data as HTML markup but instead must pass it on as
                   2292:  * data to a script engine.
                   2293:  * NOTES:
                   2294:  * - The content is passed like CDATA
                   2295:  * - the attributes for style and scripting "onXXX" are also described
                   2296:  *   as CDATA but SGML allows entities references in attributes so their
                   2297:  *   processing is identical as other attributes
                   2298:  */
                   2299: void
                   2300: htmlParseScript(htmlParserCtxtPtr ctxt) {
                   2301:     xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 1];
                   2302:     int nbchar = 0;
                   2303:     xmlChar cur;
                   2304: 
                   2305:     SHRINK;
                   2306:     cur = CUR;
                   2307:     while (IS_CHAR(cur)) {
                   2308:        if ((cur == '<') && (NXT(1) == '/')) {
                   2309:            /*
                   2310:             * One should break here, the specification is clear:
                   2311:             * Authors should therefore escape "</" within the content.
                   2312:             * Escape mechanisms are specific to each scripting or
                   2313:             * style sheet language.
                   2314:             */
                   2315:            if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
                   2316:                ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
                   2317:                break; /* while */
                   2318:        }
                   2319:        buf[nbchar++] = cur;
                   2320:        if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
                   2321:            if (ctxt->sax->cdataBlock!= NULL) {
                   2322:                /*
                   2323:                 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
                   2324:                 */
                   2325:                ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
                   2326:            }
                   2327:            nbchar = 0;
                   2328:        }
                   2329:        NEXT;
                   2330:        cur = CUR;
                   2331:     }
1.83      veillard 2332:     if (!(IS_CHAR(cur))) {
                   2333:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2334:            ctxt->sax->error(ctxt->userData,
                   2335:                "Invalid char in CDATA 0x%X\n", cur);
                   2336:        ctxt->wellFormed = 0;
                   2337:        NEXT;
                   2338:     }
                   2339: 
1.77      veillard 2340:     if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
                   2341:        if (ctxt->sax->cdataBlock!= NULL) {
                   2342:            /*
                   2343:             * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
                   2344:             */
                   2345:            ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
                   2346:        }
                   2347:     }
                   2348: }
                   2349: 
                   2350: 
                   2351: /**
1.1       daniel   2352:  * htmlParseCharData:
                   2353:  * @ctxt:  an HTML parser context
                   2354:  * @cdata:  int indicating whether we are within a CDATA section
                   2355:  *
                   2356:  * parse a CharData section.
                   2357:  * if we are within a CDATA section ']]>' marks an end of section.
                   2358:  *
                   2359:  * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
                   2360:  */
                   2361: 
                   2362: void
                   2363: htmlParseCharData(htmlParserCtxtPtr ctxt, int cdata) {
1.53      veillard 2364:     xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
                   2365:     int nbchar = 0;
                   2366:     int cur, l;
                   2367: 
                   2368:     SHRINK;
                   2369:     cur = CUR_CHAR(l);
                   2370:     while (((cur != '<') || (ctxt->token == '<')) &&
                   2371:            ((cur != '&') || (ctxt->token == '&')) && 
                   2372:           (IS_CHAR(cur))) {
                   2373:        COPY_BUF(l,buf,nbchar,cur);
                   2374:        if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
                   2375:            /*
                   2376:             * Ok the segment is to be consumed as chars.
                   2377:             */
                   2378:            if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
                   2379:                if (areBlanks(ctxt, buf, nbchar)) {
                   2380:                    if (ctxt->sax->ignorableWhitespace != NULL)
                   2381:                        ctxt->sax->ignorableWhitespace(ctxt->userData,
                   2382:                                                       buf, nbchar);
                   2383:                } else {
1.59      veillard 2384:                    htmlCheckParagraph(ctxt);
1.53      veillard 2385:                    if (ctxt->sax->characters != NULL)
                   2386:                        ctxt->sax->characters(ctxt->userData, buf, nbchar);
                   2387:                }
1.1       daniel   2388:            }
1.53      veillard 2389:            nbchar = 0;
1.1       daniel   2390:        }
1.53      veillard 2391:        NEXTL(l);
                   2392:        cur = CUR_CHAR(l);
                   2393:     }
                   2394:     if (nbchar != 0) {
                   2395:        /*
                   2396:         * Ok the segment is to be consumed as chars.
                   2397:         */
                   2398:        if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
                   2399:            if (areBlanks(ctxt, buf, nbchar)) {
                   2400:                if (ctxt->sax->ignorableWhitespace != NULL)
                   2401:                    ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
                   2402:            } else {
1.59      veillard 2403:                htmlCheckParagraph(ctxt);
1.53      veillard 2404:                if (ctxt->sax->characters != NULL)
                   2405:                    ctxt->sax->characters(ctxt->userData, buf, nbchar);
1.25      daniel   2406:            }
                   2407:        }
1.1       daniel   2408:     }
                   2409: }
                   2410: 
                   2411: /**
                   2412:  * htmlParseExternalID:
                   2413:  * @ctxt:  an HTML parser context
1.14      daniel   2414:  * @publicID:  a xmlChar** receiving PubidLiteral
1.1       daniel   2415:  * @strict: indicate whether we should restrict parsing to only
                   2416:  *          production [75], see NOTE below
                   2417:  *
                   2418:  * Parse an External ID or a Public ID
                   2419:  *
                   2420:  * NOTE: Productions [75] and [83] interract badly since [75] can generate
                   2421:  *       'PUBLIC' S PubidLiteral S SystemLiteral
                   2422:  *
                   2423:  * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
                   2424:  *                   | 'PUBLIC' S PubidLiteral S SystemLiteral
                   2425:  *
                   2426:  * [83] PublicID ::= 'PUBLIC' S PubidLiteral
                   2427:  *
                   2428:  * Returns the function returns SystemLiteral and in the second
                   2429:  *                case publicID receives PubidLiteral, is strict is off
                   2430:  *                it is possible to return NULL and have publicID set.
                   2431:  */
                   2432: 
1.14      daniel   2433: xmlChar *
                   2434: htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
                   2435:     xmlChar *URI = NULL;
1.1       daniel   2436: 
                   2437:     if ((UPPER == 'S') && (UPP(1) == 'Y') &&
                   2438:          (UPP(2) == 'S') && (UPP(3) == 'T') &&
                   2439:         (UPP(4) == 'E') && (UPP(5) == 'M')) {
                   2440:         SKIP(6);
                   2441:        if (!IS_BLANK(CUR)) {
                   2442:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2443:                ctxt->sax->error(ctxt->userData,
                   2444:                    "Space required after 'SYSTEM'\n");
                   2445:            ctxt->wellFormed = 0;
                   2446:        }
                   2447:         SKIP_BLANKS;
                   2448:        URI = htmlParseSystemLiteral(ctxt);
                   2449:        if (URI == NULL) {
                   2450:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2451:                ctxt->sax->error(ctxt->userData,
                   2452:                  "htmlParseExternalID: SYSTEM, no URI\n");
                   2453:            ctxt->wellFormed = 0;
                   2454:         }
                   2455:     } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
                   2456:               (UPP(2) == 'B') && (UPP(3) == 'L') &&
                   2457:               (UPP(4) == 'I') && (UPP(5) == 'C')) {
                   2458:         SKIP(6);
                   2459:        if (!IS_BLANK(CUR)) {
                   2460:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2461:                ctxt->sax->error(ctxt->userData,
                   2462:                    "Space required after 'PUBLIC'\n");
                   2463:            ctxt->wellFormed = 0;
                   2464:        }
                   2465:         SKIP_BLANKS;
                   2466:        *publicID = htmlParsePubidLiteral(ctxt);
                   2467:        if (*publicID == NULL) {
                   2468:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2469:                ctxt->sax->error(ctxt->userData, 
                   2470:                  "htmlParseExternalID: PUBLIC, no Public Identifier\n");
                   2471:            ctxt->wellFormed = 0;
                   2472:        }
1.5       daniel   2473:         SKIP_BLANKS;
                   2474:         if ((CUR == '"') || (CUR == '\'')) {
                   2475:            URI = htmlParseSystemLiteral(ctxt);
1.1       daniel   2476:        }
                   2477:     }
                   2478:     return(URI);
                   2479: }
                   2480: 
                   2481: /**
                   2482:  * htmlParseComment:
                   2483:  * @ctxt:  an HTML parser context
                   2484:  *
                   2485:  * Parse an XML (SGML) comment <!-- .... -->
                   2486:  *
                   2487:  * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
                   2488:  */
                   2489: void
1.31      daniel   2490: htmlParseComment(htmlParserCtxtPtr ctxt) {
1.25      daniel   2491:     xmlChar *buf = NULL;
1.56      veillard 2492:     int len;
1.31      daniel   2493:     int size = HTML_PARSER_BUFFER_SIZE;
1.56      veillard 2494:     int q, ql;
                   2495:     int r, rl;
                   2496:     int cur, l;
                   2497:     xmlParserInputState state;
1.1       daniel   2498: 
                   2499:     /*
                   2500:      * Check that there is a comment right here.
                   2501:      */
1.56      veillard 2502:     if ((RAW != '<') || (NXT(1) != '!') ||
1.1       daniel   2503:         (NXT(2) != '-') || (NXT(3) != '-')) return;
                   2504: 
1.56      veillard 2505:     state = ctxt->instate;
                   2506:     ctxt->instate = XML_PARSER_COMMENT;
                   2507:     SHRINK;
                   2508:     SKIP(4);
1.25      daniel   2509:     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
                   2510:     if (buf == NULL) {
1.81      veillard 2511:        xmlGenericError(xmlGenericErrorContext,
                   2512:                "malloc of %d byte failed\n", size);
1.56      veillard 2513:        ctxt->instate = state;
1.25      daniel   2514:        return;
                   2515:     }
1.56      veillard 2516:     q = CUR_CHAR(ql);
                   2517:     NEXTL(ql);
                   2518:     r = CUR_CHAR(rl);
                   2519:     NEXTL(rl);
                   2520:     cur = CUR_CHAR(l);
                   2521:     len = 0;
                   2522:     while (IS_CHAR(cur) &&
                   2523:            ((cur != '>') ||
                   2524:            (r != '-') || (q != '-'))) {
                   2525:        if (len + 5 >= size) {
1.25      daniel   2526:            size *= 2;
1.50      veillard 2527:            buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
1.25      daniel   2528:            if (buf == NULL) {
1.81      veillard 2529:                xmlGenericError(xmlGenericErrorContext,
                   2530:                        "realloc of %d byte failed\n", size);
1.56      veillard 2531:                ctxt->instate = state;
1.25      daniel   2532:                return;
                   2533:            }
                   2534:        }
1.56      veillard 2535:        COPY_BUF(ql,buf,len,q);
1.25      daniel   2536:        q = r;
1.56      veillard 2537:        ql = rl;
                   2538:        r = cur;
                   2539:        rl = l;
                   2540:        NEXTL(l);
                   2541:        cur = CUR_CHAR(l);
                   2542:        if (cur == 0) {
                   2543:            SHRINK;
                   2544:            GROW;
                   2545:            cur = CUR_CHAR(l);
                   2546:        }
1.1       daniel   2547:     }
1.56      veillard 2548:     buf[len] = 0;
                   2549:     if (!IS_CHAR(cur)) {
1.67      veillard 2550:        ctxt->errNo = XML_ERR_COMMENT_NOT_FINISHED;
1.1       daniel   2551:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.56      veillard 2552:            ctxt->sax->error(ctxt->userData,
                   2553:                             "Comment not terminated \n<!--%.50s\n", buf);
1.1       daniel   2554:        ctxt->wellFormed = 0;
1.56      veillard 2555:        xmlFree(buf);
1.1       daniel   2556:     } else {
                   2557:         NEXT;
1.56      veillard 2558:        if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
                   2559:            (!ctxt->disableSAX))
1.31      daniel   2560:            ctxt->sax->comment(ctxt->userData, buf);
1.56      veillard 2561:        xmlFree(buf);
1.1       daniel   2562:     }
1.56      veillard 2563:     ctxt->instate = state;
1.1       daniel   2564: }
                   2565: 
                   2566: /**
                   2567:  * htmlParseCharRef:
                   2568:  * @ctxt:  an HTML parser context
                   2569:  *
                   2570:  * parse Reference declarations
                   2571:  *
                   2572:  * [66] CharRef ::= '&#' [0-9]+ ';' |
                   2573:  *                  '&#x' [0-9a-fA-F]+ ';'
                   2574:  *
                   2575:  * Returns the value parsed (as an int)
                   2576:  */
                   2577: int
                   2578: htmlParseCharRef(htmlParserCtxtPtr ctxt) {
                   2579:     int val = 0;
                   2580: 
                   2581:     if ((CUR == '&') && (NXT(1) == '#') &&
                   2582:         (NXT(2) == 'x')) {
                   2583:        SKIP(3);
                   2584:        while (CUR != ';') {
                   2585:            if ((CUR >= '0') && (CUR <= '9')) 
                   2586:                val = val * 16 + (CUR - '0');
                   2587:            else if ((CUR >= 'a') && (CUR <= 'f'))
                   2588:                val = val * 16 + (CUR - 'a') + 10;
                   2589:            else if ((CUR >= 'A') && (CUR <= 'F'))
                   2590:                val = val * 16 + (CUR - 'A') + 10;
                   2591:            else {
                   2592:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2593:                    ctxt->sax->error(ctxt->userData, 
                   2594:                         "htmlParseCharRef: invalid hexadecimal value\n");
                   2595:                ctxt->wellFormed = 0;
1.84    ! veillard 2596:                return(0);
1.1       daniel   2597:            }
                   2598:            NEXT;
                   2599:        }
                   2600:        if (CUR == ';')
                   2601:            NEXT;
                   2602:     } else if  ((CUR == '&') && (NXT(1) == '#')) {
                   2603:        SKIP(2);
                   2604:        while (CUR != ';') {
                   2605:            if ((CUR >= '0') && (CUR <= '9')) 
                   2606:                val = val * 10 + (CUR - '0');
                   2607:            else {
                   2608:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2609:                    ctxt->sax->error(ctxt->userData, 
                   2610:                         "htmlParseCharRef: invalid decimal value\n");
                   2611:                ctxt->wellFormed = 0;
1.84    ! veillard 2612:                return(0);
1.1       daniel   2613:            }
                   2614:            NEXT;
                   2615:        }
                   2616:        if (CUR == ';')
                   2617:            NEXT;
                   2618:     } else {
                   2619:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2620:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid value\n");
                   2621:        ctxt->wellFormed = 0;
                   2622:     }
                   2623:     /*
                   2624:      * Check the value IS_CHAR ...
                   2625:      */
                   2626:     if (IS_CHAR(val)) {
                   2627:         return(val);
                   2628:     } else {
                   2629:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.14      daniel   2630:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid xmlChar value %d\n",
1.1       daniel   2631:                             val);
                   2632:        ctxt->wellFormed = 0;
                   2633:     }
                   2634:     return(0);
                   2635: }
                   2636: 
                   2637: 
                   2638: /**
                   2639:  * htmlParseDocTypeDecl :
                   2640:  * @ctxt:  an HTML parser context
                   2641:  *
                   2642:  * parse a DOCTYPE declaration
                   2643:  *
                   2644:  * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? 
                   2645:  *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
                   2646:  */
                   2647: 
                   2648: void
                   2649: htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
1.14      daniel   2650:     xmlChar *name;
                   2651:     xmlChar *ExternalID = NULL;
                   2652:     xmlChar *URI = NULL;
1.1       daniel   2653: 
                   2654:     /*
                   2655:      * We know that '<!DOCTYPE' has been detected.
                   2656:      */
                   2657:     SKIP(9);
                   2658: 
                   2659:     SKIP_BLANKS;
                   2660: 
                   2661:     /*
                   2662:      * Parse the DOCTYPE name.
                   2663:      */
                   2664:     name = htmlParseName(ctxt);
                   2665:     if (name == NULL) {
                   2666:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2667:            ctxt->sax->error(ctxt->userData, "htmlParseDocTypeDecl : no DOCTYPE name !\n");
                   2668:        ctxt->wellFormed = 0;
                   2669:     }
                   2670:     /*
                   2671:      * Check that upper(name) == "HTML" !!!!!!!!!!!!!
                   2672:      */
                   2673: 
                   2674:     SKIP_BLANKS;
                   2675: 
                   2676:     /*
                   2677:      * Check for SystemID and ExternalID
                   2678:      */
1.5       daniel   2679:     URI = htmlParseExternalID(ctxt, &ExternalID, 0);
1.1       daniel   2680:     SKIP_BLANKS;
                   2681: 
                   2682:     /*
                   2683:      * We should be at the end of the DOCTYPE declaration.
                   2684:      */
                   2685:     if (CUR != '>') {
                   2686:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2687:            ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
                   2688:        ctxt->wellFormed = 0;
                   2689:         /* We shouldn't try to resynchronize ... */
                   2690:     }
                   2691:     NEXT;
                   2692: 
                   2693:     /*
1.46      daniel   2694:      * Create or update the document accordingly to the DOCTYPE
1.1       daniel   2695:      */
1.46      daniel   2696:     if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
                   2697:        (!ctxt->disableSAX))
                   2698:        ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
1.1       daniel   2699: 
                   2700:     /*
                   2701:      * Cleanup, since we don't use all those identifiers
                   2702:      */
1.11      daniel   2703:     if (URI != NULL) xmlFree(URI);
                   2704:     if (ExternalID != NULL) xmlFree(ExternalID);
                   2705:     if (name != NULL) xmlFree(name);
1.1       daniel   2706: }
                   2707: 
                   2708: /**
                   2709:  * htmlParseAttribute:
                   2710:  * @ctxt:  an HTML parser context
1.14      daniel   2711:  * @value:  a xmlChar ** used to store the value of the attribute
1.1       daniel   2712:  *
                   2713:  * parse an attribute
                   2714:  *
                   2715:  * [41] Attribute ::= Name Eq AttValue
                   2716:  *
                   2717:  * [25] Eq ::= S? '=' S?
                   2718:  *
                   2719:  * With namespace:
                   2720:  *
                   2721:  * [NS 11] Attribute ::= QName Eq AttValue
                   2722:  *
                   2723:  * Also the case QName == xmlns:??? is handled independently as a namespace
                   2724:  * definition.
                   2725:  *
                   2726:  * Returns the attribute name, and the value in *value.
                   2727:  */
                   2728: 
1.14      daniel   2729: xmlChar *
                   2730: htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
1.31      daniel   2731:     xmlChar *name, *val = NULL;
1.1       daniel   2732: 
                   2733:     *value = NULL;
1.74      veillard 2734:     name = htmlParseHTMLName(ctxt);
1.1       daniel   2735:     if (name == NULL) {
                   2736:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2737:            ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
                   2738:        ctxt->wellFormed = 0;
                   2739:         return(NULL);
                   2740:     }
                   2741: 
                   2742:     /*
                   2743:      * read the value
                   2744:      */
                   2745:     SKIP_BLANKS;
                   2746:     if (CUR == '=') {
                   2747:         NEXT;
                   2748:        SKIP_BLANKS;
                   2749:        val = htmlParseAttValue(ctxt);
1.42      daniel   2750:        /******
1.1       daniel   2751:     } else {
1.42      daniel   2752:         * TODO : some attribute must have values, some may not
1.1       daniel   2753:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.31      daniel   2754:            ctxt->sax->warning(ctxt->userData,
1.42      daniel   2755:               "No value for attribute %s\n", name); */
1.1       daniel   2756:     }
                   2757: 
                   2758:     *value = val;
                   2759:     return(name);
                   2760: }
                   2761: 
                   2762: /**
1.47      daniel   2763:  * htmlCheckEncoding:
                   2764:  * @ctxt:  an HTML parser context
                   2765:  * @attvalue: the attribute value
                   2766:  *
                   2767:  * Checks an http-equiv attribute from a Meta tag to detect
                   2768:  * the encoding
                   2769:  * If a new encoding is detected the parser is switched to decode
                   2770:  * it and pass UTF8
                   2771:  */
                   2772: void
                   2773: htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
                   2774:     const xmlChar *encoding;
                   2775: 
                   2776:     if ((ctxt == NULL) || (attvalue == NULL))
                   2777:        return;
                   2778: 
1.69      veillard 2779:     encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
1.47      daniel   2780:     if (encoding != NULL) {
                   2781:        encoding += 8;
                   2782:     } else {
1.69      veillard 2783:        encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
1.47      daniel   2784:        if (encoding != NULL)
                   2785:            encoding += 9;
                   2786:     }
                   2787:     if (encoding != NULL) {
                   2788:        xmlCharEncoding enc;
                   2789:        xmlCharEncodingHandlerPtr handler;
                   2790: 
                   2791:        while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
                   2792: 
                   2793:        if (ctxt->input->encoding != NULL)
                   2794:            xmlFree((xmlChar *) ctxt->input->encoding);
                   2795:        ctxt->input->encoding = xmlStrdup(encoding);
                   2796: 
                   2797:        enc = xmlParseCharEncoding((const char *) encoding);
                   2798:        /*
                   2799:         * registered set of known encodings
                   2800:         */
                   2801:        if (enc != XML_CHAR_ENCODING_ERROR) {
                   2802:            xmlSwitchEncoding(ctxt, enc);
1.53      veillard 2803:            ctxt->charset = XML_CHAR_ENCODING_UTF8;
1.47      daniel   2804:        } else {
                   2805:            /*
                   2806:             * fallback for unknown encodings
                   2807:             */
                   2808:            handler = xmlFindCharEncodingHandler((const char *) encoding);
                   2809:            if (handler != NULL) {
                   2810:                xmlSwitchToEncoding(ctxt, handler);
1.54      veillard 2811:                ctxt->charset = XML_CHAR_ENCODING_UTF8;
1.47      daniel   2812:            } else {
                   2813:                ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
                   2814:            }
                   2815:        }
1.54      veillard 2816: 
                   2817:        if ((ctxt->input->buf != NULL) &&
                   2818:            (ctxt->input->buf->encoder != NULL) &&
                   2819:            (ctxt->input->buf->raw != NULL) &&
                   2820:            (ctxt->input->buf->buffer != NULL)) {
                   2821:            int nbchars;
1.56      veillard 2822:            int processed;
1.54      veillard 2823: 
                   2824:            /*
                   2825:             * convert as much as possible to the parser reading buffer.
                   2826:             */
1.56      veillard 2827:            processed = ctxt->input->cur - ctxt->input->base;
                   2828:            xmlBufferShrink(ctxt->input->buf->buffer, processed);
1.54      veillard 2829:            nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
                   2830:                                       ctxt->input->buf->buffer,
                   2831:                                       ctxt->input->buf->raw);
                   2832:            if (nbchars < 0) {
1.67      veillard 2833:                ctxt->errNo = XML_ERR_INVALID_ENCODING;
1.54      veillard 2834:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2835:                    ctxt->sax->error(ctxt->userData, 
                   2836:                     "htmlCheckEncoding: encoder error\n");
                   2837:            }
1.56      veillard 2838:            ctxt->input->base =
                   2839:            ctxt->input->cur = ctxt->input->buf->buffer->content;
1.54      veillard 2840:        }
1.47      daniel   2841:     }
                   2842: }
                   2843: 
                   2844: /**
                   2845:  * htmlCheckMeta:
                   2846:  * @ctxt:  an HTML parser context
                   2847:  * @atts:  the attributes values
                   2848:  *
                   2849:  * Checks an attributes from a Meta tag
                   2850:  */
                   2851: void
                   2852: htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
                   2853:     int i;
                   2854:     const xmlChar *att, *value;
                   2855:     int http = 0;
                   2856:     const xmlChar *content = NULL;
                   2857: 
                   2858:     if ((ctxt == NULL) || (atts == NULL))
                   2859:        return;
                   2860: 
                   2861:     i = 0;
                   2862:     att = atts[i++];
                   2863:     while (att != NULL) {
                   2864:        value = atts[i++];
1.69      veillard 2865:        if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
                   2866:         && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
1.47      daniel   2867:            http = 1;
1.69      veillard 2868:        else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
1.47      daniel   2869:            content = value;
                   2870:        att = atts[i++];
                   2871:     }
                   2872:     if ((http) && (content != NULL))
                   2873:        htmlCheckEncoding(ctxt, content);
                   2874: 
                   2875: }
                   2876: 
                   2877: /**
1.1       daniel   2878:  * htmlParseStartTag:
                   2879:  * @ctxt:  an HTML parser context
                   2880:  * 
                   2881:  * parse a start of tag either for rule element or
                   2882:  * EmptyElement. In both case we don't parse the tag closing chars.
                   2883:  *
                   2884:  * [40] STag ::= '<' Name (S Attribute)* S? '>'
                   2885:  *
                   2886:  * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
                   2887:  *
                   2888:  * With namespace:
                   2889:  *
                   2890:  * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
                   2891:  *
                   2892:  * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
                   2893:  *
                   2894:  */
                   2895: 
1.18      daniel   2896: void
1.1       daniel   2897: htmlParseStartTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2898:     xmlChar *name;
                   2899:     xmlChar *attname;
                   2900:     xmlChar *attvalue;
                   2901:     const xmlChar **atts = NULL;
1.1       daniel   2902:     int nbatts = 0;
                   2903:     int maxatts = 0;
1.47      daniel   2904:     int meta = 0;
1.1       daniel   2905:     int i;
                   2906: 
1.18      daniel   2907:     if (CUR != '<') return;
1.1       daniel   2908:     NEXT;
                   2909: 
1.19      daniel   2910:     GROW;
1.1       daniel   2911:     name = htmlParseHTMLName(ctxt);
                   2912:     if (name == NULL) {
                   2913:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2914:            ctxt->sax->error(ctxt->userData, 
                   2915:             "htmlParseStartTag: invalid element name\n");
                   2916:        ctxt->wellFormed = 0;
1.80      veillard 2917:        /* Dump the bogus tag like browsers do */
                   2918:        while ((IS_CHAR(CUR)) && (CUR != '>'))
                   2919:            NEXT;
1.18      daniel   2920:         return;
1.1       daniel   2921:     }
1.73      veillard 2922:     if (xmlStrEqual(name, BAD_CAST"meta"))
1.47      daniel   2923:        meta = 1;
1.1       daniel   2924: 
                   2925:     /*
                   2926:      * Check for auto-closure of HTML elements.
                   2927:      */
                   2928:     htmlAutoClose(ctxt, name);
1.43      daniel   2929: 
                   2930:     /*
                   2931:      * Check for implied HTML elements.
                   2932:      */
                   2933:     htmlCheckImplied(ctxt, name);
1.1       daniel   2934: 
                   2935:     /*
                   2936:      * Now parse the attributes, it ends up with the ending
                   2937:      *
                   2938:      * (S Attribute)* S?
                   2939:      */
                   2940:     SKIP_BLANKS;
                   2941:     while ((IS_CHAR(CUR)) &&
                   2942:            (CUR != '>') && 
                   2943:           ((CUR != '/') || (NXT(1) != '>'))) {
1.26      daniel   2944:        long cons = ctxt->nbChars;
1.1       daniel   2945: 
1.19      daniel   2946:        GROW;
1.1       daniel   2947:        attname = htmlParseAttribute(ctxt, &attvalue);
1.31      daniel   2948:         if (attname != NULL) {
1.47      daniel   2949: 
1.1       daniel   2950:            /*
                   2951:             * Well formedness requires at most one declaration of an attribute
                   2952:             */
                   2953:            for (i = 0; i < nbatts;i += 2) {
1.73      veillard 2954:                if (xmlStrEqual(atts[i], attname)) {
1.1       daniel   2955:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.19      daniel   2956:                        ctxt->sax->error(ctxt->userData,
                   2957:                                         "Attribute %s redefined\n",
                   2958:                                         attname);
1.1       daniel   2959:                    ctxt->wellFormed = 0;
1.11      daniel   2960:                    xmlFree(attname);
1.31      daniel   2961:                    if (attvalue != NULL)
                   2962:                        xmlFree(attvalue);
1.19      daniel   2963:                    goto failed;
1.1       daniel   2964:                }
                   2965:            }
                   2966: 
                   2967:            /*
                   2968:             * Add the pair to atts
                   2969:             */
                   2970:            if (atts == NULL) {
                   2971:                maxatts = 10;
1.14      daniel   2972:                atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
1.1       daniel   2973:                if (atts == NULL) {
1.81      veillard 2974:                    xmlGenericError(xmlGenericErrorContext,
                   2975:                            "malloc of %ld byte failed\n",
1.14      daniel   2976:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2977:                    if (name != NULL) xmlFree(name);
                   2978:                    return;
1.1       daniel   2979:                }
1.23      daniel   2980:            } else if (nbatts + 4 > maxatts) {
1.1       daniel   2981:                maxatts *= 2;
1.71      veillard 2982:                atts = (const xmlChar **) xmlRealloc((void *) atts,
                   2983:                                                     maxatts * sizeof(xmlChar *));
1.1       daniel   2984:                if (atts == NULL) {
1.81      veillard 2985:                    xmlGenericError(xmlGenericErrorContext,
                   2986:                            "realloc of %ld byte failed\n",
1.14      daniel   2987:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2988:                    if (name != NULL) xmlFree(name);
                   2989:                    return;
1.1       daniel   2990:                }
                   2991:            }
                   2992:            atts[nbatts++] = attname;
                   2993:            atts[nbatts++] = attvalue;
                   2994:            atts[nbatts] = NULL;
                   2995:            atts[nbatts + 1] = NULL;
1.80      veillard 2996:        }
                   2997:        else {
                   2998:            /* Dump the bogus attribute string up to the next blank or
                   2999:             * the end of the tag. */
                   3000:            while ((IS_CHAR(CUR)) && !(IS_BLANK(CUR)) && (CUR != '>')
                   3001:             && ((CUR != '/') || (NXT(1) != '>')))
                   3002:                NEXT;
1.1       daniel   3003:        }
                   3004: 
1.19      daniel   3005: failed:
1.1       daniel   3006:        SKIP_BLANKS;
1.26      daniel   3007:         if (cons == ctxt->nbChars) {
1.1       daniel   3008:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3009:                ctxt->sax->error(ctxt->userData, 
                   3010:                 "htmlParseStartTag: problem parsing attributes\n");
                   3011:            ctxt->wellFormed = 0;
                   3012:            break;
                   3013:        }
                   3014:     }
                   3015: 
                   3016:     /*
1.47      daniel   3017:      * Handle specific association to the META tag
                   3018:      */
                   3019:     if (meta)
                   3020:        htmlCheckMeta(ctxt, atts);
                   3021: 
                   3022:     /*
1.1       daniel   3023:      * SAX: Start of Element !
                   3024:      */
1.15      daniel   3025:     htmlnamePush(ctxt, xmlStrdup(name));
1.18      daniel   3026: #ifdef DEBUG
1.81      veillard 3027:     xmlGenericError(xmlGenericErrorContext,"Start of element %s: pushed %s\n", name, ctxt->name);
1.18      daniel   3028: #endif    
1.1       daniel   3029:     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                   3030:         ctxt->sax->startElement(ctxt->userData, name, atts);
                   3031: 
                   3032:     if (atts != NULL) {
1.31      daniel   3033:         for (i = 0;i < nbatts;i++) {
                   3034:            if (atts[i] != NULL)
                   3035:                xmlFree((xmlChar *) atts[i]);
                   3036:        }
1.45      daniel   3037:        xmlFree((void *) atts);
1.1       daniel   3038:     }
1.18      daniel   3039:     if (name != NULL) xmlFree(name);
1.1       daniel   3040: }
                   3041: 
                   3042: /**
                   3043:  * htmlParseEndTag:
                   3044:  * @ctxt:  an HTML parser context
                   3045:  *
                   3046:  * parse an end of tag
                   3047:  *
                   3048:  * [42] ETag ::= '</' Name S? '>'
                   3049:  *
                   3050:  * With namespace
                   3051:  *
                   3052:  * [NS 9] ETag ::= '</' QName S? '>'
                   3053:  */
                   3054: 
                   3055: void
1.18      daniel   3056: htmlParseEndTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   3057:     xmlChar *name;
1.15      daniel   3058:     xmlChar *oldname;
1.1       daniel   3059:     int i;
                   3060: 
                   3061:     if ((CUR != '<') || (NXT(1) != '/')) {
                   3062:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3063:            ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
                   3064:        ctxt->wellFormed = 0;
                   3065:        return;
                   3066:     }
                   3067:     SKIP(2);
                   3068: 
                   3069:     name = htmlParseHTMLName(ctxt);
1.24      daniel   3070:     if (name == NULL) return;
1.1       daniel   3071: 
                   3072:     /*
                   3073:      * We should definitely be at the ending "S? '>'" part
                   3074:      */
                   3075:     SKIP_BLANKS;
                   3076:     if ((!IS_CHAR(CUR)) || (CUR != '>')) {
                   3077:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3078:            ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
                   3079:        ctxt->wellFormed = 0;
                   3080:     } else
                   3081:        NEXT;
                   3082: 
                   3083:     /*
1.18      daniel   3084:      * If the name read is not one of the element in the parsing stack
                   3085:      * then return, it's just an error.
1.1       daniel   3086:      */
1.18      daniel   3087:     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
1.73      veillard 3088:         if (xmlStrEqual(name, ctxt->nameTab[i])) break;
1.1       daniel   3089:     }
                   3090:     if (i < 0) {
                   3091:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.18      daniel   3092:            ctxt->sax->error(ctxt->userData,
                   3093:             "Unexpected end tag : %s\n", name);
1.11      daniel   3094:        xmlFree(name);
1.1       daniel   3095:        ctxt->wellFormed = 0;
                   3096:        return;
                   3097:     }
                   3098: 
1.18      daniel   3099: 
1.1       daniel   3100:     /*
                   3101:      * Check for auto-closure of HTML elements.
                   3102:      */
1.18      daniel   3103: 
1.1       daniel   3104:     htmlAutoCloseOnClose(ctxt, name);
                   3105: 
                   3106:     /*
                   3107:      * Well formedness constraints, opening and closing must match.
                   3108:      * With the exception that the autoclose may have popped stuff out
                   3109:      * of the stack.
                   3110:      */
1.73      veillard 3111:     if (!xmlStrEqual(name, ctxt->name)) {
1.18      daniel   3112: #ifdef DEBUG
1.81      veillard 3113:        xmlGenericError(xmlGenericErrorContext,"End of tag %s: expecting %s\n", name, ctxt->name);
1.18      daniel   3114: #endif
1.15      daniel   3115:         if ((ctxt->name != NULL) && 
1.73      veillard 3116:            (!xmlStrEqual(ctxt->name, name))) {
1.1       daniel   3117:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3118:                ctxt->sax->error(ctxt->userData,
                   3119:                 "Opening and ending tag mismatch: %s and %s\n",
1.15      daniel   3120:                                 name, ctxt->name);
1.1       daniel   3121:            ctxt->wellFormed = 0;
                   3122:         }
                   3123:     }
                   3124: 
                   3125:     /*
                   3126:      * SAX: End of Tag
                   3127:      */
1.15      daniel   3128:     oldname = ctxt->name;
1.73      veillard 3129:     if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
1.18      daniel   3130:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3131:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3132:        oldname = htmlnamePop(ctxt);
1.18      daniel   3133:        if (oldname != NULL) {
                   3134: #ifdef DEBUG
1.81      veillard 3135:            xmlGenericError(xmlGenericErrorContext,"End of tag %s: popping out %s\n", name, oldname);
1.18      daniel   3136: #endif
                   3137:            xmlFree(oldname);
                   3138: #ifdef DEBUG
                   3139:        } else {
1.81      veillard 3140:            xmlGenericError(xmlGenericErrorContext,"End of tag %s: stack empty !!!\n", name);
1.18      daniel   3141: #endif
                   3142:        }
                   3143:     }
1.1       daniel   3144: 
                   3145:     if (name != NULL)
1.11      daniel   3146:        xmlFree(name);
1.1       daniel   3147: 
                   3148:     return;
                   3149: }
                   3150: 
                   3151: 
                   3152: /**
                   3153:  * htmlParseReference:
                   3154:  * @ctxt:  an HTML parser context
                   3155:  * 
                   3156:  * parse and handle entity references in content,
                   3157:  * this will end-up in a call to character() since this is either a
                   3158:  * CharRef, or a predefined entity.
                   3159:  */
                   3160: void
                   3161: htmlParseReference(htmlParserCtxtPtr ctxt) {
                   3162:     htmlEntityDescPtr ent;
1.52      veillard 3163:     xmlChar out[6];
1.14      daniel   3164:     xmlChar *name;
1.1       daniel   3165:     if (CUR != '&') return;
                   3166: 
                   3167:     if (NXT(1) == '#') {
1.52      veillard 3168:        unsigned int c;
                   3169:        int bits, i = 0;
                   3170: 
                   3171:        c = htmlParseCharRef(ctxt);
1.84    ! veillard 3172:        if (c == 0)
        !          3173:            return;
        !          3174: 
1.52      veillard 3175:         if      (c <    0x80) { out[i++]= c;                bits= -6; }
                   3176:         else if (c <   0x800) { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   3177:         else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   3178:         else                  { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   3179:  
                   3180:         for ( ; bits >= 0; bits-= 6) {
                   3181:             out[i++]= ((c >> bits) & 0x3F) | 0x80;
                   3182:         }
                   3183:        out[i] = 0;
                   3184: 
1.59      veillard 3185:        htmlCheckParagraph(ctxt);
1.1       daniel   3186:        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.52      veillard 3187:            ctxt->sax->characters(ctxt->userData, out, i);
1.1       daniel   3188:     } else {
                   3189:        ent = htmlParseEntityRef(ctxt, &name);
1.32      daniel   3190:        if (name == NULL) {
1.59      veillard 3191:            htmlCheckParagraph(ctxt);
1.58      veillard 3192:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   3193:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.32      daniel   3194:            return;
                   3195:        }
1.52      veillard 3196:        if ((ent == NULL) || (ent->value <= 0)) {
1.59      veillard 3197:            htmlCheckParagraph(ctxt);
1.1       daniel   3198:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
1.8       daniel   3199:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.1       daniel   3200:                ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
1.32      daniel   3201:                /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
1.1       daniel   3202:            }
                   3203:        } else {
1.52      veillard 3204:            unsigned int c;
                   3205:            int bits, i = 0;
                   3206: 
                   3207:            c = ent->value;
                   3208:            if      (c <    0x80)
                   3209:                    { out[i++]= c;                bits= -6; }
                   3210:            else if (c <   0x800)
                   3211:                    { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
                   3212:            else if (c < 0x10000)
                   3213:                    { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
                   3214:            else                 
                   3215:                    { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
                   3216:      
                   3217:            for ( ; bits >= 0; bits-= 6) {
                   3218:                out[i++]= ((c >> bits) & 0x3F) | 0x80;
                   3219:            }
                   3220:            out[i] = 0;
                   3221: 
1.59      veillard 3222:            htmlCheckParagraph(ctxt);
1.1       daniel   3223:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.52      veillard 3224:                ctxt->sax->characters(ctxt->userData, out, i);
1.1       daniel   3225:        }
1.11      daniel   3226:        xmlFree(name);
1.1       daniel   3227:     }
                   3228: }
                   3229: 
                   3230: /**
                   3231:  * htmlParseContent:
                   3232:  * @ctxt:  an HTML parser context
                   3233:  * @name:  the node name
                   3234:  *
                   3235:  * Parse a content: comment, sub-element, reference or text.
                   3236:  *
                   3237:  */
                   3238: 
                   3239: void
1.18      daniel   3240: htmlParseContent(htmlParserCtxtPtr ctxt) {
1.15      daniel   3241:     xmlChar *currentNode;
1.18      daniel   3242:     int depth;
1.1       daniel   3243: 
1.26      daniel   3244:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   3245:     depth = ctxt->nameNr;
                   3246:     while (1) {
1.26      daniel   3247:        long cons = ctxt->nbChars;
1.1       daniel   3248: 
1.18      daniel   3249:         GROW;
                   3250:        /*
                   3251:         * Our tag or one of it's parent or children is ending.
                   3252:         */
                   3253:         if ((CUR == '<') && (NXT(1) == '/')) {
                   3254:            htmlParseEndTag(ctxt);
1.26      daniel   3255:            if (currentNode != NULL) xmlFree(currentNode);
1.18      daniel   3256:            return;
                   3257:         }
                   3258: 
                   3259:        /*
                   3260:         * Has this node been popped out during parsing of
                   3261:         * the next element
                   3262:         */
1.73      veillard 3263:         if ((!xmlStrEqual(currentNode, ctxt->name)) &&
1.26      daniel   3264:            (depth >= ctxt->nameNr)) {
                   3265:            if (currentNode != NULL) xmlFree(currentNode);
                   3266:            return;
                   3267:        }
1.18      daniel   3268: 
1.77      veillard 3269:        if ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
                   3270:            (xmlStrEqual(currentNode, BAD_CAST"style"))) {
                   3271:            /*
                   3272:             * Handle SCRIPT/STYLE separately
                   3273:             */
                   3274:            htmlParseScript(ctxt);
                   3275:        } else {
                   3276:            /*
                   3277:             * Sometimes DOCTYPE arrives in the middle of the document
                   3278:             */
                   3279:            if ((CUR == '<') && (NXT(1) == '!') &&
                   3280:                (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3281:                (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3282:                (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3283:                (UPP(8) == 'E')) {
                   3284:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3285:                    ctxt->sax->error(ctxt->userData,
                   3286:                                     "Misplaced DOCTYPE declaration\n");
                   3287:                ctxt->wellFormed = 0;
                   3288:                htmlParseDocTypeDecl(ctxt);
                   3289:            }
1.59      veillard 3290: 
1.77      veillard 3291:            /*
                   3292:             * First case :  a comment
                   3293:             */
                   3294:            if ((CUR == '<') && (NXT(1) == '!') &&
                   3295:                (NXT(2) == '-') && (NXT(3) == '-')) {
                   3296:                htmlParseComment(ctxt);
                   3297:            }
1.1       daniel   3298: 
1.77      veillard 3299:            /*
                   3300:             * Second case :  a sub-element.
                   3301:             */
                   3302:            else if (CUR == '<') {
                   3303:                htmlParseElement(ctxt);
                   3304:            }
1.1       daniel   3305: 
1.77      veillard 3306:            /*
                   3307:             * Third case : a reference. If if has not been resolved,
                   3308:             *    parsing returns it's Name, create the node 
                   3309:             */
                   3310:            else if (CUR == '&') {
                   3311:                htmlParseReference(ctxt);
                   3312:            }
1.1       daniel   3313: 
1.77      veillard 3314:            /*
                   3315:             * Fourth : end of the resource
                   3316:             */
                   3317:            else if (CUR == 0) {
                   3318:                htmlAutoClose(ctxt, NULL);
                   3319:            }
1.47      daniel   3320: 
1.77      veillard 3321:            /*
                   3322:             * Last case, text. Note that References are handled directly.
                   3323:             */
                   3324:            else {
                   3325:                htmlParseCharData(ctxt, 0);
                   3326:            }
1.1       daniel   3327: 
1.77      veillard 3328:            if (cons == ctxt->nbChars) {
                   3329:                if (ctxt->node != NULL) {
                   3330:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3331:                        ctxt->sax->error(ctxt->userData,
                   3332:                                         "detected an error in element content\n");
                   3333:                    ctxt->wellFormed = 0;
                   3334:                }
                   3335:                break;
1.22      daniel   3336:            }
1.1       daniel   3337:        }
1.5       daniel   3338:         GROW;
1.1       daniel   3339:     }
1.26      daniel   3340:     if (currentNode != NULL) xmlFree(currentNode);
1.1       daniel   3341: }
                   3342: 
                   3343: /**
                   3344:  * htmlParseElement:
                   3345:  * @ctxt:  an HTML parser context
                   3346:  *
                   3347:  * parse an HTML element, this is highly recursive
                   3348:  *
                   3349:  * [39] element ::= EmptyElemTag | STag content ETag
                   3350:  *
                   3351:  * [41] Attribute ::= Name Eq AttValue
                   3352:  */
                   3353: 
                   3354: void
                   3355: htmlParseElement(htmlParserCtxtPtr ctxt) {
1.14      daniel   3356:     xmlChar *name;
1.16      daniel   3357:     xmlChar *currentNode = NULL;
1.1       daniel   3358:     htmlElemDescPtr info;
1.10      daniel   3359:     htmlParserNodeInfo node_info;
1.31      daniel   3360:     xmlChar *oldname;
1.18      daniel   3361:     int depth = ctxt->nameNr;
1.1       daniel   3362: 
                   3363:     /* Capture start position */
1.10      daniel   3364:     if (ctxt->record_info) {
                   3365:         node_info.begin_pos = ctxt->input->consumed +
                   3366:                           (CUR_PTR - ctxt->input->base);
                   3367:        node_info.begin_line = ctxt->input->line;
                   3368:     }
1.1       daniel   3369: 
1.26      daniel   3370:     oldname = xmlStrdup(ctxt->name);
1.18      daniel   3371:     htmlParseStartTag(ctxt);
                   3372:     name = ctxt->name;
1.19      daniel   3373: #ifdef DEBUG
                   3374:     if (oldname == NULL)
1.81      veillard 3375:        xmlGenericError(xmlGenericErrorContext,
                   3376:                "Start of element %s\n", name);
1.19      daniel   3377:     else if (name == NULL)     
1.81      veillard 3378:        xmlGenericError(xmlGenericErrorContext,
                   3379:                "Start of element failed, was %s\n", oldname);
1.19      daniel   3380:     else       
1.81      veillard 3381:        xmlGenericError(xmlGenericErrorContext,
                   3382:                "Start of element %s, was %s\n", name, oldname);
1.19      daniel   3383: #endif
1.73      veillard 3384:     if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
1.18      daniel   3385:         (name == NULL)) {
1.19      daniel   3386:        if (CUR == '>')
                   3387:            NEXT;
1.26      daniel   3388:        if (oldname != NULL)
                   3389:            xmlFree(oldname);
1.1       daniel   3390:         return;
                   3391:     }
1.26      daniel   3392:     if (oldname != NULL)
                   3393:        xmlFree(oldname);
1.1       daniel   3394: 
                   3395:     /*
                   3396:      * Lookup the info for that element.
                   3397:      */
                   3398:     info = htmlTagLookup(name);
                   3399:     if (info == NULL) {
                   3400:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3401:            ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   3402:                             name);
                   3403:        ctxt->wellFormed = 0;
                   3404:     } else if (info->depr) {
                   3405: /***************************
                   3406:        if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   3407:            ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
                   3408:                               name);
                   3409:  ***************************/
                   3410:     }
                   3411: 
                   3412:     /*
                   3413:      * Check for an Empty Element labelled the XML/SGML way
                   3414:      */
                   3415:     if ((CUR == '/') && (NXT(1) == '>')) {
                   3416:         SKIP(2);
                   3417:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3418:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3419:        oldname = htmlnamePop(ctxt);
1.18      daniel   3420: #ifdef DEBUG
1.81      veillard 3421:         xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n", oldname);
1.18      daniel   3422: #endif
1.17      daniel   3423:        if (oldname != NULL)
                   3424:            xmlFree(oldname);
1.1       daniel   3425:        return;
                   3426:     }
                   3427: 
1.5       daniel   3428:     if (CUR == '>') {
                   3429:         NEXT;
                   3430:     } else {
1.1       daniel   3431:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.56      veillard 3432:            ctxt->sax->error(ctxt->userData,
                   3433:                             "Couldn't find end of Start Tag %s\n",
                   3434:                             name);
1.1       daniel   3435:        ctxt->wellFormed = 0;
                   3436: 
                   3437:        /*
                   3438:         * end of parsing of this node.
                   3439:         */
1.73      veillard 3440:        if (xmlStrEqual(name, ctxt->name)) { 
1.18      daniel   3441:            nodePop(ctxt);
1.24      daniel   3442:            oldname = htmlnamePop(ctxt);
1.18      daniel   3443: #ifdef DEBUG
1.81      veillard 3444:            xmlGenericError(xmlGenericErrorContext,"End of start tag problem: popping out %s\n", oldname);
1.18      daniel   3445: #endif
                   3446:            if (oldname != NULL)
                   3447:                xmlFree(oldname);
                   3448:        }    
1.10      daniel   3449: 
                   3450:        /*
                   3451:         * Capture end position and add node
                   3452:         */
                   3453:        if ( currentNode != NULL && ctxt->record_info ) {
                   3454:           node_info.end_pos = ctxt->input->consumed +
                   3455:                              (CUR_PTR - ctxt->input->base);
                   3456:           node_info.end_line = ctxt->input->line;
1.15      daniel   3457:           node_info.node = ctxt->node;
1.10      daniel   3458:           xmlParserAddNodeInfo(ctxt, &node_info);
                   3459:        }
1.1       daniel   3460:        return;
                   3461:     }
                   3462: 
                   3463:     /*
                   3464:      * Check for an Empty Element from DTD definition
                   3465:      */
                   3466:     if ((info != NULL) && (info->empty)) {
                   3467:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3468:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   3469:        oldname = htmlnamePop(ctxt);
1.18      daniel   3470: #ifdef DEBUG
1.81      veillard 3471:        xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
1.18      daniel   3472: #endif
1.17      daniel   3473:        if (oldname != NULL)
                   3474:            xmlFree(oldname);
1.1       daniel   3475:        return;
                   3476:     }
                   3477: 
                   3478:     /*
                   3479:      * Parse the content of the element:
                   3480:      */
1.26      daniel   3481:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   3482:     depth = ctxt->nameNr;
                   3483:     while (IS_CHAR(CUR)) {
                   3484:        htmlParseContent(ctxt);
                   3485:        if (ctxt->nameNr < depth) break; 
                   3486:     }  
1.1       daniel   3487: 
                   3488:     if (!IS_CHAR(CUR)) {
1.49      daniel   3489:        /************
1.1       daniel   3490:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3491:            ctxt->sax->error(ctxt->userData,
1.18      daniel   3492:                 "Premature end of data in tag %s\n", currentNode);
1.1       daniel   3493:        ctxt->wellFormed = 0;
1.49      daniel   3494:         *************/
1.1       daniel   3495: 
                   3496:        /*
                   3497:         * end of parsing of this node.
                   3498:         */
                   3499:        nodePop(ctxt);
1.24      daniel   3500:        oldname = htmlnamePop(ctxt);
1.18      daniel   3501: #ifdef DEBUG
1.81      veillard 3502:        xmlGenericError(xmlGenericErrorContext,"Premature end of tag %s : popping out %s\n", name, oldname);
1.18      daniel   3503: #endif
1.17      daniel   3504:        if (oldname != NULL)
                   3505:            xmlFree(oldname);
1.26      daniel   3506:        if (currentNode != NULL)
                   3507:            xmlFree(currentNode);
1.1       daniel   3508:        return;
                   3509:     }
1.10      daniel   3510: 
                   3511:     /*
                   3512:      * Capture end position and add node
                   3513:      */
                   3514:     if ( currentNode != NULL && ctxt->record_info ) {
                   3515:        node_info.end_pos = ctxt->input->consumed +
                   3516:                           (CUR_PTR - ctxt->input->base);
                   3517:        node_info.end_line = ctxt->input->line;
1.15      daniel   3518:        node_info.node = ctxt->node;
1.10      daniel   3519:        xmlParserAddNodeInfo(ctxt, &node_info);
                   3520:     }
1.26      daniel   3521:     if (currentNode != NULL)
                   3522:        xmlFree(currentNode);
1.1       daniel   3523: }
                   3524: 
                   3525: /**
                   3526:  * htmlParseDocument :
                   3527:  * @ctxt:  an HTML parser context
                   3528:  * 
                   3529:  * parse an HTML document (and build a tree if using the standard SAX
                   3530:  * interface).
                   3531:  *
                   3532:  * Returns 0, -1 in case of error. the parser context is augmented
                   3533:  *                as a result of the parsing.
                   3534:  */
                   3535: 
                   3536: int
                   3537: htmlParseDocument(htmlParserCtxtPtr ctxt) {
1.59      veillard 3538:     xmlDtdPtr dtd;
                   3539: 
1.1       daniel   3540:     htmlDefaultSAXHandlerInit();
                   3541:     ctxt->html = 1;
                   3542: 
1.5       daniel   3543:     GROW;
1.1       daniel   3544:     /*
1.9       daniel   3545:      * SAX: beginning of the document processing.
1.1       daniel   3546:      */
                   3547:     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3548:         ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
                   3549: 
                   3550:     /*
                   3551:      * Wipe out everything which is before the first '<'
                   3552:      */
1.22      daniel   3553:     SKIP_BLANKS;
1.1       daniel   3554:     if (CUR == 0) {
                   3555:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3556:            ctxt->sax->error(ctxt->userData, "Document is empty\n");
                   3557:        ctxt->wellFormed = 0;
                   3558:     }
                   3559: 
1.40      daniel   3560:     if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
                   3561:        ctxt->sax->startDocument(ctxt->userData);
                   3562: 
                   3563: 
1.22      daniel   3564:     /*
                   3565:      * Parse possible comments before any content
                   3566:      */
                   3567:     while ((CUR == '<') && (NXT(1) == '!') &&
                   3568:            (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   3569:         htmlParseComment(ctxt);           
1.22      daniel   3570:        SKIP_BLANKS;
                   3571:     }     
                   3572: 
1.1       daniel   3573: 
                   3574:     /*
                   3575:      * Then possibly doc type declaration(s) and more Misc
                   3576:      * (doctypedecl Misc*)?
                   3577:      */
                   3578:     if ((CUR == '<') && (NXT(1) == '!') &&
                   3579:        (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3580:        (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3581:        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3582:        (UPP(8) == 'E')) {
                   3583:        htmlParseDocTypeDecl(ctxt);
                   3584:     }
                   3585:     SKIP_BLANKS;
                   3586: 
                   3587:     /*
1.55      veillard 3588:      * Parse possible comments before any content
                   3589:      */
                   3590:     while ((CUR == '<') && (NXT(1) == '!') &&
                   3591:            (NXT(2) == '-') && (NXT(3) == '-')) {
                   3592:         htmlParseComment(ctxt);           
                   3593:        SKIP_BLANKS;
                   3594:     }     
                   3595: 
                   3596:     /*
1.1       daniel   3597:      * Time to start parsing the tree itself
                   3598:      */
1.22      daniel   3599:     htmlParseContent(ctxt);
1.1       daniel   3600: 
                   3601:     /*
1.47      daniel   3602:      * autoclose
                   3603:      */
                   3604:     if (CUR == 0)
                   3605:        htmlAutoClose(ctxt, NULL);
                   3606: 
                   3607: 
                   3608:     /*
1.1       daniel   3609:      * SAX: end of the document processing.
                   3610:      */
                   3611:     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3612:         ctxt->sax->endDocument(ctxt->userData);
1.59      veillard 3613: 
                   3614:     if (ctxt->myDoc != NULL) {
                   3615:        dtd = xmlGetIntSubset(ctxt->myDoc);
                   3616:        if (dtd == NULL)
                   3617:            ctxt->myDoc->intSubset = 
                   3618:                xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML", 
                   3619:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   3620:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
                   3621:     }
1.1       daniel   3622:     if (! ctxt->wellFormed) return(-1);
                   3623:     return(0);
                   3624: }
                   3625: 
                   3626: 
1.30      daniel   3627: /************************************************************************
                   3628:  *                                                                     *
                   3629:  *                     Parser contexts handling                        *
                   3630:  *                                                                     *
                   3631:  ************************************************************************/
1.1       daniel   3632: 
                   3633: /**
                   3634:  * xmlInitParserCtxt:
                   3635:  * @ctxt:  an HTML parser context
                   3636:  *
                   3637:  * Initialize a parser context
                   3638:  */
                   3639: 
                   3640: void
                   3641: htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
                   3642: {
                   3643:     htmlSAXHandler *sax;
                   3644: 
1.21      daniel   3645:     if (ctxt == NULL) return;
                   3646:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   3647: 
1.11      daniel   3648:     sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
1.1       daniel   3649:     if (sax == NULL) {
1.81      veillard 3650:         xmlGenericError(xmlGenericErrorContext,
                   3651:                "htmlInitParserCtxt: out of memory\n");
1.1       daniel   3652:     }
1.68      veillard 3653:     else
                   3654:         memset(sax, 0, sizeof(htmlSAXHandler));
1.1       daniel   3655: 
                   3656:     /* Allocate the Input stack */
1.19      daniel   3657:     ctxt->inputTab = (htmlParserInputPtr *) 
                   3658:                       xmlMalloc(5 * sizeof(htmlParserInputPtr));
                   3659:     if (ctxt->inputTab == NULL) {
1.81      veillard 3660:         xmlGenericError(xmlGenericErrorContext,
                   3661:                "htmlInitParserCtxt: out of memory\n");
1.65      veillard 3662:        ctxt->inputNr = 0;
                   3663:        ctxt->inputMax = 0;
                   3664:        ctxt->input = NULL;
                   3665:        return;
1.19      daniel   3666:     }
1.1       daniel   3667:     ctxt->inputNr = 0;
                   3668:     ctxt->inputMax = 5;
                   3669:     ctxt->input = NULL;
                   3670:     ctxt->version = NULL;
                   3671:     ctxt->encoding = NULL;
                   3672:     ctxt->standalone = -1;
1.30      daniel   3673:     ctxt->instate = XML_PARSER_START;
1.1       daniel   3674: 
                   3675:     /* Allocate the Node stack */
1.11      daniel   3676:     ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
1.65      veillard 3677:     if (ctxt->nodeTab == NULL) {
1.81      veillard 3678:         xmlGenericError(xmlGenericErrorContext,
                   3679:                "htmlInitParserCtxt: out of memory\n");
1.65      veillard 3680:        ctxt->nodeNr = 0;
                   3681:        ctxt->nodeMax = 0;
                   3682:        ctxt->node = NULL;
                   3683:        ctxt->inputNr = 0;
                   3684:        ctxt->inputMax = 0;
                   3685:        ctxt->input = NULL;
                   3686:        return;
                   3687:     }
1.1       daniel   3688:     ctxt->nodeNr = 0;
                   3689:     ctxt->nodeMax = 10;
                   3690:     ctxt->node = NULL;
                   3691: 
1.15      daniel   3692:     /* Allocate the Name stack */
                   3693:     ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1.65      veillard 3694:     if (ctxt->nameTab == NULL) {
1.81      veillard 3695:         xmlGenericError(xmlGenericErrorContext,
                   3696:                "htmlInitParserCtxt: out of memory\n");
1.65      veillard 3697:        ctxt->nameNr = 0;
                   3698:        ctxt->nameMax = 10;
                   3699:        ctxt->name = NULL;
                   3700:        ctxt->nodeNr = 0;
                   3701:        ctxt->nodeMax = 0;
                   3702:        ctxt->node = NULL;
                   3703:        ctxt->inputNr = 0;
                   3704:        ctxt->inputMax = 0;
                   3705:        ctxt->input = NULL;
                   3706:        return;
                   3707:     }
1.15      daniel   3708:     ctxt->nameNr = 0;
                   3709:     ctxt->nameMax = 10;
                   3710:     ctxt->name = NULL;
                   3711: 
1.1       daniel   3712:     if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
                   3713:     else {
                   3714:         ctxt->sax = sax;
                   3715:        memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
                   3716:     }
                   3717:     ctxt->userData = ctxt;
                   3718:     ctxt->myDoc = NULL;
                   3719:     ctxt->wellFormed = 1;
                   3720:     ctxt->replaceEntities = 0;
                   3721:     ctxt->html = 1;
                   3722:     ctxt->record_info = 0;
1.21      daniel   3723:     ctxt->validate = 0;
1.26      daniel   3724:     ctxt->nbChars = 0;
1.30      daniel   3725:     ctxt->checkIndex = 0;
1.1       daniel   3726:     xmlInitNodeInfoSeq(&ctxt->node_seq);
                   3727: }
                   3728: 
                   3729: /**
                   3730:  * htmlFreeParserCtxt:
                   3731:  * @ctxt:  an HTML parser context
                   3732:  *
                   3733:  * Free all the memory used by a parser context. However the parsed
                   3734:  * document in ctxt->myDoc is not freed.
                   3735:  */
                   3736: 
                   3737: void
                   3738: htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
                   3739: {
1.47      daniel   3740:     xmlFreeParserCtxt(ctxt);
1.1       daniel   3741: }
                   3742: 
                   3743: /**
                   3744:  * htmlCreateDocParserCtxt :
1.14      daniel   3745:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   3746:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3747:  *
                   3748:  * Create a parser context for an HTML document.
                   3749:  *
                   3750:  * Returns the new parser context or NULL
                   3751:  */
                   3752: htmlParserCtxtPtr
1.14      daniel   3753: htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding) {
1.1       daniel   3754:     htmlParserCtxtPtr ctxt;
                   3755:     htmlParserInputPtr input;
                   3756:     /* htmlCharEncoding enc; */
                   3757: 
1.11      daniel   3758:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   3759:     if (ctxt == NULL) {
                   3760:         perror("malloc");
                   3761:        return(NULL);
                   3762:     }
                   3763:     htmlInitParserCtxt(ctxt);
1.11      daniel   3764:     input = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   3765:     if (input == NULL) {
                   3766:         perror("malloc");
1.11      daniel   3767:        xmlFree(ctxt);
1.1       daniel   3768:        return(NULL);
                   3769:     }
1.19      daniel   3770:     memset(input, 0, sizeof(htmlParserInput));
1.1       daniel   3771: 
                   3772:     input->line = 1;
                   3773:     input->col = 1;
                   3774:     input->base = cur;
                   3775:     input->cur = cur;
                   3776: 
                   3777:     inputPush(ctxt, input);
                   3778:     return(ctxt);
                   3779: }
                   3780: 
1.31      daniel   3781: /************************************************************************
                   3782:  *                                                                     *
                   3783:  *             Progressive parsing interfaces                          *
                   3784:  *                                                                     *
                   3785:  ************************************************************************/
                   3786: 
                   3787: /**
                   3788:  * htmlParseLookupSequence:
                   3789:  * @ctxt:  an HTML parser context
                   3790:  * @first:  the first char to lookup
                   3791:  * @next:  the next char to lookup or zero
                   3792:  * @third:  the next char to lookup or zero
                   3793:  *
                   3794:  * Try to find if a sequence (first, next, third) or  just (first next) or
                   3795:  * (first) is available in the input stream.
                   3796:  * This function has a side effect of (possibly) incrementing ctxt->checkIndex
                   3797:  * to avoid rescanning sequences of bytes, it DOES change the state of the
                   3798:  * parser, do not use liberally.
                   3799:  * This is basically similar to xmlParseLookupSequence()
                   3800:  *
                   3801:  * Returns the index to the current parsing point if the full sequence
                   3802:  *      is available, -1 otherwise.
                   3803:  */
                   3804: int
                   3805: htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
                   3806:                        xmlChar next, xmlChar third) {
                   3807:     int base, len;
                   3808:     htmlParserInputPtr in;
                   3809:     const xmlChar *buf;
                   3810: 
                   3811:     in = ctxt->input;
                   3812:     if (in == NULL) return(-1);
                   3813:     base = in->cur - in->base;
                   3814:     if (base < 0) return(-1);
                   3815:     if (ctxt->checkIndex > base)
                   3816:         base = ctxt->checkIndex;
                   3817:     if (in->buf == NULL) {
                   3818:        buf = in->base;
                   3819:        len = in->length;
                   3820:     } else {
                   3821:        buf = in->buf->buffer->content;
                   3822:        len = in->buf->buffer->use;
                   3823:     }
                   3824:     /* take into account the sequence length */
                   3825:     if (third) len -= 2;
                   3826:     else if (next) len --;
                   3827:     for (;base < len;base++) {
                   3828:         if (buf[base] == first) {
                   3829:            if (third != 0) {
                   3830:                if ((buf[base + 1] != next) ||
                   3831:                    (buf[base + 2] != third)) continue;
                   3832:            } else if (next != 0) {
                   3833:                if (buf[base + 1] != next) continue;
                   3834:            }
                   3835:            ctxt->checkIndex = 0;
                   3836: #ifdef DEBUG_PUSH
                   3837:            if (next == 0)
1.81      veillard 3838:                xmlGenericError(xmlGenericErrorContext,
                   3839:                        "HPP: lookup '%c' found at %d\n",
1.31      daniel   3840:                        first, base);
                   3841:            else if (third == 0)
1.81      veillard 3842:                xmlGenericError(xmlGenericErrorContext,
                   3843:                        "HPP: lookup '%c%c' found at %d\n",
1.31      daniel   3844:                        first, next, base);
                   3845:            else 
1.81      veillard 3846:                xmlGenericError(xmlGenericErrorContext,
                   3847:                        "HPP: lookup '%c%c%c' found at %d\n",
1.31      daniel   3848:                        first, next, third, base);
                   3849: #endif
                   3850:            return(base - (in->cur - in->base));
                   3851:        }
                   3852:     }
                   3853:     ctxt->checkIndex = base;
                   3854: #ifdef DEBUG_PUSH
                   3855:     if (next == 0)
1.81      veillard 3856:        xmlGenericError(xmlGenericErrorContext,
                   3857:                "HPP: lookup '%c' failed\n", first);
1.31      daniel   3858:     else if (third == 0)
1.81      veillard 3859:        xmlGenericError(xmlGenericErrorContext,
                   3860:                "HPP: lookup '%c%c' failed\n", first, next);
1.31      daniel   3861:     else       
1.81      veillard 3862:        xmlGenericError(xmlGenericErrorContext,
                   3863:                "HPP: lookup '%c%c%c' failed\n", first, next, third);
1.31      daniel   3864: #endif
                   3865:     return(-1);
                   3866: }
                   3867: 
                   3868: /**
1.32      daniel   3869:  * htmlParseTryOrFinish:
1.31      daniel   3870:  * @ctxt:  an HTML parser context
1.32      daniel   3871:  * @terminate:  last chunk indicator
1.31      daniel   3872:  *
                   3873:  * Try to progress on parsing
                   3874:  *
                   3875:  * Returns zero if no parsing was possible
                   3876:  */
                   3877: int
1.32      daniel   3878: htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
1.31      daniel   3879:     int ret = 0;
                   3880:     htmlParserInputPtr in;
1.47      daniel   3881:     int avail = 0;
1.31      daniel   3882:     xmlChar cur, next;
                   3883: 
                   3884: #ifdef DEBUG_PUSH
                   3885:     switch (ctxt->instate) {
                   3886:        case XML_PARSER_EOF:
1.81      veillard 3887:            xmlGenericError(xmlGenericErrorContext,
                   3888:                    "HPP: try EOF\n"); break;
1.31      daniel   3889:        case XML_PARSER_START:
1.81      veillard 3890:            xmlGenericError(xmlGenericErrorContext,
                   3891:                    "HPP: try START\n"); break;
1.31      daniel   3892:        case XML_PARSER_MISC:
1.81      veillard 3893:            xmlGenericError(xmlGenericErrorContext,
                   3894:                    "HPP: try MISC\n");break;
1.31      daniel   3895:        case XML_PARSER_COMMENT:
1.81      veillard 3896:            xmlGenericError(xmlGenericErrorContext,
                   3897:                    "HPP: try COMMENT\n");break;
1.31      daniel   3898:        case XML_PARSER_PROLOG:
1.81      veillard 3899:            xmlGenericError(xmlGenericErrorContext,
                   3900:                    "HPP: try PROLOG\n");break;
1.31      daniel   3901:        case XML_PARSER_START_TAG:
1.81      veillard 3902:            xmlGenericError(xmlGenericErrorContext,
                   3903:                    "HPP: try START_TAG\n");break;
1.31      daniel   3904:        case XML_PARSER_CONTENT:
1.81      veillard 3905:            xmlGenericError(xmlGenericErrorContext,
                   3906:                    "HPP: try CONTENT\n");break;
1.31      daniel   3907:        case XML_PARSER_CDATA_SECTION:
1.81      veillard 3908:            xmlGenericError(xmlGenericErrorContext,
                   3909:                    "HPP: try CDATA_SECTION\n");break;
1.31      daniel   3910:        case XML_PARSER_END_TAG:
1.81      veillard 3911:            xmlGenericError(xmlGenericErrorContext,
                   3912:                    "HPP: try END_TAG\n");break;
1.31      daniel   3913:        case XML_PARSER_ENTITY_DECL:
1.81      veillard 3914:            xmlGenericError(xmlGenericErrorContext,
                   3915:                    "HPP: try ENTITY_DECL\n");break;
1.31      daniel   3916:        case XML_PARSER_ENTITY_VALUE:
1.81      veillard 3917:            xmlGenericError(xmlGenericErrorContext,
                   3918:                    "HPP: try ENTITY_VALUE\n");break;
1.31      daniel   3919:        case XML_PARSER_ATTRIBUTE_VALUE:
1.81      veillard 3920:            xmlGenericError(xmlGenericErrorContext,
                   3921:                    "HPP: try ATTRIBUTE_VALUE\n");break;
1.31      daniel   3922:        case XML_PARSER_DTD:
1.81      veillard 3923:            xmlGenericError(xmlGenericErrorContext,
                   3924:                    "HPP: try DTD\n");break;
1.31      daniel   3925:        case XML_PARSER_EPILOG:
1.81      veillard 3926:            xmlGenericError(xmlGenericErrorContext,
                   3927:                    "HPP: try EPILOG\n");break;
1.31      daniel   3928:        case XML_PARSER_PI:
1.81      veillard 3929:            xmlGenericError(xmlGenericErrorContext,
                   3930:                    "HPP: try PI\n");break;
1.77      veillard 3931:        case XML_PARSER_SYSTEM_LITERAL:
1.81      veillard 3932:            xmlGenericError(xmlGenericErrorContext,
                   3933:                    "HPP: try SYSTEM_LITERAL\n");break;
1.31      daniel   3934:     }
                   3935: #endif
                   3936: 
                   3937:     while (1) {
                   3938: 
                   3939:        in = ctxt->input;
                   3940:        if (in == NULL) break;
                   3941:        if (in->buf == NULL)
                   3942:            avail = in->length - (in->cur - in->base);
                   3943:        else
                   3944:            avail = in->buf->buffer->use - (in->cur - in->base);
1.47      daniel   3945:        if ((avail == 0) && (terminate)) {
                   3946:            htmlAutoClose(ctxt, NULL);
1.54      veillard 3947:            if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { 
                   3948:                /*
                   3949:                 * SAX: end of the document processing.
                   3950:                 */
1.47      daniel   3951:                ctxt->instate = XML_PARSER_EOF;
1.54      veillard 3952:                if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3953:                    ctxt->sax->endDocument(ctxt->userData);
                   3954:            }
1.47      daniel   3955:        }
1.31      daniel   3956:         if (avail < 1)
                   3957:            goto done;
                   3958:         switch (ctxt->instate) {
                   3959:             case XML_PARSER_EOF:
                   3960:                /*
                   3961:                 * Document parsing is done !
                   3962:                 */
                   3963:                goto done;
                   3964:             case XML_PARSER_START:
                   3965:                /*
                   3966:                 * Very first chars read from the document flow.
                   3967:                 */
                   3968:                cur = in->cur[0];
                   3969:                if (IS_BLANK(cur)) {
                   3970:                    SKIP_BLANKS;
                   3971:                    if (in->buf == NULL)
                   3972:                        avail = in->length - (in->cur - in->base);
                   3973:                    else
                   3974:                        avail = in->buf->buffer->use - (in->cur - in->base);
                   3975:                }
                   3976:                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3977:                    ctxt->sax->setDocumentLocator(ctxt->userData,
                   3978:                                                  &xmlDefaultSAXLocator);
1.46      daniel   3979:                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
                   3980:                    (!ctxt->disableSAX))
                   3981:                    ctxt->sax->startDocument(ctxt->userData);
                   3982: 
1.31      daniel   3983:                cur = in->cur[0];
                   3984:                next = in->cur[1];
                   3985:                if ((cur == '<') && (next == '!') &&
                   3986:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3987:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3988:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3989:                    (UPP(8) == 'E')) {
1.32      daniel   3990:                    if ((!terminate) &&
                   3991:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3992:                        goto done;
                   3993: #ifdef DEBUG_PUSH
1.81      veillard 3994:                    xmlGenericError(xmlGenericErrorContext,
                   3995:                            "HPP: Parsing internal subset\n");
1.31      daniel   3996: #endif
                   3997:                    htmlParseDocTypeDecl(ctxt);
                   3998:                    ctxt->instate = XML_PARSER_PROLOG;
                   3999: #ifdef DEBUG_PUSH
1.81      veillard 4000:                    xmlGenericError(xmlGenericErrorContext,
                   4001:                            "HPP: entering PROLOG\n");
1.31      daniel   4002: #endif
                   4003:                 } else {
                   4004:                    ctxt->instate = XML_PARSER_MISC;
                   4005:                }
                   4006: #ifdef DEBUG_PUSH
1.81      veillard 4007:                xmlGenericError(xmlGenericErrorContext,
                   4008:                        "HPP: entering MISC\n");
1.31      daniel   4009: #endif
                   4010:                break;
                   4011:             case XML_PARSER_MISC:
                   4012:                SKIP_BLANKS;
                   4013:                if (in->buf == NULL)
                   4014:                    avail = in->length - (in->cur - in->base);
                   4015:                else
                   4016:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   4017:                if (avail < 2)
                   4018:                    goto done;
                   4019:                cur = in->cur[0];
                   4020:                next = in->cur[1];
                   4021:                if ((cur == '<') && (next == '!') &&
                   4022:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   4023:                    if ((!terminate) &&
                   4024:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   4025:                        goto done;
                   4026: #ifdef DEBUG_PUSH
1.81      veillard 4027:                    xmlGenericError(xmlGenericErrorContext,
                   4028:                            "HPP: Parsing Comment\n");
1.31      daniel   4029: #endif
                   4030:                    htmlParseComment(ctxt);
                   4031:                    ctxt->instate = XML_PARSER_MISC;
                   4032:                } else if ((cur == '<') && (next == '!') &&
                   4033:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   4034:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   4035:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   4036:                    (UPP(8) == 'E')) {
1.32      daniel   4037:                    if ((!terminate) &&
                   4038:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   4039:                        goto done;
                   4040: #ifdef DEBUG_PUSH
1.81      veillard 4041:                    xmlGenericError(xmlGenericErrorContext,
                   4042:                            "HPP: Parsing internal subset\n");
1.31      daniel   4043: #endif
                   4044:                    htmlParseDocTypeDecl(ctxt);
                   4045:                    ctxt->instate = XML_PARSER_PROLOG;
                   4046: #ifdef DEBUG_PUSH
1.81      veillard 4047:                    xmlGenericError(xmlGenericErrorContext,
                   4048:                            "HPP: entering PROLOG\n");
1.31      daniel   4049: #endif
                   4050:                } else if ((cur == '<') && (next == '!') &&
                   4051:                           (avail < 9)) {
                   4052:                    goto done;
                   4053:                } else {
                   4054:                    ctxt->instate = XML_PARSER_START_TAG;
                   4055: #ifdef DEBUG_PUSH
1.81      veillard 4056:                    xmlGenericError(xmlGenericErrorContext,
                   4057:                            "HPP: entering START_TAG\n");
1.31      daniel   4058: #endif
                   4059:                }
                   4060:                break;
                   4061:             case XML_PARSER_PROLOG:
                   4062:                SKIP_BLANKS;
                   4063:                if (in->buf == NULL)
                   4064:                    avail = in->length - (in->cur - in->base);
                   4065:                else
                   4066:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   4067:                if (avail < 2) 
                   4068:                    goto done;
                   4069:                cur = in->cur[0];
                   4070:                next = in->cur[1];
                   4071:                if ((cur == '<') && (next == '!') &&
                   4072:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   4073:                    if ((!terminate) &&
                   4074:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   4075:                        goto done;
                   4076: #ifdef DEBUG_PUSH
1.81      veillard 4077:                    xmlGenericError(xmlGenericErrorContext,
                   4078:                            "HPP: Parsing Comment\n");
1.31      daniel   4079: #endif
                   4080:                    htmlParseComment(ctxt);
                   4081:                    ctxt->instate = XML_PARSER_PROLOG;
                   4082:                } else if ((cur == '<') && (next == '!') &&
                   4083:                           (avail < 4)) {
                   4084:                    goto done;
                   4085:                } else {
                   4086:                    ctxt->instate = XML_PARSER_START_TAG;
                   4087: #ifdef DEBUG_PUSH
1.81      veillard 4088:                    xmlGenericError(xmlGenericErrorContext,
                   4089:                            "HPP: entering START_TAG\n");
1.31      daniel   4090: #endif
                   4091:                }
                   4092:                break;
                   4093:             case XML_PARSER_EPILOG:
                   4094:                if (in->buf == NULL)
                   4095:                    avail = in->length - (in->cur - in->base);
                   4096:                else
                   4097:                    avail = in->buf->buffer->use - (in->cur - in->base);
1.55      veillard 4098:                if (avail < 1)
                   4099:                    goto done;
                   4100:                cur = in->cur[0];
                   4101:                if (IS_BLANK(cur)) {
                   4102:                    htmlParseCharData(ctxt, 0);
                   4103:                    goto done;
                   4104:                }
1.31      daniel   4105:                if (avail < 2)
                   4106:                    goto done;
                   4107:                next = in->cur[1];
                   4108:                if ((cur == '<') && (next == '!') &&
                   4109:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   4110:                    if ((!terminate) &&
                   4111:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   4112:                        goto done;
                   4113: #ifdef DEBUG_PUSH
1.81      veillard 4114:                    xmlGenericError(xmlGenericErrorContext,
                   4115:                            "HPP: Parsing Comment\n");
1.31      daniel   4116: #endif
                   4117:                    htmlParseComment(ctxt);
                   4118:                    ctxt->instate = XML_PARSER_EPILOG;
                   4119:                } else if ((cur == '<') && (next == '!') &&
                   4120:                           (avail < 4)) {
                   4121:                    goto done;
                   4122:                } else {
1.67      veillard 4123:                    ctxt->errNo = XML_ERR_DOCUMENT_END;
1.31      daniel   4124:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4125:                        ctxt->sax->error(ctxt->userData,
                   4126:                            "Extra content at the end of the document\n");
                   4127:                    ctxt->wellFormed = 0;
                   4128:                    ctxt->instate = XML_PARSER_EOF;
                   4129: #ifdef DEBUG_PUSH
1.81      veillard 4130:                    xmlGenericError(xmlGenericErrorContext,
                   4131:                            "HPP: entering EOF\n");
1.31      daniel   4132: #endif
                   4133:                    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4134:                        ctxt->sax->endDocument(ctxt->userData);
                   4135:                    goto done;
                   4136:                }
                   4137:                break;
                   4138:             case XML_PARSER_START_TAG: {
                   4139:                xmlChar *name, *oldname;
                   4140:                int depth = ctxt->nameNr;
                   4141:                htmlElemDescPtr info;
                   4142: 
                   4143:                if (avail < 2)
                   4144:                    goto done;
                   4145:                cur = in->cur[0];
                   4146:                if (cur != '<') {
                   4147:                    ctxt->instate = XML_PARSER_CONTENT;
                   4148: #ifdef DEBUG_PUSH
1.81      veillard 4149:                    xmlGenericError(xmlGenericErrorContext,
                   4150:                            "HPP: entering CONTENT\n");
1.31      daniel   4151: #endif
                   4152:                    break;
                   4153:                }
1.32      daniel   4154:                if ((!terminate) &&
                   4155:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   4156:                    goto done;
                   4157: 
                   4158:                oldname = xmlStrdup(ctxt->name);
                   4159:                htmlParseStartTag(ctxt);
                   4160:                name = ctxt->name;
                   4161: #ifdef DEBUG
                   4162:                if (oldname == NULL)
1.81      veillard 4163:                    xmlGenericError(xmlGenericErrorContext,
                   4164:                            "Start of element %s\n", name);
1.31      daniel   4165:                else if (name == NULL)  
1.81      veillard 4166:                    xmlGenericError(xmlGenericErrorContext,
                   4167:                            "Start of element failed, was %s\n",
1.31      daniel   4168:                            oldname);
                   4169:                else    
1.81      veillard 4170:                    xmlGenericError(xmlGenericErrorContext,
                   4171:                            "Start of element %s, was %s\n",
1.31      daniel   4172:                            name, oldname);
                   4173: #endif
                   4174:                if (((depth == ctxt->nameNr) &&
1.73      veillard 4175:                     (xmlStrEqual(oldname, ctxt->name))) ||
1.31      daniel   4176:                    (name == NULL)) {
                   4177:                    if (CUR == '>')
                   4178:                        NEXT;
                   4179:                    if (oldname != NULL)
                   4180:                        xmlFree(oldname);
                   4181:                    break;
                   4182:                }
                   4183:                if (oldname != NULL)
                   4184:                    xmlFree(oldname);
                   4185: 
                   4186:                /*
                   4187:                 * Lookup the info for that element.
                   4188:                 */
                   4189:                info = htmlTagLookup(name);
                   4190:                if (info == NULL) {
                   4191:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4192:                        ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   4193:                                         name);
                   4194:                    ctxt->wellFormed = 0;
                   4195:                } else if (info->depr) {
                   4196:                    /***************************
                   4197:                    if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   4198:                        ctxt->sax->warning(ctxt->userData,
                   4199:                                           "Tag %s is deprecated\n",
                   4200:                                           name);
                   4201:                     ***************************/
                   4202:                }
                   4203: 
                   4204:                /*
                   4205:                 * Check for an Empty Element labelled the XML/SGML way
                   4206:                 */
                   4207:                if ((CUR == '/') && (NXT(1) == '>')) {
                   4208:                    SKIP(2);
                   4209:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   4210:                        ctxt->sax->endElement(ctxt->userData, name);
                   4211:                    oldname = htmlnamePop(ctxt);
                   4212: #ifdef DEBUG
1.81      veillard 4213:                    xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n",
1.31      daniel   4214:                            oldname);
                   4215: #endif
                   4216:                    if (oldname != NULL)
                   4217:                        xmlFree(oldname);
                   4218:                    ctxt->instate = XML_PARSER_CONTENT;
                   4219: #ifdef DEBUG_PUSH
1.81      veillard 4220:                    xmlGenericError(xmlGenericErrorContext,
                   4221:                            "HPP: entering CONTENT\n");
1.31      daniel   4222: #endif
                   4223:                    break;
                   4224:                }
                   4225: 
                   4226:                if (CUR == '>') {
                   4227:                    NEXT;
                   4228:                } else {
                   4229:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4230:                        ctxt->sax->error(ctxt->userData, 
                   4231:                                         "Couldn't find end of Start Tag %s\n",
                   4232:                                         name);
                   4233:                    ctxt->wellFormed = 0;
                   4234: 
                   4235:                    /*
                   4236:                     * end of parsing of this node.
                   4237:                     */
1.73      veillard 4238:                    if (xmlStrEqual(name, ctxt->name)) { 
1.31      daniel   4239:                        nodePop(ctxt);
                   4240:                        oldname = htmlnamePop(ctxt);
                   4241: #ifdef DEBUG
1.81      veillard 4242:                        xmlGenericError(xmlGenericErrorContext,
1.31      daniel   4243:                         "End of start tag problem: popping out %s\n", oldname);
                   4244: #endif
                   4245:                        if (oldname != NULL)
                   4246:                            xmlFree(oldname);
                   4247:                    }    
                   4248: 
                   4249:                    ctxt->instate = XML_PARSER_CONTENT;
                   4250: #ifdef DEBUG_PUSH
1.81      veillard 4251:                    xmlGenericError(xmlGenericErrorContext,
                   4252:                            "HPP: entering CONTENT\n");
1.31      daniel   4253: #endif
                   4254:                    break;
                   4255:                }
                   4256: 
                   4257:                /*
                   4258:                 * Check for an Empty Element from DTD definition
                   4259:                 */
                   4260:                if ((info != NULL) && (info->empty)) {
                   4261:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   4262:                        ctxt->sax->endElement(ctxt->userData, name);
                   4263:                    oldname = htmlnamePop(ctxt);
                   4264: #ifdef DEBUG
1.81      veillard 4265:                    xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
1.31      daniel   4266: #endif
                   4267:                    if (oldname != NULL)
                   4268:                        xmlFree(oldname);
                   4269:                }
                   4270:                ctxt->instate = XML_PARSER_CONTENT;
                   4271: #ifdef DEBUG_PUSH
1.81      veillard 4272:                xmlGenericError(xmlGenericErrorContext,
                   4273:                        "HPP: entering CONTENT\n");
1.31      daniel   4274: #endif
                   4275:                 break;
                   4276:            }
1.56      veillard 4277:             case XML_PARSER_CONTENT: {
                   4278:                long cons;
1.31      daniel   4279:                 /*
                   4280:                 * Handle preparsed entities and charRef
                   4281:                 */
                   4282:                if (ctxt->token != 0) {
1.47      daniel   4283:                    xmlChar chr[2] = { 0 , 0 } ;
1.31      daniel   4284: 
1.47      daniel   4285:                    chr[0] = (xmlChar) ctxt->token;
1.59      veillard 4286:                    htmlCheckParagraph(ctxt);
1.31      daniel   4287:                    if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1.47      daniel   4288:                        ctxt->sax->characters(ctxt->userData, chr, 1);
1.31      daniel   4289:                    ctxt->token = 0;
                   4290:                    ctxt->checkIndex = 0;
                   4291:                }
1.47      daniel   4292:                if ((avail == 1) && (terminate)) {
                   4293:                    cur = in->cur[0];
                   4294:                    if ((cur != '<') && (cur != '&')) {
1.48      daniel   4295:                        if (ctxt->sax != NULL) {
                   4296:                            if (IS_BLANK(cur)) {
                   4297:                                if (ctxt->sax->ignorableWhitespace != NULL)
                   4298:                                    ctxt->sax->ignorableWhitespace(
                   4299:                                            ctxt->userData, &cur, 1);
                   4300:                            } else {
1.59      veillard 4301:                                htmlCheckParagraph(ctxt);
1.48      daniel   4302:                                if (ctxt->sax->characters != NULL)
                   4303:                                    ctxt->sax->characters(
                   4304:                                            ctxt->userData, &cur, 1);
                   4305:                            }
                   4306:                        }
1.47      daniel   4307:                        ctxt->token = 0;
                   4308:                        ctxt->checkIndex = 0;
                   4309:                        NEXT;
                   4310:                    }
                   4311:                    break;
                   4312:                }
1.31      daniel   4313:                if (avail < 2)
                   4314:                    goto done;
                   4315:                cur = in->cur[0];
                   4316:                next = in->cur[1];
1.56      veillard 4317:                cons = ctxt->nbChars;
1.77      veillard 4318:                if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
                   4319:                    (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
                   4320:                    /*
                   4321:                     * Handle SCRIPT/STYLE separately
                   4322:                     */
1.59      veillard 4323:                    if ((!terminate) &&
1.77      veillard 4324:                        (htmlParseLookupSequence(ctxt, '<', '/', 0) < 0))
1.31      daniel   4325:                        goto done;
1.77      veillard 4326:                    htmlParseScript(ctxt);
                   4327:                    if ((cur == '<') && (next == '/')) {
                   4328:                        ctxt->instate = XML_PARSER_END_TAG;
                   4329:                        ctxt->checkIndex = 0;
1.31      daniel   4330: #ifdef DEBUG_PUSH
1.81      veillard 4331:                        xmlGenericError(xmlGenericErrorContext,
                   4332:                                "HPP: entering END_TAG\n");
1.31      daniel   4333: #endif
1.77      veillard 4334:                        break;
                   4335:                    }
                   4336:                } else {
                   4337:                    /*
                   4338:                     * Sometimes DOCTYPE arrives in the middle of the document
                   4339:                     */
                   4340:                    if ((cur == '<') && (next == '!') &&
                   4341:                        (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   4342:                        (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   4343:                        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   4344:                        (UPP(8) == 'E')) {
                   4345:                        if ((!terminate) &&
                   4346:                            (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
                   4347:                            goto done;
                   4348:                        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4349:                            ctxt->sax->error(ctxt->userData,
                   4350:                                 "Misplaced DOCTYPE declaration\n");
                   4351:                        ctxt->wellFormed = 0;
                   4352:                        htmlParseDocTypeDecl(ctxt);
                   4353:                    } else if ((cur == '<') && (next == '!') &&
                   4354:                        (in->cur[2] == '-') && (in->cur[3] == '-')) {
                   4355:                        if ((!terminate) &&
                   4356:                            (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
                   4357:                            goto done;
1.31      daniel   4358: #ifdef DEBUG_PUSH
1.81      veillard 4359:                        xmlGenericError(xmlGenericErrorContext,
                   4360:                                "HPP: Parsing Comment\n");
1.31      daniel   4361: #endif
1.77      veillard 4362:                        htmlParseComment(ctxt);
                   4363:                        ctxt->instate = XML_PARSER_CONTENT;
                   4364:                    } else if ((cur == '<') && (next == '!') && (avail < 4)) {
                   4365:                        goto done;
                   4366:                    } else if ((cur == '<') && (next == '/')) {
                   4367:                        ctxt->instate = XML_PARSER_END_TAG;
                   4368:                        ctxt->checkIndex = 0;
1.31      daniel   4369: #ifdef DEBUG_PUSH
1.81      veillard 4370:                        xmlGenericError(xmlGenericErrorContext,
                   4371:                                "HPP: entering END_TAG\n");
1.31      daniel   4372: #endif
1.77      veillard 4373:                        break;
                   4374:                    } else if (cur == '<') {
                   4375:                        ctxt->instate = XML_PARSER_START_TAG;
                   4376:                        ctxt->checkIndex = 0;
1.31      daniel   4377: #ifdef DEBUG_PUSH
1.81      veillard 4378:                        xmlGenericError(xmlGenericErrorContext,
                   4379:                                "HPP: entering START_TAG\n");
1.31      daniel   4380: #endif
1.77      veillard 4381:                        break;
                   4382:                    } else if (cur == '&') {
1.32      daniel   4383:                        if ((!terminate) &&
1.77      veillard 4384:                            (htmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
1.31      daniel   4385:                            goto done;
                   4386: #ifdef DEBUG_PUSH
1.81      veillard 4387:                        xmlGenericError(xmlGenericErrorContext,
                   4388:                                "HPP: Parsing Reference\n");
1.77      veillard 4389: #endif
                   4390:                        /* TODO: check generation of subtrees if noent !!! */
                   4391:                        htmlParseReference(ctxt);
                   4392:                    } else {
                   4393:                        /* TODO Avoid the extra copy, handle directly !!!!!! */
                   4394:                        /*
                   4395:                         * Goal of the following test is :
                   4396:                         *  - minimize calls to the SAX 'character' callback
                   4397:                         *    when they are mergeable
                   4398:                         */
                   4399:                        if ((ctxt->inputNr == 1) &&
                   4400:                            (avail < HTML_PARSER_BIG_BUFFER_SIZE)) {
                   4401:                            if ((!terminate) &&
                   4402:                                (htmlParseLookupSequence(ctxt, '<', 0, 0) < 0))
                   4403:                                goto done;
                   4404:                        }
                   4405:                        ctxt->checkIndex = 0;
                   4406: #ifdef DEBUG_PUSH
1.81      veillard 4407:                        xmlGenericError(xmlGenericErrorContext,
                   4408:                                "HPP: Parsing char data\n");
1.31      daniel   4409: #endif
1.77      veillard 4410:                        htmlParseCharData(ctxt, 0);
                   4411:                    }
1.31      daniel   4412:                }
1.56      veillard 4413:                if (cons == ctxt->nbChars) {
                   4414:                    if (ctxt->node != NULL) {
                   4415:                        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4416:                            ctxt->sax->error(ctxt->userData,
                   4417:                                 "detected an error in element content\n");
                   4418:                        ctxt->wellFormed = 0;
                   4419:                    }
1.70      veillard 4420:                    NEXT;
1.56      veillard 4421:                    break;
                   4422:                }
                   4423: 
1.31      daniel   4424:                break;
1.56      veillard 4425:            }
1.31      daniel   4426:             case XML_PARSER_END_TAG:
                   4427:                if (avail < 2)
                   4428:                    goto done;
1.32      daniel   4429:                if ((!terminate) &&
                   4430:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   4431:                    goto done;
                   4432:                htmlParseEndTag(ctxt);
                   4433:                if (ctxt->nameNr == 0) {
                   4434:                    ctxt->instate = XML_PARSER_EPILOG;
                   4435:                } else {
                   4436:                    ctxt->instate = XML_PARSER_CONTENT;
                   4437:                }
                   4438:                ctxt->checkIndex = 0;
                   4439: #ifdef DEBUG_PUSH
1.81      veillard 4440:                xmlGenericError(xmlGenericErrorContext,
                   4441:                        "HPP: entering CONTENT\n");
1.31      daniel   4442: #endif
                   4443:                break;
                   4444:             case XML_PARSER_CDATA_SECTION:
1.81      veillard 4445:                xmlGenericError(xmlGenericErrorContext,
                   4446:                        "HPP: internal error, state == CDATA\n");
1.31      daniel   4447:                ctxt->instate = XML_PARSER_CONTENT;
                   4448:                ctxt->checkIndex = 0;
                   4449: #ifdef DEBUG_PUSH
1.81      veillard 4450:                xmlGenericError(xmlGenericErrorContext,
                   4451:                        "HPP: entering CONTENT\n");
1.31      daniel   4452: #endif
                   4453:                break;
                   4454:             case XML_PARSER_DTD:
1.81      veillard 4455:                xmlGenericError(xmlGenericErrorContext,
                   4456:                        "HPP: internal error, state == DTD\n");
1.31      daniel   4457:                ctxt->instate = XML_PARSER_CONTENT;
                   4458:                ctxt->checkIndex = 0;
                   4459: #ifdef DEBUG_PUSH
1.81      veillard 4460:                xmlGenericError(xmlGenericErrorContext,
                   4461:                        "HPP: entering CONTENT\n");
1.31      daniel   4462: #endif
                   4463:                break;
                   4464:             case XML_PARSER_COMMENT:
1.81      veillard 4465:                xmlGenericError(xmlGenericErrorContext,
                   4466:                        "HPP: internal error, state == COMMENT\n");
1.31      daniel   4467:                ctxt->instate = XML_PARSER_CONTENT;
                   4468:                ctxt->checkIndex = 0;
                   4469: #ifdef DEBUG_PUSH
1.81      veillard 4470:                xmlGenericError(xmlGenericErrorContext,
                   4471:                        "HPP: entering CONTENT\n");
1.31      daniel   4472: #endif
                   4473:                break;
                   4474:             case XML_PARSER_PI:
1.81      veillard 4475:                xmlGenericError(xmlGenericErrorContext,
                   4476:                        "HPP: internal error, state == PI\n");
1.31      daniel   4477:                ctxt->instate = XML_PARSER_CONTENT;
                   4478:                ctxt->checkIndex = 0;
                   4479: #ifdef DEBUG_PUSH
1.81      veillard 4480:                xmlGenericError(xmlGenericErrorContext,
                   4481:                        "HPP: entering CONTENT\n");
1.31      daniel   4482: #endif
                   4483:                break;
                   4484:             case XML_PARSER_ENTITY_DECL:
1.81      veillard 4485:                xmlGenericError(xmlGenericErrorContext,
                   4486:                        "HPP: internal error, state == ENTITY_DECL\n");
1.31      daniel   4487:                ctxt->instate = XML_PARSER_CONTENT;
                   4488:                ctxt->checkIndex = 0;
                   4489: #ifdef DEBUG_PUSH
1.81      veillard 4490:                xmlGenericError(xmlGenericErrorContext,
                   4491:                        "HPP: entering CONTENT\n");
1.31      daniel   4492: #endif
                   4493:                break;
                   4494:             case XML_PARSER_ENTITY_VALUE:
1.81      veillard 4495:                xmlGenericError(xmlGenericErrorContext,
                   4496:                        "HPP: internal error, state == ENTITY_VALUE\n");
1.31      daniel   4497:                ctxt->instate = XML_PARSER_CONTENT;
                   4498:                ctxt->checkIndex = 0;
                   4499: #ifdef DEBUG_PUSH
1.81      veillard 4500:                xmlGenericError(xmlGenericErrorContext,
                   4501:                        "HPP: entering DTD\n");
1.31      daniel   4502: #endif
                   4503:                break;
                   4504:             case XML_PARSER_ATTRIBUTE_VALUE:
1.81      veillard 4505:                xmlGenericError(xmlGenericErrorContext,
                   4506:                        "HPP: internal error, state == ATTRIBUTE_VALUE\n");
1.31      daniel   4507:                ctxt->instate = XML_PARSER_START_TAG;
                   4508:                ctxt->checkIndex = 0;
                   4509: #ifdef DEBUG_PUSH
1.81      veillard 4510:                xmlGenericError(xmlGenericErrorContext,
                   4511:                        "HPP: entering START_TAG\n");
1.53      veillard 4512: #endif
                   4513:                break;
                   4514:            case XML_PARSER_SYSTEM_LITERAL:
1.81      veillard 4515:                xmlGenericError(xmlGenericErrorContext,
                   4516:                        "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
1.53      veillard 4517:                ctxt->instate = XML_PARSER_CONTENT;
                   4518:                ctxt->checkIndex = 0;
                   4519: #ifdef DEBUG_PUSH
1.81      veillard 4520:                xmlGenericError(xmlGenericErrorContext,
                   4521:                        "HPP: entering CONTENT\n");
1.31      daniel   4522: #endif
                   4523:                break;
                   4524:        }
                   4525:     }
                   4526: done:    
1.47      daniel   4527:     if ((avail == 0) && (terminate)) {
                   4528:        htmlAutoClose(ctxt, NULL);
1.54      veillard 4529:        if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { 
                   4530:            /*
                   4531:             * SAX: end of the document processing.
                   4532:             */
1.47      daniel   4533:            ctxt->instate = XML_PARSER_EOF;
1.54      veillard 4534:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4535:                ctxt->sax->endDocument(ctxt->userData);
                   4536:        }
1.59      veillard 4537:     }
                   4538:     if ((ctxt->myDoc != NULL) &&
                   4539:        ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
                   4540:         (ctxt->instate == XML_PARSER_EPILOG))) {
                   4541:        xmlDtdPtr dtd;
                   4542:        dtd = xmlGetIntSubset(ctxt->myDoc);
                   4543:        if (dtd == NULL)
                   4544:            ctxt->myDoc->intSubset = 
                   4545:                xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "HTML", 
                   4546:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   4547:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
1.47      daniel   4548:     }
1.31      daniel   4549: #ifdef DEBUG_PUSH
1.81      veillard 4550:     xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
1.31      daniel   4551: #endif
                   4552:     return(ret);
                   4553: }
                   4554: 
                   4555: /**
1.32      daniel   4556:  * htmlParseTry:
                   4557:  * @ctxt:  an HTML parser context
                   4558:  *
                   4559:  * Try to progress on parsing
                   4560:  *
                   4561:  * Returns zero if no parsing was possible
                   4562:  */
                   4563: int
                   4564: htmlParseTry(htmlParserCtxtPtr ctxt) {
                   4565:     return(htmlParseTryOrFinish(ctxt, 0));
                   4566: }
                   4567: 
                   4568: /**
1.31      daniel   4569:  * htmlParseChunk:
                   4570:  * @ctxt:  an XML parser context
                   4571:  * @chunk:  an char array
                   4572:  * @size:  the size in byte of the chunk
                   4573:  * @terminate:  last chunk indicator
                   4574:  *
                   4575:  * Parse a Chunk of memory
                   4576:  *
                   4577:  * Returns zero if no error, the xmlParserErrors otherwise.
                   4578:  */
                   4579: int
                   4580: htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
                   4581:               int terminate) {
                   4582:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   4583:         (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
                   4584:        int base = ctxt->input->base - ctxt->input->buf->buffer->content;
                   4585:        int cur = ctxt->input->cur - ctxt->input->base;
                   4586:        
                   4587:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   4588:        ctxt->input->base = ctxt->input->buf->buffer->content + base;
                   4589:        ctxt->input->cur = ctxt->input->base + cur;
                   4590: #ifdef DEBUG_PUSH
1.81      veillard 4591:        xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
1.31      daniel   4592: #endif
                   4593: 
1.34      daniel   4594:        if ((terminate) || (ctxt->input->buf->buffer->use > 80))
                   4595:            htmlParseTryOrFinish(ctxt, terminate);
1.60      veillard 4596:     } else if (ctxt->instate != XML_PARSER_EOF) {
                   4597:        xmlParserInputBufferPush(ctxt->input->buf, 0, "");
1.32      daniel   4598:         htmlParseTryOrFinish(ctxt, terminate);
1.60      veillard 4599:     }
1.31      daniel   4600:     if (terminate) {
                   4601:        if ((ctxt->instate != XML_PARSER_EOF) &&
                   4602:            (ctxt->instate != XML_PARSER_EPILOG) &&
                   4603:            (ctxt->instate != XML_PARSER_MISC)) {
1.67      veillard 4604:            ctxt->errNo = XML_ERR_DOCUMENT_END;
1.31      daniel   4605:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   4606:                ctxt->sax->error(ctxt->userData,
                   4607:                    "Extra content at the end of the document\n");
                   4608:            ctxt->wellFormed = 0;
                   4609:        } 
                   4610:        if (ctxt->instate != XML_PARSER_EOF) {
                   4611:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   4612:                ctxt->sax->endDocument(ctxt->userData);
                   4613:        }
                   4614:        ctxt->instate = XML_PARSER_EOF;
                   4615:     }
                   4616:     return((xmlParserErrors) ctxt->errNo);           
                   4617: }
                   4618: 
                   4619: /************************************************************************
                   4620:  *                                                                     *
                   4621:  *                     User entry points                               *
                   4622:  *                                                                     *
                   4623:  ************************************************************************/
                   4624: 
                   4625: /**
                   4626:  * htmlCreatePushParserCtxt :
                   4627:  * @sax:  a SAX handler
                   4628:  * @user_data:  The user data returned on SAX callbacks
                   4629:  * @chunk:  a pointer to an array of chars
                   4630:  * @size:  number of chars in the array
                   4631:  * @filename:  an optional file name or URI
                   4632:  * @enc:  an optional encoding
                   4633:  *
                   4634:  * Create a parser context for using the HTML parser in push mode
                   4635:  * To allow content encoding detection, @size should be >= 4
                   4636:  * The value of @filename is used for fetching external entities
                   4637:  * and error/warning reports.
                   4638:  *
                   4639:  * Returns the new parser context or NULL
                   4640:  */
                   4641: htmlParserCtxtPtr
                   4642: htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, 
                   4643:                          const char *chunk, int size, const char *filename,
                   4644:                         xmlCharEncoding enc) {
                   4645:     htmlParserCtxtPtr ctxt;
                   4646:     htmlParserInputPtr inputStream;
                   4647:     xmlParserInputBufferPtr buf;
                   4648: 
                   4649:     buf = xmlAllocParserInputBuffer(enc);
                   4650:     if (buf == NULL) return(NULL);
                   4651: 
                   4652:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
                   4653:     if (ctxt == NULL) {
                   4654:        xmlFree(buf);
                   4655:        return(NULL);
                   4656:     }
                   4657:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   4658:     htmlInitParserCtxt(ctxt);
                   4659:     if (sax != NULL) {
                   4660:        if (ctxt->sax != &htmlDefaultSAXHandler)
                   4661:            xmlFree(ctxt->sax);
                   4662:        ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
                   4663:        if (ctxt->sax == NULL) {
                   4664:            xmlFree(buf);
                   4665:            xmlFree(ctxt);
                   4666:            return(NULL);
                   4667:        }
                   4668:        memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
                   4669:        if (user_data != NULL)
                   4670:            ctxt->userData = user_data;
                   4671:     }  
                   4672:     if (filename == NULL) {
                   4673:        ctxt->directory = NULL;
                   4674:     } else {
                   4675:         ctxt->directory = xmlParserGetDirectory(filename);
                   4676:     }
                   4677: 
                   4678:     inputStream = htmlNewInputStream(ctxt);
                   4679:     if (inputStream == NULL) {
                   4680:        xmlFreeParserCtxt(ctxt);
                   4681:        return(NULL);
                   4682:     }
                   4683: 
                   4684:     if (filename == NULL)
                   4685:        inputStream->filename = NULL;
                   4686:     else
                   4687:        inputStream->filename = xmlMemStrdup(filename);
                   4688:     inputStream->buf = buf;
                   4689:     inputStream->base = inputStream->buf->buffer->content;
                   4690:     inputStream->cur = inputStream->buf->buffer->content;
                   4691: 
                   4692:     inputPush(ctxt, inputStream);
                   4693: 
                   4694:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   4695:         (ctxt->input->buf != NULL))  {       
                   4696:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   4697: #ifdef DEBUG_PUSH
1.81      veillard 4698:        xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
1.31      daniel   4699: #endif
                   4700:     }
                   4701: 
                   4702:     return(ctxt);
                   4703: }
1.1       daniel   4704: 
                   4705: /**
                   4706:  * htmlSAXParseDoc :
1.14      daniel   4707:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   4708:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4709:  * @sax:  the SAX handler block
                   4710:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   4711:  *
                   4712:  * parse an HTML in-memory document and build a tree.
                   4713:  * It use the given SAX function block to handle the parsing callback.
                   4714:  * If sax is NULL, fallback to the default DOM tree building routines.
                   4715:  * 
                   4716:  * Returns the resulting document tree
                   4717:  */
                   4718: 
                   4719: htmlDocPtr
1.14      daniel   4720: htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
1.1       daniel   4721:     htmlDocPtr ret;
                   4722:     htmlParserCtxtPtr ctxt;
                   4723: 
                   4724:     if (cur == NULL) return(NULL);
                   4725: 
                   4726: 
                   4727:     ctxt = htmlCreateDocParserCtxt(cur, encoding);
                   4728:     if (ctxt == NULL) return(NULL);
                   4729:     if (sax != NULL) { 
                   4730:         ctxt->sax = sax;
                   4731:         ctxt->userData = userData;
                   4732:     }
                   4733: 
                   4734:     htmlParseDocument(ctxt);
                   4735:     ret = ctxt->myDoc;
                   4736:     if (sax != NULL) {
                   4737:        ctxt->sax = NULL;
                   4738:        ctxt->userData = NULL;
                   4739:     }
                   4740:     htmlFreeParserCtxt(ctxt);
                   4741:     
                   4742:     return(ret);
                   4743: }
                   4744: 
                   4745: /**
                   4746:  * htmlParseDoc :
1.14      daniel   4747:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   4748:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4749:  *
                   4750:  * parse an HTML in-memory document and build a tree.
                   4751:  * 
                   4752:  * Returns the resulting document tree
                   4753:  */
                   4754: 
                   4755: htmlDocPtr
1.14      daniel   4756: htmlParseDoc(xmlChar *cur, const char *encoding) {
1.1       daniel   4757:     return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
                   4758: }
                   4759: 
                   4760: 
                   4761: /**
                   4762:  * htmlCreateFileParserCtxt :
                   4763:  * @filename:  the filename
                   4764:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4765:  *
                   4766:  * Create a parser context for a file content. 
                   4767:  * Automatic support for ZLIB/Compress compressed document is provided
                   4768:  * by default if found at compile-time.
                   4769:  *
                   4770:  * Returns the new parser context or NULL
                   4771:  */
                   4772: htmlParserCtxtPtr
                   4773: htmlCreateFileParserCtxt(const char *filename, const char *encoding)
                   4774: {
                   4775:     htmlParserCtxtPtr ctxt;
                   4776:     htmlParserInputPtr inputStream;
1.5       daniel   4777:     xmlParserInputBufferPtr buf;
1.1       daniel   4778:     /* htmlCharEncoding enc; */
                   4779: 
1.5       daniel   4780:     buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
                   4781:     if (buf == NULL) return(NULL);
1.1       daniel   4782: 
1.11      daniel   4783:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   4784:     if (ctxt == NULL) {
                   4785:         perror("malloc");
                   4786:        return(NULL);
                   4787:     }
1.19      daniel   4788:     memset(ctxt, 0, sizeof(htmlParserCtxt));
1.1       daniel   4789:     htmlInitParserCtxt(ctxt);
1.11      daniel   4790:     inputStream = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   4791:     if (inputStream == NULL) {
                   4792:         perror("malloc");
1.11      daniel   4793:        xmlFree(ctxt);
1.1       daniel   4794:        return(NULL);
                   4795:     }
1.19      daniel   4796:     memset(inputStream, 0, sizeof(htmlParserInput));
1.1       daniel   4797: 
1.11      daniel   4798:     inputStream->filename = xmlMemStrdup(filename);
1.1       daniel   4799:     inputStream->line = 1;
                   4800:     inputStream->col = 1;
1.5       daniel   4801:     inputStream->buf = buf;
1.21      daniel   4802:     inputStream->directory = NULL;
1.1       daniel   4803: 
1.5       daniel   4804:     inputStream->base = inputStream->buf->buffer->content;
                   4805:     inputStream->cur = inputStream->buf->buffer->content;
                   4806:     inputStream->free = NULL;
1.1       daniel   4807: 
                   4808:     inputPush(ctxt, inputStream);
                   4809:     return(ctxt);
                   4810: }
                   4811: 
                   4812: /**
                   4813:  * htmlSAXParseFile :
                   4814:  * @filename:  the filename
                   4815:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4816:  * @sax:  the SAX handler block
                   4817:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   4818:  *
                   4819:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   4820:  * compressed document is provided by default if found at compile-time.
                   4821:  * It use the given SAX function block to handle the parsing callback.
                   4822:  * If sax is NULL, fallback to the default DOM tree building routines.
                   4823:  *
                   4824:  * Returns the resulting document tree
                   4825:  */
                   4826: 
                   4827: htmlDocPtr
                   4828: htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, 
                   4829:                  void *userData) {
                   4830:     htmlDocPtr ret;
                   4831:     htmlParserCtxtPtr ctxt;
1.57      veillard 4832:     htmlSAXHandlerPtr oldsax = NULL;
1.1       daniel   4833: 
                   4834:     ctxt = htmlCreateFileParserCtxt(filename, encoding);
                   4835:     if (ctxt == NULL) return(NULL);
                   4836:     if (sax != NULL) {
1.55      veillard 4837:        oldsax = ctxt->sax;
1.1       daniel   4838:         ctxt->sax = sax;
                   4839:         ctxt->userData = userData;
                   4840:     }
                   4841: 
                   4842:     htmlParseDocument(ctxt);
                   4843: 
                   4844:     ret = ctxt->myDoc;
                   4845:     if (sax != NULL) {
1.55      veillard 4846:         ctxt->sax = oldsax;
1.1       daniel   4847:         ctxt->userData = NULL;
                   4848:     }
                   4849:     htmlFreeParserCtxt(ctxt);
                   4850:     
                   4851:     return(ret);
                   4852: }
                   4853: 
                   4854: /**
                   4855:  * htmlParseFile :
                   4856:  * @filename:  the filename
                   4857:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   4858:  *
                   4859:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   4860:  * compressed document is provided by default if found at compile-time.
                   4861:  *
                   4862:  * Returns the resulting document tree
                   4863:  */
                   4864: 
                   4865: htmlDocPtr
                   4866: htmlParseFile(const char *filename, const char *encoding) {
                   4867:     return(htmlSAXParseFile(filename, encoding, NULL, NULL));
                   4868: }
1.39      daniel   4869: 
                   4870: #endif /* LIBXML_HTML_ENABLED */

Webmaster