html-attributes-utils 0.9.2 → 1.0.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: 3808ce542e4f9fd7160573fe17ff2ebc2e8c918235f1f2d901b1f14f29dbfa5a
4
- data.tar.gz: 7481b962ddd46b11916f19a09d7bd83dd9837d0a79bf81e122b25a15b2c205b8
3
+ metadata.gz: 55226e6e32681583a11fd45f4ea8752e41c74a173088c2250875a4ef003a702e
4
+ data.tar.gz: afeb51d41d803e0ce1e6042b6518aa456ef4d48264559133f6f55afcbbfd3e73
5
5
  SHA512:
6
- metadata.gz: 7135311cae1f633bc62f6f833ab2aa78356390ee44d323388d223b273daa1ce4c4ba72ef3acadeae31d64f68ba275db40560e006deb066f6ec1b3ab197d4da0d
7
- data.tar.gz: 29b428ff663d2bf89cac66ec754f8dd4a01232c9af07ad7e9f6b1a74bcdc1b58ea140d7b7cce883848373332106108556681eb773d9d76b7cc1b5274492b4e9c
6
+ metadata.gz: e351c56554bc35d7c2436dcdd0067f4c6d3397bb8b12069a416df034bad94a4ac16ad4040994cc3334292288efc3bae71504875e838d126b1771a3c53237c003
7
+ data.tar.gz: f3f0244ae2e300083f393aff3f9b0f39ae398e505068ee16577bec095c10eff2b0c08695ab26432854b75e6cde2fc1f9aecb3a70ec27bbae83c1e3fe8bbd226d
data/README.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # HTML Attributes Utilities
2
2
 
3
+ ![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/dfe-digital/html-attributes-utils/Tests/main)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/da15b0accce3baf10c34/maintainability)](https://codeclimate.com/github/DFE-Digital/html-attributes-utils/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/da15b0accce3baf10c34/test_coverage)](https://codeclimate.com/github/DFE-Digital/html-attributes-utils/test_coverage)
6
+ ![GitHub](https://img.shields.io/github/license/dfe-digital/html-attributes-utils)
7
+ ![Supported ruby versions](https://img.shields.io/badge/ruby-%3E%3D%202.7.6-red)
8
+ ![Supported rails versions](https://img.shields.io/badge/rails-%3E%3D%206.1.6-red)
9
+
10
+
3
11
  This is a small library intended to make it easier to deal with HTML
4
12
  attributes. It was written to reduce overlap in the
5
13
  [govuk-components](https://github.com/DFE-Digital/govuk-components) and
@@ -16,6 +16,7 @@ module HTMLAttributesUtils
16
16
  %i(aria flowto),
17
17
  %i(aria labelledby),
18
18
  %i(aria owns),
19
+ %i(rel),
19
20
  ].freeze
20
21
 
21
22
  refine Hash do
@@ -47,12 +48,7 @@ module HTMLAttributesUtils
47
48
  originals.each_with_object(originals) { |(key, value), merged|
48
49
  next unless overrides.key?(key)
49
50
 
50
- merged[key] = combine_values(
51
- value,
52
- overrides[key],
53
- parents: [*parents, *key],
54
- mergeable_attributes: mergeable_attributes
55
- )
51
+ merged[key] = process_pair(key, value, parents: parents, overrides: overrides, mergeable_attributes: mergeable_attributes)
56
52
 
57
53
  overrides.delete(key)
58
54
  }.merge(overrides)
@@ -76,19 +72,40 @@ module HTMLAttributesUtils
76
72
 
77
73
  private
78
74
 
75
+ def process_pair(key, value, parents:, overrides:, mergeable_attributes:)
76
+ combine_values(
77
+ value,
78
+ overrides[key],
79
+ parents: [*parents, *key],
80
+ mergeable_attributes: mergeable_attributes
81
+ )
82
+ end
83
+
79
84
  def tidy_value(value)
80
85
  case value
81
- when Hash
82
- value.deep_tidy_html_attributes.presence
83
- when Array
84
- (value.empty?) ? nil : value.map { |v| tidy_value(v) }.compact
85
- when TrueClass, FalseClass
86
- value
87
- else
88
- value.to_s.strip.presence
86
+ when TrueClass, FalseClass then value
87
+ when Hash then tidy_hash(value)
88
+ when Array then tidy_array(value)
89
+ else tidy_remaining(value)
89
90
  end
90
91
  end
91
92
 
93
+ def tidy_hash(hash)
94
+ return nil if hash.empty?
95
+
96
+ hash.deep_tidy_html_attributes
97
+ end
98
+
99
+ def tidy_array(array)
100
+ return nil if array.empty?
101
+
102
+ array.map { |v| tidy_value(v) }.compact
103
+ end
104
+
105
+ def tidy_remaining(value)
106
+ value.to_s.strip.presence
107
+ end
108
+
92
109
  def combine_values(value, override, **kwargs)
93
110
  case split_attribute_list(value, **kwargs)
94
111
  when Array
@@ -116,9 +133,7 @@ module HTMLAttributesUtils
116
133
  end
117
134
 
118
135
  def split_attribute_list(value, parents:, mergeable_attributes:)
119
- return value.split if value.is_a?(String) && mergeable_attributes.include?(parents)
120
-
121
- value
136
+ mergeable_attributes.include?(parents) ? try_split(value) : value
122
137
  end
123
138
  end
124
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-attributes-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Yates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-26 00:00:00.000000000 Z
11
+ date: 2022-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport