Skip to content

Commit eed58b0

Browse files
authored
Merge pull request #178 from dmitri-gb/fix-detection-in-zero-size-elements
Detect text fragments in elements with 0 width/height and visible overflow
2 parents 0c9512f + dc5583c commit eed58b0

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/text-fragment-utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,10 @@ const isNodeVisible =
593593
const nodeStyle = window.getComputedStyle(elt);
594594
// If the node is not rendered, just skip it.
595595
if (nodeStyle.visibility === 'hidden' || nodeStyle.display === 'none' ||
596-
parseInt(nodeStyle.height, 10) === 0 ||
597-
parseInt(nodeStyle.width, 10) === 0 ||
596+
parseInt(nodeStyle.height, 10) === 0 &&
597+
nodeStyle.overflowY != 'visible' ||
598+
parseInt(nodeStyle.width, 10) === 0 &&
599+
nodeStyle.overflowX != 'visible' ||
598600
parseInt(nodeStyle.opacity, 10) === 0) {
599601
return false;
600602
}

test/unit/hidden-overflow.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
<p style="float: left">Target text-fragment 1</p>
3+
</div>
4+
<div style="overflow: hidden; height: 0">
5+
<p>Target text-fragment 2</p>
6+
</div>
7+
<div style="overflow: clip">
8+
<p style="float: left">Target text-fragment 3</p>
9+
</div>

test/unit/text-fragment-utils-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,4 +1138,35 @@ describe('TextFragmentUtils', function() {
11381138
utils.forTesting.acceptTextNodeIfVisibleInRange(divNode, range);
11391139
expect(filterOutput).toEqual(NodeFilter.FILTER_REJECT);
11401140
});
1141+
1142+
it('Ignores zero-width/height elements with hidden overflow', function() {
1143+
document.body.innerHTML = __html__['hidden-overflow.html'];
1144+
{
1145+
const directives = utils.getFragmentDirectives(
1146+
'#:~:text=Target%20text-fragment%201',
1147+
);
1148+
const parsedDirectives = utils.parseFragmentDirectives(directives);
1149+
const processedDirectives =
1150+
utils.processFragmentDirectives(parsedDirectives)['text'];
1151+
expect(processedDirectives[0].length).toBe(1);
1152+
}
1153+
{
1154+
const directives = utils.getFragmentDirectives(
1155+
'#:~:text=Target%20text-fragment%202',
1156+
);
1157+
const parsedDirectives = utils.parseFragmentDirectives(directives);
1158+
const processedDirectives =
1159+
utils.processFragmentDirectives(parsedDirectives)['text'];
1160+
expect(processedDirectives[0].length).toBe(0);
1161+
}
1162+
{
1163+
const directives = utils.getFragmentDirectives(
1164+
'#:~:text=Target%20text-fragment%203',
1165+
);
1166+
const parsedDirectives = utils.parseFragmentDirectives(directives);
1167+
const processedDirectives =
1168+
utils.processFragmentDirectives(parsedDirectives)['text'];
1169+
expect(processedDirectives[0].length).toBe(0);
1170+
}
1171+
});
11411172
});

0 commit comments

Comments
 (0)