Jforrester has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/350891 )
Change subject: [WIP] ElementLinearData: Don't use IVStores, rely on annotation hashes ...................................................................... [WIP] ElementLinearData: Don't use IVStores, rely on annotation hashes Change-Id: I5a4f55c9eb6dc6d369319f444d80ac0c33c6c954 --- M src/dm/lineardata/ve.dm.ElementLinearData.js M tests/dm/lineardata/ve.dm.ElementLinearData.test.js 2 files changed, 56 insertions(+), 26 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/91/350891/1 diff --git a/src/dm/lineardata/ve.dm.ElementLinearData.js b/src/dm/lineardata/ve.dm.ElementLinearData.js index c53355d..59b61da 100644 --- a/src/dm/lineardata/ve.dm.ElementLinearData.js +++ b/src/dm/lineardata/ve.dm.ElementLinearData.js @@ -87,12 +87,10 @@ * * @param {Object|Array|string} a First element * @param {Object|Array|string} b Second element - * @param {ve.dm.IndexValueStore} aStore First element's store - * @param {ve.dm.IndexValueStore} [bStore] Second element's store, if different * @return {boolean} Elements are comparable */ -ve.dm.ElementLinearData.static.compareElements = function ( a, b, aStore, bStore ) { - var aType, aSet, bSet, aAnnotations, bAnnotations; +ve.dm.ElementLinearData.static.compareElements = function ( a, b ) { + var aType, aAnnotations, bAnnotations, annotation; if ( a === b ) { return true; @@ -125,10 +123,27 @@ bAnnotations = b.annotations; } - aSet = new ve.dm.AnnotationSet( aStore, aAnnotations || [] ); - bSet = new ve.dm.AnnotationSet( bStore || aStore, bAnnotations || [] ); - - return aSet.compareTo( bSet ); + if ( !aAnnotations && !bAnnotations ) { + // Neither is annotated + return true; + } + // eslint-disable-next-line no-bitwise + if ( aAnnotations ^ bAnnotations ) { + // One is annotated, the other isn't + return false; + } + if ( aAnnotations.length !== bAnnotations.length ) { + // One is more annotated + return false; + } + for ( annotation in aAnnotations ) { + if ( bAnnotations.indexOf( aAnnotations[ annotation ] ) === -1 ) { + // One lacks an annotation another has + return false; + } + } + // Elements are equal without annotations, and the annotation arrays are equal + return true; }; /* Methods */ diff --git a/tests/dm/lineardata/ve.dm.ElementLinearData.test.js b/tests/dm/lineardata/ve.dm.ElementLinearData.test.js index a46b2bb..3e87d81 100644 --- a/tests/dm/lineardata/ve.dm.ElementLinearData.test.js +++ b/tests/dm/lineardata/ve.dm.ElementLinearData.test.js @@ -2034,77 +2034,92 @@ } ); -QUnit.test( 'compareElementsUnannotated', function ( assert ) { +QUnit.test( 'compareElements and compareElementsUnannotated', function ( assert ) { var i, + boldHash = 'h49981eab0f8056ff', + italicHash = 'hefd27ef3bf2041dd', cases = [ { a: '母', b: '母', comparison: true, - msg: 'Identical unannotated characters are identical' + msg: 'Identical unannotated characters' }, { a: '다', b: '가', comparison: false, - msg: 'Non-identical unannotated characters are not identical' + msg: 'Non-identical unannotated characters' }, { - a: [ 'F', [ 0 ] ], - b: [ 'F', [ 0 ] ], + a: [ 'F', [ boldHash ] ], + b: [ 'F', [ boldHash ] ], comparison: true, - msg: 'Identically-annotated identical characters are identical' + msg: 'Identically-annotated identical characters' }, { - a: [ 'F', [ 0 ] ], - b: [ 'F', [ 1 ] ], - comparison: true, - msg: 'Identical characters, non-identically-annotated, are identical (!)' + a: [ 'F', [ boldHash ] ], + b: [ 'F', [ italicHash ] ], + comparison: false, + comparisonUnannotated: true, + msg: 'Identical characters, non-identically-annotated' }, { a: 'F', - b: [ 'F', [ 0 ] ], + b: [ 'F', [ boldHash ] ], + comparison: false, + comparisonUnannotated: true, + msg: 'Identical characters, one annotated, one not' + }, + { + a: { type: 'paragraph' }, + b: { type: 'paragraph' }, comparison: true, - msg: 'Identical characters, one annotated, one not, are identical (!)' + msg: 'Identical opening paragraphs' }, { a: { type: 'heading' }, b: { type: 'heading' }, comparison: true, - msg: 'Identical opening elements are identical' + msg: 'Identical opening elements' }, { a: { type: 'heading' }, b: { type: '/heading' }, comparison: false, - msg: 'Matching opening and closing elements are not identical' + msg: 'Matching opening and closing elements' }, { a: { type: 'heading', attributes: { level: 3 } }, b: { type: 'heading', attributes: { level: 3 } }, comparison: true, - msg: 'Identical elements with identical attributes are identical' + msg: 'Identical elements with identical attributes' }, { a: { type: 'heading', attributes: { level: 3 } }, b: { type: 'heading', attributes: { level: 2 } }, comparison: false, - msg: 'Identical elements with non-identical attributes are not identical' + msg: 'Identical elements with non-identical attributes' }, { a: { type: 'heading', attributes: { level: 3 } }, b: { type: 'heading' }, comparison: false, - msg: 'Identical elements, one without an attribute, are not identical' + msg: 'Identical elements, one without an attribute' } ]; for ( i = 0; i < cases.length; i++ ) { assert.equal( - ve.dm.ElementLinearData.static.compareElementsUnannotated( cases[ i ].a, cases[ i ].b ), + ve.dm.ElementLinearData.static.compareElements( cases[ i ].a, cases[ i ].b ), cases[ i ].comparison, cases[ i ].msg ); + assert.equal( + ve.dm.ElementLinearData.static.compareElementsUnannotated( cases[ i ].a, cases[ i ].b ), + cases[ i ].comparisonUnannotated || cases[ i ].comparison, + cases[ i ].msg + ' (unannotated)' + ); } } ); -- To view, visit https://gerrit.wikimedia.org/r/350891 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5a4f55c9eb6dc6d369319f444d80ac0c33c6c954 Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Jforrester <jforres...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits