Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4854: visible.diff

File visible.diff, 2.2 kB (added by Tobie, 3 years ago)

js + test + benchmarks

  • test/unit/dom.html

    old new  
    1717    #not_floating_style { float: none } 
    1818    #floating_style { float: left } 
    1919    #op2 { opacity:0.5;filter:alpha(opacity=50)progid:DXImageTransform.Microsoft.Blur(strength=10);} 
     20    #hidden_element {display: none;} 
    2021 
    2122  /* ]]> */ 
    2223  </style> 
     
    3031<!-- Log output --> 
    3132<div id="testlog"> </div> 
    3233 
     34<span id="visible_element"> 
     35  visible 
     36</span> 
     37<div id="hidden_element_inline_style" style="display: none;"> 
     38  hidden 
     39</div> 
     40<div id="hidden_element"> 
     41  hidden 
     42  <div id="child_of_a_hidden_element"> 
     43    parent hidden 
     44    <div id="grandchild_of_a_hidden_element"> 
     45      grandparent hidden 
     46    </div> 
     47  </div> 
     48</div> 
     49 
    3350<div> 
    3451  <table> 
    3552    <tbody id="table"> 
     
    153170      assertIdentical(elt, $(elt)); 
    154171      assertRespondsTo('hide', elt); 
    155172    }}, 
     173 
     174    testVisible: function() {with(this) { 
     175      assert($('visible_element').visible()); 
     176      assert(!$('hidden_element_inline_style').visible()); 
     177      assert(!$('hidden_element').visible()); 
     178      assert(!$('child_of_a_hidden_element').visible()); 
     179      assert(!$('grandchild_of_a_hidden_element').visible()); 
     180      benchmark(function(){ 
     181        $('visible_element').visible(); 
     182      },1000); 
     183      benchmark(function(){ 
     184        $('visible_element').style.display != 'none'; 
     185      },1000); 
     186    }}, 
    156187     
    157188    testGetElementsByClassName: function() {with(this) { 
    158189      assertElementsMatch(document.getElementsByClassName('A'), 'p.A', 'ul#class_names_ul.A', 'li.A.C'); 
  • src/dom.js

    old new  
    7575 
    7676Element.Methods = { 
    7777  visible: function(element) { 
    78     return $(element).style.display != 'none'; 
     78    element = $(element); 
     79    var style = element.getStyle('display'); 
     80    if(style == 'none' || style == null) return false; 
     81    if(element.ancestors().any(function(ancestor) { 
     82      return ancestor.getStyle('display') == 'none'; 
     83    })) return false; 
     84    return true; 
    7985  }, 
    8086   
    8187  toggle: function(element) {