From a20df265424d811c71ec69d24d2f7af588811eb7 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Tue, 16 Jan 2007 21:08:59 +0000 Subject: [PATCH] Much better html rendering with css support and style inliner git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@175 9869982d-c50d-0410-be91-f2a2ec7c7c7b --- banana/banana.inc.php.in | 7 +++ banana/message.func.inc.php | 107 +++++++++++++++++++++++++++++++++++++++----- banana/page.inc.php | 10 +++++ banana/spool.inc.php | 3 ++ css/style.css | 3 ++ 5 files changed, 119 insertions(+), 11 deletions(-) diff --git a/banana/banana.inc.php.in b/banana/banana.inc.php.in index df1768b..2f7daa4 100644 --- a/banana/banana.inc.php.in +++ b/banana/banana.inc.php.in @@ -251,6 +251,13 @@ class Banana return Banana::$page->run(); } + /** Return the CSS code to include in the headers + */ + public function css() + { + return Banana::$page->css; + } + /**************************************************************************/ /* actions */ /**************************************************************************/ diff --git a/banana/message.func.inc.php b/banana/message.func.inc.php index 15da405..7dfb6a4 100644 --- a/banana/message.func.inc.php +++ b/banana/message.func.inc.php @@ -251,7 +251,65 @@ function banana_removeEvilAttributes($tagSource) $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'. 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup'; return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource)); -} +} + +function banana_cleanStyles($tag, $attributes) +{ + static $td_style, $conv, $size_conv; + if (!isset($td_style)) { + $conv = array('style' => 'style', 'width' => 'width', 'height' => 'height', 'border' => 'border-size', + 'size' => 'font-size', 'align' => 'text-align', 'valign' => 'vertical-align', 'face' => 'font', + 'bgcolor' => 'background-color', 'color' => 'color', 'style' => 'style', + 'cellpadding' => 'padding', 'cellspacing' => 'border-spacing'); + $size_conv = array(1 => 'xx-small', 2 => 'x-small', 3 => 'small', 4 => 'medium', 5 => 'large', + 6 => 'x-large', 7 => 'xx-large', + '-2' => 'xx-small', '-1' => 'x-small', '+1' => 'medium', '+2' => 'large', + '+3' => 'x-large', '+4' => 'xx-large'); + $td_style = array(); + } + if ($tag == 'table') { + array_unshift($td_style, ''); + } + if ($tag == '/table') { + array_shift($td_style); + } + if ($tag{0} == '/') { + return ''; + } + if ($tag == 'td') { + $style = $td_style[0]; + } else { + $style = ''; + } + $attributes = str_replace("\n", ' ', stripslashes($attributes)); + $attributes = str_replace('= "', '="', $attributes); + foreach ($conv as $att=>$stl) { + $pattern = '/\b' . preg_quote($att, '/') . '="(.+?)"/i'; + if (preg_match($pattern, $attributes, $matches)) { + $attributes = preg_replace($pattern, '', $attributes); + $val = $matches[1]; + if ($att == 'cellspacing' && strpos($style, 'border-collapse') === false) { + $style .= "border-collapse: separate; border-spacing: $val $val; "; + } elseif ($att == 'cellpadding' && $tag == 'table') { + $td_style[0] = "$stl: {$val}px; "; + } elseif ($att == 'style') { + $val = rtrim($val, ' ;'); + $style .= "$val; "; + } elseif ($att == 'size') { + $val = $size_conv[$val]; + $style .= "$stl: $val; "; + } elseif (is_numeric($val)) { + $style .= "$stl: {$val}px; "; + } else { + $style .= "$stl: $val; "; + } + } + } + if (!empty($style)) { + $style = 'style="' . $style . '" '; + } + return ' ' . $style . trim($attributes); +} /** * @return string @@ -260,18 +318,13 @@ function banana_removeEvilAttributes($tagSource) */ function banana_cleanHtml($source) { - $allowedTags = '