File: ve.ui.MWLanguageVariantInspector.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (840 lines) | stat: -rw-r--r-- 25,956 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*!
 * VisualEditor UserInterface LanguageVariantInspector class.
 *
 * @copyright See AUTHORS.txt
 */

/**
 * Inspector for a ve.dm.MWLanguageVariantNode.
 *
 * @class
 * @extends ve.ui.NodeInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWLanguageVariantInspector = function VeUiMWLanguageVariantInspector() {
	// Parent constructor
	ve.ui.MWLanguageVariantInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWLanguageVariantInspector, ve.ui.NodeInspector );

/* Static properties */

ve.ui.MWLanguageVariantInspector.static.name = 'mwLanguageVariant-disabled';

ve.ui.MWLanguageVariantInspector.static.title = OO.ui.deferMsg(
	'visualeditor-mwlanguagevariantinspector-title-disabled'
);

ve.ui.MWLanguageVariantInspector.static.modelClasses = [
	ve.dm.MWLanguageVariantBlockNode,
	ve.dm.MWLanguageVariantInlineNode,
	ve.dm.MWLanguageVariantHiddenNode
];

ve.ui.MWLanguageVariantInspector.static.size = 'large';

ve.ui.MWLanguageVariantInspector.static.actions = [
	{
		action: 'remove',
		label: OO.ui.deferMsg( 'visualeditor-inspector-remove-tooltip' ),
		flags: 'destructive',
		modes: 'edit'
	},
	...ve.ui.MWLanguageVariantInspector.super.static.actions
];

ve.ui.MWLanguageVariantInspector.static.defaultVariantType =
	'mwLanguageVariantInline';

ve.ui.MWLanguageVariantInspector.static.includeCommands = null;

// This is very similar to the exclude list in ve.ui.MWMediaDialog
ve.ui.MWLanguageVariantInspector.static.excludeCommands = [
	// No formatting
	'paragraph',
	'heading1',
	'heading2',
	'heading3',
	'heading4',
	'heading5',
	'heading6',
	'preformatted',
	'blockquote',
	// TODO: Decide if tables tools should be allowed
	'tableCellHeader',
	'tableCellData',
	// No structure
	'bullet',
	'bulletWrapOnce',
	'number',
	'numberWrapOnce',
	'indent',
	'outdent'
];

/**
 * Get the import rules for embedded target widgets in this inspector.
 *
 * @see ve.ui.MWMediaDialog#getImportRules
 * @return {Object} Import rules
 */
ve.ui.MWLanguageVariantInspector.static.getImportRules = function () {
	return ve.extendObject(
		ve.copy( ve.init.target.constructor.static.importRules ),
		{
			// TODO: We might want to include some of the
			// conversion/sanitization done by ve.ui.MWMediaDialog
		}
	);
};

/* Methods */

/**
 * Return a valid `variantInfo` object which will be used when a new
 * node of this subclass is inserted.  For instance,
 * ve.ui.MWLanguageVariantDisabledInspector will return the appropriate
 * object to use when the equivalent of wikitext `-{}-` is inserted
 * in the document.
 *
 * @return {Object}
 */
ve.ui.MWLanguageVariantInspector.prototype.getDefaultVariantInfo = null;

/**
 * Convert the current inspector state to new content which can be used
 * to update the ve.dm.SurfaceFragment backing this inspector.
 *
 * @param {Object} An existing variantInfo object for this node, which will be
 *  mutated to update it with the latest content from this inspector.
 * @return {string|Array} New content for the ve.dm.SurfaceFragment
 *  being inspected/updated.
 */
ve.ui.MWLanguageVariantInspector.prototype.getContentFromInspector = null;

// Helper functions for creating sub-document editors for embedded HTML

/**
 * Helper function to create a subdocument editor for HTML embedded in the
 * language variant node.
 *
 * @param {string} [placeholder] Placeholder text for this editor.
 * @return {ve.ui.TargetWidget}
 */
