slimmer 13.2.2 → 15.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dad0beacc561e5407f29aaeabea18a46087d12b5bc22b28fa9bffbc4c17c0fd
4
- data.tar.gz: 11bc7fc2a2e3d9add7485fd327751be25aa1b744ed5e90dfde7bd6f0a58991bf
3
+ metadata.gz: c70c70db54b35b2b0d9300478fc396a4052d948363c0763ebafe73dd44d06392
4
+ data.tar.gz: 651cf3eb861931f9ffe3a64591b9c914f1af49cfcd94bfd10e5a05c2eda1d897
5
5
  SHA512:
6
- metadata.gz: 13594f752cdecb7288c867c063d83237a1a6aee22720bee8b6cdc233c377a3c7c7bb4664591a51962b82c2f8d72325038621230270d1e37c4d58b3b46464fb81
7
- data.tar.gz: f03a441b1f847173daf557889a5acc52fae82f5a7763cb03bb4baa53160443a52666fc457d7e2e108907f0a3f16552d33a1a4e2bbe4106effad350d4ed81c356
6
+ metadata.gz: 3fb17efde462d25955b73cf2b6938bfad1dc1784d6432ca424c3c3d1e084bf8b3864f85c829f99f20720ef2401b58d6af671562cee4a414af840dc77dce6823b
7
+ data.tar.gz: 6c2588cfd1ead6255634e198189ce6b02e03c75ffc3e7199f0a4981abf96e77853c225e2ed0aea18a60990a6a75ba9865e1035932f1fa2cc369da1ce12b3fc9e
@@ -1,3 +1,24 @@
1
+ # 15.1.0
2
+
3
+ * Add `<base>` tag into the `<head>`.
4
+
5
+ # 15.0.0
6
+
7
+ * BREAKING: Test templates no longer embed links to production JavaScript files,
8
+ apps should provide their own dependencies.
9
+
10
+ # 14.0.0
11
+
12
+ * Remove handling of non-200 responses (#245)
13
+
14
+ # 13.4.0
15
+
16
+ * Revert X-Slimmer-Ignore-Error header (#246)
17
+
18
+ # 13.3.0
19
+
20
+ * Add X-Slimmer-Ignore-Error header flag to prevent overriding non-200 status responses
21
+
1
22
  # 13.2.2
2
23
 
3
24
  * Fix bug where parent_name was called without app
data/README.md CHANGED
@@ -96,3 +96,7 @@ Add the following code to spec/spec_helper:
96
96
  ```rb
97
97
  require 'slimmer/rspec'
98
98
  ```
99
+
100
+ ## Licence
101
+
102
+ [MIT License](LICENCE)
data/Rakefile CHANGED
@@ -1,8 +1,9 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
1
  require "bundler/gem_tasks"
4
2
  require "rdoc/task"
5
3
  require "rake/testtask"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
6
7
 
7
8
  Dir.glob("lib/tasks/*.rake").each { |r| import r }
8
9
 
@@ -18,4 +19,4 @@ Rake::TestTask.new("test") do |t|
18
19
  t.verbose = true
19
20
  end
20
21
 
21
- task default: %i[test]
22
+ task default: %i[test rubocop]
@@ -35,7 +35,7 @@ module Slimmer
35
35
  if response_can_be_rewritten?(status, headers)
36
36
  response = Rack::Response.new(body, status, headers)
37
37
 
38
- if !skip_slimmer?(env, response)
38
+ unless skip_slimmer?(env, response)
39
39
  status, headers, body = rewrite_response(env, response)
40
40
  end
41
41
  end
@@ -61,7 +61,7 @@ module Slimmer
61
61
  end
62
62
 
63
63
  def skip_slimmer_header?(response)
64
- !!response.headers[Headers::SKIP_HEADER]
64
+ response.headers.key?(Headers::SKIP_HEADER)
65
65
  end
66
66
 
67
67
  def s(body)
@@ -80,24 +80,14 @@ module Slimmer
80
80
 
81
81
  def rewrite_response(env, response)
82
82
  request = Rack::Request.new(env)
83
+ return response.finish unless response.status == 200
83
84
 
84
85
  # Store the request id so it can be passed on with any template requests
85
86
  GovukRequestId.value = env["HTTP_GOVUK_REQUEST_ID"]
86
87
 
87
- rewritten_body = case response.status
88
- when 200
89
- @skin.success request, response, s(response.body)
90
- when 403
91
- @skin.error "403", s(response.body), request.env
92
- when 404
93
- @skin.error "404", s(response.body), request.env
94
- when 410
95
- @skin.error "410", s(response.body), request.env
96
- else
97
- @skin.error "500", s(response.body), request.env
98
- end
99
-
88
+ rewritten_body = @skin.success request, response, s(response.body)
100
89
  rewritten_body = [rewritten_body] unless rewritten_body.respond_to?(:each)
90
+
101
91
  response.body = rewritten_body
102
92
  response.headers["Content-Length"] = content_length(response.body)
103
93
 
@@ -9,17 +9,17 @@ module Slimmer
9
9
 
10
10
  # @private
11
11
  SLIMMER_HEADER_MAPPING = {
12
- application_name: "Application-Name",
13
- format: "Format",
14
- page_owner: "Page-Owner",
15
- organisations: "Organisations",
16
- world_locations: "World-Locations",
17
- result_count: "Result-Count",
18
- search_parameters: "Search-Parameters",
19
- section: "Section",
20
- skip: "Skip",
21
- template: "Template",
22
- remove_search: "Remove-Search",
12
+ application_name: "Application-Name",
13
+ format: "Format",
14
+ page_owner: "Page-Owner",
15
+ organisations: "Organisations",
16
+ world_locations: "World-Locations",
17
+ result_count: "Result-Count",
18
+ search_parameters: "Search-Parameters",
19
+ section: "Section",
20
+ skip: "Skip",
21
+ template: "Template",
22
+ remove_search: "Remove-Search",
23
23
  }.freeze
24
24
 
25
25
  # @private
@@ -1,10 +1,11 @@
1
1
  module Slimmer::Processors
2
2
  class TagMover
3
3
  def filter(src, dest)
4
- move_tags(src, dest, "script", dest_node: "body", keys: %w(src inner_html))
4
+ move_tags(src, dest, "script", dest_node: "body", keys: %w[src inner_html])
5
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
- move_tags(src, dest, "meta", must_have: %w(property content), keys: %w(property content), insertion_location: :top)
6
+ move_tags(src, dest, "meta", must_have: %w[name content], keys: %w[name content http-equiv], insertion_location: :top)
7
+ move_tags(src, dest, "meta", must_have: %w[property content], keys: %w[property content], insertion_location: :top)
8
+ move_tags(src, dest, "base", must_have: %w[href])
8
9
  end
9
10
 
10
11
  def include_tag?(node, min_attrs)
@@ -39,15 +40,15 @@ module Slimmer::Processors
39
40
  dest_node = opts[:dest_node] || "head"
40
41
 
41
42
  src.css(type).each do |node|
42
- if include_tag?(node, min_attrs) && !already_there.include?(tag_fingerprint(node, comparison_attrs))
43
- node = wrap_node(src, node)
44
- node.remove
45
-
46
- if opts[:insertion_location] == :top
47
- dest.at_xpath("/html/#{dest_node}").prepend_child(node)
48
- else
49
- dest.at_xpath("/html/#{dest_node}") << node
50
- end
43
+ next unless include_tag?(node, min_attrs) && !already_there.include?(tag_fingerprint(node, comparison_attrs))
44
+
45
+ node = wrap_node(src, node)
46
+ node.remove
47
+
48
+ if opts[:insertion_location] == :top
49
+ dest.at_xpath("/html/#{dest_node}").prepend_child(node)
50
+ else
51
+ dest.at_xpath("/html/#{dest_node}") << node
51
52
  end
52
53
  end
53
54
  end
@@ -5,12 +5,12 @@ module Slimmer
5
5
  class Skin
6
6
  attr_accessor :asset_host, :logger, :strict, :options
7
7
 
8
- def initialize options = {}
8
+ def initialize(options = {})
9
9
  @options = options
10
10
  @asset_host = options[:asset_host]
11
11
 
12
12
  @logger = options[:logger] || NullLogger.instance
13
- @strict = options[:strict] || %w{development test}.include?(ENV["RACK_ENV"])
13
+ @strict = options[:strict] || %w[development test].include?(ENV["RACK_ENV"])
14
14
  end
15
15
 
16
16
  def template(template_name)
@@ -42,7 +42,7 @@ module Slimmer
42
42
  doc = Nokogiri::HTML.parse(html)
43
43
  if strict
44
44
  errors = doc.errors.select(&:error?).reject { |e| ignorable?(e) }
45
- if !errors.empty?
45
+ unless 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| sprintf("%4d: %s", lineno, line) }
63
63
  marker = " " * (error.column - 1) + "-----v"
64
64
  context.insert(context_size, marker)
65
65
  context.join("\n")
@@ -112,12 +112,5 @@ module Slimmer
112
112
  template_name = response.headers[Headers::TEMPLATE_HEADER] || "core_layout"
113
113
  process(processors, body, template(template_name), source_request.env)
114
114
  end
115
-
116
- def error(template_name, body, rack_env)
117
- processors = [
118
- Processors::TitleInserter.new,
119
- ]
120
- process(processors, body, template(template_name), rack_env)
121
- end
122
115
  end
123
116
  end
@@ -1,16 +1,15 @@
1
1
  module Slimmer
2
2
  module Template
3
- def self.included into
3
+ def self.included(into)
4
4
  into.extend ClassMethods
5
5
  end
6
6
 
7
- def slimmer_template template_name
7
+ def slimmer_template(template_name)
8
8
  response.headers[Slimmer::Headers::TEMPLATE_HEADER] = template_name.to_s
9
9
  end
10
10
 
11
-
12
11
  module ClassMethods
13
- def slimmer_template template_name
12
+ def slimmer_template(template_name)
14
13
  after_action do
15
14
  response.headers[Slimmer::Headers::TEMPLATE_HEADER] ||= template_name.to_s
16
15
  end
@@ -2,10 +2,10 @@ require "slimmer/skin"
2
2
 
3
3
  module Slimmer
4
4
  class Skin
5
- def load_template name
5
+ def load_template(name)
6
6
  logger.debug "Slimmer: TEST MODE - Loading fixture template from #{__FILE__}"
7
7
  if name =~ /\A(.*)\.raw\z/
8
- %{<div id="test-#{$1}"></div>}
8
+ %(<div id="test-#{Regexp.last_match(1)}"></div>)
9
9
  elsif File.exist?(template_path = File.join(File.dirname(__FILE__), "test_templates", "#{name}.html"))
10
10
  File.read(template_path)
11
11
  else
@@ -19,9 +19,5 @@
19
19
  <div id="wrapper"></div>
20
20
 
21
21
  <footer id="footer"></footer>
22
-
23
- <script src="https://assets.publishing.service.gov.uk/static/govuk-template.js" type="text/javascript"></script>
24
- <script src="https://assets.publishing.service.gov.uk/static/libs/jquery/jquery-1.7.2.js" type="text/javascript"></script>
25
- <script src="https://assets.publishing.service.gov.uk/static/header-footer-only.js" type="text/javascript"></script>
26
22
  </body>
27
23
  </html>
@@ -19,9 +19,5 @@
19
19
  <div id="wrapper"></div>
20
20
 
21
21
  <footer id="footer"></footer>
22
-
23
- <script src="https://assets.publishing.service.gov.uk/static/govuk-template.js" type="text/javascript"></script>
24
- <script src="https://assets.publishing.service.gov.uk/static/libs/jquery/jquery-1.7.2.js" type="text/javascript"></script>
25
- <script src="https://assets.publishing.service.gov.uk/static/application.js" type="text/javascript"></script>
26
22
  </body>
27
23
  </html>
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = "13.2.2".freeze
2
+ VERSION = "15.1.0".freeze
3
3
  end
@@ -6,9 +6,9 @@ namespace :slimmer do
6
6
  path_to_static = "../static/public"
7
7
  path_to_public = "public"
8
8
  commands = ["cd #{path_to_public}"]
9
- Dir.glob("../static/public/*") { |f|
9
+ Dir.glob("../static/public/*") do |f|
10
10
  commands << "ln -s #{path_to_static}/#{f}"
11
- }
11
+ end
12
12
  commands << ["cd .."]
13
13
  run commands.join(" && ")
14
14
  end
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.2.2
4
+ version: 15.1.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: 2020-03-04 00:00:00.000000000 Z
11
+ date: 2020-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -184,14 +184,14 @@ dependencies:
184
184
  requirements:
185
185
  - - "~>"
186
186
  - !ruby/object:Gem::Version
187
- version: 2.0.0
187
+ version: '3'
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: 2.0.0
194
+ version: '3'
195
195
  - !ruby/object:Gem::Dependency
196
196
  name: timecop
197
197
  requirement: !ruby/object:Gem::Requirement