Skip to content

Commit

Permalink
fix(sfc): treat custom block content as raw text
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 23, 2019
1 parent 90ddb7c commit d6275a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export interface ParserOptions {
isCustomElement?: (tag: string) => boolean
isBuiltInComponent?: (tag: string) => symbol | void
getNamespace?: (tag: string, parent: ElementNode | undefined) => Namespace
getTextMode?: (tag: string, ns: Namespace) => TextModes
getTextMode?: (
tag: string,
ns: Namespace,
parent: ElementNode | undefined
) => TextModes
delimiters?: [string, string] // ['{{', '}}']

// Map to HTML entities. E.g., `{ "amp;": "&" }`
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function parseElement(

// Children.
ancestors.push(element)
const mode = context.options.getTextMode(element.tag, element.ns)
const mode = context.options.getTextMode(element.tag, element.ns, parent)
const children = parseChildren(context, mode, ancestors)
ancestors.pop()

Expand Down
10 changes: 8 additions & 2 deletions packages/compiler-sfc/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ h1 { color: red }
<template v-if="ok">ok</template>
<div><div></div></div>
`
const sfc = parse(`<template>${content}</template>`).descriptor
expect(sfc.template!.content).toBe(content)
const { descriptor } = parse(`<template>${content}</template>`)
expect(descriptor.template!.content).toBe(content)
})

test('error tolerance', () => {
Expand All @@ -102,6 +102,12 @@ h1 { color: red }
expect(errors.length).toBe(1)
})

test('treat custom blocks as raw text', () => {
const { errors, descriptor } = parse(`<foo> <-& </foo>`)
expect(errors.length).toBe(0)
expect(descriptor.customBlocks[0].content).toBe(` <-& `)
})

describe('warnings', () => {
test('should only allow single template element', () => {
parse(`<template><div/></template><template><div/></template>`)
Expand Down
12 changes: 11 additions & 1 deletion packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
NodeTypes,
ElementNode,
SourceLocation,
CompilerError
CompilerError,
TextModes
} from '@vue/compiler-core'
import { RawSourceMap, SourceMapGenerator } from 'source-map'
import LRUCache from 'lru-cache'
Expand Down Expand Up @@ -89,6 +90,15 @@ export function parse(
isNativeTag: () => true,
// preserve all whitespaces
isPreTag: () => true,
getTextMode: (tag, _ns, parent) => {
// all top level elements except <template> are parsed as raw text
// containers
if (!parent && tag !== 'template') {
return TextModes.RAWTEXT
} else {
return TextModes.DATA
}
},
onError: e => {
errors.push(e)
}
Expand Down

0 comments on commit d6275a3

Please sign in to comment.