2 * HTMLparser.c : an HTML 4.0 non-verifying parser
4 * See Copyright for the status of this software.
11 #ifdef LIBXML_HTML_ENABLED
20 #ifdef HAVE_SYS_STAT_H
29 #ifdef LIBXML_ZLIB_ENABLED
33 #include <libxml/xmlmemory.h>
34 #include <libxml/tree.h>
35 #include <libxml/parser.h>
36 #include <libxml/parserInternals.h>
37 #include <libxml/xmlerror.h>
38 #include <libxml/HTMLparser.h>
39 #include <libxml/HTMLtree.h>
40 #include <libxml/entities.h>
41 #include <libxml/encoding.h>
42 #include <libxml/valid.h>
43 #include <libxml/xmlIO.h>
44 #include <libxml/globals.h>
45 #include <libxml/uri.h>
50 #define HTML_MAX_NAMELEN 1000
51 #define HTML_PARSER_BIG_BUFFER_SIZE 1000
52 #define HTML_PARSER_BUFFER_SIZE 100
55 /* #define DEBUG_PUSH */
57 static int htmlOmittedDefaultValue
= 1;
59 xmlChar
* htmlDecodeEntities(htmlParserCtxtPtr ctxt
, int len
,
60 xmlChar end
, xmlChar end2
, xmlChar end3
);
61 static void htmlParseComment(htmlParserCtxtPtr ctxt
);
63 /************************************************************************
65 * Some factorized error routines *
67 ************************************************************************/
71 * @ctxt: an HTML parser context
72 * @extra: extra information
74 * Handle a redefinition of attribute error
77 htmlErrMemory(xmlParserCtxtPtr ctxt
, const char *extra
)
79 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
80 (ctxt
->instate
== XML_PARSER_EOF
))
83 ctxt
->errNo
= XML_ERR_NO_MEMORY
;
84 ctxt
->instate
= XML_PARSER_EOF
;
88 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, NULL
, XML_FROM_PARSER
,
89 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0, extra
,
91 "Memory allocation failed : %s\n", extra
);
93 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, NULL
, XML_FROM_PARSER
,
94 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0, NULL
,
95 NULL
, NULL
, 0, 0, "Memory allocation failed\n");
100 * @ctxt: an HTML parser context
101 * @error: the error number
102 * @msg: the error message
103 * @str1: string infor
104 * @str2: string infor
106 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
108 static void LIBXML_ATTR_FORMAT(3,0)
109 htmlParseErr(xmlParserCtxtPtr ctxt
, xmlParserErrors error
,
110 const char *msg
, const xmlChar
*str1
, const xmlChar
*str2
)
112 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
113 (ctxt
->instate
== XML_PARSER_EOF
))
117 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, NULL
, XML_FROM_HTML
, error
,
118 XML_ERR_ERROR
, NULL
, 0,
119 (const char *) str1
, (const char *) str2
,
123 ctxt
->wellFormed
= 0;
128 * @ctxt: an HTML parser context
129 * @error: the error number
130 * @msg: the error message
133 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
135 static void LIBXML_ATTR_FORMAT(3,0)
136 htmlParseErrInt(xmlParserCtxtPtr ctxt
, xmlParserErrors error
,
137 const char *msg
, int val
)
139 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
140 (ctxt
->instate
== XML_PARSER_EOF
))
144 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, NULL
, XML_FROM_HTML
, error
,
145 XML_ERR_ERROR
, NULL
, 0, NULL
, NULL
,
146 NULL
, val
, 0, msg
, val
);
148 ctxt
->wellFormed
= 0;
151 /************************************************************************
153 * Parser stacks related functions and macros *
155 ************************************************************************/
159 * @ctxt: an HTML parser context
160 * @value: the element name
162 * Pushes a new element name on top of the name stack
164 * Returns 0 in case of error, the index in the stack otherwise
167 htmlnamePush(htmlParserCtxtPtr ctxt
, const xmlChar
* value
)
169 if ((ctxt
->html
< 3) && (xmlStrEqual(value
, BAD_CAST
"head")))
171 if ((ctxt
->html
< 10) && (xmlStrEqual(value
, BAD_CAST
"body")))
173 if (ctxt
->nameNr
>= ctxt
->nameMax
) {
175 ctxt
->nameTab
= (const xmlChar
* *)
176 xmlRealloc((xmlChar
* *)ctxt
->nameTab
,
178 sizeof(ctxt
->nameTab
[0]));
179 if (ctxt
->nameTab
== NULL
) {
180 htmlErrMemory(ctxt
, NULL
);
184 ctxt
->nameTab
[ctxt
->nameNr
] = value
;
186 return (ctxt
->nameNr
++);
190 * @ctxt: an HTML parser context
192 * Pops the top element name from the name stack
194 * Returns the name just removed
196 static const xmlChar
*
197 htmlnamePop(htmlParserCtxtPtr ctxt
)
201 if (ctxt
->nameNr
<= 0)
204 if (ctxt
->nameNr
< 0)
206 if (ctxt
->nameNr
> 0)
207 ctxt
->name
= ctxt
->nameTab
[ctxt
->nameNr
- 1];
210 ret
= ctxt
->nameTab
[ctxt
->nameNr
];
211 ctxt
->nameTab
[ctxt
->nameNr
] = NULL
;
217 * @ctxt: an HTML parser context
218 * @value: the node info
220 * Pushes a new element name on top of the node info stack
222 * Returns 0 in case of error, the index in the stack otherwise
225 htmlNodeInfoPush(htmlParserCtxtPtr ctxt
, htmlParserNodeInfo
*value
)
227 if (ctxt
->nodeInfoNr
>= ctxt
->nodeInfoMax
) {
228 if (ctxt
->nodeInfoMax
== 0)
229 ctxt
->nodeInfoMax
= 5;
230 ctxt
->nodeInfoMax
*= 2;
231 ctxt
->nodeInfoTab
= (htmlParserNodeInfo
*)
232 xmlRealloc((htmlParserNodeInfo
*)ctxt
->nodeInfoTab
,
234 sizeof(ctxt
->nodeInfoTab
[0]));
235 if (ctxt
->nodeInfoTab
== NULL
) {
236 htmlErrMemory(ctxt
, NULL
);
240 ctxt
->nodeInfoTab
[ctxt
->nodeInfoNr
] = *value
;
241 ctxt
->nodeInfo
= &ctxt
->nodeInfoTab
[ctxt
->nodeInfoNr
];
242 return (ctxt
->nodeInfoNr
++);
247 * @ctxt: an HTML parser context
249 * Pops the top element name from the node info stack
251 * Returns 0 in case of error, the pointer to NodeInfo otherwise
253 static htmlParserNodeInfo
*
254 htmlNodeInfoPop(htmlParserCtxtPtr ctxt
)
256 if (ctxt
->nodeInfoNr
<= 0)
259 if (ctxt
->nodeInfoNr
< 0)
261 if (ctxt
->nodeInfoNr
> 0)
262 ctxt
->nodeInfo
= &ctxt
->nodeInfoTab
[ctxt
->nodeInfoNr
- 1];
264 ctxt
->nodeInfo
= NULL
;
265 return &ctxt
->nodeInfoTab
[ctxt
->nodeInfoNr
];
269 * Macros for accessing the content. Those should be used only by the parser,
272 * Dirty macros, i.e. one need to make assumption on the context to use them
274 * CUR_PTR return the current pointer to the xmlChar to be parsed.
275 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
276 * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
277 * in UNICODE mode. This should be used internally by the parser
278 * only to compare to ASCII values otherwise it would break when
279 * running with UTF-8 encoding.
280 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
281 * to compare on ASCII based substring.
282 * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
283 * it should be used only to compare on ASCII based substring.
284 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
285 * strings without newlines within the parser.
287 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
289 * CURRENT Returns the current char value, with the full decoding of
290 * UTF-8 if we are using this mode. It returns an int.
291 * NEXT Skip to the next character, this does the proper decoding
292 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
293 * NEXTL(l) Skip the current unicode character of l xmlChars long.
294 * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
297 #define UPPER (toupper(*ctxt->input->cur))
299 #define SKIP(val) ctxt->input->cur += (val),ctxt->input->col+=(val)
301 #define NXT(val) ctxt->input->cur[(val)]
303 #define UPP(val) (toupper(ctxt->input->cur[(val)]))
305 #define CUR_PTR ctxt->input->cur
306 #define BASE_PTR ctxt->input->base
308 #define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
309 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
310 xmlParserInputShrink(ctxt->input)
312 #define GROW if ((ctxt->progressive == 0) && \
313 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
314 xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
316 #define CURRENT ((int) (*ctxt->input->cur))
318 #define SKIP_BLANKS htmlSkipBlankChars(ctxt)
320 /* Imported from XML */
322 /* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
323 #define CUR ((int) (*ctxt->input->cur))
324 #define NEXT xmlNextChar(ctxt)
326 #define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
329 #define NEXTL(l) do { \
330 if (*(ctxt->input->cur) == '\n') { \
331 ctxt->input->line++; ctxt->input->col = 1; \
332 } else ctxt->input->col++; \
333 ctxt->token = 0; ctxt->input->cur += l; \
338 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
339 if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
342 #define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
343 #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
345 #define COPY_BUF(l,b,i,v) \
346 if (l == 1) b[i++] = (xmlChar) v; \
347 else i += xmlCopyChar(l,&b[i],v)
351 * @the HTML parser context
353 * Ty to find and encoding in the current data available in the input
354 * buffer this is needed to try to switch to the proper encoding when
355 * one face a character error.
356 * That's an heuristic, since it's operating outside of parsing it could
357 * try to use a meta which had been commented out, that's the reason it
358 * should only be used in case of error, not as a default.
360 * Returns an encoding string or NULL if not found, the string need to
364 htmlFindEncoding(xmlParserCtxtPtr ctxt
) {
365 const xmlChar
*start
, *cur
, *end
;
367 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
) ||
368 (ctxt
->input
->encoding
!= NULL
) || (ctxt
->input
->buf
== NULL
) ||
369 (ctxt
->input
->buf
->encoder
!= NULL
))
371 if ((ctxt
->input
->cur
== NULL
) || (ctxt
->input
->end
== NULL
))
374 start
= ctxt
->input
->cur
;
375 end
= ctxt
->input
->end
;
376 /* we also expect the input buffer to be zero terminated */
380 cur
= xmlStrcasestr(start
, BAD_CAST
"HTTP-EQUIV");
383 cur
= xmlStrcasestr(cur
, BAD_CAST
"CONTENT");
386 cur
= xmlStrcasestr(cur
, BAD_CAST
"CHARSET=");
391 while (((*cur
>= 'A') && (*cur
<= 'Z')) ||
392 ((*cur
>= 'a') && (*cur
<= 'z')) ||
393 ((*cur
>= '0') && (*cur
<= '9')) ||
394 (*cur
== '-') || (*cur
== '_') || (*cur
== ':') || (*cur
== '/'))
398 return(xmlStrndup(start
, cur
- start
));
403 * @ctxt: the HTML parser context
404 * @len: pointer to the length of the char read
406 * The current char value, if using UTF-8 this may actually span multiple
407 * bytes in the input buffer. Implement the end of line normalization:
408 * 2.11 End-of-Line Handling
409 * If the encoding is unspecified, in the case we find an ISO-Latin-1
410 * char, then the encoding converter is plugged in automatically.
412 * Returns the current char value and its length
416 htmlCurrentChar(xmlParserCtxtPtr ctxt
, int *len
) {
417 const unsigned char *cur
;
421 if (ctxt
->instate
== XML_PARSER_EOF
)
424 if (ctxt
->token
!= 0) {
428 if (ctxt
->charset
!= XML_CHAR_ENCODING_UTF8
) {
430 xmlCharEncodingHandlerPtr handler
;
433 * Assume it's a fixed length encoding (1) with
434 * a compatible encoding for the ASCII set, since
435 * HTML constructs only use < 128 chars
437 if ((int) *ctxt
->input
->cur
< 0x80) {
439 if ((*ctxt
->input
->cur
== 0) &&
440 (ctxt
->input
->cur
< ctxt
->input
->end
)) {
441 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
442 "Char 0x%X out of allowed range\n", 0);
445 return((int) *ctxt
->input
->cur
);
449 * Humm this is bad, do an automatic flow conversion
451 guess
= htmlFindEncoding(ctxt
);
453 xmlSwitchEncoding(ctxt
, XML_CHAR_ENCODING_8859_1
);
455 if (ctxt
->input
->encoding
!= NULL
)
456 xmlFree((xmlChar
*) ctxt
->input
->encoding
);
457 ctxt
->input
->encoding
= guess
;
458 handler
= xmlFindCharEncodingHandler((const char *) guess
);
459 if (handler
!= NULL
) {
461 * Don't use UTF-8 encoder which isn't required and
462 * can produce invalid UTF-8.
464 if (!xmlStrEqual(BAD_CAST handler
->name
, BAD_CAST
"UTF-8"))
465 xmlSwitchToEncoding(ctxt
, handler
);
467 htmlParseErr(ctxt
, XML_ERR_INVALID_ENCODING
,
468 "Unsupported encoding %s", guess
, NULL
);
471 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
475 * We are supposed to handle UTF8, check it's valid
476 * From rfc2044: encoding of the Unicode values on UTF-8:
478 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
479 * 0000 0000-0000 007F 0xxxxxxx
480 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
481 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
483 * Check for the 0x110000 limit too
485 cur
= ctxt
->input
->cur
;
491 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
492 cur
= ctxt
->input
->cur
;
494 if ((cur
[1] & 0xc0) != 0x80)
496 if ((c
& 0xe0) == 0xe0) {
499 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
500 cur
= ctxt
->input
->cur
;
502 if ((cur
[2] & 0xc0) != 0x80)
504 if ((c
& 0xf0) == 0xf0) {
506 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
507 cur
= ctxt
->input
->cur
;
509 if (((c
& 0xf8) != 0xf0) ||
510 ((cur
[3] & 0xc0) != 0x80))
514 val
= (cur
[0] & 0x7) << 18;
515 val
|= (cur
[1] & 0x3f) << 12;
516 val
|= (cur
[2] & 0x3f) << 6;
517 val
|= cur
[3] & 0x3f;
523 val
= (cur
[0] & 0xf) << 12;
524 val
|= (cur
[1] & 0x3f) << 6;
525 val
|= cur
[2] & 0x3f;
532 val
= (cur
[0] & 0x1f) << 6;
533 val
|= cur
[1] & 0x3f;
538 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
539 "Char 0x%X out of allowed range\n", val
);
543 if ((*ctxt
->input
->cur
== 0) &&
544 (ctxt
->input
->cur
< ctxt
->input
->end
)) {
545 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
546 "Char 0x%X out of allowed range\n", 0);
552 return((int) *ctxt
->input
->cur
);
557 * If we detect an UTF8 error that probably mean that the
558 * input encoding didn't get properly advertised in the
559 * declaration header. Report the error and switch the encoding
560 * to ISO-Latin-1 (if you don't like this policy, just declare the
566 if (ctxt
->input
->end
- ctxt
->input
->cur
>= 4) {
567 snprintf(buffer
, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
568 ctxt
->input
->cur
[0], ctxt
->input
->cur
[1],
569 ctxt
->input
->cur
[2], ctxt
->input
->cur
[3]);
571 snprintf(buffer
, 149, "Bytes: 0x%02X\n", ctxt
->input
->cur
[0]);
573 htmlParseErr(ctxt
, XML_ERR_INVALID_ENCODING
,
574 "Input is not proper UTF-8, indicate encoding !\n",
575 BAD_CAST buffer
, NULL
);
579 * Don't switch encodings twice. Note that if there's an encoder, we
580 * shouldn't receive invalid UTF-8 anyway.
582 * Note that if ctxt->input->buf == NULL, switching encodings is
583 * impossible, see Gitlab issue #34.
585 if ((ctxt
->input
->buf
!= NULL
) &&
586 (ctxt
->input
->buf
->encoder
== NULL
))
587 xmlSwitchEncoding(ctxt
, XML_CHAR_ENCODING_8859_1
);
589 return((int) *ctxt
->input
->cur
);
593 * htmlSkipBlankChars:
594 * @ctxt: the HTML parser context
596 * skip all blanks character found at that point in the input streams.
598 * Returns the number of space chars skipped
602 htmlSkipBlankChars(xmlParserCtxtPtr ctxt
) {
605 while (IS_BLANK_CH(*(ctxt
->input
->cur
))) {
606 if ((*ctxt
->input
->cur
== 0) &&
607 (xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
) <= 0)) {
610 if (*(ctxt
->input
->cur
) == '\n') {
611 ctxt
->input
->line
++; ctxt
->input
->col
= 1;
612 } else ctxt
->input
->col
++;
614 if (*ctxt
->input
->cur
== 0)
615 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
625 /************************************************************************
627 * The list of HTML elements and their properties *
629 ************************************************************************/
632 * Start Tag: 1 means the start tag can be omitted
633 * End Tag: 1 means the end tag can be omitted
634 * 2 means it's forbidden (empty elements)
635 * 3 means the tag is stylistic and should be closed easily
636 * Depr: this element is deprecated
637 * DTD: 1 means that this element is valid only in the Loose DTD
638 * 2 means that this element is valid only in the Frameset DTD
640 * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
641 , subElements , impliedsubelt , Attributes, userdata
644 /* Definitions and a couple of vars for HTML Elements */
646 #define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small"
647 #define NB_FONTSTYLE 8
648 #define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym"
650 #define SPECIAL "a", "img", "applet", "embed", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe"
651 #define NB_SPECIAL 16
652 #define INLINE FONTSTYLE, PHRASE, SPECIAL, FORMCTRL
653 #define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL
654 #define BLOCK HEADING, LIST, "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address"
655 #define NB_BLOCK NB_HEADING + NB_LIST + 14
656 #define FORMCTRL "input", "select", "textarea", "label", "button"
657 #define NB_FORMCTRL 5
660 #define HEADING "h1", "h2", "h3", "h4", "h5", "h6"
662 #define LIST "ul", "ol", "dir", "menu"
665 #define NB_MODIFIER 0
666 #define FLOW BLOCK,INLINE
667 #define NB_FLOW NB_BLOCK + NB_INLINE
671 static const char* const html_flow
[] = { FLOW
, NULL
} ;
672 static const char* const html_inline
[] = { INLINE
, NULL
} ;
674 /* placeholders: elts with content but no subelements */
675 static const char* const html_pcdata
[] = { NULL
} ;
676 #define html_cdata html_pcdata
679 /* ... and for HTML Attributes */
681 #define COREATTRS "id", "class", "style", "title"
682 #define NB_COREATTRS 4
683 #define I18N "lang", "dir"
685 #define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"
687 #define ATTRS COREATTRS,I18N,EVENTS
688 #define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS
689 #define CELLHALIGN "align", "char", "charoff"
690 #define NB_CELLHALIGN 3
691 #define CELLVALIGN "valign"
692 #define NB_CELLVALIGN 1
694 static const char* const html_attrs
[] = { ATTRS
, NULL
} ;
695 static const char* const core_i18n_attrs
[] = { COREATTRS
, I18N
, NULL
} ;
696 static const char* const core_attrs
[] = { COREATTRS
, NULL
} ;
697 static const char* const i18n_attrs
[] = { I18N
, NULL
} ;
700 /* Other declarations that should go inline ... */
701 static const char* const a_attrs
[] = { ATTRS
, "charset", "type", "name",
702 "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
703 "tabindex", "onfocus", "onblur", NULL
} ;
704 static const char* const target_attr
[] = { "target", NULL
} ;
705 static const char* const rows_cols_attr
[] = { "rows", "cols", NULL
} ;
706 static const char* const alt_attr
[] = { "alt", NULL
} ;
707 static const char* const src_alt_attrs
[] = { "src", "alt", NULL
} ;
708 static const char* const href_attrs
[] = { "href", NULL
} ;
709 static const char* const clear_attrs
[] = { "clear", NULL
} ;
710 static const char* const inline_p
[] = { INLINE
, "p", NULL
} ;
712 static const char* const flow_param
[] = { FLOW
, "param", NULL
} ;
713 static const char* const applet_attrs
[] = { COREATTRS
, "codebase",
714 "archive", "alt", "name", "height", "width", "align",
715 "hspace", "vspace", NULL
} ;
716 static const char* const area_attrs
[] = { "shape", "coords", "href", "nohref",
717 "tabindex", "accesskey", "onfocus", "onblur", NULL
} ;
718 static const char* const basefont_attrs
[] =
719 { "id", "size", "color", "face", NULL
} ;
720 static const char* const quote_attrs
[] = { ATTRS
, "cite", NULL
} ;
721 static const char* const body_contents
[] = { FLOW
, "ins", "del", NULL
} ;
722 static const char* const body_attrs
[] = { ATTRS
, "onload", "onunload", NULL
} ;
723 static const char* const body_depr
[] = { "background", "bgcolor", "text",
724 "link", "vlink", "alink", NULL
} ;
725 static const char* const button_attrs
[] = { ATTRS
, "name", "value", "type",
726 "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL
} ;
729 static const char* const col_attrs
[] = { ATTRS
, "span", "width", CELLHALIGN
, CELLVALIGN
, NULL
} ;
730 static const char* const col_elt
[] = { "col", NULL
} ;
731 static const char* const edit_attrs
[] = { ATTRS
, "datetime", "cite", NULL
} ;
732 static const char* const compact_attrs
[] = { ATTRS
, "compact", NULL
} ;
733 static const char* const dl_contents
[] = { "dt", "dd", NULL
} ;
734 static const char* const compact_attr
[] = { "compact", NULL
} ;
735 static const char* const label_attr
[] = { "label", NULL
} ;
736 static const char* const fieldset_contents
[] = { FLOW
, "legend" } ;
737 static const char* const font_attrs
[] = { COREATTRS
, I18N
, "size", "color", "face" , NULL
} ;
738 static const char* const form_contents
[] = { HEADING
, LIST
, INLINE
, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL
} ;
739 static const char* const form_attrs
[] = { ATTRS
, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL
} ;
740 static const char* const frame_attrs
[] = { COREATTRS
, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL
} ;
741 static const char* const frameset_attrs
[] = { COREATTRS
, "rows", "cols", "onload", "onunload", NULL
} ;
742 static const char* const frameset_contents
[] = { "frameset", "frame", "noframes", NULL
} ;
743 static const char* const head_attrs
[] = { I18N
, "profile", NULL
} ;
744 static const char* const head_contents
[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL
} ;
745 static const char* const hr_depr
[] = { "align", "noshade", "size", "width", NULL
} ;
746 static const char* const version_attr
[] = { "version", NULL
} ;
747 static const char* const html_content
[] = { "head", "body", "frameset", NULL
} ;
748 static const char* const iframe_attrs
[] = { COREATTRS
, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL
} ;
749 static const char* const img_attrs
[] = { ATTRS
, "longdesc", "name", "height", "width", "usemap", "ismap", NULL
} ;
750 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
} ;
751 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
} ;
752 static const char* const prompt_attrs
[] = { COREATTRS
, I18N
, "prompt", NULL
} ;
753 static const char* const label_attrs
[] = { ATTRS
, "for", "accesskey", "onfocus", "onblur", NULL
} ;
754 static const char* const legend_attrs
[] = { ATTRS
, "accesskey", NULL
} ;
755 static const char* const align_attr
[] = { "align", NULL
} ;
756 static const char* const link_attrs
[] = { ATTRS
, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL
} ;
757 static const char* const map_contents
[] = { BLOCK
, "area", NULL
} ;
758 static const char* const name_attr
[] = { "name", NULL
} ;
759 static const char* const action_attr
[] = { "action", NULL
} ;
760 static const char* const blockli_elt
[] = { BLOCK
, "li", NULL
} ;
761 static const char* const meta_attrs
[] = { I18N
, "http-equiv", "name", "scheme", "charset", NULL
} ;
762 static const char* const content_attr
[] = { "content", NULL
} ;
763 static const char* const type_attr
[] = { "type", NULL
} ;
764 static const char* const noframes_content
[] = { "body", FLOW MODIFIER
, NULL
} ;
765 static const char* const object_contents
[] = { FLOW
, "param", NULL
} ;
766 static const char* const object_attrs
[] = { ATTRS
, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL
} ;
767 static const char* const object_depr
[] = { "align", "border", "hspace", "vspace", NULL
} ;
768 static const char* const ol_attrs
[] = { "type", "compact", "start", NULL
} ;
769 static const char* const option_elt
[] = { "option", NULL
} ;
770 static const char* const optgroup_attrs
[] = { ATTRS
, "disabled", NULL
} ;
771 static const char* const option_attrs
[] = { ATTRS
, "disabled", "label", "selected", "value", NULL
} ;
772 static const char* const param_attrs
[] = { "id", "value", "valuetype", "type", NULL
} ;
773 static const char* const width_attr
[] = { "width", NULL
} ;
774 static const char* const pre_content
[] = { PHRASE
, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL
} ;
775 static const char* const script_attrs
[] = { "charset", "src", "defer", "event", "for", NULL
} ;
776 static const char* const language_attr
[] = { "language", NULL
} ;
777 static const char* const select_content
[] = { "optgroup", "option", NULL
} ;
778 static const char* const select_attrs
[] = { ATTRS
, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL
} ;
779 static const char* const style_attrs
[] = { I18N
, "media", "title", NULL
} ;
780 static const char* const table_attrs
[] = { ATTRS
, "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL
} ;
781 static const char* const table_depr
[] = { "align", "bgcolor", NULL
} ;
782 static const char* const table_contents
[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL
} ;
783 static const char* const tr_elt
[] = { "tr", NULL
} ;
784 static const char* const talign_attrs
[] = { ATTRS
, CELLHALIGN
, CELLVALIGN
, NULL
} ;
785 static const char* const th_td_depr
[] = { "nowrap", "bgcolor", "width", "height", NULL
} ;
786 static const char* const th_td_attr
[] = { ATTRS
, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN
, CELLVALIGN
, NULL
} ;
787 static const char* const textarea_attrs
[] = { ATTRS
, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL
} ;
788 static const char* const tr_contents
[] = { "th", "td", NULL
} ;
789 static const char* const bgcolor_attr
[] = { "bgcolor", NULL
} ;
790 static const char* const li_elt
[] = { "li", NULL
} ;
791 static const char* const ul_depr
[] = { "type", "compact", NULL
} ;
792 static const char* const dir_attr
[] = { "dir", NULL
} ;
794 #define DECL (const char**)
796 static const htmlElemDesc
797 html40ElementTable
[] = {
798 { "a", 0, 0, 0, 0, 0, 0, 1, "anchor ",
799 DECL html_inline
, NULL
, DECL a_attrs
, DECL target_attr
, NULL
801 { "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
802 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
804 { "acronym", 0, 0, 0, 0, 0, 0, 1, "",
805 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
807 { "address", 0, 0, 0, 0, 0, 0, 0, "information on author ",
808 DECL inline_p
, NULL
, DECL html_attrs
, NULL
, NULL
810 { "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
811 DECL flow_param
, NULL
, NULL
, DECL applet_attrs
, NULL
813 { "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
814 EMPTY
, NULL
, DECL area_attrs
, DECL target_attr
, DECL alt_attr
816 { "b", 0, 3, 0, 0, 0, 0, 1, "bold text style",
817 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
819 { "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
820 EMPTY
, NULL
, NULL
, DECL target_attr
, DECL href_attrs
822 { "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
823 EMPTY
, NULL
, NULL
, DECL basefont_attrs
, NULL
825 { "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
826 DECL html_inline
, NULL
, DECL core_i18n_attrs
, NULL
, DECL dir_attr
828 { "big", 0, 3, 0, 0, 0, 0, 1, "large text style",
829 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
831 { "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
832 DECL html_flow
, NULL
, DECL quote_attrs
, NULL
, NULL
834 { "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
835 DECL body_contents
, "div" , DECL body_attrs
, DECL body_depr
, NULL
837 { "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ",
838 EMPTY
, NULL
, DECL core_attrs
, DECL clear_attrs
, NULL
840 { "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
841 DECL html_flow MODIFIER
, NULL
, DECL button_attrs
, NULL
, NULL
843 { "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ",
844 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
846 { "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
847 DECL html_flow
, NULL
, NULL
, DECL html_attrs
, NULL
849 { "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
850 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
852 { "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
853 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
855 { "col", 0, 2, 2, 1, 0, 0, 0, "table column ",
856 EMPTY
, NULL
, DECL col_attrs
, NULL
, NULL
858 { "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
859 DECL col_elt
, "col" , DECL col_attrs
, NULL
, NULL
861 { "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ",
862 DECL html_flow
, NULL
, DECL html_attrs
, NULL
, NULL
864 { "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ",
865 DECL html_flow
, NULL
, DECL edit_attrs
, NULL
, NULL
867 { "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition",
868 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
870 { "dir", 0, 0, 0, 0, 1, 1, 0, "directory list",
871 DECL blockli_elt
, "li" , NULL
, DECL compact_attrs
, NULL
873 { "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container",
874 DECL html_flow
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
876 { "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ",
877 DECL dl_contents
, "dd" , DECL html_attrs
, DECL compact_attr
, NULL
879 { "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ",
880 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
882 { "em", 0, 3, 0, 0, 0, 0, 1, "emphasis",
883 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
885 { "embed", 0, 1, 0, 0, 1, 1, 1, "generic embedded object ",
886 EMPTY
, NULL
, DECL embed_attrs
, NULL
, NULL
888 { "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
889 DECL fieldset_contents
, NULL
, DECL html_attrs
, NULL
, NULL
891 { "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
892 DECL html_inline
, NULL
, NULL
, DECL font_attrs
, NULL
894 { "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
895 DECL form_contents
, "fieldset", DECL form_attrs
, DECL target_attr
, DECL action_attr
897 { "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " ,
898 EMPTY
, NULL
, NULL
, DECL frame_attrs
, NULL
900 { "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
901 DECL frameset_contents
, "noframes" , NULL
, DECL frameset_attrs
, NULL
903 { "h1", 0, 0, 0, 0, 0, 0, 0, "heading ",
904 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
906 { "h2", 0, 0, 0, 0, 0, 0, 0, "heading ",
907 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
909 { "h3", 0, 0, 0, 0, 0, 0, 0, "heading ",
910 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
912 { "h4", 0, 0, 0, 0, 0, 0, 0, "heading ",
913 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
915 { "h5", 0, 0, 0, 0, 0, 0, 0, "heading ",
916 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
918 { "h6", 0, 0, 0, 0, 0, 0, 0, "heading ",
919 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
921 { "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
922 DECL head_contents
, NULL
, DECL head_attrs
, NULL
, NULL
924 { "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
925 EMPTY
, NULL
, DECL html_attrs
, DECL hr_depr
, NULL
927 { "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
928 DECL html_content
, NULL
, DECL i18n_attrs
, DECL version_attr
, NULL
930 { "i", 0, 3, 0, 0, 0, 0, 1, "italic text style",
931 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
933 { "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
934 DECL html_flow
, NULL
, NULL
, DECL iframe_attrs
, NULL
936 { "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ",
937 EMPTY
, NULL
, DECL img_attrs
, DECL align_attr
, DECL src_alt_attrs
939 { "input", 0, 2, 2, 1, 0, 0, 1, "form control ",
940 EMPTY
, NULL
, DECL input_attrs
, DECL align_attr
, NULL
942 { "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text",
943 DECL html_flow
, NULL
, DECL edit_attrs
, NULL
, NULL
945 { "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ",
946 EMPTY
, NULL
, NULL
, DECL prompt_attrs
, NULL
948 { "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
949 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
951 { "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ",
952 DECL html_inline MODIFIER
, NULL
, DECL label_attrs
, NULL
, NULL
954 { "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
955 DECL html_inline
, NULL
, DECL legend_attrs
, DECL align_attr
, NULL
957 { "li", 0, 1, 1, 0, 0, 0, 0, "list item ",
958 DECL html_flow
, NULL
, DECL html_attrs
, NULL
, NULL
960 { "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
961 EMPTY
, NULL
, DECL link_attrs
, DECL target_attr
, NULL
963 { "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ",
964 DECL map_contents
, NULL
, DECL html_attrs
, NULL
, DECL name_attr
966 { "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
967 DECL blockli_elt
, NULL
, NULL
, DECL compact_attrs
, NULL
969 { "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
970 EMPTY
, NULL
, DECL meta_attrs
, NULL
, DECL content_attr
972 { "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
973 DECL noframes_content
, "body" , DECL html_attrs
, NULL
, NULL
975 { "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
976 DECL html_flow
, "div", DECL html_attrs
, NULL
, NULL
978 { "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
979 DECL object_contents
, "div" , DECL object_attrs
, DECL object_depr
, NULL
981 { "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ",
982 DECL li_elt
, "li" , DECL html_attrs
, DECL ol_attrs
, NULL
984 { "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
985 DECL option_elt
, "option", DECL optgroup_attrs
, NULL
, DECL label_attr
987 { "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
988 DECL html_pcdata
, NULL
, DECL option_attrs
, NULL
, NULL
990 { "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ",
991 DECL html_inline
, NULL
, DECL html_attrs
, DECL align_attr
, NULL
993 { "param", 0, 2, 2, 1, 0, 0, 0, "named property value ",
994 EMPTY
, NULL
, DECL param_attrs
, NULL
, DECL name_attr
996 { "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ",
997 DECL pre_content
, NULL
, DECL html_attrs
, DECL width_attr
, NULL
999 { "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
1000 DECL html_inline
, NULL
, DECL quote_attrs
, NULL
, NULL
1002 { "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style",
1003 DECL html_inline
, NULL
, NULL
, DECL html_attrs
, NULL
1005 { "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
1006 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1008 { "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
1009 DECL html_cdata
, NULL
, DECL script_attrs
, DECL language_attr
, DECL type_attr
1011 { "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
1012 DECL select_content
, NULL
, DECL select_attrs
, NULL
, NULL
1014 { "small", 0, 3, 0, 0, 0, 0, 1, "small text style",
1015 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1017 { "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
1018 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1020 { "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
1021 DECL html_inline
, NULL
, NULL
, DECL html_attrs
, NULL
1023 { "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
1024 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1026 { "style", 0, 0, 0, 0, 0, 0, 0, "style info ",
1027 DECL html_cdata
, NULL
, DECL style_attrs
, NULL
, DECL type_attr
1029 { "sub", 0, 3, 0, 0, 0, 0, 1, "subscript",
1030 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1032 { "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ",
1033 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1035 { "table", 0, 0, 0, 0, 0, 0, 0, "",
1036 DECL table_contents
, "tr" , DECL table_attrs
, DECL table_depr
, NULL
1038 { "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ",
1039 DECL tr_elt
, "tr" , DECL talign_attrs
, NULL
, NULL
1041 { "td", 0, 0, 0, 0, 0, 0, 0, "table data cell",
1042 DECL html_flow
, NULL
, DECL th_td_attr
, DECL th_td_depr
, NULL
1044 { "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
1045 DECL html_pcdata
, NULL
, DECL textarea_attrs
, NULL
, DECL rows_cols_attr
1047 { "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ",
1048 DECL tr_elt
, "tr" , DECL talign_attrs
, NULL
, NULL
1050 { "th", 0, 1, 0, 0, 0, 0, 0, "table header cell",
1051 DECL html_flow
, NULL
, DECL th_td_attr
, DECL th_td_depr
, NULL
1053 { "thead", 0, 1, 0, 0, 0, 0, 0, "table header ",
1054 DECL tr_elt
, "tr" , DECL talign_attrs
, NULL
, NULL
1056 { "title", 0, 0, 0, 0, 0, 0, 0, "document title ",
1057 DECL html_pcdata
, NULL
, DECL i18n_attrs
, NULL
, NULL
1059 { "tr", 0, 0, 0, 0, 0, 0, 0, "table row ",
1060 DECL tr_contents
, "td" , DECL talign_attrs
, DECL bgcolor_attr
, NULL
1062 { "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
1063 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1065 { "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style",
1066 DECL html_inline
, NULL
, NULL
, DECL html_attrs
, NULL
1068 { "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ",
1069 DECL li_elt
, "li" , DECL html_attrs
, DECL ul_depr
, NULL
1071 { "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
1072 DECL html_inline
, NULL
, DECL html_attrs
, NULL
, NULL
1079 } htmlStartCloseEntry
;
1082 * start tags that imply the end of current element
1084 static const htmlStartCloseEntry htmlStartClose
[] = {
1086 { "a", "fieldset" },
1090 { "address", "dd" },
1091 { "address", "dl" },
1092 { "address", "dt" },
1093 { "address", "form" },
1094 { "address", "li" },
1095 { "address", "ul" },
1101 { "caption", "col" },
1102 { "caption", "colgroup" },
1103 { "caption", "tbody" },
1104 { "caption", "tfoot" },
1105 { "caption", "thead" },
1106 { "caption", "tr" },
1108 { "col", "colgroup" },
1113 { "colgroup", "colgroup" },
1114 { "colgroup", "tbody" },
1115 { "colgroup", "tfoot" },
1116 { "colgroup", "thead" },
1117 { "colgroup", "tr" },
1128 { "font", "center" },
1132 { "h1", "fieldset" },
1137 { "h2", "fieldset" },
1142 { "h3", "fieldset" },
1147 { "h4", "fieldset" },
1152 { "h5", "fieldset" },
1157 { "h6", "fieldset" },
1164 { "head", "acronym" },
1165 { "head", "address" },
1169 { "head", "blockquote" },
1172 { "head", "center" },
1182 { "head", "fieldset" },
1185 { "head", "frameset" },
1194 { "head", "iframe" },
1198 { "head", "listing" },
1207 { "head", "small" },
1209 { "head", "strike" },
1210 { "head", "strong" },
1213 { "head", "table" },
1224 { "legend", "fieldset" },
1227 { "link", "frameset" },
1228 { "listing", "dd" },
1229 { "listing", "dl" },
1230 { "listing", "dt" },
1231 { "listing", "fieldset" },
1232 { "listing", "form" },
1233 { "listing", "li" },
1234 { "listing", "table" },
1235 { "listing", "ul" },
1243 { "option", "optgroup" },
1244 { "option", "option" },
1246 { "p", "blockquote" },
1251 { "p", "colgroup" },
1257 { "p", "fieldset" },
1259 { "p", "frameset" },
1286 { "pre", "fieldset" },
1292 { "script", "noscript" },
1297 { "style", "body" },
1298 { "style", "frameset" },
1299 { "tbody", "tbody" },
1300 { "tbody", "tfoot" },
1306 { "tfoot", "tbody" },
1312 { "thead", "tbody" },
1313 { "thead", "tfoot" },
1314 { "title", "body" },
1315 { "title", "frameset" },
1323 { "ul", "address" },
1331 { "xmp", "fieldset" },
1339 * The list of HTML elements which are supposed not to have
1340 * CDATA content and where a p element will be implied
1342 * TODO: extend that list by reading the HTML SGML DTD on
1345 static const char *const htmlNoContentElements
[] = {
1352 * The list of HTML attributes which are of content %Script;
1353 * NOTE: when adding ones, check htmlIsScriptAttribute() since
1354 * it assumes the name starts with 'on'
1356 static const char *const htmlScriptAttributes
[] = {
1378 * This table is used by the htmlparser to know what to do with
1379 * broken html pages. By assigning different priorities to different
1380 * elements the parser can decide how to handle extra endtags.
1381 * Endtags are only allowed to close elements with lower or equal
1390 static const elementPriority htmlEndPriority
[] = {
1402 {NULL
, 100} /* Default priority */
1405 /************************************************************************
1407 * functions to handle HTML specific data *
1409 ************************************************************************/
1412 * htmlInitAutoClose:
1414 * This is a no-op now.
1417 htmlInitAutoClose(void) {
1421 htmlCompareTags(const void *key
, const void *member
) {
1422 const xmlChar
*tag
= (const xmlChar
*) key
;
1423 const htmlElemDesc
*desc
= (const htmlElemDesc
*) member
;
1425 return(xmlStrcasecmp(tag
, BAD_CAST desc
->name
));
1430 * @tag: The tag name in lowercase
1432 * Lookup the HTML tag in the ElementTable
1434 * Returns the related htmlElemDescPtr or NULL if not found.
1436 const htmlElemDesc
*
1437 htmlTagLookup(const xmlChar
*tag
) {
1441 return((const htmlElemDesc
*) bsearch(tag
, html40ElementTable
,
1442 sizeof(html40ElementTable
) / sizeof(htmlElemDesc
),
1443 sizeof(htmlElemDesc
), htmlCompareTags
));
1447 * htmlGetEndPriority:
1448 * @name: The name of the element to look up the priority for.
1450 * Return value: The "endtag" priority.
1453 htmlGetEndPriority (const xmlChar
*name
) {
1456 while ((htmlEndPriority
[i
].name
!= NULL
) &&
1457 (!xmlStrEqual((const xmlChar
*)htmlEndPriority
[i
].name
, name
)))
1460 return(htmlEndPriority
[i
].priority
);
1465 htmlCompareStartClose(const void *vkey
, const void *member
) {
1466 const htmlStartCloseEntry
*key
= (const htmlStartCloseEntry
*) vkey
;
1467 const htmlStartCloseEntry
*entry
= (const htmlStartCloseEntry
*) member
;
1470 ret
= strcmp(key
->oldTag
, entry
->oldTag
);
1472 ret
= strcmp(key
->newTag
, entry
->newTag
);
1478 * htmlCheckAutoClose:
1479 * @newtag: The new tag name
1480 * @oldtag: The old tag name
1482 * Checks whether the new tag is one of the registered valid tags for
1485 * Returns 0 if no, 1 if yes.
1488 htmlCheckAutoClose(const xmlChar
* newtag
, const xmlChar
* oldtag
)
1490 htmlStartCloseEntry key
;
1493 key
.oldTag
= (const char *) oldtag
;
1494 key
.newTag
= (const char *) newtag
;
1495 res
= bsearch(&key
, htmlStartClose
,
1496 sizeof(htmlStartClose
) / sizeof(htmlStartCloseEntry
),
1497 sizeof(htmlStartCloseEntry
), htmlCompareStartClose
);
1498 return(res
!= NULL
);
1502 * htmlAutoCloseOnClose:
1503 * @ctxt: an HTML parser context
1504 * @newtag: The new tag name
1505 * @force: force the tag closure
1507 * The HTML DTD allows an ending tag to implicitly close other tags.
1510 htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt
, const xmlChar
* newtag
)
1512 const htmlElemDesc
*info
;
1515 priority
= htmlGetEndPriority(newtag
);
1517 for (i
= (ctxt
->nameNr
- 1); i
>= 0; i
--) {
1519 if (xmlStrEqual(newtag
, ctxt
->nameTab
[i
]))
1522 * A misplaced endtag can only close elements with lower
1523 * or equal priority, so if we find an element with higher
1524 * priority before we find an element with
1525 * matching name, we just ignore this endtag
1527 if (htmlGetEndPriority(ctxt
->nameTab
[i
]) > priority
)
1533 while (!xmlStrEqual(newtag
, ctxt
->name
)) {
1534 info
= htmlTagLookup(ctxt
->name
);
1535 if ((info
!= NULL
) && (info
->endTag
== 3)) {
1536 htmlParseErr(ctxt
, XML_ERR_TAG_NAME_MISMATCH
,
1537 "Opening and ending tag mismatch: %s and %s\n",
1538 newtag
, ctxt
->name
);
1540 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
1541 ctxt
->sax
->endElement(ctxt
->userData
, ctxt
->name
);
1547 * htmlAutoCloseOnEnd:
1548 * @ctxt: an HTML parser context
1550 * Close all remaining tags at the end of the stream
1553 htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt
)
1557 if (ctxt
->nameNr
== 0)
1559 for (i
= (ctxt
->nameNr
- 1); i
>= 0; i
--) {
1560 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
1561 ctxt
->sax
->endElement(ctxt
->userData
, ctxt
->name
);
1568 * @ctxt: an HTML parser context
1569 * @newtag: The new tag name or NULL
1571 * The HTML DTD allows a tag to implicitly close other tags.
1572 * The list is kept in htmlStartClose array. This function is
1573 * called when a new tag has been detected and generates the
1574 * appropriates closes if possible/needed.
1575 * If newtag is NULL this mean we are at the end of the resource
1576 * and we should check
1579 htmlAutoClose(htmlParserCtxtPtr ctxt
, const xmlChar
* newtag
)
1581 while ((newtag
!= NULL
) && (ctxt
->name
!= NULL
) &&
1582 (htmlCheckAutoClose(newtag
, ctxt
->name
))) {
1583 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
1584 ctxt
->sax
->endElement(ctxt
->userData
, ctxt
->name
);
1587 if (newtag
== NULL
) {
1588 htmlAutoCloseOnEnd(ctxt
);
1591 while ((newtag
== NULL
) && (ctxt
->name
!= NULL
) &&
1592 ((xmlStrEqual(ctxt
->name
, BAD_CAST
"head")) ||
1593 (xmlStrEqual(ctxt
->name
, BAD_CAST
"body")) ||
1594 (xmlStrEqual(ctxt
->name
, BAD_CAST
"html")))) {
1595 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
1596 ctxt
->sax
->endElement(ctxt
->userData
, ctxt
->name
);
1603 * @doc: the HTML document
1604 * @name: The tag name
1605 * @elem: the HTML element
1607 * The HTML DTD allows a tag to implicitly close other tags.
1608 * The list is kept in htmlStartClose array. This function checks
1609 * if the element or one of it's children would autoclose the
1612 * Returns 1 if autoclose, 0 otherwise
1615 htmlAutoCloseTag(htmlDocPtr doc
, const xmlChar
*name
, htmlNodePtr elem
) {
1618 if (elem
== NULL
) return(1);
1619 if (xmlStrEqual(name
, elem
->name
)) return(0);
1620 if (htmlCheckAutoClose(elem
->name
, name
)) return(1);
1621 child
= elem
->children
;
1622 while (child
!= NULL
) {
1623 if (htmlAutoCloseTag(doc
, name
, child
)) return(1);
1624 child
= child
->next
;
1631 * @doc: the HTML document
1632 * @elem: the HTML element
1634 * The HTML DTD allows a tag to implicitly close other tags.
1635 * The list is kept in htmlStartClose array. This function checks
1636 * if a tag is autoclosed by one of it's child
1638 * Returns 1 if autoclosed, 0 otherwise
1641 htmlIsAutoClosed(htmlDocPtr doc
, htmlNodePtr elem
) {
1644 if (elem
== NULL
) return(1);
1645 child
= elem
->children
;
1646 while (child
!= NULL
) {
1647 if (htmlAutoCloseTag(doc
, elem
->name
, child
)) return(1);
1648 child
= child
->next
;
1655 * @ctxt: an HTML parser context
1656 * @newtag: The new tag name
1658 * The HTML DTD allows a tag to exists only implicitly
1659 * called when a new tag has been detected and generates the
1660 * appropriates implicit tags if missing
1663 htmlCheckImplied(htmlParserCtxtPtr ctxt
, const xmlChar
*newtag
) {
1666 if (ctxt
->options
& HTML_PARSE_NOIMPLIED
)
1668 if (!htmlOmittedDefaultValue
)
1670 if (xmlStrEqual(newtag
, BAD_CAST
"html"))
1672 if (ctxt
->nameNr
<= 0) {
1673 htmlnamePush(ctxt
, BAD_CAST
"html");
1674 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->startElement
!= NULL
))
1675 ctxt
->sax
->startElement(ctxt
->userData
, BAD_CAST
"html", NULL
);
1677 if ((xmlStrEqual(newtag
, BAD_CAST
"body")) || (xmlStrEqual(newtag
, BAD_CAST
"head")))
1679 if ((ctxt
->nameNr
<= 1) &&
1680 ((xmlStrEqual(newtag
, BAD_CAST
"script")) ||
1681 (xmlStrEqual(newtag
, BAD_CAST
"style")) ||
1682 (xmlStrEqual(newtag
, BAD_CAST
"meta")) ||
1683 (xmlStrEqual(newtag
, BAD_CAST
"link")) ||
1684 (xmlStrEqual(newtag
, BAD_CAST
"title")) ||
1685 (xmlStrEqual(newtag
, BAD_CAST
"base")))) {
1686 if (ctxt
->html
>= 3) {
1687 /* we already saw or generated an <head> before */
1691 * dropped OBJECT ... i you put it first BODY will be
1694 htmlnamePush(ctxt
, BAD_CAST
"head");
1695 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->startElement
!= NULL
))
1696 ctxt
->sax
->startElement(ctxt
->userData
, BAD_CAST
"head", NULL
);
1697 } else if ((!xmlStrEqual(newtag
, BAD_CAST
"noframes")) &&
1698 (!xmlStrEqual(newtag
, BAD_CAST
"frame")) &&
1699 (!xmlStrEqual(newtag
, BAD_CAST
"frameset"))) {
1700 if (ctxt
->html
>= 10) {
1701 /* we already saw or generated a <body> before */
1704 for (i
= 0;i
< ctxt
->nameNr
;i
++) {
1705 if (xmlStrEqual(ctxt
->nameTab
[i
], BAD_CAST
"body")) {
1708 if (xmlStrEqual(ctxt
->nameTab
[i
], BAD_CAST
"head")) {
1713 htmlnamePush(ctxt
, BAD_CAST
"body");
1714 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->startElement
!= NULL
))
1715 ctxt
->sax
->startElement(ctxt
->userData
, BAD_CAST
"body", NULL
);
1720 * htmlCheckParagraph
1721 * @ctxt: an HTML parser context
1723 * Check whether a p element need to be implied before inserting
1724 * characters in the current element.
1726 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1731 htmlCheckParagraph(htmlParserCtxtPtr ctxt
) {
1739 htmlAutoClose(ctxt
, BAD_CAST
"p");
1740 htmlCheckImplied(ctxt
, BAD_CAST
"p");
1741 htmlnamePush(ctxt
, BAD_CAST
"p");
1742 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->startElement
!= NULL
))
1743 ctxt
->sax
->startElement(ctxt
->userData
, BAD_CAST
"p", NULL
);
1746 if (!htmlOmittedDefaultValue
)
1748 for (i
= 0; htmlNoContentElements
[i
] != NULL
; i
++) {
1749 if (xmlStrEqual(tag
, BAD_CAST htmlNoContentElements
[i
])) {
1750 htmlAutoClose(ctxt
, BAD_CAST
"p");
1751 htmlCheckImplied(ctxt
, BAD_CAST
"p");
1752 htmlnamePush(ctxt
, BAD_CAST
"p");
1753 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->startElement
!= NULL
))
1754 ctxt
->sax
->startElement(ctxt
->userData
, BAD_CAST
"p", NULL
);
1762 * htmlIsScriptAttribute:
1763 * @name: an attribute name
1765 * Check if an attribute is of content type Script
1767 * Returns 1 is the attribute is a script 0 otherwise
1770 htmlIsScriptAttribute(const xmlChar
*name
) {
1776 * all script attributes start with 'on'
1778 if ((name
[0] != 'o') || (name
[1] != 'n'))
1781 i
< sizeof(htmlScriptAttributes
)/sizeof(htmlScriptAttributes
[0]);
1783 if (xmlStrEqual(name
, (const xmlChar
*) htmlScriptAttributes
[i
]))
1789 /************************************************************************
1791 * The list of HTML predefined entities *
1793 ************************************************************************/
1796 static const htmlEntityDesc html40EntitiesTable
[] = {
1798 * the 4 absolute ones, plus apostrophe.
1800 { 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1801 { 38, "amp", "ampersand, U+0026 ISOnum" },
1802 { 39, "apos", "single quote" },
1803 { 60, "lt", "less-than sign, U+003C ISOnum" },
1804 { 62, "gt", "greater-than sign, U+003E ISOnum" },
1807 * A bunch still in the 128-255 range
1808 * Replacing them depend really on the charset used.
1810 { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1811 { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1812 { 162, "cent", "cent sign, U+00A2 ISOnum" },
1813 { 163, "pound","pound sign, U+00A3 ISOnum" },
1814 { 164, "curren","currency sign, U+00A4 ISOnum" },
1815 { 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
1816 { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1817 { 167, "sect", "section sign, U+00A7 ISOnum" },
1818 { 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1819 { 169, "copy", "copyright sign, U+00A9 ISOnum" },
1820 { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1821 { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1822 { 172, "not", "not sign, U+00AC ISOnum" },
1823 { 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1824 { 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
1825 { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1826 { 176, "deg", "degree sign, U+00B0 ISOnum" },
1827 { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1828 { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1829 { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1830 { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1831 { 181, "micro","micro sign, U+00B5 ISOnum" },
1832 { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1833 { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1834 { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1835 { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1836 { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1837 { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1838 { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1839 { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1840 { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1841 { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1842 { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1843 { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1844 { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1845 { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1846 { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1847 { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1848 { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1849 { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1850 { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1851 { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1852 { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1853 { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1854 { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1855 { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1856 { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1857 { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1858 { 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
1859 { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1860 { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1861 { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1862 { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1863 { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1864 { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1865 { 215, "times","multiplication sign, U+00D7 ISOnum" },
1866 { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1867 { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1868 { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1869 { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1870 { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1871 { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1872 { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1873 { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1874 { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1875 { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1876 { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1877 { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1878 { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1879 { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1880 { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1881 { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1882 { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1883 { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1884 { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1885 { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1886 { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1887 { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1888 { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1889 { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1890 { 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
1891 { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1892 { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1893 { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1894 { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1895 { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1896 { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1897 { 247, "divide","division sign, U+00F7 ISOnum" },
1898 { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1899 { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1900 { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1901 { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1902 { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1903 { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1904 { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1905 { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1907 { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1908 { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
1909 { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1910 { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1911 { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1914 * Anything below should really be kept as entities references
1916 { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1918 { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1919 { 732, "tilde","small tilde, U+02DC ISOdia" },
1921 { 913, "Alpha","greek capital letter alpha, U+0391" },
1922 { 914, "Beta", "greek capital letter beta, U+0392" },
1923 { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1924 { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1925 { 917, "Epsilon","greek capital letter epsilon, U+0395" },
1926 { 918, "Zeta", "greek capital letter zeta, U+0396" },
1927 { 919, "Eta", "greek capital letter eta, U+0397" },
1928 { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1929 { 921, "Iota", "greek capital letter iota, U+0399" },
1930 { 922, "Kappa","greek capital letter kappa, U+039A" },
1931 { 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
1932 { 924, "Mu", "greek capital letter mu, U+039C" },
1933 { 925, "Nu", "greek capital letter nu, U+039D" },
1934 { 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1935 { 927, "Omicron","greek capital letter omicron, U+039F" },
1936 { 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1937 { 929, "Rho", "greek capital letter rho, U+03A1" },
1938 { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1939 { 932, "Tau", "greek capital letter tau, U+03A4" },
1940 { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1941 { 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
1942 { 935, "Chi", "greek capital letter chi, U+03A7" },
1943 { 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
1944 { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1946 { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1947 { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1948 { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1949 { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
1950 { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1951 { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1952 { 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
1953 { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
1954 { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1955 { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1956 { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1957 { 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
1958 { 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
1959 { 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
1960 { 959, "omicron","greek small letter omicron, U+03BF NEW" },
1961 { 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1962 { 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
1963 { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1964 { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1965 { 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
1966 { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1967 { 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
1968 { 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
1969 { 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
1970 { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
1971 { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1972 { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1973 { 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
1975 { 8194, "ensp", "en space, U+2002 ISOpub" },
1976 { 8195, "emsp", "em space, U+2003 ISOpub" },
1977 { 8201, "thinsp","thin space, U+2009 ISOpub" },
1978 { 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1979 { 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
1980 { 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
1981 { 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
1982 { 8211, "ndash","en dash, U+2013 ISOpub" },
1983 { 8212, "mdash","em dash, U+2014 ISOpub" },
1984 { 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1985 { 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1986 { 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1987 { 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1988 { 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1989 { 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1990 { 8224, "dagger","dagger, U+2020 ISOpub" },
1991 { 8225, "Dagger","double dagger, U+2021 ISOpub" },
1993 { 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1994 { 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1996 { 8240, "permil","per mille sign, U+2030 ISOtech" },
1998 { 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1999 { 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
2001 { 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
2002 { 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
2004 { 8254, "oline","overline = spacing overscore, U+203E NEW" },
2005 { 8260, "frasl","fraction slash, U+2044 NEW" },
2007 { 8364, "euro", "euro sign, U+20AC NEW" },
2009 { 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
2010 { 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
2011 { 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
2012 { 8482, "trade","trade mark sign, U+2122 ISOnum" },
2013 { 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
2014 { 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
2015 { 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
2016 { 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
2017 { 8595, "darr", "downwards arrow, U+2193 ISOnum" },
2018 { 8596, "harr", "left right arrow, U+2194 ISOamsa" },
2019 { 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
2020 { 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
2021 { 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
2022 { 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
2023 { 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
2024 { 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
2026 { 8704, "forall","for all, U+2200 ISOtech" },
2027 { 8706, "part", "partial differential, U+2202 ISOtech" },
2028 { 8707, "exist","there exists, U+2203 ISOtech" },
2029 { 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
2030 { 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
2031 { 8712, "isin", "element of, U+2208 ISOtech" },
2032 { 8713, "notin","not an element of, U+2209 ISOtech" },
2033 { 8715, "ni", "contains as member, U+220B ISOtech" },
2034 { 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
2035 { 8721, "sum", "n-ary summation, U+2211 ISOamsb" },
2036 { 8722, "minus","minus sign, U+2212 ISOtech" },
2037 { 8727, "lowast","asterisk operator, U+2217 ISOtech" },
2038 { 8730, "radic","square root = radical sign, U+221A ISOtech" },
2039 { 8733, "prop", "proportional to, U+221D ISOtech" },
2040 { 8734, "infin","infinity, U+221E ISOtech" },
2041 { 8736, "ang", "angle, U+2220 ISOamso" },
2042 { 8743, "and", "logical and = wedge, U+2227 ISOtech" },
2043 { 8744, "or", "logical or = vee, U+2228 ISOtech" },
2044 { 8745, "cap", "intersection = cap, U+2229 ISOtech" },
2045 { 8746, "cup", "union = cup, U+222A ISOtech" },
2046 { 8747, "int", "integral, U+222B ISOtech" },
2047 { 8756, "there4","therefore, U+2234 ISOtech" },
2048 { 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
2049 { 8773, "cong", "approximately equal to, U+2245 ISOtech" },
2050 { 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
2051 { 8800, "ne", "not equal to, U+2260 ISOtech" },
2052 { 8801, "equiv","identical to, U+2261 ISOtech" },
2053 { 8804, "le", "less-than or equal to, U+2264 ISOtech" },
2054 { 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
2055 { 8834, "sub", "subset of, U+2282 ISOtech" },
2056 { 8835, "sup", "superset of, U+2283 ISOtech" },
2057 { 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
2058 { 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
2059 { 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
2060 { 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
2061 { 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
2062 { 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
2063 { 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
2064 { 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
2065 { 8969, "rceil","right ceiling, U+2309 ISOamsc" },
2066 { 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
2067 { 8971, "rfloor","right floor, U+230B ISOamsc" },
2068 { 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
2069 { 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
2070 { 9674, "loz", "lozenge, U+25CA ISOpub" },
2072 { 9824, "spades","black spade suit, U+2660 ISOpub" },
2073 { 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
2074 { 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
2075 { 9830, "diams","black diamond suit, U+2666 ISOpub" },
2079 /************************************************************************
2081 * Commodity functions to handle entities *
2083 ************************************************************************/
2086 * Macro used to grow the current buffer.
2088 #define growBuffer(buffer) { \
2090 buffer##_size *= 2; \
2091 tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
2092 if (tmp == NULL) { \
2093 htmlErrMemory(ctxt, "growing buffer\n"); \
2102 * @name: the entity name
2104 * Lookup the given entity in EntitiesTable
2106 * TODO: the linear scan is really ugly, an hash table is really needed.
2108 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
2110 const htmlEntityDesc
*
2111 htmlEntityLookup(const xmlChar
*name
) {
2114 for (i
= 0;i
< (sizeof(html40EntitiesTable
)/
2115 sizeof(html40EntitiesTable
[0]));i
++) {
2116 if (xmlStrEqual(name
, BAD_CAST html40EntitiesTable
[i
].name
)) {
2117 return((htmlEntityDescPtr
) &html40EntitiesTable
[i
]);
2124 * htmlEntityValueLookup:
2125 * @value: the entity's unicode value
2127 * Lookup the given entity in EntitiesTable
2129 * TODO: the linear scan is really ugly, an hash table is really needed.
2131 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
2133 const htmlEntityDesc
*
2134 htmlEntityValueLookup(unsigned int value
) {
2137 for (i
= 0;i
< (sizeof(html40EntitiesTable
)/
2138 sizeof(html40EntitiesTable
[0]));i
++) {
2139 if (html40EntitiesTable
[i
].value
>= value
) {
2140 if (html40EntitiesTable
[i
].value
> value
)
2142 return((htmlEntityDescPtr
) &html40EntitiesTable
[i
]);
2150 * @out: a pointer to an array of bytes to store the result
2151 * @outlen: the length of @out
2152 * @in: a pointer to an array of UTF-8 chars
2153 * @inlen: the length of @in
2155 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2156 * plus HTML entities block of chars out.
2158 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2159 * The value of @inlen after return is the number of octets consumed
2160 * as the return value is positive, else unpredictable.
2161 * The value of @outlen after return is the number of octets consumed.
2164 UTF8ToHtml(unsigned char* out
, int *outlen
,
2165 const unsigned char* in
, int *inlen
) {
2166 const unsigned char* processed
= in
;
2167 const unsigned char* outend
;
2168 const unsigned char* outstart
= out
;
2169 const unsigned char* instart
= in
;
2170 const unsigned char* inend
;
2174 if ((out
== NULL
) || (outlen
== NULL
) || (inlen
== NULL
)) return(-1);
2177 * initialization nothing to do
2183 inend
= in
+ (*inlen
);
2184 outend
= out
+ (*outlen
);
2185 while (in
< inend
) {
2187 if (d
< 0x80) { c
= d
; trailing
= 0; }
2188 else if (d
< 0xC0) {
2189 /* trailing byte in leading position */
2190 *outlen
= out
- outstart
;
2191 *inlen
= processed
- instart
;
2193 } else if (d
< 0xE0) { c
= d
& 0x1F; trailing
= 1; }
2194 else if (d
< 0xF0) { c
= d
& 0x0F; trailing
= 2; }
2195 else if (d
< 0xF8) { c
= d
& 0x07; trailing
= 3; }
2197 /* no chance for this in Ascii */
2198 *outlen
= out
- outstart
;
2199 *inlen
= processed
- instart
;
2203 if (inend
- in
< trailing
) {
2207 for ( ; trailing
; trailing
--) {
2208 if ((in
>= inend
) || (((d
= *in
++) & 0xC0) != 0x80))
2214 /* assertion: c is a single UTF-4 value */
2216 if (out
+ 1 >= outend
)
2221 const htmlEntityDesc
* ent
;
2226 * Try to lookup a predefined HTML entity for it
2229 ent
= htmlEntityValueLookup(c
);
2231 snprintf(nbuf
, sizeof(nbuf
), "#%u", c
);
2237 if (out
+ 2 + len
>= outend
)
2240 memcpy(out
, cp
, len
);
2246 *outlen
= out
- outstart
;
2247 *inlen
= processed
- instart
;
2252 * htmlEncodeEntities:
2253 * @out: a pointer to an array of bytes to store the result
2254 * @outlen: the length of @out
2255 * @in: a pointer to an array of UTF-8 chars
2256 * @inlen: the length of @in
2257 * @quoteChar: the quote character to escape (' or ") or zero.
2259 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2260 * plus HTML entities block of chars out.
2262 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2263 * The value of @inlen after return is the number of octets consumed
2264 * as the return value is positive, else unpredictable.
2265 * The value of @outlen after return is the number of octets consumed.
2268 htmlEncodeEntities(unsigned char* out
, int *outlen
,
2269 const unsigned char* in
, int *inlen
, int quoteChar
) {
2270 const unsigned char* processed
= in
;
2271 const unsigned char* outend
;
2272 const unsigned char* outstart
= out
;
2273 const unsigned char* instart
= in
;
2274 const unsigned char* inend
;
2278 if ((out
== NULL
) || (outlen
== NULL
) || (inlen
== NULL
) || (in
== NULL
))
2280 outend
= out
+ (*outlen
);
2281 inend
= in
+ (*inlen
);
2282 while (in
< inend
) {
2284 if (d
< 0x80) { c
= d
; trailing
= 0; }
2285 else if (d
< 0xC0) {
2286 /* trailing byte in leading position */
2287 *outlen
= out
- outstart
;
2288 *inlen
= processed
- instart
;
2290 } else if (d
< 0xE0) { c
= d
& 0x1F; trailing
= 1; }
2291 else if (d
< 0xF0) { c
= d
& 0x0F; trailing
= 2; }
2292 else if (d
< 0xF8) { c
= d
& 0x07; trailing
= 3; }
2294 /* no chance for this in Ascii */
2295 *outlen
= out
- outstart
;
2296 *inlen
= processed
- instart
;
2300 if (inend
- in
< trailing
)
2303 while (trailing
--) {
2304 if (((d
= *in
++) & 0xC0) != 0x80) {
2305 *outlen
= out
- outstart
;
2306 *inlen
= processed
- instart
;
2313 /* assertion: c is a single UTF-4 value */
2314 if ((c
< 0x80) && (c
!= (unsigned int) quoteChar
) &&
2315 (c
!= '&') && (c
!= '<') && (c
!= '>')) {
2320 const htmlEntityDesc
* ent
;
2326 * Try to lookup a predefined HTML entity for it
2328 ent
= htmlEntityValueLookup(c
);
2330 snprintf(nbuf
, sizeof(nbuf
), "#%u", c
);
2336 if (out
+ 2 + len
> outend
)
2339 memcpy(out
, cp
, len
);
2345 *outlen
= out
- outstart
;
2346 *inlen
= processed
- instart
;
2350 /************************************************************************
2352 * Commodity functions to handle streams *
2354 ************************************************************************/
2356 #ifdef LIBXML_PUSH_ENABLED
2358 * htmlNewInputStream:
2359 * @ctxt: an HTML parser context
2361 * Create a new input stream structure
2362 * Returns the new input stream or NULL
2364 static htmlParserInputPtr
2365 htmlNewInputStream(htmlParserCtxtPtr ctxt
) {
2366 htmlParserInputPtr input
;
2368 input
= (xmlParserInputPtr
) xmlMalloc(sizeof(htmlParserInput
));
2369 if (input
== NULL
) {
2370 htmlErrMemory(ctxt
, "couldn't allocate a new input stream\n");
2373 memset(input
, 0, sizeof(htmlParserInput
));
2374 input
->filename
= NULL
;
2375 input
->directory
= NULL
;
2383 input
->version
= NULL
;
2384 input
->consumed
= 0;
2391 /************************************************************************
2393 * Commodity functions, cleanup needed ? *
2395 ************************************************************************/
2397 * all tags allowing pc data from the html 4.01 loose dtd
2398 * NOTE: it might be more appropriate to integrate this information
2399 * into the html40ElementTable array but I don't want to risk any
2400 * binary incompatibility
2402 static const char *allowPCData
[] = {
2403 "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
2404 "blockquote", "body", "button", "caption", "center", "cite", "code",
2405 "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
2406 "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
2407 "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
2408 "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
2413 * @ctxt: an HTML parser context
2415 * @len: the size of @str
2417 * Is this a sequence of blank chars that one can ignore ?
2419 * Returns 1 if ignorable 0 otherwise.
2422 static int areBlanks(htmlParserCtxtPtr ctxt
, const xmlChar
*str
, int len
) {
2425 xmlNodePtr lastChild
;
2428 for (j
= 0;j
< len
;j
++)
2429 if (!(IS_BLANK_CH(str
[j
]))) return(0);
2431 if (CUR
== 0) return(1);
2432 if (CUR
!= '<') return(0);
2433 if (ctxt
->name
== NULL
)
2435 if (xmlStrEqual(ctxt
->name
, BAD_CAST
"html"))
2437 if (xmlStrEqual(ctxt
->name
, BAD_CAST
"head"))
2440 /* Only strip CDATA children of the body tag for strict HTML DTDs */
2441 if (xmlStrEqual(ctxt
->name
, BAD_CAST
"body") && ctxt
->myDoc
!= NULL
) {
2442 dtd
= xmlGetIntSubset(ctxt
->myDoc
);
2443 if (dtd
!= NULL
&& dtd
->ExternalID
!= NULL
) {
2444 if (!xmlStrcasecmp(dtd
->ExternalID
, BAD_CAST
"-//W3C//DTD HTML 4.01//EN") ||
2445 !xmlStrcasecmp(dtd
->ExternalID
, BAD_CAST
"-//W3C//DTD HTML 4//EN"))
2450 if (ctxt
->node
== NULL
) return(0);
2451 lastChild
= xmlGetLastChild(ctxt
->node
);
2452 while ((lastChild
) && (lastChild
->type
== XML_COMMENT_NODE
))
2453 lastChild
= lastChild
->prev
;
2454 if (lastChild
== NULL
) {
2455 if ((ctxt
->node
->type
!= XML_ELEMENT_NODE
) &&
2456 (ctxt
->node
->content
!= NULL
)) return(0);
2457 /* keep ws in constructs like ...<b> </b>...
2458 for all tags "b" allowing PCDATA */
2459 for ( i
= 0; i
< sizeof(allowPCData
)/sizeof(allowPCData
[0]); i
++ ) {
2460 if ( xmlStrEqual(ctxt
->name
, BAD_CAST allowPCData
[i
]) ) {
2464 } else if (xmlNodeIsText(lastChild
)) {
2467 /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
2468 for all tags "p" allowing PCDATA */
2469 for ( i
= 0; i
< sizeof(allowPCData
)/sizeof(allowPCData
[0]); i
++ ) {
2470 if ( xmlStrEqual(lastChild
->name
, BAD_CAST allowPCData
[i
]) ) {
2480 * @URI: URI for the dtd, or NULL
2481 * @ExternalID: the external ID of the DTD, or NULL
2483 * Creates a new HTML document without a DTD node if @URI and @ExternalID
2486 * Returns a new document, do not initialize the DTD if not provided
2489 htmlNewDocNoDtD(const xmlChar
*URI
, const xmlChar
*ExternalID
) {
2493 * Allocate a new document and fill the fields.
2495 cur
= (xmlDocPtr
) xmlMalloc(sizeof(xmlDoc
));
2497 htmlErrMemory(NULL
, "HTML document creation failed\n");
2500 memset(cur
, 0, sizeof(xmlDoc
));
2502 cur
->type
= XML_HTML_DOCUMENT_NODE
;
2503 cur
->version
= NULL
;
2504 cur
->intSubset
= NULL
;
2507 cur
->children
= NULL
;
2508 cur
->extSubset
= NULL
;
2510 cur
->encoding
= NULL
;
2511 cur
->standalone
= 1;
2512 cur
->compression
= 0;
2515 cur
->_private
= NULL
;
2516 cur
->charset
= XML_CHAR_ENCODING_UTF8
;
2517 cur
->properties
= XML_DOC_HTML
| XML_DOC_USERBUILT
;
2518 if ((ExternalID
!= NULL
) ||
2520 xmlCreateIntSubset(cur
, BAD_CAST
"html", ExternalID
, URI
);
2521 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2522 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
2528 * @URI: URI for the dtd, or NULL
2529 * @ExternalID: the external ID of the DTD, or NULL
2531 * Creates a new HTML document
2533 * Returns a new document
2536 htmlNewDoc(const xmlChar
*URI
, const xmlChar
*ExternalID
) {
2537 if ((URI
== NULL
) && (ExternalID
== NULL
))
2538 return(htmlNewDocNoDtD(
2539 BAD_CAST
"http://www.w3.org/TR/REC-html40/loose.dtd",
2540 BAD_CAST
"-//W3C//DTD HTML 4.0 Transitional//EN"));
2542 return(htmlNewDocNoDtD(URI
, ExternalID
));
2546 /************************************************************************
2548 * The parser itself *
2549 * Relates to http://www.w3.org/TR/html40 *
2551 ************************************************************************/
2553 /************************************************************************
2555 * The parser itself *
2557 ************************************************************************/
2559 static const xmlChar
* htmlParseNameComplex(xmlParserCtxtPtr ctxt
);
2562 * htmlParseHTMLName:
2563 * @ctxt: an HTML parser context
2565 * parse an HTML tag or attribute name, note that we convert it to lowercase
2566 * since HTML names are not case-sensitive.
2568 * Returns the Tag Name parsed or NULL
2571 static const xmlChar
*
2572 htmlParseHTMLName(htmlParserCtxtPtr ctxt
) {
2574 xmlChar loc
[HTML_PARSER_BUFFER_SIZE
];
2576 if (!IS_ASCII_LETTER(CUR
) && (CUR
!= '_') &&
2577 (CUR
!= ':') && (CUR
!= '.')) return(NULL
);
2579 while ((i
< HTML_PARSER_BUFFER_SIZE
) &&
2580 ((IS_ASCII_LETTER(CUR
)) || (IS_ASCII_DIGIT(CUR
)) ||
2581 (CUR
== ':') || (CUR
== '-') || (CUR
== '_') ||
2583 if ((CUR
>= 'A') && (CUR
<= 'Z')) loc
[i
] = CUR
+ 0x20;
2590 return(xmlDictLookup(ctxt
->dict
, loc
, i
));
2595 * htmlParseHTMLName_nonInvasive:
2596 * @ctxt: an HTML parser context
2598 * parse an HTML tag or attribute name, note that we convert it to lowercase
2599 * since HTML names are not case-sensitive, this doesn't consume the data
2600 * from the stream, it's a look-ahead
2602 * Returns the Tag Name parsed or NULL
2605 static const xmlChar
*
2606 htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt
) {
2608 xmlChar loc
[HTML_PARSER_BUFFER_SIZE
];
2610 if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') &&
2611 (NXT(1) != ':')) return(NULL
);
2613 while ((i
< HTML_PARSER_BUFFER_SIZE
) &&
2614 ((IS_ASCII_LETTER(NXT(1+i
))) || (IS_ASCII_DIGIT(NXT(1+i
))) ||
2615 (NXT(1+i
) == ':') || (NXT(1+i
) == '-') || (NXT(1+i
) == '_'))) {
2616 if ((NXT(1+i
) >= 'A') && (NXT(1+i
) <= 'Z')) loc
[i
] = NXT(1+i
) + 0x20;
2617 else loc
[i
] = NXT(1+i
);
2621 return(xmlDictLookup(ctxt
->dict
, loc
, i
));
2627 * @ctxt: an HTML parser context
2629 * parse an HTML name, this routine is case sensitive.
2631 * Returns the Name parsed or NULL
2634 static const xmlChar
*
2635 htmlParseName(htmlParserCtxtPtr ctxt
) {
2643 * Accelerator for simple ASCII names
2645 in
= ctxt
->input
->cur
;
2646 if (((*in
>= 0x61) && (*in
<= 0x7A)) ||
2647 ((*in
>= 0x41) && (*in
<= 0x5A)) ||
2648 (*in
== '_') || (*in
== ':')) {
2650 while (((*in
>= 0x61) && (*in
<= 0x7A)) ||
2651 ((*in
>= 0x41) && (*in
<= 0x5A)) ||
2652 ((*in
>= 0x30) && (*in
<= 0x39)) ||
2653 (*in
== '_') || (*in
== '-') ||
2654 (*in
== ':') || (*in
== '.'))
2657 if (in
== ctxt
->input
->end
)
2660 if ((*in
> 0) && (*in
< 0x80)) {
2661 count
= in
- ctxt
->input
->cur
;
2662 ret
= xmlDictLookup(ctxt
->dict
, ctxt
->input
->cur
, count
);
2663 ctxt
->input
->cur
= in
;
2664 ctxt
->input
->col
+= count
;
2668 return(htmlParseNameComplex(ctxt
));
2671 static const xmlChar
*
2672 htmlParseNameComplex(xmlParserCtxtPtr ctxt
) {
2676 const xmlChar
*base
= ctxt
->input
->base
;
2679 * Handler for more complex cases
2683 if ((c
== ' ') || (c
== '>') || (c
== '/') || /* accelerators */
2684 (!IS_LETTER(c
) && (c
!= '_') &&
2689 while ((c
!= ' ') && (c
!= '>') && (c
!= '/') && /* test bigname.xml */
2690 ((IS_LETTER(c
)) || (IS_DIGIT(c
)) ||
2691 (c
== '.') || (c
== '-') ||
2692 (c
== '_') || (c
== ':') ||
2693 (IS_COMBINING(c
)) ||
2694 (IS_EXTENDER(c
)))) {
2695 if (count
++ > 100) {
2702 if (ctxt
->input
->base
!= base
) {
2704 * We changed encoding from an unknown encoding
2705 * Input buffer changed location, so we better start again
2707 return(htmlParseNameComplex(ctxt
));
2711 if (ctxt
->input
->cur
- ctxt
->input
->base
< len
) {
2713 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
2714 "unexpected change of input buffer", NULL
, NULL
);
2718 return(xmlDictLookup(ctxt
->dict
, ctxt
->input
->cur
- len
, len
));
2723 * htmlParseHTMLAttribute:
2724 * @ctxt: an HTML parser context
2725 * @stop: a char stop value
2727 * parse an HTML attribute value till the stop (quote), if
2728 * stop is 0 then it stops at the first space
2730 * Returns the attribute parsed or NULL
2734 htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt
, const xmlChar stop
) {
2735 xmlChar
*buffer
= NULL
;
2736 int buffer_size
= 0;
2737 xmlChar
*out
= NULL
;
2738 const xmlChar
*name
= NULL
;
2739 const xmlChar
*cur
= NULL
;
2740 const htmlEntityDesc
* ent
;
2743 * allocate a translation buffer.
2745 buffer_size
= HTML_PARSER_BUFFER_SIZE
;
2746 buffer
= (xmlChar
*) xmlMallocAtomic(buffer_size
* sizeof(xmlChar
));
2747 if (buffer
== NULL
) {
2748 htmlErrMemory(ctxt
, "buffer allocation failed\n");
2754 * Ok loop until we reach one of the ending chars
2756 while ((CUR
!= 0) && (CUR
!= stop
)) {
2757 if ((stop
== 0) && (CUR
== '>')) break;
2758 if ((stop
== 0) && (IS_BLANK_CH(CUR
))) break;
2760 if (NXT(1) == '#') {
2764 c
= htmlParseCharRef(ctxt
);
2766 { *out
++ = c
; bits
= -6; }
2768 { *out
++ =((c
>> 6) & 0x1F) | 0xC0; bits
= 0; }
2769 else if (c
< 0x10000)
2770 { *out
++ =((c
>> 12) & 0x0F) | 0xE0; bits
= 6; }
2772 { *out
++ =((c
>> 18) & 0x07) | 0xF0; bits
= 12; }
2774 for ( ; bits
>= 0; bits
-= 6) {
2775 *out
++ = ((c
>> bits
) & 0x3F) | 0x80;
2778 if (out
- buffer
> buffer_size
- 100) {
2779 int indx
= out
- buffer
;
2782 out
= &buffer
[indx
];
2785 ent
= htmlParseEntityRef(ctxt
, &name
);
2788 if (out
- buffer
> buffer_size
- 100) {
2789 int indx
= out
- buffer
;
2792 out
= &buffer
[indx
];
2794 } else if (ent
== NULL
) {
2798 if (out
- buffer
> buffer_size
- 100) {
2799 int indx
= out
- buffer
;
2802 out
= &buffer
[indx
];
2810 if (out
- buffer
> buffer_size
- 100) {
2811 int indx
= out
- buffer
;
2814 out
= &buffer
[indx
];
2818 { *out
++ = c
; bits
= -6; }
2820 { *out
++ =((c
>> 6) & 0x1F) | 0xC0; bits
= 0; }
2821 else if (c
< 0x10000)
2822 { *out
++ =((c
>> 12) & 0x0F) | 0xE0; bits
= 6; }
2824 { *out
++ =((c
>> 18) & 0x07) | 0xF0; bits
= 12; }
2826 for ( ; bits
>= 0; bits
-= 6) {
2827 *out
++ = ((c
>> bits
) & 0x3F) | 0x80;
2835 if (out
- buffer
> buffer_size
- 100) {
2836 int indx
= out
- buffer
;
2839 out
= &buffer
[indx
];
2843 { *out
++ = c
; bits
= -6; }
2845 { *out
++ =((c
>> 6) & 0x1F) | 0xC0; bits
= 0; }
2846 else if (c
< 0x10000)
2847 { *out
++ =((c
>> 12) & 0x0F) | 0xE0; bits
= 6; }
2849 { *out
++ =((c
>> 18) & 0x07) | 0xF0; bits
= 12; }
2851 for ( ; bits
>= 0; bits
-= 6) {
2852 *out
++ = ((c
>> bits
) & 0x3F) | 0x80;
2862 * htmlParseEntityRef:
2863 * @ctxt: an HTML parser context
2864 * @str: location to store the entity name
2866 * parse an HTML ENTITY references
2868 * [68] EntityRef ::= '&' Name ';'
2870 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2871 * if non-NULL *str will have to be freed by the caller.
2873 const htmlEntityDesc
*
2874 htmlParseEntityRef(htmlParserCtxtPtr ctxt
, const xmlChar
**str
) {
2875 const xmlChar
*name
;
2876 const htmlEntityDesc
* ent
= NULL
;
2878 if (str
!= NULL
) *str
= NULL
;
2879 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) return(NULL
);
2883 name
= htmlParseName(ctxt
);
2885 htmlParseErr(ctxt
, XML_ERR_NAME_REQUIRED
,
2886 "htmlParseEntityRef: no name\n", NULL
, NULL
);
2894 * Lookup the entity in the table.
2896 ent
= htmlEntityLookup(name
);
2897 if (ent
!= NULL
) /* OK that's ugly !!! */
2900 htmlParseErr(ctxt
, XML_ERR_ENTITYREF_SEMICOL_MISSING
,
2901 "htmlParseEntityRef: expecting ';'\n",
2912 * htmlParseAttValue:
2913 * @ctxt: an HTML parser context
2915 * parse a value for an attribute
2916 * Note: the parser won't do substitution of entities here, this
2917 * will be handled later in xmlStringGetNodeList, unless it was
2918 * asked for ctxt->replaceEntities != 0
2920 * Returns the AttValue parsed or NULL.
2924 htmlParseAttValue(htmlParserCtxtPtr ctxt
) {
2925 xmlChar
*ret
= NULL
;
2929 ret
= htmlParseHTMLAttribute(ctxt
, '"');
2931 htmlParseErr(ctxt
, XML_ERR_ATTRIBUTE_NOT_FINISHED
,
2932 "AttValue: \" expected\n", NULL
, NULL
);
2935 } else if (CUR
== '\'') {
2937 ret
= htmlParseHTMLAttribute(ctxt
, '\'');
2939 htmlParseErr(ctxt
, XML_ERR_ATTRIBUTE_NOT_FINISHED
,
2940 "AttValue: ' expected\n", NULL
, NULL
);
2945 * That's an HTMLism, the attribute value may not be quoted
2947 ret
= htmlParseHTMLAttribute(ctxt
, 0);
2949 htmlParseErr(ctxt
, XML_ERR_ATTRIBUTE_WITHOUT_VALUE
,
2950 "AttValue: no value found\n", NULL
, NULL
);
2957 * htmlParseSystemLiteral:
2958 * @ctxt: an HTML parser context
2960 * parse an HTML Literal
2962 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2964 * Returns the SystemLiteral parsed or NULL
2968 htmlParseSystemLiteral(htmlParserCtxtPtr ctxt
) {
2969 size_t len
= 0, startPosition
= 0;
2972 xmlChar
*ret
= NULL
;
2974 if ((CUR
!= '"') && (CUR
!= '\'')) {
2975 htmlParseErr(ctxt
, XML_ERR_LITERAL_NOT_STARTED
,
2976 "SystemLiteral \" or ' expected\n", NULL
, NULL
);
2982 if (CUR_PTR
< BASE_PTR
)
2984 startPosition
= CUR_PTR
- BASE_PTR
;
2986 while ((CUR
!= 0) && (CUR
!= quote
)) {
2987 /* TODO: Handle UTF-8 */
2988 if (!IS_CHAR_CH(CUR
)) {
2989 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
2990 "Invalid char in SystemLiteral 0x%X\n", CUR
);
2997 htmlParseErr(ctxt
, XML_ERR_LITERAL_NOT_FINISHED
,
2998 "Unfinished SystemLiteral\n", NULL
, NULL
);
3002 ret
= xmlStrndup((BASE_PTR
+startPosition
), len
);
3009 * htmlParsePubidLiteral:
3010 * @ctxt: an HTML parser context
3012 * parse an HTML public literal
3014 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3016 * Returns the PubidLiteral parsed or NULL.
3020 htmlParsePubidLiteral(htmlParserCtxtPtr ctxt
) {
3021 size_t len
= 0, startPosition
= 0;
3024 xmlChar
*ret
= NULL
;
3026 if ((CUR
!= '"') && (CUR
!= '\'')) {
3027 htmlParseErr(ctxt
, XML_ERR_LITERAL_NOT_STARTED
,
3028 "PubidLiteral \" or ' expected\n", NULL
, NULL
);
3035 * Name ::= (Letter | '_') (NameChar)*
3037 if (CUR_PTR
< BASE_PTR
)
3039 startPosition
= CUR_PTR
- BASE_PTR
;
3041 while ((CUR
!= 0) && (CUR
!= quote
)) {
3042 if (!IS_PUBIDCHAR_CH(CUR
)) {
3043 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
3044 "Invalid char in PubidLiteral 0x%X\n", CUR
);
3052 htmlParseErr(ctxt
, XML_ERR_LITERAL_NOT_FINISHED
,
3053 "Unfinished PubidLiteral\n", NULL
, NULL
);
3057 ret
= xmlStrndup((BASE_PTR
+ startPosition
), len
);
3065 * @ctxt: an HTML parser context
3067 * parse the content of an HTML SCRIPT or STYLE element
3068 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
3069 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
3070 * http://www.w3.org/TR/html4/types.html#type-script
3071 * http://www.w3.org/TR/html4/types.html#h-6.15
3072 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
3074 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
3075 * element and the value of intrinsic event attributes. User agents must
3076 * not evaluate script data as HTML markup but instead must pass it on as
3077 * data to a script engine.
3079 * - The content is passed like CDATA
3080 * - the attributes for style and scripting "onXXX" are also described
3081 * as CDATA but SGML allows entities references in attributes so their
3082 * processing is identical as other attributes
3085 htmlParseScript(htmlParserCtxtPtr ctxt
) {
3086 xmlChar buf
[HTML_PARSER_BIG_BUFFER_SIZE
+ 5];
3093 if ((cur
== '<') && (NXT(1) == '/')) {
3095 * One should break here, the specification is clear:
3096 * Authors should therefore escape "</" within the content.
3097 * Escape mechanisms are specific to each scripting or
3098 * style sheet language.
3100 * In recovery mode, only break if end tag match the
3101 * current tag, effectively ignoring all tags inside the
3102 * script/style block and treating the entire block as
3105 if (ctxt
->recovery
) {
3106 if (xmlStrncasecmp(ctxt
->name
, ctxt
->input
->cur
+2,
3107 xmlStrlen(ctxt
->name
)) == 0)
3111 htmlParseErr(ctxt
, XML_ERR_TAG_NAME_MISMATCH
,
3112 "Element %s embeds close tag\n",
3116 if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
3117 ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
3124 COPY_BUF(l
,buf
,nbchar
,cur
);
3126 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
3127 "Invalid char in CDATA 0x%X\n", cur
);
3129 if (nbchar
>= HTML_PARSER_BIG_BUFFER_SIZE
) {
3131 if (ctxt
->sax
->cdataBlock
!= NULL
) {
3133 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
3135 ctxt
->sax
->cdataBlock(ctxt
->userData
, buf
, nbchar
);
3136 } else if (ctxt
->sax
->characters
!= NULL
) {
3137 ctxt
->sax
->characters(ctxt
->userData
, buf
, nbchar
);
3146 if ((nbchar
!= 0) && (ctxt
->sax
!= NULL
) && (!ctxt
->disableSAX
)) {
3148 if (ctxt
->sax
->cdataBlock
!= NULL
) {
3150 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
3152 ctxt
->sax
->cdataBlock(ctxt
->userData
, buf
, nbchar
);
3153 } else if (ctxt
->sax
->characters
!= NULL
) {
3154 ctxt
->sax
->characters(ctxt
->userData
, buf
, nbchar
);
3161 * htmlParseCharDataInternal:
3162 * @ctxt: an HTML parser context
3163 * @readahead: optional read ahead character in ascii range
3165 * parse a CharData section.
3166 * if we are within a CDATA section ']]>' marks an end of section.
3168 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3172 htmlParseCharDataInternal(htmlParserCtxtPtr ctxt
, int readahead
) {
3173 xmlChar buf
[HTML_PARSER_BIG_BUFFER_SIZE
+ 6];
3179 buf
[nbchar
++] = readahead
;
3183 while (((cur
!= '<') || (ctxt
->token
== '<')) &&
3184 ((cur
!= '&') || (ctxt
->token
== '&')) &&
3186 if (!(IS_CHAR(cur
))) {
3187 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
3188 "Invalid char in CDATA 0x%X\n", cur
);
3190 COPY_BUF(l
,buf
,nbchar
,cur
);
3192 if (nbchar
>= HTML_PARSER_BIG_BUFFER_SIZE
) {
3196 * Ok the segment is to be consumed as chars.
3198 if ((ctxt
->sax
!= NULL
) && (!ctxt
->disableSAX
)) {
3199 if (areBlanks(ctxt
, buf
, nbchar
)) {
3200 if (ctxt
->keepBlanks
) {
3201 if (ctxt
->sax
->characters
!= NULL
)
3202 ctxt
->sax
->characters(ctxt
->userData
, buf
, nbchar
);
3204 if (ctxt
->sax
->ignorableWhitespace
!= NULL
)
3205 ctxt
->sax
->ignorableWhitespace(ctxt
->userData
,
3209 htmlCheckParagraph(ctxt
);
3210 if (ctxt
->sax
->characters
!= NULL
)
3211 ctxt
->sax
->characters(ctxt
->userData
, buf
, nbchar
);
3218 if (chunk
> HTML_PARSER_BUFFER_SIZE
) {
3234 * Ok the segment is to be consumed as chars.
3236 if ((ctxt
->sax
!= NULL
) && (!ctxt
->disableSAX
)) {
3237 if (areBlanks(ctxt
, buf
, nbchar
)) {
3238 if (ctxt
->keepBlanks
) {
3239 if (ctxt
->sax
->characters
!= NULL
)
3240 ctxt
->sax
->characters(ctxt
->userData
, buf
, nbchar
);
3242 if (ctxt
->sax
->ignorableWhitespace
!= NULL
)
3243 ctxt
->sax
->ignorableWhitespace(ctxt
->userData
,
3247 htmlCheckParagraph(ctxt
);
3248 if (ctxt
->sax
->characters
!= NULL
)
3249 ctxt
->sax
->characters(ctxt
->userData
, buf
, nbchar
);
3257 ctxt
->instate
= XML_PARSER_EOF
;
3262 * htmlParseCharData:
3263 * @ctxt: an HTML parser context
3265 * parse a CharData section.
3266 * if we are within a CDATA section ']]>' marks an end of section.
3268 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3272 htmlParseCharData(htmlParserCtxtPtr ctxt
) {
3273 htmlParseCharDataInternal(ctxt
, 0);
3277 * htmlParseExternalID:
3278 * @ctxt: an HTML parser context
3279 * @publicID: a xmlChar** receiving PubidLiteral
3281 * Parse an External ID or a Public ID
3283 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3284 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3286 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3288 * Returns the function returns SystemLiteral and in the second
3289 * case publicID receives PubidLiteral, is strict is off
3290 * it is possible to return NULL and have publicID set.
3294 htmlParseExternalID(htmlParserCtxtPtr ctxt
, xmlChar
**publicID
) {
3295 xmlChar
*URI
= NULL
;
3297 if ((UPPER
== 'S') && (UPP(1) == 'Y') &&
3298 (UPP(2) == 'S') && (UPP(3) == 'T') &&
3299 (UPP(4) == 'E') && (UPP(5) == 'M')) {
3301 if (!IS_BLANK_CH(CUR
)) {
3302 htmlParseErr(ctxt
, XML_ERR_SPACE_REQUIRED
,
3303 "Space required after 'SYSTEM'\n", NULL
, NULL
);
3306 URI
= htmlParseSystemLiteral(ctxt
);
3308 htmlParseErr(ctxt
, XML_ERR_URI_REQUIRED
,
3309 "htmlParseExternalID: SYSTEM, no URI\n", NULL
, NULL
);
3311 } else if ((UPPER
== 'P') && (UPP(1) == 'U') &&
3312 (UPP(2) == 'B') && (UPP(3) == 'L') &&
3313 (UPP(4) == 'I') && (UPP(5) == 'C')) {
3315 if (!IS_BLANK_CH(CUR
)) {
3316 htmlParseErr(ctxt
, XML_ERR_SPACE_REQUIRED
,
3317 "Space required after 'PUBLIC'\n", NULL
, NULL
);
3320 *publicID
= htmlParsePubidLiteral(ctxt
);
3321 if (*publicID
== NULL
) {
3322 htmlParseErr(ctxt
, XML_ERR_PUBID_REQUIRED
,
3323 "htmlParseExternalID: PUBLIC, no Public Identifier\n",
3327 if ((CUR
== '"') || (CUR
== '\'')) {
3328 URI
= htmlParseSystemLiteral(ctxt
);
3336 * @ctxt: an XML parser context
3338 * parse an XML Processing Instruction.
3340 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3343 htmlParsePI(htmlParserCtxtPtr ctxt
) {
3344 xmlChar
*buf
= NULL
;
3346 int size
= HTML_PARSER_BUFFER_SIZE
;
3348 const xmlChar
*target
;
3349 xmlParserInputState state
;
3352 if ((RAW
== '<') && (NXT(1) == '?')) {
3353 state
= ctxt
->instate
;
3354 ctxt
->instate
= XML_PARSER_PI
;
3356 * this is a Processing Instruction.
3362 * Parse the target name and check for special support like
3365 target
= htmlParseName(ctxt
);
3366 if (target
!= NULL
) {
3373 if ((ctxt
->sax
) && (!ctxt
->disableSAX
) &&
3374 (ctxt
->sax
->processingInstruction
!= NULL
))
3375 ctxt
->sax
->processingInstruction(ctxt
->userData
,
3377 ctxt
->instate
= state
;
3380 buf
= (xmlChar
*) xmlMallocAtomic(size
* sizeof(xmlChar
));
3382 htmlErrMemory(ctxt
, NULL
);
3383 ctxt
->instate
= state
;
3387 if (!IS_BLANK(cur
)) {
3388 htmlParseErr(ctxt
, XML_ERR_SPACE_REQUIRED
,
3389 "ParsePI: PI %s space expected\n", target
, NULL
);
3393 while ((cur
!= 0) && (cur
!= '>')) {
3394 if (len
+ 5 >= size
) {
3398 tmp
= (xmlChar
*) xmlRealloc(buf
, size
* sizeof(xmlChar
));
3400 htmlErrMemory(ctxt
, NULL
);
3402 ctxt
->instate
= state
;
3413 COPY_BUF(l
,buf
,len
,cur
);
3415 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
3416 "Invalid char in processing instruction "
3429 htmlParseErr(ctxt
, XML_ERR_PI_NOT_FINISHED
,
3430 "ParsePI: PI %s never end ...\n", target
, NULL
);
3437 if ((ctxt
->sax
) && (!ctxt
->disableSAX
) &&
3438 (ctxt
->sax
->processingInstruction
!= NULL
))
3439 ctxt
->sax
->processingInstruction(ctxt
->userData
,
3444 htmlParseErr(ctxt
, XML_ERR_PI_NOT_STARTED
,
3445 "PI is not started correctly", NULL
, NULL
);
3447 ctxt
->instate
= state
;
3453 * @ctxt: an HTML parser context
3455 * Parse an XML (SGML) comment <!-- .... -->
3457 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3460 htmlParseComment(htmlParserCtxtPtr ctxt
) {
3461 xmlChar
*buf
= NULL
;
3463 int size
= HTML_PARSER_BUFFER_SIZE
;
3468 xmlParserInputState state
;
3471 * Check that there is a comment right here.
3473 if ((RAW
!= '<') || (NXT(1) != '!') ||
3474 (NXT(2) != '-') || (NXT(3) != '-')) return;
3476 state
= ctxt
->instate
;
3477 ctxt
->instate
= XML_PARSER_COMMENT
;
3480 buf
= (xmlChar
*) xmlMallocAtomic(size
* sizeof(xmlChar
));
3482 htmlErrMemory(ctxt
, "buffer allocation failed\n");
3483 ctxt
->instate
= state
;
3497 while ((cur
!= 0) &&
3499 (r
!= '-') || (q
!= '-'))) {
3501 next
= CUR_CHAR(nl
);
3505 next
= CUR_CHAR(nl
);
3508 if ((q
== '-') && (r
== '-') && (cur
== '!') && (next
== '>')) {
3509 htmlParseErr(ctxt
, XML_ERR_COMMENT_NOT_FINISHED
,
3510 "Comment incorrectly closed by '--!>'", NULL
, NULL
);
3515 if (len
+ 5 >= size
) {
3519 tmp
= (xmlChar
*) xmlRealloc(buf
, size
* sizeof(xmlChar
));
3522 htmlErrMemory(ctxt
, "growing buffer failed\n");
3523 ctxt
->instate
= state
;
3529 COPY_BUF(ql
,buf
,len
,q
);
3531 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
3532 "Invalid char in comment 0x%X\n", q
);
3545 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->comment
!= NULL
) &&
3546 (!ctxt
->disableSAX
))
3547 ctxt
->sax
->comment(ctxt
->userData
, buf
);
3549 ctxt
->instate
= state
;
3554 htmlParseErr(ctxt
, XML_ERR_COMMENT_NOT_FINISHED
,
3555 "Comment not terminated \n<!--%.50s\n", buf
, NULL
);
3561 * @ctxt: an HTML parser context
3563 * parse Reference declarations
3565 * [66] CharRef ::= '&#' [0-9]+ ';' |
3566 * '&#x' [0-9a-fA-F]+ ';'
3568 * Returns the value parsed (as an int)
3571 htmlParseCharRef(htmlParserCtxtPtr ctxt
) {
3574 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) {
3575 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
3576 "htmlParseCharRef: context error\n",
3580 if ((CUR
== '&') && (NXT(1) == '#') &&
3581 ((NXT(2) == 'x') || NXT(2) == 'X')) {
3583 while (CUR
!= ';') {
3584 if ((CUR
>= '0') && (CUR
<= '9')) {
3586 val
= val
* 16 + (CUR
- '0');
3587 } else if ((CUR
>= 'a') && (CUR
<= 'f')) {
3589 val
= val
* 16 + (CUR
- 'a') + 10;
3590 } else if ((CUR
>= 'A') && (CUR
<= 'F')) {
3592 val
= val
* 16 + (CUR
- 'A') + 10;
3594 htmlParseErr(ctxt
, XML_ERR_INVALID_HEX_CHARREF
,
3595 "htmlParseCharRef: missing semicolon\n",
3603 } else if ((CUR
== '&') && (NXT(1) == '#')) {
3605 while (CUR
!= ';') {
3606 if ((CUR
>= '0') && (CUR
<= '9')) {
3608 val
= val
* 10 + (CUR
- '0');
3610 htmlParseErr(ctxt
, XML_ERR_INVALID_DEC_CHARREF
,
3611 "htmlParseCharRef: missing semicolon\n",
3620 htmlParseErr(ctxt
, XML_ERR_INVALID_CHARREF
,
3621 "htmlParseCharRef: invalid value\n", NULL
, NULL
);
3624 * Check the value IS_CHAR ...
3628 } else if (val
>= 0x110000) {
3629 htmlParseErr(ctxt
, XML_ERR_INVALID_CHAR
,
3630 "htmlParseCharRef: value too large\n", NULL
, NULL
);
3632 htmlParseErrInt(ctxt
, XML_ERR_INVALID_CHAR
,
3633 "htmlParseCharRef: invalid xmlChar value %d\n",
3641 * htmlParseDocTypeDecl:
3642 * @ctxt: an HTML parser context
3644 * parse a DOCTYPE declaration
3646 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3647 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3651 htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt
) {
3652 const xmlChar
*name
;
3653 xmlChar
*ExternalID
= NULL
;
3654 xmlChar
*URI
= NULL
;
3657 * We know that '<!DOCTYPE' has been detected.
3664 * Parse the DOCTYPE name.
3666 name
= htmlParseName(ctxt
);
3668 htmlParseErr(ctxt
, XML_ERR_NAME_REQUIRED
,
3669 "htmlParseDocTypeDecl : no DOCTYPE name !\n",
3673 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
3679 * Check for SystemID and ExternalID
3681 URI
= htmlParseExternalID(ctxt
, &ExternalID
);
3685 * We should be at the end of the DOCTYPE declaration.
3688 htmlParseErr(ctxt
, XML_ERR_DOCTYPE_NOT_FINISHED
,
3689 "DOCTYPE improperly terminated\n", NULL
, NULL
);
3690 /* Ignore bogus content */
3691 while ((CUR
!= 0) && (CUR
!= '>'))
3698 * Create or update the document accordingly to the DOCTYPE
3700 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->internalSubset
!= NULL
) &&
3701 (!ctxt
->disableSAX
))
3702 ctxt
->sax
->internalSubset(ctxt
->userData
, name
, ExternalID
, URI
);
3705 * Cleanup, since we don't use all those identifiers
3707 if (URI
!= NULL
) xmlFree(URI
);
3708 if (ExternalID
!= NULL
) xmlFree(ExternalID
);
3712 * htmlParseAttribute:
3713 * @ctxt: an HTML parser context
3714 * @value: a xmlChar ** used to store the value of the attribute
3716 * parse an attribute
3718 * [41] Attribute ::= Name Eq AttValue
3720 * [25] Eq ::= S? '=' S?
3724 * [NS 11] Attribute ::= QName Eq AttValue
3726 * Also the case QName == xmlns:??? is handled independently as a namespace
3729 * Returns the attribute name, and the value in *value.
3732 static const xmlChar
*
3733 htmlParseAttribute(htmlParserCtxtPtr ctxt
, xmlChar
**value
) {
3734 const xmlChar
*name
;
3735 xmlChar
*val
= NULL
;
3738 name
= htmlParseHTMLName(ctxt
);
3740 htmlParseErr(ctxt
, XML_ERR_NAME_REQUIRED
,
3741 "error parsing attribute name\n", NULL
, NULL
);
3752 val
= htmlParseAttValue(ctxt
);
3760 * htmlCheckEncodingDirect:
3761 * @ctxt: an HTML parser context
3762 * @attvalue: the attribute value
3764 * Checks an attribute value to detect
3766 * If a new encoding is detected the parser is switched to decode
3770 htmlCheckEncodingDirect(htmlParserCtxtPtr ctxt
, const xmlChar
*encoding
) {
3772 if ((ctxt
== NULL
) || (encoding
== NULL
) ||
3773 (ctxt
->options
& HTML_PARSE_IGNORE_ENC
))
3776 /* do not change encoding */
3777 if (ctxt
->input
->encoding
!= NULL
)
3780 if (encoding
!= NULL
) {
3781 xmlCharEncoding enc
;
3782 xmlCharEncodingHandlerPtr handler
;
3784 while ((*encoding
== ' ') || (*encoding
== '\t')) encoding
++;
3786 if (ctxt
->input
->encoding
!= NULL
)
3787 xmlFree((xmlChar
*) ctxt
->input
->encoding
);
3788 ctxt
->input
->encoding
= xmlStrdup(encoding
);
3790 enc
= xmlParseCharEncoding((const char *) encoding
);
3792 * registered set of known encodings
3794 if (enc
!= XML_CHAR_ENCODING_ERROR
) {
3795 if (((enc
== XML_CHAR_ENCODING_UTF16LE
) ||
3796 (enc
== XML_CHAR_ENCODING_UTF16BE
) ||
3797 (enc
== XML_CHAR_ENCODING_UCS4LE
) ||
3798 (enc
== XML_CHAR_ENCODING_UCS4BE
)) &&
3799 (ctxt
->input
->buf
!= NULL
) &&
3800 (ctxt
->input
->buf
->encoder
== NULL
)) {
3801 htmlParseErr(ctxt
, XML_ERR_INVALID_ENCODING
,
3802 "htmlCheckEncoding: wrong encoding meta\n",
3805 xmlSwitchEncoding(ctxt
, enc
);
3807 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
3810 * fallback for unknown encodings
3812 handler
= xmlFindCharEncodingHandler((const char *) encoding
);
3813 if (handler
!= NULL
) {
3814 xmlSwitchToEncoding(ctxt
, handler
);
3815 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
3817 htmlParseErr(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
3818 "htmlCheckEncoding: unknown encoding %s\n",
3823 if ((ctxt
->input
->buf
!= NULL
) &&
3824 (ctxt
->input
->buf
->encoder
!= NULL
) &&
3825 (ctxt
->input
->buf
->raw
!= NULL
) &&
3826 (ctxt
->input
->buf
->buffer
!= NULL
)) {
3831 * convert as much as possible to the parser reading buffer.
3833 processed
= ctxt
->input
->cur
- ctxt
->input
->base
;
3834 xmlBufShrink(ctxt
->input
->buf
->buffer
, processed
);
3835 nbchars
= xmlCharEncInput(ctxt
->input
->buf
, 1);
3836 xmlBufResetInput(ctxt
->input
->buf
->buffer
, ctxt
->input
);
3838 htmlParseErr(ctxt
, XML_ERR_INVALID_ENCODING
,
3839 "htmlCheckEncoding: encoder error\n",
3847 * htmlCheckEncoding:
3848 * @ctxt: an HTML parser context
3849 * @attvalue: the attribute value
3851 * Checks an http-equiv attribute from a Meta tag to detect
3853 * If a new encoding is detected the parser is switched to decode
3857 htmlCheckEncoding(htmlParserCtxtPtr ctxt
, const xmlChar
*attvalue
) {
3858 const xmlChar
*encoding
;
3863 encoding
= xmlStrcasestr(attvalue
, BAD_CAST
"charset");
3864 if (encoding
!= NULL
) {
3870 if (encoding
&& IS_BLANK_CH(*encoding
))
3871 encoding
= xmlStrcasestr(attvalue
, BAD_CAST
"=");
3872 if (encoding
&& *encoding
== '=') {
3874 htmlCheckEncodingDirect(ctxt
, encoding
);
3880 * @ctxt: an HTML parser context
3881 * @atts: the attributes values
3883 * Checks an attributes from a Meta tag
3886 htmlCheckMeta(htmlParserCtxtPtr ctxt
, const xmlChar
**atts
) {
3888 const xmlChar
*att
, *value
;
3890 const xmlChar
*content
= NULL
;
3892 if ((ctxt
== NULL
) || (atts
== NULL
))
3897 while (att
!= NULL
) {
3899 if ((value
!= NULL
) && (!xmlStrcasecmp(att
, BAD_CAST
"http-equiv"))
3900 && (!xmlStrcasecmp(value
, BAD_CAST
"Content-Type")))
3902 else if ((value
!= NULL
) && (!xmlStrcasecmp(att
, BAD_CAST
"charset")))
3903 htmlCheckEncodingDirect(ctxt
, value
);
3904 else if ((value
!= NULL
) && (!xmlStrcasecmp(att
, BAD_CAST
"content")))
3908 if ((http
) && (content
!= NULL
))
3909 htmlCheckEncoding(ctxt
, content
);
3914 * htmlParseStartTag:
3915 * @ctxt: an HTML parser context
3917 * parse a start of tag either for rule element or
3918 * EmptyElement. In both case we don't parse the tag closing chars.
3920 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3922 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3926 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3928 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3930 * Returns 0 in case of success, -1 in case of error and 1 if discarded
3934 htmlParseStartTag(htmlParserCtxtPtr ctxt
) {
3935 const xmlChar
*name
;
3936 const xmlChar
*attname
;
3938 const xmlChar
**atts
;
3945 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) {
3946 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
3947 "htmlParseStartTag: context error\n", NULL
, NULL
);
3950 if (ctxt
->instate
== XML_PARSER_EOF
)
3952 if (CUR
!= '<') return -1;
3956 maxatts
= ctxt
->maxatts
;
3959 name
= htmlParseHTMLName(ctxt
);
3961 htmlParseErr(ctxt
, XML_ERR_NAME_REQUIRED
,
3962 "htmlParseStartTag: invalid element name\n",
3964 /* Dump the bogus tag like browsers do */
3965 while ((CUR
!= 0) && (CUR
!= '>') &&
3966 (ctxt
->instate
!= XML_PARSER_EOF
))
3970 if (xmlStrEqual(name
, BAD_CAST
"meta"))
3974 * Check for auto-closure of HTML elements.
3976 htmlAutoClose(ctxt
, name
);
3979 * Check for implied HTML elements.
3981 htmlCheckImplied(ctxt
, name
);
3984 * Avoid html at any level > 0, head at any level != 1
3985 * or any attempt to recurse body
3987 if ((ctxt
->nameNr
> 0) && (xmlStrEqual(name
, BAD_CAST
"html"))) {
3988 htmlParseErr(ctxt
, XML_HTML_STRUCURE_ERROR
,
3989 "htmlParseStartTag: misplaced <html> tag\n",
3994 if ((ctxt
->nameNr
!= 1) &&
3995 (xmlStrEqual(name
, BAD_CAST
"head"))) {
3996 htmlParseErr(ctxt
, XML_HTML_STRUCURE_ERROR
,
3997 "htmlParseStartTag: misplaced <head> tag\n",
4002 if (xmlStrEqual(name
, BAD_CAST
"body")) {
4004 for (indx
= 0;indx
< ctxt
->nameNr
;indx
++) {
4005 if (xmlStrEqual(ctxt
->nameTab
[indx
], BAD_CAST
"body")) {
4006 htmlParseErr(ctxt
, XML_HTML_STRUCURE_ERROR
,
4007 "htmlParseStartTag: misplaced <body> tag\n",
4016 * Now parse the attributes, it ends up with the ending
4021 while ((CUR
!= 0) &&
4023 ((CUR
!= '/') || (NXT(1) != '>'))) {
4025 attname
= htmlParseAttribute(ctxt
, &attvalue
);
4026 if (attname
!= NULL
) {
4029 * Well formedness requires at most one declaration of an attribute
4031 for (i
= 0; i
< nbatts
;i
+= 2) {
4032 if (xmlStrEqual(atts
[i
], attname
)) {
4033 htmlParseErr(ctxt
, XML_ERR_ATTRIBUTE_REDEFINED
,
4034 "Attribute %s redefined\n", attname
, NULL
);
4035 if (attvalue
!= NULL
)
4042 * Add the pair to atts
4045 maxatts
= 22; /* allow for 10 attrs by default */
4046 atts
= (const xmlChar
**)
4047 xmlMalloc(maxatts
* sizeof(xmlChar
*));
4049 htmlErrMemory(ctxt
, NULL
);
4050 if (attvalue
!= NULL
)
4055 ctxt
->maxatts
= maxatts
;
4056 } else if (nbatts
+ 4 > maxatts
) {
4060 n
= (const xmlChar
**) xmlRealloc((void *) atts
,
4061 maxatts
* sizeof(const xmlChar
*));
4063 htmlErrMemory(ctxt
, NULL
);
4064 if (attvalue
!= NULL
)
4070 ctxt
->maxatts
= maxatts
;
4072 atts
[nbatts
++] = attname
;
4073 atts
[nbatts
++] = attvalue
;
4074 atts
[nbatts
] = NULL
;
4075 atts
[nbatts
+ 1] = NULL
;
4078 if (attvalue
!= NULL
)
4080 /* Dump the bogus attribute string up to the next blank or
4081 * the end of the tag. */
4082 while ((CUR
!= 0) &&
4083 !(IS_BLANK_CH(CUR
)) && (CUR
!= '>') &&
4084 ((CUR
!= '/') || (NXT(1) != '>')))
4093 * Handle specific association to the META tag
4095 if (meta
&& (nbatts
!= 0))
4096 htmlCheckMeta(ctxt
, atts
);
4099 * SAX: Start of Element !
4102 htmlnamePush(ctxt
, name
);
4103 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->startElement
!= NULL
)) {
4105 ctxt
->sax
->startElement(ctxt
->userData
, name
, atts
);
4107 ctxt
->sax
->startElement(ctxt
->userData
, name
, NULL
);
4112 for (i
= 1;i
< nbatts
;i
+= 2) {
4113 if (atts
[i
] != NULL
)
4114 xmlFree((xmlChar
*) atts
[i
]);
4123 * @ctxt: an HTML parser context
4125 * parse an end of tag
4127 * [42] ETag ::= '</' Name S? '>'
4131 * [NS 9] ETag ::= '</' QName S? '>'
4133 * Returns 1 if the current level should be closed.
4137 htmlParseEndTag(htmlParserCtxtPtr ctxt
)
4139 const xmlChar
*name
;
4140 const xmlChar
*oldname
;
4143 if ((CUR
!= '<') || (NXT(1) != '/')) {
4144 htmlParseErr(ctxt
, XML_ERR_LTSLASH_REQUIRED
,
4145 "htmlParseEndTag: '</' not found\n", NULL
, NULL
);
4150 name
= htmlParseHTMLName(ctxt
);
4154 * We should definitely be at the ending "S? '>'" part
4158 htmlParseErr(ctxt
, XML_ERR_GT_REQUIRED
,
4159 "End tag : expected '>'\n", NULL
, NULL
);
4160 /* Skip to next '>' */
4161 while ((CUR
!= 0) && (CUR
!= '>'))
4168 * if we ignored misplaced tags in htmlParseStartTag don't pop them
4171 if ((ctxt
->depth
> 0) &&
4172 (xmlStrEqual(name
, BAD_CAST
"html") ||
4173 xmlStrEqual(name
, BAD_CAST
"body") ||
4174 xmlStrEqual(name
, BAD_CAST
"head"))) {
4180 * If the name read is not one of the element in the parsing stack
4181 * then return, it's just an error.
4183 for (i
= (ctxt
->nameNr
- 1); i
>= 0; i
--) {
4184 if (xmlStrEqual(name
, ctxt
->nameTab
[i
]))
4188 htmlParseErr(ctxt
, XML_ERR_TAG_NAME_MISMATCH
,
4189 "Unexpected end tag : %s\n", name
, NULL
);
4195 * Check for auto-closure of HTML elements.
4198 htmlAutoCloseOnClose(ctxt
, name
);
4201 * Well formedness constraints, opening and closing must match.
4202 * With the exception that the autoclose may have popped stuff out
4205 if ((ctxt
->name
!= NULL
) && (!xmlStrEqual(ctxt
->name
, name
))) {
4206 htmlParseErr(ctxt
, XML_ERR_TAG_NAME_MISMATCH
,
4207 "Opening and ending tag mismatch: %s and %s\n",
4214 oldname
= ctxt
->name
;
4215 if ((oldname
!= NULL
) && (xmlStrEqual(oldname
, name
))) {
4216 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
4217 ctxt
->sax
->endElement(ctxt
->userData
, name
);
4218 htmlNodeInfoPop(ctxt
);
4230 * htmlParseReference:
4231 * @ctxt: an HTML parser context
4233 * parse and handle entity references in content,
4234 * this will end-up in a call to character() since this is either a
4235 * CharRef, or a predefined entity.
4238 htmlParseReference(htmlParserCtxtPtr ctxt
) {
4239 const htmlEntityDesc
* ent
;
4241 const xmlChar
*name
;
4242 if (CUR
!= '&') return;
4244 if (NXT(1) == '#') {
4248 c
= htmlParseCharRef(ctxt
);
4252 if (c
< 0x80) { out
[i
++]= c
; bits
= -6; }
4253 else if (c
< 0x800) { out
[i
++]=((c
>> 6) & 0x1F) | 0xC0; bits
= 0; }
4254 else if (c
< 0x10000) { out
[i
++]=((c
>> 12) & 0x0F) | 0xE0; bits
= 6; }
4255 else { out
[i
++]=((c
>> 18) & 0x07) | 0xF0; bits
= 12; }
4257 for ( ; bits
>= 0; bits
-= 6) {
4258 out
[i
++]= ((c
>> bits
) & 0x3F) | 0x80;
4262 htmlCheckParagraph(ctxt
);
4263 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->characters
!= NULL
))
4264 ctxt
->sax
->characters(ctxt
->userData
, out
, i
);
4266 ent
= htmlParseEntityRef(ctxt
, &name
);
4268 htmlCheckParagraph(ctxt
);
4269 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->characters
!= NULL
))
4270 ctxt
->sax
->characters(ctxt
->userData
, BAD_CAST
"&", 1);
4273 if ((ent
== NULL
) || !(ent
->value
> 0)) {
4274 htmlCheckParagraph(ctxt
);
4275 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->characters
!= NULL
)) {
4276 ctxt
->sax
->characters(ctxt
->userData
, BAD_CAST
"&", 1);
4277 ctxt
->sax
->characters(ctxt
->userData
, name
, xmlStrlen(name
));
4278 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
4286 { out
[i
++]= c
; bits
= -6; }
4288 { out
[i
++]=((c
>> 6) & 0x1F) | 0xC0; bits
= 0; }
4289 else if (c
< 0x10000)
4290 { out
[i
++]=((c
>> 12) & 0x0F) | 0xE0; bits
= 6; }
4292 { out
[i
++]=((c
>> 18) & 0x07) | 0xF0; bits
= 12; }
4294 for ( ; bits
>= 0; bits
-= 6) {
4295 out
[i
++]= ((c
>> bits
) & 0x3F) | 0x80;
4299 htmlCheckParagraph(ctxt
);
4300 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->characters
!= NULL
))
4301 ctxt
->sax
->characters(ctxt
->userData
, out
, i
);
4308 * @ctxt: an HTML parser context
4310 * Parse a content: comment, sub-element, reference or text.
4311 * Kept for compatibility with old code
4315 htmlParseContent(htmlParserCtxtPtr ctxt
) {
4316 xmlChar
*currentNode
;
4318 const xmlChar
*name
;
4320 currentNode
= xmlStrdup(ctxt
->name
);
4321 depth
= ctxt
->nameNr
;
4325 if (ctxt
->instate
== XML_PARSER_EOF
)
4329 * Our tag or one of it's parent or children is ending.
4331 if ((CUR
== '<') && (NXT(1) == '/')) {
4332 if (htmlParseEndTag(ctxt
) &&
4333 ((currentNode
!= NULL
) || (ctxt
->nameNr
== 0))) {
4334 if (currentNode
!= NULL
)
4335 xmlFree(currentNode
);
4338 continue; /* while */
4341 else if ((CUR
== '<') &&
4342 ((IS_ASCII_LETTER(NXT(1))) ||
4343 (NXT(1) == '_') || (NXT(1) == ':'))) {
4344 name
= htmlParseHTMLName_nonInvasive(ctxt
);
4346 htmlParseErr(ctxt
, XML_ERR_NAME_REQUIRED
,
4347 "htmlParseStartTag: invalid element name\n",
4349 /* Dump the bogus tag like browsers do */
4350 while ((CUR
!= 0) && (CUR
!= '>'))
4353 if (currentNode
!= NULL
)
4354 xmlFree(currentNode
);
4358 if (ctxt
->name
!= NULL
) {
4359 if (htmlCheckAutoClose(name
, ctxt
->name
) == 1) {
4360 htmlAutoClose(ctxt
, name
);
4367 * Has this node been popped out during parsing of
4370 if ((ctxt
->nameNr
> 0) && (depth
>= ctxt
->nameNr
) &&
4371 (!xmlStrEqual(currentNode
, ctxt
->name
)))
4373 if (currentNode
!= NULL
) xmlFree(currentNode
);
4377 if ((CUR
!= 0) && ((xmlStrEqual(currentNode
, BAD_CAST
"script")) ||
4378 (xmlStrEqual(currentNode
, BAD_CAST
"style")))) {
4380 * Handle SCRIPT/STYLE separately
4382 htmlParseScript(ctxt
);
4385 * Sometimes DOCTYPE arrives in the middle of the document
4387 if ((CUR
== '<') && (NXT(1) == '!') &&
4388 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4389 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4390 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4392 htmlParseErr(ctxt
, XML_HTML_STRUCURE_ERROR
,
4393 "Misplaced DOCTYPE declaration\n",
4394 BAD_CAST
"DOCTYPE" , NULL
);
4395 htmlParseDocTypeDecl(ctxt
);
4399 * First case : a comment
4401 if ((CUR
== '<') && (NXT(1) == '!') &&
4402 (NXT(2) == '-') && (NXT(3) == '-')) {
4403 htmlParseComment(ctxt
);
4407 * Second case : a Processing Instruction.
4409 else if ((CUR
== '<') && (NXT(1) == '?')) {
4414 * Third case : a sub-element.
4416 else if ((CUR
== '<') && IS_ASCII_LETTER(NXT(1))) {
4417 htmlParseElement(ctxt
);
4419 else if (CUR
== '<') {
4420 if ((ctxt
->sax
!= NULL
) && (!ctxt
->disableSAX
) &&
4421 (ctxt
->sax
->characters
!= NULL
))
4422 ctxt
->sax
->characters(ctxt
->userData
, BAD_CAST
"<", 1);
4427 * Fourth case : a reference. If if has not been resolved,
4428 * parsing returns it's Name, create the node
4430 else if (CUR
== '&') {
4431 htmlParseReference(ctxt
);
4435 * Fifth case : end of the resource
4437 else if (CUR
== 0) {
4438 htmlAutoCloseOnEnd(ctxt
);
4443 * Last case, text. Note that References are handled directly.
4446 htmlParseCharData(ctxt
);
4451 if (currentNode
!= NULL
) xmlFree(currentNode
);
4456 * @ctxt: an HTML parser context
4458 * parse an HTML element, this is highly recursive
4459 * this is kept for compatibility with previous code versions
4461 * [39] element ::= EmptyElemTag | STag content ETag
4463 * [41] Attribute ::= Name Eq AttValue
4467 htmlParseElement(htmlParserCtxtPtr ctxt
) {
4468 const xmlChar
*name
;
4469 xmlChar
*currentNode
= NULL
;
4470 const htmlElemDesc
* info
;
4471 htmlParserNodeInfo node_info
;
4474 const xmlChar
*oldptr
;
4476 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) {
4477 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
4478 "htmlParseElement: context error\n", NULL
, NULL
);
4482 if (ctxt
->instate
== XML_PARSER_EOF
)
4485 /* Capture start position */
4486 if (ctxt
->record_info
) {
4487 node_info
.begin_pos
= ctxt
->input
->consumed
+
4488 (CUR_PTR
- ctxt
->input
->base
);
4489 node_info
.begin_line
= ctxt
->input
->line
;
4492 failed
= htmlParseStartTag(ctxt
);
4494 if ((failed
== -1) || (name
== NULL
)) {
4501 * Lookup the info for that element.
4503 info
= htmlTagLookup(name
);
4505 htmlParseErr(ctxt
, XML_HTML_UNKNOWN_TAG
,
4506 "Tag %s invalid\n", name
, NULL
);
4510 * Check for an Empty Element labeled the XML/SGML way
4512 if ((CUR
== '/') && (NXT(1) == '>')) {
4514 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
4515 ctxt
->sax
->endElement(ctxt
->userData
, name
);
4523 htmlParseErr(ctxt
, XML_ERR_GT_REQUIRED
,
4524 "Couldn't find end of Start Tag %s\n", name
, NULL
);
4527 * end of parsing of this node.
4529 if (xmlStrEqual(name
, ctxt
->name
)) {
4535 * Capture end position and add node
4537 if (ctxt
->record_info
) {
4538 node_info
.end_pos
= ctxt
->input
->consumed
+
4539 (CUR_PTR
- ctxt
->input
->base
);
4540 node_info
.end_line
= ctxt
->input
->line
;
4541 node_info
.node
= ctxt
->node
;
4542 xmlParserAddNodeInfo(ctxt
, &node_info
);
4548 * Check for an Empty Element from DTD definition
4550 if ((info
!= NULL
) && (info
->empty
)) {
4551 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
4552 ctxt
->sax
->endElement(ctxt
->userData
, name
);
4558 * Parse the content of the element:
4560 currentNode
= xmlStrdup(ctxt
->name
);
4561 depth
= ctxt
->nameNr
;
4563 oldptr
= ctxt
->input
->cur
;
4564 htmlParseContent(ctxt
);
4565 if (oldptr
==ctxt
->input
->cur
) break;
4566 if (ctxt
->nameNr
< depth
) break;
4570 * Capture end position and add node
4572 if ( currentNode
!= NULL
&& ctxt
->record_info
) {
4573 node_info
.end_pos
= ctxt
->input
->consumed
+
4574 (CUR_PTR
- ctxt
->input
->base
);
4575 node_info
.end_line
= ctxt
->input
->line
;
4576 node_info
.node
= ctxt
->node
;
4577 xmlParserAddNodeInfo(ctxt
, &node_info
);
4580 htmlAutoCloseOnEnd(ctxt
);
4583 if (currentNode
!= NULL
)
4584 xmlFree(currentNode
);
4588 htmlParserFinishElementParsing(htmlParserCtxtPtr ctxt
) {
4590 * Capture end position and add node
4592 if ( ctxt
->node
!= NULL
&& ctxt
->record_info
) {
4593 ctxt
->nodeInfo
->end_pos
= ctxt
->input
->consumed
+
4594 (CUR_PTR
- ctxt
->input
->base
);
4595 ctxt
->nodeInfo
->end_line
= ctxt
->input
->line
;
4596 ctxt
->nodeInfo
->node
= ctxt
->node
;
4597 xmlParserAddNodeInfo(ctxt
, ctxt
->nodeInfo
);
4598 htmlNodeInfoPop(ctxt
);
4601 htmlAutoCloseOnEnd(ctxt
);
4606 * htmlParseElementInternal:
4607 * @ctxt: an HTML parser context
4609 * parse an HTML element, new version, non recursive
4611 * [39] element ::= EmptyElemTag | STag content ETag
4613 * [41] Attribute ::= Name Eq AttValue
4617 htmlParseElementInternal(htmlParserCtxtPtr ctxt
) {
4618 const xmlChar
*name
;
4619 const htmlElemDesc
* info
;
4620 htmlParserNodeInfo node_info
= { NULL
, 0, 0, 0, 0 };
4623 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) {
4624 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
4625 "htmlParseElementInternal: context error\n", NULL
, NULL
);
4629 if (ctxt
->instate
== XML_PARSER_EOF
)
4632 /* Capture start position */
4633 if (ctxt
->record_info
) {
4634 node_info
.begin_pos
= ctxt
->input
->consumed
+
4635 (CUR_PTR
- ctxt
->input
->base
);
4636 node_info
.begin_line
= ctxt
->input
->line
;
4639 failed
= htmlParseStartTag(ctxt
);
4641 if ((failed
== -1) || (name
== NULL
)) {
4648 * Lookup the info for that element.
4650 info
= htmlTagLookup(name
);
4652 htmlParseErr(ctxt
, XML_HTML_UNKNOWN_TAG
,
4653 "Tag %s invalid\n", name
, NULL
);
4657 * Check for an Empty Element labeled the XML/SGML way
4659 if ((CUR
== '/') && (NXT(1) == '>')) {
4661 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
4662 ctxt
->sax
->endElement(ctxt
->userData
, name
);
4670 htmlParseErr(ctxt
, XML_ERR_GT_REQUIRED
,
4671 "Couldn't find end of Start Tag %s\n", name
, NULL
);
4674 * end of parsing of this node.
4676 if (xmlStrEqual(name
, ctxt
->name
)) {
4681 if (ctxt
->record_info
)
4682 htmlNodeInfoPush(ctxt
, &node_info
);
4683 htmlParserFinishElementParsing(ctxt
);
4688 * Check for an Empty Element from DTD definition
4690 if ((info
!= NULL
) && (info
->empty
)) {
4691 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
4692 ctxt
->sax
->endElement(ctxt
->userData
, name
);
4697 if (ctxt
->record_info
)
4698 htmlNodeInfoPush(ctxt
, &node_info
);
4702 * htmlParseContentInternal:
4703 * @ctxt: an HTML parser context
4705 * Parse a content: comment, sub-element, reference or text.
4706 * New version for non recursive htmlParseElementInternal
4710 htmlParseContentInternal(htmlParserCtxtPtr ctxt
) {
4711 xmlChar
*currentNode
;
4713 const xmlChar
*name
;
4715 currentNode
= xmlStrdup(ctxt
->name
);
4716 depth
= ctxt
->nameNr
;
4720 if (ctxt
->instate
== XML_PARSER_EOF
)
4724 * Our tag or one of it's parent or children is ending.
4726 if ((CUR
== '<') && (NXT(1) == '/')) {
4727 if (htmlParseEndTag(ctxt
) &&
4728 ((currentNode
!= NULL
) || (ctxt
->nameNr
== 0))) {
4729 if (currentNode
!= NULL
)
4730 xmlFree(currentNode
);
4732 currentNode
= xmlStrdup(ctxt
->name
);
4733 depth
= ctxt
->nameNr
;
4735 continue; /* while */
4738 else if ((CUR
== '<') &&
4739 ((IS_ASCII_LETTER(NXT(1))) ||
4740 (NXT(1) == '_') || (NXT(1) == ':'))) {
4741 name
= htmlParseHTMLName_nonInvasive(ctxt
);
4743 htmlParseErr(ctxt
, XML_ERR_NAME_REQUIRED
,
4744 "htmlParseStartTag: invalid element name\n",
4746 /* Dump the bogus tag like browsers do */
4747 while ((CUR
== 0) && (CUR
!= '>'))
4750 htmlParserFinishElementParsing(ctxt
);
4751 if (currentNode
!= NULL
)
4752 xmlFree(currentNode
);
4754 currentNode
= xmlStrdup(ctxt
->name
);
4755 depth
= ctxt
->nameNr
;
4759 if (ctxt
->name
!= NULL
) {
4760 if (htmlCheckAutoClose(name
, ctxt
->name
) == 1) {
4761 htmlAutoClose(ctxt
, name
);
4768 * Has this node been popped out during parsing of
4771 if ((ctxt
->nameNr
> 0) && (depth
>= ctxt
->nameNr
) &&
4772 (!xmlStrEqual(currentNode
, ctxt
->name
)))
4774 htmlParserFinishElementParsing(ctxt
);
4775 if (currentNode
!= NULL
) xmlFree(currentNode
);
4777 currentNode
= xmlStrdup(ctxt
->name
);
4778 depth
= ctxt
->nameNr
;
4782 if ((CUR
!= 0) && ((xmlStrEqual(currentNode
, BAD_CAST
"script")) ||
4783 (xmlStrEqual(currentNode
, BAD_CAST
"style")))) {
4785 * Handle SCRIPT/STYLE separately
4787 htmlParseScript(ctxt
);
4790 * Sometimes DOCTYPE arrives in the middle of the document
4792 if ((CUR
== '<') && (NXT(1) == '!') &&
4793 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4794 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4795 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4797 htmlParseErr(ctxt
, XML_HTML_STRUCURE_ERROR
,
4798 "Misplaced DOCTYPE declaration\n",
4799 BAD_CAST
"DOCTYPE" , NULL
);
4800 htmlParseDocTypeDecl(ctxt
);
4804 * First case : a comment
4806 if ((CUR
== '<') && (NXT(1) == '!') &&
4807 (NXT(2) == '-') && (NXT(3) == '-')) {
4808 htmlParseComment(ctxt
);
4812 * Second case : a Processing Instruction.
4814 else if ((CUR
== '<') && (NXT(1) == '?')) {
4819 * Third case : a sub-element.
4821 else if ((CUR
== '<') && IS_ASCII_LETTER(NXT(1))) {
4822 htmlParseElementInternal(ctxt
);
4823 if (currentNode
!= NULL
) xmlFree(currentNode
);
4825 currentNode
= xmlStrdup(ctxt
->name
);
4826 depth
= ctxt
->nameNr
;
4828 else if (CUR
== '<') {
4829 if ((ctxt
->sax
!= NULL
) && (!ctxt
->disableSAX
) &&
4830 (ctxt
->sax
->characters
!= NULL
))
4831 ctxt
->sax
->characters(ctxt
->userData
, BAD_CAST
"<", 1);
4836 * Fourth case : a reference. If if has not been resolved,
4837 * parsing returns it's Name, create the node
4839 else if (CUR
== '&') {
4840 htmlParseReference(ctxt
);
4844 * Fifth case : end of the resource
4846 else if (CUR
== 0) {
4847 htmlAutoCloseOnEnd(ctxt
);
4852 * Last case, text. Note that References are handled directly.
4855 htmlParseCharData(ctxt
);
4860 if (currentNode
!= NULL
) xmlFree(currentNode
);
4865 * @ctxt: an HTML parser context
4867 * Parse a content: comment, sub-element, reference or text.
4868 * This is the entry point when called from parser.c
4872 __htmlParseContent(void *ctxt
) {
4874 htmlParseContentInternal((htmlParserCtxtPtr
) ctxt
);
4878 * htmlParseDocument:
4879 * @ctxt: an HTML parser context
4881 * parse an HTML document (and build a tree if using the standard SAX
4884 * Returns 0, -1 in case of error. the parser context is augmented
4885 * as a result of the parsing.
4889 htmlParseDocument(htmlParserCtxtPtr ctxt
) {
4891 xmlCharEncoding enc
;
4896 htmlDefaultSAXHandlerInit();
4898 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) {
4899 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
4900 "htmlParseDocument: context error\n", NULL
, NULL
);
4901 return(XML_ERR_INTERNAL_ERROR
);
4904 ctxt
->linenumbers
= 1;
4907 * SAX: beginning of the document processing.
4909 if ((ctxt
->sax
) && (ctxt
->sax
->setDocumentLocator
))
4910 ctxt
->sax
->setDocumentLocator(ctxt
->userData
, &xmlDefaultSAXLocator
);
4912 if ((ctxt
->encoding
== (const xmlChar
*)XML_CHAR_ENCODING_NONE
) &&
4913 ((ctxt
->input
->end
- ctxt
->input
->cur
) >= 4)) {
4915 * Get the 4 first bytes and decode the charset
4916 * if enc != XML_CHAR_ENCODING_NONE
4917 * plug some encoding conversion routines.
4923 enc
= xmlDetectCharEncoding(&start
[0], 4);
4924 if (enc
!= XML_CHAR_ENCODING_NONE
) {
4925 xmlSwitchEncoding(ctxt
, enc
);
4930 * Wipe out everything which is before the first '<'
4934 htmlParseErr(ctxt
, XML_ERR_DOCUMENT_EMPTY
,
4935 "Document is empty\n", NULL
, NULL
);
4938 if ((ctxt
->sax
) && (ctxt
->sax
->startDocument
) && (!ctxt
->disableSAX
))
4939 ctxt
->sax
->startDocument(ctxt
->userData
);
4943 * Parse possible comments and PIs before any content
4945 while (((CUR
== '<') && (NXT(1) == '!') &&
4946 (NXT(2) == '-') && (NXT(3) == '-')) ||
4947 ((CUR
== '<') && (NXT(1) == '?'))) {
4948 htmlParseComment(ctxt
);
4955 * Then possibly doc type declaration(s) and more Misc
4956 * (doctypedecl Misc*)?
4958 if ((CUR
== '<') && (NXT(1) == '!') &&
4959 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4960 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4961 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4963 htmlParseDocTypeDecl(ctxt
);
4968 * Parse possible comments and PIs before any content
4970 while (((CUR
== '<') && (NXT(1) == '!') &&
4971 (NXT(2) == '-') && (NXT(3) == '-')) ||
4972 ((CUR
== '<') && (NXT(1) == '?'))) {
4973 htmlParseComment(ctxt
);
4979 * Time to start parsing the tree itself
4981 htmlParseContentInternal(ctxt
);
4987 htmlAutoCloseOnEnd(ctxt
);
4991 * SAX: end of the document processing.
4993 if ((ctxt
->sax
) && (ctxt
->sax
->endDocument
!= NULL
))
4994 ctxt
->sax
->endDocument(ctxt
->userData
);
4996 if ((!(ctxt
->options
& HTML_PARSE_NODEFDTD
)) && (ctxt
->myDoc
!= NULL
)) {
4997 dtd
= xmlGetIntSubset(ctxt
->myDoc
);
4999 ctxt
->myDoc
->intSubset
=
5000 xmlCreateIntSubset(ctxt
->myDoc
, BAD_CAST
"html",
5001 BAD_CAST
"-//W3C//DTD HTML 4.0 Transitional//EN",
5002 BAD_CAST
"http://www.w3.org/TR/REC-html40/loose.dtd");
5004 if (! ctxt
->wellFormed
) return(-1);
5009 /************************************************************************
5011 * Parser contexts handling *
5013 ************************************************************************/
5016 * htmlInitParserCtxt:
5017 * @ctxt: an HTML parser context
5019 * Initialize a parser context
5021 * Returns 0 in case of success and -1 in case of error
5025 htmlInitParserCtxt(htmlParserCtxtPtr ctxt
)
5027 htmlSAXHandler
*sax
;
5029 if (ctxt
== NULL
) return(-1);
5030 memset(ctxt
, 0, sizeof(htmlParserCtxt
));
5032 ctxt
->dict
= xmlDictCreate();
5033 if (ctxt
->dict
== NULL
) {
5034 htmlErrMemory(NULL
, "htmlInitParserCtxt: out of memory\n");
5037 sax
= (htmlSAXHandler
*) xmlMalloc(sizeof(htmlSAXHandler
));
5039 htmlErrMemory(NULL
, "htmlInitParserCtxt: out of memory\n");
5043 memset(sax
, 0, sizeof(htmlSAXHandler
));
5045 /* Allocate the Input stack */
5046 ctxt
->inputTab
= (htmlParserInputPtr
*)
5047 xmlMalloc(5 * sizeof(htmlParserInputPtr
));
5048 if (ctxt
->inputTab
== NULL
) {
5049 htmlErrMemory(NULL
, "htmlInitParserCtxt: out of memory\n");
5058 ctxt
->version
= NULL
;
5059 ctxt
->encoding
= NULL
;
5060 ctxt
->standalone
= -1;
5061 ctxt
->instate
= XML_PARSER_START
;
5063 /* Allocate the Node stack */
5064 ctxt
->nodeTab
= (htmlNodePtr
*) xmlMalloc(10 * sizeof(htmlNodePtr
));
5065 if (ctxt
->nodeTab
== NULL
) {
5066 htmlErrMemory(NULL
, "htmlInitParserCtxt: out of memory\n");
5079 /* Allocate the Name stack */
5080 ctxt
->nameTab
= (const xmlChar
**) xmlMalloc(10 * sizeof(xmlChar
*));
5081 if (ctxt
->nameTab
== NULL
) {
5082 htmlErrMemory(NULL
, "htmlInitParserCtxt: out of memory\n");
5098 ctxt
->nodeInfoTab
= NULL
;
5099 ctxt
->nodeInfoNr
= 0;
5100 ctxt
->nodeInfoMax
= 0;
5102 if (sax
== NULL
) ctxt
->sax
= (xmlSAXHandlerPtr
) &htmlDefaultSAXHandler
;
5105 memcpy(sax
, &htmlDefaultSAXHandler
, sizeof(xmlSAXHandlerV1
));
5107 ctxt
->userData
= ctxt
;
5109 ctxt
->wellFormed
= 1;
5110 ctxt
->replaceEntities
= 0;
5111 ctxt
->linenumbers
= xmlLineNumbersDefaultValue
;
5112 ctxt
->keepBlanks
= xmlKeepBlanksDefaultValue
;
5114 ctxt
->vctxt
.finishDtd
= XML_CTXT_FINISH_DTD_0
;
5115 ctxt
->vctxt
.userData
= ctxt
;
5116 ctxt
->vctxt
.error
= xmlParserValidityError
;
5117 ctxt
->vctxt
.warning
= xmlParserValidityWarning
;
5118 ctxt
->record_info
= 0;
5120 ctxt
->checkIndex
= 0;
5121 ctxt
->catalogs
= NULL
;
5122 xmlInitNodeInfoSeq(&ctxt
->node_seq
);
5127 * htmlFreeParserCtxt:
5128 * @ctxt: an HTML parser context
5130 * Free all the memory used by a parser context. However the parsed
5131 * document in ctxt->myDoc is not freed.
5135 htmlFreeParserCtxt(htmlParserCtxtPtr ctxt
)
5137 xmlFreeParserCtxt(ctxt
);
5141 * htmlNewParserCtxt:
5143 * Allocate and initialize a new parser context.
5145 * Returns the htmlParserCtxtPtr or NULL in case of allocation error
5149 htmlNewParserCtxt(void)
5151 xmlParserCtxtPtr ctxt
;
5153 ctxt
= (xmlParserCtxtPtr
) xmlMalloc(sizeof(xmlParserCtxt
));
5155 htmlErrMemory(NULL
, "NewParserCtxt: out of memory\n");
5158 memset(ctxt
, 0, sizeof(xmlParserCtxt
));
5159 if (htmlInitParserCtxt(ctxt
) < 0) {
5160 htmlFreeParserCtxt(ctxt
);
5167 * htmlCreateMemoryParserCtxt:
5168 * @buffer: a pointer to a char array
5169 * @size: the size of the array
5171 * Create a parser context for an HTML in-memory document.
5173 * Returns the new parser context or NULL
5176 htmlCreateMemoryParserCtxt(const char *buffer
, int size
) {
5177 xmlParserCtxtPtr ctxt
;
5178 xmlParserInputPtr input
;
5179 xmlParserInputBufferPtr buf
;
5186 ctxt
= htmlNewParserCtxt();
5190 buf
= xmlParserInputBufferCreateMem(buffer
, size
, XML_CHAR_ENCODING_NONE
);
5191 if (buf
== NULL
) return(NULL
);
5193 input
= xmlNewInputStream(ctxt
);
5194 if (input
== NULL
) {
5195 xmlFreeParserInputBuffer(buf
);
5196 xmlFreeParserCtxt(ctxt
);
5200 input
->filename
= NULL
;
5202 xmlBufResetInput(buf
->buffer
, input
);
5204 inputPush(ctxt
, input
);
5209 * htmlCreateDocParserCtxt:
5210 * @cur: a pointer to an array of xmlChar
5211 * @encoding: a free form C string describing the HTML document encoding, or NULL
5213 * Create a parser context for an HTML document.
5215 * TODO: check the need to add encoding handling there
5217 * Returns the new parser context or NULL
5219 static htmlParserCtxtPtr
5220 htmlCreateDocParserCtxt(const xmlChar
*cur
, const char *encoding
) {
5222 htmlParserCtxtPtr ctxt
;
5226 len
= xmlStrlen(cur
);
5227 ctxt
= htmlCreateMemoryParserCtxt((char *)cur
, len
);
5231 if (encoding
!= NULL
) {
5232 xmlCharEncoding enc
;
5233 xmlCharEncodingHandlerPtr handler
;
5235 if (ctxt
->input
->encoding
!= NULL
)
5236 xmlFree((xmlChar
*) ctxt
->input
->encoding
);
5237 ctxt
->input
->encoding
= xmlStrdup((const xmlChar
*) encoding
);
5239 enc
= xmlParseCharEncoding(encoding
);
5241 * registered set of known encodings
5243 if (enc
!= XML_CHAR_ENCODING_ERROR
) {
5244 xmlSwitchEncoding(ctxt
, enc
);
5245 if (ctxt
->errNo
== XML_ERR_UNSUPPORTED_ENCODING
) {
5246 htmlParseErr(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
5247 "Unsupported encoding %s\n",
5248 (const xmlChar
*) encoding
, NULL
);
5252 * fallback for unknown encodings
5254 handler
= xmlFindCharEncodingHandler((const char *) encoding
);
5255 if (handler
!= NULL
) {
5256 xmlSwitchToEncoding(ctxt
, handler
);
5258 htmlParseErr(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
5259 "Unsupported encoding %s\n",
5260 (const xmlChar
*) encoding
, NULL
);
5267 #ifdef LIBXML_PUSH_ENABLED
5268 /************************************************************************
5270 * Progressive parsing interfaces *
5272 ************************************************************************/
5275 * htmlParseLookupSequence:
5276 * @ctxt: an HTML parser context
5277 * @first: the first char to lookup
5278 * @next: the next char to lookup or zero
5279 * @third: the next char to lookup or zero
5280 * @ignoreattrval: skip over attribute values
5282 * Try to find if a sequence (first, next, third) or just (first next) or
5283 * (first) is available in the input stream.
5284 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
5285 * to avoid rescanning sequences of bytes, it DOES change the state of the
5286 * parser, do not use liberally.
5287 * This is basically similar to xmlParseLookupSequence()
5289 * Returns the index to the current parsing point if the full sequence
5290 * is available, -1 otherwise.
5293 htmlParseLookupSequence(htmlParserCtxtPtr ctxt
, xmlChar first
,
5294 xmlChar next
, xmlChar third
, int ignoreattrval
)
5297 htmlParserInputPtr in
;
5300 char valdellim
= 0x0;
5306 base
= in
->cur
- in
->base
;
5310 if (ctxt
->checkIndex
> base
) {
5311 base
= ctxt
->checkIndex
;
5312 /* Abuse hasPErefs member to restore current state. */
5313 invalue
= ctxt
->hasPErefs
& 1 ? 1 : 0;
5316 if (in
->buf
== NULL
) {
5320 buf
= xmlBufContent(in
->buf
->buffer
);
5321 len
= xmlBufUse(in
->buf
->buffer
);
5324 /* take into account the sequence length */
5329 for (; base
< len
; base
++) {
5330 if (ignoreattrval
) {
5331 if (buf
[base
] == '"' || buf
[base
] == '\'') {
5333 if (buf
[base
] == valdellim
) {
5338 valdellim
= buf
[base
];
5342 } else if (invalue
) {
5346 if (buf
[base
] == first
) {
5348 if ((buf
[base
+ 1] != next
) || (buf
[base
+ 2] != third
))
5350 } else if (next
!= 0) {
5351 if (buf
[base
+ 1] != next
)
5354 ctxt
->checkIndex
= 0;
5357 xmlGenericError(xmlGenericErrorContext
,
5358 "HPP: lookup '%c' found at %d\n",
5360 else if (third
== 0)
5361 xmlGenericError(xmlGenericErrorContext
,
5362 "HPP: lookup '%c%c' found at %d\n",
5365 xmlGenericError(xmlGenericErrorContext
,
5366 "HPP: lookup '%c%c%c' found at %d\n",
5367 first
, next
, third
, base
);
5369 return (base
- (in
->cur
- in
->base
));
5372 ctxt
->checkIndex
= base
;
5373 /* Abuse hasPErefs member to track current state. */
5375 ctxt
->hasPErefs
|= 1;
5377 ctxt
->hasPErefs
&= ~1;
5380 xmlGenericError(xmlGenericErrorContext
,
5381 "HPP: lookup '%c' failed\n", first
);
5382 else if (third
== 0)
5383 xmlGenericError(xmlGenericErrorContext
,
5384 "HPP: lookup '%c%c' failed\n", first
, next
);
5386 xmlGenericError(xmlGenericErrorContext
,
5387 "HPP: lookup '%c%c%c' failed\n", first
, next
,
5394 * htmlParseLookupCommentEnd:
5395 * @ctxt: an HTML parser context
5397 * Try to find a comment end tag in the input stream
5398 * The search includes "-->" as well as WHATWG-recommended incorrectly-closed tags.
5399 * (See https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment)
5400 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
5401 * to avoid rescanning sequences of bytes, it DOES change the state of the
5402 * parser, do not use liberally.
5403 * This wraps to htmlParseLookupSequence()
5405 * Returns the index to the current parsing point if the full sequence is available, -1 otherwise.
5408 htmlParseLookupCommentEnd(htmlParserCtxtPtr ctxt
)
5411 int cur
= CUR_PTR
- BASE_PTR
;
5414 mark
= htmlParseLookupSequence(ctxt
, '-', '-', 0, 0);
5416 (NXT(mark
+2) == '>') ||
5417 ((NXT(mark
+2) == '!') && (NXT(mark
+3) == '>'))) {
5420 ctxt
->checkIndex
= cur
+ mark
+ 1;
5427 * htmlParseTryOrFinish:
5428 * @ctxt: an HTML parser context
5429 * @terminate: last chunk indicator
5431 * Try to progress on parsing
5433 * Returns zero if no parsing was possible
5436 htmlParseTryOrFinish(htmlParserCtxtPtr ctxt
, int terminate
) {
5438 htmlParserInputPtr in
;
5439 ptrdiff_t avail
= 0;
5442 htmlParserNodeInfo node_info
;
5445 switch (ctxt
->instate
) {
5446 case XML_PARSER_EOF
:
5447 xmlGenericError(xmlGenericErrorContext
,
5448 "HPP: try EOF\n"); break;
5449 case XML_PARSER_START
:
5450 xmlGenericError(xmlGenericErrorContext
,
5451 "HPP: try START\n"); break;
5452 case XML_PARSER_MISC
:
5453 xmlGenericError(xmlGenericErrorContext
,
5454 "HPP: try MISC\n");break;
5455 case XML_PARSER_COMMENT
:
5456 xmlGenericError(xmlGenericErrorContext
,
5457 "HPP: try COMMENT\n");break;
5458 case XML_PARSER_PROLOG
:
5459 xmlGenericError(xmlGenericErrorContext
,
5460 "HPP: try PROLOG\n");break;
5461 case XML_PARSER_START_TAG
:
5462 xmlGenericError(xmlGenericErrorContext
,
5463 "HPP: try START_TAG\n");break;
5464 case XML_PARSER_CONTENT
:
5465 xmlGenericError(xmlGenericErrorContext
,
5466 "HPP: try CONTENT\n");break;
5467 case XML_PARSER_CDATA_SECTION
:
5468 xmlGenericError(xmlGenericErrorContext
,
5469 "HPP: try CDATA_SECTION\n");break;
5470 case XML_PARSER_END_TAG
:
5471 xmlGenericError(xmlGenericErrorContext
,
5472 "HPP: try END_TAG\n");break;
5473 case XML_PARSER_ENTITY_DECL
:
5474 xmlGenericError(xmlGenericErrorContext
,
5475 "HPP: try ENTITY_DECL\n");break;
5476 case XML_PARSER_ENTITY_VALUE
:
5477 xmlGenericError(xmlGenericErrorContext
,
5478 "HPP: try ENTITY_VALUE\n");break;
5479 case XML_PARSER_ATTRIBUTE_VALUE
:
5480 xmlGenericError(xmlGenericErrorContext
,
5481 "HPP: try ATTRIBUTE_VALUE\n");break;
5482 case XML_PARSER_DTD
:
5483 xmlGenericError(xmlGenericErrorContext
,
5484 "HPP: try DTD\n");break;
5485 case XML_PARSER_EPILOG
:
5486 xmlGenericError(xmlGenericErrorContext
,
5487 "HPP: try EPILOG\n");break;
5489 xmlGenericError(xmlGenericErrorContext
,
5490 "HPP: try PI\n");break;
5491 case XML_PARSER_SYSTEM_LITERAL
:
5492 xmlGenericError(xmlGenericErrorContext
,
5493 "HPP: try SYSTEM_LITERAL\n");break;
5500 if (in
== NULL
) break;
5501 if (in
->buf
== NULL
)
5502 avail
= in
->length
- (in
->cur
- in
->base
);
5504 avail
= (ptrdiff_t)xmlBufUse(in
->buf
->buffer
) -
5505 (in
->cur
- in
->base
);
5506 if ((avail
== 0) && (terminate
)) {
5507 htmlAutoCloseOnEnd(ctxt
);
5508 if ((ctxt
->nameNr
== 0) && (ctxt
->instate
!= XML_PARSER_EOF
)) {
5510 * SAX: end of the document processing.
5512 ctxt
->instate
= XML_PARSER_EOF
;
5513 if ((ctxt
->sax
) && (ctxt
->sax
->endDocument
!= NULL
))
5514 ctxt
->sax
->endDocument(ctxt
->userData
);
5520 * This is done to make progress and avoid an infinite loop
5521 * if a parsing attempt was aborted by hitting a NUL byte. After
5522 * changing htmlCurrentChar, this probably isn't necessary anymore.
5523 * We should consider removing this check.
5531 switch (ctxt
->instate
) {
5532 case XML_PARSER_EOF
:
5534 * Document parsing is done !
5537 case XML_PARSER_START
:
5539 * Very first chars read from the document flow.
5542 if (IS_BLANK_CH(cur
)) {
5544 if (in
->buf
== NULL
)
5545 avail
= in
->length
- (in
->cur
- in
->base
);
5547 avail
= (ptrdiff_t)xmlBufUse(in
->buf
->buffer
) -
5548 (in
->cur
- in
->base
);
5550 if ((ctxt
->sax
) && (ctxt
->sax
->setDocumentLocator
))
5551 ctxt
->sax
->setDocumentLocator(ctxt
->userData
,
5552 &xmlDefaultSAXLocator
);
5553 if ((ctxt
->sax
) && (ctxt
->sax
->startDocument
) &&
5554 (!ctxt
->disableSAX
))
5555 ctxt
->sax
->startDocument(ctxt
->userData
);
5559 if ((cur
== '<') && (next
== '!') &&
5560 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5561 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5562 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5565 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 1) < 0))
5568 xmlGenericError(xmlGenericErrorContext
,
5569 "HPP: Parsing internal subset\n");
5571 htmlParseDocTypeDecl(ctxt
);
5572 ctxt
->instate
= XML_PARSER_PROLOG
;
5574 xmlGenericError(xmlGenericErrorContext
,
5575 "HPP: entering PROLOG\n");
5578 ctxt
->instate
= XML_PARSER_MISC
;
5580 xmlGenericError(xmlGenericErrorContext
,
5581 "HPP: entering MISC\n");
5585 case XML_PARSER_MISC
:
5587 if (in
->buf
== NULL
)
5588 avail
= in
->length
- (in
->cur
- in
->base
);
5590 avail
= (ptrdiff_t)xmlBufUse(in
->buf
->buffer
) -
5591 (in
->cur
- in
->base
);
5593 * no chars in buffer
5598 * not enough chars in buffer
5609 if ((cur
== '<') && (next
== '!') &&
5610 (in
->cur
[2] == '-') && (in
->cur
[3] == '-')) {
5611 if ((!terminate
) && (htmlParseLookupCommentEnd(ctxt
) < 0))
5614 xmlGenericError(xmlGenericErrorContext
,
5615 "HPP: Parsing Comment\n");
5617 htmlParseComment(ctxt
);
5618 ctxt
->instate
= XML_PARSER_MISC
;
5619 } else if ((cur
== '<') && (next
== '?')) {
5621 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 0) < 0))
5624 xmlGenericError(xmlGenericErrorContext
,
5625 "HPP: Parsing PI\n");
5628 ctxt
->instate
= XML_PARSER_MISC
;
5629 } else if ((cur
== '<') && (next
== '!') &&
5630 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5631 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5632 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5635 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 1) < 0))
5638 xmlGenericError(xmlGenericErrorContext
,
5639 "HPP: Parsing internal subset\n");
5641 htmlParseDocTypeDecl(ctxt
);
5642 ctxt
->instate
= XML_PARSER_PROLOG
;
5644 xmlGenericError(xmlGenericErrorContext
,
5645 "HPP: entering PROLOG\n");
5647 } else if ((cur
== '<') && (next
== '!') &&
5651 ctxt
->instate
= XML_PARSER_CONTENT
;
5653 xmlGenericError(xmlGenericErrorContext
,
5654 "HPP: entering START_TAG\n");
5658 case XML_PARSER_PROLOG
:
5660 if (in
->buf
== NULL
)
5661 avail
= in
->length
- (in
->cur
- in
->base
);
5663 avail
= (ptrdiff_t)xmlBufUse(in
->buf
->buffer
) -
5664 (in
->cur
- in
->base
);
5669 if ((cur
== '<') && (next
== '!') &&
5670 (in
->cur
[2] == '-') && (in
->cur
[3] == '-')) {
5671 if ((!terminate
) && (htmlParseLookupCommentEnd(ctxt
) < 0))
5674 xmlGenericError(xmlGenericErrorContext
,
5675 "HPP: Parsing Comment\n");
5677 htmlParseComment(ctxt
);
5678 ctxt
->instate
= XML_PARSER_PROLOG
;
5679 } else if ((cur
== '<') && (next
== '?')) {
5681 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 0) < 0))
5684 xmlGenericError(xmlGenericErrorContext
,
5685 "HPP: Parsing PI\n");
5688 ctxt
->instate
= XML_PARSER_PROLOG
;
5689 } else if ((cur
== '<') && (next
== '!') &&
5693 ctxt
->instate
= XML_PARSER_CONTENT
;
5695 xmlGenericError(xmlGenericErrorContext
,
5696 "HPP: entering START_TAG\n");
5700 case XML_PARSER_EPILOG
:
5701 if (in
->buf
== NULL
)
5702 avail
= in
->length
- (in
->cur
- in
->base
);
5704 avail
= (ptrdiff_t)xmlBufUse(in
->buf
->buffer
) -
5705 (in
->cur
- in
->base
);
5709 if (IS_BLANK_CH(cur
)) {
5710 htmlParseCharData(ctxt
);
5716 if ((cur
== '<') && (next
== '!') &&
5717 (in
->cur
[2] == '-') && (in
->cur
[3] == '-')) {
5718 if ((!terminate
) && (htmlParseLookupCommentEnd(ctxt
) < 0))
5721 xmlGenericError(xmlGenericErrorContext
,
5722 "HPP: Parsing Comment\n");
5724 htmlParseComment(ctxt
);
5725 ctxt
->instate
= XML_PARSER_EPILOG
;
5726 } else if ((cur
== '<') && (next
== '?')) {
5728 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 0) < 0))
5731 xmlGenericError(xmlGenericErrorContext
,
5732 "HPP: Parsing PI\n");
5735 ctxt
->instate
= XML_PARSER_EPILOG
;
5736 } else if ((cur
== '<') && (next
== '!') &&
5740 ctxt
->errNo
= XML_ERR_DOCUMENT_END
;
5741 ctxt
->wellFormed
= 0;
5742 ctxt
->instate
= XML_PARSER_EOF
;
5744 xmlGenericError(xmlGenericErrorContext
,
5745 "HPP: entering EOF\n");
5747 if ((ctxt
->sax
) && (ctxt
->sax
->endDocument
!= NULL
))
5748 ctxt
->sax
->endDocument(ctxt
->userData
);
5752 case XML_PARSER_START_TAG
: {
5753 const xmlChar
*name
;
5755 const htmlElemDesc
* info
;
5758 * no chars in buffer
5763 * not enough chars in buffer
5775 ctxt
->instate
= XML_PARSER_CONTENT
;
5777 xmlGenericError(xmlGenericErrorContext
,
5778 "HPP: entering CONTENT\n");
5783 ctxt
->instate
= XML_PARSER_END_TAG
;
5784 ctxt
->checkIndex
= 0;
5786 xmlGenericError(xmlGenericErrorContext
,
5787 "HPP: entering END_TAG\n");
5792 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 1) < 0))
5795 /* Capture start position */
5796 if (ctxt
->record_info
) {
5797 node_info
.begin_pos
= ctxt
->input
->consumed
+
5798 (CUR_PTR
- ctxt
->input
->base
);
5799 node_info
.begin_line
= ctxt
->input
->line
;
5803 failed
= htmlParseStartTag(ctxt
);
5805 if ((failed
== -1) ||
5813 * Lookup the info for that element.
5815 info
= htmlTagLookup(name
);
5817 htmlParseErr(ctxt
, XML_HTML_UNKNOWN_TAG
,
5818 "Tag %s invalid\n", name
, NULL
);
5822 * Check for an Empty Element labeled the XML/SGML way
5824 if ((CUR
== '/') && (NXT(1) == '>')) {
5826 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
5827 ctxt
->sax
->endElement(ctxt
->userData
, name
);
5829 ctxt
->instate
= XML_PARSER_CONTENT
;
5831 xmlGenericError(xmlGenericErrorContext
,
5832 "HPP: entering CONTENT\n");
5840 htmlParseErr(ctxt
, XML_ERR_GT_REQUIRED
,
5841 "Couldn't find end of Start Tag %s\n",
5845 * end of parsing of this node.
5847 if (xmlStrEqual(name
, ctxt
->name
)) {
5852 if (ctxt
->record_info
)
5853 htmlNodeInfoPush(ctxt
, &node_info
);
5855 ctxt
->instate
= XML_PARSER_CONTENT
;
5857 xmlGenericError(xmlGenericErrorContext
,
5858 "HPP: entering CONTENT\n");
5864 * Check for an Empty Element from DTD definition
5866 if ((info
!= NULL
) && (info
->empty
)) {
5867 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->endElement
!= NULL
))
5868 ctxt
->sax
->endElement(ctxt
->userData
, name
);
5872 if (ctxt
->record_info
)
5873 htmlNodeInfoPush(ctxt
, &node_info
);
5875 ctxt
->instate
= XML_PARSER_CONTENT
;
5877 xmlGenericError(xmlGenericErrorContext
,
5878 "HPP: entering CONTENT\n");
5882 case XML_PARSER_CONTENT
: {
5883 xmlChar chr
[2] = { 0, 0 };
5886 * Handle preparsed entities and charRef
5888 if (ctxt
->token
!= 0) {
5889 chr
[0] = (xmlChar
) ctxt
->token
;
5890 htmlCheckParagraph(ctxt
);
5891 if ((ctxt
->sax
!= NULL
) && (ctxt
->sax
->characters
!= NULL
))
5892 ctxt
->sax
->characters(ctxt
->userData
, chr
, 1);
5894 ctxt
->checkIndex
= 0;
5896 if ((avail
== 1) && (terminate
)) {
5898 if ((cur
!= '<') && (cur
!= '&')) {
5899 if (ctxt
->sax
!= NULL
) {
5901 if (IS_BLANK_CH(cur
)) {
5902 if (ctxt
->keepBlanks
) {
5903 if (ctxt
->sax
->characters
!= NULL
)
5904 ctxt
->sax
->characters(
5905 ctxt
->userData
, chr
, 1);
5907 if (ctxt
->sax
->ignorableWhitespace
!= NULL
)
5908 ctxt
->sax
->ignorableWhitespace(
5909 ctxt
->userData
, chr
, 1);
5912 htmlCheckParagraph(ctxt
);
5913 if (ctxt
->sax
->characters
!= NULL
)
5914 ctxt
->sax
->characters(
5915 ctxt
->userData
, chr
, 1);
5919 ctxt
->checkIndex
= 0;
5928 if ((xmlStrEqual(ctxt
->name
, BAD_CAST
"script")) ||
5929 (xmlStrEqual(ctxt
->name
, BAD_CAST
"style"))) {
5931 * Handle SCRIPT/STYLE separately
5937 idx
= htmlParseLookupSequence(ctxt
, '<', '/', 0, 0);
5940 val
= in
->cur
[idx
+ 2];
5941 if (val
== 0) /* bad cut of input */
5944 htmlParseScript(ctxt
);
5945 if ((cur
== '<') && (next
== '/')) {
5946 ctxt
->instate
= XML_PARSER_END_TAG
;
5947 ctxt
->checkIndex
= 0;
5949 xmlGenericError(xmlGenericErrorContext
,
5950 "HPP: entering END_TAG\n");
5956 * Sometimes DOCTYPE arrives in the middle of the document
5958 if ((cur
== '<') && (next
== '!') &&
5959 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5960 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5961 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5964 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 1) < 0))
5966 htmlParseErr(ctxt
, XML_HTML_STRUCURE_ERROR
,
5967 "Misplaced DOCTYPE declaration\n",
5968 BAD_CAST
"DOCTYPE" , NULL
);
5969 htmlParseDocTypeDecl(ctxt
);
5970 } else if ((cur
== '<') && (next
== '!') &&
5971 (in
->cur
[2] == '-') && (in
->cur
[3] == '-')) {
5972 if ((!terminate
) && (htmlParseLookupCommentEnd(ctxt
) < 0))
5975 xmlGenericError(xmlGenericErrorContext
,
5976 "HPP: Parsing Comment\n");
5978 htmlParseComment(ctxt
);
5979 ctxt
->instate
= XML_PARSER_CONTENT
;
5980 } else if ((cur
== '<') && (next
== '?')) {
5982 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 0) < 0))
5985 xmlGenericError(xmlGenericErrorContext
,
5986 "HPP: Parsing PI\n");
5989 ctxt
->instate
= XML_PARSER_CONTENT
;
5990 } else if ((cur
== '<') && (next
== '!') && (avail
< 4)) {
5992 } else if ((cur
== '<') && (next
== '/')) {
5993 ctxt
->instate
= XML_PARSER_END_TAG
;
5994 ctxt
->checkIndex
= 0;
5996 xmlGenericError(xmlGenericErrorContext
,
5997 "HPP: entering END_TAG\n");
6000 } else if ((cur
== '<') && IS_ASCII_LETTER(next
)) {
6001 if ((!terminate
) && (next
== 0))
6003 ctxt
->instate
= XML_PARSER_START_TAG
;
6004 ctxt
->checkIndex
= 0;
6006 xmlGenericError(xmlGenericErrorContext
,
6007 "HPP: entering START_TAG\n");
6010 } else if (cur
== '<') {
6011 if ((ctxt
->sax
!= NULL
) && (!ctxt
->disableSAX
) &&
6012 (ctxt
->sax
->characters
!= NULL
))
6013 ctxt
->sax
->characters(ctxt
->userData
,
6018 * check that the text sequence is complete
6019 * before handing out the data to the parser
6020 * to avoid problems with erroneous end of
6024 (htmlParseLookupSequence(ctxt
, '<', 0, 0, 0) < 0))
6026 ctxt
->checkIndex
= 0;
6028 xmlGenericError(xmlGenericErrorContext
,
6029 "HPP: Parsing char data\n");
6031 while ((ctxt
->instate
!= XML_PARSER_EOF
) &&
6032 (cur
!= '<') && (in
->cur
< in
->end
)) {
6034 htmlParseReference(ctxt
);
6036 htmlParseCharData(ctxt
);
6045 case XML_PARSER_END_TAG
:
6049 (htmlParseLookupSequence(ctxt
, '>', 0, 0, 0) < 0))
6051 htmlParseEndTag(ctxt
);
6052 if (ctxt
->nameNr
== 0) {
6053 ctxt
->instate
= XML_PARSER_EPILOG
;
6055 ctxt
->instate
= XML_PARSER_CONTENT
;
6057 ctxt
->checkIndex
= 0;
6059 xmlGenericError(xmlGenericErrorContext
,
6060 "HPP: entering CONTENT\n");
6063 case XML_PARSER_CDATA_SECTION
:
6064 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6065 "HPP: internal error, state == CDATA\n",
6067 ctxt
->instate
= XML_PARSER_CONTENT
;
6068 ctxt
->checkIndex
= 0;
6070 xmlGenericError(xmlGenericErrorContext
,
6071 "HPP: entering CONTENT\n");
6074 case XML_PARSER_DTD
:
6075 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6076 "HPP: internal error, state == DTD\n",
6078 ctxt
->instate
= XML_PARSER_CONTENT
;
6079 ctxt
->checkIndex
= 0;
6081 xmlGenericError(xmlGenericErrorContext
,
6082 "HPP: entering CONTENT\n");
6085 case XML_PARSER_COMMENT
:
6086 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6087 "HPP: internal error, state == COMMENT\n",
6089 ctxt
->instate
= XML_PARSER_CONTENT
;
6090 ctxt
->checkIndex
= 0;
6092 xmlGenericError(xmlGenericErrorContext
,
6093 "HPP: entering CONTENT\n");
6097 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6098 "HPP: internal error, state == PI\n",
6100 ctxt
->instate
= XML_PARSER_CONTENT
;
6101 ctxt
->checkIndex
= 0;
6103 xmlGenericError(xmlGenericErrorContext
,
6104 "HPP: entering CONTENT\n");
6107 case XML_PARSER_ENTITY_DECL
:
6108 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6109 "HPP: internal error, state == ENTITY_DECL\n",
6111 ctxt
->instate
= XML_PARSER_CONTENT
;
6112 ctxt
->checkIndex
= 0;
6114 xmlGenericError(xmlGenericErrorContext
,
6115 "HPP: entering CONTENT\n");
6118 case XML_PARSER_ENTITY_VALUE
:
6119 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6120 "HPP: internal error, state == ENTITY_VALUE\n",
6122 ctxt
->instate
= XML_PARSER_CONTENT
;
6123 ctxt
->checkIndex
= 0;
6125 xmlGenericError(xmlGenericErrorContext
,
6126 "HPP: entering DTD\n");
6129 case XML_PARSER_ATTRIBUTE_VALUE
:
6130 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6131 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
6133 ctxt
->instate
= XML_PARSER_START_TAG
;
6134 ctxt
->checkIndex
= 0;
6136 xmlGenericError(xmlGenericErrorContext
,
6137 "HPP: entering START_TAG\n");
6140 case XML_PARSER_SYSTEM_LITERAL
:
6141 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6142 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
6144 ctxt
->instate
= XML_PARSER_CONTENT
;
6145 ctxt
->checkIndex
= 0;
6147 xmlGenericError(xmlGenericErrorContext
,
6148 "HPP: entering CONTENT\n");
6151 case XML_PARSER_IGNORE
:
6152 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6153 "HPP: internal error, state == XML_PARSER_IGNORE\n",
6155 ctxt
->instate
= XML_PARSER_CONTENT
;
6156 ctxt
->checkIndex
= 0;
6158 xmlGenericError(xmlGenericErrorContext
,
6159 "HPP: entering CONTENT\n");
6162 case XML_PARSER_PUBLIC_LITERAL
:
6163 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6164 "HPP: internal error, state == XML_PARSER_LITERAL\n",
6166 ctxt
->instate
= XML_PARSER_CONTENT
;
6167 ctxt
->checkIndex
= 0;
6169 xmlGenericError(xmlGenericErrorContext
,
6170 "HPP: entering CONTENT\n");
6177 if ((avail
== 0) && (terminate
)) {
6178 htmlAutoCloseOnEnd(ctxt
);
6179 if ((ctxt
->nameNr
== 0) && (ctxt
->instate
!= XML_PARSER_EOF
)) {
6181 * SAX: end of the document processing.
6183 ctxt
->instate
= XML_PARSER_EOF
;
6184 if ((ctxt
->sax
) && (ctxt
->sax
->endDocument
!= NULL
))
6185 ctxt
->sax
->endDocument(ctxt
->userData
);
6188 if ((!(ctxt
->options
& HTML_PARSE_NODEFDTD
)) && (ctxt
->myDoc
!= NULL
) &&
6189 ((terminate
) || (ctxt
->instate
== XML_PARSER_EOF
) ||
6190 (ctxt
->instate
== XML_PARSER_EPILOG
))) {
6192 dtd
= xmlGetIntSubset(ctxt
->myDoc
);
6194 ctxt
->myDoc
->intSubset
=
6195 xmlCreateIntSubset(ctxt
->myDoc
, BAD_CAST
"html",
6196 BAD_CAST
"-//W3C//DTD HTML 4.0 Transitional//EN",
6197 BAD_CAST
"http://www.w3.org/TR/REC-html40/loose.dtd");
6200 xmlGenericError(xmlGenericErrorContext
, "HPP: done %d\n", ret
);
6207 * @ctxt: an HTML parser context
6208 * @chunk: an char array
6209 * @size: the size in byte of the chunk
6210 * @terminate: last chunk indicator
6212 * Parse a Chunk of memory
6214 * Returns zero if no error, the xmlParserErrors otherwise.
6217 htmlParseChunk(htmlParserCtxtPtr ctxt
, const char *chunk
, int size
,
6219 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
)) {
6220 htmlParseErr(ctxt
, XML_ERR_INTERNAL_ERROR
,
6221 "htmlParseChunk: context error\n", NULL
, NULL
);
6222 return(XML_ERR_INTERNAL_ERROR
);
6224 if ((size
> 0) && (chunk
!= NULL
) && (ctxt
->input
!= NULL
) &&
6225 (ctxt
->input
->buf
!= NULL
) && (ctxt
->instate
!= XML_PARSER_EOF
)) {
6226 size_t base
= xmlBufGetInputBase(ctxt
->input
->buf
->buffer
, ctxt
->input
);
6227 size_t cur
= ctxt
->input
->cur
- ctxt
->input
->base
;
6230 res
= xmlParserInputBufferPush(ctxt
->input
->buf
, size
, chunk
);
6231 xmlBufSetInputBaseCur(ctxt
->input
->buf
->buffer
, ctxt
->input
, base
, cur
);
6233 ctxt
->errNo
= XML_PARSER_EOF
;
6234 ctxt
->disableSAX
= 1;
6235 return (XML_PARSER_EOF
);
6238 xmlGenericError(xmlGenericErrorContext
, "HPP: pushed %d\n", size
);
6242 if ((terminate
) || (ctxt
->input
->buf
->buffer
->use
> 80))
6243 htmlParseTryOrFinish(ctxt
, terminate
);
6245 } else if (ctxt
->instate
!= XML_PARSER_EOF
) {
6246 if ((ctxt
->input
!= NULL
) && ctxt
->input
->buf
!= NULL
) {
6247 xmlParserInputBufferPtr in
= ctxt
->input
->buf
;
6248 if ((in
->encoder
!= NULL
) && (in
->buffer
!= NULL
) &&
6249 (in
->raw
!= NULL
)) {
6251 size_t base
= xmlBufGetInputBase(in
->buffer
, ctxt
->input
);
6252 size_t current
= ctxt
->input
->cur
- ctxt
->input
->base
;
6254 nbchars
= xmlCharEncInput(in
, terminate
);
6255 xmlBufSetInputBaseCur(in
->buffer
, ctxt
->input
, base
, current
);
6257 htmlParseErr(ctxt
, XML_ERR_INVALID_ENCODING
,
6258 "encoder error\n", NULL
, NULL
);
6259 return(XML_ERR_INVALID_ENCODING
);
6264 htmlParseTryOrFinish(ctxt
, terminate
);
6266 if ((ctxt
->instate
!= XML_PARSER_EOF
) &&
6267 (ctxt
->instate
!= XML_PARSER_EPILOG
) &&
6268 (ctxt
->instate
!= XML_PARSER_MISC
)) {
6269 ctxt
->errNo
= XML_ERR_DOCUMENT_END
;
6270 ctxt
->wellFormed
= 0;
6272 if (ctxt
->instate
!= XML_PARSER_EOF
) {
6273 if ((ctxt
->sax
) && (ctxt
->sax
->endDocument
!= NULL
))
6274 ctxt
->sax
->endDocument(ctxt
->userData
);
6276 ctxt
->instate
= XML_PARSER_EOF
;
6278 return((xmlParserErrors
) ctxt
->errNo
);
6281 /************************************************************************
6283 * User entry points *
6285 ************************************************************************/
6288 * htmlCreatePushParserCtxt:
6289 * @sax: a SAX handler
6290 * @user_data: The user data returned on SAX callbacks
6291 * @chunk: a pointer to an array of chars
6292 * @size: number of chars in the array
6293 * @filename: an optional file name or URI
6294 * @enc: an optional encoding
6296 * Create a parser context for using the HTML parser in push mode
6297 * The value of @filename is used for fetching external entities
6298 * and error/warning reports.
6300 * Returns the new parser context or NULL
6303 htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax
, void *user_data
,
6304 const char *chunk
, int size
, const char *filename
,
6305 xmlCharEncoding enc
) {
6306 htmlParserCtxtPtr ctxt
;
6307 htmlParserInputPtr inputStream
;
6308 xmlParserInputBufferPtr buf
;
6312 buf
= xmlAllocParserInputBuffer(enc
);
6313 if (buf
== NULL
) return(NULL
);
6315 ctxt
= htmlNewParserCtxt();
6317 xmlFreeParserInputBuffer(buf
);
6320 if(enc
==XML_CHAR_ENCODING_UTF8
|| buf
->encoder
)
6321 ctxt
->charset
=XML_CHAR_ENCODING_UTF8
;
6323 if (ctxt
->sax
!= (xmlSAXHandlerPtr
) &htmlDefaultSAXHandler
)
6325 ctxt
->sax
= (htmlSAXHandlerPtr
) xmlMalloc(sizeof(htmlSAXHandler
));
6326 if (ctxt
->sax
== NULL
) {
6331 memcpy(ctxt
->sax
, sax
, sizeof(htmlSAXHandler
));
6332 if (user_data
!= NULL
)
6333 ctxt
->userData
= user_data
;
6335 if (filename
== NULL
) {
6336 ctxt
->directory
= NULL
;
6338 ctxt
->directory
= xmlParserGetDirectory(filename
);
6341 inputStream
= htmlNewInputStream(ctxt
);
6342 if (inputStream
== NULL
) {
6343 xmlFreeParserCtxt(ctxt
);
6348 if (filename
== NULL
)
6349 inputStream
->filename
= NULL
;
6351 inputStream
->filename
= (char *)
6352 xmlCanonicPath((const xmlChar
*) filename
);
6353 inputStream
->buf
= buf
;
6354 xmlBufResetInput(buf
->buffer
, inputStream
);
6356 inputPush(ctxt
, inputStream
);
6358 if ((size
> 0) && (chunk
!= NULL
) && (ctxt
->input
!= NULL
) &&
6359 (ctxt
->input
->buf
!= NULL
)) {
6360 size_t base
= xmlBufGetInputBase(ctxt
->input
->buf
->buffer
, ctxt
->input
);
6361 size_t cur
= ctxt
->input
->cur
- ctxt
->input
->base
;
6363 xmlParserInputBufferPush(ctxt
->input
->buf
, size
, chunk
);
6365 xmlBufSetInputBaseCur(ctxt
->input
->buf
->buffer
, ctxt
->input
, base
, cur
);
6367 xmlGenericError(xmlGenericErrorContext
, "HPP: pushed %d\n", size
);
6370 ctxt
->progressive
= 1;
6374 #endif /* LIBXML_PUSH_ENABLED */
6378 * @cur: a pointer to an array of xmlChar
6379 * @encoding: a free form C string describing the HTML document encoding, or NULL
6380 * @sax: the SAX handler block
6381 * @userData: if using SAX, this pointer will be provided on callbacks.
6383 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
6384 * to handle parse events. If sax is NULL, fallback to the default DOM
6385 * behavior and return a tree.
6387 * Returns the resulting document tree unless SAX is NULL or the document is
6392 htmlSAXParseDoc(const xmlChar
*cur
, const char *encoding
,
6393 htmlSAXHandlerPtr sax
, void *userData
) {
6395 htmlParserCtxtPtr ctxt
;
6399 if (cur
== NULL
) return(NULL
);
6402 ctxt
= htmlCreateDocParserCtxt(cur
, encoding
);
6403 if (ctxt
== NULL
) return(NULL
);
6405 if (ctxt
->sax
!= NULL
) xmlFree (ctxt
->sax
);
6407 ctxt
->userData
= userData
;
6410 htmlParseDocument(ctxt
);
6414 ctxt
->userData
= NULL
;
6416 htmlFreeParserCtxt(ctxt
);
6423 * @cur: a pointer to an array of xmlChar
6424 * @encoding: a free form C string describing the HTML document encoding, or NULL
6426 * parse an HTML in-memory document and build a tree.
6428 * Returns the resulting document tree
6432 htmlParseDoc(const xmlChar
*cur
, const char *encoding
) {
6433 return(htmlSAXParseDoc(cur
, encoding
, NULL
, NULL
));
6438 * htmlCreateFileParserCtxt:
6439 * @filename: the filename
6440 * @encoding: a free form C string describing the HTML document encoding, or NULL
6442 * Create a parser context for a file content.
6443 * Automatic support for ZLIB/Compress compressed document is provided
6444 * by default if found at compile-time.
6446 * Returns the new parser context or NULL
6449 htmlCreateFileParserCtxt(const char *filename
, const char *encoding
)
6451 htmlParserCtxtPtr ctxt
;
6452 htmlParserInputPtr inputStream
;
6453 char *canonicFilename
;
6454 /* htmlCharEncoding enc; */
6455 xmlChar
*content
, *content_line
= (xmlChar
*) "charset=";
6457 if (filename
== NULL
)
6460 ctxt
= htmlNewParserCtxt();
6464 canonicFilename
= (char *) xmlCanonicPath((const xmlChar
*) filename
);
6465 if (canonicFilename
== NULL
) {
6466 #ifdef LIBXML_SAX1_ENABLED
6467 if (xmlDefaultSAXHandler
.error
!= NULL
) {
6468 xmlDefaultSAXHandler
.error(NULL
, "out of memory\n");
6471 xmlFreeParserCtxt(ctxt
);
6475 inputStream
= xmlLoadExternalEntity(canonicFilename
, NULL
, ctxt
);
6476 xmlFree(canonicFilename
);
6477 if (inputStream
== NULL
) {
6478 xmlFreeParserCtxt(ctxt
);
6482 inputPush(ctxt
, inputStream
);
6486 size_t l
= strlen(encoding
);
6489 content
= xmlMallocAtomic (xmlStrlen(content_line
) + l
+ 1);
6491 strcpy ((char *)content
, (char *)content_line
);
6492 strcat ((char *)content
, (char *)encoding
);
6493 htmlCheckEncoding (ctxt
, content
);
6504 * @filename: the filename
6505 * @encoding: a free form C string describing the HTML document encoding, or NULL
6506 * @sax: the SAX handler block
6507 * @userData: if using SAX, this pointer will be provided on callbacks.
6509 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
6510 * compressed document is provided by default if found at compile-time.
6511 * It use the given SAX function block to handle the parsing callback.
6512 * If sax is NULL, fallback to the default DOM tree building routines.
6514 * Returns the resulting document tree unless SAX is NULL or the document is
6519 htmlSAXParseFile(const char *filename
, const char *encoding
, htmlSAXHandlerPtr sax
,
6522 htmlParserCtxtPtr ctxt
;
6523 htmlSAXHandlerPtr oldsax
= NULL
;
6527 ctxt
= htmlCreateFileParserCtxt(filename
, encoding
);
6528 if (ctxt
== NULL
) return(NULL
);
6532 ctxt
->userData
= userData
;
6535 htmlParseDocument(ctxt
);
6540 ctxt
->userData
= NULL
;
6542 htmlFreeParserCtxt(ctxt
);
6549 * @filename: the filename
6550 * @encoding: a free form C string describing the HTML document encoding, or NULL
6552 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
6553 * compressed document is provided by default if found at compile-time.
6555 * Returns the resulting document tree
6559 htmlParseFile(const char *filename
, const char *encoding
) {
6560 return(htmlSAXParseFile(filename
, encoding
, NULL
, NULL
));
6564 * htmlHandleOmittedElem:
6567 * Set and return the previous value for handling HTML omitted tags.
6569 * Returns the last value for 0 for no handling, 1 for auto insertion.
6573 htmlHandleOmittedElem(int val
) {
6574 int old
= htmlOmittedDefaultValue
;
6576 htmlOmittedDefaultValue
= val
;
6581 * htmlElementAllowedHere:
6582 * @parent: HTML parent element
6583 * @elt: HTML element
6585 * Checks whether an HTML element may be a direct child of a parent element.
6586 * Note - doesn't check for deprecated elements
6588 * Returns 1 if allowed; 0 otherwise.
6591 htmlElementAllowedHere(const htmlElemDesc
* parent
, const xmlChar
* elt
) {
6594 if ( ! elt
|| ! parent
|| ! parent
->subelts
)
6597 for ( p
= parent
->subelts
; *p
; ++p
)
6598 if ( !xmlStrcmp((const xmlChar
*)*p
, elt
) )
6604 * htmlElementStatusHere:
6605 * @parent: HTML parent element
6606 * @elt: HTML element
6608 * Checks whether an HTML element may be a direct child of a parent element.
6609 * and if so whether it is valid or deprecated.
6611 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
6614 htmlElementStatusHere(const htmlElemDesc
* parent
, const htmlElemDesc
* elt
) {
6615 if ( ! parent
|| ! elt
)
6616 return HTML_INVALID
;
6617 if ( ! htmlElementAllowedHere(parent
, (const xmlChar
*) elt
->name
) )
6618 return HTML_INVALID
;
6620 return ( elt
->dtd
== 0 ) ? HTML_VALID
: HTML_DEPRECATED
;
6624 * @elt: HTML element
6625 * @attr: HTML attribute
6626 * @legacy: whether to allow deprecated attributes
6628 * Checks whether an attribute is valid for an element
6629 * Has full knowledge of Required and Deprecated attributes
6631 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
6634 htmlAttrAllowed(const htmlElemDesc
* elt
, const xmlChar
* attr
, int legacy
) {
6637 if ( !elt
|| ! attr
)
6638 return HTML_INVALID
;
6640 if ( elt
->attrs_req
)
6641 for ( p
= elt
->attrs_req
; *p
; ++p
)
6642 if ( !xmlStrcmp((const xmlChar
*)*p
, attr
) )
6643 return HTML_REQUIRED
;
6645 if ( elt
->attrs_opt
)
6646 for ( p
= elt
->attrs_opt
; *p
; ++p
)
6647 if ( !xmlStrcmp((const xmlChar
*)*p
, attr
) )
6650 if ( legacy
&& elt
->attrs_depr
)
6651 for ( p
= elt
->attrs_depr
; *p
; ++p
)
6652 if ( !xmlStrcmp((const xmlChar
*)*p
, attr
) )
6653 return HTML_DEPRECATED
;
6655 return HTML_INVALID
;
6659 * @node: an htmlNodePtr in a tree
6660 * @legacy: whether to allow deprecated elements (YES is faster here
6661 * for Element nodes)
6663 * Checks whether the tree node is valid. Experimental (the author
6664 * only uses the HTML enhancements in a SAX parser)
6666 * Return: for Element nodes, a return from htmlElementAllowedHere (if
6667 * legacy allowed) or htmlElementStatusHere (otherwise).
6668 * for Attribute nodes, a return from htmlAttrAllowed
6669 * for other nodes, HTML_NA (no checks performed)
6672 htmlNodeStatus(const htmlNodePtr node
, int legacy
) {
6674 return HTML_INVALID
;
6676 switch ( node
->type
) {
6677 case XML_ELEMENT_NODE
:
6679 ? ( htmlElementAllowedHere (
6680 htmlTagLookup(node
->parent
->name
) , node
->name
6681 ) ? HTML_VALID
: HTML_INVALID
)
6682 : htmlElementStatusHere(
6683 htmlTagLookup(node
->parent
->name
) ,
6684 htmlTagLookup(node
->name
) )
6686 case XML_ATTRIBUTE_NODE
:
6687 return htmlAttrAllowed(
6688 htmlTagLookup(node
->parent
->name
) , node
->name
, legacy
) ;
6689 default: return HTML_NA
;
6692 /************************************************************************
6694 * New set (2.6.0) of simpler and more flexible APIs *
6696 ************************************************************************/
6701 * Free a string if it is not owned by the "dict" dictionary in the
6704 #define DICT_FREE(str) \
6705 if ((str) && ((!dict) || \
6706 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
6707 xmlFree((char *)(str));
6711 * @ctxt: an HTML parser context
6713 * Reset a parser context
6716 htmlCtxtReset(htmlParserCtxtPtr ctxt
)
6718 xmlParserInputPtr input
;
6727 while ((input
= inputPop(ctxt
)) != NULL
) { /* Non consuming */
6728 xmlFreeInputStream(input
);
6734 if (ctxt
->spaceTab
!= NULL
) {
6735 ctxt
->spaceTab
[0] = -1;
6736 ctxt
->space
= &ctxt
->spaceTab
[0];
6748 DICT_FREE(ctxt
->version
);
6749 ctxt
->version
= NULL
;
6750 DICT_FREE(ctxt
->encoding
);
6751 ctxt
->encoding
= NULL
;
6752 DICT_FREE(ctxt
->directory
);
6753 ctxt
->directory
= NULL
;
6754 DICT_FREE(ctxt
->extSubURI
);
6755 ctxt
->extSubURI
= NULL
;
6756 DICT_FREE(ctxt
->extSubSystem
);
6757 ctxt
->extSubSystem
= NULL
;
6758 if (ctxt
->myDoc
!= NULL
)
6759 xmlFreeDoc(ctxt
->myDoc
);
6762 ctxt
->standalone
= -1;
6763 ctxt
->hasExternalSubset
= 0;
6764 ctxt
->hasPErefs
= 0;
6767 ctxt
->instate
= XML_PARSER_START
;
6770 ctxt
->wellFormed
= 1;
6771 ctxt
->nsWellFormed
= 1;
6772 ctxt
->disableSAX
= 0;
6774 ctxt
->vctxt
.userData
= ctxt
;
6775 ctxt
->vctxt
.error
= xmlParserValidityError
;
6776 ctxt
->vctxt
.warning
= xmlParserValidityWarning
;
6777 ctxt
->record_info
= 0;
6778 ctxt
->checkIndex
= 0;
6780 ctxt
->errNo
= XML_ERR_OK
;
6782 ctxt
->charset
= XML_CHAR_ENCODING_NONE
;
6783 ctxt
->catalogs
= NULL
;
6784 xmlInitNodeInfoSeq(&ctxt
->node_seq
);
6786 if (ctxt
->attsDefault
!= NULL
) {
6787 xmlHashFree(ctxt
->attsDefault
, xmlHashDefaultDeallocator
);
6788 ctxt
->attsDefault
= NULL
;
6790 if (ctxt
->attsSpecial
!= NULL
) {
6791 xmlHashFree(ctxt
->attsSpecial
, NULL
);
6792 ctxt
->attsSpecial
= NULL
;
6797 * htmlCtxtUseOptions:
6798 * @ctxt: an HTML parser context
6799 * @options: a combination of htmlParserOption(s)
6801 * Applies the options to the parser context
6803 * Returns 0 in case of success, the set of unknown or unimplemented options
6807 htmlCtxtUseOptions(htmlParserCtxtPtr ctxt
, int options
)
6812 if (options
& HTML_PARSE_NOWARNING
) {
6813 ctxt
->sax
->warning
= NULL
;
6814 ctxt
->vctxt
.warning
= NULL
;
6815 options
-= XML_PARSE_NOWARNING
;
6816 ctxt
->options
|= XML_PARSE_NOWARNING
;
6818 if (options
& HTML_PARSE_NOERROR
) {
6819 ctxt
->sax
->error
= NULL
;
6820 ctxt
->vctxt
.error
= NULL
;
6821 ctxt
->sax
->fatalError
= NULL
;
6822 options
-= XML_PARSE_NOERROR
;
6823 ctxt
->options
|= XML_PARSE_NOERROR
;
6825 if (options
& HTML_PARSE_PEDANTIC
) {
6827 options
-= XML_PARSE_PEDANTIC
;
6828 ctxt
->options
|= XML_PARSE_PEDANTIC
;
6831 if (options
& XML_PARSE_NOBLANKS
) {
6832 ctxt
->keepBlanks
= 0;
6833 ctxt
->sax
->ignorableWhitespace
= xmlSAX2IgnorableWhitespace
;
6834 options
-= XML_PARSE_NOBLANKS
;
6835 ctxt
->options
|= XML_PARSE_NOBLANKS
;
6837 ctxt
->keepBlanks
= 1;
6838 if (options
& HTML_PARSE_RECOVER
) {
6840 options
-= HTML_PARSE_RECOVER
;
6843 if (options
& HTML_PARSE_COMPACT
) {
6844 ctxt
->options
|= HTML_PARSE_COMPACT
;
6845 options
-= HTML_PARSE_COMPACT
;
6847 if (options
& XML_PARSE_HUGE
) {
6848 ctxt
->options
|= XML_PARSE_HUGE
;
6849 options
-= XML_PARSE_HUGE
;
6851 if (options
& HTML_PARSE_NODEFDTD
) {
6852 ctxt
->options
|= HTML_PARSE_NODEFDTD
;
6853 options
-= HTML_PARSE_NODEFDTD
;
6855 if (options
& HTML_PARSE_IGNORE_ENC
) {
6856 ctxt
->options
|= HTML_PARSE_IGNORE_ENC
;
6857 options
-= HTML_PARSE_IGNORE_ENC
;
6859 if (options
& HTML_PARSE_NOIMPLIED
) {
6860 ctxt
->options
|= HTML_PARSE_NOIMPLIED
;
6861 options
-= HTML_PARSE_NOIMPLIED
;
6863 ctxt
->dictNames
= 0;
6869 * @ctxt: an HTML parser context
6870 * @URL: the base URL to use for the document
6871 * @encoding: the document encoding, or NULL
6872 * @options: a combination of htmlParserOption(s)
6873 * @reuse: keep the context for reuse
6875 * Common front-end for the htmlRead functions
6877 * Returns the resulting document tree or NULL
6880 htmlDoRead(htmlParserCtxtPtr ctxt
, const char *URL
, const char *encoding
,
6881 int options
, int reuse
)
6885 htmlCtxtUseOptions(ctxt
, options
);
6887 if (encoding
!= NULL
) {
6888 xmlCharEncodingHandlerPtr hdlr
;
6890 hdlr
= xmlFindCharEncodingHandler(encoding
);
6892 xmlSwitchToEncoding(ctxt
, hdlr
);
6893 if (ctxt
->input
->encoding
!= NULL
)
6894 xmlFree((xmlChar
*) ctxt
->input
->encoding
);
6895 ctxt
->input
->encoding
= xmlStrdup((xmlChar
*)encoding
);
6898 if ((URL
!= NULL
) && (ctxt
->input
!= NULL
) &&
6899 (ctxt
->input
->filename
== NULL
))
6900 ctxt
->input
->filename
= (char *) xmlStrdup((const xmlChar
*) URL
);
6901 htmlParseDocument(ctxt
);
6905 if ((ctxt
->dictNames
) &&
6907 (ret
->dict
== ctxt
->dict
))
6909 xmlFreeParserCtxt(ctxt
);
6916 * @cur: a pointer to a zero terminated string
6917 * @URL: the base URL to use for the document
6918 * @encoding: the document encoding, or NULL
6919 * @options: a combination of htmlParserOption(s)
6921 * parse an XML in-memory document and build a tree.
6923 * Returns the resulting document tree
6926 htmlReadDoc(const xmlChar
* cur
, const char *URL
, const char *encoding
, int options
)
6928 htmlParserCtxtPtr ctxt
;
6934 ctxt
= htmlCreateDocParserCtxt(cur
, NULL
);
6937 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 0));
6942 * @filename: a file or URL
6943 * @encoding: the document encoding, or NULL
6944 * @options: a combination of htmlParserOption(s)
6946 * parse an XML file from the filesystem or the network.
6948 * Returns the resulting document tree
6951 htmlReadFile(const char *filename
, const char *encoding
, int options
)
6953 htmlParserCtxtPtr ctxt
;
6956 ctxt
= htmlCreateFileParserCtxt(filename
, encoding
);
6959 return (htmlDoRead(ctxt
, NULL
, NULL
, options
, 0));
6964 * @buffer: a pointer to a char array
6965 * @size: the size of the array
6966 * @URL: the base URL to use for the document
6967 * @encoding: the document encoding, or NULL
6968 * @options: a combination of htmlParserOption(s)
6970 * parse an XML in-memory document and build a tree.
6972 * Returns the resulting document tree
6975 htmlReadMemory(const char *buffer
, int size
, const char *URL
, const char *encoding
, int options
)
6977 htmlParserCtxtPtr ctxt
;
6980 ctxt
= xmlCreateMemoryParserCtxt(buffer
, size
);
6983 htmlDefaultSAXHandlerInit();
6984 if (ctxt
->sax
!= NULL
)
6985 memcpy(ctxt
->sax
, &htmlDefaultSAXHandler
, sizeof(xmlSAXHandlerV1
));
6986 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 0));
6991 * @fd: an open file descriptor
6992 * @URL: the base URL to use for the document
6993 * @encoding: the document encoding, or NULL
6994 * @options: a combination of htmlParserOption(s)
6996 * parse an HTML from a file descriptor and build a tree.
6997 * NOTE that the file descriptor will not be closed when the
6998 * reader is closed or reset.
7000 * Returns the resulting document tree
7003 htmlReadFd(int fd
, const char *URL
, const char *encoding
, int options
)
7005 htmlParserCtxtPtr ctxt
;
7006 xmlParserInputBufferPtr input
;
7007 htmlParserInputPtr stream
;
7013 input
= xmlParserInputBufferCreateFd(fd
, XML_CHAR_ENCODING_NONE
);
7016 input
->closecallback
= NULL
;
7017 ctxt
= htmlNewParserCtxt();
7019 xmlFreeParserInputBuffer(input
);
7022 stream
= xmlNewIOInputStream(ctxt
, input
, XML_CHAR_ENCODING_NONE
);
7023 if (stream
== NULL
) {
7024 xmlFreeParserInputBuffer(input
);
7025 htmlFreeParserCtxt(ctxt
);
7028 inputPush(ctxt
, stream
);
7029 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 0));
7034 * @ioread: an I/O read function
7035 * @ioclose: an I/O close function
7036 * @ioctx: an I/O handler
7037 * @URL: the base URL to use for the document
7038 * @encoding: the document encoding, or NULL
7039 * @options: a combination of htmlParserOption(s)
7041 * parse an HTML document from I/O functions and source and build a tree.
7043 * Returns the resulting document tree
7046 htmlReadIO(xmlInputReadCallback ioread
, xmlInputCloseCallback ioclose
,
7047 void *ioctx
, const char *URL
, const char *encoding
, int options
)
7049 htmlParserCtxtPtr ctxt
;
7050 xmlParserInputBufferPtr input
;
7051 xmlParserInputPtr stream
;
7057 input
= xmlParserInputBufferCreateIO(ioread
, ioclose
, ioctx
,
7058 XML_CHAR_ENCODING_NONE
);
7059 if (input
== NULL
) {
7060 if (ioclose
!= NULL
)
7064 ctxt
= htmlNewParserCtxt();
7066 xmlFreeParserInputBuffer(input
);
7069 stream
= xmlNewIOInputStream(ctxt
, input
, XML_CHAR_ENCODING_NONE
);
7070 if (stream
== NULL
) {
7071 xmlFreeParserInputBuffer(input
);
7072 xmlFreeParserCtxt(ctxt
);
7075 inputPush(ctxt
, stream
);
7076 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 0));
7081 * @ctxt: an HTML parser context
7082 * @cur: a pointer to a zero terminated string
7083 * @URL: the base URL to use for the document
7084 * @encoding: the document encoding, or NULL
7085 * @options: a combination of htmlParserOption(s)
7087 * parse an XML in-memory document and build a tree.
7088 * This reuses the existing @ctxt parser context
7090 * Returns the resulting document tree
7093 htmlCtxtReadDoc(htmlParserCtxtPtr ctxt
, const xmlChar
* cur
,
7094 const char *URL
, const char *encoding
, int options
)
7096 xmlParserInputPtr stream
;
7104 htmlCtxtReset(ctxt
);
7106 stream
= xmlNewStringInputStream(ctxt
, cur
);
7107 if (stream
== NULL
) {
7110 inputPush(ctxt
, stream
);
7111 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 1));
7116 * @ctxt: an HTML parser context
7117 * @filename: a file or URL
7118 * @encoding: the document encoding, or NULL
7119 * @options: a combination of htmlParserOption(s)
7121 * parse an XML file from the filesystem or the network.
7122 * This reuses the existing @ctxt parser context
7124 * Returns the resulting document tree
7127 htmlCtxtReadFile(htmlParserCtxtPtr ctxt
, const char *filename
,
7128 const char *encoding
, int options
)
7130 xmlParserInputPtr stream
;
7132 if (filename
== NULL
)
7138 htmlCtxtReset(ctxt
);
7140 stream
= xmlLoadExternalEntity(filename
, NULL
, ctxt
);
7141 if (stream
== NULL
) {
7144 inputPush(ctxt
, stream
);
7145 return (htmlDoRead(ctxt
, NULL
, encoding
, options
, 1));
7149 * htmlCtxtReadMemory:
7150 * @ctxt: an HTML parser context
7151 * @buffer: a pointer to a char array
7152 * @size: the size of the array
7153 * @URL: the base URL to use for the document
7154 * @encoding: the document encoding, or NULL
7155 * @options: a combination of htmlParserOption(s)
7157 * parse an XML in-memory document and build a tree.
7158 * This reuses the existing @ctxt parser context
7160 * Returns the resulting document tree
7163 htmlCtxtReadMemory(htmlParserCtxtPtr ctxt
, const char *buffer
, int size
,
7164 const char *URL
, const char *encoding
, int options
)
7166 xmlParserInputBufferPtr input
;
7167 xmlParserInputPtr stream
;
7175 htmlCtxtReset(ctxt
);
7177 input
= xmlParserInputBufferCreateMem(buffer
, size
, XML_CHAR_ENCODING_NONE
);
7178 if (input
== NULL
) {
7182 stream
= xmlNewIOInputStream(ctxt
, input
, XML_CHAR_ENCODING_NONE
);
7183 if (stream
== NULL
) {
7184 xmlFreeParserInputBuffer(input
);
7188 inputPush(ctxt
, stream
);
7189 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 1));
7194 * @ctxt: an HTML parser context
7195 * @fd: an open file descriptor
7196 * @URL: the base URL to use for the document
7197 * @encoding: the document encoding, or NULL
7198 * @options: a combination of htmlParserOption(s)
7200 * parse an XML from a file descriptor and build a tree.
7201 * This reuses the existing @ctxt parser context
7203 * Returns the resulting document tree
7206 htmlCtxtReadFd(htmlParserCtxtPtr ctxt
, int fd
,
7207 const char *URL
, const char *encoding
, int options
)
7209 xmlParserInputBufferPtr input
;
7210 xmlParserInputPtr stream
;
7218 htmlCtxtReset(ctxt
);
7221 input
= xmlParserInputBufferCreateFd(fd
, XML_CHAR_ENCODING_NONE
);
7224 stream
= xmlNewIOInputStream(ctxt
, input
, XML_CHAR_ENCODING_NONE
);
7225 if (stream
== NULL
) {
7226 xmlFreeParserInputBuffer(input
);
7229 inputPush(ctxt
, stream
);
7230 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 1));
7235 * @ctxt: an HTML parser context
7236 * @ioread: an I/O read function
7237 * @ioclose: an I/O close function
7238 * @ioctx: an I/O handler
7239 * @URL: the base URL to use for the document
7240 * @encoding: the document encoding, or NULL
7241 * @options: a combination of htmlParserOption(s)
7243 * parse an HTML document from I/O functions and source and build a tree.
7244 * This reuses the existing @ctxt parser context
7246 * Returns the resulting document tree
7249 htmlCtxtReadIO(htmlParserCtxtPtr ctxt
, xmlInputReadCallback ioread
,
7250 xmlInputCloseCallback ioclose
, void *ioctx
,
7252 const char *encoding
, int options
)
7254 xmlParserInputBufferPtr input
;
7255 xmlParserInputPtr stream
;
7263 htmlCtxtReset(ctxt
);
7265 input
= xmlParserInputBufferCreateIO(ioread
, ioclose
, ioctx
,
7266 XML_CHAR_ENCODING_NONE
);
7267 if (input
== NULL
) {
7268 if (ioclose
!= NULL
)
7272 stream
= xmlNewIOInputStream(ctxt
, input
, XML_CHAR_ENCODING_NONE
);
7273 if (stream
== NULL
) {
7274 xmlFreeParserInputBuffer(input
);
7277 inputPush(ctxt
, stream
);
7278 return (htmlDoRead(ctxt
, URL
, encoding
, options
, 1));
7281 #define bottom_HTMLparser
7282 #include "elfgcchack.h"
7283 #endif /* LIBXML_HTML_ENABLED */