mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 10:35:48 +00:00
Only use HTML attributes when comparing annotations for serialisation
The UI still needs a simplified version of annotation comparison so it can clear/set annotations, whereas for serailisation we need to compare selected HTML attributes (and once Parsoid is fixed, compare all attributes). Change-Id: I1a5521a3f51ea373a21cfbc9a007a2381c12f4fe
This commit is contained in:
parent
d31918d145
commit
29d61dbea4
|
@ -52,8 +52,7 @@ ve.dm.LinkAnnotation.static.toDomElements = function ( dataElement, doc ) {
|
|||
ve.dm.LinkAnnotation.prototype.getComparableObject = function () {
|
||||
return {
|
||||
'type': this.getType(),
|
||||
'href': this.getAttribute( 'href' ),
|
||||
'htmlAttributes': this.getComparableHtmlAttributes()
|
||||
'href': this.getAttribute( 'href' )
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -69,8 +69,7 @@ ve.dm.TextStyleAnnotation.static.toDomElements = function ( dataElement, doc ) {
|
|||
|
||||
ve.dm.TextStyleAnnotation.prototype.getComparableObject = function () {
|
||||
return {
|
||||
'type': this.getType(),
|
||||
'htmlAttributes': this.getComparableHtmlAttributes()
|
||||
'type': this.getType()
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -79,13 +79,36 @@ ve.dm.Annotation.prototype.getComparableObject = function () {
|
|||
*
|
||||
* This should be removed once similar annotation merging is handled correctly
|
||||
* by Parsoid.
|
||||
*
|
||||
* @returns {Object} An object all HTML attributes except data-parsoid
|
||||
*/
|
||||
ve.dm.Annotation.prototype.getComparableHtmlAttributes = function () {
|
||||
var i, length, attributes = ve.copyArray( this.getHtmlAttributes() );
|
||||
for ( i = 0, length = attributes.length; i < length; i++ ) {
|
||||
if ( attributes[i].values ) {
|
||||
delete attributes[i].values['data-parsoid'];
|
||||
}
|
||||
var comparableAttributes, attributes = this.getHtmlAttributes();
|
||||
|
||||
if ( attributes[0] ) {
|
||||
comparableAttributes = ve.copyObject( attributes[0].values );
|
||||
delete comparableAttributes['data-parsoid'];
|
||||
return comparableAttributes;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
return attributes;
|
||||
};
|
||||
|
||||
/**
|
||||
* HACK: This method adds in HTML attributes so comparable objects aren't serialized
|
||||
* together if they have different HTML attributes.
|
||||
*
|
||||
* This method needs to be different from getComparableObject which is
|
||||
* still used for editing annotations.
|
||||
*
|
||||
* @returns {Object} An object containing a subset of the annotation's properties and HTML attributes
|
||||
*/
|
||||
ve.dm.Annotation.prototype.getComparableObjectForSerialization = function () {
|
||||
var object = this.getComparableObject(),
|
||||
htmlAttributes = this.getComparableHtmlAttributes();
|
||||
|
||||
if ( !ve.isEmptyObject( htmlAttributes ) ) {
|
||||
object.htmlAttributes = htmlAttributes;
|
||||
}
|
||||
return object;
|
||||
};
|
|
@ -256,6 +256,25 @@ ve.dm.AnnotationSet.prototype.containsComparable = function ( annotation ) {
|
|||
}, true );
|
||||
};
|
||||
|
||||
/**
|
||||
* HACK: Check if the set contains an annotation comparable to the specified one
|
||||
* for the purposes of serialization.
|
||||
*
|
||||
* This method uses getComparableObjectForSerialization which also includes
|
||||
* HTML attributes.
|
||||
*
|
||||
* @param {ve.dm.Annotation} annotation Annotation to compare to
|
||||
* @returns {boolean} At least one comprable annotation found
|
||||
*/
|
||||
ve.dm.AnnotationSet.prototype.containsComparableForSerialization = function ( annotation ) {
|
||||
return this.filter( function ( a ) {
|
||||
return ve.compare(
|
||||
annotation.getComparableObjectForSerialization(),
|
||||
a.getComparableObjectForSerialization()
|
||||
);
|
||||
}, true );
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the set contains at least one annotation where a given property matches a given filter.
|
||||
*
|
||||
|
|
|
@ -74,7 +74,7 @@ ve.dm.Converter.openAndCloseAnnotations = function ( currentSet, targetSet, open
|
|||
// Go through annotationStack from bottom to top (low to high),
|
||||
// and find the first annotation that's not in annotations.
|
||||
for ( i = 0, len = currentSet.getLength(); i < len; i++ ) {
|
||||
if ( !targetSet.containsComparable( currentSet.get( i ) ) ) {
|
||||
if ( !targetSet.containsComparableForSerialization( currentSet.get( i ) ) ) {
|
||||
startClosingAt = i;
|
||||
break;
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ ve.dm.Converter.openAndCloseAnnotations = function ( currentSet, targetSet, open
|
|||
// Open annotations as needed
|
||||
for ( i = 0, len = targetSet.getLength(); i < len; i++ ) {
|
||||
annotation = targetSet.get( i );
|
||||
if ( !currentSet.containsComparable( annotation ) ) {
|
||||
if ( !currentSet.containsComparableForSerialization( annotation ) ) {
|
||||
open( annotation );
|
||||
// Add to currentClone
|
||||
currentSet.push( annotation );
|
||||
|
|
Loading…
Reference in a new issue