-
-
Notifications
You must be signed in to change notification settings - Fork 400
New Rule: button-type-require
#1615
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,43 @@ | ||
import HTMLParser, { Block } from '../htmlparser' | ||
import Reporter from '../reporter' | ||
|
||
export default { | ||
id: 'button-type-require', | ||
description: | ||
'The type attribute of a <button> element must be present with a valid value: "button", "submit", or "reset".', | ||
init(parser: HTMLParser, reporter: Reporter): void { | ||
parser.addListener('tagstart', (event: Block): void => { | ||
const tagName = event.tagName.toLowerCase() | ||
|
||
if (tagName === 'button') { | ||
const mapAttrs = parser.getMapAttrs(event.attrs) | ||
const col = event.col + tagName.length + 1 | ||
|
||
if (mapAttrs.type === undefined) { | ||
reporter.warn( | ||
'The type attribute must be present on <button> elements.', | ||
event.line, | ||
col, | ||
this, | ||
event.raw | ||
) | ||
} else { | ||
const typeValue = mapAttrs.type.toLowerCase() | ||
if ( | ||
typeValue !== 'button' && | ||
typeValue !== 'submit' && | ||
typeValue !== 'reset' | ||
) { | ||
reporter.warn( | ||
'The type attribute of <button> must have a valid value: "button", "submit", or "reset".', | ||
event.line, | ||
col, | ||
this, | ||
event.raw | ||
) | ||
} | ||
} | ||
} | ||
}) | ||
}, | ||
} |
This file contains hidden or 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
This file contains hidden or 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,64 @@ | ||
const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
|
||
const ruleId = 'button-type-require' | ||
const ruleOptions = {} | ||
|
||
ruleOptions[ruleId] = true | ||
|
||
describe(`Rules: ${ruleId}`, () => { | ||
it('Button with type="button" should not result in an error', () => { | ||
const code = '<button type="button">Click me</button>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Button with type="submit" should not result in an error', () => { | ||
const code = '<button type="submit">Submit</button>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Button with type="reset" should not result in an error', () => { | ||
const code = '<button type="reset">Reset</button>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Button without type attribute should result in an error', () => { | ||
const code = '<button>Click me</button>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].line).toBe(1) | ||
expect(messages[0].col).toBe(8) | ||
expect(messages[0].type).toBe('warning') | ||
expect(messages[0].message).toBe( | ||
'The type attribute must be present on <button> elements.' | ||
) | ||
}) | ||
|
||
it('Button with invalid type value should result in an error', () => { | ||
const code = '<button type="invalid">Click me</button>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].line).toBe(1) | ||
expect(messages[0].col).toBe(8) | ||
expect(messages[0].type).toBe('warning') | ||
expect(messages[0].message).toBe( | ||
'The type attribute of <button> must have a valid value: "button", "submit", or "reset".' | ||
) | ||
}) | ||
|
||
it('Button with uppercase type value should not result in an error', () => { | ||
const code = '<button type="BUTTON">Click me</button>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Other elements should not be affected by this rule', () => { | ||
const code = '<div>Not a button</div><input type="text">' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
}) |
This file contains hidden or 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
This file contains hidden or 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,46 @@ | ||
--- | ||
id: button-type-require | ||
title: button-type-require | ||
description: Requires button elements to have a valid type attribute for better browser compatibility and user experience. | ||
sidebar: | ||
hidden: true | ||
badge: New | ||
--- | ||
|
||
import { Badge } from '@astrojs/starlight/components'; | ||
|
||
The type attribute of a `<button>` element must be present with a valid value: "button", "submit", or "reset". | ||
|
||
Level: <Badge text="Warning" variant="caution" /> | ||
|
||
## Config value | ||
|
||
- `true`: enable rule | ||
- `false`: disable rule | ||
|
||
### The following patterns are **not** considered rule violations: | ||
|
||
```html | ||
<button type="button">Click me</button> | ||
<button type="submit">Submit form</button> | ||
<button type="reset">Reset form</button> | ||
``` | ||
|
||
### The following patterns are considered rule violations: | ||
|
||
```html | ||
<button>No type specified</button> | ||
<button type="invalid">Invalid type</button> | ||
``` | ||
|
||
## Why this rule is important | ||
|
||
HTML buttons default to `type="submit"` when used within a form if no type is specified, which can lead to unexpected form submissions. Explicitly setting the type attribute makes the button's behavior predictable and clear to both developers and users. | ||
|
||
The HTML specification requires that button elements have one of three valid types: | ||
|
||
- `button`: A generic button with no default behavior | ||
- `submit`: Submits the form data to the server (default) | ||
- `reset`: Resets all form controls to their initial values | ||
|
||
This rule helps ensure that buttons have explicit, valid types for better browser compatibility and user experience. |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.