All files / src/dm/nodes ve.dm.CommentNode.js

94.59% Statements 35/37
78.57% Branches 11/14
100% Functions 5/5
94.44% Lines 34/36

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                              1x   48x     48x         1x   1x       1x   1x   1x   29x 29x       29x                 1x 58x   11x 11x 11x 11x 11x 47x   19x 19x 19x 19x 19x 19x 19x 19x       28x 28x       1x 1x 1x 1x   1x            
/*!
 * VisualEditor DataModel CommentNode class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * @class
 * @abstract
 * @extends ve.dm.LeafNode
 * @mixes ve.dm.FocusableNode
 *
 * @constructor
 * @param {Object} element Reference to element in meta-linmod
 */
ve.dm.CommentNode = function VeDmCommentNode( element ) {
	// Parent constructor
	ve.dm.CommentNode.super.call( this, element );
 
	// Mixin constructors
	ve.dm.FocusableNode.call( this );
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.CommentNode, ve.dm.LeafNode );
 
OO.mixinClass( ve.dm.CommentNode, ve.dm.FocusableNode );
 
/* Static Properties */
 
ve.dm.CommentNode.static.isContent = true;
 
ve.dm.CommentNode.static.preserveHtmlAttributes = false;
 
ve.dm.CommentNode.static.toDataElement = function ( domElements, converter ) {
	let text;
	if ( domElements[ 0 ].nodeType === Node.COMMENT_NODE ) {
		text = ve.safeDecodeEntities( domElements[ 0 ].data );
	} else E{
		text = domElements[ 0 ].getAttribute( 'data-ve-comment' );
	}
	return {
		// Disallows comment nodes between table rows and such
		type: converter.isValidChildNodeType( 'comment' ) && text !== '' ? 'comment' : 'commentMeta',
		attributes: {
			text: text
		}
	};
};
 
ve.dm.CommentNode.static.toDomElements = function ( dataElement, doc, converter ) {
	if ( converter.isForClipboard() ) {
		// Fake comment node
		const span = doc.createElement( 'span' );
		span.setAttribute( 'rel', 've:Comment' );
		span.setAttribute( 'data-ve-comment', dataElement.attributes.text );
		span.appendChild( doc.createTextNode( '\u00a0' ) );
		return [ span ];
	} else if ( converter.isForPreview() ) {
		// isForPreview(), use CE rendering
		const modelNode = ve.dm.nodeFactory.createFromElement( dataElement );
		modelNode.setDocument( converter.internalList.getDocument() );
		const viewNode = ve.ce.nodeFactory.createFromModel( modelNode );
		viewNode.updateInvisibleIconSync( true );
		viewNode.$element.attr( 'title', dataElement.attributes.text );
		const els = viewNode.$element.toArray();
		viewNode.destroy();
		return els;
	} else {
		// Real comment node
		// Encode & - > (see T95040, T144708)
		const data = dataElement.attributes.text.replace( /[-&>]/g, ( c ) => '&#x' + c.charCodeAt( 0 ).toString( 16 ).toUpperCase() + ';' );
		return [ doc.createComment( data ) ];
	}
};
 
ve.dm.CommentNode.static.describeChange = function ( key, change ) {
	Eif ( key === 'text' ) {
		const diff = this.getAttributeDiff( change.from, change.to );
		if ( diff ) {
			// TODO: Use a word-break based diff for comment text
			return ve.htmlMsg( 'visualeditor-changedesc-comment-diff', diff );
		} else E{
			return ve.htmlMsg( 'visualeditor-changedesc-comment', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
		}
	}
};