Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
19e7db58
Unverified
Commit
19e7db58
authored
Apr 01, 2020
by
Shi-Hao Hong
Committed by
GitHub
Apr 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[gen_l10n] Escape special JSON characters in generateString utility function (#53605)
parent
a94e647d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
42 deletions
+72
-42
localizations_utils.dart
dev/tools/localization/localizations_utils.dart
+21
-16
localizations_utils_test.dart
dev/tools/test/localization/localizations_utils_test.dart
+20
-4
gen_l10n_test.dart
...s/flutter_tools/test/integration.shard/gen_l10n_test.dart
+22
-21
gen_l10n_project.dart
...ls/test/integration.shard/test_data/gen_l10n_project.dart
+9
-1
No files found.
dev/tools/localization/localizations_utils.dart
View file @
19e7db58
...
...
@@ -373,36 +373,36 @@ class $classNamePrefix$camelCaseName extends $superClass {''';
/// foo "bar" => 'foo "bar"'
/// foo 'bar' => "foo 'bar'"
/// foo 'bar' "baz" => '''foo 'bar' "baz"'''
/// foo\bar => 'foo\\bar'
/// foo\nbar => 'foo\\\\nbar'
/// ```
///
/// When [shouldEscapeDollar] is set to true, the
/// result avoids character escaping, with the
/// exception of the dollar sign:
/// This function is used by tools that take in a JSON-formatted file to
/// generate Dart code. For this reason, characters with special meaning
/// in JSON files. For example, the backspace character (\b) have to be
/// properly escaped by this function so that the generated Dart code
/// correctly represents this character:
/// ```
/// foo\bar => 'foo\\bar'
/// foo\nbar => 'foo\\nbar'
/// foo\\nbar => 'foo\\\\nbar'
/// foo\\bar => 'foo\\\\bar'
/// foo\ bar => 'foo\\ bar'
/// ```
///
/// When [shouldEscapeDollar] is set to true, the '$' characters in the
/// input will be replaced by '$' in the returned string:
/// ```
/// foo$bar = 'foo\$bar'
/// ```
///
/// When [shouldEscapeDollar] is set to false, the
/// result tries to avoid character escaping:
///
/// When [shouldEscapeDollar] is set to false, '$' will be replaced
/// by '\$' in the returned string:
/// ```
/// foo$bar => 'foo\\\$bar'
/// ```
///
/// [shouldEscapeDollar] is true by default.
///
/// Strings with newlines are not supported.
String
generateString
(
String
value
,
{
bool
escapeDollar
=
true
})
{
assert
(
escapeDollar
!=
null
);
assert
(
!
value
.
contains
(
'
\n
'
),
'Since it is assumed that the input string comes '
'from a json/arb file source, messages cannot '
'contain newlines.'
);
const
String
backslash
=
'__BACKSLASH__'
;
assert
(
...
...
@@ -419,6 +419,11 @@ String generateString(String value, { bool escapeDollar = true }) {
value
=
value
.
replaceAll
(
"'"
,
"
\\
'"
)
.
replaceAll
(
'"'
,
'
\\
"'
)
.
replaceAll
(
'
\n
'
,
'
\\
n'
)
.
replaceAll
(
'
\
f'
,
'
\\
f'
)
.
replaceAll
(
'
\t
'
,
'
\\
t'
)
.
replaceAll
(
'
\r
'
,
'
\\
r'
)
.
replaceAll
(
'
\
b'
,
'
\\
b'
)
.
replaceAll
(
backslash
,
'
\\\\
'
);
return
"'
$value
'"
;
...
...
dev/tools/test/localization/localizations_utils_test.dart
View file @
19e7db58
...
...
@@ -45,15 +45,31 @@ void main() {
});
test
(
'handles backslash'
,
()
{
expect
(
generateString
(
r'ab\c'
),
"'ab
\\
\\
c'"
);
expect
(
generateString
(
r'ab\c'
),
r"'ab
\\c'"
);
});
test
(
'handles backslash followed by "n" character'
,
()
{
expect
(
generateString
(
r'ab\nc'
),
"'ab
\\
\\
nc'"
);
expect
(
generateString
(
r'ab\nc'
),
r"'ab
\\nc'"
);
});
test
(
'does not support multiline strings'
,
()
{
expect
(()
=>
generateString
(
'ab
\n
c'
),
throwsA
(
isA
<
AssertionError
>()));
test
(
'supports newline escaping'
,
()
{
expect
(
generateString
(
'ab
\n
c'
),
"'ab
\\
nc'"
);
});
test
(
'supports form feed escaping'
,
()
{
expect
(
generateString
(
'ab
\
fc'
),
"'ab
\\
fc'"
);
});
test
(
'supports tab escaping'
,
()
{
expect
(
generateString
(
'ab
\t
c'
),
"'ab
\\
tc'"
);
});
test
(
'supports carriage return escaping'
,
()
{
expect
(
generateString
(
'ab
\r
c'
),
"'ab
\\
rc'"
);
});
test
(
'supports backspace escaping'
,
()
{
expect
(
generateString
(
'ab
\
bc'
),
"'ab
\\
bc'"
);
});
});
}
packages/flutter_tools/test/integration.shard/gen_l10n_test.dart
View file @
19e7db58
...
...
@@ -88,28 +88,29 @@ void main() {
'#l10n 9 (Hello GB fallback World)
\n
'
'#l10n 10 (--- General formatting tests ---)
\n
'
'#l10n 11 (Hello World)
\n
'
'#l10n 12 (Hello World)
\n
'
'#l10n 12 (Hello
_NEWLINE_
World)
\n
'
'#l10n 13 (Hello World)
\n
'
'#l10n 14 (Hello World on Friday, January 1, 1960)
\n
'
'#l10n 15 (Hello world argument on 1/1/1960 at 00:00)
\n
'
'#l10n 16 (Hello World from 1960 to 2020)
\n
'
'#l10n 17 (Hello for 123)
\n
'
'#l10n 18 (Hello for price USD123.00)
\n
'
'#l10n 19 (Hello)
\n
'
'#l10n 20 (Hello World)
\n
'
'#l10n 21 (Hello two worlds)
\n
'
'#l10n 22 (Hello)
\n
'
'#l10n 23 (Hello new World)
\n
'
'#l10n 24 (Hello two new worlds)
\n
'
'#l10n 25 (Hello on Friday, January 1, 1960)
\n
'
'#l10n 26 (Hello World, on Friday, January 1, 1960)
\n
'
'#l10n 27 (Hello two worlds, on Friday, January 1, 1960)
\n
'
'#l10n 28 (Hello other 0 worlds, with a total of 100 citizens)
\n
'
'#l10n 29 (Hello World of 101 citizens)
\n
'
'#l10n 30 (Hello two worlds with 102 total citizens)
\n
'
'#l10n 31 ([Hello] -World- #123#)
\n
'
'#l10n 32 (Flutter
\'
s amazing!)
\n
'
'#l10n 33 (Flutter is "amazing"!)
\n
'
'#l10n 14 (Hello World)
\n
'
'#l10n 15 (Hello World on Friday, January 1, 1960)
\n
'
'#l10n 16 (Hello world argument on 1/1/1960 at 00:00)
\n
'
'#l10n 17 (Hello World from 1960 to 2020)
\n
'
'#l10n 18 (Hello for 123)
\n
'
'#l10n 19 (Hello for price USD123.00)
\n
'
'#l10n 20 (Hello)
\n
'
'#l10n 21 (Hello World)
\n
'
'#l10n 22 (Hello two worlds)
\n
'
'#l10n 23 (Hello)
\n
'
'#l10n 24 (Hello new World)
\n
'
'#l10n 25 (Hello two new worlds)
\n
'
'#l10n 26 (Hello on Friday, January 1, 1960)
\n
'
'#l10n 27 (Hello World, on Friday, January 1, 1960)
\n
'
'#l10n 28 (Hello two worlds, on Friday, January 1, 1960)
\n
'
'#l10n 29 (Hello other 0 worlds, with a total of 100 citizens)
\n
'
'#l10n 30 (Hello World of 101 citizens)
\n
'
'#l10n 31 (Hello two worlds with 102 total citizens)
\n
'
'#l10n 32 ([Hello] -World- #123#)
\n
'
'#l10n 33 (Flutter
\'
s amazing!)
\n
'
'#l10n 34 (Flutter is "amazing"!)
\n
'
'#l10n END
\n
'
);
});
...
...
packages/flutter_tools/test/integration.shard/test_data/gen_l10n_project.dart
View file @
19e7db58
...
...
@@ -124,6 +124,7 @@ class Home extends StatelessWidget {
final AppLocalizations localizations = AppLocalizations.of(context);
results.addAll(<String>[
'
$
{
localizations
.
helloWorld
}
',
'
$
{
localizations
.
helloNewlineWorld
}
',
'
$
{
localizations
.
hello
(
"World"
)}
',
'
$
{
localizations
.
greeting
(
"Hello"
,
"World"
)}
',
'
$
{
localizations
.
helloWorldOn
(
DateTime
(
1960
))}
',
...
...
@@ -154,7 +155,9 @@ class Home extends StatelessWidget {
try {
int n = 0;
for (final String result in results) {
print('
#l10n
$n
(
$result
)
');
// Newline character replacement is necessary because
// the stream breaks up stdout by new lines.
print('
#l10n
$n
(
$
{
result
.
replaceAll
(
'
\n
'
,
'_NEWLINE_'
)})
');
n += 1;
}
}
...
...
@@ -188,6 +191,11 @@ void main() {
"description": "The conventional newborn programmer greeting"
},
"helloNewlineWorld": "Hello
\n
World",
"@helloNewlineWorld": {
"description": "The JSON decoder should convert backslash-n to a newline character in the generated Dart string."
},
"hello": "Hello {world}",
"@hello": {
"description": "A message with a single parameter",
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment