Skip to content

Commit 27ef4ac

Browse files
authored
normalize newlines before doing the character count for sms (#407)
1 parent f05c8ab commit 27ef4ac

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

.github/actions/waffles/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ docopt==0.6.2
22
Flask==2.3.3
33
markupsafe==2.1.5
44
setuptools==78.1.1 # required for distutils in Python 3.12
5-
git+https://github.com/cds-snc/notifier-utils.git@53.2.20#egg=notifications-utils
5+
git+https://github.com/cds-snc/notifier-utils.git@53.2.21#egg=notifications-utils

notifications_utils/template.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,14 @@ def _encoded_content(self):
211211
When values are set, placeholders are already replaced via __str__. When no values are
212212
set, placeholder syntax is stripped before encoding so that placeholder names don't
213213
inflate the character count or skew Unicode detection.
214+
215+
normalise_newlines is applied in both paths so that CRLF sequences (\\r\\n) submitted by
216+
browsers are counted as a single newline unit, matching what is actually transmitted.
214217
"""
215218
if self._values:
216219
# we always want to call SMSMessageTemplate.__str__ regardless of subclass, to avoid any html formatting
217220
return SMSMessageTemplate.__str__(self)
218-
return sms_encode(add_prefix(Field.placeholder_pattern.sub("", self.content.strip()), self.prefix))
221+
return normalise_newlines(sms_encode(add_prefix(Field.placeholder_pattern.sub("", self.content.strip()), self.prefix)))
219222

220223
@property
221224
def content_count(self):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "notifications-utils"
3-
version = "53.2.20"
3+
version = "53.2.21"
44
description = "Shared python code for Notification - Provides logging utils etc."
55
authors = ["Canadian Digital Service"]
66
license = "MIT license"

tests/test_base_template.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,15 @@ def test_extracting_placeholders(template_content, template_subject, expected):
7878
# should be replaced with a ?
7979
("深", None, 1, 1),
8080
("'First line.\n", None, 12, 12),
81-
("\t\n\r", None, 0, 0),
82-
# variables do not count towards the character count for sms, since they will be replaced
81+
("\t\n\r", None, 0, 0), # CRLF newlines (\r\n) from browser form submissions must be normalised to \n
82+
# before counting, so each line break costs exactly 1 unit (not 2).
83+
("Hello\r\nWorld", None, 11, 11),
84+
(
85+
"Line1\r\nLine2\r\nLine3",
86+
None,
87+
17,
88+
17,
89+
), # variables do not count towards the character count for sms, since they will be replaced
8390
("((placeholder))", None, 0, 3),
8491
("((placeholder))", "Service name", 14, 17),
8592
("Foo", "((placeholder))", 20, 20), # placeholder doesn’t work in service name

0 commit comments

Comments
 (0)