<commit_before><?php
/*
 * White HTML Filter
 * https://github.com/lincanbin/White-HTML-Filter
 *
 * Copyright 2017 <NAME> (<EMAIL>)
 * http://www.94cb.com/
 *
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * A php-based HTML tag and attribute whitelist filter.
 */



/**
 * @property WhiteHTMLFilterConfig config
 * @property DOMDocument dom
 */
class WhiteHTMLFilter
{
	public $config;
	public $removedTags;
	private $dom = NULL;
	private $TEMP_CONTENT;
	private $PARENT_TAG_NAME;

	/**
	 * The empty elements in HTML
	 * https://developer.mozilla.org/en-US/docs/Glossary/Empty_element
	 */
	protected $emptyElementList = array(
		'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', //'keygen',
		'link', 'meta', 'param', 'source', 'track', 'wbr'
	);

	/**
	 * WhiteHTMLFilter constructor.
	 * @throws Exception
	 */
	public function __construct()
	{
		if (extension_loaded("dom") === false) {
			throw new Exception('DOM extension is required. http://php.net/manual/en/dom.installation.php');
		}
		$this->config = new WhiteHTMLFilterConfig();

		if (!$this->dom) {
			$this->dom = new DOMDocument('1.0', 'UTF-8');
		}
		$this->dom->preserveWhiteSpace = true;
		$this->dom->formatOutput = false;
		$this->TEMP_CONTENT = 'a7c598c8-fcb7-4bde-af9c-91c6515fbf7a-lincanbin-' . md5(mt_rand());
		$this->PARENT_TAG_NAME = substr('tag' . md5(mt_rand()), 0, 8);
		//Disable libxml errors
		libxml_use_internal_errors(true);
	}

	/**
	 * Get current tag whitelist
	 * @return array
	 */
	public function getWhiteListTags()
	{
		return ($this->config->WhiteListTag);
	}

	/**
	 * Load document markup into the class for cleaning
	 * @param string $html The markup to clean
	 * @return bool
	 */
	public function loadHTML($html)
	{
		$html = str_replace(chr(13), '', $html);
		$html = '<?xml version="1.0" encoding="utf-8" ?><' . $this->PARENT_TAG_NAME . '>' . $html . '</' . $this->PARENT_TAG_NAME . '>';
		if (version_compare(PHP_VERSION, '5.4.0') < 0) {
			return $this->dom->loadHTML($html);
		} else {
			return $this->dom->loadHTML($html, LIBXML_HTML_NODEFDTD);
		}

	}

	/**
	 * Output the result
	 * @return string HTML string
	 */
	public function outputHtml()
	{
		$result = '';
		if (!is_null($this->dom)) {
			//SaveXML : <br/><img/>
			//SaveHTML: <br><img>
			$result = trim($this->dom->saveXML($this->getRealElement()));
			$result = str_replace($this->TEMP_CONTENT, '', $result);<commit_msg>Enhance compatibility.<commit_after><?php
/*
 * White HTML Filter
 * https://github.com/lincanbin/White-HTML-Filter
 *
 * Copyright 2017 <NAME> (<EMAIL>)
 * http://www.94cb.com/
 *
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * A php-based HTML tag and attribute whitelist filter.
 */



/**
 * @property WhiteHTMLFilterConfig config
 * @property DOMDocument dom
 */
class WhiteHTMLFilter
{
	public $config;
	public $removedTags;
	private $dom = NULL;
	private $TEMP_CONTENT;
	private $PARENT_TAG_NAME;

	/**
	 * The empty elements in HTML
	 * https://developer.mozilla.org/en-US/docs/Glossary/Empty_element
	 */
	protected $emptyElementList = array(
		'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', //'keygen',
		'link', 'meta', 'param', 'source', 'track', 'wbr'
	);

	/**
	 * WhiteHTMLFilter constructor.
	 * @throws Exception
	 */
	public function __construct()
	{
		if (extension_loaded("dom") === false) {
			throw new Exception('DOM extension is required. http://php.net/manual/en/dom.installation.php');
		}
		$this->config = new WhiteHTMLFilterConfig();

		if (!$this->dom) {
			$this->dom = new DOMDocument('1.0', 'UTF-8');
		}
		$this->dom->preserveWhiteSpace = true;
		$this->dom->formatOutput = false;
		$this->TEMP_CONTENT = 'a7c598c8-fcb7-4bde-af9c-91c6515fbf7a-lincanbin-' . md5(mt_rand());
		$this->PARENT_TAG_NAME = substr('tag' . md5(mt_rand()), 0, 8);
		//Disable libxml errors
		libxml_use_internal_errors(true);
	}

	/**
	 * Get current tag whitelist
	 * @return array
	 */
	public function getWhiteListTags()
	{
		return ($this->config->WhiteListTag);
	}

	/**
	 * Load document markup into the class for cleaning
	 * @param string $html The markup to clean
	 * @return bool
	 */
	public function loadHTML($html)
	{
		$html = str_replace(chr(13), '', $html);
		$html = '<?xml version="1.0" encoding="utf-8" ?><' . $this->PARENT_TAG_NAME . '>' . $html . '</' . $this->PARENT_TAG_NAME . '>';
		if (defined('LIBXML_HTML_NODEFDTD')) {
			return $this->dom->loadHTML($html, LIBXML_HTML_NODEFDTD);
		} else {
			return $this->dom->loadHTML($html);
		}

	}

	/**
	 * Output the result
	 * @return string HTML string
	 */
	public function outputHtml()
	{
		$result = '';
		if (!is_null($this->dom)) {
			//SaveXML : <br/><img/>
			//SaveHTML: <br><img>
			$result = trim($this->dom->saveXML($this->getRealElement()));
			$result = str_replace($this->TEMP_CONTENT, '', $result);
{'fraction_non_alphanumeric': 0.18471855313960578, 'fraction_numerical': 0.015850436903068484, 'mean_word_length': 3.71455938697318, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 2, '>': 65, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4092564', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1726, 'n_words': 366}