import dedent from "dedent"
import { describe, test } from "vitest"
import { HTMLNoEmptyAttributesRule } from "../../src/rules/html-no-empty-attributes.js"
import { createLinterTest } from "../helpers/linter-test-helper.js"
const { expectNoOffenses, expectWarning, assertOffenses } = createLinterTest(HTMLNoEmptyAttributesRule)
describe("html-no-empty-attributes", () => {
test("passes for attributes with meaningful values", () => {
expectNoOffenses(`
`)
})
test("fails for empty id attribute", () => {
expectWarning('Attribute `id` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for empty class attribute", () => {
expectWarning('Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for empty name attribute", () => {
expectWarning('Attribute `name` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for empty for attribute", () => {
expectWarning('Attribute `for` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for empty src attribute", () => {
expectWarning('Attribute `src` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for empty href attribute", () => {
expectWarning('Attribute `href` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(`Link`)
})
test("fails for empty title attribute", () => {
expectWarning('Attribute `title` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(`Link`)
})
test("fails for empty data attribute", () => {
expectWarning('Attribute `data` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for data-* attributes with empty values", () => {
expectWarning('Data attribute `data-value` should not have an empty value. Either provide a meaningful value or use `data-value` instead of `data-value=""`.')
expectWarning('Data attribute `data-config` should not have an empty value. Either provide a meaningful value or use `data-config` instead of `data-config=""`.')
assertOffenses(``)
})
test("fails for aria-* attributes with empty values", () => {
expectWarning('Attribute `aria-label` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
expectWarning('Attribute `aria-describedby` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for role attribute with empty value", () => {
expectWarning('Attribute `role` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("fails for multiple empty attributes", () => {
expectWarning('Attribute `id` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
expectWarning('Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
expectWarning('Data attribute `data-test` should not have an empty value. Either provide a meaningful value or use `data-test` instead of `data-test=""`.')
assertOffenses(``)
})
test("fails for whitespace-only values", () => {
expectWarning('Attribute `id` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
expectWarning('Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("passes for attributes with ERB output", () => {
expectNoOffenses(``)
})
test("passes for mixed static and ERB content", () => {
expectNoOffenses(``)
})
test("fails for dynamic data-* attribute name with empty value", () => {
expectWarning('Data attribute `data-<%= key %>` should not have an empty value. Either provide a meaningful value or use `data-<%= key %>` instead of `data-<%= key %>=""`.')
assertOffenses(`
="">
`)
})
test("fails for dynamic data-* composite name with empty value", () => {
expectWarning('Data attribute `data-<%= key %>-id` should not have an empty value. Either provide a meaningful value or use `data-<%= key %>-id` instead of `data-<%= key %>-id=""`.')
assertOffenses(`
-id="">
`)
})
test("fails for dynamic data-* attribute name with whitespace-only value", () => {
expectWarning('Data attribute `data-<%= key %>` should not have an empty value. Either provide a meaningful value or use `data-<%= key %>` instead of `data-<%= key %>=" "`.')
assertOffenses(`
=" ">
`)
})
test("fails for dynamic aria-* attribute name with empty value", () => {
expectWarning('Attribute `aria-<%= prop %>` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("passes for dynamic attribute name with ERB value", () => {
expectNoOffenses(`
="<%= value %>">
`)
})
test("fails for attribute with ERB that doesn't output anything", () => {
expectWarning('Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(``)
})
test("passes for attribute with static value and ERB that doesn't output anything", () => {
expectNoOffenses(``)
})
test("passes for data-* attributes without explicit values", () => {
expectNoOffenses(``)
})
test("fails for data-* attributes with explicit empty string values", () => {
expectWarning('Data attribute `data-test` should not have an empty value. Either provide a meaningful value or use `data-test` instead of `data-test=""`.')
expectWarning('Data attribute `data-value` should not have an empty value. Either provide a meaningful value or use `data-value` instead of `data-value=""`.')
assertOffenses(``)
})
test("mixed data attributes: passes for implicit values, fails for explicit empty values", () => {
expectWarning('Data attribute `data-config` should not have an empty value. Either provide a meaningful value or use `data-config` instead of `data-config=""`.')
assertOffenses(``)
})
test("passes for data-turbo-permanent without value", () => {
expectNoOffenses(`
Content
`)
})
test("fails for data-turbo-permanent with explicit empty value", () => {
expectWarning('Data attribute `data-turbo-permanent` should not have an empty value. Either provide a meaningful value or use `data-turbo-permanent` instead of `data-turbo-permanent=""`.')
assertOffenses(`
Content
`)
})
test("passes for class with ERB control flow containing static text", () => {
expectNoOffenses(dedent`
Content
`)
})
test("passes for class with ERB control flow containing output tags", () => {
expectNoOffenses(dedent`
Content
`)
})
test("fails for class with ERB control flow containing only non-output tags", () => {
expectWarning('Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.')
assertOffenses(dedent`
Content
`)
})
test("passes for attribute containing an escaped ERB tag", () => {
expectNoOffenses(dedent`
Content
`)
})
describe("ActionView tag helpers", () => {
test("passes for tag.div with a non-empty attribute", () => {
expectNoOffenses('<%= tag.div class: "container" %>')
})
test("fails for tag.div with an empty attribute", () => {
expectWarning("Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.")
assertOffenses('<%= tag.div class: "" %>')
})
test("passes for an empty data attribute, which can't be written without a value", () => {
expectNoOffenses('<%= tag.div data: { foo: "" } %>')
})
test("passes for an empty data attribute written as a dasherized string key", () => {
expectNoOffenses('<%= tag.div "data-foo": "" %>')
})
test("fails for an empty aria attribute, which has no valueless form in HTML either", () => {
expectWarning("Attribute `aria-label` must not be empty. Either provide a meaningful value or remove the attribute entirely.")
assertOffenses('<%= tag.div aria: { label: "" } %>')
})
test("fails for content_tag with an empty attribute", () => {
expectWarning("Attribute `class` must not be empty. Either provide a meaningful value or remove the attribute entirely.")
assertOffenses('<%= content_tag :div, "content", class: "" %>')
})
test("passes for a dynamic value, which must not be read as an empty one", () => {
expectNoOffenses('<%= tag.div class: value %>')
})
test("passes for an interpolated value", () => {
expectNoOffenses('<%= tag.div class: "btn-#{kind}" %>')
})
test("passes for a nil value, which ActionView omits entirely", () => {
expectNoOffenses('<%= tag.div class: nil %>')
})
test("passes for a nil data value, which ActionView omits entirely", () => {
expectNoOffenses('<%= tag.div data: { foo: nil } %>')
})
})
})