Skip to content

Commit

Permalink
fix(rumtime-core): custom dom props should be cloned when cloning a h…
Browse files Browse the repository at this point in the history
…oisted DOM (#3080)

fix #3072
  • Loading branch information
HcySunYang committed Mar 25, 2021
1 parent eb1fae6 commit 5dbe834
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/runtime-dom/__tests__/nodeOps.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { nodeOps } from '../src/nodeOps'

describe('nodeOps', () => {
test('the _value property should be cloned', () => {
const el = nodeOps.createElement('input') as HTMLDivElement & {
_value: any
}
el._value = 1
const cloned = nodeOps.cloneNode!(el) as HTMLDivElement & { _value: any }
expect(cloned._value).toBe(1)
})
})
15 changes: 14 additions & 1 deletion packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,20 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
},

cloneNode(el) {
return el.cloneNode(true)
const cloned = el.cloneNode(true)
// #3072
// - in `patchDOMProp`, we store the actual value in the `el._value` property.
// - normally, elements using `:value` bindings will not be hoisted, but if
// the bound value is a constant, e.g. `:value="true"` - they do get
// hoisted.
// - in production, hoisted nodes are cloned when subsequent inserts, but
// cloneNode() does not copy the custom property we attached.
// - This may need to account for other custom DOM properties we attach to
// elements in addition to `_value` in the future.
if (`_value` in el) {
;(cloned as any)._value = (el as any)._value
}
return cloned
},

// __UNSAFE__
Expand Down

0 comments on commit 5dbe834

Please sign in to comment.