Coverage Report

Created: 2023-11-19 06:13

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