File tree Expand file tree Collapse file tree
src/phpDocumentor/GraphViz
tests/phpDocumentor/GraphViz/Test Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -99,7 +99,9 @@ public function __toString()
9999 }
100100
101101 $ value = $ this ->getValue ();
102- if (!$ this ->isValueInHtml () || $ this ->isValueContainingSpecials ()) {
102+ if ($ this ->isValueContainingSpecials ()) {
103+ $ value = '" ' . $ this ->encodeSpecials () . '" ' ;
104+ } elseif (!$ this ->isValueInHtml ()) {
103105 $ value = '" ' . addslashes ($ value ) . '" ' ;
104106 }
105107 return $ key . '= ' . $ value ;
@@ -126,4 +128,17 @@ public function isValueContainingSpecials()
126128 {
127129 return strstr ($ this ->getValue (), "\\" ) !== false ;
128130 }
131+
132+ /**
133+ * Encode special characters so the escape sequences aren't removed
134+ *
135+ * @see http://www.graphviz.org/doc/info/attrs.html#k:escString
136+ * @return string
137+ */
138+ protected function encodeSpecials ()
139+ {
140+ $ value = $ this ->getValue ();
141+ $ regex = '( \'|"| \\x00| \\\\(?![ \\\\NGETHLnlr])) ' ;
142+ return preg_replace ($ regex , '\\\\$0 ' , $ value );
143+ }
129144}
Original file line number Diff line number Diff line change @@ -158,6 +158,35 @@ public function testToString()
158158 );
159159 }
160160
161+ /**
162+ * Tests whether the toString provides a valid GraphViz attribute string.
163+ *
164+ * @covers \phpDocumentor\GraphViz\Attribute::__toString
165+ * @covers \phpDocumentor\GraphViz\Attribute::encodeSpecials
166+ *
167+ * @return void
168+ */
169+ public function testToStringWithSpecials ()
170+ {
171+ $ this ->fixture = new Attribute ('a ' , 'b ' );
172+
173+ $ this ->fixture ->setValue ('a\la ' );
174+ $ this ->assertSame (
175+ 'a="a\la" ' , (string )$ this ->fixture ,
176+ 'Specials should not be escaped '
177+ );
178+ $ this ->fixture ->setValue ('a\l"a ' );
179+ $ this ->assertSame (
180+ 'a="a\l\"a" ' , (string )$ this ->fixture ,
181+ 'Specials should not be escaped, but quotes should '
182+ );
183+ $ this ->fixture ->setValue ('a \\\\l"a ' );
184+ $ this ->assertSame (
185+ 'a="a \\\\l\"a" ' , (string )$ this ->fixture ,
186+ 'Double backslashes should stay the same '
187+ );
188+ }
189+
161190 /**
162191 * Tests whether the isValueContainingSpecials function
163192 *
@@ -173,4 +202,6 @@ public function testIsValueContainingSpecials()
173202 $ this ->fixture ->setValue ('+ ship(): boolean ' );
174203 $ this ->assertFalse ($ this ->fixture ->isValueContainingSpecials ());
175204 }
205+
206+
176207}
You can’t perform that action at this time.
0 commit comments