andreruffert / rangeslider.js

🎚 HTML5 input range slider element jQuery polyfill
https://rangeslider.js.org
MIT License
2.16k stars 401 forks source link

isHidden does not have a null check #136

Closed mvantassel closed 9 years ago

mvantassel commented 9 years ago

TypeError: 'null' is not an object (evaluating 'element.offsetWidth')

andreruffert commented 9 years ago

@mvantassel thanks for reporting! When exactly does this error show up?

mvantassel commented 9 years ago

Hey, I'm seeing it in my unit tests for my implementation. The view that contains the range slider is rendered and available when instantiating but for some reason is null when it gets to that method. I'll get you a stack trace soon. AFK Thanks!

mvantassel commented 9 years ago

Hey,

Here is some more information. Unfortunately I can't show you the implementation but know that the element that the jquery plugin is attached to does exist in the dom.

Uncaught TypeError: Cannot read property 'offsetWidth' of null
  isHidden  
  getHiddenParentNodes  
  getDimension  
  Plugin.update 
  Plugin.init   
  Plugin    

in getHiddenParentNodes, ‘element’ is:

div.rangeslider__handle

when the tests pass ‘element’ is:

<div class="rangeslider" id="js-rangeslider-9" style=""><div class="rangeslider__fill"></div><div class="rangeslider__handle"></div></div>

This looks to be a race condition in rangeslider when it builds the component

samhavens commented 9 years ago

I ran across this as well, same error message, and same element causing the problem in getHiddenParentNodes. For me, it happened when I created a slider with value of zero, as in this line when attribute['weight'] = 0:

return '<div class="round-rect"><input class="slider" id="slider' + attribute['id'] + '" type="range" min="0" max="10" value="' + attribute['weight'] + '" step="0.1" /></div>';

my fix was changing the isHidden function:

    function isHidden(element) {
        if (//sh - added 4/17
            !element) {
            return false;
        };
        if (element.offsetWidth === 0 ||
            element.offsetHeight === 0 ||
            // Also Consider native `<details>` elements.
            element.open === false) {
            return true;
        }
        return false;
    }
andreruffert commented 9 years ago

Hey guys @mvantassel @samhavens pr #144 should fix this issue. Can you pls give me a feedback? Cheers! :v:

samhavens commented 9 years ago

@andreruffert Looks good to me! :+1:

andreruffert commented 9 years ago

Madd! Thanks for the double check! :ok_hand: Will be in the next Release v1.2.1

mvantassel commented 9 years ago

Works perfect. That's the change I added to my fork. Thanks @andreruffert !