docomo_css 0.0.1 → 0.0.2
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/README.rdoc +2 -19
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/docomo_css.gemspec +12 -8
- data/init.rb +1 -2
- data/lib/docomo_css.rb +78 -92
- data/rails/init.rb +2 -2
- data/test/actual.css +4 -0
- data/test/actual.html +13 -0
- data/test/docomo_css_test.rb +176 -28
- data/test/expected.html +27 -0
- metadata +8 -7
- data/install.rb +0 -1
- data/uninstall.rb +0 -1
data/README.rdoc
CHANGED
|
@@ -13,20 +13,9 @@ Instead, every element must use its own style attribute, for example
|
|
|
13
13
|
|
|
14
14
|
docomo_css works around this by inlining all CSS automatically for you.
|
|
15
15
|
|
|
16
|
-
Furthermore, DoCoMo phones have a number of limitations about how you can style
|
|
17
|
-
elements. For instance, the following will not be rendered properly.
|
|
18
|
-
|
|
19
|
-
<h1 style="color:red">Foo</h1>
|
|
20
|
-
|
|
21
|
-
However, this can be worked around by doing the following
|
|
22
|
-
|
|
23
|
-
<h1><span style="color:red">Foo</span></h1>
|
|
24
|
-
|
|
25
|
-
This library will automatically do this transformation for you if you apply css styles to unsupported elements like h1.
|
|
26
|
-
|
|
27
16
|
== Install
|
|
28
17
|
|
|
29
|
-
sudo gem install
|
|
18
|
+
sudo gem install docomo_css
|
|
30
19
|
|
|
31
20
|
== Usage
|
|
32
21
|
|
|
@@ -42,7 +31,6 @@ This library will automatically do this transformation for you if you apply css
|
|
|
42
31
|
<%= stylesheet_link_tag 'foo', :media => 'handheld, tty' %>
|
|
43
32
|
</head>
|
|
44
33
|
<body>
|
|
45
|
-
<h1>Header</h1>
|
|
46
34
|
<div class="title">bar</div>
|
|
47
35
|
</body>
|
|
48
36
|
</html>
|
|
@@ -51,10 +39,6 @@ This library will automatically do this transformation for you if you apply css
|
|
|
51
39
|
.title {
|
|
52
40
|
color: red;
|
|
53
41
|
}
|
|
54
|
-
|
|
55
|
-
h1 {
|
|
56
|
-
color:blue:
|
|
57
|
-
}
|
|
58
42
|
|
|
59
43
|
# result
|
|
60
44
|
<html>
|
|
@@ -62,7 +46,6 @@ This library will automatically do this transformation for you if you apply css
|
|
|
62
46
|
<%= stylesheet_link_tag 'foo', :media => 'handheld, tty' %>
|
|
63
47
|
</head>
|
|
64
48
|
<body>
|
|
65
|
-
<h1><span style="color:blue">Header</span></h1>
|
|
66
49
|
<div class="title" style="color:red">bar</div>
|
|
67
50
|
</body>
|
|
68
51
|
</html>
|
|
@@ -80,4 +63,4 @@ Copyright (c) 2008 milk1000cc, released under the MIT license
|
|
|
80
63
|
|
|
81
64
|
milk1000cc <mailto:info@milk1000.cc>
|
|
82
65
|
|
|
83
|
-
Gem
|
|
66
|
+
Gem and refactorings by Paul McMahon <mailto:paul@mobalean.com>
|
data/Rakefile
CHANGED
|
@@ -31,7 +31,7 @@ begin
|
|
|
31
31
|
s.homepage = "http://www.milk1000.cc/"
|
|
32
32
|
s.authors = ["milk1000cc", "Paul McMahon"]
|
|
33
33
|
s.add_dependency 'hpricot'
|
|
34
|
-
s.add_dependency '
|
|
34
|
+
s.add_dependency 'tiny_css'
|
|
35
35
|
end
|
|
36
36
|
rescue LoadError
|
|
37
37
|
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.0.
|
|
1
|
+
0.0.2
|
data/docomo_css.gemspec
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
|
1
4
|
# -*- encoding: utf-8 -*-
|
|
2
5
|
|
|
3
6
|
Gem::Specification.new do |s|
|
|
4
7
|
s.name = %q{docomo_css}
|
|
5
|
-
s.version = "0.0.
|
|
8
|
+
s.version = "0.0.2"
|
|
6
9
|
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
11
|
s.authors = ["milk1000cc", "Paul McMahon"]
|
|
9
|
-
s.date = %q{
|
|
12
|
+
s.date = %q{2010-09-15}
|
|
10
13
|
s.description = %q{Inlines CSS so that you can use external CSS with docomo handsets.}
|
|
11
14
|
s.email = %q{info@milk1000.cc}
|
|
12
15
|
s.extra_rdoc_files = [
|
|
@@ -20,17 +23,17 @@ Gem::Specification.new do |s|
|
|
|
20
23
|
"VERSION",
|
|
21
24
|
"docomo_css.gemspec",
|
|
22
25
|
"init.rb",
|
|
23
|
-
"install.rb",
|
|
24
26
|
"lib/docomo_css.rb",
|
|
25
27
|
"rails/init.rb",
|
|
28
|
+
"test/actual.css",
|
|
29
|
+
"test/actual.html",
|
|
26
30
|
"test/docomo_css_test.rb",
|
|
27
|
-
"
|
|
31
|
+
"test/expected.html"
|
|
28
32
|
]
|
|
29
|
-
s.has_rdoc = true
|
|
30
33
|
s.homepage = %q{http://www.milk1000.cc/}
|
|
31
34
|
s.rdoc_options = ["--charset=UTF-8"]
|
|
32
35
|
s.require_paths = ["lib"]
|
|
33
|
-
s.rubygems_version = %q{1.3.
|
|
36
|
+
s.rubygems_version = %q{1.3.7}
|
|
34
37
|
s.summary = %q{CSS inliner}
|
|
35
38
|
s.test_files = [
|
|
36
39
|
"test/docomo_css_test.rb"
|
|
@@ -38,9 +41,9 @@ Gem::Specification.new do |s|
|
|
|
38
41
|
|
|
39
42
|
if s.respond_to? :specification_version then
|
|
40
43
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
41
|
-
s.specification_version =
|
|
44
|
+
s.specification_version = 3
|
|
42
45
|
|
|
43
|
-
if Gem::Version.new(Gem::
|
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
44
47
|
s.add_runtime_dependency(%q<hpricot>, [">= 0"])
|
|
45
48
|
s.add_runtime_dependency(%q<tiny_css>, [">= 0"])
|
|
46
49
|
else
|
|
@@ -52,3 +55,4 @@ Gem::Specification.new do |s|
|
|
|
52
55
|
s.add_dependency(%q<tiny_css>, [">= 0"])
|
|
53
56
|
end
|
|
54
57
|
end
|
|
58
|
+
|
data/init.rb
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
require '
|
|
2
|
-
ActionController::Base.send :include, DocomoCss::Rails
|
|
1
|
+
require File.join File.dirname(__FILE__), 'rails', 'init'
|
data/lib/docomo_css.rb
CHANGED
|
@@ -1,130 +1,116 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'nokogiri'
|
|
2
2
|
require 'tiny_css'
|
|
3
3
|
|
|
4
4
|
module DocomoCss
|
|
5
5
|
|
|
6
|
-
def self.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
@handlers[k] = PseudoSelectorHandler.new(k)
|
|
14
|
-
end
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend ClassMethods
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
def docomo_filter
|
|
12
|
+
after_filter DocomoCssFilter.new
|
|
15
13
|
end
|
|
16
|
-
@handlers
|
|
17
14
|
end
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
class DocomoCssFilter
|
|
17
|
+
def after(controller)
|
|
18
|
+
return unless controller.response.content_type =~ /application\/xhtml\+xml/
|
|
19
|
+
return unless controller.request.user_agent =~ /docomo/i
|
|
20
|
+
return if docomo_2_0_browser?(controller)
|
|
21
|
+
body = escape_numeric_character_reference controller.response.body
|
|
22
|
+
body = embed_css remove_xml_declare(body)
|
|
23
|
+
controller.response.body = unescape_numeric_character_reference body
|
|
24
|
+
end
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
def embed_css(body)
|
|
27
|
+
doc = Nokogiri::HTML(body)
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
stylesheet_link_node(doc).each do |linknode|
|
|
30
|
+
href = linknode['href'] or next
|
|
31
|
+
|
|
32
|
+
next unless FileTest.exist? css_path(href)
|
|
33
|
+
css = TinyCss.new.read(css_path(href))
|
|
34
|
+
embed_pseudo_style(doc, extract_pseudo_style(css))
|
|
35
|
+
embed_style(doc, css)
|
|
31
36
|
end
|
|
37
|
+
xml_declare(doc) + doc.to_xhtml(:indent => 0)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def xml_declare(doc)
|
|
41
|
+
<<-XML
|
|
42
|
+
<?xml version="1.0" encoding="#{doc.encoding}"?>
|
|
43
|
+
XML
|
|
44
|
+
end
|
|
32
45
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
46
|
+
def remove_xml_declare(body)
|
|
47
|
+
body.gsub(%r'<\?xml[^\?]*?\?>', '')
|
|
48
|
+
end
|
|
36
49
|
|
|
37
|
-
|
|
50
|
+
def embed_style(doc, css)
|
|
38
51
|
css.style.each do |selector, style|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
(doc/('head')).append style
|
|
52
|
+
stringified_style = stringify_style(style)
|
|
53
|
+
doc.css(selector).each do |element|
|
|
54
|
+
element['style'] = merge_style element['style'], stringified_style
|
|
55
|
+
end
|
|
44
56
|
end
|
|
45
57
|
end
|
|
46
|
-
doc.search('//link[@docomo_css="remove_after_inline"]').remove
|
|
47
58
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
content
|
|
52
|
-
end
|
|
59
|
+
def stringify_style(style)
|
|
60
|
+
style.map { |k, v| "#{ k }:#{ v }" }.join ';'
|
|
61
|
+
end
|
|
53
62
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
def merge_style(style, other_style)
|
|
64
|
+
return other_style if style == nil
|
|
65
|
+
style += ";" unless style =~ /;\Z/
|
|
66
|
+
style + other_style
|
|
67
|
+
end
|
|
57
68
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
@selector = selector
|
|
69
|
+
def escape_numeric_character_reference(text)
|
|
70
|
+
text.gsub /&#(\d+);/, 'HTMLCSSINLINERESCAPE\1::::::::'
|
|
61
71
|
end
|
|
62
|
-
end
|
|
63
72
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
style_style.style[@selector] = style
|
|
73
|
+
def unescape_numeric_character_reference(text)
|
|
74
|
+
text.gsub /HTMLCSSINLINERESCAPE(\d+)::::::::/, '&#\1;'
|
|
67
75
|
end
|
|
68
|
-
end
|
|
69
76
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
super(selector)
|
|
73
|
-
@element_handler = element_handler
|
|
77
|
+
def stylesheet_link_node(document)
|
|
78
|
+
document.xpath '//link[@rel="stylesheet"]'
|
|
74
79
|
end
|
|
75
80
|
|
|
76
|
-
def
|
|
77
|
-
(
|
|
78
|
-
@element_handler.add_style(element, style)
|
|
79
|
-
end
|
|
81
|
+
def css_path(href)
|
|
82
|
+
File.join(Rails.root, 'public', href.gsub(/\?\d+/, ''))
|
|
80
83
|
end
|
|
81
|
-
end
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
def extract_pseudo_style(css)
|
|
86
|
+
pseudo_style = TinyCss.new
|
|
87
|
+
pseudo_selectors(css).each do |v|
|
|
88
|
+
pseudo_style.style[v] = css.style[v]
|
|
89
|
+
css.style.delete(v)
|
|
90
|
+
end
|
|
91
|
+
pseudo_style
|
|
87
92
|
end
|
|
88
93
|
|
|
89
|
-
def
|
|
90
|
-
if
|
|
91
|
-
unsupported = TinyCss::OrderedHash.new
|
|
92
|
-
supported = TinyCss::OrderedHash.new
|
|
93
|
-
style.each do |k,v|
|
|
94
|
-
s = @unsupported_styles.include?(k) ? unsupported : supported
|
|
95
|
-
s[k] = v
|
|
96
|
-
end
|
|
94
|
+
def embed_pseudo_style(doc, pseudo_style)
|
|
95
|
+
return if pseudo_style.style.keys.empty?
|
|
97
96
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
element.search("/").wrap("<span></span>")
|
|
106
|
-
wrapped_children = element.search("span")
|
|
107
|
-
end
|
|
108
|
-
wrapped_children.each do |c|
|
|
109
|
-
add_style(c, unsupported)
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
else
|
|
113
|
-
_add_style(element, style)
|
|
114
|
-
end
|
|
97
|
+
raise unless doc.at('/html/head')
|
|
98
|
+
doc.at('/html/head').add_child <<-STYLE
|
|
99
|
+
<style type="text/css">
|
|
100
|
+
#{pseudo_style.write_string}
|
|
101
|
+
</style>
|
|
102
|
+
STYLE
|
|
103
|
+
doc
|
|
115
104
|
end
|
|
116
105
|
|
|
117
|
-
def
|
|
118
|
-
|
|
119
|
-
style_attr = (!style_attr) ? stringify_style(style) :
|
|
120
|
-
[style_attr, stringify_style(style)].join(';')
|
|
121
|
-
element[:style] = style_attr
|
|
106
|
+
def pseudo_selectors(css)
|
|
107
|
+
css.style.keys.grep(/a:(link|focus|visited)/)
|
|
122
108
|
end
|
|
123
109
|
|
|
124
110
|
private
|
|
125
111
|
|
|
126
|
-
def
|
|
127
|
-
|
|
112
|
+
def docomo_2_0_browser?(controller)
|
|
113
|
+
controller.request.user_agent =~ /DoCoMo\/2\.0 [^(]*\(c(\d+);/ && $1.to_i >= 500
|
|
128
114
|
end
|
|
129
115
|
end
|
|
130
116
|
end
|
data/rails/init.rb
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require 'docomo_css
|
|
2
|
-
ActionController::Base.send :include, DocomoCss
|
|
1
|
+
require 'docomo_css'
|
|
2
|
+
ActionController::Base.send :include, DocomoCss
|
data/test/actual.css
ADDED
data/test/actual.html
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="Shift_JIS"?>
|
|
2
|
+
<!DOCTYPE html PUBLIC "-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.3) 1.0//EN" "i-xhtml_4ja_10.dtd">
|
|
3
|
+
<html>
|
|
4
|
+
<head>
|
|
5
|
+
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=Shift_JIS" />
|
|
6
|
+
<link rel="stylesheet" href="/actual.css" />
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div class="content">
|
|
10
|
+
<a href="/">TOP</a>
|
|
11
|
+
</div>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
data/test/docomo_css_test.rb
CHANGED
|
@@ -1,46 +1,194 @@
|
|
|
1
1
|
require 'test/unit'
|
|
2
2
|
require 'rubygems'
|
|
3
|
-
|
|
4
|
-
require 'docomo_css'
|
|
3
|
+
require 'mocha'
|
|
4
|
+
require File.join File.dirname(__FILE__), '..', 'lib', 'docomo_css'
|
|
5
|
+
|
|
6
|
+
Rails = Mocha::Mockery.instance.unnamed_mock
|
|
7
|
+
Rails.stubs(:root => File.dirname(__FILE__))
|
|
5
8
|
|
|
6
9
|
class DocomoCssTest < Test::Unit::TestCase
|
|
7
|
-
def
|
|
8
|
-
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
def setup
|
|
11
|
+
@filter = DocomoCss::DocomoCssFilter.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_invalid_response_content_type
|
|
15
|
+
response = mock("invalid_response") do
|
|
16
|
+
expects(:content_type).returns('text/html').once
|
|
17
|
+
expects(:body).never
|
|
18
|
+
end
|
|
19
|
+
controller = stub("invalid_controller", :response => response)
|
|
20
|
+
@filter.after(controller)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_escape_numeric_character_reference
|
|
24
|
+
assert_equal "HTMLCSSINLINERESCAPE123456789::::::::", @filter.escape_numeric_character_reference("�")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_unescape_numeric_character_reference
|
|
28
|
+
assert_equal "�", @filter.unescape_numeric_character_reference("HTMLCSSINLINERESCAPE123456789::::::::")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_pseudo_selectors
|
|
32
|
+
css = TinyCss.new.read_string(<<-CSS)
|
|
33
|
+
a:visited { color: FF00FF; }
|
|
34
|
+
CSS
|
|
35
|
+
assert_equal ["a:visited"], @filter.pseudo_selectors(css)
|
|
36
|
+
|
|
37
|
+
css = TinyCss.new.read_string(<<-CSS)
|
|
38
|
+
.purple { color: FF00FF; }
|
|
39
|
+
CSS
|
|
40
|
+
assert_equal [], @filter.pseudo_selectors(css)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_stylesheet_link_node
|
|
44
|
+
doc = Nokogiri::HTML(<<-HTML)
|
|
45
|
+
<link href="a.css"/>
|
|
46
|
+
<link href="b.css" rel="stylesheet"/>
|
|
47
|
+
HTML
|
|
48
|
+
|
|
49
|
+
@filter.stylesheet_link_node(doc).each do |node|
|
|
50
|
+
assert_equal 'b.css', node['href']
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_css_path
|
|
55
|
+
href = "/stylesheets/all.css?1274411517"
|
|
56
|
+
assert_equal "#{Rails.root}/public/stylesheets/all.css", @filter.css_path(href)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_extract_pseudo_style
|
|
60
|
+
css = TinyCss.new.read_string <<-CSS
|
|
61
|
+
a:link { color: red; }
|
|
62
|
+
a:focus { color: green; }
|
|
63
|
+
a:visited { color: blue; }
|
|
64
|
+
div.title { background-color: #999 }
|
|
65
|
+
CSS
|
|
66
|
+
|
|
67
|
+
pseudo_style = @filter.extract_pseudo_style css
|
|
68
|
+
assert_equal('red', pseudo_style.style['a:link']['color'])
|
|
69
|
+
assert_equal('green', pseudo_style.style['a:focus']['color'])
|
|
70
|
+
assert_equal('blue', pseudo_style.style['a:visited']['color'])
|
|
71
|
+
assert_equal('#999', css.style['div.title']['background-color'])
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_embed_pseudo_style
|
|
75
|
+
css = TinyCss.new
|
|
76
|
+
assert_equal nil, @filter.embed_pseudo_style(nil, css)
|
|
77
|
+
|
|
78
|
+
css = TinyCss.new.read_string <<-CSS
|
|
79
|
+
a:link { color: red; }
|
|
80
|
+
a:focus { color: green; }
|
|
81
|
+
a:visited { color: blue; }
|
|
82
|
+
CSS
|
|
83
|
+
|
|
84
|
+
doc = Nokogiri::HTML <<-HTML
|
|
85
|
+
<html>
|
|
86
|
+
<head></head>
|
|
87
|
+
<body></body>
|
|
88
|
+
</html>
|
|
89
|
+
HTML
|
|
90
|
+
doc = @filter.embed_pseudo_style doc, css
|
|
91
|
+
assert_match %r'<style .*?/style>'m, doc.to_xhtml
|
|
92
|
+
|
|
93
|
+
doc = Nokogiri::HTML <<-HTML
|
|
94
|
+
<html>
|
|
95
|
+
<body></body>
|
|
96
|
+
</html>
|
|
97
|
+
HTML
|
|
98
|
+
assert_raise RuntimeError do
|
|
99
|
+
@filter.embed_pseudo_style doc, css
|
|
22
100
|
end
|
|
23
101
|
end
|
|
24
102
|
|
|
25
|
-
def
|
|
26
|
-
|
|
103
|
+
def test_embed_style
|
|
104
|
+
css = TinyCss.new.read_string <<-CSS
|
|
105
|
+
.title { color: red; }
|
|
106
|
+
CSS
|
|
107
|
+
|
|
108
|
+
doc = Nokogiri::HTML <<-HTML
|
|
109
|
+
<html>
|
|
110
|
+
<body>
|
|
111
|
+
<div class="title">bar</div>
|
|
112
|
+
</body>
|
|
113
|
+
</html>
|
|
114
|
+
HTML
|
|
115
|
+
@filter.embed_style doc, css
|
|
116
|
+
assert_match %r'style="color:red"', doc.to_xhtml
|
|
117
|
+
|
|
118
|
+
doc = Nokogiri::HTML <<-HTML
|
|
119
|
+
<html>
|
|
120
|
+
<body>
|
|
121
|
+
<div class="title" style="background-color:black">bar</div>
|
|
122
|
+
</body>
|
|
123
|
+
</html>
|
|
124
|
+
HTML
|
|
125
|
+
@filter.embed_style doc, css
|
|
126
|
+
assert_match %r'style="background-color:black;color:red"', doc.to_xhtml
|
|
127
|
+
|
|
128
|
+
doc = Nokogiri::HTML <<-HTML
|
|
129
|
+
<html>
|
|
130
|
+
<body>
|
|
131
|
+
<div class="title" style="background-color:silver;">bar</div>
|
|
132
|
+
</body>
|
|
133
|
+
</html>
|
|
134
|
+
HTML
|
|
135
|
+
@filter.embed_style doc, css
|
|
136
|
+
assert_match %r'style="background-color:silver;color:red"', doc.to_xhtml
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def test_xml_declare
|
|
140
|
+
doc = stub("doc", :encoding => "Shift_JIS")
|
|
141
|
+
assert_equal <<-XML, @filter.xml_declare(doc)
|
|
142
|
+
<?xml version="1.0" encoding="Shift_JIS"?>
|
|
143
|
+
XML
|
|
144
|
+
|
|
145
|
+
doc = stub("doc", :encoding => "UTF-8")
|
|
146
|
+
assert_equal <<-XML, @filter.xml_declare(doc)
|
|
147
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
148
|
+
XML
|
|
27
149
|
end
|
|
28
150
|
|
|
29
|
-
def
|
|
30
|
-
|
|
151
|
+
def test_remove_xml_declare
|
|
152
|
+
assert_equal '', @filter.remove_xml_declare('<?xml version="1.0"?>')
|
|
153
|
+
assert_equal '', @filter.remove_xml_declare('<?xml encoding="Shift_JIS"?>')
|
|
154
|
+
assert_equal '', @filter.remove_xml_declare('<?xml version="1.0" encoding="Shift_JIS" ?>')
|
|
155
|
+
assert_equal '', @filter.remove_xml_declare('<?xml?>')
|
|
31
156
|
end
|
|
32
157
|
|
|
33
|
-
def
|
|
34
|
-
|
|
158
|
+
def test_output_with_docomo_1_0_browser
|
|
159
|
+
request = stub('request', :user_agent => 'DoCoMo/2.0 D905i(c100;TB;W24H17)')
|
|
160
|
+
response = stub("response") do
|
|
161
|
+
expects(:content_type).returns('application/xhtml+xml')
|
|
162
|
+
expects(:body).returns(File.open(File.join(File.dirname(__FILE__), 'actual.html'), 'rb'){ |f| f.read })
|
|
163
|
+
expects(:body=).with(File.open(File.join(File.dirname(__FILE__), 'expected.html'), 'rb'){ |f| f.read })
|
|
164
|
+
end
|
|
165
|
+
controller = stub("controller", :response => response, :request => request)
|
|
166
|
+
@filter.stubs(:css_path).returns(File.join(File.dirname(__FILE__), 'actual.css'))
|
|
167
|
+
|
|
168
|
+
@filter.after(controller)
|
|
35
169
|
end
|
|
36
170
|
|
|
37
|
-
def
|
|
38
|
-
|
|
39
|
-
|
|
171
|
+
def test_output_with_docomo_1_0_browser_and_utf8_charset
|
|
172
|
+
request = stub('request', :user_agent => 'DoCoMo/2.0 D905i(c100;TB;W24H17)')
|
|
173
|
+
response = stub("response") do
|
|
174
|
+
expects(:content_type).returns('application/xhtml+xml; charset=utf-8')
|
|
175
|
+
expects(:body).returns(File.open(File.join(File.dirname(__FILE__), 'actual.html'), 'rb'){ |f| f.read })
|
|
176
|
+
expects(:body=).with(File.open(File.join(File.dirname(__FILE__), 'expected.html'), 'rb'){ |f| f.read })
|
|
40
177
|
end
|
|
178
|
+
controller = stub("controller", :response => response, :request => request)
|
|
179
|
+
@filter.stubs(:css_path).returns(File.join(File.dirname(__FILE__), 'actual.css'))
|
|
180
|
+
|
|
181
|
+
@filter.after(controller)
|
|
41
182
|
end
|
|
42
183
|
|
|
43
|
-
def
|
|
44
|
-
|
|
184
|
+
def test_output_with_docomo_2_0_browser
|
|
185
|
+
request = stub('request', :user_agent => 'DoCoMo/2.0 N03B(c500;TB;W24H16)')
|
|
186
|
+
response = stub("response") do
|
|
187
|
+
expects(:content_type).returns('application/xhtml+xml')
|
|
188
|
+
expects(:body).never
|
|
189
|
+
end
|
|
190
|
+
controller = stub("controller", :response => response, :request => request)
|
|
191
|
+
|
|
192
|
+
@filter.after(controller)
|
|
45
193
|
end
|
|
46
194
|
end
|
data/test/expected.html
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="Shift_JIS"?>
|
|
2
|
+
<!DOCTYPE html PUBLIC "-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.3) 1.0//EN" "i-xhtml_4ja_10.dtd">
|
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
4
|
+
<head>
|
|
5
|
+
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=Shift_JIS" />
|
|
6
|
+
<link rel="stylesheet" href="/actual.css" />
|
|
7
|
+
<style type="text/css">
|
|
8
|
+
<![CDATA[
|
|
9
|
+
a:visited {
|
|
10
|
+
color: blue;
|
|
11
|
+
}
|
|
12
|
+
a:link {
|
|
13
|
+
color: red;
|
|
14
|
+
}
|
|
15
|
+
a:focus {
|
|
16
|
+
color: green;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
]]>
|
|
20
|
+
</style>
|
|
21
|
+
</head>
|
|
22
|
+
<body>
|
|
23
|
+
<div class="content" style="background-color:#999">
|
|
24
|
+
<a href="/">TOP</a>
|
|
25
|
+
</div>
|
|
26
|
+
</body>
|
|
27
|
+
</html>
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: docomo_css
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 27
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
version: 0.0.
|
|
9
|
+
- 2
|
|
10
|
+
version: 0.0.2
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- milk1000cc
|
|
@@ -16,7 +16,7 @@ autorequire:
|
|
|
16
16
|
bindir: bin
|
|
17
17
|
cert_chain: []
|
|
18
18
|
|
|
19
|
-
date:
|
|
19
|
+
date: 2010-09-15 00:00:00 +09:00
|
|
20
20
|
default_executable:
|
|
21
21
|
dependencies:
|
|
22
22
|
- !ruby/object:Gem::Dependency
|
|
@@ -63,11 +63,12 @@ files:
|
|
|
63
63
|
- VERSION
|
|
64
64
|
- docomo_css.gemspec
|
|
65
65
|
- init.rb
|
|
66
|
-
- install.rb
|
|
67
66
|
- lib/docomo_css.rb
|
|
68
67
|
- rails/init.rb
|
|
68
|
+
- test/actual.css
|
|
69
|
+
- test/actual.html
|
|
69
70
|
- test/docomo_css_test.rb
|
|
70
|
-
-
|
|
71
|
+
- test/expected.html
|
|
71
72
|
has_rdoc: true
|
|
72
73
|
homepage: http://www.milk1000.cc/
|
|
73
74
|
licenses: []
|
|
@@ -100,7 +101,7 @@ requirements: []
|
|
|
100
101
|
rubyforge_project:
|
|
101
102
|
rubygems_version: 1.3.7
|
|
102
103
|
signing_key:
|
|
103
|
-
specification_version:
|
|
104
|
+
specification_version: 3
|
|
104
105
|
summary: CSS inliner
|
|
105
106
|
test_files:
|
|
106
107
|
- test/docomo_css_test.rb
|
data/install.rb
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# Install hook code here
|
data/uninstall.rb
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# Uninstall hook code here
|