Skip to content

Commit

Permalink
Ensure introtext and fulltext are valid html
Browse files Browse the repository at this point in the history
The splitting may caused html to get invalid (missing end tags).
  • Loading branch information
Chrissi2812 committed Dec 7, 2018
1 parent cdd1adc commit 196318d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libraries/src/Table/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\Table\Observer\ContentHistory as ContentHistoryObserver;
use Joomla\Registry\Registry;
use Joomla\String\StringHelper;
use Joomla\Utilities\DomHelper;

/**
* Content table
Expand Down Expand Up @@ -147,7 +148,11 @@ public function bind($array, $ignore = '')
}
else
{
list ($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
list ($this->introtext, $this->fulltext) = array_map(function ($value)
{
return DomHelper::fixHtml($value);
}
, preg_split($pattern, $array['articletext'], 2));
}
}

Expand Down
42 changes: 42 additions & 0 deletions libraries/vendor/joomla/utilities/src/DomHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Part of the Joomla Framework Utilities Package
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Utilities;

/**
* Dom Helper.
*
* @since 3.8
*/
class DomHelper
{
public static $dom = null;

private static function getDomInstance()
{
return self::$dom ? self::$dom : self::$dom = new DOMDocument();
}

/**
* Function to fix Html errors like missing end tags.
*
* @param string $html
*
* @return string
*/
public static function fixHtml($html)
{
$dom = self::getDomInstance();
@$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html, LIBXML_HTML_NODEFDTD);
// Fix the html errors
$value = $dom->saveHtml($dom->getElementsByTagName('body')->item(0));

// Remove body tag
return mb_strimwidth($value, 6, mb_strwidth($value, 'UTF-8') - 13, '', 'UTF-8');
}
}

0 comments on commit 196318d

Please sign in to comment.