mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 07:49:24 +03:00
Fix print of block string with leading space and quotation
ref: graphql/graphql-js#1190
This commit is contained in:
parent
022c490011
commit
7705e50e44
@ -307,12 +307,14 @@ class Printer
|
|||||||
*/
|
*/
|
||||||
public function block($array)
|
public function block($array)
|
||||||
{
|
{
|
||||||
return $array && $this->length($array) ? $this->indent("{\n" . $this->join($array, "\n")) . "\n}" : '{}';
|
return ($array && $this->length($array))
|
||||||
|
? "{\n" . $this->indent($this->join($array, "\n")) . "\n}"
|
||||||
|
: '{}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function indent($maybeString)
|
public function indent($maybeString)
|
||||||
{
|
{
|
||||||
return $maybeString ? str_replace("\n", "\n ", $maybeString) : '';
|
return $maybeString ? ' ' . str_replace("\n", "\n ", $maybeString) : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function manyList($start, $list, $separator, $end)
|
public function manyList($start, $list, $separator, $end)
|
||||||
@ -344,12 +346,9 @@ class Printer
|
|||||||
* a single-line, adding a leading blank line would strip that whitespace.
|
* a single-line, adding a leading blank line would strip that whitespace.
|
||||||
*/
|
*/
|
||||||
private function printBlockString($value, $isDescription) {
|
private function printBlockString($value, $isDescription) {
|
||||||
|
$escaped = str_replace('"""', '\\"""', $value);
|
||||||
return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false)
|
return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false)
|
||||||
? ('"""' . str_replace('"""', '\\"""', $value) . '"""')
|
? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""')
|
||||||
: (
|
: ("\"\"\"\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n\"\"\"");
|
||||||
$isDescription
|
|
||||||
? ("\"\"\"\n" . str_replace('"""', '\\"""', $value) . "\n\"\"\"")
|
|
||||||
: ($this->indent("\"\"\"\n" . str_replace('"""', '\\"""', $value)) . "\n\"\"\"")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
';
|
';
|
||||||
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @it correctly prints block strings with a first line indentation
|
* @it correctly prints block strings with a first line indentation
|
||||||
*/
|
*/
|
||||||
@ -132,6 +132,25 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it correctly prints single-line with leading space and quotation
|
||||||
|
*/
|
||||||
|
public function testCorrectlyPrintsSingleLineStringsWithLeadingSpaceAndQuotation()
|
||||||
|
{
|
||||||
|
$mutationAstWithArtifacts = Parser::parse(
|
||||||
|
'{
|
||||||
|
field(arg: """ space-led value "quoted string"
|
||||||
|
""")
|
||||||
|
}'
|
||||||
|
);
|
||||||
|
$expected = '{
|
||||||
|
field(arg: """ space-led value "quoted string"
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
';
|
||||||
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @it prints kitchen sink
|
* @it prints kitchen sink
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user