White space optimization

pull/178/head
M66B 5 years ago
parent a091779302
commit f335e07f97

@ -1823,6 +1823,7 @@ public class HtmlHelper {
// https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace // https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
TextNode tnode; TextNode tnode;
String text; String text;
int index;
for (int i = 0; i < block.size(); ) { for (int i = 0; i < block.size(); ) {
tnode = block.get(i); tnode = block.get(i);
text = tnode.getWholeText(); text = tnode.getWholeText();
@ -1838,9 +1839,14 @@ public class HtmlHelper {
text = TRIM_WHITESPACE_NL.matcher(text).replaceAll(" "); text = TRIM_WHITESPACE_NL.matcher(text).replaceAll(" ");
// Remove leading whitespace // Remove leading whitespace
if (i == 0 || endsWithWhitespace(block.get(i - 1).text())) if (i == 0 || endsWithWhitespace(block.get(i - 1).text())) {
while (startsWithWhiteSpace(text)) index = 0;
text = text.substring(1); while (isWhiteSpace(text, index))
index++;
if (index > 0)
text = text.substring(index);
}
tnode.text(text); tnode.text(text);
@ -1855,9 +1861,13 @@ public class HtmlHelper {
while (i > 0) { while (i > 0) {
tnode = block.get(i - 1); tnode = block.get(i - 1);
text = tnode.getWholeText(); text = tnode.getWholeText();
if (endsWithWhitespace(text)) { index = text.length() - 1;
while (endsWithWhitespace(text)) if (isWhiteSpace(text, index)) {
text = text.substring(0, text.length() - 1); index--;
while (isWhiteSpace(text, index))
index--;
text = text.substring(0, index + 1);
tnode.text(text); tnode.text(text);
@ -1879,18 +1889,15 @@ public class HtmlHelper {
} }
} }
boolean startsWithWhiteSpace(String text) { boolean isWhiteSpace(String text, int index) {
int len = text.length(); if (index < 0 || index >= text.length())
if (len == 0)
return false; return false;
return WHITESPACE_NL.contains(text.substring(0, 1)); char kar = text.charAt(index);
return (WHITESPACE_NL.indexOf(kar) >= 0);
} }
boolean endsWithWhitespace(String text) { boolean endsWithWhitespace(String text) {
int len = text.length(); return isWhiteSpace(text, text.length() - 1);
if (len == 0)
return false;
return WHITESPACE_NL.contains(text.substring(len - 1));
} }
}, document.body()); }, document.body());

Loading…
Cancel
Save