fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-05-16 12:55 pm

[dw-free] html modifying cut displays as text when ajax expander used

[commit: http://hg.dwscoalition.org/dw-free/rev/05266368d9d8]

http://bugs.dwscoalition.org/show_bug.cgi?id=2698

Silently ignore mismatched ending tags on display to avoid clutter.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/LJ/CleanHTML.pm
  • t/clean-event.t
--------------------------------------------------------------------------------
diff -r ff0b65563611 -r 05266368d9d8 cgi-bin/LJ/CleanHTML.pm
--- a/cgi-bin/LJ/CleanHTML.pm	Mon May 16 18:21:29 2011 +0800
+++ b/cgi-bin/LJ/CleanHTML.pm	Mon May 16 20:54:52 2011 +0800
@@ -1071,8 +1071,12 @@
                             $newdata .= "</$tag>";
                             $opencount{$tag}--;
                         }
+                    } elsif ( ! $allow || $form_tag->{$tag} && ! $opencount{form}) {
+                        # tag wasn't allowed, or we have an out of scope form tag? display it then
+                        $newdata .= "&lt;/$tag&gt;";
                     } else {
-                        $newdata .= "&lt;/$tag&gt;";
+                        # mismatched or not nested properly, just keep quiet and let it go
+                        # we'll have corrected elsewhere if possible
                     }
                 }
 
diff -r ff0b65563611 -r 05266368d9d8 t/clean-event.t
--- a/t/clean-event.t	Mon May 16 18:21:29 2011 +0800
+++ b/t/clean-event.t	Mon May 16 20:54:52 2011 +0800
@@ -52,9 +52,9 @@
 # in this case, we consider the <span> within the div as unclosed
 # and the closing </span> as extra/unrelated.
 # Therefore, we close the opening tag (which needs to be closed)
-# and escape the closing tag (which has no opening tag)
+# and ignore the remaining closing </span> tag (which has no opening tag)
 $orig_post  = qq{<div><span></div></span>};
-$clean_post = qq{<div><span></span></div>&lt;/span&gt;};
+$clean_post = qq{<div><span></span></div>};
 $clean->();
 is( $orig_post, $clean_post, "Wrong closing tag order" );
 
@@ -198,4 +198,47 @@
     is( $orig_post, $clean_post, "blink tag allowed" );
 
 }
+
+note( "mismatched and misnested tags" );
+{
+    # form tags not in a form should be displayed
+    my $form_inner = qq{<select><option>hello</option><option>bye</option></select>};
+    $orig_post = qq{<form>$form_inner</form>};
+    $clean_post = qq{<form>$form_inner</form>};
+    $clean->();
+    is( $orig_post, $clean_post, "form tags within a form are allowed" );
+
+    $orig_post = $form_inner;
+    $clean_post = qq{&lt;select ... &gt;&lt;option ... &gt;hello&lt;/option&gt;&lt;option ... &gt;bye&lt;/option&gt;&lt;/select&gt;};
+    $clean->();
+    is( $orig_post, $clean_post, "form tags outside a form are escaped and displayed" );
+
+    my $table_inner = qq{<tr><td>hello</td><td>bye</td></tr>};
+    $orig_post  = qq{<table>$table_inner</table>};
+    $clean_post = qq{<table>$table_inner</table>};
+    $clean->();
+    is( $orig_post, $clean_post, "table tags within a table are allowed" );
+
+    $orig_post = $table_inner;
+    $clean_post = qq{&lt;tr&gt;&lt;td&gt;hello&lt;/td&gt;&lt;td&gt;bye&lt;/td&gt;&lt;/tr&gt;};
+    $clean->();
+    is( $orig_post, $clean_post, "table tags outside a table are escaped and displayed" );
+
+    $orig_post = qq{strong</strong> not <em><b>strong</em></b>};
+    $clean_post = qq{strong not <em><b>strong</b></em>};
+    $clean->();
+    is( $orig_post, $clean_post, "mismatched closing tags or misnested closing tags shouldn't be displayed" );
+
+    $entry_text = qq{before <strong><cut text="cut">in strong</strong>out strong</cut>after};
+
+    $orig_post = $entry_text;
+    $cut_text = qq{in strongout strong};
+    $clean->( { cut_retrieve => 1 } );
+    is( $orig_post, $cut_text, "Text under cut with mismatched HTML tags within and with-out the cut (ignored)" );
+
+    $orig_post = $entry_text;
+    $clean_post = qq{before <strong><a name="cutid1"></a>in strong</strong>out strongafter};
+    $clean->();
+    is ( $orig_post, $clean_post, "Full text of entry, with mismatched HTML tags within and with-out the cut" );
+}
 1;
--------------------------------------------------------------------------------