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

Fix Android XML format special characters escape/ unescape #321

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions openformats/formats/android_unescaped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from openformats.formats.android import AndroidHandler


class AndroidUnescapedHandler(AndroidHandler):
@staticmethod
def escape(string):
string = AndroidHandler.escape(string)
return (
string.replace("&", "&")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\n", "\\n")
.replace("\t", "\\t")
.replace("@", "\\@")
.replace("?", "\\?")
)

@staticmethod
def unescape(string):
string = AndroidHandler.unescape(string)
return (
string.replace("\\?", "?")
.replace("\\@", "@")
.replace("\\t", "\t")
.replace("\\n", "\n")
.replace("&gt;", ">")
.replace("&lt;", "<")
.replace("&amp;", "&")
)
3 changes: 3 additions & 0 deletions openformats/tests/formats/android/files/1_el.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<!-- Testing simple string -->
<string name="a_string">el:Simple string</string>

<!-- Testing string with special characters -->
<string name="b_string">el:Simple string &amp; \"with\" \'special characters\'</string>

<!-- Testing with product attribute -->
<string name="a_string" product="a_product">el:Simple string with product</string>

Expand Down
3 changes: 3 additions & 0 deletions openformats/tests/formats/android/files/1_en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<!-- Testing simple string -->
<string name="a_string">Simple string</string>

<!-- Testing string with special characters -->
<string name="b_string">Simple string &amp; \"with\" \'special characters\'</string>

<!-- Testing with product attribute -->
<string name="a_string" product="a_product">Simple string with product</string>

Expand Down
3 changes: 3 additions & 0 deletions openformats/tests/formats/android/files/1_tpl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<!-- Testing simple string -->
<string name="a_string">ac2562b9f825de04b7ae4736274b1e5f_tr</string>

<!-- Testing string with special characters -->
<string name="b_string">2cb7c4a9823431f0a9a4efc70603b465_tr</string>

<!-- Testing with product attribute -->
<string name="a_string" product="a_product">160db9c24b122af9e0f4218ad732d0ce_tr</string>

Expand Down
60 changes: 60 additions & 0 deletions openformats/tests/formats/android/test_android_unescaped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest
from openformats.formats.android_unescaped import AndroidUnescapedHandler
from openformats.tests.formats.common import CommonFormatTestMixin
from openformats.tests.utils.strings import (
generate_random_string,
strip_leading_spaces,
bytes_to_string,
)

from openformats.strings import OpenString


class AndroidUnescapedTestCase(CommonFormatTestMixin, unittest.TestCase):
HANDLER_CLASS = AndroidUnescapedHandler
TESTFILE_BASE = "openformats/tests/formats/android/files"

def setUp(self):
super(AndroidUnescapedTestCase, self).setUp()
self.handler = AndroidUnescapedHandler()

def test_string(self):
random_key = generate_random_string()
random_string = generate_random_string()
random_openstring = OpenString(random_key, random_string, order=0)
random_hash = random_openstring.template_replacement

source_python_template = """
<resources>
<string name="{key}">{string}</string>
</resources>
"""
source = source_python_template.format(key=random_key, string=random_string)

template, stringset = self.handler.parse(source)
compiled = self.handler.compile(template, [random_openstring])

self.assertEqual(
template, source_python_template.format(key=random_key, string=random_hash)
)
self.assertEqual(len(stringset), 1)
self.assertEqual(stringset[0].__dict__, random_openstring.__dict__)
self.assertEqual(compiled, source)

def test_escape(self):
rich = "&<>'\n\t@?" + '"'
raw = "&amp;&lt;&gt;\\'\\n\\t\\@\\?" + '\\"'

self.assertEqual(
AndroidUnescapedHandler.escape(rich),
raw,
)

def test_unescape(self):
rich = "&<>'\n\t@?" + '"'
raw = "&amp;&lt;&gt;\\'\\n\\t\\@\\?" + '\\"'

self.assertEqual(
AndroidUnescapedHandler.unescape(raw),
rich,
)
Loading