meta-tags 2.23.0 → 2.25.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
- checksums.yaml.gz.sig +0 -0
- data/AGENTS.md +4 -0
- data/CHANGELOG.md +95 -0
- data/README.md +134 -18
- data/SECURITY.md +40 -0
- data/certs/kpumuk.pem +7 -7
- data/lib/generators/meta_tags/templates/config/initializers/meta_tags.rb +10 -0
- data/lib/meta_tags/configuration.rb +10 -0
- data/lib/meta_tags/content_tag.rb +1 -1
- data/lib/meta_tags/controller_helper.rb +22 -3
- data/lib/meta_tags/meta_tags_collection.rb +97 -29
- data/lib/meta_tags/railtie.rb +4 -0
- data/lib/meta_tags/renderer.rb +47 -9
- data/lib/meta_tags/tag.rb +25 -1
- data/lib/meta_tags/text_normalizer.rb +78 -21
- data/lib/meta_tags/version.rb +1 -1
- data/lib/meta_tags/view_helper.rb +3 -0
- data/lib/meta_tags.rb +6 -1
- data/sig/_private/rails_support.rbs +7 -0
- data/sig/lib/meta_tags/configuration.rbs +6 -4
- data/sig/lib/meta_tags/controller_helper.rbs +6 -0
- data/sig/lib/meta_tags/meta_tags_collection.rbs +8 -2
- data/sig/lib/meta_tags/rails_interfaces.rbs +2 -0
- data/sig/lib/meta_tags/renderer.rbs +11 -7
- data/sig/lib/meta_tags/tag.rbs +3 -1
- data/sig/lib/meta_tags/text_normalizer.rbs +6 -3
- data/sig/lib/meta_tags/view_helper.rbs +2 -1
- data/sig/lib/meta_tags.rbs +2 -0
- data.tar.gz.sig +0 -0
- metadata +20 -18
- metadata.gz.sig +0 -0
|
@@ -15,6 +15,10 @@ module MetaTags
|
|
|
15
15
|
# A string or regexp separator to truncate text at a natural break.
|
|
16
16
|
attr_accessor :truncate_on_natural_separator
|
|
17
17
|
|
|
18
|
+
# When true, multi-item arrays stop at item boundaries instead of
|
|
19
|
+
# truncating the overflowing item.
|
|
20
|
+
attr_accessor :truncate_array_items_at_boundaries
|
|
21
|
+
|
|
18
22
|
# How many characters to truncate description to.
|
|
19
23
|
attr_accessor :description_limit
|
|
20
24
|
|
|
@@ -35,6 +39,10 @@ module MetaTags
|
|
|
35
39
|
# Default is false.
|
|
36
40
|
attr_accessor :minify_output
|
|
37
41
|
|
|
42
|
+
# When true, Symbols inside nested meta tag arrays resolve as references to
|
|
43
|
+
# normalized top-level tags. Default is false until MetaTags 3.0.
|
|
44
|
+
attr_accessor :resolve_symbolic_references_in_arrays
|
|
45
|
+
|
|
38
46
|
# Custom meta tags that should use the `property` attribute instead of `name`.
|
|
39
47
|
# An array of strings or symbols representing their names or name prefixes.
|
|
40
48
|
attr_reader :property_tags
|
|
@@ -94,6 +102,7 @@ module MetaTags
|
|
|
94
102
|
@title_limit = 70
|
|
95
103
|
@truncate_site_title_first = false
|
|
96
104
|
@truncate_on_natural_separator = " "
|
|
105
|
+
@truncate_array_items_at_boundaries = false
|
|
97
106
|
@title_tag_attributes = {}
|
|
98
107
|
@description_limit = 300
|
|
99
108
|
@keywords_limit = 255
|
|
@@ -102,6 +111,7 @@ module MetaTags
|
|
|
102
111
|
@property_tags = default_property_tags.dup
|
|
103
112
|
@open_meta_tags = true
|
|
104
113
|
@minify_output = false
|
|
114
|
+
@resolve_symbolic_references_in_arrays = false
|
|
105
115
|
@skip_canonical_links_on_noindex = false
|
|
106
116
|
end
|
|
107
117
|
end
|
|
@@ -9,7 +9,7 @@ module MetaTags
|
|
|
9
9
|
# @param view [ActionView::Base] instance of a Rails view.
|
|
10
10
|
# @return [String] HTML string for the tag.
|
|
11
11
|
def render(view)
|
|
12
|
-
view.content_tag(name, attributes[:content], serialize_iso8601_attributes(attributes.except(:content)))
|
|
12
|
+
view.content_tag(name, attributes[:content], drop_blank_attribute_names(serialize_iso8601_attributes(attributes.except(:content))))
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
end
|
|
@@ -17,9 +17,19 @@ module MetaTags
|
|
|
17
17
|
# @yield optional block forwarded to +render+.
|
|
18
18
|
# @yieldreturn [Object] result of the render block.
|
|
19
19
|
def render(*args, &block)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
set_meta_tags_from_instance_variables
|
|
21
|
+
|
|
22
|
+
super
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Processes controller instance-variable metadata before rendering to a string.
|
|
26
|
+
#
|
|
27
|
+
# @param args [Array<Object>] positional arguments forwarded to +render_to_string+.
|
|
28
|
+
# @param block [Proc, nil] optional block forwarded to +render_to_string+.
|
|
29
|
+
# @yield optional block forwarded to +render_to_string+.
|
|
30
|
+
# @yieldreturn [Object] result of the render block.
|
|
31
|
+
def render_to_string(*args, &block)
|
|
32
|
+
set_meta_tags_from_instance_variables
|
|
23
33
|
|
|
24
34
|
super
|
|
25
35
|
end
|
|
@@ -38,5 +48,14 @@ module MetaTags
|
|
|
38
48
|
@meta_tags ||= MetaTagsCollection.new
|
|
39
49
|
end
|
|
40
50
|
protected :meta_tags
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
# Copies controller instance-variable metadata into the collection.
|
|
55
|
+
def set_meta_tags_from_instance_variables
|
|
56
|
+
meta_tags[:title] = @page_title if defined?(@page_title) && @page_title
|
|
57
|
+
meta_tags[:keywords] = @page_keywords if defined?(@page_keywords) && @page_keywords
|
|
58
|
+
meta_tags[:description] = @page_description if defined?(@page_description) && @page_description
|
|
59
|
+
end
|
|
41
60
|
end
|
|
42
61
|
end
|
|
@@ -70,13 +70,10 @@ module MetaTags
|
|
|
70
70
|
# @param defaults [Hash] list of default meta tag attributes.
|
|
71
71
|
# @return [String] page title.
|
|
72
72
|
def page_title(defaults = {})
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
full_title.presence || old_site || ""
|
|
78
|
-
ensure
|
|
79
|
-
@meta_tags[:site] = old_site if had_site
|
|
73
|
+
with_defaults(defaults) do
|
|
74
|
+
site = extract(:site)
|
|
75
|
+
extract_full_title.presence || TextNormalizer.normalize_title(site, [], "")
|
|
76
|
+
end
|
|
80
77
|
end
|
|
81
78
|
|
|
82
79
|
# Deletes and returns a meta tag value by name.
|
|
@@ -99,11 +96,12 @@ module MetaTags
|
|
|
99
96
|
# @return [String] page title.
|
|
100
97
|
def extract_full_title
|
|
101
98
|
site_title = extract(:site) || ""
|
|
99
|
+
lowercase = extract(:lowercase) == true
|
|
102
100
|
title = extract_title
|
|
103
101
|
separator = extract_separator
|
|
104
102
|
reverse = extract(:reverse) == true
|
|
105
103
|
|
|
106
|
-
TextNormalizer.normalize_title(site_title, title, separator, reverse)
|
|
104
|
+
TextNormalizer.normalize_title(site_title, title, separator, reverse: reverse, lowercase: lowercase)
|
|
107
105
|
end
|
|
108
106
|
|
|
109
107
|
# Extracts page title as an array of segments without site title and separators.
|
|
@@ -113,11 +111,7 @@ module MetaTags
|
|
|
113
111
|
title = extract(:title).presence
|
|
114
112
|
return [] unless title
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
title = Array(title)
|
|
118
|
-
return title.map(&:downcase) if extract(:lowercase) == true
|
|
119
|
-
|
|
120
|
-
title
|
|
114
|
+
Array(title)
|
|
121
115
|
end
|
|
122
116
|
|
|
123
117
|
# Extracts title separator as a string.
|
|
@@ -137,10 +131,48 @@ module MetaTags
|
|
|
137
131
|
TextNormalizer.safe_join([prefix, separator, suffix], "")
|
|
138
132
|
end
|
|
139
133
|
|
|
140
|
-
# Extracts
|
|
134
|
+
# Extracts robots settings as a Hash mapping tag names to rendered values.
|
|
141
135
|
#
|
|
142
|
-
# @return [Hash{String => String}]
|
|
136
|
+
# @return [Hash{String => String}] robots attributes.
|
|
143
137
|
def extract_robots
|
|
138
|
+
result = robots
|
|
139
|
+
delete(:noindex, :index, :follow, :nofollow, :noarchive)
|
|
140
|
+
[:robots, :googlebot, :bingbot].each do |bot|
|
|
141
|
+
extract(bot) if meta_tags[bot].is_a?(Hash)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
result
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Returns whether robots settings produce a noindex directive.
|
|
148
|
+
#
|
|
149
|
+
# @return [Boolean] true when a noindex directive will be rendered.
|
|
150
|
+
def noindex?
|
|
151
|
+
contents = robots.values
|
|
152
|
+
property_tags = MetaTags.config.property_tags.map(&:to_s)
|
|
153
|
+
meta_tags.each do |key, values|
|
|
154
|
+
# Crawler names are case-insensitive, but rendering keeps the key's spelling.
|
|
155
|
+
key = key.to_s
|
|
156
|
+
next unless %w[robots googlebot bingbot].any? { |bot| key.casecmp?(bot) }
|
|
157
|
+
next if values.is_a?(Hash) || property_tags.include?(key)
|
|
158
|
+
|
|
159
|
+
contents.concat(Array(values))
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
contents.any? do |content|
|
|
163
|
+
content.to_s.split(",").any? do |directive|
|
|
164
|
+
directive = directive.strip
|
|
165
|
+
directive.casecmp?("noindex") || directive.casecmp?("none")
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
protected
|
|
171
|
+
|
|
172
|
+
# Returns robots settings without modifying the collection.
|
|
173
|
+
#
|
|
174
|
+
# @return [Hash{String => String}] robots attributes.
|
|
175
|
+
def robots
|
|
144
176
|
# @type var result: Hash[String, Array[String]]
|
|
145
177
|
result = Hash.new { |h, k| h[k] = [] }
|
|
146
178
|
|
|
@@ -154,18 +186,36 @@ module MetaTags
|
|
|
154
186
|
calculate_robots_attributes(result, attributes)
|
|
155
187
|
end
|
|
156
188
|
|
|
189
|
+
[:robots, :googlebot, :bingbot].each do |bot|
|
|
190
|
+
values = meta_tags[bot]
|
|
191
|
+
if values.is_a?(Hash)
|
|
192
|
+
values.each do |key, value|
|
|
193
|
+
next if value == false
|
|
194
|
+
|
|
195
|
+
directive = (value.nil? || value == true) ? key.to_s : "#{key}:#{value}"
|
|
196
|
+
result[bot.to_s] << directive
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
157
201
|
result.transform_values { |v| v.join(", ") }
|
|
158
202
|
end
|
|
159
203
|
|
|
160
|
-
protected
|
|
161
|
-
|
|
162
204
|
# Converts input hash to HashWithIndifferentAccess and renames :open_graph to :og.
|
|
205
|
+
# When both aliases contain hashes, they are deep-merged with :og taking precedence.
|
|
163
206
|
#
|
|
164
207
|
# @param meta_tags [Hash] list of meta tags.
|
|
165
208
|
# @return [ActiveSupport::HashWithIndifferentAccess] normalized meta tags list.
|
|
166
209
|
def normalize_open_graph(meta_tags)
|
|
167
210
|
meta_tags = meta_tags.with_indifferent_access
|
|
168
|
-
|
|
211
|
+
if meta_tags.key?(:open_graph)
|
|
212
|
+
open_graph = meta_tags.delete(:open_graph)
|
|
213
|
+
if meta_tags.key?(:og)
|
|
214
|
+
meta_tags[:og] = open_graph.dup.deep_merge!(meta_tags[:og]) if open_graph.is_a?(Hash) && meta_tags[:og].is_a?(Hash)
|
|
215
|
+
else
|
|
216
|
+
meta_tags[:og] = open_graph
|
|
217
|
+
end
|
|
218
|
+
end
|
|
169
219
|
meta_tags
|
|
170
220
|
end
|
|
171
221
|
|
|
@@ -182,15 +232,32 @@ module MetaTags
|
|
|
182
232
|
# Extracts a robots attribute name/value pair (noindex, nofollow, etc.).
|
|
183
233
|
#
|
|
184
234
|
# @param name [String, Symbol] noindex attribute name.
|
|
185
|
-
# @return [Array<
|
|
235
|
+
# @return [Array<Object>] normalized robots tag name(s) and directive value.
|
|
186
236
|
def extract_robots_attribute(name)
|
|
187
|
-
noindex =
|
|
188
|
-
noindex_name
|
|
189
|
-
|
|
237
|
+
noindex = meta_tags[name]
|
|
238
|
+
# @type var noindex_name: String | Array[String]
|
|
239
|
+
noindex_name = if noindex.is_a?(Array)
|
|
240
|
+
noindex.reject { |target| target.is_a?(String) && target.blank? }.map(&:to_s)
|
|
241
|
+
elsif noindex.is_a?(String)
|
|
242
|
+
noindex.presence || []
|
|
243
|
+
else
|
|
244
|
+
"robots"
|
|
245
|
+
end
|
|
246
|
+
noindex_value = if robots_attribute_present?(noindex) && noindex_name.present?
|
|
247
|
+
name.to_s
|
|
248
|
+
end
|
|
190
249
|
|
|
191
250
|
[noindex_name, noindex_value]
|
|
192
251
|
end
|
|
193
252
|
|
|
253
|
+
# Returns whether a robots attribute resolves to at least one target.
|
|
254
|
+
#
|
|
255
|
+
# @param value [Object] robots attribute value.
|
|
256
|
+
# @return [Boolean] true when the attribute produces a directive.
|
|
257
|
+
def robots_attribute_present?(value)
|
|
258
|
+
!!value && (!value.is_a?(Array) || !value.empty?)
|
|
259
|
+
end
|
|
260
|
+
|
|
194
261
|
# Appends resolved robots directives while preserving first-write priority.
|
|
195
262
|
#
|
|
196
263
|
# @param result [Hash{String => Array<String>}] robots directives grouped by tag name.
|
|
@@ -203,26 +270,27 @@ module MetaTags
|
|
|
203
270
|
names, value = extract_robots_attribute(attribute)
|
|
204
271
|
next unless value
|
|
205
272
|
|
|
206
|
-
|
|
207
|
-
|
|
273
|
+
robot_names = names.is_a?(String) ? [names] : names
|
|
274
|
+
robot_names.each do |name|
|
|
208
275
|
apply_robots_value(result, name, value, processed)
|
|
209
276
|
end
|
|
210
277
|
end
|
|
211
278
|
end
|
|
212
279
|
|
|
213
|
-
# Records a robots directive unless it has already been set for the same
|
|
280
|
+
# Records a robots directive unless it has already been set for the same
|
|
281
|
+
# crawler. Crawler names are compared case-insensitively, while the
|
|
282
|
+
# directive is recorded under the first spelling supplied.
|
|
214
283
|
#
|
|
215
284
|
# @param result [Hash{String => Array<String>}] robots directives grouped by tag name.
|
|
216
|
-
# @param name [String
|
|
285
|
+
# @param name [String] robots tag name.
|
|
217
286
|
# @param value [String] robots directive value.
|
|
218
|
-
# @param processed [Set<String>] names already recorded in this pass.
|
|
287
|
+
# @param processed [Set<String>] downcased names already recorded in this pass.
|
|
219
288
|
# @return [void]
|
|
220
289
|
def apply_robots_value(result, name, value, processed)
|
|
221
290
|
name = name.to_s
|
|
222
|
-
return
|
|
291
|
+
return unless processed.add?(name.downcase)
|
|
223
292
|
|
|
224
293
|
result[name] << value
|
|
225
|
-
processed << name
|
|
226
294
|
end
|
|
227
295
|
end
|
|
228
296
|
end
|
data/lib/meta_tags/railtie.rb
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
module MetaTags
|
|
4
4
|
# Hooks MetaTags helpers into Rails controllers and views.
|
|
5
5
|
class Railtie < Rails::Railtie
|
|
6
|
+
initializer "meta_tags.register_deprecator", before: :load_environment_config do |app|
|
|
7
|
+
app.deprecators[:meta_tags] = MetaTags.deprecator if app.respond_to?(:deprecators)
|
|
8
|
+
end
|
|
9
|
+
|
|
6
10
|
initializer "meta_tags.setup_action_controller" do
|
|
7
11
|
ActiveSupport.on_load :action_controller do
|
|
8
12
|
include MetaTags::ControllerHelper
|
data/lib/meta_tags/renderer.rb
CHANGED
|
@@ -77,7 +77,7 @@ module MetaTags
|
|
|
77
77
|
|
|
78
78
|
icon.each do |icon_params|
|
|
79
79
|
icon_params = {rel: "icon", type: "image/x-icon"}.with_indifferent_access.merge(icon_params)
|
|
80
|
-
tags << Tag.new(:link, icon_params)
|
|
80
|
+
tags << Tag.new(:link, icon_params) if icon_params[:href].present?
|
|
81
81
|
end
|
|
82
82
|
end
|
|
83
83
|
|
|
@@ -123,7 +123,8 @@ module MetaTags
|
|
|
123
123
|
end
|
|
124
124
|
elsif alternate.is_a?(Array)
|
|
125
125
|
alternate.each do |link_params|
|
|
126
|
-
|
|
126
|
+
link_params = {rel: "alternate"}.with_indifferent_access.merge(link_params)
|
|
127
|
+
tags << Tag.new(:link, link_params) if link_params[:href].present?
|
|
127
128
|
end
|
|
128
129
|
end
|
|
129
130
|
end
|
|
@@ -160,7 +161,7 @@ module MetaTags
|
|
|
160
161
|
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
161
162
|
def render_canonical_link(tags)
|
|
162
163
|
href = meta_tags.extract(:canonical) # extract, so it's not used anywhere else
|
|
163
|
-
return if MetaTags.config.skip_canonical_links_on_noindex && meta_tags
|
|
164
|
+
return if MetaTags.config.skip_canonical_links_on_noindex && meta_tags.noindex?
|
|
164
165
|
return if href.blank?
|
|
165
166
|
|
|
166
167
|
@normalized_meta_tags[:canonical] = href
|
|
@@ -214,7 +215,11 @@ module MetaTags
|
|
|
214
215
|
when Array
|
|
215
216
|
process_array(tags, property, content, itemprop: itemprop)
|
|
216
217
|
else
|
|
217
|
-
|
|
218
|
+
if content.is_a?(Symbol) && MetaTags.config.resolve_symbolic_references_in_arrays
|
|
219
|
+
process_reference(tags, property, content, itemprop: itemprop)
|
|
220
|
+
else
|
|
221
|
+
render_tag(tags, property, content, itemprop: itemprop)
|
|
222
|
+
end
|
|
218
223
|
end
|
|
219
224
|
end
|
|
220
225
|
|
|
@@ -225,8 +230,12 @@ module MetaTags
|
|
|
225
230
|
# @param content [Hash] nested meta tag attributes.
|
|
226
231
|
# @param itemprop [String, Symbol, nil] inherited itemprop value.
|
|
227
232
|
def process_hash(tags, property, content, itemprop: nil)
|
|
228
|
-
|
|
233
|
+
itemprop_key = content.key?(:itemprop) ? :itemprop : "itemprop"
|
|
234
|
+
current_itemprop = content.key?(itemprop_key) ? content[itemprop_key] : itemprop
|
|
235
|
+
|
|
229
236
|
content.each do |key, value|
|
|
237
|
+
next if key.to_s == "itemprop"
|
|
238
|
+
|
|
230
239
|
if key.to_s == "_"
|
|
231
240
|
key = property
|
|
232
241
|
next_itemprop = current_itemprop
|
|
@@ -235,12 +244,11 @@ module MetaTags
|
|
|
235
244
|
next_itemprop = nil
|
|
236
245
|
end
|
|
237
246
|
|
|
238
|
-
|
|
239
|
-
|
|
247
|
+
if value.is_a?(Symbol)
|
|
248
|
+
process_reference(tags, key, value, itemprop: next_itemprop)
|
|
240
249
|
else
|
|
241
|
-
value
|
|
250
|
+
process_tree(tags, key, value, itemprop: next_itemprop)
|
|
242
251
|
end
|
|
243
|
-
process_tree(tags, key, normalized_value, itemprop: next_itemprop)
|
|
244
252
|
end
|
|
245
253
|
end
|
|
246
254
|
|
|
@@ -251,9 +259,39 @@ module MetaTags
|
|
|
251
259
|
# @param content [Array] array of nested meta tag attributes or values.
|
|
252
260
|
# @param itemprop [String, Symbol, nil] value of the itemprop attribute.
|
|
253
261
|
def process_array(tags, property, content, itemprop: nil)
|
|
262
|
+
unless MetaTags.config.resolve_symbolic_references_in_arrays
|
|
263
|
+
symbol_reference = content.find { |value| value.is_a?(Symbol) }
|
|
264
|
+
if symbol_reference
|
|
265
|
+
MetaTags.deprecator.warn(
|
|
266
|
+
"Passing #{symbol_reference.inspect} directly inside a nested meta tag Array is deprecated. " \
|
|
267
|
+
"It is rendered literally in MetaTags 2.x but will be interpreted as a mirrored reference in MetaTags 3.0. " \
|
|
268
|
+
"Set MetaTags.config.resolve_symbolic_references_in_arrays = true to opt in now; this behavior will " \
|
|
269
|
+
"become the default in MetaTags 3.0. Use a String to keep a literal value."
|
|
270
|
+
)
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
254
274
|
content.each { |value| process_tree(tags, property, value, itemprop: itemprop) }
|
|
255
275
|
end
|
|
256
276
|
|
|
277
|
+
# Resolves a Symbol reference and processes its normalized value once.
|
|
278
|
+
#
|
|
279
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
280
|
+
# @param property [String, Symbol] the meta tag property name.
|
|
281
|
+
# @param reference [Symbol] normalized top-level meta tag reference.
|
|
282
|
+
# @param itemprop [String, Symbol, nil] value of the itemprop attribute.
|
|
283
|
+
def process_reference(tags, property, reference, itemprop: nil)
|
|
284
|
+
content = normalized_meta_tags[reference]
|
|
285
|
+
case content
|
|
286
|
+
when Hash
|
|
287
|
+
process_hash(tags, property, content, itemprop: itemprop)
|
|
288
|
+
when Array
|
|
289
|
+
process_array(tags, property, content, itemprop: itemprop)
|
|
290
|
+
else
|
|
291
|
+
render_tag(tags, property, content, itemprop: itemprop)
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
257
295
|
# Renders a single meta tag.
|
|
258
296
|
#
|
|
259
297
|
# @param tags [Array<Tag>] a buffer object to store tags in.
|
data/lib/meta_tags/tag.rb
CHANGED
|
@@ -19,11 +19,35 @@ module MetaTags
|
|
|
19
19
|
# @param view [ActionView::Base] instance of a Rails view.
|
|
20
20
|
# @return [String] HTML string for the tag.
|
|
21
21
|
def render(view)
|
|
22
|
-
view.tag(name, serialize_iso8601_attributes(attributes), MetaTags.config.open_meta_tags?)
|
|
22
|
+
view.tag(name, drop_blank_attribute_names(serialize_iso8601_attributes(attributes)), MetaTags.config.open_meta_tags?)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
protected
|
|
26
26
|
|
|
27
|
+
# Removes attributes whose names are blank once converted to a String,
|
|
28
|
+
# including names nested under +data+ and +aria+. Rails 6.1 through 7.2,
|
|
29
|
+
# 8.0 before 8.0.5.2, and 8.1 before 8.1.4.1 render such a name as a
|
|
30
|
+
# nameless attribute, which lets its value be parsed as further
|
|
31
|
+
# attributes (CVE-2026-33168, GHSA-68p9-83h6-5wpj). Patched Rails drops
|
|
32
|
+
# these names too.
|
|
33
|
+
#
|
|
34
|
+
# @param attributes [Hash] HTML tag attributes.
|
|
35
|
+
# @return [Hash] HTML tag attributes without blank names.
|
|
36
|
+
def drop_blank_attribute_names(attributes)
|
|
37
|
+
# @type var result: Hash[String | Symbol, untyped]
|
|
38
|
+
result = {}
|
|
39
|
+
attributes.each do |key, value|
|
|
40
|
+
name = key.to_s
|
|
41
|
+
next if name.blank?
|
|
42
|
+
|
|
43
|
+
if value.is_a?(Hash) && (name == "data" || name == "aria")
|
|
44
|
+
value = value.reject { |nested_key, _| nested_key.to_s.blank? }
|
|
45
|
+
end
|
|
46
|
+
result[key] = value
|
|
47
|
+
end
|
|
48
|
+
result
|
|
49
|
+
end
|
|
50
|
+
|
|
27
51
|
# Serializes attribute values that support ISO 8601 formatting.
|
|
28
52
|
#
|
|
29
53
|
# @param attributes [Hash] HTML tag attributes.
|
|
@@ -11,13 +11,16 @@ module MetaTags
|
|
|
11
11
|
# @param title [Array<String>] title segments.
|
|
12
12
|
# @param separator [String] a string to join title parts with.
|
|
13
13
|
# @param reverse [Boolean] whether title should be reversed.
|
|
14
|
+
# @param lowercase [Boolean] whether title segments should be lowercased.
|
|
14
15
|
# @return [String] title with HTML tags removed.
|
|
15
|
-
def normalize_title(site_title, title, separator, reverse
|
|
16
|
-
|
|
16
|
+
def normalize_title(site_title, title, separator, reverse: false, lowercase: false)
|
|
17
|
+
limit = MetaTags.config.title_limit
|
|
18
|
+
clean_title = cleanup_strings(title, limit: limit)
|
|
19
|
+
clean_title.each(&:downcase!) if lowercase
|
|
17
20
|
clean_title.reverse! if reverse
|
|
18
21
|
|
|
19
|
-
site_title = cleanup_string(site_title)
|
|
20
|
-
separator = cleanup_string(separator, strip: false)
|
|
22
|
+
site_title = cleanup_string(site_title, limit: limit)
|
|
23
|
+
separator = cleanup_string(separator, strip: false, limit: limit)
|
|
21
24
|
|
|
22
25
|
# Truncate title and site title
|
|
23
26
|
site_title, clean_title = truncate_title(site_title, clean_title, separator)
|
|
@@ -41,10 +44,11 @@ module MetaTags
|
|
|
41
44
|
# description could be another object not a string, but since it probably
|
|
42
45
|
# serves the same purpose, we can ask it to convert itself to a string
|
|
43
46
|
# and continue from there
|
|
44
|
-
|
|
47
|
+
limit = MetaTags.config.description_limit
|
|
48
|
+
description = cleanup_string(description, limit: limit)
|
|
45
49
|
return "" if description.blank?
|
|
46
50
|
|
|
47
|
-
truncate(description,
|
|
51
|
+
truncate(description, limit)
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
# Normalize keywords value.
|
|
@@ -52,14 +56,22 @@ module MetaTags
|
|
|
52
56
|
# @param keywords [String, Array<String>] list of keywords as a string or Array.
|
|
53
57
|
# @return [String] list of keywords joined with comma, with tags removed.
|
|
54
58
|
def normalize_keywords(keywords)
|
|
55
|
-
|
|
59
|
+
limit = MetaTags.config.keywords_limit
|
|
60
|
+
lowercase = MetaTags.config.keywords_lowercase
|
|
61
|
+
keywords = Array(keywords).flatten
|
|
62
|
+
keywords = keywords.lazy if keywords.size > 32 && limit.to_i > 0
|
|
63
|
+
keywords = keywords.filter_map do |keyword|
|
|
64
|
+
string = cleanup_string(keyword, limit: limit)
|
|
65
|
+
next if string.blank?
|
|
66
|
+
|
|
67
|
+
string.downcase! if lowercase
|
|
68
|
+
string
|
|
69
|
+
end
|
|
56
70
|
return "" if keywords.blank?
|
|
57
71
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
keywords = truncate_array(keywords, MetaTags.config.keywords_limit, separator)
|
|
62
|
-
safe_join(keywords, separator)
|
|
72
|
+
separator = cleanup_string MetaTags.config.keywords_separator, strip: false, limit: limit
|
|
73
|
+
keywords = truncate_array(keywords, limit, separator).to_a
|
|
74
|
+
keywords.empty? ? "" : safe_join(keywords, separator)
|
|
63
75
|
end
|
|
64
76
|
|
|
65
77
|
# Easy way to get access to Rails helpers.
|
|
@@ -75,8 +87,23 @@ module MetaTags
|
|
|
75
87
|
# @return [String] HTML-safe string with no HTML tags.
|
|
76
88
|
def strip_tags(string)
|
|
77
89
|
if defined?(Loofah)
|
|
78
|
-
#
|
|
79
|
-
|
|
90
|
+
# Only plain UTF-8 text can skip parsing without encoding or control changes.
|
|
91
|
+
if string.encoding == Encoding::UTF_8 && string.ascii_only? && !string.match?(/[<&\x00-\x08\x0B-\x1F\x7F]/)
|
|
92
|
+
# Loofah returns US-ASCII for an empty fragment, UTF-8 for other text.
|
|
93
|
+
encoding = string.empty? ? Encoding::US_ASCII : Encoding::UTF_8
|
|
94
|
+
# Do not retain the caller's HTML-safety flag or mutate its string.
|
|
95
|
+
return String.new(string, encoding: encoding)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Instead of strip_tags we will use Loofah to strip tags from now on.
|
|
99
|
+
# Like Rails' strip_tags, drop script and style source, including the
|
|
100
|
+
# rest of the value after an unclosed <script> or <style>.
|
|
101
|
+
fragment = Loofah.fragment(string)
|
|
102
|
+
# Separate queries stay linear: DocumentFragment#css queries each
|
|
103
|
+
# top-level node, and an XPath union is quadratic in its matches.
|
|
104
|
+
fragment.xpath(".//script").remove
|
|
105
|
+
fragment.xpath(".//style").remove
|
|
106
|
+
fragment.text(encode_special_chars: false)
|
|
80
107
|
else
|
|
81
108
|
helpers.strip_tags(string)
|
|
82
109
|
end
|
|
@@ -96,15 +123,20 @@ module MetaTags
|
|
|
96
123
|
|
|
97
124
|
# Removes HTML tags and squashes down all the spaces.
|
|
98
125
|
#
|
|
126
|
+
# With a positive +limit+, markup beyond what a value truncated to +limit+
|
|
127
|
+
# can use is not parsed. See #truncate_markup.
|
|
128
|
+
#
|
|
99
129
|
# @param string [String, nil] input string.
|
|
100
130
|
# @param strip [Boolean] whether to trim leading and trailing whitespace.
|
|
131
|
+
# @param limit [Integer, nil] number of characters the result will be
|
|
132
|
+
# truncated to, or +nil+ or 0 to parse the whole input.
|
|
101
133
|
# @return [String] input string with no HTML tags and consecutive
|
|
102
134
|
# whitespace characters squashed into a single space.
|
|
103
|
-
def cleanup_string(string, strip: true)
|
|
135
|
+
def cleanup_string(string, strip: true, limit: nil)
|
|
104
136
|
return "" if string.nil?
|
|
105
137
|
raise ArgumentError, "Expected a string or an object that implements #to_str" unless string.respond_to?(:to_str)
|
|
106
138
|
|
|
107
|
-
s = strip_tags(string.to_str)
|
|
139
|
+
s = strip_tags(truncate_markup(string.to_str, limit))
|
|
108
140
|
s = s.dup if s.frozen?
|
|
109
141
|
s.gsub!(/\s+/, " ")
|
|
110
142
|
s.strip! if strip
|
|
@@ -116,10 +148,11 @@ module MetaTags
|
|
|
116
148
|
#
|
|
117
149
|
# @param strings [String, Array<String>] input string(s).
|
|
118
150
|
# @param strip [Boolean] whether to trim leading and trailing whitespace.
|
|
151
|
+
# @param limit [Integer, nil] number of characters each result will be truncated to.
|
|
119
152
|
# @return [Array<String>] clean strings.
|
|
120
153
|
# @see cleanup_string
|
|
121
|
-
def cleanup_strings(strings, strip: true)
|
|
122
|
-
strings = Array(strings).flatten.map! { |s| cleanup_string(s, strip: strip) }
|
|
154
|
+
def cleanup_strings(strings, strip: true, limit: nil)
|
|
155
|
+
strings = Array(strings).flatten.map! { |s| cleanup_string(s, strip: strip, limit: limit) }
|
|
123
156
|
strings.reject!(&:blank?)
|
|
124
157
|
strings
|
|
125
158
|
end
|
|
@@ -144,21 +177,22 @@ module MetaTags
|
|
|
144
177
|
|
|
145
178
|
# Truncates an array of strings to a specific limit.
|
|
146
179
|
#
|
|
147
|
-
# @param string_array [
|
|
180
|
+
# @param string_array [Enumerable<String>] input strings.
|
|
148
181
|
# @param limit [Integer, nil] number of characters to truncate to.
|
|
149
182
|
# @param separator [String] separator that will be used to join the array later.
|
|
150
|
-
# @return [
|
|
183
|
+
# @return [Enumerable<String>] truncated array, or the original input if unlimited.
|
|
151
184
|
def truncate_array(string_array, limit = nil, separator = "")
|
|
152
185
|
return string_array if limit.nil? || limit <= 0
|
|
153
186
|
|
|
154
187
|
length = 0
|
|
188
|
+
preserve_items = MetaTags.config.truncate_array_items_at_boundaries
|
|
155
189
|
result = [] # : Array[String]
|
|
156
190
|
|
|
157
191
|
string_array.each do |string|
|
|
158
192
|
limit_left = calculate_limit_left(limit, length, result, separator)
|
|
159
193
|
|
|
160
194
|
if string.length > limit_left
|
|
161
|
-
result << truncate(string, limit_left)
|
|
195
|
+
result << truncate(string, limit_left) if !preserve_items || result.empty?
|
|
162
196
|
break string_array
|
|
163
197
|
end
|
|
164
198
|
|
|
@@ -174,6 +208,29 @@ module MetaTags
|
|
|
174
208
|
|
|
175
209
|
private
|
|
176
210
|
|
|
211
|
+
# Cuts +string+ before its <tt>([limit * 4, 2048].max + 1)</tt>th "<", so the
|
|
212
|
+
# number of tags, comments, and other markup the HTML parser handles, which
|
|
213
|
+
# drives its cost, does not grow with the size of a value that will be
|
|
214
|
+
# truncated to +limit+ characters. Text before a "<" is complete, so the cut
|
|
215
|
+
# never splits a character or a character reference, and long attribute
|
|
216
|
+
# values, scripts, and runs of text or whitespace are kept whole. Every "<"
|
|
217
|
+
# counts, including those inside scripts, comments, and attribute values,
|
|
218
|
+
# because telling them apart takes the parser itself.
|
|
219
|
+
#
|
|
220
|
+
# @param string [String] input string.
|
|
221
|
+
# @param limit [Integer, nil] number of characters the result will be truncated to.
|
|
222
|
+
# @return [String] the input, or a prefix of it when it has too much markup.
|
|
223
|
+
def truncate_markup(string, limit)
|
|
224
|
+
return string if limit.to_i <= 0 || !string.encoding.ascii_compatible?
|
|
225
|
+
|
|
226
|
+
markup_limit = [limit.to_i * 4, 2048].max
|
|
227
|
+
return string if string.count("<") <= markup_limit
|
|
228
|
+
|
|
229
|
+
offset = 0
|
|
230
|
+
markup_limit.times { offset = string.byteindex("<", offset).to_i + 1 }
|
|
231
|
+
string.byteslice(0, string.byteindex("<", offset).to_i).to_s
|
|
232
|
+
end
|
|
233
|
+
|
|
177
234
|
# Calculates how many characters are left for the next segment.
|
|
178
235
|
#
|
|
179
236
|
# @param limit [Integer] total truncation limit.
|
data/lib/meta_tags/version.rb
CHANGED
|
@@ -151,6 +151,9 @@ module MetaTags
|
|
|
151
151
|
# @option default [String, Integer] :refresh (nil) meta refresh tag;
|
|
152
152
|
# @option default [Hash] :open_graph ({}) add Open Graph meta tags.
|
|
153
153
|
# @option default [Hash] :open_search ({}) add Open Search link tag.
|
|
154
|
+
# @option default [Hash] :robots ({}) add robots meta tags.
|
|
155
|
+
# @option default [Hash] :googlebot ({}) add googlebot meta tags.
|
|
156
|
+
# @option default [Hash] :bingbot ({}) add bingbot meta tags.
|
|
154
157
|
# @return [String] HTML meta tags to render in HEAD section of the
|
|
155
158
|
# HTML document.
|
|
156
159
|
# @example Render meta tags in a layout
|
data/lib/meta_tags.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "set"
|
|
4
3
|
require "active_support/core_ext/hash/indifferent_access"
|
|
4
|
+
require "active_support/deprecation"
|
|
5
5
|
|
|
6
6
|
# MetaTags gem namespace.
|
|
7
7
|
module MetaTags
|
|
@@ -10,6 +10,11 @@ module MetaTags
|
|
|
10
10
|
@config ||= Configuration.new
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
+
# Returns the deprecator used for MetaTags API warnings.
|
|
14
|
+
def self.deprecator
|
|
15
|
+
@deprecator ||= ActiveSupport::Deprecation.new("3.0", "MetaTags")
|
|
16
|
+
end
|
|
17
|
+
|
|
13
18
|
# Configures MetaTags gem.
|
|
14
19
|
#
|
|
15
20
|
# @yield [Configuration] configuration object.
|