Overwriting selected text in input elem with maxlength chars

Discussion of features and help with problems encountered while using libRocket

Overwriting selected text in input elem with maxlength chars

Postby RobertP on Thu Nov 19, 2015 11:43 pm

Let's say you have a text input element that has its maxlength attribute set to 4:

pseudocode rml:
Code: Select all
<input type="text" maxlength="4" />


Then you type into the element until it fills up to its maxlength.

In this example, after the 4th character is typed, text can no longer be added to the text input field. If you try to type more text into the field beyond the 4th character, your keystrokes are ignored. This is expected.

However, if you select some or all of the text that's already in the text field (by using a mouse click and drag, or by using shift + left/right arrow keys), the next character that you subsequently type should overwrite that selected text.

In libRocket, this doesn't work if the text field already contains maxlength characters. Any character that you type gets ignored, when what should happen is the text that is selected gets overwritten by the character you type.

If the text field contains less than maxlength characters, then overwriting the selected text works fine.

I'm guessing that the place to look would be in WidgetTextInput.cpp, WidgetTextInput::ProcessEvent(), in the block that begins with

Code: Select all
if (event == "textinput")


In that block, maybe try changing the innermost "if" condition from this:

Code: Select all
if (max_length < 0 || (int) Core::String(GetElement()->GetAttribute< Rocket::Core::String >("value", "")).Length() < max_length)


to this:

Code: Select all
if (max_length < 0 || (int) Core::String(GetElement()->GetAttribute< Rocket::Core::String >("value", "")).Length() - selection_length < max_length)


I'm not very familiar with the libRocket source code, so I'm not sure what side effects/repercussions this change would have.

I'm about to try it right now....
RobertP
 
Posts: 8
Joined: Tue Apr 07, 2015 2:22 pm

Return to Using libRocket