/lib/gems/gems/libxml-ruby-2.0.6-x86-mingw32/ext/libxml/ruby_xml_html_parser_context.c

http://github.com/shouldly/shouldly · C · 306 lines · 199 code · 40 blank · 67 comment · 30 complexity · 9c516059ea5f80b3ad4242e1936b1899 MD5 · raw file

  1. /* Please see the LICENSE file for copyright and distribution information */
  2. #include "ruby_libxml.h"
  3. #include "ruby_xml_html_parser_context.h"
  4. /*
  5. * Document-class: LibXML::XML::HTMLParser::Context
  6. *
  7. * The XML::HTMLParser::Context class provides in-depth control over how
  8. * a document is parsed.
  9. */
  10. VALUE cXMLHtmlParserContext;
  11. static ID IO_ATTR;
  12. /* OS X 10.5 ships with libxml2 version 2.6.16 which does not expose the
  13. htmlNewParserCtxt (or htmlInitParserCtxt which it uses) method. htmlNewParserCtxt
  14. wasn't added to the libxml2 header files until 2.6.27. So the next two
  15. methods are simply copied from a newer version of libxml2 (2.7.2). */
  16. #if LIBXML_VERSION < 20627
  17. #define XML_CTXT_FINISH_DTD_0 0xabcd1234
  18. static int htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
  19. {
  20. htmlSAXHandler *sax;
  21. if (ctxt == NULL) return(-1);
  22. memset(ctxt, 0, sizeof(htmlParserCtxt));
  23. ctxt->dict = xmlDictCreate();
  24. if (ctxt->dict == NULL) {
  25. rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
  26. return(-1);
  27. }
  28. sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
  29. if (sax == NULL) {
  30. rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
  31. return(-1);
  32. }
  33. else
  34. memset(sax, 0, sizeof(htmlSAXHandler));
  35. ctxt->inputTab = (htmlParserInputPtr *) xmlMalloc(5 * sizeof(htmlParserInputPtr));
  36. if (ctxt->inputTab == NULL) {
  37. rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
  38. ctxt->inputNr = 0;
  39. ctxt->inputMax = 0;
  40. ctxt->input = NULL;
  41. return(-1);
  42. }
  43. ctxt->inputNr = 0;
  44. ctxt->inputMax = 5;
  45. ctxt->input = NULL;
  46. ctxt->version = NULL;
  47. ctxt->encoding = NULL;
  48. ctxt->standalone = -1;
  49. ctxt->instate = XML_PARSER_START;
  50. ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
  51. if (ctxt->nodeTab == NULL) {
  52. rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
  53. ctxt->nodeNr = 0;
  54. ctxt->nodeMax = 0;
  55. ctxt->node = NULL;
  56. ctxt->inputNr = 0;
  57. ctxt->inputMax = 0;
  58. ctxt->input = NULL;
  59. return(-1);
  60. }
  61. ctxt->nodeNr = 0;
  62. ctxt->nodeMax = 10;
  63. ctxt->node = NULL;
  64. ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
  65. if (ctxt->nameTab == NULL) {
  66. rb_raise(rb_eNoMemError, "htmlInitParserCtxt: out of memory\n");
  67. ctxt->nameNr = 0;
  68. ctxt->nameMax = 10;
  69. ctxt->name = NULL;
  70. ctxt->nodeNr = 0;
  71. ctxt->nodeMax = 0;
  72. ctxt->node = NULL;
  73. ctxt->inputNr = 0;
  74. ctxt->inputMax = 0;
  75. ctxt->input = NULL;
  76. return(-1);
  77. }
  78. ctxt->nameNr = 0;
  79. ctxt->nameMax = 10;
  80. ctxt->name = NULL;
  81. if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
  82. else {
  83. ctxt->sax = sax;
  84. memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
  85. }
  86. ctxt->userData = ctxt;
  87. ctxt->myDoc = NULL;
  88. ctxt->wellFormed = 1;
  89. ctxt->replaceEntities = 0;
  90. ctxt->linenumbers = xmlLineNumbersDefaultValue;
  91. ctxt->html = 1;
  92. ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
  93. ctxt->vctxt.userData = ctxt;
  94. ctxt->vctxt.error = xmlParserValidityError;
  95. ctxt->vctxt.warning = xmlParserValidityWarning;
  96. ctxt->record_info = 0;
  97. ctxt->validate = 0;
  98. ctxt->nbChars = 0;
  99. ctxt->checkIndex = 0;
  100. ctxt->catalogs = NULL;
  101. xmlInitNodeInfoSeq(&ctxt->node_seq);
  102. return(0);
  103. }
  104. static htmlParserCtxtPtr htmlNewParserCtxt(void)
  105. {
  106. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
  107. if (ctxt == NULL) {
  108. rb_raise(rb_eNoMemError, "NewParserCtxt: out of memory\n");
  109. return(NULL);
  110. }
  111. memset(ctxt, 0, sizeof(xmlParserCtxt));
  112. if (htmlInitParserCtxt(ctxt) < 0) {
  113. htmlFreeParserCtxt(ctxt);
  114. return(NULL);
  115. }
  116. return(ctxt);
  117. }
  118. #endif
  119. static void rxml_html_parser_context_free(htmlParserCtxtPtr ctxt)
  120. {
  121. htmlFreeParserCtxt(ctxt);
  122. }
  123. static VALUE rxml_html_parser_context_wrap(htmlParserCtxtPtr ctxt)
  124. {
  125. return Data_Wrap_Struct(cXMLHtmlParserContext, NULL, rxml_html_parser_context_free, ctxt);
  126. }
  127. /* call-seq:
  128. * XML::HTMLParser::Context.file(file) -> XML::HTMLParser::Context
  129. *
  130. * Creates a new parser context based on the specified file or uri.
  131. *
  132. * Parameters:
  133. *
  134. * file - A filename or uri.
  135. */
  136. static VALUE rxml_html_parser_context_file(VALUE klass, VALUE file)
  137. {
  138. htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
  139. if (!ctxt)
  140. rxml_raise(&xmlLastError);
  141. /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
  142. xmlCtxtUseOptionsInternal (called below) initialize slightly different
  143. context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
  144. sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
  145. htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
  146. return rxml_html_parser_context_wrap(ctxt);
  147. }
  148. /* call-seq:
  149. * XML::HTMLParser::Context.io(io) -> XML::HTMLParser::Context
  150. *
  151. * Creates a new parser context based on the specified io object.
  152. *
  153. * Parameters:
  154. *
  155. * io - A ruby IO object.
  156. */
  157. static VALUE rxml_html_parser_context_io(VALUE klass, VALUE io)
  158. {
  159. VALUE result;
  160. htmlParserCtxtPtr ctxt;
  161. xmlParserInputBufferPtr input;
  162. xmlParserInputPtr stream;
  163. if (NIL_P(io))
  164. rb_raise(rb_eTypeError, "Must pass in an IO object");
  165. input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL,
  166. (void*)io, XML_CHAR_ENCODING_NONE);
  167. ctxt = htmlNewParserCtxt();
  168. if (!ctxt)
  169. {
  170. xmlFreeParserInputBuffer(input);
  171. rxml_raise(&xmlLastError);
  172. }
  173. /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
  174. xmlCtxtUseOptionsInternal (called below) initialize slightly different
  175. context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
  176. sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
  177. htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
  178. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  179. if (!stream)
  180. {
  181. xmlFreeParserInputBuffer(input);
  182. xmlFreeParserCtxt(ctxt);
  183. rxml_raise(&xmlLastError);
  184. }
  185. inputPush(ctxt, stream);
  186. result = rxml_html_parser_context_wrap(ctxt);
  187. /* Attach io object to parser so it won't get freed.*/
  188. rb_ivar_set(result, IO_ATTR, io);
  189. return result;
  190. }
  191. /* call-seq:
  192. * XML::HTMLParser::Context.string(string) -> XML::HTMLParser::Context
  193. *
  194. * Creates a new parser context based on the specified string.
  195. *
  196. * Parameters:
  197. *
  198. * string - A string that contains the data to parse.
  199. */
  200. static VALUE rxml_html_parser_context_string(VALUE klass, VALUE string)
  201. {
  202. htmlParserCtxtPtr ctxt;
  203. Check_Type(string, T_STRING);
  204. if (RSTRING_LEN(string) == 0)
  205. rb_raise(rb_eArgError, "Must specify a string with one or more characters");
  206. ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
  207. RSTRING_LEN(string));
  208. if (!ctxt)
  209. rxml_raise(&xmlLastError);
  210. /* This is annoying, but xmlInitParserCtxt (called indirectly above) and
  211. xmlCtxtUseOptionsInternal (called below) initialize slightly different
  212. context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
  213. sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
  214. htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
  215. htmlDefaultSAXHandlerInit();
  216. if (ctxt->sax != NULL)
  217. memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
  218. return rxml_html_parser_context_wrap(ctxt);
  219. }
  220. /*
  221. * call-seq:
  222. * context.disable_cdata = (true|false)
  223. *
  224. * Control whether the CDATA nodes will be created in this context.
  225. */
  226. static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE bool)
  227. {
  228. htmlParserCtxtPtr ctxt;
  229. Data_Get_Struct(self, htmlParserCtxt, ctxt);
  230. if (ctxt->sax == NULL)
  231. rb_raise(rb_eRuntimeError, "Sax handler is not yet set");
  232. /* LibXML controls this internally with the default SAX handler. */
  233. if (bool)
  234. ctxt->sax->cdataBlock = NULL;
  235. else
  236. ctxt->sax->cdataBlock = htmlDefaultSAXHandler.cdataBlock;
  237. return bool;
  238. }
  239. /*
  240. * call-seq:
  241. * context.options = XML::Parser::Options::NOENT |
  242. XML::Parser::Options::NOCDATA
  243. *
  244. * Provides control over the execution of a parser. Valid values
  245. * are the constants defined on XML::Parser::Options. Multiple
  246. * options can be combined by using Bitwise OR (|).
  247. */
  248. static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
  249. {
  250. int result;
  251. htmlParserCtxtPtr ctxt;
  252. Check_Type(options, T_FIXNUM);
  253. Data_Get_Struct(self, htmlParserCtxt, ctxt);
  254. result = htmlCtxtUseOptions(ctxt, NUM2INT(options));
  255. return self;
  256. }
  257. void rxml_init_html_parser_context(void)
  258. {
  259. IO_ATTR = ID2SYM(rb_intern("@io"));
  260. cXMLHtmlParserContext = rb_define_class_under(cXMLHtmlParser, "Context", cXMLParserContext);
  261. rb_define_singleton_method(cXMLHtmlParserContext, "file", rxml_html_parser_context_file, 1);
  262. rb_define_singleton_method(cXMLHtmlParserContext, "io", rxml_html_parser_context_io, 1);
  263. rb_define_singleton_method(cXMLHtmlParserContext, "string", rxml_html_parser_context_string, 1);
  264. rb_define_method(cXMLHtmlParserContext, "disable_cdata=", rxml_html_parser_context_disable_cdata_set, 1);
  265. rb_define_method(cXMLHtmlParserContext, "options=", rxml_html_parser_context_options_set, 1);
  266. }