1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-03 12:47:56 +02: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:
Edward Z. Yang
2006-08-03 00:14:10 +00:00
parent 80281dda55
commit 064fd603d3
4 changed files with 135 additions and 12 deletions

View File

@@ -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();

View File

@@ -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;
}
}
?>