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