Merge pull request #201 from alice/bodyScroll
Don't need to take container scroll into account if container is body
diff --git a/Changelog.md b/Changelog.md
index 604fe5d..2d1ebcf 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -14,6 +14,7 @@
* Revert #150 which was causing the extension not to work.
* AX_HTML_02 (duplicate IDs) now only audits elements that are referenced by an IDREF (#141);
* Fix #171 by being smarter about finding the composed parent node.
+* Tweak in canScrollTo to handle the (common) case where the container is `document.body` (#243).
## 2.9.0 - 2015-09-04
@@ -127,4 +128,4 @@
* axs.utils.isLowContrast() now rounds to the nearest 0.1 before checking (so `#777` is now a passing value)
* MainRoleOnInappropriateElement was always failing due to accessible name calculation taking the main role into account and not descending into content (now just gets descendant content directly)
-* UnfocusableElementsWithOnClick had a dangling if-statement causing very noisy false positives
\ No newline at end of file
+* UnfocusableElementsWithOnClick had a dangling if-statement causing very noisy false positives
diff --git a/src/js/AccessibilityUtils.js b/src/js/AccessibilityUtils.js
index 3466133..657348a 100644
--- a/src/js/AccessibilityUtils.js
+++ b/src/js/AccessibilityUtils.js
@@ -103,16 +103,21 @@
axs.utils.canScrollTo = function(element, container) {
var rect = element.getBoundingClientRect();
var containerRect = container.getBoundingClientRect();
- var containerTop = containerRect.top;
- var containerLeft = containerRect.left;
+ if (container == container.ownerDocument.body) {
+ var absoluteTop = containerRect.top;
+ var absoluteLeft = containerRect.left;
+ } else {
+ var absoluteTop = containerRect.top - container.scrollTop;
+ var absoluteLeft = containerRect.left - container.scrollLeft;
+ }
var containerScrollArea =
- { top: containerTop - container.scrollTop,
- bottom: containerTop - container.scrollTop + container.scrollHeight,
- left: containerLeft - container.scrollLeft,
- right: containerLeft - container.scrollLeft + container.scrollWidth };
+ { top: absoluteTop,
+ bottom: absoluteTop + container.scrollHeight,
+ left: absoluteLeft,
+ right: absoluteLeft + container.scrollWidth };
if (rect.right < containerScrollArea.left || rect.bottom < containerScrollArea.top ||
- rect.left > containerScrollArea.right || rect.top > containerScrollArea.bottom) {
+ rect.left > containerScrollArea.right || rect.top > containerScrollArea.bottom) {
return false;
}