aboutsummaryrefslogtreecommitdiff
path: root/src/render/input_renderer.cc
blob: 076fa2a759e5a5fe6a1feedee073d1447504fae1 (plain)
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
/*
 * 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/render/input_renderer.hh"
#include "ricochet/render/internal.hh"
#include "ricochet/render/utils.hh"

namespace ricochet::render {

namespace {

std::string build_input_hint( const std::string& tag_name, bool checked )
{
  std::string placeholder = extract_attr( tag_name, "placeholder" );
  const std::string type = extract_attr( tag_name, "type" );
  std::string val = extract_attr( tag_name, "value" );

  if ( !placeholder.empty() ) {
    return placeholder;
  }
  if ( type == "submit" || type == "button" ) {
    return val.empty() ? "Submit" : val;
  }
  if ( type == "checkbox" ) {
    return checked ? "[X] " + val : "[ ] " + val;
  }
  if ( type == "radio" ) {
    return checked ? "(X) " + val : "( ) " + val;
  }
  if ( type == "hidden" ) {
    return "hidden";
  }
  if ( !val.empty() ) {
    return val;
  }
  return "text";
}

} // namespace

void render_input_node( const parser::DomNode& node,
                        RenderResult& result,
                        const FormContext& ctx,
                        std::string& output )
{
  std::string type = extract_attr( node.tag_name, "type" );
  const std::size_t space_pos = node.tag_name.find( ' ' );
  const std::string base_tag
    = ( space_pos == std::string::npos ) ? node.tag_name : node.tag_name.substr( 0, space_pos );
  if ( base_tag == "button" && type.empty() ) {
    type = "submit";
  }
  const std::string default_val = extract_attr( node.tag_name, "value" );
  const bool is_checked = node.tag_name.contains( "checked" );
  result.inputs.push_back(
    { extract_attr( node.tag_name, "name" ), ctx.action, ctx.method, default_val, type, is_checked } );

  std::string hint = build_input_hint( node.tag_name, is_checked );
  if ( base_tag == "button" ) {
    hint = "Submit";
  }
  if ( hint != "hidden" ) {
    output += " \033[7;33m[I" + std::to_string( result.inputs.size() ) + ":" + hint + "]\033[0m ";
  }
}

} // namespace ricochet::render