slim-embedded-minify 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae5665cb9baa381cc6a7f9abef491e6bc9805c1d7a77af47428a16dd4cd1ce07
4
- data.tar.gz: 7a38354fdb3dacb1ea4c034cce7129f3aa7c66e22d0c7326e685c9cc84d06f9a
3
+ metadata.gz: 14375e81992194bca2bc3c42733011ccb783db89cf89a3dffbb62ec3245f2184
4
+ data.tar.gz: 824370e31b97a05335c8d4c4a64c1bed55ea02ea9a678faa8609a4308c1657b6
5
5
  SHA512:
6
- metadata.gz: a46495c0cf4e673ecf61693e04246d7bea875b2ff6dadddea29b68d94ed4e34a8417cc506243fdf81d9ce718e73b09fd12403edef342e2f4598c72306db93116
7
- data.tar.gz: ea8111ae9eb161bfc10f5b5594cd1c207cc00b50a7c06f823b4b9ca6aabda4dd01ee6d3b68cad86d47fe11b02151a33b12942f55897a9ccacc29eb84d893aa80
6
+ metadata.gz: 947549a68e10777ab21e2779ac67491a7be49cdb54e800f08e8b0fa891138ee72dc249f3216bbe9ac8f2b9f1e3574f1592a156c2ef90fa3c751771b1237e317b
7
+ data.tar.gz: df9d4962e7ba6e74492306c5367ff7c44bb6c5a9527b77dd19514bd2104da0a0de0020cd0bac68e58ccdbe33de7cfe48bb3819eef2cb4c4fb2391a38540e69e4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.2.5 - 2023-07-27
4
+
5
+ Fix to not delete anything other than comments.
6
+
7
+ ## 0.2.4 - 2023-07-27
8
+
9
+ - Fix deletion of strings when `/*` is present in the string.
10
+
3
11
  ## 0.2.3 - 2023-07-27
4
12
 
5
13
  - Fix to not removing line when a block comment if it starts at the end of a line.
@@ -11,11 +11,41 @@ module Slim
11
11
  super(engine, minified_body, attrs)
12
12
  end
13
13
 
14
- private
15
-
16
14
  def remove_comments!(line)
