<commit_before>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file provides an abstract implementation of the inline autocomplete
// infrastructure defined in autocomplete_input_listener.h.

#include "webkit/glue/autocomplete_input_listener.h"

#pragma warning(push, 0)
#include "HTMLInputElement.h"
#include "HTMLFormElement.h"
#include "Document.h"
#include "Frame.h"
#include "Editor.h"
#include "EventNames.h"
#include "Event.h"
#pragma warning(pop)

#undef LOG

#include "base/logging.h"
#include "webkit/glue/editor_client_impl.h"
#include "webkit/glue/glue_util.h"

namespace webkit_glue {

// Hack (1 of 2) for http://bugs.webkit.org/show_bug.cgi?id=16976. This bug
// causes the caret position to be set after handling input events, which
// trumps our modifications, so for now we tell the EditorClient to preserve
// whatever selection set by our code.
// TODO(timsteele): Remove this function altogether once bug is fixed.
static void PreserveSelection(WebCore::HTMLInputElement* element) {
  WebCore::EditorClient* ec =
      element->form()->document()->frame()->editor()->client();
  EditorClientImpl* client = static_cast<EditorClientImpl*>(ec);
  client->PreserveSelection();
}

HTMLInputDelegate::HTMLInputDelegate(WebCore::HTMLInputElement* element)
    : element_(element) {
  // Reference the element for the lifetime of this delegate.
  // e is NULL when testing.
  if (element_)
    element_->ref();
}

HTMLInputDelegate::~HTMLInputDelegate() {
  if (element_)
    element_->deref();
}

bool HTMLInputDelegate::IsCaretAtEndOfText(size_t input_length,
                                           size_t previous_length) const {
  // Hack 2 of 2 for http://bugs.webkit.org/show_bug.cgi?id=16976.
  // TODO(timsteele): This check should only return early if
  // !(selectionEnd == selectionStart == user_input.length()).
  // However, because of webkit bug #16976 the caret is not properly moved
  // until after the handlers have executed, so for now we do the following
  // several checks. The first check handles the case webkit sets the End
  // selection but not the Start selection correctly, and the second is for
  // when webcore sets neither. This won't be perfect if the user moves the
  // selection around during inline autocomplete, but for now its the
  // friendliest behavior we can offer. Once the bug is fixed this method
  // should no longer need the previous_length parameter.
  if (((element_->selectionEnd() != element_->selectionStart() + 1) ||
       (element_->selectionEnd() != input_length)) &&
      ((element_->selectionEnd() != element_->selectionStart()) ||
       (element_->selectionEnd() != previous_length))) {
    return false;
  }
  return true;
}

void HTMLInputDelegate::SetValue(const std::wstring& value) {
  element_->setValue(StdWStringToString(value));
}

std::wstring HTMLInputDelegate::GetValue() const {
  return StringToStdWString(element_->value());
}

void HTMLInputDelegate::SetSelectionRange(size_t start, size_t end) {
  element_->setSelectionRange(start, end);
  // Hack, see comments for PreserveSelection().
  PreserveSelection(element_);
}

void HTMLInputDelegate::OnFinishedAutocompleting() {
  // This sets the input element to an autofilled state which will result in it
  // having a yellow background.
  element_->setAutofilled(true);
  // Notify any changeEvent listeners.
  element_->onChange();
}

AutocompleteInputListener::AutocompleteInputListener(<commit_msg>Fix linux compile issue.  Patch by <NAME> <<EMAIL>>. Original review at http://codereview.chromium.org/3170<commit_after>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file provides an abstract implementation of the inline autocomplete
// infrastructure defined in autocomplete_input_listener.h.

#include "webkit/glue/autocomplete_input_listener.h"

#pragma warning(push, 0)
#include "HTMLInputElement.h"
#include "HTMLFormElement.h"
#include "Document.h"
#include "Frame.h"
#include "Editor.h"
#include "EventNames.h"
#include "Event.h"
#pragma warning(pop)

#undef LOG

#include "base/logging.h"
#include "webkit/glue/editor_client_impl.h"
#include "webkit/glue/glue_util.h"

namespace webkit_glue {

// Hack (1 of 2) for http://bugs.webkit.org/show_bug.cgi?id=16976. This bug
// causes the caret position to be set after handling input events, which
// trumps our modifications, so for now we tell the EditorClient to preserve
// whatever selection set by our code.
// TODO(timsteele): Remove this function altogether once bug is fixed.
static void PreserveSelection(WebCore::HTMLInputElement* element) {
  WebCore::EditorClient* ec =
      element->form()->document()->frame()->editor()->client();
  EditorClientImpl* client = static_cast<EditorClientImpl*>(ec);
  client->PreserveSelection();
}

HTMLInputDelegate::HTMLInputDelegate(WebCore::HTMLInputElement* element)
    : element_(element) {
  // Reference the element for the lifetime of this delegate.
  // e is NULL when testing.
  if (element_)
    element_->ref();
}

HTMLInputDelegate::~HTMLInputDelegate() {
  if (element_)
    element_->deref();
}

bool HTMLInputDelegate::IsCaretAtEndOfText(size_t input_length,
                                           size_t previous_length) const {
  // Hack 2 of 2 for http://bugs.webkit.org/show_bug.cgi?id=16976.
  // TODO(timsteele): This check should only return early if
  // !(selectionEnd == selectionStart == user_input.length()).
  // However, because of webkit bug #16976 the caret is not properly moved
  // until after the handlers have executed, so for now we do the following
  // several checks. The first check handles the case webkit sets the End
  // selection but not the Start selection correctly, and the second is for
  // when webcore sets neither. This won't be perfect if the user moves the
  // selection around during inline autocomplete, but for now its the
  // friendliest behavior we can offer. Once the bug is fixed this method
  // should no longer need the previous_length parameter.
  if (((element_->selectionEnd() != element_->selectionStart() + 1) ||
       (element_->selectionEnd() != static_cast<int>(input_length))) &&
      ((element_->selectionEnd() != element_->selectionStart()) ||
       (element_->selectionEnd() != static_cast<int>(previous_length)))) {
    return false;
  }
  return true;
}

void HTMLInputDelegate::SetValue(const std::wstring& value) {
  element_->setValue(StdWStringToString(value));
}

std::wstring HTMLInputDelegate::GetValue() const {
  return StringToStdWString(element_->value());
}

void HTMLInputDelegate::SetSelectionRange(size_t start, size_t end) {
  element_->setSelectionRange(start, end);
  // Hack, see comments for PreserveSelection().
  PreserveSelection(element_);
}

void HTMLInputDelegate::OnFinishedAutocompleting() {
  // This sets the input element to an autofilled state which will result in it
  // having a yellow background.
  element_->setAutofilled(true);
  // Notify any changeEvent listeners.
  element_->onChange();
}

AutocompleteInputListener::AutocompleteInputListener(
{'fraction_non_alphanumeric': 0.11168902272105119, 'fraction_numerical': 0.008486175745962223, 'mean_word_length': 4.294927536231884, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3509271', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2027, 'n_words': 678}