// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
FOUND_CLASSES,
GenericSearchProvider,
HTMLSearchEngine
} from '@jupyterlab/documentsearch';
import { Widget } from '@lumino/widgets';
const MATCH_CLASSES = FOUND_CLASSES.join(' ');
function mark(text: string): string {
return `${text}`;
}
/**
* The DOM of `$x = 10$` typeset by MathJax: a text node per character, a line
* break opportunity after x, and the hidden MathML copy.
*/
function formula(x = '𝑥', equals = '=', one = '1', zero = '0'): string {
return (
'
${formula()}
`; const matches = await HTMLSearchEngine.search(/10/g, node); expect( matches.map(match => match.parts?.map(part => part.text)) ).toEqual([['1', '0']]); }); }); }); describe('GenericSearchProvider', () => { let provider: GenericSearchProvider; let widget: Widget; beforeEach(() => { widget = new Widget(); provider = new GenericSearchProvider(widget); }); afterEach(async () => { widget.dispose(); }); describe('#preserveCase()', () => { it.each([ ['OLD_TEXT_1', 'new_text_1', 'NEW_TEXT_1'], ['OLD_TEXT_1', 'NEW_TEXT_1', 'NEW_TEXT_1'], ['old_text_1', 'new_text_1', 'new_text_1'], ['old_text_1', 'NEW_TEXT_1', 'new_text_1'], ['Old', 'new', 'New'], ['Old', '𐐶ew', '𐐎ew'] ])( 'should copy case from %s to %s yielding %s', (oldText, newText, expected) => { const replace = GenericSearchProvider.preserveCase(oldText, newText); expect(replace).toEqual(expected); } ); }); describe('#startQuery()', () => { it.each([ [/x/, 'xyz', `xyz`], [/y/, '
xyz', `xyz`], [/z/, '
xyz', `xyz`], [ /x/, '
xyz',
`xyz`
],
[
/y/,
'xyz',
`xyz`
],
[
/z/,
'xyz',
`xyz`
],
[
/x/,
'xyz',
`xyz`
],
[
/y/,
'xyz',
`xyz`
],
[
/z/,
'xyz',
`xyz`
],
[
/x/,
'xyz',
`xyz`
],
[
/y/,
'xyz',
`xyz`
],
[
/z/,
'xyz',
`xyz`
]
])(
'should highlight %s fragment in %s',
async (query, content, expected) => {
widget.node.innerHTML = content;
await provider.startQuery(query);
expect(widget.node.firstElementChild!.innerHTML).toEqual(expected);
}
);
it.each([
[/z/, ''],
[/y/, ''],
[/x/, ''],
[
/1/,
'' ] ])( 'should ignore %s in unsupported fragment %s', async (query, content) => { widget.node.innerHTML = content; await provider.startQuery(query); expect(widget.node.innerHTML).toEqual(content); } ); it.each([ [/10/, formula('𝑥', '=', mark('1'), mark('0'))], [/x/i, formula(mark('𝑥'))], [/x=10/, formula(mark('𝑥'), mark('='), mark('1'), mark('0'))] ])('should highlight %s in a formula', async (query, expected) => { widget.node.innerHTML = `
${formula()}
`; await provider.startQuery(query); expect(widget.node.firstElementChild!.innerHTML).toEqual(expected); }); it('should count a match that spans several text nodes once', async () => { widget.node.innerHTML = `${formula()}
`; await provider.startQuery(/10/); expect(provider.matchesCount).toBe(1); }); it('should find a function name in a formula as a whole word', async () => { widget.node.innerHTML = `${FUNCTION_FORMULA}
`; await provider.startQuery(/\bsin\b/); expect(provider.matchesCount).toBe(1); }); it('should find a function applied to a parenthesis in a formula', async () => { widget.node.innerHTML = `${APPLIED_FUNCTION_FORMULA}
`; await provider.startQuery(/sin\(x\)/); expect(provider.matchesCount).toBe(1); }); it('should highlight each character of a text node of a formula', async () => { widget.node.innerHTML = `${TEXT_RUN_FORMULA}
`; await provider.startQuery(/a/i); expect(widget.node.querySelector('mjx-utext')!.innerHTML).toEqual( mark('A') + mark('A') ); }); it('should count the dots of an ellipsis in a formula as one match', async () => { widget.node.innerHTML = `${ELLIPSIS_FORMULA}
`; await provider.startQuery(/\./); expect(provider.matchesCount).toBe(1); }); }); describe('#highlightNext()', () => { it('should select every text node of the current match', async () => { widget.node.innerHTML = `${formula()}
`; await provider.startQuery(/10/); await provider.highlightNext(); const selected = Array.from(widget.node.querySelectorAll('mark'), m => m.classList.contains('jp-current-match') ); expect(selected).toEqual([true, true]); }); }); describe('#endQuery()', () => { it.each([ [/x/, 'xyz'], [/y/, '
xyz'], [/z/, '
xyz'], [/x/, '
xyz'],
[/y/, 'xyz'],
[/z/, 'xyz'],
[/x/, 'xyz'],
[/y/, 'xyz'],
[/z/, 'xyz'],
[/x/, 'xyz'],
[/y/, 'xyz'],
[/z/, 'xyz'],
[/10/, `${formula()}
`], [/x=10/, `${formula()}
`], [/a/i, `${TEXT_RUN_FORMULA}
`], [/\./, `${ELLIPSIS_FORMULA}
`], [/sin\(x\)/, `${APPLIED_FUNCTION_FORMULA}
`] ])( 'should restore highlighted %s fragment in %s', async (query, content) => { widget.node.innerHTML = content; await provider.startQuery(query); await provider.endQuery(); expect(widget.node.innerHTML).toEqual(content); } ); }); }); });