Skip to content

Commit bd7fe2a

Browse files
committed
Don't pass a block to input elements
1 parent 7e0d413 commit bd7fe2a

4 files changed

Lines changed: 99 additions & 37 deletions

File tree

lib/superform/rails/components/input.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Superform
22
module Rails
33
module Components
44
class Input < Field
5-
def view_template(&)
5+
def view_template
66
input(**attributes)
77
end
88

lib/superform/rails/field.rb

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,73 +72,73 @@ def human_attribute_name
7272
# field(:birthday).date
7373
# field(:secret).hidden(value: "token123")
7474
# field(:gender).radio("male", id: "user_gender_male")
75-
def text(*, **, &)
76-
input(*, **, type: :text, &)
75+
def text(*, **)
76+
input(*, **, type: :text)
7777
end
7878

79-
def hidden(*, **, &)
80-
input(*, **, type: :hidden, &)
79+
def hidden(*, **)
80+
input(*, **, type: :hidden)
8181
end
8282

83-
def password(*, **, &)
84-
input(*, **, type: :password, &)
83+
def password(*, **)
84+
input(*, **, type: :password)
8585
end
8686

87-
def email(*, **, &)
88-
input(*, **, type: :email, &)
87+
def email(*, **)
88+
input(*, **, type: :email)
8989
end
9090

91-
def url(*, **, &)
92-
input(*, **, type: :url, &)
91+
def url(*, **)
92+
input(*, **, type: :url)
9393
end
9494

95-
def tel(*, **, &)
96-
input(*, **, type: :tel, &)
95+
def tel(*, **)
96+
input(*, **, type: :tel)
9797
end
9898
alias_method :phone, :tel
9999

100-
def number(*, **, &)
101-
input(*, **, type: :number, &)
100+
def number(*, **)
101+
input(*, **, type: :number)
102102
end
103103

104-
def range(*, **, &)
105-
input(*, **, type: :range, &)
104+
def range(*, **)
105+
input(*, **, type: :range)
106106
end
107107

108-
def date(*, **, &)
109-
input(*, **, type: :date, &)
108+
def date(*, **)
109+
input(*, **, type: :date)
110110
end
111111

112-
def time(*, **, &)
113-
input(*, **, type: :time, &)
112+
def time(*, **)
113+
input(*, **, type: :time)
114114
end
115115

116-
def datetime(*, **, &)
117-
input(*, **, type: :"datetime-local", &)
116+
def datetime(*, **)
117+
input(*, **, type: :"datetime-local")
118118
end
119119

120-
def month(*, **, &)
121-
input(*, **, type: :month, &)
120+
def month(*, **)
121+
input(*, **, type: :month)
122122
end
123123

124-
def week(*, **, &)
125-
input(*, **, type: :week, &)
124+
def week(*, **)
125+
input(*, **, type: :week)
126126
end
127127

128-
def color(*, **, &)
129-
input(*, **, type: :color, &)
128+
def color(*, **)
129+
input(*, **, type: :color)
130130
end
131131

132-
def search(*, **, &)
133-
input(*, **, type: :search, &)
132+
def search(*, **)
133+
input(*, **, type: :search)
134134
end
135135

136-
def file(*, **, &)
137-
input(*, **, type: :file, &)
136+
def file(*, **)
137+
input(*, **, type: :file)
138138
end
139139

140-
def radio(value, *, **, &)
141-
input(*, **, type: :radio, value: value, &)
140+
def radio(value, *, **)
141+
input(*, **, type: :radio, value: value)
142142
end
143143

144144
# Rails compatibility aliases

spec/superform/rails/components/input_component_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RSpec.describe Superform::Rails::Components::Input do
1+
RSpec.describe Superform::Rails::Components::Input, type: :view do
22
let(:field) do
33
object = double("object", "foo=": nil)
44
object = double("object", "foo": value)
@@ -65,4 +65,19 @@
6565
end
6666
it { is_expected.to have_client_provided_value }
6767
end
68+
69+
describe "block handling" do
70+
it "does not accept blocks in view_template" do
71+
# Input is a void element and should not render block content
72+
html = render(component)
73+
expect(html).to eq('<input id="foo" name="foo" type="text" value="a string">')
74+
end
75+
76+
it "ignores block content if accidentally passed" do
77+
# Even if someone tries to pass a block, it should be ignored
78+
html = render(component) { "This should not appear" }
79+
expect(html).to eq('<input id="foo" name="foo" type="text" value="a string">')
80+
expect(html).not_to include("This should not appear")
81+
end
82+
end
6883
end

spec/superform/rails/field_convenience_methods_spec.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "spec_helper"
44

5-
RSpec.describe Superform::Rails::Form::Field do
5+
RSpec.describe Superform::Rails::Form::Field, type: :view do
66
let(:user) { User.new(email: "test@example.com", first_name: "John") }
77
let(:form) { Superform::Rails::Form.new(user) }
88
let(:field) { form.field(:email) }
@@ -60,4 +60,51 @@
6060
expect(component.type).to eq("radio")
6161
end
6262
end
63+
64+
describe "block handling" do
65+
# Input elements are void elements and should not accept blocks.
66+
# These tests verify that blocks are properly ignored.
67+
68+
it "does not render block content for text input" do
69+
html = render(field.text { "Block content" })
70+
expect(html).not_to include("Block content")
71+
expect(html).to match(/<input[^>]*type="text"[^>]*>/)
72+
end
73+
74+
it "does not render block content for email input" do
75+
html = render(field.email { "Block content" })
76+
expect(html).not_to include("Block content")
77+
expect(html).to match(/<input[^>]*type="email"[^>]*>/)
78+
end
79+
80+
it "does not render block content for password input" do
81+
html = render(field.password { "Block content" })
82+
expect(html).not_to include("Block content")
83+
expect(html).to match(/<input[^>]*type="password"[^>]*>/)
84+
end
85+
86+
it "does not render block content for hidden input" do
87+
html = render(field.hidden { "Block content" })
88+
expect(html).not_to include("Block content")
89+
expect(html).to match(/<input[^>]*type="hidden"[^>]*>/)
90+
end
91+
92+
it "does not render block content for number input" do
93+
html = render(field.number { "Block content" })
94+
expect(html).not_to include("Block content")
95+
expect(html).to match(/<input[^>]*type="number"[^>]*>/)
96+
end
97+
98+
it "does not render block content for date input" do
99+
html = render(field.date { "Block content" })
100+
expect(html).not_to include("Block content")
101+
expect(html).to match(/<input[^>]*type="date"[^>]*>/)
102+
end
103+
104+
it "does not render block content for file input" do
105+
html = render(field.file { "Block content" })
106+
expect(html).not_to include("Block content")
107+
expect(html).to match(/<input[^>]*type="file"[^>]*>/)
108+
end
109+
end
63110
end

0 commit comments

Comments
 (0)