panzerdp / voca

The ultimate JavaScript string library
https://vocajs.pages.dev
MIT License
3.6k stars 137 forks source link

explorer support #33

Closed Kreider186 closed 6 years ago

Kreider186 commented 6 years ago

Hi! I tried the voca.js on explorer v11 and it crashed.

I'm not an expert in javascript but I managed to make it work by adding a few lines. Maybe it could be useful to others?

Modifies:

At the very beginning of the file:

//object.keys support
if (!Object.keys) {
  Object.keys = function(obj) {
    var keys = [];
    for (var i in obj) {
      if (obj.hasOwnProperty(i)) {
      keys.push(i);
    }
  }
 return keys;
  };
}

//object.values support
if (!Object.values) {

Object.values = function(obj) {

var res = [];
    for (var i in obj) {
        if (obj.hasOwnProperty(i)) {
            res.push(obj[i]);
        }
    }
    return res;
  };
}

//Array.foreach support
if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this);
        }
    }
}

//Array.isArray support
if (!Array.isArray) {
    Array.isArray = function(item) {
      if(item instanceof Array){return true;}
      return false;
    }
}

//bind support
if (!Function.prototype.bind) {
  Function.prototype.bind = function(oThis) {
    if (typeof this !== 'function') {
      // closest thing possible to the ECMAScript 5
      // internal IsCallable function
      throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
    }

    var aArgs   = Array.prototype.slice.call(arguments, 1),
        fToBind = this,
        fNOP    = function() {},
        fBound  = function() {
          return fToBind.apply(this instanceof fNOP && oThis
                 ? this
                 : oThis,
                 aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}

(function (global, factory) { 
[...]

And then the function isNil

function isNil(value) {
    return value === undefined || value === null || value === '';
}

Now it works.

panzerdp commented 6 years ago

@Kreider186, Please show the code you're running and what was the error message. Thanks.

Kreider186 commented 6 years ago

@panzerdp I was running the voca.js build (directly into browser's web page) and there wasn't any error messages, simply it didn't work at all (with internet explorer, it works perfecly with chrome).

Inspecting the code with explorer's debugger I found that many functions were not supported. So I add the functions above in order to overwrite non-supported functions.

Once overwritten the unsupported functions, the code was still not working and again no exceptions/errors were thrown.

Then, I figured out that here:

ReplacementIndex.prototype.getIndexByPosition = function (position) {
  return isNil(position) ? this.index : position - 1;
};

the function was returning -1 because in explorer the value of position wasn't undefined nor null but simply nothing ('').

So I edited the isNil function this way:

function isNil(value) { return value === undefined || value === null || value === ''; }

And now it works.

panzerdp commented 6 years ago

Please tell me what exact voca function doesnt work in IE. I took a try and everything is ok on my side. Thanks.

Sent from my iPhone

On Feb 8, 2018, at 21:41, Kreider186 notifications@github.com wrote:

@panzerdp I was running the voca.js build (directly into browser's web page) and there wasn't any error messages, simply it didn't work at all (with internet explorer, it works perfecly with chrome).

Inspecting the code with explorer's debugger I found that many functions were not supported. So I add the functions above in order to overwrite non-supported functions.

Once overwritten the unsupported functions, the code was still not working and again no exceptions/errors were thrown.

Then, I figured out that here:

ReplacementIndex.prototype.getIndexByPosition = function (position) { return isNil(position) ? this.index : position - 1; }; the function was returning -1 because in explorer the value of position wasn't undefined nor null but simply nothing ('').

So I edited the isNil function this way:

function isNil(value) { return value === undefined || value === null || value === ''; }

And now it works.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Kreider186 commented 6 years ago

@panzerdp I've made a pdf with some screenshot and the description of what happened step by step. Hope it helps.

p.s.: the 3rd error was triggered by my my script because I was already replacing some unsupported function (objext.keys and object.values) but I removed the hack for the screenshot report. screenshots.pdf

panzerdp commented 6 years ago

@Kreider186, The problem is in meta tag <meta http-equiv="X-UA-Compatible" content="IE=8"> that makes the browser work in IE 8 mode (which does not support Object.keys(), func.bind(), etc).

Voca however supports IE 9 and above.

Try to remove this meta tag and see if it works.

Kreider186 commented 6 years ago

@panzerdp Nhope, that doesn't solve the issue. But I tried to replace the meta tag with:

<meta http-equiv="X-UA-Compatible" content="IE=edge">

And at least the last error disappeard (the one on isNil function). Ill'do some other research.

Kreider186 commented 6 years ago

@panzerdp Mmm... what the heck. I've no idea of what happened but today it works. Putting the right meta did the trick and now everything works. Thank you!

panzerdp commented 6 years ago

You're welcome.