Skip to content

Commit

Permalink
fix: Remove fake div if returning early because caretRangeFromPoint
Browse files Browse the repository at this point in the history
… is null (#1134)

Fixes #1114
  • Loading branch information
Stephie authored Jul 17, 2022
1 parent 1b58e6a commit e7d40b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions extension/rikaicontent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,9 @@ class RcxContent {
range = document.caretRangeFromPoint(ev.clientX, ev.clientY);
// If we don't have a valid range, don't do any more work
if (range === null) {
if (fake) {
document.body.removeChild(fake);
}
return;
}
const startNode = range.startContainer;
Expand Down
23 changes: 23 additions & 0 deletions extension/test/rikaicontent_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ describe('RcxContent', function () {
expect(console.log).to.not.have.been.called;
});

it('removes the fake div of an input element when `caretRangeFromPoint` returns null', function () {
sinon
.stub(document, 'caretRangeFromPoint')
.returns(null as unknown as Range);
const inputValue = '66';
const input = insertHtmlIntoDomAndReturnFirstTextNode(
`<input type="range" value="${inputValue}">`
) as HTMLInputElement;

triggerMousemoveAtElementStart(input);

expect(doesDocumentContainDivWithText(inputValue)).to.be.false;
});

it('triggers xsearch message when above Japanese text', function () {
const clock = sinon.useFakeTimers();
const span = insertHtmlIntoDomAndReturnFirstTextNode(
Expand Down Expand Up @@ -926,6 +940,15 @@ describe('RcxContent', function () {
});
});

function doesDocumentContainDivWithText(text: string): boolean {
return document.evaluate(
`//div[text()="${text}"]`,
document,
null,
XPathResult.BOOLEAN_TYPE
).booleanValue;
}

// Required if testing downstream methods which expect a proper hover event to have
// already been processed.
function seedRcxContentWithNormalMouseMove() {
Expand Down

0 comments on commit e7d40b8

Please sign in to comment.