From c1a105975bc94c20617031094ef7a360d6149e8c Mon Sep 17 00:00:00 2001 From: Stefan Printezis Date: Fri, 28 Oct 2016 12:37:15 +0200 Subject: [PATCH 1/4] 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); From df0e11272078345a4efb2630e407b471669e10a3 Mon Sep 17 00:00:00 2001 From: Stefan Printezis Date: Sun, 30 Oct 2016 01:21:38 +0200 Subject: [PATCH 2/4] Less ambiguous test samples for the select options and placeholder --- tests/FormBuilderTest.php | 40 +++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index e3e8041..e60212c 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -237,15 +237,15 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase { $form1 = $this->formBuilder->textarea('foo'); $form2 = $this->formBuilder->textarea('foo', 'foobar'); - $form3 = $this->formBuilder->textarea('foo', '&'); - $form4 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); - $form5 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); + $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); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + $this->assertEquals('', $form5); } public function testSelect() @@ -289,7 +289,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase 'Large sizes' => [ 'L' => 'Large', 'XL' => 'Extra Large', - 'XXL' => 'Extra Large', ], 'S' => 'Small', ], @@ -302,7 +301,18 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals( $select, - '' + '' + ); + + $select = $this->formBuilder->select( + 'encoded_html', + ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'], + null + ); + + $this->assertEquals( + $select, + '' ); } @@ -334,10 +344,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', @@ -347,6 +357,16 @@ 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() From 6d1d46a258d3cd662f36e8f956634950b4517eeb Mon Sep 17 00:00:00 2001 From: Stefan Printezis Date: Thu, 10 Nov 2016 23:58:02 +0100 Subject: [PATCH 3/4] Created a function for correctly converting characters to entities. --- src/FormBuilder.php | 8 ++++---- src/HtmlBuilder.php | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 0e03984..9b2fe1c 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('' . htmlentities($value, ENT_QUOTES, 'UTF-8') . ''); + return $this->toHtmlString('' . $this->html->escapeAll($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) . '>' . htmlentities($display, ENT_QUOTES, 'UTF-8') . ''); + return $this->toHtmlString('html->attributes($options) . '>' . $this->html->escapeAll($display) . ''); } /** @@ -698,7 +698,7 @@ class FormBuilder $options = compact('selected'); $options['value'] = ''; - return $this->toHtmlString('html->attributes($options) . '>' . htmlentities($display, ENT_QUOTES, 'UTF-8') . ''); + return $this->toHtmlString('html->attributes($options) . '>' . $this->html->escapeAll($display) . ''); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index b42ce53..cd504f8 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -53,6 +53,14 @@ class HtmlBuilder return htmlentities($value, ENT_QUOTES, 'UTF-8', false); } + /** + * Convert all applicable characters to HTML entities. + */ + public function escapeAll($value) + { + return htmlentities($value, ENT_QUOTES, 'UTF-8'); + } + /** * Convert entities to HTML characters. * @@ -387,7 +395,7 @@ class HtmlBuilder if (is_array($value)) { return $this->nestedListing($key, $type, $value); } else { - return '
  • ' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '
  • '; + return '
  • ' . $this->html->escapeAll($value) . '
  • '; } } @@ -449,7 +457,7 @@ class HtmlBuilder } if (! is_null($value)) { - return $key . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '"'; + return $key . '="' . $this->html->escapeAll($value) . '"'; } } From f153be53130cac34e3dae7d3d7e86ded96e90532 Mon Sep 17 00:00:00 2001 From: Stefan Printezis Date: Fri, 11 Nov 2016 00:05:46 +0100 Subject: [PATCH 4/4] Added function parameters and corrected 2 function calls --- src/HtmlBuilder.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index cd504f8..e6bc518 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -55,6 +55,10 @@ class HtmlBuilder /** * Convert all applicable characters to HTML entities. + * + * @param string $value + * + * @return string */ public function escapeAll($value) { @@ -395,7 +399,7 @@ class HtmlBuilder if (is_array($value)) { return $this->nestedListing($key, $type, $value); } else { - return '
  • ' . $this->html->escapeAll($value) . '
  • '; + return '
  • ' . $this->escapeAll($value) . '
  • '; } } @@ -457,7 +461,7 @@ class HtmlBuilder } if (! is_null($value)) { - return $key . '="' . $this->html->escapeAll($value) . '"'; + return $key . '="' . $this->escapeAll($value) . '"'; } }