slim-embedded-minify 0.2.4 → 0.2.6

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: f18ed0ba3778ee7349be7c14d9d3974077bff29611ecd20eddab983394b38eb3
4
- data.tar.gz: 9956d7da18d3074e0b9209491db3a057c85fb015823b231d7db11a7c1bbb1c22
3
+ metadata.gz: a0acd763c9c3c2869fee898e2da36f1a26aa1aa2430d9cfe9c3008db35ecd442
4
+ data.tar.gz: b3d30f07bbf45aa062deac825d4e849958f495fc4cae6cb819362ebb32f8c53b
5
5
  SHA512:
6
- metadata.gz: e9da11747c1bbe17719d9e7844d5f846a0eb28182c64f285dddf6f61fee5330cac386a33c490397ff770c205b38e7895e59961070f9b3110617c9321c539a193
7
- data.tar.gz: c0b7c531155113b1d118212976dbd60bdd6ed887bca10939ab18c5f881ee98a599cfe329f3bdd6adda8c3a1161403b7cce7cd69a5972de92a44e859b89885cf8
6
+ metadata.gz: f7e085fd30ce3b514709e55e8999c919e2ab4f25fbdcc7c3b62617a8ee09e890d732af7d6c03064eda833b002fbc5b2cec9cccf3ef5afe7ab93a7c843b708ad5
7
+ data.tar.gz: 26466135a7c571e04eb572f49906f8f17d3ccc21ae1be8ca44e9a00c1da1ae591cb7583d16cbada24d34e0f324a7bf1dd32afc7b4248ac82afed387527fffd58
data/CHANGELOG.md CHANGED
@@ -1,8 +1,16 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.2.6 - 2023-08-01
4
+
5
+ - Fix incorrect removing when using escaped quotation.
6
+
7
+ ## 0.2.5 - 2023-07-27
8
+
9
+ - Fix to not delete anything other than comments.
10
+
3
11
  ## 0.2.4 - 2023-07-27
4
12
 
5
- - Fix deletion of strings when `/*` is present in the string
13
+ - Fix deletion of strings when `/*` is present in the string.
6
14
 
7
15
  ## 0.2.3 - 2023-07-27
8
16
 
@@ -11,11 +11,46 @@ 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
+ escaped = false
18
+ escaped_backslash = false
19
+ inside_char = nil
20
+ line[-1] = line.last.chars.each_with_index.map do |char, index|
21
+ next if need_deletion_all
22
+
23
+ if char == "/" && next_char(line, index) == "*" && inside_char.nil?
24
+ if remaining_string_range(line, index).include?("*/")
25
+ need_deletion = true
26
+ next
27
+ end
28
+ elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
29
+ need_deletion = false
30
+ next
31
+ elsif char == "/" && next_char(line, index) == "/" && inside_char.nil? && !need_deletion
32
+ need_deletion_all = true
33
+ next
34
+ elsif char == "\\" && next_char(line, index) == "\\" && inside_char
35
+ escaped_backslash = true
36
+ next char
37
+ elsif char == "\\"
38
+ if ["'", '"'].include?(next_char(line, index)) && inside_char == next_char(line, index) && !escaped_backslash
39
+ escaped = true
40
+ end
41
+ escaped_backslash = false
42
+ next char
43
+ elsif ["'", '"'].include?(char) && !need_deletion
44
+ if inside_char == char
45
+ inside_char = nil unless escaped
46
+ escaped = false
47
+ next char
48
+ end
49
+
50
+ inside_char = char if inside_char.nil?
51
+ end
52
+ char if !need_deletion || inside_char
53
+ end&.compact&.join
19
54
  end
20
55
  end
21
56
  end
@@ -13,43 +13,113 @@ 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.sub!(/(?<!['"])\/\*.*$/, '')
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.sub!(/.*\*\/(?<!['"])/, '')
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] if index.positive?
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
+ inside_char = nil
93
+ escaped = false
94
+ escaped_backslash = false
95
+ line.last.chars.each_with_index.filter_map do |char, index|
96
+ if ["'", '"'].include?(char) && inside_char.nil?
97
+ inside_char = char
98
+ next
99
+ elsif char == "\\" && next_char(line, index) == "\\" && inside_char
100
+ escaped_backslash = true
101
+ next
102
+ elsif char == "\\"
103
+ if ["'",
104
+ '"'].include?(next_char(line, index)) && inside_char == next_char(line, index) && !escaped_backslash
105
+ escaped = true
106
+ end
107
+ escaped_backslash = false
108
+ next
109
+ elsif char == inside_char && !inside_char.nil?
110
+ inside_char = nil unless escaped
111
+ escaped = false
112
+ next
113
+ elsif inside_char
114
+ next
115
+ else
116
+ char
117
+ end
118
+ end.join
49
119
  end
50
120
 
51
121
  def empty_line?(line)
52
- line.last.gsub(/[\n\s]/, '').empty?
122
+ line.last.gsub(/[\n\s]/, "").empty?
53
123
  end
54
124
  end
55
125
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SlimEmbeddedMinify
4
- VERSION = "0.2.4"
4
+ VERSION = "0.2.6"
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.4
4
+ version: 0.2.6
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-27 00:00:00.000000000 Z
11
+ date: 2023-08-01 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: