html-attributes-utils 0.9.2 → 1.0.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.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/html_attributes_utils.rb +32 -17
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55226e6e32681583a11fd45f4ea8752e41c74a173088c2250875a4ef003a702e
|
4
|
+
data.tar.gz: afeb51d41d803e0ce1e6042b6518aa456ef4d48264559133f6f55afcbbfd3e73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
+

|
4
|
+
[](https://codeclimate.com/github/DFE-Digital/html-attributes-utils/maintainability)
|
5
|
+
[](https://codeclimate.com/github/DFE-Digital/html-attributes-utils/test_coverage)
|
6
|
+

|
7
|
+

|
8
|
+

|
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] =
|
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
|
82
|
-
|
83
|
-
when Array
|
84
|
-
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2022-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|