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

100% Statements 27/27
100% Branches 19/19
100% Functions 5/5
100% Lines 27/27
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                                                                13×       13×   33× 26×   33×   33× 26×     13×                                   10×     10×   10× 112× 15× 97× 60×       10×      
/*!
 * VisualEditor DataModel Converter class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
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 ) {
	var 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 ) {
	var i, l,
		lines = sourceText.split( /\r\n|\r|\n/ ),
		content = [];
 
	for ( 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 ) {
	var i,
		text = '';
 
	range = range || new ve.Range( 0, data.length );
 
	for ( i = range.start; i < range.end; i++ ) {
		if ( data[ i ].type === '/paragraph' && data[ i + 1 ] && data[ i + 1 ].type === 'paragraph' ) {
			text += '\n';
		} else if ( !data[ i ].type ) {
			text += data[ i ];
		}
	}
 
	return text;
};
 
ve.dm.sourceConverter = new ve.dm.SourceConverter();