resolveComponent,
ComponentOptions,
ref,
- defineComponent
+ defineComponent,
+ createTextVNode,
+ createStaticVNode
} from 'vue'
import { escapeHtml, mockWarn } from '@vue/shared'
import { renderToString, renderComponent } from '../src/renderToString'
})
})
+ describe('raw vnode types', () => {
+ test('Text', async () => {
+ expect(await renderToString(createTextVNode('hello <div>'))).toBe(
+ `hello <div>`
+ )
+ })
+
+ test('Comment', async () => {
+ // https://www.w3.org/TR/html52/syntax.html#comments
+ expect(
+ await renderToString(
+ h('div', [
+ createCommentVNode('>foo'),
+ createCommentVNode('->foo'),
+ createCommentVNode('<!--foo-->'),
+ createCommentVNode('--!>foo<!-')
+ ])
+ )
+ ).toBe(`<div><!--foo--><!--foo--><!--foo--><!--foo--></div>`)
+ })
+
+ test('Static', async () => {
+ const content = `<div id="ok">hello<span>world</span></div>`
+ expect(await renderToString(createStaticVNode(content))).toBe(content)
+ })
+ })
+
describe('scopeId', () => {
// note: here we are only testing scopeId handling for vdom serialization.
// compiled srr render functions will include scopeId directly in strings.
createVNode,
Text,
Comment,
+ Static,
Fragment,
ssrUtils,
Slots,
return (compileCache[template] = Function('require', code)(require))
}
+// https://www.w3.org/TR/html52/syntax.html#comments
+const commentStripRE = /^-?>|<!--|-->|--!>|<!-$/g
+
function renderVNode(
push: PushFn,
vnode: VNode,
const { type, shapeFlag, children } = vnode
switch (type) {
case Text:
- push(children as string)
+ push(escapeHtml(children as string))
break
case Comment:
- push(children ? `<!--${children}-->` : `<!---->`)
+ push(
+ children
+ ? `<!--${(children as string).replace(commentStripRE, '')}-->`
+ : `<!---->`
+ )
+ break
+ case Static:
+ push(children as string)
break
case Fragment:
push(`<!--[-->`) // open