Skip to content
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

Extend SpaceInHtmlTag to trim values #389

Open
wants to merge 1 commit 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
13 changes: 13 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,15 @@ 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)
open_quote, str, close_quote = *value
leading_spaces = str.chars.take_while { |char| char == " " }.count
trailing_spaces = str.chars.reverse.take_while { |char| char == " " }.count
first_non_space_pos = open_quote.loc.end_pos + leading_spaces
last_non_space_pos = close_quote.loc.begin_pos - trailing_spaces
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 +116,10 @@ def process_attributes(processed_source, attributes)
)
end
end

def quoted_value?(value)
value.children.length == 3
end
end
end
end
38 changes: 38 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 Down Expand Up @@ -147,6 +157,24 @@
]))
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 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
end

context "when space is missing" do
Expand Down Expand Up @@ -431,6 +459,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
Loading