Skip to content

Commit

Permalink
Eliminate unnecessary nested statement(s) within else clauses
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strickroth <[email protected]>
  • Loading branch information
csware committed Feb 2, 2024
1 parent cd61b17 commit 81d7404
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 69 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/owasp/html/AttributePolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ static final class AttributePolicyJoiner
Optional<List<AttributePolicy>> split(AttributePolicy x) {
if (x instanceof JoinedAttributePolicy) {
return Optional.of(((JoinedAttributePolicy) x).policies);
} else {
return Optional.empty();
}
return Optional.empty();
}

@Override
Expand Down
51 changes: 23 additions & 28 deletions src/main/java/org/owasp/html/CssTokens.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,16 +736,15 @@ private boolean consumeIgnorable() {
if (ast < 0) {
pos = cssLimit; // Unclosed /* comment */
break;
} else {
// Advance over a run of '*'s.
pos = ast + 1;
while (pos < cssLimit && css.charAt(pos) == '*') {
++pos;
}
if (pos < cssLimit && css.charAt(pos) == '/') {
++pos;
break;
}
}
// Advance over a run of '*'s.
pos = ast + 1;
while (pos < cssLimit && css.charAt(pos) == '*') {
++pos;
}
if (pos < cssLimit && css.charAt(pos) == '/') {
++pos;
break;
}
}
} else if (next == '/') { // Non-standard but widely supported
Expand Down Expand Up @@ -778,10 +777,9 @@ private boolean consumeIgnorable() {
}
if (pos == posBefore) {
return false;
} else {
breakOutput();
return true;
}
breakOutput();
return true;
}

private void breakOutput() {
Expand Down Expand Up @@ -846,9 +844,8 @@ private boolean consumeAtKeyword() {
--pos; // back up over '@'
sb.setLength(bufferLengthBeforeWrite); // Unwrite the '@'
return false;
} else {
return true;
}
return true;
}


Expand Down Expand Up @@ -1122,11 +1119,10 @@ private TokenType consumeString() {
pos += 2;
}
continue;
} else {
decoded = consumeAndDecodeEscapeSequence();
if (decoded < 0) {
break;
}
}
decoded = consumeAndDecodeEscapeSequence();
if (decoded < 0) {
break;
}
} else {
++pos;
Expand All @@ -1137,11 +1133,11 @@ private TokenType consumeString() {
if (closed) {
sb.append('\'');
return TokenType.STRING;
} else { // Drop <bad-string>s
sb.setLength(startOfStringOnOutput);
breakOutput();
return TokenType.WHITESPACE;
}
// Drop <bad-string>s
sb.setLength(startOfStringOnOutput);
breakOutput();
return TokenType.WHITESPACE;
}

private @Nullable TokenType consumeHash() {
Expand Down Expand Up @@ -1258,11 +1254,10 @@ private boolean consumeUnicodeRange() {
sb.setCharAt(bufferStart + 1, 'r');
sb.setCharAt(bufferStart + 2, 'l');
return TokenType.URL;
} else {
sb.setLength(bufferStart);
breakOutput();
return TokenType.WHITESPACE;
}
sb.setLength(bufferStart);
breakOutput();
return TokenType.WHITESPACE;
} else if (parenAfter) {
openBracket('(');
++pos;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/owasp/html/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public void handle(Object x) {
public void handle(Throwable th) {
if (th instanceof RuntimeException) {
throw (RuntimeException) th;
} else {
throw new AssertionError(null, th);
}
throw new AssertionError(null, th);
}
};
}
5 changes: 2 additions & 3 deletions src/main/java/org/owasp/html/HtmlEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -2467,10 +2467,9 @@ public static int appendDecodedEntity(
if (codepoint < 0) {
sb.append('&');
return offset + 1;
} else {
sb.appendCodePoint(codepoint);
return tail;
}
sb.appendCodePoint(codepoint);
return tail;
}

private static boolean isHtmlIdContinueChar(char ch) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/owasp/html/HtmlLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ protected HtmlToken produce() {
state = State.SAW_EQ;
// Skip the '=' token
return produce();
} else {
// Reclassify as attribute name
token = HtmlInputSplitter.reclassify(
token, HtmlTokenType.ATTRNAME);
}
// Reclassify as attribute name
token = HtmlInputSplitter.reclassify(
token, HtmlTokenType.ATTRNAME);
} else {
state = State.IN_TAG;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/owasp/html/HtmlPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,8 @@ private HtmlTagSkipType getHtmlTagSkipType(String elementName) {
if (htmlTagSkipType == null) {
if (DEFAULT_SKIP_TAG_MAP_IF_EMPTY_ATTR.containsKey(elementName)) {
return HtmlTagSkipType.SKIP_BY_DEFAULT;
} else {
return HtmlTagSkipType.DO_NOT_SKIP_BY_DEFAULT;
}
return HtmlTagSkipType.DO_NOT_SKIP_BY_DEFAULT;
}

return htmlTagSkipType;
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/org/owasp/html/HtmlStreamRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,22 @@ private final void writeCloseTag(String uncanonElementName)
if (!lastTagOpened.equals(elementName)) {
error("Tag content cannot appear inside CDATA element", elementName);
return;
} else {
StringBuilder cdataContent = pendingUnescaped;
pendingUnescaped = null;
Encoding.stripBannedCodeunits(cdataContent);
int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent);
if (problemIndex == -1) {
if (cdataContent.length() != 0) {
output.append(cdataContent);
}
} else {
error(
"Invalid CDATA text content",
cdataContent.subSequence(
problemIndex,
Math.min(problemIndex + 10, cdataContent.length())));
// Still output the close tag.
}
StringBuilder cdataContent = pendingUnescaped;
pendingUnescaped = null;
Encoding.stripBannedCodeunits(cdataContent);
int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent);
if (problemIndex == -1) {
if (cdataContent.length() != 0) {
output.append(cdataContent);
}
} else {
error(
"Invalid CDATA text content",
cdataContent.subSequence(
problemIndex,
Math.min(problemIndex + 10, cdataContent.length())));
// Still output the close tag.
}
if ("plaintext".equals(elementName)) { return; }
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/owasp/html/PolicyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ public <CTX> HtmlSanitizer.Policy apply(
@Nullable CTX context) {
if (listener == null) {
return apply(out);
} else {
HtmlChangeReporter<CTX> r = new HtmlChangeReporter<>(
out, listener, context);
r.setPolicy(apply(r.getWrappedRenderer()));
return r.getWrappedPolicy();
}
}
HtmlChangeReporter<CTX> r = new HtmlChangeReporter<>(
out, listener, context);
r.setPolicy(apply(r.getWrappedRenderer()));
return r.getWrappedPolicy();
}

/** A convenience function that sanitizes a string of HTML. */
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/owasp/html/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ private Trie(
this.childMap = ZERO_CHARS;
this.children = ((Trie<T>[]) ZERO_TRIES);
return;
} else {
++pos;
}
++pos;
} else {
this.value = null;
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/owasp/html/ExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public static final void testExamplesRun() throws Exception {
+ captured.toString("UTF-8"));
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
} else {
throw new AssertionError(null, ex);
}
throw new AssertionError(null, ex);
} finally {
System.setIn(stdin);
System.setOut(stdout);
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/owasp/html/HtmlSanitizerFuzzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ public void run() {
if (failure != null) {
if (failure instanceof RuntimeException) {
throw (RuntimeException) failure;
} else {
throw new AssertionError(null, failure);
}
throw new AssertionError(null, failure);
}
}

Expand Down

0 comments on commit 81d7404

Please sign in to comment.