HTMLparser.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /*
  76. * There is only few public functions.
  77. */
  78. XML_DEPRECATED
  79. XMLPUBFUN void
  80. htmlInitAutoClose (void);
  81. XMLPUBFUN const htmlElemDesc *
  82. htmlTagLookup (const xmlChar *tag);
  83. XMLPUBFUN const htmlEntityDesc *
  84. htmlEntityLookup(const xmlChar *name);
  85. XMLPUBFUN const htmlEntityDesc *
  86. htmlEntityValueLookup(unsigned int value);
  87. XMLPUBFUN int
  88. htmlIsAutoClosed(htmlDocPtr doc,
  89. htmlNodePtr elem);
  90. XMLPUBFUN int
  91. htmlAutoCloseTag(htmlDocPtr doc,
  92. const xmlChar *name,
  93. htmlNodePtr elem);
  94. XML_DEPRECATED
  95. XMLPUBFUN const htmlEntityDesc *
  96. htmlParseEntityRef(htmlParserCtxtPtr ctxt,
  97. const xmlChar **str);
  98. XML_DEPRECATED
  99. XMLPUBFUN int
  100. htmlParseCharRef(htmlParserCtxtPtr ctxt);
  101. XML_DEPRECATED
  102. XMLPUBFUN void
  103. htmlParseElement(htmlParserCtxtPtr ctxt);
  104. XMLPUBFUN htmlParserCtxtPtr
  105. htmlNewParserCtxt(void);
  106. XMLPUBFUN htmlParserCtxtPtr
  107. htmlNewSAXParserCtxt(const htmlSAXHandler *sax,
  108. void *userData);
  109. XMLPUBFUN htmlParserCtxtPtr
  110. htmlCreateMemoryParserCtxt(const char *buffer,
  111. int size);
  112. XMLPUBFUN int
  113. htmlParseDocument(htmlParserCtxtPtr ctxt);
  114. XML_DEPRECATED
  115. XMLPUBFUN htmlDocPtr
  116. htmlSAXParseDoc (const xmlChar *cur,
  117. const char *encoding,
  118. htmlSAXHandlerPtr sax,
  119. void *userData);
  120. XMLPUBFUN htmlDocPtr
  121. htmlParseDoc (const xmlChar *cur,
  122. const char *encoding);
  123. XMLPUBFUN htmlParserCtxtPtr
  124. htmlCreateFileParserCtxt(const char *filename,
  125. const char *encoding);
  126. XML_DEPRECATED
  127. XMLPUBFUN htmlDocPtr
  128. htmlSAXParseFile(const char *filename,
  129. const char *encoding,
  130. htmlSAXHandlerPtr sax,
  131. void *userData);
  132. XMLPUBFUN htmlDocPtr
  133. htmlParseFile (const char *filename,
  134. const char *encoding);
  135. XMLPUBFUN int
  136. UTF8ToHtml (unsigned char *out,
  137. int *outlen,
  138. const unsigned char *in,
  139. int *inlen);
  140. XMLPUBFUN int
  141. htmlEncodeEntities(unsigned char *out,
  142. int *outlen,
  143. const unsigned char *in,
  144. int *inlen, int quoteChar);
  145. XMLPUBFUN int
  146. htmlIsScriptAttribute(const xmlChar *name);
  147. XMLPUBFUN int
  148. htmlHandleOmittedElem(int val);
  149. #ifdef LIBXML_PUSH_ENABLED
  150. /**
  151. * Interfaces for the Push mode.
  152. */
  153. XMLPUBFUN htmlParserCtxtPtr
  154. htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax,
  155. void *user_data,
  156. const char *chunk,
  157. int size,
  158. const char *filename,
  159. xmlCharEncoding enc);
  160. XMLPUBFUN int
  161. htmlParseChunk (htmlParserCtxtPtr ctxt,
  162. const char *chunk,
  163. int size,
  164. int terminate);
  165. #endif /* LIBXML_PUSH_ENABLED */
  166. XMLPUBFUN void
  167. htmlFreeParserCtxt (htmlParserCtxtPtr ctxt);
  168. /*
  169. * New set of simpler/more flexible APIs
  170. */
  171. /**
  172. * xmlParserOption:
  173. *
  174. * This is the set of XML parser options that can be passed down
  175. * to the xmlReadDoc() and similar calls.
  176. */
  177. typedef enum {
  178. HTML_PARSE_RECOVER = 1<<0, /* Relaxed parsing */
  179. HTML_PARSE_NODEFDTD = 1<<2, /* do not default a doctype if not found */
  180. HTML_PARSE_NOERROR = 1<<5, /* suppress error reports */
  181. HTML_PARSE_NOWARNING= 1<<6, /* suppress warning reports */
  182. HTML_PARSE_PEDANTIC = 1<<7, /* pedantic error reporting */
  183. HTML_PARSE_NOBLANKS = 1<<8, /* remove blank nodes */
  184. HTML_PARSE_NONET = 1<<11,/* Forbid network access */
  185. HTML_PARSE_NOIMPLIED= 1<<13,/* Do not add implied html/body... elements */
  186. HTML_PARSE_COMPACT = 1<<16,/* compact small text nodes */
  187. HTML_PARSE_IGNORE_ENC=1<<21 /* ignore internal document encoding hint */
  188. } htmlParserOption;
  189. XMLPUBFUN void
  190. htmlCtxtReset (htmlParserCtxtPtr ctxt);
  191. XMLPUBFUN int
  192. htmlCtxtUseOptions (htmlParserCtxtPtr ctxt,
  193. int options);
  194. XMLPUBFUN htmlDocPtr
  195. htmlReadDoc (const xmlChar *cur,
  196. const char *URL,
  197. const char *encoding,
  198. int options);
  199. XMLPUBFUN htmlDocPtr
  200. htmlReadFile (const char *URL,
  201. const char *encoding,
  202. int options);
  203. XMLPUBFUN htmlDocPtr
  204. htmlReadMemory (const char *buffer,
  205. int size,
  206. const char *URL,
  207. const char *encoding,
  208. int options);
  209. XMLPUBFUN htmlDocPtr
  210. htmlReadFd (int fd,
  211. const char *URL,
  212. const char *encoding,
  213. int options);
  214. XMLPUBFUN htmlDocPtr
  215. htmlReadIO (xmlInputReadCallback ioread,
  216. xmlInputCloseCallback ioclose,
  217. void *ioctx,
  218. const char *URL,
  219. const char *encoding,
  220. int options);
  221. XMLPUBFUN htmlDocPtr
  222. htmlCtxtReadDoc (xmlParserCtxtPtr ctxt,
  223. const xmlChar *cur,
  224. const char *URL,
  225. const char *encoding,
  226. int options);
  227. XMLPUBFUN htmlDocPtr
  228. htmlCtxtReadFile (xmlParserCtxtPtr ctxt,
  229. const char *filename,
  230. const char *encoding,
  231. int options);
  232. XMLPUBFUN htmlDocPtr
  233. htmlCtxtReadMemory (xmlParserCtxtPtr ctxt,
  234. const char *buffer,
  235. int size,
  236. const char *URL,
  237. const char *encoding,
  238. int options);
  239. XMLPUBFUN htmlDocPtr
  240. htmlCtxtReadFd (xmlParserCtxtPtr ctxt,
  241. int fd,
  242. const char *URL,
  243. const char *encoding,
  244. int options);
  245. XMLPUBFUN htmlDocPtr
  246. htmlCtxtReadIO (xmlParserCtxtPtr ctxt,
  247. xmlInputReadCallback ioread,
  248. xmlInputCloseCallback ioclose,
  249. void *ioctx,
  250. const char *URL,
  251. const char *encoding,
  252. int options);
  253. /* NRK/Jan2003: further knowledge of HTML structure
  254. */
  255. typedef enum {
  256. HTML_NA = 0 , /* something we don't check at all */
  257. HTML_INVALID = 0x1 ,
  258. HTML_DEPRECATED = 0x2 ,
  259. HTML_VALID = 0x4 ,
  260. HTML_REQUIRED = 0xc /* VALID bit set so ( & HTML_VALID ) is TRUE */
  261. } htmlStatus ;
  262. /* Using htmlElemDesc rather than name here, to emphasise the fact
  263. that otherwise there's a lookup overhead
  264. */
  265. XMLPUBFUN htmlStatus htmlAttrAllowed(const htmlElemDesc*, const xmlChar*, int) ;
  266. XMLPUBFUN int htmlElementAllowedHere(const htmlElemDesc*, const xmlChar*) ;
  267. XMLPUBFUN htmlStatus htmlElementStatusHere(const htmlElemDesc*, const htmlElemDesc*) ;
  268. XMLPUBFUN htmlStatus htmlNodeStatus(const htmlNodePtr, int) ;
  269. /**
  270. * htmlDefaultSubelement:
  271. * @elt: HTML element
  272. *
  273. * Returns the default subelement for this element
  274. */
  275. #define htmlDefaultSubelement(elt) elt->defaultsubelt
  276. /**
  277. * htmlElementAllowedHereDesc:
  278. * @parent: HTML parent element
  279. * @elt: HTML element
  280. *
  281. * Checks whether an HTML element description may be a
  282. * direct child of the specified element.
  283. *
  284. * Returns 1 if allowed; 0 otherwise.
  285. */
  286. #define htmlElementAllowedHereDesc(parent,elt) \
  287. htmlElementAllowedHere((parent), (elt)->name)
  288. /**
  289. * htmlRequiredAttrs:
  290. * @elt: HTML element
  291. *
  292. * Returns the attributes required for the specified element.
  293. */
  294. #define htmlRequiredAttrs(elt) (elt)->attrs_req
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif /* LIBXML_HTML_ENABLED */
  299. #endif /* __HTML_PARSER_H__ */