@@ -38,6 +38,7 @@ import {
38
38
} from './compat/compatConfig'
39
39
40
40
type OptionalOptions =
41
+ | 'whitespace'
41
42
| 'isNativeTag'
42
43
| 'isBuiltInComponent'
43
44
| keyof CompilerCompatOptions
@@ -65,7 +66,6 @@ const decodeMap: Record<string, string> = {
65
66
66
67
export const defaultParserOptions : MergedParserOptions = {
67
68
delimiters : [ `{{` , `}}` ] ,
68
- whitespace : 'condense' ,
69
69
getNamespace : ( ) => Namespaces . HTML ,
70
70
getTextMode : ( ) => TextModes . DATA ,
71
71
isVoidTag : NO ,
@@ -222,35 +222,37 @@ function parseChildren(
222
222
223
223
// Whitespace handling strategy like v2
224
224
let removedWhitespace = false
225
- if ( context . options . whitespace === 'condense' && mode !== TextModes . RAWTEXT && mode !== TextModes . RCDATA ) {
225
+ if ( mode !== TextModes . RAWTEXT && mode !== TextModes . RCDATA ) {
226
+ const preserve = context . options . whitespace === 'preserve'
226
227
for ( let i = 0 ; i < nodes . length ; i ++ ) {
227
228
const node = nodes [ i ]
228
229
if ( ! context . inPre && node . type === NodeTypes . TEXT ) {
229
230
if ( ! / [ ^ \t \r \n \f ] / . test ( node . content ) ) {
230
231
const prev = nodes [ i - 1 ]
231
232
const next = nodes [ i + 1 ]
232
- // If :
233
+ // Remove if :
233
234
// - the whitespace is the first or last node, or:
234
- // - the whitespace is adjacent to a comment, or:
235
- // - the whitespace is between two elements AND contains newline
236
- // Then the whitespace is ignored.
235
+ // - (condense mode) the whitespace is adjacent to a comment, or:
236
+ // - (condense mode) the whitespace is between two elements AND contains newline
237
237
if (
238
238
! prev ||
239
239
! next ||
240
- prev . type === NodeTypes . COMMENT ||
241
- next . type === NodeTypes . COMMENT ||
242
- ( prev . type === NodeTypes . ELEMENT &&
243
- next . type === NodeTypes . ELEMENT &&
244
- / [ \r \n ] / . test ( node . content ) )
240
+ ( ! preserve &&
241
+ ( prev . type === NodeTypes . COMMENT ||
242
+ next . type === NodeTypes . COMMENT ||
243
+ ( prev . type === NodeTypes . ELEMENT &&
244
+ next . type === NodeTypes . ELEMENT &&
245
+ / [ \r \n ] / . test ( node . content ) ) ) )
245
246
) {
246
247
removedWhitespace = true
247
248
nodes [ i ] = null as any
248
249
} else {
249
- // Otherwise, condensed consecutive whitespace inside the text
250
- // down to a single space
250
+ // Otherwise, the whitespace is condensed into a single space
251
251
node . content = ' '
252
252
}
253
- } else {
253
+ } else if ( ! preserve ) {
254
+ // in condense mode, consecutive whitespaces in text are condensed
255
+ // down to a single space.
254
256
node . content = node . content . replace ( / [ \t \r \n \f ] + / g, ' ' )
255
257
}
256
258
}
0 commit comments