' )
.addClass( 've-ce-mwReferencesListNode-muted' )
.text( ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) )
).attr( 'data-diff-action', action );
}
listItemNode.setAttribute( 'value', index + 1 );
return [ listItemNode ];
};
/**
* Get the HTML for the linear diff of a single internal list item that has changed
* from the old document to the new document.
*
* @param {Object} diff List item diff
* @param {ve.dm.InternalItemNode} oldNode
* @param {ve.dm.InternalItemNode} newNode
* @param {string|null} move 'up' or 'down' if the node has moved
* @param {number} newIndex
* @return {HTMLElement[]} HTML elements to display the linear diff
*/
ve.ui.DiffElement.prototype.getInternalListChangedNodeElements = function ( diff, oldNode, newNode, move, newIndex ) {
const listItemNode = document.createElement( 'li' );
let data = this.getChangedDocListData( newNode, diff, true );
// Remove internal list wrapper
data = data.slice( 1, data.length - 2 );
this.markMove( move, listItemNode );
const newDoc = newNode.getRoot().getDocument();
const documentSlice = newDoc.cloneWithData( data, true, true );
const body = ve.dm.converter.getDomFromModel( documentSlice, ve.dm.Converter.static.PREVIEW_MODE ).body;
while ( body.childNodes.length ) {
listItemNode.appendChild(
listItemNode.ownerDocument.adoptNode( body.childNodes[ 0 ] )
);
}
listItemNode.setAttribute( 'value', newIndex + 1 );
return [ listItemNode ];
};
/**
* Compare attributes of two nodes
*
* @param {Array} data Linear data containing new node
* @param {number} offset Offset in data
* @param {Object} attributeChange Attribute change object containing oldAttributes and newAttributes
* @return {OO.ui.OptionWidget|null} Change description item, or null if nothing to describe
*/
ve.ui.DiffElement.prototype.compareNodeAttributes = function ( data, offset, attributeChange ) {
const attributeChanges = this.constructor.static.compareAttributes( attributeChange.oldAttributes, attributeChange.newAttributes );
const changes = ve.dm.modelRegistry.lookup( data[ offset ].type ).static.describeChanges( attributeChanges, attributeChange.newAttributes, data[ offset ] );
// Don't describe the same change twice
if ( changes.length && (
!( data[ offset ].internal ) ||
!( data[ offset ].internal.diff ) ||
data[ offset ].internal.diff[ 'data-diff-id' ] === undefined )
) {
const item = this.getChangeDescriptionItem( changes );
this.addAttributesToElement( data, offset, { 'data-diff-id': item.getData() } );
return item;
}
return null;
};
/**
* Get a change description item from a set of changes
*
* @param {Array} changes List of changes, each change being either text or a Node array
* @param {string[]} classes Additional classes
* @return {OO.ui.OptionWidget} Change description item
*/
ve.ui.DiffElement.prototype.getChangeDescriptionItem = function ( changes, classes ) {
const elementId = this.elementId;
let $label = $( [] );
for ( let i = 0, l = changes.length; i < l; i++ ) {
const $change = $( '' );
if ( typeof changes[ i ] === 'string' ) {
$change.text( changes[ i ] );
} else {
// changes[ i ] is definitely not an HTML string in this branch
// eslint-disable-next-line no-jquery/no-append-html
$change.append( changes[ i ] );
}
$label = $label.add( $change );
}
// eslint-disable-next-line mediawiki/class-doc
const item = new OO.ui.OptionWidget( {
label: $label,
data: elementId,
classes: [ 've-ui-diffElement-attributeChange', ...classes || [] ]
} );
this.elementId++;
return item;
};
/**
* Mark an element with attributes to be added later by the converter.
*
* @param {Array} data Data containing element to be marked
* @param {number} offset Offset of element to be marked
* @param {Object} attributes Attributes to set
*/
ve.ui.DiffElement.prototype.addAttributesToElement = function ( data, offset, attributes ) {
const newElement = ve.copy( data[ offset ] );
// NB we modify the linear data here, but then this is a cloned document.
for ( const key in attributes ) {
if ( attributes[ key ] !== undefined ) {
ve.setProp( newElement, 'internal', 'diff', key, attributes[ key ] );
}
}
// Don't let any nodes get unwrapped
ve.deleteProp( newElement, 'internal', 'generated' );
data.splice( offset, 1, newElement );
};
/**
* Mark an HTML element or data element as moved
*
* @param {string|null} move 'up' or 'down' if the node has moved
* @param {HTMLElement|Array} elementOrData Linear data or HTMLElement
* @param {number} [offset=0] Linear mode offset
*/
ve.ui.DiffElement.prototype.markMove = function ( move, elementOrData, offset ) {
if ( !move ) {
return;
}
// The following messages are used here:
// * visualeditor-diff-moved-up
// * visualeditor-diff-moved-down
// The following classes are used here:
// * ve-ui-diffElement-moved-up
// * ve-ui-diffElement-moved-down
const item = this.getChangeDescriptionItem( [ ve.msg( 'visualeditor-diff-moved-' + move ) ], [ 've-ui-diffElement-moved-' + move ] );
if ( Array.isArray( elementOrData ) ) {
this.addAttributesToElement( elementOrData, offset || 0, { 'data-diff-move': move, 'data-diff-id': item.getData() } );
} else {
elementOrData.setAttribute( 'data-diff-move', move );
elementOrData.setAttribute( 'data-diff-id', item.getData() );
}
this.descriptionItemsStack.push( item );
};
/**
* Annotate some data to highlight diff
*
* @param {Array} linearDiff Linear diff, mapping arrays of linear data to diff
* actions (remove, insert or retain)
* @param {ve.dm.Node} newNode Node from the new document
* @return {Array} Data with annotations added
*/
ve.ui.DiffElement.prototype.annotateNode = function ( linearDiff, newNode ) {
const DIFF_DELETE = ve.DiffMatchPatch.static.DIFF_DELETE,
DIFF_INSERT = ve.DiffMatchPatch.static.DIFF_INSERT,
DIFF_CHANGE_DELETE = ve.DiffMatchPatch.static.DIFF_CHANGE_DELETE,
DIFF_CHANGE_INSERT = ve.DiffMatchPatch.static.DIFF_CHANGE_INSERT,
items = [],
newDoc = newNode.getRoot().getDocument();
let start = 0; // The starting index for a range for building an annotation
// Make a new document from the diff
const diffDocData = linearDiff[ 0 ][ 1 ].slice();
const ilen = linearDiff.length;
for ( let i = 1; i < ilen; i++ ) {
ve.batchPush( diffDocData, linearDiff[ i ][ 1 ] );
}
const diffDoc = newDoc.cloneWithData( diffDocData );
// Add spans with the appropriate attributes for removes and inserts
// TODO: do insert and remove outside of loop
for ( let i = 0; i < ilen; i++ ) {
const end = start + linearDiff[ i ][ 1 ].length;
if ( start !== end ) {
const range = new ve.Range( start, end );
const type = linearDiff[ i ][ 0 ];
if ( type !== 0 ) {
let typeAsString, domElementType, annType;
switch ( type ) {
case DIFF_DELETE:
typeAsString = 'remove';
domElementType = 'del';
annType = 'textStyle/delete';
break;
case DIFF_INSERT:
typeAsString = 'insert';
domElementType = 'ins';
annType = 'textStyle/insert';
break;
case DIFF_CHANGE_DELETE:
typeAsString = 'change-remove';
domElementType = 'span';
annType = 'textStyle/span';
break;
case DIFF_CHANGE_INSERT:
typeAsString = 'change-insert';
domElementType = 'span';
annType = 'textStyle/span';
break;
}
const domElement = document.createElement( domElementType );
domElement.setAttribute( 'data-diff-action', typeAsString );
const domElements = [ domElement ];
const changes = [];
if ( linearDiff[ i ].annotationChanges ) {
linearDiff[ i ].annotationChanges.forEach( ( annotationChange ) => {
let attributeChanges;
if ( annotationChange.oldAnnotation && annotationChange.newAnnotation ) {
attributeChanges = this.constructor.static.compareAttributes(
annotationChange.oldAnnotation.getAttributes(),
annotationChange.newAnnotation.getAttributes()
);
ve.batchPush( changes, ve.dm.modelRegistry.lookup( annotationChange.newAnnotation.getType() ).static.describeChanges(
attributeChanges, annotationChange.newAnnotation.getAttributes(), annotationChange.newAnnotation.getElement()
) );
} else if ( annotationChange.newAnnotation ) {
ve.batchPush( changes, annotationChange.newAnnotation.describeAdded() );
} else if ( annotationChange.oldAnnotation ) {
ve.batchPush( changes, annotationChange.oldAnnotation.describeRemoved() );
}
} );
}
if ( linearDiff[ i ].attributeChanges ) {
const element = linearDiff[ i ][ 1 ][ 0 ];
linearDiff[ i ].attributeChanges.forEach( ( attributeChange ) => {
const attributeChanges = this.constructor.static.compareAttributes(
attributeChange.oldAttributes,
attributeChange.newAttributes
);
ve.batchPush( changes, ve.dm.modelRegistry.lookup( element.type ).static.describeChanges(
attributeChanges, element.attributes, element
) );
} );
}
if ( changes.length ) {
const item = this.getChangeDescriptionItem( changes );
domElement.setAttribute( 'data-diff-id', item.getData() );
items.push( item );
}
const originalDomElementsHash = diffDoc.getStore().hash(
domElements,
domElements.map( ve.getNodeHtml ).join( '' )
);
const annHash = diffDoc.getStore().hash(
ve.dm.annotationFactory.create( annType, {
type: annType,
originalDomElementsHash: originalDomElementsHash
} )
);
// Insert annotation above annotations that span the entire range
// and at least one character more
const annHashLists = [];
for (
let j = Math.max( 0, range.start - 1 );
j < Math.min( range.end + 1, diffDoc.data.getLength() );
j++
) {
annHashLists[ j ] =
diffDoc.data.getAnnotationHashesFromOffset( j );
}
const height = Math.min(
ve.getCommonStartSequenceLength(
annHashLists.slice(
Math.max( 0, range.start - 1 ),
range.end
)
),
ve.getCommonStartSequenceLength(
annHashLists.slice(
range.start,
Math.min( range.end + 1, diffDoc.data.getLength() )
)
)
);
for ( let j = range.start; j < range.end; j++ ) {
annHashLists[ j ].splice( height, 0, annHash );
diffDoc.data.setAnnotationHashesAtOffset(
j,
annHashLists[ j ]
);
}
}
}
start = end;
}
this.descriptionItemsStack.push.apply( this.descriptionItemsStack, items );
// Merge the stores and get the data
newDoc.getStore().merge( diffDoc.getStore() );
const annotatedLinearDiff = diffDoc.getData( { start: 0, end: diffDoc.getLength() } );
return annotatedLinearDiff;
};
ve.ui.metaListDiffRegistry = new OO.Registry();