0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-18 18:25:18 +00:00

Rename ExtractStyleBlocks configuration parameters.

Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
Edward Z. Yang 2009-05-25 18:26:37 -04:00
parent e194b8efc6
commit bfbe29d5a1
10 changed files with 119 additions and 22 deletions

7
NEWS
View File

@ -15,6 +15,11 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
had both namespace and directive now have a single unified key.
# Some configuration directives were renamed, specifically:
%AutoFormatParam.PurifierLinkifyDocURL -> %AutoFormat.PurifierLinkify.DocURL
%FilterParam.ExtractStyleBlocksEscaping -> %Filter.ExtractStyleBlocks.Escaping
%FilterParam.ExtractStyleBlocksScope -> %Filter.ExtractStyleBlocks.Scope
%FilterParam.ExtractStyleBlocksTidyImpl -> %Filter.ExtractStyleBlocks.TidyImpl
As usual, the old directive names will still work, but will through E_NOTICE
errors.
! More robust support for name="" and id=""
! HTMLPurifier_Config::inherit($config) allows you to inherit one
configuration, and have changes to that configuration be propagated
@ -26,6 +31,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
tags that contain non-breaking spaces as well other whitespace. You
can also modify which tags should have &nbsp; maintained with
%AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions.
. Created script maintenance/rename-config.php for renaming a configuration
directive while maintaining its alias. This script does not change source code.
3.3.0, released 2009-02-16
! Implement CSS property 'overflow' when %CSS.AllowTricky is true.

View File

@ -152,6 +152,7 @@ class HTMLPurifier
$filters = array();
foreach ($filter_flags as $filter => $flag) {
if (!$flag) continue;
if (strpos($filter, '.') !== false) continue;
$class = "HTMLPurifier_Filter_$filter";
$filters[] = new $class;
}

View File

