All files / src/ui/widgets ve.ui.WhitespacePreservingTextInputWidget.js

95.83% Statements 23/24
71.42% Branches 10/14
100% Functions 4/4
95.83% Lines 23/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                  1x   1x     1x   1x   1x 1x   1x         1x                 1x 9x 9x 9x 4x     9x 9x 9x 4x     9x               1x 1x               1x 1x       1x    
/*!
 * VisualEditor UserInterface WhitespacePreservingTextInputWidget class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Text input widget which hides but preserves leading and trailing whitespace
 *
 * @class
 * @extends OO.ui.MultilineTextInputWidget
 *
 * @constructor
 * @param {Object} [config] Configuration options
 * @param {string} [config.valueAndWhitespace=''] Initial value and whitespace
 * @param {number} [config.limit] Maximum number of characters to preserve at each end
 */
ve.ui.WhitespacePreservingTextInputWidget = function VeUiWhitespacePreservingTextInputWidget( config ) {
	// Configuration
	config = config || {};
 
	// Parent constructor
	ve.ui.WhitespacePreservingTextInputWidget.super.call( this, config );
 
	this.limit = config.limit;
 
	this.setWhitespace( [ '', '' ] );
	this.setValueAndWhitespace( config.valueAndWhitespace || '' );
 
	this.$element.addClass( 've-ui-whitespacePreservingTextInputWidget' );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.WhitespacePreservingTextInputWidget, OO.ui.MultilineTextInputWidget );
 
/* Methods */
 
/**
 * Set the value of the widget and extract whitespace.
 *
 * @param {string} value
 */
ve.ui.WhitespacePreservingTextInputWidget.prototype.setValueAndWhitespace = function ( value ) {
	const leftValue = this.limit ? value.slice( 0, this.limit ) : value;
	this.whitespace[ 0 ] = leftValue.match( /^\s*/ )[ 0 ];
	if ( this.whitespace[ 0 ] ) {
		value = value.slice( this.whitespace[ 0 ].length );
	}
 
	const rightValue = this.limit ? value.slice( -this.limit ) : value;
	this.whitespace[ 1 ] = rightValue.match( /\s*$/ )[ 0 ];
	if ( this.whitespace[ 1 ] ) {
		value = value.slice( 0, -this.whitespace[ 1 ].length );
	}
 
	this.setValue( value );
};
 
/**
 * Set the value of the widget and extract whitespace.
 *
 * @param {string[]} whitespace Outer whitespace
 */
ve.ui.WhitespacePreservingTextInputWidget.prototype.setWhitespace = function ( whitespace ) {
	this.whitespace = whitespace;
};
 
/**
 * Get the value of text widget, including hidden outer whitespace
 *
 * @return {string} Text widget value including whitespace
 */
ve.ui.WhitespacePreservingTextInputWidget.prototype.getValueAndWhitespace = function () {
	Iif ( !this.whitespace ) {
		// In case getValue() is called from a parent constructor
		return this.value;
	}
	return this.whitespace[ 0 ] + this.value + this.whitespace[ 1 ];
};