2020-02-06 03:23:03 +08:00
|
|
|
import {
|
|
|
|
DOMErrorCodes,
|
|
|
|
type DirectiveTransform,
|
|
|
|
ElementTypes,
|
|
|
|
type ExpressionNode,
|
|
|
|
NodeTypes,
|
|
|
|
type PlainElementNode,
|
|
|
|
createCallExpression,
|
|
|
|
createConditionalExpression,
|
|
|
|
createDOMCompilerError,
|
2020-02-06 06:01:00 +08:00
|
|
|
createInterpolation,
|
2020-02-06 03:23:03 +08:00
|
|
|
createObjectProperty,
|
|
|
|
createSimpleExpression,
|
|
|
|
findProp,
|
2020-02-06 06:01:00 +08:00
|
|
|
hasDynamicKeyVBind,
|
2020-02-06 03:23:03 +08:00
|
|
|
transformModel,
|
|
|
|
} from '@vue/compiler-dom'
|
2020-02-06 06:01:00 +08:00
|
|
|
import {
|
|
|
|
SSR_INCLUDE_BOOLEAN_ATTR,
|
|
|
|
SSR_LOOSE_CONTAIN,
|
2023-10-20 20:39:31 +08:00
|
|
|
SSR_LOOSE_EQUAL,
|
2023-07-11 17:13:18 +08:00
|
|
|
SSR_RENDER_DYNAMIC_MODEL,
|
2020-02-06 06:01:00 +08:00
|
|
|
} from '../runtimeHelpers'
|
|
|
|
import type { DirectiveTransformResult } from 'packages/compiler-core/src/transform'
|
2020-02-05 01:20:51 +08:00
|
|
|
|
2020-02-05 05:47:12 +08:00
|
|
|
export const ssrTransformModel: DirectiveTransform = (dir, node, context) => {
|
2020-02-06 03:23:03 +08:00
|
|
|
const model = dir.exp!
|
|
|
|
|
|
|
|
function checkDuplicatedValue() {
|
|
|
|
const value = findProp(node, 'value')
|
|
|
|
if (value) {
|
|
|
|
context.onError(
|
|
|
|
createDOMCompilerError(
|
|
|
|
DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE,
|
|
|
|
value.loc,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 20:39:31 +08:00
|
|
|
function processOption(plainNode: PlainElementNode) {
|
|
|
|
if (plainNode.tag === 'option') {
|
|
|
|
if (plainNode.props.findIndex(p => p.name === 'selected') === -1) {
|
|
|
|
const value = findValueBinding(plainNode)
|
|
|
|
plainNode.ssrCodegenNode!.elements.push(
|
|
|
|
createConditionalExpression(
|
|
|
|
createCallExpression(context.helper(SSR_INCLUDE_BOOLEAN_ATTR), [
|
|
|
|
createConditionalExpression(
|
|
|
|
createCallExpression(`Array.isArray`, [model]),
|
|
|
|
createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [
|
|
|
|
model,
|
|
|
|
value,
|
|
|
|
]),
|
|
|
|
createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
|
|
|
|
model,
|
|
|
|
value,
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
createSimpleExpression(' selected', true),
|
|
|
|
createSimpleExpression('', true),
|
|
|
|
false /* no newline */,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if (plainNode.tag === 'optgroup') {
|
|
|
|
plainNode.children.forEach(option =>
|
|
|
|
processOption(option as PlainElementNode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:23:03 +08:00
|
|
|
if (node.tagType === ElementTypes.ELEMENT) {
|
2020-02-06 06:01:00 +08:00
|
|
|
const res: DirectiveTransformResult = { props: [] }
|
2020-02-06 03:23:03 +08:00
|
|
|
const defaultProps = [
|
|
|
|
// default value binding for text type inputs
|
2020-02-07 01:05:53 +08:00
|
|
|
createObjectProperty(`value`, model),
|
2020-02-06 03:23:03 +08:00
|
|
|
]
|
|
|
|
if (node.tag === 'input') {
|
|
|
|
const type = findProp(node, 'type')
|
|
|
|
if (type) {
|
2020-02-06 06:01:00 +08:00
|
|
|
const value = findValueBinding(node)
|
2020-02-06 03:23:03 +08:00
|
|
|
if (type.type === NodeTypes.DIRECTIVE) {
|
|
|
|
// dynamic type
|
2020-02-06 06:01:00 +08:00
|
|
|
res.ssrTagParts = [
|
|
|
|
createCallExpression(context.helper(SSR_RENDER_DYNAMIC_MODEL), [
|
|
|
|
type.exp!,
|
|
|
|
model,
|
|
|
|
value,
|
|
|
|
]),
|
|
|
|
]
|
2020-02-06 03:23:03 +08:00
|
|
|
} else if (type.value) {
|
|
|
|
// static type
|
|
|
|
switch (type.value.content) {
|
|
|
|
case 'radio':
|
2020-02-06 06:01:00 +08:00
|
|
|
res.props = [
|
2020-02-06 03:23:03 +08:00
|
|
|
createObjectProperty(
|
2020-02-07 01:05:53 +08:00
|
|
|
`checked`,
|
2020-02-06 03:23:03 +08:00
|
|
|
createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
|
|
|
|
model,
|
2020-02-06 06:01:00 +08:00
|
|
|
value,
|
2020-02-06 03:23:03 +08:00
|
|
|
]),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
break
|
|
|
|
case 'checkbox':
|
2020-12-02 01:43:59 +08:00
|
|
|
const trueValueBinding = findProp(node, 'true-value')
|
|
|
|
if (trueValueBinding) {
|
|
|
|
const trueValue =
|
|
|
|
trueValueBinding.type === NodeTypes.ATTRIBUTE
|
|
|
|
? JSON.stringify(trueValueBinding.value!.content)
|
|
|
|
: trueValueBinding.exp!
|
|
|
|
res.props = [
|
|
|
|
createObjectProperty(
|
|
|
|
`checked`,
|
|
|
|
createCallExpression(context.helper(SSR_LOOSE_EQUAL), [
|
2020-02-06 03:23:03 +08:00
|
|
|
model,
|
2020-12-02 01:43:59 +08:00
|
|
|
trueValue,
|
|
|
|
]),
|
2020-02-06 03:23:03 +08:00
|
|
|
),
|
2020-12-02 01:43:59 +08:00
|
|
|
]
|
|
|
|
} else {
|
|
|
|
res.props = [
|
|
|
|
createObjectProperty(
|
|
|
|
`checked`,
|
|
|
|
createConditionalExpression(
|
|
|
|
createCallExpression(`Array.isArray`, [model]),
|
|
|
|
createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [
|
|
|
|
model,
|
|
|
|
value,
|
|
|
|
]),
|
|
|
|
model,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|
2020-02-06 03:23:03 +08:00
|
|
|
break
|
|
|
|
case 'file':
|
|
|
|
context.onError(
|
|
|
|
createDOMCompilerError(
|
|
|
|
DOMErrorCodes.X_V_MODEL_ON_FILE_INPUT_ELEMENT,
|
|
|
|
dir.loc,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
checkDuplicatedValue()
|
2020-02-06 06:01:00 +08:00
|
|
|
res.props = defaultProps
|
2020-02-06 03:23:03 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 06:01:00 +08:00
|
|
|
} else if (hasDynamicKeyVBind(node)) {
|
|
|
|
// dynamic type due to dynamic v-bind
|
|
|
|
// NOOP, handled in ssrTransformElement due to need to rewrite
|
|
|
|
// the entire props expression
|
2020-02-06 03:23:03 +08:00
|
|
|
} else {
|
2020-02-06 06:01:00 +08:00
|
|
|
// text type
|
2020-02-06 03:23:03 +08:00
|
|
|
checkDuplicatedValue()
|
2020-02-06 06:01:00 +08:00
|
|
|
res.props = defaultProps
|
2020-02-06 03:23:03 +08:00
|
|
|
}
|
|
|
|
} else if (node.tag === 'textarea') {
|
|
|
|
checkDuplicatedValue()
|
|
|
|
node.children = [createInterpolation(model, model.loc)]
|
|
|
|
} else if (node.tag === 'select') {
|
2023-10-20 20:39:31 +08:00
|
|
|
node.children.forEach(child => {
|
|
|
|
if (child.type === NodeTypes.ELEMENT) {
|
|
|
|
processOption(child as PlainElementNode)
|
2023-07-11 17:13:18 +08:00
|
|
|
}
|
|
|
|
})
|
2020-02-06 03:23:03 +08:00
|
|
|
} else {
|
|
|
|
context.onError(
|
|
|
|
createDOMCompilerError(
|
|
|
|
DOMErrorCodes.X_V_MODEL_ON_INVALID_ELEMENT,
|
|
|
|
dir.loc,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:01:00 +08:00
|
|
|
return res
|
2020-02-06 03:23:03 +08:00
|
|
|
} else {
|
|
|
|
// component v-model
|
|
|
|
return transformModel(dir, node, context)
|
2020-02-05 01:20:51 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 03:23:03 +08:00
|
|
|
|
|
|
|
function findValueBinding(node: PlainElementNode): ExpressionNode {
|
|
|
|
const valueBinding = findProp(node, 'value')
|
|
|
|
return valueBinding
|
|
|
|
? valueBinding.type === NodeTypes.DIRECTIVE
|
|
|
|
? valueBinding.exp!
|
|
|
|
: createSimpleExpression(valueBinding.value!.content, true)
|
|
|
|
: createSimpleExpression(`null`, false)
|
|
|
|
}
|