-
Notifications
You must be signed in to change notification settings - Fork 45
feat: new rule proposal: no-invalid-entity-351 #371
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
yeonjuan
merged 9 commits into
yeonjuan:main
from
perpetual-cosmos:feature/no-invalid-entity-351
Jun 20, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
99ebfb0
feat:New rule proposal: no-invalid-entity-351
perpetual-cosmos 7b67d8f
Merge branch 'yeonjuan:main' into feature/no-invalid-entity-351
perpetual-cosmos 5f52351
Merge branch 'yeonjuan:main' into feature/no-invalid-entity-351
perpetual-cosmos 6a88a90
Update no-invalid-entity.js removed extra whitespace
perpetual-cosmos 6882fec
fixes: yarn lint. run yarn format and added nsnb in cspell
perpetual-cosmos 2a35a6e
Merge branch 'main' into pr-edit
yeonjuan 9f111c3
Update rules.md
yeonjuan 6bd90d1
fix
yeonjuan 57ae8e6
Update no-invalid-entity.test.js
yeonjuan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ | |
"treegrid", | ||
"contentinfo", | ||
"smashingmagazine", | ||
"contenteditable" | ||
"contenteditable", | ||
"nbsb" | ||
] | ||
} |
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,43 @@ | ||
# no-invalid-entity | ||
|
||
Disallow use of invalid HTML entities. | ||
|
||
## Rule Details | ||
|
||
This rule disallows the use of invalid HTML entities in your markup. HTML entities are special codes used to represent characters with specific meanings in HTML, such as `<` (`<`) or `&` (`&`). Invalid entities—such as typos, undefined entities, or malformed numeric references—can be silently ignored by browsers, leading to rendering issues or confusion. | ||
|
||
The rule validates both named entities (e.g., ` `) and numeric entities (e.g., ` ` for decimal or ` ` for hexadecimal) against a list of valid entities defined in `entities.json`. An entity is considered invalid if: | ||
|
||
- It is a named entity not found in `entities.json` (e.g., `&nbsb;` or `&unknown;`). | ||
- It is a numeric entity with an invalid format (e.g., `&#zzzz;`). | ||
- It is a numeric entity outside the valid Unicode range (0 to 0x10FFFF, e.g., `�`). | ||
|
||
## How to use | ||
|
||
```js,.eslintrc.js | ||
module.exports = { | ||
rules: { | ||
"@html-eslint/no-invalid-entity": "error", | ||
}, | ||
}; | ||
``` | ||
|
||
### Examples | ||
|
||
#### Incorrect Code | ||
|
||
```html | ||
<p>&nbsb;</p> <!-- typo --> | ||
<p>&unknown;</p> <!-- undefined entity --> | ||
<p>&#zzzz;</p> <!-- invalid numeric reference --> | ||
``` | ||
|
||
#### Correct Code | ||
|
||
```html | ||
<p>< > &    </p> | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
Disable this rule if you are intentionally using invalid entities for specific purposes, such as testing or non-standard rendering. Be aware that invalid entities may not render consistently across different browsers. |
Large diffs are not rendered by default.
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,108 @@ | ||
/** | ||
* @typedef { import("../types").RuleModule<[]> } RuleModule | ||
* @typedef { import("../types").SuggestionReportDescriptor } SuggestionReportDescriptor | ||
* @typedef { import("@html-eslint/types").Text} Text | ||
*/ | ||
|
||
// Define the type for entities.json | ||
/** | ||
* @typedef {Object} EntityData | ||
* @property {number[]} codepoints | ||
* @property {string} characters | ||
*/ | ||
|
||
/** @type {{ [key: string]: EntityData }} */ | ||
const entities = require("../data/entities.json"); | ||
|
||
const { RULE_CATEGORY } = require("../constants"); | ||
const { createVisitors } = require("./utils/visitors"); | ||
const { getRuleUrl } = require("./utils/rule"); | ||
|
||
const MESSAGE_IDS = { | ||
INVALID_ENTITY: "invalidEntity", | ||
}; | ||
|
||
/** | ||
* @type {RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: "code", | ||
docs: { | ||
description: "Disallows the use of invalid HTML entities", | ||
category: RULE_CATEGORY.BEST_PRACTICE, | ||
recommended: false, | ||
url: getRuleUrl("no-invalid-entity"), | ||
}, | ||
fixable: null, | ||
hasSuggestions: false, | ||
schema: [], | ||
messages: { | ||
[MESSAGE_IDS.INVALID_ENTITY]: "Invalid HTML entity '{{entity}}' used.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
/** | ||
* @param {Text} node | ||
*/ | ||
function check(node) { | ||
const text = node.value; | ||
|
||
// Regular expression to match named and numeric entities | ||
const entityRegex = /&([a-zA-Z]+|#[0-9]+|#x[0-9a-fA-F]+|[#][^;]+);/g; | ||
let match; | ||
|
||
while ((match = entityRegex.exec(text)) !== null) { | ||
const entity = match[0]; | ||
const entityName = match[1]; | ||
|
||
// Check named entities | ||
if (!entityName.startsWith("#")) { | ||
const fullEntity = `&${entityName};`; | ||
if (!Object.prototype.hasOwnProperty.call(entities, fullEntity)) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_IDS.INVALID_ENTITY, | ||
data: { entity }, | ||
}); | ||
} | ||
} | ||
// Check numeric entities | ||
else { | ||
const isHex = entityName[1] === "x"; | ||
const numStr = isHex ? entityName.slice(2) : entityName.slice(1); | ||
const num = isHex ? parseInt(numStr, 16) : parseInt(numStr, 10); | ||
|
||
// If the number is not a valid integer, report an error | ||
if (isNaN(num)) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_IDS.INVALID_ENTITY, | ||
data: { entity }, | ||
}); | ||
continue; | ||
} | ||
|
||
// Check if the numeric entity is valid (exists in entities.json or within valid Unicode range) | ||
const entityKey = Object.keys(entities).find((key) => { | ||
const codepoints = entities[key].codepoints; | ||
return codepoints.length === 1 && codepoints[0] === num; | ||
}); | ||
|
||
if (!entityKey && (num < 0 || num > 0x10ffff)) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_IDS.INVALID_ENTITY, | ||
data: { entity }, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return createVisitors(context, { | ||
Text: check, | ||
}); | ||
}, | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/eslint-plugin/tests/rules/no-invalid-entity.test.js
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,40 @@ | ||
const createRuleTester = require("../rule-tester"); | ||
const rule = require("../../lib/rules/no-invalid-entity"); | ||
|
||
const ruleTester = createRuleTester(); | ||
const templateRuleTester = createRuleTester("espree"); | ||
|
||
ruleTester.run("no-invalid-entity", rule, { | ||
valid: [ | ||
{ code: "<p>< > & </p>" }, | ||
{ code: "<p>한</p>" }, | ||
], | ||
invalid: [ | ||
{ | ||
code: "<p>&nbsb;</p>", // Typo in entity name | ||
errors: [{ message: "Invalid HTML entity '&nbsb;' used." }], | ||
}, | ||
{ | ||
code: "<p>&unknown;</p>", // Undefined entity | ||
errors: [{ message: "Invalid HTML entity '&unknown;' used." }], | ||
}, | ||
{ | ||
code: "<p>&#zzzz;</p>", // Invalid numeric entity | ||
errors: [{ message: "Invalid HTML entity '&#zzzz;' used." }], | ||
}, | ||
{ | ||
code: "<p>�</p>", | ||
errors: [{ message: "Invalid HTML entity '�' used." }], | ||
}, | ||
], | ||
}); | ||
|
||
templateRuleTester.run("[template] no-invalid-entity", rule, { | ||
valid: [{ code: `html\`<p>< > & </p>\`` }], | ||
invalid: [ | ||
{ | ||
code: "html`<p>&nbsb;</p>`", | ||
errors: [{ message: "Invalid HTML entity '&nbsb;' used." }], | ||
}, | ||
], | ||
}); |
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.