<commit_before>/*
 * Copyright 2011, 2012 Esrille Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "HTMLInputElementImp.h"

#include <boost/bind.hpp>

#include <org/w3c/dom/Text.h>
#include <org/w3c/dom/events/KeyboardEvent.h>
#include <org/w3c/dom/html/HTMLTemplateElement.h>

#include "utf.h"
#include "DocumentImp.h"
#include "HTMLTemplateElementImp.h"
#include "css/CSSStyleDeclarationImp.h"     // TODO: only for XBL; isolate this later.

namespace org
{
namespace w3c
{
namespace dom
{
namespace bootstrap
{

namespace {

const char16_t* typeKeywords[] = {
    u"hidden",
    u"text",
    u"search",
    u"tel",
    u"url",
    u"email",
    u"password",
    u"datetime",
    u"date",
    u"month",
    u"week",
    u"time",
    u"datetime-local",
    u"number",
    u"range",
    u"color",
    u"checkbox",
    u"radio",
    u"file",
    u"submit",
    u"image",
    u"reset",
    u"button",
};

}  // namespace

HTMLInputElementImp::HTMLInputElementImp(DocumentImp* ownerDocument) :
    ObjectMixin(ownerDocument, u"input"),
    type(Text),
    form(0),
    clickListener(boost::bind(&HTMLInputElementImp::handleClick, this, _1)),
    keydownListener(boost::bind(&HTMLInputElementImp::handleKeydown, this, _1)),
    cursor(0),
    checked(false)
{
}

HTMLInputElementImp::HTMLInputElementImp(HTMLInputElementImp* org, bool deep) :
    ObjectMixin(org, deep),
    type(org->type),
    form(0),
    clickListener(boost::bind(&HTMLInputElementImp::handleClick, this, _1)),
    keydownListener(boost::bind(&HTMLInputElementImp::handleKeydown, this, _1)),
    cursor(0),
    checked(org->checked)
{
    // TODO: check event listeners.
}

void HTMLInputElementImp::eval()
{
    HTMLElementImp::eval();
    HTMLElementImp::evalHspace(this);
    HTMLElementImp::evalVspace(this);

    setType(getAttribute(u"type"));

    if (!getDisabled() && type != Hidden)
        setTabIndex(0);

    if (hasAttribute(u"checked"))
        checked = true;

    switch (type) {
    case Text:
    case Search:
    case Telephone:
    case URL:
    case Email:
    case Password: {
        Nullable<std::u16string> size = getAttribute(u"size");
        std::u16string length(u"20em");
        if (size.hasValue()) {
            std::u16string length(size.value());
            if (toUnsigned(length))
                length += u"em";
            else
                length = u"20em";
        }
        css::CSSStyleDeclaration style = getStyle();
        style.setProperty(u"min-width", length, u"non-css");
        break;
    }
    default:
        break;
    }
}

void HTMLInputElementImp::handleClick(events::Event event)
{
    if (type == SubmitButton) {
        html::HTMLFormElement form = getForm();<commit_msg>(HTMLInputElementImp::eval) : Fix a bug<commit_after>/*
 * Copyright 2011, 2012 Esrille Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "HTMLInputElementImp.h"

#include <boost/bind.hpp>

#include <org/w3c/dom/Text.h>
#include <org/w3c/dom/events/KeyboardEvent.h>
#include <org/w3c/dom/html/HTMLTemplateElement.h>

#include "utf.h"
#include "DocumentImp.h"
#include "HTMLTemplateElementImp.h"
#include "css/CSSStyleDeclarationImp.h"     // TODO: only for XBL; isolate this later.

namespace org
{
namespace w3c
{
namespace dom
{
namespace bootstrap
{

namespace {

const char16_t* typeKeywords[] = {
    u"hidden",
    u"text",
    u"search",
    u"tel",
    u"url",
    u"email",
    u"password",
    u"datetime",
    u"date",
    u"month",
    u"week",
    u"time",
    u"datetime-local",
    u"number",
    u"range",
    u"color",
    u"checkbox",
    u"radio",
    u"file",
    u"submit",
    u"image",
    u"reset",
    u"button",
};

}  // namespace

HTMLInputElementImp::HTMLInputElementImp(DocumentImp* ownerDocument) :
    ObjectMixin(ownerDocument, u"input"),
    type(Text),
    form(0),
    clickListener(boost::bind(&HTMLInputElementImp::handleClick, this, _1)),
    keydownListener(boost::bind(&HTMLInputElementImp::handleKeydown, this, _1)),
    cursor(0),
    checked(false)
{
}

HTMLInputElementImp::HTMLInputElementImp(HTMLInputElementImp* org, bool deep) :
    ObjectMixin(org, deep),
    type(org->type),
    form(0),
    clickListener(boost::bind(&HTMLInputElementImp::handleClick, this, _1)),
    keydownListener(boost::bind(&HTMLInputElementImp::handleKeydown, this, _1)),
    cursor(0),
    checked(org->checked)
{
    // TODO: check event listeners.
}

void HTMLInputElementImp::eval()
{
    HTMLElementImp::eval();
    HTMLElementImp::evalHspace(this);
    HTMLElementImp::evalVspace(this);

    setType(getAttribute(u"type"));

    if (!getDisabled() && type != Hidden)
        setTabIndex(0);

    if (hasAttribute(u"checked"))
        checked = true;

    switch (type) {
    case Text:
    case Search:
    case Telephone:
    case URL:
    case Email:
    case Password: {
        Nullable<std::u16string> size = getAttribute(u"size");
        std::u16string length(u"20em");
        if (size.hasValue()) {
            std::u16string length(size.value());
            if (toUnsigned(length))
                length += u"em";
            else
                length = u"20em";
        }
        css::CSSStyleDeclaration style = getStyle();
        style.setProperty(u"width", length, u"non-css");
        break;
    }
    default:
        break;
    }
}

void HTMLInputElementImp::handleClick(events::Event event)
{
    if (type == SubmitButton) {
        html::HTMLFormElement form = getForm();
{'fraction_non_alphanumeric': 0.13444341658957756, 'fraction_numerical': 0.011409189022510022, 'mean_word_length': 3.256561679790026, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7135959', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2056, 'n_words': 486}