| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- /* @flow */
- import he from 'he'
- import { parseHTML } from './html-parser'
- import { parseText } from './text-parser'
- import { parseFilters } from './filter-parser'
- import { cached, no, camelize } from 'shared/util'
- import { genAssignmentCode } from '../directives/model'
- import { isIE, isEdge, isServerRendering } from 'core/util/env'
- import {
- addProp,
- addAttr,
- baseWarn,
- addHandler,
- addDirective,
- getBindingAttr,
- getAndRemoveAttr,
- pluckModuleFunction
- } from '../helpers'
- export const onRE = /^@|^v-on:/
- export const dirRE = /^v-|^@|^:/
- export const forAliasRE = /(.*?)\s+(?:in|of)\s+(.*)/
- export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
- const stripParensRE = /^\(|\)$/g
- const argRE = /:(.*)$/
- const bindRE = /^:|^v-bind:/
- const modifierRE = /\.[^.]+/g
- const literalValueRE = /^(\{.*\}|\[.*\])$/
- const decodeHTMLCached = cached(he.decode)
- // configurable state
- export let warn: any
- let literalPropId
- let delimiters
- let transforms
- let preTransforms
- let postTransforms
- let platformIsPreTag
- let platformMustUseProp
- let platformIsReservedTag
- let platformGetTagNamespace
- type Attr = { name: string; value: string };
- export function createASTElement (
- tag: string,
- attrs: Array<Attr>,
- parent: ASTElement | void
- ): ASTElement {
- return {
- type: 1,
- tag,
- attrsList: attrs,
- attrsMap: makeAttrsMap(attrs),
- parent,
- children: []
- }
- }
- /**
- * Convert HTML string to AST.
- */
- export function parse (
- template: string,
- options: CompilerOptions
- ): ASTElement | void {
- warn = options.warn || baseWarn
- literalPropId = 0
- platformIsPreTag = options.isPreTag || no
- platformMustUseProp = options.mustUseProp || no
- platformIsReservedTag = options.isReservedTag || no
- platformGetTagNamespace = options.getTagNamespace || no
- transforms = pluckModuleFunction(options.modules, 'transformNode')
- preTransforms = pluckModuleFunction(options.modules, 'preTransformNode')
- postTransforms = pluckModuleFunction(options.modules, 'postTransformNode')
- delimiters = options.delimiters
- const stack = []
- const preserveWhitespace = options.preserveWhitespace !== false
- let root
- let currentParent
- let inVPre = false
- let inPre = false
- let warned = false
- function warnOnce (msg) {
- if (!warned) {
- warned = true
- warn(msg)
- }
- }
- function closeElement (element) {
- // check pre state
- if (element.pre) {
- inVPre = false
- }
- if (platformIsPreTag(element.tag)) {
- inPre = false
- }
- // apply post-transforms
- for (let i = 0; i < postTransforms.length; i++) {
- postTransforms[i](element, options)
- }
- }
- parseHTML(template, {
- warn,
- expectHTML: options.expectHTML,
- isUnaryTag: options.isUnaryTag,
- canBeLeftOpenTag: options.canBeLeftOpenTag,
- shouldDecodeNewlines: options.shouldDecodeNewlines,
- shouldDecodeNewlinesForHref: options.shouldDecodeNewlinesForHref,
- shouldKeepComment: options.comments,
- start (tag, attrs, unary) {
- // check namespace.
- // inherit parent ns if there is one
- const ns = (currentParent && currentParent.ns) || platformGetTagNamespace(tag)
- // handle IE svg bug
- /* istanbul ignore if */
- if (isIE && ns === 'svg') {
- attrs = guardIESVGBug(attrs)
- }
- let element: ASTElement = createASTElement(tag, attrs, currentParent)
- if (ns) {
- element.ns = ns
- }
- if (isForbiddenTag(element) && !isServerRendering()) {
- element.forbidden = true
- process.env.NODE_ENV !== 'production' && warn(
- 'Templates should only be responsible for mapping the state to the ' +
- 'UI. Avoid placing tags with side-effects in your templates, such as ' +
- `<${tag}>` + ', as they will not be parsed.'
- )
- }
- // apply pre-transforms
- for (let i = 0; i < preTransforms.length; i++) {
- element = preTransforms[i](element, options) || element
- }
- if (!inVPre) {
- processPre(element)
- if (element.pre) {
- inVPre = true
- }
- }
- if (platformIsPreTag(element.tag)) {
- inPre = true
- }
- if (inVPre) {
- processRawAttrs(element)
- } else if (!element.processed) {
- // structural directives
- processFor(element)
- processIf(element)
- processOnce(element)
- // element-scope stuff
- processElement(element, options)
- }
- function checkRootConstraints (el) {
- if (process.env.NODE_ENV !== 'production') {
- if (el.tag === 'slot' || el.tag === 'template') {
- warnOnce(
- `Cannot use <${el.tag}> as component root element because it may ` +
- 'contain multiple nodes.'
- )
- }
- if (el.attrsMap.hasOwnProperty('v-for')) {
- warnOnce(
- 'Cannot use v-for on stateful component root element because ' +
- 'it renders multiple elements.'
- )
- }
- }
- }
- // tree management
- if (!root) {
- root = element
- checkRootConstraints(root)
- } else if (!stack.length) {
- // allow root elements with v-if, v-else-if and v-else
- if (root.if && (element.elseif || element.else)) {
- checkRootConstraints(element)
- addIfCondition(root, {
- exp: element.elseif,
- block: element
- })
- } else if (process.env.NODE_ENV !== 'production') {
- warnOnce(
- `Component template should contain exactly one root element. ` +
- `If you are using v-if on multiple elements, ` +
- `use v-else-if to chain them instead.`
- )
- }
- }
- if (currentParent && !element.forbidden) {
- if (element.elseif || element.else) {
- processIfConditions(element, currentParent)
- } else if (element.slotScope) { // scoped slot
- currentParent.plain = false
- const name = element.slotTarget || '"default"'
- ;(currentParent.scopedSlots || (currentParent.scopedSlots = {}))[name] = element
- } else {
- currentParent.children.push(element)
- element.parent = currentParent
- }
- }
- if (!unary) {
- currentParent = element
- stack.push(element)
- } else {
- closeElement(element)
- }
- },
- end () {
- // remove trailing whitespace
- const element = stack[stack.length - 1]
- const lastNode = element.children[element.children.length - 1]
- if (lastNode && lastNode.type === 3 && lastNode.text === ' ' && !inPre) {
- element.children.pop()
- }
- // pop stack
- stack.length -= 1
- currentParent = stack[stack.length - 1]
- closeElement(element)
- },
- chars (text: string) {
- if (!currentParent) {
- if (process.env.NODE_ENV !== 'production') {
- if (text === template) {
- warnOnce(
- 'Component template requires a root element, rather than just text.'
- )
- } else if ((text = text.trim())) {
- warnOnce(
- `text "${text}" outside root element will be ignored.`
- )
- }
- }
- return
- }
- // IE textarea placeholder bug
- /* istanbul ignore if */
- if (isIE &&
- currentParent.tag === 'textarea' &&
- currentParent.attrsMap.placeholder === text
- ) {
- return
- }
- const children = currentParent.children
- text = inPre || text.trim()
- ? isTextTag(currentParent) ? text : decodeHTMLCached(text)
- // only preserve whitespace if its not right after a starting tag
- : preserveWhitespace && children.length ? ' ' : ''
- if (text) {
- let res
- if (!inVPre && text !== ' ' && (res = parseText(text, delimiters))) {
- children.push({
- type: 2,
- expression: res.expression,
- tokens: res.tokens,
- text
- })
- } else if (text !== ' ' || !children.length || children[children.length - 1].text !== ' ') {
- children.push({
- type: 3,
- text
- })
- }
- }
- },
- comment (text: string) {
- currentParent.children.push({
- type: 3,
- text,
- isComment: true
- })
- }
- })
- return root
- }
- function processPre (el) {
- if (getAndRemoveAttr(el, 'v-pre') != null) {
- el.pre = true
- }
- }
- function processRawAttrs (el) {
- const l = el.attrsList.length
- if (l) {
- const attrs = el.attrs = new Array(l)
- for (let i = 0; i < l; i++) {
- attrs[i] = {
- name: el.attrsList[i].name,
- value: JSON.stringify(el.attrsList[i].value)
- }
- }
- } else if (!el.pre) {
- // non root node in pre blocks with no attributes
- el.plain = true
- }
- }
- export function processElement (element: ASTElement, options: CompilerOptions) {
- processKey(element)
- // determine whether this is a plain element after
- // removing structural attributes
- element.plain = !element.key && !element.attrsList.length
- processRef(element)
- processSlot(element)
- processComponent(element)
- for (let i = 0; i < transforms.length; i++) {
- element = transforms[i](element, options) || element
- }
- processAttrs(element)
- }
- function processKey (el) {
- const exp = getBindingAttr(el, 'key')
- if (exp) {
- if (process.env.NODE_ENV !== 'production' && el.tag === 'template') {
- warn(`<template> cannot be keyed. Place the key on real elements instead.`)
- }
- el.key = exp
- }
- }
- function processRef (el) {
- const ref = getBindingAttr(el, 'ref')
- if (ref) {
- el.ref = ref
- el.refInFor = checkInFor(el)
- }
- }
- export function processFor (el: ASTElement) {
- let exp
- if ((exp = getAndRemoveAttr(el, 'v-for'))) {
- const inMatch = exp.match(forAliasRE)
- if (!inMatch) {
- process.env.NODE_ENV !== 'production' && warn(
- `Invalid v-for expression: ${exp}`
- )
- return
- }
- el.for = inMatch[2].trim()
- const alias = inMatch[1].trim().replace(stripParensRE, '')
- const iteratorMatch = alias.match(forIteratorRE)
- if (iteratorMatch) {
- el.alias = alias.replace(forIteratorRE, '')
- el.iterator1 = iteratorMatch[1].trim()
- if (iteratorMatch[2]) {
- el.iterator2 = iteratorMatch[2].trim()
- }
- } else {
- el.alias = alias
- }
- }
- }
- function processIf (el) {
- const exp = getAndRemoveAttr(el, 'v-if')
- if (exp) {
- el.if = exp
- addIfCondition(el, {
- exp: exp,
- block: el
- })
- } else {
- if (getAndRemoveAttr(el, 'v-else') != null) {
- el.else = true
- }
- const elseif = getAndRemoveAttr(el, 'v-else-if')
- if (elseif) {
- el.elseif = elseif
- }
- }
- }
- function processIfConditions (el, parent) {
- const prev = findPrevElement(parent.children)
- if (prev && prev.if) {
- addIfCondition(prev, {
- exp: el.elseif,
- block: el
- })
- } else if (process.env.NODE_ENV !== 'production') {
- warn(
- `v-${el.elseif ? ('else-if="' + el.elseif + '"') : 'else'} ` +
- `used on element <${el.tag}> without corresponding v-if.`
- )
- }
- }
- function findPrevElement (children: Array<any>): ASTElement | void {
- let i = children.length
- while (i--) {
- if (children[i].type === 1) {
- return children[i]
- } else {
- if (process.env.NODE_ENV !== 'production' && children[i].text !== ' ') {
- warn(
- `text "${children[i].text.trim()}" between v-if and v-else(-if) ` +
- `will be ignored.`
- )
- }
- children.pop()
- }
- }
- }
- export function addIfCondition (el: ASTElement, condition: ASTIfCondition) {
- if (!el.ifConditions) {
- el.ifConditions = []
- }
- el.ifConditions.push(condition)
- }
- function processOnce (el) {
- const once = getAndRemoveAttr(el, 'v-once')
- if (once != null) {
- el.once = true
- }
- }
- function processSlot (el) {
- if (el.tag === 'slot') {
- el.slotName = getBindingAttr(el, 'name')
- if (process.env.NODE_ENV !== 'production' && el.key) {
- warn(
- `\`key\` does not work on <slot> because slots are abstract outlets ` +
- `and can possibly expand into multiple elements. ` +
- `Use the key on a wrapping element instead.`
- )
- }
- } else {
- let slotScope
- if (el.tag === 'template') {
- slotScope = getAndRemoveAttr(el, 'scope')
- /* istanbul ignore if */
- if (process.env.NODE_ENV !== 'production' && slotScope) {
- warn(
- `the "scope" attribute for scoped slots have been deprecated and ` +
- `replaced by "slot-scope" since 2.5. The new "slot-scope" attribute ` +
- `can also be used on plain elements in addition to <template> to ` +
- `denote scoped slots.`,
- true
- )
- }
- el.slotScope = slotScope || getAndRemoveAttr(el, 'slot-scope')
- } else if ((slotScope = getAndRemoveAttr(el, 'slot-scope'))) {
- /* istanbul ignore if */
- if (process.env.NODE_ENV !== 'production' && el.attrsMap['v-for']) {
- warn(
- `Ambiguous combined usage of slot-scope and v-for on <${el.tag}> ` +
- `(v-for takes higher priority). Use a wrapper <template> for the ` +
- `scoped slot to make it clearer.`,
- true
- )
- }
- el.slotScope = slotScope
- }
- const slotTarget = getBindingAttr(el, 'slot')
- if (slotTarget) {
- el.slotTarget = slotTarget === '""' ? '"default"' : slotTarget
- // preserve slot as an attribute for native shadow DOM compat
- // only for non-scoped slots.
- if (el.tag !== 'template' && !el.slotScope) {
- addAttr(el, 'slot', slotTarget)
- }
- }
- }
- }
- function processComponent (el) {
- let binding
- if ((binding = getBindingAttr(el, 'is'))) {
- el.component = binding
- }
- if (getAndRemoveAttr(el, 'inline-template') != null) {
- el.inlineTemplate = true
- }
- }
- function processAttrs (el) {
- const list = el.attrsList
- let i, l, name, rawName, value, modifiers, isProp
- for (i = 0, l = list.length; i < l; i++) {
- name = rawName = list[i].name
- value = list[i].value
- if (dirRE.test(name)) {
- // mark element as dynamic
- el.hasBindings = true
- // modifiers
- modifiers = parseModifiers(name)
- if (modifiers) {
- name = name.replace(modifierRE, '')
- }
- if (bindRE.test(name)) { // v-bind
- name = name.replace(bindRE, '')
- value = parseFilters(value)
- isProp = false
- if (modifiers) {
- if (modifiers.prop) {
- isProp = true
- name = camelize(name)
- if (name === 'innerHtml') name = 'innerHTML'
- }
- if (modifiers.camel) {
- name = camelize(name)
- }
- if (modifiers.sync) {
- addHandler(
- el,
- `update:${camelize(name)}`,
- genAssignmentCode(value, `$event`)
- )
- }
- }
- // optimize literal values in component props by wrapping them
- // in an inline watcher to avoid unnecessary re-renders
- if (
- !platformIsReservedTag(el.tag) &&
- el.tag !== 'slot' &&
- literalValueRE.test(value.trim())
- ) {
- value = `_a(${literalPropId++},function(){return ${value}})`
- }
- if (isProp || (
- !el.component && platformMustUseProp(el.tag, el.attrsMap.type, name)
- )) {
- addProp(el, name, value)
- } else {
- addAttr(el, name, value)
- }
- } else if (onRE.test(name)) { // v-on
- name = name.replace(onRE, '')
- addHandler(el, name, value, modifiers, false, warn)
- } else { // normal directives
- name = name.replace(dirRE, '')
- // parse arg
- const argMatch = name.match(argRE)
- const arg = argMatch && argMatch[1]
- if (arg) {
- name = name.slice(0, -(arg.length + 1))
- }
- addDirective(el, name, rawName, value, arg, modifiers)
- if (process.env.NODE_ENV !== 'production' && name === 'model') {
- checkForAliasModel(el, value)
- }
- }
- } else {
- // literal attribute
- if (process.env.NODE_ENV !== 'production') {
- const res = parseText(value, delimiters)
- if (res) {
- warn(
- `${name}="${value}": ` +
- 'Interpolation inside attributes has been removed. ' +
- 'Use v-bind or the colon shorthand instead. For example, ' +
- 'instead of <div id="{{ val }}">, use <div :id="val">.'
- )
- }
- }
- addAttr(el, name, JSON.stringify(value))
- // #6887 firefox doesn't update muted state if set via attribute
- // even immediately after element creation
- if (!el.component &&
- name === 'muted' &&
- platformMustUseProp(el.tag, el.attrsMap.type, name)) {
- addProp(el, name, 'true')
- }
- }
- }
- }
- function checkInFor (el: ASTElement): boolean {
- let parent = el
- while (parent) {
- if (parent.for !== undefined) {
- return true
- }
- parent = parent.parent
- }
- return false
- }
- function parseModifiers (name: string): Object | void {
- const match = name.match(modifierRE)
- if (match) {
- const ret = {}
- match.forEach(m => { ret[m.slice(1)] = true })
- return ret
- }
- }
- function makeAttrsMap (attrs: Array<Object>): Object {
- const map = {}
- for (let i = 0, l = attrs.length; i < l; i++) {
- if (
- process.env.NODE_ENV !== 'production' &&
- map[attrs[i].name] && !isIE && !isEdge
- ) {
- warn('duplicate attribute: ' + attrs[i].name)
- }
- map[attrs[i].name] = attrs[i].value
- }
- return map
- }
- // for script (e.g. type="x/template") or style, do not decode content
- function isTextTag (el): boolean {
- return el.tag === 'script' || el.tag === 'style'
- }
- function isForbiddenTag (el): boolean {
- return (
- el.tag === 'style' ||
- (el.tag === 'script' && (
- !el.attrsMap.type ||
- el.attrsMap.type === 'text/javascript'
- ))
- )
- }
- const ieNSBug = /^xmlns:NS\d+/
- const ieNSPrefix = /^NS\d+:/
- /* istanbul ignore next */
- function guardIESVGBug (attrs) {
- const res = []
- for (let i = 0; i < attrs.length; i++) {
- const attr = attrs[i]
- if (!ieNSBug.test(attr.name)) {
- attr.name = attr.name.replace(ieNSPrefix, '')
- res.push(attr)
- }
- }
- return res
- }
- function checkForAliasModel (el, value) {
- let _el = el
- while (_el) {
- if (_el.for && _el.alias === value) {
- warn(
- `<${el.tag} v-model="${value}">: ` +
- `You are binding v-model directly to a v-for iteration alias. ` +
- `This will not be able to modify the v-for source array because ` +
- `writing to the alias is like modifying a function local variable. ` +
- `Consider using an array of objects and use v-model on an object property instead.`
- )
- }
- _el = _el.parent
- }
- }
|