Skip to content

Add namespace check of svg & mathML instead of tag names #120

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 2 commits into from
Aug 2, 2017
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
2 changes: 1 addition & 1 deletion lib/rules/html-no-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const utils = require('../utils')
function create (context) {
utils.registerTemplateBodyVisitor(context, {
'VElement' (node) {
if (utils.isSvgElementName(node.name)) {
if (utils.isSvgElementNode(node)) {
return
}

Expand Down
62 changes: 27 additions & 35 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
// ------------------------------------------------------------------------------

const HTML_ELEMENT_NAMES = new Set(require('./html-elements.json'))
const SVG_ELEMENT_NAMES = new Set(require('./svg-elements.json'))
const VOID_ELEMENT_NAMES = new Set(require('./void-elements.json'))
const assert = require('assert')
const vueEslintParser = require('vue-eslint-parser')

// ------------------------------------------------------------------------------
// Exports
Expand All @@ -38,24 +38,6 @@ module.exports = {
context.parserServices.registerTemplateBodyVisitor(context, visitor)
},

/**
* Get the token store of template body from parser services.
* If the parser service of `vue-eslint-parser` was not found,
* this generates a warning.
*
* @returns {void}
*/
getTemplateBodyTokenStore () {
if (context.parserServices.getTemplateBodyTokenStore == null) {
context.report({
loc: { line: 1, column: 0 },
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'
})
return context.getSourceCode()
}
return context.parserServices.getTemplateBodyTokenStore()
},

/**
* Check whether the given node is the root element or not.
* @param {ASTNode} node The element node to check.
Expand Down Expand Up @@ -200,34 +182,44 @@ module.exports = {
isCustomComponent (node) {
assert(node && node.type === 'VElement')

const name = node.name
return (
!(this.isHtmlElementName(name) || this.isSvgElementName(name)) ||
this.hasAttribute(node, 'is') ||
this.hasDirective(node, 'bind', 'is')
!(this.isKnownHtmlElementNode(node) || this.isSvgElementNode(node) || this.isMathMLElementNode(node)) ||
this.hasAttribute(node, 'is') ||
this.hasDirective(node, 'bind', 'is')
)
},

/**
* Check whether the given name is a HTML element name or not.
* @param {string} name The name to check.
* @returns {boolean} `true` if the name is a HTML element name.
* Check whether the given node is a HTML element or not.
* @param {ASTNode} node The node to check.
* @returns {boolean} `true` if the node is a HTML element.
*/
isHtmlElementName (name) {
assert(typeof name === 'string')
isKnownHtmlElementNode (node) {
assert(node && node.type === 'VElement')

return HTML_ELEMENT_NAMES.has(name.toLowerCase())
return node.namespace === vueEslintParser.AST.NS.HTML && HTML_ELEMENT_NAMES.has(node.name.toLowerCase())
},

/**
* Check whether the given name is a SVG element name or not.
* @param {string} name The name to check.
* @returns {boolean} `true` if the name is a SVG element name.
* Check whether the given node is a SVG element or not.
* @param {ASTNode} node The node to check.
* @returns {boolean} `true` if the name is a SVG element.
*/
isSvgElementName (name) {
assert(typeof name === 'string')
isSvgElementNode (node) {
assert(node && node.type === 'VElement')

return node.namespace === vueEslintParser.AST.NS.SVG
},

/**
* Check whether the given name is a MathML element or not.
* @param {ASTNode} name The node to check.
* @returns {boolean} `true` if the node is a MathML element.
*/
isMathMLElementNode (node) {
assert(node && node.type === 'VElement')

return SVG_ELEMENT_NAMES.has(name.toLowerCase())
return node.namespace === vueEslintParser.AST.NS.MathML
},

/**
Expand Down
1 change: 0 additions & 1 deletion lib/utils/svg-elements.json

This file was deleted.