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

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

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                              1x   47x     47x         1x   1x       1x   1x   1x   28x 28x       28x                 1x 56x   11x 11x 11x 11x 11x 45x   19x 19x 19x 19x 19x 19x 19x 19x       26x 28x   26x       1x 1x 1x 1x   1x            
/*!
 * VisualEditor DataModel CommentNode class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * @class
 * @abstract
 * @extends ve.dm.LeafNode
 * @mixins 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 ) {
	var text;
	Eif ( domElements[ 0 ].nodeType === Node.COMMENT_NODE ) {
		text = ve.safeDecodeEntities( domElements[ 0 ].data );
	} else {
		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
		var 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
		var modelNode = ve.dm.nodeFactory.createFromElement( dataElement );
		modelNode.setDocument( converter.internalList.getDocument() );
		var viewNode = ve.ce.nodeFactory.createFromModel( modelNode );
		viewNode.updateInvisibleIconSync( true );
		viewNode.$element.attr( 'title', dataElement.attributes.text );
		var els = viewNode.$element.toArray();
		viewNode.destroy();
		return els;
	} else {
		// Real comment node
		// Encode & - > (see T95040, T144708)
		var data = dataElement.attributes.text.replace( /[-&>]/g, function ( c ) {
			return '&#x' + c.charCodeAt( 0 ).toString( 16 ).toUpperCase() + ';';
		} );
		return [ doc.createComment( data ) ];
	}
};
 
ve.dm.CommentNode.static.describeChange = function ( key, change ) {
	Eif ( key === 'text' ) {
		var diff = this.getAttributeDiff( change.from, change.to );
		Eif ( diff ) {
			// TODO: Use a word-break based diff for comment text
			return ve.htmlMsg( 'visualeditor-changedesc-comment-diff', diff );
		} else {
			return ve.htmlMsg( 'visualeditor-changedesc-comment', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
		}
	}
};