diff --git a/src/FormBuilder.php b/src/FormBuilder.php index dbe378c..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('' . $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/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 75b56a1..e160df6 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -2,10 +2,19 @@ namespace Collective\Html; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Str; 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', HtmlBuilder::class); $this->app->alias('form', FormBuilder::class); + + $this->setBladeDirectives('Html', get_class_methods(HtmlBuilder::class)); + $this->setBladeDirectives('Form', get_class_methods(FormBuilder::class)); } /** @@ -55,6 +67,28 @@ class HtmlServiceProvider extends ServiceProvider }); } + /** + * Set blade directives + * + * @param string $namespace + * @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. * diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index ff7b37e..d117158 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -239,13 +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', '&'); + $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('', $form5); + $this->assertEquals('', $form6); } public function testSelect() @@ -303,17 +305,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 +348,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.');