From 5bd5085a550c073b3c9e91646fa6ff5eb61a85be Mon Sep 17 00:00:00 2001 From: Patrick Lam Date: Sun, 24 Oct 2021 19:51:47 +1300 Subject: [PATCH] Make local copy of methods "isWhiteSpace" and "in" from jsoup. Class XTokenQueue is mostly borrowed from jsoup's TokenQueue per the doc comments. It imports internal util class StringUtil from jsoup and uses two methods from it. StringUtil's doc comments says "designed for internal jsoup use only". It's probably safer to have a local copy of these simple methods, as done in this PR. --- .../java/us/codecraft/xsoup/XTokenQueue.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/us/codecraft/xsoup/XTokenQueue.java b/src/main/java/us/codecraft/xsoup/XTokenQueue.java index 81caa76..da7264f 100644 --- a/src/main/java/us/codecraft/xsoup/XTokenQueue.java +++ b/src/main/java/us/codecraft/xsoup/XTokenQueue.java @@ -5,7 +5,6 @@ import java.util.regex.Pattern; import org.jsoup.helper.Validate; -import org.jsoup.internal.StringUtil; /** * A character queue with parsing helpers. @@ -58,7 +57,7 @@ public static String unescape(String in) { public static String trimQuotes(String str) { Validate.isTrue(str != null && str.length() > 0); String quote = str.substring(0, 1); - if (StringUtil.in(quote, "\"", "'")) { + if (in(quote, "\"", "'")) { Validate.isTrue(str.endsWith(quote), "Quote" + " for " + str + " is incomplete!"); str = str.substring(1, str.length() - 1); } @@ -199,7 +198,7 @@ public boolean matchChomp(String seq) { * @return if starts with whitespace */ public boolean matchesWhitespace() { - return !isEmpty() && StringUtil.isWhitespace(queue.charAt(pos)); + return !isEmpty() && isWhitespace(queue.charAt(pos)); } /** @@ -564,4 +563,25 @@ else if (matchesAny(quotes)) { } return params; } + + /** + * Tests if a code point is "whitespace" as defined in the HTML spec. Used for output HTML. + * Copied from jsoup's org.jsoup.internal.StringUtil. + * @param c code point to test + * @return true if code point is whitespace, false otherwise + * @see #isActuallyWhitespace(int) + */ + private static boolean isWhitespace(int c){ + return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'; + } + + // also copied from jsoup's org.jsoup.internal.StringUtil. + private static boolean in(final String needle, final String... haystack) { + final int len = haystack.length; + for (int i = 0; i < len; i++) { + if (haystack[i].equals(needle)) + return true; + } + return false; + } }