From 7f2382ade76c6eca48bdb417f411bc41811db017 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Fri, 3 May 2024 14:37:28 +0100 Subject: [PATCH] Convert local functions to arrow functions and remove `this` bindings Change-Id: I2b0218bfe1688410c06c098723dd49580c74eeff --- src/ce/ve.ce.ContentBranchNode.js | 13 +++--- src/dm/ve.dm.Document.js | 20 ++++----- src/dm/ve.dm.ModelRegistry.js | 55 +++++++++++------------- src/init/sa/ve.init.sa.SafeStorage.js | 10 ++--- src/ui/widgets/ve.ui.CompletionWidget.js | 11 +++-- src/ve.EventSequencer.js | 7 +-- 6 files changed, 52 insertions(+), 64 deletions(-) diff --git a/src/ce/ve.ce.ContentBranchNode.js b/src/ce/ve.ce.ContentBranchNode.js index 179d937d9..7c07c378a 100644 --- a/src/ce/ve.ce.ContentBranchNode.js +++ b/src/ce/ve.ce.ContentBranchNode.js @@ -234,8 +234,7 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { annotations: null, unicorns: null }, - buffer = '', - node = this; + buffer = ''; // Source mode optimization if ( this.getModel().getDocument().sourceMode ) { @@ -248,7 +247,7 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { return wrapper; } - function openAnnotation( annotation ) { + var openAnnotation = ( annotation ) => { var ann; annotationsChanged = true; if ( buffer !== '' ) { @@ -261,14 +260,14 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { } // Create a new DOM node and descend into it annotation.store = store; - ann = ve.ce.annotationFactory.create( annotation.getType(), annotation, node ); + ann = ve.ce.annotationFactory.create( annotation.getType(), annotation, this ); ann.appendTo( current ); annotationStack.push( ann ); nodeStack.push( current ); current = ann.getContentContainer(); - } + }; - function closeAnnotation() { + var closeAnnotation = () => { var ann; annotationsChanged = true; if ( buffer !== '' ) { @@ -283,7 +282,7 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { ann = annotationStack.pop(); ann.attachContents(); current = nodeStack.pop(); - } + }; var i, ilen; // Gather annotated HTML from the child nodes diff --git a/src/dm/ve.dm.Document.js b/src/dm/ve.dm.Document.js index cca214a90..838d6c833 100644 --- a/src/dm/ve.dm.Document.js +++ b/src/dm/ve.dm.Document.js @@ -1151,36 +1151,34 @@ ve.dm.Document.prototype.rebuildTree = function () { * @param {ve.dm.Node[]} removedNodes Removed nodes */ ve.dm.Document.prototype.updateNodesByType = function ( addedNodes, removedNodes ) { - var doc = this; - - function remove( node ) { + var remove = ( node ) => { var type = node.getType(), - nodes = doc.nodesByType[ type ] || [], + nodes = this.nodesByType[ type ] || [], index = nodes.indexOf( node ); if ( index !== -1 ) { nodes.splice( index, 1 ); if ( !nodes.length ) { - delete doc.nodesByType[ type ]; + delete this.nodesByType[ type ]; } } - } + }; - function add( node ) { + var add = ( node ) => { var type = node.getType(), - nodes = doc.nodesByType[ type ] = doc.nodesByType[ type ] || []; + nodes = this.nodesByType[ type ] = this.nodesByType[ type ] || []; nodes.push( node ); - } + }; - function traverse( nodes, action ) { + var traverse = ( nodes, action ) => { nodes.forEach( ( node ) => { if ( node.hasChildren() ) { node.traverse( action ); } action( node ); } ); - } + }; traverse( removedNodes, remove ); traverse( addedNodes, add ); diff --git a/src/dm/ve.dm.ModelRegistry.js b/src/dm/ve.dm.ModelRegistry.js index d1d92a556..e617593e5 100644 --- a/src/dm/ve.dm.ModelRegistry.js +++ b/src/dm/ve.dm.ModelRegistry.js @@ -251,28 +251,25 @@ */ ve.dm.ModelRegistry.prototype.matchElement = function ( node, forceAboutGrouping, excludeTypes ) { var types, - nodeName = node.nodeName.toLowerCase(), - reg = this; + nodeName = node.nodeName.toLowerCase(); - function byRegistrationOrderDesc( a, b ) { - return reg.registrationOrder[ b ] - reg.registrationOrder[ a ]; - } + var byRegistrationOrderDesc = ( a, b ) => this.registrationOrder[ b ] - this.registrationOrder[ a ]; - function matchTypeRegExps( type, tag, withFunc ) { + var matchTypeRegExps = ( type, tag, withFunc ) => { var matchedModels = [], - models = reg.modelsWithTypeRegExps[ +withFunc ]; + models = this.modelsWithTypeRegExps[ +withFunc ]; for ( var j = 0; j < models.length; j++ ) { if ( excludeTypes && excludeTypes.indexOf( models[ j ] ) !== -1 ) { continue; } - var matchTypes = reg.registry[ models[ j ] ].static.getMatchRdfaTypes(); + var matchTypes = this.registry[ models[ j ] ].static.getMatchRdfaTypes(); for ( var k = 0; k < matchTypes.length; k++ ) { if ( matchTypes[ k ] instanceof RegExp && matchTypes[ k ].test( type ) && ( - ( tag === '' && reg.registry[ models[ j ] ].static.matchTagNames === null ) || - ( reg.registry[ models[ j ] ].static.matchTagNames || [] ).indexOf( tag ) !== -1 + ( tag === '' && this.registry[ models[ j ] ].static.matchTagNames === null ) || + ( this.registry[ models[ j ] ].static.matchTagNames || [] ).indexOf( tag ) !== -1 ) ) { matchedModels.push( models[ j ] ); @@ -280,9 +277,9 @@ } } return matchedModels; - } + }; - function allTypesAllowed( model ) { + var allTypesAllowed = ( model ) => { var allowedTypes = model.static.getAllowedRdfaTypes(); var matchTypes = model.static.getMatchRdfaTypes(); @@ -312,60 +309,60 @@ } } return true; - } + }; - function matchWithFunc( tag ) { + var matchWithFunc = ( tag ) => { var queue = [], queue2 = []; var j; for ( j = 0; j < types.length; j++ ) { // Queue string matches and regexp matches separately - queue = queue.concat( ve.getProp( reg.modelsByTypeAndTag, 1, types[ j ], tag ) || [] ); + queue = queue.concat( ve.getProp( this.modelsByTypeAndTag, 1, types[ j ], tag ) || [] ); if ( excludeTypes ) { queue = OO.simpleArrayDifference( queue, excludeTypes ); } queue2 = queue2.concat( matchTypeRegExps( types[ j ], tag, true ) ); } // Filter out matches which contain types which aren't allowed - queue = queue.filter( ( name ) => allTypesAllowed( reg.lookup( name ) ) ); - queue2 = queue2.filter( ( name ) => allTypesAllowed( reg.lookup( name ) ) ); + queue = queue.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); + queue2 = queue2.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); if ( forceAboutGrouping ) { // Filter out matches that don't support about grouping - queue = queue.filter( ( name ) => reg.registry[ name ].static.enableAboutGrouping ); - queue2 = queue2.filter( ( name ) => reg.registry[ name ].static.enableAboutGrouping ); + queue = queue.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); + queue2 = queue2.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); } // Try string matches first, then regexp matches queue.sort( byRegistrationOrderDesc ); queue2.sort( byRegistrationOrderDesc ); queue = queue.concat( queue2 ); for ( j = 0; j < queue.length; j++ ) { - if ( reg.registry[ queue[ j ] ].static.matchFunction( node ) ) { + if ( this.registry[ queue[ j ] ].static.matchFunction( node ) ) { return queue[ j ]; } } return null; - } + }; - function matchWithoutFunc( tag ) { + var matchWithoutFunc = ( tag ) => { var queue = [], queue2 = [], winningName = null; var j; for ( j = 0; j < types.length; j++ ) { // Queue string and regexp matches separately - queue = queue.concat( ve.getProp( reg.modelsByTypeAndTag, 0, types[ j ], tag ) || [] ); + queue = queue.concat( ve.getProp( this.modelsByTypeAndTag, 0, types[ j ], tag ) || [] ); if ( excludeTypes ) { queue = OO.simpleArrayDifference( queue, excludeTypes ); } queue2 = queue2.concat( matchTypeRegExps( types[ j ], tag, false ) ); } // Filter out matches which contain types which aren't allowed - queue = queue.filter( ( name ) => allTypesAllowed( reg.lookup( name ) ) ); - queue2 = queue2.filter( ( name ) => allTypesAllowed( reg.lookup( name ) ) ); + queue = queue.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); + queue2 = queue2.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); if ( forceAboutGrouping ) { // Filter out matches that don't support about grouping - queue = queue.filter( ( name ) => reg.registry[ name ].static.enableAboutGrouping ); - queue2 = queue2.filter( ( name ) => reg.registry[ name ].static.enableAboutGrouping ); + queue = queue.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); + queue2 = queue2.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); } // Only try regexp matches if there are no string matches queue = queue.length > 0 ? queue : queue2; @@ -373,13 +370,13 @@ for ( j = 0; j < queue.length; j++ ) { if ( winningName === null || - reg.registrationOrder[ winningName ] < reg.registrationOrder[ queue[ j ] ] + this.registrationOrder[ winningName ] < this.registrationOrder[ queue[ j ] ] ) { winningName = queue[ j ]; } } return winningName; - } + }; types = []; if ( node.getAttribute ) { diff --git a/src/init/sa/ve.init.sa.SafeStorage.js b/src/init/sa/ve.init.sa.SafeStorage.js index 52cfff7eb..a922136f3 100644 --- a/src/init/sa/ve.init.sa.SafeStorage.js +++ b/src/init/sa/ve.init.sa.SafeStorage.js @@ -135,13 +135,12 @@ * @inheritdoc */ ve.init.sa.SafeStorage.prototype.clearExpired = function () { - var storage = this; return this.getExpiryKeys().then( ( keys ) => $.Deferred( ( d ) => { - requestIdleCallback( function iterate( deadline ) { + var iterate = ( deadline ) => { while ( keys[ 0 ] !== undefined && deadline.timeRemaining() > MIN_WORK_TIME ) { var key = keys.shift(); - if ( storage.isExpired( key ) ) { - storage.remove( key ); + if ( this.isExpired( key ) ) { + this.remove( key ); } } if ( keys[ 0 ] !== undefined ) { @@ -150,7 +149,8 @@ } else { return d.resolve(); } - } ); + }; + requestIdleCallback( iterate ); } ) ); }; diff --git a/src/ui/widgets/ve.ui.CompletionWidget.js b/src/ui/widgets/ve.ui.CompletionWidget.js index 15694b7ba..5f8f8b318 100644 --- a/src/ui/widgets/ve.ui.CompletionWidget.js +++ b/src/ui/widgets/ve.ui.CompletionWidget.js @@ -235,18 +235,17 @@ ve.ui.CompletionWidget.prototype.onMenuToggle = function ( visible ) { */ ve.ui.CompletionWidget.prototype.onModelSelect = function () { var range = this.getCompletionRange(); - var widget = this; - function countMatches() { - var matches = widget.menu.getItems().length; - if ( widget.header.getLabel() !== null ) { + var countMatches = () => { + var matches = this.menu.getItems().length; + if ( this.header.getLabel() !== null ) { matches--; } - if ( widget.action.constructor.static.alwaysIncludeInput ) { + if ( this.action.constructor.static.alwaysIncludeInput ) { matches--; } return matches; - } + }; if ( !range || range.isBackwards() || this.action.shouldAbandon( this.surfaceModel.getDocument().data.getText( false, range ), countMatches() ) ) { this.teardown(); diff --git a/src/ve.EventSequencer.js b/src/ve.EventSequencer.js index ad9c5638e..6f189af58 100644 --- a/src/ve.EventSequencer.js +++ b/src/ve.EventSequencer.js @@ -48,7 +48,6 @@ * @param {string[]} eventNames List of event Names to listen to */ ve.EventSequencer = function VeEventSequencer( eventNames ) { - var eventSequencer = this; this.$node = null; this.eventNames = eventNames; this.eventHandlers = {}; @@ -60,11 +59,7 @@ ve.EventSequencer = function VeEventSequencer( eventNames ) { * @param {string} name The event's name * @return {Function} An event handler */ - function makeEventHandler( name ) { - return function ( ev ) { - return eventSequencer.onEvent( name, ev ); - }; - } + var makeEventHandler = ( name ) => ( ev ) => this.onEvent( name, ev ); /** * @property {Object[]} Pending calls