From f2b4998faf4eab8e97989700bb4a57e0aefaf3a3 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Fri, 12 Jul 2013 22:21:20 +0100 Subject: [PATCH] HACK: Don't merge adjacent annotations from Parsoid Adjacent annotations should not be merged if they both originate from Parsoid. This is a hack because this logic should be in Parsoid, not VE. Bug: 49873 Change-Id: If1e23e3039178300d72b1c0c585931417bb603b5 --- modules/ve-mw/test/dm/ve.dm.mwExample.js | 83 +++++++++++++++++++++++- modules/ve/dm/ve.dm.Annotation.js | 35 +++++++++- modules/ve/dm/ve.dm.AnnotationSet.js | 5 +- 3 files changed, 117 insertions(+), 6 deletions(-) diff --git a/modules/ve-mw/test/dm/ve.dm.mwExample.js b/modules/ve-mw/test/dm/ve.dm.mwExample.js index d343293712..3373912cb6 100644 --- a/modules/ve-mw/test/dm/ve.dm.mwExample.js +++ b/modules/ve-mw/test/dm/ve.dm.mwExample.js @@ -535,6 +535,77 @@ ve.dm.mwExample.references = [ ]; ve.dm.mwExample.domToDataCases = { + 'adjacent annotations': { + 'html': + '' + + 'abcd ' + + 'ab ' + + 'abc' + + '', + 'data': [ + { 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } }, + [ 'a', [ ve.dm.example.bold ] ], + [ + 'b', + [ { + 'type': 'textStyle/bold', + 'htmlAttributes': [ { 'values': { + 'data-parsoid': '1' + } } ] + } ] + ], + [ 'c', [ ve.dm.example.bold ] ], + [ + 'd', + [ { + 'type': 'textStyle/bold', + 'htmlAttributes': [ { 'values': { + 'data-parsoid': '2' + } } ] + } ] + ], + ' ', + [ 'a', [ ve.dm.example.bold ] ], + [ 'b', [ ve.dm.example.bold ] ], + ' ', + [ + 'a', + [ { + 'type': 'textStyle/bold', + 'htmlAttributes': [ { 'values': { + 'data-parsoid': '3' + } } ] + } ] + ], + [ + 'b', + [ { + 'type': 'textStyle/bold', + 'htmlAttributes': [ { 'values': { + 'data-parsoid': '3' + } } ] + } ] + ], + [ + 'c', + [ { + 'type': 'textStyle/bold', + 'htmlAttributes': [ { 'values': { + 'data-parsoid': '4' + } } ] + } ] + ], + { 'type': '/paragraph' }, + { 'type': 'internalList' }, + { 'type': '/internalList' } + ], + 'normalizedHtml': + '' + + 'abcd ' + + 'ab ' + + 'abc' + + '' + }, 'mw:Image': { 'html': '

' + ve.dm.mwExample.MWInlineImageHtml + '

', 'data': [ @@ -1433,7 +1504,17 @@ ve.dm.mwExample.domToDataCases = { ] }, 'attribute preservation does not crash due to text node split': { - 'html': '
foo bar baz
', + 'html': + '' + + '
' + + '' + + '' + + '' + + '
' + + ' foo bar baz' + + '
' + + '
' + + '', 'data': [ { 'type': 'mwBlockImage', diff --git a/modules/ve/dm/ve.dm.Annotation.js b/modules/ve/dm/ve.dm.Annotation.js index 305b3a935d..0f8ccc4d38 100644 --- a/modules/ve/dm/ve.dm.Annotation.js +++ b/modules/ve/dm/ve.dm.Annotation.js @@ -111,4 +111,37 @@ ve.dm.Annotation.prototype.getComparableObjectForSerialization = function () { object.htmlAttributes = htmlAttributes; } return object; -}; \ No newline at end of file +}; + +/** + * HACK: Check if the annotation was generated by the converter + * + * Used by compareToForSerialization to avoid merging generated annotations. + * + * @returns {boolean} The annotation was generated + */ +ve.dm.Annotation.prototype.isGenerated = function () { + var attributes = this.getHtmlAttributes(); + return attributes[0] && attributes[0].values && attributes[0].values['data-parsoid']; +}; + +/** + * HACK: Compare to another annotation for serialization + * + * Compares two annotations using getComparableObjectForSerialization, unless + * they are both generated annotations, in which case they must be identical. + * + * @param {ve.dm.Annotation} annotation Annotation to compare to + * @returns {boolean} The other annotation is similar to this one + */ +ve.dm.Annotation.prototype.compareToForSerialization = function ( annotation ) { + // If both annotations were generated + if ( this.isGenerated() && annotation.isGenerated() ) { + return ve.compare( this, annotation ); + } + + return ve.compare( + this.getComparableObjectForSerialization(), + annotation.getComparableObjectForSerialization() + ); +}; diff --git a/modules/ve/dm/ve.dm.AnnotationSet.js b/modules/ve/dm/ve.dm.AnnotationSet.js index 0a7eb79ba8..bb62a49c29 100644 --- a/modules/ve/dm/ve.dm.AnnotationSet.js +++ b/modules/ve/dm/ve.dm.AnnotationSet.js @@ -268,10 +268,7 @@ ve.dm.AnnotationSet.prototype.containsComparable = function ( annotation ) { */ ve.dm.AnnotationSet.prototype.containsComparableForSerialization = function ( annotation ) { return this.filter( function ( a ) { - return ve.compare( - annotation.getComparableObjectForSerialization(), - a.getComparableObjectForSerialization() - ); + return annotation.compareToForSerialization( a ); }, true ); };