Skip to content

Commit 719d5e9

Browse files
TheoChevalierflodolo
authored andcommitted
Highlight special characters, support comparison and onestring view
Fixes #761 Make special char highlighting consistent accross views + highlight source strings
1 parent 0732efa commit 719d5e9

10 files changed

Lines changed: 69 additions & 56 deletions

File tree

app/classes/Transvision/ShowResults.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -215,25 +215,6 @@ public static function formatEntity($entity, $highlight = false)
215215
return $repo . '<span class="superset">&nbsp;&bull;&nbsp;</span>' . $path . '<br>' . $entity;
216216
}
217217

218-
/**
219-
* Highlight specific elements in the string.
220-
*
221-
* @param string $string Source text
222-
* @return string Same string with specific sub-strings in <span>
223-
* elements for styling with CSS
224-
*/
225-
public static function highlight($string)
226-
{
227-
$replacements = [
228-
' ' => '<span class="highlight-space" title="White space"> </span>',
229-
' ' => '<span class="highlight-red" title="Unicode non-breaking space"> </span>',
230-
'' => '<span class="highlight-gray" title="Real ellipsis">…</span>',
231-
'&hellip;' => '<span class="highlight-red" title="HTML ellipsis">…</span>',
232-
];
233-
234-
return Strings::multipleStringReplace($replacements, $string);
235-
}
236-
237218
/**
238219
* Html table of search results used by the main view (needs a lot of refactoring)
239220
*
@@ -328,20 +309,15 @@ public static function resultsTable($search_object, $search_results, $page)
328309
$target_string = htmlspecialchars($target_string);
329310
$source_string = Utils::highlightString($source_string);
330311
$target_string = Utils::highlightString($target_string);
312+
$source_string = Strings::highlightSpecial($source_string);
313+
$target_string = Strings::highlightSpecial($target_string);
331314

332315
if ($extra_locale) {
333316
$target_string2 = htmlspecialchars($target_string2);
334317
$target_string2 = Utils::highlightString($target_string2);
318+
$target_string2 = Strings::highlightSpecial($target_string2);
335319
}
336320

337-
$replacements = [
338-
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // Nbsp highlight
339-
'' => '<span class="highlight-red" title="Thin space"> </span>', // Thin space highlight
340-
'' => '<span class="highlight-gray">…</span>', // Right ellipsis highlight
341-
'&hellip;' => '<span class="highlight-gray">…</span>', // Right ellipsis highlight
342-
];
343-
344-
$target_string = Strings::multipleStringReplace($replacements, $target_string);
345321
$clipboard_target_string = 'clip_' . md5($target_string);
346322
$clipboard_target_string2 = 'clip_' . md5($target_string2);
347323

app/classes/Transvision/Strings.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ public static function multipleStringReplace($replacements, $string)
102102
return str_replace(array_keys($replacements), $replacements, $string);
103103
}
104104

105+
/**
106+
* Highlight special characters in the string
107+
*
108+
* @param string $string Source text
109+
* @param boolean $exclude_whitespaces Optional param to specify if we need
110+
* to highlight white spaces. White
111+
* spaces are not highlighted by default.
112+
* @return string Same string with specific sub-strings in <span>
113+
* elements for styling with CSS
114+
*/
115+
public static function highlightSpecial($string, $exclude_whitespaces = true)
116+
{
117+
$replacements = [
118+
' ' => '<span class="highlight-space" title="White space"> </span>',
119+
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>',
120+
'' => '<span class="highlight-red" title="Narrow no-break space"> </span>',
121+
'' => '<span class="highlight-gray" title="Real ellipsis">…</span>',
122+
'&hellip;' => '<span class="highlight-red" title="HTML ellipsis">…</span>',
123+
];
124+
125+
if ($exclude_whitespaces) {
126+
unset($replacements[' ']);
127+
}
128+
129+
return self::multipleStringReplace($replacements, $string);
130+
}
131+
105132
/**
106133
* Get multibyte UTF-8 string length, html tags stripped
107134
*

app/models/channelcomparison.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,25 @@
4545

4646
/*
4747
Find new locale strings added between repositories, store them with the
48-
reference string.
48+
reference string and highlight special characters.
4949
*/
5050
$added_strings = array_diff_key($strings[$chan1], $strings[$chan2]);
5151
$new_strings = [];
5252
$en_US_strings_chan1 = Utils::getRepoStrings('en-US', $chan1);
53+
5354
foreach ($added_strings as $string_id => $translation) {
5455
$reference_string = isset($en_US_strings_chan1[$string_id])
5556
? $en_US_strings_chan1[$string_id]
56-
: '@N/A@';
57+
: '@@missing@@';
58+
5759
$new_strings[$string_id] = [
58-
'reference' => $reference_string,
59-
'translation' => $translation,
60+
'reference' => Utils::secureText($reference_string),
61+
'translation' => Strings::highlightSpecial(Utils::secureText($translation)),
6062
];
6163
}
64+
65+
// Highlight special characters in common strings
66+
foreach ($common_strings as $key => &$value) {
67+
$common_strings[$key] = Strings::highlightSpecial(Utils::secureText($value));
68+
$strings[$chan2][$key] = Strings::highlightSpecial(Utils::secureText($strings[$chan2][$key]));
69+
}

