martin-g commented on a change in pull request #351: URL: https://github.com/apache/tomcat/pull/351#discussion_r494077383
########## File path: java/org/apache/jasper/compiler/NewlineReductionServletWriter.java ########## @@ -0,0 +1,41 @@ +package org.apache.jasper.compiler; + +import java.io.PrintWriter; + +/** + * This class filters duplicate newlines instructions from the compiler output, + * and therefore from the runtime JSP. The duplicates typically happen because + * the compiler has multiple branches that write them, but they operate + * independently and don't realize that the previous output was identical. + * + * Removing these lines makes the JSP more efficient by executing fewer operations during runtime. + * + * @author Engebretson, John + * @author Kamnani, Jatin + * + */ +public class NewlineReductionServletWriter extends ServletWriter { + private static final String NEWLINE_WRITE_TEXT = "out.write('\\n');"; Review comment: This one is a bit strange. I haven't written JSP code in more than 10 years now but if I want to produce several new lines I'd do `out.write("\n\n\n\n")` instead of 4 times `out.write('\\n');`. In what cases one would have several `out.write('\\n');` ? ########## File path: java/org/apache/jasper/compiler/Generator.java ########## @@ -2095,6 +2101,20 @@ public void visit(Node.JspElement n) throws JasperException { public void visit(Node.TemplateText n) throws JasperException { String text = n.getText(); + // If the flag is active, attempt to minimize the frequency of + // regex operations. + if ((ctxt!=null) && + ctxt.getOptions().getJSPWhiteSpaceTrimFlag() && + text.contains("\n")) { + // Ensure there are no <pre> or </pre> tags embedded in this + // text - if there are, we want to NOT modify the whitespace. + Matcher preMatcher = PRE_TAG_PATTERN.matcher(text); + if (!preMatcher.matches()) { + Matcher matcher = BLANK_LINE_PATTERN.matcher(text); + String revisedText = matcher.replaceAll("\n"); Review comment: This will drop also any `\r`s in the string. Is this intentional ? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org