tiny_css 0.0.2 → 0.1.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 milk1000cc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -44,5 +44,27 @@ module TinyCss
44
44
  each { |k, v| ary << "#{ k.inspect }=>#{ v.inspect }" }
45
45
  "{#{ ary.join(', ') }}"
46
46
  end
47
+
48
+ def to_s
49
+ map { |k, v| "#{ k }:#{ v }" }.join ';'
50
+ end
51
+
52
+ def empty?
53
+ @hash.empty?
54
+ end
55
+
56
+ def key?(key)
57
+ keys.include?(key)
58
+ end
59
+
60
+ def split(*keys_to_split)
61
+ h = self.class.new
62
+ keys.each do |k|
63
+ if keys_to_split.include?(k) && v = delete(k)
64
+ h[k] = v
65
+ end
66
+ end
67
+ h
68
+ end
47
69
  end
48
70
  end
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_css
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - milk1000cc
14
+ - Paul McMahon
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2008-07-10 00:00:00 +09:00
19
+ date: 2010-12-20 00:00:00 +09:00
19
20
  default_executable:
20
21
  dependencies: []
21
22
 
@@ -28,15 +29,11 @@ extensions: []
28
29
  extra_rdoc_files: []
29
30
 
30
31
  files:
32
+ - MIT-LICENSE
31
33
  - README
32
- - lib/tiny_css.rb
33
34
  - lib/tiny_css/base.rb
34
35
  - lib/tiny_css/ordered_hash.rb
35
- - spec/base_spec.rb
36
- - spec/ordered_hash_spec.rb
37
- - spec/spec.opts
38
- - spec/style.css
39
- - tiny_css.gemspec
36
+ - lib/tiny_css.rb
40
37
  has_rdoc: true
41
38
  homepage: http://github.com/milk1000cc/tiny_css
42
39
  licenses: []
