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(rikaicontent): Properly check for text areas and input elements when deciding to save cursor information on mousedown #1282

Merged
merged 3 commits into from
Nov 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
9 changes: 6 additions & 3 deletions extension/rikaicontent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,13 @@ class RcxContent {
// oldCaret to -1 as an indicator not to restore position
// Otherwise, we switch our saved textarea to whereever
// we just clicked
if (!('form' in ev.target!)) {
window.rikaichan!.oldCaret = -1;
} else {
if (
ev.target instanceof HTMLTextAreaElement ||
ev.target instanceof HTMLInputElement
) {
window.rikaichan!.oldTA = ev.target;
} else {
window.rikaichan!.oldCaret = -1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion extension/test/data_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('data.ts', function () {
// Increase timeout from 2000ms since data tests can take longer.
// Make it relative to current timeout so config level changes are taken
// into account. (ie browserstack)
this.timeout(this.timeout() * 2);
this.timeout(this.timeout() * 3);
before(async function () {
// stub sinon chrome getURL method to return the path it's given
// Required to load dictionary files.
Expand Down
40 changes: 40 additions & 0 deletions extension/test/rikaicontent_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,38 @@ describe('RcxContent', function () {
});
});

describe('mouseDown', function () {
it('clears oldCaret value when clicking outside text box', function () {
const span = insertHtmlIntoDomAndReturnFirstTextNode(
'<span>testtest</span>'
) as HTMLSpanElement;

triggerLeftMouseDownAtElementStart(span);

expect(window.rikaichan?.oldCaret).to.equal(-1);
});

it('saves text box element in `oldTA` when clicking inside a text area', function () {
const textarea = insertHtmlIntoDomAndReturnFirstTextNode(
'<textarea>testtest</textarea>'
) as HTMLTextAreaElement;

triggerLeftMouseDownAtElementStart(textarea);

expect(window.rikaichan?.oldTA).to.equal(textarea);
});

it('saves text box element in `oldTA` when clicking inside an input box', function () {
const input = insertHtmlIntoDomAndReturnFirstTextNode(
'<input>testtest</input>'
) as HTMLInputElement;

triggerLeftMouseDownAtElementStart(input);

expect(window.rikaichan?.oldTA).to.equal(input);
});
});

describe('processEntry', function () {
describe('when highlight config option enabled', function () {
it('does not try to highlight text in Google Docs', function () {
Expand Down Expand Up @@ -1025,6 +1057,14 @@ function triggerMousemoveAtElementStartWithOffset(
});
}

function triggerLeftMouseDownAtElementStart(element: Element) {
simulant.fire(element, 'mousedown', {
button: 0,
clientX: Math.ceil(element.getBoundingClientRect().left),
clientY: Math.ceil(element.getBoundingClientRect().top),
});
}

function insertHtmlIntoDomAndReturnFirstTextNode(htmlString: string): Node {
const template = document.createElement('template');
template.innerHTML = htmlString;
Expand Down