html-attributes-utils 0.9.2 → 1.0.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 +4 -4
- data/README.md +8 -0
- data/lib/html_attributes_utils.rb +34 -18
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61d705b8b6000833dbd60e659fcca312f61e6d4cfbd10a3f25bcc13845f6ef27
|
4
|
+
data.tar.gz: a5fc3a72c775fd555f94efd98f03a9324010fb0ac9b80385eeddb0b8923a8f0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf02f936411794118c6e001d8ab67452394442859450133e4402d4a04f6c305620e3f215bdb5f340dc55e3249328641efa3d85cb130000a2915ab0756cd651a4
|
7
|
+
data.tar.gz: '09d8ca2b7e2444332f64b50b4fe93e19ee6aae4408688f993455e8db19d33b2de36df51c242db97373aa8a1787401f4c651c83587df9ce6bfeb7edc525b6e4a0'
|
data/README.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# HTML Attributes Utilities
|
2
2
|
|
3
|
+
[](https://github.com/DFE-Digital/html-attributes-utils/actions/workflows/tests.yml)
|
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
|
@@ -8,14 +8,16 @@ module HTMLAttributesUtils
|
|
8
8
|
# They are stored as nested arrays so when we're walking multiple
|
9
9
|
# levels on the deep merge the structure can be identified. This means
|
10
10
|
# the library works with the Rails-preferred format of
|
11
|
-
# `aria: { describedby: "xyz" }` rather than `aria-describdby
|
11
|
+
# `aria: { describedby: "xyz" }` rather than `"aria-describdby" => "xyz"`
|
12
12
|
DEFAULT_MERGEABLE_ATTRIBUTES = [
|
13
13
|
%i(class),
|
14
14
|
%i(aria controls),
|
15
15
|
%i(aria describedby),
|
16
16
|
%i(aria flowto),
|
17
17
|
%i(aria labelledby),
|
18
|
+
%i(data aria controls),
|
18
19
|
%i(aria owns),
|
20
|
+
%i(rel),
|
19
21
|
].freeze
|
20
22
|
|
21
23
|
refine Hash do
|
@@ -47,12 +49,7 @@ module HTMLAttributesUtils
|
|
47
49
|
originals.each_with_object(originals) { |(key, value), merged|
|
48
50
|
next unless overrides.key?(key)
|
49
51
|
|
50
|
-
merged[key] =
|
51
|
-
value,
|
52
|
-
overrides[key],
|
53
|
-
parents: [*parents, *key],
|
54
|
-
mergeable_attributes: mergeable_attributes
|
55
|
-
)
|
52
|
+
merged[key] = process_pair(key, value, parents: parents, overrides: overrides, mergeable_attributes: mergeable_attributes)
|
56
53
|
|
57
54
|
overrides.delete(key)
|
58
55
|
}.merge(overrides)
|
@@ -76,19 +73,40 @@ module HTMLAttributesUtils
|
|
76
73
|
|
77
74
|
private
|
78
75
|
|
76
|
+
def process_pair(key, value, parents:, overrides:, mergeable_attributes:)
|
77
|
+
combine_values(
|
78
|
+
value,
|
79
|
+
overrides[key],
|
80
|
+
parents: [*parents, *key],
|
81
|
+
mergeable_attributes: mergeable_attributes
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
79
85
|
def tidy_value(value)
|
80
86
|
case value
|
81
|
-
when
|
82
|
-
|
83
|
-
when Array
|
84
|
-
|
85
|
-
when TrueClass, FalseClass
|
86
|
-
value
|
87
|
-
else
|
88
|
-
value.to_s.strip.presence
|
87
|
+
when TrueClass, FalseClass then value
|
88
|
+
when Hash then tidy_hash(value)
|
89
|
+
when Array then tidy_array(value)
|
90
|
+
else tidy_remaining(value)
|
89
91
|
end
|
90
92
|
end
|
91
93
|
|
94
|
+
def tidy_hash(hash)
|
95
|
+
return nil if hash.empty?
|
96
|
+
|
97
|
+
hash.deep_tidy_html_attributes
|
98
|
+
end
|
99
|
+
|
100
|
+
def tidy_array(array)
|
101
|
+
return nil if array.empty?
|
102
|
+
|
103
|
+
array.map { |v| tidy_value(v) }.compact
|
104
|
+
end
|
105
|
+
|
106
|
+
def tidy_remaining(value)
|
107
|
+
value.to_s.strip.presence
|
108
|
+
end
|
109
|
+
|
92
110
|
def combine_values(value, override, **kwargs)
|
93
111
|
case split_attribute_list(value, **kwargs)
|
94
112
|
when Array
|
@@ -116,9 +134,7 @@ module HTMLAttributesUtils
|
|
116
134
|
end
|
117
135
|
|
118
136
|
def split_attribute_list(value, parents:, mergeable_attributes:)
|
119
|
-
|
120
|
-
|
121
|
-
value
|
137
|
+
mergeable_attributes.include?(parents) ? try_split(value) : value
|
122
138
|
end
|
123
139
|
end
|
124
140
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Yates
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '0'
|
117
117
|
requirements: []
|
118
|
-
rubygems_version: 3.
|
118
|
+
rubygems_version: 3.4.10
|
119
119
|
signing_key:
|
120
120
|
specification_version: 4
|
121
121
|
summary: HTML attribute hash utilities
|