-
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 mo…
…use (#561) 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: @web/test-runner + Mocha/Chai/Sinon + Puppeteer Headless Chrome. Works with vscode debugging for fast test driven development. With --coverage flag, we get a coverage report that we can upload to codecov. Fixes #366 Fixes #159
- Loading branch information
Showing
9 changed files
with
2,141 additions
and
134 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,3 +1,4 @@ | ||
node_modules | ||
dist | ||
.vscode | ||
coverage |
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,2 @@ | ||
import * as sinonChrome from 'sinon-chrome'; | ||
window.chrome = sinonChrome as unknown as typeof 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,81 @@ | ||
import { TestOnlyRcxContent } from '../rikaicontent'; | ||
import { expect, use } from '@esm-bundle/chai'; | ||
import chrome from 'sinon-chrome'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
use(sinonChai); | ||
|
||
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).to.have.been.calledWith( | ||
sinon.match({ type: 'xsearch', text: '試す' }), | ||
sinon.match.any | ||
); | ||
}); | ||
}); | ||
|
||
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).to.have.been.calledWith( | ||
sinon.match({ type: 'xsearch', text: '試す' }), | ||
sinon.match.any | ||
); | ||
}); | ||
}); | ||
|
||
describe('when given Japanese word is interrupted with text wrapped by visible span', () => { | ||
it('sends "xsearch" message with all text included', () => { | ||
const rcxContent = new TestOnlyRcxContent(); | ||
const span = insertHtmlIntoDomAndReturnFirstTextNode( | ||
'<span>試<span>test</span>す</span>' | ||
); | ||
|
||
executeShowForGivenNode(rcxContent, span); | ||
|
||
expect(chrome.runtime.sendMessage).to.have.been.calledWith( | ||
sinon.match({ type: 'xsearch', text: '試testす' }), | ||
sinon.match.any | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
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 | ||
); | ||
} |
Oops, something went wrong.