diff --git a/src/FormBuilder.php b/src/FormBuilder.php old mode 100755 new mode 100644 index a6ebd53..b9ec8b4 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -13,7 +13,6 @@ use Illuminate\Contracts\Routing\UrlGenerator; class FormBuilder { - use Macroable, Componentable { Macroable::__call as macroCall; Componentable::__call as componentCall; @@ -210,16 +209,21 @@ class FormBuilder * @param string $name * @param string $value * @param array $options + * @param bool $escape_html * * @return \Illuminate\Support\HtmlString */ - public function label($name, $value = null, $options = []) + public function label($name, $value = null, $options = [], $escape_html = true) { $this->labels[] = $name; $options = $this->html->attributes($options); - $value = e($this->formatLabel($name, $value)); + $value = $this->formatLabel($name, $value); + + if ($escape_html) { + $value = $this->html->entities($value); + } return $this->toHtmlString(''); } @@ -1080,7 +1084,7 @@ class FormBuilder return $value; } - if (! is_null($this->old($name))) { + if (! is_null($this->old($name)) && $name != '_method') { return $this->old($name); } diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index bcc5088..037e873 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -10,7 +10,6 @@ use Illuminate\Contracts\Routing\UrlGenerator; class HtmlBuilder { - use Macroable, Componentable { Macroable::__call as macroCall; Componentable::__call as componentCall; @@ -147,10 +146,11 @@ class HtmlBuilder * @param string $title * @param array $attributes * @param bool $secure + * @param bool $escape * * @return \Illuminate\Support\HtmlString */ - public function link($url, $title = null, $attributes = [], $secure = null) + public function link($url, $title = null, $attributes = [], $secure = null, $escape = true) { $url = $this->url->to($url, [], $secure); @@ -158,7 +158,11 @@ class HtmlBuilder $title = $url; } - return $this->toHtmlString('attributes($attributes) . '>' . $this->entities($title) . ''); + if ($escape) { + $title = $this->entities($title); + } + + return $this->toHtmlString('attributes($attributes) . '>' . $title . ''); } /** @@ -242,18 +246,23 @@ class HtmlBuilder * @param string $email * @param string $title * @param array $attributes + * @param bool $escape * * @return \Illuminate\Support\HtmlString */ - public function mailto($email, $title = null, $attributes = []) + public function mailto($email, $title = null, $attributes = [], $escape = true) { $email = $this->email($email); $title = $title ?: $email; + if ($escape) { + $title = $this->entities($title); + } + $email = $this->obfuscate('mailto:') . $email; - return $this->toHtmlString('attributes($attributes) . '>' . $this->entities($title) . ''); + return $this->toHtmlString('attributes($attributes) . '>' . $title . ''); } /** diff --git a/src/helpers.php b/src/helpers.php index 684102d..14f0361 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -8,12 +8,13 @@ if (! function_exists('link_to')) { * @param string $title * @param array $attributes * @param bool $secure + * @param bool $escape * * @return string */ - function link_to($url, $title = null, $attributes = [], $secure = null) + function link_to($url, $title = null, $attributes = [], $secure = null, $escape = true) { - return app('html')->link($url, $title, $attributes, $secure); + return app('html')->link($url, $title, $attributes, $secure, $escape); } } diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 77d9524..d0490bc 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -14,7 +14,6 @@ use Mockery as m; class FormAccessibleTest extends PHPUnit_Framework_TestCase { - public function setUp() { Capsule::table('models')->truncate(); @@ -65,7 +64,6 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase class ModelThatUsesForms extends Model { - use FormAccessible; protected $table = 'models'; @@ -93,7 +91,6 @@ class ModelThatUsesForms extends Model class ModelThatDoesntUseForms extends Model { - protected $table = 'models'; public function getStringAttribute($value) diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index ee8ac49..3e4eaad 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -58,9 +58,11 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase { $form1 = $this->formBuilder->label('foo', 'Foobar'); $form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']); + $form3 = $this->formBuilder->label('foo', 'Foobar bar', null, false); $this->assertEquals('', $form1); $this->assertEquals('', $form2); + $this->assertEquals('', $form3); } public function testFormInput() @@ -532,7 +534,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase class FormBuilderModelStub { - protected $data; public function __construct(array $data = []) diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 837328c..44baeb8 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -89,4 +89,30 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase $this->assertTrue($this->htmlBuilder->hasComponent('tweet')); } + + public function testLink() + { + $result1 = $this->htmlBuilder->link("http://www.example.com", "Example.com", ["class" => "example-link"], null, true); + + $result2 = $this->htmlBuilder->link("http://www.example.com", "Example.com", ["class" => "example-link"], null, false); + + $this->assertEquals('<span>Example.com</span>', $result1); + $this->assertEquals('Example.com', $result2); + } + + public function testMailto() + { + $htmlBuilder = m::mock('Collective\Html\HtmlBuilder[obfuscate,email]', [$this->urlGenerator, $this->viewFactory]); + $htmlBuilder->shouldReceive('obfuscate', 'email')->andReturnUsing(function () { + $args = func_get_args(); + return $args[0]; + }); + + $result1 = $htmlBuilder->mailto("person@example.com", "First Name Last", ["class" => "example-link"], true); + + $result2 = $htmlBuilder->mailto("person@example.com", "First Name Last", ["class" => "example-link"], false); + + $this->assertEquals('<span>First Name Last</span>', $result1); + $this->assertEquals('First Name Last', $result2); + } }