Skip to content

feat(no-restricted-html-elements): support all element types #2755

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 4 commits into from
Jun 10, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/true-oranges-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-vue': patch
---

[vue/no-restricted-html-elements](https://eslint.vuejs.org/rules/no-restricted-html-elements.html) now also checks SVG and MathML elements.
2 changes: 1 addition & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ For example:
| [vue/no-restricted-component-names] | disallow specific component names | :bulb: | :hammer: |
| [vue/no-restricted-component-options] | disallow specific component option | | :hammer: |
| [vue/no-restricted-custom-event] | disallow specific custom event | :bulb: | :hammer: |
| [vue/no-restricted-html-elements] | disallow specific HTML elements | | :hammer: |
| [vue/no-restricted-html-elements] | disallow specific elements | | :hammer: |
| [vue/no-restricted-props] | disallow specific props | :bulb: | :hammer: |
| [vue/no-restricted-static-attribute] | disallow specific attribute | | :hammer: |
| [vue/no-restricted-v-bind] | disallow specific argument in `v-bind` | | :hammer: |
Expand Down
10 changes: 5 additions & 5 deletions docs/rules/no-restricted-html-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
pageClass: rule-details
sidebarDepth: 0
title: vue/no-restricted-html-elements
description: disallow specific HTML elements
description: disallow specific elements
since: v8.6.0
---

# vue/no-restricted-html-elements

> disallow specific HTML elements
> disallow specific elements

## :book: Rule Details

This rule allows you to specify HTML elements that you don't want to use in your application.
This rule allows you to specify HTML, SVG, and MathML elements that you don't want to use in your application.

<eslint-code-block :rules="{'vue/no-restricted-html-elements': ['error', 'marquee', 'button'] }">

Expand All @@ -33,7 +33,7 @@ This rule allows you to specify HTML elements that you don't want to use in your

## :wrench: Options

This rule takes a list of strings, where each string is an HTML element name to be restricted:
This rule takes a list of strings, where each string is an element name to be restricted:

```json
{
Expand Down Expand Up @@ -73,7 +73,7 @@ Alternatively, the rule also accepts objects.

The following properties can be specified for the object.

- `element` ... Specify the HTML element or an array of HTML elements.
- `element` ... Specify the element name or an array of element names.
- `message` ... Specify an optional custom message.

### `{ "element": "marquee" }, { "element": "a" }`
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/no-restricted-html-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow specific HTML elements',
description: 'disallow specific elements',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-restricted-html-elements.html'
},
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = {
minItems: 0
},
messages: {
forbiddenElement: 'Unexpected use of forbidden HTML element {{name}}.',
forbiddenElement: 'Unexpected use of forbidden element {{name}}.',
// eslint-disable-next-line eslint-plugin/report-message-format
customMessage: '{{message}}'
}
Expand All @@ -55,7 +55,11 @@ module.exports = {
* @param {VElement} node
*/
VElement(node) {
if (!utils.isHtmlElementNode(node)) {
if (
!utils.isHtmlElementNode(node) &&
!utils.isSvgElementNode(node) &&
!utils.isMathElementNode(node)
) {
return
}

Expand Down
73 changes: 71 additions & 2 deletions tests/lib/rules/no-restricted-html-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ tester.run('no-restricted-html-elements', rule, {
filename: 'test.vue',
code: '<template><main><article></article></main></template>',
options: [{ element: ['div', 'span'] }]
},
// SVG
{
filename: 'test.vue',
code: '<template><svg><rect width="100" height="100"></rect></svg></template>',
options: ['circle']
},
// Math
{
filename: 'test.vue',
code: '<template><math><mn>2</mn></math></template>',
options: ['mi']
}
],
invalid: [
Expand All @@ -45,7 +57,7 @@ tester.run('no-restricted-html-elements', rule, {
options: ['button'],
errors: [
{
message: 'Unexpected use of forbidden HTML element button.',
message: 'Unexpected use of forbidden element button.',
line: 1,
column: 16
}
Expand All @@ -57,7 +69,7 @@ tester.run('no-restricted-html-elements', rule, {
options: ['div'],
errors: [
{
message: 'Unexpected use of forbidden HTML element div.',
message: 'Unexpected use of forbidden element div.',
line: 1,
column: 11
}
Expand Down Expand Up @@ -96,6 +108,63 @@ tester.run('no-restricted-html-elements', rule, {
column: 18
}
]
},
// SVG
{
filename: 'test.vue',
code: '<template><svg><circle r="50"></circle></svg></template>',
options: ['circle'],
errors: [
{
message: 'Unexpected use of forbidden element circle.',
line: 1,
column: 16
}
]
},
{
filename: 'test.vue',
code: '<template><svg><rect width="100" height="100"></rect><path d="M10,10 L20,20"></path></svg></template>',
options: [
{ element: ['rect', 'path'], message: 'Use simplified shapes instead' }
],
errors: [
{
message: 'Use simplified shapes instead',
line: 1,
column: 16
},
{
message: 'Use simplified shapes instead',
line: 1,
column: 54
}
]
},
// Math
{
filename: 'test.vue',
code: '<template><math><mfrac><mn>1</mn><mn>2</mn></mfrac></math></template>',
options: ['mfrac'],
errors: [
{
message: 'Unexpected use of forbidden element mfrac.',
line: 1,
column: 17
}
]
},
{
filename: 'test.vue',
code: '<template><math><mi>x</mi><mo>=</mo><mn>5</mn></math></template>',
options: [{ element: 'mo', message: 'Avoid using operators directly' }],
errors: [
{
message: 'Avoid using operators directly',
line: 1,
column: 27
}
]
}
]
})
Loading