data/spec/base_spec.rb DELETED
@@ -1,220 +0,0 @@
1
- require File.dirname(__FILE__) + '/../lib/tiny_css'
2
- include TinyCss
3
-
4
- describe Base, '削除に関して' do
5
- before do
6
- @css = TinyCss.new.read_string('h1, p { color: red; background: blue; }')
7
- end
8
-
9
- it 'セレクタを削除したら、style.keys にそのセレクタが含まれていないこと' do
10
- @css.style.keys.should include('h1')
11
- @css.style.delete 'h1'
12
- @css.style.keys.should_not include('h1')
13
- end
14
-
15
- it 'セレクタを削除したら、write_string の結果にもそのセレクタが含まれていないこと' do
16
- @css.style.delete 'h1'
17
- @css.write_string.should == "p {\n\tbackground: blue;\n\tcolor: red;\n}\n"
18
- end
19
-
20
- it 'プロパティを削除したら、style[セレクタ].keys にそのプロパティが' +
21
- '含まれていないこと' do
22
- @css.style['h1'].keys.should include('color')
23
- @css.style['h1'].delete 'color'
24
- @css.style['h1'].keys.should_not include('color')
25
- end
26
-
27
- it 'プロパティを削除したら、write_string の結果にも反映されていること' do
28
- @css.style['h1'].delete 'color'
29
- @css.write_string.should == "p {\n\tbackground: blue;\n\tcolor: red;\n}\n" +
30
- "h1 {\n\tbackground: blue;\n}\n"
31
- end
32
- end
33
-
34
- describe Base, 'CSS 文字列を指定してパースするとき' do
35
- before do
36
- @css1 = TinyCss.new.read_string('h3 { color: red; }')
37
- @css2 = TinyCss.new.read_string('h1, p { color: red }')
38
- @css3 = TinyCss.new.read_string("h3 { color: red; \n /*z-index: 4;*/\n" +
39
- "background: blue }")
40
- @css4 = TinyCss.new.read_string('h3, div .foo { color: red; ' +
41
- 'background: blue },div{color:red;}')
42
- @selectors2 = ['h1', 'p']
43
- end
44
-
45
- it 'は、self を返すこと' do
46
- @css1.should be_instance_of(Base)
47
- @css2.should be_instance_of(Base)
48
- @css3.should be_instance_of(Base)
49
- @css4.should be_instance_of(Base)
50
- end
51
-
52
- it 'は、style[セレクタ][プロパティ] で値が取り出せること' do
53
- @css1.style['h3']['color'].should == 'red'
54
- @css2.style['h1']['color'].should == 'red'
55
- @css2.style['p']['color'].should == 'red'
56
- @css3.style['h3']['color'].should == 'red'
57
- @css3.style['h3']['background'].should == 'blue'
58
- @css4.style['h3']['color'].should == 'red'
59
- @css4.style['h3']['background'].should == 'blue'
60
- @css4.style['div']['color'].should == 'red'
61
- end
62
-
63
- it 'は、セレクタ文字列中のホワイトスペースが半角 1 スペースに変換されていること' do
64
- @css4.style['div .foo']['color'].should == 'red'
65
- @css4.style['div .foo']['background'].should == 'blue'
66
- end
67
-
68
- it 'は、コメント内のスタイルが反映されていないこと' do
69
- @css3.style['h3']['z-index'].should_not == '4'
70
- end
71
-
72
- it 'は、style.keys で順番にセレクタが得られること' do
73
- @css2.style.keys.should == @selectors2
74
- end
75
-
76
- it 'は、style[セレクタ].keys で順番にプロパティが得られること' do
77
- @css4.style['div .foo'].keys.should == ['color', 'background']
78
- end
79
- end
80
-
81
- describe Base, '} で終わらない CSS 文字列を指定してパースするとき' do
82
- before do
83
- @proc1 = Proc.new { TinyCss.new.read_string('h3 { color: red;') }
84
- @proc2 = Proc.new {
85
- TinyCss.new.read_string('h1, p { color: red }, f')
86
- }
87
- @proc3 = Proc.new {
88
- TinyCss.new.read_string('h1, p { color: red }, div { margin: 0')
89
- }
90
- end
91
-
92
- it 'は、例外 TinyCss::Error が発生すること' do
93
- msg = "Invalid or unexpected style data 'h3 { color: red;'"
94
- @proc1.should raise_error(Error, msg)
95
- msg = "Invalid or unexpected style data ', f'"
96
- @proc2.should raise_error(Error, msg)
97
- msg = "Invalid or unexpected style data ', div { margin: 0'"
98
- @proc3.should raise_error(Error, msg)
99
- end
100
- end
101
-
102
- describe Base, 'セレクタの後 { で始まらない CSS 文字列を指定してパースするとき' do
103
- before do
104
- @proc1 = Proc.new { TinyCss.new.read_string('h3 color: red; }') }
105
- @proc2 = Proc.new {
106
- TinyCss.new.read_string('h1, p { color: red }, div }')
107
- }
108
- end
109
-
110
- it 'は、例外 TinyCss::Error が発生すること' do
111
- msg = "Invalid or unexpected style data 'h3 color: red; }'"
112
- @proc1.should raise_error(Error, msg)
113
-
114
- msg = "Invalid or unexpected style data ', div }'"
115
- @proc2.should raise_error(Error, msg)
116
- end
117
- end
118
-
119
- describe Base, 'プロパティと値が : で区切られていないとき' do
120
- before do
121
- @proc = Proc.new { TinyCss.new.read_string('h3 { color }') }
122
- end
123
-
124
- it 'は、例外 TinyCss::Error が発生すること' do
125
- msg = "unexpected property ' color ' in style 'h3'"
126
- @proc.should raise_error(Error, msg)
127
- end
128
- end
129
-
130
- describe Base do
131
- before do
132
- @css = TinyCss.new.read(File.join(File.dirname(__FILE__), 'style.css'))
133
- @selectors = ['div', 'h1', 'h2', 'div#test', 'div.foo ul', 'p#hoge',
134
- '.bar div']
135
- end
136
-
137
- describe Base, 'CSS ファイルを指定してパースするとき' do
138
- it 'は、self を返すこと' do
139
- @css.should be_instance_of(Base)
140
- end
141
-
142
- it 'は、style[セレクタ][プロパティ] で値が取り出せること' do
143
- @css.style['div']['padding'].should == '0'
144
- @css.style['div']['margin'].should == '0'
145
- @css.style['h1']['padding'].should == '0'
146
- @css.style['h1']['margin'].should == '0'
147
- @css.style['h2']['padding'].should == '0'
148
- @css.style['h2']['margin'].should == '0'
149
- @css.style['div#test']['border'].should == '1px solid black'
150
- @css.style['div#test']['padding'].should == '1px'
151
- @css.style['div#test']['padding-left'].should == '3px'
152
- @css.style['p#hoge']['color'].should == 'red'
153
- @css.style['p#hoge']['background'].should == '#ff0000'
154
- end
155
-
156
- it 'は、セレクタ文字列中のホワイトスペースが半角 1 スペースに変換されていること' do
157
- @css.style['div.foo ul']['list-style-type'].should == 'circle'
158
- @css.style['div.foo ul']['text-decoration'].should == 'underline'
159
- end
160
-
161
- it 'は、コメント内のスタイルが反映されていないこと' do
162
- @css.style['p#hoge']['font-size'].should_not == 'big'
163
- end
164
-
165
- it 'は、style.keys で順番にセレクタが得られること' do
166
- @css.style.keys.should == @selectors
167
- end
168
-
169
- it 'は、style[セレクタ].keys で順番にプロパティが得られること' do
170
- @css.style['.bar div'].keys.should == ['color', 'z-index', 'background']
171
- end
172
-
173
- it 'は、style.each で順番にセレクタとスタイルが得られること' do
174
- selectors, styles = [], []
175
- @css.style.each do |selector, style|
176
- selectors << selector
177
- styles << style
178
- end
179
- selectors.should == @selectors
180
-
181
- properties, values = [], []
182
- styles[3].each do |property, value|
183
- properties << property
184
- values << value
185
- end
186
- properties.should == ['border', 'padding', 'padding-left']
187
- values.should == ['1px solid black', '1px', '3px']
188
- end
189
- end
190
-
191
- describe Base, 'CSS 文字列を取得するとき' do
192
- before do
193
- @css.style['p#added']['background'] = 'yellow'
194
- @result = @css.write_string
195
- end
196
-
197
- it 'は、ソート(セレクタは逆ソート)されて整形されていること' do
198
- @result.should == "p#hoge {\n\tbackground: #ff0000;\n\tcolor: red;\n" +
199
- "\tz-index: 3;\n}\np#added {\n\tbackground: yellow;\n}\nh2 {\n\t" +
200
- "margin: 0;\n\tpadding: 0;\n}\nh1 {\n\tmargin: 0;\n\tpadding: 0;\n" +
201
- "}\ndiv.foo ul {\n\tlist-style-type: circle;\n\ttext-decoration: " +
202
- "underline;\n}\ndiv#test {\n\tborder: 1px solid black;\n\tpadding: " +
203
- "1px;\n\tpadding-left: 3px;\n}\ndiv {\n\tmargin: 0;\n\tpadding: 0;\n" +
204
- "}\n.bar div {\n\tbackground: #ff0000;\n\tcolor: red;\n\tz-index: " +
205
- "3;\n}\n"
206
- end
207
-
208
- it 'は、write_string で 第 1 引数に false を指定するとソートされないこと' do
209
- result = @css.write_string(false)
210
- result.should == "div {\n\tmargin: 0;\n\tpadding: 0;\n}\nh1 {\n\t" +
211
- "margin: 0;\n\tpadding: 0;\n}\nh2 {\n\tmargin: 0;\n\tpadding: 0;\n}" +
212
- "\ndiv#test {\n\tborder: 1px solid black;\n\tpadding: 1px;\n\t" +
213
- "padding-left: 3px;\n}\ndiv.foo ul {\n\tlist-style-type: circle;\n" +
214
- "\ttext-decoration: underline;\n}\np#hoge {\n\tbackground: #ff0000;\n" +
215
- "\tcolor: red;\n\tz-index: 3;\n}\n.bar div {\n\tbackground: #ff0000;" +
216
- "\n\tcolor: red;\n\tz-index: 3;\n}\np#added {\n\tbackground: yellow;" +
217
- "\n}\n"
218
- end
219
- end
220
- end
@@ -1,187 +0,0 @@
1
- require File.dirname(__FILE__) + '/../lib/tiny_css'
2
- include TinyCss
3
-
4
- describe OrderedHash, '未定義のキーが指定されたとき' do
5
- before do
6
- @oh = OrderedHash.new
7
- end
8
-
9
- it 'は、OrderedHash のインスタンスが得られること' do
10
- @oh[:a].should be_instance_of(OrderedHash)
11
-
12
- end
13
-
14
- it 'は、keys に そのキーが文字列で追加されること' do
15
- @oh[:a][:b] = 3
16
- @oh.keys.last.should == 'a'
17
- end
18
- end
19
-
20
- describe OrderedHash, '代入・参照操作について' do
21
- before do
22
- @oh1, @oh2 = OrderedHash.new, OrderedHash.new
23
- @oh1[:a] = 3
24
- @oh2['b'] = 4
25
- end
26
-
27
- it 'は、キーから値が取り出せること' do
28
- @oh1[:a].should == 3
29
- @oh2['b'].should == 4
30
- end
31
-
32
- it 'は、キーがシンボルでも文字列でも同じ値を指すこと' do
33
- @oh1[:a.to_s].should == @oh1[:a]
34
- @oh2['b'.to_sym].should == @oh2['b']
35
- end
36
-
37
- it 'は、キーが文字列に変換されていること' do
38
- @oh1.keys.should_not include(:a)
39
- @oh1.keys.should include('a')
40
- @oh2.keys.should_not include(:b)
41
- @oh2.keys.should include('b')
42
- end
43
-
44
- it 'は、きちんと上書き処理がされること' do
45
- @oh1['a'] = 4
46
- @oh1['a'].should == 4
47
- end
48
- end
49
-
50
- describe OrderedHash, '#dup について' do
51
- before do
52
- @oh = OrderedHash.new
53
- @dup = @oh.dup
54
- end
55
-
56
- it 'は、self と 戻り値 の object_id が相違なること' do
57
- @oh.object_id.should_not == @dup.object_id
58
- end
59
-
60
- it 'は、self.keys と 戻り値.keys の object_id が相違なること' do
61
- @oh.keys.object_id.should_not == @dup.keys.object_id
62
- end
63
- end
64
-
65
- describe OrderedHash, '#delete について' do
66
- before(:each) do
67
- @oh = OrderedHash.new
68
- @oh['foo'] = 3
69
- end
70
-
71
- it '存在するキーに対する関連を取り除いた場合は、取り除かれた値を返すこと' do
72
- result = @oh.delete('foo')
73
- result.should == 3
74
- end
75
-
76
- it '存在するキーに対する関連を取り除いた場合は、取り除かれた値を返すこと' +
77
- '(シンボルでキーを指定した場合も)' do
78
- result = @oh.delete(:foo)
79
- result.should == 3
80
- end
81
-
82
- it '存在するキーに対する関連を取り除いた場合は、keys からそのキーがなくなっていること' do
83
- @oh.keys.should include('foo')
84
- @oh.delete 'foo'
85
- @oh.keys.should_not include('foo')
86
- end
87
-
88
- it '存在するキーに対する関連を取り除いた場合は、keys からそのキーがなくなっていること' +
89
- '(シンボルでキーを指定した場合も)' do
90
- @oh.keys.should include('foo')
91
- @oh.delete :foo
92
- @oh.keys.should_not include('foo')
93
- end
94
-
95
- it '存在しないキーを指定された場合は、nil が返ること' do
96
- result = @oh.delete('none')
97
- result.should == nil
98
-
99
- result = @oh.delete(:none)
100
- result.should be_nil
101
- end
102
-
103
- it 'ブロックが与えられたときは、key にマッチするものがなかった時に評価して、' +
104
- 'その結果を返すこと' do
105
- result = @oh.delete('foo') { |key| key + 'no' }
106
- result.should == 3
107
-
108
- result = @oh.delete('none') { |key| key + 'no' }
109
- result.should == 'noneno'
110
- end
111
-
112
- it 'ブロックが与えられて、シンボルの key が与えられたときは' +
113
- 'key にマッチするものがなかった時にブロックを評価して、その結果を返すこと ' +
114
- '(ブロックには文字列のキーが渡される)' do
115
- result = @oh.delete(:foo) { |key| key + 'no' }
116
- result.should == 3
117
-
118
- result = @oh.delete(:none) { |key| key + 'no' }
119
- result.should == 'noneno'
120
- end
121
- end
122
-
123
- describe OrderedHash, '#each について' do
124
- before do
125
- @oh1, @oh2 = OrderedHash.new, OrderedHash.new
126
- @keys1, @keys2 = [], []
127
- @values1, @values2 = [], []
128
-
129
- @oh1['1'] = 'one'
130
- @oh1['2'] = 'two'
131
- @oh1['3'] = 'three'
132
- @oh2['2'] = 'two'
133
- @oh2['1'] = 'one'
134
- @oh2['3'] = 'three'
135
-
136
- @result1 = @oh1.each { |k, v|
137
- @keys1 << k
138
- @values1 << v
139
- }
140
-
141
- @result2 = @oh2.each { |k, v|
142
- @keys2 << k
143
- @values2 << v
144
- }
145
- end
146
-
147
- it 'は、代入順に要素が処理されること' do
148
- @keys1.should == ['1', '2', '3']
149
- @keys2.should == ['2', '1', '3']
150
- @values1.should == ['one', 'two', 'three']
151
- @values2.should == ['two', 'one', 'three']
152
- end
153
-
154
- it 'は、self を返すこと' do
155
- @result1.should == @oh1
156
- @result2.should == @oh2
157
- end
158
- end
159
-
160
- describe OrderedHash, '#inspect について' do
161
- before do
162
- oh1, oh2 = OrderedHash.new, OrderedHash.new
163
- oh1['1'] = 'one'
164
- oh1['2'] = 'two'
165
- oh1['3'] = 'three'
166
- oh2['2'] = 'two'
167
- oh2['1'] = 'one'
168
- oh2['3'] = 'three'
169
- @inspect1, @inspect2 = oh1.inspect, oh2.inspect
170
- end
171
-
172
- it 'は、{#{k.inspect}=>#{v.inspect}, ...} を返すこと' do
173
- @inspect1.should ==
174
- '{' +
175
- "#{ '1'.inspect }=>#{ 'one'.inspect }, " +
176
- "#{ '2'.inspect }=>#{ 'two'.inspect }, " +
177
- "#{ '3'.inspect }=>#{ 'three'.inspect }" +
178
- '}'
179
- @inspect2.should ==
180
- '{' +
181
- "#{ '2'.inspect }=>#{ 'two'.inspect }, " +
182
- "#{ '1'.inspect }=>#{ 'one'.inspect }, " +
183
- "#{ '3'.inspect }=>#{ 'three'.inspect }" +
184
- '}'
185
- end
186
- end
187
-
data/spec/spec.opts DELETED
@@ -1,4 +0,0 @@
1
- --colour
2
- --format progress
3
- --loadby mtime
4
- --reverse
data/spec/style.css DELETED
@@ -1,19 +0,0 @@
1
- div, h1, h2 {
2
- padding: 0; margin: 0;
3
- }
4
- div#test {
5
- border: 1px solid black;
6
- padding: 1px;
7
- padding-left: 3px;
8
- }
9
- div.foo ul {
10
- list-style-type: circle;
11
- text-decoration: underline;
12
- }
13
- p#hoge, .bar div
14
- {
15
- color: red;
16
- /* font-size: big; */
17
- z-index: 3 ;
18
- background: #ff0000;
19
- }
data/tiny_css.gemspec DELETED
@@ -1,12 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = "tiny_css"
3
- s.version = "0.0.2"
4
- s.date = "2008-07-10"
5
- s.summary = ""
6
- s.email = "info@milk1000.cc"
7
- s.homepage = "http://github.com/milk1000cc/tiny_css"
8
- s.description = ""
9
- s.has_rdoc = false
10
- s.authors = ["milk1000cc"]
11
- s.files = ["README", "lib/tiny_css.rb", "lib/tiny_css/base.rb", "lib/tiny_css/ordered_hash.rb", "spec/base_spec.rb", "spec/ordered_hash_spec.rb", "spec/spec.opts", "spec/style.css", "tiny_css.gemspec"]
12
- end