SearchInputType.cpp [plain text]
#include "config.h"
#include "SearchInputType.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "InputTypeNames.h"
#include "KeyboardEvent.h"
#include "RenderSearchField.h"
#include "ShadowRoot.h"
#include "TextControlInnerElements.h"
namespace WebCore {
using namespace HTMLNames;
SearchInputType::SearchInputType(HTMLInputElement& element)
: BaseTextInputType(element)
, m_resultsButton(nullptr)
, m_cancelButton(nullptr)
, m_searchEventTimer(*this, &SearchInputType::searchEventTimerFired)
{
}
SearchInputType::~SearchInputType()
{
}
void SearchInputType::addSearchResult()
{
#if !PLATFORM(IOS)
if (is<RenderSearchField>(element().renderer()))
downcast<RenderSearchField>(*element().renderer()).addSearchResult();
#endif
}
static void updateResultButtonPseudoType(SearchFieldResultsButtonElement& resultButton, int maxResults)
{
if (!maxResults)
resultButton.setPseudo(AtomicString("-webkit-search-results-decoration", AtomicString::ConstructFromLiteral));
else if (maxResults < 0)
resultButton.setPseudo(AtomicString("-webkit-search-decoration", AtomicString::ConstructFromLiteral));
else if (maxResults > 0)
resultButton.setPseudo(AtomicString("-webkit-search-results-button", AtomicString::ConstructFromLiteral));
}
void SearchInputType::maxResultsAttributeChanged()
{
if (m_resultsButton)
updateResultButtonPseudoType(*m_resultsButton, element().maxResults());
}
RenderPtr<RenderElement> SearchInputType::createInputRenderer(RenderStyle&& style)
{
return createRenderer<RenderSearchField>(element(), WTFMove(style));
}
const AtomicString& SearchInputType::formControlType() const
{
return InputTypeNames::search();
}
bool SearchInputType::isSearchField() const
{
return true;
}
bool SearchInputType::needsContainer() const
{
return true;
}
void SearchInputType::createShadowSubtree()
{
ASSERT(!m_resultsButton);
ASSERT(!m_cancelButton);
TextFieldInputType::createShadowSubtree();
HTMLElement* container = containerElement();
HTMLElement* textWrapper = innerBlockElement();
ASSERT(container);
ASSERT(textWrapper);
m_resultsButton = SearchFieldResultsButtonElement::create(element().document());
updateResultButtonPseudoType(*m_resultsButton, element().maxResults());
container->insertBefore(*m_resultsButton, textWrapper);
m_cancelButton = SearchFieldCancelButtonElement::create(element().document());
container->insertBefore(*m_cancelButton, textWrapper->nextSibling());
}
HTMLElement* SearchInputType::resultsButtonElement() const
{
return m_resultsButton.get();
}
HTMLElement* SearchInputType::cancelButtonElement() const
{
return m_cancelButton.get();
}
void SearchInputType::handleKeydownEvent(KeyboardEvent& event)
{
if (element().isDisabledOrReadOnly()) {
TextFieldInputType::handleKeydownEvent(event);
return;
}
const String& key = event.keyIdentifier();
if (key == "U+001B") {
Ref<HTMLInputElement> input(this->element());
input->setValueForUser(emptyString());
input->onSearch();
event.setDefaultHandled();
return;
}
TextFieldInputType::handleKeydownEvent(event);
}
void SearchInputType::destroyShadowSubtree()
{
TextFieldInputType::destroyShadowSubtree();
m_resultsButton = nullptr;
m_cancelButton = nullptr;
}
void SearchInputType::startSearchEventTimer()
{
ASSERT(element().renderer());
unsigned length = element().innerTextValue().length();
if (!length) {
m_searchEventTimer.startOneShot(0_ms);
return;
}
m_searchEventTimer.startOneShot(std::max(200_ms, 600_ms - 100_ms * length));
}
void SearchInputType::stopSearchEventTimer()
{
m_searchEventTimer.stop();
}
void SearchInputType::searchEventTimerFired()
{
element().onSearch();
}
bool SearchInputType::searchEventsShouldBeDispatched() const
{
return element().hasAttributeWithoutSynchronization(incrementalAttr);
}
void SearchInputType::didSetValueByUserEdit()
{
if (m_cancelButton && is<RenderSearchField>(element().renderer()))
downcast<RenderSearchField>(*element().renderer()).updateCancelButtonVisibility();
if (searchEventsShouldBeDispatched())
startSearchEventTimer();
TextFieldInputType::didSetValueByUserEdit();
}
bool SearchInputType::sizeShouldIncludeDecoration(int, int& preferredSize) const
{
preferredSize = element().size();
return true;
}
float SearchInputType::decorationWidth() const
{
float width = 0;
if (m_resultsButton)
width += m_resultsButton->computedStyle()->logicalWidth().value();
if (m_cancelButton)
width += m_cancelButton->computedStyle()->logicalWidth().value();
return width;
}
}