berns 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/berns.rb +47 -32
  3. metadata +33 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 675a795e6f002f4a7a01d8f0cf2212cf700ee532cbfc6f931ac8da70916549c9
4
- data.tar.gz: 26f950193def91bbe73e1bf6aee8a5976b91093aa6c4b627463bdf519724bdbc
3
+ metadata.gz: 1f544945cd9c43afe1ce6d3bdf6e7a3c026b6386925cbe7ac9a6ba36ba228b76
4
+ data.tar.gz: 21add20e6f88c5c56ba48a5b60d48c80e4f4be0b9b351540345479f2a5f94896
5
5
  SHA512:
6
- metadata.gz: 0bf3f0367bc6d49a93c623a3974646dbcbeb67999dc336fd03e8b3cab6610e804e2975e981a522c007e363ba03aed3c8b8e819186f47dfc95de811714e51307a
7
- data.tar.gz: 643b84ca007b078b79339f83a505e0aabd62bb0beca30890bb8fb4fb9451a8a63a052eec2abbc28127bdc5a504fb98b5d71ea8e7db095fc16f3292bb0c75f424
6
+ metadata.gz: 40ac56793d022c1191c775aba8020221e6251b9c9be9f61485bd4e0ad3b90e3b46e2e1f4aa1070a3fadce6026b2f48f4b31da156bdba89930fe7c3d557fff06f
7
+ data.tar.gz: 1af75731f2ce07e6978653b45f077ff20784d950cecbfce74970185c3c2a487c306187d821fed30edba3f4e920a5a7741c9bd009c584f86c7e1c6a0a3d9272fe
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
+ require 'cgi/escape'
3
+
2
4
  # A utility library for generating HTML strings
3
5
  module Berns
6
+ HASH = {}.freeze
4
7
  SPACE = ' '
5
8
  EMPTY = ''
6
9
 
@@ -13,17 +16,17 @@ module Berns
13
16
  # Full list of standard HTML5 elements - https://www.w3schools.com/TAgs/default.asp
14
17
  STANDARD = %i[a abbr address article aside audio b bdi bdo blockquote body button canvas caption cite code colgroup datalist dd del details dfn dialog div dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header html i iframe ins kbd label legend li main map mark menu meter nav noscript object ol optgroup option output p picture pre progress q rp rt ruby s samp script section select small span strong style sub summary table tbody td template textarea tfoot th thead time title tr u ul var video].freeze
15
18
 
16
- # Dynamically defined methods that are simple proxies to {Markup#element}.
19
+ # Dynamically defined methods that are simple proxies to {#element}.
17
20
  STANDARD.each do |elm|
18
- define_singleton_method(elm) do |arguments = {}, &block|
19
- element(elm, arguments, &block)
21
+ define_singleton_method(elm) do |attributes = HASH, &block|
22
+ element(elm, attributes, &block)
20
23
  end
21
24
  end
22
25
 
23
- # Dynamically defined methods that are simple proxies to {Markup#void}.
26
+ # Dynamically defined methods that are simple proxies to {#void}.
24
27
  VOID.each do |elm|
25
- define_singleton_method(elm) do |arguments = {}|
26
- void(elm, arguments)
28
+ define_singleton_method(elm) do |attributes = HASH|
29
+ void(elm, attributes)
27
30
  end
28
31
  end
29
32
 
@@ -52,14 +55,10 @@ module Berns
52
55
  # @yieldreturn [String]
53
56
  # The textual content of the element. May be HTML or plain text.
54
57
  # @return [String]
55
- def self.element(tag, attributes = {})
56
- content = yield if block_given?
57
-
58
- # Move stuff around unless the attributes are empty.
59
- attrs = to_attributes(attributes)
60
- attrs = " #{ attrs }" unless attrs.empty?
58
+ def self.element(tag, attributes = HASH)
59
+ return "<#{ tag }>#{ yield if block_given? }</#{ tag }>" if attributes.empty?
61
60
 
62
- "<#{ tag }#{ attrs }>#{ content }</#{ tag }>"
61
+ "<#{ tag } #{ to_attributes(attributes) }>#{ yield if block_given? }</#{ tag }>"
63
62
  end
