Skip to content

Commit cc37321

Browse files
committed
Encode specials correctly.
Currently, all special escape sequences are double-escaped, converting \l into \l. This means that you cannot use an escape sequence in any of the attributes. This patch encodes the value while leaving escape sequences in play. It uses the valid escape sequences listed http://www.graphviz.org/doc/info/attrs.html#k:escString:
1 parent 767cf6f commit cc37321

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/phpDocumentor/GraphViz/Attribute.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
* @return string
136+
*/
137+
protected function encodeSpecials()
138+
{
139+
$value = $this->getValue();
140+
$validEscapes = 'NGEGTHLnlr';
141+
$regex = '(\'|"|\\x00|\\\\(?![\\\\NGETHLnlr]))';
142+
return preg_replace($regex, '\\\\$0', $value);
143+
}
129144
}

tests/phpDocumentor/GraphViz/Test/AttributeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)