refactor(ssr): move escapeHtml to shared
This commit is contained in:
parent
327670a034
commit
d1eed36452
@ -1,10 +1,10 @@
|
||||
import { compile } from '../../src'
|
||||
import { compile } from '../src'
|
||||
|
||||
function getElementString(src: string): string {
|
||||
return compile(src).code.match(/_push\((.*)\)/)![1]
|
||||
}
|
||||
|
||||
describe('ssr transform element', () => {
|
||||
describe('ssr compile integration test', () => {
|
||||
test('basic elements', () => {
|
||||
expect(getElementString(`<div></div>`)).toMatchInlineSnapshot(
|
||||
`"\`<div></div>\`"`
|
||||
@ -22,4 +22,12 @@ describe('ssr transform element', () => {
|
||||
getElementString(`<div><span></span><span></span></div>`)
|
||||
).toMatchInlineSnapshot(`"\`<div><span></span><span></span></div>\`"`)
|
||||
})
|
||||
|
||||
test('nested elements with static text', () => {
|
||||
expect(
|
||||
getElementString(`<div><span>hello</span>><span>bye</span></div>`)
|
||||
).toMatchInlineSnapshot(
|
||||
`"\`<div><span>hello</span>><span>bye</span></div>\`"`
|
||||
)
|
||||
})
|
||||
})
|
@ -10,7 +10,7 @@ import {
|
||||
ElementTypes,
|
||||
createBlockStatement
|
||||
} from '@vue/compiler-dom'
|
||||
import { isString } from '@vue/shared'
|
||||
import { isString, escapeHtml } from '@vue/shared'
|
||||
|
||||
// Because SSR codegen output is completely different from client-side output
|
||||
// (e.g. multiple elements can be concatenated into a single template literal
|
||||
@ -85,7 +85,7 @@ function processChildren(
|
||||
// TODO
|
||||
}
|
||||
} else if (child.type === NodeTypes.TEXT) {
|
||||
// TODO
|
||||
context.pushStringPart(escapeHtml(child.content))
|
||||
} else if (child.type === NodeTypes.IF) {
|
||||
// TODO
|
||||
} else if (child.type === NodeTypes.FOR) {
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
TemplateLiteral,
|
||||
createTemplateLiteral
|
||||
} from '@vue/compiler-dom'
|
||||
import { escapeHtml } from '@vue/server-renderer'
|
||||
import { escapeHtml } from '@vue/shared'
|
||||
|
||||
/*
|
||||
## Simple Element
|
||||
|
@ -1,15 +1,5 @@
|
||||
import { escapeHtml, interpolate } from '../src'
|
||||
|
||||
test('ssr: escapeHTML', () => {
|
||||
expect(escapeHtml(`foo`)).toBe(`foo`)
|
||||
expect(escapeHtml(true)).toBe(`true`)
|
||||
expect(escapeHtml(false)).toBe(`false`)
|
||||
expect(escapeHtml(`a && b`)).toBe(`a && b`)
|
||||
expect(escapeHtml(`"foo"`)).toBe(`"foo"`)
|
||||
expect(escapeHtml(`'bar'`)).toBe(`'bar'`)
|
||||
expect(escapeHtml(`<div>`)).toBe(`<div>`)
|
||||
})
|
||||
|
||||
test('ssr: interpolate', () => {
|
||||
expect(interpolate(0)).toBe(`0`)
|
||||
expect(interpolate(`foo`)).toBe(`foo`)
|
@ -4,4 +4,13 @@ export { renderToString } from './renderToString'
|
||||
// internal
|
||||
export { renderComponent, renderSlot } from './renderToString'
|
||||
export { renderClass, renderStyle, renderProps } from './renderProps'
|
||||
export { escapeHtml, interpolate } from './ssrUtils'
|
||||
|
||||
// utils
|
||||
import { escapeHtml as _escapeHtml, toDisplayString } from '@vue/shared'
|
||||
|
||||
// cast type to avoid dts dependency on @vue/shared (which is inlined)
|
||||
export const escapeHtml = _escapeHtml as (raw: string) => string
|
||||
|
||||
export function interpolate(value: unknown): string {
|
||||
return escapeHtml(toDisplayString(value))
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { escapeHtml } from './ssrUtils'
|
||||
import { escapeHtml } from '@vue/shared'
|
||||
import {
|
||||
normalizeClass,
|
||||
normalizeStyle,
|
||||
|
@ -22,7 +22,7 @@ import {
|
||||
isVoidTag
|
||||
} from '@vue/shared'
|
||||
import { renderProps } from './renderProps'
|
||||
import { escapeHtml } from './ssrUtils'
|
||||
import { escapeHtml } from '@vue/shared'
|
||||
|
||||
const {
|
||||
isVNode,
|
||||
|
11
packages/shared/__tests__/escapeHtml.spec.ts
Normal file
11
packages/shared/__tests__/escapeHtml.spec.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { escapeHtml } from '../src'
|
||||
|
||||
test('ssr: escapeHTML', () => {
|
||||
expect(escapeHtml(`foo`)).toBe(`foo`)
|
||||
expect(escapeHtml(true)).toBe(`true`)
|
||||
expect(escapeHtml(false)).toBe(`false`)
|
||||
expect(escapeHtml(`a && b`)).toBe(`a && b`)
|
||||
expect(escapeHtml(`"foo"`)).toBe(`"foo"`)
|
||||
expect(escapeHtml(`'bar'`)).toBe(`'bar'`)
|
||||
expect(escapeHtml(`<div>`)).toBe(`<div>`)
|
||||
})
|
@ -1,5 +1,3 @@
|
||||
import { toDisplayString } from '@vue/shared'
|
||||
|
||||
const escapeRE = /["'&<>]/
|
||||
|
||||
export function escapeHtml(string: unknown) {
|
||||
@ -45,7 +43,3 @@ export function escapeHtml(string: unknown) {
|
||||
|
||||
return lastIndex !== index ? html + str.substring(lastIndex, index) : html
|
||||
}
|
||||
|
||||
export function interpolate(value: unknown) {
|
||||
return escapeHtml(toDisplayString(value))
|
||||
}
|
@ -8,6 +8,7 @@ export * from './mockWarn'
|
||||
export * from './normalizeProp'
|
||||
export * from './domTagConfig'
|
||||
export * from './domAttrConfig'
|
||||
export * from './escapeHtml'
|
||||
|
||||
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
|
||||
? Object.freeze({})
|
||||
|
Loading…
Reference in New Issue
Block a user