Skip to content

Commit

Permalink
fix(runtome-dom): properly support creating customized built-in element
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 27, 2020
1 parent 412ec86 commit b1d0b04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export interface RendererOptions<
): void
insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
remove(el: HostNode): void
createElement(type: string, isSVG?: boolean): HostElement
createElement(
type: string,
isSVG?: boolean,
isCustomizedBuiltIn?: string
): HostElement
createText(text: string): HostNode
createComment(text: string): HostNode
setText(node: HostNode, text: string): void
Expand Down Expand Up @@ -549,7 +553,11 @@ function baseCreateRenderer(
// exactly the same, and we can simply do a clone here.
el = vnode.el = hostCloneNode(vnode.el)
} else {
el = vnode.el = hostCreateElement(vnode.type as string, isSVG)
el = vnode.el = hostCreateElement(
vnode.type as string,
isSVG,
props && props.is
)
// props
if (props) {
for (const key in props) {
Expand Down
20 changes: 20 additions & 0 deletions packages/runtime-dom/__tests__/customizedBuiltIn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { render, h } from '@vue/runtime-dom'

describe('customimized built-in elements support', () => {
let createElement: jest.SpyInstance
afterEach(() => {
createElement.mockRestore()
})

test('should created element with is option', () => {
const root = document.createElement('div')
createElement = jest.spyOn(document, 'createElement')
render(h('button', { is: 'plastic-button' }), root)
expect(createElement.mock.calls[0]).toMatchObject([
'button',
{ is: 'plastic-button' }
])
// should also render the attribute
expect(root.innerHTML).toBe(`<button is="plastic-button"></button>`)
})
})
6 changes: 4 additions & 2 deletions packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
}
},

createElement: (tag, isSVG): Element =>
isSVG ? doc.createElementNS(svgNS, tag) : doc.createElement(tag),
createElement: (tag, isSVG, is): Element =>
isSVG
? doc.createElementNS(svgNS, tag)
: doc.createElement(tag, is ? { is } : undefined),

createText: text => doc.createTextNode(text),

Expand Down

0 comments on commit b1d0b04

Please sign in to comment.