File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed
src/phpDocumentor/GraphViz
tests/phpDocumentor/GraphViz/Test Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -99,7 +99,9 @@ public function __toString()
99
99
}
100
100
101
101
$ value = $ this ->getValue ();
102
- if (!$ this ->isValueInHtml () || $ this ->isValueContainingSpecials ()) {
102
+ if ($ this ->isValueContainingSpecials ()) {
103
+ $ value = '" ' . $ this ->encodeSpecials () . '" ' ;
104
+ } elseif (!$ this ->isValueInHtml ()) {
103
105
$ value = '" ' . addslashes ($ value ) . '" ' ;
104
106
}
105
107
return $ key . '= ' . $ value ;
@@ -126,4 +128,17 @@ public function isValueContainingSpecials()
126
128
{
127
129
return strstr ($ this ->getValue (), "\\" ) !== false ;
128
130
}
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
+ }
129
144
}
Original file line number Diff line number Diff line change @@ -158,6 +158,35 @@ public function testToString()
158
158
);
159
159
}
160
160
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
+
161
190
/**
162
191
* Tests whether the isValueContainingSpecials function
163
192
*
@@ -173,4 +202,6 @@ public function testIsValueContainingSpecials()
173
202
$ this ->fixture ->setValue ('+ ship(): boolean ' );
174
203
$ this ->assertFalse ($ this ->fixture ->isValueContainingSpecials ());
175
204
}
205
+
206
+
176
207
}
You can’t perform that action at this time.
0 commit comments