css_inliner 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -0
- data/css_inliner.gemspec +2 -1
- data/lib/css_inliner/csspool.rb +34 -1
- data/lib/css_inliner/extractor.rb +9 -0
- data/lib/css_inliner/version.rb +1 -1
- data/test/fixtures/sample1/group1-1.css +3 -0
- data/test/fixtures/sample1/group1-2.css +3 -0
- data/test/fixtures/sample1/group2-1.css +3 -0
- data/test/helper.rb +1 -0
- data/test/test_csspool.rb +141 -0
- data/test/test_extractor.rb +27 -0
- metadata +28 -24
data/README.rdoc
CHANGED
@@ -69,6 +69,11 @@ Note that this is <b>not stable at all</b>.
|
|
69
69
|
CSSInliner.process html, '.', 'article' # => The first article element
|
70
70
|
CSSInliner.process html, 'http://example.net/stylesheets' # Passed URI will be used as base directory when resolving relative URI of CSS
|
71
71
|
|
72
|
+
== History
|
73
|
+
=== 0.3.0 / 2012-05-28
|
74
|
+
* Group external CSS files refered by <link> element by "title" attribute.
|
75
|
+
* Work on progress: CSSPool::CSS::Declaration#expand_dimension
|
76
|
+
|
72
77
|
== Note
|
73
78
|
Note that
|
74
79
|
<link rel="stylesheet" href="...">
|
data/css_inliner.gemspec
CHANGED
@@ -17,12 +17,13 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
|
+
s.has_rdoc = 'yard'
|
20
21
|
|
21
22
|
s.add_runtime_dependency "nokogiri", '~> 1'
|
22
23
|
s.add_runtime_dependency "csspool", '~> 3'
|
23
24
|
s.add_runtime_dependency "bsearch"
|
24
25
|
|
25
|
-
s.add_development_dependency "test-unit"
|
26
|
+
s.add_development_dependency "test-unit-full"
|
26
27
|
s.add_development_dependency "cover_me", '~> 1'
|
27
28
|
s.add_development_dependency "yard"
|
28
29
|
s.add_development_dependency "pry"
|
data/lib/css_inliner/csspool.rb
CHANGED
@@ -33,10 +33,29 @@ module CSSPool
|
|
33
33
|
end
|
34
34
|
|
35
35
|
class Declaration
|
36
|
+
class InvalidExpressionCountError < StandardError; end
|
37
|
+
|
38
|
+
DIMENSIONS = %w[top right bottom left]
|
39
|
+
# property => property of [top, right, bottom, left]
|
40
|
+
PROPERTY_EXPANSION = {}
|
41
|
+
%w[margin padding].each do |prop|
|
42
|
+
PROPERTY_EXPANSION[prop] = DIMENSIONS.map {|dim| "#{prop}-#{dim}"}
|
43
|
+
end
|
44
|
+
%w[color style width].each do |subprop|
|
45
|
+
PROPERTY_EXPANSION["border-#{subprop}"] = DIMENSIONS.map {|dim| "border-#{dim}-#{subprop}"}
|
46
|
+
end
|
47
|
+
# count of expressions => index of [top, right, bottom, left]
|
48
|
+
EXPANSION_INDICES = {
|
49
|
+
1 => [0, 0, 0, 0],
|
50
|
+
2 => [0, 1, 0, 1],
|
51
|
+
3 => [0, 1, 2, 1],
|
52
|
+
4 => [0, 1, 2, 3]
|
53
|
+
}
|
54
|
+
|
36
55
|
# @param [Declaration] other
|
37
56
|
# @return [Declaration] self
|
38
57
|
def update(other)
|
39
|
-
raise ArgumentError
|
58
|
+
raise ArgumentError, 'different property' unless property == other.property
|
40
59
|
self.expressions = other.expressions if !important? or other.important?
|
41
60
|
self
|
42
61
|
end
|
@@ -47,6 +66,20 @@ module CSSPool
|
|
47
66
|
def merge(other)
|
48
67
|
dup.update other
|
49
68
|
end
|
69
|
+
|
70
|
+
# @return [Array<Declaration>] array of declaration indicating four dimensions
|
71
|
+
def expand_dimension
|
72
|
+
expanded_properties = PROPERTY_EXPANSION[property]
|
73
|
+
return [self] unless expanded_properties
|
74
|
+
|
75
|
+
expansion_map = EXPANSION_INDICES[expressions.length]
|
76
|
+
raise InvalidExpressionCountError, "has #{expressions.length} properties" unless expansion_map
|
77
|
+
|
78
|
+
expanded_properties.each.with_index.map {|prop, i|
|
79
|
+
expression = expressions[expansion_map[i]]
|
80
|
+
Declaration.new(prop, expression, important, rule_set)
|
81
|
+
}
|
82
|
+
end
|
50
83
|
end
|
51
84
|
end
|
52
85
|
|
@@ -45,8 +45,17 @@ module CSSInliner
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def extract_from_link(remove_link_element = true)
|
48
|
+
group = nil
|
48
49
|
@document.css('link').inject([]) do |sources, link|
|
49
50
|
next unless link['rel'] == 'stylesheet'
|
51
|
+
title = link['title']
|
52
|
+
if title
|
53
|
+
if group.nil?
|
54
|
+
group = title
|
55
|
+
elsif title != group
|
56
|
+
next sources
|
57
|
+
end
|
58
|
+
end
|
50
59
|
begin
|
51
60
|
# To do: detect file encoding before open it(read only @charset value)
|
52
61
|
open(File.join(basedir, link['href']), 'r:BOM|UTF-8') {|f| sources << f.read}
|
data/lib/css_inliner/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -0,0 +1,141 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TestCSSPool < CSSInlinerTestCase
|
4
|
+
def setup
|
5
|
+
super
|
6
|
+
|
7
|
+
@css = CSSPool.CSS <<EOC
|
8
|
+
p {
|
9
|
+
color: black;
|
10
|
+
}
|
11
|
+
p {
|
12
|
+
color: white;
|
13
|
+
}
|
14
|
+
p {
|
15
|
+
color: blue !important;
|
16
|
+
}
|
17
|
+
p {
|
18
|
+
color: red;
|
19
|
+
}
|
20
|
+
EOC
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_update_basic
|
24
|
+
base = @css.rule_sets.first.declarations.first
|
25
|
+
other = @css.rule_sets.last.declarations.first
|
26
|
+
base.update other
|
27
|
+
|
28
|
+
assert_equal 'color: red;', base.to_css.strip
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_udpate_with_important
|
32
|
+
base = @css.rule_sets[2].declarations.first
|
33
|
+
other = @css.rule_sets.first.declarations.first
|
34
|
+
base.update other
|
35
|
+
|
36
|
+
assert_equal 'color: blue !important;', base.to_css.strip
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_non_dimension_expandable
|
40
|
+
no_dimension = CSSPool.CSS <<EOC
|
41
|
+
p {
|
42
|
+
color: blue;
|
43
|
+
}
|
44
|
+
EOC
|
45
|
+
expected = CSSPool.CSS <<EOC
|
46
|
+
p {
|
47
|
+
color: blue;
|
48
|
+
}
|
49
|
+
EOC
|
50
|
+
|
51
|
+
assert_equal expected.rule_sets.first.declarations.to_s,
|
52
|
+
no_dimension.rule_sets.first.declarations.first.expand_dimension.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_expand_five_dimensions
|
56
|
+
five_dimensions = CSSPool.CSS <<EOC
|
57
|
+
p {
|
58
|
+
border-width: 1em 2em 3em 4em 5em;
|
59
|
+
}
|
60
|
+
EOC
|
61
|
+
assert_raise CSSPool::CSS::Declaration::InvalidExpressionCountError do
|
62
|
+
five_dimensions.rule_sets.first.declarations.first.expand_dimension
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_expand_four_dimensions
|
67
|
+
four_dimensions = CSSPool.CSS <<EOC
|
68
|
+
p {
|
69
|
+
margin: 1em 2em 3em 4em;
|
70
|
+
}
|
71
|
+
EOC
|
72
|
+
expected = CSSPool.CSS <<EOC
|
73
|
+
p {
|
74
|
+
margin-top: 1em;
|
75
|
+
margin-right: 2em;
|
76
|
+
margin-bottom: 3em;
|
77
|
+
margin-left: 4em;
|
78
|
+
}
|
79
|
+
EOC
|
80
|
+
|
81
|
+
assert_equal expected.rule_sets.first.declarations.to_s,
|
82
|
+
four_dimensions.rule_sets.first.declarations.first.expand_dimension.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_expand_three_dimensions
|
86
|
+
three_dimensions = CSSPool.CSS <<EOC
|
87
|
+
p {
|
88
|
+
padding: 1em 2em 3em;
|
89
|
+
}
|
90
|
+
EOC
|
91
|
+
expected = CSSPool.CSS <<EOC
|
92
|
+
p {
|
93
|
+
padding-top: 1em;
|
94
|
+
padding-right: 2em;
|
95
|
+
padding-bottom: 3em;
|
96
|
+
padding-left: 2em;
|
97
|
+
}
|
98
|
+
EOC
|
99
|
+
|
100
|
+
assert_equal expected.rule_sets.first.declarations.to_s,
|
101
|
+
three_dimensions.rule_sets.first.declarations.first.expand_dimension.to_s
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_expand_two_dimensions
|
105
|
+
two_dimensions = CSSPool.CSS <<EOC
|
106
|
+
p {
|
107
|
+
border-color: red blue;
|
108
|
+
}
|
109
|
+
EOC
|
110
|
+
expected = CSSPool.CSS <<EOC
|
111
|
+
p {
|
112
|
+
border-top-color: red;
|
113
|
+
border-right-color: blue;
|
114
|
+
border-bottom-color: red;
|
115
|
+
border-left-color: blue;
|
116
|
+
}
|
117
|
+
EOC
|
118
|
+
|
119
|
+
assert_equal expected.rule_sets.first.declarations.to_s,
|
120
|
+
two_dimensions.rule_sets.first.declarations.first.expand_dimension.to_s
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_expand_one_dimension
|
124
|
+
one_dimension = CSSPool.CSS <<EOC
|
125
|
+
p {
|
126
|
+
border-style: dotted;
|
127
|
+
}
|
128
|
+
EOC
|
129
|
+
expected = CSSPool.CSS <<EOC
|
130
|
+
p {
|
131
|
+
border-top-style: dotted;
|
132
|
+
border-right-style: dotted;
|
133
|
+
border-bottom-style: dotted;
|
134
|
+
border-left-style: dotted;
|
135
|
+
}
|
136
|
+
EOC
|
137
|
+
|
138
|
+
assert_equal expected.rule_sets.first.declarations.to_s,
|
139
|
+
one_dimension.rule_sets.first.declarations.first.expand_dimension.to_s
|
140
|
+
end
|
141
|
+
end
|
data/test/test_extractor.rb
CHANGED
@@ -50,6 +50,33 @@ class TestExtractor < CSSInlinerTestCase
|
|
50
50
|
assert_equal expected, @doc3.css('style').to_s
|
51
51
|
end
|
52
52
|
|
53
|
+
def test_extract_from_grouped_files
|
54
|
+
doc = Nokogiri.HTML <<EOH
|
55
|
+
<html>
|
56
|
+
<head>
|
57
|
+
<link rel="stylesheet" type="text/css" title="group1" href="group1-1.css">
|
58
|
+
<link rel="stylesheet" type="text/css" title="group1" href="group1-2.css">
|
59
|
+
<link rel="stylesheet" type="text/css" title="group2" href="group2-1.css">
|
60
|
+
</head>
|
61
|
+
<body>
|
62
|
+
</body>
|
63
|
+
</html>
|
64
|
+
EOH
|
65
|
+
expected = [
|
66
|
+
'#group1-1 {
|
67
|
+
color: black;
|
68
|
+
}
|
69
|
+
',
|
70
|
+
'#group1-2 {
|
71
|
+
color: black;
|
72
|
+
}
|
73
|
+
'
|
74
|
+
]
|
75
|
+
extractor = Extractor.new(doc, @sample1_dir)
|
76
|
+
|
77
|
+
assert_equal expected, extractor.extract_from_link
|
78
|
+
end
|
79
|
+
|
53
80
|
def test_integrate_basic
|
54
81
|
sources = [
|
55
82
|
'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: css_inliner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-28 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement: &
|
16
|
+
requirement: &12120420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *12120420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: csspool
|
27
|
-
requirement: &
|
27
|
+
requirement: &12135420 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *12135420
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bsearch
|
38
|
-
requirement: &
|
38
|
+
requirement: &12134440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,21 +43,21 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *12134440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name: test-unit
|
49
|
-
requirement: &
|
48
|
+
name: test-unit-full
|
49
|
+
requirement: &12142540 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *12142540
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: cover_me
|
60
|
-
requirement: &
|
60
|
+
requirement: &12139920 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '1'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *12139920
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: yard
|
71
|
-
requirement: &
|
71
|
+
requirement: &12136920 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *12136920
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: pry
|
82
|
-
requirement: &
|
82
|
+
requirement: &12981720 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *12981720
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: pry-doc
|
93
|
-
requirement: &
|
93
|
+
requirement: &12979580 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *12979580
|
102
102
|
description: ! "\n inline CSS from external file(s) and/or style elment(s) in head
|
103
103
|
element\n into style attibute of HTML elements\n "
|
104
104
|
email:
|
@@ -126,6 +126,9 @@ files:
|
|
126
126
|
- setup.rb
|
127
127
|
- test/fixtures/sample1/CSSInliner.html
|
128
128
|
- test/fixtures/sample1/created.rid
|
129
|
+
- test/fixtures/sample1/group1-1.css
|
130
|
+
- test/fixtures/sample1/group1-2.css
|
131
|
+
- test/fixtures/sample1/group2-1.css
|
129
132
|
- test/fixtures/sample1/images/brick.png
|
130
133
|
- test/fixtures/sample1/images/brick_link.png
|
131
134
|
- test/fixtures/sample1/images/bug.png
|
@@ -252,6 +255,7 @@ files:
|
|
252
255
|
- test/fixtures/sample4/rdoc.css
|
253
256
|
- test/helper.rb
|
254
257
|
- test/test_css_inliner.rb
|
258
|
+
- test/test_csspool.rb
|
255
259
|
- test/test_extractor.rb
|
256
260
|
homepage: http://gitorious.org/css_inliner
|
257
261
|
licenses: []
|
@@ -267,7 +271,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
267
271
|
version: '0'
|
268
272
|
segments:
|
269
273
|
- 0
|
270
|
-
hash:
|
274
|
+
hash: 4524569264957260771
|
271
275
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
272
276
|
none: false
|
273
277
|
requirements:
|
@@ -276,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
276
280
|
version: '0'
|
277
281
|
segments:
|
278
282
|
- 0
|
279
|
-
hash:
|
283
|
+
hash: 4524569264957260771
|
280
284
|
requirements: []
|
281
285
|
rubyforge_project:
|
282
286
|
rubygems_version: 1.8.8
|
@@ -284,4 +288,4 @@ signing_key:
|
|
284
288
|
specification_version: 3
|
285
289
|
summary: inline CSS into HTML attribute of elements
|
286
290
|
test_files: []
|
287
|
-
has_rdoc:
|
291
|
+
has_rdoc: yard
|