Commit eb2ee827 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Fix incompatible encodings error in RKellyAdapter.

Convert latin1 escape sequences into UTF-8.  By default they resulted
in ASCII-8BIT (binary) encoding, which wasn't compatible with UTF-8,
resulting in error.

Added wrapper encoding method to cope with Ruby 1.8 which has no
builtin encodings support.

The tests file is now also a pure ASCII file, so no funky encoding-stuff
happens there.

Fixes #491
parent e48d7c92
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -481,9 +481,9 @@ module JsDuck
          if STRING_ESCAPES[s]
            STRING_ESCAPES[s]
          elsif s =~ /^\\[0-9]/
            s[1..-1].oct.chr
            nr_to_str(s[1..-1].oct, s)
          elsif s =~ /^\\x[0-9A-F]/
            s[2..-1].hex.chr
            nr_to_str(s[2..-1].hex, s)
          elsif s =~ /^\\u[0-9A-F]/
            [s[2..-1].hex].pack("U")
          else
@@ -492,6 +492,20 @@ module JsDuck
        end
      end

      # Converts a latin1 character code to UTF-8 string.
      # When running in Ruby <= 1.8, only converts ASCII chars,
      # others are left as escape sequences.
      def nr_to_str(nr, original)
        str = nr.chr
        if str.respond_to?(:encode)
          str.encode('UTF-8', 'ISO-8859-1')
        elsif nr < 127
          str
        else
          original
        end
      end

      STRING_ESCAPES = {
        '\b' => "\b",
        '\f' => "\f",
+18 −3
Original line number Diff line number Diff line
# encoding: ASCII-8BIT
# encoding: ASCII
require "rkelly"
require "jsduck/js/rkelly_adapter"

@@ -40,6 +40,17 @@ describe JsDuck::Js::RKellyAdapter do
  end

  describe "values of strings" do
    def nr_to_str(nr, original)
      str = nr.chr
      if str.respond_to?(:encode)
        str.encode('UTF-8', 'ISO-8859-1')
      elsif nr < 127
        str
      else
        original
      end
    end

    it "single-quoted" do
      adapt_value("'foo'").should == 'foo'
    end
@@ -57,17 +68,21 @@ describe JsDuck::Js::RKellyAdapter do
    end

    it "with latin1 octal escape" do
      adapt_value('"\101 \251"').should == "A \251"
      adapt_value('"\101 \251"').should == "A " + nr_to_str(0251, '\251')
    end

    it "with latin1 hex escape" do
      adapt_value('"\x41 \xA9"').should == "A \xA9"
      adapt_value('"\x41 \xA9"').should == "A " + nr_to_str(0xA9, '\xA9')
    end

    it "with unicode escape" do
      adapt_value('"\u00A9"').should == [0x00A9].pack("U")
    end

    it "with multiple escapes together" do
      adapt_value('"\xA0\u1680"').should == nr_to_str(0xA0, '\xA0') + [0x1680].pack("U")
    end

    it "with Ruby-like variable interpolation" do
      adapt_value('"#{foo}"').should == '#{foo}'
    end