~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebKit/Shared/glib/InputMethodState.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Igalia S.L.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 
14
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
15
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 
17
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
19
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
20
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
21
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
22
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
23
 * THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "InputMethodState.h"
 
28
 
 
29
#include "ArgumentCoders.h"
 
30
#include <WebCore/HTMLInputElement.h>
 
31
 
 
32
namespace WebKit {
 
33
 
 
34
void InputMethodState::setPurposeOrHintForInputMode(WebCore::InputMode inputMode)
 
35
{
 
36
    switch (inputMode) {
 
37
    case WebCore::InputMode::None:
 
38
        hints.add(InputMethodState::Hint::InhibitOnScreenKeyboard);
 
39
        break;
 
40
    case WebCore::InputMode::Unspecified:
 
41
    case WebCore::InputMode::Text:
 
42
        purpose = Purpose::FreeForm;
 
43
        break;
 
44
    case WebCore::InputMode::Telephone:
 
45
        purpose = Purpose::Phone;
 
46
        break;
 
47
    case WebCore::InputMode::Url:
 
48
        purpose = Purpose::Url;
 
49
        break;
 
50
    case WebCore::InputMode::Email:
 
51
        purpose = Purpose::Email;
 
52
        break;
 
53
    case WebCore::InputMode::Numeric:
 
54
        purpose = Purpose::Digits;
 
55
        break;
 
56
    case WebCore::InputMode::Decimal:
 
57
        purpose = Purpose::Number;
 
58
        break;
 
59
    case WebCore::InputMode::Search:
 
60
        break;
 
61
    }
 
62
}
 
63
 
 
64
static bool inputElementHasDigitsPattern(WebCore::HTMLInputElement& element)
 
65
{
 
66
    const auto& pattern = element.attributeWithoutSynchronization(WebCore::HTMLNames::patternAttr);
 
67
    return pattern == "\\d*" || pattern == "[0-9]*";
 
68
}
 
69
 
 
70
void InputMethodState::setPurposeForInputElement(WebCore::HTMLInputElement& element)
 
71
{
 
72
    if (element.isPasswordField())
 
73
        purpose = Purpose::Password;
 
74
    else if (element.isEmailField())
 
75
        purpose = Purpose::Email;
 
76
    else if (element.isTelephoneField())
 
77
        purpose = Purpose::Phone;
 
78
    else if (element.isNumberField())
 
79
        purpose = inputElementHasDigitsPattern(element) ? Purpose::Digits : Purpose::Number;
 
80
    else if (element.isURLField())
 
81
        purpose = Purpose::Url;
 
82
    else if (element.isText() && inputElementHasDigitsPattern(element))
 
83
        purpose = Purpose::Digits;
 
84
}
 
85
 
 
86
void InputMethodState::addHintsForAutocapitalizeType(AutocapitalizeType autocapitalizeType)
 
87
{
 
88
    switch (autocapitalizeType) {
 
89
    case AutocapitalizeTypeDefault:
 
90
        break;
 
91
    case AutocapitalizeTypeNone:
 
92
        hints.add(InputMethodState::Hint::Lowercase);
 
93
        break;
 
94
    case AutocapitalizeTypeWords:
 
95
        hints.add(InputMethodState::Hint::UppercaseWords);
 
96
        break;
 
97
    case AutocapitalizeTypeSentences:
 
98
        hints.add(InputMethodState::Hint::UppercaseSentences);
 
99
        break;
 
100
    case AutocapitalizeTypeAllCharacters:
 
101
        hints.add(InputMethodState::Hint::UppercaseChars);
 
102
        break;
 
103
    }
 
104
}
 
105
 
 
106
void InputMethodState::encode(IPC::Encoder& encoder) const
 
107
{
 
108
    encoder.encodeEnum(purpose);
 
109
    encoder << hints;
 
110
}
 
111
 
 
112
Optional<InputMethodState> InputMethodState::decode(IPC::Decoder& decoder)
 
113
{
 
114
    InputMethodState state;
 
115
    if (!decoder.decodeEnum(state.purpose))
 
116
        return WTF::nullopt;
 
117
    if (!decoder.decode(state.hints))
 
118
        return WTF::nullopt;
 
119
    return state;
 
120
}
 
121
 
 
122
} // namespace WebKit