Testing v-HTML Directive Transformation in uni-app UTS Android Compiler
This test suite validates the v-html directive transformation functionality in the uni-app UTS compiler for Android. It ensures proper handling of HTML content injection through v-html directives while maintaining security and compliance with the Vue compiler core specifications.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
dcloudio/uni-app
packages/uni-app-uts/__tests__/android/transforms/vHtml.spec.ts
import { extend } from '@vue/shared'
import {
type CompilerOptions,
baseParse as parse,
transform,
transformElement,
} from '@vue/compiler-core'
import { ErrorCodes } from '../../../src/plugins/android/uvue/compiler/errors'
import { transformVHtml } from '../../../src/plugins/android/uvue/compiler/transforms/vHtml'
import { transformExpression } from '../../../src/plugins/android/uvue/compiler/transforms/transformExpression'
import { assert } from '../testUtils'
function parseWithVHtml(template: string, options: CompilerOptions = {}) {
const ast = parse(template)
transform(
ast,
extend({}, options, {
prefixIdentifiers: options.prefixIdentifiers,
nodeTransforms: [transformVHtml, transformExpression, transformElement],
})
)
return ast
}
describe('compiler: transform v-html', () => {
test('simple expression', () => {
assert(
`<view v-html="html" />`,
`createElementVNode(\"view\", null, [
createElementVNode(\"rich-text\", utsMapOf({ nodes: _ctx.html }), null, 8 /* PROPS */, [\"nodes\"])
])`
)
})
test('Non-Element should be ignored', () => {
assert(`<Foo v-html="html" />`, `createVNode(_component_Foo)`)
})
test('should throw error when missing expression', () => {
const onError = jest.fn()
parseWithVHtml('<view v-html="" />', {
onError,
prefixIdentifiers: true,
})
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith(
expect.objectContaining({
code: ErrorCodes.X_V_HTML_NO_EXPRESSION,
})
)
})
test('should throw error when has children', () => {
const onError = jest.fn()
parseWithVHtml(
'<view v-html="html"><text>text child in v-html</text></view>',
{
onError,
prefixIdentifiers: true,
}
)
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith(
expect.objectContaining({
code: ErrorCodes.X_V_HTML_WITH_CHILDREN,
})
)
})
})