Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove fake div if returning early because caretRangeFromPoint is null #1134

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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