-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from htmlacademy/fix/tag-req-attr
Fix tag-req-attr
- Loading branch information
Showing
2 changed files
with
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# htmlacademy/tag-req-attr | ||
|
||
Если установлено, указанные атрибуты должны присутствовать в указанном теге. | ||
|
||
Форк: https://linthtml.vercel.app/user-guide/rules/list/tag-req-attr | ||
|
||
## true | ||
|
||
```json | ||
'htmlacademy/tag-req-attr': [ | ||
true, { | ||
'input': [ | ||
{ | ||
name: 'name' | ||
}, | ||
], | ||
// Другие элементы... | ||
}, | ||
] | ||
``` | ||
|
||
```json | ||
{ | ||
"tag-req-attr": [ | ||
true, | ||
{ | ||
"img": [ | ||
{ | ||
"name": "src" | ||
}, | ||
{ | ||
"name": "alt" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Нарушениями считаются следующие модели: | ||
|
||
```html | ||
<img/> | ||
``` | ||
|
||
```html | ||
<img src="link"/> | ||
``` | ||
|
||
```html | ||
<img alt="No image"> | ||
``` | ||
|
||
Следующие детали не считаются нарушениями: | ||
|
||
```html | ||
<img alt="Picture of a cute cat" src="https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwiHzdu5n4ThAhXOxYUKHebmDXoQjRx6BAgBEAU&url=https%3A%2F%2Fimgur.com%2Fgallery%2FHzG2YW8&psig=AOvVaw3w5Zu0oMuDZy83zsfn0NMU&ust=1552742695628256"> | ||
``` | ||
|
||
## ignore | ||
|
||
Поле `ignore` позволяет игнорировать атрибуты в зависимости от их значений. | ||
|
||
```json | ||
{ | ||
'htmlacademy/tag-req-attr': [ | ||
true, | ||
{ | ||
'input': [ | ||
{ | ||
name: 'name', | ||
ignore: { | ||
'type': 'submit' | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Нарушениями считаются следующие модели: | ||
|
||
```html | ||
<input name="name" type="submit"> | ||
``` | ||
|
||
Следующие детали **не** считаются нарушениями: | ||
|
||
Если у элемента `input` атрибут `type` имеет значение `submit`, то атрибут `name` не обязателен. | ||
```html | ||
<input type="submit" value="Submit"> | ||
``` |
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,51 @@ | ||
'use strict'; | ||
// eslint-disable-next-line camelcase | ||
const { is_tag_node, has_non_empty_attribute, has_attribute, attribute_value } = require('@linthtml/dom-utils'); | ||
const checkAttributes = (node, requiredAttributes, report) => { | ||
requiredAttributes.forEach(({ name, allowEmpty, ignore }) => { | ||
allowEmpty = typeof allowEmpty === 'undefined' ? false : allowEmpty; | ||
if (ignore) { | ||
let shouldIgnore = false; | ||
for (const key in ignore) { | ||
if (has_attribute(node, key) && attribute_value(node, key).chars === ignore[key]) { | ||
shouldIgnore = true; | ||
break; | ||
} | ||
} | ||
if (shouldIgnore) { | ||
return; | ||
} | ||
} | ||
|
||
if (!has_attribute(node, name) || !has_non_empty_attribute(node, name, allowEmpty)) { | ||
report({ | ||
code: 'E057', | ||
position: node.open.loc, | ||
meta: { | ||
data: { | ||
attribute: name, | ||
tag: node.name | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
|
||
module.exports = { | ||
name: 'htmlacademy/tag-req-attr', | ||
// eslint-disable-next-line camelcase | ||
lint(node, rule_config, { report }) { | ||
if (is_tag_node(node)) { | ||
// eslint-disable-next-line camelcase | ||
for (const tagName in rule_config) { | ||
if (Object.hasOwnProperty.call(rule_config, tagName) && tagName === node.name) { // Ensured property belongs to object | ||
// eslint-disable-next-line camelcase | ||
const requiredAttributes = rule_config[tagName]; | ||
checkAttributes(node, requiredAttributes, report); | ||
} | ||
} | ||
} | ||
}, | ||
}; |