diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 1501eab43..183fb4161 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -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 diff --git a/lib/WebGUI/HTML.pm b/lib/WebGUI/HTML.pm index 40835f758..046cacb59 100644 --- a/lib/WebGUI/HTML.pm +++ b/lib/WebGUI/HTML.pm @@ -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/\//ixsg; diff --git a/t/HTML.t b/t/HTML.t index 3b6fd5255..cbdd66d81 100644 --- a/t/HTML.t +++ b/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#
  
ABC DEF

To provide cost efficient products that put you in control of your own future.

Our Company
Welcome to our Intranet!
#, + 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!ABC
DEF
123!, + output => q!ABC DEF 123 !, + type => 'all', + comment => 'all filter complex HTML', + }, + { + inputText => q!
Some text in a sentence.
Some more text in a sentence.
!, + output => q!Some text in a sentence. Some more text in a sentence. !, + type => 'all', + comment => 'all filter JT', + }, { inputText => q!

Paragraph

^H();,^SQL("select * from users","^0;,^1;")!, output => q!

Paragraph

^H();,^SQL("select * from users","^0;,^1;")!, @@ -36,19 +54,19 @@ my @filterSets = ( }, { inputText => q!

Paragraph

!, - output => q!Paragraph!, + output => q!Paragraph !, type => 'all', comment => 'all filter HTML', }, { inputText => q!
This is some text here. Let's see what we get when we add a table.
ab
cd
Here'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.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!

Paragraph

^H();!, - output => q!Paragraph^H();!, + output => q!Paragraph ^H();!, type => 'all', comment => 'all filters macros and HTML', },