Skip to content

Commit

Permalink
Fix to not delete anything other than comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Jul 27, 2023
1 parent f127f72 commit ebeab75
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 14 deletions.
37 changes: 33 additions & 4 deletions lib/slim/embedded/minify/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@ def on_slim_embedded(engine, body, attrs)
minified_body = minify(body)
super(engine, minified_body, attrs)
end
def remove_comments!(line)
need_deletion = false
need_deletion_all = false
inside_char = nil
line[-1] = line.last.chars.each_with_index.map do |char, index|
next if need_deletion_all

private
if char == '/' && next_char(line, index) == '*' && inside_char.nil?
if remaining_string_range(line, index).match?(/\*\//)
need_deletion = true
next
end
elsif char == '/' && prev_char(line, index) == '*' && inside_char.nil? && need_deletion
need_deletion = false
next
elsif char == '/' && next_char(line, index) == '/' && inside_char.nil? && !need_deletion
need_deletion_all = true
next
elsif char == '"' && !need_deletion
if inside_char == '"'
inside_char = nil
next char
end

def remove_comments!(line)
line.last.gsub!(/((?<!['"])\/\*.*?\*\/(?<!['"]))/, '')
line.last.gsub!(/((?<!['"])\/\/.*)/, '')
inside_char = '"' if inside_char.nil?
elsif char == "'" && !need_deletion
if inside_char == "'"
inside_char = nil
next char
end

inside_char = "'" if inside_char.nil?
end
char unless need_deletion
end&.compact&.join
end
end
end
Expand Down
55 changes: 51 additions & 4 deletions lib/slim/embedded/minify/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,73 @@ def minify(body)
body.map do |line|
if line.instance_of?(Array) && line.first == :slim
remove_comments!(line)
remove_whitespace!(line)
next if line.last.nil?

remove_whitespace!(line)
stripped_quotes = stripped_quotes(line)
if stripped_quotes.match?(%r{/\*})
if stripped_quotes.match?(%r{/\*}) && !multiline_comment
multiline_comment = true
line.last.sub!(/(?<!['"])\/\*.*$/, '')
line[-1] = line.last.reverse.sub(/.*?\*\//, '').reverse
elsif multiline_comment
next unless stripped_quotes.match?(%r{\*/})

multiline_comment = false
line.last.sub!(/.*\*\/(?<!['"])/, '')
end
if stripped_quotes.match?(%r{/\*}) && !multiline_comment
multiline_comment = true
line[-1] = line.last.reverse.sub(/.*?\*\//, '').reverse
end
next if empty_line?(line)
end
line
end.compact
end

def minify_multiple_comments!(line)
end

def remove_comments!(line)
line.last.gsub!(/((?<!['"])\/\*.*?\*\/(?<!['"]))/, '')
need_deletion = 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).match?(/\*\//)
need_deletion = true
next
end
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
end

inside_char = '"' if inside_char.nil?
elsif char == "'" && !need_deletion
if inside_char == "'"
inside_char = nil
next char
end

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

def remaining_string_range(line, index)
line.last[index..-1]
end

def prev_char(line, index)
line.last[index - 1]
end

def next_char(line, index)
line.last[index + 1]
end

def remove_whitespace!(line)
Expand Down
12 changes: 7 additions & 5 deletions test/embedded/minify/javascript_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ def test_render_with_javascript_and_multiline_comment
comment */alert('hello')
/* comment */alert("argument /* argument")/* comment
comment */
p Hi
SLIM
assert_html <<~HTML.chomp, source
<script>
$(function() {})
alert('hello')</script><p>Hi</p>
alert('hello')
alert("argument /* argument")</script><p>Hi</p>
HTML
end

Expand All @@ -63,18 +66,17 @@ def test_render_with_javascript_and_singleline_comment
$(function() {});
/* ... * comment / */
/* comment */"/* string */"/* comment */
/* ... * comment / */alert("/* argument */")/*... * comment /*/
/* comment */alert("/* argument */")/*comment*/
/* comment */alert("/* argument")/* comment
comment */
p Hi
SLIM
assert_html <<~HTML.chomp, source
<script>
$(function() {});
"/* string */"
alert("/* argument */")
alert("/* argument */")
alert("/* argument")</script><p>Hi</p>
alert("/* argument */")</script><p>Hi</p>
HTML
end

Expand Down
3 changes: 2 additions & 1 deletion test/embedded/minify/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def test_render_with_css_and_multiple_comment
*/
h1 {
color: blue;/* multiline
comment */ font-size: 12px;
comment */ font-size: 12px;/* comment
after */
}
SLIM
assert_html <<~HTML.chomp, source
Expand Down

0 comments on commit ebeab75

Please sign in to comment.