Skip to content

Commit

Permalink
В настройки гибридного поля "HTML" добавлена опция "Удалять пустые те…
Browse files Browse the repository at this point in the history
…ги", при включении которой в поле при сохранении удаляются теги: `<p></p>`, `<p> </p>`, `<p><br></p>`, `<p><br /></p>`, `<p> <br /></p>`, `<p>&nbsp;</p>`.

issue #298
  • Loading branch information
butschster committed Oct 28, 2014
1 parent 74d20a3 commit 23a9808
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
21 changes: 21 additions & 0 deletions cms/modules/kodicms/classes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ public static function parse_tags($tags)

return $allowed_html;
}

/**
*
* @param string $string
* @param array $tags
* @return string
*/
public static function remove_empty_tags($string, array $tags = array('p', 'div'))
{
$tags = array_unique($tags);

foreach ($tags as $tag)
{
$string = preg_replace_callback("/<{$tag}[^>]*>[\s|&nbsp;|<br ?\/?>]*<\/{$tag}>/iU", function($match) {
return '';
}, $string);
}

return $string;
}

/**#@+
* @access private
Expand Down Expand Up @@ -1251,6 +1271,7 @@ private function decodeEntities($string)
$string = preg_replace_callback('/&#[Xx]([0-9A-Fa-f]+);/', function($match) {
return chr(hexdec($match[1]));
}, $string);

return $string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@ class DataSource_Hybrid_Field_Primitive_HTML extends DataSource_Hybrid_Field_Pri
protected $_props = array(
'default' => NULL,
'filter_html' => FALSE,
'remove_empty_tags' => FALSE,
'allowed_tags' => '<b><i><u><p><ul><li><ol>'
);

public function booleans()
{
return array('filter_html');
return array('filter_html', 'remove_empty_tags');
}

public function set(array $data)
{
return parent::set($data);
}

public function onSetValue($value, DataSource_Hybrid_Document $doc)
{
$value = parent::onSetValue($value, $doc);

if($this->remove_empty_tags === TRUE)
{
return Kses::remove_empty_tags($value);
}

return $value;
}

public function onUpdateDocument(DataSource_Hybrid_Document $old = NULL, DataSource_Hybrid_Document $new)
{
Expand Down
1 change: 1 addition & 0 deletions cms/plugins/hybrid/i18n/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
'Max file size: :size' => 'Максимальный размер файла: :size',
'Allow HTML tags' => 'Разрешить HTML теги',
'Filter HTML tags' => 'Фильтровать HTML теги',
'Remove empty tags' => 'Удалять пустые теги',
'Create hybrid section' => 'Создать гибридный раздел',
'Slug separator' => 'Разделитель',
'Slug from header' => 'Брать значение из Заголовка',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<div class="checkbox">
<label>
<?php echo Form::checkbox('remove_empty_tags', 1, $field->remove_empty_tags == 1, array(
'id' => 'remove_empty_tags'
)); ?> <?php echo __('Remove empty tags'); ?>
</label>
</div>

<div class="checkbox">
<label>
<?php echo Form::checkbox('filter_html', 1, $field->filter_html == 1, array(
Expand Down

0 comments on commit 23a9808

Please sign in to comment.