ve.ui.MWLanguageVariantInspector.prototype.createTextTarget = function ( placeholder ) {
	return ve.init.target.createTargetWidget( {
		toolbarGroups: [],
		includeCommands: this.constructor.static.includeCommands,
		excludeCommands: this.constructor.static.excludeCommands,
		importRules: this.constructor.static.getImportRules(),
		inDialog: this.constructor.static.name,
		placeholder: placeholder || null
	} );
};

/**
 * Helper function to initialize a ve.ui.TargetWidget with a given HTML
 * string extracted from the language variant node.
 *
 * @param {ve.ui.TargetWidget} [textTarget] A subdocument editor widget
 *   created by ve.ui.MWLanguageVariantInspector#createTextTarget.
 * @param {string} [htmlString] The HTML string extracted from this node.
 * @return {ve.dm.Document} The document model now backing the widget.
 */
ve.ui.MWLanguageVariantInspector.prototype.setupTextTargetDoc = function ( textTarget, htmlString ) {
	const doc = this.variantNode.getDocument().newFromHtml( htmlString );
	textTarget.setDocument( doc );
	return doc;
};

/**
 * Helper function to serialize the document backing a `ve.ui.TargetWidget`
 * back into HTML which can be embedded into the language variant node.
 * This method needs to do a bit of hackery to remove unnecessary p-wrapping
 * and (TODO) determine if an inline node needs to be converted to a
 * block node or vice-versa.
 *
 * @param {ve.dm.Document} doc The document backing an editor widget, as returned
 *  by ve.ui.MWLanguageVariantInspector#setupTextTargetDoc.
 * @return {string} An HTML string appropriate for embedding into a
 *  language variant node.
 */
ve.ui.MWLanguageVariantInspector.prototype.getHtmlForDoc = function ( doc ) {
	const surface = new ve.dm.Surface( doc );

	// Remove outermost p-wrapping, if present
	try {
		surface.change(
			ve.dm.TransactionBuilder.static.newFromWrap( doc, new ve.Range( 0, doc.data.countNonInternalElements() ), [], [], [ { type: 'paragraph' } ], [] )
		);
	} catch ( e ) {
		// Sometimes there is no p-wrapping, for example: "* foo"
		// Sometimes there are multiple <p> tags in the output.
		// That's okay: ignore the error and use what we've got.
	}
	// XXX return a flag to indicate whether contents are now inline or block?
	const targetHtmlDoc = ve.dm.converter.getDomFromModel( doc );
	return ve.properInnerHtml( targetHtmlDoc.body );
};

// Inspector implementation

/**
 * Handle frame ready events.
 */
ve.ui.MWLanguageVariantInspector.prototype.initialize = function () {
	// Parent method
	ve.ui.MWLanguageVariantInspector.super.prototype.initialize.call( this );
	this.$content.addClass( 've-ui-mwLanguageVariantInspector-content' );
};

/**
 * @inheritdoc
 */
ve.ui.MWLanguageVariantInspector.prototype.getActionProcess = function ( action ) {
	if ( action === 'remove' || action === 'insert' ) {
		return new OO.ui.Process( () => {
			this.close( { action: action } );
		} );
	}
	return ve.ui.MWLanguageVariantInspector.super.prototype.getActionProcess.call( this, action );
};

/**
 * Handle the inspector being setup.
 *
 * @param {Object} [data] Inspector opening data
 * @return {OO.ui.Process}
 */
ve.ui.MWLanguageVariantInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWLanguageVariantInspector.super.prototype.getSetupProcess.call( this, data )
		.next( () => {
			this.getFragment().getSurface().pushStaging();

			this.variantNode = this.getSelectedNode();
			if ( !this.variantNode ) {
				this.getFragment().insertContent( [
					{
						type: this.constructor.static.defaultVariantType,
						attributes: {
							variantInfo: this.getDefaultVariantInfo()
						}
					},
					{ type: '/' + this.constructor.static.defaultVariantType }
				] ).select();
				this.variantNode = this.getSelectedNode();
			}
		} );
};

/**
 * @inheritdoc
 */
