]> gitweb.factorcode.org Git - factor.git/commitdiff
graphviz.dot: fix escaping logic
authorBjörn Lindqvist <bjourne@gmail.com>
Fri, 11 Mar 2016 08:44:42 +0000 (09:44 +0100)
committerBjörn Lindqvist <bjourne@gmail.com>
Fri, 11 Mar 2016 08:44:42 +0000 (09:44 +0100)
It used escape the escape char \, but it shouldn't do that because it
leads to double escaping.

extra/graphviz/dot/dot-tests.factor [new file with mode: 0644]
extra/graphviz/dot/dot.factor

diff --git a/extra/graphviz/dot/dot-tests.factor b/extra/graphviz/dot/dot-tests.factor
new file mode 100644 (file)
index 0000000..190b936
--- /dev/null
@@ -0,0 +1,21 @@
+USING: graphviz.dot.private io.streams.string sequences tools.test ;
+IN: graphviz.dot.tests
+
+! Making sure strings are escaped properly
+{
+    {
+        "\"BAH\" "
+        "\"LINE1\\nLINE2\" "
+        "\"\\lLINE1\\lLINE2\" "
+        "\"hum\\\"ho\\\"\" "
+    }
+} [
+    {
+        "BAH"
+        "LINE1\\nLINE2"
+        "\\lLINE1\\lLINE2"
+        "hum\"ho\""
+    } [
+        [ dot. ] with-string-writer
+    ] map
+] unit-test
index 82109cc87b66b3437a8a3cb8f87b3eac5c98dba1..48677794b88b35a4a8a2d728ecefb642db7b870d 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2012 Alex Vondrak.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors classes classes.tuple combinators formatting graphviz
-graphviz.attributes io io.files kernel namespaces prettyprint.backend
-sequences splitting strings words ;
+graphviz.attributes io io.files kernel namespaces sequences splitting
+strings words ;
 IN: graphviz.dot
 
 <PRIVATE
@@ -14,8 +14,12 @@ GENERIC: dot. ( obj -- )
 ! option in case there's a keyword clash, spaces in the ID,
 ! etc.  This does mean that HTML labels aren't supported, but
 ! they don't seem to work using the Graphviz API anyway.
+!
+! Special escaping logic is required here because of the \l escape
+! sequence.
 : quote-string ( str -- str' )
-    "\"" "\"" unparse-string "\0" split "" join ;
+    { { "\"" "\\\"" } { "\0" "" } } [ first2 replace ] each
+    "\"" "\"" surround ;
 
 M: string dot. quote-string "%s " printf ;