slimmer 13.1.0 → 13.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/lib/slimmer/app.rb +19 -17
- data/lib/slimmer/headers.rb +14 -13
- data/lib/slimmer/processors/body_inserter.rb +2 -2
- data/lib/slimmer/processors/conditional_comment_mover.rb +0 -2
- data/lib/slimmer/processors/footer_remover.rb +1 -1
- data/lib/slimmer/processors/header_context_inserter.rb +3 -3
- data/lib/slimmer/processors/metadata_inserter.rb +1 -1
- data/lib/slimmer/processors/navigation_mover.rb +0 -1
- data/lib/slimmer/processors/search_parameter_inserter.rb +1 -1
- data/lib/slimmer/processors/search_path_setter.rb +1 -1
- data/lib/slimmer/processors/search_remover.rb +1 -1
- data/lib/slimmer/processors/tag_mover.rb +6 -4
- data/lib/slimmer/processors/title_inserter.rb +2 -2
- data/lib/slimmer/skin.rb +11 -11
- data/lib/slimmer/version.rb +1 -1
- data/lib/tasks/slimmer.rake +1 -1
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 966481420a36ac4cbaf811706d2ae142b9ce1556c6b8be10ca682766cbe8e2e6
|
4
|
+
data.tar.gz: 79cb54e5f8d0db751cbc40ff3acd6c573395fec2e13e4bb9e43021b93c69a3a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd9b2d065c5cc805cc92f84728fb4990352030ac25ad5f8ad024b5794e90bc52b82c04f08eb1a7adfbd45abef17849e2ae977d95f579f3d184d290af5a9a4d3b
|
7
|
+
data.tar.gz: d846dd0e2ce3edcb9503708e0c162e5899530e988abf6513a785bef995cc2193df948718892e3200d4408e3ee79b34aead7a9ccec4af042b6214a98f480444b7
|
data/CHANGELOG.md
CHANGED
data/lib/slimmer/app.rb
CHANGED
@@ -4,20 +4,21 @@ module Slimmer
|
|
4
4
|
class App
|
5
5
|
attr_accessor :logger
|
6
6
|
|
7
|
-
def initialize(app, *args
|
7
|
+
def initialize(app, *args)
|
8
8
|
options = args.first || {}
|
9
9
|
@app = app
|
10
10
|
|
11
11
|
logger = options[:logger] || NullLogger.instance
|
12
12
|
self.logger = logger
|
13
13
|
begin
|
14
|
-
if logger.level
|
14
|
+
if logger.level.zero? # Log set to debug level
|
15
15
|
unless options[:enable_debugging]
|
16
16
|
self.logger = logger.dup
|
17
17
|
self.logger.level = 1 # info
|
18
18
|
end
|
19
19
|
end
|
20
|
-
rescue NoMethodError #
|
20
|
+
rescue NoMethodError # rubocop:disable Lint/HandleExceptions
|
21
|
+
# In case logger doesn't respond_to? :level
|
21
22
|
end
|
22
23
|
|
23
24
|
if options.key? :template_path
|
@@ -60,7 +61,7 @@ module Slimmer
|
|
60
61
|
|
61
62
|
def skip_slimmer_param?(env)
|
62
63
|
skip = Rack::Request.new(env).params['skip_slimmer']
|
63
|
-
skip
|
64
|
+
skip && skip.to_i.positive?
|
64
65
|
end
|
65
66
|
|
66
67
|
def skip_slimmer_header?(response)
|
@@ -69,8 +70,9 @@ module Slimmer
|
|
69
70
|
|
70
71
|
def s(body)
|
71
72
|
return body.to_s unless body.respond_to?(:each)
|
73
|
+
|
72
74
|
b = ""
|
73
|
-
body.each {|a| b << a }
|
75
|
+
body.each { |a| b << a }
|
74
76
|
b
|
75
77
|
end
|
76
78
|
|
@@ -87,17 +89,17 @@ module Slimmer
|
|
87
89
|
GovukRequestId.value = env['HTTP_GOVUK_REQUEST_ID']
|
88
90
|
|
89
91
|
rewritten_body = case response.status
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
92
|
+
when 200
|
93
|
+
@skin.success request, response, s(response.body)
|
94
|
+
when 403
|
95
|
+
@skin.error '403', s(response.body), request.env
|
96
|
+
when 404
|
97
|
+
@skin.error '404', s(response.body), request.env
|
98
|
+
when 410
|
99
|
+
@skin.error '410', s(response.body), request.env
|
100
|
+
else
|
101
|
+
@skin.error '500', s(response.body), request.env
|
102
|
+
end
|
101
103
|
|
102
104
|
rewritten_body = [rewritten_body] unless rewritten_body.respond_to?(:each)
|
103
105
|
response.body = rewritten_body
|
@@ -107,7 +109,7 @@ module Slimmer
|
|
107
109
|
end
|
108
110
|
|
109
111
|
def strip_slimmer_headers(headers)
|
110
|
-
headers.reject {|k,
|
112
|
+
headers.reject { |k, _v| k =~ /\A#{Headers::HEADER_PREFIX}/ }
|
111
113
|
end
|
112
114
|
end
|
113
115
|
end
|
data/lib/slimmer/headers.rb
CHANGED
@@ -5,7 +5,7 @@ module Slimmer
|
|
5
5
|
InvalidHeader = Class.new(RuntimeError)
|
6
6
|
|
7
7
|
# @private
|
8
|
-
HEADER_PREFIX = "X-Slimmer"
|
8
|
+
HEADER_PREFIX = "X-Slimmer".freeze
|
9
9
|
|
10
10
|
# @private
|
11
11
|
SLIMMER_HEADER_MAPPING = {
|
@@ -20,40 +20,40 @@ module Slimmer
|
|
20
20
|
skip: "Skip",
|
21
21
|
template: "Template",
|
22
22
|
remove_search: "Remove-Search",
|
23
|
-
}
|
23
|
+
}.freeze
|
24
24
|
|
25
25
|
# @private
|
26
|
-
APPLICATION_NAME_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:application_name]}"
|
26
|
+
APPLICATION_NAME_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:application_name]}".freeze
|
27
27
|
|
28
28
|
# @private
|
29
|
-
FORMAT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}"
|
29
|
+
FORMAT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}".freeze
|
30
30
|
|
31
31
|
# @private
|
32
|
-
ORGANISATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:organisations]}"
|
32
|
+
ORGANISATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:organisations]}".freeze
|
33
33
|
|
34
34
|
# @private
|
35
|
-
WORLD_LOCATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:world_locations]}"
|
35
|
+
WORLD_LOCATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:world_locations]}".freeze
|
36
36
|
|
37
37
|
# @private
|
38
|
-
PAGE_OWNER_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:page_owner]}"
|
38
|
+
PAGE_OWNER_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:page_owner]}".freeze
|
39
39
|
|
40
40
|
# @private
|
41
|
-
RESULT_COUNT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:result_count]}"
|
41
|
+
RESULT_COUNT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:result_count]}".freeze
|
42
42
|
|
43
43
|
# @private
|
44
|
-
SEARCH_PATH_HEADER = "#{HEADER_PREFIX}-Search-Path"
|
44
|
+
SEARCH_PATH_HEADER = "#{HEADER_PREFIX}-Search-Path".freeze
|
45
45
|
|
46
46
|
# @private
|
47
|
-
SEARCH_PARAMETERS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:search_parameters]}"
|
47
|
+
SEARCH_PARAMETERS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:search_parameters]}".freeze
|
48
48
|
|
49
49
|
# @private
|
50
|
-
SKIP_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:skip]}"
|
50
|
+
SKIP_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:skip]}".freeze
|
51
51
|
|
52
52
|
# @private
|
53
|
-
TEMPLATE_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:template]}"
|
53
|
+
TEMPLATE_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:template]}".freeze
|
54
54
|
|
55
55
|
# @private
|
56
|
-
REMOVE_SEARCH_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_search]}"
|
56
|
+
REMOVE_SEARCH_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_search]}".freeze
|
57
57
|
|
58
58
|
# Set the "slimmer headers" to configure the page
|
59
59
|
#
|
@@ -71,6 +71,7 @@ module Slimmer
|
|
71
71
|
# @option hash [String] world_locations
|
72
72
|
def set_slimmer_headers(hash)
|
73
73
|
raise InvalidHeader if (hash.keys - SLIMMER_HEADER_MAPPING.keys).any?
|
74
|
+
|
74
75
|
SLIMMER_HEADER_MAPPING.each do |hash_key, header_suffix|
|
75
76
|
value = hash[hash_key]
|
76
77
|
headers["#{HEADER_PREFIX}-#{header_suffix}"] = value.to_s if value
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Slimmer::Processors
|
2
2
|
class BodyInserter
|
3
|
-
def initialize(source_id='wrapper', destination_id='wrapper')
|
3
|
+
def initialize(source_id = 'wrapper', destination_id = 'wrapper')
|
4
4
|
@source_selector = '#' + source_id
|
5
5
|
@destination_selector = '#' + destination_id
|
6
6
|
end
|
7
7
|
|
8
|
-
def filter(src,dest)
|
8
|
+
def filter(src, dest)
|
9
9
|
body = Nokogiri::HTML.fragment(src.at_css(@source_selector).to_html)
|
10
10
|
dest.at_css(@destination_selector).replace(body)
|
11
11
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Slimmer::Processors
|
2
2
|
class HeaderContextInserter
|
3
|
-
def initialize(path='.header-context')
|
3
|
+
def initialize(path = '.header-context')
|
4
4
|
@path = path
|
5
5
|
end
|
6
6
|
|
7
|
-
def filter(src,dest)
|
8
|
-
if dest.at_css(@path) && replacement = src.at_css(@path)
|
7
|
+
def filter(src, dest)
|
8
|
+
if dest.at_css(@path) && (replacement = src.at_css(@path))
|
9
9
|
header_context = src.fragment(replacement)
|
10
10
|
dest.at_css(@path).replace(header_context)
|
11
11
|
end
|
@@ -6,7 +6,7 @@ module Slimmer::Processors
|
|
6
6
|
@response = response
|
7
7
|
end
|
8
8
|
|
9
|
-
def filter(
|
9
|
+
def filter(_content_document, page_template)
|
10
10
|
search_form = page_template.at_css('form#search')
|
11
11
|
if search_parameters && search_form
|
12
12
|
search_parameters.each_pair do |name, value|
|
@@ -4,7 +4,7 @@ module Slimmer::Processors
|
|
4
4
|
@response = response
|
5
5
|
end
|
6
6
|
|
7
|
-
def filter(
|
7
|
+
def filter(_content_document, page_template)
|
8
8
|
if search_scope && page_template.at_css('form#search')
|
9
9
|
page_template.at_css('form#search').attributes["action"].value = search_scope
|
10
10
|
end
|
@@ -2,8 +2,8 @@ module Slimmer::Processors
|
|
2
2
|
class TagMover
|
3
3
|
def filter(src, dest)
|
4
4
|
move_tags(src, dest, 'script', dest_node: 'body', keys: %w(src inner_html))
|
5
|
-
move_tags(src, dest, 'link', must_have: [
|
6
|
-
move_tags(src, dest, 'meta', must_have: %w(name content), keys: [
|
5
|
+
move_tags(src, dest, 'link', must_have: %w[href])
|
6
|
+
move_tags(src, dest, 'meta', must_have: %w(name content), keys: %w[name content http-equiv], insertion_location: :top)
|
7
7
|
move_tags(src, dest, 'meta', must_have: %w(property content), keys: %w(property content), insertion_location: :top)
|
8
8
|
end
|
9
9
|
|
@@ -12,13 +12,15 @@ module Slimmer::Processors
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def tag_fingerprint(node, attrs)
|
15
|
-
attrs.collect do |attr_name|
|
15
|
+
collected_attrs = attrs.collect do |attr_name|
|
16
16
|
if attr_name == 'inner_html'
|
17
17
|
node.content
|
18
18
|
else
|
19
19
|
node.has_attribute?(attr_name) ? node.attr(attr_name) : nil
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
22
|
+
|
23
|
+
collected_attrs.compact.sort
|
22
24
|
end
|
23
25
|
|
24
26
|
def wrap_node(src, node)
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Slimmer::Processors
|
2
2
|
class TitleInserter
|
3
|
-
def filter(src,dest)
|
3
|
+
def filter(src, dest)
|
4
4
|
title = src.at_css('head title')
|
5
5
|
head = dest.at_xpath('/html/head')
|
6
6
|
if head && title
|
7
|
-
insert_title(title,head)
|
7
|
+
insert_title(title, head)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
data/lib/slimmer/skin.rb
CHANGED
@@ -10,7 +10,7 @@ module Slimmer
|
|
10
10
|
@asset_host = options[:asset_host]
|
11
11
|
|
12
12
|
@logger = options[:logger] || NullLogger.instance
|
13
|
-
@strict = options[:strict] ||
|
13
|
+
@strict = options[:strict] || %w{development test}.include?(ENV['RACK_ENV'])
|
14
14
|
end
|
15
15
|
|
16
16
|
def template(template_name)
|
@@ -34,15 +34,15 @@ module Slimmer
|
|
34
34
|
"#{host}templates/#{template_name}.html.erb"
|
35
35
|
end
|
36
36
|
|
37
|
-
def report_parse_errors_if_strict!(nokogiri_doc,
|
37
|
+
def report_parse_errors_if_strict!(nokogiri_doc, _description_for_error_message)
|
38
38
|
nokogiri_doc
|
39
39
|
end
|
40
40
|
|
41
41
|
def parse_html(html, description_for_error_message)
|
42
42
|
doc = Nokogiri::HTML.parse(html)
|
43
43
|
if strict
|
44
|
-
errors = doc.errors.select
|
45
|
-
if errors.
|
44
|
+
errors = doc.errors.select(&:error?).reject { |e| ignorable?(e) }
|
45
|
+
if !errors.empty?
|
46
46
|
error = errors.first
|
47
47
|
message = "In #{description_for_error_message}: '#{error.message}' at line #{error.line} col #{error.column} (code #{error.code}).\n"
|
48
48
|
message << "Add ?skip_slimmer=1 to the url to show the raw backend request.\n\n"
|
@@ -59,7 +59,7 @@ module Slimmer
|
|
59
59
|
lines = [""] + html.split("\n")
|
60
60
|
from = [1, error.line - context_size].max
|
61
61
|
to = [lines.size - 1, error.line + context_size].min
|
62
|
-
context = (from..to).zip(lines[from..to]).map {|lineno, line| "%4d: %s" % [lineno, line] }
|
62
|
+
context = (from..to).zip(lines[from..to]).map { |lineno, line| "%4d: %s" % [lineno, line] } # rubocop:disable Style/FormatStringToken
|
63
63
|
marker = " " * (error.column - 1) + "-----v"
|
64
64
|
context.insert(context_size, marker)
|
65
65
|
context.join("\n")
|
@@ -70,7 +70,7 @@ module Slimmer
|
|
70
70
|
ignorable_codes.include?(error.code) || error.message.match(/Element script embeds close tag/) || error.message.match(/Unexpected end tag : noscript/)
|
71
71
|
end
|
72
72
|
|
73
|
-
def process(processors,body,template,
|
73
|
+
def process(processors, body, template, _rack_env)
|
74
74
|
logger.debug "Slimmer: starting skinning process"
|
75
75
|
src = parse_html(body.to_s, "backend response")
|
76
76
|
dest = parse_html(template, "template")
|
@@ -95,14 +95,14 @@ module Slimmer
|
|
95
95
|
wrapper_id = options[:wrapper_id] || 'wrapper'
|
96
96
|
|
97
97
|
processors = [
|
98
|
-
Processors::TitleInserter.new
|
99
|
-
Processors::TagMover.new
|
98
|
+
Processors::TitleInserter.new,
|
99
|
+
Processors::TagMover.new,
|
100
100
|
Processors::NavigationMover.new(self),
|
101
|
-
Processors::ConditionalCommentMover.new
|
101
|
+
Processors::ConditionalCommentMover.new,
|
102
102
|
Processors::BodyInserter.new(wrapper_id),
|
103
103
|
Processors::BodyClassCopier.new,
|
104
104
|
Processors::InsideHeaderInserter.new,
|
105
|
-
Processors::HeaderContextInserter.new
|
105
|
+
Processors::HeaderContextInserter.new,
|
106
106
|
Processors::MetadataInserter.new(response, options[:app_name]),
|
107
107
|
Processors::SearchParameterInserter.new(response),
|
108
108
|
Processors::SearchPathSetter.new(response),
|
@@ -115,7 +115,7 @@ module Slimmer
|
|
115
115
|
|
116
116
|
def error(template_name, body, rack_env)
|
117
117
|
processors = [
|
118
|
-
Processors::TitleInserter.new
|
118
|
+
Processors::TitleInserter.new
|
119
119
|
]
|
120
120
|
process(processors, body, template(template_name), rack_env)
|
121
121
|
end
|
data/lib/slimmer/version.rb
CHANGED
data/lib/tasks/slimmer.rake
CHANGED
@@ -6,7 +6,7 @@ namespace :slimmer do
|
|
6
6
|
path_to_static = "../static/public"
|
7
7
|
path_to_public = "public"
|
8
8
|
commands = ["cd #{path_to_public}"]
|
9
|
-
|
9
|
+
Dir.glob("../static/public/*") { |f|
|
10
10
|
commands << "ln -s #{path_to_static}/#{f}"
|
11
11
|
}
|
12
12
|
commands << ["cd .."]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slimmer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 13.
|
4
|
+
version: 13.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -184,14 +184,14 @@ dependencies:
|
|
184
184
|
requirements:
|
185
185
|
- - '='
|
186
186
|
- !ruby/object:Gem::Version
|
187
|
-
version: 3.
|
187
|
+
version: 3.5.0
|
188
188
|
type: :development
|
189
189
|
prerelease: false
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
192
|
- - '='
|
193
193
|
- !ruby/object:Gem::Version
|
194
|
-
version: 3.
|
194
|
+
version: 3.5.0
|
195
195
|
- !ruby/object:Gem::Dependency
|
196
196
|
name: timecop
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -212,14 +212,14 @@ dependencies:
|
|
212
212
|
requirements:
|
213
213
|
- - "~>"
|
214
214
|
- !ruby/object:Gem::Version
|
215
|
-
version: 3.
|
215
|
+
version: 3.11.5
|
216
216
|
type: :development
|
217
217
|
prerelease: false
|
218
218
|
version_requirements: !ruby/object:Gem::Requirement
|
219
219
|
requirements:
|
220
220
|
- - "~>"
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
version: 3.
|
222
|
+
version: 3.11.5
|
223
223
|
description: Rack middleware for skinning pages using a specific template
|
224
224
|
email:
|
225
225
|
- govuk-dev@digital.cabinet-office.gov.uk
|
@@ -281,8 +281,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
281
281
|
- !ruby/object:Gem::Version
|
282
282
|
version: '0'
|
283
283
|
requirements: []
|
284
|
-
|
285
|
-
rubygems_version: 2.7.6
|
284
|
+
rubygems_version: 3.0.3
|
286
285
|
signing_key:
|
287
286
|
specification_version: 4
|
288
287
|
summary: Thinner than the skinner
|