2021-04-13 07:42:09 +08:00
|
|
|
import { ErrorHandlingOptions, ParserOptions } from './options'
|
2020-06-11 04:54:23 +08:00
|
|
|
import { NO, isArray, makeMap, extend } from '@vue/shared'
|
2021-04-13 07:42:09 +08:00
|
|
|
import {
|
|
|
|
ErrorCodes,
|
|
|
|
createCompilerError,
|
|
|
|
defaultOnError,
|
|
|
|
defaultOnWarn
|
|
|
|
} from './errors'
|
2019-09-20 11:05:51 +08:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
advancePositionWithMutation,
|
2019-11-30 01:42:04 +08:00
|
|
|
advancePositionWithClone,
|
2021-06-22 04:16:49 +08:00
|
|
|
isCoreComponent,
|
|
|
|
isBindKey
|
2019-09-20 11:05:51 +08:00
|
|
|
} from './utils'
|
2019-09-17 02:43:29 +08:00
|
|
|
import {
|
2019-09-17 23:07:46 +08:00
|
|
|
Namespaces,
|
2019-09-17 02:43:29 +08:00
|
|
|
AttributeNode,
|
|
|
|
CommentNode,
|
|
|
|
DirectiveNode,
|
|
|
|
ElementNode,
|
|
|
|
ElementTypes,
|
|
|
|
ExpressionNode,
|
|
|
|
NodeTypes,
|
|
|
|
Position,
|
|
|
|
RootNode,
|
|
|
|
SourceLocation,
|
2019-09-18 07:08:47 +08:00
|
|
|
TextNode,
|
2019-10-01 21:27:34 +08:00
|
|
|
TemplateChildNode,
|
2020-02-08 02:56:18 +08:00
|
|
|
InterpolationNode,
|
2020-11-21 08:26:07 +08:00
|
|
|
createRoot,
|
|
|
|
ConstantTypes
|
2019-09-17 02:43:29 +08:00
|
|
|
} from './ast'
|
2021-04-16 23:43:05 +08:00
|
|
|
import {
|
|
|
|
checkCompatEnabled,
|
|
|
|
CompilerCompatOptions,
|
2021-04-18 02:38:30 +08:00
|
|
|
CompilerDeprecationTypes,
|
2021-05-01 03:50:32 +08:00
|
|
|
isCompatEnabled,
|
2021-04-18 02:38:30 +08:00
|
|
|
warnDeprecation
|
2021-04-16 23:43:05 +08:00
|
|
|
} from './compat/compatConfig'
|
2019-09-17 02:43:29 +08:00
|
|
|
|
2021-04-13 07:42:09 +08:00
|
|
|
type OptionalOptions =
|
2021-04-26 22:49:21 +08:00
|
|
|
| 'whitespace'
|
2021-04-13 07:42:09 +08:00
|
|
|
| 'isNativeTag'
|
|
|
|
| 'isBuiltInComponent'
|
|
|
|
| keyof CompilerCompatOptions
|
2019-11-30 01:13:49 +08:00
|
|
|
type MergedParserOptions = Omit<Required<ParserOptions>, OptionalOptions> &
|
|
|
|
Pick<ParserOptions, OptionalOptions>
|
2020-12-05 05:28:26 +08:00
|
|
|
type AttributeValue =
|
|
|
|
| {
|
|
|
|
content: string
|
|
|
|
isQuoted: boolean
|
|
|
|
loc: SourceLocation
|
|
|
|
}
|
|
|
|
| undefined
|
2019-10-11 02:54:06 +08:00
|
|
|
|
2020-04-09 06:51:25 +08:00
|
|
|
// The default decoder only provides escapes for characters reserved as part of
|
2020-05-01 21:42:58 +08:00
|
|
|
// the template syntax, and is only used if the custom renderer did not provide
|
2020-04-09 06:51:25 +08:00
|
|
|
// a platform-specific decoder.
|
|
|
|
const decodeRE = /&(gt|lt|amp|apos|quot);/g
|
|
|
|
const decodeMap: Record<string, string> = {
|
|
|
|
gt: '>',
|
|
|
|
lt: '<',
|
|
|
|
amp: '&',
|
|
|
|
apos: "'",
|
|
|
|
quot: '"'
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:54:06 +08:00
|
|
|
export const defaultParserOptions: MergedParserOptions = {
|
2019-09-17 23:07:46 +08:00
|
|
|
delimiters: [`{{`, `}}`],
|
|
|
|
getNamespace: () => Namespaces.HTML,
|
|
|
|
getTextMode: () => TextModes.DATA,
|
2019-10-11 02:54:06 +08:00
|
|
|
isVoidTag: NO,
|
2019-10-25 04:42:09 +08:00
|
|
|
isPreTag: NO,
|
2019-10-16 05:30:47 +08:00
|
|
|
isCustomElement: NO,
|
2020-04-09 06:51:25 +08:00
|
|
|
decodeEntities: (rawText: string): string =>
|
|
|
|
rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
|
2020-08-17 23:20:28 +08:00
|
|
|
onError: defaultOnError,
|
2021-04-13 07:42:09 +08:00
|
|
|
onWarn: defaultOnWarn,
|
2021-07-19 23:40:37 +08:00
|
|
|
comments: __DEV__
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const enum TextModes {
|
2019-09-17 23:07:46 +08:00
|
|
|
// | Elements | Entities | End sign | Inside of
|
2020-05-02 06:48:44 +08:00
|
|
|
DATA, // | ✔ | ✔ | End tags of ancestors |
|
|
|
|
RCDATA, // | ✘ | ✔ | End tag of the parent | <textarea>
|
|
|
|
RAWTEXT, // | ✘ | ✘ | End tag of the parent | <style>,<script>
|
2019-09-17 02:43:29 +08:00
|
|
|
CDATA,
|
|
|
|
ATTRIBUTE_VALUE
|
|
|
|
}
|
|
|
|
|
2020-04-09 06:51:25 +08:00
|
|
|
export interface ParserContext {
|
2019-10-11 02:54:06 +08:00
|
|
|
options: MergedParserOptions
|
2019-09-17 02:43:29 +08:00
|
|
|
readonly originalSource: string
|
|
|
|
source: string
|
|
|
|
offset: number
|
|
|
|
line: number
|
|
|
|
column: number
|
2021-07-20 07:23:44 +08:00
|
|
|
inPre: boolean // HTML <pre> tag, preserve whitespaces
|
|
|
|
inVPre: boolean // v-pre, do not process directives and interpolations
|
2021-04-13 07:42:09 +08:00
|
|
|
onWarn: NonNullable<ErrorHandlingOptions['onWarn']>
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 08:44:21 +08:00
|
|
|
export function baseParse(
|
|
|
|
content: string,
|
|
|
|
options: ParserOptions = {}
|
|
|
|
): RootNode {
|
2019-09-17 02:43:29 +08:00
|
|
|
const context = createParserContext(content, options)
|
|
|
|
const start = getCursor(context)
|
2020-02-08 02:56:18 +08:00
|
|
|
return createRoot(
|
|
|
|
parseChildren(context, TextModes.DATA, []),
|
|
|
|
getSelection(context, start)
|
|
|
|
)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createParserContext(
|
|
|
|
content: string,
|
2020-07-23 09:29:59 +08:00
|
|
|
rawOptions: ParserOptions
|
2019-09-17 02:43:29 +08:00
|
|
|
): ParserContext {
|
2020-07-23 09:29:59 +08:00
|
|
|
const options = extend({}, defaultParserOptions)
|
2021-07-19 23:40:37 +08:00
|
|
|
|
|
|
|
let key: keyof ParserOptions
|
|
|
|
for (key in rawOptions) {
|
2020-07-23 09:29:59 +08:00
|
|
|
// @ts-ignore
|
2021-07-19 23:40:37 +08:00
|
|
|
options[key] =
|
|
|
|
rawOptions[key] === undefined
|
|
|
|
? defaultParserOptions[key]
|
|
|
|
: rawOptions[key]
|
2020-07-23 09:29:59 +08:00
|
|
|
}
|
2019-09-17 02:43:29 +08:00
|
|
|
return {
|
2020-07-23 09:29:59 +08:00
|
|
|
options,
|
2019-09-17 02:43:29 +08:00
|
|
|
column: 1,
|
|
|
|
line: 1,
|
|
|
|
offset: 0,
|
|
|
|
originalSource: content,
|
|
|
|
source: content,
|
2021-07-20 07:23:44 +08:00
|
|
|
inPre: false,
|
|
|
|
inVPre: false,
|
2021-04-13 07:42:09 +08:00
|
|
|
onWarn: options.onWarn
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseChildren(
|
|
|
|
context: ParserContext,
|
|
|
|
mode: TextModes,
|
|
|
|
ancestors: ElementNode[]
|
2019-10-01 21:27:34 +08:00
|
|
|
): TemplateChildNode[] {
|
2019-09-17 02:43:29 +08:00
|
|
|
const parent = last(ancestors)
|
|
|
|
const ns = parent ? parent.ns : Namespaces.HTML
|
2019-10-01 21:27:34 +08:00
|
|
|
const nodes: TemplateChildNode[] = []
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
while (!isEnd(context, mode, ancestors)) {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(context.source.length > 0)
|
2019-09-17 02:43:29 +08:00
|
|
|
const s = context.source
|
2019-10-01 21:27:34 +08:00
|
|
|
let node: TemplateChildNode | TemplateChildNode[] | undefined = undefined
|
2019-09-17 02:43:29 +08:00
|
|
|
|
2020-03-24 05:25:29 +08:00
|
|
|
if (mode === TextModes.DATA || mode === TextModes.RCDATA) {
|
2020-04-04 09:02:20 +08:00
|
|
|
if (!context.inVPre && startsWith(s, context.options.delimiters[0])) {
|
2019-12-11 04:30:17 +08:00
|
|
|
// '{{'
|
|
|
|
node = parseInterpolation(context, mode)
|
2020-03-24 05:25:29 +08:00
|
|
|
} else if (mode === TextModes.DATA && s[0] === '<') {
|
2019-12-11 04:30:17 +08:00
|
|
|
// https://html.spec.whatwg.org/multipage/parsing.html#tag-open-state
|
|
|
|
if (s.length === 1) {
|
|
|
|
emitError(context, ErrorCodes.EOF_BEFORE_TAG_NAME, 1)
|
|
|
|
} else if (s[1] === '!') {
|
|
|
|
// https://html.spec.whatwg.org/multipage/parsing.html#markup-declaration-open-state
|
|
|
|
if (startsWith(s, '<!--')) {
|
|
|
|
node = parseComment(context)
|
|
|
|
} else if (startsWith(s, '<!DOCTYPE')) {
|
|
|
|
// Ignore DOCTYPE by a limitation.
|
|
|
|
node = parseBogusComment(context)
|
|
|
|
} else if (startsWith(s, '<![CDATA[')) {
|
|
|
|
if (ns !== Namespaces.HTML) {
|
|
|
|
node = parseCDATA(context, ancestors)
|
|
|
|
} else {
|
|
|
|
emitError(context, ErrorCodes.CDATA_IN_HTML_CONTENT)
|
|
|
|
node = parseBogusComment(context)
|
|
|
|
}
|
2019-09-17 02:43:29 +08:00
|
|
|
} else {
|
2019-12-11 04:30:17 +08:00
|
|
|
emitError(context, ErrorCodes.INCORRECTLY_OPENED_COMMENT)
|
2019-09-17 02:43:29 +08:00
|
|
|
node = parseBogusComment(context)
|
|
|
|
}
|
2019-12-11 04:30:17 +08:00
|
|
|
} else if (s[1] === '/') {
|
|
|
|
// https://html.spec.whatwg.org/multipage/parsing.html#end-tag-open-state
|
|
|
|
if (s.length === 2) {
|
|
|
|
emitError(context, ErrorCodes.EOF_BEFORE_TAG_NAME, 2)
|
|
|
|
} else if (s[2] === '>') {
|
|
|
|
emitError(context, ErrorCodes.MISSING_END_TAG_NAME, 2)
|
|
|
|
advanceBy(context, 3)
|
|
|
|
continue
|
|
|
|
} else if (/[a-z]/i.test(s[2])) {
|
|
|
|
emitError(context, ErrorCodes.X_INVALID_END_TAG)
|
|
|
|
parseTag(context, TagType.End, parent)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
emitError(
|
|
|
|
context,
|
|
|
|
ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
|
|
|
|
2
|
|
|
|
)
|
|
|
|
node = parseBogusComment(context)
|
|
|
|
}
|
|
|
|
} else if (/[a-z]/i.test(s[1])) {
|
|
|
|
node = parseElement(context, ancestors)
|
2021-05-01 03:50:32 +08:00
|
|
|
|
|
|
|
// 2.x <template> with no directive compat
|
|
|
|
if (
|
|
|
|
__COMPAT__ &&
|
|
|
|
isCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_NATIVE_TEMPLATE,
|
|
|
|
context
|
|
|
|
) &&
|
|
|
|
node &&
|
|
|
|
node.tag === 'template' &&
|
|
|
|
!node.props.some(
|
|
|
|
p =>
|
|
|
|
p.type === NodeTypes.DIRECTIVE &&
|
2021-05-05 06:10:57 +08:00
|
|
|
isSpecialTemplateDirective(p.name)
|
2021-05-01 03:50:32 +08:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
__DEV__ &&
|
|
|
|
warnDeprecation(
|
|
|
|
CompilerDeprecationTypes.COMPILER_NATIVE_TEMPLATE,
|
|
|
|
context,
|
|
|
|
node.loc
|
|
|
|
)
|
|
|
|
node = node.children
|
|
|
|
}
|
2019-12-11 04:30:17 +08:00
|
|
|
} else if (s[1] === '?') {
|
|
|
|
emitError(
|
|
|
|
context,
|
|
|
|
ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
|
|
|
|
1
|
|
|
|
)
|
2019-09-17 02:43:29 +08:00
|
|
|
node = parseBogusComment(context)
|
|
|
|
} else {
|
2019-12-11 04:30:17 +08:00
|
|
|
emitError(context, ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME, 1)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!node) {
|
|
|
|
node = parseText(context, mode)
|
|
|
|
}
|
|
|
|
|
2019-11-08 22:52:24 +08:00
|
|
|
if (isArray(node)) {
|
2019-09-18 07:08:47 +08:00
|
|
|
for (let i = 0; i < node.length; i++) {
|
2019-10-25 04:22:48 +08:00
|
|
|
pushNode(nodes, node[i])
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
2019-09-17 02:43:29 +08:00
|
|
|
} else {
|
2019-10-25 04:22:48 +08:00
|
|
|
pushNode(nodes, node)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 21:28:09 +08:00
|
|
|
// Whitespace handling strategy like v2
|
2019-10-25 04:22:48 +08:00
|
|
|
let removedWhitespace = false
|
2021-04-26 22:49:21 +08:00
|
|
|
if (mode !== TextModes.RAWTEXT && mode !== TextModes.RCDATA) {
|
2021-07-20 06:54:35 +08:00
|
|
|
const shouldCondense = context.options.whitespace !== 'preserve'
|
2020-10-06 05:53:17 +08:00
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
const node = nodes[i]
|
|
|
|
if (!context.inPre && node.type === NodeTypes.TEXT) {
|
|
|
|
if (!/[^\t\r\n\f ]/.test(node.content)) {
|
|
|
|
const prev = nodes[i - 1]
|
|
|
|
const next = nodes[i + 1]
|
2021-04-26 22:49:21 +08:00
|
|
|
// Remove if:
|
2020-10-06 05:53:17 +08:00
|
|
|
// - the whitespace is the first or last node, or:
|
2021-04-26 22:49:21 +08:00
|
|
|
// - (condense mode) the whitespace is adjacent to a comment, or:
|
|
|
|
// - (condense mode) the whitespace is between two elements AND contains newline
|
2020-10-06 05:53:17 +08:00
|
|
|
if (
|
|
|
|
!prev ||
|
|
|
|
!next ||
|
2021-07-20 06:54:35 +08:00
|
|
|
(shouldCondense &&
|
2021-04-26 22:49:21 +08:00
|
|
|
(prev.type === NodeTypes.COMMENT ||
|
|
|
|
next.type === NodeTypes.COMMENT ||
|
|
|
|
(prev.type === NodeTypes.ELEMENT &&
|
|
|
|
next.type === NodeTypes.ELEMENT &&
|
|
|
|
/[\r\n]/.test(node.content))))
|
2020-10-06 05:53:17 +08:00
|
|
|
) {
|
|
|
|
removedWhitespace = true
|
|
|
|
nodes[i] = null as any
|
2019-10-25 04:42:09 +08:00
|
|
|
} else {
|
2021-04-26 22:49:21 +08:00
|
|
|
// Otherwise, the whitespace is condensed into a single space
|
2020-10-06 05:53:17 +08:00
|
|
|
node.content = ' '
|
2019-10-25 04:42:09 +08:00
|
|
|
}
|
2021-07-20 06:54:35 +08:00
|
|
|
} else if (shouldCondense) {
|
2021-04-26 22:49:21 +08:00
|
|
|
// in condense mode, consecutive whitespaces in text are condensed
|
|
|
|
// down to a single space.
|
2020-10-06 05:53:17 +08:00
|
|
|
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
|
2019-10-25 04:22:48 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:40:37 +08:00
|
|
|
// Remove comment nodes if desired by configuration.
|
|
|
|
else if (node.type === NodeTypes.COMMENT && !context.options.comments) {
|
2020-10-06 05:53:17 +08:00
|
|
|
removedWhitespace = true
|
|
|
|
nodes[i] = null as any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (context.inPre && parent && context.options.isPreTag(parent.tag)) {
|
2020-04-04 09:02:20 +08:00
|
|
|
// remove leading newline per html spec
|
|
|
|
// https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
|
|
|
|
const first = nodes[0]
|
|
|
|
if (first && first.type === NodeTypes.TEXT) {
|
|
|
|
first.content = first.content.replace(/^\r?\n/, '')
|
|
|
|
}
|
2019-10-25 04:22:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 00:26:45 +08:00
|
|
|
return removedWhitespace ? nodes.filter(Boolean) : nodes
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-10-25 04:22:48 +08:00
|
|
|
function pushNode(nodes: TemplateChildNode[], node: TemplateChildNode): void {
|
2019-10-25 00:47:29 +08:00
|
|
|
if (node.type === NodeTypes.TEXT) {
|
2019-10-25 04:22:48 +08:00
|
|
|
const prev = last(nodes)
|
2019-10-25 00:47:29 +08:00
|
|
|
// Merge if both this and the previous node are text and those are
|
|
|
|
// consecutive. This happens for cases like "a < b".
|
|
|
|
if (
|
|
|
|
prev &&
|
|
|
|
prev.type === NodeTypes.TEXT &&
|
|
|
|
prev.loc.end.offset === node.loc.start.offset
|
|
|
|
) {
|
|
|
|
prev.content += node.content
|
|
|
|
prev.loc.end = node.loc.end
|
|
|
|
prev.loc.source += node.loc.source
|
|
|
|
return
|
|
|
|
}
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
2019-10-25 00:47:29 +08:00
|
|
|
|
|
|
|
nodes.push(node)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseCDATA(
|
|
|
|
context: ParserContext,
|
|
|
|
ancestors: ElementNode[]
|
2019-10-01 21:27:34 +08:00
|
|
|
): TemplateChildNode[] {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ &&
|
2019-09-17 03:06:45 +08:00
|
|
|
assert(last(ancestors) == null || last(ancestors)!.ns !== Namespaces.HTML)
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(startsWith(context.source, '<![CDATA['))
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
advanceBy(context, 9)
|
|
|
|
const nodes = parseChildren(context, TextModes.CDATA, ancestors)
|
|
|
|
if (context.source.length === 0) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.EOF_IN_CDATA)
|
2019-09-17 02:43:29 +08:00
|
|
|
} else {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(startsWith(context.source, ']]>'))
|
2019-09-17 02:43:29 +08:00
|
|
|
advanceBy(context, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseComment(context: ParserContext): CommentNode {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(startsWith(context.source, '<!--'))
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
const start = getCursor(context)
|
|
|
|
let content: string
|
|
|
|
|
|
|
|
// Regular comment.
|
|
|
|
const match = /--(\!)?>/.exec(context.source)
|
|
|
|
if (!match) {
|
|
|
|
content = context.source.slice(4)
|
|
|
|
advanceBy(context, context.source.length)
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.EOF_IN_COMMENT)
|
2019-09-17 02:43:29 +08:00
|
|
|
} else {
|
|
|
|
if (match.index <= 3) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
if (match[1]) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.INCORRECTLY_CLOSED_COMMENT)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
content = context.source.slice(4, match.index)
|
|
|
|
|
|
|
|
// Advancing with reporting nested comments.
|
|
|
|
const s = context.source.slice(0, match.index)
|
|
|
|
let prevIndex = 1,
|
|
|
|
nestedIndex = 0
|
|
|
|
while ((nestedIndex = s.indexOf('<!--', prevIndex)) !== -1) {
|
|
|
|
advanceBy(context, nestedIndex - prevIndex + 1)
|
|
|
|
if (nestedIndex + 4 < s.length) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.NESTED_COMMENT)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
prevIndex = nestedIndex + 1
|
|
|
|
}
|
|
|
|
advanceBy(context, match.index + match[0].length - prevIndex + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: NodeTypes.COMMENT,
|
|
|
|
content,
|
|
|
|
loc: getSelection(context, start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseBogusComment(context: ParserContext): CommentNode | undefined {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(/^<(?:[\!\?]|\/[^a-z>])/i.test(context.source))
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
const start = getCursor(context)
|
|
|
|
const contentStart = context.source[1] === '?' ? 1 : 2
|
|
|
|
let content: string
|
|
|
|
|
|
|
|
const closeIndex = context.source.indexOf('>')
|
|
|
|
if (closeIndex === -1) {
|
|
|
|
content = context.source.slice(contentStart)
|
|
|
|
advanceBy(context, context.source.length)
|
|
|
|
} else {
|
|
|
|
content = context.source.slice(contentStart, closeIndex)
|
|
|
|
advanceBy(context, closeIndex + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: NodeTypes.COMMENT,
|
|
|
|
content,
|
|
|
|
loc: getSelection(context, start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseElement(
|
|
|
|
context: ParserContext,
|
|
|
|
ancestors: ElementNode[]
|
|
|
|
): ElementNode | undefined {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(/^<[a-z]/i.test(context.source))
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
// Start tag.
|
2019-10-10 04:00:08 +08:00
|
|
|
const wasInPre = context.inPre
|
2020-04-04 09:02:20 +08:00
|
|
|
const wasInVPre = context.inVPre
|
2019-09-17 02:43:29 +08:00
|
|
|
const parent = last(ancestors)
|
|
|
|
const element = parseTag(context, TagType.Start, parent)
|
2019-10-10 04:00:08 +08:00
|
|
|
const isPreBoundary = context.inPre && !wasInPre
|
2020-04-04 09:02:20 +08:00
|
|
|
const isVPreBoundary = context.inVPre && !wasInVPre
|
2019-09-17 02:43:29 +08:00
|
|
|
|
2019-09-21 00:16:19 +08:00
|
|
|
if (element.isSelfClosing || context.options.isVoidTag(element.tag)) {
|
2021-07-02 03:34:12 +08:00
|
|
|
// #4030 self-closing <pre> tag
|
2021-07-20 07:23:44 +08:00
|
|
|
if (isPreBoundary) {
|
|
|
|
context.inPre = false
|
2021-07-02 03:34:12 +08:00
|
|
|
}
|
2021-07-20 07:29:28 +08:00
|
|
|
if (isVPreBoundary) {
|
|
|
|
context.inVPre = false
|
|
|
|
}
|
2019-09-17 02:43:29 +08:00
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
|
|
|
// Children.
|
|
|
|
ancestors.push(element)
|
2020-05-07 23:01:36 +08:00
|
|
|
const mode = context.options.getTextMode(element, parent)
|
2019-09-17 02:43:29 +08:00
|
|
|
const children = parseChildren(context, mode, ancestors)
|
|
|
|
ancestors.pop()
|
|
|
|
|
2021-04-18 11:19:40 +08:00
|
|
|
// 2.x inline-template compat
|
|
|
|
if (__COMPAT__) {
|
|
|
|
const inlineTemplateProp = element.props.find(
|
|
|
|
p => p.type === NodeTypes.ATTRIBUTE && p.name === 'inline-template'
|
|
|
|
) as AttributeNode
|
|
|
|
if (
|
|
|
|
inlineTemplateProp &&
|
|
|
|
checkCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_INLINE_TEMPLATE,
|
|
|
|
context,
|
|
|
|
inlineTemplateProp.loc
|
|
|
|
)
|
|
|
|
) {
|
2021-05-01 03:50:32 +08:00
|
|
|
const loc = getSelection(context, element.loc.end)
|
|
|
|
inlineTemplateProp.value = {
|
|
|
|
type: NodeTypes.TEXT,
|
|
|
|
content: loc.source,
|
|
|
|
loc
|
|
|
|
}
|
2021-04-18 11:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
element.children = children
|
|
|
|
|
|
|
|
// End tag.
|
|
|
|
if (startsWithEndTagOpen(context.source, element.tag)) {
|
|
|
|
parseTag(context, TagType.End, parent)
|
|
|
|
} else {
|
2019-12-20 05:25:05 +08:00
|
|
|
emitError(context, ErrorCodes.X_MISSING_END_TAG, 0, element.loc.start)
|
2019-09-17 02:43:29 +08:00
|
|
|
if (context.source.length === 0 && element.tag.toLowerCase() === 'script') {
|
|
|
|
const first = children[0]
|
|
|
|
if (first && startsWith(first.loc.source, '<!--')) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.loc = getSelection(context, element.loc.start)
|
2019-10-10 04:00:08 +08:00
|
|
|
|
|
|
|
if (isPreBoundary) {
|
2021-07-20 07:23:44 +08:00
|
|
|
context.inPre = false
|
2019-10-10 04:00:08 +08:00
|
|
|
}
|
2020-04-04 09:02:20 +08:00
|
|
|
if (isVPreBoundary) {
|
2021-07-20 07:23:44 +08:00
|
|
|
context.inVPre = false
|
2020-04-04 09:02:20 +08:00
|
|
|
}
|
2019-09-17 02:43:29 +08:00
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
|
|
|
const enum TagType {
|
|
|
|
Start,
|
|
|
|
End
|
|
|
|
}
|
|
|
|
|
2020-02-01 06:20:52 +08:00
|
|
|
const isSpecialTemplateDirective = /*#__PURE__*/ makeMap(
|
|
|
|
`if,else,else-if,for,slot`
|
|
|
|
)
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
/**
|
|
|
|
* Parse a tag (E.g. `<div id=a>`) with that type (start tag or end tag).
|
|
|
|
*/
|
2021-04-18 10:05:18 +08:00
|
|
|
function parseTag(
|
|
|
|
context: ParserContext,
|
|
|
|
type: TagType.Start,
|
|
|
|
parent: ElementNode | undefined
|
|
|
|
): ElementNode
|
|
|
|
function parseTag(
|
|
|
|
context: ParserContext,
|
|
|
|
type: TagType.End,
|
|
|
|
parent: ElementNode | undefined
|
|
|
|
): void
|
2019-09-17 02:43:29 +08:00
|
|
|
function parseTag(
|
|
|
|
context: ParserContext,
|
|
|
|
type: TagType,
|
|
|
|
parent: ElementNode | undefined
|
2021-04-18 10:05:18 +08:00
|
|
|
): ElementNode | undefined {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(/^<\/?[a-z]/i.test(context.source))
|
|
|
|
__TEST__ &&
|
2019-09-17 03:06:45 +08:00
|
|
|
assert(
|
|
|
|
type === (startsWith(context.source, '</') ? TagType.End : TagType.Start)
|
|
|
|
)
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
// Tag open.
|
|
|
|
const start = getCursor(context)
|
|
|
|
const match = /^<\/?([a-z][^\t\r\n\f />]*)/i.exec(context.source)!
|
|
|
|
const tag = match[1]
|
2019-09-21 00:16:19 +08:00
|
|
|
const ns = context.options.getNamespace(tag, parent)
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
advanceBy(context, match[0].length)
|
|
|
|
advanceSpaces(context)
|
|
|
|
|
2019-10-10 04:00:08 +08:00
|
|
|
// save current state in case we need to re-parse attributes with v-pre
|
|
|
|
const cursor = getCursor(context)
|
|
|
|
const currentSource = context.source
|
2019-09-17 02:43:29 +08:00
|
|
|
|
2020-04-04 09:02:20 +08:00
|
|
|
// check <pre> tag
|
2021-07-20 06:54:35 +08:00
|
|
|
if (context.options.isPreTag(tag)) {
|
2021-07-20 07:23:44 +08:00
|
|
|
context.inPre = true
|
2020-04-04 09:02:20 +08:00
|
|
|
}
|
2021-07-02 03:34:12 +08:00
|
|
|
|
|
|
|
// Attributes.
|
|
|
|
let props = parseAttributes(context, type)
|
2020-04-04 09:02:20 +08:00
|
|
|
|
2019-10-10 04:00:08 +08:00
|
|
|
// check v-pre
|
|
|
|
if (
|
2021-04-18 10:05:18 +08:00
|
|
|
type === TagType.Start &&
|
2020-04-04 09:02:20 +08:00
|
|
|
!context.inVPre &&
|
2019-10-10 04:00:08 +08:00
|
|
|
props.some(p => p.type === NodeTypes.DIRECTIVE && p.name === 'pre')
|
|
|
|
) {
|
2021-07-20 07:23:44 +08:00
|
|
|
context.inVPre = true
|
2019-10-10 04:00:08 +08:00
|
|
|
// reset context
|
|
|
|
extend(context, cursor)
|
|
|
|
context.source = currentSource
|
|
|
|
// re-parse attrs and filter out v-pre itself
|
|
|
|
props = parseAttributes(context, type).filter(p => p.name !== 'v-pre')
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
2021-04-18 10:05:18 +08:00
|
|
|
// Tag close.
|
|
|
|
let isSelfClosing = false
|
|
|
|
if (context.source.length === 0) {
|
|
|
|
emitError(context, ErrorCodes.EOF_IN_TAG)
|
|
|
|
} else {
|
|
|
|
isSelfClosing = startsWith(context.source, '/>')
|
|
|
|
if (type === TagType.End && isSelfClosing) {
|
|
|
|
emitError(context, ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS)
|
|
|
|
}
|
|
|
|
advanceBy(context, isSelfClosing ? 2 : 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === TagType.End) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-18 11:19:40 +08:00
|
|
|
// 2.x deprecation checks
|
2021-05-01 03:50:32 +08:00
|
|
|
if (
|
|
|
|
__COMPAT__ &&
|
|
|
|
__DEV__ &&
|
|
|
|
isCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE,
|
|
|
|
context
|
|
|
|
)
|
|
|
|
) {
|
2021-04-18 02:38:30 +08:00
|
|
|
let hasIf = false
|
|
|
|
let hasFor = false
|
|
|
|
for (let i = 0; i < props.length; i++) {
|
|
|
|
const p = props[i]
|
|
|
|
if (p.type === NodeTypes.DIRECTIVE) {
|
|
|
|
if (p.name === 'if') {
|
|
|
|
hasIf = true
|
|
|
|
} else if (p.name === 'for') {
|
|
|
|
hasFor = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasIf && hasFor) {
|
|
|
|
warnDeprecation(
|
|
|
|
CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE,
|
|
|
|
context,
|
|
|
|
getSelection(context, start)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 04:00:08 +08:00
|
|
|
let tagType = ElementTypes.ELEMENT
|
2021-06-22 04:16:49 +08:00
|
|
|
if (!context.inVPre) {
|
|
|
|
if (tag === 'slot') {
|
|
|
|
tagType = ElementTypes.SLOT
|
|
|
|
} else if (tag === 'template') {
|
2021-04-16 23:43:05 +08:00
|
|
|
if (
|
2021-06-22 04:16:49 +08:00
|
|
|
props.some(
|
|
|
|
p =>
|
|
|
|
p.type === NodeTypes.DIRECTIVE && isSpecialTemplateDirective(p.name)
|
2021-04-16 23:43:05 +08:00
|
|
|
)
|
|
|
|
) {
|
2021-06-22 04:16:49 +08:00
|
|
|
tagType = ElementTypes.TEMPLATE
|
2021-04-16 23:43:05 +08:00
|
|
|
}
|
2021-06-22 04:16:49 +08:00
|
|
|
} else if (isComponent(tag, props, context)) {
|
2019-11-05 23:26:36 +08:00
|
|
|
tagType = ElementTypes.COMPONENT
|
2019-10-11 02:54:06 +08:00
|
|
|
}
|
2019-10-10 04:00:08 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
return {
|
|
|
|
type: NodeTypes.ELEMENT,
|
|
|
|
ns,
|
|
|
|
tag,
|
|
|
|
tagType,
|
2019-09-22 05:42:12 +08:00
|
|
|
props,
|
2019-09-17 02:43:29 +08:00
|
|
|
isSelfClosing,
|
|
|
|
children: [],
|
2019-09-22 05:42:12 +08:00
|
|
|
loc: getSelection(context, start),
|
|
|
|
codegenNode: undefined // to be created during transform phase
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:16:49 +08:00
|
|
|
function isComponent(
|
|
|
|
tag: string,
|
|
|
|
props: (AttributeNode | DirectiveNode)[],
|
|
|
|
context: ParserContext
|
|
|
|
) {
|
|
|
|
const options = context.options
|
|
|
|
if (options.isCustomElement(tag)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
tag === 'component' ||
|
|
|
|
/^[A-Z]/.test(tag) ||
|
|
|
|
isCoreComponent(tag) ||
|
|
|
|
(options.isBuiltInComponent && options.isBuiltInComponent(tag)) ||
|
|
|
|
(options.isNativeTag && !options.isNativeTag(tag))
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// at this point the tag should be a native tag, but check for potential "is"
|
|
|
|
// casting
|
|
|
|
for (let i = 0; i < props.length; i++) {
|
|
|
|
const p = props[i]
|
|
|
|
if (p.type === NodeTypes.ATTRIBUTE) {
|
|
|
|
if (p.name === 'is' && p.value) {
|
|
|
|
if (p.value.content.startsWith('vue:')) {
|
|
|
|
return true
|
|
|
|
} else if (
|
|
|
|
__COMPAT__ &&
|
|
|
|
checkCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_IS_ON_ELEMENT,
|
|
|
|
context,
|
|
|
|
p.loc
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// directive
|
|
|
|
// v-is (TODO Deprecate)
|
|
|
|
if (p.name === 'is') {
|
|
|
|
return true
|
|
|
|
} else if (
|
|
|
|
// :is on plain element - only treat as component in compat mode
|
|
|
|
p.name === 'bind' &&
|
|
|
|
isBindKey(p.arg, 'is') &&
|
|
|
|
__COMPAT__ &&
|
|
|
|
checkCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_IS_ON_ELEMENT,
|
|
|
|
context,
|
|
|
|
p.loc
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 04:00:08 +08:00
|
|
|
function parseAttributes(
|
|
|
|
context: ParserContext,
|
|
|
|
type: TagType
|
|
|
|
): (AttributeNode | DirectiveNode)[] {
|
|
|
|
const props = []
|
|
|
|
const attributeNames = new Set<string>()
|
|
|
|
while (
|
|
|
|
context.source.length > 0 &&
|
|
|
|
!startsWith(context.source, '>') &&
|
|
|
|
!startsWith(context.source, '/>')
|
|
|
|
) {
|
|
|
|
if (startsWith(context.source, '/')) {
|
|
|
|
emitError(context, ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG)
|
|
|
|
advanceBy(context, 1)
|
|
|
|
advanceSpaces(context)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (type === TagType.End) {
|
|
|
|
emitError(context, ErrorCodes.END_TAG_WITH_ATTRIBUTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
const attr = parseAttribute(context, attributeNames)
|
2021-09-08 00:01:17 +08:00
|
|
|
|
|
|
|
// Trim whitespace between class
|
|
|
|
// https://github.com/vuejs/vue-next/issues/4251
|
|
|
|
if (
|
|
|
|
attr.type === NodeTypes.ATTRIBUTE &&
|
|
|
|
attr.value &&
|
|
|
|
attr.name === 'class'
|
|
|
|
) {
|
|
|
|
attr.value.content = attr.value.content.replace(/\s+/g, ' ').trim()
|
|
|
|
}
|
|
|
|
|
2019-10-10 04:00:08 +08:00
|
|
|
if (type === TagType.Start) {
|
|
|
|
props.push(attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^[^\t\r\n\f />]/.test(context.source)) {
|
|
|
|
emitError(context, ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES)
|
|
|
|
}
|
|
|
|
advanceSpaces(context)
|
|
|
|
}
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
function parseAttribute(
|
|
|
|
context: ParserContext,
|
|
|
|
nameSet: Set<string>
|
|
|
|
): AttributeNode | DirectiveNode {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(/^[^\t\r\n\f />]/.test(context.source))
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
// Name.
|
|
|
|
const start = getCursor(context)
|
|
|
|
const match = /^[^\t\r\n\f />][^\t\r\n\f />=]*/.exec(context.source)!
|
|
|
|
const name = match[0]
|
|
|
|
|
|
|
|
if (nameSet.has(name)) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.DUPLICATE_ATTRIBUTE)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
nameSet.add(name)
|
|
|
|
|
|
|
|
if (name[0] === '=') {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const pattern = /["'<]/g
|
|
|
|
let m: RegExpExecArray | null
|
2020-03-19 06:14:51 +08:00
|
|
|
while ((m = pattern.exec(name))) {
|
2019-09-17 02:43:29 +08:00
|
|
|
emitError(
|
|
|
|
context,
|
2019-09-18 07:08:47 +08:00
|
|
|
ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
|
2019-09-17 02:43:29 +08:00
|
|
|
m.index
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
advanceBy(context, name.length)
|
|
|
|
|
|
|
|
// Value
|
2020-12-05 05:28:26 +08:00
|
|
|
let value: AttributeValue = undefined
|
2019-09-26 07:17:45 +08:00
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
if (/^[\t\r\n\f ]*=/.test(context.source)) {
|
|
|
|
advanceSpaces(context)
|
|
|
|
advanceBy(context, 1)
|
|
|
|
advanceSpaces(context)
|
|
|
|
value = parseAttributeValue(context)
|
|
|
|
if (!value) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.MISSING_ATTRIBUTE_VALUE)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const loc = getSelection(context, start)
|
|
|
|
|
2021-09-02 21:42:20 +08:00
|
|
|
if (!context.inVPre && /^(v-[A-Za-z0-9-]|:|\.|@|#)/.test(name)) {
|
2021-07-20 06:24:18 +08:00
|
|
|
const match =
|
|
|
|
/(?:^v-([a-z0-9-]+))?(?:(?::|^\.|^@|^#)(\[[^\]]+\]|[^\.]+))?(.+)?$/i.exec(
|
|
|
|
name
|
|
|
|
)!
|
2019-09-17 02:43:29 +08:00
|
|
|
|
2021-07-14 03:58:18 +08:00
|
|
|
let isPropShorthand = startsWith(name, '.')
|
2021-04-17 05:11:44 +08:00
|
|
|
let dirName =
|
2020-06-13 04:09:27 +08:00
|
|
|
match[1] ||
|
2021-07-14 03:58:18 +08:00
|
|
|
(isPropShorthand || startsWith(name, ':')
|
|
|
|
? 'bind'
|
|
|
|
: startsWith(name, '@')
|
2021-07-20 06:24:18 +08:00
|
|
|
? 'on'
|
|
|
|
: 'slot')
|
2019-09-17 02:43:29 +08:00
|
|
|
let arg: ExpressionNode | undefined
|
|
|
|
|
|
|
|
if (match[2]) {
|
2020-06-13 04:09:27 +08:00
|
|
|
const isSlot = dirName === 'slot'
|
2021-03-27 23:20:39 +08:00
|
|
|
const startOffset = name.lastIndexOf(match[2])
|
2019-09-17 02:43:29 +08:00
|
|
|
const loc = getSelection(
|
|
|
|
context,
|
|
|
|
getNewPosition(context, start, startOffset),
|
2020-06-13 04:09:27 +08:00
|
|
|
getNewPosition(
|
|
|
|
context,
|
|
|
|
start,
|
|
|
|
startOffset + match[2].length + ((isSlot && match[3]) || '').length
|
|
|
|
)
|
2019-09-17 02:43:29 +08:00
|
|
|
)
|
|
|
|
let content = match[2]
|
|
|
|
let isStatic = true
|
|
|
|
|
|
|
|
if (content.startsWith('[')) {
|
|
|
|
isStatic = false
|
|
|
|
|
|
|
|
if (!content.endsWith(']')) {
|
|
|
|
emitError(
|
|
|
|
context,
|
2019-09-18 07:08:47 +08:00
|
|
|
ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END
|
2019-09-17 02:43:29 +08:00
|
|
|
)
|
2021-09-07 23:36:37 +08:00
|
|
|
content = content.substr(1)
|
|
|
|
} else {
|
|
|
|
content = content.substr(1, content.length - 2)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
2020-06-13 04:09:27 +08:00
|
|
|
} else if (isSlot) {
|
|
|
|
// #1241 special case for v-slot: vuetify relies extensively on slot
|
|
|
|
// names containing dots. v-slot doesn't have any modifiers and Vue 2.x
|
|
|
|
// supports such usage so we are keeping it consistent with 2.x.
|
|
|
|
content += match[3] || ''
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
arg = {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2019-09-17 02:43:29 +08:00
|
|
|
content,
|
|
|
|
isStatic,
|
2020-11-21 08:26:07 +08:00
|
|
|
constType: isStatic
|
|
|
|
? ConstantTypes.CAN_STRINGIFY
|
|
|
|
: ConstantTypes.NOT_CONSTANT,
|
2019-09-17 02:43:29 +08:00
|
|
|
loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:17:45 +08:00
|
|
|
if (value && value.isQuoted) {
|
|
|
|
const valueLoc = value.loc
|
|
|
|
valueLoc.start.offset++
|
|
|
|
valueLoc.start.column++
|
|
|
|
valueLoc.end = advancePositionWithClone(valueLoc.start, value.content)
|
2019-09-27 02:55:53 +08:00
|
|
|
valueLoc.source = valueLoc.source.slice(1, -1)
|
2019-09-26 07:17:45 +08:00
|
|
|
}
|
|
|
|
|
2021-04-17 05:11:44 +08:00
|
|
|
const modifiers = match[3] ? match[3].substr(1).split('.') : []
|
2021-07-14 03:58:18 +08:00
|
|
|
if (isPropShorthand) modifiers.push('prop')
|
2021-04-17 05:11:44 +08:00
|
|
|
|
|
|
|
// 2.x compat v-bind:foo.sync -> v-model:foo
|
2021-04-18 10:50:16 +08:00
|
|
|
if (__COMPAT__ && dirName === 'bind' && arg) {
|
|
|
|
if (
|
|
|
|
modifiers.includes('sync') &&
|
|
|
|
checkCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_V_BIND_SYNC,
|
|
|
|
context,
|
|
|
|
loc,
|
|
|
|
arg.loc.source
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
dirName = 'model'
|
|
|
|
modifiers.splice(modifiers.indexOf('sync'), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (__DEV__ && modifiers.includes('prop')) {
|
|
|
|
checkCompatEnabled(
|
|
|
|
CompilerDeprecationTypes.COMPILER_V_BIND_PROP,
|
|
|
|
context,
|
|
|
|
loc
|
|
|
|
)
|
|
|
|
}
|
2021-04-17 05:11:44 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
return {
|
|
|
|
type: NodeTypes.DIRECTIVE,
|
2020-06-13 04:09:27 +08:00
|
|
|
name: dirName,
|
2019-09-17 02:43:29 +08:00
|
|
|
exp: value && {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
2019-09-17 02:43:29 +08:00
|
|
|
content: value.content,
|
|
|
|
isStatic: false,
|
2019-10-15 23:51:52 +08:00
|
|
|
// Treat as non-constant by default. This can be potentially set to
|
2020-11-21 08:26:07 +08:00
|
|
|
// other values by `transformExpression` to make it eligible for hoisting.
|
|
|
|
constType: ConstantTypes.NOT_CONSTANT,
|
2019-09-17 02:43:29 +08:00
|
|
|
loc: value.loc
|
|
|
|
},
|
|
|
|
arg,
|
2021-04-17 05:11:44 +08:00
|
|
|
modifiers,
|
2019-09-17 02:43:29 +08:00
|
|
|
loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:42:20 +08:00
|
|
|
// missing directive name or illegal directive name
|
|
|
|
if (!context.inVPre && startsWith(name, 'v-')) {
|
|
|
|
emitError(context, ErrorCodes.X_MISSING_DIRECTIVE_NAME)
|
|
|
|
}
|
|
|
|
|
2019-09-17 02:43:29 +08:00
|
|
|
return {
|
|
|
|
type: NodeTypes.ATTRIBUTE,
|
|
|
|
name,
|
|
|
|
value: value && {
|
|
|
|
type: NodeTypes.TEXT,
|
|
|
|
content: value.content,
|
|
|
|
loc: value.loc
|
|
|
|
},
|
|
|
|
loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:28:26 +08:00
|
|
|
function parseAttributeValue(context: ParserContext): AttributeValue {
|
2019-09-17 02:43:29 +08:00
|
|
|
const start = getCursor(context)
|
|
|
|
let content: string
|
|
|
|
|
2019-09-26 07:17:45 +08:00
|
|
|
const quote = context.source[0]
|
|
|
|
const isQuoted = quote === `"` || quote === `'`
|
|
|
|
if (isQuoted) {
|
2019-09-17 02:43:29 +08:00
|
|
|
// Quoted value.
|
|
|
|
advanceBy(context, 1)
|
|
|
|
|
|
|
|
const endIndex = context.source.indexOf(quote)
|
|
|
|
if (endIndex === -1) {
|
|
|
|
content = parseTextData(
|
|
|
|
context,
|
|
|
|
context.source.length,
|
|
|
|
TextModes.ATTRIBUTE_VALUE
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
content = parseTextData(context, endIndex, TextModes.ATTRIBUTE_VALUE)
|
|
|
|
advanceBy(context, 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Unquoted
|
|
|
|
const match = /^[^\t\r\n\f >]+/.exec(context.source)
|
|
|
|
if (!match) {
|
|
|
|
return undefined
|
|
|
|
}
|
2020-02-21 18:21:24 +08:00
|
|
|
const unexpectedChars = /["'<=`]/g
|
2019-09-17 02:43:29 +08:00
|
|
|
let m: RegExpExecArray | null
|
2020-03-19 06:14:51 +08:00
|
|
|
while ((m = unexpectedChars.exec(match[0]))) {
|
2019-09-17 02:43:29 +08:00
|
|
|
emitError(
|
|
|
|
context,
|
2019-09-18 07:08:47 +08:00
|
|
|
ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
|
2019-09-17 02:43:29 +08:00
|
|
|
m.index
|
|
|
|
)
|
|
|
|
}
|
|
|
|
content = parseTextData(context, match[0].length, TextModes.ATTRIBUTE_VALUE)
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:17:45 +08:00
|
|
|
return { content, isQuoted, loc: getSelection(context, start) }
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseInterpolation(
|
|
|
|
context: ParserContext,
|
|
|
|
mode: TextModes
|
2019-09-27 23:42:02 +08:00
|
|
|
): InterpolationNode | undefined {
|
2019-09-21 00:16:19 +08:00
|
|
|
const [open, close] = context.options.delimiters
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(startsWith(context.source, open))
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
const closeIndex = context.source.indexOf(close, open.length)
|
|
|
|
if (closeIndex === -1) {
|
2019-09-18 07:08:47 +08:00
|
|
|
emitError(context, ErrorCodes.X_MISSING_INTERPOLATION_END)
|
2019-09-17 02:43:29 +08:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2019-09-27 02:55:53 +08:00
|
|
|
const start = getCursor(context)
|
2019-09-27 23:42:02 +08:00
|
|
|
advanceBy(context, open.length)
|
|
|
|
const innerStart = getCursor(context)
|
|
|
|
const innerEnd = getCursor(context)
|
2019-09-27 02:55:53 +08:00
|
|
|
const rawContentLength = closeIndex - open.length
|
|
|
|
const rawContent = context.source.slice(0, rawContentLength)
|
|
|
|
const preTrimContent = parseTextData(context, rawContentLength, mode)
|
|
|
|
const content = preTrimContent.trim()
|
|
|
|
const startOffset = preTrimContent.indexOf(content)
|
|
|
|
if (startOffset > 0) {
|
2019-09-27 23:42:02 +08:00
|
|
|
advancePositionWithMutation(innerStart, rawContent, startOffset)
|
2019-09-27 02:55:53 +08:00
|
|
|
}
|
|
|
|
const endOffset =
|
|
|
|
rawContentLength - (preTrimContent.length - content.length - startOffset)
|
2019-09-27 23:42:02 +08:00
|
|
|
advancePositionWithMutation(innerEnd, rawContent, endOffset)
|
2019-09-17 02:43:29 +08:00
|
|
|
advanceBy(context, close.length)
|
|
|
|
|
|
|
|
return {
|
2019-09-27 23:42:02 +08:00
|
|
|
type: NodeTypes.INTERPOLATION,
|
|
|
|
content: {
|
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
|
|
|
isStatic: false,
|
2019-10-15 23:41:24 +08:00
|
|
|
// Set `isConstant` to false by default and will decide in transformExpression
|
2020-11-21 08:26:07 +08:00
|
|
|
constType: ConstantTypes.NOT_CONSTANT,
|
2019-09-27 23:42:02 +08:00
|
|
|
content,
|
|
|
|
loc: getSelection(context, innerStart, innerEnd)
|
|
|
|
},
|
|
|
|
loc: getSelection(context, start)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseText(context: ParserContext, mode: TextModes): TextNode {
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(context.source.length > 0)
|
2019-09-17 02:43:29 +08:00
|
|
|
|
2021-09-02 04:43:10 +08:00
|
|
|
const endTokens =
|
|
|
|
mode === TextModes.CDATA ? [']]>'] : ['<', context.options.delimiters[0]]
|
2019-11-16 06:48:01 +08:00
|
|
|
|
|
|
|
let endIndex = context.source.length
|
|
|
|
for (let i = 0; i < endTokens.length; i++) {
|
|
|
|
const index = context.source.indexOf(endTokens[i], 1)
|
|
|
|
if (index !== -1 && endIndex > index) {
|
|
|
|
endIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(endIndex > 0)
|
2019-09-17 02:43:29 +08:00
|
|
|
|
|
|
|
const start = getCursor(context)
|
|
|
|
const content = parseTextData(context, endIndex, mode)
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: NodeTypes.TEXT,
|
|
|
|
content,
|
2019-10-25 04:22:48 +08:00
|
|
|
loc: getSelection(context, start)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get text data with a given length from the current location.
|
|
|
|
* This translates HTML entities in the text data.
|
|
|
|
*/
|
|
|
|
function parseTextData(
|
|
|
|
context: ParserContext,
|
|
|
|
length: number,
|
|
|
|
mode: TextModes
|
|
|
|
): string {
|
2020-04-09 06:51:25 +08:00
|
|
|
const rawText = context.source.slice(0, length)
|
|
|
|
advanceBy(context, length)
|
2019-11-16 05:17:52 +08:00
|
|
|
if (
|
|
|
|
mode === TextModes.RAWTEXT ||
|
|
|
|
mode === TextModes.CDATA ||
|
|
|
|
rawText.indexOf('&') === -1
|
|
|
|
) {
|
|
|
|
return rawText
|
2020-04-09 06:51:25 +08:00
|
|
|
} else {
|
|
|
|
// DATA or RCDATA containing "&"". Entity decoding required.
|
|
|
|
return context.options.decodeEntities(
|
|
|
|
rawText,
|
|
|
|
mode === TextModes.ATTRIBUTE_VALUE
|
|
|
|
)
|
2019-09-17 02:43:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 03:27:40 +08:00
|
|
|
function getCursor(context: ParserContext): Position {
|
|
|
|
const { column, line, offset } = context
|
|
|
|
return { column, line, offset }
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelection(
|
|
|
|
context: ParserContext,
|
|
|
|
start: Position,
|
|
|
|
end?: Position
|
|
|
|
): SourceLocation {
|
|
|
|
end = end || getCursor(context)
|
|
|
|
return {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
source: context.originalSource.slice(start.offset, end.offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function last<T>(xs: T[]): T | undefined {
|
|
|
|
return xs[xs.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
function startsWith(source: string, searchString: string): boolean {
|
|
|
|
return source.startsWith(searchString)
|
|
|
|
}
|
|
|
|
|
|
|
|
function advanceBy(context: ParserContext, numberOfCharacters: number): void {
|
2019-09-20 01:23:49 +08:00
|
|
|
const { source } = context
|
2019-11-16 06:29:08 +08:00
|
|
|
__TEST__ && assert(numberOfCharacters <= source.length)
|
2019-09-20 01:59:24 +08:00
|
|
|
advancePositionWithMutation(context, source, numberOfCharacters)
|
2019-09-17 03:27:40 +08:00
|
|
|
context.source = source.slice(numberOfCharacters)
|
|
|
|
}
|
|
|
|
|
|
|
|
function advanceSpaces(context: ParserContext): void {
|
|
|
|
const match = /^[\t\r\n\f ]+/.exec(context.source)
|
|
|
|
if (match) {
|
|
|
|
advanceBy(context, match[0].length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewPosition(
|
|
|
|
context: ParserContext,
|
|
|
|
start: Position,
|
|
|
|
numberOfCharacters: number
|
|
|
|
): Position {
|
2019-09-20 11:05:51 +08:00
|
|
|
return advancePositionWithClone(
|
|
|
|
start,
|
|
|
|
context.originalSource.slice(start.offset, numberOfCharacters),
|
|
|
|
numberOfCharacters
|
|
|
|
)
|
2019-09-17 03:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function emitError(
|
|
|
|
context: ParserContext,
|
2019-09-18 07:08:47 +08:00
|
|
|
code: ErrorCodes,
|
2019-12-20 05:25:05 +08:00
|
|
|
offset?: number,
|
|
|
|
loc: Position = getCursor(context)
|
2019-09-17 03:27:40 +08:00
|
|
|
): void {
|
|
|
|
if (offset) {
|
|
|
|
loc.offset += offset
|
|
|
|
loc.column += offset
|
|
|
|
}
|
2019-09-23 10:19:42 +08:00
|
|
|
context.options.onError(
|
|
|
|
createCompilerError(code, {
|
|
|
|
start: loc,
|
|
|
|
end: loc,
|
|
|
|
source: ''
|
|
|
|
})
|
|
|
|
)
|
2019-09-17 03:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function isEnd(
|
|
|
|
context: ParserContext,
|
|
|
|
mode: TextModes,
|
|
|
|
ancestors: ElementNode[]
|
|
|
|
): boolean {
|
|
|
|
const s = context.source
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case TextModes.DATA:
|
|
|
|
if (startsWith(s, '</')) {
|
2021-02-08 19:32:12 +08:00
|
|
|
// TODO: probably bad performance
|
2019-09-17 03:27:40 +08:00
|
|
|
for (let i = ancestors.length - 1; i >= 0; --i) {
|
|
|
|
if (startsWithEndTagOpen(s, ancestors[i].tag)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case TextModes.RCDATA:
|
|
|
|
case TextModes.RAWTEXT: {
|
|
|
|
const parent = last(ancestors)
|
|
|
|
if (parent && startsWithEndTagOpen(s, parent.tag)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
case TextModes.CDATA:
|
|
|
|
if (startsWith(s, ']]>')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return !s
|
|
|
|
}
|
|
|
|
|
|
|
|
function startsWithEndTagOpen(source: string, tag: string): boolean {
|
|
|
|
return (
|
|
|
|
startsWith(source, '</') &&
|
|
|
|
source.substr(2, tag.length).toLowerCase() === tag.toLowerCase() &&
|
2020-07-07 04:00:26 +08:00
|
|
|
/[\t\r\n\f />]/.test(source[2 + tag.length] || '>')
|
2019-09-17 03:27:40 +08:00
|
|
|
)
|
|
|
|
}
|