Fix form rendering bugs I introduced.

In translating from pointers to strings, I
need to handle the nullable case better.
This commit is contained in:
2025-05-20 02:19:51 -04:00
parent 0c350a85c1
commit bd581c55b6

View File

@ -161,8 +161,8 @@ class DilloHtmlInput {
public: //BUG: for now everything is public
DilloHtmlInputType type;
Embed *embed; /* May be NULL (think: hidden input) */
char *name;
char *init_str; /* note: some overloading - for buttons, init_str
std::optional< std::string > name;
std::optional< std::string > init_str; /* note: some overloading - for buttons, init_str
is simply the value of the button; for text
entries, it is the initial value */
DilloHtmlSelect *select;
@ -177,7 +177,7 @@ private:
public:
DilloHtmlInput (DilloHtmlInputType type, Embed *embed,
const char *name, const char *init_str, bool init_val);
const std::optional< std::string > &name, const std::optional< std::string > &init_str, bool init_val);
~DilloHtmlInput ();
void appendValuesTo(Dlist *values, bool is_active_submit);
void reset();
@ -290,10 +290,10 @@ void a_Html_form_display_hiddens2(void *vform, bool display)
* Add an HTML control
*/
static void Html_add_input(DilloHtml *html, DilloHtmlInputType type,
Embed *embed, const char *name,
const char *init_str, bool init_val)
Embed *embed, const std::optional< std::string > &name,
const std::optional< std::string > &init_str, bool init_val)
{
_MSG("name=[%s] init_str=[%s] init_val=[%d]\n", name, init_str, init_val);
_MSG("name=[%s] init_str=[%s] init_val=[%d]\n", name.value_or( "" ).c_str(), init_str.value_or( "" ).c_str(), init_val);
auto input = std::make_unique< DilloHtmlInput >(type, embed, name, init_str,
init_val);
if (html->InFlags & IN_FORM) {
@ -323,7 +323,7 @@ static std::shared_ptr< DilloHtmlInput > Html_get_radio_input(DilloHtml *html, c
for (int idx = 0; idx < inputs->size(); idx++) {
auto input = inputs->at(idx);
if (input->type == DILLO_HTML_INPUT_RADIO &&
input->name && !dStrAsciiCasecmp(input->name, name))
input->name && !dStrAsciiCasecmp(input->name.value().c_str(), name))
return input;
}
}
@ -558,8 +558,7 @@ void Html_tag_open_input(DilloHtml *html, const char *tag, int tagsize)
embed = new Embed (resource);
if (inp_type != DILLO_HTML_INPUT_UNKNOWN) {
Html_add_input(html, inp_type, embed, name ? name.value().c_str() : nullptr,
init_str.value_or( "" ).c_str(), init_val);
Html_add_input(html, inp_type, embed, name, init_str, init_val);
}
if (embed != NULL && inp_type != DILLO_HTML_INPUT_IMAGE &&
@ -622,7 +621,7 @@ void Html_tag_open_isindex(DilloHtml *html, const char *tag, int tagsize)
EntryResource *entryResource = factory->createEntryResource (20, false,
NULL, NULL);
embed = new Embed (entryResource);
Html_add_input(html, DILLO_HTML_INPUT_INDEX, embed, NULL, NULL, FALSE);
Html_add_input(html, DILLO_HTML_INPUT_INDEX, embed, std::nullopt, std::nullopt, FALSE);
HT2TB(html)->addWidget (embed, html->backgroundStyle ());
@ -689,7 +688,7 @@ void Html_tag_content_textarea(DilloHtml *html, const char *tag, int tagsize)
/* Readonly or not? */
if (a_Html_get_attr(html, tag, tagsize, "readonly"))
textres->setEditable(false);
Html_add_input(html, DILLO_HTML_INPUT_TEXTAREA, embed, name, NULL, false);
Html_add_input(html, DILLO_HTML_INPUT_TEXTAREA, embed, name, std::nullopt, false);
HT2TB(html)->addWidget (embed, html->backgroundStyle ());
dFree(name);
@ -783,7 +782,7 @@ void Html_tag_open_select(DilloHtml *html, const char *tag, int tagsize)
}
HT2TB(html)->addWidget (embed, html->backgroundStyle ());
Html_add_input(html, type, embed, name.value().c_str(), NULL, false);
Html_add_input(html, type, embed, name, std::nullopt, false);
a_Html_stash_init(html);
}
@ -964,7 +963,7 @@ void Html_tag_open_button(DilloHtml *html, const char *tag, int tagsize)
auto value = a_Html_get_attr_wdef(html, tag, tagsize, "value", NULL);
auto name = a_Html_get_attr_wdef(html, tag, tagsize, "name", NULL);
Html_add_input(html, inp_type, embed, name.value().c_str(), value.value().c_str(), FALSE);
Html_add_input(html, inp_type, embed, name, value, FALSE);
}
}
@ -1159,7 +1158,7 @@ std::optional< std::string > DilloHtmlForm::buildQueryData(DilloHtmlInput *activ
for (int i = 0; i < inputs.size(); i++) {
auto input = inputs.at (i);
std::string name= input->name ? input->name : "";
std::string name= input->name.value_or( "" );
bool is_active_submit = (input.get() == active_submit);
int valcount;
@ -1178,7 +1177,7 @@ std::optional< std::string > DilloHtmlForm::buildQueryData(DilloHtmlInput *activ
LabelButtonResource *lbr =
(LabelButtonResource*) input->embed->getResource();
const char *filename = lbr->getLabel();
if (filename[0] && strcmp(filename, input->init_str)) {
if (filename[0] && strcmp(filename, input->init_str.value().c_str())) {
const char *p = strrchr(filename, '/');
if (p)
filename = p + 1; /* don't reveal path */
@ -1278,8 +1277,8 @@ char *DilloHtmlForm::makeMultipartBoundary(iconv_t char_encoder,
bool is_active_submit = (input.get() == active_submit);
input->appendValuesTo(values, is_active_submit);
if (input->name) {
std::string dstr= input->name;;
if (input->name.has_value()) {
std::string dstr= input->name.value();
dstr = encodeText(char_encoder, std::move( dstr ));
dStr_append_l(DataStr, dstr.c_str(), dstr.size());
}
@ -1287,7 +1286,7 @@ char *DilloHtmlForm::makeMultipartBoundary(iconv_t char_encoder,
LabelButtonResource *lbr =
(LabelButtonResource*)input->embed->getResource();
const char *filename = lbr->getLabel();
if (filename[0] && strcmp(filename, input->init_str)) {
if (filename[0] && strcmp(filename, input->init_str.value().c_str())) {
std::string dstr = filename;
dstr = encodeText(char_encoder, std::move( dstr ));
dStr_append_l(DataStr, dstr.c_str(), dstr.size());
@ -1590,7 +1589,7 @@ std::shared_ptr< DilloHtmlInput > DilloHtmlForm::getRadioInput (const char *name
for (int idx = 0; idx < inputs.size(); idx++) {
auto input = inputs.at(idx);
if (input->type == DILLO_HTML_INPUT_RADIO &&
input->name && !dStrAsciiCasecmp(input->name, name))
input->name && !dStrAsciiCasecmp(input->name.value().c_str(), name))
return input;
}
return nullptr;
@ -1654,13 +1653,13 @@ void DilloHtmlReceiver::clicked (Resource *resource,
* Constructor
*/
DilloHtmlInput::DilloHtmlInput (DilloHtmlInputType type2, Embed *embed2,
const char *name2, const char *init_str2,
const std::optional< std::string > &name2, const std::optional< std::string > &init_str2,
bool init_val2)
{
type = type2;
embed = embed2;
name = (name2) ? dStrdup(name2) : NULL;
init_str = (init_str2) ? dStrdup(init_str2) : NULL;
name = name2;
init_str = init_str2;
init_val = init_val2;
select = NULL;
switch (type) {
@ -1680,8 +1679,6 @@ DilloHtmlInput::DilloHtmlInput (DilloHtmlInputType type2, Embed *embed2,
*/
DilloHtmlInput::~DilloHtmlInput ()
{
dFree(name);
dFree(init_str);
dStr_free(file_data, 1);
if (select)
delete select;
@ -1788,14 +1785,14 @@ void DilloHtmlInput::appendValuesTo(Dlist *values, bool is_active_submit)
ToggleButtonResource *cb_r =
(ToggleButtonResource*)embed->getResource();
if (name && init_str && cb_r->isActivated()) {
dList_append(values, dStr_new(init_str));
dList_append(values, dStr_new(init_str.value().c_str()));
}
}
break;
case DILLO_HTML_INPUT_SUBMIT:
case DILLO_HTML_INPUT_BUTTON_SUBMIT:
if (is_active_submit)
dList_append(values, dStr_new(init_str));
dList_append(values, dStr_new(init_str.value_or( "" ).c_str()));
break;
case DILLO_HTML_INPUT_SELECT:
case DILLO_HTML_INPUT_SEL_LIST:
@ -1808,7 +1805,7 @@ void DilloHtmlInput::appendValuesTo(Dlist *values, bool is_active_submit)
{
LabelButtonResource *lbr = (LabelButtonResource*)embed->getResource();
const char *filename = lbr->getLabel();
if (filename[0] && strcmp(filename, init_str)) {
if (filename[0] && strcmp(filename, init_str.value().c_str())) {
if (file_data) {
Dstr *file = dStr_sized_new(file_data->len);
dStr_append_l(file, file_data->str, file_data->len);
@ -1848,7 +1845,7 @@ void DilloHtmlInput::reset ()
case DILLO_HTML_INPUT_HIDDEN:
{
EntryResource *entryres = (EntryResource*)embed->getResource();
entryres->setText(init_str ? init_str : "");
entryres->setText(init_str.value_or( "" ).c_str());
}
break;
case DILLO_HTML_INPUT_CHECKBOX:
@ -1867,16 +1864,16 @@ void DilloHtmlInput::reset ()
}
break;
case DILLO_HTML_INPUT_TEXTAREA:
if (init_str != NULL) {
if (init_str.has_value()) {
MultiLineTextResource *textres =
(MultiLineTextResource*)embed->getResource();
textres->setText(init_str ? init_str : "");
textres->setText(init_str.value().c_str());
}
break;
case DILLO_HTML_INPUT_FILE:
{
LabelButtonResource *lbr = (LabelButtonResource*)embed->getResource();
lbr->setLabel(init_str);
lbr->setLabel(init_str.value().c_str());
}
break;
default: