File: encoding_test.rb

package info (click to toggle)
ruby-sass 3.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,396 kB
  • sloc: ruby: 32,443; sh: 26; makefile: 25
file content (188 lines) | stat: -rwxr-xr-x 4,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/../test_helper'
require File.dirname(__FILE__) + '/test_helper'
require 'sass/util/test'

class EncodingTest < Minitest::Test
  include Sass::Util::Test

  def test_encoding_error
    render("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
    assert(false, "Expected exception")
  rescue Sass::SyntaxError => e
    assert_equal(3, e.sass_line)
    assert_equal('Invalid UTF-8 character "\xFE"', e.message)
  end

  def test_ascii_incompatible_encoding_error
    template = "foo\nbar\nb_z".encode("utf-16le")
    template[9] = "\xFE".force_encoding("utf-16le")
    render(template)
    assert(false, "Expected exception")
  rescue Sass::SyntaxError => e
    assert_equal(3, e.sass_line)
    assert_equal('Invalid UTF-16LE character "\xFE"', e.message)
  end

  def test_prefers_charset_to_ruby_encoding
    assert_renders_encoded(<<CSS, <<SASS.encode("IBM866").force_encoding("UTF-8"))
@charset "UTF-8";
fЖЖ {
  a: b; }
CSS
@charset "ibm866"
fЖЖ
  a: b
SASS
  end

  def test_uses_ruby_encoding_without_charset
    assert_renders_encoded(<<CSS, <<SASS.encode("IBM866"))
@charset "UTF-8";
тАЬ {
  a: b; }
CSS
тАЬ
  a: b
SASS
  end

  def test_multibyte_charset_without_bom_declared_as_binary
    engine = Sass::Engine.new(<<SASS.encode("UTF-16LE").force_encoding("BINARY"))
@charset "utf-16le"
fóó
  a: b
SASS
    # Since multibyte encodings' @charset declarations aren't
    # ASCII-compatible, we have to interpret the files as UTF-8 which will
    # inevitably fail.
    assert_raise_message(Sass::SyntaxError, "Invalid UTF-8 character \"\\xF3\"") {engine.render}
  end

  def test_multibyte_charset_without_bom_declared_as_utf_8
    engine = Sass::Engine.new(<<SASS.encode("UTF-16LE").force_encoding("UTF-8"))
@charset "utf-16le"
fóó
  a: b
SASS
    # Since multibyte encodings' @charset declarations aren't
    # ASCII-compatible, we have to interpret the files as UTF-8 which will
    # inevitably fail.
    assert_raise_message(Sass::SyntaxError, "Invalid UTF-8 character \"\\xF3\"") {engine.render}
  end

  def test_utf_16le_with_bom
    assert_renders_encoded(<<CSS, <<SASS.encode("UTF-16LE").force_encoding("BINARY"))
@charset "UTF-8";
fóó {
  a: b; }
CSS
\uFEFFfóó
  a: b
SASS
  end

  def test_utf_16be_with_bom
    assert_renders_encoded(<<CSS, <<SASS.encode("UTF-16BE").force_encoding("BINARY"))
@charset "UTF-8";
fóó {
  a: b; }
CSS
\uFEFFfóó
  a: b
SASS
  end

  def test_utf_8_with_bom
    assert_renders_encoded(<<CSS, <<SASS.force_encoding("BINARY"))
@charset "UTF-8";
fóó {
  a: b; }
CSS
\uFEFFfóó
  a: b
SASS
  end

  def test_charset_with_multibyte_encoding
    engine = Sass::Engine.new(<<SASS)
@charset "utf-32be"
fóó
  a: b
SASS
    # The charset declaration is just false here, so we should get an
    # encoding error.
    assert_raise_message(Sass::SyntaxError, "Invalid UTF-32BE character \"\\xC3\"") {engine.render}
  end

  def test_charset_with_special_case_encoding
    # For some reason, a file with an ASCII-compatible UTF-16 charset
    # declaration is specced to be parsed as UTF-8.
    assert_renders_encoded(<<CSS, <<SASS.force_encoding("BINARY"))
@charset "UTF-8";
fóó {
  a: b; }
CSS
@charset "utf-16"
fóó
  a: b
SASS
  end

  def test_compressed_output_uses_bom
    assert_equal("\uFEFFfóó{a:b}\n", render(<<SASS, :style => :compressed))
fóó
  a: b
SASS
  end

  def test_newline_normalization
    assert_equal("/* foo\nbar\nbaz\nbang\nqux */\n",
      render("/* foo\nbar\r\nbaz\fbang\rqux */", :syntax => :scss))
  end

  def test_null_normalization
    assert_equal(<<CSS, render("/* foo\x00bar\x00baz */", :syntax => :scss))
@charset "UTF-8";
/* foo�bar�baz */
CSS
  end

  # Regression

  def test_multibyte_prop_name
    assert_equal(<<CSS, render(<<SASS))
@charset "UTF-8";
#bar {
  cölor: blue; }
CSS
#bar
  cölor: blue
SASS
  end

  def test_multibyte_and_interpolation
    assert_equal(<<CSS, render(<<SCSS, :syntax => :scss))
#bar {
  background: a 0%; }
CSS
#bar {
  // 
  background: \#{a} 0%;
}
SCSS
  end

  private

  def assert_renders_encoded(css, sass)
    result = render(sass)
    assert_equal css.encoding, result.encoding
    assert_equal css, result
  end

  def render(sass, options = {})
    munge_filename options
    Sass::Engine.new(sass, options).render
  end
end