Skip to content

Commit 1a504b3

Browse files
author
Pooja0504
committed
Updated get-visible-child-text-rects
1 parent 26f1d78 commit 1a504b3

File tree

1 file changed

+41
-74
lines changed

1 file changed

+41
-74
lines changed

lib/commons/dom/get-visible-child-text-rects.js

Lines changed: 41 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getNodeFromTree } from '../../core/utils';
1+
import { getNodeFromTree, memoize } from '../../core/utils';
22
import { sanitize } from '../text';
33
import { getIntersectionRect, getRectCenter, isPointInRect } from '../math';
44
import getOverflowHiddenAncestors from './get-overflow-hidden-ancestors';
@@ -11,72 +11,49 @@ import cache from '../../core/base/cache';
1111
* @instance
1212
* @param {Element} node
1313
*/
14-
const getVisibleChildTextRects = (node, options = {}) => {
15-
const {
16-
checkTextRectOutsideNodeBoundingRect = false,
17-
includeOutsideBounds = true,
18-
includeOverflowHidden = false,
19-
checkNoVisibleRectsIdentified = false
20-
} = options;
21-
const vNode = getNodeFromTree(node);
22-
const nodeRect = vNode.boundingClientRect;
23-
const clientRects = [];
24-
const overflowHiddenNodes = getOverflowHiddenAncestors(vNode);
14+
const getVisibleChildTextRects = memoize(
15+
function getVisibleChildTextRectsMemoized(node) {
16+
const vNode = getNodeFromTree(node);
17+
const nodeRect = vNode.boundingClientRect;
18+
const clientRects = [];
19+
const overflowHiddenNodes = getOverflowHiddenAncestors(vNode);
2520

26-
node.childNodes.forEach(textNode => {
27-
if (textNode.nodeType !== 3 || sanitize(textNode.nodeValue) === '') {
28-
return;
29-
}
21+
node.childNodes.forEach(textNode => {
22+
if (textNode.nodeType !== 3 || sanitize(textNode.nodeValue) === '') {
23+
return;
24+
}
3025

31-
const contentRects = getContentRects(textNode);
32-
if (
33-
includeOutsideBounds &&
34-
isOutsideNodeBounds(
35-
contentRects,
36-
nodeRect,
37-
checkTextRectOutsideNodeBoundingRect
38-
) &&
39-
(!cache.get('ruleId') ||
40-
cache.get('ruleId') === 'reflow-4x-zoom-scroll' ||
41-
cache.get('ruleId') === 'color-contrast')
42-
) {
43-
return;
44-
}
26+
const contentRects = getContentRects(textNode);
27+
if (isOutsideNodeBounds(contentRects, nodeRect) && !cache.get('ruleId')) {
28+
return;
29+
}
4530

46-
clientRects.push(
47-
...filterHiddenRects(
48-
contentRects,
49-
includeOverflowHidden ? [] : overflowHiddenNodes
50-
)
51-
);
52-
});
31+
clientRects.push(...filterHiddenRects(contentRects, overflowHiddenNodes));
32+
});
5333

54-
// a11y-engine-domforge change
55-
if (
56-
clientRects.length <= 0 &&
57-
((cache.get('ruleId') && cache.get('ruleId') === 'resize-2x-zoom') ||
58-
checkNoVisibleRectsIdentified)
59-
) {
60-
return [];
34+
// a11y-engine-domforge change
35+
if (clientRects.length <= 0) {
36+
return [];
37+
}
38+
/**
39+
* if all text rects are larger than the bounds of the node,
40+
* or goes outside of the bounds of the node, we need to use
41+
* the nodes bounding rect so we stay within the bounds of the
42+
* element.
43+
*
44+
* @see https://github.com/dequelabs/axe-core/issues/2178
45+
* @see https://github.com/dequelabs/axe-core/issues/2483
46+
* @see https://github.com/dequelabs/axe-core/issues/2681
47+
*
48+
* also need to resize the nodeRect to fit within the bounds of any overflow: hidden ancestors.
49+
*
50+
* @see https://github.com/dequelabs/axe-core/issues/4253
51+
*/
52+
return clientRects.length
53+
? clientRects
54+
: filterHiddenRects([nodeRect], overflowHiddenNodes);
6155
}
62-
/**
63-
* if all text rects are larger than the bounds of the node,
64-
* or goes outside of the bounds of the node, we need to use
65-
* the nodes bounding rect so we stay within the bounds of the
66-
* element.
67-
*
68-
* @see https://github.com/dequelabs/axe-core/issues/2178
69-
* @see https://github.com/dequelabs/axe-core/issues/2483
70-
* @see https://github.com/dequelabs/axe-core/issues/2681
71-
*
72-
* also need to resize the nodeRect to fit within the bounds of any overflow: hidden ancestors.
73-
*
74-
* @see https://github.com/dequelabs/axe-core/issues/4253
75-
*/
76-
return clientRects.length
77-
? clientRects
78-
: filterHiddenRects([nodeRect], overflowHiddenNodes);
79-
};
56+
);
8057
export default getVisibleChildTextRects;
8158

8259
function getContentRects(node) {
@@ -91,20 +68,10 @@ function getContentRects(node) {
9168
* when determining the rect stack we will also use the midpoint
9269
* of the text rect to determine out of bounds
9370
*/
94-
function isOutsideNodeBounds(
95-
rects,
96-
nodeRect,
97-
checkTextRectOutsideNodeBoundingRect = false
98-
) {
71+
function isOutsideNodeBounds(rects, nodeRect) {
9972
return rects.some(rect => {
10073
const centerPoint = getRectCenter(rect);
101-
if (checkTextRectOutsideNodeBoundingRect) {
102-
return (
103-
!isPointInRect(centerPoint, nodeRect) || rect.right > nodeRect.right
104-
);
105-
} else {
106-
return !isPointInRect(centerPoint, nodeRect);
107-
}
74+
return !isPointInRect(centerPoint, nodeRect);
10875
});
10976
}
11077

0 commit comments

Comments
 (0)