Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[8.0] Only escape callable output of add and edit column." #1914

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 37 additions & 50 deletions src/Processors/DataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ class DataProcessor
*/
protected $includeIndex;

/**
* @var string
*/
protected $indexColumn;

/**
* @var array
*/
Expand Down Expand Up @@ -103,17 +98,21 @@ public function process($object = false)
$indexColumn = config('datatables.index_column', 'DT_RowIndex');

foreach ($this->results as $row) {
$data = $this->escapeRow(Helper::convertToArray($row));
$value = $this->addColumns($data, $row);
$value = $this->editColumns($value, $row);
$value = $this->setupRowVariables($value, $row);
$value = $this->selectOnlyNeededColumns($value);
$value = $this->removeExcessColumns($value);
$value = $this->addIndexColumn($value);
$data = Helper::convertToArray($row);
$value = $this->addColumns($data, $row);
$value = $this->editColumns($value, $row);
$value = $this->setupRowVariables($value, $row);
$value = $this->selectOnlyNeededColumns($value);
$value = $this->removeExcessColumns($value);

if ($this->includeIndex) {
$value[$indexColumn] = ++$this->start;
}

$this->output[] = $object ? $value : $this->flatten($value);
}

return $this->output;
return $this->escapeColumns($this->output);
}

/**
Expand All @@ -126,28 +125,13 @@ public function process($object = false)
protected function addColumns($data, $row)
{
foreach ($this->appendColumns as $key => $value) {
$value['content'] = Helper::compileContent($value['content'], $data, $row, $this->shouldEscapeColumn($key));
$value['content'] = Helper::compileContent($value['content'], $data, $row);
$data = Helper::includeInArray($value, $data);
}

return $data;
}

/**
* Process add index column.
*
* @param mixed $data
* @return array
*/
protected function addIndexColumn($data)
{
if ($this->includeIndex) {
$data[$this->indexColumn] = ++$this->start;
}

return $data;
}

/**
* Process edit columns.
*
Expand All @@ -158,7 +142,7 @@ protected function addIndexColumn($data)
protected function editColumns($data, $row)
{
foreach ($this->editColumns as $key => $value) {
$value['content'] = Helper::compileContent($value['content'], $data, $row, $this->shouldEscapeColumn($key));
$value['content'] = Helper::compileContent($value['content'], $data, $row);
Arr::set($data, $value['name'], $value['content']);
}

Expand Down Expand Up @@ -223,7 +207,6 @@ protected function removeExcessColumns(array $data)
public function flatten(array $array)
{
$return = [];

foreach ($array as $key => $value) {
if (in_array($key, $this->exceptions)) {
$return[$key] = $value;
Expand All @@ -235,6 +218,28 @@ public function flatten(array $array)
return $return;
}

/**
* Escape column values as declared.
*
* @param array $output
* @return array
*/
protected function escapeColumns(array $output)
{
return array_map(function ($row) {
if ($this->escapeColumns == '*') {
$row = $this->escapeRow($row);
} elseif (is_array($this->escapeColumns)) {
$columns = array_diff($this->escapeColumns, $this->rawColumns);
foreach ($columns as $key) {
array_set($row, $key, e(array_get($row, $key)));
}
}

return $row;
}, $output);
}

/**
* Escape all values of row.
*
Expand All @@ -244,9 +249,8 @@ public function flatten(array $array)
protected function escapeRow(array $row)
{
$arrayDot = array_filter(array_dot($row));

foreach ($arrayDot as $key => $value) {
if ($this->shouldEscapeColumn($key)) {
if (! in_array($key, $this->rawColumns)) {
$arrayDot[$key] = e($value);
}
}
Expand All @@ -257,21 +261,4 @@ protected function escapeRow(array $row)

return $row;
}

/**
* Whether to escape column or no.
*
* @param string $key
* @return bool
*/
protected function shouldEscapeColumn($key)
{
if ($this->escapeColumns === '*') {
return ! in_array($key, $this->rawColumns); // escape if is not a raw column
} elseif (is_array($this->escapeColumns)) {
return in_array($key, array_diff($this->escapeColumns, $this->rawColumns));
} else {
return false;
}
}
}
9 changes: 4 additions & 5 deletions src/Utilities/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,17 @@ protected static function isItemOrderInvalid($item, $array)
* @param mixed $content Pre-processed content
* @param array $data data to use with blade template
* @param mixed $param parameter to call with callable
* @param bool $escape whether to escape the output of the callable
* @return mixed
*/
public static function compileContent($content, array $data, $param, $escape = true)
public static function compileContent($content, array $data, $param)
{
if (is_string($content)) {
return static::compileBlade($content, static::getMixedValue($data, $param));
} elseif (is_callable($content)) {
return $escape ? e($content($param)) : $content($param);
} else {
return $content;
return $content($param);
}

return $content;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/config/datatables.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

return [

/*
* DataTables search options.
*/
Expand Down