-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a `TagAttribute` linter to forbid the use of the specified tag attribute. The following use cases are considered. * Forbid `style=""` attribute and use the css embedded engine or css file instead. * Forbid `class=""` attribute and use `.class` syntax instead.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module SlimLint | ||
# Checks for forbidden tag attributes. | ||
class Linter::TagAttribute < Linter | ||
include LinterRegistry | ||
|
||
on [:html, :attr] do |sexp| | ||
_, _, name = sexp | ||
|
||
forbidden_attributes = config['forbidden_attributes'] | ||
forbidden_attributes.each do |forbidden_attribute| | ||
next unless name[/^#{forbidden_attribute}$/i] | ||
|
||
report_lint(sexp, "Forbidden tag attribute `#{name}` found") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe SlimLint::Linter::TagAttribute do | ||
include_context 'linter' | ||
|
||
context 'when a lowercased attribute is contained in forbidden_attributes' do | ||
let(:config) do | ||
{ 'forbidden_attributes' => %w[style] } | ||
end | ||
|
||
let(:slim) { 'p style="{ color: red; }"' } | ||
|
||
it { should report_lint line: 1 } | ||
end | ||
|
||
context 'when an uppercased attribute is contained in forbidden_attributes' do | ||
let(:config) do | ||
{ 'forbidden_attributes' => %w[style] } | ||
end | ||
|
||
let(:slim) { 'P STYLE="{ color: red; }"' } | ||
|
||
it { should report_lint line: 1 } | ||
end | ||
|
||
context 'when an attribute is not contained in forbidden_attributes' do | ||
let(:config) do | ||
{ 'forbidden_attributes' => %w[style] } | ||
end | ||
|
||
let(:slim) { 'style media="all"' } | ||
|
||
it { should_not report_lint } | ||
end | ||
end |