64
63
 
65
64
  # Same as above, but generates void elements i.e. ones without any textual
@@ -69,12 +68,10 @@ module Berns
69
68
  # void(:br) # => "<br>"
70
69
  #
71
70
  # @return [String]
72
- def self.void(tag, attributes = {})
73
- # Move stuff around unless the attributes are empty.
74
- attrs = to_attributes(attributes)
75
- attrs = " #{ attrs }" unless attrs.empty?
71
+ def self.void(tag, attributes = HASH)
72
+ return "<#{ tag }>" if attributes.empty?
76
73
 
77
- "<#{ tag }#{ attrs }>"
74
+ "<#{ tag } #{ to_attributes(attributes) }>"
78
75
  end
79
76
 
80
77
  # Converts a hash into HTML attributes by mapping each key/value combination
@@ -88,12 +85,22 @@ module Berns
88
85
  #
89
86
  # @param attributes [Hash]
90
87
  # The hash to convert to HTML attributes.
88
+ #
91
89
  # @return [String]
92
90
  # The space-joined string containing HTML attributes.
93
91
  def self.to_attributes(attributes)
94
- attributes.map do |attribute, value|
95
- to_attribute(attribute, value)
96
- end.join(SPACE).chomp(SPACE)
92
+ return EMPTY if attributes.empty?
93
+
94
+ string = +''
95
+
96
+ attributes.each do |attr, value|
97
+ string << SPACE
98
+
99
+ to_attribute(attr, value, string)
100
+ end
101
+
102
+ string.strip!
103
+ string
97
104
  end
98
105
 
99
106
  # Converts a single attribute and value into an HTML attribute string.
@@ -105,20 +112,28 @@ module Berns
105
112
  # The attribute key.
106
113
  # @param value [String, Boolean, Hash]
107
114
  # The value to assign to the attribute.
115
+ # @param string [String, nil]
116
+ # The string modify in place with attributes and values.
117
+ #
108
118
  # @return [String]
109
119
  # A single HTML attribute.
110
- def self.to_attribute(attribute, value)
111
- case value
112
- when TrueClass
113
- attribute.to_s
114
- when Hash
115
- value.map do |subattribute, subvalue|
116
- "#{ attribute }-#{ to_attribute(subattribute, subvalue) }"
117
- end.join(SPACE)
118
- when FalseClass
119
- EMPTY
120
+ def self.to_attribute(attribute, value, string = +'')
121
+ if value.is_a?(FalseClass) # rubocop:disable Style/CaseLikeIf
122
+ # noop
123
+ elsif value.is_a?(TrueClass)
124
+ string << attribute.to_s
125
+ elsif value.is_a?(Hash)
126
+ value.each do |attr, subval|
127
+ string << SPACE
128
+
129
+ to_attribute(attr.nil? ? attribute : "#{ attribute }-#{ attr }", subval, string)
130
+ end
131
+
132
+ string.strip!
120
133
  else
121
- "#{ attribute }='#{ value }'"
134
+ string << %(#{ attribute }="#{ CGI.escapeHTML(value.to_s) }")
122
135
  end
136
+
137
+ string
123
138
  end
124
139
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berns
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Beck
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-08-12 00:00:00.000000000 Z
12
+ date: 2020-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -53,9 +53,37 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop-performance
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
56
84
  description: A utility library for generating HTML strings.
57
85
  email:
58
- - taylor.beck@engageft.com
86
+ - beck.taylorg@gmail.com
59
87
  - evan@lecklider.com
60
88
  executables: []
61
89
  extensions: []
@@ -76,14 +104,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
104
  requirements:
77
105
  - - ">="
78
106
  - !ruby/object:Gem::Version
79
- version: 2.3.0
107
+ version: 2.5.0
80
108
  required_rubygems_version: !ruby/object:Gem::Requirement
81
109
  requirements:
82
110
  - - ">="
83
111
  - !ruby/object:Gem::Version
84
112
  version: '0'
85
113
  requirements: []
86
- rubygems_version: 3.0.4
114
+ rubygems_version: 3.1.4
87
115
  signing_key:
88
116
  specification_version: 4
89
117
  summary: A utility library for generating HTML strings.