Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure the placeholder replacements is also done for ConsStrings #1293

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions 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 @@ -214,8 +214,8 @@ public static String format(Context cx, Scriptable scope, Object[] args) {
int argIndex = 0;

Object first = args[0];
if (first instanceof String) {
String msg = (String) first;
if (first instanceof String || first instanceof ConsString) {
String msg = first.toString();
Matcher matcher = FMT_REG.matcher(msg);

argIndex = 1;
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
18 changes: 18 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 Expand Up @@ -500,6 +512,12 @@ public void time() {
new PrinterCall(Level.WARN, new Object[] {"Timer 'c' does not exist."})));
}

@Test
public void printConsString() {
String js = "var msg = '['; msg += '%s'; msg += ']'; console.log(msg, 1234)";
assertPrintMsg(js, "[1234]");
}

private static void assertFormat(Object[] args, String expected) {
try (Context cx = Context.enter()) {
Scriptable scope = cx.initStandardObjects();
Expand Down