This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 5ab4d876aa5e6d7e088080d374ac5ed756c98c0d Author: Juan Pablo Santos RodrÃguez <[email protected]> AuthorDate: Sat Apr 5 14:31:49 2025 +0200 Move escapeHTMLEntities to TextUtil --- .../apache/wiki/parser/JSPWikiMarkupParser.java | 65 +++------------------- .../main/java/org/apache/wiki/util/TextUtil.java | 51 ++++++++++++++++- 2 files changed, 57 insertions(+), 59 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java b/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java index c6e75cef2..0998f6068 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java @@ -241,19 +241,18 @@ public class JSPWikiMarkupParser extends MarkupParser { } /** - * Creates a JDOM anchor element. Can be overridden to change the URL creation, - * if you really know what you are doing. + * Creates a JDOM anchor element. Can be overridden to change the URL creation, if you really know what you are doing. * * @param type One of the types above * @param link URL to which to link to * @param text Link text * @param section If a particular section identifier is required. - * @return An A element. + * @return An 'A' element. * @since 2.4.78 */ private Element createAnchor( final int type, final String link, String text, String section ) { - text = escapeHTMLEntities( text ); - section = escapeHTMLEntities( section ); + text = TextUtil.escapeHTMLEntities( text ); + section = TextUtil.escapeHTMLEntities( section ); final Element el = new Element( "a" ); el.setAttribute( "class", CLASS_TYPES[ type ] ); el.setAttribute( "href", link + section ); @@ -299,7 +298,7 @@ public class JSPWikiMarkupParser extends MarkupParser { el = new Element( "a" ).setAttribute( "class", CLASS_FOOTNOTE ); el.setAttribute( "name", "ref-" + m_context.getName() + "-" + link.substring( 1 ) ); if( !m_allowHTML ) { - el.addContent( "[" + escapeHTMLEntities( text ) + "]" ); + el.addContent( "[" + TextUtil.escapeHTMLEntities( text ) + "]" ); } else { el.addContent( "[" + text + "]" ); } @@ -424,7 +423,7 @@ public class JSPWikiMarkupParser extends MarkupParser { String buf; if( !m_allowHTML ) { - buf = escapeHTMLEntities(m_plainTextBuf.toString()); + buf = TextUtil.escapeHTMLEntities( m_plainTextBuf.toString() ); } else { buf = m_plainTextBuf.toString(); } @@ -496,56 +495,6 @@ public class JSPWikiMarkupParser extends MarkupParser { return numChars; } - /** - * Escapes XML entities in a HTML-compatible way (i.e. does not escape entities that are already escaped). - * - * @param buf - * @return An escaped string. - */ - private String escapeHTMLEntities( final String buf ) { - final StringBuilder tmpBuf = new StringBuilder( buf.length() + 20 ); - for( int i = 0; i < buf.length(); i++ ) { - final char ch = buf.charAt(i); - if( ch == '<' ) { - tmpBuf.append("<"); - } else if( ch == '>' ) { - tmpBuf.append(">"); - } else if( ch == '\"' ) { - tmpBuf.append("""); - } else if( ch == '&' ) { - // If the following is an XML entity reference (&#.*;) we'll leave it as it is; otherwise we'll replace it with an & - boolean isEntity = false; - final StringBuilder entityBuf = new StringBuilder(); - if( i < buf.length() -1 ) { - for( int j = i; j < buf.length(); j++ ) { - final char ch2 = buf.charAt( j ); - if( Character.isLetterOrDigit( ch2 ) || (ch2 == '#' && j == i+1) || ch2 == ';' || ch2 == '&' ) { - entityBuf.append(ch2); - if( ch2 == ';' ) { - isEntity = true; - break; - } - } else { - break; - } - } - } - - if( isEntity ) { - tmpBuf.append( entityBuf ); - i = i + entityBuf.length() - 1; - } else { - tmpBuf.append( "&" ); - } - - } else { - tmpBuf.append( ch ); - } - } - - return tmpBuf.toString(); - } - private Element pushElement( final Element e ) { flushPlainText(); m_currentElement.addContent( e ); @@ -996,7 +945,7 @@ public class JSPWikiMarkupParser extends MarkupParser { addElement( outlinkImage() ); } } else { - final Object[] args = { escapeHTMLEntities( extWiki ) }; + final Object[] args = { TextUtil.escapeHTMLEntities( extWiki ) }; addElement( makeError( MessageFormat.format( rb.getString( "markupparser.error.nointerwikiref" ), args ) ) ); } } diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java b/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java index 9b7df6c36..b8d9473e2 100644 --- a/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java +++ b/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java @@ -685,6 +685,56 @@ public final class TextUtil { return clean.toString(); } + /** + * Escapes XML entities in an HTML-compatible way (i.e. does not escape entities that are already escaped). + * + * @param buf String to be escaped. + * @return An escaped string. + */ + public static String escapeHTMLEntities( final String buf ) { + final StringBuilder tmpBuf = new StringBuilder( buf.length() + 20 ); + for( int i = 0; i < buf.length(); i++ ) { + final char ch = buf.charAt(i); + if( ch == '<' ) { + tmpBuf.append("<"); + } else if( ch == '>' ) { + tmpBuf.append(">"); + } else if( ch == '\"' ) { + tmpBuf.append("""); + } else if( ch == '&' ) { + // If the following is an XML entity reference (&#.*;) we'll leave it as it is; otherwise we'll replace it with an & + boolean isEntity = false; + final StringBuilder entityBuf = new StringBuilder(); + if( i < buf.length() -1 ) { + for( int j = i; j < buf.length(); j++ ) { + final char ch2 = buf.charAt( j ); + if( Character.isLetterOrDigit( ch2 ) || (ch2 == '#' && j == i+1) || ch2 == ';' || ch2 == '&' ) { + entityBuf.append(ch2); + if( ch2 == ';' ) { + isEntity = true; + break; + } + } else { + break; + } + } + } + + if( isEntity ) { + tmpBuf.append( entityBuf ); + i = i + entityBuf.length() - 1; + } else { + tmpBuf.append( "&" ); + } + + } else { + tmpBuf.append( ch ); + } + } + + return tmpBuf.toString(); + } + /** * Creates a Properties object based on an array which contains alternatively a key and a value. It is useful * for generating default mappings. For example: @@ -779,7 +829,6 @@ public final class TextUtil { * @since 2.1.98. */ public static String repeatString( final String what, final int times ) { - return IntStream.range(0, times).mapToObj(i -> what).collect(Collectors.joining()); }
