1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
* Ricochet - A minimalist CLI web browser for fast, distraction-free navigation.
* Copyright (C) 2026 Richard Qin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ricochet/core/form_handler.hh"
#include "ricochet/core/ui_input.hh"
#include "ricochet/core/utils.hh"
namespace ricochet::core {
InputAction submit_form( const render::InputField& target_input,
const std::vector<render::InputField>& inputs,
const std::string& current_url,
std::vector<HttpRequest>& history,
std::size_t& history_idx )
{
std::string body;
for ( const auto& in : inputs ) {
if ( in.action == target_input.action && !in.name.empty() && in.type != "submit" && in.type != "button" ) {
if ( ( in.type == "checkbox" || in.type == "radio" ) && !in.checked ) {
continue;
}
std::string encoded = in.value;
for ( char& ch : encoded ) {
if ( ch == ' ' ) {
ch = '+';
}
}
if ( !body.empty() ) {
body += "&";
}
body += in.name + "=" + encoded;
}
}
const std::string action
= target_input.action.empty() ? current_url : normalize_url( target_input.action, current_url );
HttpRequest next_req { .url = action, .method = "GET", .body = "" };
if ( target_input.method == "post" || target_input.method == "POST" ) {
next_req.method = "POST";
next_req.body = body;
} else {
const char sep = action.contains( '?' ) ? '&' : '?';
next_req.url += sep + body;
}
history.resize( history_idx + 1 );
history.push_back( next_req );
history_idx++;
return InputAction::Navigate;
}
void update_input_value( PageData& page_data, std::size_t index, const std::string& query )
{
page_data.inputs[index - 1].value = query;
const std::string marker = "[I" + std::to_string( index ) + ":";
const std::string replacement = "[I" + std::to_string( index ) + ":" + query + "]";
auto update_lines = [&]( std::vector<std::string>& lines_arr ) {
for ( auto& line : lines_arr ) {
const std::size_t pos = line.find( marker );
if ( pos != std::string::npos ) {
const std::size_t end = line.find( ']', pos );
if ( end != std::string::npos ) {
line.replace( pos, end - pos + 1, replacement );
}
}
}
};
update_lines( page_data.lines );
update_lines( page_data.original_lines );
}
void update_single_line_ui( std::string& line, const std::string& marker, const render::InputField& in )
{
const std::string checked_str = marker + ( in.type == "checkbox" ? "[X] " : "(X) " ) + in.value + "]";
const std::string unchecked_str = marker + ( in.type == "checkbox" ? "[ ] " : "( ) " ) + in.value + "]";
const std::string& target_str = in.checked ? checked_str : unchecked_str;
const std::string& old_str = in.checked ? unchecked_str : checked_str;
const std::size_t pos = line.find( old_str );
if ( pos != std::string::npos ) {
line.replace( pos, old_str.length(), target_str );
}
}
void update_toggle_ui( PageData& page_data )
{
for ( std::size_t i = 0; i < page_data.inputs.size(); ++i ) {
const auto& in = page_data.inputs[i];
if ( in.type != "checkbox" && in.type != "radio" ) {
continue;
}
const std::string marker = "[I" + std::to_string( i + 1 ) + ":";
for ( auto& line : page_data.lines ) {
update_single_line_ui( line, marker, in );
}
for ( auto& line : page_data.original_lines ) {
update_single_line_ui( line, marker, in );
}
}
}
InputAction handle_form_input( const tui::Terminal& terminal,
PageData& page_data,
const std::string& current_url,
std::vector<HttpRequest>& history,
std::size_t& history_idx )
{
const std::string num_input = get_terminal_input( terminal, "Select Input [#]:" );
if ( num_input.empty() ) {
return InputAction::None;
}
std::size_t index = 0;
try {
index = std::stoul( num_input );
} catch ( ... ) {
return InputAction::None;
}
if ( index == 0 || index > page_data.inputs.size() ) {
return InputAction::None;
}
auto& target_input = page_data.inputs[index - 1];
if ( target_input.type == "submit" || target_input.type == "button" ) {
return submit_form( target_input, page_data.inputs, current_url, history, history_idx );
}
if ( target_input.type == "checkbox" || target_input.type == "radio" ) {
if ( target_input.type == "radio" ) {
for ( auto& in : page_data.inputs ) {
if ( in.name == target_input.name && in.action == target_input.action ) {
in.checked = false;
}
}
target_input.checked = true;
} else {
target_input.checked = !target_input.checked;
}
update_toggle_ui( page_data );
return InputAction::None;
}
const std::string query = get_terminal_input( terminal, "Enter Text [I" + std::to_string( index ) + "]:" );
if ( query.empty() ) {
return InputAction::None;
}
update_input_value( page_data, index, query );
return InputAction::None;
}
} // namespace ricochet::core
|