Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewrite insert-whitespace code to be better (jamie)

Make strip mode 'literal' imply 'do not insert whitespace'
  • Loading branch information...
commit 518bf4d5c82bee5a566647900efc40aea535f232 1 parent 41bfa30
@pudge pudge authored
View
62 Slash.pm
@@ -1192,31 +1192,25 @@ sub sqlConnect {
# return \$dbh;
}
-###############################################################################
-#
-# Some Random Dave Code for HTML validation
-# (pretty much the last legacy of daveCode[tm] by demaagd@imagegroup.com
-#
-
########################################################
sub stripByMode {
my($str, $fmode, $no_white_fix) = @_;
$fmode ||= 'nohtml';
+ $no_white_fix = defined($no_white_fix) ? $no_white_fix : $fmode eq 'literal';
- # ASCII only ?
-# $str =~ s/[^\011\040\033-176]/sprintf '&#%d;', ord $1/ge;
-
+ # insert whitespace into long words, convert <>& to HTML entities
if ($fmode eq 'literal' || $fmode eq 'exttrans' || $fmode eq 'attribute' || $fmode eq 'code') {
- $str =~ s/(\S{90})/$1 /g unless $no_white_fix;
+ $str = breakHtml($str) unless $no_white_fix;
# Encode all HTML tags
$str =~ s/&/&amp;/g;
$str =~ s/</&lt;/g;
$str =~ s/>/&gt;/g;
}
- # this "if" block part of patch from Ben Tilly
+ # convert regular text to HTML-ized text, insert P, etc.
if ($fmode eq 'plaintext' || $fmode eq 'exttrans' || $fmode eq 'code') {
- $str = stripBadHtml($str, $no_white_fix);
+ $str = stripBadHtml($str);
+ $str = breakHtml($str) unless $no_white_fix;
$str =~ s/\n/<BR>/gi; # pp breaks
$str =~ s/(?:<BR>\s*){2,}<BR>/<BR><BR>/gi;
# Preserve leading indents
@@ -1224,19 +1218,22 @@ sub stripByMode {
$str =~ s/<BR>\n?( +)/"<BR>\n" . ("&nbsp; " x length($1))/ieg;
$str = '<CODE>' . $str . '</CODE>' if $fmode eq 'code';
+ # strip out all HTML
} elsif ($fmode eq 'nohtml') {
$str =~ s/<.*?>//g;
$str =~ s/<//g;
$str =~ s/>//g;
- $str =~ s/(\S{90})/$1 /g unless $no_white_fix;
+ $str = breakHtml($str) unless $no_white_fix;
+ # convert HTML attribute to allowed text (just convert ")
} elsif ($fmode eq 'attribute') {
$str =~ s/"/&#34;/g;
# $str =~ s/'/&#39;/g; # ' should be OK if we use
# " consistently
} else {
- $str = stripBadHtml($str, $no_white_fix);
+ $str = stripBadHtml($str);
+ $str = breakHtml($str) unless $no_white_fix;
}
return $str;
@@ -1244,16 +1241,45 @@ sub stripByMode {
########################################################
sub stripBadHtml {
- my($str, $no_white_fix) = @_;
+ my($str) = @_;
$str =~ s/<(?!.*?>)//gs;
$str =~ s/<(.*?)>/approveTag($1)/sge;
+ $str =~ s/> </> $1/g;
+
+ return $str;
+}
- $str =~ s/></> </g;
+########################################################
+{
+ my %is_break_tag;
- $str =~ s/([^<>\s]{90})/$1 /g unless $no_white_fix;
+ sub breakHtml {
+ my($comment, $mwl) = @_;
+ my($new, $l, $c, $in_tag, $this_tag, $cwl);
- return $str;
+ $mwl = $mwl || 80;
+ $l = length($comment);
+
+ %is_break_tag = map { uc, 1 } qw(HR BR LI P OL UL BLOCKQUOTE DIV)
+ unless scalar keys %is_break_tag;
+
+ for (my $i = 0; $i < $l; $new .= $c, ++$i) {
+ $c = substr($comment, $i, 1);
+ if ($c eq '<') { $in_tag = 1 }
+ elsif ($c eq '>') {
+ $in_tag = 0;
+ $this_tag =~ s{^/?(\S+).*}{\U$1};
+ $cwl = 0 if $is_break_tag{$this_tag};
+ $this_tag = '';
+ }
+ elsif ($in_tag) { $this_tag .= $c }
+ elsif ($c =~ /\s/) { $cwl = 0 }
+ elsif (++$cwl > $mwl) { $new .= ' '; $cwl = 1 }
+ }
+
+ return $new;
+ }
}
########################################################
View
4 plugins/Submit/submit.pl
@@ -205,7 +205,7 @@ sub previewForm {
selectTopic("tid", $tid);
selectSection("section", $I{F}{section} || $I{defaultsection});
- printf <<ADMIN, stripByMode($introtext, 'literal', 1);
+ printf <<ADMIN, stripByMode($introtext, 'literal');
<INPUT TYPE="SUBMIT" NAME="op" VALUE="preview"><BR>
<BR>Intro Copy<BR>
<TEXTAREA NAME="introtext" COLS="70" ROWS="10" WRAP="VIRTUAL">%s</TEXTAREA><BR>
@@ -532,7 +532,7 @@ sub displayForm {
print formLabel("The Scoop",
"HTML is fine, but double check those URLs and HTML tags!");
- printf <<EOT, stripByMode($I{F}{story}, 'literal', 1);
+ printf <<EOT, stripByMode($I{F}{story}, 'literal');
<TEXTAREA WRAP="VIRTUAL" COLS="70" ROWS="12" NAME="story">%s</TEXTAREA><BR>
<FONT SIZE="2">(Are you sure you included a URL? Didja test them for typos?)</FONT><P>
View
12 public_html/admin.pl
@@ -281,7 +281,7 @@ sub authorEdit {
}
for ($quote, $copy) {
- $_ = stripByMode($_, 'literal', 1);
+ $_ = stripByMode($_, 'literal');
}
print <<EOT;
@@ -483,8 +483,8 @@ sub blockEdit {
my($block, $bseclev, $type, $description) =
sqlSelect('block,seclev,type,description', 'blocks', "bid='$bid'") if $bid;
- my $description_ta = stripByMode($description, 'literal', 1);
- $block = stripByMode($block, 'literal', 1);
+ my $description_ta = stripByMode($description, 'literal');
+ $block = stripByMode($block, 'literal');
# main table
print <<EOT;
@@ -1221,8 +1221,8 @@ sub editstory {
my @extracolumns = sqlSelectColumns($S->{section})
if sqlTableExists($S->{section});
- my $introtext = stripByMode($S->{introtext}, 'literal', 1);
- my $bodytext = stripByMode($S->{bodytext}, 'literal', 1);
+ my $introtext = stripByMode($S->{introtext}, 'literal');
+ my $bodytext = stripByMode($S->{bodytext}, 'literal');
my $SECT = getSection($S->{section});
print '<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="0">';
@@ -1496,7 +1496,7 @@ sub editFilter {
"content_filters","filter_id=$filter_id");
# this has to be here - it really screws up the block editor
- $err_message = stripByMode($err_message, 'literal', 1);
+ $err_message = stripByMode($err_message, 'literal');
my $textarea = <<EOT;
<TEXTAREA NAME="err_message" COLS="50" ROWS="2">$err_message</TEXTAREA>
EOT
View
4 public_html/comments.pl
@@ -631,9 +631,7 @@ sub previewForm {
##################################################################
# Saves the Comment
sub submitComment {
- $I{F}{postersubj} = stripByMode(
- $I{F}{postersubj}, 'nohtml', $I{U}{aseclev}, ''
- );
+ $I{F}{postersubj} = stripByMode($I{F}{postersubj}, 'nohtml');
$I{F}{postercomment} = stripByMode($I{F}{postercomment}, $I{F}{posttype});
($I{F}{postercomment}, $I{F}{postersubj}) =
View
4 public_html/submit.pl
@@ -205,7 +205,7 @@ sub previewForm {
selectTopic("tid", $tid);
selectSection("section", $I{F}{section} || $I{defaultsection});
- printf <<ADMIN, stripByMode($introtext, 'literal', 1);
+ printf <<ADMIN, stripByMode($introtext, 'literal');
<INPUT TYPE="SUBMIT" NAME="op" VALUE="preview"><BR>
<BR>Intro Copy<BR>
<TEXTAREA NAME="introtext" COLS="70" ROWS="10" WRAP="VIRTUAL">%s</TEXTAREA><BR>
@@ -532,7 +532,7 @@ sub displayForm {
print formLabel("The Scoop",
"HTML is fine, but double check those URLs and HTML tags!");
- printf <<EOT, stripByMode($I{F}{story}, 'literal', 1);
+ printf <<EOT, stripByMode($I{F}{story}, 'literal');
<TEXTAREA WRAP="VIRTUAL" COLS="70" ROWS="12" NAME="story">%s</TEXTAREA><BR>
<FONT SIZE="2">(Are you sure you included a URL? Didja test them for typos?)</FONT><P>
View
12 themes/slashcode/htdocs/admin.pl
@@ -281,7 +281,7 @@ sub authorEdit {
}
for ($quote, $copy) {
- $_ = stripByMode($_, 'literal', 1);
+ $_ = stripByMode($_, 'literal');
}
print <<EOT;
@@ -483,8 +483,8 @@ sub blockEdit {
my($block, $bseclev, $type, $description) =
sqlSelect('block,seclev,type,description', 'blocks', "bid='$bid'") if $bid;
- my $description_ta = stripByMode($description, 'literal', 1);
- $block = stripByMode($block, 'literal', 1);
+ my $description_ta = stripByMode($description, 'literal');
+ $block = stripByMode($block, 'literal');
# main table
print <<EOT;
@@ -1221,8 +1221,8 @@ sub editstory {
my @extracolumns = sqlSelectColumns($S->{section})
if sqlTableExists($S->{section});
- my $introtext = stripByMode($S->{introtext}, 'literal', 1);
- my $bodytext = stripByMode($S->{bodytext}, 'literal', 1);
+ my $introtext = stripByMode($S->{introtext}, 'literal');
+ my $bodytext = stripByMode($S->{bodytext}, 'literal');
my $SECT = getSection($S->{section});
print '<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="0">';
@@ -1496,7 +1496,7 @@ sub editFilter {
"content_filters","filter_id=$filter_id");
# this has to be here - it really screws up the block editor
- $err_message = stripByMode($err_message, 'literal', 1);
+ $err_message = stripByMode($err_message, 'literal');
my $textarea = <<EOT;
<TEXTAREA NAME="err_message" COLS="50" ROWS="2">$err_message</TEXTAREA>
EOT
View
4 themes/slashcode/htdocs/comments.pl
@@ -631,9 +631,7 @@ sub previewForm {
##################################################################
# Saves the Comment
sub submitComment {
- $I{F}{postersubj} = stripByMode(
- $I{F}{postersubj}, 'nohtml', $I{U}{aseclev}, ''
- );
+ $I{F}{postersubj} = stripByMode($I{F}{postersubj}, 'nohtml');
$I{F}{postercomment} = stripByMode($I{F}{postercomment}, $I{F}{posttype});
($I{F}{postercomment}, $I{F}{postersubj}) =
View
4 themes/slashcode/htdocs/submit.pl
@@ -205,7 +205,7 @@ sub previewForm {
selectTopic("tid", $tid);
selectSection("section", $I{F}{section} || $I{defaultsection});
- printf <<ADMIN, stripByMode($introtext, 'literal', 1);
+ printf <<ADMIN, stripByMode($introtext, 'literal');
<INPUT TYPE="SUBMIT" NAME="op" VALUE="preview"><BR>
<BR>Intro Copy<BR>
<TEXTAREA NAME="introtext" COLS="70" ROWS="10" WRAP="VIRTUAL">%s</TEXTAREA><BR>
@@ -532,7 +532,7 @@ sub displayForm {
print formLabel("The Scoop",
"HTML is fine, but double check those URLs and HTML tags!");
- printf <<EOT, stripByMode($I{F}{story}, 'literal', 1);
+ printf <<EOT, stripByMode($I{F}{story}, 'literal');
<TEXTAREA WRAP="VIRTUAL" COLS="70" ROWS="12" NAME="story">%s</TEXTAREA><BR>
<FONT SIZE="2">(Are you sure you included a URL? Didja test them for typos?)</FONT><P>
Please sign in to comment.
Something went wrong with that request. Please try again.