Be more precise about encoding newlines: Do not add any new linebreaks,
plus use the system specific line separator.
This commit is contained in:
hns 2003-09-16 13:11:59 +00:00
parent 4466c58864
commit e6959610db

View file

@ -275,7 +275,7 @@ public final class HtmlEncoder {
} }
// HTML block tags need to suppress automatic newline to <br> // HTML block tags need to suppress automatic newline to <br>
// conversion around them to look good. However, they differ // conversion around them to look good. However, they differ
// in how many newlines around them should ignored. These sets // in how many newlines around them should ignored. These sets
// help to treat each tag right in newline conversion. // help to treat each tag right in newline conversion.
static final HashSet swallowAll = new HashSet(); static final HashSet swallowAll = new HashSet();
@ -342,10 +342,12 @@ public final class HtmlEncoder {
emptyTags.add("param"); emptyTags.add("param");
} }
final static byte TAG_NAME = 0; static final byte TAG_NAME = 0;
final static byte TAG_SPACE = 1; static final byte TAG_SPACE = 1;
final static byte TAG_ATT_NAME = 2; static final byte TAG_ATT_NAME = 2;
final static byte TAG_ATT_VAL = 3; static final byte TAG_ATT_VAL = 3;
static final String newLine = System.getProperty("line.separator");
/** /**
@ -529,11 +531,13 @@ public final class HtmlEncoder {
} }
if ((linebreaks > 0 || swallowLinebreaks > 0) && !Character.isWhitespace(c)) { if ((linebreaks > 0 || swallowLinebreaks > 0) && !Character.isWhitespace(c)) {
if (!insidePreTag && (linebreaks > swallowLinebreaks)) { if (!insidePreTag) {
linebreaks -= swallowLinebreaks; for (int k = 0; k < linebreaks; k++) {
if (k >= swallowLinebreaks) {
for (int k = 0; k < linebreaks; k++) ret.append("<br />");
ret.append("<br />\n"); }
ret.append(newLine);
}
} }
if (!insideTag) { if (!insideTag) {
@ -628,13 +632,17 @@ public final class HtmlEncoder {
break; break;
case '\n': case '\n':
ret.append('\n');
if (!insideTag) { if (!insideTag) {
linebreaks++; linebreaks++;
} else {
ret.append('\n');
} }
break; break;
case '\r':
if (!insideTag) {
break;
}
case '>': case '>':
@ -731,11 +739,13 @@ public final class HtmlEncoder {
} }
// add remaining newlines we may have collected // add remaining newlines we may have collected
if ((linebreaks > 0) && (linebreaks > swallowLinebreaks)) { if (linebreaks > 0) {
linebreaks -= swallowLinebreaks; for (int i = 0; i < linebreaks; i++) {
if (i >= swallowLinebreaks) {
for (int i = 0; i < linebreaks; i++) ret.append("<br />");
ret.append("<br />\n"); }
ret.append(newLine);
}
} }
} }
@ -830,11 +840,10 @@ public final class HtmlEncoder {
break; break;
case '\n': case '\n':
ret.append('\n');
if (encodeNewline) { if (encodeNewline) {
ret.append("<br />"); ret.append("<br />");
} }
ret.append('\n');
break; break;