Skip to content

Fixed: not to indentation the contents on the <textarea> in html-indent #681

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
Nov 26, 2018
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
4 changes: 3 additions & 1 deletion lib/utils/indent-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const LT_CHAR = /[\r\n\u2028\u2029]/
const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])/g
const BLOCK_COMMENT_PREFIX = /^\s*\*/
const ITERATION_OPTS = Object.freeze({ includeComments: true, filter: isNotWhitespace })
const PREFORMATTED_ELEMENT_NAMES = ['pre', 'textarea']

/**
* Normalize options.
Expand Down Expand Up @@ -324,6 +325,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
includeComments: true,
filter: token => token != null && (
token.type === 'HTMLText' ||
token.type === 'HTMLRCDataText' ||
token.type === 'HTMLTagOpen' ||
token.type === 'HTMLEndTagOpen' ||
token.type === 'HTMLComment'
Expand Down Expand Up @@ -881,7 +883,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
},

VElement (node) {
if (node.name !== 'pre') {
if (!PREFORMATTED_ELEMENT_NAMES.includes(node.name)) {
const isTopLevel = node.parent.type !== 'VElement'
const offset = isTopLevel ? options.baseIndent : 1
processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, offset)
Expand Down
93 changes: 79 additions & 14 deletions tests/lib/rules/html-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ tester.run('html-indent', rule, loadPatterns(
}]
},

// Pre
// Pre, Textarea
{
filename: 'test.vue',
code: unIndent`
Expand All @@ -282,6 +282,18 @@ tester.run('html-indent', rule, loadPatterns(
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<textarea>
aaa
bbb
ccc
</textarea>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
Expand Down Expand Up @@ -327,6 +339,17 @@ tester.run('html-indent', rule, loadPatterns(
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<div><textarea>aaa
bbb ccc
ddd
fff</textarea></div>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
Expand Down Expand Up @@ -619,7 +642,7 @@ tester.run('html-indent', rule, loadPatterns(
]
},

// Pre
// Pre, Textarea
{
filename: 'test.vue',
code: unIndent`
Expand Down Expand Up @@ -668,19 +691,61 @@ tester.run('html-indent', rule, loadPatterns(
</template>
`,
output: unIndent`
<template>
<pre
:class="[
'a',
'b',
'c'
]"
<template>
<pre
:class="[
'a',
'b',
'c'
]"
>
aaa
bbb
ccc
</pre>
</template>
`,
errors: [
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 4 },
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 5 },
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 6 },
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 7 },
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 8 }
]
},
{
filename: 'test.vue',
code: unIndent`
<template>
<textarea
:class="[
'a',
'b',
'c'
]"
>
aaa
bbb
ccc
</pre>
</template>
aaa
bbb
ccc
</textarea>
</template>
`,
output: unIndent`
<template>
<textarea
:class="[
'a',
'b',
'c'
]"
>
aaa
bbb
ccc
</textarea>
</template>
`,
errors: [
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
Expand Down