Repository: jtablesaw/tablesaw
Pull request #702 (2019-10-30 05:13:01)

Escapes backslash too when creating Javascript string
Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Forgot to escape backslash itself (`\`) when I did PR #699

## Testing
Yes.

emilianbold: So, @lwhite1, can I just merge small PRs like this directly?

You have my email, we can talk over there about "house rules".
benmccann: It's probably better to use a Json handling library like Jackson which is used other places in the code
emilianbold: That seems a bit heavy but makes sense. I don't see Jackson in the dependencies though.
benmccann: It looks like it's used in the `saw` and `json` sub-projects
emilianbold: OK, using the util from Jackson now.

Patch:
diff --git a/jsplot/pom.xml b/jsplot/pom.xml
index aaa6312d..a6ff1a3f 100644
--- a/jsplot/pom.xml
+++ b/jsplot/pom.xml
@@ -90,6 +90,10 @@
     </build>
 
     <dependencies>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-core</artifactId>
+        </dependency>
         <dependency>
             <groupId>io.pebbletemplates</groupId>
             <artifactId>pebble</artifactId>
diff --git a/jsplot/src/main/java/tech/tablesaw/plotly/Utils.java b/jsplot/src/main/java/tech/tablesaw/plotly/Utils.java
index a9fafec8..09326b4a 100644
--- a/jsplot/src/main/java/tech/tablesaw/plotly/Utils.java
+++ b/jsplot/src/main/java/tech/tablesaw/plotly/Utils.java
@@ -1,5 +1,6 @@
 package tech.tablesaw.plotly;
 
+import com.fasterxml.jackson.core.io.JsonStringEncoder;
 import java.util.Arrays;
 
 public class Utils {
@@ -13,8 +14,12 @@ public class Utils {
     return "'" + string + "'";
   }
 
-  private static String escape(String s) {
-    return s.replaceAll("\\'", "\\\\'");
+  /**
+   * Escapes string for Javascript, assuming but without surrounding it with doublequotes (") and
+   * saves to output to the given StringBuilder.
+   */
+  private static void escape(String s, StringBuilder sb) {
+    JsonStringEncoder.getInstance().quoteAsString(s, sb);
   }
 
   /** @return a Javascript array of strings (escaped if needed) */
@@ -22,9 +27,9 @@ public class Utils {
     StringBuilder builder = new StringBuilder("[");
     for (int i = 0; i < data.length; i++) {
       Object o = data[i];
-      builder.append("'");
-      builder.append(escape(String.valueOf(o)));
-      builder.append("'");
+      builder.append("\"");
+      escape(String.valueOf(o), builder);
+      builder.append("\"");
       if (i < data.length - 1) {
         builder.append(",");
       }
diff --git a/pom.xml b/pom.xml
index 6ba58e0c..72bc1942 100644
--- a/pom.xml
+++ b/pom.xml
@@ -252,10 +252,15 @@
 
     <dependencyManagement>
         <dependencies>
+            <dependency>
+                <groupId>com.fasterxml.jackson.core</groupId>
+                <artifactId>jackson-core</artifactId>
+                <version>2.10.1</version>
+            </dependency>
             <dependency>
                 <groupId>com.fasterxml.jackson.core</groupId>
                 <artifactId>jackson-databind</artifactId>
-                <version>2.9.10.1</version>
+                <version>2.10.1</version>
             </dependency>
             <dependency>
                 <groupId>org.slf4j</groupId>


Test patch:
diff --git a/jsplot/src/test/java/tech/tablesaw/plotly/UtilsTest.java b/jsplot/src/test/java/tech/tablesaw/plotly/UtilsTest.java
index f926702c..2e1df5dc 100644
--- a/jsplot/src/test/java/tech/tablesaw/plotly/UtilsTest.java
+++ b/jsplot/src/test/java/tech/tablesaw/plotly/UtilsTest.java
@@ -1,5 +1,6 @@
 package tech.tablesaw.plotly;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
@@ -8,7 +9,9 @@ public class UtilsTest {
 
   @Test
   public void testEscapeQuote() {
-    String s = Utils.dataAsString(new String[] {"Bobby's tables"});
-    assertTrue(s.contains("\\"));
+    String s = Utils.dataAsString(new String[] {"Bobby\"s tables"});
+    assertTrue(s.contains("\\"), s);
+    String s2 = Utils.dataAsString(new String[] {"Hello\\"});
+    assertEquals("[\"Hello\\\\\"]", s2);
   }
 }
diff --git a/jsplot/src/test/java/tech/tablesaw/plotly/components/FigureTest.java b/jsplot/src/test/java/tech/tablesaw/plotly/components/FigureTest.java
index 61998a18..ac9fa1a8 100644
--- a/jsplot/src/test/java/tech/tablesaw/plotly/components/FigureTest.java
+++ b/jsplot/src/test/java/tech/tablesaw/plotly/components/FigureTest.java
@@ -25,8 +25,8 @@ class FigureTest {
             + "        \n"
             + "var trace0 =\n"
             + "{\n"
-            + "x: ['1.0','2.0','3.0','4.0','5.0'],\n"
-            + "y: ['1.0','4.0','9.0','16.0','25.0'],\n"
+            + "x: [\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"],\n"
+            + "y: [\"1.0\",\"4.0\",\"9.0\",\"16.0\",\"25.0\"],\n"
             + "mode: 'markers',\n"
             + "xaxis: 'x',\n"
             + "yaxis: 'y',\n"
@@ -91,8 +91,8 @@ class FigureTest {
             + "\n"
             + "var trace0 =\n"
             + "{\n"
-            + "x: ['1.0','2.0','3.0','4.0','5.0'],\n"
-            + "y: ['1.0','4.0','9.0','16.0','25.0'],\n"
+            + "x: [\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"],\n"
+            + "y: [\"1.0\",\"4.0\",\"9.0\",\"16.0\",\"25.0\"],\n"
             + "mode: 'markers',\n"
             + "xaxis: 'x',\n"
             + "yaxis: 'y',\n"