clone icon indicating copy to clipboard operation
clone copied to clipboard

TypeError: The HTMLDivElement.align setter can only be used on instances of HTMLDivElement

Open k-j-kim opened this issue 8 years ago • 4 comments

When copying a DOM object, following line fails:

child[i] = _clone(parent[i], depth - 1); https://github.com/pvorb/clone/blob/e3f252d1eb24f4e269337098295604e266719d4d/clone.js#L156

TypeError: The HTMLDivElement.align setter can only be used on instances of HTMLDivElement

It seems like calling Object.getPrototypeOf on a DOM element gives HTMLDivElementPrototype which has a slightly different behavior than HtmlDivElement.

Reproducible in Safari Version 10.0 (10602.1.50.0.10)

k-j-kim avatar Apr 07 '17 23:04 k-j-kim

Hm, maybe we should use the cloneNode method when dealing with document nodes?

Can confirm that this fails in Safari 10.1:

Object.create(Object.getPrototypeOf(document.createElement('div'))).align = 'foo'

but this does not:

document.createElement('div').cloneNode().align = 'foo'

We'd want to copy over properties afterwards. I don't think we can clone event listeners.

rictic avatar Apr 07 '17 23:04 rictic

btw, Object.create( Object.getPrototypeOf(document.createElement('div'))).align = 'foo' will fail in Chrome (Version 57.0.2987.133 (64-bit)) as well, although with a different error message: Uncaught TypeError: Illegal invocation.

Agree that with document nodes, cloneNode should be used.

k-j-kim avatar Apr 07 '17 23:04 k-j-kim

Any updates on this?

kermit-the-frog avatar Jan 11 '18 12:01 kermit-the-frog

There are more cases where Object.create(Object.getPrototypeOf(...)) fails. For example: const url = clone(new URL('http://example.com')) throws the same Uncaught TypeError: Illegal invocation in chrome and TypeError: 'set href' called on an object that does not implement interface URL. in Firefox

tomhooijenga avatar Aug 14 '18 09:08 tomhooijenga