1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright (C) 2010 Google Inc. All rights reserved.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright (C) 2011 Apple Inc. All rights reserved.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Redistribution and use in source and binary forms, with or without
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * modification, are permitted provided that the following conditions are
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * met:
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *     * Redistributions of source code must retain the above copyright
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * notice, this list of conditions and the following disclaimer.
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *     * Redistributions in binary form must reproduce the above
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copyright notice, this list of conditions and the following disclaimer
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * in the documentation and/or other materials provided with the
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribution.
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *     * Neither the name of Google Inc. nor the names of its
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * contributors may be used to endorse or promote products derived from
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * this software without specific prior written permission.
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "config.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/html/forms/CheckboxInputType.h"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/InputTypeNames.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/events/KeyboardEvent.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/html/HTMLInputElement.h"
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "platform/text/PlatformLocale.h"
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "wtf/PassOwnPtr.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace blink {
42
43PassRefPtrWillBeRawPtr<InputType> CheckboxInputType::create(HTMLInputElement& element)
44{
45    return adoptRefWillBeNoop(new CheckboxInputType(element));
46}
47
48const AtomicString& CheckboxInputType::formControlType() const
49{
50    return InputTypeNames::checkbox;
51}
52
53bool CheckboxInputType::valueMissing(const String&) const
54{
55    return element().isRequired() && !element().checked();
56}
57
58String CheckboxInputType::valueMissingText() const
59{
60    return locale().queryString(blink::WebLocalizedString::ValidationValueMissingForCheckbox);
61}
62
63void CheckboxInputType::handleKeyupEvent(KeyboardEvent* event)
64{
65    const String& key = event->keyIdentifier();
66    if (key != "U+0020")
67        return;
68    dispatchSimulatedClickIfActive(event);
69}
70
71PassOwnPtrWillBeRawPtr<ClickHandlingState> CheckboxInputType::willDispatchClick()
72{
73    // An event handler can use preventDefault or "return false" to reverse the checking we do here.
74    // The ClickHandlingState object contains what we need to undo what we did here in didDispatchClick.
75
76    OwnPtrWillBeRawPtr<ClickHandlingState> state = adoptPtrWillBeNoop(new ClickHandlingState);
77
78    state->checked = element().checked();
79    state->indeterminate = element().indeterminate();
80
81    if (state->indeterminate)
82        element().setIndeterminate(false);
83
84    element().setChecked(!state->checked, DispatchChangeEvent);
85
86    return state.release();
87}
88
89void CheckboxInputType::didDispatchClick(Event* event, const ClickHandlingState& state)
90{
91    if (event->defaultPrevented() || event->defaultHandled()) {
92        element().setIndeterminate(state.indeterminate);
93        element().setChecked(state.checked);
94    }
95
96    // The work we did in willDispatchClick was default handling.
97    event->setDefaultHandled();
98}
99
100bool CheckboxInputType::shouldAppearIndeterminate() const
101{
102    return element().indeterminate();
103}
104
105} // namespace blink
106