diff options
| author | Jez Ng | 2012-01-14 04:38:50 +0800 | 
|---|---|---|
| committer | Jez Ng | 2012-01-15 14:48:11 +0800 | 
| commit | 4ca60a80f0e3d5c4818d05417c869bde6340d792 (patch) | |
| tree | 00907902cdcade26e5dc7f422f3b21f9cd1f6fc7 /lib | |
| parent | e84617a1512dbfdba685c3dccf5f82ae0ddc60e8 (diff) | |
| download | vimium-4ca60a80f0e3d5c4818d05417c869bde6340d792.tar.bz2 | |
Scroll the body element if last activated node is not visible.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/utils.js | 44 | 
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js index ef961833..0c86970e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -30,4 +30,48 @@ var utils = {      }      return document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null);    }, + +  /** +   * Returns the first visible clientRect of an element if it exists. Otherwise it returns null. +   */ +  getVisibleClientRect: function(element) { +    // Note: this call will be expensive if we modify the DOM in between calls. +    var clientRects = element.getClientRects(); +    var clientRectsLength = clientRects.length; + +    for (var i = 0; i < clientRectsLength; i++) { +      if (clientRects[i].top < 0 || clientRects[i].top >= window.innerHeight - 4 || +          clientRects[i].left < 0 || clientRects[i].left  >= window.innerWidth - 4) +        continue; + +      if (clientRects[i].width < 3 || clientRects[i].height < 3) +        continue; + +      // eliminate invisible elements (see test_harnesses/visibility_test.html) +      var computedStyle = window.getComputedStyle(element, null); +      if (computedStyle.getPropertyValue('visibility') != 'visible' || +          computedStyle.getPropertyValue('display') == 'none') +        continue; + +      return clientRects[i]; +    } + +    for (var i = 0; i < clientRectsLength; i++) { +      // If the link has zero dimensions, it may be wrapping visible +      // but floated elements. Check for this. +      if (clientRects[i].width == 0 || clientRects[i].height == 0) { +        for (var j = 0, childrenCount = element.children.length; j < childrenCount; j++) { +          var computedStyle = window.getComputedStyle(element.children[j], null); +          // Ignore child elements which are not floated and not absolutely positioned for parent elements with zero width/height +          if (computedStyle.getPropertyValue('float') == 'none' && computedStyle.getPropertyValue('position') != 'absolute') +            continue; +          var childClientRect = this.getVisibleClientRect(element.children[j]); +          if (childClientRect === null) +            continue; +          return childClientRect; +        } +      } +    }; +    return null; +  },  };  | 
