Skip to content

Commit a925071

Browse files
authored
feat: New rule: tag-no-obsolete (#1660)
* feat: New rule: `tag-no-obsolete` * Update index.mdx
1 parent f40d329 commit a925071

File tree

8 files changed

+143
-2
lines changed

8 files changed

+143
-2
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"msapplication",
3939
"nocolor",
4040
"nodir",
41+
"noembed",
4142
"nomix",
4243
"noopener",
4344
"npmjs",

dist/core/rules/index.js

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export { default as spaceTabMixedDisabled } from './space-tab-mixed-disabled'
3333
export { default as specCharEscape } from './spec-char-escape'
3434
export { default as srcNotEmpty } from './src-not-empty'
3535
export { default as styleDisabled } from './style-disabled'
36+
export { default as tagNoObsolete } from './tag-no-obsolete'
3637
export { default as tagnameLowercase } from './tagname-lowercase'
3738
export { default as tagnameSpecialChars } from './tagname-specialchars'
3839
export { default as tagPair } from './tag-pair'

src/core/rules/tag-no-obsolete.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Rule } from '../types'
2+
3+
// List of obsolete HTML5 tags
4+
const OBSOLETE_TAGS = [
5+
'applet',
6+
'acronym',
7+
'bgsound',
8+
'dir',
9+
'frame',
10+
'frameset',
11+
'noframes',
12+
'isindex',
13+
'keygen',
14+
'listing',
15+
'menuitem',
16+
'nextid',
17+
'noembed',
18+
'plaintext',
19+
'rb',
20+
'rtc',
21+
'strike',
22+
'xmp',
23+
'basefont',
24+
'big',
25+
'blink',
26+
'center',
27+
'font',
28+
'marquee',
29+
'multicol',
30+
'nobr',
31+
'spacer',
32+
'tt',
33+
]
34+
35+
export default {
36+
id: 'tag-no-obsolete',
37+
description: 'Disallows the use of obsolete HTML5 tags.',
38+
init(parser, reporter, _options) {
39+
parser.addListener('tagstart,tagend', (event) => {
40+
const tagName = event.tagName.toLowerCase()
41+
42+
if (OBSOLETE_TAGS.includes(tagName)) {
43+
reporter.error(
44+
`The tag [ ${event.tagName} ] is obsolete in HTML5 and should not be used.`,
45+
event.line,
46+
event.col,
47+
this,
48+
event.raw
49+
)
50+
}
51+
})
52+
},
53+
} as Rule

src/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface Ruleset {
5151
'spec-char-escape'?: boolean
5252
'src-not-empty'?: boolean
5353
'style-disabled'?: boolean
54+
'tag-no-obsolete'?: boolean
5455
'tag-pair'?: boolean
5556
'tag-self-close'?: boolean
5657
'tagname-lowercase'?: boolean

test/rules/tag-no-obsolete.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const HTMLHint = require('../../dist/htmlhint.js').HTMLHint
2+
3+
const ruleId = 'tag-no-obsolete'
4+
const ruleOptions = {}
5+
6+
ruleOptions[ruleId] = true
7+
8+
describe(`Rules: ${ruleId}`, () => {
9+
it('Obsolete HTML5 tags should result in an error', () => {
10+
const code =
11+
'<center>Centered text</center><font color="red">Red text</font><marquee>Scrolling text</marquee>'
12+
const messages = HTMLHint.verify(code, ruleOptions)
13+
expect(messages.length).toBe(6) // 3 opening tags + 3 closing tags
14+
messages.forEach((msg) => expect(msg.rule.id).toBe(ruleId))
15+
})
16+
17+
it('Non-obsolete HTML5 tags should not result in an error', () => {
18+
const code = '<div>Content</div><span>Text</span><p>Paragraph</p>'
19+
const messages = HTMLHint.verify(code, ruleOptions)
20+
expect(messages.length).toBe(0)
21+
})
22+
23+
it('Mixed case obsolete tags should still be detected', () => {
24+
const code = '<CENTER>Centered</CENTER><Font>Styled</Font>'
25+
const messages = HTMLHint.verify(code, ruleOptions)
26+
expect(messages.length).toBe(4) // 2 opening tags + 2 closing tags
27+
messages.forEach((msg) => expect(msg.rule.id).toBe(ruleId))
28+
})
29+
30+
it('Self-closing obsolete tags should be detected', () => {
31+
const code = '<br><hr><img src="test.jpg"><spacer>'
32+
const messages = HTMLHint.verify(code, ruleOptions)
33+
expect(messages.length).toBe(1) // Only spacer is obsolete
34+
messages.forEach((msg) => expect(msg.rule.id).toBe(ruleId))
35+
})
36+
})

website/src/content/docs/rules/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ description: A complete list of all the rules for HTMLHint
4141
- [`href-abs-or-rel`](href-abs-or-rel/): An href attribute must be either absolute or relative.
4242
- [`main-require`](main-require/): A document must have at least one `<main>` element in the `<body>` tag.
4343
- [`src-not-empty`](src-not-empty/): The src attribute of an img(script,link) must have a value.
44+
{/* - [`tag-no-obsolete`](tag-no-obsolete/): Disallows the use of obsolete HTML5 tags. */}
4445
- [`tag-pair`](tag-pair/): Tag must be paired.
4546
- [`tag-self-close`](tag-self-close/): Empty tags must be self closed.
4647
- [`tagname-lowercase`](tagname-lowercase/): All HTML element names must be in lowercase.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
id: tag-no-obsolete
3+
title: tag-no-obsolete
4+
description: Disallows the use of obsolete HTML5 tags that are no longer supported in modern browsers.
5+
sidebar:
6+
badge: New
7+
hidden: true
8+
---
9+
10+
import { Badge } from '@astrojs/starlight/components';
11+
12+
Disallows the use of obsolete HTML5 tags.
13+
14+
Level: <Badge text="Error" variant="danger" />
15+
16+
## Config value
17+
18+
- `true`: enable rule
19+
- `false`: disable rule
20+
21+
## Description
22+
23+
This rule prevents the use of HTML tags that have been deprecated and are considered obsolete in HTML5. These tags are no longer supported by modern browsers and should be replaced with appropriate alternatives.
24+
25+
### Obsolete tags include:
26+
27+
- `applet`, `acronym`, `bgsound`, `dir`, `frame`, `frameset`, `noframes`
28+
- `isindex`, `keygen`, `listing`, `menuitem`, `nextid`, `noembed`
29+
- `plaintext`, `rb`, `rtc`, `strike`, `xmp`, `basefont`, `big`
30+
- `blink`, `center`, `font`, `marquee`, `multicol`, `nobr`, `spacer`, `tt`
31+
32+
### The following patterns are **not** considered rule violations
33+
34+
```html
35+
<div>Content</div>
36+
<span>Text</span>
37+
<p>Paragraph</p>
38+
```
39+
40+
### The following patterns are considered rule violations:
41+
42+
```html
43+
<center>Centered text</center>
44+
<font color="red">Red text</font>
45+
<marquee>Scrolling text</marquee>
46+
```

0 commit comments

Comments
 (0)