From cd3eec561f463f0dabc1c8da5a421a5373690538 Mon Sep 17 00:00:00 2001 From: telday Date: Mon, 19 May 2025 11:31:12 -0600 Subject: [PATCH 1/2] Adds ability to include "." char in mime-type --- data_url/__init__.py | 2 +- test/test_url.py | 49 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/data_url/__init__.py b/data_url/__init__.py index ef7d006..a8617e8 100644 --- a/data_url/__init__.py +++ b/data_url/__init__.py @@ -2,7 +2,7 @@ import base64 DATA_URL_RE = re.compile( - r"data:(?P([\w-]+\/[\w+-]+(;[\w-]+\=[\w-]+)?)?)(?P;base64)?,(?P[\w\d.~%\=\/\+-]+)" + r"data:(?P([\w-]+\/[\w+\.-]+(;[\w-]+\=[\w-]+)?)?)(?P;base64)?,(?P[\w\d.~%\=\/\+-]+)" ) def construct_data_url(mime_type, base64_encoded, data): diff --git a/test/test_url.py b/test/test_url.py index 8675c58..f9635d5 100644 --- a/test/test_url.py +++ b/test/test_url.py @@ -31,6 +31,19 @@ def test_construct_data_url_without_mime_type(self): self.assertEqual(data, deconstructed_url.data) self.assertEqual(url, f"data:,{data}") + def test_construct_data_url_with_dot_mime_type(self): + mime_type = "image/some.png" + base64_encoded = False + data = str(uuid.uuid4()).strip() + url = construct_data_url(mime_type, base64_encoded, data) + + deconstructed_url = DataURL.from_url(url) + + self.assertEqual(mime_type, deconstructed_url.mime_type) + self.assertEqual(base64_encoded, deconstructed_url.is_base64_encoded) + self.assertEqual(data, deconstructed_url.data) + self.assertEqual(url, f"data:image/some.png,{data}") + def test_construct_data_url(self): mime_type = "text/plain" base64_encoded = False @@ -82,8 +95,42 @@ def test_string_with_empty_mimetype(self): self.base64_encoded = False self.data = str(uuid.uuid4()) self.raw_data = self.data - self.expected_url = f"data:,{self.data}" + self.expected_url = f"data:{self.mime_type},{self.data}" + self.url = DataURL.from_data(self.mime_type, self.base64_encoded, self.data) + + self.assertEqual(type(self.url.data), str) + self.run_assertions() + + def test_string_with_dot_mimetype(self): + self.mime_type = "image/some.png" + self.base64_encoded = False + self.data = str(uuid.uuid4()) + self.raw_data = self.data + self.expected_url = f"data:{self.mime_type},{self.data}" self.url = DataURL.from_data(self.mime_type, self.base64_encoded, self.data) + + self.assertEqual(type(self.url.data), str) + self.run_assertions() + + def test_string_with_plus_and_dot_mimetype(self): + self.mime_type = "image/some.png+other" + self.base64_encoded = True + self.raw_data = bytes(str(uuid.uuid4()), "UTF-8") + self.data = base64.b64encode(self.raw_data).decode("UTF-8") + self.expected_url = f"data:{self.mime_type};base64,{self.data}" + self.url = DataURL.from_data(self.mime_type, self.base64_encoded, self.data) + + self.assertEqual(type(self.url.data), bytes) + self.run_assertions() + + def test_string_with_plus_mimetype(self): + self.mime_type = "image/some+other" + self.base64_encoded = False + self.data = str(uuid.uuid4()) + self.raw_data = self.data + self.expected_url = f"data:{self.mime_type},{self.data}" + self.url = DataURL.from_data(self.mime_type, self.base64_encoded, self.data) + self.assertEqual(type(self.url.data), str) self.run_assertions() From 19ebf86276244f16ccfe71eb6560273dc6e99e7a Mon Sep 17 00:00:00 2001 From: telday Date: Mon, 19 May 2025 11:36:59 -0600 Subject: [PATCH 2/2] Bump project version --- CHANGELOG.md | 6 ++++++ VERSION | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64c77dc..e54560b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.2.1] - 2025-05-19 + +### Fixed + +- Allow for "." character to be used in [mime-types](https://github.com/telday/data_url/issues/8). + ## [1.2.0] - 2024-11-20 ### Fixed diff --git a/VERSION b/VERSION index 26aaba0..6085e94 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.0 +1.2.1