Skip to content

Instantly share code, notes, and snippets.

@libc
Created February 5, 2009 23:10
Show Gist options
  • Save libc/59086 to your computer and use it in GitHub Desktop.
Save libc/59086 to your computer and use it in GitHub Desktop.
diff --git a/ext/hpricot_scan/hpricot_scan.rl b/ext/hpricot_scan/hpricot_scan.rl
index d9ea15c..60b62b3 100644
--- a/ext/hpricot_scan/hpricot_scan.rl
+++ b/ext/hpricot_scan/hpricot_scan.rl
@@ -24,7 +24,7 @@ static VALUE mHpricot, rb_eHpricotParseError;
static VALUE cBaseEle, cBogusETag, cCData, cComment, cDoc, cDocType, cElem, cETag, cText,
cXMLDecl, cProcIns, symAllow, symDeny;
static ID s_ElementContent;
-static ID s_downcase, s_new, s_parent, s_read, s_to_str;
+static ID s_downcase, s_new, s_parent, s_read, s_to_str, s_force_encoding, s_set_encoding;
static ID iv_parent;
static VALUE reProcInsParse;
@@ -451,6 +451,10 @@ VALUE hpricot_scan(int argc, VALUE *argv, VALUE self)
if ( rb_respond_to( port, s_to_str ) )
{
port = rb_funcall( port, s_to_str, 0 );
+ if(rb_respond_to(port, s_force_encoding))
+ {
+ rb_funcall(port, s_force_encoding, 1, rb_str_new2("ascii-8bit"));
+ }
StringValue(port);
}
else
@@ -458,6 +462,14 @@ VALUE hpricot_scan(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eArgError, "an Hpricot document must be built from an input source (a String or IO object.)");
}
}
+ else
+ {
+ if(rb_respond_to(port, s_set_encoding))
+ {
+ port = rb_funcall(port, s_set_encoding, 1, rb_str_new2("ascii-8bit"));
+ }
+ }
+
if (TYPE(opts) != T_HASH)
opts = Qnil;
@@ -679,6 +691,8 @@ void Init_hpricot_scan()
s_parent = rb_intern("parent");
s_read = rb_intern("read");
s_to_str = rb_intern("to_str");
+ s_force_encoding = rb_intern("force_encoding");
+ s_set_encoding = rb_intern("set_encoding");
iv_parent = rb_intern("parent");
sym_xmldecl = ID2SYM(rb_intern("xmldecl"));
sym_doctype = ID2SYM(rb_intern("doctype"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment