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

40.62% Statements 39/96
43.33% Branches 13/30
33.33% Functions 5/15
39.78% Lines 37/93

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 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                              1x   5x         1x         1x                                           1x               1x                                           1x 5x 1x   1x 1x 1x     4x       4x           1x 2x   2x       2x           2x 1x 1x   13x       13x       13x 5x 5x 5x 8x             1x     1x     1x                 1x           1x                                                                                                             1x 1x     1x                                     1x                                              
/*!
 * VisualEditor DataModel SourceSurfaceFragment class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Surface fragment for editing surfaces in source mode.
 *
 * @class
 * @extends ve.dm.SurfaceFragment
 *
 * @constructor
 * @param {ve.dm.Document} doc
 */
ve.dm.SourceSurfaceFragment = function VeDmSourceSurfaceFragment() {
	// Parent constructors
	ve.dm.SourceSurfaceFragment.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.SourceSurfaceFragment, ve.dm.SurfaceFragment );
 
/**
 * @inheritdoc
 */
ve.dm.SourceSurfaceFragment.prototype.annotateContent = function () {
	const text = this.getText( true );
 
	this.pushPending( this.convertFromSource( text ).then( ( selectionDocument ) => {
		const tempSurfaceModel = new ve.dm.Surface( selectionDocument );
		const tempFragment = tempSurfaceModel.getLinearFragment(
			// TODO: Find content offsets
			selectionDocument.getDocumentRange()
		);
		tempFragment.annotateContent.apply( tempFragment, arguments );
 
		this.clearPending();
		this.insertDocument( tempFragment.getDocument() );
		return this.getPending();
	} ) );
 
	return this;
};
 
/**
 * @inheritdoc
 */
ve.dm.SourceSurfaceFragment.prototype.getAnnotations = function () {
	// Source surface contains no annotations
	return new ve.dm.AnnotationSet( this.getDocument().getStore() );
};
 
/**
 * @inheritdoc
 */
ve.dm.SourceSurfaceFragment.prototype.convertNodes = function () {
	const text = this.getText( true );
 
	this.pushPending( this.convertFromSource( text ).then( ( selectionDocument ) => {
		const tempSurfaceModel = new ve.dm.Surface( selectionDocument );
		const tempFragment = tempSurfaceModel.getLinearFragment(
			// TODO: Find content offsets
			selectionDocument.getDocumentRange()
		);
		tempFragment.convertNodes.apply( tempFragment, arguments );
 
		this.clearPending();
		this.insertDocument( tempFragment.getDocument() );
		return this.getPending();
	} ) );
 
	return this;
};
 
/**
 * @inheritdoc
 */
ve.dm.SourceSurfaceFragment.prototype.insertContent = function ( content, annotate ) {
	if ( typeof content !== 'string' ) {
		const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), content );
		// Pass `annotate` as `ignoreCoveringAnnotations`. If matching the target annotation (plain text) strip covering annotations.
		Eif ( !data.isPlainText( null, false, [ 'paragraph' ], annotate ) ) {
			this.insertDocument( new ve.dm.Document( content.concat( { type: 'internalList' }, { type: '/internalList' } ) ) );
			return this;
		}
	} else {
		content = ve.dm.sourceConverter.getDataFromSourceText( content, true );
	}
 
	// Parent method
	return ve.dm.SourceSurfaceFragment.super.prototype.insertContent.call( this, content );
};
 
/**
 * @inheritdoc
 */
ve.dm.SourceSurfaceFragment.prototype.insertDocument = function ( doc, newDocRange, annotate ) {
	const range = this.getSelection().getCoveringRange();
 
	Iif ( !range ) {
		return this;
	}
 
	Iif ( newDocRange ) {
		doc = doc.shallowCloneFromRange( newDocRange );
		newDocRange = doc.originalRange;
	}
 
	// Pass `annotate` as `ignoreCoveringAnnotations`. If matching the target annotation (plain text) strip covering annotations.
	if ( doc.data.isPlainText( newDocRange, false, [ 'paragraph' ], annotate ) ) {
		const data = doc.data.getDataSlice( newDocRange );
		for ( let i = 0, l = data.length; i < l; i++ ) {
			// Remove any text annotations, as we have determined them to be covering
			Iif ( Array.isArray( data[ i ] ) ) {
				data[ i ] = data[ i ][ 0 ];
			}
			// Convert newlines to paragraph breaks (T156498)
			Iif ( data[ i ] === '\r' ) {
				data.splice( i, 1 );
				i--;
				l--;
			} else if ( data[ i ] === '\n' ) {
				data.splice( i, 1, { type: '/paragraph' }, { type: 'paragraph' } );
				i++;
				l++;
			} else Iif ( data[ i ] === '\u00a0' ) {
				// Replace non-breaking spaces, they were probably created during HTML conversion
				// and are invisible to the user so likely to just cause problems.
				// If you want a non-breaking space in source you should probably use &nbsp; T154382
				data[ i ] = ' ';
			}
		}
		return ve.dm.SourceSurfaceFragment.super.prototype.insertContent.call( this, data );
	}
 
	this.pushPending(
		this.convertToSource( doc ).then(
			( source ) => {
				this.insertContent( source.trim() );
			},
			function () {
				ve.error( 'Failed to convert document', arguments );
				return ve.createDeferred().reject().promise();
			}
		)
	);
 
	return this;
};
 
/**
 * @inheritdoc
 */
ve.dm.SourceSurfaceFragment.prototype.wrapAllNodes = function ( wrapOuter, wrapEach ) {
	const range = this.getSelection().getCoveringRange();
 
	if ( !range ) {
		return this;
	}
 
	function getOpening( element ) {
		element.internal = {
			whitespace: [ '\n', '\n', '\n', '\n' ]
		};
		return element;
	}
 
	function getClosing( element ) {
		return { type: '/' + element.type };
	}
 
	if ( !Array.isArray( wrapOuter ) ) {
		wrapOuter = [ wrapOuter ];
	}
 
	wrapEach = wrapEach || [];
 
	if ( !Array.isArray( wrapEach ) ) {
		wrapEach = [ wrapEach ];
	}
 
	const nodes = this.getSelectedLeafNodes();
 
	const content = wrapOuter.map( getOpening );
	for ( let i = 0; i < nodes.length; i++ ) {
		const node = nodes[ i ];
		ve.batchPush( content, wrapEach.map( getOpening ) );
		ve.batchPush( content, this.getSurface().getLinearFragment( node.getRange() ).getText().split( '' ) );
		ve.batchPush( content, wrapEach.reverse().map( getClosing ) );
	}
	ve.batchPush( content, wrapOuter.reverse().map( getClosing ) );
 
	this.insertContent( content );
 
	return this;
};
 
/**
 * Convert model slice to source text
 *
 * The default implementation converts to HTML synchronously.
 *
 * If the conversion is asynchronous it should lock the surface
 * until complete.
 *
 * @param {ve.dm.Document} doc
 * @return {jQuery.Promise} Promise which resolves with source, or rejects
 */
ve.dm.SourceSurfaceFragment.prototype.convertToSource = function ( doc ) {
	Iif ( !doc.data.hasContent() ) {
		return ve.createDeferred().resolve( '' ).promise();
	} else {
		return ve.createDeferred().resolve(
			ve.properInnerHtml(
				ve.dm.converter.getDomFromModel( doc ).body
			)
		).promise();
	}
};
 
/**
 * Convert source text to model slice
 *
 * The default implementation converts from HTML synchronously.
 *
 * If the conversion is asynchronous it should lock the surface
 * until complete.
 *
 * @param {string} source Source text
 * @return {jQuery.Promise} Promise which resolves with document model
 */
ve.dm.SourceSurfaceFragment.prototype.convertFromSource = function ( source ) {
	const lang = this.getDocument().getLang(),
		dir = this.getDocument().getDir();
 
	if ( !source ) {
		return ve.createDeferred().resolve(
			new ve.dm.Document(
				[
					{ type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' },
					{ type: 'internalList' }, { type: '/internalList' }
				],
				null, null, null, null,
				lang, dir
			)
		).promise();
	} else {
		return ve.createDeferred().resolve(
			ve.dm.converter.getModelFromDom(
				ve.createDocumentFromHtml( source, { lang: lang, dir: dir } )
			)
		).promise();
	}
};