Skip to content

Commit

Permalink
Put quotes around path components which have special characters
Browse files Browse the repository at this point in the history
Someday we might want to support this properly in wrap()/unwrap() but
for now it's only required on a component that is always at the end of
the path.
  • Loading branch information
kaedroho committed Feb 19, 2021
1 parent 6c87175 commit 61297cd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
11 changes: 9 additions & 2 deletions wagtail_localize/segments/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
from ..strings import extract_strings


def quote_path_component(text):
"""
Puts quotes around the path compoenents, and escapes any special characters.
"""
return "'" + text.replace("\\", "\\\\") .replace("'", "\\'") + "'"


class StreamFieldSegmentExtractor:
def __init__(self, field, include_overridables=False):
self.field = field
Expand Down Expand Up @@ -55,7 +62,7 @@ def handle_block(self, block_type, block_value):
StringSegmentValue("", string, attrs=attrs)
for string, attrs in strings
] + [
OverridableSegmentValue(href, href)
OverridableSegmentValue(quote_path_component(href), href)
for href in hrefs
]
return ret
Expand Down Expand Up @@ -153,7 +160,7 @@ def extract_segments(instance):
StringSegmentValue("", string, attrs=attrs)
for string, attrs in strings
] + [
OverridableSegmentValue(href, href)
OverridableSegmentValue(quote_path_component(href), href)
for href in hrefs
]

Expand Down
12 changes: 11 additions & 1 deletion wagtail_localize/segments/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@
from .types import OverridableSegmentValue, StringSegmentValue


def unquote_path_component(text):
"""
Removes quotes around a quoted path component, and unescapes any special characters.
"""
if text[0] != "'" or text[-1] != "'":
raise ValueError("value must be a quoted string")

return text[1:-1].replace("\\'", "'").replace("\\\\", "\\")


def organise_template_segments(segments):
# The first segment is always the template, followed by the texts in order of their position

segments.sort(key=lambda segment: segment.order)
template = segments[0]
xrefs = {
segment.path: segment.data
unquote_path_component(segment.path): segment.data
for segment in segments
if isinstance(segment, OverridableSegmentValue) and segment.data
}
Expand Down
2 changes: 1 addition & 1 deletion wagtail_localize/segments/tests/test_segment_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def make_test_page(**kwargs):
}
),
OverridableSegmentValue(
"http://example.com",
"'http://example.com'",
"http://example.com"
)
]
Expand Down
6 changes: 3 additions & 3 deletions wagtail_localize/tests/test_edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_edit_page_translation(self):
self.assertEqual(
[(segment['contentPath'], segment['value']) for segment in props['segments'] if segment['type'] == 'synchronised_value'],
[
('test_richtextfield.http://example.com', 'http://example.com'),
("test_richtextfield.'http://example.com'", 'http://example.com'),
('test_synchronized_emailfield', '[email protected]'),
]
)
Expand All @@ -200,7 +200,7 @@ def test_edit_page_translation(self):
# Check synchronised value
synchronised_value_segment = props['segments'][9]
self.assertEqual(synchronised_value_segment['type'], 'synchronised_value')
self.assertEqual(synchronised_value_segment['contentPath'], 'test_richtextfield.http://example.com')
self.assertEqual(synchronised_value_segment['contentPath'], "test_richtextfield.'http://example.com'")
self.assertEqual(synchronised_value_segment['location'], {'blockId': None, 'field': 'Test richtextfield', 'fieldHelpText': '', 'order': 6, 'subField': None, 'tab': 'content', 'widget': {'type': 'text'}})
self.assertEqual(synchronised_value_segment['value'], 'http://example.com')

Expand Down Expand Up @@ -360,7 +360,7 @@ def test_override_types(self):
self.assertEqual(
[(segment['contentPath'], segment['location']['widget'], segment['value']) for segment in props['segments'] if segment['type'] == 'synchronised_value'],
[
('test_richtextfield.http://example.com', {'type': 'text'}, 'http://example.com'),
("test_richtextfield.'http://example.com'", {'type': 'text'}, 'http://example.com'),
(f'test_streamfield.{url_block_id}', {'type': 'text'}, "https://wagtail.io/"),
(f'test_streamfield.{page_block_id}', {'type': 'page_chooser', 'allowed_page_types': ['wagtailcore.page']}, self.page.id),
(f'test_streamfield.{image_block_id}', {'type': 'image_chooser'}, self.page.test_synchronized_image.id),
Expand Down

0 comments on commit 61297cd

Please sign in to comment.