Skip to content

Commit ab17af2

Browse files
committed
Extend SpaceInHtmlTag to trim values
In our code base, we merged in some code that looked like this: ``` <input type=" button " /> ``` Which ultimately did not work. The browser did not understand this. We wondered if ERB Lint could have caught it. And now here we are. What do we think?
1 parent 1430686 commit ab17af2

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/erb_lint/linters/space_in_html_tag.rb

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ def process_attributes(processed_source, attributes)
9696
name, equal, value = *attribute
9797
no_space(processed_source, name.loc.end_pos...equal.loc.begin_pos) if name && equal
9898
no_space(processed_source, equal.loc.end_pos...value.loc.begin_pos) if equal && value
99+
if value && quoted_value?(value)
100+
open_quote, str, close_quote = *value
101+
leading_spaces = str.chars.take_while { |char| char == " " }.count
102+
trailing_spaces = str.chars.reverse.take_while { |char| char == " " }.count
103+
first_non_space_pos = open_quote.loc.end_pos + leading_spaces
104+
last_non_space_pos = close_quote.loc.begin_pos - trailing_spaces
105+
no_space(processed_source, (open_quote.loc.end_pos)...first_non_space_pos)
106+
no_space(processed_source, last_non_space_pos...close_quote.loc.begin_pos)
107+
end
99108

100109
next if index >= attributes.children.size - 1
101110

@@ -107,6 +116,10 @@ def process_attributes(processed_source, attributes)
107116
)
108117
end
109118
end
119+
120+
def quoted_value?(value)
121+
value.children.length == 3
122+
end
110123
end
111124
end
112125
end

spec/erb_lint/linters/space_in_html_tag_spec.rb

+38
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
it { expect(subject).to(eq([])) }
5353
end
5454

55+
context "self-closing tag with valueless attribute" do
56+
let(:file) { "<input autofocus />" }
57+
it { expect(subject).to(eq([])) }
58+
end
59+
60+
context "self-closing tag with no quotes around attribute value" do
61+
let(:file) { "<input class=foo />" }
62+
it { expect(subject).to(eq([])) }
63+
end
64+
5565
context "between attributes" do
5666
let(:file) { '<input class="foo" name="bar" />' }
5767
it { expect(subject).to(eq([])) }
@@ -147,6 +157,24 @@
147157
]))
148158
end
149159
end
160+
161+
context "at start of value" do
162+
let(:file) { "<div foo=' bar'>" }
163+
it do
164+
expect(subject).to(eq([
165+
build_offense(10..11, "Extra space detected where there should be no space."),
166+
]))
167+
end
168+
end
169+
170+
context "at end of value" do
171+
let(:file) { "<div foo='bar '>" }
172+
it do
173+
expect(subject).to(eq([
174+
build_offense(13..14, "Extra space detected where there should be no space."),
175+
]))
176+
end
177+
end
150178
end
151179

152180
context "when space is missing" do
@@ -431,6 +459,16 @@
431459
let(:file) { "<div foo= 'bar'>" }
432460
it { expect(subject).to(eq("<div foo='bar'>")) }
433461
end
462+
463+
context "at start of value" do
464+
let(:file) { "<div foo=' bar'>" }
465+
it { expect(subject).to(eq("<div foo='bar'>")) }
466+
end
467+
468+
context "at end of value" do
469+
let(:file) { "<div foo='bar '>" }
470+
it { expect(subject).to(eq("<div foo='bar'>")) }
471+
end
434472
end
435473

436474
context "when space is missing" do

0 commit comments

Comments
 (0)