From e1657fd7ce49bff3c3ecad3c56ae527347505c34 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 3 Nov 2017 15:30:47 -0400 Subject: [PATCH] fix(ssr): properly render because it is the only edge case +// that must be done at runtime instead of compile time. +export default function model (node: VNodeWithData, dir: VNodeDirective) { + if (!node.children) return + const value = dir.value + const isMultiple = node.data.attrs && node.data.attrs.multiple + for (let i = 0, l = node.children.length; i < l; i++) { + const option = node.children[i] + if (option.tag === 'option') { + if (isMultiple) { + const selected = + Array.isArray(value) && + (looseIndexOf(value, getValue(option)) > -1) + if (selected) { + setSelected(option) + } + } else { + if (looseEqual(value, getValue(option))) { + setSelected(option) + return + } + } + } + } +} + +function getValue (option) { + const data = option.data || {} + return ( + (data.attrs && data.attrs.value) || + (data.domProps && data.domProps.value) || + (option.children && option.children[0] && option.children[0].text) + ) +} + +function setSelected (option) { + const data = option.data || (option.data = {}) + const attrs = data.attrs || (data.attrs = {}) + attrs.selected = '' +} diff --git a/src/server/optimizing-compiler/optimizer.js b/src/server/optimizing-compiler/optimizer.js index 1a671803caa..92cfe22eb4a 100644 --- a/src/server/optimizing-compiler/optimizer.js +++ b/src/server/optimizing-compiler/optimizer.js @@ -113,7 +113,8 @@ function isUnOptimizableTree (node: ASTNode): boolean { return ( isBuiltInTag(node.tag) || // built-in (slot, component) !isPlatformReservedTag(node.tag) || // custom component - !!node.component // "is" component + !!node.component || // "is" component + isSelectWithModel(node) // cannot be optimized because it requires a runtime check +// to determine proper selected option +function isSelectWithModel (node: ASTNode): ?boolean { + return ( + node.type === 1 && + node.tag === 'select' && + node.directives && + node.directives.some(d => d.name === 'model') + ) +} diff --git a/test/ssr/ssr-string.spec.js b/test/ssr/ssr-string.spec.js index 20f5ec51a21..3e8470f9250 100644 --- a/test/ssr/ssr-string.spec.js +++ b/test/ssr/ssr-string.spec.js @@ -1034,6 +1034,84 @@ describe('SSR: renderToString', () => { done() }) }) + + it('render v-model with + + + + ` + }, result => { + expect(result).toContain( + '' + ) + done() + }) + }) + + it('render v-model with + + + + + ` + }, result => { + expect(result).toContain( + '' + ) + done() + }) + }) + + it('render v-model with + + + + ` + }, result => { + expect(result).toContain( + '' + ) + done() + }) + }) }) function renderVmWithOptions (options, cb) {