slimmer 3.21.0 → 3.22.0
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/lib/slimmer/processors/tag_mover.rb +7 -3
- data/lib/slimmer/version.rb +1 -1
- data/test/processors/tag_mover_test.rb +79 -0
- data/test/test_helper.rb +7 -5
- metadata +6 -4
@@ -1,7 +1,7 @@
|
|
1
1
|
module Slimmer::Processors
|
2
2
|
class TagMover
|
3
3
|
def filter(src,dest)
|
4
|
-
move_tags(src, dest, 'script', :dest_node => 'body', :
|
4
|
+
move_tags(src, dest, 'script', :dest_node => 'body', :keys => ['src', 'inner_html'])
|
5
5
|
move_tags(src, dest, 'link', :must_have => ['href'])
|
6
6
|
move_tags(src, dest, 'meta', :must_have => ['name', 'content'], :keys => ['name', 'content', 'http-equiv'])
|
7
7
|
end
|
@@ -12,7 +12,11 @@ module Slimmer::Processors
|
|
12
12
|
|
13
13
|
def tag_fingerprint(node, attrs)
|
14
14
|
attrs.collect do |attr_name|
|
15
|
-
|
15
|
+
if attr_name == 'inner_html'
|
16
|
+
node.content
|
17
|
+
else
|
18
|
+
node.has_attribute?(attr_name) ? node.attr(attr_name) : nil
|
19
|
+
end
|
16
20
|
end.compact.sort
|
17
21
|
end
|
18
22
|
|
@@ -25,7 +29,7 @@ module Slimmer::Processors
|
|
25
29
|
|
26
30
|
def move_tags(src, dest, type, opts)
|
27
31
|
comparison_attrs = opts[:keys] || opts[:must_have]
|
28
|
-
min_attrs = opts[:must_have]
|
32
|
+
min_attrs = opts[:must_have] || []
|
29
33
|
already_there = dest.css(type).map { |node|
|
30
34
|
tag_fingerprint(node, comparison_attrs)
|
31
35
|
}.compact
|
data/lib/slimmer/version.rb
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
|
3
|
+
class TagMoverTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
super
|
6
|
+
@source = as_nokogiri %{
|
7
|
+
<html>
|
8
|
+
<head>
|
9
|
+
<link rel="stylesheet" href="http://www.example.com/foo.css" />
|
10
|
+
<link rel="hrefless_link" />
|
11
|
+
<link rel="stylesheet" href="http://www.example.com/duplicate.css" />
|
12
|
+
<meta name="foo" content="bar" />
|
13
|
+
<meta name="no_content" />
|
14
|
+
<meta content="no_name" />
|
15
|
+
<meta name="duplicate" content="name and content" />
|
16
|
+
</head>
|
17
|
+
<body class="mainstream">
|
18
|
+
<div id="wrapper"></div>
|
19
|
+
<script src="http://www.example.com/foo.js"></script>
|
20
|
+
<script src="http://www.example.com/duplicate.js"></script>
|
21
|
+
</body>
|
22
|
+
</html>
|
23
|
+
}
|
24
|
+
@template = as_nokogiri %{
|
25
|
+
<html>
|
26
|
+
<head>
|
27
|
+
<link rel="stylesheet" href="http://www.example.com/duplicate.css" />
|
28
|
+
<meta name="duplicate" content="name and content" />
|
29
|
+
</head>
|
30
|
+
<body class="mainstream">
|
31
|
+
<div id="wrapper"></div>
|
32
|
+
<div id="related-items"></div>
|
33
|
+
<script src="http://www.example.com/duplicate.js"></script>
|
34
|
+
<script src="http://www.example.com/existing.js"></script>
|
35
|
+
</body>
|
36
|
+
</html>
|
37
|
+
}
|
38
|
+
Slimmer::Processors::TagMover.new.filter(@source, @template)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_move_script_tags_into_the_body
|
42
|
+
assert_in @template, "script[src='http://www.example.com/foo.js']", nil, "Should have moved the script tag with src 'http://www.example.com/foo.js'"
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_ignore_script_tags_already_in_the_destination_with_the_same_src_and_content
|
46
|
+
assert @template.css("script[src='http://www.example.com/duplicate.js']").length == 1, "Expected there to only be one script tag with src 'http://www.example.com/duplicate.js'"
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_place_source_script_tags_after_template_ones
|
50
|
+
assert @template.to_s.index("foo.js") > @template.to_s.index("existing.js"), "Expected foo.js to be after existing.js"
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def test_should_move_link_tags_into_the_head
|
55
|
+
assert_in @template, "link[href='http://www.example.com/foo.css']", nil, "Should have moved the link tag with href 'http://www.example.com/foo.css'"
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_should_ignore_link_tags_with_no_href
|
59
|
+
assert_not_in @template, "link[rel='hrefless_link']"
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_ignore_link_tags_already_in_the_destination_with_the_same_href
|
63
|
+
assert @template.css("link[href='http://www.example.com/duplicate.css']").length == 1, "Expected there to only be one link tag with href 'http://www.example.com/duplicate.css'"
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def test_should_move_meta_tags_into_the_head
|
68
|
+
assert_in @template, "meta[name='foo'][content='bar']", nil, "Should have moved the foo=bar meta tag"
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_should_ignore_meta_tags_with_no_name_or_content
|
72
|
+
assert_not_in @template, "meta[name='no_content']"
|
73
|
+
assert_not_in @template, "meta[content='no_name']"
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_should_ignore_meta_tags_already_in_the_destination_with_the_same_name_and_content
|
77
|
+
assert @template.css("meta[name='duplicate'][content='name and content']").length == 1, "Expected there to only be one duplicate=name and content meta tag."
|
78
|
+
end
|
79
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -107,15 +107,17 @@ class SlimmerIntegrationTest < MiniTest::Unit::TestCase
|
|
107
107
|
message = "Expected to find #{selector.inspect} in the output template"
|
108
108
|
end
|
109
109
|
end
|
110
|
-
|
111
|
-
|
110
|
+
|
111
|
+
matched_elements = Nokogiri::HTML.parse(last_response.body).css(selector)
|
112
|
+
assert matched_elements.length > 0, message + ", but selector not found."
|
113
|
+
|
112
114
|
if content
|
113
|
-
message << ". But found #{element.inner_html.to_s}"
|
114
115
|
if content.is_a?(Regexp)
|
115
|
-
|
116
|
+
found_match = matched_elements.inject(false) { |had_match, element| had_match || element.inner_html.match(content) }
|
116
117
|
else
|
117
|
-
|
118
|
+
found_match = matched_elements.inject(false) { |had_match, element| had_match || element.inner_html == content }
|
118
119
|
end
|
120
|
+
assert found_match, message + ". The selector was found but not with \"#{content}\"."
|
119
121
|
end
|
120
122
|
end
|
121
123
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slimmer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.22.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-10-
|
13
|
+
date: 2013-10-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: nokogiri
|
@@ -305,6 +305,7 @@ files:
|
|
305
305
|
- test/processors/campaign_notification_inserter_test.rb
|
306
306
|
- test/processors/section_inserter_test.rb
|
307
307
|
- test/processors/report_a_problem_inserter_test.rb
|
308
|
+
- test/processors/tag_mover_test.rb
|
308
309
|
- test/processors/logo_class_inserter_test.rb
|
309
310
|
- bin/render_slimmer_error
|
310
311
|
homepage: http://github.com/alphagov/slimmer
|
@@ -321,7 +322,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
321
322
|
version: '0'
|
322
323
|
segments:
|
323
324
|
- 0
|
324
|
-
hash:
|
325
|
+
hash: -1973635234500395245
|
325
326
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
326
327
|
none: false
|
327
328
|
requirements:
|
@@ -330,7 +331,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
330
331
|
version: '0'
|
331
332
|
segments:
|
332
333
|
- 0
|
333
|
-
hash:
|
334
|
+
hash: -1973635234500395245
|
334
335
|
requirements: []
|
335
336
|
rubyforge_project: slimmer
|
336
337
|
rubygems_version: 1.8.23
|
@@ -363,4 +364,5 @@ test_files:
|
|
363
364
|
- test/processors/campaign_notification_inserter_test.rb
|
364
365
|
- test/processors/section_inserter_test.rb
|
365
366
|
- test/processors/report_a_problem_inserter_test.rb
|
367
|
+
- test/processors/tag_mover_test.rb
|
366
368
|
- test/processors/logo_class_inserter_test.rb
|