app/views/channelcomparison.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
<?php foreach ($common_strings as $key => $value) : ?>
6767
<tr>
6868
<?=$td('Key', ShowResults::formatEntity($key))?>
69-
<?=$td($chan1, Utils::secureText($value))?>
70-
<?=$td($chan2, Utils::secureText($strings[$chan2][$key]))?>
69+
<?=$td($chan1, $value)?>
70+
<?=$td($chan2, $strings[$chan2][$key])?>
7171
</tr>
7272
<?php endforeach; ?>
7373
</tbody>
@@ -91,12 +91,12 @@
9191
<?php foreach ($new_strings as $string_id => $string_values): ?>
9292
<tr>
9393
<?=$td('Entity', ShowResults::formatEntity($string_id))?>
94-
<?php if ($new_strings[$string_id]['reference'] != '@N/A@'): ?>
95-
<?=$td('en-US', Utils::secureText($string_values['reference']))?>
94+
<?php if ($new_strings[$string_id]['reference'] != '@@missing@@'): ?>
95+
<?=$td('en-US', $string_values['reference'])?>
9696
<?php else: ?>
97-
<?=$td('en-US', '<em class="error">(not available)</em>')?>
97+
<?=$td('en-US', '<em class="error">Warning: Missing string</em>')?>
9898
<?php endif; ?>
99-
<?=$td($locale, Utils::secureText($string_values['translation']))?>
99+
<?=$td($locale, $string_values['translation'])?>
100100
</tr>
101101
<?php endforeach; ?>
102102
<tbody>

app/views/consistency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
echo '<a href="' . $search_link . '" title="Search for this string">' . Utils::secureText($data['source']) . "</a></td>\n";
6666
echo '<td>';
6767
foreach ($data['target'] as $target) {
68-
echo '<div class="inconsistent_translation highlight-specialchars">' . ShowResults::highlight(Utils::secureText($target)) . "</div>\n";
68+
echo '<div class="inconsistent_translation highlight-specialchars">' . Strings::highlightSpecial(Utils::secureText($target), false) . "</div>\n";
6969
}
7070
echo "</td>\n</tr>\n";
7171
}

app/views/onestring.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
if (! $translation) {
3737
echo " <td><em class='error'>Warning: Empty string</em></td><td></td>\n";
3838
} else {
39-
echo " <td lang='#{$locale}' {$rtl_support} >" . Utils::secureText($translation) . "</td>\n" .
39+
echo " <td lang='#{$locale}' {$rtl_support} >" . Strings::highlightSpecial(Utils::secureText($translation)) . "</td>\n" .
4040
" <td><a class='onestring_search' href='{$search_link}' title='Search for the entity in this locale'>🔍</a></td>\n";
4141
}
4242

