Skip to content

Commit

Permalink
Prevent removal of HTML5 definitions when setting HTML.* keys (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
xemlock authored Mar 4, 2018
1 parent fa86587 commit a823948
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
53 changes: 31 additions & 22 deletions library/HTMLPurifier/HTML5Config.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
<?php

class HTMLPurifier_HTML5Config
class HTMLPurifier_HTML5Config extends HTMLPurifier_Config
{
const REVISION = 0x000106;

/**
* @param string|array|HTMLPurifier_Config $config
* @param HTMLPurifier_ConfigSchema $schema
* @return HTMLPurifier_Config
*/
public static function create($config, HTMLPurifier_ConfigSchema $schema = null)
public static function create($config, $schema = null)
{
if ($config instanceof HTMLPurifier_Config) {
$schema = $config->def;
$config = null;
}

if (!$schema instanceof HTMLPurifier_ConfigSchema) {
$schema = HTMLPurifier_ConfigSchema::instance();
}

if ($config instanceof HTMLPurifier_Config) {
$configObj = $config;
$configObj = new self($schema);
$configObj->set('Core.Encoding', 'UTF-8');
$configObj->set('HTML.Doctype', 'HTML 4.01 Transitional');

} else {
$configObj = new HTMLPurifier_Config($schema);
$configObj->set('Core.Encoding', 'UTF-8');
$configObj->set('HTML.Doctype', 'HTML 4.01 Transitional');
$configObj->set('HTML.DefinitionID', __CLASS__);
$configObj->set('HTML.DefinitionRev', self::REVISION);

if (is_string($config)) {
$configObj->loadIni($config);
if (is_string($config)) {
$configObj->loadIni($config);

} elseif (is_array($config)) {
$configObj->loadArray($config);
}
} elseif (is_array($config)) {
$configObj->loadArray($config);
}

// Prevent auto-finalization of config when retrieving HTML definition
$autoFinalize = $configObj->autoFinalize;
$configObj->autoFinalize = false;

$def = $configObj->getHTMLDefinition(true);
HTMLPurifier_HTML5Definition::setup($def);

$configObj->autoFinalize = $autoFinalize;

return $configObj;
}

Expand All @@ -52,4 +48,17 @@ public static function createDefault()
$config = self::create(null, $schema);
return $config;
}

public function getDefinition($type, $raw = false, $optimized = false)
{
// Setting HTML.* keys removes any previously instantiated HTML
// definition object, so set up HTML5 definition as late as possible
$needSetup = $type === 'HTML' && !isset($this->definitions[$type]);
if ($needSetup) {
if ($def = parent::getDefinition($type, true, true)) {
HTMLPurifier_HTML5Definition::setup($def);
}
}
return parent::getDefinition($type, $raw, $optimized);
}
}
2 changes: 1 addition & 1 deletion tests/HTMLPurifier/HTML5ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function testCreate()
$this->assertInstanceOf('HTMLPurifier_Config', $config);

$config2 = HTMLPurifier_HTML5Config::create($config);
$this->assertSame($config, $config2);
$this->assertSame($config->def, $config2->def);

$config3 = HTMLPurifier_HTML5Config::create(array(
'Core.Encoding' => 'iso-8859-1',
Expand Down

0 comments on commit a823948

Please sign in to comment.