HTMLparser.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_NODEFDTD = 1<<2, /* do not default a doctype if not found */
  166. HTML_PARSE_NOERROR = 1<<5, /* suppress error reports */
  167. HTML_PARSE_NOWARNING= 1<<6, /* suppress warning reports */
  168. HTML_PARSE_PEDANTIC = 1<<7, /* pedantic error reporting */
  169. HTML_PARSE_NOBLANKS = 1<<8, /* remove blank nodes */
  170. HTML_PARSE_NONET = 1<<11,/* Forbid network access */
  171. HTML_PARSE_NOIMPLIED= 1<<13,/* Do not add implied html/body... elements */
  172. HTML_PARSE_COMPACT = 1<<16,/* compact small text nodes */
  173. HTML_PARSE_IGNORE_ENC=1<<21 /* ignore internal document encoding hint */
  174. } htmlParserOption;
  175. XMLPUBFUN void XMLCALL
  176. htmlCtxtReset (htmlParserCtxtPtr ctxt);
  177. XMLPUBFUN int XMLCALL
  178. htmlCtxtUseOptions (htmlParserCtxtPtr ctxt,
  179. int options);
  180. XMLPUBFUN htmlDocPtr XMLCALL
  181. htmlReadDoc (const xmlChar *cur,
  182. const char *URL,
  183. const char *encoding,
  184. int options);
  185. XMLPUBFUN htmlDocPtr XMLCALL
  186. htmlReadFile (const char *URL,
  187. const char *encoding,
  188. int options);
  189. XMLPUBFUN htmlDocPtr XMLCALL
  190. htmlReadMemory (const char *buffer,
  191. int size,
  192. const char *URL,
  193. const char *encoding,
  194. int options);
  195. XMLPUBFUN htmlDocPtr XMLCALL
  196. htmlReadFd (int fd,
  197. const char *URL,
  198. const char *encoding,
  199. int options);
  200. XMLPUBFUN htmlDocPtr XMLCALL
  201. htmlReadIO (xmlInputReadCallback ioread,
  202. xmlInputCloseCallback ioclose,
  203. void *ioctx,
  204. const char *URL,
  205. const char *encoding,
  206. int options);
  207. XMLPUBFUN htmlDocPtr XMLCALL
  208. htmlCtxtReadDoc (xmlParserCtxtPtr ctxt,
  209. const xmlChar *cur,
  210. const char *URL,
  211. const char *encoding,
  212. int options);
  213. XMLPUBFUN htmlDocPtr XMLCALL
  214. htmlCtxtReadFile (xmlParserCtxtPtr ctxt,
  215. const char *filename,
  216. const char *encoding,
  217. int options);
  218. XMLPUBFUN htmlDocPtr XMLCALL
  219. htmlCtxtReadMemory (xmlParserCtxtPtr ctxt,
  220. const char *buffer,
  221. int size,
  222. const char *URL,
  223. const char *encoding,
  224. int options);
  225. XMLPUBFUN htmlDocPtr XMLCALL
  226. htmlCtxtReadFd (xmlParserCtxtPtr ctxt,
  227. int fd,
  228. const char *URL,
  229. const char *encoding,
  230. int options);
  231. XMLPUBFUN htmlDocPtr XMLCALL
  232. htmlCtxtReadIO (xmlParserCtxtPtr ctxt,
  233. xmlInputReadCallback ioread,
  234. xmlInputCloseCallback ioclose,
  235. void *ioctx,
  236. const char *URL,
  237. const char *encoding,
  238. int options);
  239. /* NRK/Jan2003: further knowledge of HTML structure
  240. */
  241. typedef enum {
  242. HTML_NA = 0 , /* something we don't check at all */
  243. HTML_INVALID = 0x1 ,
  244. HTML_DEPRECATED = 0x2 ,
  245. HTML_VALID = 0x4 ,
  246. HTML_REQUIRED = 0xc /* VALID bit set so ( & HTML_VALID ) is TRUE */
  247. } htmlStatus ;
  248. /* Using htmlElemDesc rather than name here, to emphasise the fact
  249. that otherwise there's a lookup overhead
  250. */
  251. XMLPUBFUN htmlStatus XMLCALL htmlAttrAllowed(const htmlElemDesc*, const xmlChar*, int) ;
  252. XMLPUBFUN int XMLCALL htmlElementAllowedHere(const htmlElemDesc*, const xmlChar*) ;
  253. XMLPUBFUN htmlStatus XMLCALL htmlElementStatusHere(const htmlElemDesc*, const htmlElemDesc*) ;
  254. XMLPUBFUN htmlStatus XMLCALL htmlNodeStatus(const htmlNodePtr, int) ;
  255. /**
  256. * htmlDefaultSubelement:
  257. * @elt: HTML element
  258. *
  259. * Returns the default subelement for this element
  260. */
  261. #define htmlDefaultSubelement(elt) elt->defaultsubelt
  262. /**
  263. * htmlElementAllowedHereDesc:
  264. * @parent: HTML parent element
  265. * @elt: HTML element
  266. *
  267. * Checks whether an HTML element description may be a
  268. * direct child of the specified element.
  269. *
  270. * Returns 1 if allowed; 0 otherwise.
  271. */
  272. #define htmlElementAllowedHereDesc(parent,elt) \
  273. htmlElementAllowedHere((parent), (elt)->name)
  274. /**
  275. * htmlRequiredAttrs:
  276. * @elt: HTML element
  277. *
  278. * Returns the attributes required for the specified element.
  279. */
  280. #define htmlRequiredAttrs(elt) (elt)->attrs_req
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif /* LIBXML_HTML_ENABLED */
  285. #endif /* __HTML_PARSER_H__ */