Skip to content

Commit 4595482

Browse files
committed
add more tests.
1 parent fb87f96 commit 4595482

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ function getKind (node) {
6868

6969
/**
7070
* Check whether the given element is empty or not.
71-
* This ignores whitespaces.
71+
* This ignores whitespaces, doesn't ignore comments.
7272
* @param {VElement} node The element node to check.
7373
* @returns {boolean} `true` if the element is empty.
7474
*/
75-
function isEmpty (node) {
76-
return node.children.every(child => child.type === 'VText' && child.value.trim() === '')
75+
function isEmpty (node, sourceCode) {
76+
const start = node.startTag.range[1]
77+
const end = (node.endTag != null) ? node.endTag.range[0] : node.range[1]
78+
79+
return sourceCode.text.slice(start, end).trim() === ''
7780
}
7881

7982
/**
@@ -83,14 +86,15 @@ function isEmpty (node) {
8386
* @returns {object} AST event handlers.
8487
*/
8588
function create (context) {
89+
const sourceCode = context.getSourceCode()
8690
const options = parseOptions(context.options[0])
8791

8892
utils.registerTemplateBodyVisitor(context, {
8993
'VElement' (node) {
9094
const kind = getKind(node)
9195
const mode = options[kind]
9296

93-
if (mode === 'always' && !node.startTag.selfClosing && isEmpty(node)) {
97+
if (mode === 'always' && !node.startTag.selfClosing && isEmpty(node, sourceCode)) {
9498
context.report({
9599
node,
96100
loc: node.loc,

tests/lib/rules/html-self-closing-style.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,46 @@ tester.run('html-self-closing-style', rule, {
5757
'<template><img></template>',
5858
'<template><x-test/></template>',
5959
'<template><svg><path/></svg></template>',
60-
'<template><math><mspace/></math></template>'
60+
'<template><math><mspace/></math></template>',
6161

62-
// Other cases are in `invalid` tests.
62+
// Don't error if there are comments in their content.
63+
{
64+
code: '<template><div><!-- comment --></div></template>',
65+
output: null,
66+
options: [{ html: { normal: 'always' }}]
67+
}
68+
69+
// other cases are in `invalid` tests.
6370
],
6471
invalid: [
72+
// default
73+
{
74+
code: '<template><div/></template>',
75+
output: '<template><div></div></template>',
76+
errors: ['Disallow self-closing on HTML elements.']
77+
},
78+
{
79+
code: '<template><img/></template>',
80+
output: '<template><img></template>',
81+
errors: ['Disallow self-closing on HTML void elements.']
82+
},
83+
{
84+
code: '<template><x-test></x-test></template>',
85+
output: '<template><x-test/></template>',
86+
errors: ['Require self-closing on Vue.js custom components.']
87+
},
88+
{
89+
code: '<template><svg><path></path></svg></template>',
90+
output: '<template><svg><path/></svg></template>',
91+
errors: ['Require self-closing on SVG elements.']
92+
},
93+
{
94+
code: '<template><math><mspace></mspace></math></template>',
95+
output: '<template><math><mspace/></math></template>',
96+
errors: ['Require self-closing on MathML elements.']
97+
},
98+
99+
// others
65100
{
66101
code: ALL_CODE,
67102
output: `<template>

0 commit comments

Comments
 (0)