From c1a105975bc94c20617031094ef7a360d6149e8c Mon Sep 17 00:00:00 2001 From: Stefan Printezis Date: Fri, 28 Oct 2016 12:37:15 +0200 Subject: [PATCH] Values are encoded consistently by the formbuilder and the htmlbuilder --- src/FormBuilder.php | 8 ++++---- src/HtmlBuilder.php | 6 +++--- tests/FormBuilderTest.php | 17 ++++++++++------- tests/HtmlBuilderTest.php | 24 +++++++++++++++++++++++- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 4e7cf0f..0e03984 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('' . htmlentities($value, ENT_QUOTES, 'UTF-8') . ''); } /** @@ -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) . '>' . e($display) . ''); + return $this->toHtmlString('html->attributes($options) . '>' . htmlentities($display, ENT_QUOTES, 'UTF-8') . ''); } /** @@ -698,7 +698,7 @@ class FormBuilder $options = compact('selected'); $options['value'] = ''; - return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); + return $this->toHtmlString('html->attributes($options) . '>' . htmlentities($display, ENT_QUOTES, 'UTF-8') . ''); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 7306fc4..b42ce53 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -288,7 +288,7 @@ class HtmlBuilder { return str_repeat(' ', $num); } - + /** * Generate an ordered list of items. * @@ -387,7 +387,7 @@ class HtmlBuilder if (is_array($value)) { return $this->nestedListing($key, $type, $value); } else { - return '
  • ' . e($value) . '
  • '; + return '
  • ' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '
  • '; } } @@ -449,7 +449,7 @@ class HtmlBuilder } if (! is_null($value)) { - return $key . '="' . e($value) . '"'; + return $key . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '"'; } } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 3e4eaad..e3e8041 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -237,13 +237,15 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase { $form1 = $this->formBuilder->textarea('foo'); $form2 = $this->formBuilder->textarea('foo', 'foobar'); - $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); - $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); + $form3 = $this->formBuilder->textarea('foo', '&'); + $form4 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); + $form5 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); $this->assertEquals('', $form1); $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + $this->assertEquals('', $form5); } public function testSelect() @@ -287,6 +289,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase 'Large sizes' => [ 'L' => 'Large', 'XL' => 'Extra Large', + 'XXL' => 'Extra Large', ], 'S' => 'Small', ], @@ -299,7 +302,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals( $select, - '' + '' ); } @@ -331,10 +334,10 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase 'size', ['L' => 'Large', 'S' => 'Small'], null, - ['placeholder' => 'Select One...'] + ['placeholder' => 'Select  One...'] ); $this->assertEquals($select, - ''); + ''); $select = $this->formBuilder->select( 'size', diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 44baeb8..747c89a 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -39,6 +39,28 @@ 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. &
    ', $ol); + } + + public function testUl() + { + $list = ['foo', 'bar', '&']; + + $attributes = ['class' => 'example']; + + $ul = $this->htmlBuilder->ul($list, $attributes); + + $this->assertEquals('
    • foo
    • bar
    • &
    ', $ul); + } + public function testMeta() { $result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.'); @@ -58,7 +80,7 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase $this->htmlBuilder->image('http://example.com/image1'), $this->htmlBuilder->image('http://example.com/image2'), ]; - + $result4 = $this->htmlBuilder->tag('div', $content, ['class' => 'row']); $this->assertEquals('

    ' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '

    ' . PHP_EOL, $result1);