Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use id as custom selector
  • Loading branch information
timobleeker committed Oct 14, 2020
1 parent 74a337b commit 01283b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lib/recaptcha/helpers.rb
Expand Up @@ -139,7 +139,7 @@ def self.to_message(_key, default)
skip_script = (options.delete(:script) == false) || (options.delete(:external_script) == false)
ui = options.delete(:ui)

data_attribute_keys = [:badge, :theme, :type, :callback, :expired_callback, :error_callback, :size, :selector]
data_attribute_keys = [:badge, :theme, :type, :callback, :expired_callback, :error_callback, :size]
data_attribute_keys << :tabindex unless ui == :button
data_attributes = {}
data_attribute_keys.each do |data_attribute|
Expand Down Expand Up @@ -271,8 +271,7 @@ def self.recaptcha_v3_default_callback_name(action)
private_class_method def self.default_callback(options = {})
nonce = options[:nonce]
nonce_attr = " nonce='#{nonce}'" if nonce
selector_tag = options[:selector]
selector_attr = options[:selector] ? "[data-selector='#{selector_tag}']" : '.g-recaptcha'
selector_attr = options[:id] ? "##{options[:id]}" : ".g-recaptcha"

<<-HTML
<script#{nonce_attr}>
Expand Down
8 changes: 4 additions & 4 deletions test/client_helper_test.rb
Expand Up @@ -210,13 +210,13 @@
end

it "includes a custom selector if provided" do
html = invisible_recaptcha_tags(selector: 'custom-selector')
html.must_include("data-selector=\"custom-selector\"")
html.must_include("document.querySelector(\"[data-selector='custom-selector']\")")
html = invisible_recaptcha_tags(id: 'custom-selector')
html.must_include("id=\"custom-selector\"")
html.must_include("document.querySelector(\"#custom-selector\")")
end

it "uses default selector if no custom selector has been provided" do
html = invisible_recaptcha_tags()
html = invisible_recaptcha_tags
html.must_include("document.querySelector(\".g-recaptcha\")")
end

Expand Down

0 comments on commit 01283b1

Please sign in to comment.