mediawiki-extensions-CodeMi.../tests/jest/codemirror.bidiIsolation.test.js
MusikAnimal e1f397f048 CodeMirrorBidiIsolation: fix for when the tag is the first element
This fixes a careless error introduced in I1338afeefa where we don't do
a strict equality test and thus if the tag is the first element (at
position 0), the following bracket is also treated as the start
position. Here we simply do a strict equality test against null (the
reset value), and viola, several issues are fixed.

Bug: T358804
Change-Id: I86021d363ecc33a7551bc887439dc1902914026f
2024-04-09 22:04:45 -04:00

44 lines
1.9 KiB
JavaScript

import CodeMirror from '../../src/codemirror.js';
import mediaWikiLang from '../../src/codemirror.mode.mediawiki.js';
const testCases = [
{
title: 'wraps HTML tags with span.cm-bidi-isolate',
input: 'שלום<span class="foobar">שלום</span>שלום',
output: '<div class="cm-line">שלום<span class="cm-bidi-isolate"><span class="cm-mw-htmltag-bracket">&lt;</span><span class="cm-mw-htmltag-name">span </span><span class="cm-mw-htmltag-attribute">class="foobar"</span><span class="cm-mw-htmltag-bracket">&gt;</span></span>שלום<span class="cm-bidi-isolate"><span class="cm-mw-htmltag-bracket">&lt;/</span><span class="cm-mw-htmltag-name">span</span><span class="cm-mw-htmltag-bracket">&gt;</span></span>שלום</div>'
},
{
title: 'wraps self-closing extension tags with span.cm-bidi-isolate',
input: '<ref name="foo" />',
output: '<div class="cm-line"><span class="cm-bidi-isolate"><span class="cm-mw-exttag-bracket cm-mw-ext-ref">&lt;</span><span class="cm-mw-exttag-name cm-mw-ext-ref">ref </span><span class="cm-mw-exttag-attribute cm-mw-ext-ref">name="foo" </span><span class="cm-mw-exttag-bracket cm-mw-ext-ref">/&gt;</span></span></div>'
}
];
// 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 },
{ tags: { ref: true } }
);
cm.initialize( [ ...cm.defaultExtensions, mwLang ] );
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 );
}
);
} );