Skip to content

Commit 84f1801

Browse files
author
Dimitris Rongotis
committed
wip2
1 parent 04e324f commit 84f1801

File tree

4 files changed

+87
-31
lines changed

4 files changed

+87
-31
lines changed

openformats/formats/android.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ class AndroidDumbXml(DumbXml):
1919
# def __init__(self, source, text_position=0):
2020
# super().__init__(source, text_position)
2121
# self.text_position = text_position
22-
22+
def _find_next_lt(self, start):
23+
in_cdata = False
24+
for ptr in six.moves.xrange(start, len(self.source)):
25+
candidate = self.source[ptr]
26+
if in_cdata:
27+
if (candidate == ']' and
28+
self.source[ptr:ptr + len("]]>")] == "]]>"):
29+
in_cdata = False
30+
else:
31+
if candidate == self.LESS_THAN:
32+
# Check against CDATA
33+
if self.source[ptr:ptr + len("<![CDATA[")] == "<![CDATA[":
34+
self._text_position=ptr+len("<![CDATA[")
35+
in_cdata = True
36+
else:
37+
return ptr
38+
# We reached the end of the string, lets return accordingly
39+
return len(self.source)
2340

2441
@property
2542
def content(self):
@@ -33,15 +50,10 @@ def content(self):
3350
return self.text
3451
if self.content_end is None:
3552
return None
36-
37-
pattern = re.compile(r'!\[CDATA')
38-
match = pattern.search(self._text)
39-
40-
if match:
41-
self._text_position = self._text_position+match.start()+match.end()
42-
return self.source[self.text_position:self.content_end-3]
43-
else:
44-
return self.source[self.text_position:self.content_end]
53+
result = self.source[self.text_position:self.content_end]
54+
if result.endswith("]]>"):
55+
result = self.source[self.text_position:self.content_end-len("]]>")]
56+
return result
4557

4658

4759
class AndroidHandler(Handler):
@@ -159,16 +171,10 @@ def _handle_string(self, child):
159171
product,
160172
child
161173
)
162-
pattern = re.compile(r'!\[CDATA')
163-
match = pattern.search(child.content)
174+
164175
if string is not None:
165176
# <string>My Text</string>
166177
# ^
167-
168-
169-
print(string.string)
170-
from ipdb import set_trace
171-
#set_trace()
172178
self.transcriber.copy_until(child.text_position)
173179
self.transcriber.add(string.template_replacement)
174180
# <string>My Text</string>
@@ -399,12 +405,11 @@ def _get_child_attributes(self, child):
399405
def compile(self, template, stringset, is_source=True, language_info=None,
400406
**kwargs):
401407
resources_tag_position = template.index(self.PARSE_START)
402-
403408
self.transcriber = Transcriber(template[resources_tag_position:])
404409
source = self.transcriber.source
405410

