Skip to content
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

Escaping fixed #96

Merged
merged 2 commits into from Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fixtures/quotes.po
Expand Up @@ -14,4 +14,4 @@ msgid "a\nb\nc"
msgstr "linebreaks"

msgid "a\"b\"c"
msgstr "quotes"
msgstr "quotes in \"translation\""
6 changes: 3 additions & 3 deletions src/Parser.php
Expand Up @@ -320,7 +320,7 @@ protected function parseComment($line, $entry)
*/
protected function parseHeaders($msgstr)
{
$headers = \array_filter(\explode('\\n', $msgstr));
$headers = \array_filter(\explode("\n", $msgstr));

return new Header($headers);
}
Expand Down Expand Up @@ -356,7 +356,7 @@ protected function shouldCloseEntry($line, array $entry)
*/
protected function unquote($value)
{
return \preg_replace('/^\"|\"$/', '', $value);
return \stripcslashes(\preg_replace('/^\"|\"$/', '', $value));
}

/**
Expand Down Expand Up @@ -389,7 +389,7 @@ protected function isHeader(array $entry)
'Plural-Forms:',
);

$headers = \explode('\n', $entry['msgstr']);
$headers = \explode("\n", $entry['msgstr']);
// Remove text after double colon
$headers = \array_map(
function ($header) {
Expand Down
17 changes: 5 additions & 12 deletions src/PoCompiler.php
Expand Up @@ -254,17 +254,9 @@ protected function buildProperty($property, $value, $obsolete = false)
*/
protected function cleanExport($string)
{
$quote = '"';
$slash = '\\';
$newline = "\n";
$string = sprintf('"%s"', addcslashes($string, "\42\134")); // " and \
pherrymason marked this conversation as resolved.
Show resolved Hide resolved

// escape qoutes that are not allready escaped
$string = \preg_replace('#(?<!\\\)"#', "$slash$quote", $string);

// remove empty strings
$string = \str_replace("$newline$quote$quote", '', $string);

return "$quote$string$quote";
return str_replace(chr(13), '\n', $string);
}

/**
Expand All @@ -273,7 +265,8 @@ protected function cleanExport($string)
*/
private function wrapString($value)
{
$wrapped = \wordwrap($value, $this->wrappingColumn, " \n");
return \explode("\n", $wrapped);
$value = str_replace("\n", chr(13) . chr(28), $value);
pherrymason marked this conversation as resolved.
Show resolved Hide resolved
$wrapped = \wordwrap($value, $this->wrappingColumn, sprintf(' %c', 28));
return \explode(chr(28), $wrapped);
}
}
8 changes: 5 additions & 3 deletions tests/UnitTest/ReadPoTest.php
Expand Up @@ -20,7 +20,7 @@ public function testBasic()
$entry = $catalog->getEntry('string.2');
$this->assertNotNull($entry);
$this->assertEquals('string.2', $entry->getMsgId());
$this->assertEquals('translation \"quoted\"', $entry->getMsgStr());
$this->assertEquals('translation "quoted"', $entry->getMsgStr());
}

public function testBasicMultiline()
Expand Down Expand Up @@ -215,7 +215,9 @@ public function testProperQuotesEscaping()
$catalog = $this->parseFile('quotes.po');

$this->assertCount(2, $catalog->getEntries());
$this->assertNotNull($catalog->getEntry('a\"b\"c'));
$this->assertNotNull($catalog->getEntry('a\nb\nc'));
$this->assertNotNull($catalog->getEntry("a\nb\nc"));

$entryWithQuotes = $catalog->getEntry('a"b"c');
$this->assertEquals('a"b"c', $entryWithQuotes->getMsgId());
}
}
12 changes: 10 additions & 2 deletions tests/WriteTest.php
Expand Up @@ -95,7 +95,14 @@ public function testDoubleEscaped()

$entry = EntryFactory::createFromArray(array(
'msgid' => 'a\nb\nc',
'msgstr' => 'linebreaks'
'msgstr' => 'slashes'
));
$catalogSource->addEntry($entry);

// Entry with line breaks
$entry = EntryFactory::createFromArray(array(
'msgid' => "a\nb\nc",
'msgstr' => "proper\nlinebreaks"
));
$catalogSource->addEntry($entry);

Expand All @@ -106,9 +113,10 @@ public function testDoubleEscaped()
}

$catalog = $this->parseFile('temp.po');
$this->assertCount(2, $catalog->getEntries());
$this->assertCount(3, $catalog->getEntries());
$this->assertNotNull($catalog->getEntry('a\"b\"c'));
$this->assertNotNull($catalog->getEntry('a\nb\nc'));
$this->assertNotNull($catalog->getEntry("a\nb\nc"));
}

public function testWrapping()
Expand Down