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

fix(compiler-dom): should bail hoisted on break content with innerHTM… #1230

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,18 @@ describe('stringify static html', () => {
type: NodeTypes.VNODE_CALL // not CALL_EXPRESSION
})
})

// https://github.com/vitejs/vite/issues/226
test('should bail on break content with innerHTML (eg.tables related tags)', () => {
const { ast } = compileWithStringify(
`<div><div>${repeat(
`<tr class="foo">foo</tr>`,
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
)}</div></div>`
)
expect(ast.hoists.length).toBe(1)
expect(ast.hoists[0]).toMatchObject({
type: NodeTypes.VNODE_CALL // not CALL_EXPRESSION
})
})
})
11 changes: 10 additions & 1 deletion packages/compiler-dom/src/transforms/stringifyStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
toDisplayString,
normalizeClass,
normalizeStyle,
stringifyStyle
stringifyStyle,
makeMap
} from '@vue/shared'

export const enum StringifyThresholds {
Expand Down Expand Up @@ -59,6 +60,12 @@ type StringifiableNode = PlainElementNode | TextCallNode
* This optimization is only performed in Node.js.
*/
export const stringifyStatic: HoistTransform = (children, context) => {
if (
context.parent!.type === NodeTypes.ELEMENT &&
canNotStringifiableTag((context.parent as PlainElementNode).tag)
) {
return
}
let nc = 0 // current node count
let ec = 0 // current element with binding count
const currentChunk: StringifiableNode[] = []
Expand Down Expand Up @@ -136,6 +143,8 @@ const isStringifiableAttr = (name: string) => {
return isKnownAttr(name) || dataAriaRE.test(name)
}

const canNotStringifiableTag = makeMap('p,table,thead,tr,th,tbody,td,colspan')

const replaceHoist = (
node: StringifiableNode,
replacement: JSChildNode | null,
Expand Down