diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php
index ec7e54a..320fd8f 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;
/**
@@ -22,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]);
@@ -38,7 +39,7 @@ public static function render(array $parameters)
$queryString = self::buildQueryString($queryParameters, $sortParameter, $direction);
- return ''.htmlentities($title).$trailingTag;
+ return ''.e($title).$trailingTag;
}
@@ -54,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] : [];
@@ -90,12 +91,23 @@ public static function explodeSortParameter($parameter)
/**
- * @param string $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);
@@ -249,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/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"
diff --git a/tests/SortableLinkTest.php b/tests/SortableLinkTest.php
index cf150d6..2113640 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;
/**
@@ -52,12 +53,66 @@ 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()
+ {
+ 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 +132,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'];