All files / src/dm ve.dm.SourceConverter.js

100% Statements 31/31
100% Branches 19/19
100% Functions 5/5
100% Lines 28/28

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95            1x                           1x 7x   7x     7x   7x                   1x 17x 17x   17x   41x 34x   41x   41x 34x     17x                 1x 3x                   1x 10x   10x   10x   112x 60x 52x       18x       10x     1x  
/*!
 * VisualEditor DataModel Converter class.
 *
 * @copyright See AUTHORS.txt
 */
 
ve.dm.SourceConverter = function VeDmSourceConverter() {
};
 
/**
 * Convert from source text to a document model
 *
 * Equivalent to ve.dm.Converter#getModelFromDom.
 *
 * @param {string} sourceText Source text
 * @param {Object} [options] Conversion options
 * @param {string} [options.lang] Document language code
 * @param {string} [options.dir] Document directionality (ltr/rtl)
 * @return {ve.dm.Document} Document model
 */
ve.dm.SourceConverter.prototype.getModelFromSourceText = function ( sourceText, options ) {
	const data = this.getDataFromSourceText( sourceText );
 
	options = options || {};
 
	// TODO: Internal lists are completely unused in source mode
	data.push( { type: 'internalList' }, { type: '/internalList' } );
 
	return new ve.dm.Document( data, undefined, undefined, undefined, undefined, options.lang, options.dir, undefined, true );
};
 
/**
 * Convert from source text to linear data
 *
 * @param {string} sourceText Source text
 * @param {boolean} [inline] Convert text for inline insertion, so skip opening and closing paragraph elements
 * @return {Array} Linear data
 */
ve.dm.SourceConverter.prototype.getDataFromSourceText = function ( sourceText, inline ) {
	const lines = sourceText.split( /\r\n|\r|\n/ ),
		content = [];
 
	for ( let i = 0, l = lines.length; i < l; i++ ) {
		// Skip opening <p> in inline mode
		if ( !( inline && i === 0 ) ) {
			content.push( { type: 'paragraph' } );
		}
		ve.batchPush( content, lines[ i ].split( '' ) );
		// Skip closing </p> in inline mode
		if ( !( inline && i === l - 1 ) ) {
			content.push( { type: '/paragraph' } );
		}
	}
	return content;
};
 
/**
 * Convert from document model to source text
 *
 * @param {ve.dm.Document} model Document model
 * @return {string} Source text
 */
ve.dm.SourceConverter.prototype.getSourceTextFromModel = function ( model ) {
	return this.getSourceTextFromDataRange( model.data.data );
};
 
/**
 * Convert from linear data to source text, optionally with a specified range
 *
 * @param {Array} data Linear data
 * @param {ve.Range} [range] Range, defaults to full data set.
 * @return {string} Source text
 */
ve.dm.SourceConverter.prototype.getSourceTextFromDataRange = function ( data, range ) {
	let text = '';
 
	range = range || new ve.Range( 0, data.length );
 
	for ( let i = range.start; i < range.end; i++ ) {
		// Check for the most common case first for best performance
		if ( !data[ i ].type ) {
			text += data[ i ];
		} else if ( data[ i ].type === '/paragraph' &&
			( !data[ i + 1 ] || data[ i + 1 ].type === 'paragraph' )
		) {
			// (T243606) Append a newline after each full paragraph, including the last one in the range
			text += '\n';
		}
	}
 
	return text;
};
 
ve.dm.sourceConverter = new ve.dm.SourceConverter();