yui-compressor 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,16 @@
1
- require "open3"
1
+ require "popen4"
2
+ require "shellwords"
2
3
  require "stringio"
3
4
 
4
5
  module YUI #:nodoc:
5
6
  class Compressor
6
- VERSION = "0.9.4"
7
-
8
- YUI_ERROR_MARKER = '[ERROR]'
7
+ VERSION = "0.9.6"
9
8
 
10
9
  class Error < StandardError; end
11
10
  class OptionError < Error; end
12
11
  class RuntimeError < Error; end
13
12
 
14
- attr_reader :options, :command
13
+ attr_reader :options
15
14
 
16
15
  def self.default_options #:nodoc:
17
16
  { :charset => "utf-8", :line_break => nil }
@@ -26,6 +25,10 @@ module YUI #:nodoc:
26
25
  @command = [path_to_java, "-jar", path_to_jar_file, *(command_option_for_type + command_options)]
27
26
  end
28
27
 
28
+ def command #:nodoc:
29
+ @command.map { |word| Shellwords.escape(word) }.join(" ")
30
+ end
31
+
29
32
  # Compress a stream or string of code with YUI Compressor. (A stream is
30
33
  # any object that responds to +read+ and +close+ like an IO.) If a block
31
34
  # is given, you can read the compressed code from the block's argument.
@@ -61,23 +64,28 @@ module YUI #:nodoc:
61
64
  #
62
65
  def compress(stream_or_string)
63
66
  streamify(stream_or_string) do |stream|
64
- Open3.popen3(*command) do |stdin, stdout, stderr|
67
+ output = true
68
+ status = POpen4.popen4(command, "b") do |stdout, stderr, stdin, pid|
65
69
  begin
66
70
  stdin.binmode
67
71
  transfer(stream, stdin)
68
72
 
69
- raise if stderr.read.include?(YUI_ERROR_MARKER)
70
-
71
73
  if block_given?
72
74
  yield stdout
73
75
  else
74
- stdout.read
76
+ output = stdout.read
75
77
  end
76
78
 
77
79
  rescue Exception => e
78
80
  raise RuntimeError, "compression failed"
79
81
  end
80
82
  end
83
+
84
+ if status.exitstatus.zero?
85
+ output
86
+ else
87
+ raise RuntimeError, "compression failed"
88
+ end
81
89
  end
82
90
  end
83
91
 
@@ -114,6 +122,7 @@ module YUI #:nodoc:
114
122
  while buffer = from_stream.read(4096)
115
123
  to_stream.write(buffer)
116
124
  end
125
+ from_stream.close
117
126
  to_stream.close
118
127
  end
119
128
 
@@ -7,7 +7,7 @@ module YUI
7
7
  div.warning {
8
8
  display: none;
9
9
  }
10
-
10
+
11
11
  div.error {
12
12
  background: red;
13
13
  color: white;
@@ -35,23 +35,30 @@ module YUI
35
35
  END_JS
36
36
 
37
37
  FIXTURE_ERROR_JS = "var x = {class: 'name'};"
38
-
38
+
39
39
  def test_compressor_should_raise_when_instantiated
40
40
  assert_raises YUI::Compressor::Error do
41
41
  YUI::Compressor.new
42
42
  end
43
43
  end
44
-
44
+
45
45
  def test_css_should_be_compressed
46
46
  @compressor = YUI::CssCompressor.new
47
47
  assert_equal "div.warning{display:none}div.error{background:red;color:white}@media screen and (max-device-width:640px){body{font-size:90%}}", @compressor.compress(FIXTURE_CSS)
48
48
  end
49
-
49
+
50
50
  def test_js_should_be_compressed
51
51
  @compressor = YUI::JavaScriptCompressor.new
52
52
  assert_equal "var Foo={a:1};Foo.bar=(function(baz){if(false){doSomething()}else{for(var index=0;index<baz.length;index++){doSomething(baz[index])}}})(\"hello\");", @compressor.compress(FIXTURE_JS)
53
53
  end
54
-
54
+
55
+ def test_large_js_should_be_compressed
56
+ assert_nothing_raised do
57
+ @compressor = YUI::JavaScriptCompressor.new
58
+ @compressor.compress(FIXTURE_JS * 200)
59
+ end
60
+ end
61
+
55
62
  def test_compress_should_raise_when_an_unknown_option_is_specified
56
63
  assert_raises YUI::Compressor::OptionError do
57
64
  @compressor = YUI::CssCompressor.new(:foo => "bar")
@@ -63,7 +70,7 @@ module YUI
63
70
  @compressor = YUI::CssCompressor.new
64
71
  assert_equal "div.warning{display:none}div.error{background:red;color:white}@media screen and (max-device-width:640px){body{font-size:90%}}", @compressor.compress(StringIO.new(FIXTURE_CSS))
65
72
  end
66
-
73
+
67
74
  def test_compress_should_accept_a_block_and_yield_an_io
68
75
  @compressor = YUI::CssCompressor.new
69
76
  @compressor.compress(FIXTURE_CSS) do |stream|
@@ -71,23 +78,22 @@ module YUI
71
78
  assert_equal "div.warning{display:none}div.error{background:red;color:white}@media screen and (max-device-width:640px){body{font-size:90%}}", stream.read
72
79
  end
73
80
  end
74
-
81
+
75
82
  def test_line_break_option_should_insert_line_breaks_in_css
76
83
  @compressor = YUI::CssCompressor.new(:line_break => 0)
77
84
  assert_equal "div.warning{display:none}\ndiv.error{background:red;color:white}\n@media screen and (max-device-width:640px){body{font-size:90%}\n}", @compressor.compress(FIXTURE_CSS)
78
-
79
85
  end
80
-
86
+
81
87
  def test_line_break_option_should_insert_line_breaks_in_js
82
88
  @compressor = YUI::JavaScriptCompressor.new(:line_break => 0)
83
89
  assert_equal "var Foo={a:1};\nFoo.bar=(function(baz){if(false){doSomething()\n}else{for(var index=0;\nindex<baz.length;\nindex++){doSomething(baz[index])\n}}})(\"hello\");", @compressor.compress(FIXTURE_JS)
84
90
  end
85
-
91
+
86
92
  def test_munge_option_should_munge_local_variable_names
87
93
  @compressor = YUI::JavaScriptCompressor.new(:munge => true)
88
94
  assert_equal "var Foo={a:1};Foo.bar=(function(b){if(false){doSomething()}else{for(var a=0;a<b.length;a++){doSomething(b[a])}}})(\"hello\");", @compressor.compress(FIXTURE_JS)
89
95
  end
90
-
96
+
91
97
  def test_optimize_option_should_not_modify_property_accesses_or_object_literal_keys_when_false
92
98
  @compressor = YUI::JavaScriptCompressor.new(:optimize => false)
93
99
  assert_equal "var Foo={\"a\":1};Foo[\"bar\"]=(function(baz){if(false){doSomething()}else{for(var index=0;index<baz.length;index++){doSomething(baz[index])}}})(\"hello\");", @compressor.compress(FIXTURE_JS)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yui-compressor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 5
10
- version: 0.9.5
9
+ - 6
10
+ version: 0.9.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Stephenson
@@ -15,10 +15,25 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-29 00:00:00 -05:00
18
+ date: 2011-03-30 00:00:00 -05:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: POpen4
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 4
34
+ version: 0.1.4
35
+ type: :runtime
36
+ version_requirements: *id001
22
37
  description: A Ruby interface to YUI Compressor for minifying JavaScript and CSS assets.
23
38
  email: sstephenson@gmail.com
24
39
  executables: []