Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows to set empty html attr #1548

Merged
merged 5 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,15 +927,22 @@ function parse_form_attributes($attributes, $default): string

foreach ($default as $key => $val)
{
if ($key === 'value')
if(!is_bool($val))
{
$val = esc($val, 'html');
if ($key === 'value')
{
$val = esc($val, 'html');
}
elseif ($key === 'name' && ! strlen($default['name']))
{
continue;
}
$att .= $key . '="' . $val . '" ';
}
elseif ($key === 'name' && ! strlen($default['name']))
else
{
continue;
$att .= $key . ' ';
}
$att .= $key . '="' . $val . '" ';
}

return $att;
Expand Down
55 changes: 55 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,5 +810,60 @@ public function testSetRadioDefault()
$this->assertEquals(' checked="checked"', set_radio('code', 'alpha', true));
$this->assertEquals('', set_radio('code', 'beta', false));
}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesTrue()
{
$expected = 'readonly ';
$this->assertEquals($expected, parse_form_attributes(['readonly' => true], []));
}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesFalse()
{
$expected = 'disabled ';
$this->assertEquals($expected, parse_form_attributes(['disabled' => false], []));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't a false setting on a value make it so that it does not appear in the attributes list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we have more flexibility outside form helper function if we will treat true as well as false in the same way.
However I know what you are thinking so I am open to changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I can live with that, and it's still valid HTML, so I'm good. Will merge.

}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesNull()
{
$expected = 'bar="" ';
$this->assertEquals($expected, parse_form_attributes(['bar' => null], []));
}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesStringEmpty()
{
$expected = 'bar="" ';
$this->assertEquals($expected, parse_form_attributes(['bar' => ''], []));
}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesStringFoo()
{
$expected = 'bar="foo" ';
$this->assertEquals($expected, parse_form_attributes(['bar' => 'foo'], []));
}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesInt0()
{
$expected = 'ok="0" ';
$this->assertEquals($expected, parse_form_attributes(['ok' => 0], []));
}


// ------------------------------------------------------------------------
public function testFormParseFormAttributesInt1()
{
$expected = 'ok="1" ';
$this->assertEquals($expected, parse_form_attributes(['ok' => 1], []));
}
}