Skip to content

Commit 5ebc1aa

Browse files
committed
Merge pull request philc#945 from shyiko/master
Fixed detection of links which are only partially inside the viewport
2 parents 366c0a5 + 004f9a1 commit 5ebc1aa

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/dom_utils.coffee

+13-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,21 @@ DomUtils =
4646
#
4747
getVisibleClientRect: (element) ->
4848
# Note: this call will be expensive if we modify the DOM in between calls.
49-
clientRects = element.getClientRects()
49+
clientRects = ({
50+
top: clientRect.top, right: clientRect.right, bottom: clientRect.bottom, left: clientRect.left,
51+
width: clientRect.width, height: clientRect.height
52+
} for clientRect in element.getClientRects())
5053

5154
for clientRect in clientRects
52-
if (clientRect.top < -2 || clientRect.top >= window.innerHeight - 4 ||
53-
clientRect.left < -2 || clientRect.left >= window.innerWidth - 4)
55+
if (clientRect.top < 0)
56+
clientRect.height += clientRect.top
57+
clientRect.top = 0
58+
59+
if (clientRect.left < 0)
60+
clientRect.width += clientRect.left
61+
clientRect.left = 0
62+
63+
if (clientRect.top >= window.innerHeight - 4 || clientRect.left >= window.innerWidth - 4)
5464
continue
5565

5666
if (clientRect.width < 3 || clientRect.height < 3)

tests/dom_tests/dom_utils_test.coffee

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ context "Check visibility",
4242
assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo'
4343
assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'bar'
4444

45+
should "detect links only partially outside viewport as visible", ->
46+
document.getElementById("test-div").innerHTML = """
47+
<a id='foo' style='position:absolute;top:-10px'>test</a>
48+
<a id='bar' style='position:absolute;left:-10px'>test</a>
49+
"""
50+
assert.isTrue (DomUtils.getVisibleClientRect document.getElementById 'foo') != null
51+
assert.isTrue (DomUtils.getVisibleClientRect document.getElementById 'bar') != null
52+
4553
should "detect opacity:0 links as hidden", ->
4654
document.getElementById("test-div").innerHTML = """
4755
<a id='foo' style='opacity:0'>test</a>

tests/unit_tests/utils_test.coffee

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require "./test_helper.js"
22
extend(global, require "../../lib/utils.js")
3+
Utils.getCurrentVersion = -> '1.43'
4+
global.localStorage = {}
35
extend(global, require "../../background_scripts/settings.js")
46

57
context "isUrl",

0 commit comments

Comments
 (0)