title | id | category |
---|---|---|
Required attributes |
required-attr |
validation |
Warns if specified attributes or required attribute on specs are not appeared on an element.
This rule refer HTML Living Standard based MDN Web docs. It has settings in @markuplint/html-spec
.
The src
attribute is required on <img>
element on HTML Living Standard based MDN Web docs.
👎 Example of incorrect code for this rule
<img />
👍 Example of correct code for this rule
<img src="/path/to/image.png" />
When the alt
attribute is required, set {"required-attr": "alt"}
.
👎 Example of incorrect code for this rule
<img src="/path/to/image.png" />
👍 Example of correct code for this rule
<img src="/path/to/image.png" alt="alternative text" />
{
"rules": {
"required-attr": "alt"
}
}
{
"rules": {
"required-attr": ["alt", "src"]
}
}
Type: string | string[]
value | default | description |
---|---|---|
"attribute-name" |
[] | Attribute name string or array of attribute names to warn if they are not appeared. |
Since we ordinary want to configure required attributes for each element type, required-attr
rule should be configured in the nodeRules
option.
Example configuration that alt
attribute must be required on <img>
element:
{
"rules": {
"required-attr": true
},
"nodeRules": [
{
"tagName": "img",
"rules": {
"required-attr": "alt"
}
}
]
}