Skip to content

Extend SpaceInHtmlTag to trim values #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/erb_lint/linters/space_in_html_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ def process_attributes(processed_source, attributes)
name, equal, value = *attribute
no_space(processed_source, name.loc.end_pos...equal.loc.begin_pos) if name && equal
no_space(processed_source, equal.loc.end_pos...value.loc.begin_pos) if equal && value
if value && quoted_value?(value)
children = value.children.dup
open_quote = children.shift
close_quote = children.pop

leading_spaces_count = if children.first.is_a?(String)
children.first.chars.take_while { |char| char == " " }.count
else
# value starts with erb tag
0
end

trailing_spaces_count = if children.last.is_a?(String)
children.last.chars.reverse.take_while { |char| char == " " }.count
else
# value ends with erb tag
0
end
first_non_space_pos = open_quote.loc.end_pos + leading_spaces_count
last_non_space_pos = close_quote.loc.begin_pos - trailing_spaces_count
no_space(processed_source, (open_quote.loc.end_pos)...first_non_space_pos)
no_space(processed_source, last_non_space_pos...close_quote.loc.begin_pos)
end

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

Expand All @@ -107,6 +130,12 @@ def process_attributes(processed_source, attributes)
)
end
end

def quoted_value?(value)
value.children.length >= 3 &&
value.children.first.type == :quote &&
value.children.last.type == :quote
end
end
end
end
63 changes: 63 additions & 0 deletions spec/erb_lint/linters/space_in_html_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
it { expect(subject).to(eq([])) }
end

context "self-closing tag with valueless attribute" do
let(:file) { "<input autofocus />" }
it { expect(subject).to(eq([])) }
end

context "self-closing tag with no quotes around attribute value" do
let(:file) { "<input class=foo />" }
it { expect(subject).to(eq([])) }
end

context "between attributes" do
let(:file) { '<input class="foo" name="bar" />' }
it { expect(subject).to(eq([])) }
Expand All @@ -73,6 +83,13 @@
it { expect(subject).to(eq([])) }
end

context "tag with erb in attribute value" do
let(:file) { <<~HTML }
<input class='<%= foo %>' />
HTML
it { expect(subject).to(eq([])) }
end

context "multi-line tag with erb" do
let(:file) { <<~HTML }
<input
Expand Down Expand Up @@ -147,6 +164,42 @@
]))
end
end

context "at start of value" do
let(:file) { "<div foo=' bar'>" }
it do
expect(subject).to(eq([
build_offense(10..11, "Extra space detected where there should be no space."),
]))
end
end

context "at start of value with embedded ruby" do
let(:file) { "<div foo=' <%= foo %>'>" }
it do
expect(subject).to(eq([
build_offense(10..11, "Extra space detected where there should be no space."),
]))
end
end

context "at end of value" do
let(:file) { "<div foo='bar '>" }
it do
expect(subject).to(eq([
build_offense(13..14, "Extra space detected where there should be no space."),
]))
end
end

context "at end of value with embedded ruby" do
let(:file) { "<div foo='<%= foo %> <%= bar %> '>" }
it do
expect(subject).to(eq([
build_offense(31..32, "Extra space detected where there should be no space."),
]))
end
end
end

context "when space is missing" do
Expand Down Expand Up @@ -431,6 +484,16 @@
let(:file) { "<div foo= 'bar'>" }
it { expect(subject).to(eq("<div foo='bar'>")) }
end

context "at start of value" do
let(:file) { "<div foo=' bar'>" }
it { expect(subject).to(eq("<div foo='bar'>")) }
end

context "at end of value" do
let(:file) { "<div foo='bar '>" }
it { expect(subject).to(eq("<div foo='bar'>")) }
end
end

context "when space is missing" do
Expand Down