Project

General

Profile

I have created WLineEdit::setAutocomplete(), looking for feedback

Added by Steve Drake about 11 years ago

Similar to the existing WLineEdit::setEchoMode(), I have created a WLineEdit::setAutocomplete() method to allow application control over the "autocomplete" attribute of an input field.

The most relevant code looks like this:

void WLineEdit::setAutocomplete(bool enabled)
{
  if (autocomplete_ != enabled) {
    autocomplete_ = enabled;
    flags_.set(BIT_AUTOCOMPLETE_CHANGED);
    repaint(RepaintPropertyAttribute);
  }
}

void WLineEdit::updateDom(DomElement& element, bool all)
{
  if (all || flags_.test(BIT_CONTENT_CHANGED)) {
    element.setProperty(Wt::PropertyValue, content_.toUTF8());
    flags_.reset(BIT_CONTENT_CHANGED);
  }

  if (all || flags_.test(BIT_ECHO_MODE_CHANGED)) {
    element.setAttribute("type", echoMode_ == Normal ? "text" : "password");
    flags_.reset(BIT_ECHO_MODE_CHANGED);
  }

  if (all || flags_.test(BIT_AUTOCOMPLETE_CHANGED)) {
    element.setAttribute("autocomplete", autocomplete_ == true ? "on" : "off");
    flags_.reset(BIT_AUTOCOMPLETE_CHANGED);
  }

  if (all || flags_.test(BIT_TEXT_SIZE_CHANGED)) {
    element.setAttribute("size",
             boost::lexical_cast<std::string>(textSize_));
    flags_.reset(BIT_TEXT_SIZE_CHANGED);
  }

  if (all || flags_.test(BIT_MAX_LENGTH_CHANGED)) {
    if (!all || maxLength_ > 0)
      element.setAttribute("maxLength",
               boost::lexical_cast<std::string>(maxLength_));

    flags_.reset(BIT_MAX_LENGTH_CHANGED);
  }

  WFormWidget::updateDom(element, all);
}

In my web app, I turn off autocomplete for a handful of WLineEdit instances, but leave it the default for most. When I inspect the generated HTML, I see that for every WLineEdit widget, the autocomplete attribute is specified as either "on" or "off" and has the expected value, such as:

   <input id="ogsbin3" autocomplete="on" maxlength="40" ...

or

   <input id="ogsbin3" autocomplete="off" maxlength="40" ...

Previous to my change, the autocomplete attribute was always absent, which allowed it to default to "on", such as:

   <input id="ogsbin3" maxlength="40" ...

Does this seem like a pretty safe way that I have implemented the feature in Wt? Or should I change something such that autocomplete="off" is not emitted and just let it default to "off"?

(When I am done with this change I can submit a patch for potential inclusion in Wt.)

Thanks!

Steve


Replies (2)

RE: I have created WLineEdit::setAutocomplete(), looking for feedback - Added by Koen Deforche about 11 years ago

Hey Steve,

This sounds useful indeed!

The choice you are facing is one that we resolve with backwards compatibility in mind --- so if the autocomplete attribute is absent then it defaults to "on", and we usually do not emit the attribute in that case (if the spec really specifies the default value).

So you need an extra check:

if (!all || !autoComplete_)

This avoids emitting the default value for an initial rendering.

Regards,

koen

RE: I have created WLineEdit::setAutocomplete(), looking for feedback - Added by Steve Drake about 11 years ago

Good suggestion. I incorporated the change and it works as expected. Attached is a patch that can be applied to Wt 3.2.1.

Steve

    (1-2/2)