XmlUtility.php in Feeds extensible parsers 8
File
src/Utility/XmlUtility.php
View source
<?php
namespace Drupal\feeds_ex\Utility;
use DOMDocument;
use RuntimeException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class XmlUtility {
use StringTranslationTrait;
public function createHtmlDocument($source, $options = 0) {
$source = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n" . $source;
$document = $this
->buildDomDocument();
$options |= LIBXML_NONET;
$options |= defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0;
$options |= defined('LIBXML_PARSEHUGE') ? LIBXML_PARSEHUGE : 0;
$success = $document
->loadHTML($source, $options);
if (!$success) {
throw new RuntimeException($this
->t('There was an error parsing the HTML document.'));
}
return $document;
}
public function decodeNamedHtmlEntities($markup) {
$map = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES | ENT_HTML5, 'UTF-8'));
unset($map['&'], $map['<'], $map['>']);
return strtr($markup, $map);
}
protected function buildDomDocument() {
$document = new DOMDocument();
$document->strictErrorChecking = FALSE;
$document->resolveExternals = FALSE;
$document->substituteEntities = FALSE;
$document->recover = TRUE;
return $document;
}
}