Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Adds new rule htmlacademy/req-submit-button #90

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
50 changes: 50 additions & 0 deletions rules/req-submit-button/fixture/success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html class="page" lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>HTML Academy: Девайс</title>
<link href="./css/style.min.css" rel="stylesheet">
</head>
<body class="index-page">
<form action="index.html">
<label>
Text field: <input type="text" name="name">
</label>
<button type="submit">Отправить</button>
</form>

<form action="index.html">
<label>
Text field: <input type="text" name="name">
</label>
<div>
<button type="submit">Отправить</button>
</div>
</form>

<form action="index.html">
<label>
Text field: <input type="text" name="name">
</label>
<input type="submit" value="Отправить" name="submit" aria-label="submit">
</form>

<form action="index.html">
<label>
Text field: <input type="text" name="name">
</label>
<div>
<input type="submit" value="Отправить" name="submit" aria-label="submit">
</div>
</form>

<form id="my-form" action="index.html">
<label>
Text field: <input type="text" name="name">
</label>
</form>
<button form="my-form" type="submit">Submit</button>

</body>
</html>
34 changes: 34 additions & 0 deletions rules/req-submit-button/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const { is_tag_node, attribute_has_value } = require('@linthtml/dom-utils');

const formSubmitter = ['button', 'input'];

const findSubmitters = (node) => {
let submitters = [];
if (is_tag_node(node) && formSubmitter.includes(node.name) && attribute_has_value(node, 'type', 'submit')) {
submitters.push(node);
}
if (node.children) {
for (const child of node.children) {
submitters = submitters.concat(findSubmitters(child));
}
}
return submitters;
};

module.exports = {
name: 'htmlacademy/req-submit-button',
// eslint-disable-next-line camelcase
lint(node, rule_config, {report}) {
if (is_tag_node(node) && node.name === 'form') {
const submitters = findSubmitters(node);

if(submitters.length === 0) {
report({
position: node.loc,
message: 'Submitter inside form must have type="submit".',
});
}
}
}
};