-
Notifications
You must be signed in to change notification settings - Fork 359
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
Allow safe tags in escapeHtml #3998
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* Escape view helper | ||
* | ||
* PHP version 8 | ||
* | ||
* Copyright (C) Villanova University 2010. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package View_Helpers | ||
* @author Maccabee Levine <msl321@lehigh.edu> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
|
||
namespace VuFind\View\Helper\Root; | ||
|
||
use Laminas\View\Helper\EscapeHtml as LaminasEscapeHtml; | ||
|
||
/** | ||
* Escape view helper | ||
* | ||
* @category VuFind | ||
* @package View_Helpers | ||
* @author Maccabee Levine <msl321@lehigh.edu> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
class EscapeHtml extends \Laminas\View\Helper\AbstractHelper | ||
{ | ||
protected $laminasEscapeHtml; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->laminasEscapeHtml = new LaminasEscapeHtml(); | ||
} | ||
|
||
/** | ||
* This helper calls Laminas escapeHtml, but allows safe styling characters | ||
* | ||
* @param string $str The string to escape | ||
* @param array $except Array of tag names to leave as is. Only simple tags | ||
* (no attributes). | ||
* | ||
* @return string The partially escaped string | ||
*/ | ||
public function __invoke($str, $except = ['em', 'i', 'b']) | ||
{ | ||
$escaped = $this->laminasEscapeHtml->__invoke($str); | ||
|
||
// Revert ok chars | ||
foreach ($except as $tag) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be more efficient to create arrays of searches and replacements and then do a single str_replace call, instead of doing multiple str_replace calls in a loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...but if @EreMaijala's solution is the best way forward, it's a moot point. :-) |
||
$escaped = str_replace("<{$tag}>", "<{$tag}>", $escaped); | ||
$escaped = str_replace("</{$tag}>", "</{$tag}>", $escaped); | ||
} | ||
|
||
return $escaped; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so the effect is easily demoed, on any record with a reasonably long title.