Annotation of XML/HTMLparser.c, revision 1.82

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

Webmaster