Skip to content

Instantly share code, notes, and snippets.

@libc
Created January 28, 2010 09:09
Show Gist options
  • Select an option

  • Save libc/288568 to your computer and use it in GitHub Desktop.

Select an option

Save libc/288568 to your computer and use it in GitHub Desktop.
diff --git a/ext/nokogiri/html_sax_parser_context.c b/ext/nokogiri/html_sax_parser_context.c
index be1ea23..f09b901 100644
--- a/ext/nokogiri/html_sax_parser_context.c
+++ b/ext/nokogiri/html_sax_parser_context.c
@@ -25,9 +25,9 @@ static VALUE parse_memory(VALUE klass, VALUE data, VALUE encoding)
);
if(RTEST(encoding)) {
- xmlCharEncoding enc = xmlParseCharEncoding(StringValuePtr(encoding));
- if(enc != XML_CHAR_ENCODING_ERROR) {
- xmlSwitchEncoding(ctxt, enc);
+ xmlCharEncodingHandlerPtr enc = xmlFindCharEncodingHandler(StringValuePtr(encoding));
+ if(enc != NULL) {
+ xmlSwitchToEncoding(ctxt, enc);
if(ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
rb_raise(rb_eRuntimeError, "Unsupported encoding %s",
StringValuePtr(encoding));
diff --git a/lib/nokogiri/ffi/html/sax/parser_context.rb b/lib/nokogiri/ffi/html/sax/parser_context.rb
index d157b2c..c9e4124 100644
--- a/lib/nokogiri/ffi/html/sax/parser_context.rb
+++ b/lib/nokogiri/ffi/html/sax/parser_context.rb
@@ -20,9 +20,9 @@ module Nokogiri
pc = allocate
pc.cstruct = LibXML::XmlParserContext.new ctx
if encoding
- enc = LibXML.xmlParseCharEncoding(encoding)
- if enc != LibXML::XML_CHAR_ENCODING_ERROR
- LibXML.xmlSwitchEncoding(ctx, enc)
+ enc = LibXML.xmlFindCharEncodingHandler(encoding)
+ if !enc.null?
+ LibXML.xmlSwitchToEncoding(ctx, enc)
end
end
pc
diff --git a/lib/nokogiri/ffi/libxml.rb b/lib/nokogiri/ffi/libxml.rb
index cae94bb..19351ef 100644
--- a/lib/nokogiri/ffi/libxml.rb
+++ b/lib/nokogiri/ffi/libxml.rb
@@ -294,7 +294,7 @@ module Nokogiri
attach_function :free, [:pointer], :void
attach_function :xmlParseCharEncoding, [:string], :int
- attach_function :xmlSwitchEncoding, [:pointer, :int], :void
+ attach_function :xmlSwitchToEncoding, [:pointer, :pointer], :void
# helpers
POINTER_SIZE = FFI.type_size(:pointer)
diff --git a/test/html/sax/test_parser.rb b/test/html/sax/test_parser.rb
index 8aa7ff9..93cf616 100644
--- a/test/html/sax/test_parser.rb
+++ b/test/html/sax/test_parser.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require "helper"
module Nokogiri
@@ -50,6 +51,15 @@ module Nokogiri
end
end
+ def test_parse_force_encoding
+ @parser.parse_memory(<<-HTML, 'UTF-8')
+ <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
+ Информация
+ HTML
+ assert_equal("Информация",
+ @parser.document.data.join.strip)
+ end
+
def test_parse_document
@parser.parse_memory(<<-eoxml)
<p>Paragraph 1</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment