Catrope has uploaded a new change for review. https://gerrit.wikimedia.org/r/50028
Change subject: Make string type matches override regex type matches ...................................................................... Make string type matches override regex type matches Change-Id: Ic21e27c047d8cb1161a6613bb5232d0d19b37aa6 --- M modules/ve/dm/ve.dm.ModelRegistry.js M modules/ve/test/dm/ve.dm.ModelRegistry.test.js 2 files changed, 27 insertions(+), 9 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor refs/changes/28/50028/1 diff --git a/modules/ve/dm/ve.dm.ModelRegistry.js b/modules/ve/dm/ve.dm.ModelRegistry.js index f265a1c..961095c 100644 --- a/modules/ve/dm/ve.dm.ModelRegistry.js +++ b/modules/ve/dm/ve.dm.ModelRegistry.js @@ -201,13 +201,16 @@ } function matchWithFunc( types, tag ) { - var i, queue = []; + var i, queue = [], queue2 = []; for ( i = 0; i < types.length; i++ ) { - queue = queue - .concat( ve.getProp( reg.modelsByTypeAndTag, 1, types[i], tag ) || [] ) - .concat( matchTypeRegExps( types[i], tag, true ) ); + // Queue string matches and regexp matches separately + queue = queue.concat( ve.getProp( reg.modelsByTypeAndTag, 1, types[i], tag ) || [] ); + queue2 = queue2.concat( matchTypeRegExps( types[i], tag, true ) ); } + // Try string matches first, then regexp matches queue.sort( byRegistrationOrderDesc ); + queue2.sort( byRegistrationOrderDesc ); + queue = queue.concat( queue2 ); for ( i = 0; i < queue.length; i++ ) { if ( reg.registry[queue[i]].static.matchFunction( element ) ) { return queue[i]; @@ -217,12 +220,14 @@ } function matchWithoutFunc( types, tag ) { - var i, queue = [], winningName = null; + var i, queue = [], queue2 = [], winningName = null; for ( i = 0; i < types.length; i++ ) { - queue = queue - .concat( ve.getProp( reg.modelsByTypeAndTag, 0, types[i], tag ) || [] ) - .concat( matchTypeRegExps( types[i], tag, false ) ); + // Queue string and regexp matches separately + queue = queue.concat( ve.getProp( reg.modelsByTypeAndTag, 0, types[i], tag ) || [] ); + queue2 = queue2.concat( matchTypeRegExps( types[i], tag, false ) ); } + // Try string matches first, then regexp matches + queue = queue.concat( queue2 ); for ( i = 0; i < queue.length; i++ ) { if ( winningName === null || diff --git a/modules/ve/test/dm/ve.dm.ModelRegistry.test.js b/modules/ve/test/dm/ve.dm.ModelRegistry.test.js index 379ff45..a760a2a 100644 --- a/modules/ve/test/dm/ve.dm.ModelRegistry.test.js +++ b/modules/ve/test/dm/ve.dm.ModelRegistry.test.js @@ -83,6 +83,14 @@ ve.dm.StubBarNode.static.toDataElement = function () {}; ve.dm.StubBarNode.static.toDomElements = function () {}; +ve.dm.StubAbbrNode = function VeDmStubAbbrNode( children, element ) { + ve.dm.BranchNode.call( this, 'stub-abbr', children, element ); +}; +ve.inheritClass( ve.dm.StubAbbrNode, ve.dm.BranchNode ); +ve.dm.StubAbbrNode.static.name = 'stub-abbr'; +ve.dm.StubAbbrNode.static.matchTagNames = ['abbr']; +ve.dm.StubAbbrNode.static.matchRdfaTypes = ['mw:abbr']; + ve.dm.StubRegExpNode = function VeDmStubRegExpNode( children, element ) { ve.dm.BranchNode.call( this, 'stub-regexp', children, element ); }; @@ -93,7 +101,7 @@ /* Tests */ -QUnit.test( 'matchElement', 18, function ( assert ) { +QUnit.test( 'matchElement', 20, function ( assert ) { var registry = new ve.dm.ModelRegistry(), element; element = document.createElement( 'a' ); assert.deepEqual( registry.matchElement( element ), null, 'matchElement() returns null if registry empty' ); @@ -107,6 +115,7 @@ registry.register( ve.dm.StubSingleTypeAndFuncAnnotation ); registry.register( ve.dm.StubSingleTagAndTypeAndFuncAnnotation ); registry.register( ve.dm.StubBarNode ); + registry.register( ve.dm.StubAbbrNode ); registry.register( ve.dm.StubRegExpNode ); element = document.createElement( 'b' ); @@ -129,6 +138,8 @@ element = document.createElement( 'abbr' ); element.setAttribute( 'rel', 'mw:baz' ); assert.deepEqual( registry.matchElement( element ), 'stub-regexp', 'RegExp type match' ); + element.setAttribute( 'rel', 'mw:abbr' ); + assert.deepEqual( registry.matchElement( element ), 'stub-abbr', 'String match overrides RegExp match' ); registry.registerExtensionSpecificType( /^mw:/ ); registry.registerExtensionSpecificType( 'foo' ); @@ -150,4 +161,6 @@ element = document.createElement( 'abbr' ); element.setAttribute( 'rel', 'mw:baz' ); assert.deepEqual( registry.matchElement( element ), 'stub-regexp', 'RegExp type match for extension-specific type' ); + element.setAttribute( 'rel', 'mw:abbr' ); + assert.deepEqual( registry.matchElement( element ), 'stub-abbr', 'String match overrides RegExp match for extension-specific type' ); } ); -- To view, visit https://gerrit.wikimedia.org/r/50028 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic21e27c047d8cb1161a6613bb5232d0d19b37aa6 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Catrope <roan.katt...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits