Revised WebGUI::HTML::filter "all" so that text does not run together when tags are removed. Added additional tests to HTML.t.
This commit is contained in:
parent
b5d749f6d2
commit
3ebd2ef625
3 changed files with 59 additions and 5 deletions
|
|
@ -21,6 +21,8 @@
|
|||
doesn't work.
|
||||
- Fixed a cross-Matrix linking problem when you have two or more Matricies on
|
||||
one site with the same category names.
|
||||
- fix: Revised WebGUI::HTML::filter "all" so that text does not run together when
|
||||
tags are removed. Added additional tests to HTML.t. (Eric Kennedy)
|
||||
|
||||
|
||||
7.0.2
|
||||
|
|
|
|||
|
|
@ -102,8 +102,42 @@ sub filter {
|
|||
my $html = shift;
|
||||
my $type = shift;
|
||||
if ($type eq "all") {
|
||||
my $filter = HTML::TagFilter->new(allow=>{'none'},strip_comments=>1);
|
||||
$html = $filter->filter($html);
|
||||
#Hash used to keep track of depth within tags
|
||||
my %html_parser_inside_tag;
|
||||
#String containing text output from HTML::Parser
|
||||
my $html_parser_text = "" ;
|
||||
#Hash containing HTML tags (as keys) that create whitespace when rendered by the browser
|
||||
my %html_parser_whitespace_tags = ('p'=>1, 'br'=>1, 'hr'=>1, 'td'=>1, 'th'=>1,
|
||||
'tr'=>1, 'table'=>1, 'ul'=>1, 'li'=>1, 'div'=>1) ;
|
||||
#HTML::Parser event handler called at the start and end of each HTML tag, adds whitespace (if necessary)
|
||||
#to output if the tag creates whitespace. This was done to keep text from running together inappropriately.
|
||||
my $html_parser_tag_sub = sub {
|
||||
my($tag, $num) = @_;
|
||||
$html_parser_inside_tag{$tag} += $num;
|
||||
if ($html_parser_whitespace_tags{$tag} &&
|
||||
($html_parser_text =~ /\S$/)) { #add space only if no preceeding space
|
||||
$html_parser_text .= " " ;
|
||||
}
|
||||
} ;
|
||||
#HTML::Parser event handler called with non-tag text (no tags)
|
||||
my $html_parser_text_sub = sub {
|
||||
return if $html_parser_inside_tag{script} || $html_parser_inside_tag{style}; # do not output text
|
||||
$html_parser_text .= $_[0] ;
|
||||
} ;
|
||||
my $parser = HTML::Parser->new(api_version => 3,
|
||||
handlers => [start => [$html_parser_tag_sub,
|
||||
"tagname, '+1'"],
|
||||
end => [$html_parser_tag_sub,
|
||||
"tagname, '-1'"],
|
||||
text => [$html_parser_text_sub,
|
||||
"text"]
|
||||
],
|
||||
marked_sections => 1,
|
||||
) ;
|
||||
$parser->parse($html) ;
|
||||
$parser->eof() ;
|
||||
$html = $html_parser_text ;
|
||||
$html =~ s/ / /ixsg ;
|
||||
WebGUI::Macro::negate(\$html);
|
||||
} elsif ($type eq "javascript") {
|
||||
$html =~ s/\<script.*?\/script\>//ixsg;
|
||||
|
|
|
|||
24
t/HTML.t
24
t/HTML.t
|
|
@ -22,6 +22,24 @@ use Test::More; # increment this value for each test you create
|
|||
my $session = WebGUI::Test->session;
|
||||
|
||||
my @filterSets = (
|
||||
{
|
||||
inputText => q#<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><img src="/uploads/abc.jpg" align="bottom" border="0" /> </td><td style="padding-left: 10px"> <!--StartFragment --> <center><b>ABC DEF</b></center><br />To provide cost<b> </b>efficient products that put you in c<b>ontrol</b> of your own future. <br /><br /><center><b>Our Company</b></center><ul class="BodyText"><li><a href="http://www.abc.com" target="_blank">ABC Company</a></li></ul><div align="center">Welcome to our Intranet! </div></td></tr></tbody></table>#,
|
||||
output => q# ABC DEF To provide cost efficient products that put you in control of your own future. Our Company ABC Company Welcome to our Intranet! #,
|
||||
type => 'all',
|
||||
comment => 'all filter complex HTML',
|
||||
},
|
||||
{
|
||||
inputText => q!<tr style="height: 14px"><td style="border: medium none #d4d0c8; height: 14px; background-color: transparent" class="xl28"><b><font face="Arial" size="2">ABC</font></b></td><td style="border: medium none #d4d0c8; background-color: transparent" class="xl25"><b><font face="Arial" size="2"></font></b><br /></td><td style="border: medium none #d4d0c8; background-color: transparent" class="xl24"><font face="Arial" size="2">DEF</font></td><td style="border: medium none #d4d0c8; background-color: transparent" class="xl25"><font face="Arial" size="2"></font><br /></td><td style="border: medium none #d4d0c8; background-color: transparent" class="xl26"><b><font face="Arial" size="2">123</font></b></td></tr>!,
|
||||
output => q!ABC DEF 123 !,
|
||||
type => 'all',
|
||||
comment => 'all filter complex HTML',
|
||||
},
|
||||
{
|
||||
inputText => q!<div>Some text in a sentence.</div><div>Some more text in a sentence.</div>!,
|
||||
output => q!Some text in a sentence. Some more text in a sentence. !,
|
||||
type => 'all',
|
||||
comment => 'all filter JT',
|
||||
},
|
||||
{
|
||||
inputText => q!<p>Paragraph</p>^H();,^SQL("select * from users","^0;,^1;")!,
|
||||
output => q!<p>Paragraph</p>^H();,^SQL("select * from users","^0;,^1;")!,
|
||||
|
|
@ -36,19 +54,19 @@ my @filterSets = (
|
|||
},
|
||||
{
|
||||
inputText => q!<p>Paragraph</p>!,
|
||||
output => q!Paragraph!,
|
||||
output => q!Paragraph !,
|
||||
type => 'all',
|
||||
comment => 'all filter HTML',
|
||||
},
|
||||
{
|
||||
inputText => q!<div class="something"><div style="float: left;">This <span>is some</span> <i>text</i> here. Let's see what we get when we add a table.<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table></div><div>Here's a little more text with <b>bold</b>, <strong>strong</strong>, and <strong><b>bold strong</b></strong>.</div>!,
|
||||
output => q!This is some text here. Let's see what we get when we add a table.abcdHere's a little more text with bold, strong, and bold strong.!,
|
||||
output => q!This is some text here. Let's see what we get when we add a table. a b c d Here's a little more text with bold, strong, and bold strong. !,
|
||||
type => 'all',
|
||||
comment => 'all filter complex HTML',
|
||||
},
|
||||
{
|
||||
inputText => q!<p>Paragraph</p>^H();!,
|
||||
output => q!Paragraph^H();!,
|
||||
output => q!Paragraph ^H();!,
|
||||
type => 'all',
|
||||
comment => 'all filters macros and HTML',
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue