Skip to content

Commit 770bd51

Browse files
Требует атрибуты width и height у source, внутри picture (#99)
* Adds req-source-width-height rule * Adds a description for req-source-width-height
1 parent b9e13fe commit 770bd51

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# htmlacademy/req-source-width-height
2+
3+
Правило требует наличие атрибутов `width` и `height` у `<source>`, находящихся внутри `<piture>`.
4+
5+
## true
6+
Если включён, `<source>` без атрибутов `width` и `height` считаются проблемой.
7+
8+
9+
Проблемными считаются следующие шаблоны:
10+
```html
11+
<picture>
12+
<source srcset="images/image-tablet.jpg" media="(min-width: 768px)">
13+
<img src="images/image-mobile.jpg" width="320" height="148" alt="">
14+
</picture>
15+
```
16+
17+
Следующие шаблоны **не** считаются проблемами:
18+
19+
```html
20+
<picture>
21+
<source srcset="images/image-tablet.jpg" width="768" height="480" media="(min-width: 768px)">
22+
<img src="images/image-mobile.jpg" width="320" height="148" alt="">
23+
</picture>
24+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const { has_non_empty_attribute, is_tag_node } = require('@linthtml/dom-utils');
3+
4+
module.exports = {
5+
name: 'htmlacademy/req-source-width-height',
6+
// eslint-disable-next-line camelcase
7+
lint(node, { report }) {
8+
if (is_tag_node(node) && node.name === 'picture') {
9+
node.children.forEach((child) => {
10+
if (is_tag_node(child) && child.name === 'source') {
11+
const hasWidth = has_non_empty_attribute(child, 'width');
12+
const hasHeight = has_non_empty_attribute(child, 'height');
13+
14+
if (!hasWidth || !hasHeight) {
15+
report({
16+
position: child.loc,
17+
message: 'The <source> tag inside <picture> must have both "width" and "height" attributes.',
18+
});
19+
}
20+
}
21+
});
22+
}
23+
}
24+
};

0 commit comments

Comments
 (0)