HTMLparser.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Summary: interface for an HTML 4.0 non-verifying parser
  3. * Description: this module implements an HTML 4.0 non-verifying parser
  4. * with API compatible with the XML parser ones. It should
  5. * be able to parse "real world" HTML, even if severely
  6. * broken from a specification point of view.
  7. *
  8. * Copy: See Copyright for the status of this software.
  9. *
  10. * Author: Daniel Veillard
  11. */
  12. #ifndef __HTML_PARSER_H__
  13. #define __HTML_PARSER_H__
  14. #include <libxml/xmlversion.h>
  15. #include <libxml/parser.h>
  16. #ifdef LIBXML_HTML_ENABLED
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*
  21. * Most of the back-end structures from XML and HTML are shared.
  22. */
  23. typedef xmlParserCtxt htmlParserCtxt;
  24. typedef xmlParserCtxtPtr htmlParserCtxtPtr;
  25. typedef xmlParserNodeInfo htmlParserNodeInfo;
  26. typedef xmlSAXHandler htmlSAXHandler;
  27. typedef xmlSAXHandlerPtr htmlSAXHandlerPtr;
  28. typedef xmlParserInput htmlParserInput;
  29. typedef xmlParserInputPtr htmlParserInputPtr;
  30. typedef xmlDocPtr htmlDocPtr;
  31. typedef xmlNodePtr htmlNodePtr;
  32. /*
  33. * Internal description of an HTML element, representing HTML 4.01
  34. * and XHTML 1.0 (which share the same structure).
  35. */
  36. typedef struct _htmlElemDesc htmlElemDesc;
  37. typedef htmlElemDesc *htmlElemDescPtr;
  38. struct _htmlElemDesc {
  39. const char *name; /* The tag name */
  40. char startTag; /* Whether the start tag can be implied */
  41. char endTag; /* Whether the end tag can be implied */
  42. char saveEndTag; /* Whether the end tag should be saved */
  43. char empty; /* Is this an empty element ? */
  44. char depr; /* Is this a deprecated element ? */
  45. char dtd; /* 1: only in Loose DTD, 2: only Frameset one */
  46. char isinline; /* is this a block 0 or inline 1 element */
  47. const char *desc; /* the description */
  48. /* NRK Jan.2003
  49. * New fields encapsulating HTML structure
  50. *
  51. * Bugs:
  52. * This is a very limited representation. It fails to tell us when
  53. * an element *requires* subelements (we only have whether they're
  54. * allowed or not), and it doesn't tell us where CDATA and PCDATA
  55. * are allowed. Some element relationships are not fully represented:
  56. * these are flagged with the word MODIFIER
  57. */
  58. const char** subelts; /* allowed sub-elements of this element */
  59. const char* defaultsubelt; /* subelement for suggested auto-repair
  60. if necessary or NULL */
  61. const char** attrs_opt; /* Optional Attributes */
  62. const char** attrs_depr; /* Additional deprecated attributes */
  63. const char** attrs_req; /* Required attributes */
  64. };
  65. /*
  66. * Internal description of an HTML entity.
  67. */
  68. typedef struct _htmlEntityDesc htmlEntityDesc;
  69. typedef htmlEntityDesc *htmlEntityDescPtr;
  70. struct _htmlEntityDesc {
  71. unsigned int value; /* the UNICODE value for the character */
  72. const char *name; /* The entity name */
  73. const char *desc; /* the description */
  74. };
  75. /** DOC_DISABLE */
  76. #ifdef LIBXML_SAX1_ENABLED
  77. #define XML_GLOBALS_HTML \
  78. XML_OP(htmlDefaultSAXHandler, xmlSAXHandlerV1, XML_DEPRECATED)
  79. #else
  80. #define XML_GLOBALS_HTML
  81. #endif
  82. #define XML_OP XML_DECLARE_GLOBAL
  83. XML_GLOBALS_HTML
  84. #undef XML_OP
  85. #if defined(LIBXML_THREAD_ENABLED) && !defined(XML_GLOBALS_NO_REDEFINITION)
  86. #define htmlDefaultSAXHandler XML_GLOBAL_MACRO(htmlDefaultSAXHandler)
  87. #endif
  88. /** DOC_ENABLE */
  89. /*
  90. * There is only few public functions.
  91. */
  92. XML_DEPRECATED
  93. XMLPUBFUN void
  94. htmlInitAutoClose (void);
  95. XMLPUBFUN const htmlElemDesc *
  96. htmlTagLookup (const xmlChar *tag);
  97. XMLPUBFUN const htmlEntityDesc *
  98. htmlEntityLookup(const xmlChar *name);
  99. XMLPUBFUN const htmlEntityDesc *
  100. htmlEntityValueLookup(unsigned int value);
  101. XMLPUBFUN int
  102. htmlIsAutoClosed(htmlDocPtr doc,
  103. htmlNodePtr elem);
  104. XMLPUBFUN int
  105. htmlAutoCloseTag(htmlDocPtr doc,
  106. const xmlChar *name,
  107. htmlNodePtr elem);
  108. XML_DEPRECATED
  109. XMLPUBFUN const htmlEntityDesc *
  110. htmlParseEntityRef(htmlParserCtxtPtr ctxt,
  111. const xmlChar **str);
  112. XML_DEPRECATED
  113. XMLPUBFUN int
  114. htmlParseCharRef(htmlParserCtxtPtr ctxt);
  115. XML_DEPRECATED
  116. XMLPUBFUN void
  117. htmlParseElement(htmlParserCtxtPtr ctxt);
  118. XMLPUBFUN htmlParserCtxtPtr
  119. htmlNewParserCtxt(void);
  120. XMLPUBFUN htmlParserCtxtPtr
  121. htmlNewSAXParserCtxt(const htmlSAXHandler *sax,
  122. void *userData);
  123. XMLPUBFUN htmlParserCtxtPtr
  124. htmlCreateMemoryParserCtxt(const char *buffer,
  125. int size);
  126. XMLPUBFUN int
  127. htmlParseDocument(htmlParserCtxtPtr ctxt);
  128. XML_DEPRECATED
  129. XMLPUBFUN htmlDocPtr
  130. htmlSAXParseDoc (const xmlChar *cur,
  131. const char *encoding,
  132. htmlSAXHandlerPtr sax,
  133. void *userData);
  134. XMLPUBFUN htmlDocPtr
  135. htmlParseDoc (const xmlChar *cur,
  136. const char *encoding);
  137. XMLPUBFUN htmlParserCtxtPtr
  138. htmlCreateFileParserCtxt(const char *filename,
  139. const char *encoding);
  140. XML_DEPRECATED
  141. XMLPUBFUN htmlDocPtr
  142. htmlSAXParseFile(const char *filename,
  143. const char *encoding,
  144. htmlSAXHandlerPtr sax,
  145. void *userData);
  146. XMLPUBFUN htmlDocPtr
  147. htmlParseFile (const char *filename,
  148. const char *encoding);
  149. XMLPUBFUN int
  150. UTF8ToHtml (unsigned char *out,
  151. int *outlen,
  152. const unsigned char *in,
  153. int *inlen);
  154. XMLPUBFUN int
  155. htmlEncodeEntities(unsigned char *out,
  156. int *outlen,
  157. const unsigned char *in,
  158. int *inlen, int quoteChar);
  159. XMLPUBFUN int
  160. htmlIsScriptAttribute(const xmlChar *name);
  161. XMLPUBFUN int
  162. htmlHandleOmittedElem(int val);
  163. #ifdef LIBXML_PUSH_ENABLED
  164. /**
  165. * Interfaces for the Push mode.
  166. */
  167. XMLPUBFUN htmlParserCtxtPtr
  168. htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax,
  169. void *user_data,
  170. const char *chunk,
  171. int size,
  172. const char *filename,
  173. xmlCharEncoding enc);
  174. XMLPUBFUN int
  175. htmlParseChunk (htmlParserCtxtPtr ctxt,
  176. const char *chunk,
  177. int size,
  178. int terminate);
  179. #endif /* LIBXML_PUSH_ENABLED */
  180. XMLPUBFUN void
  181. htmlFreeParserCtxt (htmlParserCtxtPtr ctxt);
  182. /*
  183. * New set of simpler/more flexible APIs
  184. */
  185. /**
  186. * xmlParserOption:
  187. *
  188. * This is the set of XML parser options that can be passed down
  189. * to the xmlReadDoc() and similar calls.
  190. */
  191. typedef enum {
  192. HTML_PARSE_RECOVER = 1<<0, /* Relaxed parsing */
  193. HTML_PARSE_NODEFDTD = 1<<2, /* do not default a doctype if not found */
  194. HTML_PARSE_NOERROR = 1<<5, /* suppress error reports */
  195. HTML_PARSE_NOWARNING= 1<<6, /* suppress warning reports */
  196. HTML_PARSE_PEDANTIC = 1<<7, /* pedantic error reporting */
  197. HTML_PARSE_NOBLANKS = 1<<8, /* remove blank nodes */
  198. HTML_PARSE_NONET = 1<<11,/* Forbid network access */
  199. HTML_PARSE_NOIMPLIED= 1<<13,/* Do not add implied html/body... elements */
  200. HTML_PARSE_COMPACT = 1<<16,/* compact small text nodes */
  201. HTML_PARSE_IGNORE_ENC=1<<21 /* ignore internal document encoding hint */
  202. } htmlParserOption;
  203. XMLPUBFUN void
  204. htmlCtxtReset (htmlParserCtxtPtr ctxt);
  205. XMLPUBFUN int
  206. htmlCtxtUseOptions (htmlParserCtxtPtr ctxt,
  207. int options);
  208. XMLPUBFUN htmlDocPtr
  209. htmlReadDoc (const xmlChar *cur,
  210. const char *URL,
  211. const char *encoding,
  212. int options);
  213. XMLPUBFUN htmlDocPtr
  214. htmlReadFile (const char *URL,
  215. const char *encoding,
  216. int options);
  217. XMLPUBFUN htmlDocPtr
  218. htmlReadMemory (const char *buffer,
  219. int size,
  220. const char *URL,
  221. const char *encoding,
  222. int options);
  223. XMLPUBFUN htmlDocPtr
  224. htmlReadFd (int fd,
  225. const char *URL,
  226. const char *encoding,
  227. int options);
  228. XMLPUBFUN htmlDocPtr
  229. htmlReadIO (xmlInputReadCallback ioread,
  230. xmlInputCloseCallback ioclose,
  231. void *ioctx,
  232. const char *URL,
  233. const char *encoding,
  234. int options);
  235. XMLPUBFUN htmlDocPtr
  236. htmlCtxtReadDoc (xmlParserCtxtPtr ctxt,
  237. const xmlChar *cur,
  238. const char *URL,
  239. const char *encoding,
  240. int options);
  241. XMLPUBFUN htmlDocPtr
  242. htmlCtxtReadFile (xmlParserCtxtPtr ctxt,
  243. const char *filename,
  244. const char *encoding,
  245. int options);
  246. XMLPUBFUN htmlDocPtr
  247. htmlCtxtReadMemory (xmlParserCtxtPtr ctxt,
  248. const char *buffer,
  249. int size,
  250. const char *URL,
  251. const char *encoding,
  252. int options);
  253. XMLPUBFUN htmlDocPtr
  254. htmlCtxtReadFd (xmlParserCtxtPtr ctxt,
  255. int fd,
  256. const char *URL,
  257. const char *encoding,
  258. int options);
  259. XMLPUBFUN htmlDocPtr
  260. htmlCtxtReadIO (xmlParserCtxtPtr ctxt,
  261. xmlInputReadCallback ioread,
  262. xmlInputCloseCallback ioclose,
  263. void *ioctx,
  264. const char *URL,
  265. const char *encoding,
  266. int options);
  267. /* NRK/Jan2003: further knowledge of HTML structure
  268. */
  269. typedef enum {
  270. HTML_NA = 0 , /* something we don't check at all */
  271. HTML_INVALID = 0x1 ,
  272. HTML_DEPRECATED = 0x2 ,
  273. HTML_VALID = 0x4 ,
  274. HTML_REQUIRED = 0xc /* VALID bit set so ( & HTML_VALID ) is TRUE */
  275. } htmlStatus ;
  276. /* Using htmlElemDesc rather than name here, to emphasise the fact
  277. that otherwise there's a lookup overhead
  278. */
  279. XMLPUBFUN htmlStatus htmlAttrAllowed(const htmlElemDesc*, const xmlChar*, int) ;
  280. XMLPUBFUN int htmlElementAllowedHere(const htmlElemDesc*, const xmlChar*) ;
  281. XMLPUBFUN htmlStatus htmlElementStatusHere(const htmlElemDesc*, const htmlElemDesc*) ;
  282. XMLPUBFUN htmlStatus htmlNodeStatus(const htmlNodePtr, int) ;
  283. /**
  284. * htmlDefaultSubelement:
  285. * @elt: HTML element
  286. *
  287. * Returns the default subelement for this element
  288. */
  289. #define htmlDefaultSubelement(elt) elt->defaultsubelt
  290. /**
  291. * htmlElementAllowedHereDesc:
  292. * @parent: HTML parent element
  293. * @elt: HTML element
  294. *
  295. * Checks whether an HTML element description may be a
  296. * direct child of the specified element.
  297. *
  298. * Returns 1 if allowed; 0 otherwise.
  299. */
  300. #define htmlElementAllowedHereDesc(parent,elt) \
  301. htmlElementAllowedHere((parent), (elt)->name)
  302. /**
  303. * htmlRequiredAttrs:
  304. * @elt: HTML element
  305. *
  306. * Returns the attributes required for the specified element.
  307. */
  308. #define htmlRequiredAttrs(elt) (elt)->attrs_req
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #else /* LIBXML_HTML_ENABLED */
  313. /** DOC_DISABLE */
  314. #define XML_GLOBALS_HTML
  315. /** DOC_ENABLE */
  316. #endif /* LIBXML_HTML_ENABLED */
  317. #endif /* __HTML_PARSER_H__ */