(dir, node, context)
| 25 | import type { DirectiveTransformResult } from 'packages/compiler-core/src/transform' |
| 26 | |
| 27 | export const ssrTransformModel: DirectiveTransform = (dir, node, context) => { |
| 28 | const model = dir.exp! |
| 29 | |
| 30 | function checkDuplicatedValue() { |
| 31 | const value = findProp(node, 'value') |
| 32 | if (value) { |
| 33 | context.onError( |
| 34 | createDOMCompilerError( |
| 35 | DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE, |
| 36 | value.loc, |
| 37 | ), |
| 38 | ) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const processSelectChildren = (children: TemplateChildNode[]) => { |
| 43 | children.forEach(child => { |
| 44 | if (child.type === NodeTypes.ELEMENT) { |
| 45 | processOption(child as PlainElementNode) |
| 46 | } else if (child.type === NodeTypes.FOR) { |
| 47 | processSelectChildren(child.children) |
| 48 | } else if (child.type === NodeTypes.IF) { |
| 49 | child.branches.forEach(b => processSelectChildren(b.children)) |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | function processOption(plainNode: PlainElementNode) { |
| 55 | if (plainNode.tag === 'option') { |
| 56 | if (plainNode.props.findIndex(p => p.name === 'selected') === -1) { |
| 57 | const value = findValueBinding(plainNode) |
| 58 | plainNode.ssrCodegenNode!.elements.push( |
| 59 | createConditionalExpression( |
| 60 | createCallExpression(context.helper(SSR_INCLUDE_BOOLEAN_ATTR), [ |
| 61 | createConditionalExpression( |
| 62 | createCallExpression(`Array.isArray`, [model]), |
| 63 | createCallExpression(context.helper(SSR_LOOSE_CONTAIN), [ |
| 64 | model, |
| 65 | value, |
| 66 | ]), |
| 67 | createCallExpression(context.helper(SSR_LOOSE_EQUAL), [ |
| 68 | model, |
| 69 | value, |
| 70 | ]), |
| 71 | ), |
| 72 | ]), |
| 73 | createSimpleExpression(' selected', true), |
| 74 | createSimpleExpression('', true), |
| 75 | false /* no newline */, |
| 76 | ), |
| 77 | ) |
| 78 | } |
| 79 | } else if (plainNode.tag === 'optgroup') { |
| 80 | processSelectChildren(plainNode.children) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (node.tagType === ElementTypes.ELEMENT) { |
nothing calls this directly
no test coverage detected