Skip to content

Commit

Permalink
ignore %c styling but count to not disturb further replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Dec 23, 2022
1 parent db9a373 commit 9032f89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/org/mozilla/javascript/NativeConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class NativeConsole extends IdScriptableObject {

private static final String DEFAULT_LABEL = "default";

private static final Pattern FMT_REG = Pattern.compile("%[sfdioO%]");
private static final Pattern FMT_REG = Pattern.compile("%[sfdioOc%]");

private final Map<String, Long> timers = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -249,6 +249,9 @@ public static String format(Context cx, Scriptable scope, Object[] args) {
replaceArg = formatObj(cx, scope, val);
break;

// %c is not supported,
// simply removed from the output

default:
replaceArg = "";
break;
Expand Down
12 changes: 12 additions & 0 deletions testsrc/org/mozilla/javascript/tests/NativeConsoleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ public void formatObject() {
}
}

@Test
public void formatStyling() {
assertFormat(new Object[] {"%c", "color: orange"}, "");
assertFormat(new Object[] {"12%c34", "color: orange"}, "1234");
assertFormat(new Object[] {"%c", "color: orange", "color: blue"}, "color: blue");

// %c counts
assertFormat(new Object[] {"12%c34%s", "color: orange", "ab"}, "1234ab");

assertFormat(new Object[] {"%c"}, "%c");
}

@Test
public void formatValueOnly() {
try (Context cx = Context.enter()) {
Expand Down

0 comments on commit 9032f89

Please sign in to comment.