-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_utils.cpp
More file actions
103 lines (93 loc) · 3.77 KB
/
html_utils.cpp
File metadata and controls
103 lines (93 loc) · 3.77 KB
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
#include "html_utils.h"
namespace HtmlUtils {
String escape(const String& s) {
String out;
out.reserve(s.length());
for (size_t i = 0; i < s.length(); i++) {
char c = s[i];
switch (c) {
case '&': out += "&"; break;
case '<': out += "<"; break;
case '>': out += ">"; break;
case '"': out += """; break;
case '\'': out += "'"; break;
default: out += c;
}
}
return out;
}
static String wrapInputGroup(const String& name, const String& label, const String& inputTag) {
String out;
out += "<div class=\"input-group\">\n";
out += " <label for=\"" + escape(name) + "\">" + escape(label) + "</label>\n";
out += " " + inputTag + "\n";
out += "</div>\n";
return out;
}
String inputText(const String& name, const String& label, const String& value, int maxLen) {
String tag = "<input type=\"text\" id=\"" + escape(name) + "\" name=\"" + escape(name) +
"\" value=\"" + escape(value) + "\"";
if (maxLen > 0) tag += " maxlength=\"" + String(maxLen) + "\"";
tag += ">";
return wrapInputGroup(name, label, tag);
}
String inputPassword(const String& name, const String& label, const String& placeholder) {
String tag = "<input type=\"password\" id=\"" + escape(name) + "\" name=\"" + escape(name) + "\"";
if (placeholder.length()) tag += " placeholder=\"" + escape(placeholder) + "\"";
tag += ">";
return wrapInputGroup(name, label, tag);
}
String inputNumber(const String& name, const String& label, long value, long minVal, long maxVal) {
String tag = "<input type=\"number\" id=\"" + escape(name) + "\" name=\"" + escape(name) +
"\" min=\"" + String(minVal) + "\" max=\"" + String(maxVal) +
"\" value=\"" + String(value) + "\" required>";
return wrapInputGroup(name, label, tag);
}
String radioGroup(const String& name, const RadioOption* options, int count, const String& selectedValue) {
String out = "<div class=\"radio-group\">\n";
for (int i = 0; i < count; i++) {
String id = name + "_" + String(i);
bool checked = selectedValue == options[i].value;
out += " <div class=\"radio-item\">\n";
out += " <input type=\"radio\" id=\"" + escape(id) + "\" name=\"" + escape(name) +
"\" value=\"" + escape(options[i].value) + "\"";
if (checked) out += " checked";
out += ">\n";
out += " <label for=\"" + escape(id) + "\">" + escape(options[i].label) + "</label>\n";
out += " </div>\n";
}
out += "</div>\n";
return out;
}
String selectWithCustom(const String& name, const String& label,
const char* const* options, int count,
const String& currentValue,
const String& customOptionValue) {
bool isPreset = false;
for (int i = 0; i < count; i++) {
if (currentValue == options[i]) { isPreset = true; break; }
}
String tag = "<select id=\"" + escape(name) + "\" name=\"" + escape(name) + "\">\n";
for (int i = 0; i < count; i++) {
String opt = options[i];
tag += " <option value=\"" + escape(opt) + "\"";
if (isPreset && opt == currentValue) tag += " selected";
tag += ">" + escape(opt) + "</option>\n";
}
tag += " <option value=\"" + escape(customOptionValue) + "\"";
if (!isPreset) tag += " selected";
tag += ">Custom...</option>\n";
tag += "</select>";
return wrapInputGroup(name, label, tag);
}
String checkboxItem(const String& name, const String& label, bool checked) {
String out;
out += "<div class=\"checkbox-item\">\n";
out += " <input type=\"checkbox\" id=\"" + escape(name) + "\" name=\"" + escape(name) + "\"";
if (checked) out += " checked";
out += ">\n";
out += " <label for=\"" + escape(name) + "\">" + escape(label) + "</label>\n";
out += "</div>\n";
return out;
}
} // namespace HtmlUtils