Skip to content

support static tbody tr td elements #1268

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

Closed
wants to merge 4 commits into from
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
17 changes: 17 additions & 0 deletions packages/runtime-dom/__tests__/rendererStaticNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ describe('static vnode handling', () => {
render(h('div', [h('b'), h('b'), s]), root)
expect(root.innerHTML).toBe(`<div><b></b><b></b>${content}</div>`)
})

test('should support tbody tr td...',()=>{
const content1 = `<tbody><tr><td>hello</td></tr></tbody>`
const content2 = `<tr><td>hello</td></tr>`
const content3 = `<td>hello</td>`

const root = document.createElement('div')
const s1 = createStaticVNode(content1,1)
const s2 = createStaticVNode(content2,1)
const s3 = createStaticVNode(content3,1)
render(h('table', [s1]), root)
expect(root.innerHTML).toBe(`<table>${content1}</table>`)
render(h('table', [s2]), root)
expect(root.innerHTML).toBe(`<table>${content2}</table>`)
render(h('tr', [s3]), root)
expect(root.innerHTML).toBe(`<tr>${content3}</tr>`)
})
})
32 changes: 30 additions & 2 deletions packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { RendererOptions } from '@vue/runtime-core'


interface wrapConfig {
[name: string]: [number,string,string]
}
export const svgNS = 'http://www.w3.org/2000/svg'

const doc = (typeof document !== 'undefined' ? document : null) as Document

let tempContainer: HTMLElement
let tempSVGContainer: SVGElement

let wrapMap:wrapConfig = {
thead: [ 1, "<table>", "</table>" ],
tr: [ 2, "<table><tbody>", "</tbody></table>" ],
td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ]
}
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead
wrapMap.th = wrapMap.td
const rtagName = /<([\w:]+)/


export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
insert: (child, parent, anchor) => {
if (anchor) {
Expand Down Expand Up @@ -59,11 +74,22 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
// Static content here can only come from compiled templates.
// As long as the user only uses trusted templates, this is safe.
insertStaticContent(content, parent, anchor, isSVG) {
const temp = isSVG
let temp = isSVG
? tempSVGContainer ||
(tempSVGContainer = doc.createElementNS(svgNS, 'svg'))
: tempContainer || (tempContainer = doc.createElement('div'))
temp.innerHTML = content
const tag = ( rtagName.exec( content ) || ["", ""] )[1].toLowerCase()
if(wrapMap[tag]){
// wrapping table labels to tr|td elements to return correct dom
let [depth, before, after] = wrapMap[ tag ] || [ 0, "", "" ]
temp.innerHTML = before+content+after
while(depth--){
temp = temp.firstChild as HTMLElement|SVGElement
}
}else{
temp.innerHTML = content
}

const first = temp.firstChild as Element
let node: Element | null = first
let last: Element = node
Expand All @@ -75,3 +101,5 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
return [first, last]
}
}