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

WIP: fixing android escaping #133

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions lib/babelish/csv2android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def language_filepaths(language)

def process_value(row_value, default_value)
value = super(row_value, default_value)
value = escape_android_characters(value)

# if the value begins and ends with a quote we must leave them unescapted
if value.size > 4 && value[0, 2] == "\\\"" && value[value.size - 2, value.size] == "\\\""
value[0, 2] = "\""
Expand All @@ -28,6 +30,16 @@ def process_value(row_value, default_value)
value.to_utf8
end

# see https://developer.android.com/guide/topics/resources/string-resource#escaping_quotes
def escape_android_characters(value)
value.gsub!(/'/, "'" => '\\\'') # \' should be the result...
falkorichter marked this conversation as resolved.
Show resolved Hide resolved
value.gsub!(/&/, "&" => '&')
falkorichter marked this conversation as resolved.
Show resolved Hide resolved
value.gsub!(/</, "<" => '&lt;')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

value.gsub!(/\?/, "?" => '\?')
value.gsub!(/@/, "@" => '\@')
value
end

def get_row_format(row_key, row_value, comment = nil, indentation = 0)
entry = comment.to_s.empty? ? "" : "\n\t<!-- #{comment} -->\n"
entry + "\t<string name=\"#{row_key}\">#{row_value}</string>\n"
Expand Down
12 changes: 12 additions & 0 deletions test/babelish/test_csv2android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,16 @@ def test_converting_with_basename
# clean up
system("rm -rf ./values-en")
end

def test_android_character_escaping
converter = Babelish::CSV2Android.new('test/data/test_data.csv',
falkorichter marked this conversation as resolved.
Show resolved Hide resolved
{ "English" => "en" },
{ output_basename: "super_strings" })
falkorichter marked this conversation as resolved.
Show resolved Hide resolved
input_string = "? ' @ < &"

converted_string = converter.escape_android_characters(input_string)

assert_equal '\? \\\' \@ &lt; &amp;', converted_string

falkorichter marked this conversation as resolved.
Show resolved Hide resolved
end
end