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
*
* @param {OO.ui.TextInputWidget} textInputWidget Text input widget
* @param {number} [limit] Byte limit, defaults to $input's maxlength
*
* @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' );
limit = limit || +textInputWidget.$input.attr( 'maxlength' );
+ if ( !filterFunction || typeof filterFunction !== 'function' ) {
+ filterFunction = undefined;
+ }
- 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 {
if ( remaining > 99 ) {
remaining = '';
} else {
updateCount();
// Actually enforce limit
updateCount();
// Actually enforce limit
- textInputWidget.$input.byteLimit( limit );
+ textInputWidget.$input.byteLimit( limit, filterFunction );
* Uses jQuery#codePointLimit to enforce the limit.
*
* @param {OO.ui.TextInputWidget} textInputWidget Text input widget
* 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' );
limit = limit || +textInputWidget.$input.attr( 'maxlength' );
+ if ( !filterFunction || typeof filterFunction !== 'function' ) {
+ filterFunction = undefined;
+ }
- 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 {
if ( remaining > 99 ) {
remaining = '';
} else {
updateCount();
// Actually enforce limit
updateCount();
// Actually enforce limit
- textInputWidget.$input.codePointLimit( limit );
+ textInputWidget.$input.codePointLimit( limit, filterFunction );