Skip to content

Commit 6702538

Browse files
devgroksolkimicreb
authored andcommitted
Stop instances of DOM Node being wrapped as it throws a TypeError when accessing it via Reflect.get
1 parent bb77031 commit 6702538

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/observer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ export function observable (obj) {
6565
)
6666
}
6767

68+
function isDomNode (obj) {
69+
return typeof Node === 'function' && obj instanceof Node
70+
}
71+
6872
function instrumentObservable (obj) {
6973
const instrument = instrumentations.get(Object.getPrototypeOf(obj))
7074
// these objects break, when they are wrapped with proxies
71-
if (instrument === false) {
75+
if (instrument === false || isDomNode(obj)) {
7276
return obj
7377
}
7478
// these objects can be wrapped by Proxies, but require special instrumentation beforehand

tests/observe.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,16 @@ describe('observe', () => {
328328
await nextTick()
329329
expect(dummy).to.equal('p2p1p3')
330330
})
331+
332+
it('should not error when a DOM element is added', async () => {
333+
let dummy = null
334+
const observed = observable({ obj: null })
335+
observe(() => (dummy = observed.obj && observed.obj.nodeType))
336+
337+
await nextTick()
338+
expect(dummy).to.equal(null)
339+
observed.obj = document
340+
await nextTick()
341+
expect(dummy).to.equal(9)
342+
})
331343
})

0 commit comments

Comments
 (0)