mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-03-11 17:18:44 +00:00
Commit TagTransform_Font and associated test-cases.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@143 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
80281dda55
commit
064fd603d3
@ -210,13 +210,7 @@ class HTMLPurifier_Definition
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// UNIMP : info_tag_transform : transformations of tags
|
||||
|
||||
// font -> span / attributes: size color face
|
||||
// css: font-size color font-family
|
||||
// menu -> ul
|
||||
// dir -> ul
|
||||
// center -> div / css: text-align: center;
|
||||
|
||||
//$this->info_tag_transform['font'] = new HTMLPurifier_TagTransform_Font();
|
||||
$this->info_tag_transform['font'] = new HTMLPurifier_TagTransform_Font();
|
||||
$this->info_tag_transform['menu'] = new HTMLPurifier_TagTransform_Simple('ul');
|
||||
$this->info_tag_transform['dir'] = new HTMLPurifier_TagTransform_Simple('ul');
|
||||
$this->info_tag_transform['center'] = new HTMLPurifier_TagTransform_Center();
|
||||
|
@ -44,7 +44,13 @@ class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform
|
||||
|
||||
class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform
|
||||
{
|
||||
var $transform_to = 'div';
|
||||
|
||||
function transform($tag) {
|
||||
if ($tag->type == 'end') {
|
||||
$new_tag = new HTMLPurifier_Token_End($this->transform_to);
|
||||
return $new_tag;
|
||||
}
|
||||
$attributes = $tag->attributes;
|
||||
$prepend_css = 'text-align:center;';
|
||||
if (isset($attributes['style'])) {
|
||||
@ -53,14 +59,11 @@ class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform
|
||||
$attributes['style'] = $prepend_css;
|
||||
}
|
||||
switch ($tag->type) {
|
||||
case 'end':
|
||||
$new_tag = new HTMLPurifier_Token_End('div');
|
||||
break;
|
||||
case 'start':
|
||||
$new_tag = new HTMLPurifier_Token_Start('div', $attributes);
|
||||
$new_tag = new HTMLPurifier_Token_Start($this->transform_to, $attributes);
|
||||
break;
|
||||
case 'empty':
|
||||
$new_tag = new HTMLPurifier_Token_Empty('div', $attributes);
|
||||
$new_tag = new HTMLPurifier_Token_Empty($this->transform_to, $attributes);
|
||||
break;
|
||||
default:
|
||||
trigger_error('Failed tag transformation', E_USER_WARNING);
|
||||
@ -70,4 +73,79 @@ class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform
|
||||
}
|
||||
}
|
||||
|
||||
class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
|
||||
{
|
||||
|
||||
var $transform_to = 'span';
|
||||
|
||||
var $_size_lookup = array(
|
||||
'1' => 'xx-small',
|
||||
'2' => 'small',
|
||||
'3' => 'medium',
|
||||
'4' => 'large',
|
||||
'5' => 'x-large',
|
||||
'6' => 'xx-large',
|
||||
'7' => '300%',
|
||||
'-1' => 'smaller',
|
||||
'+1' => 'larger',
|
||||
'-2' => '60%',
|
||||
'+2' => '150%',
|
||||
'+4' => '300%'
|
||||
);
|
||||
|
||||
function transform($tag) {
|
||||
|
||||
if ($tag->type == 'end') {
|
||||
$new_tag = new HTMLPurifier_Token_End($this->transform_to);
|
||||
return $new_tag;
|
||||
}
|
||||
|
||||
// font size lookup table based off of:
|
||||
// http://style.cleverchimp.com/font_size_intervals/altintervals.html
|
||||
$attributes = $tag->attributes;
|
||||
$prepend_style = '';
|
||||
|
||||
// handle color transform
|
||||
if (isset($attributes['color'])) {
|
||||
$prepend_style .= 'color:' . $attributes['color'] . ';';
|
||||
unset($attributes['color']);
|
||||
}
|
||||
|
||||
// handle face transform
|
||||
if (isset($attributes['face'])) {
|
||||
$prepend_style .= 'font-family:' . $attributes['face'] . ';';
|
||||
unset($attributes['face']);
|
||||
}
|
||||
|
||||
// handle size transform
|
||||
if (isset($attributes['size'])) {
|
||||
if (isset($this->_size_lookup[$attributes['size']])) {
|
||||
$prepend_style .= 'font-size:' .
|
||||
$this->_size_lookup[$attributes['size']] . ';';
|
||||
}
|
||||
unset($attributes['size']);
|
||||
}
|
||||
|
||||
if ($prepend_style) {
|
||||
$attributes['style'] = isset($attributes['style']) ?
|
||||
$prepend_style . $attributes['style'] :
|
||||
$prepend_style;
|
||||
}
|
||||
|
||||
switch ($tag->type) {
|
||||
case 'start':
|
||||
$new_tag = new HTMLPurifier_Token_Start($this->transform_to, $attributes);
|
||||
break;
|
||||
case 'empty':
|
||||
$new_tag = new HTMLPurifier_Token_Empty($this->transform_to, $attributes);
|
||||
break;
|
||||
default:
|
||||
trigger_error('Failed tag transformation', E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
return $new_tag;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -32,6 +32,10 @@ class HTMLPurifier_Strategy_RemoveForeignElementsTest
|
||||
$inputs[4] = '<center>Look I am Centered!</center>';
|
||||
$expect[4] = '<div style="text-align:center;">Look I am Centered!</div>';
|
||||
|
||||
// test font transform
|
||||
$inputs[5] = '<font color="red" face="Arial" size="6">Big Warning!</font>';
|
||||
$expect[5] = '<span style="color:red;font-family:Arial;font-size:xx-large;">Big Warning!</span>';
|
||||
|
||||
$this->assertStrategyWorks($strategy, $inputs, $expect);
|
||||
}
|
||||
|
||||
|
@ -81,6 +81,53 @@ class HTMLPurifier_TagTransformTest extends UnitTestCase
|
||||
|
||||
}
|
||||
|
||||
function assertSizeToStyle($transformer, $size, $style) {
|
||||
$this->assertTransformation(
|
||||
$transformer,
|
||||
'font', array('size' => $size),
|
||||
'span', array('style' => 'font-size:' . $style . ';')
|
||||
);
|
||||
}
|
||||
|
||||
function testFont() {
|
||||
|
||||
$transformer = new HTMLPurifier_TagTransform_Font();
|
||||
|
||||
// test a font-face transformation
|
||||
$this->assertTransformation(
|
||||
$transformer,
|
||||
'font', array('face' => 'Arial'),
|
||||
'span', array('style' => 'font-family:Arial;')
|
||||
);
|
||||
|
||||
// test a color transformation
|
||||
$this->assertTransformation(
|
||||
$transformer,
|
||||
'font', array('color' => 'red'),
|
||||
'span', array('style' => 'color:red;')
|
||||
);
|
||||
|
||||
// test the size transforms
|
||||
$this->assertSizeToStyle($transformer, '1', 'xx-small');
|
||||
$this->assertSizeToStyle($transformer, '2', 'small');
|
||||
$this->assertSizeToStyle($transformer, '3', 'medium');
|
||||
$this->assertSizeToStyle($transformer, '4', 'large');
|
||||
$this->assertSizeToStyle($transformer, '5', 'x-large');
|
||||
$this->assertSizeToStyle($transformer, '6', 'xx-large');
|
||||
$this->assertSizeToStyle($transformer, '7', '300%');
|
||||
$this->assertSizeToStyle($transformer, '-1', 'smaller');
|
||||
$this->assertSizeToStyle($transformer, '+1', 'larger');
|
||||
$this->assertSizeToStyle($transformer, '-2', '60%');
|
||||
$this->assertSizeToStyle($transformer, '+2', '150%');
|
||||
$this->assertSizeToStyle($transformer, '+4', '300%');
|
||||
|
||||
// test multiple transforms, the alphabetical ordering is important
|
||||
$this->assertTransformation(
|
||||
$transformer,
|
||||
'font', array('color' => 'red', 'face' => 'Arial', 'size' => '6'),
|
||||
'span', array('style' => 'color:red;font-family:Arial;font-size:xx-large;')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user