slimmer 15.1.0 → 15.4.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c70c70db54b35b2b0d9300478fc396a4052d948363c0763ebafe73dd44d06392
4
- data.tar.gz: 651cf3eb861931f9ffe3a64591b9c914f1af49cfcd94bfd10e5a05c2eda1d897
3
+ metadata.gz: 9198a64c1829a19f7f0a89e3b0e7ae4b504b728cac13f935398343e962e1d63c
4
+ data.tar.gz: 6baeb15e0d17b8f3b3f9bd51b8ebe3a324a7c7511372d37d0f8713ef4785c518
5
5
  SHA512:
6
- metadata.gz: 3fb17efde462d25955b73cf2b6938bfad1dc1784d6432ca424c3c3d1e084bf8b3864f85c829f99f20720ef2401b58d6af671562cee4a414af840dc77dce6823b
7
- data.tar.gz: 6c2588cfd1ead6255634e198189ce6b02e03c75ffc3e7199f0a4981abf96e77853c225e2ed0aea18a60990a6a75ba9865e1035932f1fa2cc369da1ce12b3fc9e
6
+ metadata.gz: 11ec9e71961fd062c4a7b3378134fc090f9e574ea487093d81b4eb8a3b0a6a31e38633c93cedd9efdde3848971cc429aea0666bb01048edcac1d6587a54689b5
7
+ data.tar.gz: e412a52f54e6d1eee23769a6aa0d6b9a3a3bec0b6147f349cde5f448886c70fb283db165d5794d4dae8068ec4aff84c086d0474c511a54bef1a8fff730df57ca
data/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ # 15.4.1
2
+
3
+ * Add support for layout header component in static (#263)
4
+
5
+ # 15.4.0
6
+
7
+ * Hoist `<async>` and `<defer>` JS Script tags to `<head>`. (#261)
8
+
9
+ # 15.3.0
10
+
11
+ * Introduce separate, internal exception class for intermittent
12
+ template retrieval errors.
13
+
14
+ # 15.2.0
15
+
16
+ * Add X-Slimmer-Show-Accounts header to choose between accounts
17
+ header components. (#255)
18
+ * Update to Ruby 2.7.2. (#254)
19
+
20
+ # 15.1.1
21
+
22
+ * Amend toggle button selector (#251)
23
+
1
24
  # 15.1.0
2
25
 
3
26
  * Add `<base>` tag into the `<head>`.
data/lib/slimmer.rb CHANGED
@@ -28,15 +28,14 @@ module Slimmer
28
28
  autoload :HTTPClient, "slimmer/http_client"
29
29
 
30
30
  module Processors
31
+ autoload :AccountsShower, "slimmer/processors/accounts_shower"
31
32
  autoload :BodyClassCopier, "slimmer/processors/body_class_copier"
32
33
  autoload :BodyInserter, "slimmer/processors/body_inserter"
33
34
  autoload :ConditionalCommentMover, "slimmer/processors/conditional_comment_mover"
34
- autoload :FooterRemover, "slimmer/processors/footer_remover"
35
35
  autoload :MetadataInserter, "slimmer/processors/metadata_inserter"
36
36
  autoload :HeaderContextInserter, "slimmer/processors/header_context_inserter"
37
37
  autoload :InsideHeaderInserter, "slimmer/processors/inside_header_inserter"
38
38
  autoload :NavigationMover, "slimmer/processors/navigation_mover"
39
- autoload :SearchIndexSetter, "slimmer/processors/search_index_setter"
40
39
  autoload :SearchPathSetter, "slimmer/processors/search_path_setter"
41
40
  autoload :SearchParameterInserter, "slimmer/processors/search_parameter_inserter"
42
41
  autoload :SearchRemover, "slimmer/processors/search_remover"
@@ -44,6 +43,7 @@ module Slimmer
44
43
  autoload :TitleInserter, "slimmer/processors/title_inserter"
45
44
  end
46
45
 
47
- class TemplateNotFoundException < StandardError; end
48
46
  class CouldNotRetrieveTemplate < StandardError; end
47
+ class TemplateNotFoundException < CouldNotRetrieveTemplate; end
48
+ class IntermittentRetrievalError < CouldNotRetrieveTemplate; end
49
49
  end
@@ -20,6 +20,7 @@ module Slimmer
20
20
  skip: "Skip",
21
21
  template: "Template",
22
22
  remove_search: "Remove-Search",
23
+ show_accounts: "Show-Accounts",
23
24
  }.freeze
24
25
 
25
26
  # @private
@@ -55,6 +56,9 @@ module Slimmer
55
56
  # @private
56
57
  REMOVE_SEARCH_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_search]}".freeze
57
58
 
59
+ # @private
60
+ SHOW_ACCOUNTS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:show_accounts]}".freeze
61
+
58
62
  # Set the "slimmer headers" to configure the page
59
63
  #
60
64
  # @param hash [Hash] the options
@@ -66,6 +70,7 @@ module Slimmer
66
70
  # @option hash [String] result_count
67
71
  # @option hash [String] search_parameters
68
72
  # @option hash [String] section
73
+ # @option hash [String] show_accounts
69
74
  # @option hash [String] skip
70
75
  # @option hash [String] template
71
76
  # @option hash [String] world_locations
@@ -0,0 +1,47 @@
1
+ module Slimmer::Processors
2
+ class AccountsShower
3
+ def initialize(headers)
4
+ @headers = headers
5
+ end
6
+
7
+ def filter(_src, dest)
8
+ header_value = @headers[Slimmer::Headers::SHOW_ACCOUNTS_HEADER]
9
+ layout_header = dest.at_css(".gem-c-layout-header")
10
+ static_header = dest.at_css("#global-header")
11
+ if header_value && layout_header
12
+ static_header.remove if static_header
13
+ elsif !header_value
14
+ layout_header.remove if layout_header
15
+ end
16
+
17
+ if header_value == "signed-in"
18
+ remove_signed_out(dest)
19
+ elsif header_value == "signed-out"
20
+ remove_signed_in(dest)
21
+ else
22
+ remove_signed_out(dest)
23
+ remove_signed_in(dest)
24
+ end
25
+ end
26
+
27
+ def remove_signed_out(dest)
28
+ signed_out = dest.at_css("#global-header #accounts-signed-out")
29
+ signed_out_link = dest.css(".gem-c-layout-header [data-link-for='accounts-signed-out']")
30
+
31
+ signed_out.remove if signed_out
32
+ signed_out_link.each do |link|
33
+ link.parent.remove
34
+ end
35
+ end
36
+
37
+ def remove_signed_in(dest)
38
+ signed_in = dest.at_css("#global-header #accounts-signed-in")
39
+ signed_in_link = dest.css(".gem-c-layout-header [data-link-for='accounts-signed-in']")
40
+
41
+ signed_in.remove if signed_in
42
+ signed_in_link.each do |link|
43
+ link.parent.remove
44
+ end
45
+ end
46
+ end
47
+ end
@@ -9,7 +9,7 @@ module Slimmer::Processors
9
9
  search = dest.at_css("#global-header #search")
10
10
  search.remove if search
11
11
 
12
- search_link = dest.at_css("#global-header a[href='#search']")
12
+ search_link = dest.at_css("#global-header .search-toggle")
13
13
  search_link.remove if search_link
14
14
  end
15
15
  end
@@ -1,11 +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])
5
4
  move_tags(src, dest, "link", must_have: %w[href])
6
5
  move_tags(src, dest, "meta", must_have: %w[name content], keys: %w[name content http-equiv], insertion_location: :top)
7
6
  move_tags(src, dest, "meta", must_have: %w[property content], keys: %w[property content], insertion_location: :top)
8
7
  move_tags(src, dest, "base", must_have: %w[href])
8
+ move_tags(src, dest, "script", keys: %w[src inner_html], head_if_attributes: %w[async defer])
9
9
  end
10
10
 
11
11
  def include_tag?(node, min_attrs)
@@ -31,21 +31,34 @@ module Slimmer::Processors
31
31
  node
32
32
  end
33
33
 
34
+ def head_or_body(node, head_if_attributes)
35
+ if head_if_attributes.any? { |attribute| node.has_attribute?(attribute) }
36
+ "head"
37
+ else
38
+ "body"
39
+ end
40
+ end
41
+
34
42
  def move_tags(src, dest, type, opts)
35
43
  comparison_attrs = opts[:keys] || opts[:must_have]
36
44
  min_attrs = opts[:must_have] || []
