html-attributes-utils 0.9.0 → 0.9.2a1
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 +39 -1
- data/lib/html_attributes_utils.rb +26 -34
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 631ae9c1de4c05565e53583b4d4ad4b11919fb8b4ee65bd0fe8e751ce1cb71cd
|
4
|
+
data.tar.gz: 59f130f0f247944cb2c3d72f7a76ebc55bd21f4fb1ce373e8fa0cd99e2b034b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7419b9e2b423592a8ed9d2c062ba251229833e129d62eb3c4749140e530dd62dbade2e19bdadcbaea0bf5a8d9cef72a7524c23af9db7260f4108920fd13bca27
|
7
|
+
data.tar.gz: cf98c264969fb7b22db802c1020f69d8d1659a9dd701bf1768611c4ad0ae134c4e12c18dafbd3e8ad52bc283cad7f1cc0d2fe3fb476cca96d41a34eff96f5c87
|
data/README.md
CHANGED
@@ -1,3 +1,41 @@
|
|
1
1
|
# HTML Attributes Utilities
|
2
2
|
|
3
|
-
This is a small library intended to make it easier to deal with
|
3
|
+
This is a small library intended to make it easier to deal with HTML
|
4
|
+
attributes. It was written to reduce overlap in the
|
5
|
+
[govuk-components](https://github.com/DFE-Digital/govuk-components) and
|
6
|
+
[govuk-formbuilder](https://github.com/DFE-Digital/govuk-formbuilder) libraries.
|
7
|
+
|
8
|
+
It provides refinements for `Hash` that allow:
|
9
|
+
|
10
|
+
* deep merging while protecting default values from being overwritten
|
11
|
+
* tidying hashes by removing key/value pairs that have empty or nil values
|
12
|
+
|
13
|
+
## Example use
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require "html_attributes_utils"
|
17
|
+
|
18
|
+
def MyPresenter
|
19
|
+
using HTMLAttributesUtils
|
20
|
+
|
21
|
+
def initialize(custom_attributes = {})
|
22
|
+
@custom_attributes = custom_attributes
|
23
|
+
end
|
24
|
+
|
25
|
+
def attributes
|
26
|
+
default_attributes
|
27
|
+
.deep_merge_html_attributes(@custom_attributes)
|
28
|
+
.deep_tidy_html_attributes
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def default_attributes
|
34
|
+
{ lang: "en-GB", class: "govuk-juggling-widget" }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
MyPresenter.new(lang: "fr", class: "stripes", some_other_attr: "").attributes
|
39
|
+
|
40
|
+
=> { lang: "fr", class: ["govuk-juggling-widget", "stripes"] }
|
41
|
+
```
|
@@ -1,6 +1,23 @@
|
|
1
1
|
require "active_support/core_ext/hash/keys"
|
2
2
|
|
3
3
|
module HTMLAttributesUtils
|
4
|
+
# DEFAULT_MERGEABLE_ATTRIBUTES is a list of HTML attributes where the value
|
5
|
+
# contain multiple elements separated by spaces. We use it to target values
|
6
|
+
# to split so the arrays can be cleanly merged.
|
7
|
+
#
|
8
|
+
# They are stored as nested arrays so when we're walking multiple
|
9
|
+
# levels on the deep merge the structure can be identified. This means
|
10
|
+
# the library works with the Rails-preferred format of
|
11
|
+
# `aria: { describedby: "xyz" }` rather than `aria-describdby: `xyz`
|
12
|
+
DEFAULT_MERGEABLE_ATTRIBUTES = [
|
13
|
+
%i(class),
|
14
|
+
%i(aria controls),
|
15
|
+
%i(aria describedby),
|
16
|
+
%i(aria flowto),
|
17
|
+
%i(aria labelledby),
|
18
|
+
%i(aria owns),
|
19
|
+
].freeze
|
20
|
+
|
4
21
|
refine Hash do
|
5
22
|
# Merge the incoming hash into the current one in a way that suits
|
6
23
|
# HTML attributes.
|
@@ -21,18 +38,19 @@ module HTMLAttributesUtils
|
|
21
38
|
#
|
22
39
|
# => { class: "blue", data: { size: "medium", controller: "reply" } }
|
23
40
|
#
|
24
|
-
def deep_merge_html_attributes(custom, parents = [], mergeable_attributes:
|
41
|
+
def deep_merge_html_attributes(custom, parents = [], mergeable_attributes: DEFAULT_MERGEABLE_ATTRIBUTES)
|
25
42
|
return custom unless custom.is_a?(Hash)
|
26
43
|
|
27
44
|
overrides = custom.deep_symbolize_keys
|
45
|
+
originals = deep_symbolize_keys
|
28
46
|
|
29
|
-
|
47
|
+
originals.each_with_object(originals) { |(key, value), merged|
|
30
48
|
next unless overrides.key?(key)
|
31
49
|
|
32
50
|
merged[key] = combine_values(
|
33
51
|
value,
|
34
52
|
overrides[key],
|
35
|
-
parents: parents
|
53
|
+
parents: [*parents, *key],
|
36
54
|
mergeable_attributes: mergeable_attributes
|
37
55
|
)
|
38
56
|
|
@@ -53,9 +71,7 @@ module HTMLAttributesUtils
|
|
53
71
|
# => { class: "blue" }
|
54
72
|
#
|
55
73
|
def deep_tidy_html_attributes
|
56
|
-
each_with_object({})
|
57
|
-
(tidy[key] = tidy_value(value)) unless skippable_value?(value)
|
58
|
-
end
|
74
|
+
each_with_object({}) { |(k, v), tidied| (tidied[k] = tidy_value(v)) unless v.nil? }.compact
|
59
75
|
end
|
60
76
|
|
61
77
|
private
|
@@ -65,27 +81,14 @@ module HTMLAttributesUtils
|
|
65
81
|
when Hash
|
66
82
|
value.deep_tidy_html_attributes.presence
|
67
83
|
when Array
|
68
|
-
value.
|
69
|
-
when
|
70
|
-
value.strip.presence
|
71
|
-
else
|
84
|
+
(value.empty?) ? nil : value.map { |v| tidy_value(v) }.compact
|
85
|
+
when TrueClass, FalseClass
|
72
86
|
value
|
87
|
+
else
|
88
|
+
value.to_s.strip.presence
|
73
89
|
end
|
74
90
|
end
|
75
91
|
|
76
|
-
# Identiy attribute values we don't care about.
|
77
|
-
#
|
78
|
-
# We can't check for this using Rails' `#presence` because we want `false` to be
|
79
|
-
# included so some HTML attributes that default to on can be disabled, like
|
80
|
-
# `autocomplete: false`
|
81
|
-
#
|
82
|
-
# FIXME: there is probably a nicer way of doing this...
|
83
|
-
def skippable_value?(value)
|
84
|
-
return false if [true, false].include?(value)
|
85
|
-
|
86
|
-
value.blank?
|
87
|
-
end
|
88
|
-
|
89
92
|
def combine_values(value, override, **kwargs)
|
90
93
|
case split_attribute_list(value, **kwargs)
|
91
94
|
when Array
|
@@ -117,16 +120,5 @@ module HTMLAttributesUtils
|
|
117
120
|
|
118
121
|
value
|
119
122
|
end
|
120
|
-
|
121
|
-
def default_mergeable_attributes
|
122
|
-
[
|
123
|
-
%i(class),
|
124
|
-
%i(aria controls),
|
125
|
-
%i(aria describedby),
|
126
|
-
%i(aria flowto),
|
127
|
-
%i(aria labelledby),
|
128
|
-
%i(aria owns),
|
129
|
-
]
|
130
|
-
end
|
131
123
|
end
|
132
124
|
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.
|
4
|
+
version: 0.9.2a1
|
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-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 4.3.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.20'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.20'
|
69
83
|
description: A small collection of utilities to ease working with hashes of HTML attributes
|
70
84
|
email:
|
71
85
|
- peter.yates@graphia.co.uk
|
@@ -97,9 +111,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
111
|
version: '0'
|
98
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
113
|
requirements:
|
100
|
-
- - "
|
114
|
+
- - ">"
|
101
115
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
116
|
+
version: 1.3.1
|
103
117
|
requirements: []
|
104
118
|
rubygems_version: 3.3.7
|
105
119
|
signing_key:
|