406411
parsed = AndroidDumbXml(source)
407-
412+
408413
# Check against 'tools:locale' attribute
409414
if language_info is not None and 'tools:locale' in parsed.attrib:
410415
value_position, value = next((
@@ -464,10 +469,23 @@ def _compile_string(self, child):
464469
It will compile the tag if matching string exists. Otherwise it will
465470
skip it.
466471
"""
472+
473+
def _search_for_cdata(_destination):
474+
result = False
475+
if len(_destination) > 1:
476+
_string = _destination[1]
477+
pattern = re.compile(r'!\[CDATA')
478+
match = pattern.search(_string)
479+
if match:
480+
result= True
481+
return result
467482
if self._should_compile(child):
483+
484+
468485
self.transcriber.copy_until(child.text_position)
469486
self.transcriber.add(self.next_string.string)
470-
self.transcriber.skip_until(child.content_end)
487+
ret = _search_for_cdata(self.transcriber.__dict__['destination'])
488+
self.transcriber.skip_until(child.content_end if not ret else child.content_end-len("]]>"))
471489
self.transcriber.copy_until(child.tail_position)
472490
self.transcriber.mark_section_start()
473491
self.transcriber.copy_until(child.end)

openformats/tests/formats/android/files/1_en_cdata.xml

Whitespace-only changes.

openformats/tests/formats/android/test_android_unescaped.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212
from openformats.strings import OpenString
1313

1414

15-
class AndroidUnescapedTestCase( unittest.TestCase):
16-
handler = AndroidUnescapedHandler()
15+
class AndroidUnescapedTestCase(CommonFormatTestMixin, unittest.TestCase):
1716
HANDLER_CLASS = AndroidUnescapedHandler
1817
TESTFILE_BASE = "openformats/tests/formats/android/files"
1918

20-
# def setUp(self):
21-
# super(AndroidUnescapedTestCase, self).setUp()
22-
# self.handler = AndroidUnescapedHandler()
19+
def setUp(self):
20+
super(AndroidUnescapedTestCase, self).setUp()
21+
self.handler = AndroidUnescapedHandler()
2322

2423
def test_string(self):
2524
self.maxDiff = None
2625
random_key = generate_random_string()
27-
uploaded_string = """<![CDATA[<b>Before using Onshape, you must agree to the Terms of Use and Privacy Policy. See Terms of Use below and link to the Privacy Policy <a href = \"https://www.onshape.com/privacy-center/privacy-policy\">here</a>.</b>]]>"""
26+
uploaded_string = """&amp; &lt; &gt; \\' \\n \\t \\@ \\? \\" <xliff:g id="1">%1$s</xliff:g> \@ \?"""
2827
uploaded_openstring = OpenString(random_key, uploaded_string, order=0)
2928
uploaded_hash = uploaded_openstring.template_replacement
3029

@@ -35,12 +34,8 @@ def test_string(self):
3534
"""
3635
source = source_python_template.format(key=random_key, string=uploaded_string)
3736
template, stringset = self.handler.parse(source)
38-
from icecream import ic
39-
ic(template)
40-
from ipdb import set_trace
41-
#set_trace()
4237
compiled = self.handler.compile(template, [uploaded_openstring])
43-
ic(compiled)
38+
4439
self.assertEqual(
4540
template,
4641
source_python_template.format(key=random_key, string=uploaded_hash),
@@ -49,6 +44,46 @@ def test_string(self):
4944
self.assertEqual(stringset[0].__dict__, uploaded_openstring.__dict__)
5045
self.assertEqual(compiled, source)
5146

47+
48+
def test_cdata_string(self):
49+
self.maxDiff = None
50+
random_key = generate_random_string()
51+
uploaded_string = """<![CDATA[<b>test</b>]]>"""
52+
saved_string = """<b>test</b>"""
53+
uploaded_openstring = OpenString(random_key, saved_string, order=0)
54+
uploaded_hash = uploaded_openstring.template_replacement
55+
56+
source_python_template = """
57+
<resources>
58+
<string name="{key}">{string}</string>
59+
</resources>
60+
"""
61+
cdata_source_python_template = """
62+
<resources>
63+
<string name="{key}"><![CDATA[{string}]]></string>
64+
</resources>
65+
"""
66+
67+
source = source_python_template.format(key=random_key, string=uploaded_string)
68+
template, stringset = self.handler.parse(source)
69+
70+
compiled = self.handler.compile(template, [uploaded_openstring])
71+
self.assertEqual(
72+
template,
73+
cdata_source_python_template.format(key=random_key, string=uploaded_hash),
74+
)
75+
self.assertEqual(len(stringset), 1)
76+
self.assertEqual(stringset[0].__dict__, uploaded_openstring.__dict__)
77+
self.assertEqual(compiled, source)
78+
79+
def test_escape(self):
80+
rich = '&>"\n\t@? <xliff:g id="1">%1$s &</xliff:g> @ ?'
81+
raw = '&amp;&gt;\\"\\n\\t\\@\\? <xliff:g id="1">%1$s &</xliff:g> \\@ \\?'
82+
83+
self.assertEqual(
84+
AndroidUnescapedHandler.escape(rich),
85+
raw,
86+
)
5287
def test_escape(self):
5388
rich = '&>"\n\t@? <xliff:g id="1">%1$s &</xliff:g> @ ?'
5489
raw = '&amp;&gt;\\"\\n\\t\\@\\? <xliff:g id="1">%1$s &</xliff:g> \\@ \\?'

openformats/tests/util_tests/test_xml.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def test_one_child(self):
122122
('<r><a>hello <![CDATA[</a>]]></a></r>',
123123
'<a>hello <![CDATA[</a>]]></a>', 32, 3, 'a', [], {}, 6,
124124
'hello <![CDATA[</a>]]>', 32, '', 32),
125+
# ('<resource><string name="onshape_edu_plan"><![CDATA[<b>Onshape Education Plan</b>]]></string></resource>',
126+
# '<a>hello <![CDATA[</a>]]></a>', 32, 3, 'a', [], {}, 6,
127+
# 'hello <![CDATA[</a>]]>', 32, '', 32),
125128
('<r><a>hello world</a> tail</r>', '<a>hello world</a> tail', 26,
126129
3, 'a', [], {}, 6, 'hello world', 21, ' tail', 26),
127130
('<r><a b="c">hello world</a> tail</r>',

0 commit comments

Comments
 (0)