Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implementing v-html transform #132

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/compiler-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export const enum ErrorCodes {
X_FOR_MALFORMED_EXPRESSION,
X_V_BIND_NO_EXPRESSION,
X_V_ON_NO_EXPRESSION,
X_V_HTML_NO_EXPRESSION,
X_V_HTML_UNEXPECTED_USAGE,
X_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
X_NAMED_SLOT_ON_COMPONENT,
X_MIXED_SLOT_USAGE,
Expand Down
67 changes: 67 additions & 0 deletions packages/compiler-dom/__tests__/transforms/vHtml.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
parse,
transform,
CompilerOptions,
ElementNode,
NodeTypes
} from '@vue/compiler-core'
import { mockWarn } from '@vue/runtime-test'
import { transformHtml } from '../../src/transforms/vHtml'

function transformWithHtmlTransform(
template: string,
options: CompilerOptions = {}
) {
const ast = parse(template)
transform(ast, {
nodeTransforms: [transformHtml],
...options
})
return {
root: ast,
node: ast.children[0] as ElementNode
}
}

describe('compiler: html transform', () => {
mockWarn()
it('should add `innerHtml` prop', () => {
const { node } = transformWithHtmlTransform(`<div v-html="test"/>`)
expect(node.props[0]).toMatchObject({
type: NodeTypes.DIRECTIVE,
name: `bind`,
arg: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `innerHTML`,
isStatic: true
},
exp: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `test`,
isStatic: false
}
})
})

it('should remove all children', () => {
const { node } = transformWithHtmlTransform(
`<div v-html="test"><p>foo</p><p>bar</p></div>`
)
expect(node.children).toHaveLength(0)
expect(node.props[0]).toMatchObject({
type: NodeTypes.DIRECTIVE,
name: `bind`,
arg: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `innerHTML`,
isStatic: true
},
exp: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `test`,
isStatic: false
}
})
expect(`"v-html" replaced children on "div" element`).toHaveBeenWarned()
})
})
7 changes: 6 additions & 1 deletion packages/compiler-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { baseCompile, CompilerOptions, CodegenResult } from '@vue/compiler-core'
import { parserOptionsMinimal } from './parserOptionsMinimal'
import { parserOptionsStandard } from './parserOptionsStandard'
import { transformStyle } from './transforms/transformStyle'
import { transformHtml } from './transforms/vHtml'

export function compile(
template: string,
Expand All @@ -10,7 +11,11 @@ export function compile(
return baseCompile(template, {
...options,
...(__BROWSER__ ? parserOptionsMinimal : parserOptionsStandard),
nodeTransforms: [transformStyle, ...(options.nodeTransforms || [])],
nodeTransforms: [
transformStyle,
transformHtml,
...(options.nodeTransforms || [])
],
directiveTransforms: {
// TODO include DOM-specific directiveTransforms
...(options.directiveTransforms || {})
Expand Down
46 changes: 45 additions & 1 deletion packages/compiler-dom/src/transforms/vHtml.ts
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
// TODO
import {
createStructuralDirectiveTransform,
createCompilerError,
ErrorCodes,
createSimpleExpression,
SimpleExpressionNode,
NodeTypes
} from '@vue/compiler-core'

export const transformHtml = createStructuralDirectiveTransform(
Copy link
Member

@yyx990803 yyx990803 Oct 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v-html (and v-text) shouldn't be structural directive transforms (The only structural directives are v-if and v-for). They should just be normal transforms mapping the directive to the corresponding prop.

'html',
(node, dir, context) => {
if (!dir.exp || !(dir.exp as SimpleExpressionNode).content.trim()) {
const loc = dir.exp ? dir.exp.loc : node.loc
context.onError(
createCompilerError(ErrorCodes.X_V_HTML_NO_EXPRESSION, dir.loc)
)
dir.exp = createSimpleExpression('', true, loc)
}

// v-show can't be used outside of an element
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// v-show can't be used outside of an element
// v-html can't be used outside of an element

if (node.type !== NodeTypes.ELEMENT) {
createCompilerError(ErrorCodes.X_V_HTML_UNEXPECTED_USAGE, dir.loc)
}

// add prop innerHTML
node.props.push({
type: NodeTypes.DIRECTIVE,
name: `bind`,
arg: createSimpleExpression(`innerHTML`, true, dir.loc),
exp: dir.exp,
modifiers: [],
loc: dir.loc
})

if (node.children.length > 0) {
// remove all the children, since they will be overridden by the `innerHTML`
node.children = []

if (__DEV__) {
console.warn(`"v-html" replaced children on "${node.tag}" element`)
}
}
}
)