-
-
Notifications
You must be signed in to change notification settings - Fork 401
feat: Add meta-charset-require
rule
#1628
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
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
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
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,50 @@ | ||
import { Block, Listener } from '../htmlparser' | ||
import { Rule } from '../types' | ||
|
||
export default { | ||
id: 'meta-charset-require', | ||
description: '<meta charset=""> must be present in <head> tag.', | ||
init(parser, reporter) { | ||
let headSeen = false | ||
let metaCharsetSeen = false | ||
let metaCharsetContent = '' | ||
let headEvent: Block | null = null | ||
|
||
const onTagStart: Listener = (event) => { | ||
const tagName = event.tagName.toLowerCase() | ||
if (tagName === 'head') { | ||
headSeen = true | ||
headEvent = event | ||
} else if (tagName === 'meta') { | ||
const mapAttrs = parser.getMapAttrs(event.attrs) | ||
if (mapAttrs['charset'] !== undefined) { | ||
metaCharsetSeen = true | ||
metaCharsetContent = mapAttrs['charset'] || '' | ||
} | ||
} | ||
} | ||
|
||
parser.addListener('tagstart', onTagStart) | ||
parser.addListener('end', () => { | ||
if (headSeen && headEvent) { | ||
if (!metaCharsetSeen) { | ||
reporter.error( | ||
'<meta charset=""> must be present in <head> tag.', | ||
headEvent.line, | ||
headEvent.col, | ||
this, | ||
headEvent.raw | ||
) | ||
} else if (metaCharsetContent.trim() === '') { | ||
reporter.error( | ||
'<meta charset=""> value must not be empty.', | ||
headEvent.line, | ||
headEvent.col, | ||
this, | ||
headEvent.raw | ||
) | ||
} | ||
} | ||
}) | ||
}, | ||
} as Rule |
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 @@ | ||
const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
const ruleId = 'meta-charset-require' | ||
|
||
describe('Rule: meta-charset-require', () => { | ||
it('should not report an error when a valid meta charset is present', () => { | ||
const code = `<!DOCTYPE html><html><head><meta charset="utf-8"></head><body></body></html>` | ||
const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('should report an error when meta charset is missing', () => { | ||
const code = `<!DOCTYPE html><html><head></head><body></body></html>` | ||
const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].message).toBe( | ||
'<meta charset=""> must be present in <head> tag.' | ||
) | ||
}) | ||
|
||
it('should report an error when meta charset value is blank', () => { | ||
const code = `<!DOCTYPE html><html><head><meta charset=""></head><body></body></html>` | ||
const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].message).toBe( | ||
'<meta charset=""> value must not be empty.' | ||
) | ||
}) | ||
|
||
it('should report an error when meta charset value is only whitespace', () => { | ||
const code = `<!DOCTYPE html><html><head><meta charset=" "></head><body></body></html>` | ||
const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].message).toBe( | ||
'<meta charset=""> value must not be empty.' | ||
) | ||
}) | ||
|
||
it('should report an error when meta charset is missing and only other meta tags are present', () => { | ||
const code = `<!DOCTYPE html><html><head><meta name="description" content="desc"></head><body></body></html>` | ||
const messages = HTMLHint.verify(code, { [ruleId]: true }) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].message).toBe( | ||
'<meta charset=""> must be present in <head> tag.' | ||
) | ||
}) | ||
}) |
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
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,38 @@ | ||
--- | ||
id: meta-charset-require | ||
title: meta-charset-require | ||
description: Ensures every HTML document includes a meta charset tag within the head element for proper character encoding. | ||
sidebar: | ||
hidden: true | ||
badge: New | ||
--- | ||
|
||
import { Badge } from '@astrojs/starlight/components'; | ||
|
||
A `<meta charset="">` must be present in `<head>` tag. | ||
|
||
Level: <Badge text="Error" variant="danger" /> | ||
|
||
## Config value | ||
|
||
1. true: enable rule | ||
2. false: disable rule | ||
|
||
### The following patterns are **not** considered rule violations | ||
|
||
```html | ||
<html><head><meta charset="utf-8"></head></html> | ||
``` | ||
|
||
### The following patterns are considered rule violations | ||
|
||
```html | ||
<!-- Missing meta charset --> | ||
<html><head></head></html> | ||
|
||
<!-- Empty meta charset value --> | ||
<html><head><meta charset=""></head></html> | ||
|
||
<!-- Whitespace-only meta charset value --> | ||
<html><head><meta charset=" "></head></html> | ||
``` |
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.