Skip to content

Commit

Permalink
Fix incorrect removing when using escaped quotation for css
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Aug 1, 2023
1 parent 8c25d59 commit 1453687
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
28 changes: 17 additions & 11 deletions lib/slim/embedded/minify/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ def minify(body)

def remove_comments!(line)
need_deletion = false
escaped = false
escaped_backslash = false
inside_char = nil
line[-1] = line.last.chars.each_with_index.map do |char, index|

if char == "/" && next_char(line, index) == "*" && inside_char.nil?
if remaining_string_range(line, index).include?("*/")
need_deletion = true
Expand All @@ -51,22 +54,25 @@ def remove_comments!(line)
elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
need_deletion = false
next
elsif char == '"' && !need_deletion
if inside_char == '"'
inside_char = nil
next char
elsif char == "\\" && next_char(line, index) == "\\" && inside_char
escaped_backslash = true
next char
elsif char == "\\"
if ["'", '"'].include?(next_char(line, index)) && inside_char == next_char(line, index) && !escaped_backslash
escaped = true
end

inside_char = '"' if inside_char.nil?
elsif char == "'" && !need_deletion
if inside_char == "'"
inside_char = nil
escaped_backslash = false
next char
elsif ["'", '"'].include?(char) && !need_deletion
if inside_char == char
inside_char = nil unless escaped
escaped = false
next char
end

inside_char = "'" if inside_char.nil?
inside_char = char if inside_char.nil?
end
char unless need_deletion
char if !need_deletion || inside_char
end&.compact&.join
end

Expand Down
30 changes: 27 additions & 3 deletions test/embedded/minify/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,45 @@ def test_render_with_css_multiple_attributes
end

def test_render_with_css_and_comment
source = <<~SLIM
source = <<~'SLIM'
css:
/* comment */
h1 {
/* comment * / */font-family: "/*foo*/", '/*bar*/';/** /comment */
color: blue;
/* comment */
}
h2 {/* comment */font-family: "\\"/* comment */}/* comment */
SLIM
assert_html <<~HTML.chomp, source
assert_html <<~'HTML'.chomp, source
<style>
h1 {
font-family: "/*foo*/", '/*bar*/';
color: blue;
}</style>
}
h2 {font-family: "\\"}</style>
HTML
end

def test_render_with_css_and_string_single_quotes
source = <<~'SLIM'
css:
h1 { font-size: '\'/* string */\'' }
SLIM

assert_html <<~'HTML'.chomp, source
<style>h1 { font-size: '\'/* string */\'' }</style>
HTML
end

def test_render_with_css_and_string_double_quotes
source = <<~'SLIM'
css:
h1 { font-size: "\"/* string */\"" }
SLIM

assert_html <<~'HTML'.chomp, source
<style>h1 { font-size: "\"/* string */\"" }</style>
HTML
end

Expand Down

0 comments on commit 1453687

Please sign in to comment.