Skip to content

Commit b8653d3

Browse files
authored
fix(compiler): condense whitespaces in static class attributes (#4432)
fix #4251
1 parent 5d262e0 commit b8653d3

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

packages/compiler-core/__tests__/parse.spec.ts

+65
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,71 @@ describe('compiler: parse', () => {
10151015
})
10161016
})
10171017

1018+
// https://github.com/vuejs/vue-next/issues/4251
1019+
test('class attribute should ignore whitespace when parsed', () => {
1020+
const ast = baseParse('<div class=" \n\t c \t\n "></div>')
1021+
const element = ast.children[0] as ElementNode
1022+
1023+
expect(element).toStrictEqual({
1024+
children: [],
1025+
codegenNode: undefined,
1026+
isSelfClosing: false,
1027+
loc: {
1028+
end: {
1029+
column: 10,
1030+
line: 3,
1031+
offset: 29
1032+
},
1033+
source: '<div class=" \n\t c \t\n "></div>',
1034+
start: {
1035+
column: 1,
1036+
line: 1,
1037+
offset: 0
1038+
}
1039+
},
1040+
ns: 0,
1041+
props: [
1042+
{
1043+
loc: {
1044+
end: {
1045+
column: 3,
1046+
line: 3,
1047+
offset: 22
1048+
},
1049+
source: 'class=" \n\t c \t\n "',
1050+
start: {
1051+
column: 6,
1052+
line: 1,
1053+
offset: 5
1054+
}
1055+
},
1056+
name: 'class',
1057+
type: 6,
1058+
value: {
1059+
content: 'c',
1060+
loc: {
1061+
end: {
1062+
column: 3,
1063+
line: 3,
1064+
offset: 22
1065+
},
1066+
source: '" \n\t c \t\n "',
1067+
start: {
1068+
column: 12,
1069+
line: 1,
1070+
offset: 11
1071+
}
1072+
},
1073+
type: 2
1074+
}
1075+
}
1076+
],
1077+
tag: 'div',
1078+
tagType: 0,
1079+
type: 1
1080+
})
1081+
})
1082+
10181083
test('directive with no value', () => {
10191084
const ast = baseParse('<div v-if/>')
10201085
const directive = (ast.children[0] as ElementNode).props[0]

packages/compiler-core/src/parse.ts

+11
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,17 @@ function parseAttributes(
716716
}
717717

718718
const attr = parseAttribute(context, attributeNames)
719+
720+
// Trim whitespace between class
721+
// https://github.com/vuejs/vue-next/issues/4251
722+
if (
723+
attr.type === NodeTypes.ATTRIBUTE &&
724+
attr.value &&
725+
attr.name === 'class'
726+
) {
727+
attr.value.content = attr.value.content.replace(/\s+/g, ' ').trim()
728+
}
729+
719730
if (type === TagType.Start) {
720731
props.push(attr)
721732
}

0 commit comments

Comments
 (0)