const CodeMirror = require( '../../resources/codemirror.js' );
const mediaWikiLang = require( '../../resources/codemirror.mediawiki.js' );
const bidiIsolationExtension = require( '../../resources/codemirror.mediawiki.bidiIsolation.js' );
const testCases = [
{
title: 'wraps HTML tags with span.cm-bidi-isolate',
input: 'שלוםשלוםשלום',
output: '
שלום<span class="foobar">שלום</span>שלום
'
},
{
title: 'wraps self-closing extension tags with span.cm-bidi-isolate',
input: '',
output: '<ref name="foo" />
'
}
];
// Setup CodeMirror instance.
const textarea = document.createElement( 'textarea' );
textarea.dir = 'rtl';
document.body.appendChild( textarea );
const cm = new CodeMirror( textarea );
const mwLang = mediaWikiLang(
{ bidiIsolation: true },
{
doubleUnderscore: [ {}, {} ],
functionSynonyms: [ {}, {} ],
tags: { ref: true },
urlProtocols: 'http\\:\\/\\/'
}
);
cm.initialize( [ ...cm.defaultExtensions, mwLang ] );
// Normally ran by mw.hook, but we don't mock the hook system in the Jest tests.
cm.preferences.registerExtension( 'bidiIsolation', bidiIsolationExtension, cm.view );
describe( 'CodeMirrorBidiIsolation', () => {
it.each( testCases )(
'bidi isolation ($title)',
( { input, output } ) => {
cm.view.dispatch( {
changes: {
from: 0,
to: cm.view.state.doc.length,
insert: input
}
} );
cm.$textarea.textSelection = jest.fn().mockReturnValue( input );
expect( cm.view.dom.querySelector( '.cm-content' ).innerHTML ).toStrictEqual( output );
}
);
} );