app/views/results_entities.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
$bz_target_string = $target_string = isset($tmx_target[$entity])
3232
? Utils::secureText($tmx_target[$entity])
3333
: '@@missing@@';
34-
// Highlight non-breaking spaces only after strings have been escaped
35-
$target_string = str_replace(' ', '<span class="highlight-gray"> </span>', $target_string);
36-
37-
$source_string = Utils::secureText($tmx_source[$entity]);
34+
// Highlight special characters only after strings have been escaped
35+
$target_string = Strings::highlightSpecial($target_string);
36+
$source_string = Strings::highlightSpecial(Utils::secureText($tmx_source[$entity]));
3837

3938
$clipboard_target_string = 'clip_' . md5($target_string);
4039

@@ -46,8 +45,8 @@
4645
$bz_target_string2 = $target_string2 = isset($tmx_target2[$entity])
4746
? Utils::secureText($tmx_target2[$entity])
4847
: '';
49-
// Highlight non-breaking spaces only after strings have been escaped
50-
$target_string2 = str_replace(' ', '<span class="highlight-gray"> </span>', $target_string2);
48+
// Highlight special characters only after strings have been escaped
49+
$target_string2 = Strings::highlightSpecial($target_string2);
5150

5251
$clipboard_target_string2 = 'clip_' . md5($target_string2);
5352

tests/units/Transvision/ShowResults.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ public function testGetTMXResults($a, $b, $c)
3838
->isEqualTo($c);
3939
}
4040

41-
public function testHighlight()
42-
{
43-
$obj = new _ShowResults();
44-
$this
45-
->string($obj->highlight('Foo is bar ; Bar is Foo…'))
46-
->isEqualTo('Foo<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>bar<span class="highlight-red" title="Unicode non-breaking space"> </span>;<span class="highlight-red" title="Unicode non-breaking space"> </span>Bar<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>Foo<span class="highlight-gray" title="Real ellipsis">…</span>');
47-
}
48-
4941
public function getTranslationMemoryResultsDP()
5042
{
5143
include TMX . 'en-US/cache_en-US_central.php';

tests/units/Transvision/Strings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ public function testmultipleStringReplace($a, $b, $c)
118118
->isEqualTo($c);
119119
}
120120

121+
public function testHighlightSpecial()
122+
{
123+
$obj = new _Strings();
124+
$this
125+
->string($obj->highlightSpecial('Foo is bar ; Bar is Foo…'))
126+
->isEqualTo('Foo is bar<span class="highlight-gray" title="Non breakable space"> </span>;<span class="highlight-gray" title="Non breakable space"> </span>Bar is Foo<span class="highlight-gray" title="Real ellipsis">…</span>');
127+
$this
128+
->string($obj->highlightSpecial('Foo is bar ; Bar is Foo…', false))
129+
->isEqualTo('Foo<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>bar<span class="highlight-gray" title="Non breakable space"> </span>;<span class="highlight-gray" title="Non breakable space"> </span>Bar<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>Foo<span class="highlight-gray" title="Real ellipsis">…</span>');
130+
}
131+
121132
public function getLengthDP()
122133
{
123134
return [

web/style/transvision.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,18 @@ table tr td.no_highligth {
474474
white-space: pre-wrap; /* That allows exposing contiguous white spaces */
475475
}
476476

477-
.highlight-specialchars .highlight-gray {
477+
.highlight-gray {
478478
background-color: rgba(0, 0, 0, 0.2);
479479
}
480480

481-
.highlight-specialchars .highlight-space {
481+
.highlight-space {
482482
background-color: rgba(0, 0, 0, 0.1);
483483
display: inline-block;
484484
height: 0.5em;
485485
vertical-align: middle;
486486
}
487487

488-
.highlight-specialchars .highlight-red {
488+
.highlight-red {
489489
background-color: rgba(255, 0, 0, 0.4);
490490
}
491491

0 commit comments

Comments
 (0)