From f9a4e185f411ada1e720874f27380d311261b250 Mon Sep 17 00:00:00 2001 From: James Alday Date: Sat, 21 May 2016 15:10:11 -0400 Subject: [PATCH 1/5] Make escaping of labels for HTML and mailto links optional --- src/HtmlBuilder.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index bcc5088..3c3c463 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -147,10 +147,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 +159,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 +247,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 . ''); } /** From 0d1d1fcd5a0d4d63bbcf1e4a4ce354fce6a00746 Mon Sep 17 00:00:00 2001 From: James Alday Date: Sat, 21 May 2016 15:50:41 -0400 Subject: [PATCH 2/5] Add 'escape' option to helper function --- src/helpers.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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); } } From 92a4833e1192a0cfb865e5196ca709c576431153 Mon Sep 17 00:00:00 2001 From: James Alday Date: Fri, 10 Jun 2016 14:01:04 -0400 Subject: [PATCH 3/5] Add test for htmlBuilder link and mailto escaping --- tests/HtmlBuilderTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 837328c..4c24c76 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); + } } From 0fc25932dbc9029b3772b9d849aad551e973e55e Mon Sep 17 00:00:00 2001 From: James Alday Date: Fri, 10 Jun 2016 14:03:38 -0400 Subject: [PATCH 4/5] Update closure syntax for StyleCI check --- tests/HtmlBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 4c24c76..44baeb8 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -103,7 +103,7 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase public function testMailto() { $htmlBuilder = m::mock('Collective\Html\HtmlBuilder[obfuscate,email]', [$this->urlGenerator, $this->viewFactory]); - $htmlBuilder->shouldReceive('obfuscate', 'email')->andReturnUsing(function() { + $htmlBuilder->shouldReceive('obfuscate', 'email')->andReturnUsing(function () { $args = func_get_args(); return $args[0]; }); From 24ee0aadc05dfa53a3ccf5e4f75d8abbd8c1bf4c Mon Sep 17 00:00:00 2001 From: James Alday Date: Fri, 10 Jun 2016 15:12:35 -0400 Subject: [PATCH 5/5] StyleCI fixes - remove empty line after class declaration --- src/FormBuilder.php | 1 - src/HtmlBuilder.php | 1 - tests/FormAccessibleTest.php | 3 --- tests/FormBuilderTest.php | 1 - 4 files changed, 6 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index d4fb35f..a9a17d1 100755 --- 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; diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 3c3c463..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; 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..41cd166 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -532,7 +532,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase class FormBuilderModelStub { - protected $data; public function __construct(array $data = [])