17
- line.last.gsub!(/((?<!['"])\/\*.*?\*\/(?<!['"]))/, '')
18
- line.last.gsub!(/((?<!['"])\/\/.*)/, '')
15
+ need_deletion = false
16
+ need_deletion_all = false
17
+ inside_char = nil
18
+ line[-1] = line.last.chars.each_with_index.map do |char, index|
19
+ next if need_deletion_all
20
+
21
+ if char == "/" && next_char(line, index) == "*" && inside_char.nil?
22
+ if remaining_string_range(line, index).include?("*/")
23
+ need_deletion = true
24
+ next
25
+ end
26
+ elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
27
+ need_deletion = false
28
+ next
29
+ elsif char == "/" && next_char(line, index) == "/" && inside_char.nil? && !need_deletion
30
+ need_deletion_all = true
31
+ next
32
+ elsif char == '"' && !need_deletion
33
+ if inside_char == '"'
34
+ inside_char = nil
35
+ next char
36
+ end
37
+
38
+ inside_char = '"' if inside_char.nil?
39
+ elsif char == "'" && !need_deletion
40
+ if inside_char == "'"
41
+ inside_char = nil
42
+ next char
43
+ end
44
+
45
+ inside_char = "'" if inside_char.nil?
46
+ end
47
+ char unless need_deletion
48
+ end&.compact&.join
19
49
  end
20
50
  end
21
51
  end
@@ -13,43 +13,87 @@ module Slim
13
13
 
14
14
  def minify(body)
15
15
  multiline_comment = false
16
- body.map do |line|
16
+ body.filter_map do |line|
17
17
  if line.instance_of?(Array) && line.first == :slim
18
18
  remove_comments!(line)
19
- remove_whitespace!(line)
19
+ next if line.last.nil?
20
20
 
21
+ remove_whitespace!(line)
21
22
  stripped_quotes = stripped_quotes(line)
22
- if stripped_quotes.match?(%r{/\*})
23
+ if stripped_quotes.include?("/*") && !multiline_comment
23
24
  multiline_comment = true
24
- line.last.gsub!(/\/\*.*$/, '')
25
+ line[-1] = line.last.reverse.sub(%r{.*?\*/}, "").reverse
25
26
  elsif multiline_comment
26
- next unless stripped_quotes.match?(%r{\*/})
27
+ next unless stripped_quotes.include?("*/")
27
28
 
28
29
  multiline_comment = false
29
- line.last.gsub!(/.*\*\//, '')
30
+ line.last.sub!(%r{.*\*/(?<!['"])}, "")
31
+ end
32
+ if stripped_quotes.include?("/*") && !multiline_comment
33
+ multiline_comment = true
34
+ line[-1] = line.last.reverse.sub(%r{.*?\*/}, "").reverse
30
35
  end
31
36
  next if empty_line?(line)
32
37
  end
33
38
  line
34
- end.compact
39
+ end
35
40
  end
36
41
 
37
42
  def remove_comments!(line)
38
- line.last.gsub!(/((?<!['"])\/\*.*?\*\/(?<!['"]))/, '')
43
+ need_deletion = false
44
+ inside_char = nil
45
+ line[-1] = line.last.chars.each_with_index.map do |char, index|
46
+ if char == "/" && next_char(line, index) == "*" && inside_char.nil?
47
+ if remaining_string_range(line, index).include?("*/")
48
+ need_deletion = true
49
+ next
50
+ end
51
+ elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
52
+ need_deletion = false
53
+ next
54
+ elsif char == '"' && !need_deletion
55
+ if inside_char == '"'
56
+ inside_char = nil
57
+ next char
58
+ end
59
+
60
+ inside_char = '"' if inside_char.nil?
61
+ elsif char == "'" && !need_deletion
62
+ if inside_char == "'"
63
+ inside_char = nil
64
+ next char
65
+ end
66
+
67
+ inside_char = "'" if inside_char.nil?
68
+ end
69
+ char unless need_deletion
70
+ end&.compact&.join
71
+ end
72
+
73
+ def remaining_string_range(line, index)
74
+ line.last[index..]
75
+ end
76
+
77
+ def prev_char(line, index)
78
+ line.last[index - 1]
79
+ end
80
+
81
+ def next_char(line, index)
82
+ line.last[index + 1]
39
83
  end
40
84
 
41
85
  def remove_whitespace!(line)
42
- if line.last.gsub(/\n/, '').match?(/^\s*$/)
43
- line.last.gsub!(/^\s*$/, '')
44
- end
86
+ return unless line.last.delete("\n").match?(/^\s*$/)
87
+
88
+ line.last.gsub!(/^\s*$/, "")
45
89
  end
46
90
 
47
91
  def stripped_quotes(line)
48
- line.last.gsub(/(['"]).*?\1/, '')
92
+ line.last.gsub(/(['"]).*?\1/, "")
49
93
  end
50
94
 
51
95
  def empty_line?(line)
52
- line.last.gsub(/[\n\s]/, '').empty?
96
+ line.last.gsub(/[\n\s]/, "").empty?
53
97
  end
54
98
  end
55
99
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SlimEmbeddedMinify
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.5"
5
5
  end
@@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.bindir = "exe"
28
28
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
- spec.add_runtime_dependency 'slim', '~> 5.1'
30
+ spec.add_runtime_dependency "slim", "~> 5.1"
31
+ spec.metadata["rubygems_mfa_required"] = "true"
31
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim-embedded-minify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yudai Takada
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-26 00:00:00.000000000 Z
11
+ date: 2023-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slim
@@ -49,6 +49,7 @@ metadata:
49
49
  homepage_uri: https://github.com/ydah/slim-embedded-minify
50
50
  source_code_uri: https://github.com/ydah/slim-embedded-minify
51
51
  changelog_uri: https://github.com/ydah/slim-embedded-minify/releases
52
+ rubygems_mfa_required: 'true'
52
53
  post_install_message:
53
54
  rdoc_options: []
54
55
  require_paths: