Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.SourceConverter.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Converter class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 ve.dm.SourceConverter = function VeDmSourceConverter() {
8 };
9
10 /**
11 * Convert from source text to a document model
12 *
13 * Equivalent to ve.dm.Converter#getModelFromDom.
14 *
15 * @param {string} sourceText Source text
16 * @param {Object} [options] Conversion options
17 * @param {string} [options.lang] Document language code
18 * @param {string} [options.dir] Document directionality (ltr/rtl)
19 * @return {ve.dm.Document} Document model
20 */
21 ve.dm.SourceConverter.prototype.getModelFromSourceText = function ( sourceText, options ) {
22 const data = this.getDataFromSourceText( sourceText );
23
24 options = options || {};
25
26 // TODO: Internal lists are completely unused in source mode
27 data.push( { type: 'internalList' }, { type: '/internalList' } );
28
29 return new ve.dm.Document( data, undefined, undefined, undefined, undefined, options.lang, options.dir, undefined, true );
30 };
31
32 /**
33 * Convert from source text to linear data
34 *
35 * @param {string} sourceText Source text
36 * @param {boolean} [inline] Convert text for inline insertion, so skip opening and closing paragraph elements
37 * @return {Array} Linear data
38 */
39 ve.dm.SourceConverter.prototype.getDataFromSourceText = function ( sourceText, inline ) {
40 const lines = sourceText.split( /\r\n|\r|\n/ ),
41 content = [];
42
43 for ( let i = 0, l = lines.length; i < l; i++ ) {
44 // Skip opening <p> in inline mode
45 if ( !( inline && i === 0 ) ) {
46 content.push( { type: 'paragraph' } );
47 }
48 ve.batchPush( content, lines[ i ].split( '' ) );
49 // Skip closing </p> in inline mode
50 if ( !( inline && i === l - 1 ) ) {
51 content.push( { type: '/paragraph' } );
52 }
53 }
54 return content;
55 };
56
57 /**
58 * Convert from document model to source text
59 *
60 * @param {ve.dm.Document} model Document model
61 * @return {string} Source text
62 */
63 ve.dm.SourceConverter.prototype.getSourceTextFromModel = function ( model ) {
64 return this.getSourceTextFromDataRange( model.data.data );
65 };
66
67 /**
68 * Convert from linear data to source text, optionally with a specified range
69 *
70 * @param {Array} data Linear data
71 * @param {ve.Range} [range] Range, defaults to full data set.
72 * @return {string} Source text
73 */
74 ve.dm.SourceConverter.prototype.getSourceTextFromDataRange = function ( data, range ) {
75 let text = '';
76
77 range = range || new ve.Range( 0, data.length );
78
79 for ( let i = range.start; i < range.end; i++ ) {
80 // Check for the most common case first for best performance
81 if ( !data[ i ].type ) {
82 text += data[ i ];
83 } else if ( data[ i ].type === '/paragraph' &&
84 ( !data[ i + 1 ] || data[ i + 1 ].type === 'paragraph' )
85 ) {
86 // (T243606) Append a newline after each full paragraph, including the last one in the range
87 text += '\n';
88 }
89 }
90
91 return text;
92 };
93
94 ve.dm.sourceConverter = new ve.dm.SourceConverter();