This is an automated email from the ASF dual-hosted git repository.

snoopdave pushed a commit to branch roller-6.1.x
in repository https://gitbox.apache.org/repos/asf/roller.git


The following commit(s) were added to refs/heads/roller-6.1.x by this push:
     new a8d5ece82 Drop an unclosed tag in removeHTML and print the weblog 
description as text on the main menu (#228)
a8d5ece82 is described below

commit a8d5ece82591c2c62404534f1543df3f8bcf943f
Author: David M. Johnson <[email protected]>
AuthorDate: Thu Oct 29 17:19:39 2026 -0400

    Drop an unclosed tag in removeHTML and print the weblog description as text 
on the main menu (#228)
    
    * Drop an unclosed tag in removeHTML and print the weblog description as 
text on the main menu
    
    * Keep scanning after a less-than sign that is text in removeHTML; note the 
change in CHANGES.md
    
    * Keep text such as x<y in removeHTML; drop only unclosed tags with 
attributes
    
    Claude-Session: https://claude.ai/code/session_01C5X1Z1eoSH7o25gZskPGzb
---
 CHANGES.md                                         |  6 +++
 .../apache/roller/weblogger/util/Utilities.java    | 59 +++++++++++++++++++---
 app/src/main/webapp/WEB-INF/jsps/core/MainMenu.jsp |  2 +-
 .../roller/weblogger/util/UtilitiesTest.java       | 37 ++++++++++++++
 4 files changed, 95 insertions(+), 9 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 7fc107bd1..c0070846e 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -58,6 +58,12 @@
 - **Startup fails if the XML-RPC parser cannot be configured.** Roller used to
   log an error and continue. It now stops at startup, so check the log if a
   custom XML parser is on the classpath.
+- **Text with an unclosed tag loses the tag.** Where Roller strips HTML from
+  plain-text fields (for example a weblog's description), a tag with attributes
+  and no closing `>`, such as `text <img src=x`, is now dropped. Other text 
with
+  a `<`, as in `a < b`, `x<y` or `C<C++ fans`, is kept, and no longer gets a
+  space added before the `<`. The Your Weblogs page shows a weblog's
+  description as text.
 
 ### Bug fixes
 
diff --git a/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java 
b/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
index f8a74602c..643c7834c 100644
--- a/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
+++ b/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
@@ -170,23 +170,32 @@ public class Utilities {
         while (beginTag >= start) {
             if (beginTag > 0) {
                 ret.append(str.substring(start, beginTag));
-
-                // replace each tag with a space (looks better)
-                if (addSpace) {
-                    ret.append(" ");
-                }
             }
             endTag = str.indexOf('>', beginTag);
 
             // if endTag found move "cursor" forward
             if (endTag > -1) {
+                // replace each tag with a space (looks better)
+                if (addSpace && beginTag > 0) {
+                    ret.append(" ");
+                }
                 start = endTag + 1;
                 beginTag = str.indexOf('<', start);
             }
-            // if no endTag found, get rest of str and break
+            // if no endTag found, drop the rest if this '<' starts a tag,
+            // which a browser would still parse as markup; otherwise keep the
+            // '<' as text and look at the next one
             else {
-                ret.append(str.substring(beginTag));
-                break;
+                if (startsTag(str, beginTag)) {
+                    break;
+                }
+                ret.append('<');
+                start = beginTag + 1;
+                beginTag = str.indexOf('<', start);
+                if (beginTag == -1) {
+                    ret.append(str.substring(start));
+                    break;
+                }
             }
         }
         // append everything after the last endTag
@@ -196,6 +205,40 @@ public class Utilities {
         return ret.toString().trim();
     }
 
+    /**
+     * True if the unclosed '&lt;' at the given index opens a comment, a
+     * declaration, an end tag, or a tag with attributes: a tag name, then
+     * whitespace or '/', and an '=' later on. Text such as "x&lt;y" or
+     * "a&lt;b and b&lt;c" is not a tag.
+     */
+    private static boolean startsTag(String str, int index) {
+        int i = index + 1;
+        if (i >= str.length()) {
+            return false;
+        }
+        char next = str.charAt(i);
+        if (next == '/' || next == '!' || next == '?') {
+            return true;
+        }
+        if (!isAsciiLetter(next)) {
+            return false;
+        }
+        while (i < str.length() && (isAsciiLetter(str.charAt(i))
+                || Character.isDigit(str.charAt(i)) || str.charAt(i) == '-')) {
+            i++;
+        }
+        if (i >= str.length()) {
+            return false;
+        }
+        char after = str.charAt(i);
+        return (Character.isWhitespace(after) || after == '/')
+                && str.indexOf('=', i) > -1;
+    }
+
+    private static boolean isAsciiLetter(char c) {
+        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+    }
+
     // ------------------------------------------------------------------------
     /**
      * Run both removeHTML and escapeHTML on a string.
diff --git a/app/src/main/webapp/WEB-INF/jsps/core/MainMenu.jsp 
b/app/src/main/webapp/WEB-INF/jsps/core/MainMenu.jsp
index d81dce008..c90f69006 100644
--- a/app/src/main/webapp/WEB-INF/jsps/core/MainMenu.jsp
+++ b/app/src/main/webapp/WEB-INF/jsps/core/MainMenu.jsp
@@ -71,7 +71,7 @@
             <p> <a href='<s:property value="#perms.weblog.absoluteURL" />'>
             <s:property value="#perms.weblog.absoluteURL" /></a></p>
 
-            <p><s:property value="#perms.weblog.about" escapeHtml="false" 
/></p>
+            <p><s:property value="#perms.weblog.about" /></p>
 
             <p>You have 
             <s:if test='#perms.hasAction("admin")'>ADMIN </s:if>
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java 
b/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java
index 7e008aeb2..2ab81ecdf 100644
--- a/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java
+++ b/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java
@@ -99,6 +99,43 @@ public class UtilitiesTest  {
         assertEquals(expect, result);
     }
 
+    @Test
+    public void testRemoveHTMLUnclosedTag() {
+        assertEquals("about", Utilities.removeHTML("about <img src=x", false));
+        assertEquals("about", Utilities.removeHTML("about <p>", false).trim());
+        assertEquals("a", Utilities.removeHTML("a </p", false));
+        assertEquals("a", Utilities.removeHTML("a <!-- note", false));
+        assertEquals("", Utilities.removeHTML("<img src=x", false));
+    }
+
+    @Test
+    public void testRemoveHTMLKeepsLessThanText() {
+        assertEquals("a < b", Utilities.removeHTML("a < b", false));
+        assertEquals("1 <2", Utilities.removeHTML("1 <2", false));
+        assertEquals("x <", Utilities.removeHTML("x <", false));
+        assertEquals("x<y", Utilities.removeHTML("x<y", false));
+        assertEquals("List<String", Utilities.removeHTML("List<String", 
false));
+        assertEquals("C<C++ fans", Utilities.removeHTML("C<C++ fans", false));
+        assertEquals("a<b and b<c", Utilities.removeHTML("a<b and b<c", 
false));
+    }
+
+    @Test
+    public void testRemoveHTMLDefaultAddsSpaceOnlyForTags() {
+        assertEquals("about", Utilities.removeHTML("about <img src=x"));
+        assertEquals("about", Utilities.removeHTML("about<img/src=x"));
+        assertEquals("x<y", Utilities.removeHTML("x<y"));
+        assertEquals("C<C++ fans", Utilities.removeHTML("C<C++ fans"));
+        assertEquals("a < b", Utilities.removeHTML("a < b"));
+        assertEquals("a b", Utilities.removeHTML("a<br>b"));
+    }
+
+    @Test
+    public void testRemoveHTMLUnclosedTagAfterLessThanText() {
+        assertEquals("a < b", Utilities.removeHTML("a < b <img src=x", false));
+        assertEquals("a < b < c", Utilities.removeHTML("a < b < c", false));
+        assertEquals("1 <2 <3", Utilities.removeHTML("1 <2 <3", false));
+    }
+
     @Test
     public void testTruncateNicely1() {
         String test = "blah blah blah blah blah";

Reply via email to