-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
616aea9
feat: add v-html transformer
pikax 32762b8
fix: added the errors and tests
pikax d9321c8
fix: typos
pikax 197f360
feat: add warning when replacing children
pikax 2b05095
feat: v-html change to a simple transform and add compiler error
9a696d6
fix: rename variable to prop
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||||||
'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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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`) | ||||||
} | ||||||
} | ||||||
} | ||||||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v-html
(andv-text
) shouldn't be structural directive transforms (The only structural directives arev-if
andv-for
). They should just be normal transforms mapping the directive to the corresponding prop.