-
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`. This change is also the first unit tested change so contains the initial configuration of testing. (See #159 for details on decisions). Testing Stack: Jasmine + Karma + Headless Chrome. Works with vscode debugging for fast test driven development. Unrelated changes required for compilation: - options.ts my LitToast interface needed to extend `Element` when compiling with karma and tsc. Not sure why it worked with `ts-loader` - I needed to update webpack.config to accept a partial argv since of course we are only using `mode`. Fixes #366 Fixes #159
- Loading branch information
Showing
10 changed files
with
1,954 additions
and
23 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
.vscode |
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
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 | ||
); | ||
} |
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,53 @@ | ||
import { Config, ConfigOptions } from 'karma'; | ||
import { Configuration } from 'webpack'; | ||
import webpackConfig from './webpack.config'; | ||
|
||
// Allows me to use type safe configuration with webpack plugin options. | ||
interface PluggedConfig extends Omit<Config, 'set'> { | ||
set: (config: ConfigOptions & { webpack: Configuration }) => void; | ||
} | ||
|
||
// Load production webpack config for sharing values with test config. | ||
const webpackDev = webpackConfig({}, { mode: 'development' }); | ||
module.exports = (config: PluggedConfig) => { | ||
config.set({ | ||
frameworks: ['jasmine', 'webpack'], | ||
// Load chrome.* API spies early for top level calls. | ||
files: ['extension/test/chrome_stubs.ts', 'extension/test/**/*.ts'], | ||
preprocessors: { | ||
// Typescript files need to be compiled by webpack and then sourcemapped. | ||
'**/*.ts': ['webpack', 'sourcemap'], | ||
}, | ||
reporters: ['spec'], // Prints status based on Jasmine specs. | ||
port: 9876, // karma web server port | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
browsers: ['ChromeHeadlessDebug'], | ||
customLaunchers: { | ||
ChromeHeadlessDebug: { | ||
base: 'ChromeHeadless', | ||
flags: ['--remote-debugging-port=9333'], | ||
}, | ||
}, | ||
// By default, use autowatch to re-run tests on changes. | ||
autoWatch: true, | ||
concurrency: Infinity, | ||
webpack: { | ||
module: webpackDev.module, | ||
mode: 'development', | ||
resolve: webpackDev.resolve, | ||
devtool: 'inline-source-map', | ||
output: { | ||
module: true, | ||
// Outputs absolute file paths instead of webpack:///path/to/file.extension | ||
// This makes stacktrace paths clickable in a shell (e.g. VS Code) | ||
devtoolModuleFilenameTemplate: '[absolute-resource-path]', | ||
}, | ||
experiments: webpackDev.experiments, | ||
// Needed for https://github.com/ryanclark/karma-webpack/issues/493 | ||
optimization: { | ||
splitChunks: false, | ||
}, | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.