Skip to content

Commit

Permalink
Script related fixes and optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Sep 6, 2019
1 parent a56766e commit 9a60716
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/HTML5DOMDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,30 @@ public function loadHTML($source, $options = 0)
}

$source = trim($source);

// Add CDATA around script tags content
$matches = null;
preg_match_all('/<script(.*?)>/', $source, $matches);
if (isset($matches[0])) {
$matches[0] = array_unique($matches[0]);
foreach ($matches[0] as $match) {
if (substr($match, -2, 1) !== '/') { // check if ends with />
$source = str_replace($match, $match . '<![CDATA[html5-dom-document-internal-cdata', $source);
}
}
}
$source = str_replace('</script>', 'html5-dom-document-internal-cdata]]></script>', $source);
$source = preg_replace('/(\<!\[CDATA\[html5-dom-document-internal-cdata.*?)\<\/(.*?html5-dom-document-internal-cdata\]\]>)/s', '$1<html5-dom-document-internal-cdata-endtagfix/$2', $source);
$source = str_replace('<![CDATA[html5-dom-document-internal-cdatahtml5-dom-document-internal-cdata]]>', '', $source); // clean empty script tags
$matches = null;
preg_match_all('/\<!\[CDATA\[html5-dom-document-internal-cdata.*?html5-dom-document-internal-cdata\]\]>/s', $source, $matches);
if (isset($matches[0])) {
$matches[0] = array_unique($matches[0]);
foreach ($matches[0] as $match) {
if (strpos($match, '</') !== false) { // check if contains </
$source = str_replace($match, str_replace('</', '<html5-dom-document-internal-cdata-endtagfix/', $match), $source);
}
}
}

$autoAddHtmlAndBodyTags = !defined('LIBXML_HTML_NOIMPLIED') || ($options & LIBXML_HTML_NOIMPLIED) === 0;
$autoAddDoctype = !defined('LIBXML_HTML_NODEFDTD') || ($options & LIBXML_HTML_NODEFDTD) === 0;
Expand Down
9 changes: 5 additions & 4 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1324,14 +1324,15 @@ public function testSpecialCharsInScriptTags()
$js2 = 'var f2=function(t){
return t.replace(/</g,"&lt;").replace(/>/g,"&gt;");
};';
$content = '<html><head><script src="url"/><script type="text/javascript">' . $js1 . '</script><script>' . $js2 . '</script></head></html>';
$content = '<html><head><script src="url1"/><script src="url2"></script><script type="text/javascript">' . $js1 . '</script><script>' . $js2 . '</script></head></html>';
$dom = new HTML5DOMDocument();
$dom->loadHTML($content);
$scripts = $dom->querySelectorAll('script');
$this->assertEquals($scripts[0]->innerHTML, '');
$this->assertEquals($scripts[1]->innerHTML, $js1);
$this->assertEquals($scripts[2]->innerHTML, $js2);
$this->assertEquals($dom->saveHTML(), "<!DOCTYPE html>\n" . str_replace('<script src="url"/>', '<script src="url"></script>', $content));
$this->assertEquals($scripts[1]->innerHTML, '');
$this->assertEquals($scripts[2]->innerHTML, $js1);
$this->assertEquals($scripts[3]->innerHTML, $js2);
$this->assertEquals($dom->saveHTML(), "<!DOCTYPE html>\n" . str_replace('<script src="url1"/>', '<script src="url1"></script>', $content));
}

/**
Expand Down

0 comments on commit 9a60716

Please sign in to comment.