diff --git a/packages/compiler-core/src/options.ts b/packages/compiler-core/src/options.ts index 2e08cdae..8dca9210 100644 --- a/packages/compiler-core/src/options.ts +++ b/packages/compiler-core/src/options.ts @@ -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;": "&" }` diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index b9b37953..9c3299a3 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -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() diff --git a/packages/compiler-sfc/__tests__/parse.spec.ts b/packages/compiler-sfc/__tests__/parse.spec.ts index 6a4534c1..affe1f5a 100644 --- a/packages/compiler-sfc/__tests__/parse.spec.ts +++ b/packages/compiler-sfc/__tests__/parse.spec.ts @@ -78,8 +78,8 @@ h1 { color: red }
` - const sfc = parse(``).descriptor - expect(sfc.template!.content).toBe(content) + const { descriptor } = parse(``) + expect(descriptor.template!.content).toBe(content) }) test('error tolerance', () => { @@ -102,6 +102,12 @@ h1 { color: red } expect(errors.length).toBe(1) }) + test('treat custom blocks as raw text', () => { + const { errors, descriptor } = parse(` <-& `) + expect(errors.length).toBe(0) + expect(descriptor.customBlocks[0].content).toBe(` <-& `) + }) + describe('warnings', () => { test('should only allow single template element', () => { parse(``) diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts index 38c611df..1953027c 100644 --- a/packages/compiler-sfc/src/parse.ts +++ b/packages/compiler-sfc/src/parse.ts @@ -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' @@ -89,6 +90,15 @@ export function parse( isNativeTag: () => true, // preserve all whitespaces isPreTag: () => true, + getTextMode: (tag, _ns, parent) => { + // all top level elements except