Skip to content

Escape trailing \ in QuestionHelper autocompletion #24660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\Helper;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -303,7 +304,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
// Save cursor position
$output->write("\0337");
// Write highlighted text
$output->write('<hl>'.substr($matches[$ofs], $i).'</hl>');
$output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $i)).'</hl>');
Copy link
Contributor

@ro0NL ro0NL Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant we use escape() here? (it includes escapeTrailingBackslash also) Not sure about leaking out this detail =/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in the PR description, I think this is a BC break as escape would effectively turn off interpreting tags in autocompletion options (they'll be always shown as is); while the impact should not be terrible, this doesn't seem to be the right thing to do for anything but master where it could be acceptable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!

they'll be always shown as is

Which is in fact the actual value.. no? But 👍 for keeping as is; thus escapeTrailingBackslash only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ro0NL , yeah.
I'd be glad to create another one for master with just escape as it indeed seems to be the right thing in general (interpretation of tags is not guaranteed there anyway, only <hl> accidentally works).

Copy link
Contributor

@ro0NL ro0NL Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless we qualify that a bugfix for 2.7 ;-) impact should be minimal. More or less relates to escaping exceptions #22142

also h1 leaks in as a valid style after calling... perhaps inline it using background/foreground options.

Copy link
Member

@chalasr chalasr Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep it as is until someone asks for it, escaping trailing backslashes only

// Restore cursor position
$output->write("\0338");
}
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,46 @@ public function testAskWithAutocompleteWithNonSequentialKeys()
$this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}

public function testAutocompleteWithTrailingBackslash()
{
if (!$this->hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

$inputStream = $this->getInputStream('E');

$dialog = new QuestionHelper();
$dialog->setInputStream($inputStream);
$helperSet = new HelperSet(array(new FormatterHelper()));
$dialog->setHelperSet($helperSet);

$question = new Question('');
$expectedCompletion = 'ExampleNamespace\\';
$question->setAutocompleterValues(array($expectedCompletion));

$output = $this->createOutputInterface();
$dialog->ask($this->createInputInterfaceMock(), $output, $question);

$outputStream = $output->getStream();
rewind($outputStream);
$actualOutput = stream_get_contents($outputStream);

// Shell control (esc) sequences are not so important: we only care that
// <hl> tag is interpreted correctly and replaced
$irrelevantEscSequences = array(
"\0337" => '', // Save cursor position
"\0338" => '', // Restore cursor position
"\033[K" => '', // Clear line from cursor till the end
);

$importantActualOutput = strtr($actualOutput, $irrelevantEscSequences);

// Remove colors (e.g. "\033[30m", "\033[31;41m")
$importantActualOutput = preg_replace('/\033\[\d+(;\d+)?m/', '', $importantActualOutput);

$this->assertEquals($expectedCompletion, $importantActualOutput);
}

public function testAskHiddenResponse()
{
if ('\\' === DIRECTORY_SEPARATOR) {
Expand Down