Skip to content

Commit f3d794a

Browse files
committed
feat(no-v-text-v-html-on-component): add ignore namespace option
1 parent 16c8778 commit f3d794a

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lib/rules/no-v-text-v-html-on-component.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module.exports = {
2626
type: 'string'
2727
},
2828
uniqueItems: true
29+
},
30+
ignoreElementNamespaces: {
31+
type: 'boolean'
2932
}
3033
},
3134
additionalProperties: false
@@ -41,6 +44,8 @@ module.exports = {
4144
const options = context.options[0] || {}
4245
/** @type {Set<string>} */
4346
const allow = new Set(options.allow)
47+
/** @type {boolean} */
48+
const ignoreElementNamespaces = options.ignoreElementNamespaces === true
4449

4550
/**
4651
* Check whether the given node is an allowed component or not.
@@ -56,13 +61,29 @@ module.exports = {
5661
)
5762
}
5863

64+
/** @param {VElement} element */
65+
function isCustomComponent(element) {
66+
if (ignoreElementNamespaces) {
67+
return (
68+
(!utils.isHtmlWellKnownElementName(element.rawName) &&
69+
!utils.isSvgWellKnownElementName(element.rawName) &&
70+
!utils.isMathWellKnownElementName(element.rawName)) ||
71+
utils.hasAttribute(element, 'is') ||
72+
utils.hasDirective(element, 'bind', 'is') ||
73+
utils.hasDirective(element, 'is')
74+
)
75+
}
76+
77+
return utils.isCustomComponent(element)
78+
}
79+
5980
/**
6081
* Verify for v-text and v-html directive
6182
* @param {VDirective} node
6283
*/
6384
function verify(node) {
6485
const element = node.parent.parent
65-
if (utils.isCustomComponent(element) && !isAllowedComponent(element)) {
86+
if (isCustomComponent(element) && !isAllowedComponent(element)) {
6687
context.report({
6788
node,
6889
loc: node.loc,

tests/lib/rules/no-v-text-v-html-on-component.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ tester.run('no-v-text-v-html-on-component', rule, {
5959
</template>
6060
`,
6161
options: [{ allow: ['RouterLink', 'nuxt-link'] }]
62+
},
63+
{
64+
filename: 'test.vue',
65+
code: `
66+
<template>
67+
<svg><g v-text="content" /></svg>
68+
<math><mspace v-text="content" /></math>
69+
</template>
70+
`
71+
},
72+
{
73+
filename: 'test.vue',
74+
code: `
75+
<template>
76+
<h1 v-html="content" />
77+
<g v-text="content" />
78+
<mi v-text="content" />
79+
</template>
80+
`,
81+
options: [{ ignoreElementNamespaces: true }]
6282
}
6383
],
6484
invalid: [
@@ -167,6 +187,28 @@ tester.run('no-v-text-v-html-on-component', rule, {
167187
column: 22
168188
}
169189
]
190+
},
191+
{
192+
filename: 'test.vue',
193+
code: `
194+
<template>
195+
<g v-text="content" />
196+
<mi v-text="content" />
197+
</template>
198+
`,
199+
options: [{ ignoreElementNamespaces: false }],
200+
errors: [
201+
{
202+
message: "Using v-text on component may break component's content.",
203+
line: 3,
204+
column: 12
205+
},
206+
{
207+
message: "Using v-text on component may break component's content.",
208+
line: 4,
209+
column: 13
210+
}
211+
]
170212
}
171213
]
172214
})

0 commit comments

Comments
 (0)