Skip to content

Commit ec6abe8

Browse files
committed
fix(compiler-sfc): fix whitespace preservation when block contains single self-closing tag
1 parent 47f4883 commit ec6abe8

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

packages/compiler-core/src/parse.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ export interface ParserContext {
9696
offset: number
9797
line: number
9898
column: number
99-
inPre: boolean // HTML <pre> tag, preserve whitespaces
100-
inVPre: boolean // v-pre, do not process directives and interpolations
99+
inPre: number // HTML <pre> tag, preserve whitespaces
100+
inVPre: number // v-pre, do not process directives and interpolations
101101
onWarn: NonNullable<ErrorHandlingOptions['onWarn']>
102102
}
103103

@@ -134,8 +134,8 @@ function createParserContext(
134134
offset: 0,
135135
originalSource: content,
136136
source: content,
137-
inPre: false,
138-
inVPre: false,
137+
inPre: 0,
138+
inVPre: 0,
139139
onWarn: options.onWarn
140140
}
141141
}
@@ -254,7 +254,7 @@ function parseChildren(
254254
// Whitespace handling strategy like v2
255255
let removedWhitespace = false
256256
if (mode !== TextModes.RAWTEXT && mode !== TextModes.RCDATA) {
257-
const preserve = context.options.whitespace === 'preserve'
257+
const shouldCondense = context.options.whitespace !== 'preserve'
258258
for (let i = 0; i < nodes.length; i++) {
259259
const node = nodes[i]
260260
if (!context.inPre && node.type === NodeTypes.TEXT) {
@@ -268,7 +268,7 @@ function parseChildren(
268268
if (
269269
!prev ||
270270
!next ||
271-
(!preserve &&
271+
(shouldCondense &&
272272
(prev.type === NodeTypes.COMMENT ||
273273
next.type === NodeTypes.COMMENT ||
274274
(prev.type === NodeTypes.ELEMENT &&
@@ -281,7 +281,7 @@ function parseChildren(
281281
// Otherwise, the whitespace is condensed into a single space
282282
node.content = ' '
283283
}
284-
} else if (!preserve) {
284+
} else if (shouldCondense) {
285285
// in condense mode, consecutive whitespaces in text are condensed
286286
// down to a single space.
287287
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
@@ -428,7 +428,7 @@ function parseElement(
428428
if (element.isSelfClosing || context.options.isVoidTag(element.tag)) {
429429
// #4030 self-closing <pre> tag
430430
if (context.options.isPreTag(element.tag)) {
431-
context.inPre = false
431+
context.inPre--
432432
}
433433
return element
434434
}
@@ -479,10 +479,10 @@ function parseElement(
479479
element.loc = getSelection(context, element.loc.start)
480480

481481
if (isPreBoundary) {
482-
context.inPre = false
482+
context.inPre--
483483
}
484484
if (isVPreBoundary) {
485-
context.inVPre = false
485+
context.inVPre--
486486
}
487487
return element
488488
}
@@ -534,9 +534,8 @@ function parseTag(
534534
const currentSource = context.source
535535

536536
// check <pre> tag
537-
const isPreTag = context.options.isPreTag(tag)
538-
if (isPreTag) {
539-
context.inPre = true
537+
if (context.options.isPreTag(tag)) {
538+
context.inPre++
540539
}
541540

542541
// Attributes.
@@ -548,7 +547,7 @@ function parseTag(
548547
!context.inVPre &&
549548
props.some(p => p.type === NodeTypes.DIRECTIVE && p.name === 'pre')
550549
) {
551-
context.inVPre = true
550+
context.inVPre++
552551
// reset context
553552
extend(context, cursor)
554553
context.source = currentSource

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

+16
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ h1 { color: red }
111111
)
112112
})
113113

114+
test('should parse correct range for root level self closing tag', () => {
115+
const content = `\n <div/>\n`
116+
const { descriptor } = parse(`<template>${content}</template>`)
117+
expect(descriptor.template).toBeTruthy()
118+
expect(descriptor.template!.content).toBe(content)
119+
expect(descriptor.template!.loc).toMatchObject({
120+
start: { line: 1, column: 11, offset: 10 },
121+
end: {
122+
line: 3,
123+
column: 1,
124+
offset: 10 + content.length
125+
},
126+
source: content
127+
})
128+
})
129+
114130
test('should parse correct range for blocks with no content (self closing)', () => {
115131
const { descriptor } = parse(`<template/>`)
116132
expect(descriptor.template).toBeTruthy()

0 commit comments

Comments
 (0)