From e9d7084769887cc641d71a76c356958880cee1e0 Mon Sep 17 00:00:00 2001 From: mlantz Date: Wed, 6 Apr 2016 14:35:03 -0400 Subject: [PATCH 01/11] Supports blade directives Now creates a collection of blade directives for available html, and form methods --- src/HtmlServiceProvider.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index c632e56..69736b9 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -2,10 +2,19 @@ namespace Collective\Html; +use Illuminate\Support\Str; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class HtmlServiceProvider extends ServiceProvider { + /** + * Supported Blade Directives + * + * @var array + */ + protected $directives = ['entities','decode','script','style','image','favicon','link','secureLink','linkAsset','linkSecureAsset','linkRoute','linkAction','mailto','email','ol','ul','dl','meta','tag','open','model','close','token','label','input','text','password','hidden','email','tel','number','date','datetime','datetimeLocal','time','url','file','textarea','select','selectRange','selectYear','selectMonth','getSelectOption','checkbox','radio','reset','image','color','submit','button','old' + ]; /** * Indicates if loading of the provider is deferred. @@ -27,6 +36,9 @@ class HtmlServiceProvider extends ServiceProvider $this->app->alias('html', 'Collective\Html\HtmlBuilder'); $this->app->alias('form', 'Collective\Html\FormBuilder'); + + $this->setBladeDirectives('Html', get_class_methods(HtmlBuilder::class)); + $this->setBladeDirectives('Form', get_class_methods(FormBuilder::class)); } /** @@ -55,6 +67,29 @@ class HtmlServiceProvider extends ServiceProvider }); } + /** + * Set blade directives + * + * @param string $type + * @param array $methods + * + * @return void + */ + protected function setBladeDirectives($namespace, $methods) + { + foreach ($methods as $method) { + if (in_array($method, $this->directives)) { + + $snakeMethod = Str::snake($method); + $directive = strtolower($namespace).'_'.$snakeMethod; + + Blade::directive($directive, function($expression) use ($namespace, $method) { + return ""; + }); + } + } + } + /** * Get the services provided by the provider. * From 04f921062e81c8c9d381e4c8561edd63202eeddb Mon Sep 17 00:00:00 2001 From: mlantz Date: Wed, 6 Apr 2016 15:17:41 -0400 Subject: [PATCH 02/11] style fix --- src/HtmlServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 69736b9..17082d6 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -83,7 +83,7 @@ class HtmlServiceProvider extends ServiceProvider $snakeMethod = Str::snake($method); $directive = strtolower($namespace).'_'.$snakeMethod; - Blade::directive($directive, function($expression) use ($namespace, $method) { + Blade::directive($directive, function ($expression) use ($namespace, $method) { return ""; }); } From 67b3289891173801b153716ce86367ac3a8c861f Mon Sep 17 00:00:00 2001 From: mlantz Date: Wed, 6 Apr 2016 15:18:08 -0400 Subject: [PATCH 03/11] More style fixes --- src/HtmlServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 17082d6..44c1188 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -79,10 +79,8 @@ class HtmlServiceProvider extends ServiceProvider { foreach ($methods as $method) { if (in_array($method, $this->directives)) { - $snakeMethod = Str::snake($method); $directive = strtolower($namespace).'_'.$snakeMethod; - Blade::directive($directive, function ($expression) use ($namespace, $method) { return ""; }); From 3479b19ce17fe12e360731213e48403d9d1c50bf Mon Sep 17 00:00:00 2001 From: mlantz Date: Sun, 12 Jun 2016 18:13:44 -0400 Subject: [PATCH 04/11] typo --- src/HtmlServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 44c1188..0ea3555 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -70,7 +70,7 @@ class HtmlServiceProvider extends ServiceProvider /** * Set blade directives * - * @param string $type + * @param string $namespace * @param array $methods * * @return void From 2ef4c2e42c63dd36a9782de0acb7932b7273e66f Mon Sep 17 00:00:00 2001 From: Matt Lantz Date: Fri, 21 Oct 2016 16:21:02 -0400 Subject: [PATCH 05/11] Fix for 5.3 --- src/HtmlServiceProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 0ea3555..c0ee0da 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -81,8 +81,9 @@ class HtmlServiceProvider extends ServiceProvider if (in_array($method, $this->directives)) { $snakeMethod = Str::snake($method); $directive = strtolower($namespace).'_'.$snakeMethod; + Blade::directive($directive, function ($expression) use ($namespace, $method) { - return ""; + return ""; }); } } From 05373f429f2b95c28ed04b1c56534fdba356cc94 Mon Sep 17 00:00:00 2001 From: Adam Engebretson Date: Thu, 26 Jan 2017 13:10:21 -0600 Subject: [PATCH 06/11] Preparing 5.5-dev --- composer.json | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index fd6e53a..9f7ba69 100755 --- a/composer.json +++ b/composer.json @@ -19,14 +19,14 @@ ], "require": { "php": ">=5.6.4", - "illuminate/http": "5.4.*", - "illuminate/routing": "5.4.*", - "illuminate/session": "5.4.*", - "illuminate/support": "5.4.*", - "illuminate/view": "5.4.*" + "illuminate/http": "5.5.*", + "illuminate/routing": "5.5.*", + "illuminate/session": "5.5.*", + "illuminate/support": "5.5.*", + "illuminate/view": "5.5.*" }, "require-dev": { - "illuminate/database": "5.4.*", + "illuminate/database": "5.5.*", "mockery/mockery": "~0.9.4", "phpunit/phpunit": "~5.4" }, @@ -37,5 +37,11 @@ "files": [ "src/helpers.php" ] - } + }, + "extra": { + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "minimum-stability": "dev" } From 856e1e6d6b8036e7371d0d8b20eab48f3d4b08c8 Mon Sep 17 00:00:00 2001 From: mlantz Date: Thu, 20 Apr 2017 00:02:24 -0400 Subject: [PATCH 07/11] Fixes for escape issue --- src/FormBuilder.php | 8 ++++---- src/HtmlBuilder.php | 16 ++-------------- tests/FormBuilderTest.php | 23 ----------------------- tests/HtmlBuilderTest.php | 22 ---------------------- 4 files changed, 6 insertions(+), 63 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index b10b808..28f7c69 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -485,7 +485,7 @@ class FormBuilder // the element. Then we'll create the final textarea elements HTML for us. $options = $this->html->attributes($options); - return $this->toHtmlString('' . $this->html->escapeAll($value). ''); + return $this->toHtmlString('' . e($value). ''); } /** @@ -662,7 +662,7 @@ class FormBuilder $html[] = $this->option($display, $value, $selected); } - return $this->toHtmlString('' . implode('', $html) . ''); + return $this->toHtmlString('' . implode('', $html) . ''); } /** @@ -680,7 +680,7 @@ class FormBuilder $options = ['value' => $value, 'selected' => $selected]; - return $this->toHtmlString('html->attributes($options) . '>' . $this->html->escapeAll($display) . ''); + return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); } /** @@ -698,7 +698,7 @@ class FormBuilder $options = compact('selected'); $options['value'] = ''; - return $this->toHtmlString('html->attributes($options) . '>' . $this->html->escapeAll($display) . ''); + return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index e6bc518..e531792 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -53,18 +53,6 @@ class HtmlBuilder return htmlentities($value, ENT_QUOTES, 'UTF-8', false); } - /** - * Convert all applicable characters to HTML entities. - * - * @param string $value - * - * @return string - */ - public function escapeAll($value) - { - return htmlentities($value, ENT_QUOTES, 'UTF-8'); - } - /** * Convert entities to HTML characters. * @@ -399,7 +387,7 @@ class HtmlBuilder if (is_array($value)) { return $this->nestedListing($key, $type, $value); } else { - return '
  • ' . $this->escapeAll($value) . '
  • '; + return '
  • ' . e($value) . '
  • '; } } @@ -461,7 +449,7 @@ class HtmlBuilder } if (! is_null($value)) { - return $key . '="' . $this->escapeAll($value) . '"'; + return $key . '="' . e($value) . '"'; } } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index ff7b37e..0146c2b 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -239,13 +239,11 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $form2 = $this->formBuilder->textarea('foo', 'foobar'); $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); - $form5 = $this->formBuilder->textarea('encoded_html', '&'); $this->assertEquals('', $form1); $this->assertEquals('', $form2); $this->assertEquals('', $form3); $this->assertEquals('', $form4); - $this->assertEquals('', $form5); } public function testSelect() @@ -303,17 +301,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $select, '' ); - - $select = $this->formBuilder->select( - 'encoded_html', - ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'], - null - ); - - $this->assertEquals( - $select, - '' - ); } public function testFormSelectRepopulation() @@ -357,16 +344,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ); $this->assertEquals($select, ''); - - $select = $this->formBuilder->select( - 'encoded_html', - ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'], - null, - ['placeholder' => 'Select the  '] - ); - $this->assertEquals($select, - '' - ); } public function testFormSelectYear() diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 747c89a..cdc0c4a 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -39,28 +39,6 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('
    foo
    bar
    bing
    baz
    ', $result); } - public function testOl() - { - $list = ['foo', 'bar', '&']; - - $attributes = ['class' => 'example']; - - $ol = $this->htmlBuilder->ol($list, $attributes); - - $this->assertEquals('
    1. foo
    2. bar
    3. &amp;
    ', $ol); - } - - public function testUl() - { - $list = ['foo', 'bar', '&']; - - $attributes = ['class' => 'example']; - - $ul = $this->htmlBuilder->ul($list, $attributes); - - $this->assertEquals('
    • foo
    • bar
    • &amp;
    ', $ul); - } - public function testMeta() { $result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.'); From 7fb1fb13d98e4e1c4242fb010eed0f01a2e09800 Mon Sep 17 00:00:00 2001 From: mlantz Date: Thu, 20 Apr 2017 00:08:53 -0400 Subject: [PATCH 08/11] Composer fix --- composer.json | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 9f7ba69..743e2a9 100755 --- a/composer.json +++ b/composer.json @@ -19,14 +19,14 @@ ], "require": { "php": ">=5.6.4", - "illuminate/http": "5.5.*", - "illuminate/routing": "5.5.*", - "illuminate/session": "5.5.*", - "illuminate/support": "5.5.*", - "illuminate/view": "5.5.*" + "illuminate/http": "5.4.*", + "illuminate/routing": "5.4.*", + "illuminate/session": "5.4.*", + "illuminate/support": "5.4.*", + "illuminate/view": "5.4.*" }, "require-dev": { - "illuminate/database": "5.5.*", + "illuminate/database": "5.4.*", "mockery/mockery": "~0.9.4", "phpunit/phpunit": "~5.4" }, @@ -37,11 +37,5 @@ "files": [ "src/helpers.php" ] - }, - "extra": { - "branch-alias": { - "dev-master": "5.5-dev" - } - }, - "minimum-stability": "dev" -} + } +} \ No newline at end of file From aeaffb5785b98b24205db3b5725254f89c8836b5 Mon Sep 17 00:00:00 2001 From: mlantz Date: Wed, 26 Apr 2017 01:26:03 -0400 Subject: [PATCH 09/11] Empty line --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 743e2a9..fd6e53a 100755 --- a/composer.json +++ b/composer.json @@ -38,4 +38,4 @@ "src/helpers.php" ] } -} \ No newline at end of file +} From b583044d36b9c9c3585a07c38025840aa5e0083f Mon Sep 17 00:00:00 2001 From: mlantz Date: Wed, 26 Apr 2017 11:33:01 -0400 Subject: [PATCH 10/11] Dropping e on values that should remain literal --- src/FormBuilder.php | 2 +- src/HtmlBuilder.php | 2 +- tests/FormBuilderTest.php | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 5ae7ecc..62fc08a 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -485,7 +485,7 @@ class FormBuilder // the element. Then we'll create the final textarea elements HTML for us. $options = $this->html->attributes($options); - return $this->toHtmlString('' . e($value). ''); + return $this->toHtmlString('' . $value. ''); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index e531792..73796fb 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -449,7 +449,7 @@ class HtmlBuilder } if (! is_null($value)) { - return $key . '="' . e($value) . '"'; + return $key . '="' . $value . '"'; } } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 0146c2b..3114627 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -239,11 +239,15 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $form2 = $this->formBuilder->textarea('foo', 'foobar'); $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); + $form5 = $this->formBuilder->textarea('encoded_html', 'Eggs & Sausage', ['size' => '60x50']); + $form6 = $this->formBuilder->textarea('encoded_html', 'Eggs &&& Sausage', ['size' => '60x50']); $this->assertEquals('', $form1); $this->assertEquals('', $form2); $this->assertEquals('', $form3); $this->assertEquals('', $form4); + $this->assertEquals('', $form5); + $this->assertEquals('', $form6); } public function testSelect() From bc097b24e20b4aee78c6291fe93fef37de1ad485 Mon Sep 17 00:00:00 2001 From: mlantz Date: Wed, 26 Apr 2017 11:51:23 -0400 Subject: [PATCH 11/11] Reversion to the original state with the e method --- src/FormBuilder.php | 2 +- src/HtmlBuilder.php | 2 +- tests/FormBuilderTest.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 62fc08a..5ae7ecc 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -485,7 +485,7 @@ class FormBuilder // the element. Then we'll create the final textarea elements HTML for us. $options = $this->html->attributes($options); - return $this->toHtmlString('' . $value. ''); + return $this->toHtmlString('' . e($value). ''); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 73796fb..e531792 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -449,7 +449,7 @@ class HtmlBuilder } if (! is_null($value)) { - return $key . '="' . $value . '"'; + return $key . '="' . e($value) . '"'; } } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 3114627..d117158 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -240,14 +240,14 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); $form5 = $this->formBuilder->textarea('encoded_html', 'Eggs & Sausage', ['size' => '60x50']); - $form6 = $this->formBuilder->textarea('encoded_html', 'Eggs &&& Sausage', ['size' => '60x50']); + $form6 = $this->formBuilder->textarea('encoded_html', 'Eggs && Sausage', ['size' => '60x50']); $this->assertEquals('', $form1); $this->assertEquals('', $form2); $this->assertEquals('', $form3); $this->assertEquals('', $form4); - $this->assertEquals('', $form5); - $this->assertEquals('', $form6); + $this->assertEquals('', $form5); + $this->assertEquals('', $form6); } public function testSelect()