jsmin 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/HISTORY +5 -1
  2. data/lib/jsmin.rb +71 -55
  3. metadata +3 -3
data/HISTORY CHANGED
@@ -1,5 +1,9 @@
1
- CSSMin History
1
+ JSMin History
2
2
  ================================================================================
3
3
 
4
+ Version 1.0.1 (2008-11-10)
5
+ * Ruby 1.9 compatibility.
6
+ * Minor performance improvements.
7
+
4
8
  Version 1.0.0 (2008-03-22)
5
9
  * First release.
@@ -5,19 +5,19 @@
5
5
  # as follows:
6
6
  #
7
7
  # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
8
- #
8
+ #
9
9
  # Permission is hereby granted, free of charge, to any person obtaining a copy
10
10
  # of this software and associated documentation files (the "Software"), to deal
11
11
  # in the Software without restriction, including without limitation the rights
12
12
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
13
  # copies of the Software, and to permit persons to whom the Software is
14
14
  # furnished to do so, subject to the following conditions:
15
- #
15
+ #
16
16
  # The above copyright notice and this permission notice shall be included in all
17
17
  # copies or substantial portions of the Software.
18
- #
18
+ #
19
19
  # The Software shall be used for Good, not Evil.
20
- #
20
+ #
21
21
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
22
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
23
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -34,7 +34,7 @@ require 'strscan'
34
34
  # Ruby implementation of Douglas Crockford's JavaScript minifier, JSMin.
35
35
  #
36
36
  # Author:: Ryan Grove (mailto:ryan@wonko.com)
37
- # Version:: 1.0.0 (2008-03-22)
37
+ # Version:: 1.0.1 (2008-11-10)
38
38
  # Copyright:: Copyright (c) 2008 Ryan Grove. All rights reserved.
39
39
  # Website:: http://github.com/rgrove/jsmin/
40
40
  #
@@ -46,11 +46,27 @@ require 'strscan'
46
46
  # File.open('example.js', 'r') {|file| puts JSMin.minify(file) }
47
47
  #
48
48
  module JSMin
49
- ORD_LF = "\n"[0].freeze
50
- ORD_SPACE = ' '[0].freeze
51
-
49
+ CHR_APOS = "'".freeze
50
+ CHR_ASTERISK = '*'.freeze
51
+ CHR_BACKSLASH = '\\'.freeze
52
+ CHR_CR = "\r".freeze
53
+ CHR_FRONTSLASH = '/'.freeze
54
+ CHR_LF = "\n".freeze
55
+ CHR_QUOTE = '"'.freeze
56
+ CHR_SPACE = ' '.freeze
57
+
58
+ if RUBY_VERSION >= '1.9'
59
+ ORD_LF = "\n".freeze
60
+ ORD_SPACE = ' '.freeze
61
+ ORD_TILDE = '~'.freeze
62
+ else
63
+ ORD_LF = "\n"[0].freeze
64
+ ORD_SPACE = ' '[0].freeze
65
+ ORD_TILDE = '~'[0].freeze
66
+ end
67
+
52
68
  class << self
53
-
69
+
54
70
  # Reads JavaScript from +input+ (which can be a String or an IO object) and
55
71
  # returns a String containing minified JS.
56
72
  def minify(input)
@@ -60,20 +76,20 @@ module JSMin
60
76
  @b = nil
61
77
  @lookahead = nil
62
78
  @output = ''
63
-
79
+
64
80
  action_get
65
-
81
+
66
82
  while !@a.nil? do
67
83
  case @a
68
- when ' '
84
+ when CHR_SPACE
69
85
  if alphanum?(@b)
70
86
  action_output
71
87
  else
72
88
  action_copy
73
89
  end
74
-
75
- when "\n"
76
- if @b == ' '
90
+
91
+ when CHR_LF
92
+ if @b == CHR_SPACE
77
93
  action_get
78
94
  elsif @b =~ /[{\[\(+-]/
79
95
  action_output
@@ -84,15 +100,15 @@ module JSMin
84
100
  action_copy
85
101
  end
86
102
  end
87
-
103
+
88
104
  else
89
- if @b == ' '
105
+ if @b == CHR_SPACE
90
106
  if alphanum?(@a)
91
107
  action_output
92
108
  else
93
109
  action_get
94
110
  end
95
- elsif @b == "\n"
111
+ elsif @b == CHR_LF
96
112
  if @a =~ /[}\]\)\\"+-]/
97
113
  action_output
98
114
  else
@@ -107,124 +123,124 @@ module JSMin
107
123
  end
108
124
  end
109
125
  end
110
-
126
+
111
127
  @output
112
128
  end
113
-
129
+
114
130
  private
115
-
131
+
116
132
  # Corresponds to action(1) in jsmin.c.
117
133
  def action_output
118
134
  @output << @a
119
135
  action_copy
120
136
  end
121
-
137
+
122
138
  # Corresponds to action(2) in jsmin.c.
123
139
  def action_copy
124
140
  @a = @b
125
-
126
- if @a == '\'' || @a == '"'
141
+
142
+ if @a == CHR_APOS || @a == CHR_QUOTE
127
143
  loop do
128
144
  @output << @a
129
145
  @a = get
130
-
146
+
131
147
  break if @a == @b
132
-
148
+
133
149
  if @a[0] <= ORD_LF
134
150
  raise "JSMin parse error: unterminated string literal: #{@a}"
135
151
  end
136
-
137
- if @a == '\\'
152
+
153
+ if @a == CHR_BACKSLASH
138
154
  @output << @a
139
155
  @a = get
140
-
156
+
141
157
  if @a[0] <= ORD_LF
142
158
  raise "JSMin parse error: unterminated string literal: #{@a}"
143
159
  end
144
160
  end
145
161
  end
146
162
  end
147
-
163
+
148
164
  action_get
149
165
  end
150
-
166
+
151
167
  # Corresponds to action(3) in jsmin.c.
152
168
  def action_get
153
169
  @b = nextchar
154
-
155
- if @b == '/' && (@a == "\n" || @a =~ /[\(,=:\[!&|?{};]/)
170
+
171
+ if @b == CHR_FRONTSLASH && (@a == CHR_LF || @a =~ /[\(,=:\[!&|?{};]/)
156
172
  @output << @a
157
173
  @output << @b
158
-
174
+
159
175
  loop do
160
176
  @a = get
161
-
162
- if @a == '/'
177
+
178
+ if @a == CHR_FRONTSLASH
163
179
  break
164
- elsif @a == '\\'
180
+ elsif @a == CHR_BACKSLASH
165
181
  @output << @a
166
182
  @a = get
167
183
  elsif @a[0] <= ORD_LF
168
184
  raise "JSMin parse error: unterminated regular expression " +
169
185
  "literal: #{@a}"
170
186
  end
171
-
187
+
172
188
  @output << @a
173
189
  end
174
-
190
+
175
191
  @b = nextchar
176
192
  end
177
193
  end
178
-
194
+
179
195
  # Returns true if +c+ is a letter, digit, underscore, dollar sign,
180
196
  # backslash, or non-ASCII character.
181
197
  def alphanum?(c)
182
- c.is_a?(String) && !c.empty? && (c[0] > 126 || c =~ /[0-9a-z_$\\]/i)
198
+ c.is_a?(String) && !c.empty? && (c[0] > ORD_TILDE || c =~ /[0-9a-z_$\\]/i)
183
199
  end
184
-
200
+
185
201
  # Returns the next character from the input. If the character is a control
186
202
  # character, it will be translated to a space or linefeed.
187
203
  def get
188
204
  c = @lookahead.nil? ? @js.getch : @lookahead
189
205
  @lookahead = nil
190
206
 
191
- return c if c.nil? || c == "\n" || c[0] >= ORD_SPACE
192
- return "\n" if c == "\r"
207
+ return c if c.nil? || c == CHR_LF || c[0] >= ORD_SPACE
208
+ return "\n" if c == CHR_CR
193
209
  return ' '
194
210
  end
195
-
211
+
196
212
  # Gets the next character, excluding comments.
197
213
  def nextchar
198
214
  c = get
199
- return c unless c == '/'
200
-
215
+ return c unless c == CHR_FRONTSLASH
216
+
201
217
  case peek
202
- when '/'
218
+ when CHR_FRONTSLASH
203
219
  loop do
204
220
  c = get
205
221
  return c if c[0] <= ORD_LF
206
222
  end
207
-
208
- when '*'
223
+
224
+ when CHR_ASTERISK
209
225
  get
210
226
  loop do
211
227
  case get
212
- when '*'
213
- if peek == '/'
228
+ when CHR_ASTERISK
229
+ if peek == CHR_FRONTSLASH
214
230
  get
215
231
  return ' '
216
232
  end
217
-
233
+
218
234
  when nil
219
235
  raise 'JSMin parse error: unterminated comment'
220
236
  end
221
237
  end
222
-
238
+
223
239
  else
224
240
  return c
225
241
  end
226
242
  end
227
-
243
+
228
244
  # Gets the next character without getting it.
229
245
  def peek
230
246
  @lookahead = get
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Grove
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-22 00:00:00 -07:00
12
+ date: 2008-11-10 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  requirements: []
47
47
 
48
48
  rubyforge_project: riposte
49
- rubygems_version: 1.0.1
49
+ rubygems_version: 1.2.0
50
50
  signing_key:
51
51
  specification_version: 2
52
52
  summary: Ruby implementation of Douglas Crockford's JSMin JavaScript minifier.