Skip to content

Commit 5e52f4e

Browse files
committed
fix(compiler-dom): should ignore and warn side effect tags like script and style
This keeps behavior consistency with v2.
1 parent 903e8f6 commit 5e52f4e

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { compile, CompilerError } from '../../src'
2+
3+
describe('compiler: ignore side effect tags', () => {
4+
it('should ignore script', () => {
5+
let err: CompilerError | undefined
6+
const { code } = compile(`<script>console.log(1)</script>`, {
7+
onError(e) {
8+
err = e
9+
}
10+
})
11+
expect(code).not.toMatch('script')
12+
expect(err).toBeDefined()
13+
expect(err!.message).toMatch(`Tags with side effect`)
14+
})
15+
16+
it('should ignore style', () => {
17+
let err: CompilerError | undefined
18+
const { code } = compile(`<style>h1 { color: red }</style>`, {
19+
onError(e) {
20+
err = e
21+
}
22+
})
23+
expect(code).not.toMatch('style')
24+
expect(err).toBeDefined()
25+
expect(err!.message).toMatch(`Tags with side effect`)
26+
})
27+
})

packages/compiler-dom/src/errors.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enum DOMErrorCodes {
3131
X_V_MODEL_UNNECESSARY_VALUE,
3232
X_V_SHOW_NO_EXPRESSION,
3333
X_TRANSITION_INVALID_CHILDREN,
34+
X_IGNORED_SIDE_EFFECT_TAG,
3435
__EXTEND_POINT__
3536
}
3637

@@ -44,5 +45,6 @@ export const DOMErrorMessages: { [code: number]: string } = {
4445
[DOMErrorCodes.X_V_MODEL_ON_FILE_INPUT_ELEMENT]: `v-model cannot used on file inputs since they are read-only. Use a v-on:change listener instead.`,
4546
[DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE]: `Unnecessary value binding used alongside v-model. It will interfere with v-model's behavior.`,
4647
[DOMErrorCodes.X_V_SHOW_NO_EXPRESSION]: `v-show is missing expression.`,
47-
[DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN]: `<Transition> expects exactly one child element or component.`
48+
[DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN]: `<Transition> expects exactly one child element or component.`,
49+
[DOMErrorCodes.X_IGNORED_SIDE_EFFECT_TAG]: `Tags with side effect (<script> and <style>) are ignored in client component templates.`
4850
}

packages/compiler-dom/src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { transformOn } from './transforms/vOn'
1818
import { transformShow } from './transforms/vShow'
1919
import { warnTransitionChildren } from './transforms/warnTransitionChildren'
2020
import { stringifyStatic } from './transforms/stringifyStatic'
21+
import { ignoreSideEffectTags } from './transforms/ignoreSideEffectTags'
2122
import { extend } from '@vue/shared'
2223

2324
export { parserOptions }
@@ -43,7 +44,14 @@ export function compile(
4344
return baseCompile(
4445
template,
4546
extend({}, parserOptions, options, {
46-
nodeTransforms: [...DOMNodeTransforms, ...(options.nodeTransforms || [])],
47+
nodeTransforms: [
48+
// ignore <script> and <tag>
49+
// this is not put inside DOMNodeTransforms because that list is used
50+
// by compiler-ssr to generate vnode fallback branches
51+
ignoreSideEffectTags,
52+
...DOMNodeTransforms,
53+
...(options.nodeTransforms || [])
54+
],
4755
directiveTransforms: extend(
4856
{},
4957
DOMDirectiveTransforms,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NodeTransform, NodeTypes, ElementTypes } from '@vue/compiler-core/src'
2+
import { DOMErrorCodes, createDOMCompilerError } from '../errors'
3+
4+
export const ignoreSideEffectTags: NodeTransform = (node, context) => {
5+
if (
6+
node.type === NodeTypes.ELEMENT &&
7+
node.tagType === ElementTypes.ELEMENT &&
8+
(node.tag === 'script' || node.tag === 'style')
9+
) {
10+
context.onError(
11+
createDOMCompilerError(DOMErrorCodes.X_IGNORED_SIDE_EFFECT_TAG, node.loc)
12+
)
13+
context.removeNode()
14+
}
15+
}

0 commit comments

Comments
 (0)