From f9f4eb29a58cb91396d5dc2d3417d764a5b5080e Mon Sep 17 00:00:00 2001 From: Dave James Miller Date: Wed, 6 Nov 2019 22:56:56 +0000 Subject: [PATCH 1/4] Don't apply formatting function to HtmlStrings --- src/ColumnSortable/SortableLink.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index ec7e54a..2eaf207 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -3,6 +3,7 @@ namespace Kyslik\ColumnSortable; use Kyslik\ColumnSortable\Exceptions\ColumnSortableException; +use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Str; /** @@ -38,7 +39,7 @@ public static function render(array $parameters) $queryString = self::buildQueryString($queryParameters, $sortParameter, $direction); - return ''.htmlentities($title).$trailingTag; + return ''.e($title).$trailingTag; } @@ -90,12 +91,16 @@ public static function explodeSortParameter($parameter) /** - * @param string $title + * @param string|\Illuminate\Contracts\Support\Htmlable $title * * @return string */ private static function applyFormatting($title) { + if ($title instanceof Htmlable) { + return $title; + } + $formatting_function = config('columnsortable.formatting_function', null); if ( ! is_null($formatting_function) && function_exists($formatting_function)) { $title = call_user_func($formatting_function, $title); From a6f30439ab3d7799d15f19f15bbdb2dcc89750d6 Mon Sep 17 00:00:00 2001 From: Dave James Miller Date: Wed, 6 Nov 2019 23:03:26 +0000 Subject: [PATCH 2/4] Add 'format_custom_titles' option to disable formatting custom titles --- src/ColumnSortable/SortableLink.php | 15 +++++++++++---- src/config/columnsortable.php | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index 2eaf207..e4acd7a 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -23,7 +23,7 @@ public static function render(array $parameters) { list($sortColumn, $sortParameter, $title, $queryParameters, $anchorAttributes) = self::parseParameters($parameters); - $title = self::applyFormatting($title); + $title = self::applyFormatting($title, $sortColumn); if ($mergeTitleAs = config('columnsortable.inject_title_as', null)) { request()->merge([$mergeTitleAs => $title]); @@ -55,7 +55,7 @@ public static function parseParameters(array $parameters) //TODO: needs some checks before determining $title $explodeResult = self::explodeSortParameter($parameters[0]); $sortColumn = (empty($explodeResult)) ? $parameters[0] : $explodeResult[1]; - $title = (count($parameters) === 1) ? $sortColumn : $parameters[1]; + $title = (count($parameters) === 1) ? null : $parameters[1]; $queryParameters = (isset($parameters[2]) && is_array($parameters[2])) ? $parameters[2] : []; $anchorAttributes = (isset($parameters[3]) && is_array($parameters[3])) ? $parameters[3] : []; @@ -91,16 +91,23 @@ public static function explodeSortParameter($parameter) /** - * @param string|\Illuminate\Contracts\Support\Htmlable $title + * @param string|\Illuminate\Contracts\Support\Htmlable|null $title + * @param string $sortColumn * * @return string */ - private static function applyFormatting($title) + private static function applyFormatting($title, $sortColumn) { if ($title instanceof Htmlable) { return $title; } + if ($title === null) { + $title = $sortColumn; + } elseif ( ! config('columnsortable.format_custom_titles', true)){ + return $title; + } + $formatting_function = config('columnsortable.formatting_function', null); if ( ! is_null($formatting_function) && function_exists($formatting_function)) { $title = call_user_func($formatting_function, $title); diff --git a/src/config/columnsortable.php b/src/config/columnsortable.php index a49d5c9..51b54b9 100755 --- a/src/config/columnsortable.php +++ b/src/config/columnsortable.php @@ -81,6 +81,11 @@ */ 'formatting_function' => 'ucfirst', + /* + apply formatting function to custom titles as well as column names + */ + 'format_custom_titles' => true, + /* inject title parameter in query strings, use null to turn injection off example: 'inject_title' => 't' will result in ..user/?t="formatted title of sorted column" From 4d23f6fea0ab4aac7dee5133da9df0e837abaa69 Mon Sep 17 00:00:00 2001 From: Dave James Miller Date: Wed, 6 Nov 2019 23:30:37 +0000 Subject: [PATCH 3/4] Fix broken tests and add tests for new functionality --- tests/SortableLinkTest.php | 45 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/SortableLinkTest.php b/tests/SortableLinkTest.php index cf150d6..5853f79 100644 --- a/tests/SortableLinkTest.php +++ b/tests/SortableLinkTest.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Request; +use Illuminate\Support\HtmlString; use Kyslik\ColumnSortable\SortableLink; /** @@ -53,11 +54,51 @@ public function testInjectTitleInQueryStringsIsOff() } + public function testGeneratingTitle() + { + Config::set('columnsortable.formatting_function', 'ucfirst'); + Config::set('columnsortable.format_custom_titles', true); + $link = SortableLink::render(['column']); + + $this->assertSame('Column', $link); + } + + + public function testCustomTitle() + { + Config::set('columnsortable.formatting_function', 'ucfirst'); + Config::set('columnsortable.format_custom_titles', true); + $link = SortableLink::render(['column', 'columnTitle']); + + $this->assertSame('ColumnTitle', $link); + } + + + public function testCustomTitleWithoutFormatting() + { + Config::set('columnsortable.formatting_function', 'ucfirst'); + Config::set('columnsortable.format_custom_titles', false); + $link = SortableLink::render(['column', 'columnTitle']); + + $this->assertSame('columnTitle', $link); + } + + + public function testCustomTitleWithHTML() + { + Config::set('columnsortable.formatting_function', 'ucfirst'); + Config::set('columnsortable.format_custom_titles', true); + $link = SortableLink::render(['column', new HtmlString('columnTitle')]); + + $this->assertSame('columnTitle', $link); + } + + public function testParseParameters() { $parameters = ['column']; $resultArray = SortableLink::parseParameters($parameters); - $expected = ['column', 'column', 'column', [], []]; + $expected = ['column', 'column', null, [], []]; $this->assertEquals($expected, $resultArray); $parameters = ['column', 'ColumnTitle']; @@ -77,7 +118,7 @@ public function testParseParameters() $parameters = ['relation.column']; $resultArray = SortableLink::parseParameters($parameters); - $expected = ['column', 'relation.column', 'column', [], []]; + $expected = ['column', 'relation.column', null, [], []]; $this->assertEquals($expected, $resultArray); $parameters = ['relation.column', 'ColumnTitle']; From 29f7c906803a95af771d8bf97d7287b63aa94688 Mon Sep 17 00:00:00 2001 From: Martin Kiesel Date: Sun, 1 Mar 2020 21:51:02 +0100 Subject: [PATCH 4/4] Add more tests & improve anchor attributes --- src/ColumnSortable/SortableLink.php | 4 ++++ tests/SortableLinkTest.php | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index e4acd7a..320fd8f 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -261,6 +261,10 @@ private static function buildQueryString($queryParameters, $sortParameter, $dire private static function buildAnchorAttributesString($anchorAttributes) { + if (empty($anchorAttributes)) { + return ''; + } + $attributes = []; foreach ($anchorAttributes as $k => $v) { $attributes[] = $k.('' != $v ? '="'.$v.'"' : ''); diff --git a/tests/SortableLinkTest.php b/tests/SortableLinkTest.php index 5853f79..2113640 100644 --- a/tests/SortableLinkTest.php +++ b/tests/SortableLinkTest.php @@ -53,6 +53,20 @@ public function testInjectTitleInQueryStringsIsOff() $this->assertEquals([], Request::all()); } + public function testGeneratingAnchorAttributes() + { + $link = SortableLink::render(['column', 'ColumnTitle', ['a' => 'b'], ['c' => 'd']]); + + $this->assertSame('ColumnTitle', $link); + } + + public function testGeneratingTitleWithoutFormattingFunction() + { + Config::set('columnsortable.formatting_function', null); + $link = SortableLink::render(['column']); + + $this->assertSame('column', $link); + } public function testGeneratingTitle() { @@ -60,7 +74,7 @@ public function testGeneratingTitle() Config::set('columnsortable.format_custom_titles', true); $link = SortableLink::render(['column']); - $this->assertSame('Column', $link); + $this->assertSame('Column', $link); } @@ -70,7 +84,7 @@ public function testCustomTitle() Config::set('columnsortable.format_custom_titles', true); $link = SortableLink::render(['column', 'columnTitle']); - $this->assertSame('ColumnTitle', $link); + $this->assertSame('ColumnTitle', $link); } @@ -80,7 +94,7 @@ public function testCustomTitleWithoutFormatting() Config::set('columnsortable.format_custom_titles', false); $link = SortableLink::render(['column', 'columnTitle']); - $this->assertSame('columnTitle', $link); + $this->assertSame('columnTitle', $link); } @@ -90,7 +104,7 @@ public function testCustomTitleWithHTML() Config::set('columnsortable.format_custom_titles', true); $link = SortableLink::render(['column', new HtmlString('columnTitle')]); - $this->assertSame('columnTitle', $link); + $this->assertSame('columnTitle', $link); }