Skip to content

Commit

Permalink
Fix string extraction from nullable RichTextField
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Sep 23, 2022
1 parent 7c8b75d commit 7e6de4d
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 176 deletions.
4 changes: 3 additions & 1 deletion wagtail_localize/segments/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ def extract_segments(instance):
)

elif isinstance(field, RichTextField):
value = field.value_from_object(instance)

if is_translatable:
template, strings = extract_strings(field.value_from_object(instance))
template, strings = extract_strings(value)

# Find all unique href values
hrefs = set()
Expand Down
4 changes: 4 additions & 0 deletions wagtail_localize/segments/tests/test_segment_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def test_richtextfield(self):
[segment.wrap("test_richtextfield") for segment in RICH_TEXT_TEST_OUTPUT],
)

def test_null_richtextfield(self):
page = make_test_page(test_null_richtextfield=None)
self.assertEqual(extract_segments(page), [])

def test_snippet(self):
test_snippet = TestSnippet.objects.create(field="Test content")
page = make_test_page(test_snippet=test_snippet)
Expand Down
3 changes: 3 additions & 0 deletions wagtail_localize/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ def extract_strings(html):
tuple[str, list[tuple[StringValue, dict]]]: Returns a template string, and list 2-tuples containing a
StringValue and dict of HTML attribute
"""
if html is None:
html = ""

soup = BeautifulSoup(html, "html.parser")

def wrap(elements):
Expand Down
Loading

0 comments on commit 7e6de4d

Please sign in to comment.