-import { createApp, h } from 'vue'
-import { renderToString, renderComponent, renderSlot } from '../src'
+import { createApp, h, createCommentVNode } from 'vue'
+import { renderToString, renderComponent, renderSlot, escapeHtml } from '../src'
describe('ssr: renderToString', () => {
describe('components', () => {
})
})
- describe('scopeId', () => {
- // TODO
- })
+ describe('vnode element', () => {
+ test('props', async () => {
+ expect(
+ await renderToString(
+ h('div', { id: 'foo&', class: ['bar', 'baz'] }, 'hello')
+ )
+ ).toBe(`<div id="foo&" class="bar baz">hello</div>`)
+ })
- describe('vnode', () => {
- test('text children', () => {})
+ test('text children', async () => {
+ expect(await renderToString(h('div', 'hello'))).toBe(`<div>hello</div>`)
+ })
- test('array children', () => {})
+ test('array children', async () => {
+ expect(
+ await renderToString(
+ h('div', [
+ 'foo',
+ h('span', 'bar'),
+ [h('span', 'baz')],
+ createCommentVNode('qux')
+ ])
+ )
+ ).toBe(
+ `<div>foo<span>bar</span><!----><span>baz</span><!----><!--qux--></div>`
+ )
+ })
+
+ test('void elements', async () => {
+ expect(await renderToString(h('input'))).toBe(`<input>`)
+ })
- test('void elements', () => {})
+ test('innerHTML', async () => {
+ expect(
+ await renderToString(
+ h(
+ 'div',
+ {
+ innerHTML: `<span>hello</span>`
+ },
+ 'ignored'
+ )
+ )
+ ).toBe(`<div><span>hello</span></div>`)
+ })
- test('innerHTML', () => {})
+ test('textContent', async () => {
+ expect(
+ await renderToString(
+ h(
+ 'div',
+ {
+ textContent: `<span>hello</span>`
+ },
+ 'ignored'
+ )
+ )
+ ).toBe(`<div>${escapeHtml(`<span>hello</span>`)}</div>`)
+ })
- test('textContent', () => {})
+ test('textarea value', async () => {
+ expect(
+ await renderToString(
+ h(
+ 'textarea',
+ {
+ value: `<span>hello</span>`
+ },
+ 'ignored'
+ )
+ )
+ ).toBe(`<textarea>${escapeHtml(`<span>hello</span>`)}</textarea>`)
+ })
+ })
- test('textarea value', () => {})
+ describe('scopeId', () => {
+ // TODO
})
})
isNoUnitNumericStyleProp,
isOn,
isSSRSafeAttrName,
- isBooleanAttr
+ isBooleanAttr,
+ makeMap
} from '@vue/shared'
+const shouldIgnoreProp = makeMap(`key,ref,innerHTML,textContent`)
+
export function renderProps(
props: Record<string, unknown>,
- isCustomElement: boolean = false
+ tag?: string
): string {
let ret = ''
for (const key in props) {
- if (key === 'key' || key === 'ref' || isOn(key)) {
+ if (
+ shouldIgnoreProp(key) ||
+ isOn(key) ||
+ (tag === 'textarea' && key === 'value')
+ ) {
continue
}
const value = props[key]
} else if (key === 'style') {
ret += ` style="${renderStyle(value)}"`
} else if (value != null) {
- const attrKey = isCustomElement
- ? key
- : propsToAttrMap[key] || key.toLowerCase()
+ const attrKey =
+ tag && tag.indexOf('-') > 0
+ ? key // preserve raw name on custom elements
+ : propsToAttrMap[key] || key.toLowerCase()
if (isBooleanAttr(attrKey)) {
if (value !== false) {
ret += ` ${attrKey}`
Portal,
ShapeFlags,
ssrUtils,
- Slot
+ Slot,
+ createApp
} from 'vue'
import {
isString,
import { escapeHtml } from './ssrUtils'
const {
+ isVNode,
createComponentInstance,
setupComponent,
renderComponentRoot,
return ret
}
-export async function renderToString(app: App): Promise<string> {
+export async function renderToString(input: App | VNode): Promise<string> {
+ if (isVNode(input)) {
+ return renderAppToString(createApp({ render: () => input }))
+ } else {
+ return renderAppToString(input)
+ }
+}
+
+async function renderAppToString(app: App): Promise<string> {
const resolvedBuffer = await renderComponent(app._component, app._props, null)
return unrollBuffer(resolvedBuffer)
}
return hasAsync() ? Promise.all(buffer) : (buffer as ResolvedSSRBuffer)
}
-export function renderVNode(
+function renderVNode(
push: PushFn,
vnode: VNode,
parentComponent: ComponentInternalInstance | null = null
// TODO directives
if (props !== null) {
- openTag += renderProps(props, tag.indexOf(`-`) > 0)
+ openTag += renderProps(props, tag)
}
if (scopeId !== null) {