ve.ui.MWLanguageVariantInspector.prototype.getTeardownProcess = function ( data ) {
	data = data || {};
	return ve.ui.MWLanguageVariantInspector.super.prototype.getTeardownProcess.call( this, data )
		.first( () => {
			const surfaceModel = this.getFragment().getSurface();

			if ( data.action === 'remove' ) {
				surfaceModel.popStaging();
				// If popStaging removed the node then this will be a no-op
				this.getFragment().removeContent();
			} else if ( data.action === 'done' ) {
				// Edit language variant node
				const newContent = this.getContentFromInspector(
					ve.copy( this.variantNode.getVariantInfo() )
				);
				if ( newContent[ 0 ].type === this.variantNode.getType() ) {
					this.getFragment().changeAttributes( {
						variantInfo: newContent[ 0 ].attributes.variantInfo
					} );
				} else {
					this.getFragment().removeContent();
					this.getFragment().insertContent( newContent ).select();
					this.variantNode = this.getSelectedNode();
				}
				surfaceModel.applyStaging();
			} else {
				surfaceModel.popStaging();
			}

		} );
};

/* Subclasses */

/**
 * Editor for "disabled" rules.
 *
 * @class
 * @extends ve.ui.MWLanguageVariantInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWLanguageVariantDisabledInspector = function VeUiMWLanguageVariantDisabledInspector() {
	ve.ui.MWLanguageVariantDisabledInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWLanguageVariantDisabledInspector, ve.ui.MWLanguageVariantInspector );

/* Static properties */

ve.ui.MWLanguageVariantDisabledInspector.static.name = 'mwLanguageVariant-disabled';

ve.ui.MWLanguageVariantDisabledInspector.static.title = OO.ui.deferMsg(
	'visualeditor-mwlanguagevariantinspector-title-disabled'
);

/* Methods */

ve.ui.MWLanguageVariantDisabledInspector.prototype.initialize = function () {
	ve.ui.MWLanguageVariantDisabledInspector.super.prototype.initialize.call( this );
	this.textTarget = this.createTextTarget( OO.ui.msg(
		'visualeditor-mwlanguagevariantinspector-disabled-placeholder'
	) );
	this.form.$element.append( this.textTarget.$element );
};

ve.ui.MWLanguageVariantDisabledInspector.prototype.getDefaultVariantInfo = function () {
	return { disabled: { t: '' } };
};

ve.ui.MWLanguageVariantDisabledInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWLanguageVariantDisabledInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
		const variantInfo = this.variantNode.getVariantInfo();
		this.textTargetDoc = this.setupTextTargetDoc(
			this.textTarget,
			variantInfo.disabled.t
		);
	} );
};

ve.ui.MWLanguageVariantDisabledInspector.prototype.getContentFromInspector = function ( variantInfo ) {
	// TODO should allow type to depend on targetHtmlDoc, maybe switch
	// from inline to block.
	const type = this.variantNode.getType();
	variantInfo.disabled.t = this.getHtmlForDoc( this.textTargetDoc );
	return [
		{
			type: type,
			attributes: { variantInfo: variantInfo }
		},
		{
			type: '/' + type
		}
	];
};

ve.ui.MWLanguageVariantDisabledInspector.prototype.getReadyProcess = function ( data ) {
	return ve.ui.MWLanguageVariantDisabledInspector.super.prototype.getReadyProcess.call( this, data )
		.next( () => {
			this.textTarget.focus();
		} );
};

ve.ui.MWLanguageVariantDisabledInspector.prototype.getTeardownProcess = function ( data ) {
	return ve.ui.MWLanguageVariantDisabledInspector.super.prototype.getTeardownProcess.call( this, data )
		.next( () => {
			// Reset inspector
			this.textTarget.clear();
			this.textTargetDoc = null;
		} );
};

/**
 * Editor for "name" rules.
 *
 * @class
 * @extends ve.ui.MWLanguageVariantInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWLanguageVariantNameInspector = function VeUiMWLanguageVariantNameInspector() {
	ve.ui.MWLanguageVariantNameInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWLanguageVariantNameInspector, ve.ui.MWLanguageVariantInspector );

/* Static properties */

