Author: almaw
Date: Tue Dec 5 14:04:24 2006
New Revision: 482802
URL: http://svn.apache.org/viewvc?view=rev&rev=482802
Log:
WICKET-109 Whitespace compression should avoid <pre> tags.
Modified:
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/MarkupParser.java
incubator/wicket/trunk/wicket/src/main/java/wicket/markup/MarkupParser.java
Modified:
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/MarkupParser.java
URL:
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/MarkupParser.java?view=diff&rev=482802&r1=482801&r2=482802
==============================================================================
---
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/MarkupParser.java
(original)
+++
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/MarkupParser.java
Tue Dec 5 14:04:24 2006
@@ -18,6 +18,7 @@
import java.io.IOException;
import java.text.ParseException;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import wicket.Application;
@@ -358,9 +359,54 @@
*/
protected String compressWhitespace(String rawMarkup)
{
- rawMarkup = rawMarkup.replaceAll("[ \\t]+", " ");
- rawMarkup = rawMarkup.replaceAll("( ?[\\r\\n] ?)+", "\n");
- return rawMarkup;
+ // We don't want to compress whitespace inside <pre> tags, so
we look
+ // for matches and:
+ // - Do whitespace compression on everything before the first
match.
+ // - Append the <pre>.*?</pre> match with no compression.
+ // - Loop to find the next match.
+ // - Append with compression everything between the two
matches.
+ // - Repeat until no match, then special-case the fragment
after the
+ // last <pre>.
+
+ Pattern preBlock = Pattern.compile("<pre>.*?</pre>",
Pattern.DOTALL | Pattern.MULTILINE);
+ Matcher m = preBlock.matcher(rawMarkup);
+ int lastend = 0;
+ StringBuffer sb = null;
+ while (true)
+ {
+ boolean matched = m.find();
+ String nonPre = matched
+ ? rawMarkup.substring(lastend,
m.start())
+ : rawMarkup.substring(lastend);
+ nonPre = nonPre.replaceAll("[ \\t]+", " ");
+ nonPre = nonPre.replaceAll("( ?[\\r\\n] ?)+", "\n");
+
+ // Don't create a StringBuffer if we don't actually
need one.
+ // This optimises the trivial common case where there
is no <pre>
+ // tag at all down to just doing the replaceAlls above.
+ if (lastend == 0)
+ {
+ if (matched)
+ {
+ sb = new
StringBuffer(rawMarkup.length());
+ }
+ else
+ {
+ return nonPre;
+ }
+ }
+ sb.append(nonPre);
+ if (matched)
+ {
+ sb.append(m.group());
+ lastend = m.end();
+ }
+ else
+ {
+ break;
+ }
+ }
+ return sb.toString();
}
/**
Modified:
incubator/wicket/trunk/wicket/src/main/java/wicket/markup/MarkupParser.java
URL:
http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/markup/MarkupParser.java?view=diff&rev=482802&r1=482801&r2=482802
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/markup/MarkupParser.java
(original)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/markup/MarkupParser.java
Tue Dec 5 14:04:24 2006
@@ -19,6 +19,7 @@
import java.io.IOException;
import java.text.ParseException;
import java.util.Stack;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import wicket.Page;
@@ -428,9 +429,54 @@
*/
protected String compressWhitespace(String rawMarkup)
{
- rawMarkup = rawMarkup.replaceAll("[ \\t]+", " ");
- rawMarkup = rawMarkup.replaceAll("( ?[\\r\\n] ?)+", "\n");
- return rawMarkup;
+ // We don't want to compress whitespace inside <pre> tags, so
we look
+ // for matches and:
+ // - Do whitespace compression on everything before the first
match.
+ // - Append the <pre>.*?</pre> match with no compression.
+ // - Loop to find the next match.
+ // - Append with compression everything between the two
matches.
+ // - Repeat until no match, then special-case the fragment
after the
+ // last <pre>.
+
+ Pattern preBlock = Pattern.compile("<pre>.*?</pre>",
Pattern.DOTALL | Pattern.MULTILINE);
+ Matcher m = preBlock.matcher(rawMarkup);
+ int lastend = 0;
+ StringBuffer sb = null;
+ while (true)
+ {
+ boolean matched = m.find();
+ String nonPre = matched
+ ? rawMarkup.substring(lastend,
m.start())
+ : rawMarkup.substring(lastend);
+ nonPre = nonPre.replaceAll("[ \\t]+", " ");
+ nonPre = nonPre.replaceAll("( ?[\\r\\n] ?)+", "\n");
+
+ // Don't create a StringBuffer if we don't actually
need one.
+ // This optimises the trivial common case where there
is no <pre>
+ // tag at all down to just doing the replaceAlls above.
+ if (lastend == 0)
+ {
+ if (matched)
+ {
+ sb = new
StringBuffer(rawMarkup.length());
+ }
+ else
+ {
+ return nonPre;
+ }
+ }
+ sb.append(nonPre);
+ if (matched)
+ {
+ sb.append(m.group());
+ lastend = m.end();
+ }
+ else
+ {
+ break;
+ }
+ }
+ return sb.toString();
}
/**