Skip to content

Commit

Permalink
[+]: optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Oct 23, 2016
1 parent 60197ca commit 2a5e10c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 54 deletions.
68 changes: 21 additions & 47 deletions src/voku/helper/HtmlDomParser.php
Expand Up @@ -48,10 +48,10 @@ class HtmlDomParser
protected static $domLinkReplaceHelper = array(
'orig' => array('[', ']', '{', '}',),
'tmp' => array(
'!!!!HTML_DOM__SQUARE_BRACKET_LEFT!!!!',
'!!!!HTML_DOM__SQUARE_BRACKET_RIGHT!!!!',
'!!!!HTML_DOM__BRACKET_LEFT!!!!',
'!!!!HTML_DOM__BRACKET_RIGHT!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__SQUARE_BRACKET_LEFT!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__SQUARE_BRACKET_RIGHT!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__BRACKET_LEFT!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__BRACKET_RIGHT!!!!',
),
);

Expand All @@ -61,10 +61,10 @@ class HtmlDomParser
protected static $domReplaceHelper = array(
'orig' => array('&', '|', '+', '%'),
'tmp' => array(
'!!!!HTML_DOM__AMP!!!!',
'!!!!HTML_DOM__PIPE!!!!',
'!!!!HTML_DOM__PLUS!!!!',
'!!!!HTML_DOM__PERCENT!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__AMP!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__PIPE!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__PLUS!!!!',
'!!!!SIMPLE_HTML_DOM__VOKU__PERCENT!!!!',
),
);

Expand Down Expand Up @@ -93,25 +93,15 @@ class HtmlDomParser
*/
protected $isDOMDocumentCreatedWithoutHtmlWrapper = false;

/**
* An random md5-hash, generated via "random_bytes()".
*
* @var string
*/
protected $randomHash;

/**
* Constructor
*
* @param string|SimpleHtmlDom|\DOMNode $element HTML code or SimpleHtmlDom, \DOMNode
*/
public function __construct($element = null)
{
$this->randomHash = md5(Bootup::get_random_bytes(16));
$this->document = new \DOMDocument('1.0', $this->getEncoding());

$this->addRandBytesToDomReplaceHelpers();

// DOMDocument settings
$this->document->preserveWhiteSpace = true;
$this->document->formatOutput = true;
Expand All @@ -135,22 +125,6 @@ public function __construct($element = null)
}
}

/**
* Add rand-bytes to the "Dom-Replace-Helper"-variables.
*/
protected function addRandBytesToDomReplaceHelpers()
{
/** @noinspection AlterInForeachInspection */
foreach (self::$domLinkReplaceHelper['tmp'] as &$linkHelper) {
$linkHelper .= $this->randomHash;
}

/** @noinspection AlterInForeachInspection */
foreach (self::$domReplaceHelper['tmp'] as &$domHelper) {
$domHelper .= $this->randomHash;
}
}

/**
* @param $name
* @param $arguments
Expand Down Expand Up @@ -300,19 +274,19 @@ public static function replaceToPreserveHtmlEntities($html)
*/
public static function putReplacedBackToPreserveHtmlEntities($html)
{
return str_replace(
array_merge(
self::$domLinkReplaceHelper['tmp'],
self::$domReplaceHelper['tmp'],
array('
')
),
array_merge(
self::$domLinkReplaceHelper['orig'],
self::$domReplaceHelper['orig'],
array('')
),
$html
);
static $DOM_REPLACE__HELPER_CACHE = null;
if ($DOM_REPLACE__HELPER_CACHE === null) {
$DOM_REPLACE__HELPER_CACHE['tmp'] = array_merge(
self::$domLinkReplaceHelper['tmp'],
self::$domReplaceHelper['tmp']
);
$DOM_REPLACE__HELPER_CACHE['orig'] = array_merge(
self::$domLinkReplaceHelper['orig'],
self::$domReplaceHelper['orig']
);
}

return str_replace($DOM_REPLACE__HELPER_CACHE['tmp'], $DOM_REPLACE__HELPER_CACHE['orig'], $html);
}

/**
Expand Down
7 changes: 0 additions & 7 deletions src/voku/helper/SimpleHtmlDom.php
Expand Up @@ -494,11 +494,8 @@ protected function replaceChild($string)
}

if (!empty($newDocument)) {

$newDocument = $this->cleanHtmlWrapper($newDocument);

$newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true);

$this->node->appendChild($newNode);
}

Expand All @@ -522,10 +519,6 @@ protected function replaceNode($string)

$newDocument = new HtmlDomParser($string);

// DEBUG
//echo $this->normalizeStringForComparision($newDocument->outertext) . "\n";
//echo $this->normalizeStringForComparision($string) . "\n\n";

if ($this->normalizeStringForComparision($newDocument->outertext) != $this->normalizeStringForComparision($string)) {
throw new RuntimeException('Not valid HTML fragment');
}
Expand Down
10 changes: 10 additions & 0 deletions tests/HtmlDomParserTest.php
Expand Up @@ -924,6 +924,16 @@ public function testGetElementsByClass()
);
}

public function testUtf8AndBrokenHtmlEncoding()
{
$dom = new HtmlDomParser();
$dom->load('hi</b><p>سلام<div>の家庭に、9 ☆<><');
self::assertSame(
'<p>hi</p><p>سلام</p><div>の家庭に、9 ☆</div>',
$dom->innerHtml
);
}

public function testEnforceEncoding()
{
$dom = new HtmlDomParser();
Expand Down

0 comments on commit 2a5e10c

Please sign in to comment.