Skip to content

Commit

Permalink
fix: fix leading space behavior on values
Browse files Browse the repository at this point in the history
  • Loading branch information
nbouvrette committed Oct 10, 2023
1 parent 12f600d commit d7b2ee5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions assets/tests/test-all-java-console-output
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ topic => '.properties file'
trailingNonBreakingSpace => 'this line will have a trailing non-breaking space '
valueWithEscapes => 'This is a newline
, a carriage return, a tab and a formfeed .'
valueWithLeadingSpaces => ' There are 3 leading spaces.'
website => 'https://en.wikipedia.org/'
welcome => 'Welcome to Wikipedia! '
Expand Down
2 changes: 2 additions & 0 deletions assets/tests/test-all.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ oddKey = This is line one and\\\
keyWitheven\\:this colon is not escaped
# White space characters are removed before each line, but trailing spaces aren't.
noWhiteSpace = The key will be "noWhiteSpace" without any whitespace.
# Values with leading spaces must be escaped for them to be preseved.
valueWithLeadingSpaces = \ There are 3 leading spaces.
# Make sure to add your spaces before your \ if you need them on the next line.
# In the following example, the value for "welcome" is "Welcome to Wikipedia! ".
welcome = Welcome to \
Expand Down
6 changes: 3 additions & 3 deletions src/escape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const escapeContent = (
): string =>
unescapedContent.replace(
new RegExp(`[\\s!#:=\\\\${escapeUnicode ? '\\u0000-\\u001F\\u007F-\\uFFFF' : ''}]`, 'g'),
(character) => {
(character, position) => {
switch (character) {
case ' ': {
// Escape space if required, or if it's the first character.
return escapeSpace ? '\\ ' : ' '
// Spaces.
return escapeSpace || position === 0 ? '\\ ' : ' '
}
case '\\': {
// Backslash.
Expand Down
6 changes: 3 additions & 3 deletions tests/properties-escape-unescape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('The property unicode value escaping', () => {
['こんにちは3=', 'こんにちは3\\='],
['こんにちは4\t', 'こんにちは4\\t'],
['こんにちは5 ', 'こんにちは5 '],
[' こんにちは6', ' こんにちは6'],
[' こんにちは6', '\\ こんにちは6'],
['#こんにちは7', '\\#こんにちは7'],
['!こんにちは8#', '\\!こんにちは8\\#'],
['こん にちは9', 'こん にちは9'],
Expand All @@ -71,11 +71,11 @@ describe('The property ISO-8859-1 compatible encoding value escaping', () => {
['foo3=', 'foo3\\='],
['foo4\t', 'foo4\\t'],
['foo5 ', 'foo5 '],
[' foo6', ' foo6'],
[' foo6', '\\ foo6'],
['#foo7', '\\#foo7'],
['!foo8#', '\\!foo8\\#'],
['fo o9', 'fo o9'],
[' fo o10 ', ' fo o10 '],
[' fo o10 ', '\\ fo o10 '],
['foo11\n', 'foo11\\n'],
['f\r\f\n\too12', 'f\\r\\f\\n\\too12'],
['\\foo13\\', '\\\\foo13\\\\'],
Expand Down

0 comments on commit d7b2ee5

Please sign in to comment.