HTMLparser.h 9.1 KB

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