-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(detection): Ignore invisible nodes when extracting text under mouse
Specifically, ignore `display: none` and `visibility: hidden`. Fixes #366
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
type DeepPartial<T> = { | ||
[P in keyof T]?: DeepPartial<T[P]>; | ||
}; | ||
|
||
const chromeSpy: DeepPartial<typeof chrome> & { reset: Function } = { | ||
// Creates the partial structure of chrome web APIs rikaikun tests need. | ||
runtime: { | ||
sendMessage: () => {}, | ||
onMessage: { | ||
addListener: () => {}, | ||
}, | ||
}, | ||
|
||
/** | ||
* Set's named spies for each method defined above and allows state to be | ||
* easily reset between test methods. | ||
*/ | ||
reset() { | ||
chromeSpy.runtime!.sendMessage = jasmine.createSpy( | ||
'chrome.runtime.sendMessage' | ||
); | ||
chromeSpy.runtime!.onMessage!.addListener = jasmine.createSpy( | ||
'chrome.runtime.onMessage.addListener' | ||
); | ||
}, | ||
}; | ||
|
||
chromeSpy.reset(); | ||
window.chrome = chromeSpy as unknown as typeof chrome; | ||
const hardenedChrome = chromeSpy as Required<typeof chromeSpy>; | ||
|
||
export { hardenedChrome as chrome }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// import 'jasmine-sinon'; | ||
// import * as sinonChrome from 'sinon-chrome'; | ||
import { TestOnlyRcxContent } from '../rikaicontent'; | ||
import { chrome } from './chrome_stubs'; | ||
|
||
// declare const chrome: Required<typeof chromeSpy>; | ||
|
||
describe('RcxContent.show', () => { | ||
beforeEach(() => { | ||
chrome.reset(); | ||
}); | ||
|
||
describe('when given Japanese word interrupted with text wrapped by `display: none`', () => { | ||
it('sends "xsearch" message with invisible text omitted', () => { | ||
const rcxContent = new TestOnlyRcxContent(); | ||
const span = insertHtmlIntoDomAndReturnFirstTextNode( | ||
'<span>試<span style="display:none">test</span>す</span>' | ||
); | ||
|
||
executeShowForGivenNode(rcxContent, span); | ||
|
||
expect(chrome.runtime.sendMessage).toHaveBeenCalledWith( | ||
jasmine.objectContaining({ type: 'xsearch', text: '試す' }), | ||
jasmine.any(Function) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when given Japanese word interrupted with text wrapped by `visibility: hidden`', () => { | ||
it('sends "xsearch" message with invisible text omitted', () => { | ||
const rcxContent = new TestOnlyRcxContent(); | ||
const span = insertHtmlIntoDomAndReturnFirstTextNode( | ||
'<span>試<span style="visibility: hidden">test</span>す</span>' | ||
); | ||
|
||
executeShowForGivenNode(rcxContent, span); | ||
|
||
expect(chrome.runtime.sendMessage).toHaveBeenCalledWith( | ||
jasmine.objectContaining({ type: 'xsearch', text: '試す' }), | ||
jasmine.any(Function) | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
function insertHtmlIntoDomAndReturnFirstTextNode(htmlString: string): Node { | ||
const template = document.createElement('template'); | ||
template.innerHTML = htmlString; | ||
return document.body.appendChild(template.content.firstChild!); | ||
} | ||
|
||
function executeShowForGivenNode( | ||
rcxContent: TestOnlyRcxContent, | ||
node: Node | ||
): void { | ||
rcxContent.show( | ||
{ | ||
prevRangeNode: rcxContent.getFirstTextChild(node) as Text, | ||
prevRangeOfs: 0, | ||
uofs: 0, | ||
}, | ||
0 | ||
); | ||
} |