45
+ head_if_attributes = opts[:head_if_attributes] || []
46
+ dest_node = "head"
37
47
  already_there = dest.css(type).map { |node|
38
48
  tag_fingerprint(node, comparison_attrs)
39
49
  }.compact
40
- dest_node = opts[:dest_node] || "head"
41
50
 
42
51
  src.css(type).each do |node|
43
52
  next unless include_tag?(node, min_attrs) && !already_there.include?(tag_fingerprint(node, comparison_attrs))
44
53
 
45
54
  node = wrap_node(src, node)
55
+ if head_if_attributes.any?
56
+ dest_node = head_or_body(node, head_if_attributes)
57
+ insert_at_top = true if dest_node == "head"
58
+ end
46
59
  node.remove
47
60
 
48
- if opts[:insertion_location] == :top
61
+ if opts[:insertion_location] == :top || insert_at_top
49
62
  dest.at_xpath("/html/#{dest_node}").prepend_child(node)
50
63
  else
51
64
  dest.at_xpath("/html/#{dest_node}") << node
data/lib/slimmer/skin.rb CHANGED
@@ -22,10 +22,18 @@ module Slimmer
22
22
  def load_template(template_name)
23
23
  url = template_url(template_name)
24
24
  HTTPClient.get(url)
25
- rescue RestClient::Exception => e
26
- raise TemplateNotFoundException, "Unable to fetch: '#{template_name}' from '#{url}' because #{e}", caller
27
- rescue Errno::ECONNREFUSED, SocketError, OpenSSL::SSL::SSLError => e
28
- raise CouldNotRetrieveTemplate, "Unable to fetch: '#{template_name}' from '#{url}' because #{e}", caller
25
+ rescue Errno::ECONNREFUSED, SocketError, OpenSSL::SSL::SSLError, RestClient::Exception => e
26
+ message = "Unable to fetch: '#{template_name}' from '#{url}' because #{e}"
27
+
28
+ if e.is_a?(RestClient::Exception) && e.http_code == 404
29
+ raise TemplateNotFoundException, message, caller
30
+ end
31
+
32
+ if e.is_a?(RestClient::Exception) && [502, 503, 504].include?(e.http_code)
33
+ raise IntermittentRetrievalError, message, caller
34
+ end
35
+
36
+ raise CouldNotRetrieveTemplate, message, caller
29
37
  end
30
38
 
31
39
  def template_url(template_name)
@@ -107,6 +115,7 @@ module Slimmer
107
115
  Processors::SearchParameterInserter.new(response),
108
116
  Processors::SearchPathSetter.new(response),
109
117
  Processors::SearchRemover.new(response.headers),
118
+ Processors::AccountsShower.new(response.headers),
110
119
  ]
111
120
 
112
121
  template_name = response.headers[Headers::TEMPLATE_HEADER] || "core_layout"
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = "15.1.0".freeze
2
+ VERSION = "15.4.1".freeze
3
3
  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: 15.1.0
4
+ version: 15.4.1
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-08-17 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -252,10 +252,10 @@ files:
252
252
  - lib/slimmer/govuk_request_id.rb
253
253
  - lib/slimmer/headers.rb
254
254
  - lib/slimmer/http_client.rb
255
+ - lib/slimmer/processors/accounts_shower.rb
255
256
  - lib/slimmer/processors/body_class_copier.rb
256
257
  - lib/slimmer/processors/body_inserter.rb
257
258
  - lib/slimmer/processors/conditional_comment_mover.rb
258
- - lib/slimmer/processors/footer_remover.rb
259
259
  - lib/slimmer/processors/header_context_inserter.rb
260
260
  - lib/slimmer/processors/inside_header_inserter.rb
261
261
  - lib/slimmer/processors/metadata_inserter.rb
@@ -294,7 +294,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
294
294
  - !ruby/object:Gem::Version
295
295
  version: '0'
296
296
  requirements: []
297
- rubygems_version: 3.0.3
297
+ rubygems_version: 3.1.4
298
298
  signing_key:
299
299
  specification_version: 4
300
300
  summary: Thinner than the skinner
@@ -1,8 +0,0 @@
1
- module Slimmer::Processors
2
- class FooterRemover
3
- def filter(src, _dest)
4
- footer = src.at_css("#footer")
5
- footer.remove if footer
6
- end
7
- end
8
- end