mediawiki-extensions-CodeMi.../tests/selenium/specs/textSelection-wikitext2010.js
MusikAnimal 13c9eae26e CodeMirrorPreferences: add panel to tweak prefs with the editor open
This is toggled by pressing Mod-Shift-, (or Command-Shift-, on MacOS),
which then puts focus on the preferences panel. It can be closed with
the Escape key, just like other CM panels.

The CodeMirror class comes with these extension which can be toggled in
preferences:
* Bracket matching
* Line numbering
* Line wrapping
* Highlight the active line
* Show special characters

Only bracket matching, line numbering, and line wrapping are available
in the 2017 editor.

The bidi isolation and template folding extensions are registered in
CodeMirrorModeMediaWiki as they are MW-specific. CodeMirrorPreferences'
new registerExtension() method allows any consumer of CodeMirror to add
any arbitrary extensions to the preferences panel. This is expected to
be called *after* CodeMirror has finished initializing. The
'ext.CodeMirror.ready' hook now passes the CodeMirror instance to
accommodate this.

The preferences are stored as a single user option in the database,
called 'codemirror-preferences'. The defaults can be configured with the
$wgCodeMirrorDefaultPreferences configuration setting. The
sysadmin-facing values are the familiar boolean, but since CodeMirror is
widely used, we make extra efforts to reduce the storage footprint (see
T54777). This includes only storing preferences that differ from the
defaults, and using binary representation instead of boolean values,
since the user option is stored as a string.

For now, all preferences are ignored in the 2017 editor. In a future
patch, we may add some as toggleable Tools in the VE toolbar.

Other changes:
* Refactor CSS to use a .darkmode() mixin
* Add a method to create a CSS-only fieldset in CodeMirrorPanel
* Fix Jest tests now that there are more calls to mw.user.options.get()
* Adjust Selenium tests to always use CM6
* Adjust Selenium tests to delete test pages (useful for local dev)
* Remove unused code

Bug: T359498
Change-Id: I70dcf2f49418cea632c452c1266440effad634f3
2024-11-18 22:23:22 -05:00

124 lines
4.1 KiB
JavaScript

'use strict';
const assert = require( 'assert' ),
EditPage = require( '../pageobjects/edit.page' ),
FixtureContent = require( '../fixturecontent' ),
LoginPage = require( 'wdio-mediawiki/LoginPage' ),
UserPreferences = require( '../userpreferences' ),
Util = require( 'wdio-mediawiki/Util' );
describe( 'CodeMirror textSelection for the wikitext 2010 editor', () => {
let title;
before( async () => {
title = Util.getTestString( 'CodeMirror-fixture1-' );
await LoginPage.loginAdmin();
await FixtureContent.createFixturePage( title );
await UserPreferences.enableWikitext2010EditorWithCodeMirror();
await EditPage.openForEditing( title );
await EditPage.wikiEditorToolbar.waitForDisplayed();
await EditPage.clickText();
} );
// Content is "[]{{template}}"
it( 'sets and gets the correct text when using setContents and getContents', async () => {
await browser.execute( () => $( '.cm-editor' ).textSelection( 'setContents', 'foobar' ) );
assert.strictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ),
'foobar'
);
} );
// Content is now "foobar"
it( 'sets and gets the correct selection when using setSelection and getSelection', async () => {
await browser.execute( () => {
$( '.cm-editor' ).textSelection( 'setSelection', { start: 3, end: 6 } );
} );
assert.strictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getSelection' ) ),
'bar'
);
} );
it( 'correctly replaces the selected text when using replaceSelection', async () => {
await browser.execute( () => $( '.cm-editor' ).textSelection( 'replaceSelection', 'baz' ) );
assert.strictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ),
'foobaz'
);
} );
// Content is now "foobaz"
it( 'returns the correct values for getCaretPosition', async () => {
await browser.execute( () => {
$( '.cm-editor' ).textSelection( 'setSelection', { start: 3, end: 6 } );
} );
assert.strictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getCaretPosition' ) ),
6
);
assert.deepStrictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getCaretPosition', { startAndEnd: true } ) ),
[ 3, 6 ]
);
} );
it( 'correctly wraps the selected text when using encapsulateSelection', async () => {
await browser.execute( () => {
$( '.cm-editor' ).textSelection( 'setContents', 'foobaz' )
.textSelection( 'encapsulateSelection', {
selectionStart: 0,
selectionEnd: 6,
pre: '<div>',
post: '</div>'
} );
} );
assert.strictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ),
'<div>foobaz</div>'
);
} );
it( "correctly inserts the 'peri' option when using encapsulateSelection", async () => {
await browser.execute( () => {
$( '.cm-editor' ).textSelection( 'setContents', 'foobaz' )
.textSelection( 'encapsulateSelection', {
selectionStart: 0,
selectionEnd: 6,
pre: '<div>',
post: '</div>',
peri: 'Soundgarden',
replace: true
} );
} );
assert.strictEqual(
await browser.execute( () => $( '.cm-editor' ).textSelection( 'getContents' ) ),
'<div>Soundgarden</div>'
);
} );
it( 'scrolls to the correct place when using scrollToCaretPosition', async () => {
await browser.execute( () => {
const $cmEditor = $( '.cm-editor' );
$cmEditor.textSelection( 'setContents', 'foobar\n'.repeat( 50 ) );
// Ensure caret is at the top.
$cmEditor.textSelection( 'setSelection', { start: 0 } );
// Force scrolling to the bottom.
$( '.cm-scroller' )[ 0 ].scrollTop = 5000;
// Use textSelection to scroll back to caret.
$cmEditor.textSelection( 'scrollToCaretPosition' );
} );
assert.strictEqual(
await browser.execute( () => $( '.cm-scroller' ).scrollTop() ),
0
);
} );
// Content is now "foobar\n" repeated 50 times.
it( 'retains the contents after turning CodeMirror off', async () => {
await EditPage.legacyCodeMirrorButton.click();
await EditPage.legacyTextInput.waitForDisplayed();
assert.match( await EditPage.legacyTextInput.getValue(), /foobar/ );
} );
} );