From d9e82bb8078d2b59d047799adcf9226103fa9081 Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Fri, 22 Mar 2019 13:47:05 +0100 Subject: [PATCH] Add filterFunction to visible{Byte,CodePoint}Limit The underlying {byte,codePoint}Limit functions already supported this parameter to apply to the string before limiting the length; add it to the visible limiting functions as well, displaying the remaining characters after the text is filtered. Change-Id: Id8f039ef21ed64dd97c6711efa56d59bb0264669 --- .../mediawiki.widgets.visibleLengthLimit.js | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/resources/src/mediawiki.widgets.visibleLengthLimit/mediawiki.widgets.visibleLengthLimit.js b/resources/src/mediawiki.widgets.visibleLengthLimit/mediawiki.widgets.visibleLengthLimit.js index 63da95a51d..453bf03dbe 100644 --- a/resources/src/mediawiki.widgets.visibleLengthLimit/mediawiki.widgets.visibleLengthLimit.js +++ b/resources/src/mediawiki.widgets.visibleLengthLimit/mediawiki.widgets.visibleLengthLimit.js @@ -14,12 +14,21 @@ * * @param {OO.ui.TextInputWidget} textInputWidget Text input widget * @param {number} [limit] Byte limit, defaults to $input's maxlength + * @param {Function} [filterFunction] Function to call on the string before assessing the length. */ - mw.widgets.visibleByteLimit = function ( textInputWidget, limit ) { + mw.widgets.visibleByteLimit = function ( textInputWidget, limit, filterFunction ) { limit = limit || +textInputWidget.$input.attr( 'maxlength' ); + if ( !filterFunction || typeof filterFunction !== 'function' ) { + filterFunction = undefined; + } function updateCount() { - var remaining = limit - byteLength( textInputWidget.getValue() ); + var value = textInputWidget.getValue(), + remaining; + if ( filterFunction ) { + value = filterFunction( value ); + } + remaining = limit - byteLength( value ); if ( remaining > 99 ) { remaining = ''; } else { @@ -32,7 +41,7 @@ updateCount(); // Actually enforce limit - textInputWidget.$input.byteLimit( limit ); + textInputWidget.$input.byteLimit( limit, filterFunction ); }; /** @@ -41,13 +50,22 @@ * Uses jQuery#codePointLimit to enforce the limit. * * @param {OO.ui.TextInputWidget} textInputWidget Text input widget - * @param {number} [limit] Byte limit, defaults to $input's maxlength + * @param {number} [limit] Code point limit, defaults to $input's maxlength + * @param {Function} [filterFunction] Function to call on the string before assessing the length. */ - mw.widgets.visibleCodePointLimit = function ( textInputWidget, limit ) { + mw.widgets.visibleCodePointLimit = function ( textInputWidget, limit, filterFunction ) { limit = limit || +textInputWidget.$input.attr( 'maxlength' ); + if ( !filterFunction || typeof filterFunction !== 'function' ) { + filterFunction = undefined; + } function updateCount() { - var remaining = limit - codePointLength( textInputWidget.getValue() ); + var value = textInputWidget.getValue(), + remaining; + if ( filterFunction ) { + value = filterFunction( value ); + } + remaining = limit - codePointLength( value ); if ( remaining > 99 ) { remaining = ''; } else { @@ -60,7 +78,7 @@ updateCount(); // Actually enforce limit - textInputWidget.$input.codePointLimit( limit ); + textInputWidget.$input.codePointLimit( limit, filterFunction ); }; }() ); -- 2.20.1