ve.ui.MWLanguageVariantNameInspector.static.name = 'mwLanguageVariant-name';

ve.ui.MWLanguageVariantNameInspector.static.title = OO.ui.deferMsg(
	'visualeditor-mwlanguagevariantinspector-title-name'
);

/* Methods */

ve.ui.MWLanguageVariantNameInspector.prototype.initialize = function () {
	ve.ui.MWLanguageVariantNameInspector.super.prototype.initialize.call( this );
	this.languageInput = new ve.ui.LanguageInputWidget( {
		dialogManager: this.manager.getSurface().getDialogs(),
		dirInput: 'none'
	} );
	this.form.$element.append( this.languageInput.$element );
};

ve.ui.MWLanguageVariantNameInspector.prototype.getDefaultVariantInfo = function () {
	return { name: { t: mw.config.get( 'wgUserVariant' ) || 'en' } };
};

ve.ui.MWLanguageVariantNameInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWLanguageVariantNameInspector.super.prototype.getSetupProcess.call( this, data )
		.next( () => {
			const variantInfo = this.variantNode.getVariantInfo();
			this.languageInput.setLangAndDir(
				variantInfo.name.t,
				'auto'
			);
		} );
};

ve.ui.MWLanguageVariantNameInspector.prototype.getContentFromInspector = function ( variantInfo ) {
	const type = this.variantNode.getType();
	variantInfo.name.t = this.languageInput.getLang();
	return [
		{
			type: type,
			attributes: { variantInfo: variantInfo }
		},
		{
			type: '/' + type
		}
	];
};

/**
 * Editor for "filter" rules.
 *
 * @class
 * @extends ve.ui.MWLanguageVariantInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWLanguageVariantFilterInspector = function VeUiMWLanguageVariantFilterInspector() {
	ve.ui.MWLanguageVariantFilterInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWLanguageVariantFilterInspector, ve.ui.MWLanguageVariantInspector );

/* Static properties */

ve.ui.MWLanguageVariantFilterInspector.static.name = 'mwLanguageVariant-filter';

ve.ui.MWLanguageVariantFilterInspector.static.title = OO.ui.deferMsg(
	'visualeditor-mwlanguagevariantinspector-title-filter'
);

/* Methods */

ve.ui.MWLanguageVariantFilterInspector.prototype.initialize = function () {
	ve.ui.MWLanguageVariantFilterInspector.super.prototype.initialize.call( this );
	this.textTarget = this.createTextTarget( OO.ui.msg(
		'visualeditor-mwlanguagevariantinspector-filter-text-placeholder'
	) );

	this.langWidget = new OO.ui.TagMultiselectWidget( {
		allowArbitary: false,
		allowDisplayInvalidTags: true,
		allowedValues: ve.init.platform.getLanguageCodes().sort(),
		placeholder: OO.ui.msg(
			'visualeditor-mwlanguagevariantinspector-filter-langs-placeholder'
		),
		icon: 'language'
	} );
	this.langWidget.createTagItemWidget = function ( data, label ) {
		const name = ve.init.platform.getLanguageName( data.toLowerCase() );
		label = label || ( name ? ( name + ' (' + data + ')' ) : data );
		return OO.ui.TagMultiselectWidget.prototype.createTagItemWidget.call(
			this, data, label
		);
	};

	this.form.$element.append(
		new OO.ui.FieldLayout( this.langWidget, {
			align: 'top',
			label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-filter-langs-label' )
		} ).$element
	);
	this.form.$element.append(
		new OO.ui.FieldLayout( this.textTarget, {
			align: 'top',
			label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-filter-text-label' )
		} ).$element
	);
};

ve.ui.MWLanguageVariantFilterInspector.prototype.getDefaultVariantInfo = function () {
	return { filter: { l: [ mw.config.get( 'wgUserVariant' ) ], t: '' } };
};

ve.ui.MWLanguageVariantFilterInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWLanguageVariantFilterInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
		const variantInfo = this.variantNode.getVariantInfo();
		this.textTargetDoc = this.setupTextTargetDoc(
			this.textTarget,
			variantInfo.filter.t
		);
		this.langWidget.setValue( variantInfo.filter.l );
	} );
};

ve.ui.MWLanguageVariantFilterInspector.prototype.getContentFromInspector = function ( variantInfo ) {
	// TODO should allow type to depend on targetHtmlDoc, maybe switch
	// from inline to block.
	const type = this.variantNode.getType();
	variantInfo.filter.t = this.getHtmlForDoc( this.textTargetDoc );
	variantInfo.filter.l = this.langWidget.getValue();
	return [
		{
			type: type,
			attributes: { variantInfo: variantInfo }
		},
		{
			type: '/' + type
		}
	];
};

ve.ui.MWLanguageVariantFilterInspector.prototype.getReadyProcess = function ( data ) {
	return ve.ui.MWLanguageVariantFilterInspector.super.prototype.getReadyProcess.call( this, data )
		.next( () => {
			this.textTarget.focus();
		} );
};

ve.ui.MWLanguageVariantFilterInspector.prototype.getTeardownProcess = function ( data ) {
	return ve.ui.MWLanguageVariantFilterInspector.super.prototype.getTeardownProcess.call( this, data )
		.next( () => {
			// Reset inspector
			this.langWidget.clearInput();
			this.langWidget.clearItems();
			this.textTarget.clear();
			this.textTargetDoc = null;
		} );
};

/**
 * Editor for "two-way" rules.
 *
 * @class
 * @extends ve.ui.MWLanguageVariantInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWLanguageVariantTwoWayInspector = function VeUiMWLanguageVariantTwoWayInspector() {
	ve.ui.MWLanguageVariantTwoWayInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWLanguageVariantTwoWayInspector, ve.ui.MWLanguageVariantInspector );

/* Static properties */

ve.ui.MWLanguageVariantTwoWayInspector.static.name = 'mwLanguageVariant-twoway';

ve.ui.MWLanguageVariantTwoWayInspector.static.title = OO.ui.deferMsg(
	'visualeditor-mwlanguagevariantinspector-title-twoway'
);

/* Methods */

ve.ui.MWLanguageVariantTwoWayInspector.prototype.initialize = function () {
	ve.ui.MWLanguageVariantTwoWayInspector.super.prototype.initialize.call( this );
	this.items = [];
	this.layout = new OO.ui.FieldsetLayout( { } );
	this.form.$element.append( this.layout.$element );

	this.addButton = new OO.ui.ButtonInputWidget( {
		label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-twoway-add-button' ),
		icon: 'add'
	} );
	this.form.$element.append( this.addButton.$element );

	// Events
	this.addButton.connect( this, { click: 'onAddButtonClick' } );
};

ve.ui.MWLanguageVariantTwoWayInspector.prototype.getDefaultVariantInfo = function () {
	return { twoway: [ { l: mw.config.get( 'wgUserVariant' ), t: '' } ] };
};

ve.ui.MWLanguageVariantTwoWayInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWLanguageVariantTwoWayInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
		const variantInfo = this.variantNode.getVariantInfo();
		this.layout.clearItems();
		this.items = [];
		variantInfo.twoway.forEach( ( tw, idx ) => {
			this.items[ idx ] = this.createItem( tw.l, tw.t );
			this.layout.addItems( [ this.items[ idx ].layout ] );
		} );
	} );
};

/**
 * Create widgets corresponding to a given mapping given by this rule.
 *
 * @param {string} [lang] The language code for the content text.
 * @param {string} [content] The HTML content text.
 * @return {Object} An object containing the required widgets and backing
 *  documents for this mapping item.
 */
ve.ui.MWLanguageVariantTwoWayInspector.prototype.createItem = function ( lang, content ) {
	const languageInput = new ve.ui.LanguageInputWidget( {
		dialogManager: this.manager.getSurface().getDialogs(),
		dirInput: 'none'
	} );
	const textTarget = this.createTextTarget( OO.ui.msg(
		'visualeditor-mwlanguagevariantinspector-twoway-text-placeholder'
	) );
	const clearButton = new OO.ui.ButtonInputWidget( {
		icon: 'clear',
		title: OO.ui.deferMsg(
			'visualeditor-mwlanguagevariantinspector-twoway-clear-button'
		),
		framed: false
	} );
	const layout = new OO.ui.FieldLayout(
		new OO.ui.Widget( {
			content: [
				new OO.ui.ActionFieldLayout(
					languageInput,
					clearButton
				),
				textTarget
			]
		} ), {}
	);
	const item = {
		languageInput: languageInput,
		textTarget: textTarget,
		clearButton: clearButton,
		layout: layout
	};

	// Initialize
	item.textTargetDoc = this.setupTextTargetDoc( textTarget, content );
	languageInput.setLangAndDir( lang, 'auto' );
	clearButton.connect( this, { click: [ 'onClearButtonClick', item ] } );
	return item;
};

ve.ui.MWLanguageVariantTwoWayInspector.prototype.getContentFromInspector = function ( variantInfo ) {
	// TODO should allow type to depend on targetHtmlDoc, maybe switch
	// from inline to block.
	const type = this.variantNode.getType();
	variantInfo.twoway = this.items.map( ( item ) => ( {
		l: item.languageInput.getLang(),
		t: this.getHtmlForDoc( item.textTargetDoc )
	} ) );
	return [
		{
			type: type,
			attributes: { variantInfo: variantInfo }
		},
		{
			type: '/' + type
		}
	];
};

/**
 * Create a new mapping item in the inspector.
 */
ve.ui.MWLanguageVariantTwoWayInspector.prototype.onAddButtonClick = function () {
	const idx = this.items.length;
	this.items[ idx ] = this.createItem( mw.config.get( 'wgUserVariant' ), '' );
	this.layout.addItems( [ this.items[ idx ].layout ] );
};

/**
 * Remove a mapping item from the inspector.
 *
 * @param {Object} item
 */
ve.ui.MWLanguageVariantTwoWayInspector.prototype.onClearButtonClick = function ( item ) {
	const idx = this.items.indexOf( item );
	this.items.splice( idx, 1 );
	this.layout.removeItems( [ item.layout ] );
	item.clearButton.disconnect( this );
};

/**
 * Editor for "one-way" rules.
 *
 * @class
 * @extends ve.ui.MWLanguageVariantInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWLanguageVariantOneWayInspector = function VeUiMWLanguageVariantOneWayInspector() {
	ve.ui.MWLanguageVariantOneWayInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWLanguageVariantOneWayInspector, ve.ui.MWLanguageVariantInspector );

/* Static properties */

ve.ui.MWLanguageVariantOneWayInspector.static.name = 'mwLanguageVariant-oneway';

ve.ui.MWLanguageVariantOneWayInspector.static.title = OO.ui.deferMsg(
	'visualeditor-mwlanguagevariantinspector-title-oneway'
);

/* Methods */

ve.ui.MWLanguageVariantOneWayInspector.prototype.initialize = function () {
	ve.ui.MWLanguageVariantOneWayInspector.super.prototype.initialize.call( this );
	this.items = [];
	this.layout = new OO.ui.FieldsetLayout( { } );
	this.form.$element.append( this.layout.$element );

	this.addButton = new OO.ui.ButtonInputWidget( {
		label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-oneway-add-button' ),
		icon: 'add'
	} );
	this.form.$element.append( this.addButton.$element );

	// Events
	this.addButton.connect( this, { click: 'onAddButtonClick' } );
};

ve.ui.MWLanguageVariantOneWayInspector.prototype.getDefaultVariantInfo = function () {
	return { oneway: [ { f: '', l: mw.config.get( 'wgUserVariant' ), t: '' } ] };
};

ve.ui.MWLanguageVariantOneWayInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWLanguageVariantOneWayInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
		const variantInfo = this.variantNode.getVariantInfo();
		this.layout.clearItems();
		this.items = [];
		variantInfo.oneway.forEach( ( ow, idx ) => {
			this.items[ idx ] = this.createItem( ow.f, ow.l, ow.t );
			this.layout.addItems( [ this.items[ idx ].layout ] );
		} );
	} );
};

/**
 * Create widgets corresponding to a given mapping given by this rule.
 *
 * @param {string} [from] The HTML source text.
 * @param {string} [lang] The language code for the destination text.
 * @param {string} [to] The HTML destination text.
 * @return {Object} An object containing the required widgets and backing
 *  documents for this mapping item.
 */
ve.ui.MWLanguageVariantOneWayInspector.prototype.createItem = function ( from, lang, to ) {
	const fromTextTarget = this.createTextTarget( OO.ui.msg(
		'visualeditor-mwlanguagevariantinspector-oneway-from-text-placeholder'
	) );
	const languageInput = new ve.ui.LanguageInputWidget( {
		dialogManager: this.manager.getSurface().getDialogs(),
		dirInput: 'none'
	} );
	const toTextTarget = this.createTextTarget( OO.ui.msg(
		'visualeditor-mwlanguagevariantinspector-oneway-to-text-placeholder'
	) );
	const clearButton = new OO.ui.ButtonInputWidget( {
		icon: 'clear',
		title: OO.ui.deferMsg(
			'visualeditor-mwlanguagevariantinspector-oneway-clear-button'
		),
		framed: false
	} );
	const layout = new OO.ui.FieldLayout(
		new OO.ui.Widget( {
			content: [
				new OO.ui.ActionFieldLayout(
					fromTextTarget,
					clearButton
				),
				languageInput,
				toTextTarget
			]
		} ), {}
	);
	const item = {
		fromTextTarget: fromTextTarget,
		languageInput: languageInput,
		toTextTarget: toTextTarget,
		clearButton: clearButton,
		layout: layout
	};

	// Initialize
	item.fromTextTargetDoc = this.setupTextTargetDoc( fromTextTarget, from );
	item.toTextTargetDoc = this.setupTextTargetDoc( toTextTarget, to );
	languageInput.setLangAndDir( lang, 'auto' );
	clearButton.connect( this, { click: [ 'onClearButtonClick', item ] } );
	return item;
};

ve.ui.MWLanguageVariantOneWayInspector.prototype.getContentFromInspector = function ( variantInfo ) {
	// TODO should allow type to depend on targetHtmlDoc, maybe switch
	// from inline to block.
	const type = this.variantNode.getType();
	variantInfo.oneway = this.items.map( ( item ) => ( {
		f: this.getHtmlForDoc( item.fromTextTargetDoc ),
		l: item.languageInput.getLang(),
		t: this.getHtmlForDoc( item.toTextTargetDoc )
	} ) );
	return [
		{
			type: type,
			attributes: { variantInfo: variantInfo }
		},
		{
			type: '/' + type
		}
	];
};

/**
 * Create a new mapping item in the inspector.
 */
ve.ui.MWLanguageVariantOneWayInspector.prototype.onAddButtonClick = function () {
	const idx = this.items.length;
	this.items[ idx ] = this.createItem( '', mw.config.get( 'wgUserVariant' ), '' );
	this.layout.addItems( [ this.items[ idx ].layout ] );
};

/**
 * Remove a mapping item from the inspector.
 *
 * @param {Object} item
 */
ve.ui.MWLanguageVariantOneWayInspector.prototype.onClearButtonClick = function ( item ) {
	const idx = this.items.indexOf( item );
	this.items.splice( idx, 1 );
	this.layout.removeItems( [ item.layout ] );
	item.clearButton.disconnect( this );
};

/* Registration */

ve.ui.windowFactory.register( ve.ui.MWLanguageVariantDisabledInspector );
ve.ui.windowFactory.register( ve.ui.MWLanguageVariantNameInspector );
ve.ui.windowFactory.register( ve.ui.MWLanguageVariantFilterInspector );
ve.ui.windowFactory.register( ve.ui.MWLanguageVariantTwoWayInspector );
ve.ui.windowFactory.register( ve.ui.MWLanguageVariantOneWayInspector );