Skip to content

Commit

Permalink
💅
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Jul 27, 2023
1 parent ebeab75 commit e880805
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
9 changes: 5 additions & 4 deletions lib/slim/embedded/minify/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ 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

if char == '/' && next_char(line, index) == '*' && inside_char.nil?
if remaining_string_range(line, index).match?(/\*\//)
if char == "/" && next_char(line, index) == "*" && inside_char.nil?
if remaining_string_range(line, index).include?("*/")
need_deletion = true
next
end
elsif char == '/' && prev_char(line, index) == '*' && inside_char.nil? && need_deletion
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
elsif char == "/" && next_char(line, index) == "/" && inside_char.nil? && !need_deletion
need_deletion_all = true
next
elsif char == '"' && !need_deletion
Expand Down
37 changes: 17 additions & 20 deletions lib/slim/embedded/minify/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,42 @@ def on_slim_embedded(engine, body, attrs)

def minify(body)
multiline_comment = false
body.map do |line|
body.filter_map do |line|
if line.instance_of?(Array) && line.first == :slim
remove_comments!(line)
next if line.last.nil?

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

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

def minify_multiple_comments!(line)
end
end

def remove_comments!(line)
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?(/\*\//)
if char == "/" && next_char(line, index) == "*" && inside_char.nil?
if remaining_string_range(line, index).include?("*/")
need_deletion = true
next
end
elsif char == '/' && prev_char(line, index) == '*' && inside_char.nil? && need_deletion
elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
need_deletion = false
next
elsif char == '"' && !need_deletion
Expand All @@ -74,7 +71,7 @@ def remove_comments!(line)
end

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

def prev_char(line, index)
Expand All @@ -86,17 +83,17 @@ def next_char(line, index)
end

def remove_whitespace!(line)
if line.last.gsub(/\n/, '').match?(/^\s*$/)
line.last.gsub!(/^\s*$/, '')
end
return unless line.last.delete("\n").match?(/^\s*$/)

line.last.gsub!(/^\s*$/, "")
end

def stripped_quotes(line)
line.last.gsub(/(['"]).*?\1/, '')
line.last.gsub(/(['"]).*?\1/, "")
end

def empty_line?(line)
line.last.gsub(/[\n\s]/, '').empty?
line.last.gsub(/[\n\s]/, "").empty?
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion slim-embedded-minify.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_runtime_dependency 'slim', '~> 5.1'
spec.add_runtime_dependency "slim", "~> 5.1"
spec.metadata["rubygems_mfa_required"] = "true"
end
4 changes: 2 additions & 2 deletions test/embedded/minify/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def test_render_with_css_attribute
css scoped = "true":
h1 { color: blue }
SLIM
assert_html %{<style scoped="true">h1 { color: blue }</style>}, source
assert_html %(<style scoped="true">h1 { color: blue }</style>), source
end

def test_render_with_css_multiple_attributes
source = <<~SLIM
css class="myClass" scoped = "true" :
h1 { color: blue }
SLIM
assert_html %{<style class="myClass" scoped="true">h1 { color: blue }</style>}, source
assert_html %(<style class="myClass" scoped="true">h1 { color: blue }</style>), source
end

def test_render_with_css_and_comment
Expand Down

0 comments on commit e880805

Please sign in to comment.