Fix some inputs treated like nestedModel when using an accessor (#471)

* 5.6 Prep

* Added $optgroupsAttributes parameter to select method.

* escape HTML entities in `src`-attribute

* Style fixes. Replaced == with ===.
This commit is contained in:
Luiz Gustavo Martins authored and Tom Shafer committed 2018-02-05 07:04:02 -07:00
1 parent 7e49960e8f
commit 2c5db92f66
6 files changed
+76 -27

No files matched your search

+9 -6
View File
@@ -19,14 +19,14 @@
], ],
"require": { "require": {
"php": ">=7.0.0", "php": ">=7.0.0",
"illuminate/http": "5.5.*", "illuminate/http": "5.6.x-dev",
"illuminate/routing": "5.5.*", "illuminate/routing": "5.6.x-dev",
"illuminate/session": "5.5.*", "illuminate/session": "5.6.x-dev",
"illuminate/support": "5.5.*", "illuminate/support": "5.6.x-dev",
"illuminate/view": "5.5.*" "illuminate/view": "5.6.x-dev"
}, },
"require-dev": { "require-dev": {
"illuminate/database": "5.5.*", "illuminate/database": "5.6.x-dev",
"mockery/mockery": "~0.9.4", "mockery/mockery": "~0.9.4",
"phpunit/phpunit": "~5.4" "phpunit/phpunit": "~5.4"
}, },
@@ -39,6 +39,9 @@
] ]
}, },
"extra": { "extra": {
"branch-alias": {
"dev-master": "5.6-dev"
},
"laravel": { "laravel": {
"providers": [ "providers": [
"Collective\\Html\\HtmlServiceProvider" "Collective\\Html\\HtmlServiceProvider"
+1 -1
View File
@@ -87,7 +87,7 @@ trait FormAccessible
$mutator = collect($methods) $mutator = collect($methods)
->first(function (ReflectionMethod $method) use ($key) { ->first(function (ReflectionMethod $method) use ($key) {
return $method->getName() == 'form' . Str::studly($key) . 'Attribute'; return $method->getName() === 'form' . Str::studly($key) . 'Attribute';
}); });
return (bool) $mutator; return (bool) $mutator;
+29 -17
View File
@@ -575,6 +575,7 @@ class FormBuilder
* @param string $selected * @param string $selected
* @param array $selectAttributes * @param array $selectAttributes
* @param array $optionsAttributes * @param array $optionsAttributes
* @param array $optgroupsAttributes
* *
* @return \Illuminate\Support\HtmlString * @return \Illuminate\Support\HtmlString
*/ */
@@ -583,7 +584,8 @@ class FormBuilder
$list = [], $list = [],
$selected = null, $selected = null,
array $selectAttributes = [], array $selectAttributes = [],
array $optionsAttributes = [] array $optionsAttributes = [],
array $optgroupsAttributes = []
) { ) {
$this->type = 'select'; $this->type = 'select';
@@ -609,8 +611,9 @@ class FormBuilder
} }
foreach ($list as $value => $display) { foreach ($list as $value => $display) {
$optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : []; $optionAttributes = $optionsAttributes[$value] ?? [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes); $optgroupAttributes = $optgroupsAttributes[$value] ?? [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
} }
// Once we have all of this HTML, we can join this into a single element after // Once we have all of this HTML, we can join this into a single element after
@@ -685,13 +688,14 @@ class FormBuilder
* @param string $value * @param string $value
* @param string $selected * @param string $selected
* @param array $attributes * @param array $attributes
* @param array $optgroupAttributes
* *
* @return \Illuminate\Support\HtmlString * @return \Illuminate\Support\HtmlString
*/ */
public function getSelectOption($display, $value, $selected, array $attributes = []) public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
{ {
if (is_array($display)) { if (is_array($display)) {
return $this->optionGroup($display, $value, $selected, $attributes); return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes);
} }
return $this->option($display, $value, $selected, $attributes); return $this->option($display, $value, $selected, $attributes);
@@ -704,18 +708,21 @@ class FormBuilder
* @param string $label * @param string $label
* @param string $selected * @param string $selected
* @param array $attributes * @param array $attributes
* @param array $optionsAttributes
* *
* @return \Illuminate\Support\HtmlString * @return \Illuminate\Support\HtmlString
*/ */
protected function optionGroup($list, $label, $selected, array $attributes = []) protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = [])
{ {
$html = []; $html = [];
foreach ($list as $value => $display) { foreach ($list as $value => $display) {
$html[] = $this->option($display, $value, $selected, $attributes); $optionAttributes = $optionsAttributes[$value] ?? [];
$html[] = $this->option($display, $value, $selected, $optionAttributes);
} }
return $this->toHtmlString('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>'); return $this->toHtmlString('<optgroup label="' . e($label) . '"' . $this->html->attributes($attributes) . '>' . implode('', $html) . '</optgroup>');
} }
/** /**
@@ -734,7 +741,12 @@ class FormBuilder
$options = array_merge(['value' => $value, 'selected' => $selected], $attributes); $options = array_merge(['value' => $value, 'selected' => $selected], $attributes);
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>'); $string = '<option' . $this->html->attributes($options) . '>';
if ($display !== null) {
$string .= e($display) . '</option>';
}
return $this->toHtmlString($string);
} }
/** /**
@@ -773,7 +785,7 @@ class FormBuilder
return $selected->contains($value) ? 'selected' : null; return $selected->contains($value) ? 'selected' : null;
} }
return ((string) $value == (string) $selected) ? 'selected' : null; return ((string) $value === (string) $selected) ? 'selected' : null;
} }
/** /**
@@ -854,7 +866,7 @@ class FormBuilder
return $this->getRadioCheckedState($name, $value, $checked); return $this->getRadioCheckedState($name, $value, $checked);
default: default:
return $this->getValueAttribute($name) == $value; return $this->getValueAttribute($name) === $value;
} }
} }
@@ -907,7 +919,7 @@ class FormBuilder
return $checked; return $checked;
} }
return $this->getValueAttribute($name) == $value; return $this->getValueAttribute($name) === $value;
} }
/** /**
@@ -1006,7 +1018,7 @@ class FormBuilder
{ {
$method = strtoupper($method); $method = strtoupper($method);
return $method != 'GET' ? 'POST' : $method; return $method !== 'GET' ? 'POST' : $method;
} }
/** /**
@@ -1108,7 +1120,7 @@ class FormBuilder
// If the method is something other than GET we will go ahead and attach the // If the method is something other than GET we will go ahead and attach the
// CSRF token to the form, as this can't hurt and is convenient to simply // CSRF token to the form, as this can't hurt and is convenient to simply
// always have available on every form the developers creates for them. // always have available on every form the developers creates for them.
if ($method != 'GET') { if ($method !== 'GET') {
$appendage .= $this->token(); $appendage .= $this->token();
} }
@@ -1150,7 +1162,7 @@ class FormBuilder
$old = $this->old($name); $old = $this->old($name);
if (! is_null($old) && $name != '_method') { if (! is_null($old) && $name !== '_method') {
return $old; return $old;
} }
@@ -1169,7 +1181,7 @@ class FormBuilder
} }
$request = $this->request($name); $request = $this->request($name);
if (! is_null($request) && $name != '_method') { if (! is_null($request)) {
return $request; return $request;
} }
@@ -1253,7 +1265,7 @@ class FormBuilder
*/ */
public function oldInputIsEmpty() public function oldInputIsEmpty()
{ {
return (isset($this->session) && count($this->session->getOldInput()) == 0); return (isset($this->session) && count($this->session->getOldInput()) === 0);
} }
/** /**
+3 -3
View File
@@ -162,7 +162,7 @@ class HtmlBuilder
$title = $this->entities($title); $title = $this->entities($title);
} }
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $title . '</a>'); return $this->toHtmlString('<a href="' . $this->entities($url) . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
} }
/** /**
@@ -357,7 +357,7 @@ class HtmlBuilder
{ {
$html = ''; $html = '';
if (count($list) == 0) { if (count($list) === 0) {
return $html; return $html;
} }
@@ -451,7 +451,7 @@ class HtmlBuilder
} }
// Treat boolean attributes as HTML properties // Treat boolean attributes as HTML properties
if (is_bool($value) && $key != 'value') { if (is_bool($value) && $key !== 'value') {
return $value ? $key : ''; return $value ? $key : '';
} }
+31
View File
@@ -402,6 +402,37 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
'<select class="class-name" id="select-id" name="size"><optgroup label="Large sizes"><option value="L">Large</option><option value="XL">Extra Large</option></optgroup><option value="S">Small</option></select>' '<select class="class-name" id="select-id" name="size"><optgroup label="Large sizes"><option value="L">Large</option><option value="XL">Extra Large</option></optgroup><option value="S">Small</option></select>'
); );
$select = $this->formBuilder->select(
'size',
[
'Large sizes' => [
'L' => 'Large',
'XL' => 'Extra Large',
],
'M' => 'Medium',
'Small sizes' => [
'S' => 'Small',
'XS' => 'Extra Small',
],
],
null,
[],
[
'Large sizes' => [
'L' => ['disabled']
],
'M' => ['disabled'],
],
[
'Small sizes' => ['disabled'],
]
);
$this->assertEquals(
$select,
'<select name="size"><optgroup label="Large sizes"><option value="L" disabled>Large</option><option value="XL">Extra Large</option></optgroup><option value="M" disabled>Medium</option><optgroup label="Small sizes" disabled><option value="S">Small</option><option value="XS">Extra Small</option></optgroup></select>'
);
$select = $this->formBuilder->select( $select = $this->formBuilder->select(
'encoded_html', 'encoded_html',
['no_break_space' => '&nbsp;', 'ampersand' => '&amp;', 'lower_than' => '&lt;'], ['no_break_space' => '&nbsp;', 'ampersand' => '&amp;', 'lower_than' => '&lt;'],
+3
View File
@@ -118,8 +118,11 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$result2 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, false); $result2 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, false);
$result3 = $this->htmlBuilder->link("https://a.com/b?id=4&not_id=5", "URL which needs escaping");
$this->assertEquals('<a href="http://www.example.com" class="example-link">&lt;span&gt;Example.com&lt;/span&gt;</a>', $result1); $this->assertEquals('<a href="http://www.example.com" class="example-link">&lt;span&gt;Example.com&lt;/span&gt;</a>', $result1);
$this->assertEquals('<a href="http://www.example.com" class="example-link"><span>Example.com</span></a>', $result2); $this->assertEquals('<a href="http://www.example.com" class="example-link"><span>Example.com</span></a>', $result2);
$this->assertEquals('<a href="https://a.com/b?id=4&amp;not_id=5">URL which needs escaping</a>', $result3);
} }
public function testMailto() public function testMailto()