Skip to content

Commit 6ef20aa

Browse files
ota-meshimichalsnik
authored andcommitted
Fixed: not to indentation the contents on the <textarea> in html-indent (#681)
1 parent 147c765 commit 6ef20aa

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

Diff for: lib/utils/indent-common.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const LT_CHAR = /[\r\n\u2028\u2029]/
1919
const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])/g
2020
const BLOCK_COMMENT_PREFIX = /^\s*\*/
2121
const ITERATION_OPTS = Object.freeze({ includeComments: true, filter: isNotWhitespace })
22+
const PREFORMATTED_ELEMENT_NAMES = ['pre', 'textarea']
2223

2324
/**
2425
* Normalize options.
@@ -324,6 +325,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
324325
includeComments: true,
325326
filter: token => token != null && (
326327
token.type === 'HTMLText' ||
328+
token.type === 'HTMLRCDataText' ||
327329
token.type === 'HTMLTagOpen' ||
328330
token.type === 'HTMLEndTagOpen' ||
329331
token.type === 'HTMLComment'
@@ -881,7 +883,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
881883
},
882884

883885
VElement (node) {
884-
if (node.name !== 'pre') {
886+
if (!PREFORMATTED_ELEMENT_NAMES.includes(node.name)) {
885887
const isTopLevel = node.parent.type !== 'VElement'
886888
const offset = isTopLevel ? options.baseIndent : 1
887889
processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, offset)

Diff for: tests/lib/rules/html-indent.js

+79-14
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ tester.run('html-indent', rule, loadPatterns(
269269
}]
270270
},
271271

272-
// Pre
272+
// Pre, Textarea
273273
{
274274
filename: 'test.vue',
275275
code: unIndent`
@@ -282,6 +282,18 @@ tester.run('html-indent', rule, loadPatterns(
282282
</template>
283283
`
284284
},
285+
{
286+
filename: 'test.vue',
287+
code: unIndent`
288+
<template>
289+
<textarea>
290+
aaa
291+
bbb
292+
ccc
293+
</textarea>
294+
</template>
295+
`
296+
},
285297
{
286298
filename: 'test.vue',
287299
code: unIndent`
@@ -327,6 +339,17 @@ tester.run('html-indent', rule, loadPatterns(
327339
</template>
328340
`
329341
},
342+
{
343+
filename: 'test.vue',
344+
code: unIndent`
345+
<template>
346+
<div><textarea>aaa
347+
bbb ccc
348+
ddd
349+
fff</textarea></div>
350+
</template>
351+
`
352+
},
330353
{
331354
filename: 'test.vue',
332355
code: unIndent`
@@ -619,7 +642,7 @@ tester.run('html-indent', rule, loadPatterns(
619642
]
620643
},
621644

622-
// Pre
645+
// Pre, Textarea
623646
{
624647
filename: 'test.vue',
625648
code: unIndent`
@@ -668,19 +691,61 @@ tester.run('html-indent', rule, loadPatterns(
668691
</template>
669692
`,
670693
output: unIndent`
671-
<template>
672-
<pre
673-
:class="[
674-
'a',
675-
'b',
676-
'c'
677-
]"
694+
<template>
695+
<pre
696+
:class="[
697+
'a',
698+
'b',
699+
'c'
700+
]"
701+
>
702+
aaa
703+
bbb
704+
ccc
705+
</pre>
706+
</template>
707+
`,
708+
errors: [
709+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
710+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
711+
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 4 },
712+
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 5 },
713+
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 6 },
714+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 7 },
715+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 8 }
716+
]
717+
},
718+
{
719+
filename: 'test.vue',
720+
code: unIndent`
721+
<template>
722+
<textarea
723+
:class="[
724+
'a',
725+
'b',
726+
'c'
727+
]"
678728
>
679-
aaa
680-
bbb
681-
ccc
682-
</pre>
683-
</template>
729+
aaa
730+
bbb
731+
ccc
732+
</textarea>
733+
</template>
734+
`,
735+
output: unIndent`
736+
<template>
737+
<textarea
738+
:class="[
739+
'a',
740+
'b',
741+
'c'
742+
]"
743+
>
744+
aaa
745+
bbb
746+
ccc
747+
</textarea>
748+
</template>
684749
`,
685750
errors: [
686751
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },

0 commit comments

Comments
 (0)