eligrey / eligrey/classList.js

Object.defineProperty not working with older Safari Versions

Open
#50 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.1k
Forks
226
PR merge metrics
No merged PRs in 30d

Description

Hi,

I had a problem with Safari on iOS 6.1.3.

The if check

`if (!("classList" in document.createElement("_")) || document.createElementNS && !("classList" in document.createElementNS("http://www.w3.org/2000/svg", "g")))`

resolves correctly in true and the script comes later to

`try {
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);`

This is a problem in old Safari Versions, because it is not working on DOM Objects. So there is no error but toggle or add will not work with the additional parameters.

I found a working solution for myself by adding

```
if ("document" in self) {
var setPropOnDOM = true;

// Detect if we can define properties on DOM Objects
try {
Object.defineProperty(document.createElement("_"), "classList", {
"get": function() {}
});
} catch (e) { //Old Safari versions will break at this point
setPropOnDOM = false;
}

// Full polyfill for browsers with no classList support
// Including IE < Edge missing SVGElement.classList
if (setPropOnDOM && (!("classList" in document.createElement("_")) || document.createElementNS && !("classList" in document.createElementNS("http://www.w3.org/2000/svg", "g")))) {
```

Old Safaris will resolve in the catch block and then go on with partial support which is then working like expected. I will finish it next week and come back with a clean and fully response when my unit tests are green in all browsers (including old IE, Safari and so on).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.