Skip to content

Commit da311e9

Browse files
armano2mysticatea
authored andcommitted
Add namespace check of svg & mathML instead of tag names (#120)
* Add namespace check of svg & mathML instead of checking tag names * isHtmlElementNode -> isKnownHtmlElementNode add namespace check for html
1 parent c149f33 commit da311e9

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

lib/rules/html-no-self-closing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const utils = require('../utils')
2424
function create (context) {
2525
utils.registerTemplateBodyVisitor(context, {
2626
'VElement' (node) {
27-
if (utils.isSvgElementName(node.name)) {
27+
if (utils.isSvgElementNode(node)) {
2828
return
2929
}
3030

lib/utils/index.js

+27-35
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
// ------------------------------------------------------------------------------
1111

1212
const HTML_ELEMENT_NAMES = new Set(require('./html-elements.json'))
13-
const SVG_ELEMENT_NAMES = new Set(require('./svg-elements.json'))
1413
const VOID_ELEMENT_NAMES = new Set(require('./void-elements.json'))
1514
const assert = require('assert')
15+
const vueEslintParser = require('vue-eslint-parser')
1616

1717
// ------------------------------------------------------------------------------
1818
// Exports
@@ -38,24 +38,6 @@ module.exports = {
3838
context.parserServices.registerTemplateBodyVisitor(context, visitor)
3939
},
4040

41-
/**
42-
* Get the token store of template body from parser services.
43-
* If the parser service of `vue-eslint-parser` was not found,
44-
* this generates a warning.
45-
*
46-
* @returns {void}
47-
*/
48-
getTemplateBodyTokenStore () {
49-
if (context.parserServices.getTemplateBodyTokenStore == null) {
50-
context.report({
51-
loc: { line: 1, column: 0 },
52-
message: 'Use the latest vue-eslint-parser. See also https://github.com/vuejs/eslint-plugin-vue#what-is-the-use-the-latest-vue-eslint-parser-error'
53-
})
54-
return context.getSourceCode()
55-
}
56-
return context.parserServices.getTemplateBodyTokenStore()
57-
},
58-
5941
/**
6042
* Check whether the given node is the root element or not.
6143
* @param {ASTNode} node The element node to check.
@@ -200,34 +182,44 @@ module.exports = {
200182
isCustomComponent (node) {
201183
assert(node && node.type === 'VElement')
202184

203-
const name = node.name
204185
return (
205-
!(this.isHtmlElementName(name) || this.isSvgElementName(name)) ||
206-
this.hasAttribute(node, 'is') ||
207-
this.hasDirective(node, 'bind', 'is')
186+
!(this.isKnownHtmlElementNode(node) || this.isSvgElementNode(node) || this.isMathMLElementNode(node)) ||
187+
this.hasAttribute(node, 'is') ||
188+
this.hasDirective(node, 'bind', 'is')
208189
)
209190
},
210191

211192
/**
212-
* Check whether the given name is a HTML element name or not.
213-
* @param {string} name The name to check.
214-
* @returns {boolean} `true` if the name is a HTML element name.
193+
* Check whether the given node is a HTML element or not.
194+
* @param {ASTNode} node The node to check.
195+
* @returns {boolean} `true` if the node is a HTML element.
215196
*/
216-
isHtmlElementName (name) {
217-
assert(typeof name === 'string')
197+
isKnownHtmlElementNode (node) {
198+
assert(node && node.type === 'VElement')
218199

219-
return HTML_ELEMENT_NAMES.has(name.toLowerCase())
200+
return node.namespace === vueEslintParser.AST.NS.HTML && HTML_ELEMENT_NAMES.has(node.name.toLowerCase())
220201
},
221202

222203
/**
223-
* Check whether the given name is a SVG element name or not.
224-
* @param {string} name The name to check.
225-
* @returns {boolean} `true` if the name is a SVG element name.
204+
* Check whether the given node is a SVG element or not.
205+
* @param {ASTNode} node The node to check.
206+
* @returns {boolean} `true` if the name is a SVG element.
226207
*/
227-
isSvgElementName (name) {
228-
assert(typeof name === 'string')
208+
isSvgElementNode (node) {
209+
assert(node && node.type === 'VElement')
210+
211+
return node.namespace === vueEslintParser.AST.NS.SVG
212+
},
213+
214+
/**
215+
* Check whether the given name is a MathML element or not.
216+
* @param {ASTNode} name The node to check.
217+
* @returns {boolean} `true` if the node is a MathML element.
218+
*/
219+
isMathMLElementNode (node) {
220+
assert(node && node.type === 'VElement')
229221

230-
return SVG_ELEMENT_NAMES.has(name.toLowerCase())
222+
return node.namespace === vueEslintParser.AST.NS.MathML
231223
},
232224

233225
/**

lib/utils/svg-elements.json

-1
This file was deleted.

0 commit comments

Comments
 (0)