@ -13,7 +13,6 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
}
public static function buildFromDirectory($dir = null) {
$parser = new HTMLPurifier_StringHashParser();
$builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
$interchange = new HTMLPurifier_ConfigSchema_Interchange();
@ -33,15 +32,20 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
sort($files);
foreach ($files as $file) {
$builder->build(
$interchange,
new HTMLPurifier_StringHash( $parser->parseFile($dir . $file) )
);
$builder->buildFile($interchange, $dir . $file);
}
return $interchange;
}
public function buildFile($interchange, $file) {
$parser = new HTMLPurifier_StringHashParser();
$this->build(
$interchange,
new HTMLPurifier_StringHash( $parser->parseFile($dir . $file) )
);
}
/**
* Builds an interchange object based on a hash.
* @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build

View File

@ -1,8 +1,8 @@
FilterParam.ExtractStyleBlocksEscaping
Filter.ExtractStyleBlocks.Escaping
TYPE: bool
VERSION: 3.0.0
DEFAULT: true
ALIASES: Filter.ExtractStyleBlocksEscaping
ALIASES: Filter.ExtractStyleBlocksEscaping, FilterParam.ExtractStyleBlocksEscaping
--DESCRIPTION--
<p>

View File

@ -1,8 +1,8 @@
FilterParam.ExtractStyleBlocksScope
Filter.ExtractStyleBlocks.Scope
TYPE: string/null
VERSION: 3.0.0
DEFAULT: NULL
ALIASES: Filter.ExtractStyleBlocksScope
ALIASES: Filter.ExtractStyleBlocksScope, FilterParam.ExtractStyleBlocksScope
--DESCRIPTION--
<p>

View File

@ -1,7 +1,8 @@
FilterParam.ExtractStyleBlocksTidyImpl
Filter.ExtractStyleBlocks.TidyImpl
TYPE: mixed/null
VERSION: 3.1.0
DEFAULT: NULL
ALIASES: FilterParam.ExtractStyleBlocksTidyImpl
--DESCRIPTION--
<p>
If left NULL, HTML Purifier will attempt to instantiate a <code>csstidy</code>

View File

@ -38,7 +38,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocks extends HTMLPurifier_Filter
* @todo Extend to indicate non-text/css style blocks
*/
public function preFilter($html, $config, $context) {
$tidy = $config->get('FilterParam.ExtractStyleBlocksTidyImpl');
$tidy = $config->get('Filter.ExtractStyleBlocks.TidyImpl');
if ($tidy !== null) $this->_tidy = $tidy;
$html = preg_replace_callback('#<style(?:\s.*)?>(.+)</style>#isU', array($this, 'styleCallback'), $html);
$style_blocks = $this->_styleMatches;
@ -62,7 +62,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocks extends HTMLPurifier_Filter
*/
public function cleanCSS($css, $config, $context) {
// prepare scope
$scope = $config->get('FilterParam.ExtractStyleBlocksScope');
$scope = $config->get('Filter.ExtractStyleBlocks.Scope');
if ($scope !== null) {
$scopes = array_map('trim', explode(',', $scope));
} else {
@ -120,7 +120,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocks extends HTMLPurifier_Filter
$css = $this->_tidy->print->plain();
// we are going to escape any special characters <>& to ensure
// that no funny business occurs (i.e. </style> in a font-family prop).
if ($config->get('FilterParam.ExtractStyleBlocksEscaping')) {
if ($config->get('Filter.ExtractStyleBlocks.Escaping')) {
$css = str_replace(
array('<', '>', '&'),
array('\3C ', '\3E ', '\26 '),

View File

@ -0,0 +1,84 @@
#!/usr/bin/php
<?php
chdir(dirname(__FILE__));
require_once 'common.php';
require_once '../library/HTMLPurifier.auto.php';
assertCli();
/**
* @file
* Renames a configuration directive. This involves renaming the file,
* adding an alias, and then regenerating the cache. You still have to
* manually go through and fix any calls to the directive.
* @warning This script doesn't handle multi-stringhash files.
*/
$argv = $_SERVER['argv'];
if (count($argv) < 3) {
echo "Usage: {$argv[0]} OldName NewName\n";
exit(1);
}
chdir('../library/HTMLPurifier/ConfigSchema/schema');
$old = $argv[1];
$new = $argv[2];
if (!file_exists("$old.txt")) {
echo "Cannot move undefined configuration directive $old\n";
exit(1);
}
if ($old === $new) {
echo "Attempting to move to self, aborting\n";
exit(1);
}
if (file_exists("$new.txt")) {
echo "Cannot move to already defined directive $new\n";
exit(1);
}
$file = "$old.txt";
$builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
$interchange = new HTMLPurifier_ConfigSchema_Interchange();
$builder->buildFile($interchange, $file);
$contents = file_get_contents($file);
if (strpos($contents, "\r\n") !== false) {
$nl = "\r\n";
} elseif (strpos($contents, "\r") !== false) {
$nl = "\r";
} else {
$nl = "\n";
}
// replace name with new name
$contents = str_replace($old, $new, $contents);
if ($interchange->directives[$old]->aliases) {
$pos_alias = strpos($contents, 'ALIASES:');
$pos_ins = strpos($contents, $nl, $pos_alias);
if ($pos_ins === false) $pos_ins = strlen($contents);
$contents =
substr($contents, 0, $pos_ins) . ", $old" . substr($contents, $pos_ins);
file_put_contents($file, $contents);
} else {
$lines = explode($nl, $contents);
$insert = false;
foreach ($lines as $n => $line) {
if (strncmp($line, '--', 2) === 0) {
$insert = $n;
break;
}
}
if (!$insert) {
$lines[] = "ALIASES: $old";
} else {
array_splice($lines, $insert, 0, "ALIASES: $old");
}
file_put_contents($file, implode($nl, $lines));
}
rename("$old.txt", "$new.txt") || exit(1);

View File

@ -23,7 +23,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
function assertExtractStyleBlocks($html, $expect = true, $styles = array()) {
$filter = new HTMLPurifier_Filter_ExtractStyleBlocks(); // disable cleaning
if ($expect === true) $expect = $html;
$this->config->set('FilterParam.ExtractStyleBlocksTidyImpl', false);
$this->config->set('Filter.ExtractStyleBlocks.TidyImpl', false);
$result = $filter->preFilter($html, $this->config, $this->context);
$this->assertIdentical($result, $expect);
$this->assertIdentical($this->context->get('StyleBlocks'), $styles);
@ -104,14 +104,14 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
}
function test_cleanCSS_noEscapeCodes() {
$this->config->set('FilterParam.ExtractStyleBlocksEscaping', false);
$this->config->set('Filter.ExtractStyleBlocks.Escaping', false);
$this->assertCleanCSS(
".class {\nfont-family:'</style>';\n}"
);
}
function test_cleanCSS_scope() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', '#foo');
$this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
$this->assertCleanCSS(
"p {\ntext-indent:1em;\n}",
"#foo p {\ntext-indent:1em;\n}"
@ -119,7 +119,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
}
function test_cleanCSS_scopeWithSelectorCommas() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', '#foo');
$this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
$this->assertCleanCSS(
"b, i {\ntext-decoration:underline;\n}",
"#foo b, #foo i {\ntext-decoration:underline;\n}"
@ -127,17 +127,17 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
}
function test_cleanCSS_scopeWithNaughtySelector() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', '#foo');
$this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
$this->assertCleanCSS(" + p {\ntext-indent:1em;\n}", '');
}
function test_cleanCSS_scopeWithMultipleNaughtySelectors() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', '#foo');
$this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
$this->assertCleanCSS(" ++ ++ p {\ntext-indent:1em;\n}", '');
}
function test_cleanCSS_scopeWithCommas() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', '#foo, .bar');
$this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo, .bar');
$this->assertCleanCSS(
"p {\ntext-indent:1em;\n}",
"#foo p, .bar p {\ntext-indent:1em;\n}"
@ -145,7 +145,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
}
function test_cleanCSS_scopeAllWithCommas() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', '#foo, .bar');
$this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo, .bar');
$this->assertCleanCSS(
"p, div {\ntext-indent:1em;\n}",
"#foo p, #foo div, .bar p, .bar div {\ntext-indent:1em;\n}"
@ -153,7 +153,7 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
}
function test_cleanCSS_scopeWithConflicts() {
$this->config->set('FilterParam.ExtractStyleBlocksScope', 'p');
$this->config->set('Filter.ExtractStyleBlocks.Scope', 'p');
$this->assertCleanCSS(
"div {
text-align:right;