css_modules 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -2
- data/lib/css_modules/assets/css_module.js.erb +31 -8
- data/lib/css_modules/version.rb +1 -1
- data/lib/css_modules/view_helper.rb +33 -13
- metadata +2 -3
- data/lib/css_modules/assets/base64.js +0 -136
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 352a7700216a0f529079aff18c7035208211015e
|
4
|
+
data.tar.gz: 84a4377828e9abae5e54cc2cab1efbf71d695054
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f97e68229bab8c72efa3d9b35606c5b47679c82c2b2ae5ea7533abd624b74ec1353e37612d2f4dcc391518800b66fe9addc99a161dc581d43337baa03a693ab
|
7
|
+
data.tar.gz: 94b85889226a878d04137af172ffc6b8f7d3361328ca856323b9828ebe0389ab68a045d7a7ade68551925e067033ea3e7b0e1bb331bb0dc3daf06b96562076e4
|
data/README.md
CHANGED
@@ -66,6 +66,29 @@ Then, in your view, you can access the module & its contents by name:
|
|
66
66
|
<% end %>
|
67
67
|
```
|
68
68
|
|
69
|
+
#### Extra classes
|
70
|
+
|
71
|
+
You can also provide _multiple_, space-separated class names and/or extra class names to join without a module:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# Apply "events" to "image-wrapper" and "image", then add "pull-left" without modification
|
75
|
+
css_module("events", "image-wrapper image", "pull-left")
|
76
|
+
#=> "events_123_image-wrapper events_123_image pull-left
|
77
|
+
```
|
78
|
+
|
79
|
+
#### Null module
|
80
|
+
|
81
|
+
If you pass `nil` as the module name, _no_ transformation is applied to the selectors.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
css_module(nil, "media-row")
|
85
|
+
# => "media-row"
|
86
|
+
css_module(nil) do |styles|
|
87
|
+
styles.selector("image image--main", "pull-left")
|
88
|
+
# => "image image--main pull-left"
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
69
92
|
### Use modulized names in JavaScript
|
70
93
|
|
71
94
|
In JavaScript, you can include a helper to access module styles:
|
@@ -88,10 +111,27 @@ function header() {
|
|
88
111
|
}
|
89
112
|
```
|
90
113
|
|
91
|
-
|
114
|
+
#### Extra classes
|
115
|
+
|
116
|
+
You can also provide _multiple_, space-separated class names and/or extra class names to add without a module:
|
92
117
|
|
93
118
|
```js
|
94
|
-
|
119
|
+
// Apply "events" to "image-wrapper" and "image", then add "pull-left" without modification
|
120
|
+
CSSModule("events", "image-wrapper image", "pull-left")
|
121
|
+
// "events_123_image-wrapper events_123_image pull-left"
|
122
|
+
```
|
123
|
+
|
124
|
+
#### Null module
|
125
|
+
|
126
|
+
If you pass `null` as the module name, `CSSModule` will make _no_ transformation:
|
127
|
+
|
128
|
+
```js
|
129
|
+
CSSModule(null, "item")
|
130
|
+
// => "item"
|
131
|
+
|
132
|
+
var cssModule = CSSModule(null)
|
133
|
+
cssModule("item")
|
134
|
+
// => item
|
95
135
|
```
|
96
136
|
|
97
137
|
## Installation
|
@@ -35,25 +35,48 @@ var CSSModule = (function() {
|
|
35
35
|
return stringSum % HASH_LIMIT;
|
36
36
|
};
|
37
37
|
|
38
|
-
function modulizeSelector(moduleName, selectorName
|
39
|
-
if (environment === "production") {
|
38
|
+
function modulizeSelector(moduleName, selectorName) {
|
39
|
+
if (_CSSModule.environment === "production") {
|
40
40
|
return moduleName.substr(0, 1) + computeHash(moduleName) + selectorName;
|
41
41
|
} else {
|
42
42
|
return moduleName + "_" + computeHash(moduleName) + "_" + selectorName;
|
43
43
|
}
|
44
44
|
}
|
45
45
|
|
46
|
-
function
|
47
|
-
|
46
|
+
function createJoinedSelector(moduleName, selectorNames, bareSelectorNames) {
|
47
|
+
var modulizedSelectorNames;
|
48
48
|
|
49
|
-
if (
|
50
|
-
|
49
|
+
if (moduleName) {
|
50
|
+
// Split and apply transformation to each part:
|
51
|
+
modulizedSelectorNames = selectorNames
|
52
|
+
.split(" ")
|
53
|
+
.map(function(selectorName) {
|
54
|
+
return modulizeSelector(moduleName, selectorName);
|
55
|
+
})
|
51
56
|
} else {
|
52
|
-
|
53
|
-
|
57
|
+
// Don't bother splitting, it's a null module
|
58
|
+
modulizedSelectorNames = [selectorNames]
|
59
|
+
}
|
60
|
+
|
61
|
+
if (bareSelectorNames) {
|
62
|
+
modulizedSelectorNames.push(bareSelectorNames);
|
63
|
+
}
|
64
|
+
|
65
|
+
return modulizedSelectorNames.join(" ");
|
66
|
+
}
|
67
|
+
|
68
|
+
function _CSSModule(moduleName, selectorNames, bareSelectorNames) {
|
69
|
+
if (selectorNames) {
|
70
|
+
return createJoinedSelector(moduleName, selectorNames, bareSelectorNames);
|
71
|
+
} else {
|
72
|
+
return function(selectorNames, bareSelectorNames) {
|
73
|
+
return createJoinedSelector(moduleName, selectorNames, bareSelectorNames);
|
54
74
|
};
|
55
75
|
}
|
56
76
|
};
|
57
77
|
|
78
|
+
_CSSModule.environment = "<%= CSSModules.env.to_s %>";
|
79
|
+
|
80
|
+
|
58
81
|
return _CSSModule;
|
59
82
|
})();
|
data/lib/css_modules/version.rb
CHANGED
@@ -14,8 +14,9 @@ module CSSModules
|
|
14
14
|
# css_module("events_index", "header")
|
15
15
|
# # => "..." (opaque string which matches the stylesheet)
|
16
16
|
#
|
17
|
-
# @param module_name
|
18
|
-
# @param
|
17
|
+
# @param module_name [String]
|
18
|
+
# @param selector_names [String] Space-separated DOM ids or class names
|
19
|
+
# @param bare_selector_names [String] Space-separated selectors to be appended _without_ the module
|
19
20
|
# @return [String] modulized selector name for `class=` or `id=` in a view
|
20
21
|
#
|
21
22
|
# @overload css_module(module_name, &block)
|
@@ -27,29 +28,48 @@ module CSSModules
|
|
27
28
|
# <p id="<%= events_module.selector("description") %>"> ... </p>
|
28
29
|
# <% end %>
|
29
30
|
#
|
30
|
-
# @param module_name
|
31
|
+
# @param module_name [String]
|
31
32
|
# @yieldparam [ModuleLookup] a helper for modulizing selectors within `module_name`
|
32
33
|
# @return [void]
|
33
|
-
def css_module(module_name,
|
34
|
-
|
35
|
-
|
34
|
+
def css_module(module_name, selector_names = nil, bare_selector_names = nil, &block)
|
35
|
+
lookup = ModuleLookup.new(module_name)
|
36
|
+
|
37
|
+
if selector_names.nil? && block_given?
|
36
38
|
yield(lookup)
|
37
39
|
nil
|
38
|
-
elsif
|
39
|
-
|
40
|
+
elsif selector_names.present?
|
41
|
+
lookup.selector(selector_names.to_s, bare_selector_names.to_s)
|
40
42
|
else
|
41
|
-
raise("css_module must be called with a module_name and either
|
43
|
+
raise("css_module must be called with a module_name and either selector_names or a block")
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
45
|
-
# Shorthand for getting several classnames from one module
|
46
47
|
class ModuleLookup
|
47
48
|
def initialize(module_name)
|
48
|
-
@module_name = module_name
|
49
|
+
@module_name = module_name
|
50
|
+
end
|
51
|
+
|
52
|
+
# @see {ViewHelper#css_module}
|
53
|
+
# @param selector_names [String]
|
54
|
+
# @param bare_selector_names [String]
|
55
|
+
def selector(selector_names, bare_selector_names = nil)
|
56
|
+
create_joined_selector(@module_name, selector_names.to_s, bare_selector_names.to_s)
|
49
57
|
end
|
50
58
|
|
51
|
-
|
52
|
-
|
59
|
+
private
|
60
|
+
|
61
|
+
def create_joined_selector(module_name, selector_names, bare_selector_names)
|
62
|
+
padded_bare_selector_names = bare_selector_names.present? ? " #{bare_selector_names}" : ""
|
63
|
+
case module_name
|
64
|
+
when nil
|
65
|
+
selector_names + padded_bare_selector_names
|
66
|
+
else
|
67
|
+
selector_names
|
68
|
+
.split(" ")
|
69
|
+
.map { |selector_name| CSSModules::Rewrite.modulize_selector(module_name, selector_name) }
|
70
|
+
.join(" ")
|
71
|
+
.concat(padded_bare_selector_names)
|
72
|
+
end
|
53
73
|
end
|
54
74
|
end
|
55
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: css_modules
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Mosolgo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -77,7 +77,6 @@ files:
|
|
77
77
|
- README.md
|
78
78
|
- Rakefile
|
79
79
|
- lib/css_modules.rb
|
80
|
-
- lib/css_modules/assets/base64.js
|
81
80
|
- lib/css_modules/assets/css_module.js.erb
|
82
81
|
- lib/css_modules/engine.rb
|
83
82
|
- lib/css_modules/rewrite.rb
|
@@ -1,136 +0,0 @@
|
|
1
|
-
// btoa polyfill, from https://jsfiddle.net/gabrieleromanato/qaght/
|
2
|
-
var Base64 = {
|
3
|
-
|
4
|
-
|
5
|
-
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
6
|
-
|
7
|
-
|
8
|
-
encode: function(input) {
|
9
|
-
var output = "";
|
10
|
-
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
11
|
-
var i = 0;
|
12
|
-
|
13
|
-
input = Base64._utf8_encode(input);
|
14
|
-
|
15
|
-
while (i < input.length) {
|
16
|
-
|
17
|
-
chr1 = input.charCodeAt(i++);
|
18
|
-
chr2 = input.charCodeAt(i++);
|
19
|
-
chr3 = input.charCodeAt(i++);
|
20
|
-
|
21
|
-
enc1 = chr1 >> 2;
|
22
|
-
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
23
|
-
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
24
|
-
enc4 = chr3 & 63;
|
25
|
-
|
26
|
-
if (isNaN(chr2)) {
|
27
|
-
enc3 = enc4 = 64;
|
28
|
-
} else if (isNaN(chr3)) {
|
29
|
-
enc4 = 64;
|
30
|
-
}
|
31
|
-
|
32
|
-
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
|
33
|
-
|
34
|
-
}
|
35
|
-
|
36
|
-
return output;
|
37
|
-
},
|
38
|
-
|
39
|
-
|
40
|
-
decode: function(input) {
|
41
|
-
var output = "";
|
42
|
-
var chr1, chr2, chr3;
|
43
|
-
var enc1, enc2, enc3, enc4;
|
44
|
-
var i = 0;
|
45
|
-
|
46
|
-
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
47
|
-
|
48
|
-
while (i < input.length) {
|
49
|
-
|
50
|
-
enc1 = this._keyStr.indexOf(input.charAt(i++));
|
51
|
-
enc2 = this._keyStr.indexOf(input.charAt(i++));
|
52
|
-
enc3 = this._keyStr.indexOf(input.charAt(i++));
|
53
|
-
enc4 = this._keyStr.indexOf(input.charAt(i++));
|
54
|
-
|
55
|
-
chr1 = (enc1 << 2) | (enc2 >> 4);
|
56
|
-
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
57
|
-
chr3 = ((enc3 & 3) << 6) | enc4;
|
58
|
-
|
59
|
-
output = output + String.fromCharCode(chr1);
|
60
|
-
|
61
|
-
if (enc3 != 64) {
|
62
|
-
output = output + String.fromCharCode(chr2);
|
63
|
-
}
|
64
|
-
if (enc4 != 64) {
|
65
|
-
output = output + String.fromCharCode(chr3);
|
66
|
-
}
|
67
|
-
|
68
|
-
}
|
69
|
-
|
70
|
-
output = Base64._utf8_decode(output);
|
71
|
-
|
72
|
-
return output;
|
73
|
-
|
74
|
-
},
|
75
|
-
|
76
|
-
_utf8_encode: function(string) {
|
77
|
-
string = string.replace(/\r\n/g, "\n");
|
78
|
-
var utftext = "";
|
79
|
-
|
80
|
-
for (var n = 0; n < string.length; n++) {
|
81
|
-
|
82
|
-
var c = string.charCodeAt(n);
|
83
|
-
|
84
|
-
if (c < 128) {
|
85
|
-
utftext += String.fromCharCode(c);
|
86
|
-
}
|
87
|
-
else if ((c > 127) && (c < 2048)) {
|
88
|
-
utftext += String.fromCharCode((c >> 6) | 192);
|
89
|
-
utftext += String.fromCharCode((c & 63) | 128);
|
90
|
-
}
|
91
|
-
else {
|
92
|
-
utftext += String.fromCharCode((c >> 12) | 224);
|
93
|
-
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
94
|
-
utftext += String.fromCharCode((c & 63) | 128);
|
95
|
-
}
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
return utftext;
|
100
|
-
},
|
101
|
-
|
102
|
-
_utf8_decode: function(utftext) {
|
103
|
-
var string = "";
|
104
|
-
var i = 0;
|
105
|
-
var c = c1 = c2 = 0;
|
106
|
-
|
107
|
-
while (i < utftext.length) {
|
108
|
-
|
109
|
-
c = utftext.charCodeAt(i);
|
110
|
-
|
111
|
-
if (c < 128) {
|
112
|
-
string += String.fromCharCode(c);
|
113
|
-
i++;
|
114
|
-
}
|
115
|
-
else if ((c > 191) && (c < 224)) {
|
116
|
-
c2 = utftext.charCodeAt(i + 1);
|
117
|
-
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
118
|
-
i += 2;
|
119
|
-
}
|
120
|
-
else {
|
121
|
-
c2 = utftext.charCodeAt(i + 1);
|
122
|
-
c3 = utftext.charCodeAt(i + 2);
|
123
|
-
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
124
|
-
i += 3;
|
125
|
-
}
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
return string;
|
130
|
-
}
|
131
|
-
|
132
|
-
}
|
133
|
-
|
134
|
-
if (!btoa) {
|
135
|
-
var btoa = function(str) { return Base64.encode(str) }
|
136
|
-
}
|