csso-rails 0.0.1

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.
@@ -0,0 +1,126 @@
1
+ function CSSOTranslator() {}
2
+
3
+ CSSOTranslator.prototype.translate = function(tree) {
4
+ return this._t(tree);
5
+ };
6
+
7
+ CSSOTranslator.prototype._m_simple = {
8
+ 'unary': 1, 'nth': 1, 'combinator': 1, 'ident': 1, 'number': 1, 's': 1,
9
+ 'string': 1, 'attrselector': 1, 'operator': 1, 'raw': 1, 'unknown': 1
10
+ };
11
+
12
+ CSSOTranslator.prototype._m_composite = {
13
+ 'simpleselector': 1, 'dimension': 1, 'selector': 1, 'property': 1, 'value': 1,
14
+ 'filterv': 1, 'progid': 1, 'ruleset': 1, 'atruleb': 1, 'atrulerq': 1, 'atrulers': 1,
15
+ 'stylesheet': 1
16
+ };
17
+
18
+ CSSOTranslator.prototype._m_primitive = {
19
+ 'cdo': 'cdo', 'cdc': 'cdc', 'decldelim': ';', 'namespace': '|', 'delim': ','
20
+ };
21
+
22
+ CSSOTranslator.prototype._t = function(tree) {
23
+ var t = tree[0];
24
+ if (t in this._m_primitive) return this._m_primitive[t];
25
+ else if (t in this._m_simple) return this._simple(tree);
26
+ else if (t in this._m_composite) return this._composite(tree);
27
+ return this[t](tree);
28
+ };
29
+
30
+ CSSOTranslator.prototype._composite = function(t, i) {
31
+ var s = '';
32
+ i = i === undefined ? 1 : i;
33
+ for (; i < t.length; i++) s += this._t(t[i]);
34
+ return s;
35
+ };
36
+
37
+ CSSOTranslator.prototype._simple = function(t) {
38
+ return t[1];
39
+ };
40
+
41
+ CSSOTranslator.prototype.percentage = function(t) {
42
+ return this._t(t[1]) + '%';
43
+ };
44
+
45
+ CSSOTranslator.prototype.comment = function(t) {
46
+ return '/*' + t[1] + '*/';
47
+ };
48
+
49
+ CSSOTranslator.prototype.clazz = function(t) {
50
+ return '.' + this._t(t[1]);
51
+ };
52
+
53
+ CSSOTranslator.prototype.atkeyword = function(t) {
54
+ return '@' + this._t(t[1]);
55
+ };
56
+
57
+ CSSOTranslator.prototype.shash = function(t) {
58
+ return '#' + t[1];
59
+ };
60
+
61
+ CSSOTranslator.prototype.vhash = function(t) {
62
+ return '#' + t[1];
63
+ };
64
+
65
+ CSSOTranslator.prototype.attrib = function(t) {
66
+ return '[' + this._composite(t) + ']';
67
+ };
68
+
69
+ CSSOTranslator.prototype.important = function(t) {
70
+ return '!' + this._composite(t) + 'important';
71
+ };
72
+
73
+ CSSOTranslator.prototype.nthselector = function(t) {
74
+ return ':' + this._simple(t[1]) + '(' + this._composite(t, 2) + ')';
75
+ };
76
+
77
+ CSSOTranslator.prototype.funktion = function(t) {
78
+ return this._simple(t[1]) + '(' + this._composite(t[2]) + ')';
79
+ };
80
+
81
+ CSSOTranslator.prototype.declaration = function(t) {
82
+ return this._t(t[1]) + ':' + this._t(t[2]);
83
+ };
84
+
85
+ CSSOTranslator.prototype.filter = function(t) {
86
+ return this._t(t[1]) + ':' + this._t(t[2]);
87
+ };
88
+
89
+ CSSOTranslator.prototype.block = function(t) {
90
+ return '{' + this._composite(t) + '}';
91
+ };
92
+
93
+ CSSOTranslator.prototype.braces = function(t) {
94
+ return t[1] + this._composite(t, 3) + t[2];
95
+ };
96
+
97
+ CSSOTranslator.prototype.atrules = function(t) {
98
+ return this._composite(t) + ';';
99
+ };
100
+
101
+ CSSOTranslator.prototype.atruler = function(t) {
102
+ return this._t(t[1]) + this._t(t[2]) + '{' + this._t(t[3]) + '}';
103
+ };
104
+
105
+ CSSOTranslator.prototype.pseudoe = function(t) {
106
+ return '::' + this._t(t[1]);
107
+ };
108
+
109
+ CSSOTranslator.prototype.pseudoc = function(t) {
110
+ return ':' + this._t(t[1]);
111
+ };
112
+
113
+ CSSOTranslator.prototype.uri = function(t) {
114
+ return 'url(' + this._composite(t) + ')';
115
+ };
116
+
117
+ CSSOTranslator.prototype.functionExpression = function(t) {
118
+ return 'expression(' + t[1] + ')';
119
+ };
120
+ exports.translate = function(tree) {
121
+ return new CSSOTranslator().translate(tree);
122
+ };
123
+
124
+ exports.translator = function() {
125
+ return new CSSOTranslator();
126
+ };
@@ -0,0 +1,41 @@
1
+ var $util = {};
2
+
3
+ $util.cleanInfo = function(tree) {
4
+ var r = [];
5
+ tree = tree.slice(1);
6
+
7
+ tree.forEach(function(e) {
8
+ r.push(Array.isArray(e) ? $util.cleanInfo(e) : e);
9
+ });
10
+
11
+ return r;
12
+ };
13
+
14
+ $util.treeToString = function(tree, level) {
15
+ var spaces = $util.dummySpaces(level),
16
+ level = level ? level : 0,
17
+ s = (level ? '\n' + spaces : '') + '[';
18
+
19
+ tree.forEach(function(e) {
20
+ s += (Array.isArray(e) ? $util.treeToString(e, level + 1) : e.f !== undefined ? $util.ircToString(e) : ('\'' + e.toString() + '\'')) + ', ';
21
+ });
22
+
23
+ return s.substr(0, s.length - 2) + ']';
24
+ };
25
+
26
+ $util.ircToString = function(o) {
27
+ return '{' + o.f + ',' + o.l + '}';
28
+ };
29
+
30
+ $util.dummySpaces = function(num) {
31
+ return ' '.substr(0, num * 2);
32
+ };
33
+ $util.printTree = function(tree) {
34
+ require('sys').print($util.treeToString(tree));
35
+ };
36
+
37
+ exports.cleanInfo = $util.cleanInfo;
38
+
39
+ exports.treeToString = $util.treeToString;
40
+
41
+ exports.printTree = $util.printTree;
@@ -0,0 +1,74 @@
1
+ require 'commonjs'
2
+ require 'pathname'
3
+
4
+ module Csso
5
+ class Loader
6
+ include CallJS
7
+
8
+ attr_reader :environment
9
+
10
+ class ModifiedEnvironment < CommonJS::Environment
11
+ # CommonJS' require does append .js to modules no matter if it is there already
12
+ def require module_id
13
+ path = module_id.gsub(/^\.\//, "")
14
+ path = path.gsub(/\.js$/, "")
15
+ super path
16
+ end
17
+ end
18
+
19
+ def initialize js_path=nil
20
+ @js_path = js_path || Pathname(__FILE__).dirname.join('js').to_s
21
+ @cxt = V8::Context.new
22
+ @environment = ModifiedEnvironment.new(@cxt, :path => @js_path)
23
+
24
+ [Util, Path, Fs].each do |native|
25
+ @environment.native(native.to_s.downcase, native.new)
26
+ end
27
+
28
+ [Process, Console].each do|replace|
29
+ @cxt[replace.to_s.downcase] = replace.new
30
+ end
31
+ end
32
+
33
+ def require(module_id)
34
+ @environment.require(module_id)
35
+ end
36
+
37
+ class Path
38
+ def join(*components)
39
+ File.join(*components)
40
+ end
41
+
42
+ def dirname(path)
43
+ File.dirname(path)
44
+ end
45
+ end
46
+
47
+ class Util # sys
48
+ def error(*errors)
49
+ raise errors.join(' ')
50
+ end
51
+ end
52
+
53
+ class Fs
54
+ def statSync(path)
55
+ File.stat(path)
56
+ end
57
+
58
+ def readFile(path, encoding, callback)
59
+ callback.call(nil, File.read(path))
60
+ end
61
+ end
62
+
63
+ class Process
64
+ def exit(*args)
65
+ end
66
+ end
67
+
68
+ class Console
69
+ def log(*msgs)
70
+ puts msgs.join(',')
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,33 @@
1
+ require "action_controller/railtie"
2
+
3
+ module Csso
4
+
5
+ COMPRESSOR_SYM = :csso
6
+
7
+ class Railtie < ::Rails::Railtie
8
+ initializer "csso.environment", :after => "sprockets.environment" do
9
+ CssCompressor.register
10
+ end
11
+
12
+ # saas-rails-3.2.4(and may be others) sets itself as default, ignoring config? => override :(
13
+ initializer "csso.setup", :after => :setup_compression, :group => :all do|app|
14
+ if app.config.assets.enabled && (!app.config.assets.css_compressor.respond_to?(:to_sym))
15
+ app.config.assets.css_compressor = :csso
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ class CssCompressor
22
+ def compress(css)
23
+ require 'csso'
24
+ #TODO: settings?
25
+ Csso.optimize(css, true)
26
+ end
27
+
28
+ def self.register
29
+ Sprockets::Compressors.register_css_compressor(COMPRESSOR_SYM, 'Csso::CssCompressor', :default => true)
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,53 @@
1
+ module Csso
2
+ # Utility for calling into the JavaScript runtime.
3
+ module CallJS
4
+
5
+ # @private
6
+ # Wrap JavaScript invocations with uniform error handling
7
+ #
8
+ # @yield code to wrap
9
+ def calljs err_cls=WrappedError
10
+ lock do
11
+ yield
12
+ end
13
+ rescue V8::JSError => e
14
+ raise err_cls.new(e)
15
+ end
16
+
17
+ # @private
18
+ # Ensure proper locking before entering the V8 API
19
+ #
20
+ # @yield code to wrap in lock
21
+ def lock
22
+ result, exception = nil, nil
23
+ V8::C::Locker() do
24
+ begin
25
+ result = yield
26
+ rescue Exception => e
27
+ exception = e
28
+ end
29
+ end
30
+ if exception
31
+ raise exception
32
+ else
33
+ result
34
+ end
35
+ end
36
+ end
37
+
38
+ class WrappedError < StandardError
39
+
40
+ # Copies over `error`'s message and backtrace
41
+ # @param [V8::JSError] error native error
42
+ def initialize(error)
43
+ super(error.message)
44
+ @backtrace = error.backtrace
45
+ end
46
+
47
+ # @return [Array] the backtrace frames
48
+ def backtrace
49
+ @backtrace
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,3 @@
1
+ module Csso
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Csso do
4
+
5
+ it "should optimize css" do
6
+ subject.optimize("a {\ncolor: white; }").should == "a{color:#fff}"
7
+ end
8
+
9
+ it "should optimize structure" do
10
+ subject.optimize("a {\ncolor: white; } a{color: red;}").should == "a{color:red}"
11
+ end
12
+
13
+ it "should optimize structure" do
14
+ pending "original csso is a bit broken at the moment"
15
+ # FIXME: csso produces "a{color:#fff;color:red}" on this :(
16
+ subject.optimize("a {\ncolor: white; } a{color: #ff0000;}").should == "a{color:red}"
17
+ end
18
+
19
+ it "should optimize structure in maniac mode" do
20
+ subject.optimize("a {\ncolor: white; } a{color: #ff0000;}", true).should == "a{color:red}"
21
+ end
22
+
23
+ it 'should produce no error on empty input' do
24
+ subject.optimize(nil).should == nil
25
+ subject.optimize("").should == ""
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift Pathname(__FILE__).dirname.join('..','lib')
2
+
3
+ require 'csso'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csso-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vasily Fedoseyev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: therubyracer
16
+ requirement: &70109118748280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70109118748280
25
+ - !ruby/object:Gem::Dependency
26
+ name: commonjs
27
+ requirement: &70109108381060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70109108381060
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70109108380680 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70109108380680
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70109108380140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70109108380140
58
+ description: Invoke the CSSO from Ruby
59
+ email:
60
+ - vasilyfedoseyev@gmail.com
61
+ executables:
62
+ - ruby_csso
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .gitmodules
68
+ - .rspec
69
+ - Gemfile
70
+ - README.md
71
+ - Rakefile
72
+ - bin/ruby_csso
73
+ - csso-rails.gemspec
74
+ - lib/csso-rails.rb
75
+ - lib/csso.rb
76
+ - lib/csso/js/compressor.js
77
+ - lib/csso/js/cssoapi.js
78
+ - lib/csso/js/parser.js
79
+ - lib/csso/js/translator.js
80
+ - lib/csso/js/util.js
81
+ - lib/csso/loader.rb
82
+ - lib/csso/rails.rb
83
+ - lib/csso/utils.rb
84
+ - lib/csso/version.rb
85
+ - spec/csso/csso_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/Vasfed/csso-rails
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project: csso-rails
107
+ rubygems_version: 1.8.15
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: CSS Stylesheet optimizer/compressor for Rails
111
+ test_files:
112
+ - spec/csso/csso_spec.rb
113
+ - spec/spec_helper.rb
114
+ has_rdoc: